PR c++/91304 - prefix attributes ignored in condition.
[official-gcc.git] / gcc / cp / parser.c
blobb410a6c030f29fda4e672c4ec70a4c83bcbd5e9a
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2019 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 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
50 /* The lexer. */
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token =
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
203 /* Prototypes. */
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250 static tree cp_parser_noexcept_specification_opt
251 (cp_parser *, bool, bool *, bool, bool);
252 static tree cp_parser_late_noexcept_specifier
253 (cp_parser *, tree);
254 static void noexcept_override_late_checks
255 (tree, tree);
257 static void cp_parser_initial_pragma
258 (cp_token *);
260 static bool cp_parser_omp_declare_reduction_exprs
261 (tree, cp_parser *);
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 forbidden in current context",
546 (parser->local_variables_forbidden_p
547 & LOCAL_VARS_FORBIDDEN));
548 cp_debug_print_flag (file, "'this' forbidden in current context",
549 (parser->local_variables_forbidden_p
550 & THIS_FORBIDDEN));
551 cp_debug_print_flag (file, "In unbraced linkage specification",
552 parser->in_unbraced_linkage_specification_p);
553 cp_debug_print_flag (file, "Parsing a declarator",
554 parser->in_declarator_p);
555 cp_debug_print_flag (file, "In template argument list",
556 parser->in_template_argument_list_p);
557 cp_debug_print_flag (file, "Parsing an iteration statement",
558 parser->in_statement & IN_ITERATION_STMT);
559 cp_debug_print_flag (file, "Parsing a switch statement",
560 parser->in_statement & IN_SWITCH_STMT);
561 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
562 parser->in_statement & IN_OMP_BLOCK);
563 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564 parser->in_statement & IN_OMP_FOR);
565 cp_debug_print_flag (file, "Parsing an if statement",
566 parser->in_statement & IN_IF_STMT);
567 cp_debug_print_flag (file, "Parsing a type-id in an expression "
568 "context", parser->in_type_id_in_expr_p);
569 cp_debug_print_flag (file, "String expressions should be translated "
570 "to execution character set",
571 parser->translate_strings_p);
572 cp_debug_print_flag (file, "Parsing function body outside of a "
573 "local class", parser->in_function_body);
574 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
575 parser->colon_corrects_to_scope_p);
576 cp_debug_print_flag (file, "Colon doesn't start a class definition",
577 parser->colon_doesnt_start_class_def_p);
578 if (parser->type_definition_forbidden_message)
579 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
580 parser->type_definition_forbidden_message,
581 parser->type_definition_forbidden_message_arg
582 ? parser->type_definition_forbidden_message_arg : "<none>");
583 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584 fprintf (file, "Number of class definitions in progress: %u\n",
585 parser->num_classes_being_defined);
586 fprintf (file, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser->num_template_parameter_lists);
588 cp_debug_parser_tokens (file, parser, window_size);
589 token = parser->lexer->next_token;
590 fprintf (file, "Next token to parse:\n");
591 fprintf (file, "\tToken: ");
592 cp_lexer_print_token (file, token);
593 eloc = expand_location (token->location);
594 fprintf (file, "\n\tFile: %s\n", eloc.file);
595 fprintf (file, "\tLine: %d\n", eloc.line);
596 fprintf (file, "\tColumn: %d\n", eloc.column);
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
602 cp_debug_parser (stderr, &ref);
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
608 if (ptr)
609 debug (*ptr);
610 else
611 fprintf (stderr, "<nil>\n");
614 /* Allocate memory for a new lexer object and return it. */
616 static cp_lexer *
617 cp_lexer_alloc (void)
619 cp_lexer *lexer;
621 c_common_no_more_pch ();
623 /* Allocate the memory. */
624 lexer = ggc_cleared_alloc<cp_lexer> ();
626 /* Initially we are not debugging. */
627 lexer->debugging_p = false;
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
631 /* Create the buffer. */
632 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
634 return lexer;
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 preprocessor. */
641 static cp_lexer *
642 cp_lexer_new_main (void)
644 cp_lexer *lexer;
645 cp_token token;
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token);
652 lexer = cp_lexer_alloc ();
654 /* Put the first token in the buffer. */
655 lexer->buffer->quick_push (token);
657 /* Get the remaining tokens from the preprocessor. */
658 while (token.type != CPP_EOF)
660 cp_lexer_get_preprocessor_token (lexer, &token);
661 vec_safe_push (lexer->buffer, token);
664 lexer->last_token = lexer->buffer->address ()
665 + lexer->buffer->length ()
666 - 1;
667 lexer->next_token = lexer->buffer->length ()
668 ? lexer->buffer->address ()
669 : &eof_token;
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
673 done_lexing = true;
675 gcc_assert (!lexer->next_token->purged_p);
676 return lexer;
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
685 cp_token *first = cache->first;
686 cp_token *last = cache->last;
687 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
689 /* We do not own the buffer. */
690 lexer->buffer = NULL;
691 lexer->next_token = first == last ? &eof_token : first;
692 lexer->last_token = last;
694 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
696 /* Initially we are not debugging. */
697 lexer->debugging_p = false;
699 gcc_assert (!lexer->next_token->purged_p);
700 return lexer;
703 /* Frees all resources associated with LEXER. */
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
708 vec_free (lexer->buffer);
709 lexer->saved_tokens.release ();
710 ggc_free (lexer);
713 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
714 be used. The point of this flag is to help the compiler to fold away calls
715 to cp_lexer_debugging_p within this source file at compile time, when the
716 lexer is not being debugged. */
718 #define LEXER_DEBUGGING_ENABLED_P false
720 /* Returns nonzero if debugging information should be output. */
722 static inline bool
723 cp_lexer_debugging_p (cp_lexer *lexer)
725 if (!LEXER_DEBUGGING_ENABLED_P)
726 return false;
728 return lexer->debugging_p;
732 static inline cp_token_position
733 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
735 gcc_assert (!previous_p || lexer->next_token != &eof_token);
737 return lexer->next_token - previous_p;
740 static inline cp_token *
741 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
743 return pos;
746 static inline void
747 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
749 lexer->next_token = cp_lexer_token_at (lexer, pos);
752 static inline cp_token_position
753 cp_lexer_previous_token_position (cp_lexer *lexer)
755 if (lexer->next_token == &eof_token)
756 return lexer->last_token - 1;
757 else
758 return cp_lexer_token_position (lexer, true);
761 static inline cp_token *
762 cp_lexer_previous_token (cp_lexer *lexer)
764 cp_token_position tp = cp_lexer_previous_token_position (lexer);
766 /* Skip past purged tokens. */
767 while (tp->purged_p)
769 gcc_assert (tp != vec_safe_address (lexer->buffer));
770 tp--;
773 return cp_lexer_token_at (lexer, tp);
776 /* Overload for make_location, taking the lexer to mean the location of the
777 previous token. */
779 static inline location_t
780 make_location (location_t caret, location_t start, cp_lexer *lexer)
782 cp_token *t = cp_lexer_previous_token (lexer);
783 return make_location (caret, start, t->location);
786 /* nonzero if we are presently saving tokens. */
788 static inline int
789 cp_lexer_saving_tokens (const cp_lexer* lexer)
791 return lexer->saved_tokens.length () != 0;
794 /* Store the next token from the preprocessor in *TOKEN. Return true
795 if we reach EOF. If LEXER is NULL, assume we are handling an
796 initial #pragma pch_preprocess, and thus want the lexer to return
797 processed strings. */
799 static void
800 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
802 static int is_extern_c = 0;
804 /* Get a new token from the preprocessor. */
805 token->type
806 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
807 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
808 token->keyword = RID_MAX;
809 token->purged_p = false;
810 token->error_reported = false;
812 /* On some systems, some header files are surrounded by an
813 implicit extern "C" block. Set a flag in the token if it
814 comes from such a header. */
815 is_extern_c += pending_lang_change;
816 pending_lang_change = 0;
817 token->implicit_extern_c = is_extern_c > 0;
819 /* Check to see if this token is a keyword. */
820 if (token->type == CPP_NAME)
822 if (IDENTIFIER_KEYWORD_P (token->u.value))
824 /* Mark this token as a keyword. */
825 token->type = CPP_KEYWORD;
826 /* Record which keyword. */
827 token->keyword = C_RID_CODE (token->u.value);
829 else
831 if (warn_cxx11_compat
832 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
833 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
835 /* Warn about the C++0x keyword (but still treat it as
836 an identifier). */
837 warning (OPT_Wc__11_compat,
838 "identifier %qE is a keyword in C++11",
839 token->u.value);
841 /* Clear out the C_RID_CODE so we don't warn about this
842 particular identifier-turned-keyword again. */
843 C_SET_RID_CODE (token->u.value, RID_MAX);
846 token->keyword = RID_MAX;
849 else if (token->type == CPP_AT_NAME)
851 /* This only happens in Objective-C++; it must be a keyword. */
852 token->type = CPP_KEYWORD;
853 switch (C_RID_CODE (token->u.value))
855 /* Replace 'class' with '@class', 'private' with '@private',
856 etc. This prevents confusion with the C++ keyword
857 'class', and makes the tokens consistent with other
858 Objective-C 'AT' keywords. For example '@class' is
859 reported as RID_AT_CLASS which is consistent with
860 '@synchronized', which is reported as
861 RID_AT_SYNCHRONIZED.
863 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
864 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
865 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
866 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
867 case RID_THROW: token->keyword = RID_AT_THROW; break;
868 case RID_TRY: token->keyword = RID_AT_TRY; break;
869 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
870 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
871 default: token->keyword = C_RID_CODE (token->u.value);
876 /* Update the globals input_location and the input file stack from TOKEN. */
877 static inline void
878 cp_lexer_set_source_position_from_token (cp_token *token)
880 if (token->type != CPP_EOF)
882 input_location = token->location;
886 /* Update the globals input_location and the input file stack from LEXER. */
887 static inline void
888 cp_lexer_set_source_position (cp_lexer *lexer)
890 cp_token *token = cp_lexer_peek_token (lexer);
891 cp_lexer_set_source_position_from_token (token);
894 /* Return a pointer to the next token in the token stream, but do not
895 consume it. */
897 static inline cp_token *
898 cp_lexer_peek_token (cp_lexer *lexer)
900 if (cp_lexer_debugging_p (lexer))
902 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
903 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
904 putc ('\n', cp_lexer_debug_stream);
906 return lexer->next_token;
909 /* Return true if the next token has the indicated TYPE. */
911 static inline bool
912 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
914 return cp_lexer_peek_token (lexer)->type == type;
917 /* Return true if the next token does not have the indicated TYPE. */
919 static inline bool
920 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
922 return !cp_lexer_next_token_is (lexer, type);
925 /* Return true if the next token is the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword == keyword;
933 static inline bool
934 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
936 return cp_lexer_peek_nth_token (lexer, n)->type == type;
939 static inline bool
940 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
942 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
945 /* Return true if KEYWORD can start a decl-specifier. */
947 bool
948 cp_keyword_starts_decl_specifier_p (enum rid keyword)
950 switch (keyword)
952 /* auto specifier: storage-class-specifier in C++,
953 simple-type-specifier in C++0x. */
954 case RID_AUTO:
955 /* Storage classes. */
956 case RID_REGISTER:
957 case RID_STATIC:
958 case RID_EXTERN:
959 case RID_MUTABLE:
960 case RID_THREAD:
961 /* Elaborated type specifiers. */
962 case RID_ENUM:
963 case RID_CLASS:
964 case RID_STRUCT:
965 case RID_UNION:
966 case RID_TYPENAME:
967 /* Simple type specifiers. */
968 case RID_CHAR:
969 case RID_CHAR8:
970 case RID_CHAR16:
971 case RID_CHAR32:
972 case RID_WCHAR:
973 case RID_BOOL:
974 case RID_SHORT:
975 case RID_INT:
976 case RID_LONG:
977 case RID_SIGNED:
978 case RID_UNSIGNED:
979 case RID_FLOAT:
980 case RID_DOUBLE:
981 case RID_VOID:
982 /* GNU extensions. */
983 case RID_ATTRIBUTE:
984 case RID_TYPEOF:
985 /* C++0x extensions. */
986 case RID_DECLTYPE:
987 case RID_UNDERLYING_TYPE:
988 case RID_CONSTEXPR:
989 return true;
991 default:
992 if (keyword >= RID_FIRST_INT_N
993 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
994 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
995 return true;
996 return false;
1000 /* Return true if the next token is a keyword for a decl-specifier. */
1002 static bool
1003 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1005 cp_token *token;
1007 token = cp_lexer_peek_token (lexer);
1008 return cp_keyword_starts_decl_specifier_p (token->keyword);
1011 /* Returns TRUE iff the token T begins a decltype type. */
1013 static bool
1014 token_is_decltype (cp_token *t)
1016 return (t->keyword == RID_DECLTYPE
1017 || t->type == CPP_DECLTYPE);
1020 /* Returns TRUE iff the next token begins a decltype type. */
1022 static bool
1023 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1025 cp_token *t = cp_lexer_peek_token (lexer);
1026 return token_is_decltype (t);
1029 /* Called when processing a token with tree_check_value; perform or defer the
1030 associated checks and return the value. */
1032 static tree
1033 saved_checks_value (struct tree_check *check_value)
1035 /* Perform any access checks that were deferred. */
1036 vec<deferred_access_check, va_gc> *checks;
1037 deferred_access_check *chk;
1038 checks = check_value->checks;
1039 if (checks)
1041 int i;
1042 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1043 perform_or_defer_access_check (chk->binfo,
1044 chk->decl,
1045 chk->diag_decl, tf_warning_or_error);
1047 /* Return the stored value. */
1048 return check_value->value;
1051 /* Return a pointer to the Nth token in the token stream. If N is 1,
1052 then this is precisely equivalent to cp_lexer_peek_token (except
1053 that it is not inline). One would like to disallow that case, but
1054 there is one case (cp_parser_nth_token_starts_template_id) where
1055 the caller passes a variable for N and it might be 1. */
1057 static cp_token *
1058 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1060 cp_token *token;
1062 /* N is 1-based, not zero-based. */
1063 gcc_assert (n > 0);
1065 if (cp_lexer_debugging_p (lexer))
1066 fprintf (cp_lexer_debug_stream,
1067 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1069 --n;
1070 token = lexer->next_token;
1071 gcc_assert (!n || token != &eof_token);
1072 while (n != 0)
1074 ++token;
1075 if (token == lexer->last_token)
1077 token = &eof_token;
1078 break;
1081 if (!token->purged_p)
1082 --n;
1085 if (cp_lexer_debugging_p (lexer))
1087 cp_lexer_print_token (cp_lexer_debug_stream, token);
1088 putc ('\n', cp_lexer_debug_stream);
1091 return token;
1094 /* Return the next token, and advance the lexer's next_token pointer
1095 to point to the next non-purged token. */
1097 static cp_token *
1098 cp_lexer_consume_token (cp_lexer* lexer)
1100 cp_token *token = lexer->next_token;
1102 gcc_assert (token != &eof_token);
1103 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1107 lexer->next_token++;
1108 if (lexer->next_token == lexer->last_token)
1110 lexer->next_token = &eof_token;
1111 break;
1115 while (lexer->next_token->purged_p);
1117 cp_lexer_set_source_position_from_token (token);
1119 /* Provide debugging output. */
1120 if (cp_lexer_debugging_p (lexer))
1122 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1123 cp_lexer_print_token (cp_lexer_debug_stream, token);
1124 putc ('\n', cp_lexer_debug_stream);
1127 return token;
1130 /* Permanently remove the next token from the token stream, and
1131 advance the next_token pointer to refer to the next non-purged
1132 token. */
1134 static void
1135 cp_lexer_purge_token (cp_lexer *lexer)
1137 cp_token *tok = lexer->next_token;
1139 gcc_assert (tok != &eof_token);
1140 tok->purged_p = true;
1141 tok->location = UNKNOWN_LOCATION;
1142 tok->u.value = NULL_TREE;
1143 tok->keyword = RID_MAX;
1147 tok++;
1148 if (tok == lexer->last_token)
1150 tok = &eof_token;
1151 break;
1154 while (tok->purged_p);
1155 lexer->next_token = tok;
1158 /* Permanently remove all tokens after TOK, up to, but not
1159 including, the token that will be returned next by
1160 cp_lexer_peek_token. */
1162 static void
1163 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1165 cp_token *peek = lexer->next_token;
1167 if (peek == &eof_token)
1168 peek = lexer->last_token;
1170 gcc_assert (tok < peek);
1172 for ( tok += 1; tok != peek; tok += 1)
1174 tok->purged_p = true;
1175 tok->location = UNKNOWN_LOCATION;
1176 tok->u.value = NULL_TREE;
1177 tok->keyword = RID_MAX;
1181 /* Begin saving tokens. All tokens consumed after this point will be
1182 preserved. */
1184 static void
1185 cp_lexer_save_tokens (cp_lexer* lexer)
1187 /* Provide debugging output. */
1188 if (cp_lexer_debugging_p (lexer))
1189 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1191 lexer->saved_tokens.safe_push (lexer->next_token);
1194 /* Commit to the portion of the token stream most recently saved. */
1196 static void
1197 cp_lexer_commit_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1203 lexer->saved_tokens.pop ();
1206 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1207 to the token stream. Stop saving tokens. */
1209 static void
1210 cp_lexer_rollback_tokens (cp_lexer* lexer)
1212 /* Provide debugging output. */
1213 if (cp_lexer_debugging_p (lexer))
1214 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1216 lexer->next_token = lexer->saved_tokens.pop ();
1219 /* RAII wrapper around the above functions, with sanity checking. Creating
1220 a variable saves tokens, which are committed when the variable is
1221 destroyed unless they are explicitly rolled back by calling the rollback
1222 member function. */
1224 struct saved_token_sentinel
1226 cp_lexer *lexer;
1227 unsigned len;
1228 bool commit;
1229 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1231 len = lexer->saved_tokens.length ();
1232 cp_lexer_save_tokens (lexer);
1234 void rollback ()
1236 cp_lexer_rollback_tokens (lexer);
1237 commit = false;
1239 ~saved_token_sentinel()
1241 if (commit)
1242 cp_lexer_commit_tokens (lexer);
1243 gcc_assert (lexer->saved_tokens.length () == len);
1247 /* Print a representation of the TOKEN on the STREAM. */
1249 static void
1250 cp_lexer_print_token (FILE * stream, cp_token *token)
1252 /* We don't use cpp_type2name here because the parser defines
1253 a few tokens of its own. */
1254 static const char *const token_names[] = {
1255 /* cpplib-defined token types */
1256 #define OP(e, s) #e,
1257 #define TK(e, s) #e,
1258 TTYPE_TABLE
1259 #undef OP
1260 #undef TK
1261 /* C++ parser token types - see "Manifest constants", above. */
1262 "KEYWORD",
1263 "TEMPLATE_ID",
1264 "NESTED_NAME_SPECIFIER",
1267 /* For some tokens, print the associated data. */
1268 switch (token->type)
1270 case CPP_KEYWORD:
1271 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1272 For example, `struct' is mapped to an INTEGER_CST. */
1273 if (!identifier_p (token->u.value))
1274 break;
1275 /* fall through */
1276 case CPP_NAME:
1277 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1278 break;
1280 case CPP_STRING:
1281 case CPP_STRING16:
1282 case CPP_STRING32:
1283 case CPP_WSTRING:
1284 case CPP_UTF8STRING:
1285 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1286 break;
1288 case CPP_NUMBER:
1289 print_generic_expr (stream, token->u.value);
1290 break;
1292 default:
1293 /* If we have a name for the token, print it out. Otherwise, we
1294 simply give the numeric code. */
1295 if (token->type < ARRAY_SIZE(token_names))
1296 fputs (token_names[token->type], stream);
1297 else
1298 fprintf (stream, "[%d]", token->type);
1299 break;
1303 DEBUG_FUNCTION void
1304 debug (cp_token &ref)
1306 cp_lexer_print_token (stderr, &ref);
1307 fprintf (stderr, "\n");
1310 DEBUG_FUNCTION void
1311 debug (cp_token *ptr)
1313 if (ptr)
1314 debug (*ptr);
1315 else
1316 fprintf (stderr, "<nil>\n");
1320 /* Start emitting debugging information. */
1322 static void
1323 cp_lexer_start_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1329 lexer->debugging_p = true;
1330 cp_lexer_debug_stream = stderr;
1333 /* Stop emitting debugging information. */
1335 static void
1336 cp_lexer_stop_debugging (cp_lexer* lexer)
1338 if (!LEXER_DEBUGGING_ENABLED_P)
1339 fatal_error (input_location,
1340 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1342 lexer->debugging_p = false;
1343 cp_lexer_debug_stream = NULL;
1346 /* Create a new cp_token_cache, representing a range of tokens. */
1348 static cp_token_cache *
1349 cp_token_cache_new (cp_token *first, cp_token *last)
1351 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1352 cache->first = first;
1353 cache->last = last;
1354 return cache;
1357 /* Diagnose if #pragma omp declare simd isn't followed immediately
1358 by function declaration or definition. */
1360 static inline void
1361 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1363 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1365 error ("%<#pragma omp declare simd%> not immediately followed by "
1366 "function declaration or definition");
1367 parser->omp_declare_simd = NULL;
1371 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1372 and put that into "omp declare simd" attribute. */
1374 static inline void
1375 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1377 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1379 if (fndecl == error_mark_node)
1381 parser->omp_declare_simd = NULL;
1382 return;
1384 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1386 cp_ensure_no_omp_declare_simd (parser);
1387 return;
1392 /* Diagnose if #pragma acc routine isn't followed immediately by function
1393 declaration or definition. */
1395 static inline void
1396 cp_ensure_no_oacc_routine (cp_parser *parser)
1398 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1400 error_at (parser->oacc_routine->loc,
1401 "%<#pragma acc routine%> not immediately followed by "
1402 "function declaration or definition");
1403 parser->oacc_routine = NULL;
1407 /* Decl-specifiers. */
1409 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1411 static void
1412 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1414 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1417 /* Declarators. */
1419 /* Nothing other than the parser should be creating declarators;
1420 declarators are a semi-syntactic representation of C++ entities.
1421 Other parts of the front end that need to create entities (like
1422 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1424 static cp_declarator *make_call_declarator
1425 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1426 static cp_declarator *make_array_declarator
1427 (cp_declarator *, tree);
1428 static cp_declarator *make_pointer_declarator
1429 (cp_cv_quals, cp_declarator *, tree);
1430 static cp_declarator *make_reference_declarator
1431 (cp_cv_quals, cp_declarator *, bool, tree);
1432 static cp_declarator *make_ptrmem_declarator
1433 (cp_cv_quals, tree, cp_declarator *, tree);
1435 /* An erroneous declarator. */
1436 static cp_declarator *cp_error_declarator;
1438 /* The obstack on which declarators and related data structures are
1439 allocated. */
1440 static struct obstack declarator_obstack;
1442 /* Alloc BYTES from the declarator memory pool. */
1444 static inline void *
1445 alloc_declarator (size_t bytes)
1447 return obstack_alloc (&declarator_obstack, bytes);
1450 /* Allocate a declarator of the indicated KIND. Clear fields that are
1451 common to all declarators. */
1453 static cp_declarator *
1454 make_declarator (cp_declarator_kind kind)
1456 cp_declarator *declarator;
1458 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1459 declarator->kind = kind;
1460 declarator->parenthesized = UNKNOWN_LOCATION;
1461 declarator->attributes = NULL_TREE;
1462 declarator->std_attributes = NULL_TREE;
1463 declarator->declarator = NULL;
1464 declarator->parameter_pack_p = false;
1465 declarator->id_loc = UNKNOWN_LOCATION;
1467 return declarator;
1470 /* Make a declarator for a generalized identifier. If
1471 QUALIFYING_SCOPE is non-NULL, the identifier is
1472 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1473 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1474 is, if any. */
1476 static cp_declarator *
1477 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1478 special_function_kind sfk, location_t id_location)
1480 cp_declarator *declarator;
1482 /* It is valid to write:
1484 class C { void f(); };
1485 typedef C D;
1486 void D::f();
1488 The standard is not clear about whether `typedef const C D' is
1489 legal; as of 2002-09-15 the committee is considering that
1490 question. EDG 3.0 allows that syntax. Therefore, we do as
1491 well. */
1492 if (qualifying_scope && TYPE_P (qualifying_scope))
1493 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1495 gcc_assert (identifier_p (unqualified_name)
1496 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1497 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1499 declarator = make_declarator (cdk_id);
1500 declarator->u.id.qualifying_scope = qualifying_scope;
1501 declarator->u.id.unqualified_name = unqualified_name;
1502 declarator->u.id.sfk = sfk;
1503 declarator->id_loc = id_location;
1505 return declarator;
1508 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1509 of modifiers such as const or volatile to apply to the pointer
1510 type, represented as identifiers. ATTRIBUTES represent the attributes that
1511 appertain to the pointer or reference. */
1513 cp_declarator *
1514 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1515 tree attributes)
1517 cp_declarator *declarator;
1519 declarator = make_declarator (cdk_pointer);
1520 declarator->declarator = target;
1521 declarator->u.pointer.qualifiers = cv_qualifiers;
1522 declarator->u.pointer.class_type = NULL_TREE;
1523 if (target)
1525 declarator->id_loc = target->id_loc;
1526 declarator->parameter_pack_p = target->parameter_pack_p;
1527 target->parameter_pack_p = false;
1529 else
1530 declarator->parameter_pack_p = false;
1532 declarator->std_attributes = attributes;
1534 return declarator;
1537 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1538 represent the attributes that appertain to the pointer or
1539 reference. */
1541 cp_declarator *
1542 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1543 bool rvalue_ref, tree attributes)
1545 cp_declarator *declarator;
1547 declarator = make_declarator (cdk_reference);
1548 declarator->declarator = target;
1549 declarator->u.reference.qualifiers = cv_qualifiers;
1550 declarator->u.reference.rvalue_ref = rvalue_ref;
1551 if (target)
1553 declarator->id_loc = target->id_loc;
1554 declarator->parameter_pack_p = target->parameter_pack_p;
1555 target->parameter_pack_p = false;
1557 else
1558 declarator->parameter_pack_p = false;
1560 declarator->std_attributes = attributes;
1562 return declarator;
1565 /* Like make_pointer_declarator -- but for a pointer to a non-static
1566 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1567 appertain to the pointer or reference. */
1569 cp_declarator *
1570 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1571 cp_declarator *pointee,
1572 tree attributes)
1574 cp_declarator *declarator;
1576 declarator = make_declarator (cdk_ptrmem);
1577 declarator->declarator = pointee;
1578 declarator->u.pointer.qualifiers = cv_qualifiers;
1579 declarator->u.pointer.class_type = class_type;
1581 if (pointee)
1583 declarator->parameter_pack_p = pointee->parameter_pack_p;
1584 pointee->parameter_pack_p = false;
1586 else
1587 declarator->parameter_pack_p = false;
1589 declarator->std_attributes = attributes;
1591 return declarator;
1594 /* Make a declarator for the function given by TARGET, with the
1595 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1596 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1597 indicates what exceptions can be thrown. */
1599 cp_declarator *
1600 make_call_declarator (cp_declarator *target,
1601 tree parms,
1602 cp_cv_quals cv_qualifiers,
1603 cp_virt_specifiers virt_specifiers,
1604 cp_ref_qualifier ref_qualifier,
1605 tree tx_qualifier,
1606 tree exception_specification,
1607 tree late_return_type,
1608 tree requires_clause)
1610 cp_declarator *declarator;
1612 declarator = make_declarator (cdk_function);
1613 declarator->declarator = target;
1614 declarator->u.function.parameters = parms;
1615 declarator->u.function.qualifiers = cv_qualifiers;
1616 declarator->u.function.virt_specifiers = virt_specifiers;
1617 declarator->u.function.ref_qualifier = ref_qualifier;
1618 declarator->u.function.tx_qualifier = tx_qualifier;
1619 declarator->u.function.exception_specification = exception_specification;
1620 declarator->u.function.late_return_type = late_return_type;
1621 declarator->u.function.requires_clause = requires_clause;
1622 if (target)
1624 declarator->id_loc = target->id_loc;
1625 declarator->parameter_pack_p = target->parameter_pack_p;
1626 target->parameter_pack_p = false;
1628 else
1629 declarator->parameter_pack_p = false;
1631 return declarator;
1634 /* Make a declarator for an array of BOUNDS elements, each of which is
1635 defined by ELEMENT. */
1637 cp_declarator *
1638 make_array_declarator (cp_declarator *element, tree bounds)
1640 cp_declarator *declarator;
1642 declarator = make_declarator (cdk_array);
1643 declarator->declarator = element;
1644 declarator->u.array.bounds = bounds;
1645 if (element)
1647 declarator->id_loc = element->id_loc;
1648 declarator->parameter_pack_p = element->parameter_pack_p;
1649 element->parameter_pack_p = false;
1651 else
1652 declarator->parameter_pack_p = false;
1654 return declarator;
1657 /* Determine whether the declarator we've seen so far can be a
1658 parameter pack, when followed by an ellipsis. */
1659 static bool
1660 declarator_can_be_parameter_pack (cp_declarator *declarator)
1662 if (declarator && declarator->parameter_pack_p)
1663 /* We already saw an ellipsis. */
1664 return false;
1666 /* Search for a declarator name, or any other declarator that goes
1667 after the point where the ellipsis could appear in a parameter
1668 pack. If we find any of these, then this declarator cannot be
1669 made into a parameter pack. */
1670 bool found = false;
1671 while (declarator && !found)
1673 switch ((int)declarator->kind)
1675 case cdk_id:
1676 case cdk_array:
1677 case cdk_decomp:
1678 found = true;
1679 break;
1681 case cdk_error:
1682 return true;
1684 default:
1685 declarator = declarator->declarator;
1686 break;
1690 return !found;
1693 cp_parameter_declarator *no_parameters;
1695 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1696 DECLARATOR and DEFAULT_ARGUMENT. */
1698 cp_parameter_declarator *
1699 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1700 cp_declarator *declarator,
1701 tree default_argument,
1702 location_t loc,
1703 bool template_parameter_pack_p = false)
1705 cp_parameter_declarator *parameter;
1707 parameter = ((cp_parameter_declarator *)
1708 alloc_declarator (sizeof (cp_parameter_declarator)));
1709 parameter->next = NULL;
1710 if (decl_specifiers)
1711 parameter->decl_specifiers = *decl_specifiers;
1712 else
1713 clear_decl_specs (&parameter->decl_specifiers);
1714 parameter->declarator = declarator;
1715 parameter->default_argument = default_argument;
1716 parameter->template_parameter_pack_p = template_parameter_pack_p;
1717 parameter->loc = loc;
1719 return parameter;
1722 /* Returns true iff DECLARATOR is a declaration for a function. */
1724 static bool
1725 function_declarator_p (const cp_declarator *declarator)
1727 while (declarator)
1729 if (declarator->kind == cdk_function
1730 && declarator->declarator->kind == cdk_id)
1731 return true;
1732 if (declarator->kind == cdk_id
1733 || declarator->kind == cdk_decomp
1734 || declarator->kind == cdk_error)
1735 return false;
1736 declarator = declarator->declarator;
1738 return false;
1741 /* The parser. */
1743 /* Overview
1744 --------
1746 A cp_parser parses the token stream as specified by the C++
1747 grammar. Its job is purely parsing, not semantic analysis. For
1748 example, the parser breaks the token stream into declarators,
1749 expressions, statements, and other similar syntactic constructs.
1750 It does not check that the types of the expressions on either side
1751 of an assignment-statement are compatible, or that a function is
1752 not declared with a parameter of type `void'.
1754 The parser invokes routines elsewhere in the compiler to perform
1755 semantic analysis and to build up the abstract syntax tree for the
1756 code processed.
1758 The parser (and the template instantiation code, which is, in a
1759 way, a close relative of parsing) are the only parts of the
1760 compiler that should be calling push_scope and pop_scope, or
1761 related functions. The parser (and template instantiation code)
1762 keeps track of what scope is presently active; everything else
1763 should simply honor that. (The code that generates static
1764 initializers may also need to set the scope, in order to check
1765 access control correctly when emitting the initializers.)
1767 Methodology
1768 -----------
1770 The parser is of the standard recursive-descent variety. Upcoming
1771 tokens in the token stream are examined in order to determine which
1772 production to use when parsing a non-terminal. Some C++ constructs
1773 require arbitrary look ahead to disambiguate. For example, it is
1774 impossible, in the general case, to tell whether a statement is an
1775 expression or declaration without scanning the entire statement.
1776 Therefore, the parser is capable of "parsing tentatively." When the
1777 parser is not sure what construct comes next, it enters this mode.
1778 Then, while we attempt to parse the construct, the parser queues up
1779 error messages, rather than issuing them immediately, and saves the
1780 tokens it consumes. If the construct is parsed successfully, the
1781 parser "commits", i.e., it issues any queued error messages and
1782 the tokens that were being preserved are permanently discarded.
1783 If, however, the construct is not parsed successfully, the parser
1784 rolls back its state completely so that it can resume parsing using
1785 a different alternative.
1787 Future Improvements
1788 -------------------
1790 The performance of the parser could probably be improved substantially.
1791 We could often eliminate the need to parse tentatively by looking ahead
1792 a little bit. In some places, this approach might not entirely eliminate
1793 the need to parse tentatively, but it might still speed up the average
1794 case. */
1796 /* Flags that are passed to some parsing functions. These values can
1797 be bitwise-ored together. */
1799 enum
1801 /* No flags. */
1802 CP_PARSER_FLAGS_NONE = 0x0,
1803 /* The construct is optional. If it is not present, then no error
1804 should be issued. */
1805 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1806 /* When parsing a type-specifier, treat user-defined type-names
1807 as non-type identifiers. */
1808 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1809 /* When parsing a type-specifier, do not try to parse a class-specifier
1810 or enum-specifier. */
1811 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1812 /* When parsing a decl-specifier-seq, only allow type-specifier or
1813 constexpr. */
1814 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1815 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1816 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1817 /* When parsing a decl-specifier-seq, allow missing typename. */
1818 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1821 /* This type is used for parameters and variables which hold
1822 combinations of the above flags. */
1823 typedef int cp_parser_flags;
1825 /* The different kinds of declarators we want to parse. */
1827 enum cp_parser_declarator_kind
1829 /* We want an abstract declarator. */
1830 CP_PARSER_DECLARATOR_ABSTRACT,
1831 /* We want a named declarator. */
1832 CP_PARSER_DECLARATOR_NAMED,
1833 /* We don't mind, but the name must be an unqualified-id. */
1834 CP_PARSER_DECLARATOR_EITHER
1837 /* The precedence values used to parse binary expressions. The minimum value
1838 of PREC must be 1, because zero is reserved to quickly discriminate
1839 binary operators from other tokens. */
1841 enum cp_parser_prec
1843 PREC_NOT_OPERATOR,
1844 PREC_LOGICAL_OR_EXPRESSION,
1845 PREC_LOGICAL_AND_EXPRESSION,
1846 PREC_INCLUSIVE_OR_EXPRESSION,
1847 PREC_EXCLUSIVE_OR_EXPRESSION,
1848 PREC_AND_EXPRESSION,
1849 PREC_EQUALITY_EXPRESSION,
1850 PREC_RELATIONAL_EXPRESSION,
1851 PREC_SHIFT_EXPRESSION,
1852 PREC_ADDITIVE_EXPRESSION,
1853 PREC_MULTIPLICATIVE_EXPRESSION,
1854 PREC_PM_EXPRESSION,
1855 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1858 /* A mapping from a token type to a corresponding tree node type, with a
1859 precedence value. */
1861 struct cp_parser_binary_operations_map_node
1863 /* The token type. */
1864 enum cpp_ttype token_type;
1865 /* The corresponding tree code. */
1866 enum tree_code tree_type;
1867 /* The precedence of this operator. */
1868 enum cp_parser_prec prec;
1871 struct cp_parser_expression_stack_entry
1873 /* Left hand side of the binary operation we are currently
1874 parsing. */
1875 cp_expr lhs;
1876 /* Original tree code for left hand side, if it was a binary
1877 expression itself (used for -Wparentheses). */
1878 enum tree_code lhs_type;
1879 /* Tree code for the binary operation we are parsing. */
1880 enum tree_code tree_type;
1881 /* Precedence of the binary operation we are parsing. */
1882 enum cp_parser_prec prec;
1883 /* Location of the binary operation we are parsing. */
1884 location_t loc;
1887 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1888 entries because precedence levels on the stack are monotonically
1889 increasing. */
1890 typedef struct cp_parser_expression_stack_entry
1891 cp_parser_expression_stack[NUM_PREC_VALUES];
1893 /* Prototypes. */
1895 /* Constructors and destructors. */
1897 static cp_parser_context *cp_parser_context_new
1898 (cp_parser_context *);
1900 /* Class variables. */
1902 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1904 /* The operator-precedence table used by cp_parser_binary_expression.
1905 Transformed into an associative array (binops_by_token) by
1906 cp_parser_new. */
1908 static const cp_parser_binary_operations_map_node binops[] = {
1909 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1910 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1912 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1913 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1914 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1916 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1917 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1919 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1920 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1922 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1923 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1924 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1925 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1927 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1928 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1930 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1932 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1934 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1936 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1938 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1941 /* The same as binops, but initialized by cp_parser_new so that
1942 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1943 for speed. */
1944 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1946 /* Constructors and destructors. */
1948 /* Construct a new context. The context below this one on the stack
1949 is given by NEXT. */
1951 static cp_parser_context *
1952 cp_parser_context_new (cp_parser_context* next)
1954 cp_parser_context *context;
1956 /* Allocate the storage. */
1957 if (cp_parser_context_free_list != NULL)
1959 /* Pull the first entry from the free list. */
1960 context = cp_parser_context_free_list;
1961 cp_parser_context_free_list = context->next;
1962 memset (context, 0, sizeof (*context));
1964 else
1965 context = ggc_cleared_alloc<cp_parser_context> ();
1967 /* No errors have occurred yet in this context. */
1968 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1969 /* If this is not the bottommost context, copy information that we
1970 need from the previous context. */
1971 if (next)
1973 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1974 expression, then we are parsing one in this context, too. */
1975 context->object_type = next->object_type;
1976 /* Thread the stack. */
1977 context->next = next;
1980 return context;
1983 /* Managing the unparsed function queues. */
1985 #define unparsed_funs_with_default_args \
1986 parser->unparsed_queues->last ().funs_with_default_args
1987 #define unparsed_funs_with_definitions \
1988 parser->unparsed_queues->last ().funs_with_definitions
1989 #define unparsed_nsdmis \
1990 parser->unparsed_queues->last ().nsdmis
1991 #define unparsed_classes \
1992 parser->unparsed_queues->last ().classes
1993 #define unparsed_noexcepts \
1994 parser->unparsed_queues->last ().noexcepts
1996 static void
1997 push_unparsed_function_queues (cp_parser *parser)
1999 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL,
2000 NULL };
2001 vec_safe_push (parser->unparsed_queues, e);
2004 static void
2005 pop_unparsed_function_queues (cp_parser *parser)
2007 release_tree_vector (unparsed_funs_with_definitions);
2008 parser->unparsed_queues->pop ();
2011 /* Prototypes. */
2013 /* Constructors and destructors. */
2015 static cp_parser *cp_parser_new
2016 (void);
2018 /* Routines to parse various constructs.
2020 Those that return `tree' will return the error_mark_node (rather
2021 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2022 Sometimes, they will return an ordinary node if error-recovery was
2023 attempted, even though a parse error occurred. So, to check
2024 whether or not a parse error occurred, you should always use
2025 cp_parser_error_occurred. If the construct is optional (indicated
2026 either by an `_opt' in the name of the function that does the
2027 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2028 the construct is not present. */
2030 /* Lexical conventions [gram.lex] */
2032 static cp_expr cp_parser_identifier
2033 (cp_parser *);
2034 static cp_expr cp_parser_string_literal
2035 (cp_parser *, bool, bool, bool);
2036 static cp_expr cp_parser_userdef_char_literal
2037 (cp_parser *);
2038 static tree cp_parser_userdef_string_literal
2039 (tree);
2040 static cp_expr cp_parser_userdef_numeric_literal
2041 (cp_parser *);
2043 /* Basic concepts [gram.basic] */
2045 static void cp_parser_translation_unit (cp_parser *);
2047 /* Expressions [gram.expr] */
2049 static cp_expr cp_parser_primary_expression
2050 (cp_parser *, bool, bool, bool, cp_id_kind *);
2051 static cp_expr cp_parser_id_expression
2052 (cp_parser *, bool, bool, bool *, bool, bool);
2053 static cp_expr cp_parser_unqualified_id
2054 (cp_parser *, bool, bool, bool, bool);
2055 static tree cp_parser_nested_name_specifier_opt
2056 (cp_parser *, bool, bool, bool, bool, bool = false);
2057 static tree cp_parser_nested_name_specifier
2058 (cp_parser *, bool, bool, bool, bool);
2059 static tree cp_parser_qualifying_entity
2060 (cp_parser *, bool, bool, bool, bool, bool);
2061 static cp_expr cp_parser_postfix_expression
2062 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2063 static tree cp_parser_postfix_open_square_expression
2064 (cp_parser *, tree, bool, bool);
2065 static tree cp_parser_postfix_dot_deref_expression
2066 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2067 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2068 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2069 bool = false);
2070 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2071 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2072 static void cp_parser_pseudo_destructor_name
2073 (cp_parser *, tree, tree *, tree *);
2074 static cp_expr cp_parser_unary_expression
2075 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2076 static enum tree_code cp_parser_unary_operator
2077 (cp_token *);
2078 static tree cp_parser_has_attribute_expression
2079 (cp_parser *);
2080 static tree cp_parser_new_expression
2081 (cp_parser *);
2082 static vec<tree, va_gc> *cp_parser_new_placement
2083 (cp_parser *);
2084 static tree cp_parser_new_type_id
2085 (cp_parser *, tree *);
2086 static cp_declarator *cp_parser_new_declarator_opt
2087 (cp_parser *);
2088 static cp_declarator *cp_parser_direct_new_declarator
2089 (cp_parser *);
2090 static vec<tree, va_gc> *cp_parser_new_initializer
2091 (cp_parser *);
2092 static tree cp_parser_delete_expression
2093 (cp_parser *);
2094 static cp_expr cp_parser_cast_expression
2095 (cp_parser *, bool, bool, bool, cp_id_kind *);
2096 static cp_expr cp_parser_binary_expression
2097 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2098 static tree cp_parser_question_colon_clause
2099 (cp_parser *, cp_expr);
2100 static cp_expr cp_parser_assignment_expression
2101 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2102 static enum tree_code cp_parser_assignment_operator_opt
2103 (cp_parser *);
2104 static cp_expr cp_parser_expression
2105 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2106 static cp_expr cp_parser_constant_expression
2107 (cp_parser *, bool = false, bool * = NULL, bool = false);
2108 static cp_expr cp_parser_builtin_offsetof
2109 (cp_parser *);
2110 static cp_expr cp_parser_lambda_expression
2111 (cp_parser *);
2112 static void cp_parser_lambda_introducer
2113 (cp_parser *, tree);
2114 static bool cp_parser_lambda_declarator_opt
2115 (cp_parser *, tree);
2116 static void cp_parser_lambda_body
2117 (cp_parser *, tree);
2119 /* Statements [gram.stmt.stmt] */
2121 static void cp_parser_statement
2122 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2123 static void cp_parser_label_for_labeled_statement
2124 (cp_parser *, tree);
2125 static tree cp_parser_expression_statement
2126 (cp_parser *, tree);
2127 static tree cp_parser_compound_statement
2128 (cp_parser *, tree, int, bool);
2129 static void cp_parser_statement_seq_opt
2130 (cp_parser *, tree);
2131 static tree cp_parser_selection_statement
2132 (cp_parser *, bool *, vec<tree> *);
2133 static tree cp_parser_condition
2134 (cp_parser *);
2135 static tree cp_parser_iteration_statement
2136 (cp_parser *, bool *, bool, unsigned short);
2137 static bool cp_parser_init_statement
2138 (cp_parser *, tree *decl);
2139 static tree cp_parser_for
2140 (cp_parser *, bool, unsigned short);
2141 static tree cp_parser_c_for
2142 (cp_parser *, tree, tree, bool, unsigned short);
2143 static tree cp_parser_range_for
2144 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2145 static void do_range_for_auto_deduction
2146 (tree, tree);
2147 static tree cp_parser_perform_range_for_lookup
2148 (tree, tree *, tree *);
2149 static tree cp_parser_range_for_member_function
2150 (tree, tree);
2151 static tree cp_parser_jump_statement
2152 (cp_parser *);
2153 static void cp_parser_declaration_statement
2154 (cp_parser *);
2156 static tree cp_parser_implicitly_scoped_statement
2157 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2158 static void cp_parser_already_scoped_statement
2159 (cp_parser *, bool *, const token_indent_info &);
2161 /* Declarations [gram.dcl.dcl] */
2163 static void cp_parser_declaration_seq_opt
2164 (cp_parser *);
2165 static void cp_parser_declaration
2166 (cp_parser *);
2167 static void cp_parser_toplevel_declaration
2168 (cp_parser *);
2169 static void cp_parser_block_declaration
2170 (cp_parser *, bool);
2171 static void cp_parser_simple_declaration
2172 (cp_parser *, bool, tree *);
2173 static void cp_parser_decl_specifier_seq
2174 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2175 static tree cp_parser_storage_class_specifier_opt
2176 (cp_parser *);
2177 static tree cp_parser_function_specifier_opt
2178 (cp_parser *, cp_decl_specifier_seq *);
2179 static tree cp_parser_type_specifier
2180 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2181 int *, bool *);
2182 static tree cp_parser_simple_type_specifier
2183 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2184 static tree cp_parser_type_name
2185 (cp_parser *, bool);
2186 static tree cp_parser_nonclass_name
2187 (cp_parser* parser);
2188 static tree cp_parser_elaborated_type_specifier
2189 (cp_parser *, bool, bool);
2190 static tree cp_parser_enum_specifier
2191 (cp_parser *);
2192 static void cp_parser_enumerator_list
2193 (cp_parser *, tree);
2194 static void cp_parser_enumerator_definition
2195 (cp_parser *, tree);
2196 static tree cp_parser_namespace_name
2197 (cp_parser *);
2198 static void cp_parser_namespace_definition
2199 (cp_parser *);
2200 static void cp_parser_namespace_body
2201 (cp_parser *);
2202 static tree cp_parser_qualified_namespace_specifier
2203 (cp_parser *);
2204 static void cp_parser_namespace_alias_definition
2205 (cp_parser *);
2206 static bool cp_parser_using_declaration
2207 (cp_parser *, bool);
2208 static void cp_parser_using_directive
2209 (cp_parser *);
2210 static tree cp_parser_alias_declaration
2211 (cp_parser *);
2212 static void cp_parser_asm_definition
2213 (cp_parser *);
2214 static void cp_parser_linkage_specification
2215 (cp_parser *);
2216 static void cp_parser_static_assert
2217 (cp_parser *, bool);
2218 static tree cp_parser_decltype
2219 (cp_parser *);
2220 static tree cp_parser_decomposition_declaration
2221 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2223 /* Declarators [gram.dcl.decl] */
2225 static tree cp_parser_init_declarator
2226 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2227 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2228 location_t *, tree *);
2229 static cp_declarator *cp_parser_declarator
2230 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2231 bool, bool, bool);
2232 static cp_declarator *cp_parser_direct_declarator
2233 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2234 bool);
2235 static enum tree_code cp_parser_ptr_operator
2236 (cp_parser *, tree *, cp_cv_quals *, tree *);
2237 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2238 (cp_parser *);
2239 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2240 (cp_parser *);
2241 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2242 (cp_parser *);
2243 static tree cp_parser_tx_qualifier_opt
2244 (cp_parser *);
2245 static tree cp_parser_late_return_type_opt
2246 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2247 static tree cp_parser_declarator_id
2248 (cp_parser *, bool);
2249 static tree cp_parser_type_id
2250 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2251 static tree cp_parser_template_type_arg
2252 (cp_parser *);
2253 static tree cp_parser_trailing_type_id (cp_parser *);
2254 static tree cp_parser_type_id_1
2255 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2256 static void cp_parser_type_specifier_seq
2257 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2258 static tree cp_parser_parameter_declaration_clause
2259 (cp_parser *, cp_parser_flags);
2260 static tree cp_parser_parameter_declaration_list
2261 (cp_parser *, cp_parser_flags);
2262 static cp_parameter_declarator *cp_parser_parameter_declaration
2263 (cp_parser *, cp_parser_flags, bool, bool *);
2264 static tree cp_parser_default_argument
2265 (cp_parser *, bool);
2266 static void cp_parser_function_body
2267 (cp_parser *, bool);
2268 static tree cp_parser_initializer
2269 (cp_parser *, bool *, bool *, bool = false);
2270 static cp_expr cp_parser_initializer_clause
2271 (cp_parser *, bool *);
2272 static cp_expr cp_parser_braced_list
2273 (cp_parser*, bool*);
2274 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2275 (cp_parser *, bool *, bool *);
2277 static void cp_parser_ctor_initializer_opt_and_function_body
2278 (cp_parser *, bool);
2280 static tree cp_parser_late_parsing_omp_declare_simd
2281 (cp_parser *, tree);
2283 static tree cp_parser_late_parsing_oacc_routine
2284 (cp_parser *, tree);
2286 static tree synthesize_implicit_template_parm
2287 (cp_parser *, tree);
2288 static tree finish_fully_implicit_template
2289 (cp_parser *, tree);
2290 static void abort_fully_implicit_template
2291 (cp_parser *);
2293 /* Classes [gram.class] */
2295 static tree cp_parser_class_name
2296 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2297 static tree cp_parser_class_specifier
2298 (cp_parser *);
2299 static tree cp_parser_class_head
2300 (cp_parser *, bool *);
2301 static enum tag_types cp_parser_class_key
2302 (cp_parser *);
2303 static void cp_parser_type_parameter_key
2304 (cp_parser* parser);
2305 static void cp_parser_member_specification_opt
2306 (cp_parser *);
2307 static void cp_parser_member_declaration
2308 (cp_parser *);
2309 static tree cp_parser_pure_specifier
2310 (cp_parser *);
2311 static tree cp_parser_constant_initializer
2312 (cp_parser *);
2314 /* Derived classes [gram.class.derived] */
2316 static tree cp_parser_base_clause
2317 (cp_parser *);
2318 static tree cp_parser_base_specifier
2319 (cp_parser *);
2321 /* Special member functions [gram.special] */
2323 static tree cp_parser_conversion_function_id
2324 (cp_parser *);
2325 static tree cp_parser_conversion_type_id
2326 (cp_parser *);
2327 static cp_declarator *cp_parser_conversion_declarator_opt
2328 (cp_parser *);
2329 static void cp_parser_ctor_initializer_opt
2330 (cp_parser *);
2331 static void cp_parser_mem_initializer_list
2332 (cp_parser *);
2333 static tree cp_parser_mem_initializer
2334 (cp_parser *);
2335 static tree cp_parser_mem_initializer_id
2336 (cp_parser *);
2338 /* Overloading [gram.over] */
2340 static cp_expr cp_parser_operator_function_id
2341 (cp_parser *);
2342 static cp_expr cp_parser_operator
2343 (cp_parser *, location_t);
2345 /* Templates [gram.temp] */
2347 static void cp_parser_template_declaration
2348 (cp_parser *, bool);
2349 static tree cp_parser_template_parameter_list
2350 (cp_parser *);
2351 static tree cp_parser_template_parameter
2352 (cp_parser *, bool *, bool *);
2353 static tree cp_parser_type_parameter
2354 (cp_parser *, bool *);
2355 static tree cp_parser_template_id
2356 (cp_parser *, bool, bool, enum tag_types, bool);
2357 static tree cp_parser_template_name
2358 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2359 static tree cp_parser_template_argument_list
2360 (cp_parser *);
2361 static tree cp_parser_template_argument
2362 (cp_parser *);
2363 static void cp_parser_explicit_instantiation
2364 (cp_parser *);
2365 static void cp_parser_explicit_specialization
2366 (cp_parser *);
2368 /* Exception handling [gram.exception] */
2370 static tree cp_parser_try_block
2371 (cp_parser *);
2372 static void cp_parser_function_try_block
2373 (cp_parser *);
2374 static void cp_parser_handler_seq
2375 (cp_parser *);
2376 static void cp_parser_handler
2377 (cp_parser *);
2378 static tree cp_parser_exception_declaration
2379 (cp_parser *);
2380 static tree cp_parser_throw_expression
2381 (cp_parser *);
2382 static tree cp_parser_exception_specification_opt
2383 (cp_parser *, bool = false);
2384 static tree cp_parser_type_id_list
2385 (cp_parser *);
2387 /* GNU Extensions */
2389 static tree cp_parser_asm_specification_opt
2390 (cp_parser *);
2391 static tree cp_parser_asm_operand_list
2392 (cp_parser *);
2393 static tree cp_parser_asm_clobber_list
2394 (cp_parser *);
2395 static tree cp_parser_asm_label_list
2396 (cp_parser *);
2397 static bool cp_next_tokens_can_be_attribute_p
2398 (cp_parser *);
2399 static bool cp_next_tokens_can_be_gnu_attribute_p
2400 (cp_parser *);
2401 static bool cp_next_tokens_can_be_std_attribute_p
2402 (cp_parser *);
2403 static bool cp_nth_tokens_can_be_std_attribute_p
2404 (cp_parser *, size_t);
2405 static bool cp_nth_tokens_can_be_gnu_attribute_p
2406 (cp_parser *, size_t);
2407 static bool cp_nth_tokens_can_be_attribute_p
2408 (cp_parser *, size_t);
2409 static tree cp_parser_attributes_opt
2410 (cp_parser *);
2411 static tree cp_parser_gnu_attributes_opt
2412 (cp_parser *);
2413 static tree cp_parser_gnu_attribute_list
2414 (cp_parser *, bool = false);
2415 static tree cp_parser_std_attribute
2416 (cp_parser *, tree);
2417 static tree cp_parser_std_attribute_spec
2418 (cp_parser *);
2419 static tree cp_parser_std_attribute_spec_seq
2420 (cp_parser *);
2421 static size_t cp_parser_skip_attributes_opt
2422 (cp_parser *, size_t);
2423 static bool cp_parser_extension_opt
2424 (cp_parser *, int *);
2425 static void cp_parser_label_declaration
2426 (cp_parser *);
2428 /* Concept Extensions */
2430 static tree cp_parser_requires_clause
2431 (cp_parser *);
2432 static tree cp_parser_requires_clause_opt
2433 (cp_parser *);
2434 static tree cp_parser_requires_expression
2435 (cp_parser *);
2436 static tree cp_parser_requirement_parameter_list
2437 (cp_parser *);
2438 static tree cp_parser_requirement_body
2439 (cp_parser *);
2440 static tree cp_parser_requirement_list
2441 (cp_parser *);
2442 static tree cp_parser_requirement
2443 (cp_parser *);
2444 static tree cp_parser_simple_requirement
2445 (cp_parser *);
2446 static tree cp_parser_compound_requirement
2447 (cp_parser *);
2448 static tree cp_parser_type_requirement
2449 (cp_parser *);
2450 static tree cp_parser_nested_requirement
2451 (cp_parser *);
2453 /* Transactional Memory Extensions */
2455 static tree cp_parser_transaction
2456 (cp_parser *, cp_token *);
2457 static tree cp_parser_transaction_expression
2458 (cp_parser *, enum rid);
2459 static void cp_parser_function_transaction
2460 (cp_parser *, enum rid);
2461 static tree cp_parser_transaction_cancel
2462 (cp_parser *);
2464 enum pragma_context {
2465 pragma_external,
2466 pragma_member,
2467 pragma_objc_icode,
2468 pragma_stmt,
2469 pragma_compound
2471 static bool cp_parser_pragma
2472 (cp_parser *, enum pragma_context, bool *);
2474 /* Objective-C++ Productions */
2476 static tree cp_parser_objc_message_receiver
2477 (cp_parser *);
2478 static tree cp_parser_objc_message_args
2479 (cp_parser *);
2480 static tree cp_parser_objc_message_expression
2481 (cp_parser *);
2482 static cp_expr cp_parser_objc_encode_expression
2483 (cp_parser *);
2484 static tree cp_parser_objc_defs_expression
2485 (cp_parser *);
2486 static tree cp_parser_objc_protocol_expression
2487 (cp_parser *);
2488 static tree cp_parser_objc_selector_expression
2489 (cp_parser *);
2490 static cp_expr cp_parser_objc_expression
2491 (cp_parser *);
2492 static bool cp_parser_objc_selector_p
2493 (enum cpp_ttype);
2494 static tree cp_parser_objc_selector
2495 (cp_parser *);
2496 static tree cp_parser_objc_protocol_refs_opt
2497 (cp_parser *);
2498 static void cp_parser_objc_declaration
2499 (cp_parser *, tree);
2500 static tree cp_parser_objc_statement
2501 (cp_parser *);
2502 static bool cp_parser_objc_valid_prefix_attributes
2503 (cp_parser *, tree *);
2504 static void cp_parser_objc_at_property_declaration
2505 (cp_parser *) ;
2506 static void cp_parser_objc_at_synthesize_declaration
2507 (cp_parser *) ;
2508 static void cp_parser_objc_at_dynamic_declaration
2509 (cp_parser *) ;
2510 static tree cp_parser_objc_struct_declaration
2511 (cp_parser *) ;
2513 /* Utility Routines */
2515 static cp_expr cp_parser_lookup_name
2516 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2517 static tree cp_parser_lookup_name_simple
2518 (cp_parser *, tree, location_t);
2519 static tree cp_parser_maybe_treat_template_as_class
2520 (tree, bool);
2521 static bool cp_parser_check_declarator_template_parameters
2522 (cp_parser *, cp_declarator *, location_t);
2523 static bool cp_parser_check_template_parameters
2524 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2525 static cp_expr cp_parser_simple_cast_expression
2526 (cp_parser *);
2527 static tree cp_parser_global_scope_opt
2528 (cp_parser *, bool);
2529 static bool cp_parser_constructor_declarator_p
2530 (cp_parser *, cp_parser_flags, bool);
2531 static tree cp_parser_function_definition_from_specifiers_and_declarator
2532 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2533 static tree cp_parser_function_definition_after_declarator
2534 (cp_parser *, bool);
2535 static bool cp_parser_template_declaration_after_export
2536 (cp_parser *, bool);
2537 static void cp_parser_perform_template_parameter_access_checks
2538 (vec<deferred_access_check, va_gc> *);
2539 static tree cp_parser_single_declaration
2540 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2541 static cp_expr cp_parser_functional_cast
2542 (cp_parser *, tree);
2543 static tree cp_parser_save_member_function_body
2544 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2545 static tree cp_parser_save_nsdmi
2546 (cp_parser *);
2547 static tree cp_parser_enclosed_template_argument_list
2548 (cp_parser *);
2549 static void cp_parser_save_default_args
2550 (cp_parser *, tree);
2551 static void cp_parser_late_parsing_for_member
2552 (cp_parser *, tree);
2553 static tree cp_parser_late_parse_one_default_arg
2554 (cp_parser *, tree, tree, tree);
2555 static void cp_parser_late_parsing_nsdmi
2556 (cp_parser *, tree);
2557 static void cp_parser_late_parsing_default_args
2558 (cp_parser *, tree);
2559 static tree cp_parser_sizeof_operand
2560 (cp_parser *, enum rid);
2561 static cp_expr cp_parser_trait_expr
2562 (cp_parser *, enum rid);
2563 static bool cp_parser_declares_only_class_p
2564 (cp_parser *);
2565 static void cp_parser_set_storage_class
2566 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2567 static void cp_parser_set_decl_spec_type
2568 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2569 static void set_and_check_decl_spec_loc
2570 (cp_decl_specifier_seq *decl_specs,
2571 cp_decl_spec ds, cp_token *);
2572 static bool cp_parser_friend_p
2573 (const cp_decl_specifier_seq *);
2574 static void cp_parser_required_error
2575 (cp_parser *, required_token, bool, location_t);
2576 static cp_token *cp_parser_require
2577 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2578 static cp_token *cp_parser_require_keyword
2579 (cp_parser *, enum rid, required_token);
2580 static bool cp_parser_token_starts_function_definition_p
2581 (cp_token *);
2582 static bool cp_parser_next_token_starts_class_definition_p
2583 (cp_parser *);
2584 static bool cp_parser_next_token_ends_template_argument_p
2585 (cp_parser *);
2586 static bool cp_parser_nth_token_starts_template_argument_list_p
2587 (cp_parser *, size_t);
2588 static enum tag_types cp_parser_token_is_class_key
2589 (cp_token *);
2590 static enum tag_types cp_parser_token_is_type_parameter_key
2591 (cp_token *);
2592 static void cp_parser_check_class_key
2593 (enum tag_types, tree type);
2594 static void cp_parser_check_access_in_redeclaration
2595 (tree type, location_t location);
2596 static bool cp_parser_optional_template_keyword
2597 (cp_parser *);
2598 static void cp_parser_pre_parsed_nested_name_specifier
2599 (cp_parser *);
2600 static bool cp_parser_cache_group
2601 (cp_parser *, enum cpp_ttype, unsigned);
2602 static tree cp_parser_cache_defarg
2603 (cp_parser *parser, bool nsdmi);
2604 static void cp_parser_parse_tentatively
2605 (cp_parser *);
2606 static void cp_parser_commit_to_tentative_parse
2607 (cp_parser *);
2608 static void cp_parser_commit_to_topmost_tentative_parse
2609 (cp_parser *);
2610 static void cp_parser_abort_tentative_parse
2611 (cp_parser *);
2612 static bool cp_parser_parse_definitely
2613 (cp_parser *);
2614 static inline bool cp_parser_parsing_tentatively
2615 (cp_parser *);
2616 static bool cp_parser_uncommitted_to_tentative_parse_p
2617 (cp_parser *);
2618 static void cp_parser_error
2619 (cp_parser *, const char *);
2620 static void cp_parser_name_lookup_error
2621 (cp_parser *, tree, tree, name_lookup_error, location_t);
2622 static bool cp_parser_simulate_error
2623 (cp_parser *);
2624 static bool cp_parser_check_type_definition
2625 (cp_parser *);
2626 static void cp_parser_check_for_definition_in_return_type
2627 (cp_declarator *, tree, location_t type_location);
2628 static void cp_parser_check_for_invalid_template_id
2629 (cp_parser *, tree, enum tag_types, location_t location);
2630 static bool cp_parser_non_integral_constant_expression
2631 (cp_parser *, non_integral_constant);
2632 static void cp_parser_diagnose_invalid_type_name
2633 (cp_parser *, tree, location_t);
2634 static bool cp_parser_parse_and_diagnose_invalid_type_name
2635 (cp_parser *);
2636 static int cp_parser_skip_to_closing_parenthesis
2637 (cp_parser *, bool, bool, bool);
2638 static void cp_parser_skip_to_end_of_statement
2639 (cp_parser *);
2640 static void cp_parser_consume_semicolon_at_end_of_statement
2641 (cp_parser *);
2642 static void cp_parser_skip_to_end_of_block_or_statement
2643 (cp_parser *);
2644 static bool cp_parser_skip_to_closing_brace
2645 (cp_parser *);
2646 static void cp_parser_skip_to_end_of_template_parameter_list
2647 (cp_parser *);
2648 static void cp_parser_skip_to_pragma_eol
2649 (cp_parser*, cp_token *);
2650 static bool cp_parser_error_occurred
2651 (cp_parser *);
2652 static bool cp_parser_allow_gnu_extensions_p
2653 (cp_parser *);
2654 static bool cp_parser_is_pure_string_literal
2655 (cp_token *);
2656 static bool cp_parser_is_string_literal
2657 (cp_token *);
2658 static bool cp_parser_is_keyword
2659 (cp_token *, enum rid);
2660 static tree cp_parser_make_typename_type
2661 (cp_parser *, tree, location_t location);
2662 static cp_declarator * cp_parser_make_indirect_declarator
2663 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2664 static bool cp_parser_compound_literal_p
2665 (cp_parser *);
2666 static bool cp_parser_array_designator_p
2667 (cp_parser *);
2668 static bool cp_parser_init_statement_p
2669 (cp_parser *);
2670 static bool cp_parser_skip_to_closing_square_bracket
2671 (cp_parser *);
2673 /* Concept-related syntactic transformations */
2675 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2676 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2678 // -------------------------------------------------------------------------- //
2679 // Unevaluated Operand Guard
2681 // Implementation of an RAII helper for unevaluated operand parsing.
2682 cp_unevaluated::cp_unevaluated ()
2684 ++cp_unevaluated_operand;
2685 ++c_inhibit_evaluation_warnings;
2688 cp_unevaluated::~cp_unevaluated ()
2690 --c_inhibit_evaluation_warnings;
2691 --cp_unevaluated_operand;
2694 // -------------------------------------------------------------------------- //
2695 // Tentative Parsing
2697 /* Returns nonzero if we are parsing tentatively. */
2699 static inline bool
2700 cp_parser_parsing_tentatively (cp_parser* parser)
2702 return parser->context->next != NULL;
2705 /* Returns nonzero if TOKEN is a string literal. */
2707 static bool
2708 cp_parser_is_pure_string_literal (cp_token* token)
2710 return (token->type == CPP_STRING ||
2711 token->type == CPP_STRING16 ||
2712 token->type == CPP_STRING32 ||
2713 token->type == CPP_WSTRING ||
2714 token->type == CPP_UTF8STRING);
2717 /* Returns nonzero if TOKEN is a string literal
2718 of a user-defined string literal. */
2720 static bool
2721 cp_parser_is_string_literal (cp_token* token)
2723 return (cp_parser_is_pure_string_literal (token) ||
2724 token->type == CPP_STRING_USERDEF ||
2725 token->type == CPP_STRING16_USERDEF ||
2726 token->type == CPP_STRING32_USERDEF ||
2727 token->type == CPP_WSTRING_USERDEF ||
2728 token->type == CPP_UTF8STRING_USERDEF);
2731 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2733 static bool
2734 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2736 return token->keyword == keyword;
2739 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2740 PRAGMA_NONE. */
2742 static enum pragma_kind
2743 cp_parser_pragma_kind (cp_token *token)
2745 if (token->type != CPP_PRAGMA)
2746 return PRAGMA_NONE;
2747 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2748 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2751 /* Helper function for cp_parser_error.
2752 Having peeked a token of kind TOK1_KIND that might signify
2753 a conflict marker, peek successor tokens to determine
2754 if we actually do have a conflict marker.
2755 Specifically, we consider a run of 7 '<', '=' or '>' characters
2756 at the start of a line as a conflict marker.
2757 These come through the lexer as three pairs and a single,
2758 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2759 If it returns true, *OUT_LOC is written to with the location/range
2760 of the marker. */
2762 static bool
2763 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2764 location_t *out_loc)
2766 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2767 if (token2->type != tok1_kind)
2768 return false;
2769 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2770 if (token3->type != tok1_kind)
2771 return false;
2772 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2773 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2774 return false;
2776 /* It must be at the start of the line. */
2777 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2778 if (LOCATION_COLUMN (start_loc) != 1)
2779 return false;
2781 /* We have a conflict marker. Construct a location of the form:
2782 <<<<<<<
2783 ^~~~~~~
2784 with start == caret, finishing at the end of the marker. */
2785 location_t finish_loc = get_finish (token4->location);
2786 *out_loc = make_location (start_loc, start_loc, finish_loc);
2788 return true;
2791 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2792 RT_CLOSE_PAREN. */
2794 static const char *
2795 get_matching_symbol (required_token token_desc)
2797 switch (token_desc)
2799 default:
2800 gcc_unreachable ();
2801 return "";
2802 case RT_CLOSE_BRACE:
2803 return "{";
2804 case RT_CLOSE_PAREN:
2805 return "(";
2809 /* Attempt to convert TOKEN_DESC from a required_token to an
2810 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2812 static enum cpp_ttype
2813 get_required_cpp_ttype (required_token token_desc)
2815 switch (token_desc)
2817 case RT_SEMICOLON:
2818 return CPP_SEMICOLON;
2819 case RT_OPEN_PAREN:
2820 return CPP_OPEN_PAREN;
2821 case RT_CLOSE_BRACE:
2822 return CPP_CLOSE_BRACE;
2823 case RT_OPEN_BRACE:
2824 return CPP_OPEN_BRACE;
2825 case RT_CLOSE_SQUARE:
2826 return CPP_CLOSE_SQUARE;
2827 case RT_OPEN_SQUARE:
2828 return CPP_OPEN_SQUARE;
2829 case RT_COMMA:
2830 return CPP_COMMA;
2831 case RT_COLON:
2832 return CPP_COLON;
2833 case RT_CLOSE_PAREN:
2834 return CPP_CLOSE_PAREN;
2836 default:
2837 /* Use CPP_EOF as a "no completions possible" code. */
2838 return CPP_EOF;
2843 /* Subroutine of cp_parser_error and cp_parser_required_error.
2845 Issue a diagnostic of the form
2846 FILE:LINE: MESSAGE before TOKEN
2847 where TOKEN is the next token in the input stream. MESSAGE
2848 (specified by the caller) is usually of the form "expected
2849 OTHER-TOKEN".
2851 This bypasses the check for tentative passing, and potentially
2852 adds material needed by cp_parser_required_error.
2854 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2855 suggesting insertion of the missing token.
2857 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2858 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2859 location. */
2861 static void
2862 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2863 required_token missing_token_desc,
2864 location_t matching_location)
2866 cp_token *token = cp_lexer_peek_token (parser->lexer);
2867 /* This diagnostic makes more sense if it is tagged to the line
2868 of the token we just peeked at. */
2869 cp_lexer_set_source_position_from_token (token);
2871 if (token->type == CPP_PRAGMA)
2873 error_at (token->location,
2874 "%<#pragma%> is not allowed here");
2875 cp_parser_skip_to_pragma_eol (parser, token);
2876 return;
2879 /* If this is actually a conflict marker, report it as such. */
2880 if (token->type == CPP_LSHIFT
2881 || token->type == CPP_RSHIFT
2882 || token->type == CPP_EQ_EQ)
2884 location_t loc;
2885 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2887 error_at (loc, "version control conflict marker in file");
2888 expanded_location token_exploc = expand_location (token->location);
2889 /* Consume tokens until the end of the source line. */
2890 while (1)
2892 cp_lexer_consume_token (parser->lexer);
2893 cp_token *next = cp_lexer_peek_token (parser->lexer);
2894 if (next == NULL)
2895 break;
2896 expanded_location next_exploc = expand_location (next->location);
2897 if (next_exploc.file != token_exploc.file)
2898 break;
2899 if (next_exploc.line != token_exploc.line)
2900 break;
2902 return;
2906 gcc_rich_location richloc (input_location);
2908 bool added_matching_location = false;
2910 if (missing_token_desc != RT_NONE)
2912 /* Potentially supply a fix-it hint, suggesting to add the
2913 missing token immediately after the *previous* token.
2914 This may move the primary location within richloc. */
2915 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2916 location_t prev_token_loc
2917 = cp_lexer_previous_token (parser->lexer)->location;
2918 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2920 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2921 Attempt to consolidate diagnostics by printing it as a
2922 secondary range within the main diagnostic. */
2923 if (matching_location != UNKNOWN_LOCATION)
2924 added_matching_location
2925 = richloc.add_location_if_nearby (matching_location);
2928 /* Actually emit the error. */
2929 c_parse_error (gmsgid,
2930 /* Because c_parser_error does not understand
2931 CPP_KEYWORD, keywords are treated like
2932 identifiers. */
2933 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2934 token->u.value, token->flags, &richloc);
2936 if (missing_token_desc != RT_NONE)
2938 /* If we weren't able to consolidate matching_location, then
2939 print it as a secondary diagnostic. */
2940 if (matching_location != UNKNOWN_LOCATION
2941 && !added_matching_location)
2942 inform (matching_location, "to match this %qs",
2943 get_matching_symbol (missing_token_desc));
2947 /* If not parsing tentatively, issue a diagnostic of the form
2948 FILE:LINE: MESSAGE before TOKEN
2949 where TOKEN is the next token in the input stream. MESSAGE
2950 (specified by the caller) is usually of the form "expected
2951 OTHER-TOKEN". */
2953 static void
2954 cp_parser_error (cp_parser* parser, const char* gmsgid)
2956 if (!cp_parser_simulate_error (parser))
2957 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2960 /* Issue an error about name-lookup failing. NAME is the
2961 IDENTIFIER_NODE DECL is the result of
2962 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2963 the thing that we hoped to find. */
2965 static void
2966 cp_parser_name_lookup_error (cp_parser* parser,
2967 tree name,
2968 tree decl,
2969 name_lookup_error desired,
2970 location_t location)
2972 /* If name lookup completely failed, tell the user that NAME was not
2973 declared. */
2974 if (decl == error_mark_node)
2976 if (parser->scope && parser->scope != global_namespace)
2977 error_at (location, "%<%E::%E%> has not been declared",
2978 parser->scope, name);
2979 else if (parser->scope == global_namespace)
2980 error_at (location, "%<::%E%> has not been declared", name);
2981 else if (parser->object_scope
2982 && !CLASS_TYPE_P (parser->object_scope))
2983 error_at (location, "request for member %qE in non-class type %qT",
2984 name, parser->object_scope);
2985 else if (parser->object_scope)
2986 error_at (location, "%<%T::%E%> has not been declared",
2987 parser->object_scope, name);
2988 else
2989 error_at (location, "%qE has not been declared", name);
2991 else if (parser->scope && parser->scope != global_namespace)
2993 switch (desired)
2995 case NLE_TYPE:
2996 error_at (location, "%<%E::%E%> is not a type",
2997 parser->scope, name);
2998 break;
2999 case NLE_CXX98:
3000 error_at (location, "%<%E::%E%> is not a class or namespace",
3001 parser->scope, name);
3002 break;
3003 case NLE_NOT_CXX98:
3004 error_at (location,
3005 "%<%E::%E%> is not a class, namespace, or enumeration",
3006 parser->scope, name);
3007 break;
3008 default:
3009 gcc_unreachable ();
3013 else if (parser->scope == global_namespace)
3015 switch (desired)
3017 case NLE_TYPE:
3018 error_at (location, "%<::%E%> is not a type", name);
3019 break;
3020 case NLE_CXX98:
3021 error_at (location, "%<::%E%> is not a class or namespace", name);
3022 break;
3023 case NLE_NOT_CXX98:
3024 error_at (location,
3025 "%<::%E%> is not a class, namespace, or enumeration",
3026 name);
3027 break;
3028 default:
3029 gcc_unreachable ();
3032 else
3034 switch (desired)
3036 case NLE_TYPE:
3037 error_at (location, "%qE is not a type", name);
3038 break;
3039 case NLE_CXX98:
3040 error_at (location, "%qE is not a class or namespace", name);
3041 break;
3042 case NLE_NOT_CXX98:
3043 error_at (location,
3044 "%qE is not a class, namespace, or enumeration", name);
3045 break;
3046 default:
3047 gcc_unreachable ();
3052 /* If we are parsing tentatively, remember that an error has occurred
3053 during this tentative parse. Returns true if the error was
3054 simulated; false if a message should be issued by the caller. */
3056 static bool
3057 cp_parser_simulate_error (cp_parser* parser)
3059 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3061 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3062 return true;
3064 return false;
3067 /* This function is called when a type is defined. If type
3068 definitions are forbidden at this point, an error message is
3069 issued. */
3071 static bool
3072 cp_parser_check_type_definition (cp_parser* parser)
3074 /* If types are forbidden here, issue a message. */
3075 if (parser->type_definition_forbidden_message)
3077 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3078 or %qs in the message need to be interpreted. */
3079 error (parser->type_definition_forbidden_message,
3080 parser->type_definition_forbidden_message_arg);
3081 return false;
3083 return true;
3086 /* This function is called when the DECLARATOR is processed. The TYPE
3087 was a type defined in the decl-specifiers. If it is invalid to
3088 define a type in the decl-specifiers for DECLARATOR, an error is
3089 issued. TYPE_LOCATION is the location of TYPE and is used
3090 for error reporting. */
3092 static void
3093 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3094 tree type, location_t type_location)
3096 /* [dcl.fct] forbids type definitions in return types.
3097 Unfortunately, it's not easy to know whether or not we are
3098 processing a return type until after the fact. */
3099 while (declarator
3100 && (declarator->kind == cdk_pointer
3101 || declarator->kind == cdk_reference
3102 || declarator->kind == cdk_ptrmem))
3103 declarator = declarator->declarator;
3104 if (declarator
3105 && declarator->kind == cdk_function)
3107 error_at (type_location,
3108 "new types may not be defined in a return type");
3109 inform (type_location,
3110 "(perhaps a semicolon is missing after the definition of %qT)",
3111 type);
3115 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3116 "<" in any valid C++ program. If the next token is indeed "<",
3117 issue a message warning the user about what appears to be an
3118 invalid attempt to form a template-id. LOCATION is the location
3119 of the type-specifier (TYPE) */
3121 static void
3122 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3123 tree type,
3124 enum tag_types tag_type,
3125 location_t location)
3127 cp_token_position start = 0;
3129 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3131 if (TREE_CODE (type) == TYPE_DECL)
3132 type = TREE_TYPE (type);
3133 if (TYPE_P (type) && !template_placeholder_p (type))
3134 error_at (location, "%qT is not a template", type);
3135 else if (identifier_p (type))
3137 if (tag_type != none_type)
3138 error_at (location, "%qE is not a class template", type);
3139 else
3140 error_at (location, "%qE is not a template", type);
3142 else
3143 error_at (location, "invalid template-id");
3144 /* Remember the location of the invalid "<". */
3145 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3146 start = cp_lexer_token_position (parser->lexer, true);
3147 /* Consume the "<". */
3148 cp_lexer_consume_token (parser->lexer);
3149 /* Parse the template arguments. */
3150 cp_parser_enclosed_template_argument_list (parser);
3151 /* Permanently remove the invalid template arguments so that
3152 this error message is not issued again. */
3153 if (start)
3154 cp_lexer_purge_tokens_after (parser->lexer, start);
3158 /* If parsing an integral constant-expression, issue an error message
3159 about the fact that THING appeared and return true. Otherwise,
3160 return false. In either case, set
3161 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3163 static bool
3164 cp_parser_non_integral_constant_expression (cp_parser *parser,
3165 non_integral_constant thing)
3167 parser->non_integral_constant_expression_p = true;
3168 if (parser->integral_constant_expression_p)
3170 if (!parser->allow_non_integral_constant_expression_p)
3172 const char *msg = NULL;
3173 switch (thing)
3175 case NIC_FLOAT:
3176 pedwarn (input_location, OPT_Wpedantic,
3177 "ISO C++ forbids using a floating-point literal "
3178 "in a constant-expression");
3179 return true;
3180 case NIC_CAST:
3181 error ("a cast to a type other than an integral or "
3182 "enumeration type cannot appear in a "
3183 "constant-expression");
3184 return true;
3185 case NIC_TYPEID:
3186 error ("%<typeid%> operator "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_NCC:
3190 error ("non-constant compound literals "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_FUNC_CALL:
3194 error ("a function call "
3195 "cannot appear in a constant-expression");
3196 return true;
3197 case NIC_INC:
3198 error ("an increment "
3199 "cannot appear in a constant-expression");
3200 return true;
3201 case NIC_DEC:
3202 error ("an decrement "
3203 "cannot appear in a constant-expression");
3204 return true;
3205 case NIC_ARRAY_REF:
3206 error ("an array reference "
3207 "cannot appear in a constant-expression");
3208 return true;
3209 case NIC_ADDR_LABEL:
3210 error ("the address of a label "
3211 "cannot appear in a constant-expression");
3212 return true;
3213 case NIC_OVERLOADED:
3214 error ("calls to overloaded operators "
3215 "cannot appear in a constant-expression");
3216 return true;
3217 case NIC_ASSIGNMENT:
3218 error ("an assignment cannot appear in a constant-expression");
3219 return true;
3220 case NIC_COMMA:
3221 error ("a comma operator "
3222 "cannot appear in a constant-expression");
3223 return true;
3224 case NIC_CONSTRUCTOR:
3225 error ("a call to a constructor "
3226 "cannot appear in a constant-expression");
3227 return true;
3228 case NIC_TRANSACTION:
3229 error ("a transaction expression "
3230 "cannot appear in a constant-expression");
3231 return true;
3232 case NIC_THIS:
3233 msg = "this";
3234 break;
3235 case NIC_FUNC_NAME:
3236 msg = "__FUNCTION__";
3237 break;
3238 case NIC_PRETTY_FUNC:
3239 msg = "__PRETTY_FUNCTION__";
3240 break;
3241 case NIC_C99_FUNC:
3242 msg = "__func__";
3243 break;
3244 case NIC_VA_ARG:
3245 msg = "va_arg";
3246 break;
3247 case NIC_ARROW:
3248 msg = "->";
3249 break;
3250 case NIC_POINT:
3251 msg = ".";
3252 break;
3253 case NIC_STAR:
3254 msg = "*";
3255 break;
3256 case NIC_ADDR:
3257 msg = "&";
3258 break;
3259 case NIC_PREINCREMENT:
3260 msg = "++";
3261 break;
3262 case NIC_PREDECREMENT:
3263 msg = "--";
3264 break;
3265 case NIC_NEW:
3266 msg = "new";
3267 break;
3268 case NIC_DEL:
3269 msg = "delete";
3270 break;
3271 default:
3272 gcc_unreachable ();
3274 if (msg)
3275 error ("%qs cannot appear in a constant-expression", msg);
3276 return true;
3279 return false;
3282 /* Emit a diagnostic for an invalid type name. This function commits
3283 to the current active tentative parse, if any. (Otherwise, the
3284 problematic construct might be encountered again later, resulting
3285 in duplicate error messages.) LOCATION is the location of ID. */
3287 static void
3288 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3289 location_t location)
3291 tree decl, ambiguous_decls;
3292 cp_parser_commit_to_tentative_parse (parser);
3293 /* Try to lookup the identifier. */
3294 decl = cp_parser_lookup_name (parser, id, none_type,
3295 /*is_template=*/false,
3296 /*is_namespace=*/false,
3297 /*check_dependency=*/true,
3298 &ambiguous_decls, location);
3299 if (ambiguous_decls)
3300 /* If the lookup was ambiguous, an error will already have
3301 been issued. */
3302 return;
3303 /* If the lookup found a template-name, it means that the user forgot
3304 to specify an argument list. Emit a useful error message. */
3305 if (DECL_TYPE_TEMPLATE_P (decl))
3307 auto_diagnostic_group d;
3308 error_at (location,
3309 "invalid use of template-name %qE without an argument list",
3310 decl);
3311 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3312 inform (location, "class template argument deduction is only available "
3313 "with %<-std=c++17%> or %<-std=gnu++17%>");
3314 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3316 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3317 error_at (location, "invalid use of destructor %qD as a type", id);
3318 else if (TREE_CODE (decl) == TYPE_DECL)
3319 /* Something like 'unsigned A a;' */
3320 error_at (location, "invalid combination of multiple type-specifiers");
3321 else if (!parser->scope)
3323 /* Issue an error message. */
3324 auto_diagnostic_group d;
3325 name_hint hint;
3326 if (TREE_CODE (id) == IDENTIFIER_NODE)
3327 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3328 if (const char *suggestion = hint.suggestion ())
3330 gcc_rich_location richloc (location);
3331 richloc.add_fixit_replace (suggestion);
3332 error_at (&richloc,
3333 "%qE does not name a type; did you mean %qs?",
3334 id, suggestion);
3336 else
3337 error_at (location, "%qE does not name a type", id);
3338 /* If we're in a template class, it's possible that the user was
3339 referring to a type from a base class. For example:
3341 template <typename T> struct A { typedef T X; };
3342 template <typename T> struct B : public A<T> { X x; };
3344 The user should have said "typename A<T>::X". */
3345 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3346 inform (location, "C++11 %<constexpr%> only available with "
3347 "%<-std=c++11%> or %<-std=gnu++11%>");
3348 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3349 inform (location, "C++11 %<noexcept%> only available with "
3350 "%<-std=c++11%> or %<-std=gnu++11%>");
3351 else if (cxx_dialect < cxx11
3352 && TREE_CODE (id) == IDENTIFIER_NODE
3353 && id_equal (id, "thread_local"))
3354 inform (location, "C++11 %<thread_local%> only available with "
3355 "%<-std=c++11%> or %<-std=gnu++11%>");
3356 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3357 inform (location, "%<concept%> only available with %<-fconcepts%>");
3358 else if (processing_template_decl && current_class_type
3359 && TYPE_BINFO (current_class_type))
3361 tree b;
3363 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3365 b = TREE_CHAIN (b))
3367 tree base_type = BINFO_TYPE (b);
3368 if (CLASS_TYPE_P (base_type)
3369 && dependent_type_p (base_type))
3371 tree field;
3372 /* Go from a particular instantiation of the
3373 template (which will have an empty TYPE_FIELDs),
3374 to the main version. */
3375 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3376 for (field = TYPE_FIELDS (base_type);
3377 field;
3378 field = DECL_CHAIN (field))
3379 if (TREE_CODE (field) == TYPE_DECL
3380 && DECL_NAME (field) == id)
3382 inform (location,
3383 "(perhaps %<typename %T::%E%> was intended)",
3384 BINFO_TYPE (b), id);
3385 break;
3387 if (field)
3388 break;
3393 /* Here we diagnose qualified-ids where the scope is actually correct,
3394 but the identifier does not resolve to a valid type name. */
3395 else if (parser->scope != error_mark_node)
3397 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3399 auto_diagnostic_group d;
3400 name_hint hint;
3401 if (decl == error_mark_node)
3402 hint = suggest_alternative_in_explicit_scope (location, id,
3403 parser->scope);
3404 const char *suggestion = hint.suggestion ();
3405 gcc_rich_location richloc (location_of (id));
3406 if (suggestion)
3407 richloc.add_fixit_replace (suggestion);
3408 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410 if (suggestion)
3411 error_at (&richloc,
3412 "%qE in namespace %qE does not name a template"
3413 " type; did you mean %qs?",
3414 id, parser->scope, suggestion);
3415 else
3416 error_at (&richloc,
3417 "%qE in namespace %qE does not name a template type",
3418 id, parser->scope);
3420 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3422 if (suggestion)
3423 error_at (&richloc,
3424 "%qE in namespace %qE does not name a template"
3425 " type; did you mean %qs?",
3426 TREE_OPERAND (id, 0), parser->scope, suggestion);
3427 else
3428 error_at (&richloc,
3429 "%qE in namespace %qE does not name a template"
3430 " type",
3431 TREE_OPERAND (id, 0), parser->scope);
3433 else
3435 if (suggestion)
3436 error_at (&richloc,
3437 "%qE in namespace %qE does not name a type"
3438 "; did you mean %qs?",
3439 id, parser->scope, suggestion);
3440 else
3441 error_at (&richloc,
3442 "%qE in namespace %qE does not name a type",
3443 id, parser->scope);
3445 if (DECL_P (decl))
3446 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3448 else if (CLASS_TYPE_P (parser->scope)
3449 && constructor_name_p (id, parser->scope))
3451 /* A<T>::A<T>() */
3452 auto_diagnostic_group d;
3453 error_at (location, "%<%T::%E%> names the constructor, not"
3454 " the type", parser->scope, id);
3455 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3456 error_at (location, "and %qT has no template constructors",
3457 parser->scope);
3459 else if (TYPE_P (parser->scope)
3460 && dependent_scope_p (parser->scope))
3462 gcc_rich_location richloc (location);
3463 richloc.add_fixit_insert_before ("typename ");
3464 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3465 error_at (&richloc,
3466 "need %<typename%> before %<%T::%D::%E%> because "
3467 "%<%T::%D%> is a dependent scope",
3468 TYPE_CONTEXT (parser->scope),
3469 TYPENAME_TYPE_FULLNAME (parser->scope),
3471 TYPE_CONTEXT (parser->scope),
3472 TYPENAME_TYPE_FULLNAME (parser->scope));
3473 else
3474 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3475 "%qT is a dependent scope",
3476 parser->scope, id, parser->scope);
3478 else if (TYPE_P (parser->scope))
3480 auto_diagnostic_group d;
3481 if (!COMPLETE_TYPE_P (parser->scope))
3482 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3483 parser->scope);
3484 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3485 error_at (location_of (id),
3486 "%qE in %q#T does not name a template type",
3487 id, parser->scope);
3488 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3489 error_at (location_of (id),
3490 "%qE in %q#T does not name a template type",
3491 TREE_OPERAND (id, 0), parser->scope);
3492 else
3493 error_at (location_of (id),
3494 "%qE in %q#T does not name a type",
3495 id, parser->scope);
3496 if (DECL_P (decl))
3497 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3499 else
3500 gcc_unreachable ();
3504 /* Check for a common situation where a type-name should be present,
3505 but is not, and issue a sensible error message. Returns true if an
3506 invalid type-name was detected.
3508 The situation handled by this function are variable declarations of the
3509 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3510 Usually, `ID' should name a type, but if we got here it means that it
3511 does not. We try to emit the best possible error message depending on
3512 how exactly the id-expression looks like. */
3514 static bool
3515 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3517 tree id;
3518 cp_token *token = cp_lexer_peek_token (parser->lexer);
3520 /* Avoid duplicate error about ambiguous lookup. */
3521 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3523 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3524 if (next->type == CPP_NAME && next->error_reported)
3525 goto out;
3528 cp_parser_parse_tentatively (parser);
3529 id = cp_parser_id_expression (parser,
3530 /*template_keyword_p=*/false,
3531 /*check_dependency_p=*/true,
3532 /*template_p=*/NULL,
3533 /*declarator_p=*/false,
3534 /*optional_p=*/false);
3535 /* If the next token is a (, this is a function with no explicit return
3536 type, i.e. constructor, destructor or conversion op. */
3537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3538 || TREE_CODE (id) == TYPE_DECL)
3540 cp_parser_abort_tentative_parse (parser);
3541 return false;
3543 if (!cp_parser_parse_definitely (parser))
3544 return false;
3546 /* Emit a diagnostic for the invalid type. */
3547 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3548 out:
3549 /* If we aren't in the middle of a declarator (i.e. in a
3550 parameter-declaration-clause), skip to the end of the declaration;
3551 there's no point in trying to process it. */
3552 if (!parser->in_declarator_p)
3553 cp_parser_skip_to_end_of_block_or_statement (parser);
3554 return true;
3557 /* Consume tokens up to, and including, the next non-nested closing `)'.
3558 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3559 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3560 found an unnested token of that type. */
3562 static int
3563 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3564 bool recovering,
3565 cpp_ttype or_ttype,
3566 bool consume_paren)
3568 unsigned paren_depth = 0;
3569 unsigned brace_depth = 0;
3570 unsigned square_depth = 0;
3571 unsigned condop_depth = 0;
3573 if (recovering && or_ttype == CPP_EOF
3574 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3575 return 0;
3577 while (true)
3579 cp_token * token = cp_lexer_peek_token (parser->lexer);
3581 /* Have we found what we're looking for before the closing paren? */
3582 if (token->type == or_ttype && or_ttype != CPP_EOF
3583 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3584 return -1;
3586 switch (token->type)
3588 case CPP_PRAGMA_EOL:
3589 if (!parser->lexer->in_pragma)
3590 break;
3591 /* FALLTHRU */
3592 case CPP_EOF:
3593 /* If we've run out of tokens, then there is no closing `)'. */
3594 return 0;
3596 /* This is good for lambda expression capture-lists. */
3597 case CPP_OPEN_SQUARE:
3598 ++square_depth;
3599 break;
3600 case CPP_CLOSE_SQUARE:
3601 if (!square_depth--)
3602 return 0;
3603 break;
3605 case CPP_SEMICOLON:
3606 /* This matches the processing in skip_to_end_of_statement. */
3607 if (!brace_depth)
3608 return 0;
3609 break;
3611 case CPP_OPEN_BRACE:
3612 ++brace_depth;
3613 break;
3614 case CPP_CLOSE_BRACE:
3615 if (!brace_depth--)
3616 return 0;
3617 break;
3619 case CPP_OPEN_PAREN:
3620 if (!brace_depth)
3621 ++paren_depth;
3622 break;
3624 case CPP_CLOSE_PAREN:
3625 if (!brace_depth && !paren_depth--)
3627 if (consume_paren)
3628 cp_lexer_consume_token (parser->lexer);
3629 return 1;
3631 break;
3633 case CPP_QUERY:
3634 if (!brace_depth && !paren_depth && !square_depth)
3635 ++condop_depth;
3636 break;
3638 case CPP_COLON:
3639 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3640 condop_depth--;
3641 break;
3643 default:
3644 break;
3647 /* Consume the token. */
3648 cp_lexer_consume_token (parser->lexer);
3652 /* Consume tokens up to, and including, the next non-nested closing `)'.
3653 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3654 are doing error recovery. Returns -1 if OR_COMMA is true and we
3655 found an unnested token of that type. */
3657 static int
3658 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3659 bool recovering,
3660 bool or_comma,
3661 bool consume_paren)
3663 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3664 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3665 ttype, consume_paren);
3668 /* Consume tokens until we reach the end of the current statement.
3669 Normally, that will be just before consuming a `;'. However, if a
3670 non-nested `}' comes first, then we stop before consuming that. */
3672 static void
3673 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3675 unsigned nesting_depth = 0;
3677 /* Unwind generic function template scope if necessary. */
3678 if (parser->fully_implicit_function_template_p)
3679 abort_fully_implicit_template (parser);
3681 while (true)
3683 cp_token *token = cp_lexer_peek_token (parser->lexer);
3685 switch (token->type)
3687 case CPP_PRAGMA_EOL:
3688 if (!parser->lexer->in_pragma)
3689 break;
3690 /* FALLTHRU */
3691 case CPP_EOF:
3692 /* If we've run out of tokens, stop. */
3693 return;
3695 case CPP_SEMICOLON:
3696 /* If the next token is a `;', we have reached the end of the
3697 statement. */
3698 if (!nesting_depth)
3699 return;
3700 break;
3702 case CPP_CLOSE_BRACE:
3703 /* If this is a non-nested '}', stop before consuming it.
3704 That way, when confronted with something like:
3706 { 3 + }
3708 we stop before consuming the closing '}', even though we
3709 have not yet reached a `;'. */
3710 if (nesting_depth == 0)
3711 return;
3713 /* If it is the closing '}' for a block that we have
3714 scanned, stop -- but only after consuming the token.
3715 That way given:
3717 void f g () { ... }
3718 typedef int I;
3720 we will stop after the body of the erroneously declared
3721 function, but before consuming the following `typedef'
3722 declaration. */
3723 if (--nesting_depth == 0)
3725 cp_lexer_consume_token (parser->lexer);
3726 return;
3728 break;
3730 case CPP_OPEN_BRACE:
3731 ++nesting_depth;
3732 break;
3734 default:
3735 break;
3738 /* Consume the token. */
3739 cp_lexer_consume_token (parser->lexer);
3743 /* This function is called at the end of a statement or declaration.
3744 If the next token is a semicolon, it is consumed; otherwise, error
3745 recovery is attempted. */
3747 static void
3748 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3750 /* Look for the trailing `;'. */
3751 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3753 /* If there is additional (erroneous) input, skip to the end of
3754 the statement. */
3755 cp_parser_skip_to_end_of_statement (parser);
3756 /* If the next token is now a `;', consume it. */
3757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3758 cp_lexer_consume_token (parser->lexer);
3762 /* Skip tokens until we have consumed an entire block, or until we
3763 have consumed a non-nested `;'. */
3765 static void
3766 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3768 int nesting_depth = 0;
3770 /* Unwind generic function template scope if necessary. */
3771 if (parser->fully_implicit_function_template_p)
3772 abort_fully_implicit_template (parser);
3774 while (nesting_depth >= 0)
3776 cp_token *token = cp_lexer_peek_token (parser->lexer);
3778 switch (token->type)
3780 case CPP_PRAGMA_EOL:
3781 if (!parser->lexer->in_pragma)
3782 break;
3783 /* FALLTHRU */
3784 case CPP_EOF:
3785 /* If we've run out of tokens, stop. */
3786 return;
3788 case CPP_SEMICOLON:
3789 /* Stop if this is an unnested ';'. */
3790 if (!nesting_depth)
3791 nesting_depth = -1;
3792 break;
3794 case CPP_CLOSE_BRACE:
3795 /* Stop if this is an unnested '}', or closes the outermost
3796 nesting level. */
3797 nesting_depth--;
3798 if (nesting_depth < 0)
3799 return;
3800 if (!nesting_depth)
3801 nesting_depth = -1;
3802 break;
3804 case CPP_OPEN_BRACE:
3805 /* Nest. */
3806 nesting_depth++;
3807 break;
3809 default:
3810 break;
3813 /* Consume the token. */
3814 cp_lexer_consume_token (parser->lexer);
3818 /* Skip tokens until a non-nested closing curly brace is the next
3819 token, or there are no more tokens. Return true in the first case,
3820 false otherwise. */
3822 static bool
3823 cp_parser_skip_to_closing_brace (cp_parser *parser)
3825 unsigned nesting_depth = 0;
3827 while (true)
3829 cp_token *token = cp_lexer_peek_token (parser->lexer);
3831 switch (token->type)
3833 case CPP_PRAGMA_EOL:
3834 if (!parser->lexer->in_pragma)
3835 break;
3836 /* FALLTHRU */
3837 case CPP_EOF:
3838 /* If we've run out of tokens, stop. */
3839 return false;
3841 case CPP_CLOSE_BRACE:
3842 /* If the next token is a non-nested `}', then we have reached
3843 the end of the current block. */
3844 if (nesting_depth-- == 0)
3845 return true;
3846 break;
3848 case CPP_OPEN_BRACE:
3849 /* If it the next token is a `{', then we are entering a new
3850 block. Consume the entire block. */
3851 ++nesting_depth;
3852 break;
3854 default:
3855 break;
3858 /* Consume the token. */
3859 cp_lexer_consume_token (parser->lexer);
3863 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3864 parameter is the PRAGMA token, allowing us to purge the entire pragma
3865 sequence. */
3867 static void
3868 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3870 cp_token *token;
3872 parser->lexer->in_pragma = false;
3875 token = cp_lexer_consume_token (parser->lexer);
3876 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3878 /* Ensure that the pragma is not parsed again. */
3879 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3882 /* Require pragma end of line, resyncing with it as necessary. The
3883 arguments are as for cp_parser_skip_to_pragma_eol. */
3885 static void
3886 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3888 parser->lexer->in_pragma = false;
3889 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3893 /* This is a simple wrapper around make_typename_type. When the id is
3894 an unresolved identifier node, we can provide a superior diagnostic
3895 using cp_parser_diagnose_invalid_type_name. */
3897 static tree
3898 cp_parser_make_typename_type (cp_parser *parser, tree id,
3899 location_t id_location)
3901 tree result;
3902 if (identifier_p (id))
3904 result = make_typename_type (parser->scope, id, typename_type,
3905 /*complain=*/tf_none);
3906 if (result == error_mark_node)
3907 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3908 return result;
3910 return make_typename_type (parser->scope, id, typename_type, tf_error);
3913 /* This is a wrapper around the
3914 make_{pointer,ptrmem,reference}_declarator functions that decides
3915 which one to call based on the CODE and CLASS_TYPE arguments. The
3916 CODE argument should be one of the values returned by
3917 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3918 appertain to the pointer or reference. */
3920 static cp_declarator *
3921 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3922 cp_cv_quals cv_qualifiers,
3923 cp_declarator *target,
3924 tree attributes)
3926 if (code == ERROR_MARK || target == cp_error_declarator)
3927 return cp_error_declarator;
3929 if (code == INDIRECT_REF)
3930 if (class_type == NULL_TREE)
3931 return make_pointer_declarator (cv_qualifiers, target, attributes);
3932 else
3933 return make_ptrmem_declarator (cv_qualifiers, class_type,
3934 target, attributes);
3935 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3936 return make_reference_declarator (cv_qualifiers, target,
3937 false, attributes);
3938 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3939 return make_reference_declarator (cv_qualifiers, target,
3940 true, attributes);
3941 gcc_unreachable ();
3944 /* Create a new C++ parser. */
3946 static cp_parser *
3947 cp_parser_new (void)
3949 cp_parser *parser;
3950 cp_lexer *lexer;
3951 unsigned i;
3953 /* cp_lexer_new_main is called before doing GC allocation because
3954 cp_lexer_new_main might load a PCH file. */
3955 lexer = cp_lexer_new_main ();
3957 /* Initialize the binops_by_token so that we can get the tree
3958 directly from the token. */
3959 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3960 binops_by_token[binops[i].token_type] = binops[i];
3962 parser = ggc_cleared_alloc<cp_parser> ();
3963 parser->lexer = lexer;
3964 parser->context = cp_parser_context_new (NULL);
3966 /* For now, we always accept GNU extensions. */
3967 parser->allow_gnu_extensions_p = 1;
3969 /* The `>' token is a greater-than operator, not the end of a
3970 template-id. */
3971 parser->greater_than_is_operator_p = true;
3973 parser->default_arg_ok_p = true;
3975 /* We are not parsing a constant-expression. */
3976 parser->integral_constant_expression_p = false;
3977 parser->allow_non_integral_constant_expression_p = false;
3978 parser->non_integral_constant_expression_p = false;
3980 /* Local variable names are not forbidden. */
3981 parser->local_variables_forbidden_p = 0;
3983 /* We are not processing an `extern "C"' declaration. */
3984 parser->in_unbraced_linkage_specification_p = false;
3986 /* We are not processing a declarator. */
3987 parser->in_declarator_p = false;
3989 /* We are not processing a template-argument-list. */
3990 parser->in_template_argument_list_p = false;
3992 /* We are not in an iteration statement. */
3993 parser->in_statement = 0;
3995 /* We are not in a switch statement. */
3996 parser->in_switch_statement_p = false;
3998 /* We are not parsing a type-id inside an expression. */
3999 parser->in_type_id_in_expr_p = false;
4001 /* String literals should be translated to the execution character set. */
4002 parser->translate_strings_p = true;
4004 /* We are not parsing a function body. */
4005 parser->in_function_body = false;
4007 /* We can correct until told otherwise. */
4008 parser->colon_corrects_to_scope_p = true;
4010 /* The unparsed function queue is empty. */
4011 push_unparsed_function_queues (parser);
4013 /* There are no classes being defined. */
4014 parser->num_classes_being_defined = 0;
4016 /* No template parameters apply. */
4017 parser->num_template_parameter_lists = 0;
4019 /* Special parsing data structures. */
4020 parser->omp_declare_simd = NULL;
4021 parser->oacc_routine = NULL;
4023 /* Not declaring an implicit function template. */
4024 parser->auto_is_implicit_function_template_parm_p = false;
4025 parser->fully_implicit_function_template_p = false;
4026 parser->implicit_template_parms = 0;
4027 parser->implicit_template_scope = 0;
4029 /* Allow constrained-type-specifiers. */
4030 parser->prevent_constrained_type_specifiers = 0;
4032 /* We haven't yet seen an 'extern "C"'. */
4033 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4035 return parser;
4038 /* Create a cp_lexer structure which will emit the tokens in CACHE
4039 and push it onto the parser's lexer stack. This is used for delayed
4040 parsing of in-class method bodies and default arguments, and should
4041 not be confused with tentative parsing. */
4042 static void
4043 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4045 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4046 lexer->next = parser->lexer;
4047 parser->lexer = lexer;
4049 /* Move the current source position to that of the first token in the
4050 new lexer. */
4051 cp_lexer_set_source_position_from_token (lexer->next_token);
4054 /* Pop the top lexer off the parser stack. This is never used for the
4055 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4056 static void
4057 cp_parser_pop_lexer (cp_parser *parser)
4059 cp_lexer *lexer = parser->lexer;
4060 parser->lexer = lexer->next;
4061 cp_lexer_destroy (lexer);
4063 /* Put the current source position back where it was before this
4064 lexer was pushed. */
4065 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4068 /* Lexical conventions [gram.lex] */
4070 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4071 identifier. */
4073 static cp_expr
4074 cp_parser_identifier (cp_parser* parser)
4076 cp_token *token;
4078 /* Look for the identifier. */
4079 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4080 /* Return the value. */
4081 if (token)
4082 return cp_expr (token->u.value, token->location);
4083 else
4084 return error_mark_node;
4087 /* Parse a sequence of adjacent string constants. Returns a
4088 TREE_STRING representing the combined, nul-terminated string
4089 constant. If TRANSLATE is true, translate the string to the
4090 execution character set. If WIDE_OK is true, a wide string is
4091 invalid here.
4093 C++98 [lex.string] says that if a narrow string literal token is
4094 adjacent to a wide string literal token, the behavior is undefined.
4095 However, C99 6.4.5p4 says that this results in a wide string literal.
4096 We follow C99 here, for consistency with the C front end.
4098 This code is largely lifted from lex_string() in c-lex.c.
4100 FUTURE: ObjC++ will need to handle @-strings here. */
4101 static cp_expr
4102 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4103 bool lookup_udlit = true)
4105 tree value;
4106 size_t count;
4107 struct obstack str_ob;
4108 struct obstack loc_ob;
4109 cpp_string str, istr, *strs;
4110 cp_token *tok;
4111 enum cpp_ttype type, curr_type;
4112 int have_suffix_p = 0;
4113 tree string_tree;
4114 tree suffix_id = NULL_TREE;
4115 bool curr_tok_is_userdef_p = false;
4117 tok = cp_lexer_peek_token (parser->lexer);
4118 if (!cp_parser_is_string_literal (tok))
4120 cp_parser_error (parser, "expected string-literal");
4121 return error_mark_node;
4124 location_t loc = tok->location;
4126 if (cpp_userdef_string_p (tok->type))
4128 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4129 curr_type = cpp_userdef_string_remove_type (tok->type);
4130 curr_tok_is_userdef_p = true;
4132 else
4134 string_tree = tok->u.value;
4135 curr_type = tok->type;
4137 type = curr_type;
4139 /* Try to avoid the overhead of creating and destroying an obstack
4140 for the common case of just one string. */
4141 if (!cp_parser_is_string_literal
4142 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4144 cp_lexer_consume_token (parser->lexer);
4146 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4147 str.len = TREE_STRING_LENGTH (string_tree);
4148 count = 1;
4150 if (curr_tok_is_userdef_p)
4152 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4153 have_suffix_p = 1;
4154 curr_type = cpp_userdef_string_remove_type (tok->type);
4156 else
4157 curr_type = tok->type;
4159 strs = &str;
4161 else
4163 location_t last_tok_loc = tok->location;
4164 gcc_obstack_init (&str_ob);
4165 gcc_obstack_init (&loc_ob);
4166 count = 0;
4170 cp_lexer_consume_token (parser->lexer);
4171 count++;
4172 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4173 str.len = TREE_STRING_LENGTH (string_tree);
4175 if (curr_tok_is_userdef_p)
4177 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4178 if (have_suffix_p == 0)
4180 suffix_id = curr_suffix_id;
4181 have_suffix_p = 1;
4183 else if (have_suffix_p == 1
4184 && curr_suffix_id != suffix_id)
4186 error ("inconsistent user-defined literal suffixes"
4187 " %qD and %qD in string literal",
4188 suffix_id, curr_suffix_id);
4189 have_suffix_p = -1;
4191 curr_type = cpp_userdef_string_remove_type (tok->type);
4193 else
4194 curr_type = tok->type;
4196 if (type != curr_type)
4198 if (type == CPP_STRING)
4199 type = curr_type;
4200 else if (curr_type != CPP_STRING)
4202 rich_location rich_loc (line_table, tok->location);
4203 rich_loc.add_range (last_tok_loc);
4204 error_at (&rich_loc,
4205 "unsupported non-standard concatenation "
4206 "of string literals");
4210 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4211 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4213 last_tok_loc = tok->location;
4215 tok = cp_lexer_peek_token (parser->lexer);
4216 if (cpp_userdef_string_p (tok->type))
4218 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4219 curr_type = cpp_userdef_string_remove_type (tok->type);
4220 curr_tok_is_userdef_p = true;
4222 else
4224 string_tree = tok->u.value;
4225 curr_type = tok->type;
4226 curr_tok_is_userdef_p = false;
4229 while (cp_parser_is_string_literal (tok));
4231 /* A string literal built by concatenation has its caret=start at
4232 the start of the initial string, and its finish at the finish of
4233 the final string literal. */
4234 loc = make_location (loc, loc, get_finish (last_tok_loc));
4236 strs = (cpp_string *) obstack_finish (&str_ob);
4239 if (type != CPP_STRING && !wide_ok)
4241 cp_parser_error (parser, "a wide string is invalid in this context");
4242 type = CPP_STRING;
4245 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4246 (parse_in, strs, count, &istr, type))
4248 value = build_string (istr.len, (const char *)istr.text);
4249 free (CONST_CAST (unsigned char *, istr.text));
4250 if (count > 1)
4252 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4253 gcc_assert (g_string_concat_db);
4254 g_string_concat_db->record_string_concatenation (count, locs);
4257 switch (type)
4259 default:
4260 case CPP_STRING:
4261 TREE_TYPE (value) = char_array_type_node;
4262 break;
4263 case CPP_UTF8STRING:
4264 if (flag_char8_t)
4265 TREE_TYPE (value) = char8_array_type_node;
4266 else
4267 TREE_TYPE (value) = char_array_type_node;
4268 break;
4269 case CPP_STRING16:
4270 TREE_TYPE (value) = char16_array_type_node;
4271 break;
4272 case CPP_STRING32:
4273 TREE_TYPE (value) = char32_array_type_node;
4274 break;
4275 case CPP_WSTRING:
4276 TREE_TYPE (value) = wchar_array_type_node;
4277 break;
4280 value = fix_string_type (value);
4282 if (have_suffix_p)
4284 tree literal = build_userdef_literal (suffix_id, value,
4285 OT_NONE, NULL_TREE);
4286 if (lookup_udlit)
4287 value = cp_parser_userdef_string_literal (literal);
4288 else
4289 value = literal;
4292 else
4293 /* cpp_interpret_string has issued an error. */
4294 value = error_mark_node;
4296 if (count > 1)
4298 obstack_free (&str_ob, 0);
4299 obstack_free (&loc_ob, 0);
4302 return cp_expr (value, loc);
4305 /* Look up a literal operator with the name and the exact arguments. */
4307 static tree
4308 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4310 tree decl = lookup_name (name);
4311 if (!decl || !is_overloaded_fn (decl))
4312 return error_mark_node;
4314 for (lkp_iterator iter (decl); iter; ++iter)
4316 tree fn = *iter;
4318 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4320 unsigned int ix;
4321 bool found = true;
4323 for (ix = 0;
4324 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4325 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4327 tree tparm = TREE_VALUE (parmtypes);
4328 tree targ = TREE_TYPE ((*args)[ix]);
4329 bool ptr = TYPE_PTR_P (tparm);
4330 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4331 if ((ptr || arr || !same_type_p (tparm, targ))
4332 && (!ptr || !arr
4333 || !same_type_p (TREE_TYPE (tparm),
4334 TREE_TYPE (targ))))
4335 found = false;
4338 if (found
4339 && ix == vec_safe_length (args)
4340 /* May be this should be sufficient_parms_p instead,
4341 depending on how exactly should user-defined literals
4342 work in presence of default arguments on the literal
4343 operator parameters. */
4344 && parmtypes == void_list_node)
4345 return decl;
4349 return error_mark_node;
4352 /* Parse a user-defined char constant. Returns a call to a user-defined
4353 literal operator taking the character as an argument. */
4355 static cp_expr
4356 cp_parser_userdef_char_literal (cp_parser *parser)
4358 cp_token *token = cp_lexer_consume_token (parser->lexer);
4359 tree literal = token->u.value;
4360 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4361 tree value = USERDEF_LITERAL_VALUE (literal);
4362 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4363 tree decl, result;
4365 /* Build up a call to the user-defined operator */
4366 /* Lookup the name we got back from the id-expression. */
4367 releasing_vec args;
4368 vec_safe_push (args, value);
4369 decl = lookup_literal_operator (name, args);
4370 if (!decl || decl == error_mark_node)
4372 error ("unable to find character literal operator %qD with %qT argument",
4373 name, TREE_TYPE (value));
4374 return error_mark_node;
4376 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4377 return result;
4380 /* A subroutine of cp_parser_userdef_numeric_literal to
4381 create a char... template parameter pack from a string node. */
4383 static tree
4384 make_char_string_pack (tree value)
4386 tree charvec;
4387 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4388 const char *str = TREE_STRING_POINTER (value);
4389 int i, len = TREE_STRING_LENGTH (value) - 1;
4390 tree argvec = make_tree_vec (1);
4392 /* Fill in CHARVEC with all of the parameters. */
4393 charvec = make_tree_vec (len);
4394 for (i = 0; i < len; ++i)
4396 unsigned char s[3] = { '\'', str[i], '\'' };
4397 cpp_string in = { 3, s };
4398 cpp_string out = { 0, 0 };
4399 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4400 return NULL_TREE;
4401 gcc_assert (out.len == 2);
4402 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4403 out.text[0]);
4406 /* Build the argument packs. */
4407 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4409 TREE_VEC_ELT (argvec, 0) = argpack;
4411 return argvec;
4414 /* A subroutine of cp_parser_userdef_numeric_literal to
4415 create a char... template parameter pack from a string node. */
4417 static tree
4418 make_string_pack (tree value)
4420 tree charvec;
4421 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4422 const unsigned char *str
4423 = (const unsigned char *) TREE_STRING_POINTER (value);
4424 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4425 int len = TREE_STRING_LENGTH (value) / sz - 1;
4426 tree argvec = make_tree_vec (2);
4428 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4429 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4431 /* First template parm is character type. */
4432 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4434 /* Fill in CHARVEC with all of the parameters. */
4435 charvec = make_tree_vec (len);
4436 for (int i = 0; i < len; ++i)
4437 TREE_VEC_ELT (charvec, i)
4438 = double_int_to_tree (str_char_type_node,
4439 double_int::from_buffer (str + i * sz, sz));
4441 /* Build the argument packs. */
4442 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4444 TREE_VEC_ELT (argvec, 1) = argpack;
4446 return argvec;
4449 /* Parse a user-defined numeric constant. returns a call to a user-defined
4450 literal operator. */
4452 static cp_expr
4453 cp_parser_userdef_numeric_literal (cp_parser *parser)
4455 cp_token *token = cp_lexer_consume_token (parser->lexer);
4456 tree literal = token->u.value;
4457 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4458 tree value = USERDEF_LITERAL_VALUE (literal);
4459 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4460 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4461 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4462 tree decl, result;
4464 /* Look for a literal operator taking the exact type of numeric argument
4465 as the literal value. */
4466 releasing_vec args;
4467 vec_safe_push (args, value);
4468 decl = lookup_literal_operator (name, args);
4469 if (decl && decl != error_mark_node)
4471 result = finish_call_expr (decl, &args, false, true,
4472 tf_warning_or_error);
4474 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4476 warning_at (token->location, OPT_Woverflow,
4477 "integer literal exceeds range of %qT type",
4478 long_long_unsigned_type_node);
4480 else
4482 if (overflow > 0)
4483 warning_at (token->location, OPT_Woverflow,
4484 "floating literal exceeds range of %qT type",
4485 long_double_type_node);
4486 else if (overflow < 0)
4487 warning_at (token->location, OPT_Woverflow,
4488 "floating literal truncated to zero");
4491 return result;
4494 /* If the numeric argument didn't work, look for a raw literal
4495 operator taking a const char* argument consisting of the number
4496 in string format. */
4497 args->truncate (0);
4498 vec_safe_push (args, num_string);
4499 decl = lookup_literal_operator (name, args);
4500 if (decl && decl != error_mark_node)
4502 result = finish_call_expr (decl, &args, false, true,
4503 tf_warning_or_error);
4504 return result;
4507 /* If the raw literal didn't work, look for a non-type template
4508 function with parameter pack char.... Call the function with
4509 template parameter characters representing the number. */
4510 args->truncate (0);
4511 decl = lookup_literal_operator (name, args);
4512 if (decl && decl != error_mark_node)
4514 tree tmpl_args = make_char_string_pack (num_string);
4515 if (tmpl_args == NULL_TREE)
4517 error ("failed to translate literal to execution character set %qT",
4518 num_string);
4519 return error_mark_node;
4521 decl = lookup_template_function (decl, tmpl_args);
4522 result = finish_call_expr (decl, &args, false, true,
4523 tf_warning_or_error);
4524 return result;
4527 /* In C++14 the standard library defines complex number suffixes that
4528 conflict with GNU extensions. Prefer them if <complex> is #included. */
4529 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4530 bool i14 = (cxx_dialect > cxx11
4531 && (id_equal (suffix_id, "i")
4532 || id_equal (suffix_id, "if")
4533 || id_equal (suffix_id, "il")));
4534 diagnostic_t kind = DK_ERROR;
4535 int opt = 0;
4537 if (i14 && ext)
4539 tree cxlit = lookup_qualified_name (std_node,
4540 get_identifier ("complex_literals"),
4541 0, false, false);
4542 if (cxlit == error_mark_node)
4544 /* No <complex>, so pedwarn and use GNU semantics. */
4545 kind = DK_PEDWARN;
4546 opt = OPT_Wpedantic;
4550 bool complained
4551 = emit_diagnostic (kind, input_location, opt,
4552 "unable to find numeric literal operator %qD", name);
4554 if (!complained)
4555 /* Don't inform either. */;
4556 else if (i14)
4558 inform (token->location, "add %<using namespace std::complex_literals%> "
4559 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4560 "suffixes");
4561 if (ext)
4562 inform (token->location, "or use %<j%> instead of %<i%> for the "
4563 "GNU built-in suffix");
4565 else if (!ext)
4566 inform (token->location, "use %<-fext-numeric-literals%> "
4567 "to enable more built-in suffixes");
4569 if (kind == DK_ERROR)
4570 value = error_mark_node;
4571 else
4573 /* Use the built-in semantics. */
4574 tree type;
4575 if (id_equal (suffix_id, "i"))
4577 if (TREE_CODE (value) == INTEGER_CST)
4578 type = integer_type_node;
4579 else
4580 type = double_type_node;
4582 else if (id_equal (suffix_id, "if"))
4583 type = float_type_node;
4584 else /* if (id_equal (suffix_id, "il")) */
4585 type = long_double_type_node;
4587 value = build_complex (build_complex_type (type),
4588 fold_convert (type, integer_zero_node),
4589 fold_convert (type, value));
4592 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4593 /* Avoid repeated diagnostics. */
4594 token->u.value = value;
4595 return value;
4598 /* Parse a user-defined string constant. Returns a call to a user-defined
4599 literal operator taking a character pointer and the length of the string
4600 as arguments. */
4602 static tree
4603 cp_parser_userdef_string_literal (tree literal)
4605 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4606 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4607 tree value = USERDEF_LITERAL_VALUE (literal);
4608 int len = TREE_STRING_LENGTH (value)
4609 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4610 tree decl;
4612 /* Build up a call to the user-defined operator. */
4613 /* Lookup the name we got back from the id-expression. */
4614 releasing_vec args;
4615 vec_safe_push (args, value);
4616 vec_safe_push (args, build_int_cst (size_type_node, len));
4617 decl = lookup_literal_operator (name, args);
4619 if (decl && decl != error_mark_node)
4620 return finish_call_expr (decl, &args, false, true,
4621 tf_warning_or_error);
4623 /* Look for a suitable template function, either (C++20) with a single
4624 parameter of class type, or (N3599) with typename parameter CharT and
4625 parameter pack CharT... */
4626 args->truncate (0);
4627 decl = lookup_literal_operator (name, args);
4628 if (decl && decl != error_mark_node)
4630 /* Use resolve_nondeduced_context to try to choose one form of template
4631 or the other. */
4632 tree tmpl_args = make_tree_vec (1);
4633 TREE_VEC_ELT (tmpl_args, 0) = value;
4634 decl = lookup_template_function (decl, tmpl_args);
4635 tree res = resolve_nondeduced_context (decl, tf_none);
4636 if (DECL_P (res))
4637 decl = res;
4638 else
4640 TREE_OPERAND (decl, 1) = make_string_pack (value);
4641 res = resolve_nondeduced_context (decl, tf_none);
4642 if (DECL_P (res))
4643 decl = res;
4645 if (!DECL_P (decl) && cxx_dialect > cxx17)
4646 TREE_OPERAND (decl, 1) = tmpl_args;
4647 return finish_call_expr (decl, &args, false, true,
4648 tf_warning_or_error);
4651 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4652 name, TREE_TYPE (value), size_type_node);
4653 return error_mark_node;
4657 /* Basic concepts [gram.basic] */
4659 /* Parse a translation-unit.
4661 translation-unit:
4662 declaration-seq [opt] */
4664 static void
4665 cp_parser_translation_unit (cp_parser* parser)
4667 gcc_checking_assert (!cp_error_declarator);
4669 /* Create the declarator obstack. */
4670 gcc_obstack_init (&declarator_obstack);
4671 /* Create the error declarator. */
4672 cp_error_declarator = make_declarator (cdk_error);
4673 /* Create the empty parameter list. */
4674 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4675 UNKNOWN_LOCATION);
4676 /* Remember where the base of the declarator obstack lies. */
4677 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4679 bool implicit_extern_c = false;
4681 for (;;)
4683 cp_token *token = cp_lexer_peek_token (parser->lexer);
4685 /* If we're entering or exiting a region that's implicitly
4686 extern "C", modify the lang context appropriately. */
4687 if (implicit_extern_c
4688 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4690 implicit_extern_c = !implicit_extern_c;
4691 if (implicit_extern_c)
4692 push_lang_context (lang_name_c);
4693 else
4694 pop_lang_context ();
4697 if (token->type == CPP_EOF)
4698 break;
4700 if (token->type == CPP_CLOSE_BRACE)
4702 cp_parser_error (parser, "expected declaration");
4703 cp_lexer_consume_token (parser->lexer);
4704 /* If the next token is now a `;', consume it. */
4705 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4706 cp_lexer_consume_token (parser->lexer);
4708 else
4709 cp_parser_toplevel_declaration (parser);
4712 /* Get rid of the token array; we don't need it any more. */
4713 cp_lexer_destroy (parser->lexer);
4714 parser->lexer = NULL;
4716 /* The EOF should have reset this. */
4717 gcc_checking_assert (!implicit_extern_c);
4719 /* Make sure the declarator obstack was fully cleaned up. */
4720 gcc_assert (obstack_next_free (&declarator_obstack)
4721 == declarator_obstack_base);
4724 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4725 decltype context. */
4727 static inline tsubst_flags_t
4728 complain_flags (bool decltype_p)
4730 tsubst_flags_t complain = tf_warning_or_error;
4731 if (decltype_p)
4732 complain |= tf_decltype;
4733 return complain;
4736 /* We're about to parse a collection of statements. If we're currently
4737 parsing tentatively, set up a firewall so that any nested
4738 cp_parser_commit_to_tentative_parse won't affect the current context. */
4740 static cp_token_position
4741 cp_parser_start_tentative_firewall (cp_parser *parser)
4743 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4744 return 0;
4746 cp_parser_parse_tentatively (parser);
4747 cp_parser_commit_to_topmost_tentative_parse (parser);
4748 return cp_lexer_token_position (parser->lexer, false);
4751 /* We've finished parsing the collection of statements. Wrap up the
4752 firewall and replace the relevant tokens with the parsed form. */
4754 static void
4755 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4756 tree expr)
4758 if (!start)
4759 return;
4761 /* Finish the firewall level. */
4762 cp_parser_parse_definitely (parser);
4763 /* And remember the result of the parse for when we try again. */
4764 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4765 token->type = CPP_PREPARSED_EXPR;
4766 token->u.value = expr;
4767 token->keyword = RID_MAX;
4768 cp_lexer_purge_tokens_after (parser->lexer, start);
4771 /* Like the above functions, but let the user modify the tokens. Used by
4772 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4773 later parses, so it makes sense to localize the effects of
4774 cp_parser_commit_to_tentative_parse. */
4776 struct tentative_firewall
4778 cp_parser *parser;
4779 bool set;
4781 tentative_firewall (cp_parser *p): parser(p)
4783 /* If we're currently parsing tentatively, start a committed level as a
4784 firewall and then an inner tentative parse. */
4785 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4787 cp_parser_parse_tentatively (parser);
4788 cp_parser_commit_to_topmost_tentative_parse (parser);
4789 cp_parser_parse_tentatively (parser);
4793 ~tentative_firewall()
4795 if (set)
4797 /* Finish the inner tentative parse and the firewall, propagating any
4798 uncommitted error state to the outer tentative parse. */
4799 bool err = cp_parser_error_occurred (parser);
4800 cp_parser_parse_definitely (parser);
4801 cp_parser_parse_definitely (parser);
4802 if (err)
4803 cp_parser_simulate_error (parser);
4808 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4809 This class is for tracking such a matching pair of symbols.
4810 In particular, it tracks the location of the first token,
4811 so that if the second token is missing, we can highlight the
4812 location of the first token when notifying the user about the
4813 problem. */
4815 template <typename traits_t>
4816 class token_pair
4818 public:
4819 /* token_pair's ctor. */
4820 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4822 /* If the next token is the opening symbol for this pair, consume it and
4823 return true.
4824 Otherwise, issue an error and return false.
4825 In either case, record the location of the opening token. */
4827 bool require_open (cp_parser *parser)
4829 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4830 return cp_parser_require (parser, traits_t::open_token_type,
4831 traits_t::required_token_open);
4834 /* Consume the next token from PARSER, recording its location as
4835 that of the opening token within the pair. */
4837 cp_token * consume_open (cp_parser *parser)
4839 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4840 gcc_assert (tok->type == traits_t::open_token_type);
4841 m_open_loc = tok->location;
4842 return tok;
4845 /* If the next token is the closing symbol for this pair, consume it
4846 and return it.
4847 Otherwise, issue an error, highlighting the location of the
4848 corresponding opening token, and return NULL. */
4850 cp_token *require_close (cp_parser *parser) const
4852 return cp_parser_require (parser, traits_t::close_token_type,
4853 traits_t::required_token_close,
4854 m_open_loc);
4857 private:
4858 location_t m_open_loc;
4861 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4863 struct matching_paren_traits
4865 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4866 static const enum required_token required_token_open = RT_OPEN_PAREN;
4867 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4868 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4871 /* "matching_parens" is a token_pair<T> class for tracking matching
4872 pairs of parentheses. */
4874 typedef token_pair<matching_paren_traits> matching_parens;
4876 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4878 struct matching_brace_traits
4880 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4881 static const enum required_token required_token_open = RT_OPEN_BRACE;
4882 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4883 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4886 /* "matching_braces" is a token_pair<T> class for tracking matching
4887 pairs of braces. */
4889 typedef token_pair<matching_brace_traits> matching_braces;
4892 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4893 enclosing parentheses. */
4895 static cp_expr
4896 cp_parser_statement_expr (cp_parser *parser)
4898 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4900 /* Consume the '('. */
4901 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4902 matching_parens parens;
4903 parens.consume_open (parser);
4904 /* Start the statement-expression. */
4905 tree expr = begin_stmt_expr ();
4906 /* Parse the compound-statement. */
4907 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4908 /* Finish up. */
4909 expr = finish_stmt_expr (expr, false);
4910 /* Consume the ')'. */
4911 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4912 if (!parens.require_close (parser))
4913 cp_parser_skip_to_end_of_statement (parser);
4915 cp_parser_end_tentative_firewall (parser, start, expr);
4916 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4917 return cp_expr (expr, combined_loc);
4920 /* Expressions [gram.expr] */
4922 /* Parse a fold-operator.
4924 fold-operator:
4925 - * / % ^ & | = < > << >>
4926 = -= *= /= %= ^= &= |= <<= >>=
4927 == != <= >= && || , .* ->*
4929 This returns the tree code corresponding to the matched operator
4930 as an int. When the current token matches a compound assignment
4931 opertor, the resulting tree code is the negative value of the
4932 non-assignment operator. */
4934 static int
4935 cp_parser_fold_operator (cp_token *token)
4937 switch (token->type)
4939 case CPP_PLUS: return PLUS_EXPR;
4940 case CPP_MINUS: return MINUS_EXPR;
4941 case CPP_MULT: return MULT_EXPR;
4942 case CPP_DIV: return TRUNC_DIV_EXPR;
4943 case CPP_MOD: return TRUNC_MOD_EXPR;
4944 case CPP_XOR: return BIT_XOR_EXPR;
4945 case CPP_AND: return BIT_AND_EXPR;
4946 case CPP_OR: return BIT_IOR_EXPR;
4947 case CPP_LSHIFT: return LSHIFT_EXPR;
4948 case CPP_RSHIFT: return RSHIFT_EXPR;
4950 case CPP_EQ: return -NOP_EXPR;
4951 case CPP_PLUS_EQ: return -PLUS_EXPR;
4952 case CPP_MINUS_EQ: return -MINUS_EXPR;
4953 case CPP_MULT_EQ: return -MULT_EXPR;
4954 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4955 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4956 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4957 case CPP_AND_EQ: return -BIT_AND_EXPR;
4958 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4959 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4960 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4962 case CPP_EQ_EQ: return EQ_EXPR;
4963 case CPP_NOT_EQ: return NE_EXPR;
4964 case CPP_LESS: return LT_EXPR;
4965 case CPP_GREATER: return GT_EXPR;
4966 case CPP_LESS_EQ: return LE_EXPR;
4967 case CPP_GREATER_EQ: return GE_EXPR;
4969 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4970 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4972 case CPP_COMMA: return COMPOUND_EXPR;
4974 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4975 case CPP_DEREF_STAR: return MEMBER_REF;
4977 default: return ERROR_MARK;
4981 /* Returns true if CODE indicates a binary expression, which is not allowed in
4982 the LHS of a fold-expression. More codes will need to be added to use this
4983 function in other contexts. */
4985 static bool
4986 is_binary_op (tree_code code)
4988 switch (code)
4990 case PLUS_EXPR:
4991 case POINTER_PLUS_EXPR:
4992 case MINUS_EXPR:
4993 case MULT_EXPR:
4994 case TRUNC_DIV_EXPR:
4995 case TRUNC_MOD_EXPR:
4996 case BIT_XOR_EXPR:
4997 case BIT_AND_EXPR:
4998 case BIT_IOR_EXPR:
4999 case LSHIFT_EXPR:
5000 case RSHIFT_EXPR:
5002 case MODOP_EXPR:
5004 case EQ_EXPR:
5005 case NE_EXPR:
5006 case LE_EXPR:
5007 case GE_EXPR:
5008 case LT_EXPR:
5009 case GT_EXPR:
5011 case TRUTH_ANDIF_EXPR:
5012 case TRUTH_ORIF_EXPR:
5014 case COMPOUND_EXPR:
5016 case DOTSTAR_EXPR:
5017 case MEMBER_REF:
5018 return true;
5020 default:
5021 return false;
5025 /* If the next token is a suitable fold operator, consume it and return as
5026 the function above. */
5028 static int
5029 cp_parser_fold_operator (cp_parser *parser)
5031 cp_token* token = cp_lexer_peek_token (parser->lexer);
5032 int code = cp_parser_fold_operator (token);
5033 if (code != ERROR_MARK)
5034 cp_lexer_consume_token (parser->lexer);
5035 return code;
5038 /* Parse a fold-expression.
5040 fold-expression:
5041 ( ... folding-operator cast-expression)
5042 ( cast-expression folding-operator ... )
5043 ( cast-expression folding operator ... folding-operator cast-expression)
5045 Note that the '(' and ')' are matched in primary expression. */
5047 static cp_expr
5048 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5050 cp_id_kind pidk;
5052 // Left fold.
5053 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5055 cp_lexer_consume_token (parser->lexer);
5056 int op = cp_parser_fold_operator (parser);
5057 if (op == ERROR_MARK)
5059 cp_parser_error (parser, "expected binary operator");
5060 return error_mark_node;
5063 tree expr = cp_parser_cast_expression (parser, false, false,
5064 false, &pidk);
5065 if (expr == error_mark_node)
5066 return error_mark_node;
5067 return finish_left_unary_fold_expr (expr, op);
5070 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5071 int op = cp_parser_fold_operator (parser);
5072 if (op == ERROR_MARK)
5074 cp_parser_error (parser, "expected binary operator");
5075 return error_mark_node;
5078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5080 cp_parser_error (parser, "expected ...");
5081 return error_mark_node;
5083 cp_lexer_consume_token (parser->lexer);
5085 /* The operands of a fold-expression are cast-expressions, so binary or
5086 conditional expressions are not allowed. We check this here to avoid
5087 tentative parsing. */
5088 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5089 /* OK, the expression was parenthesized. */;
5090 else if (is_binary_op (TREE_CODE (expr1)))
5091 error_at (location_of (expr1),
5092 "binary expression in operand of fold-expression");
5093 else if (TREE_CODE (expr1) == COND_EXPR
5094 || (REFERENCE_REF_P (expr1)
5095 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5096 error_at (location_of (expr1),
5097 "conditional expression in operand of fold-expression");
5099 // Right fold.
5100 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5101 return finish_right_unary_fold_expr (expr1, op);
5103 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5105 cp_parser_error (parser, "mismatched operator in fold-expression");
5106 return error_mark_node;
5108 cp_lexer_consume_token (parser->lexer);
5110 // Binary left or right fold.
5111 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5112 if (expr2 == error_mark_node)
5113 return error_mark_node;
5114 return finish_binary_fold_expr (expr1, expr2, op);
5117 /* Parse a primary-expression.
5119 primary-expression:
5120 literal
5121 this
5122 ( expression )
5123 id-expression
5124 lambda-expression (C++11)
5126 GNU Extensions:
5128 primary-expression:
5129 ( compound-statement )
5130 __builtin_va_arg ( assignment-expression , type-id )
5131 __builtin_offsetof ( type-id , offsetof-expression )
5133 C++ Extensions:
5134 __has_nothrow_assign ( type-id )
5135 __has_nothrow_constructor ( type-id )
5136 __has_nothrow_copy ( type-id )
5137 __has_trivial_assign ( type-id )
5138 __has_trivial_constructor ( type-id )
5139 __has_trivial_copy ( type-id )
5140 __has_trivial_destructor ( type-id )
5141 __has_virtual_destructor ( type-id )
5142 __is_abstract ( type-id )
5143 __is_base_of ( type-id , type-id )
5144 __is_class ( type-id )
5145 __is_empty ( type-id )
5146 __is_enum ( type-id )
5147 __is_final ( type-id )
5148 __is_literal_type ( type-id )
5149 __is_pod ( type-id )
5150 __is_polymorphic ( type-id )
5151 __is_std_layout ( type-id )
5152 __is_trivial ( type-id )
5153 __is_union ( type-id )
5155 Objective-C++ Extension:
5157 primary-expression:
5158 objc-expression
5160 literal:
5161 __null
5163 ADDRESS_P is true iff this expression was immediately preceded by
5164 "&" and therefore might denote a pointer-to-member. CAST_P is true
5165 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5166 true iff this expression is a template argument.
5168 Returns a representation of the expression. Upon return, *IDK
5169 indicates what kind of id-expression (if any) was present. */
5171 static cp_expr
5172 cp_parser_primary_expression (cp_parser *parser,
5173 bool address_p,
5174 bool cast_p,
5175 bool template_arg_p,
5176 bool decltype_p,
5177 cp_id_kind *idk)
5179 cp_token *token = NULL;
5181 /* Assume the primary expression is not an id-expression. */
5182 *idk = CP_ID_KIND_NONE;
5184 /* Peek at the next token. */
5185 token = cp_lexer_peek_token (parser->lexer);
5186 switch ((int) token->type)
5188 /* literal:
5189 integer-literal
5190 character-literal
5191 floating-literal
5192 string-literal
5193 boolean-literal
5194 pointer-literal
5195 user-defined-literal */
5196 case CPP_CHAR:
5197 case CPP_CHAR16:
5198 case CPP_CHAR32:
5199 case CPP_WCHAR:
5200 case CPP_UTF8CHAR:
5201 case CPP_NUMBER:
5202 case CPP_PREPARSED_EXPR:
5203 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5204 return cp_parser_userdef_numeric_literal (parser);
5205 token = cp_lexer_consume_token (parser->lexer);
5206 if (TREE_CODE (token->u.value) == FIXED_CST)
5208 error_at (token->location,
5209 "fixed-point types not supported in C++");
5210 return error_mark_node;
5212 /* Floating-point literals are only allowed in an integral
5213 constant expression if they are cast to an integral or
5214 enumeration type. */
5215 if (TREE_CODE (token->u.value) == REAL_CST
5216 && parser->integral_constant_expression_p
5217 && pedantic)
5219 /* CAST_P will be set even in invalid code like "int(2.7 +
5220 ...)". Therefore, we have to check that the next token
5221 is sure to end the cast. */
5222 if (cast_p)
5224 cp_token *next_token;
5226 next_token = cp_lexer_peek_token (parser->lexer);
5227 if (/* The comma at the end of an
5228 enumerator-definition. */
5229 next_token->type != CPP_COMMA
5230 /* The curly brace at the end of an enum-specifier. */
5231 && next_token->type != CPP_CLOSE_BRACE
5232 /* The end of a statement. */
5233 && next_token->type != CPP_SEMICOLON
5234 /* The end of the cast-expression. */
5235 && next_token->type != CPP_CLOSE_PAREN
5236 /* The end of an array bound. */
5237 && next_token->type != CPP_CLOSE_SQUARE
5238 /* The closing ">" in a template-argument-list. */
5239 && (next_token->type != CPP_GREATER
5240 || parser->greater_than_is_operator_p)
5241 /* C++0x only: A ">>" treated like two ">" tokens,
5242 in a template-argument-list. */
5243 && (next_token->type != CPP_RSHIFT
5244 || (cxx_dialect == cxx98)
5245 || parser->greater_than_is_operator_p))
5246 cast_p = false;
5249 /* If we are within a cast, then the constraint that the
5250 cast is to an integral or enumeration type will be
5251 checked at that point. If we are not within a cast, then
5252 this code is invalid. */
5253 if (!cast_p)
5254 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5256 return (cp_expr (token->u.value, token->location)
5257 .maybe_add_location_wrapper ());
5259 case CPP_CHAR_USERDEF:
5260 case CPP_CHAR16_USERDEF:
5261 case CPP_CHAR32_USERDEF:
5262 case CPP_WCHAR_USERDEF:
5263 case CPP_UTF8CHAR_USERDEF:
5264 return cp_parser_userdef_char_literal (parser);
5266 case CPP_STRING:
5267 case CPP_STRING16:
5268 case CPP_STRING32:
5269 case CPP_WSTRING:
5270 case CPP_UTF8STRING:
5271 case CPP_STRING_USERDEF:
5272 case CPP_STRING16_USERDEF:
5273 case CPP_STRING32_USERDEF:
5274 case CPP_WSTRING_USERDEF:
5275 case CPP_UTF8STRING_USERDEF:
5276 /* ??? Should wide strings be allowed when parser->translate_strings_p
5277 is false (i.e. in attributes)? If not, we can kill the third
5278 argument to cp_parser_string_literal. */
5279 return (cp_parser_string_literal (parser,
5280 parser->translate_strings_p,
5281 true)
5282 .maybe_add_location_wrapper ());
5284 case CPP_OPEN_PAREN:
5285 /* If we see `( { ' then we are looking at the beginning of
5286 a GNU statement-expression. */
5287 if (cp_parser_allow_gnu_extensions_p (parser)
5288 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5290 /* Statement-expressions are not allowed by the standard. */
5291 pedwarn (token->location, OPT_Wpedantic,
5292 "ISO C++ forbids braced-groups within expressions");
5294 /* And they're not allowed outside of a function-body; you
5295 cannot, for example, write:
5297 int i = ({ int j = 3; j + 1; });
5299 at class or namespace scope. */
5300 if (!parser->in_function_body
5301 || parser->in_template_argument_list_p)
5303 error_at (token->location,
5304 "statement-expressions are not allowed outside "
5305 "functions nor in template-argument lists");
5306 cp_parser_skip_to_end_of_block_or_statement (parser);
5307 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5308 cp_lexer_consume_token (parser->lexer);
5309 return error_mark_node;
5311 else
5312 return cp_parser_statement_expr (parser);
5314 /* Otherwise it's a normal parenthesized expression. */
5316 cp_expr expr;
5317 bool saved_greater_than_is_operator_p;
5319 location_t open_paren_loc = token->location;
5321 /* Consume the `('. */
5322 matching_parens parens;
5323 parens.consume_open (parser);
5324 /* Within a parenthesized expression, a `>' token is always
5325 the greater-than operator. */
5326 saved_greater_than_is_operator_p
5327 = parser->greater_than_is_operator_p;
5328 parser->greater_than_is_operator_p = true;
5330 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5331 /* Left fold expression. */
5332 expr = NULL_TREE;
5333 else
5334 /* Parse the parenthesized expression. */
5335 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5337 token = cp_lexer_peek_token (parser->lexer);
5338 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5340 expr = cp_parser_fold_expression (parser, expr);
5341 if (expr != error_mark_node
5342 && cxx_dialect < cxx17
5343 && !in_system_header_at (input_location))
5344 pedwarn (input_location, 0, "fold-expressions only available "
5345 "with %<-std=c++17%> or %<-std=gnu++17%>");
5347 else
5348 /* Let the front end know that this expression was
5349 enclosed in parentheses. This matters in case, for
5350 example, the expression is of the form `A::B', since
5351 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5352 not. */
5353 expr = finish_parenthesized_expr (expr);
5355 /* DR 705: Wrapping an unqualified name in parentheses
5356 suppresses arg-dependent lookup. We want to pass back
5357 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5358 (c++/37862), but none of the others. */
5359 if (*idk != CP_ID_KIND_QUALIFIED)
5360 *idk = CP_ID_KIND_NONE;
5362 /* The `>' token might be the end of a template-id or
5363 template-parameter-list now. */
5364 parser->greater_than_is_operator_p
5365 = saved_greater_than_is_operator_p;
5367 /* Consume the `)'. */
5368 token = cp_lexer_peek_token (parser->lexer);
5369 location_t close_paren_loc = token->location;
5370 expr.set_range (open_paren_loc, close_paren_loc);
5371 if (!parens.require_close (parser)
5372 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5373 cp_parser_skip_to_end_of_statement (parser);
5375 return expr;
5378 case CPP_OPEN_SQUARE:
5380 if (c_dialect_objc ())
5382 /* We might have an Objective-C++ message. */
5383 cp_parser_parse_tentatively (parser);
5384 tree msg = cp_parser_objc_message_expression (parser);
5385 /* If that works out, we're done ... */
5386 if (cp_parser_parse_definitely (parser))
5387 return msg;
5388 /* ... else, fall though to see if it's a lambda. */
5390 cp_expr lam = cp_parser_lambda_expression (parser);
5391 /* Don't warn about a failed tentative parse. */
5392 if (cp_parser_error_occurred (parser))
5393 return error_mark_node;
5394 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5395 return lam;
5398 case CPP_OBJC_STRING:
5399 if (c_dialect_objc ())
5400 /* We have an Objective-C++ string literal. */
5401 return cp_parser_objc_expression (parser);
5402 cp_parser_error (parser, "expected primary-expression");
5403 return error_mark_node;
5405 case CPP_KEYWORD:
5406 switch (token->keyword)
5408 /* These two are the boolean literals. */
5409 case RID_TRUE:
5410 cp_lexer_consume_token (parser->lexer);
5411 return cp_expr (boolean_true_node, token->location);
5412 case RID_FALSE:
5413 cp_lexer_consume_token (parser->lexer);
5414 return cp_expr (boolean_false_node, token->location);
5416 /* The `__null' literal. */
5417 case RID_NULL:
5418 cp_lexer_consume_token (parser->lexer);
5419 return cp_expr (null_node, token->location);
5421 /* The `nullptr' literal. */
5422 case RID_NULLPTR:
5423 cp_lexer_consume_token (parser->lexer);
5424 return cp_expr (nullptr_node, token->location);
5426 /* Recognize the `this' keyword. */
5427 case RID_THIS:
5428 cp_lexer_consume_token (parser->lexer);
5429 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5431 error_at (token->location,
5432 "%<this%> may not be used in this context");
5433 return error_mark_node;
5435 /* Pointers cannot appear in constant-expressions. */
5436 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5437 return error_mark_node;
5438 return cp_expr (finish_this_expr (), token->location);
5440 /* The `operator' keyword can be the beginning of an
5441 id-expression. */
5442 case RID_OPERATOR:
5443 goto id_expression;
5445 case RID_FUNCTION_NAME:
5446 case RID_PRETTY_FUNCTION_NAME:
5447 case RID_C99_FUNCTION_NAME:
5449 non_integral_constant name;
5451 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5452 __func__ are the names of variables -- but they are
5453 treated specially. Therefore, they are handled here,
5454 rather than relying on the generic id-expression logic
5455 below. Grammatically, these names are id-expressions.
5457 Consume the token. */
5458 token = cp_lexer_consume_token (parser->lexer);
5460 switch (token->keyword)
5462 case RID_FUNCTION_NAME:
5463 name = NIC_FUNC_NAME;
5464 break;
5465 case RID_PRETTY_FUNCTION_NAME:
5466 name = NIC_PRETTY_FUNC;
5467 break;
5468 case RID_C99_FUNCTION_NAME:
5469 name = NIC_C99_FUNC;
5470 break;
5471 default:
5472 gcc_unreachable ();
5475 if (cp_parser_non_integral_constant_expression (parser, name))
5476 return error_mark_node;
5478 /* Look up the name. */
5479 return finish_fname (token->u.value);
5482 case RID_VA_ARG:
5484 tree expression;
5485 tree type;
5486 location_t type_location;
5487 location_t start_loc
5488 = cp_lexer_peek_token (parser->lexer)->location;
5489 /* The `__builtin_va_arg' construct is used to handle
5490 `va_arg'. Consume the `__builtin_va_arg' token. */
5491 cp_lexer_consume_token (parser->lexer);
5492 /* Look for the opening `('. */
5493 matching_parens parens;
5494 parens.require_open (parser);
5495 /* Now, parse the assignment-expression. */
5496 expression = cp_parser_assignment_expression (parser);
5497 /* Look for the `,'. */
5498 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5499 type_location = cp_lexer_peek_token (parser->lexer)->location;
5500 /* Parse the type-id. */
5502 type_id_in_expr_sentinel s (parser);
5503 type = cp_parser_type_id (parser);
5505 /* Look for the closing `)'. */
5506 location_t finish_loc
5507 = cp_lexer_peek_token (parser->lexer)->location;
5508 parens.require_close (parser);
5509 /* Using `va_arg' in a constant-expression is not
5510 allowed. */
5511 if (cp_parser_non_integral_constant_expression (parser,
5512 NIC_VA_ARG))
5513 return error_mark_node;
5514 /* Construct a location of the form:
5515 __builtin_va_arg (v, int)
5516 ~~~~~~~~~~~~~~~~~~~~~^~~~
5517 with the caret at the type, ranging from the start of the
5518 "__builtin_va_arg" token to the close paren. */
5519 location_t combined_loc
5520 = make_location (type_location, start_loc, finish_loc);
5521 return build_x_va_arg (combined_loc, expression, type);
5524 case RID_OFFSETOF:
5525 return cp_parser_builtin_offsetof (parser);
5527 case RID_HAS_NOTHROW_ASSIGN:
5528 case RID_HAS_NOTHROW_CONSTRUCTOR:
5529 case RID_HAS_NOTHROW_COPY:
5530 case RID_HAS_TRIVIAL_ASSIGN:
5531 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5532 case RID_HAS_TRIVIAL_COPY:
5533 case RID_HAS_TRIVIAL_DESTRUCTOR:
5534 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5535 case RID_HAS_VIRTUAL_DESTRUCTOR:
5536 case RID_IS_ABSTRACT:
5537 case RID_IS_AGGREGATE:
5538 case RID_IS_BASE_OF:
5539 case RID_IS_CLASS:
5540 case RID_IS_EMPTY:
5541 case RID_IS_ENUM:
5542 case RID_IS_FINAL:
5543 case RID_IS_LITERAL_TYPE:
5544 case RID_IS_POD:
5545 case RID_IS_POLYMORPHIC:
5546 case RID_IS_SAME_AS:
5547 case RID_IS_STD_LAYOUT:
5548 case RID_IS_TRIVIAL:
5549 case RID_IS_TRIVIALLY_ASSIGNABLE:
5550 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5551 case RID_IS_TRIVIALLY_COPYABLE:
5552 case RID_IS_UNION:
5553 case RID_IS_ASSIGNABLE:
5554 case RID_IS_CONSTRUCTIBLE:
5555 return cp_parser_trait_expr (parser, token->keyword);
5557 // C++ concepts
5558 case RID_REQUIRES:
5559 return cp_parser_requires_expression (parser);
5561 /* Objective-C++ expressions. */
5562 case RID_AT_ENCODE:
5563 case RID_AT_PROTOCOL:
5564 case RID_AT_SELECTOR:
5565 return cp_parser_objc_expression (parser);
5567 case RID_TEMPLATE:
5568 if (parser->in_function_body
5569 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5570 == CPP_LESS))
5572 error_at (token->location,
5573 "a template declaration cannot appear at block scope");
5574 cp_parser_skip_to_end_of_block_or_statement (parser);
5575 return error_mark_node;
5577 /* FALLTHRU */
5578 default:
5579 cp_parser_error (parser, "expected primary-expression");
5580 return error_mark_node;
5583 /* An id-expression can start with either an identifier, a
5584 `::' as the beginning of a qualified-id, or the "operator"
5585 keyword. */
5586 case CPP_NAME:
5587 case CPP_SCOPE:
5588 case CPP_TEMPLATE_ID:
5589 case CPP_NESTED_NAME_SPECIFIER:
5591 id_expression:
5592 cp_expr id_expression;
5593 cp_expr decl;
5594 const char *error_msg;
5595 bool template_p;
5596 bool done;
5597 cp_token *id_expr_token;
5599 /* Parse the id-expression. */
5600 id_expression
5601 = cp_parser_id_expression (parser,
5602 /*template_keyword_p=*/false,
5603 /*check_dependency_p=*/true,
5604 &template_p,
5605 /*declarator_p=*/false,
5606 /*optional_p=*/false);
5607 if (id_expression == error_mark_node)
5608 return error_mark_node;
5609 id_expr_token = token;
5610 token = cp_lexer_peek_token (parser->lexer);
5611 done = (token->type != CPP_OPEN_SQUARE
5612 && token->type != CPP_OPEN_PAREN
5613 && token->type != CPP_DOT
5614 && token->type != CPP_DEREF
5615 && token->type != CPP_PLUS_PLUS
5616 && token->type != CPP_MINUS_MINUS);
5617 /* If we have a template-id, then no further lookup is
5618 required. If the template-id was for a template-class, we
5619 will sometimes have a TYPE_DECL at this point. */
5620 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5621 || TREE_CODE (id_expression) == TYPE_DECL)
5622 decl = id_expression;
5623 /* Look up the name. */
5624 else
5626 tree ambiguous_decls;
5628 /* If we already know that this lookup is ambiguous, then
5629 we've already issued an error message; there's no reason
5630 to check again. */
5631 if (id_expr_token->type == CPP_NAME
5632 && id_expr_token->error_reported)
5634 cp_parser_simulate_error (parser);
5635 return error_mark_node;
5638 decl = cp_parser_lookup_name (parser, id_expression,
5639 none_type,
5640 template_p,
5641 /*is_namespace=*/false,
5642 /*check_dependency=*/true,
5643 &ambiguous_decls,
5644 id_expression.get_location ());
5645 /* If the lookup was ambiguous, an error will already have
5646 been issued. */
5647 if (ambiguous_decls)
5648 return error_mark_node;
5650 /* In Objective-C++, we may have an Objective-C 2.0
5651 dot-syntax for classes here. */
5652 if (c_dialect_objc ()
5653 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5654 && TREE_CODE (decl) == TYPE_DECL
5655 && objc_is_class_name (decl))
5657 tree component;
5658 cp_lexer_consume_token (parser->lexer);
5659 component = cp_parser_identifier (parser);
5660 if (component == error_mark_node)
5661 return error_mark_node;
5663 tree result = objc_build_class_component_ref (id_expression,
5664 component);
5665 /* Build a location of the form:
5666 expr.component
5667 ~~~~~^~~~~~~~~
5668 with caret at the start of the component name (at
5669 input_location), ranging from the start of the id_expression
5670 to the end of the component name. */
5671 location_t combined_loc
5672 = make_location (input_location, id_expression.get_start (),
5673 get_finish (input_location));
5674 protected_set_expr_location (result, combined_loc);
5675 return result;
5678 /* In Objective-C++, an instance variable (ivar) may be preferred
5679 to whatever cp_parser_lookup_name() found.
5680 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5681 rest of c-family, we have to do a little extra work to preserve
5682 any location information in cp_expr "decl". Given that
5683 objc_lookup_ivar is implemented in "c-family" and "objc", we
5684 have a trip through the pure "tree" type, rather than cp_expr.
5685 Naively copying it back to "decl" would implicitly give the
5686 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5687 store an EXPR_LOCATION. Hence we only update "decl" (and
5688 hence its location_t) if we get back a different tree node. */
5689 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5690 id_expression);
5691 if (decl_tree != decl.get_value ())
5692 decl = cp_expr (decl_tree);
5694 /* If name lookup gives us a SCOPE_REF, then the
5695 qualifying scope was dependent. */
5696 if (TREE_CODE (decl) == SCOPE_REF)
5698 /* At this point, we do not know if DECL is a valid
5699 integral constant expression. We assume that it is
5700 in fact such an expression, so that code like:
5702 template <int N> struct A {
5703 int a[B<N>::i];
5706 is accepted. At template-instantiation time, we
5707 will check that B<N>::i is actually a constant. */
5708 return decl;
5710 /* Check to see if DECL is a local variable in a context
5711 where that is forbidden. */
5712 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5713 && local_variable_p (decl))
5715 error_at (id_expression.get_location (),
5716 "local variable %qD may not appear in this context",
5717 decl.get_value ());
5718 return error_mark_node;
5722 decl = (finish_id_expression
5723 (id_expression, decl, parser->scope,
5724 idk,
5725 parser->integral_constant_expression_p,
5726 parser->allow_non_integral_constant_expression_p,
5727 &parser->non_integral_constant_expression_p,
5728 template_p, done, address_p,
5729 template_arg_p,
5730 &error_msg,
5731 id_expression.get_location ()));
5732 if (error_msg)
5733 cp_parser_error (parser, error_msg);
5734 /* Build a location for an id-expression of the form:
5735 ::ns::id
5736 ~~~~~~^~
5740 i.e. from the start of the first token to the end of the final
5741 token, with the caret at the start of the unqualified-id. */
5742 location_t caret_loc = get_pure_location (id_expression.get_location ());
5743 location_t start_loc = get_start (id_expr_token->location);
5744 location_t finish_loc = get_finish (id_expression.get_location ());
5745 location_t combined_loc
5746 = make_location (caret_loc, start_loc, finish_loc);
5748 decl.set_location (combined_loc);
5749 return decl;
5752 /* Anything else is an error. */
5753 default:
5754 cp_parser_error (parser, "expected primary-expression");
5755 return error_mark_node;
5759 static inline cp_expr
5760 cp_parser_primary_expression (cp_parser *parser,
5761 bool address_p,
5762 bool cast_p,
5763 bool template_arg_p,
5764 cp_id_kind *idk)
5766 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5767 /*decltype*/false, idk);
5770 /* Parse an id-expression.
5772 id-expression:
5773 unqualified-id
5774 qualified-id
5776 qualified-id:
5777 :: [opt] nested-name-specifier template [opt] unqualified-id
5778 :: identifier
5779 :: operator-function-id
5780 :: template-id
5782 Return a representation of the unqualified portion of the
5783 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5784 a `::' or nested-name-specifier.
5786 Often, if the id-expression was a qualified-id, the caller will
5787 want to make a SCOPE_REF to represent the qualified-id. This
5788 function does not do this in order to avoid wastefully creating
5789 SCOPE_REFs when they are not required.
5791 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5792 `template' keyword.
5794 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5795 uninstantiated templates.
5797 If *TEMPLATE_P is non-NULL, it is set to true iff the
5798 `template' keyword is used to explicitly indicate that the entity
5799 named is a template.
5801 If DECLARATOR_P is true, the id-expression is appearing as part of
5802 a declarator, rather than as part of an expression. */
5804 static cp_expr
5805 cp_parser_id_expression (cp_parser *parser,
5806 bool template_keyword_p,
5807 bool check_dependency_p,
5808 bool *template_p,
5809 bool declarator_p,
5810 bool optional_p)
5812 bool global_scope_p;
5813 bool nested_name_specifier_p;
5815 /* Assume the `template' keyword was not used. */
5816 if (template_p)
5817 *template_p = template_keyword_p;
5819 /* Look for the optional `::' operator. */
5820 global_scope_p
5821 = (!template_keyword_p
5822 && (cp_parser_global_scope_opt (parser,
5823 /*current_scope_valid_p=*/false)
5824 != NULL_TREE));
5826 /* Look for the optional nested-name-specifier. */
5827 nested_name_specifier_p
5828 = (cp_parser_nested_name_specifier_opt (parser,
5829 /*typename_keyword_p=*/false,
5830 check_dependency_p,
5831 /*type_p=*/false,
5832 declarator_p,
5833 template_keyword_p)
5834 != NULL_TREE);
5836 /* If there is a nested-name-specifier, then we are looking at
5837 the first qualified-id production. */
5838 if (nested_name_specifier_p)
5840 tree saved_scope;
5841 tree saved_object_scope;
5842 tree saved_qualifying_scope;
5843 cp_expr unqualified_id;
5844 bool is_template;
5846 /* See if the next token is the `template' keyword. */
5847 if (!template_p)
5848 template_p = &is_template;
5849 *template_p = cp_parser_optional_template_keyword (parser);
5850 /* Name lookup we do during the processing of the
5851 unqualified-id might obliterate SCOPE. */
5852 saved_scope = parser->scope;
5853 saved_object_scope = parser->object_scope;
5854 saved_qualifying_scope = parser->qualifying_scope;
5855 /* Process the final unqualified-id. */
5856 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5857 check_dependency_p,
5858 declarator_p,
5859 /*optional_p=*/false);
5860 /* Restore the SAVED_SCOPE for our caller. */
5861 parser->scope = saved_scope;
5862 parser->object_scope = saved_object_scope;
5863 parser->qualifying_scope = saved_qualifying_scope;
5865 return unqualified_id;
5867 /* Otherwise, if we are in global scope, then we are looking at one
5868 of the other qualified-id productions. */
5869 else if (global_scope_p)
5871 cp_token *token;
5872 tree id;
5874 /* Peek at the next token. */
5875 token = cp_lexer_peek_token (parser->lexer);
5877 /* If it's an identifier, and the next token is not a "<", then
5878 we can avoid the template-id case. This is an optimization
5879 for this common case. */
5880 if (token->type == CPP_NAME
5881 && !cp_parser_nth_token_starts_template_argument_list_p
5882 (parser, 2))
5883 return cp_parser_identifier (parser);
5885 cp_parser_parse_tentatively (parser);
5886 /* Try a template-id. */
5887 id = cp_parser_template_id (parser,
5888 /*template_keyword_p=*/false,
5889 /*check_dependency_p=*/true,
5890 none_type,
5891 declarator_p);
5892 /* If that worked, we're done. */
5893 if (cp_parser_parse_definitely (parser))
5894 return id;
5896 /* Peek at the next token. (Changes in the token buffer may
5897 have invalidated the pointer obtained above.) */
5898 token = cp_lexer_peek_token (parser->lexer);
5900 switch (token->type)
5902 case CPP_NAME:
5903 return cp_parser_identifier (parser);
5905 case CPP_KEYWORD:
5906 if (token->keyword == RID_OPERATOR)
5907 return cp_parser_operator_function_id (parser);
5908 /* Fall through. */
5910 default:
5911 cp_parser_error (parser, "expected id-expression");
5912 return error_mark_node;
5915 else
5916 return cp_parser_unqualified_id (parser, template_keyword_p,
5917 /*check_dependency_p=*/true,
5918 declarator_p,
5919 optional_p);
5922 /* Parse an unqualified-id.
5924 unqualified-id:
5925 identifier
5926 operator-function-id
5927 conversion-function-id
5928 ~ class-name
5929 template-id
5931 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5932 keyword, in a construct like `A::template ...'.
5934 Returns a representation of unqualified-id. For the `identifier'
5935 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5936 production a BIT_NOT_EXPR is returned; the operand of the
5937 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5938 other productions, see the documentation accompanying the
5939 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5940 names are looked up in uninstantiated templates. If DECLARATOR_P
5941 is true, the unqualified-id is appearing as part of a declarator,
5942 rather than as part of an expression. */
5944 static cp_expr
5945 cp_parser_unqualified_id (cp_parser* parser,
5946 bool template_keyword_p,
5947 bool check_dependency_p,
5948 bool declarator_p,
5949 bool optional_p)
5951 cp_token *token;
5953 /* Peek at the next token. */
5954 token = cp_lexer_peek_token (parser->lexer);
5956 switch ((int) token->type)
5958 case CPP_NAME:
5960 tree id;
5962 /* We don't know yet whether or not this will be a
5963 template-id. */
5964 cp_parser_parse_tentatively (parser);
5965 /* Try a template-id. */
5966 id = cp_parser_template_id (parser, template_keyword_p,
5967 check_dependency_p,
5968 none_type,
5969 declarator_p);
5970 /* If it worked, we're done. */
5971 if (cp_parser_parse_definitely (parser))
5972 return id;
5973 /* Otherwise, it's an ordinary identifier. */
5974 return cp_parser_identifier (parser);
5977 case CPP_TEMPLATE_ID:
5978 return cp_parser_template_id (parser, template_keyword_p,
5979 check_dependency_p,
5980 none_type,
5981 declarator_p);
5983 case CPP_COMPL:
5985 tree type_decl;
5986 tree qualifying_scope;
5987 tree object_scope;
5988 tree scope;
5989 bool done;
5990 location_t tilde_loc = token->location;
5992 /* Consume the `~' token. */
5993 cp_lexer_consume_token (parser->lexer);
5994 /* Parse the class-name. The standard, as written, seems to
5995 say that:
5997 template <typename T> struct S { ~S (); };
5998 template <typename T> S<T>::~S() {}
6000 is invalid, since `~' must be followed by a class-name, but
6001 `S<T>' is dependent, and so not known to be a class.
6002 That's not right; we need to look in uninstantiated
6003 templates. A further complication arises from:
6005 template <typename T> void f(T t) {
6006 t.T::~T();
6009 Here, it is not possible to look up `T' in the scope of `T'
6010 itself. We must look in both the current scope, and the
6011 scope of the containing complete expression.
6013 Yet another issue is:
6015 struct S {
6016 int S;
6017 ~S();
6020 S::~S() {}
6022 The standard does not seem to say that the `S' in `~S'
6023 should refer to the type `S' and not the data member
6024 `S::S'. */
6026 /* DR 244 says that we look up the name after the "~" in the
6027 same scope as we looked up the qualifying name. That idea
6028 isn't fully worked out; it's more complicated than that. */
6029 scope = parser->scope;
6030 object_scope = parser->object_scope;
6031 qualifying_scope = parser->qualifying_scope;
6033 /* Check for invalid scopes. */
6034 if (scope == error_mark_node)
6036 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6037 cp_lexer_consume_token (parser->lexer);
6038 return error_mark_node;
6040 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6042 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6043 error_at (token->location,
6044 "scope %qT before %<~%> is not a class-name",
6045 scope);
6046 cp_parser_simulate_error (parser);
6047 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6048 cp_lexer_consume_token (parser->lexer);
6049 return error_mark_node;
6051 gcc_assert (!scope || TYPE_P (scope));
6053 token = cp_lexer_peek_token (parser->lexer);
6055 /* Create a location with caret == start at the tilde,
6056 finishing at the end of the peeked token, e.g:
6057 ~token
6058 ^~~~~~. */
6059 location_t loc
6060 = make_location (tilde_loc, tilde_loc, token->location);
6062 /* If the name is of the form "X::~X" it's OK even if X is a
6063 typedef. */
6065 if (scope
6066 && token->type == CPP_NAME
6067 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6068 != CPP_LESS)
6069 && (token->u.value == TYPE_IDENTIFIER (scope)
6070 || (CLASS_TYPE_P (scope)
6071 && constructor_name_p (token->u.value, scope))))
6073 cp_lexer_consume_token (parser->lexer);
6074 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6077 /* ~auto means the destructor of whatever the object is. */
6078 if (cp_parser_is_keyword (token, RID_AUTO))
6080 if (cxx_dialect < cxx14)
6081 pedwarn (loc, 0,
6082 "%<~auto%> only available with "
6083 "%<-std=c++14%> or %<-std=gnu++14%>");
6084 cp_lexer_consume_token (parser->lexer);
6085 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6088 /* If there was an explicit qualification (S::~T), first look
6089 in the scope given by the qualification (i.e., S).
6091 Note: in the calls to cp_parser_class_name below we pass
6092 typename_type so that lookup finds the injected-class-name
6093 rather than the constructor. */
6094 done = false;
6095 type_decl = NULL_TREE;
6096 if (scope)
6098 cp_parser_parse_tentatively (parser);
6099 type_decl = cp_parser_class_name (parser,
6100 /*typename_keyword_p=*/false,
6101 /*template_keyword_p=*/false,
6102 typename_type,
6103 /*check_dependency=*/false,
6104 /*class_head_p=*/false,
6105 declarator_p);
6106 if (cp_parser_parse_definitely (parser))
6107 done = true;
6109 /* In "N::S::~S", look in "N" as well. */
6110 if (!done && scope && qualifying_scope)
6112 cp_parser_parse_tentatively (parser);
6113 parser->scope = qualifying_scope;
6114 parser->object_scope = NULL_TREE;
6115 parser->qualifying_scope = NULL_TREE;
6116 type_decl
6117 = cp_parser_class_name (parser,
6118 /*typename_keyword_p=*/false,
6119 /*template_keyword_p=*/false,
6120 typename_type,
6121 /*check_dependency=*/false,
6122 /*class_head_p=*/false,
6123 declarator_p);
6124 if (cp_parser_parse_definitely (parser))
6125 done = true;
6127 /* In "p->S::~T", look in the scope given by "*p" as well. */
6128 else if (!done && object_scope)
6130 cp_parser_parse_tentatively (parser);
6131 parser->scope = object_scope;
6132 parser->object_scope = NULL_TREE;
6133 parser->qualifying_scope = NULL_TREE;
6134 type_decl
6135 = cp_parser_class_name (parser,
6136 /*typename_keyword_p=*/false,
6137 /*template_keyword_p=*/false,
6138 typename_type,
6139 /*check_dependency=*/false,
6140 /*class_head_p=*/false,
6141 declarator_p);
6142 if (cp_parser_parse_definitely (parser))
6143 done = true;
6145 /* Look in the surrounding context. */
6146 if (!done)
6148 parser->scope = NULL_TREE;
6149 parser->object_scope = NULL_TREE;
6150 parser->qualifying_scope = NULL_TREE;
6151 if (processing_template_decl)
6152 cp_parser_parse_tentatively (parser);
6153 type_decl
6154 = cp_parser_class_name (parser,
6155 /*typename_keyword_p=*/false,
6156 /*template_keyword_p=*/false,
6157 typename_type,
6158 /*check_dependency=*/false,
6159 /*class_head_p=*/false,
6160 declarator_p);
6161 if (processing_template_decl
6162 && ! cp_parser_parse_definitely (parser))
6164 /* We couldn't find a type with this name. If we're parsing
6165 tentatively, fail and try something else. */
6166 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6168 cp_parser_simulate_error (parser);
6169 return error_mark_node;
6171 /* Otherwise, accept it and check for a match at instantiation
6172 time. */
6173 type_decl = cp_parser_identifier (parser);
6174 if (type_decl != error_mark_node)
6175 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6176 return type_decl;
6179 /* If an error occurred, assume that the name of the
6180 destructor is the same as the name of the qualifying
6181 class. That allows us to keep parsing after running
6182 into ill-formed destructor names. */
6183 if (type_decl == error_mark_node && scope)
6184 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6185 else if (type_decl == error_mark_node)
6186 return error_mark_node;
6188 /* Check that destructor name and scope match. */
6189 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6191 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6192 error_at (loc,
6193 "declaration of %<~%T%> as member of %qT",
6194 type_decl, scope);
6195 cp_parser_simulate_error (parser);
6196 return error_mark_node;
6199 /* [class.dtor]
6201 A typedef-name that names a class shall not be used as the
6202 identifier in the declarator for a destructor declaration. */
6203 if (declarator_p
6204 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6205 && !DECL_SELF_REFERENCE_P (type_decl)
6206 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6207 error_at (loc,
6208 "typedef-name %qD used as destructor declarator",
6209 type_decl);
6211 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6214 case CPP_KEYWORD:
6215 if (token->keyword == RID_OPERATOR)
6217 cp_expr id;
6219 /* This could be a template-id, so we try that first. */
6220 cp_parser_parse_tentatively (parser);
6221 /* Try a template-id. */
6222 id = cp_parser_template_id (parser, template_keyword_p,
6223 /*check_dependency_p=*/true,
6224 none_type,
6225 declarator_p);
6226 /* If that worked, we're done. */
6227 if (cp_parser_parse_definitely (parser))
6228 return id;
6229 /* We still don't know whether we're looking at an
6230 operator-function-id or a conversion-function-id. */
6231 cp_parser_parse_tentatively (parser);
6232 /* Try an operator-function-id. */
6233 id = cp_parser_operator_function_id (parser);
6234 /* If that didn't work, try a conversion-function-id. */
6235 if (!cp_parser_parse_definitely (parser))
6236 id = cp_parser_conversion_function_id (parser);
6238 return id;
6240 /* Fall through. */
6242 default:
6243 if (optional_p)
6244 return NULL_TREE;
6245 cp_parser_error (parser, "expected unqualified-id");
6246 return error_mark_node;
6250 /* Parse an (optional) nested-name-specifier.
6252 nested-name-specifier: [C++98]
6253 class-or-namespace-name :: nested-name-specifier [opt]
6254 class-or-namespace-name :: template nested-name-specifier [opt]
6256 nested-name-specifier: [C++0x]
6257 type-name ::
6258 namespace-name ::
6259 nested-name-specifier identifier ::
6260 nested-name-specifier template [opt] simple-template-id ::
6262 PARSER->SCOPE should be set appropriately before this function is
6263 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6264 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6265 in name lookups.
6267 Sets PARSER->SCOPE to the class (TYPE) or namespace
6268 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6269 it unchanged if there is no nested-name-specifier. Returns the new
6270 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6272 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6273 part of a declaration and/or decl-specifier. */
6275 static tree
6276 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6277 bool typename_keyword_p,
6278 bool check_dependency_p,
6279 bool type_p,
6280 bool is_declaration,
6281 bool template_keyword_p /* = false */)
6283 bool success = false;
6284 cp_token_position start = 0;
6285 cp_token *token;
6287 /* Remember where the nested-name-specifier starts. */
6288 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6289 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6291 start = cp_lexer_token_position (parser->lexer, false);
6292 push_deferring_access_checks (dk_deferred);
6295 while (true)
6297 tree new_scope;
6298 tree old_scope;
6299 tree saved_qualifying_scope;
6301 /* Spot cases that cannot be the beginning of a
6302 nested-name-specifier. */
6303 token = cp_lexer_peek_token (parser->lexer);
6305 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6306 the already parsed nested-name-specifier. */
6307 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6309 /* Grab the nested-name-specifier and continue the loop. */
6310 cp_parser_pre_parsed_nested_name_specifier (parser);
6311 /* If we originally encountered this nested-name-specifier
6312 with IS_DECLARATION set to false, we will not have
6313 resolved TYPENAME_TYPEs, so we must do so here. */
6314 if (is_declaration
6315 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6317 new_scope = resolve_typename_type (parser->scope,
6318 /*only_current_p=*/false);
6319 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6320 parser->scope = new_scope;
6322 success = true;
6323 continue;
6326 /* Spot cases that cannot be the beginning of a
6327 nested-name-specifier. On the second and subsequent times
6328 through the loop, we look for the `template' keyword. */
6329 if (success && token->keyword == RID_TEMPLATE)
6331 /* A template-id can start a nested-name-specifier. */
6332 else if (token->type == CPP_TEMPLATE_ID)
6334 /* DR 743: decltype can be used in a nested-name-specifier. */
6335 else if (token_is_decltype (token))
6337 else
6339 /* If the next token is not an identifier, then it is
6340 definitely not a type-name or namespace-name. */
6341 if (token->type != CPP_NAME)
6342 break;
6343 /* If the following token is neither a `<' (to begin a
6344 template-id), nor a `::', then we are not looking at a
6345 nested-name-specifier. */
6346 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6348 if (token->type == CPP_COLON
6349 && parser->colon_corrects_to_scope_p
6350 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6352 gcc_rich_location richloc (token->location);
6353 richloc.add_fixit_replace ("::");
6354 error_at (&richloc,
6355 "found %<:%> in nested-name-specifier, "
6356 "expected %<::%>");
6357 token->type = CPP_SCOPE;
6360 if (token->type != CPP_SCOPE
6361 && !cp_parser_nth_token_starts_template_argument_list_p
6362 (parser, 2))
6363 break;
6366 /* The nested-name-specifier is optional, so we parse
6367 tentatively. */
6368 cp_parser_parse_tentatively (parser);
6370 /* Look for the optional `template' keyword, if this isn't the
6371 first time through the loop. */
6372 if (success)
6373 template_keyword_p = cp_parser_optional_template_keyword (parser);
6375 /* Save the old scope since the name lookup we are about to do
6376 might destroy it. */
6377 old_scope = parser->scope;
6378 saved_qualifying_scope = parser->qualifying_scope;
6379 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6380 look up names in "X<T>::I" in order to determine that "Y" is
6381 a template. So, if we have a typename at this point, we make
6382 an effort to look through it. */
6383 if (is_declaration
6384 && !typename_keyword_p
6385 && parser->scope
6386 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6387 parser->scope = resolve_typename_type (parser->scope,
6388 /*only_current_p=*/false);
6389 /* Parse the qualifying entity. */
6390 new_scope
6391 = cp_parser_qualifying_entity (parser,
6392 typename_keyword_p,
6393 template_keyword_p,
6394 check_dependency_p,
6395 type_p,
6396 is_declaration);
6397 /* Look for the `::' token. */
6398 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6400 /* If we found what we wanted, we keep going; otherwise, we're
6401 done. */
6402 if (!cp_parser_parse_definitely (parser))
6404 bool error_p = false;
6406 /* Restore the OLD_SCOPE since it was valid before the
6407 failed attempt at finding the last
6408 class-or-namespace-name. */
6409 parser->scope = old_scope;
6410 parser->qualifying_scope = saved_qualifying_scope;
6412 /* If the next token is a decltype, and the one after that is a
6413 `::', then the decltype has failed to resolve to a class or
6414 enumeration type. Give this error even when parsing
6415 tentatively since it can't possibly be valid--and we're going
6416 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6417 won't get another chance.*/
6418 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6419 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6420 == CPP_SCOPE))
6422 token = cp_lexer_consume_token (parser->lexer);
6423 error_at (token->location, "%<decltype%> evaluates to %qT, "
6424 "which is not a class or enumeration type",
6425 token->u.tree_check_value->value);
6426 parser->scope = error_mark_node;
6427 error_p = true;
6428 /* As below. */
6429 success = true;
6430 cp_lexer_consume_token (parser->lexer);
6433 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6434 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6436 /* If we have a non-type template-id followed by ::, it can't
6437 possibly be valid. */
6438 token = cp_lexer_peek_token (parser->lexer);
6439 tree tid = token->u.tree_check_value->value;
6440 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6441 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6443 tree tmpl = NULL_TREE;
6444 if (is_overloaded_fn (tid))
6446 tree fns = get_fns (tid);
6447 if (OVL_SINGLE_P (fns))
6448 tmpl = OVL_FIRST (fns);
6449 error_at (token->location, "function template-id %qD "
6450 "in nested-name-specifier", tid);
6452 else
6454 /* Variable template. */
6455 tmpl = TREE_OPERAND (tid, 0);
6456 gcc_assert (variable_template_p (tmpl));
6457 error_at (token->location, "variable template-id %qD "
6458 "in nested-name-specifier", tid);
6460 if (tmpl)
6461 inform (DECL_SOURCE_LOCATION (tmpl),
6462 "%qD declared here", tmpl);
6464 parser->scope = error_mark_node;
6465 error_p = true;
6466 /* As below. */
6467 success = true;
6468 cp_lexer_consume_token (parser->lexer);
6469 cp_lexer_consume_token (parser->lexer);
6473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6474 break;
6475 /* If the next token is an identifier, and the one after
6476 that is a `::', then any valid interpretation would have
6477 found a class-or-namespace-name. */
6478 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6479 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6480 == CPP_SCOPE)
6481 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6482 != CPP_COMPL))
6484 token = cp_lexer_consume_token (parser->lexer);
6485 if (!error_p)
6487 if (!token->error_reported)
6489 tree decl;
6490 tree ambiguous_decls;
6492 decl = cp_parser_lookup_name (parser, token->u.value,
6493 none_type,
6494 /*is_template=*/false,
6495 /*is_namespace=*/false,
6496 /*check_dependency=*/true,
6497 &ambiguous_decls,
6498 token->location);
6499 if (TREE_CODE (decl) == TEMPLATE_DECL)
6500 error_at (token->location,
6501 "%qD used without template arguments",
6502 decl);
6503 else if (ambiguous_decls)
6505 // cp_parser_lookup_name has the same diagnostic,
6506 // thus make sure to emit it at most once.
6507 if (cp_parser_uncommitted_to_tentative_parse_p
6508 (parser))
6510 error_at (token->location,
6511 "reference to %qD is ambiguous",
6512 token->u.value);
6513 print_candidates (ambiguous_decls);
6515 decl = error_mark_node;
6517 else
6519 if (cxx_dialect != cxx98)
6520 cp_parser_name_lookup_error
6521 (parser, token->u.value, decl, NLE_NOT_CXX98,
6522 token->location);
6523 else
6524 cp_parser_name_lookup_error
6525 (parser, token->u.value, decl, NLE_CXX98,
6526 token->location);
6529 parser->scope = error_mark_node;
6530 error_p = true;
6531 /* Treat this as a successful nested-name-specifier
6532 due to:
6534 [basic.lookup.qual]
6536 If the name found is not a class-name (clause
6537 _class_) or namespace-name (_namespace.def_), the
6538 program is ill-formed. */
6539 success = true;
6541 cp_lexer_consume_token (parser->lexer);
6543 break;
6545 /* We've found one valid nested-name-specifier. */
6546 success = true;
6547 /* Name lookup always gives us a DECL. */
6548 if (TREE_CODE (new_scope) == TYPE_DECL)
6549 new_scope = TREE_TYPE (new_scope);
6550 /* Uses of "template" must be followed by actual templates. */
6551 if (template_keyword_p
6552 && !(CLASS_TYPE_P (new_scope)
6553 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6554 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6555 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6556 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6557 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6558 == TEMPLATE_ID_EXPR)))
6559 permerror (input_location, TYPE_P (new_scope)
6560 ? G_("%qT is not a template")
6561 : G_("%qD is not a template"),
6562 new_scope);
6563 /* If it is a class scope, try to complete it; we are about to
6564 be looking up names inside the class. */
6565 if (TYPE_P (new_scope)
6566 /* Since checking types for dependency can be expensive,
6567 avoid doing it if the type is already complete. */
6568 && !COMPLETE_TYPE_P (new_scope)
6569 /* Do not try to complete dependent types. */
6570 && !dependent_type_p (new_scope))
6572 new_scope = complete_type (new_scope);
6573 /* If it is a typedef to current class, use the current
6574 class instead, as the typedef won't have any names inside
6575 it yet. */
6576 if (!COMPLETE_TYPE_P (new_scope)
6577 && currently_open_class (new_scope))
6578 new_scope = TYPE_MAIN_VARIANT (new_scope);
6580 /* Make sure we look in the right scope the next time through
6581 the loop. */
6582 parser->scope = new_scope;
6585 /* If parsing tentatively, replace the sequence of tokens that makes
6586 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6587 token. That way, should we re-parse the token stream, we will
6588 not have to repeat the effort required to do the parse, nor will
6589 we issue duplicate error messages. */
6590 if (success && start)
6592 cp_token *token;
6594 token = cp_lexer_token_at (parser->lexer, start);
6595 /* Reset the contents of the START token. */
6596 token->type = CPP_NESTED_NAME_SPECIFIER;
6597 /* Retrieve any deferred checks. Do not pop this access checks yet
6598 so the memory will not be reclaimed during token replacing below. */
6599 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6600 token->u.tree_check_value->value = parser->scope;
6601 token->u.tree_check_value->checks = get_deferred_access_checks ();
6602 token->u.tree_check_value->qualifying_scope =
6603 parser->qualifying_scope;
6604 token->keyword = RID_MAX;
6606 /* Purge all subsequent tokens. */
6607 cp_lexer_purge_tokens_after (parser->lexer, start);
6610 if (start)
6611 pop_to_parent_deferring_access_checks ();
6613 return success ? parser->scope : NULL_TREE;
6616 /* Parse a nested-name-specifier. See
6617 cp_parser_nested_name_specifier_opt for details. This function
6618 behaves identically, except that it will an issue an error if no
6619 nested-name-specifier is present. */
6621 static tree
6622 cp_parser_nested_name_specifier (cp_parser *parser,
6623 bool typename_keyword_p,
6624 bool check_dependency_p,
6625 bool type_p,
6626 bool is_declaration)
6628 tree scope;
6630 /* Look for the nested-name-specifier. */
6631 scope = cp_parser_nested_name_specifier_opt (parser,
6632 typename_keyword_p,
6633 check_dependency_p,
6634 type_p,
6635 is_declaration);
6636 /* If it was not present, issue an error message. */
6637 if (!scope)
6639 cp_parser_error (parser, "expected nested-name-specifier");
6640 parser->scope = NULL_TREE;
6643 return scope;
6646 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6647 this is either a class-name or a namespace-name (which corresponds
6648 to the class-or-namespace-name production in the grammar). For
6649 C++0x, it can also be a type-name that refers to an enumeration
6650 type or a simple-template-id.
6652 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6653 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6654 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6655 TYPE_P is TRUE iff the next name should be taken as a class-name,
6656 even the same name is declared to be another entity in the same
6657 scope.
6659 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6660 specified by the class-or-namespace-name. If neither is found the
6661 ERROR_MARK_NODE is returned. */
6663 static tree
6664 cp_parser_qualifying_entity (cp_parser *parser,
6665 bool typename_keyword_p,
6666 bool template_keyword_p,
6667 bool check_dependency_p,
6668 bool type_p,
6669 bool is_declaration)
6671 tree saved_scope;
6672 tree saved_qualifying_scope;
6673 tree saved_object_scope;
6674 tree scope;
6675 bool only_class_p;
6676 bool successful_parse_p;
6678 /* DR 743: decltype can appear in a nested-name-specifier. */
6679 if (cp_lexer_next_token_is_decltype (parser->lexer))
6681 scope = cp_parser_decltype (parser);
6682 if (TREE_CODE (scope) != ENUMERAL_TYPE
6683 && !MAYBE_CLASS_TYPE_P (scope))
6685 cp_parser_simulate_error (parser);
6686 return error_mark_node;
6688 if (TYPE_NAME (scope))
6689 scope = TYPE_NAME (scope);
6690 return scope;
6693 /* Before we try to parse the class-name, we must save away the
6694 current PARSER->SCOPE since cp_parser_class_name will destroy
6695 it. */
6696 saved_scope = parser->scope;
6697 saved_qualifying_scope = parser->qualifying_scope;
6698 saved_object_scope = parser->object_scope;
6699 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6700 there is no need to look for a namespace-name. */
6701 only_class_p = template_keyword_p
6702 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6703 if (!only_class_p)
6704 cp_parser_parse_tentatively (parser);
6705 scope = cp_parser_class_name (parser,
6706 typename_keyword_p,
6707 template_keyword_p,
6708 type_p ? class_type : none_type,
6709 check_dependency_p,
6710 /*class_head_p=*/false,
6711 is_declaration,
6712 /*enum_ok=*/cxx_dialect > cxx98);
6713 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6714 /* If that didn't work, try for a namespace-name. */
6715 if (!only_class_p && !successful_parse_p)
6717 /* Restore the saved scope. */
6718 parser->scope = saved_scope;
6719 parser->qualifying_scope = saved_qualifying_scope;
6720 parser->object_scope = saved_object_scope;
6721 /* If we are not looking at an identifier followed by the scope
6722 resolution operator, then this is not part of a
6723 nested-name-specifier. (Note that this function is only used
6724 to parse the components of a nested-name-specifier.) */
6725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6726 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6727 return error_mark_node;
6728 scope = cp_parser_namespace_name (parser);
6731 return scope;
6734 /* Return true if we are looking at a compound-literal, false otherwise. */
6736 static bool
6737 cp_parser_compound_literal_p (cp_parser *parser)
6739 cp_lexer_save_tokens (parser->lexer);
6741 /* Skip tokens until the next token is a closing parenthesis.
6742 If we find the closing `)', and the next token is a `{', then
6743 we are looking at a compound-literal. */
6744 bool compound_literal_p
6745 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6746 /*consume_paren=*/true)
6747 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6749 /* Roll back the tokens we skipped. */
6750 cp_lexer_rollback_tokens (parser->lexer);
6752 return compound_literal_p;
6755 /* Return true if EXPR is the integer constant zero or a complex constant
6756 of zero, without any folding, but ignoring location wrappers. */
6758 bool
6759 literal_integer_zerop (const_tree expr)
6761 return (location_wrapper_p (expr)
6762 && integer_zerop (TREE_OPERAND (expr, 0)));
6765 /* Parse a postfix-expression.
6767 postfix-expression:
6768 primary-expression
6769 postfix-expression [ expression ]
6770 postfix-expression ( expression-list [opt] )
6771 simple-type-specifier ( expression-list [opt] )
6772 typename :: [opt] nested-name-specifier identifier
6773 ( expression-list [opt] )
6774 typename :: [opt] nested-name-specifier template [opt] template-id
6775 ( expression-list [opt] )
6776 postfix-expression . template [opt] id-expression
6777 postfix-expression -> template [opt] id-expression
6778 postfix-expression . pseudo-destructor-name
6779 postfix-expression -> pseudo-destructor-name
6780 postfix-expression ++
6781 postfix-expression --
6782 dynamic_cast < type-id > ( expression )
6783 static_cast < type-id > ( expression )
6784 reinterpret_cast < type-id > ( expression )
6785 const_cast < type-id > ( expression )
6786 typeid ( expression )
6787 typeid ( type-id )
6789 GNU Extension:
6791 postfix-expression:
6792 ( type-id ) { initializer-list , [opt] }
6794 This extension is a GNU version of the C99 compound-literal
6795 construct. (The C99 grammar uses `type-name' instead of `type-id',
6796 but they are essentially the same concept.)
6798 If ADDRESS_P is true, the postfix expression is the operand of the
6799 `&' operator. CAST_P is true if this expression is the target of a
6800 cast.
6802 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6803 class member access expressions [expr.ref].
6805 Returns a representation of the expression. */
6807 static cp_expr
6808 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6809 bool member_access_only_p, bool decltype_p,
6810 cp_id_kind * pidk_return)
6812 cp_token *token;
6813 location_t loc;
6814 enum rid keyword;
6815 cp_id_kind idk = CP_ID_KIND_NONE;
6816 cp_expr postfix_expression = NULL_TREE;
6817 bool is_member_access = false;
6819 /* Peek at the next token. */
6820 token = cp_lexer_peek_token (parser->lexer);
6821 loc = token->location;
6822 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6824 /* Some of the productions are determined by keywords. */
6825 keyword = token->keyword;
6826 switch (keyword)
6828 case RID_DYNCAST:
6829 case RID_STATCAST:
6830 case RID_REINTCAST:
6831 case RID_CONSTCAST:
6833 tree type;
6834 cp_expr expression;
6835 const char *saved_message;
6836 bool saved_in_type_id_in_expr_p;
6838 /* All of these can be handled in the same way from the point
6839 of view of parsing. Begin by consuming the token
6840 identifying the cast. */
6841 cp_lexer_consume_token (parser->lexer);
6843 /* New types cannot be defined in the cast. */
6844 saved_message = parser->type_definition_forbidden_message;
6845 parser->type_definition_forbidden_message
6846 = G_("types may not be defined in casts");
6848 /* Look for the opening `<'. */
6849 cp_parser_require (parser, CPP_LESS, RT_LESS);
6850 /* Parse the type to which we are casting. */
6851 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6852 parser->in_type_id_in_expr_p = true;
6853 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6854 NULL);
6855 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6856 /* Look for the closing `>'. */
6857 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6858 /* Restore the old message. */
6859 parser->type_definition_forbidden_message = saved_message;
6861 bool saved_greater_than_is_operator_p
6862 = parser->greater_than_is_operator_p;
6863 parser->greater_than_is_operator_p = true;
6865 /* And the expression which is being cast. */
6866 matching_parens parens;
6867 parens.require_open (parser);
6868 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6869 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6870 RT_CLOSE_PAREN);
6871 location_t end_loc = close_paren ?
6872 close_paren->location : UNKNOWN_LOCATION;
6874 parser->greater_than_is_operator_p
6875 = saved_greater_than_is_operator_p;
6877 /* Only type conversions to integral or enumeration types
6878 can be used in constant-expressions. */
6879 if (!cast_valid_in_integral_constant_expression_p (type)
6880 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6882 postfix_expression = error_mark_node;
6883 break;
6886 switch (keyword)
6888 case RID_DYNCAST:
6889 postfix_expression
6890 = build_dynamic_cast (type, expression, tf_warning_or_error);
6891 break;
6892 case RID_STATCAST:
6893 postfix_expression
6894 = build_static_cast (type, expression, tf_warning_or_error);
6895 break;
6896 case RID_REINTCAST:
6897 postfix_expression
6898 = build_reinterpret_cast (type, expression,
6899 tf_warning_or_error);
6900 break;
6901 case RID_CONSTCAST:
6902 postfix_expression
6903 = build_const_cast (type, expression, tf_warning_or_error);
6904 break;
6905 default:
6906 gcc_unreachable ();
6909 /* Construct a location e.g. :
6910 reinterpret_cast <int *> (expr)
6911 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6912 ranging from the start of the "*_cast" token to the final closing
6913 paren, with the caret at the start. */
6914 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6915 postfix_expression.set_location (cp_cast_loc);
6917 break;
6919 case RID_TYPEID:
6921 tree type;
6922 const char *saved_message;
6923 bool saved_in_type_id_in_expr_p;
6925 /* Consume the `typeid' token. */
6926 cp_lexer_consume_token (parser->lexer);
6927 /* Look for the `(' token. */
6928 matching_parens parens;
6929 parens.require_open (parser);
6930 /* Types cannot be defined in a `typeid' expression. */
6931 saved_message = parser->type_definition_forbidden_message;
6932 parser->type_definition_forbidden_message
6933 = G_("types may not be defined in a %<typeid%> expression");
6934 /* We can't be sure yet whether we're looking at a type-id or an
6935 expression. */
6936 cp_parser_parse_tentatively (parser);
6937 /* Try a type-id first. */
6938 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6939 parser->in_type_id_in_expr_p = true;
6940 type = cp_parser_type_id (parser);
6941 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6942 /* Look for the `)' token. Otherwise, we can't be sure that
6943 we're not looking at an expression: consider `typeid (int
6944 (3))', for example. */
6945 cp_token *close_paren = parens.require_close (parser);
6946 /* If all went well, simply lookup the type-id. */
6947 if (cp_parser_parse_definitely (parser))
6948 postfix_expression = get_typeid (type, tf_warning_or_error);
6949 /* Otherwise, fall back to the expression variant. */
6950 else
6952 tree expression;
6954 /* Look for an expression. */
6955 expression = cp_parser_expression (parser, & idk);
6956 /* Compute its typeid. */
6957 postfix_expression = build_typeid (expression, tf_warning_or_error);
6958 /* Look for the `)' token. */
6959 close_paren = parens.require_close (parser);
6961 /* Restore the saved message. */
6962 parser->type_definition_forbidden_message = saved_message;
6963 /* `typeid' may not appear in an integral constant expression. */
6964 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6965 postfix_expression = error_mark_node;
6967 /* Construct a location e.g. :
6968 typeid (expr)
6969 ^~~~~~~~~~~~~
6970 ranging from the start of the "typeid" token to the final closing
6971 paren, with the caret at the start. */
6972 if (close_paren)
6974 location_t typeid_loc
6975 = make_location (start_loc, start_loc, close_paren->location);
6976 postfix_expression.set_location (typeid_loc);
6977 postfix_expression.maybe_add_location_wrapper ();
6980 break;
6982 case RID_TYPENAME:
6984 tree type;
6985 /* The syntax permitted here is the same permitted for an
6986 elaborated-type-specifier. */
6987 ++parser->prevent_constrained_type_specifiers;
6988 type = cp_parser_elaborated_type_specifier (parser,
6989 /*is_friend=*/false,
6990 /*is_declaration=*/false);
6991 --parser->prevent_constrained_type_specifiers;
6992 postfix_expression = cp_parser_functional_cast (parser, type);
6994 break;
6996 case RID_ADDRESSOF:
6997 case RID_BUILTIN_SHUFFLE:
6998 case RID_BUILTIN_LAUNDER:
7000 vec<tree, va_gc> *vec;
7001 unsigned int i;
7002 tree p;
7004 cp_lexer_consume_token (parser->lexer);
7005 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7006 /*cast_p=*/false, /*allow_expansion_p=*/true,
7007 /*non_constant_p=*/NULL);
7008 if (vec == NULL)
7010 postfix_expression = error_mark_node;
7011 break;
7014 FOR_EACH_VEC_ELT (*vec, i, p)
7015 mark_exp_read (p);
7017 switch (keyword)
7019 case RID_ADDRESSOF:
7020 if (vec->length () == 1)
7021 postfix_expression
7022 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7023 else
7025 error_at (loc, "wrong number of arguments to "
7026 "%<__builtin_addressof%>");
7027 postfix_expression = error_mark_node;
7029 break;
7031 case RID_BUILTIN_LAUNDER:
7032 if (vec->length () == 1)
7033 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7034 tf_warning_or_error);
7035 else
7037 error_at (loc, "wrong number of arguments to "
7038 "%<__builtin_launder%>");
7039 postfix_expression = error_mark_node;
7041 break;
7043 case RID_BUILTIN_SHUFFLE:
7044 if (vec->length () == 2)
7045 postfix_expression
7046 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7047 (*vec)[1], tf_warning_or_error);
7048 else if (vec->length () == 3)
7049 postfix_expression
7050 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7051 (*vec)[2], tf_warning_or_error);
7052 else
7054 error_at (loc, "wrong number of arguments to "
7055 "%<__builtin_shuffle%>");
7056 postfix_expression = error_mark_node;
7058 break;
7060 default:
7061 gcc_unreachable ();
7063 break;
7066 case RID_BUILTIN_CONVERTVECTOR:
7068 tree expression;
7069 tree type;
7070 /* Consume the `__builtin_convertvector' token. */
7071 cp_lexer_consume_token (parser->lexer);
7072 /* Look for the opening `('. */
7073 matching_parens parens;
7074 parens.require_open (parser);
7075 /* Now, parse the assignment-expression. */
7076 expression = cp_parser_assignment_expression (parser);
7077 /* Look for the `,'. */
7078 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7079 location_t type_location
7080 = cp_lexer_peek_token (parser->lexer)->location;
7081 /* Parse the type-id. */
7083 type_id_in_expr_sentinel s (parser);
7084 type = cp_parser_type_id (parser);
7086 /* Look for the closing `)'. */
7087 parens.require_close (parser);
7088 return cp_build_vec_convert (expression, type_location, type,
7089 tf_warning_or_error);
7092 default:
7094 tree type;
7096 /* If the next thing is a simple-type-specifier, we may be
7097 looking at a functional cast. We could also be looking at
7098 an id-expression. So, we try the functional cast, and if
7099 that doesn't work we fall back to the primary-expression. */
7100 cp_parser_parse_tentatively (parser);
7101 /* Look for the simple-type-specifier. */
7102 ++parser->prevent_constrained_type_specifiers;
7103 type = cp_parser_simple_type_specifier (parser,
7104 /*decl_specs=*/NULL,
7105 CP_PARSER_FLAGS_NONE);
7106 --parser->prevent_constrained_type_specifiers;
7107 /* Parse the cast itself. */
7108 if (!cp_parser_error_occurred (parser))
7109 postfix_expression
7110 = cp_parser_functional_cast (parser, type);
7111 /* If that worked, we're done. */
7112 if (cp_parser_parse_definitely (parser))
7113 break;
7115 /* If the functional-cast didn't work out, try a
7116 compound-literal. */
7117 if (cp_parser_allow_gnu_extensions_p (parser)
7118 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7120 cp_expr initializer = NULL_TREE;
7122 cp_parser_parse_tentatively (parser);
7124 matching_parens parens;
7125 parens.consume_open (parser);
7127 /* Avoid calling cp_parser_type_id pointlessly, see comment
7128 in cp_parser_cast_expression about c++/29234. */
7129 if (!cp_parser_compound_literal_p (parser))
7130 cp_parser_simulate_error (parser);
7131 else
7133 /* Parse the type. */
7134 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7135 parser->in_type_id_in_expr_p = true;
7136 type = cp_parser_type_id (parser);
7137 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7138 parens.require_close (parser);
7141 /* If things aren't going well, there's no need to
7142 keep going. */
7143 if (!cp_parser_error_occurred (parser))
7145 bool non_constant_p;
7146 /* Parse the brace-enclosed initializer list. */
7147 initializer = cp_parser_braced_list (parser,
7148 &non_constant_p);
7150 /* If that worked, we're definitely looking at a
7151 compound-literal expression. */
7152 if (cp_parser_parse_definitely (parser))
7154 /* Warn the user that a compound literal is not
7155 allowed in standard C++. */
7156 pedwarn (input_location, OPT_Wpedantic,
7157 "ISO C++ forbids compound-literals");
7158 /* For simplicity, we disallow compound literals in
7159 constant-expressions. We could
7160 allow compound literals of integer type, whose
7161 initializer was a constant, in constant
7162 expressions. Permitting that usage, as a further
7163 extension, would not change the meaning of any
7164 currently accepted programs. (Of course, as
7165 compound literals are not part of ISO C++, the
7166 standard has nothing to say.) */
7167 if (cp_parser_non_integral_constant_expression (parser,
7168 NIC_NCC))
7170 postfix_expression = error_mark_node;
7171 break;
7173 /* Form the representation of the compound-literal. */
7174 postfix_expression
7175 = finish_compound_literal (type, initializer,
7176 tf_warning_or_error, fcl_c99);
7177 postfix_expression.set_location (initializer.get_location ());
7178 break;
7182 /* It must be a primary-expression. */
7183 postfix_expression
7184 = cp_parser_primary_expression (parser, address_p, cast_p,
7185 /*template_arg_p=*/false,
7186 decltype_p,
7187 &idk);
7189 break;
7192 /* Note that we don't need to worry about calling build_cplus_new on a
7193 class-valued CALL_EXPR in decltype when it isn't the end of the
7194 postfix-expression; unary_complex_lvalue will take care of that for
7195 all these cases. */
7197 /* Keep looping until the postfix-expression is complete. */
7198 while (true)
7200 if (idk == CP_ID_KIND_UNQUALIFIED
7201 && identifier_p (postfix_expression)
7202 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7203 /* It is not a Koenig lookup function call. */
7204 postfix_expression
7205 = unqualified_name_lookup_error (postfix_expression);
7207 /* Peek at the next token. */
7208 token = cp_lexer_peek_token (parser->lexer);
7210 switch (token->type)
7212 case CPP_OPEN_SQUARE:
7213 if (cp_next_tokens_can_be_std_attribute_p (parser))
7215 cp_parser_error (parser,
7216 "two consecutive %<[%> shall "
7217 "only introduce an attribute");
7218 return error_mark_node;
7220 postfix_expression
7221 = cp_parser_postfix_open_square_expression (parser,
7222 postfix_expression,
7223 false,
7224 decltype_p);
7225 postfix_expression.set_range (start_loc,
7226 postfix_expression.get_location ());
7228 idk = CP_ID_KIND_NONE;
7229 is_member_access = false;
7230 break;
7232 case CPP_OPEN_PAREN:
7233 /* postfix-expression ( expression-list [opt] ) */
7235 bool koenig_p;
7236 bool is_builtin_constant_p;
7237 bool saved_integral_constant_expression_p = false;
7238 bool saved_non_integral_constant_expression_p = false;
7239 tsubst_flags_t complain = complain_flags (decltype_p);
7240 vec<tree, va_gc> *args;
7241 location_t close_paren_loc = UNKNOWN_LOCATION;
7243 is_member_access = false;
7245 tree stripped_expression
7246 = tree_strip_any_location_wrapper (postfix_expression);
7247 is_builtin_constant_p
7248 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7249 if (is_builtin_constant_p)
7251 /* The whole point of __builtin_constant_p is to allow
7252 non-constant expressions to appear as arguments. */
7253 saved_integral_constant_expression_p
7254 = parser->integral_constant_expression_p;
7255 saved_non_integral_constant_expression_p
7256 = parser->non_integral_constant_expression_p;
7257 parser->integral_constant_expression_p = false;
7259 args = (cp_parser_parenthesized_expression_list
7260 (parser, non_attr,
7261 /*cast_p=*/false, /*allow_expansion_p=*/true,
7262 /*non_constant_p=*/NULL,
7263 /*close_paren_loc=*/&close_paren_loc,
7264 /*wrap_locations_p=*/true));
7265 if (is_builtin_constant_p)
7267 parser->integral_constant_expression_p
7268 = saved_integral_constant_expression_p;
7269 parser->non_integral_constant_expression_p
7270 = saved_non_integral_constant_expression_p;
7273 if (args == NULL)
7275 postfix_expression = error_mark_node;
7276 break;
7279 /* Function calls are not permitted in
7280 constant-expressions. */
7281 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7282 && cp_parser_non_integral_constant_expression (parser,
7283 NIC_FUNC_CALL))
7285 postfix_expression = error_mark_node;
7286 release_tree_vector (args);
7287 break;
7290 koenig_p = false;
7291 if (idk == CP_ID_KIND_UNQUALIFIED
7292 || idk == CP_ID_KIND_TEMPLATE_ID)
7294 if (identifier_p (postfix_expression)
7295 /* In C++2A, we may need to perform ADL for a template
7296 name. */
7297 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7298 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7300 if (!args->is_empty ())
7302 koenig_p = true;
7303 if (!any_type_dependent_arguments_p (args))
7304 postfix_expression
7305 = perform_koenig_lookup (postfix_expression, args,
7306 complain);
7308 else
7309 postfix_expression
7310 = unqualified_fn_lookup_error (postfix_expression);
7312 /* We do not perform argument-dependent lookup if
7313 normal lookup finds a non-function, in accordance
7314 with the expected resolution of DR 218. */
7315 else if (!args->is_empty ()
7316 && is_overloaded_fn (postfix_expression))
7318 /* We only need to look at the first function,
7319 because all the fns share the attribute we're
7320 concerned with (all member fns or all local
7321 fns). */
7322 tree fn = get_first_fn (postfix_expression);
7323 fn = STRIP_TEMPLATE (fn);
7325 /* Do not do argument dependent lookup if regular
7326 lookup finds a member function or a block-scope
7327 function declaration. [basic.lookup.argdep]/3 */
7328 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7329 || DECL_FUNCTION_MEMBER_P (fn)
7330 || DECL_LOCAL_FUNCTION_P (fn)))
7332 koenig_p = true;
7333 if (!any_type_dependent_arguments_p (args))
7334 postfix_expression
7335 = perform_koenig_lookup (postfix_expression, args,
7336 complain);
7341 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7343 tree instance = TREE_OPERAND (postfix_expression, 0);
7344 tree fn = TREE_OPERAND (postfix_expression, 1);
7346 if (processing_template_decl
7347 && (type_dependent_object_expression_p (instance)
7348 || (!BASELINK_P (fn)
7349 && TREE_CODE (fn) != FIELD_DECL)
7350 || type_dependent_expression_p (fn)
7351 || any_type_dependent_arguments_p (args)))
7353 maybe_generic_this_capture (instance, fn);
7354 postfix_expression
7355 = build_min_nt_call_vec (postfix_expression, args);
7356 release_tree_vector (args);
7357 break;
7360 if (BASELINK_P (fn))
7362 postfix_expression
7363 = (build_new_method_call
7364 (instance, fn, &args, NULL_TREE,
7365 (idk == CP_ID_KIND_QUALIFIED
7366 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7367 : LOOKUP_NORMAL),
7368 /*fn_p=*/NULL,
7369 complain));
7371 else
7372 postfix_expression
7373 = finish_call_expr (postfix_expression, &args,
7374 /*disallow_virtual=*/false,
7375 /*koenig_p=*/false,
7376 complain);
7378 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7379 || TREE_CODE (postfix_expression) == MEMBER_REF
7380 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7381 postfix_expression = (build_offset_ref_call_from_tree
7382 (postfix_expression, &args,
7383 complain));
7384 else if (idk == CP_ID_KIND_QUALIFIED)
7385 /* A call to a static class member, or a namespace-scope
7386 function. */
7387 postfix_expression
7388 = finish_call_expr (postfix_expression, &args,
7389 /*disallow_virtual=*/true,
7390 koenig_p,
7391 complain);
7392 else
7393 /* All other function calls. */
7394 postfix_expression
7395 = finish_call_expr (postfix_expression, &args,
7396 /*disallow_virtual=*/false,
7397 koenig_p,
7398 complain);
7400 if (close_paren_loc != UNKNOWN_LOCATION)
7402 location_t combined_loc = make_location (token->location,
7403 start_loc,
7404 close_paren_loc);
7405 postfix_expression.set_location (combined_loc);
7408 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7409 idk = CP_ID_KIND_NONE;
7411 release_tree_vector (args);
7413 break;
7415 case CPP_DOT:
7416 case CPP_DEREF:
7417 /* postfix-expression . template [opt] id-expression
7418 postfix-expression . pseudo-destructor-name
7419 postfix-expression -> template [opt] id-expression
7420 postfix-expression -> pseudo-destructor-name */
7422 /* Consume the `.' or `->' operator. */
7423 cp_lexer_consume_token (parser->lexer);
7425 postfix_expression
7426 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7427 postfix_expression,
7428 false, &idk, loc);
7430 is_member_access = true;
7431 break;
7433 case CPP_PLUS_PLUS:
7434 /* postfix-expression ++ */
7435 /* Consume the `++' token. */
7436 cp_lexer_consume_token (parser->lexer);
7437 /* Generate a representation for the complete expression. */
7438 postfix_expression
7439 = finish_increment_expr (postfix_expression,
7440 POSTINCREMENT_EXPR);
7441 /* Increments may not appear in constant-expressions. */
7442 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7443 postfix_expression = error_mark_node;
7444 idk = CP_ID_KIND_NONE;
7445 is_member_access = false;
7446 break;
7448 case CPP_MINUS_MINUS:
7449 /* postfix-expression -- */
7450 /* Consume the `--' token. */
7451 cp_lexer_consume_token (parser->lexer);
7452 /* Generate a representation for the complete expression. */
7453 postfix_expression
7454 = finish_increment_expr (postfix_expression,
7455 POSTDECREMENT_EXPR);
7456 /* Decrements may not appear in constant-expressions. */
7457 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7458 postfix_expression = error_mark_node;
7459 idk = CP_ID_KIND_NONE;
7460 is_member_access = false;
7461 break;
7463 default:
7464 if (pidk_return != NULL)
7465 * pidk_return = idk;
7466 if (member_access_only_p)
7467 return is_member_access
7468 ? postfix_expression
7469 : cp_expr (error_mark_node);
7470 else
7471 return postfix_expression;
7475 /* We should never get here. */
7476 gcc_unreachable ();
7477 return error_mark_node;
7480 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7481 by cp_parser_builtin_offsetof. We're looking for
7483 postfix-expression [ expression ]
7484 postfix-expression [ braced-init-list ] (C++11)
7486 FOR_OFFSETOF is set if we're being called in that context, which
7487 changes how we deal with integer constant expressions. */
7489 static tree
7490 cp_parser_postfix_open_square_expression (cp_parser *parser,
7491 tree postfix_expression,
7492 bool for_offsetof,
7493 bool decltype_p)
7495 tree index = NULL_TREE;
7496 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7497 bool saved_greater_than_is_operator_p;
7499 /* Consume the `[' token. */
7500 cp_lexer_consume_token (parser->lexer);
7502 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7503 parser->greater_than_is_operator_p = true;
7505 /* Parse the index expression. */
7506 /* ??? For offsetof, there is a question of what to allow here. If
7507 offsetof is not being used in an integral constant expression context,
7508 then we *could* get the right answer by computing the value at runtime.
7509 If we are in an integral constant expression context, then we might
7510 could accept any constant expression; hard to say without analysis.
7511 Rather than open the barn door too wide right away, allow only integer
7512 constant expressions here. */
7513 if (for_offsetof)
7514 index = cp_parser_constant_expression (parser);
7515 else
7517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7519 bool expr_nonconst_p;
7520 cp_lexer_set_source_position (parser->lexer);
7521 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7522 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7524 else
7525 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
7526 /*decltype_p=*/false,
7527 /*warn_comma_p=*/warn_comma_subscript);
7530 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7532 /* Look for the closing `]'. */
7533 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7535 /* Build the ARRAY_REF. */
7536 postfix_expression = grok_array_decl (loc, postfix_expression,
7537 index, decltype_p);
7539 /* When not doing offsetof, array references are not permitted in
7540 constant-expressions. */
7541 if (!for_offsetof
7542 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7543 postfix_expression = error_mark_node;
7545 return postfix_expression;
7548 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7549 dereference of incomplete type, returns true if error_mark_node should
7550 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7551 and *DEPENDENT_P. */
7553 bool
7554 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7555 bool *dependent_p)
7557 /* In a template, be permissive by treating an object expression
7558 of incomplete type as dependent (after a pedwarn). */
7559 diagnostic_t kind = (processing_template_decl
7560 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7562 switch (TREE_CODE (*postfix_expression))
7564 case CAST_EXPR:
7565 case REINTERPRET_CAST_EXPR:
7566 case CONST_CAST_EXPR:
7567 case STATIC_CAST_EXPR:
7568 case DYNAMIC_CAST_EXPR:
7569 case IMPLICIT_CONV_EXPR:
7570 case VIEW_CONVERT_EXPR:
7571 case NON_LVALUE_EXPR:
7572 kind = DK_ERROR;
7573 break;
7574 case OVERLOAD:
7575 /* Don't emit any diagnostic for OVERLOADs. */
7576 kind = DK_IGNORED;
7577 break;
7578 default:
7579 /* Avoid clobbering e.g. DECLs. */
7580 if (!EXPR_P (*postfix_expression))
7581 kind = DK_ERROR;
7582 break;
7585 if (kind == DK_IGNORED)
7586 return false;
7588 location_t exploc = location_of (*postfix_expression);
7589 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7590 if (!MAYBE_CLASS_TYPE_P (*scope))
7591 return true;
7592 if (kind == DK_ERROR)
7593 *scope = *postfix_expression = error_mark_node;
7594 else if (processing_template_decl)
7596 *dependent_p = true;
7597 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7599 return false;
7602 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7603 by cp_parser_builtin_offsetof. We're looking for
7605 postfix-expression . template [opt] id-expression
7606 postfix-expression . pseudo-destructor-name
7607 postfix-expression -> template [opt] id-expression
7608 postfix-expression -> pseudo-destructor-name
7610 FOR_OFFSETOF is set if we're being called in that context. That sorta
7611 limits what of the above we'll actually accept, but nevermind.
7612 TOKEN_TYPE is the "." or "->" token, which will already have been
7613 removed from the stream. */
7615 static tree
7616 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7617 enum cpp_ttype token_type,
7618 cp_expr postfix_expression,
7619 bool for_offsetof, cp_id_kind *idk,
7620 location_t location)
7622 tree name;
7623 bool dependent_p;
7624 bool pseudo_destructor_p;
7625 tree scope = NULL_TREE;
7626 location_t start_loc = postfix_expression.get_start ();
7628 /* If this is a `->' operator, dereference the pointer. */
7629 if (token_type == CPP_DEREF)
7630 postfix_expression = build_x_arrow (location, postfix_expression,
7631 tf_warning_or_error);
7632 /* Check to see whether or not the expression is type-dependent and
7633 not the current instantiation. */
7634 dependent_p = type_dependent_object_expression_p (postfix_expression);
7635 /* The identifier following the `->' or `.' is not qualified. */
7636 parser->scope = NULL_TREE;
7637 parser->qualifying_scope = NULL_TREE;
7638 parser->object_scope = NULL_TREE;
7639 *idk = CP_ID_KIND_NONE;
7641 /* Enter the scope corresponding to the type of the object
7642 given by the POSTFIX_EXPRESSION. */
7643 if (!dependent_p)
7645 scope = TREE_TYPE (postfix_expression);
7646 /* According to the standard, no expression should ever have
7647 reference type. Unfortunately, we do not currently match
7648 the standard in this respect in that our internal representation
7649 of an expression may have reference type even when the standard
7650 says it does not. Therefore, we have to manually obtain the
7651 underlying type here. */
7652 scope = non_reference (scope);
7653 /* The type of the POSTFIX_EXPRESSION must be complete. */
7654 /* Unlike the object expression in other contexts, *this is not
7655 required to be of complete type for purposes of class member
7656 access (5.2.5) outside the member function body. */
7657 if (postfix_expression != current_class_ref
7658 && scope != error_mark_node
7659 && !currently_open_class (scope))
7661 scope = complete_type (scope);
7662 if (!COMPLETE_TYPE_P (scope)
7663 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7664 &dependent_p))
7665 return error_mark_node;
7668 if (!dependent_p)
7670 /* Let the name lookup machinery know that we are processing a
7671 class member access expression. */
7672 parser->context->object_type = scope;
7673 /* If something went wrong, we want to be able to discern that case,
7674 as opposed to the case where there was no SCOPE due to the type
7675 of expression being dependent. */
7676 if (!scope)
7677 scope = error_mark_node;
7678 /* If the SCOPE was erroneous, make the various semantic analysis
7679 functions exit quickly -- and without issuing additional error
7680 messages. */
7681 if (scope == error_mark_node)
7682 postfix_expression = error_mark_node;
7686 if (dependent_p)
7687 /* Tell cp_parser_lookup_name that there was an object, even though it's
7688 type-dependent. */
7689 parser->context->object_type = unknown_type_node;
7691 /* Assume this expression is not a pseudo-destructor access. */
7692 pseudo_destructor_p = false;
7694 /* If the SCOPE is a scalar type, then, if this is a valid program,
7695 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7696 is type dependent, it can be pseudo-destructor-name or something else.
7697 Try to parse it as pseudo-destructor-name first. */
7698 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7700 tree s;
7701 tree type;
7703 cp_parser_parse_tentatively (parser);
7704 /* Parse the pseudo-destructor-name. */
7705 s = NULL_TREE;
7706 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7707 &s, &type);
7708 if (dependent_p
7709 && (cp_parser_error_occurred (parser)
7710 || !SCALAR_TYPE_P (type)))
7711 cp_parser_abort_tentative_parse (parser);
7712 else if (cp_parser_parse_definitely (parser))
7714 pseudo_destructor_p = true;
7715 postfix_expression
7716 = finish_pseudo_destructor_expr (postfix_expression,
7717 s, type, location);
7721 if (!pseudo_destructor_p)
7723 /* If the SCOPE is not a scalar type, we are looking at an
7724 ordinary class member access expression, rather than a
7725 pseudo-destructor-name. */
7726 bool template_p;
7727 cp_token *token = cp_lexer_peek_token (parser->lexer);
7728 /* Parse the id-expression. */
7729 name = (cp_parser_id_expression
7730 (parser,
7731 cp_parser_optional_template_keyword (parser),
7732 /*check_dependency_p=*/true,
7733 &template_p,
7734 /*declarator_p=*/false,
7735 /*optional_p=*/false));
7736 /* In general, build a SCOPE_REF if the member name is qualified.
7737 However, if the name was not dependent and has already been
7738 resolved; there is no need to build the SCOPE_REF. For example;
7740 struct X { void f(); };
7741 template <typename T> void f(T* t) { t->X::f(); }
7743 Even though "t" is dependent, "X::f" is not and has been resolved
7744 to a BASELINK; there is no need to include scope information. */
7746 /* But we do need to remember that there was an explicit scope for
7747 virtual function calls. */
7748 if (parser->scope)
7749 *idk = CP_ID_KIND_QUALIFIED;
7751 /* If the name is a template-id that names a type, we will get a
7752 TYPE_DECL here. That is invalid code. */
7753 if (TREE_CODE (name) == TYPE_DECL)
7755 error_at (token->location, "invalid use of %qD", name);
7756 postfix_expression = error_mark_node;
7758 else
7760 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7762 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7764 error_at (token->location, "%<%D::%D%> is not a class member",
7765 parser->scope, name);
7766 postfix_expression = error_mark_node;
7768 else
7769 name = build_qualified_name (/*type=*/NULL_TREE,
7770 parser->scope,
7771 name,
7772 template_p);
7773 parser->scope = NULL_TREE;
7774 parser->qualifying_scope = NULL_TREE;
7775 parser->object_scope = NULL_TREE;
7777 if (parser->scope && name && BASELINK_P (name))
7778 adjust_result_of_qualified_name_lookup
7779 (name, parser->scope, scope);
7780 postfix_expression
7781 = finish_class_member_access_expr (postfix_expression, name,
7782 template_p,
7783 tf_warning_or_error);
7784 /* Build a location e.g.:
7785 ptr->access_expr
7786 ~~~^~~~~~~~~~~~~
7787 where the caret is at the deref token, ranging from
7788 the start of postfix_expression to the end of the access expr. */
7789 location_t combined_loc
7790 = make_location (input_location, start_loc, parser->lexer);
7791 protected_set_expr_location (postfix_expression, combined_loc);
7795 /* We no longer need to look up names in the scope of the object on
7796 the left-hand side of the `.' or `->' operator. */
7797 parser->context->object_type = NULL_TREE;
7799 /* Outside of offsetof, these operators may not appear in
7800 constant-expressions. */
7801 if (!for_offsetof
7802 && (cp_parser_non_integral_constant_expression
7803 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7804 postfix_expression = error_mark_node;
7806 return postfix_expression;
7809 /* Parse a parenthesized expression-list.
7811 expression-list:
7812 assignment-expression
7813 expression-list, assignment-expression
7815 attribute-list:
7816 expression-list
7817 identifier
7818 identifier, expression-list
7820 CAST_P is true if this expression is the target of a cast.
7822 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7823 argument pack.
7825 WRAP_LOCATIONS_P is true if expressions within this list for which
7826 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7827 their source locations.
7829 Returns a vector of trees. Each element is a representation of an
7830 assignment-expression. NULL is returned if the ( and or ) are
7831 missing. An empty, but allocated, vector is returned on no
7832 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7833 if we are parsing an attribute list for an attribute that wants a
7834 plain identifier argument, normal_attr for an attribute that wants
7835 an expression, or non_attr if we aren't parsing an attribute list. If
7836 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7837 not all of the expressions in the list were constant.
7838 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7839 will be written to with the location of the closing parenthesis. If
7840 an error occurs, it may or may not be written to. */
7842 static vec<tree, va_gc> *
7843 cp_parser_parenthesized_expression_list (cp_parser* parser,
7844 int is_attribute_list,
7845 bool cast_p,
7846 bool allow_expansion_p,
7847 bool *non_constant_p,
7848 location_t *close_paren_loc,
7849 bool wrap_locations_p)
7851 vec<tree, va_gc> *expression_list;
7852 bool fold_expr_p = is_attribute_list != non_attr;
7853 tree identifier = NULL_TREE;
7854 bool saved_greater_than_is_operator_p;
7856 /* Assume all the expressions will be constant. */
7857 if (non_constant_p)
7858 *non_constant_p = false;
7860 matching_parens parens;
7861 if (!parens.require_open (parser))
7862 return NULL;
7864 expression_list = make_tree_vector ();
7866 /* Within a parenthesized expression, a `>' token is always
7867 the greater-than operator. */
7868 saved_greater_than_is_operator_p
7869 = parser->greater_than_is_operator_p;
7870 parser->greater_than_is_operator_p = true;
7872 cp_expr expr (NULL_TREE);
7874 /* Consume expressions until there are no more. */
7875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7876 while (true)
7878 /* At the beginning of attribute lists, check to see if the
7879 next token is an identifier. */
7880 if (is_attribute_list == id_attr
7881 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7883 cp_token *token;
7885 /* Consume the identifier. */
7886 token = cp_lexer_consume_token (parser->lexer);
7887 /* Save the identifier. */
7888 identifier = token->u.value;
7890 else
7892 bool expr_non_constant_p;
7894 /* Parse the next assignment-expression. */
7895 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7897 /* A braced-init-list. */
7898 cp_lexer_set_source_position (parser->lexer);
7899 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7900 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7901 if (non_constant_p && expr_non_constant_p)
7902 *non_constant_p = true;
7904 else if (non_constant_p)
7906 expr = (cp_parser_constant_expression
7907 (parser, /*allow_non_constant_p=*/true,
7908 &expr_non_constant_p));
7909 if (expr_non_constant_p)
7910 *non_constant_p = true;
7912 else
7913 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7914 cast_p);
7916 if (fold_expr_p)
7917 expr = instantiate_non_dependent_expr (expr);
7919 /* If we have an ellipsis, then this is an expression
7920 expansion. */
7921 if (allow_expansion_p
7922 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7924 /* Consume the `...'. */
7925 cp_lexer_consume_token (parser->lexer);
7927 /* Build the argument pack. */
7928 expr = make_pack_expansion (expr);
7931 if (wrap_locations_p)
7932 expr.maybe_add_location_wrapper ();
7934 /* Add it to the list. We add error_mark_node
7935 expressions to the list, so that we can still tell if
7936 the correct form for a parenthesized expression-list
7937 is found. That gives better errors. */
7938 vec_safe_push (expression_list, expr.get_value ());
7940 if (expr == error_mark_node)
7941 goto skip_comma;
7944 /* After the first item, attribute lists look the same as
7945 expression lists. */
7946 is_attribute_list = non_attr;
7948 get_comma:;
7949 /* If the next token isn't a `,', then we are done. */
7950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7951 break;
7953 /* Otherwise, consume the `,' and keep going. */
7954 cp_lexer_consume_token (parser->lexer);
7957 if (close_paren_loc)
7958 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7960 if (!parens.require_close (parser))
7962 int ending;
7964 skip_comma:;
7965 /* We try and resync to an unnested comma, as that will give the
7966 user better diagnostics. */
7967 ending = cp_parser_skip_to_closing_parenthesis (parser,
7968 /*recovering=*/true,
7969 /*or_comma=*/true,
7970 /*consume_paren=*/true);
7971 if (ending < 0)
7972 goto get_comma;
7973 if (!ending)
7975 parser->greater_than_is_operator_p
7976 = saved_greater_than_is_operator_p;
7977 return NULL;
7981 parser->greater_than_is_operator_p
7982 = saved_greater_than_is_operator_p;
7984 if (identifier)
7985 vec_safe_insert (expression_list, 0, identifier);
7987 return expression_list;
7990 /* Parse a pseudo-destructor-name.
7992 pseudo-destructor-name:
7993 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7994 :: [opt] nested-name-specifier template template-id :: ~ type-name
7995 :: [opt] nested-name-specifier [opt] ~ type-name
7997 If either of the first two productions is used, sets *SCOPE to the
7998 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7999 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8000 or ERROR_MARK_NODE if the parse fails. */
8002 static void
8003 cp_parser_pseudo_destructor_name (cp_parser* parser,
8004 tree object,
8005 tree* scope,
8006 tree* type)
8008 bool nested_name_specifier_p;
8010 /* Handle ~auto. */
8011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8012 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8013 && !type_dependent_expression_p (object))
8015 if (cxx_dialect < cxx14)
8016 pedwarn (input_location, 0,
8017 "%<~auto%> only available with "
8018 "%<-std=c++14%> or %<-std=gnu++14%>");
8019 cp_lexer_consume_token (parser->lexer);
8020 cp_lexer_consume_token (parser->lexer);
8021 *scope = NULL_TREE;
8022 *type = TREE_TYPE (object);
8023 return;
8026 /* Assume that things will not work out. */
8027 *type = error_mark_node;
8029 /* Look for the optional `::' operator. */
8030 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8031 /* Look for the optional nested-name-specifier. */
8032 nested_name_specifier_p
8033 = (cp_parser_nested_name_specifier_opt (parser,
8034 /*typename_keyword_p=*/false,
8035 /*check_dependency_p=*/true,
8036 /*type_p=*/false,
8037 /*is_declaration=*/false)
8038 != NULL_TREE);
8039 /* Now, if we saw a nested-name-specifier, we might be doing the
8040 second production. */
8041 if (nested_name_specifier_p
8042 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8044 /* Consume the `template' keyword. */
8045 cp_lexer_consume_token (parser->lexer);
8046 /* Parse the template-id. */
8047 cp_parser_template_id (parser,
8048 /*template_keyword_p=*/true,
8049 /*check_dependency_p=*/false,
8050 class_type,
8051 /*is_declaration=*/true);
8052 /* Look for the `::' token. */
8053 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8055 /* If the next token is not a `~', then there might be some
8056 additional qualification. */
8057 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8059 /* At this point, we're looking for "type-name :: ~". The type-name
8060 must not be a class-name, since this is a pseudo-destructor. So,
8061 it must be either an enum-name, or a typedef-name -- both of which
8062 are just identifiers. So, we peek ahead to check that the "::"
8063 and "~" tokens are present; if they are not, then we can avoid
8064 calling type_name. */
8065 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8066 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8067 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8069 cp_parser_error (parser, "non-scalar type");
8070 return;
8073 /* Look for the type-name. */
8074 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8075 if (*scope == error_mark_node)
8076 return;
8078 /* Look for the `::' token. */
8079 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8081 else
8082 *scope = NULL_TREE;
8084 /* Look for the `~'. */
8085 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8087 /* Once we see the ~, this has to be a pseudo-destructor. */
8088 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8089 cp_parser_commit_to_topmost_tentative_parse (parser);
8091 /* Look for the type-name again. We are not responsible for
8092 checking that it matches the first type-name. */
8093 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8096 /* Parse a unary-expression.
8098 unary-expression:
8099 postfix-expression
8100 ++ cast-expression
8101 -- cast-expression
8102 unary-operator cast-expression
8103 sizeof unary-expression
8104 sizeof ( type-id )
8105 alignof ( type-id ) [C++0x]
8106 new-expression
8107 delete-expression
8109 GNU Extensions:
8111 unary-expression:
8112 __extension__ cast-expression
8113 __alignof__ unary-expression
8114 __alignof__ ( type-id )
8115 alignof unary-expression [C++0x]
8116 __real__ cast-expression
8117 __imag__ cast-expression
8118 && identifier
8119 sizeof ( type-id ) { initializer-list , [opt] }
8120 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8121 __alignof__ ( type-id ) { initializer-list , [opt] }
8123 ADDRESS_P is true iff the unary-expression is appearing as the
8124 operand of the `&' operator. CAST_P is true if this expression is
8125 the target of a cast.
8127 Returns a representation of the expression. */
8129 static cp_expr
8130 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8131 bool address_p, bool cast_p, bool decltype_p)
8133 cp_token *token;
8134 enum tree_code unary_operator;
8136 /* Peek at the next token. */
8137 token = cp_lexer_peek_token (parser->lexer);
8138 /* Some keywords give away the kind of expression. */
8139 if (token->type == CPP_KEYWORD)
8141 enum rid keyword = token->keyword;
8143 switch (keyword)
8145 case RID_ALIGNOF:
8146 case RID_SIZEOF:
8148 tree operand, ret;
8149 enum tree_code op;
8150 location_t start_loc = token->location;
8152 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8153 bool std_alignof = id_equal (token->u.value, "alignof");
8155 /* Consume the token. */
8156 cp_lexer_consume_token (parser->lexer);
8157 /* Parse the operand. */
8158 operand = cp_parser_sizeof_operand (parser, keyword);
8160 if (TYPE_P (operand))
8161 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8162 true);
8163 else
8165 /* ISO C++ defines alignof only with types, not with
8166 expressions. So pedwarn if alignof is used with a non-
8167 type expression. However, __alignof__ is ok. */
8168 if (std_alignof)
8169 pedwarn (token->location, OPT_Wpedantic,
8170 "ISO C++ does not allow %<alignof%> "
8171 "with a non-type");
8173 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8175 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8176 SIZEOF_EXPR with the original operand. */
8177 if (op == SIZEOF_EXPR && ret != error_mark_node)
8179 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8181 if (!processing_template_decl && TYPE_P (operand))
8183 ret = build_min (SIZEOF_EXPR, size_type_node,
8184 build1 (NOP_EXPR, operand,
8185 error_mark_node));
8186 SIZEOF_EXPR_TYPE_P (ret) = 1;
8188 else
8189 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8190 TREE_SIDE_EFFECTS (ret) = 0;
8191 TREE_READONLY (ret) = 1;
8195 /* Construct a location e.g. :
8196 alignof (expr)
8197 ^~~~~~~~~~~~~~
8198 with start == caret at the start of the "alignof"/"sizeof"
8199 token, with the endpoint at the final closing paren. */
8200 location_t compound_loc
8201 = make_location (start_loc, start_loc, parser->lexer);
8203 cp_expr ret_expr (ret);
8204 ret_expr.set_location (compound_loc);
8205 ret_expr = ret_expr.maybe_add_location_wrapper ();
8206 return ret_expr;
8209 case RID_BUILTIN_HAS_ATTRIBUTE:
8210 return cp_parser_has_attribute_expression (parser);
8212 case RID_NEW:
8213 return cp_parser_new_expression (parser);
8215 case RID_DELETE:
8216 return cp_parser_delete_expression (parser);
8218 case RID_EXTENSION:
8220 /* The saved value of the PEDANTIC flag. */
8221 int saved_pedantic;
8222 tree expr;
8224 /* Save away the PEDANTIC flag. */
8225 cp_parser_extension_opt (parser, &saved_pedantic);
8226 /* Parse the cast-expression. */
8227 expr = cp_parser_simple_cast_expression (parser);
8228 /* Restore the PEDANTIC flag. */
8229 pedantic = saved_pedantic;
8231 return expr;
8234 case RID_REALPART:
8235 case RID_IMAGPART:
8237 tree expression;
8239 /* Consume the `__real__' or `__imag__' token. */
8240 cp_lexer_consume_token (parser->lexer);
8241 /* Parse the cast-expression. */
8242 expression = cp_parser_simple_cast_expression (parser);
8243 /* Create the complete representation. */
8244 return build_x_unary_op (token->location,
8245 (keyword == RID_REALPART
8246 ? REALPART_EXPR : IMAGPART_EXPR),
8247 expression,
8248 tf_warning_or_error);
8250 break;
8252 case RID_TRANSACTION_ATOMIC:
8253 case RID_TRANSACTION_RELAXED:
8254 return cp_parser_transaction_expression (parser, keyword);
8256 case RID_NOEXCEPT:
8258 tree expr;
8259 const char *saved_message;
8260 bool saved_integral_constant_expression_p;
8261 bool saved_non_integral_constant_expression_p;
8262 bool saved_greater_than_is_operator_p;
8264 location_t start_loc = token->location;
8266 cp_lexer_consume_token (parser->lexer);
8267 matching_parens parens;
8268 parens.require_open (parser);
8270 saved_message = parser->type_definition_forbidden_message;
8271 parser->type_definition_forbidden_message
8272 = G_("types may not be defined in %<noexcept%> expressions");
8274 saved_integral_constant_expression_p
8275 = parser->integral_constant_expression_p;
8276 saved_non_integral_constant_expression_p
8277 = parser->non_integral_constant_expression_p;
8278 parser->integral_constant_expression_p = false;
8280 saved_greater_than_is_operator_p
8281 = parser->greater_than_is_operator_p;
8282 parser->greater_than_is_operator_p = true;
8284 ++cp_unevaluated_operand;
8285 ++c_inhibit_evaluation_warnings;
8286 ++cp_noexcept_operand;
8287 expr = cp_parser_expression (parser);
8288 --cp_noexcept_operand;
8289 --c_inhibit_evaluation_warnings;
8290 --cp_unevaluated_operand;
8292 parser->greater_than_is_operator_p
8293 = saved_greater_than_is_operator_p;
8295 parser->integral_constant_expression_p
8296 = saved_integral_constant_expression_p;
8297 parser->non_integral_constant_expression_p
8298 = saved_non_integral_constant_expression_p;
8300 parser->type_definition_forbidden_message = saved_message;
8302 parens.require_close (parser);
8304 /* Construct a location of the form:
8305 noexcept (expr)
8306 ^~~~~~~~~~~~~~~
8307 with start == caret, finishing at the close-paren. */
8308 location_t noexcept_loc
8309 = make_location (start_loc, start_loc, parser->lexer);
8311 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8312 noexcept_loc);
8315 default:
8316 break;
8320 /* Look for the `:: new' and `:: delete', which also signal the
8321 beginning of a new-expression, or delete-expression,
8322 respectively. If the next token is `::', then it might be one of
8323 these. */
8324 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8326 enum rid keyword;
8328 /* See if the token after the `::' is one of the keywords in
8329 which we're interested. */
8330 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8331 /* If it's `new', we have a new-expression. */
8332 if (keyword == RID_NEW)
8333 return cp_parser_new_expression (parser);
8334 /* Similarly, for `delete'. */
8335 else if (keyword == RID_DELETE)
8336 return cp_parser_delete_expression (parser);
8339 /* Look for a unary operator. */
8340 unary_operator = cp_parser_unary_operator (token);
8341 /* The `++' and `--' operators can be handled similarly, even though
8342 they are not technically unary-operators in the grammar. */
8343 if (unary_operator == ERROR_MARK)
8345 if (token->type == CPP_PLUS_PLUS)
8346 unary_operator = PREINCREMENT_EXPR;
8347 else if (token->type == CPP_MINUS_MINUS)
8348 unary_operator = PREDECREMENT_EXPR;
8349 /* Handle the GNU address-of-label extension. */
8350 else if (cp_parser_allow_gnu_extensions_p (parser)
8351 && token->type == CPP_AND_AND)
8353 tree identifier;
8354 tree expression;
8355 location_t start_loc = token->location;
8357 /* Consume the '&&' token. */
8358 cp_lexer_consume_token (parser->lexer);
8359 /* Look for the identifier. */
8360 identifier = cp_parser_identifier (parser);
8361 /* Construct a location of the form:
8362 &&label
8363 ^~~~~~~
8364 with caret==start at the "&&", finish at the end of the label. */
8365 location_t combined_loc
8366 = make_location (start_loc, start_loc, parser->lexer);
8367 /* Create an expression representing the address. */
8368 expression = finish_label_address_expr (identifier, combined_loc);
8369 if (cp_parser_non_integral_constant_expression (parser,
8370 NIC_ADDR_LABEL))
8371 expression = error_mark_node;
8372 return expression;
8375 if (unary_operator != ERROR_MARK)
8377 cp_expr cast_expression;
8378 cp_expr expression = error_mark_node;
8379 non_integral_constant non_constant_p = NIC_NONE;
8380 location_t loc = token->location;
8381 tsubst_flags_t complain = complain_flags (decltype_p);
8383 /* Consume the operator token. */
8384 token = cp_lexer_consume_token (parser->lexer);
8385 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8387 /* Parse the cast-expression. */
8388 cast_expression
8389 = cp_parser_cast_expression (parser,
8390 unary_operator == ADDR_EXPR,
8391 /*cast_p=*/false,
8392 /*decltype*/false,
8393 pidk);
8395 /* Make a location:
8396 OP_TOKEN CAST_EXPRESSION
8397 ^~~~~~~~~~~~~~~~~~~~~~~~~
8398 with start==caret at the operator token, and
8399 extending to the end of the cast_expression. */
8400 loc = make_location (loc, loc, cast_expression.get_finish ());
8402 /* Now, build an appropriate representation. */
8403 switch (unary_operator)
8405 case INDIRECT_REF:
8406 non_constant_p = NIC_STAR;
8407 expression = build_x_indirect_ref (loc, cast_expression,
8408 RO_UNARY_STAR,
8409 complain);
8410 /* TODO: build_x_indirect_ref does not always honor the
8411 location, so ensure it is set. */
8412 expression.set_location (loc);
8413 break;
8415 case ADDR_EXPR:
8416 non_constant_p = NIC_ADDR;
8417 /* Fall through. */
8418 case BIT_NOT_EXPR:
8419 expression = build_x_unary_op (loc, unary_operator,
8420 cast_expression,
8421 complain);
8422 /* TODO: build_x_unary_op does not always honor the location,
8423 so ensure it is set. */
8424 expression.set_location (loc);
8425 break;
8427 case PREINCREMENT_EXPR:
8428 case PREDECREMENT_EXPR:
8429 non_constant_p = unary_operator == PREINCREMENT_EXPR
8430 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8431 /* Fall through. */
8432 case NEGATE_EXPR:
8433 /* Immediately fold negation of a constant, unless the constant is 0
8434 (since -0 == 0) or it would overflow. */
8435 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8437 tree stripped_expr
8438 = tree_strip_any_location_wrapper (cast_expression);
8439 if (CONSTANT_CLASS_P (stripped_expr)
8440 && !integer_zerop (stripped_expr)
8441 && !TREE_OVERFLOW (stripped_expr))
8443 tree folded = fold_build1 (unary_operator,
8444 TREE_TYPE (stripped_expr),
8445 stripped_expr);
8446 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8448 expression = maybe_wrap_with_location (folded, loc);
8449 break;
8453 /* Fall through. */
8454 case UNARY_PLUS_EXPR:
8455 case TRUTH_NOT_EXPR:
8456 expression = finish_unary_op_expr (loc, unary_operator,
8457 cast_expression, complain);
8458 break;
8460 default:
8461 gcc_unreachable ();
8464 if (non_constant_p != NIC_NONE
8465 && cp_parser_non_integral_constant_expression (parser,
8466 non_constant_p))
8467 expression = error_mark_node;
8469 return expression;
8472 return cp_parser_postfix_expression (parser, address_p, cast_p,
8473 /*member_access_only_p=*/false,
8474 decltype_p,
8475 pidk);
8478 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8479 unary-operator, the corresponding tree code is returned. */
8481 static enum tree_code
8482 cp_parser_unary_operator (cp_token* token)
8484 switch (token->type)
8486 case CPP_MULT:
8487 return INDIRECT_REF;
8489 case CPP_AND:
8490 return ADDR_EXPR;
8492 case CPP_PLUS:
8493 return UNARY_PLUS_EXPR;
8495 case CPP_MINUS:
8496 return NEGATE_EXPR;
8498 case CPP_NOT:
8499 return TRUTH_NOT_EXPR;
8501 case CPP_COMPL:
8502 return BIT_NOT_EXPR;
8504 default:
8505 return ERROR_MARK;
8509 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8510 Returns a representation of the expression. */
8512 static tree
8513 cp_parser_has_attribute_expression (cp_parser *parser)
8515 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8517 /* Consume the __builtin_has_attribute token. */
8518 cp_lexer_consume_token (parser->lexer);
8520 matching_parens parens;
8521 if (!parens.require_open (parser))
8522 return error_mark_node;
8524 /* Types cannot be defined in a `sizeof' expression. Save away the
8525 old message. */
8526 const char *saved_message = parser->type_definition_forbidden_message;
8527 const char *saved_message_arg
8528 = parser->type_definition_forbidden_message_arg;
8529 parser->type_definition_forbidden_message
8530 = G_("types may not be defined in %qs expressions");
8531 parser->type_definition_forbidden_message_arg
8532 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8534 /* The restrictions on constant-expressions do not apply inside
8535 sizeof expressions. */
8536 bool saved_integral_constant_expression_p
8537 = parser->integral_constant_expression_p;
8538 bool saved_non_integral_constant_expression_p
8539 = parser->non_integral_constant_expression_p;
8540 parser->integral_constant_expression_p = false;
8542 /* Do not actually evaluate the expression. */
8543 ++cp_unevaluated_operand;
8544 ++c_inhibit_evaluation_warnings;
8546 tree oper = NULL_TREE;
8548 /* We can't be sure yet whether we're looking at a type-id or an
8549 expression. */
8550 cp_parser_parse_tentatively (parser);
8552 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8553 parser->in_type_id_in_expr_p = true;
8554 /* Look for the type-id. */
8555 oper = cp_parser_type_id (parser);
8556 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8558 cp_parser_parse_definitely (parser);
8560 /* If the type-id production did not work out, then we must be
8561 looking at an expression. */
8562 if (!oper || oper == error_mark_node)
8563 oper = cp_parser_assignment_expression (parser);
8565 STRIP_ANY_LOCATION_WRAPPER (oper);
8567 /* Go back to evaluating expressions. */
8568 --cp_unevaluated_operand;
8569 --c_inhibit_evaluation_warnings;
8571 /* And restore the old one. */
8572 parser->type_definition_forbidden_message = saved_message;
8573 parser->type_definition_forbidden_message_arg = saved_message_arg;
8574 parser->integral_constant_expression_p
8575 = saved_integral_constant_expression_p;
8576 parser->non_integral_constant_expression_p
8577 = saved_non_integral_constant_expression_p;
8579 /* Consume the comma if it's there. */
8580 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8582 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8583 /*consume_paren=*/true);
8584 return error_mark_node;
8587 /* Parse the attribute specification. */
8588 bool ret = false;
8589 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8590 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8592 if (oper != error_mark_node)
8594 /* Fold constant expressions used in attributes first. */
8595 cp_check_const_attributes (attr);
8597 /* Finally, see if OPER has been declared with ATTR. */
8598 ret = has_attribute (atloc, oper, attr, default_conversion);
8601 parens.require_close (parser);
8603 else
8605 error_at (atloc, "expected identifier");
8606 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8609 /* Construct a location e.g. :
8610 __builtin_has_attribute (oper, attr)
8611 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8612 with start == caret at the start of the built-in token,
8613 and with the endpoint at the final closing paren. */
8614 location_t compound_loc
8615 = make_location (start_loc, start_loc, parser->lexer);
8617 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8618 ret_expr.set_location (compound_loc);
8619 ret_expr = ret_expr.maybe_add_location_wrapper ();
8620 return ret_expr;
8623 /* Parse a new-expression.
8625 new-expression:
8626 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8627 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8629 Returns a representation of the expression. */
8631 static tree
8632 cp_parser_new_expression (cp_parser* parser)
8634 bool global_scope_p;
8635 vec<tree, va_gc> *placement;
8636 tree type;
8637 vec<tree, va_gc> *initializer;
8638 tree nelts = NULL_TREE;
8639 tree ret;
8641 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8643 /* Look for the optional `::' operator. */
8644 global_scope_p
8645 = (cp_parser_global_scope_opt (parser,
8646 /*current_scope_valid_p=*/false)
8647 != NULL_TREE);
8648 /* Look for the `new' operator. */
8649 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8650 /* There's no easy way to tell a new-placement from the
8651 `( type-id )' construct. */
8652 cp_parser_parse_tentatively (parser);
8653 /* Look for a new-placement. */
8654 placement = cp_parser_new_placement (parser);
8655 /* If that didn't work out, there's no new-placement. */
8656 if (!cp_parser_parse_definitely (parser))
8658 if (placement != NULL)
8659 release_tree_vector (placement);
8660 placement = NULL;
8663 /* If the next token is a `(', then we have a parenthesized
8664 type-id. */
8665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8667 cp_token *token;
8668 const char *saved_message = parser->type_definition_forbidden_message;
8670 /* Consume the `('. */
8671 matching_parens parens;
8672 parens.consume_open (parser);
8674 /* Parse the type-id. */
8675 parser->type_definition_forbidden_message
8676 = G_("types may not be defined in a new-expression");
8678 type_id_in_expr_sentinel s (parser);
8679 type = cp_parser_type_id (parser);
8681 parser->type_definition_forbidden_message = saved_message;
8683 /* Look for the closing `)'. */
8684 parens.require_close (parser);
8685 token = cp_lexer_peek_token (parser->lexer);
8686 /* There should not be a direct-new-declarator in this production,
8687 but GCC used to allowed this, so we check and emit a sensible error
8688 message for this case. */
8689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8691 error_at (token->location,
8692 "array bound forbidden after parenthesized type-id");
8693 inform (token->location,
8694 "try removing the parentheses around the type-id");
8695 cp_parser_direct_new_declarator (parser);
8698 /* Otherwise, there must be a new-type-id. */
8699 else
8700 type = cp_parser_new_type_id (parser, &nelts);
8702 /* If the next token is a `(' or '{', then we have a new-initializer. */
8703 cp_token *token = cp_lexer_peek_token (parser->lexer);
8704 if (token->type == CPP_OPEN_PAREN
8705 || token->type == CPP_OPEN_BRACE)
8706 initializer = cp_parser_new_initializer (parser);
8707 else
8708 initializer = NULL;
8710 /* A new-expression may not appear in an integral constant
8711 expression. */
8712 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8713 ret = error_mark_node;
8714 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8715 of a new-type-id or type-id of a new-expression, the new-expression shall
8716 contain a new-initializer of the form ( assignment-expression )".
8717 Additionally, consistently with the spirit of DR 1467, we want to accept
8718 'new auto { 2 }' too. */
8719 else if ((ret = type_uses_auto (type))
8720 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8721 && (vec_safe_length (initializer) != 1
8722 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8723 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8725 error_at (token->location,
8726 "initialization of new-expression for type %<auto%> "
8727 "requires exactly one element");
8728 ret = error_mark_node;
8730 else
8732 /* Construct a location e.g.:
8733 ptr = new int[100]
8734 ^~~~~~~~~~~~
8735 with caret == start at the start of the "new" token, and the end
8736 at the end of the final token we consumed. */
8737 location_t combined_loc = make_location (start_loc, start_loc,
8738 parser->lexer);
8740 /* Create a representation of the new-expression. */
8741 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8742 tf_warning_or_error);
8743 protected_set_expr_location (ret, combined_loc);
8746 if (placement != NULL)
8747 release_tree_vector (placement);
8748 if (initializer != NULL)
8749 release_tree_vector (initializer);
8751 return ret;
8754 /* Parse a new-placement.
8756 new-placement:
8757 ( expression-list )
8759 Returns the same representation as for an expression-list. */
8761 static vec<tree, va_gc> *
8762 cp_parser_new_placement (cp_parser* parser)
8764 vec<tree, va_gc> *expression_list;
8766 /* Parse the expression-list. */
8767 expression_list = (cp_parser_parenthesized_expression_list
8768 (parser, non_attr, /*cast_p=*/false,
8769 /*allow_expansion_p=*/true,
8770 /*non_constant_p=*/NULL));
8772 if (expression_list && expression_list->is_empty ())
8773 error ("expected expression-list or type-id");
8775 return expression_list;
8778 /* Parse a new-type-id.
8780 new-type-id:
8781 type-specifier-seq new-declarator [opt]
8783 Returns the TYPE allocated. If the new-type-id indicates an array
8784 type, *NELTS is set to the number of elements in the last array
8785 bound; the TYPE will not include the last array bound. */
8787 static tree
8788 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8790 cp_decl_specifier_seq type_specifier_seq;
8791 cp_declarator *new_declarator;
8792 cp_declarator *declarator;
8793 cp_declarator *outer_declarator;
8794 const char *saved_message;
8796 /* The type-specifier sequence must not contain type definitions.
8797 (It cannot contain declarations of new types either, but if they
8798 are not definitions we will catch that because they are not
8799 complete.) */
8800 saved_message = parser->type_definition_forbidden_message;
8801 parser->type_definition_forbidden_message
8802 = G_("types may not be defined in a new-type-id");
8803 /* Parse the type-specifier-seq. */
8804 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8805 /*is_declaration=*/false,
8806 /*is_trailing_return=*/false,
8807 &type_specifier_seq);
8808 /* Restore the old message. */
8809 parser->type_definition_forbidden_message = saved_message;
8811 if (type_specifier_seq.type == error_mark_node)
8812 return error_mark_node;
8814 /* Parse the new-declarator. */
8815 new_declarator = cp_parser_new_declarator_opt (parser);
8817 /* Determine the number of elements in the last array dimension, if
8818 any. */
8819 *nelts = NULL_TREE;
8820 /* Skip down to the last array dimension. */
8821 declarator = new_declarator;
8822 outer_declarator = NULL;
8823 while (declarator && (declarator->kind == cdk_pointer
8824 || declarator->kind == cdk_ptrmem))
8826 outer_declarator = declarator;
8827 declarator = declarator->declarator;
8829 while (declarator
8830 && declarator->kind == cdk_array
8831 && declarator->declarator
8832 && declarator->declarator->kind == cdk_array)
8834 outer_declarator = declarator;
8835 declarator = declarator->declarator;
8838 if (declarator && declarator->kind == cdk_array)
8840 *nelts = declarator->u.array.bounds;
8841 if (*nelts == error_mark_node)
8842 *nelts = integer_one_node;
8844 if (outer_declarator)
8845 outer_declarator->declarator = declarator->declarator;
8846 else
8847 new_declarator = NULL;
8850 return groktypename (&type_specifier_seq, new_declarator, false);
8853 /* Parse an (optional) new-declarator.
8855 new-declarator:
8856 ptr-operator new-declarator [opt]
8857 direct-new-declarator
8859 Returns the declarator. */
8861 static cp_declarator *
8862 cp_parser_new_declarator_opt (cp_parser* parser)
8864 enum tree_code code;
8865 tree type, std_attributes = NULL_TREE;
8866 cp_cv_quals cv_quals;
8868 /* We don't know if there's a ptr-operator next, or not. */
8869 cp_parser_parse_tentatively (parser);
8870 /* Look for a ptr-operator. */
8871 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8872 /* If that worked, look for more new-declarators. */
8873 if (cp_parser_parse_definitely (parser))
8875 cp_declarator *declarator;
8877 /* Parse another optional declarator. */
8878 declarator = cp_parser_new_declarator_opt (parser);
8880 declarator = cp_parser_make_indirect_declarator
8881 (code, type, cv_quals, declarator, std_attributes);
8883 return declarator;
8886 /* If the next token is a `[', there is a direct-new-declarator. */
8887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8888 return cp_parser_direct_new_declarator (parser);
8890 return NULL;
8893 /* Parse a direct-new-declarator.
8895 direct-new-declarator:
8896 [ expression ]
8897 direct-new-declarator [constant-expression]
8901 static cp_declarator *
8902 cp_parser_direct_new_declarator (cp_parser* parser)
8904 cp_declarator *declarator = NULL;
8906 while (true)
8908 tree expression;
8909 cp_token *token;
8911 /* Look for the opening `['. */
8912 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8914 token = cp_lexer_peek_token (parser->lexer);
8915 expression = cp_parser_expression (parser);
8916 /* The standard requires that the expression have integral
8917 type. DR 74 adds enumeration types. We believe that the
8918 real intent is that these expressions be handled like the
8919 expression in a `switch' condition, which also allows
8920 classes with a single conversion to integral or
8921 enumeration type. */
8922 if (!processing_template_decl)
8924 expression
8925 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8926 expression,
8927 /*complain=*/true);
8928 if (!expression)
8930 error_at (token->location,
8931 "expression in new-declarator must have integral "
8932 "or enumeration type");
8933 expression = error_mark_node;
8937 /* Look for the closing `]'. */
8938 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8940 /* Add this bound to the declarator. */
8941 declarator = make_array_declarator (declarator, expression);
8943 /* If the next token is not a `[', then there are no more
8944 bounds. */
8945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8946 break;
8949 return declarator;
8952 /* Parse a new-initializer.
8954 new-initializer:
8955 ( expression-list [opt] )
8956 braced-init-list
8958 Returns a representation of the expression-list. */
8960 static vec<tree, va_gc> *
8961 cp_parser_new_initializer (cp_parser* parser)
8963 vec<tree, va_gc> *expression_list;
8965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8967 tree t;
8968 bool expr_non_constant_p;
8969 cp_lexer_set_source_position (parser->lexer);
8970 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8971 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8972 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8973 expression_list = make_tree_vector_single (t);
8975 else
8976 expression_list = (cp_parser_parenthesized_expression_list
8977 (parser, non_attr, /*cast_p=*/false,
8978 /*allow_expansion_p=*/true,
8979 /*non_constant_p=*/NULL));
8981 return expression_list;
8984 /* Parse a delete-expression.
8986 delete-expression:
8987 :: [opt] delete cast-expression
8988 :: [opt] delete [ ] cast-expression
8990 Returns a representation of the expression. */
8992 static tree
8993 cp_parser_delete_expression (cp_parser* parser)
8995 bool global_scope_p;
8996 bool array_p;
8997 tree expression;
8999 /* Look for the optional `::' operator. */
9000 global_scope_p
9001 = (cp_parser_global_scope_opt (parser,
9002 /*current_scope_valid_p=*/false)
9003 != NULL_TREE);
9004 /* Look for the `delete' keyword. */
9005 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9006 /* See if the array syntax is in use. */
9007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9009 /* Consume the `[' token. */
9010 cp_lexer_consume_token (parser->lexer);
9011 /* Look for the `]' token. */
9012 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9013 /* Remember that this is the `[]' construct. */
9014 array_p = true;
9016 else
9017 array_p = false;
9019 /* Parse the cast-expression. */
9020 expression = cp_parser_simple_cast_expression (parser);
9022 /* A delete-expression may not appear in an integral constant
9023 expression. */
9024 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9025 return error_mark_node;
9027 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9028 tf_warning_or_error);
9031 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9032 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9033 0 otherwise. */
9035 static int
9036 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9038 cp_token *token = cp_lexer_peek_token (parser->lexer);
9039 switch (token->type)
9041 case CPP_COMMA:
9042 case CPP_SEMICOLON:
9043 case CPP_QUERY:
9044 case CPP_COLON:
9045 case CPP_CLOSE_SQUARE:
9046 case CPP_CLOSE_PAREN:
9047 case CPP_CLOSE_BRACE:
9048 case CPP_OPEN_BRACE:
9049 case CPP_DOT:
9050 case CPP_DOT_STAR:
9051 case CPP_DEREF:
9052 case CPP_DEREF_STAR:
9053 case CPP_DIV:
9054 case CPP_MOD:
9055 case CPP_LSHIFT:
9056 case CPP_RSHIFT:
9057 case CPP_LESS:
9058 case CPP_GREATER:
9059 case CPP_LESS_EQ:
9060 case CPP_GREATER_EQ:
9061 case CPP_EQ_EQ:
9062 case CPP_NOT_EQ:
9063 case CPP_EQ:
9064 case CPP_MULT_EQ:
9065 case CPP_DIV_EQ:
9066 case CPP_MOD_EQ:
9067 case CPP_PLUS_EQ:
9068 case CPP_MINUS_EQ:
9069 case CPP_RSHIFT_EQ:
9070 case CPP_LSHIFT_EQ:
9071 case CPP_AND_EQ:
9072 case CPP_XOR_EQ:
9073 case CPP_OR_EQ:
9074 case CPP_XOR:
9075 case CPP_OR:
9076 case CPP_OR_OR:
9077 case CPP_EOF:
9078 case CPP_ELLIPSIS:
9079 return 0;
9081 case CPP_OPEN_PAREN:
9082 /* In ((type ()) () the last () isn't a valid cast-expression,
9083 so the whole must be parsed as postfix-expression. */
9084 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9085 != CPP_CLOSE_PAREN;
9087 case CPP_OPEN_SQUARE:
9088 /* '[' may start a primary-expression in obj-c++ and in C++11,
9089 as a lambda-expression, eg, '(void)[]{}'. */
9090 if (cxx_dialect >= cxx11)
9091 return -1;
9092 return c_dialect_objc ();
9094 case CPP_PLUS_PLUS:
9095 case CPP_MINUS_MINUS:
9096 /* '++' and '--' may or may not start a cast-expression:
9098 struct T { void operator++(int); };
9099 void f() { (T())++; }
9103 int a;
9104 (int)++a; */
9105 return -1;
9107 default:
9108 return 1;
9112 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9113 in the order: const_cast, static_cast, reinterpret_cast.
9115 Don't suggest dynamic_cast.
9117 Return the first legal cast kind found, or NULL otherwise. */
9119 static const char *
9120 get_cast_suggestion (tree dst_type, tree orig_expr)
9122 tree trial;
9124 /* Reuse the parser logic by attempting to build the various kinds of
9125 cast, with "complain" disabled.
9126 Identify the first such cast that is valid. */
9128 /* Don't attempt to run such logic within template processing. */
9129 if (processing_template_decl)
9130 return NULL;
9132 /* First try const_cast. */
9133 trial = build_const_cast (dst_type, orig_expr, tf_none);
9134 if (trial != error_mark_node)
9135 return "const_cast";
9137 /* If that fails, try static_cast. */
9138 trial = build_static_cast (dst_type, orig_expr, tf_none);
9139 if (trial != error_mark_node)
9140 return "static_cast";
9142 /* Finally, try reinterpret_cast. */
9143 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9144 if (trial != error_mark_node)
9145 return "reinterpret_cast";
9147 /* No such cast possible. */
9148 return NULL;
9151 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9152 suggesting how to convert a C-style cast of the form:
9154 (DST_TYPE)ORIG_EXPR
9156 to a C++-style cast.
9158 The primary range of RICHLOC is asssumed to be that of the original
9159 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9160 of the parens in the C-style cast. */
9162 static void
9163 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9164 location_t close_paren_loc, tree orig_expr,
9165 tree dst_type)
9167 /* This function is non-trivial, so bail out now if the warning isn't
9168 going to be emitted. */
9169 if (!warn_old_style_cast)
9170 return;
9172 /* Try to find a legal C++ cast, trying them in order:
9173 const_cast, static_cast, reinterpret_cast. */
9174 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9175 if (!cast_suggestion)
9176 return;
9178 /* Replace the open paren with "CAST_SUGGESTION<". */
9179 pretty_printer pp;
9180 pp_printf (&pp, "%s<", cast_suggestion);
9181 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9183 /* Replace the close paren with "> (". */
9184 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9186 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9187 rich_loc->add_fixit_insert_after (")");
9191 /* Parse a cast-expression.
9193 cast-expression:
9194 unary-expression
9195 ( type-id ) cast-expression
9197 ADDRESS_P is true iff the unary-expression is appearing as the
9198 operand of the `&' operator. CAST_P is true if this expression is
9199 the target of a cast.
9201 Returns a representation of the expression. */
9203 static cp_expr
9204 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9205 bool decltype_p, cp_id_kind * pidk)
9207 /* If it's a `(', then we might be looking at a cast. */
9208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9210 tree type = NULL_TREE;
9211 cp_expr expr (NULL_TREE);
9212 int cast_expression = 0;
9213 const char *saved_message;
9215 /* There's no way to know yet whether or not this is a cast.
9216 For example, `(int (3))' is a unary-expression, while `(int)
9217 3' is a cast. So, we resort to parsing tentatively. */
9218 cp_parser_parse_tentatively (parser);
9219 /* Types may not be defined in a cast. */
9220 saved_message = parser->type_definition_forbidden_message;
9221 parser->type_definition_forbidden_message
9222 = G_("types may not be defined in casts");
9223 /* Consume the `('. */
9224 matching_parens parens;
9225 cp_token *open_paren = parens.consume_open (parser);
9226 location_t open_paren_loc = open_paren->location;
9227 location_t close_paren_loc = UNKNOWN_LOCATION;
9229 /* A very tricky bit is that `(struct S) { 3 }' is a
9230 compound-literal (which we permit in C++ as an extension).
9231 But, that construct is not a cast-expression -- it is a
9232 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9233 is legal; if the compound-literal were a cast-expression,
9234 you'd need an extra set of parentheses.) But, if we parse
9235 the type-id, and it happens to be a class-specifier, then we
9236 will commit to the parse at that point, because we cannot
9237 undo the action that is done when creating a new class. So,
9238 then we cannot back up and do a postfix-expression.
9240 Another tricky case is the following (c++/29234):
9242 struct S { void operator () (); };
9244 void foo ()
9246 ( S()() );
9249 As a type-id we parse the parenthesized S()() as a function
9250 returning a function, groktypename complains and we cannot
9251 back up in this case either.
9253 Therefore, we scan ahead to the closing `)', and check to see
9254 if the tokens after the `)' can start a cast-expression. Otherwise
9255 we are dealing with an unary-expression, a postfix-expression
9256 or something else.
9258 Yet another tricky case, in C++11, is the following (c++/54891):
9260 (void)[]{};
9262 The issue is that usually, besides the case of lambda-expressions,
9263 the parenthesized type-id cannot be followed by '[', and, eg, we
9264 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9265 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9266 we don't commit, we try a cast-expression, then an unary-expression.
9268 Save tokens so that we can put them back. */
9269 cp_lexer_save_tokens (parser->lexer);
9271 /* We may be looking at a cast-expression. */
9272 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9273 /*consume_paren=*/true))
9274 cast_expression
9275 = cp_parser_tokens_start_cast_expression (parser);
9277 /* Roll back the tokens we skipped. */
9278 cp_lexer_rollback_tokens (parser->lexer);
9279 /* If we aren't looking at a cast-expression, simulate an error so
9280 that the call to cp_parser_error_occurred below returns true. */
9281 if (!cast_expression)
9282 cp_parser_simulate_error (parser);
9283 else
9285 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9286 parser->in_type_id_in_expr_p = true;
9287 /* Look for the type-id. */
9288 type = cp_parser_type_id (parser);
9289 /* Look for the closing `)'. */
9290 cp_token *close_paren = parens.require_close (parser);
9291 if (close_paren)
9292 close_paren_loc = close_paren->location;
9293 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9296 /* Restore the saved message. */
9297 parser->type_definition_forbidden_message = saved_message;
9299 /* At this point this can only be either a cast or a
9300 parenthesized ctor such as `(T ())' that looks like a cast to
9301 function returning T. */
9302 if (!cp_parser_error_occurred (parser))
9304 /* Only commit if the cast-expression doesn't start with
9305 '++', '--', or '[' in C++11. */
9306 if (cast_expression > 0)
9307 cp_parser_commit_to_topmost_tentative_parse (parser);
9309 expr = cp_parser_cast_expression (parser,
9310 /*address_p=*/false,
9311 /*cast_p=*/true,
9312 /*decltype_p=*/false,
9313 pidk);
9315 if (cp_parser_parse_definitely (parser))
9317 /* Warn about old-style casts, if so requested. */
9318 if (warn_old_style_cast
9319 && !in_system_header_at (input_location)
9320 && !VOID_TYPE_P (type)
9321 && current_lang_name != lang_name_c)
9323 gcc_rich_location rich_loc (input_location);
9324 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9325 expr, type);
9326 warning_at (&rich_loc, OPT_Wold_style_cast,
9327 "use of old-style cast to %q#T", type);
9330 /* Only type conversions to integral or enumeration types
9331 can be used in constant-expressions. */
9332 if (!cast_valid_in_integral_constant_expression_p (type)
9333 && cp_parser_non_integral_constant_expression (parser,
9334 NIC_CAST))
9335 return error_mark_node;
9337 /* Perform the cast. */
9338 /* Make a location:
9339 (TYPE) EXPR
9340 ^~~~~~~~~~~
9341 with start==caret at the open paren, extending to the
9342 end of "expr". */
9343 location_t cast_loc = make_location (open_paren_loc,
9344 open_paren_loc,
9345 expr.get_finish ());
9346 expr = build_c_cast (cast_loc, type, expr);
9347 return expr;
9350 else
9351 cp_parser_abort_tentative_parse (parser);
9354 /* If we get here, then it's not a cast, so it must be a
9355 unary-expression. */
9356 return cp_parser_unary_expression (parser, pidk, address_p,
9357 cast_p, decltype_p);
9360 /* Parse a binary expression of the general form:
9362 pm-expression:
9363 cast-expression
9364 pm-expression .* cast-expression
9365 pm-expression ->* cast-expression
9367 multiplicative-expression:
9368 pm-expression
9369 multiplicative-expression * pm-expression
9370 multiplicative-expression / pm-expression
9371 multiplicative-expression % pm-expression
9373 additive-expression:
9374 multiplicative-expression
9375 additive-expression + multiplicative-expression
9376 additive-expression - multiplicative-expression
9378 shift-expression:
9379 additive-expression
9380 shift-expression << additive-expression
9381 shift-expression >> additive-expression
9383 relational-expression:
9384 shift-expression
9385 relational-expression < shift-expression
9386 relational-expression > shift-expression
9387 relational-expression <= shift-expression
9388 relational-expression >= shift-expression
9390 GNU Extension:
9392 relational-expression:
9393 relational-expression <? shift-expression
9394 relational-expression >? shift-expression
9396 equality-expression:
9397 relational-expression
9398 equality-expression == relational-expression
9399 equality-expression != relational-expression
9401 and-expression:
9402 equality-expression
9403 and-expression & equality-expression
9405 exclusive-or-expression:
9406 and-expression
9407 exclusive-or-expression ^ and-expression
9409 inclusive-or-expression:
9410 exclusive-or-expression
9411 inclusive-or-expression | exclusive-or-expression
9413 logical-and-expression:
9414 inclusive-or-expression
9415 logical-and-expression && inclusive-or-expression
9417 logical-or-expression:
9418 logical-and-expression
9419 logical-or-expression || logical-and-expression
9421 All these are implemented with a single function like:
9423 binary-expression:
9424 simple-cast-expression
9425 binary-expression <token> binary-expression
9427 CAST_P is true if this expression is the target of a cast.
9429 The binops_by_token map is used to get the tree codes for each <token> type.
9430 binary-expressions are associated according to a precedence table. */
9432 #define TOKEN_PRECEDENCE(token) \
9433 (((token->type == CPP_GREATER \
9434 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9435 && !parser->greater_than_is_operator_p) \
9436 ? PREC_NOT_OPERATOR \
9437 : binops_by_token[token->type].prec)
9439 static cp_expr
9440 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9441 bool no_toplevel_fold_p,
9442 bool decltype_p,
9443 enum cp_parser_prec prec,
9444 cp_id_kind * pidk)
9446 cp_parser_expression_stack stack;
9447 cp_parser_expression_stack_entry *sp = &stack[0];
9448 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9449 cp_parser_expression_stack_entry current;
9450 cp_expr rhs;
9451 cp_token *token;
9452 enum tree_code rhs_type;
9453 enum cp_parser_prec new_prec, lookahead_prec;
9454 tree overload;
9456 /* Parse the first expression. */
9457 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9458 ? TRUTH_NOT_EXPR : ERROR_MARK);
9459 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9460 cast_p, decltype_p, pidk);
9461 current.prec = prec;
9463 if (cp_parser_error_occurred (parser))
9464 return error_mark_node;
9466 for (;;)
9468 /* Get an operator token. */
9469 token = cp_lexer_peek_token (parser->lexer);
9471 if (warn_cxx11_compat
9472 && token->type == CPP_RSHIFT
9473 && !parser->greater_than_is_operator_p)
9475 if (warning_at (token->location, OPT_Wc__11_compat,
9476 "%<>>%> operator is treated"
9477 " as two right angle brackets in C++11"))
9478 inform (token->location,
9479 "suggest parentheses around %<>>%> expression");
9482 new_prec = TOKEN_PRECEDENCE (token);
9483 if (new_prec != PREC_NOT_OPERATOR
9484 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9485 /* This is a fold-expression; handle it later. */
9486 new_prec = PREC_NOT_OPERATOR;
9488 /* Popping an entry off the stack means we completed a subexpression:
9489 - either we found a token which is not an operator (`>' where it is not
9490 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9491 will happen repeatedly;
9492 - or, we found an operator which has lower priority. This is the case
9493 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9494 parsing `3 * 4'. */
9495 if (new_prec <= current.prec)
9497 if (sp == stack)
9498 break;
9499 else
9500 goto pop;
9503 get_rhs:
9504 current.tree_type = binops_by_token[token->type].tree_type;
9505 current.loc = token->location;
9507 /* We used the operator token. */
9508 cp_lexer_consume_token (parser->lexer);
9510 /* For "false && x" or "true || x", x will never be executed;
9511 disable warnings while evaluating it. */
9512 if ((current.tree_type == TRUTH_ANDIF_EXPR
9513 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9514 || (current.tree_type == TRUTH_ORIF_EXPR
9515 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9517 disable_warnings_sp = sp;
9518 ++c_inhibit_evaluation_warnings;
9521 /* Extract another operand. It may be the RHS of this expression
9522 or the LHS of a new, higher priority expression. */
9523 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9524 ? TRUTH_NOT_EXPR : ERROR_MARK);
9525 rhs = cp_parser_simple_cast_expression (parser);
9527 /* Get another operator token. Look up its precedence to avoid
9528 building a useless (immediately popped) stack entry for common
9529 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9530 token = cp_lexer_peek_token (parser->lexer);
9531 lookahead_prec = TOKEN_PRECEDENCE (token);
9532 if (lookahead_prec != PREC_NOT_OPERATOR
9533 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9534 lookahead_prec = PREC_NOT_OPERATOR;
9535 if (lookahead_prec > new_prec)
9537 /* ... and prepare to parse the RHS of the new, higher priority
9538 expression. Since precedence levels on the stack are
9539 monotonically increasing, we do not have to care about
9540 stack overflows. */
9541 *sp = current;
9542 ++sp;
9543 current.lhs = rhs;
9544 current.lhs_type = rhs_type;
9545 current.prec = new_prec;
9546 new_prec = lookahead_prec;
9547 goto get_rhs;
9549 pop:
9550 lookahead_prec = new_prec;
9551 /* If the stack is not empty, we have parsed into LHS the right side
9552 (`4' in the example above) of an expression we had suspended.
9553 We can use the information on the stack to recover the LHS (`3')
9554 from the stack together with the tree code (`MULT_EXPR'), and
9555 the precedence of the higher level subexpression
9556 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9557 which will be used to actually build the additive expression. */
9558 rhs = current.lhs;
9559 rhs_type = current.lhs_type;
9560 --sp;
9561 current = *sp;
9564 /* Undo the disabling of warnings done above. */
9565 if (sp == disable_warnings_sp)
9567 disable_warnings_sp = NULL;
9568 --c_inhibit_evaluation_warnings;
9571 if (warn_logical_not_paren
9572 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9573 && current.lhs_type == TRUTH_NOT_EXPR
9574 /* Avoid warning for !!x == y. */
9575 && (TREE_CODE (current.lhs) != NE_EXPR
9576 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9577 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9578 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9579 /* Avoid warning for !b == y where b is boolean. */
9580 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9581 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9582 != BOOLEAN_TYPE))))
9583 /* Avoid warning for !!b == y where b is boolean. */
9584 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9585 || TREE_TYPE (current.lhs) == NULL_TREE
9586 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9587 warn_logical_not_parentheses (current.loc, current.tree_type,
9588 current.lhs, maybe_constant_value (rhs));
9590 overload = NULL;
9592 location_t combined_loc = make_location (current.loc,
9593 current.lhs.get_start (),
9594 rhs.get_finish ());
9596 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9597 ERROR_MARK for everything that is not a binary expression.
9598 This makes warn_about_parentheses miss some warnings that
9599 involve unary operators. For unary expressions we should
9600 pass the correct tree_code unless the unary expression was
9601 surrounded by parentheses.
9603 if (no_toplevel_fold_p
9604 && lookahead_prec <= current.prec
9605 && sp == stack)
9607 if (current.lhs == error_mark_node || rhs == error_mark_node)
9608 current.lhs = error_mark_node;
9609 else
9611 current.lhs
9612 = build_min (current.tree_type,
9613 TREE_CODE_CLASS (current.tree_type)
9614 == tcc_comparison
9615 ? boolean_type_node : TREE_TYPE (current.lhs),
9616 current.lhs.get_value (), rhs.get_value ());
9617 SET_EXPR_LOCATION (current.lhs, combined_loc);
9620 else
9622 op_location_t op_loc (current.loc, combined_loc);
9623 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9624 current.lhs, current.lhs_type,
9625 rhs, rhs_type, &overload,
9626 complain_flags (decltype_p));
9627 /* TODO: build_x_binary_op doesn't always honor the location. */
9628 current.lhs.set_location (combined_loc);
9630 current.lhs_type = current.tree_type;
9632 /* If the binary operator required the use of an overloaded operator,
9633 then this expression cannot be an integral constant-expression.
9634 An overloaded operator can be used even if both operands are
9635 otherwise permissible in an integral constant-expression if at
9636 least one of the operands is of enumeration type. */
9638 if (overload
9639 && cp_parser_non_integral_constant_expression (parser,
9640 NIC_OVERLOADED))
9641 return error_mark_node;
9644 return current.lhs;
9647 static cp_expr
9648 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9649 bool no_toplevel_fold_p,
9650 enum cp_parser_prec prec,
9651 cp_id_kind * pidk)
9653 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9654 /*decltype*/false, prec, pidk);
9657 /* Parse the `? expression : assignment-expression' part of a
9658 conditional-expression. The LOGICAL_OR_EXPR is the
9659 logical-or-expression that started the conditional-expression.
9660 Returns a representation of the entire conditional-expression.
9662 This routine is used by cp_parser_assignment_expression.
9664 ? expression : assignment-expression
9666 GNU Extensions:
9668 ? : assignment-expression */
9670 static tree
9671 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9673 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9674 cp_expr assignment_expr;
9675 struct cp_token *token;
9676 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9678 /* Consume the `?' token. */
9679 cp_lexer_consume_token (parser->lexer);
9680 token = cp_lexer_peek_token (parser->lexer);
9681 if (cp_parser_allow_gnu_extensions_p (parser)
9682 && token->type == CPP_COLON)
9684 pedwarn (token->location, OPT_Wpedantic,
9685 "ISO C++ does not allow %<?:%> with omitted middle operand");
9686 /* Implicit true clause. */
9687 expr = NULL_TREE;
9688 c_inhibit_evaluation_warnings +=
9689 folded_logical_or_expr == truthvalue_true_node;
9690 warn_for_omitted_condop (token->location, logical_or_expr);
9692 else
9694 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9695 parser->colon_corrects_to_scope_p = false;
9696 /* Parse the expression. */
9697 c_inhibit_evaluation_warnings +=
9698 folded_logical_or_expr == truthvalue_false_node;
9699 expr = cp_parser_expression (parser);
9700 c_inhibit_evaluation_warnings +=
9701 ((folded_logical_or_expr == truthvalue_true_node)
9702 - (folded_logical_or_expr == truthvalue_false_node));
9703 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9706 /* The next token should be a `:'. */
9707 cp_parser_require (parser, CPP_COLON, RT_COLON);
9708 /* Parse the assignment-expression. */
9709 assignment_expr = cp_parser_assignment_expression (parser);
9710 c_inhibit_evaluation_warnings -=
9711 folded_logical_or_expr == truthvalue_true_node;
9713 /* Make a location:
9714 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9715 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9716 with the caret at the "?", ranging from the start of
9717 the logical_or_expr to the end of the assignment_expr. */
9718 loc = make_location (loc,
9719 logical_or_expr.get_start (),
9720 assignment_expr.get_finish ());
9722 /* Build the conditional-expression. */
9723 return build_x_conditional_expr (loc, logical_or_expr,
9724 expr,
9725 assignment_expr,
9726 tf_warning_or_error);
9729 /* Parse an assignment-expression.
9731 assignment-expression:
9732 conditional-expression
9733 logical-or-expression assignment-operator assignment_expression
9734 throw-expression
9736 CAST_P is true if this expression is the target of a cast.
9737 DECLTYPE_P is true if this expression is the operand of decltype.
9739 Returns a representation for the expression. */
9741 static cp_expr
9742 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9743 bool cast_p, bool decltype_p)
9745 cp_expr expr;
9747 /* If the next token is the `throw' keyword, then we're looking at
9748 a throw-expression. */
9749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9750 expr = cp_parser_throw_expression (parser);
9751 /* Otherwise, it must be that we are looking at a
9752 logical-or-expression. */
9753 else
9755 /* Parse the binary expressions (logical-or-expression). */
9756 expr = cp_parser_binary_expression (parser, cast_p, false,
9757 decltype_p,
9758 PREC_NOT_OPERATOR, pidk);
9759 /* If the next token is a `?' then we're actually looking at a
9760 conditional-expression. */
9761 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9762 return cp_parser_question_colon_clause (parser, expr);
9763 else
9765 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9767 /* If it's an assignment-operator, we're using the second
9768 production. */
9769 enum tree_code assignment_operator
9770 = cp_parser_assignment_operator_opt (parser);
9771 if (assignment_operator != ERROR_MARK)
9773 bool non_constant_p;
9775 /* Parse the right-hand side of the assignment. */
9776 cp_expr rhs = cp_parser_initializer_clause (parser,
9777 &non_constant_p);
9779 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9780 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9782 /* An assignment may not appear in a
9783 constant-expression. */
9784 if (cp_parser_non_integral_constant_expression (parser,
9785 NIC_ASSIGNMENT))
9786 return error_mark_node;
9787 /* Build the assignment expression. Its default
9788 location:
9789 LHS = RHS
9790 ~~~~^~~~~
9791 is the location of the '=' token as the
9792 caret, ranging from the start of the lhs to the
9793 end of the rhs. */
9794 loc = make_location (loc,
9795 expr.get_start (),
9796 rhs.get_finish ());
9797 expr = build_x_modify_expr (loc, expr,
9798 assignment_operator,
9799 rhs,
9800 complain_flags (decltype_p));
9801 /* TODO: build_x_modify_expr doesn't honor the location,
9802 so we must set it here. */
9803 expr.set_location (loc);
9808 return expr;
9811 /* Parse an (optional) assignment-operator.
9813 assignment-operator: one of
9814 = *= /= %= += -= >>= <<= &= ^= |=
9816 GNU Extension:
9818 assignment-operator: one of
9819 <?= >?=
9821 If the next token is an assignment operator, the corresponding tree
9822 code is returned, and the token is consumed. For example, for
9823 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9824 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9825 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9826 operator, ERROR_MARK is returned. */
9828 static enum tree_code
9829 cp_parser_assignment_operator_opt (cp_parser* parser)
9831 enum tree_code op;
9832 cp_token *token;
9834 /* Peek at the next token. */
9835 token = cp_lexer_peek_token (parser->lexer);
9837 switch (token->type)
9839 case CPP_EQ:
9840 op = NOP_EXPR;
9841 break;
9843 case CPP_MULT_EQ:
9844 op = MULT_EXPR;
9845 break;
9847 case CPP_DIV_EQ:
9848 op = TRUNC_DIV_EXPR;
9849 break;
9851 case CPP_MOD_EQ:
9852 op = TRUNC_MOD_EXPR;
9853 break;
9855 case CPP_PLUS_EQ:
9856 op = PLUS_EXPR;
9857 break;
9859 case CPP_MINUS_EQ:
9860 op = MINUS_EXPR;
9861 break;
9863 case CPP_RSHIFT_EQ:
9864 op = RSHIFT_EXPR;
9865 break;
9867 case CPP_LSHIFT_EQ:
9868 op = LSHIFT_EXPR;
9869 break;
9871 case CPP_AND_EQ:
9872 op = BIT_AND_EXPR;
9873 break;
9875 case CPP_XOR_EQ:
9876 op = BIT_XOR_EXPR;
9877 break;
9879 case CPP_OR_EQ:
9880 op = BIT_IOR_EXPR;
9881 break;
9883 default:
9884 /* Nothing else is an assignment operator. */
9885 op = ERROR_MARK;
9888 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9889 if (op != ERROR_MARK
9890 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9891 op = ERROR_MARK;
9893 /* If it was an assignment operator, consume it. */
9894 if (op != ERROR_MARK)
9895 cp_lexer_consume_token (parser->lexer);
9897 return op;
9900 /* Parse an expression.
9902 expression:
9903 assignment-expression
9904 expression , assignment-expression
9906 CAST_P is true if this expression is the target of a cast.
9907 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9908 except possibly parenthesized or on the RHS of a comma (N3276).
9909 WARN_COMMA_P is true if a comma should be diagnosed.
9911 Returns a representation of the expression. */
9913 static cp_expr
9914 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9915 bool cast_p, bool decltype_p, bool warn_comma_p)
9917 cp_expr expression = NULL_TREE;
9918 location_t loc = UNKNOWN_LOCATION;
9920 while (true)
9922 cp_expr assignment_expression;
9924 /* Parse the next assignment-expression. */
9925 assignment_expression
9926 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9928 /* We don't create a temporary for a call that is the immediate operand
9929 of decltype or on the RHS of a comma. But when we see a comma, we
9930 need to create a temporary for a call on the LHS. */
9931 if (decltype_p && !processing_template_decl
9932 && TREE_CODE (assignment_expression) == CALL_EXPR
9933 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9934 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9935 assignment_expression
9936 = build_cplus_new (TREE_TYPE (assignment_expression),
9937 assignment_expression, tf_warning_or_error);
9939 /* If this is the first assignment-expression, we can just
9940 save it away. */
9941 if (!expression)
9942 expression = assignment_expression;
9943 else
9945 /* Create a location with caret at the comma, ranging
9946 from the start of the LHS to the end of the RHS. */
9947 loc = make_location (loc,
9948 expression.get_start (),
9949 assignment_expression.get_finish ());
9950 expression = build_x_compound_expr (loc, expression,
9951 assignment_expression,
9952 complain_flags (decltype_p));
9953 expression.set_location (loc);
9955 /* If the next token is not a comma, or we're in a fold-expression, then
9956 we are done with the expression. */
9957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9958 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9959 break;
9960 /* Consume the `,'. */
9961 loc = cp_lexer_peek_token (parser->lexer)->location;
9962 if (warn_comma_p)
9964 /* [depr.comma.subscript]: A comma expression appearing as
9965 the expr-or-braced-init-list of a subscripting expression
9966 is deprecated. A parenthesized comma expression is not
9967 deprecated. */
9968 warning_at (loc, OPT_Wcomma_subscript,
9969 "top-level comma expression in array subscript "
9970 "is deprecated");
9971 warn_comma_p = false;
9973 cp_lexer_consume_token (parser->lexer);
9974 /* A comma operator cannot appear in a constant-expression. */
9975 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9976 expression = error_mark_node;
9979 return expression;
9982 /* Parse a constant-expression.
9984 constant-expression:
9985 conditional-expression
9987 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9988 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9989 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9990 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9991 only parse a conditional-expression, otherwise parse an
9992 assignment-expression. See below for rationale. */
9994 static cp_expr
9995 cp_parser_constant_expression (cp_parser* parser,
9996 bool allow_non_constant_p,
9997 bool *non_constant_p,
9998 bool strict_p)
10000 bool saved_integral_constant_expression_p;
10001 bool saved_allow_non_integral_constant_expression_p;
10002 bool saved_non_integral_constant_expression_p;
10003 cp_expr expression;
10005 /* It might seem that we could simply parse the
10006 conditional-expression, and then check to see if it were
10007 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10008 one that the compiler can figure out is constant, possibly after
10009 doing some simplifications or optimizations. The standard has a
10010 precise definition of constant-expression, and we must honor
10011 that, even though it is somewhat more restrictive.
10013 For example:
10015 int i[(2, 3)];
10017 is not a legal declaration, because `(2, 3)' is not a
10018 constant-expression. The `,' operator is forbidden in a
10019 constant-expression. However, GCC's constant-folding machinery
10020 will fold this operation to an INTEGER_CST for `3'. */
10022 /* Save the old settings. */
10023 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10024 saved_allow_non_integral_constant_expression_p
10025 = parser->allow_non_integral_constant_expression_p;
10026 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10027 /* We are now parsing a constant-expression. */
10028 parser->integral_constant_expression_p = true;
10029 parser->allow_non_integral_constant_expression_p
10030 = (allow_non_constant_p || cxx_dialect >= cxx11);
10031 parser->non_integral_constant_expression_p = false;
10032 /* Although the grammar says "conditional-expression", when not STRICT_P,
10033 we parse an "assignment-expression", which also permits
10034 "throw-expression" and the use of assignment operators. In the case
10035 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10036 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10037 actually essential that we look for an assignment-expression.
10038 For example, cp_parser_initializer_clauses uses this function to
10039 determine whether a particular assignment-expression is in fact
10040 constant. */
10041 if (strict_p)
10043 /* Parse the binary expressions (logical-or-expression). */
10044 expression = cp_parser_binary_expression (parser, false, false, false,
10045 PREC_NOT_OPERATOR, NULL);
10046 /* If the next token is a `?' then we're actually looking at
10047 a conditional-expression; otherwise we're done. */
10048 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10049 expression = cp_parser_question_colon_clause (parser, expression);
10051 else
10052 expression = cp_parser_assignment_expression (parser);
10053 /* Restore the old settings. */
10054 parser->integral_constant_expression_p
10055 = saved_integral_constant_expression_p;
10056 parser->allow_non_integral_constant_expression_p
10057 = saved_allow_non_integral_constant_expression_p;
10058 if (cxx_dialect >= cxx11)
10060 /* Require an rvalue constant expression here; that's what our
10061 callers expect. Reference constant expressions are handled
10062 separately in e.g. cp_parser_template_argument. */
10063 tree decay = expression;
10064 if (TREE_TYPE (expression)
10065 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10066 decay = build_address (expression);
10067 bool is_const = potential_rvalue_constant_expression (decay);
10068 parser->non_integral_constant_expression_p = !is_const;
10069 if (!is_const && !allow_non_constant_p)
10070 require_potential_rvalue_constant_expression (decay);
10072 if (allow_non_constant_p)
10073 *non_constant_p = parser->non_integral_constant_expression_p;
10074 parser->non_integral_constant_expression_p
10075 = saved_non_integral_constant_expression_p;
10077 return expression;
10080 /* Parse __builtin_offsetof.
10082 offsetof-expression:
10083 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10085 offsetof-member-designator:
10086 id-expression
10087 | offsetof-member-designator "." id-expression
10088 | offsetof-member-designator "[" expression "]"
10089 | offsetof-member-designator "->" id-expression */
10091 static cp_expr
10092 cp_parser_builtin_offsetof (cp_parser *parser)
10094 int save_ice_p, save_non_ice_p;
10095 tree type;
10096 cp_expr expr;
10097 cp_id_kind dummy;
10098 cp_token *token;
10099 location_t finish_loc;
10101 /* We're about to accept non-integral-constant things, but will
10102 definitely yield an integral constant expression. Save and
10103 restore these values around our local parsing. */
10104 save_ice_p = parser->integral_constant_expression_p;
10105 save_non_ice_p = parser->non_integral_constant_expression_p;
10107 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10109 /* Consume the "__builtin_offsetof" token. */
10110 cp_lexer_consume_token (parser->lexer);
10111 /* Consume the opening `('. */
10112 matching_parens parens;
10113 parens.require_open (parser);
10114 /* Parse the type-id. */
10115 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10117 const char *saved_message = parser->type_definition_forbidden_message;
10118 parser->type_definition_forbidden_message
10119 = G_("types may not be defined within %<__builtin_offsetof%>");
10120 type = cp_parser_type_id (parser);
10121 parser->type_definition_forbidden_message = saved_message;
10123 /* Look for the `,'. */
10124 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10125 token = cp_lexer_peek_token (parser->lexer);
10127 /* Build the (type *)null that begins the traditional offsetof macro. */
10128 tree object_ptr
10129 = build_static_cast (build_pointer_type (type), null_pointer_node,
10130 tf_warning_or_error);
10132 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10133 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10134 true, &dummy, token->location);
10135 while (true)
10137 token = cp_lexer_peek_token (parser->lexer);
10138 switch (token->type)
10140 case CPP_OPEN_SQUARE:
10141 /* offsetof-member-designator "[" expression "]" */
10142 expr = cp_parser_postfix_open_square_expression (parser, expr,
10143 true, false);
10144 break;
10146 case CPP_DEREF:
10147 /* offsetof-member-designator "->" identifier */
10148 expr = grok_array_decl (token->location, expr,
10149 integer_zero_node, false);
10150 /* FALLTHRU */
10152 case CPP_DOT:
10153 /* offsetof-member-designator "." identifier */
10154 cp_lexer_consume_token (parser->lexer);
10155 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10156 expr, true, &dummy,
10157 token->location);
10158 break;
10160 case CPP_CLOSE_PAREN:
10161 /* Consume the ")" token. */
10162 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10163 cp_lexer_consume_token (parser->lexer);
10164 goto success;
10166 default:
10167 /* Error. We know the following require will fail, but
10168 that gives the proper error message. */
10169 parens.require_close (parser);
10170 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10171 expr = error_mark_node;
10172 goto failure;
10176 success:
10177 /* Make a location of the form:
10178 __builtin_offsetof (struct s, f)
10179 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10180 with caret at the type-id, ranging from the start of the
10181 "_builtin_offsetof" token to the close paren. */
10182 loc = make_location (loc, start_loc, finish_loc);
10183 /* The result will be an INTEGER_CST, so we need to explicitly
10184 preserve the location. */
10185 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10187 failure:
10188 parser->integral_constant_expression_p = save_ice_p;
10189 parser->non_integral_constant_expression_p = save_non_ice_p;
10191 expr = expr.maybe_add_location_wrapper ();
10192 return expr;
10195 /* Parse a trait expression.
10197 Returns a representation of the expression, the underlying type
10198 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10200 static cp_expr
10201 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10203 cp_trait_kind kind;
10204 tree type1, type2 = NULL_TREE;
10205 bool binary = false;
10206 bool variadic = false;
10208 switch (keyword)
10210 case RID_HAS_NOTHROW_ASSIGN:
10211 kind = CPTK_HAS_NOTHROW_ASSIGN;
10212 break;
10213 case RID_HAS_NOTHROW_CONSTRUCTOR:
10214 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10215 break;
10216 case RID_HAS_NOTHROW_COPY:
10217 kind = CPTK_HAS_NOTHROW_COPY;
10218 break;
10219 case RID_HAS_TRIVIAL_ASSIGN:
10220 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10221 break;
10222 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10223 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10224 break;
10225 case RID_HAS_TRIVIAL_COPY:
10226 kind = CPTK_HAS_TRIVIAL_COPY;
10227 break;
10228 case RID_HAS_TRIVIAL_DESTRUCTOR:
10229 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10230 break;
10231 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10232 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10233 break;
10234 case RID_HAS_VIRTUAL_DESTRUCTOR:
10235 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10236 break;
10237 case RID_IS_ABSTRACT:
10238 kind = CPTK_IS_ABSTRACT;
10239 break;
10240 case RID_IS_AGGREGATE:
10241 kind = CPTK_IS_AGGREGATE;
10242 break;
10243 case RID_IS_BASE_OF:
10244 kind = CPTK_IS_BASE_OF;
10245 binary = true;
10246 break;
10247 case RID_IS_CLASS:
10248 kind = CPTK_IS_CLASS;
10249 break;
10250 case RID_IS_EMPTY:
10251 kind = CPTK_IS_EMPTY;
10252 break;
10253 case RID_IS_ENUM:
10254 kind = CPTK_IS_ENUM;
10255 break;
10256 case RID_IS_FINAL:
10257 kind = CPTK_IS_FINAL;
10258 break;
10259 case RID_IS_LITERAL_TYPE:
10260 kind = CPTK_IS_LITERAL_TYPE;
10261 break;
10262 case RID_IS_POD:
10263 kind = CPTK_IS_POD;
10264 break;
10265 case RID_IS_POLYMORPHIC:
10266 kind = CPTK_IS_POLYMORPHIC;
10267 break;
10268 case RID_IS_SAME_AS:
10269 kind = CPTK_IS_SAME_AS;
10270 binary = true;
10271 break;
10272 case RID_IS_STD_LAYOUT:
10273 kind = CPTK_IS_STD_LAYOUT;
10274 break;
10275 case RID_IS_TRIVIAL:
10276 kind = CPTK_IS_TRIVIAL;
10277 break;
10278 case RID_IS_TRIVIALLY_ASSIGNABLE:
10279 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10280 binary = true;
10281 break;
10282 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10283 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10284 variadic = true;
10285 break;
10286 case RID_IS_TRIVIALLY_COPYABLE:
10287 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10288 break;
10289 case RID_IS_UNION:
10290 kind = CPTK_IS_UNION;
10291 break;
10292 case RID_UNDERLYING_TYPE:
10293 kind = CPTK_UNDERLYING_TYPE;
10294 break;
10295 case RID_BASES:
10296 kind = CPTK_BASES;
10297 break;
10298 case RID_DIRECT_BASES:
10299 kind = CPTK_DIRECT_BASES;
10300 break;
10301 case RID_IS_ASSIGNABLE:
10302 kind = CPTK_IS_ASSIGNABLE;
10303 binary = true;
10304 break;
10305 case RID_IS_CONSTRUCTIBLE:
10306 kind = CPTK_IS_CONSTRUCTIBLE;
10307 variadic = true;
10308 break;
10309 default:
10310 gcc_unreachable ();
10313 /* Get location of initial token. */
10314 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10316 /* Consume the token. */
10317 cp_lexer_consume_token (parser->lexer);
10319 matching_parens parens;
10320 parens.require_open (parser);
10323 type_id_in_expr_sentinel s (parser);
10324 type1 = cp_parser_type_id (parser);
10327 if (type1 == error_mark_node)
10328 return error_mark_node;
10330 if (binary)
10332 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10335 type_id_in_expr_sentinel s (parser);
10336 type2 = cp_parser_type_id (parser);
10339 if (type2 == error_mark_node)
10340 return error_mark_node;
10342 else if (variadic)
10344 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10346 cp_lexer_consume_token (parser->lexer);
10347 tree elt = cp_parser_type_id (parser);
10348 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10350 cp_lexer_consume_token (parser->lexer);
10351 elt = make_pack_expansion (elt);
10353 if (elt == error_mark_node)
10354 return error_mark_node;
10355 type2 = tree_cons (NULL_TREE, elt, type2);
10359 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10360 parens.require_close (parser);
10362 /* Construct a location of the form:
10363 __is_trivially_copyable(_Tp)
10364 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10365 with start == caret, finishing at the close-paren. */
10366 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10368 /* Complete the trait expression, which may mean either processing
10369 the trait expr now or saving it for template instantiation. */
10370 switch (kind)
10372 case CPTK_UNDERLYING_TYPE:
10373 return cp_expr (finish_underlying_type (type1), trait_loc);
10374 case CPTK_BASES:
10375 return cp_expr (finish_bases (type1, false), trait_loc);
10376 case CPTK_DIRECT_BASES:
10377 return cp_expr (finish_bases (type1, true), trait_loc);
10378 default:
10379 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10383 /* Parse a lambda expression.
10385 lambda-expression:
10386 lambda-introducer lambda-declarator [opt] compound-statement
10388 Returns a representation of the expression. */
10390 static cp_expr
10391 cp_parser_lambda_expression (cp_parser* parser)
10393 tree lambda_expr = build_lambda_expr ();
10394 tree type;
10395 bool ok = true;
10396 cp_token *token = cp_lexer_peek_token (parser->lexer);
10397 cp_token_position start = 0;
10399 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10401 if (cxx_dialect >= cxx2a)
10402 /* C++20 allows lambdas in unevaluated context. */;
10403 else if (cp_unevaluated_operand)
10405 if (!token->error_reported)
10407 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10408 "lambda-expression in unevaluated context"
10409 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10410 token->error_reported = true;
10412 ok = false;
10414 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10416 if (!token->error_reported)
10418 error_at (token->location, "lambda-expression in template-argument"
10419 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10420 token->error_reported = true;
10422 ok = false;
10425 /* We may be in the middle of deferred access check. Disable
10426 it now. */
10427 push_deferring_access_checks (dk_no_deferred);
10429 cp_parser_lambda_introducer (parser, lambda_expr);
10430 if (cp_parser_error_occurred (parser))
10431 return error_mark_node;
10433 type = begin_lambda_type (lambda_expr);
10434 if (type == error_mark_node)
10435 return error_mark_node;
10437 record_lambda_scope (lambda_expr);
10439 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10440 determine_visibility (TYPE_NAME (type));
10442 /* Now that we've started the type, add the capture fields for any
10443 explicit captures. */
10444 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10447 /* Inside the class, surrounding template-parameter-lists do not apply. */
10448 unsigned int saved_num_template_parameter_lists
10449 = parser->num_template_parameter_lists;
10450 unsigned char in_statement = parser->in_statement;
10451 bool in_switch_statement_p = parser->in_switch_statement_p;
10452 bool fully_implicit_function_template_p
10453 = parser->fully_implicit_function_template_p;
10454 tree implicit_template_parms = parser->implicit_template_parms;
10455 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10456 bool auto_is_implicit_function_template_parm_p
10457 = parser->auto_is_implicit_function_template_parm_p;
10459 parser->num_template_parameter_lists = 0;
10460 parser->in_statement = 0;
10461 parser->in_switch_statement_p = false;
10462 parser->fully_implicit_function_template_p = false;
10463 parser->implicit_template_parms = 0;
10464 parser->implicit_template_scope = 0;
10465 parser->auto_is_implicit_function_template_parm_p = false;
10467 /* By virtue of defining a local class, a lambda expression has access to
10468 the private variables of enclosing classes. */
10470 if (cp_parser_start_tentative_firewall (parser))
10471 start = token;
10473 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10475 if (ok && cp_parser_error_occurred (parser))
10476 ok = false;
10478 if (ok)
10480 cp_parser_lambda_body (parser, lambda_expr);
10482 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10484 if (cp_parser_skip_to_closing_brace (parser))
10485 cp_lexer_consume_token (parser->lexer);
10488 /* The capture list was built up in reverse order; fix that now. */
10489 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10490 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10492 if (ok)
10493 maybe_add_lambda_conv_op (type);
10495 finish_struct (type, /*attributes=*/NULL_TREE);
10497 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10498 parser->in_statement = in_statement;
10499 parser->in_switch_statement_p = in_switch_statement_p;
10500 parser->fully_implicit_function_template_p
10501 = fully_implicit_function_template_p;
10502 parser->implicit_template_parms = implicit_template_parms;
10503 parser->implicit_template_scope = implicit_template_scope;
10504 parser->auto_is_implicit_function_template_parm_p
10505 = auto_is_implicit_function_template_parm_p;
10508 /* This field is only used during parsing of the lambda. */
10509 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10511 /* This lambda shouldn't have any proxies left at this point. */
10512 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10513 /* And now that we're done, push proxies for an enclosing lambda. */
10514 insert_pending_capture_proxies ();
10516 /* Update the lambda expression to a range. */
10517 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10518 token->location,
10519 parser->lexer);
10521 if (ok)
10522 lambda_expr = build_lambda_object (lambda_expr);
10523 else
10524 lambda_expr = error_mark_node;
10526 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10528 pop_deferring_access_checks ();
10530 return lambda_expr;
10533 /* Parse the beginning of a lambda expression.
10535 lambda-introducer:
10536 [ lambda-capture [opt] ]
10538 LAMBDA_EXPR is the current representation of the lambda expression. */
10540 static void
10541 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10543 /* Need commas after the first capture. */
10544 bool first = true;
10546 /* Eat the leading `['. */
10547 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10549 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10550 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10551 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10552 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10553 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10554 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10555 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10557 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10559 cp_lexer_consume_token (parser->lexer);
10560 first = false;
10562 if (!(at_function_scope_p () || parsing_nsdmi ()))
10563 error ("non-local lambda expression cannot have a capture-default");
10566 hash_set<tree, true> ids;
10567 tree first_capture_id = NULL_TREE;
10568 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10570 cp_token* capture_token;
10571 tree capture_id;
10572 tree capture_init_expr;
10573 cp_id_kind idk = CP_ID_KIND_NONE;
10574 bool explicit_init_p = false;
10576 enum capture_kind_type
10578 BY_COPY,
10579 BY_REFERENCE
10581 enum capture_kind_type capture_kind = BY_COPY;
10583 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10585 error ("expected end of capture-list");
10586 return;
10589 if (first)
10590 first = false;
10591 else
10592 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10594 /* Possibly capture `this'. */
10595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10597 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10598 if (cxx_dialect < cxx2a
10599 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10600 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10601 "with by-copy capture default");
10602 cp_lexer_consume_token (parser->lexer);
10603 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10604 pedwarn (input_location, 0,
10605 "already captured %qD in lambda expression",
10606 this_identifier);
10607 else
10608 add_capture (lambda_expr, /*id=*/this_identifier,
10609 /*initializer=*/finish_this_expr (),
10610 /*by_reference_p=*/true, explicit_init_p);
10611 continue;
10614 /* Possibly capture `*this'. */
10615 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10616 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10618 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10619 if (cxx_dialect < cxx17)
10620 pedwarn (loc, 0, "%<*this%> capture only available with "
10621 "%<-std=c++17%> or %<-std=gnu++17%>");
10622 cp_lexer_consume_token (parser->lexer);
10623 cp_lexer_consume_token (parser->lexer);
10624 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10625 pedwarn (input_location, 0,
10626 "already captured %qD in lambda expression",
10627 this_identifier);
10628 else
10629 add_capture (lambda_expr, /*id=*/this_identifier,
10630 /*initializer=*/finish_this_expr (),
10631 /*by_reference_p=*/false, explicit_init_p);
10632 continue;
10635 /* But reject `&this'. */
10636 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10637 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10639 error_at (cp_lexer_peek_token (parser->lexer)->location,
10640 "%<this%> cannot be captured by reference");
10641 cp_lexer_consume_token (parser->lexer);
10642 cp_lexer_consume_token (parser->lexer);
10643 continue;
10646 bool init_pack_expansion = false;
10647 location_t ellipsis_loc = UNKNOWN_LOCATION;
10648 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10650 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10651 if (cxx_dialect < cxx2a)
10652 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10653 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10654 cp_lexer_consume_token (parser->lexer);
10655 init_pack_expansion = true;
10658 /* Remember whether we want to capture as a reference or not. */
10659 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10661 capture_kind = BY_REFERENCE;
10662 cp_lexer_consume_token (parser->lexer);
10665 /* Get the identifier. */
10666 capture_token = cp_lexer_peek_token (parser->lexer);
10667 capture_id = cp_parser_identifier (parser);
10669 if (capture_id == error_mark_node)
10670 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10671 delimiters, but I modified this to stop on unnested ']' as well. It
10672 was already changed to stop on unnested '}', so the
10673 "closing_parenthesis" name is no more misleading with my change. */
10675 cp_parser_skip_to_closing_parenthesis (parser,
10676 /*recovering=*/true,
10677 /*or_comma=*/true,
10678 /*consume_paren=*/true);
10679 break;
10682 /* Find the initializer for this capture. */
10683 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10684 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10685 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10687 bool direct, non_constant;
10688 /* An explicit initializer exists. */
10689 if (cxx_dialect < cxx14)
10690 pedwarn (input_location, 0,
10691 "lambda capture initializers "
10692 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10693 capture_init_expr = cp_parser_initializer (parser, &direct,
10694 &non_constant, true);
10695 explicit_init_p = true;
10696 if (capture_init_expr == NULL_TREE)
10698 error ("empty initializer for lambda init-capture");
10699 capture_init_expr = error_mark_node;
10701 if (init_pack_expansion)
10702 capture_init_expr = make_pack_expansion (capture_init_expr);
10704 else
10706 const char* error_msg;
10708 /* Turn the identifier into an id-expression. */
10709 capture_init_expr
10710 = cp_parser_lookup_name_simple (parser, capture_id,
10711 capture_token->location);
10713 if (capture_init_expr == error_mark_node)
10715 unqualified_name_lookup_error (capture_id);
10716 continue;
10718 else if (!VAR_P (capture_init_expr)
10719 && TREE_CODE (capture_init_expr) != PARM_DECL)
10721 error_at (capture_token->location,
10722 "capture of non-variable %qE",
10723 capture_init_expr);
10724 if (DECL_P (capture_init_expr))
10725 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10726 "%q#D declared here", capture_init_expr);
10727 continue;
10729 if (VAR_P (capture_init_expr)
10730 && decl_storage_duration (capture_init_expr) != dk_auto)
10732 if (pedwarn (capture_token->location, 0, "capture of variable "
10733 "%qD with non-automatic storage duration",
10734 capture_init_expr))
10735 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10736 "%q#D declared here", capture_init_expr);
10737 continue;
10740 capture_init_expr
10741 = finish_id_expression
10742 (capture_id,
10743 capture_init_expr,
10744 parser->scope,
10745 &idk,
10746 /*integral_constant_expression_p=*/false,
10747 /*allow_non_integral_constant_expression_p=*/false,
10748 /*non_integral_constant_expression_p=*/NULL,
10749 /*template_p=*/false,
10750 /*done=*/true,
10751 /*address_p=*/false,
10752 /*template_arg_p=*/false,
10753 &error_msg,
10754 capture_token->location);
10756 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10758 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10759 cp_lexer_consume_token (parser->lexer);
10760 capture_init_expr = make_pack_expansion (capture_init_expr);
10761 if (init_pack_expansion)
10763 /* If what follows is an initializer, the second '...' is
10764 invalid. But for cases like [...xs...], the first one
10765 is invalid. */
10766 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10767 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10768 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10769 ellipsis_loc = loc;
10770 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10771 continue;
10776 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10777 && !explicit_init_p)
10779 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10780 && capture_kind == BY_COPY)
10781 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10782 "of %qD redundant with by-copy capture default",
10783 capture_id);
10784 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10785 && capture_kind == BY_REFERENCE)
10786 pedwarn (capture_token->location, 0, "explicit by-reference "
10787 "capture of %qD redundant with by-reference capture "
10788 "default", capture_id);
10791 /* Check for duplicates.
10792 Optimize for the zero or one explicit captures cases and only create
10793 the hash_set after adding second capture. */
10794 bool found = false;
10795 if (!ids.is_empty ())
10796 found = ids.add (capture_id);
10797 else if (first_capture_id == NULL_TREE)
10798 first_capture_id = capture_id;
10799 else if (capture_id == first_capture_id)
10800 found = true;
10801 else
10803 ids.add (first_capture_id);
10804 ids.add (capture_id);
10806 if (found)
10807 pedwarn (input_location, 0,
10808 "already captured %qD in lambda expression", capture_id);
10809 else
10810 add_capture (lambda_expr, capture_id, capture_init_expr,
10811 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10812 explicit_init_p);
10814 /* If there is any qualification still in effect, clear it
10815 now; we will be starting fresh with the next capture. */
10816 parser->scope = NULL_TREE;
10817 parser->qualifying_scope = NULL_TREE;
10818 parser->object_scope = NULL_TREE;
10821 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10824 /* Parse the (optional) middle of a lambda expression.
10826 lambda-declarator:
10827 < template-parameter-list [opt] >
10828 ( parameter-declaration-clause [opt] )
10829 attribute-specifier [opt]
10830 decl-specifier-seq [opt]
10831 exception-specification [opt]
10832 lambda-return-type-clause [opt]
10834 LAMBDA_EXPR is the current representation of the lambda expression. */
10836 static bool
10837 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10839 /* 5.1.1.4 of the standard says:
10840 If a lambda-expression does not include a lambda-declarator, it is as if
10841 the lambda-declarator were ().
10842 This means an empty parameter list, no attributes, and no exception
10843 specification. */
10844 tree param_list = void_list_node;
10845 tree std_attrs = NULL_TREE;
10846 tree gnu_attrs = NULL_TREE;
10847 tree exception_spec = NULL_TREE;
10848 tree template_param_list = NULL_TREE;
10849 tree tx_qual = NULL_TREE;
10850 tree return_type = NULL_TREE;
10851 cp_decl_specifier_seq lambda_specs;
10852 clear_decl_specs (&lambda_specs);
10854 /* The template-parameter-list is optional, but must begin with
10855 an opening angle if present. */
10856 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10858 if (cxx_dialect < cxx14)
10859 pedwarn (parser->lexer->next_token->location, 0,
10860 "lambda templates are only available with "
10861 "%<-std=c++14%> or %<-std=gnu++14%>");
10862 else if (cxx_dialect < cxx2a)
10863 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10864 "lambda templates are only available with "
10865 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10867 cp_lexer_consume_token (parser->lexer);
10869 template_param_list = cp_parser_template_parameter_list (parser);
10871 cp_parser_skip_to_end_of_template_parameter_list (parser);
10873 /* We just processed one more parameter list. */
10874 ++parser->num_template_parameter_lists;
10877 /* The parameter-declaration-clause is optional (unless
10878 template-parameter-list was given), but must begin with an
10879 opening parenthesis if present. */
10880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10882 matching_parens parens;
10883 parens.consume_open (parser);
10885 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10887 /* Parse parameters. */
10888 param_list
10889 = cp_parser_parameter_declaration_clause
10890 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10892 /* Default arguments shall not be specified in the
10893 parameter-declaration-clause of a lambda-declarator. */
10894 if (cxx_dialect < cxx14)
10895 for (tree t = param_list; t; t = TREE_CHAIN (t))
10896 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10897 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10898 "default argument specified for lambda parameter");
10900 parens.require_close (parser);
10902 /* In the decl-specifier-seq of the lambda-declarator, each
10903 decl-specifier shall either be mutable or constexpr. */
10904 int declares_class_or_enum;
10905 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10906 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10907 cp_parser_decl_specifier_seq (parser,
10908 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10909 &lambda_specs, &declares_class_or_enum);
10910 if (lambda_specs.storage_class == sc_mutable)
10912 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10913 if (lambda_specs.conflicting_specifiers_p)
10914 error_at (lambda_specs.locations[ds_storage_class],
10915 "duplicate %<mutable%>");
10918 tx_qual = cp_parser_tx_qualifier_opt (parser);
10920 /* Parse optional exception specification. */
10921 exception_spec = cp_parser_exception_specification_opt (parser);
10923 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10925 /* Parse optional trailing return type. */
10926 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10928 cp_lexer_consume_token (parser->lexer);
10929 return_type = cp_parser_trailing_type_id (parser);
10932 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10933 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10935 /* The function parameters must be in scope all the way until after the
10936 trailing-return-type in case of decltype. */
10937 pop_bindings_and_leave_scope ();
10939 else if (template_param_list != NULL_TREE) // generate diagnostic
10940 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10942 /* Create the function call operator.
10944 Messing with declarators like this is no uglier than building up the
10945 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10946 other code. */
10948 cp_decl_specifier_seq return_type_specs;
10949 cp_declarator* declarator;
10950 tree fco;
10951 int quals;
10952 void *p;
10954 clear_decl_specs (&return_type_specs);
10955 return_type_specs.type = make_auto ();
10957 if (lambda_specs.locations[ds_constexpr])
10959 if (cxx_dialect >= cxx17)
10960 return_type_specs.locations[ds_constexpr]
10961 = lambda_specs.locations[ds_constexpr];
10962 else
10963 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10964 "lambda only available with %<-std=c++17%> or "
10965 "%<-std=gnu++17%>");
10968 p = obstack_alloc (&declarator_obstack, 0);
10970 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10971 LAMBDA_EXPR_LOCATION (lambda_expr));
10973 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10974 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10975 declarator = make_call_declarator (declarator, param_list, quals,
10976 VIRT_SPEC_UNSPECIFIED,
10977 REF_QUAL_NONE,
10978 tx_qual,
10979 exception_spec,
10980 return_type,
10981 /*requires_clause*/NULL_TREE);
10982 declarator->std_attributes = std_attrs;
10984 fco = grokmethod (&return_type_specs,
10985 declarator,
10986 gnu_attrs);
10987 if (fco != error_mark_node)
10989 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10990 DECL_ARTIFICIAL (fco) = 1;
10991 /* Give the object parameter a different name. */
10992 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10993 DECL_SET_LAMBDA_FUNCTION (fco, true);
10995 if (template_param_list)
10997 fco = finish_member_template_decl (fco);
10998 finish_template_decl (template_param_list);
10999 --parser->num_template_parameter_lists;
11001 else if (parser->fully_implicit_function_template_p)
11002 fco = finish_fully_implicit_template (parser, fco);
11004 finish_member_declaration (fco);
11006 obstack_free (&declarator_obstack, p);
11008 return (fco != error_mark_node);
11012 /* Parse the body of a lambda expression, which is simply
11014 compound-statement
11016 but which requires special handling.
11017 LAMBDA_EXPR is the current representation of the lambda expression. */
11019 static void
11020 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11022 bool nested = (current_function_decl != NULL_TREE);
11023 unsigned char local_variables_forbidden_p
11024 = parser->local_variables_forbidden_p;
11025 bool in_function_body = parser->in_function_body;
11027 /* The body of a lambda-expression is not a subexpression of the enclosing
11028 expression. */
11029 cp_evaluated ev;
11031 if (nested)
11032 push_function_context ();
11033 else
11034 /* Still increment function_depth so that we don't GC in the
11035 middle of an expression. */
11036 ++function_depth;
11038 vec<tree> omp_privatization_save;
11039 save_omp_privatization_clauses (omp_privatization_save);
11040 /* Clear this in case we're in the middle of a default argument. */
11041 parser->local_variables_forbidden_p = 0;
11042 parser->in_function_body = true;
11045 local_specialization_stack s (lss_copy);
11046 tree fco = lambda_function (lambda_expr);
11047 tree body = start_lambda_function (fco, lambda_expr);
11048 matching_braces braces;
11050 if (braces.require_open (parser))
11052 tree compound_stmt = begin_compound_stmt (0);
11054 /* Originally C++11 required us to peek for 'return expr'; and
11055 process it specially here to deduce the return type. N3638
11056 removed the need for that. */
11058 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11059 cp_parser_label_declaration (parser);
11060 cp_parser_statement_seq_opt (parser, NULL_TREE);
11061 braces.require_close (parser);
11063 finish_compound_stmt (compound_stmt);
11066 finish_lambda_function (body);
11069 restore_omp_privatization_clauses (omp_privatization_save);
11070 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11071 parser->in_function_body = in_function_body;
11072 if (nested)
11073 pop_function_context();
11074 else
11075 --function_depth;
11078 /* Statements [gram.stmt.stmt] */
11080 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11082 static void
11083 add_debug_begin_stmt (location_t loc)
11085 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11086 return;
11087 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11088 /* A concept is never expanded normally. */
11089 return;
11091 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11092 SET_EXPR_LOCATION (stmt, loc);
11093 add_stmt (stmt);
11096 /* Parse a statement.
11098 statement:
11099 labeled-statement
11100 expression-statement
11101 compound-statement
11102 selection-statement
11103 iteration-statement
11104 jump-statement
11105 declaration-statement
11106 try-block
11108 C++11:
11110 statement:
11111 labeled-statement
11112 attribute-specifier-seq (opt) expression-statement
11113 attribute-specifier-seq (opt) compound-statement
11114 attribute-specifier-seq (opt) selection-statement
11115 attribute-specifier-seq (opt) iteration-statement
11116 attribute-specifier-seq (opt) jump-statement
11117 declaration-statement
11118 attribute-specifier-seq (opt) try-block
11120 init-statement:
11121 expression-statement
11122 simple-declaration
11124 TM Extension:
11126 statement:
11127 atomic-statement
11129 IN_COMPOUND is true when the statement is nested inside a
11130 cp_parser_compound_statement; this matters for certain pragmas.
11132 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11133 is a (possibly labeled) if statement which is not enclosed in braces
11134 and has an else clause. This is used to implement -Wparentheses.
11136 CHAIN is a vector of if-else-if conditions. */
11138 static void
11139 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11140 bool in_compound, bool *if_p, vec<tree> *chain,
11141 location_t *loc_after_labels)
11143 tree statement, std_attrs = NULL_TREE;
11144 cp_token *token;
11145 location_t statement_location, attrs_loc;
11147 restart:
11148 if (if_p != NULL)
11149 *if_p = false;
11150 /* There is no statement yet. */
11151 statement = NULL_TREE;
11153 saved_token_sentinel saved_tokens (parser->lexer);
11154 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11155 if (c_dialect_objc ())
11156 /* In obj-c++, seeing '[[' might be the either the beginning of
11157 c++11 attributes, or a nested objc-message-expression. So
11158 let's parse the c++11 attributes tentatively. */
11159 cp_parser_parse_tentatively (parser);
11160 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11161 if (std_attrs)
11162 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
11163 if (c_dialect_objc ())
11165 if (!cp_parser_parse_definitely (parser))
11166 std_attrs = NULL_TREE;
11169 /* Peek at the next token. */
11170 token = cp_lexer_peek_token (parser->lexer);
11171 /* Remember the location of the first token in the statement. */
11172 cp_token *statement_token = token;
11173 statement_location = token->location;
11174 add_debug_begin_stmt (statement_location);
11175 /* If this is a keyword, then that will often determine what kind of
11176 statement we have. */
11177 if (token->type == CPP_KEYWORD)
11179 enum rid keyword = token->keyword;
11181 switch (keyword)
11183 case RID_CASE:
11184 case RID_DEFAULT:
11185 /* Looks like a labeled-statement with a case label.
11186 Parse the label, and then use tail recursion to parse
11187 the statement. */
11188 cp_parser_label_for_labeled_statement (parser, std_attrs);
11189 in_compound = false;
11190 goto restart;
11192 case RID_IF:
11193 case RID_SWITCH:
11194 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11195 statement = cp_parser_selection_statement (parser, if_p, chain);
11196 break;
11198 case RID_WHILE:
11199 case RID_DO:
11200 case RID_FOR:
11201 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11202 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11203 break;
11205 case RID_BREAK:
11206 case RID_CONTINUE:
11207 case RID_RETURN:
11208 case RID_GOTO:
11209 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11210 statement = cp_parser_jump_statement (parser);
11211 break;
11213 /* Objective-C++ exception-handling constructs. */
11214 case RID_AT_TRY:
11215 case RID_AT_CATCH:
11216 case RID_AT_FINALLY:
11217 case RID_AT_SYNCHRONIZED:
11218 case RID_AT_THROW:
11219 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11220 statement = cp_parser_objc_statement (parser);
11221 break;
11223 case RID_TRY:
11224 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11225 statement = cp_parser_try_block (parser);
11226 break;
11228 case RID_NAMESPACE:
11229 /* This must be a namespace alias definition. */
11230 if (std_attrs != NULL_TREE)
11232 /* Attributes should be parsed as part of the the
11233 declaration, so let's un-parse them. */
11234 saved_tokens.rollback();
11235 std_attrs = NULL_TREE;
11237 cp_parser_declaration_statement (parser);
11238 return;
11240 case RID_TRANSACTION_ATOMIC:
11241 case RID_TRANSACTION_RELAXED:
11242 case RID_SYNCHRONIZED:
11243 case RID_ATOMIC_NOEXCEPT:
11244 case RID_ATOMIC_CANCEL:
11245 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11246 statement = cp_parser_transaction (parser, token);
11247 break;
11248 case RID_TRANSACTION_CANCEL:
11249 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11250 statement = cp_parser_transaction_cancel (parser);
11251 break;
11253 default:
11254 /* It might be a keyword like `int' that can start a
11255 declaration-statement. */
11256 break;
11259 else if (token->type == CPP_NAME)
11261 /* If the next token is a `:', then we are looking at a
11262 labeled-statement. */
11263 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11264 if (token->type == CPP_COLON)
11266 /* Looks like a labeled-statement with an ordinary label.
11267 Parse the label, and then use tail recursion to parse
11268 the statement. */
11270 cp_parser_label_for_labeled_statement (parser, std_attrs);
11271 in_compound = false;
11272 goto restart;
11275 /* Anything that starts with a `{' must be a compound-statement. */
11276 else if (token->type == CPP_OPEN_BRACE)
11277 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11278 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11279 a statement all its own. */
11280 else if (token->type == CPP_PRAGMA)
11282 /* Only certain OpenMP pragmas are attached to statements, and thus
11283 are considered statements themselves. All others are not. In
11284 the context of a compound, accept the pragma as a "statement" and
11285 return so that we can check for a close brace. Otherwise we
11286 require a real statement and must go back and read one. */
11287 if (in_compound)
11288 cp_parser_pragma (parser, pragma_compound, if_p);
11289 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11290 goto restart;
11291 return;
11293 else if (token->type == CPP_EOF)
11295 cp_parser_error (parser, "expected statement");
11296 return;
11299 /* Everything else must be a declaration-statement or an
11300 expression-statement. Try for the declaration-statement
11301 first, unless we are looking at a `;', in which case we know that
11302 we have an expression-statement. */
11303 if (!statement)
11305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11307 if (std_attrs != NULL_TREE)
11308 /* Attributes should be parsed as part of the declaration,
11309 so let's un-parse them. */
11310 saved_tokens.rollback();
11312 cp_parser_parse_tentatively (parser);
11313 /* Try to parse the declaration-statement. */
11314 cp_parser_declaration_statement (parser);
11315 /* If that worked, we're done. */
11316 if (cp_parser_parse_definitely (parser))
11317 return;
11318 /* It didn't work, restore the post-attribute position. */
11319 if (std_attrs)
11320 cp_lexer_set_token_position (parser->lexer, statement_token);
11322 /* All preceding labels have been parsed at this point. */
11323 if (loc_after_labels != NULL)
11324 *loc_after_labels = statement_location;
11326 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11328 /* Look for an expression-statement instead. */
11329 statement = cp_parser_expression_statement (parser, in_statement_expr);
11331 /* Handle [[fallthrough]];. */
11332 if (attribute_fallthrough_p (std_attrs))
11334 /* The next token after the fallthrough attribute is ';'. */
11335 if (statement == NULL_TREE)
11337 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11338 statement = build_call_expr_internal_loc (statement_location,
11339 IFN_FALLTHROUGH,
11340 void_type_node, 0);
11341 finish_expr_stmt (statement);
11343 else
11344 warning_at (statement_location, OPT_Wattributes,
11345 "%<fallthrough%> attribute not followed by %<;%>");
11346 std_attrs = NULL_TREE;
11350 /* Set the line number for the statement. */
11351 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11352 SET_EXPR_LOCATION (statement, statement_location);
11354 /* Allow "[[fallthrough]];", but warn otherwise. */
11355 if (std_attrs != NULL_TREE)
11356 warning_at (attrs_loc,
11357 OPT_Wattributes,
11358 "attributes at the beginning of statement are ignored");
11361 /* Append ATTR to attribute list ATTRS. */
11363 static tree
11364 attr_chainon (tree attrs, tree attr)
11366 if (attrs == error_mark_node)
11367 return error_mark_node;
11368 if (attr == error_mark_node)
11369 return error_mark_node;
11370 return chainon (attrs, attr);
11373 /* Parse the label for a labeled-statement, i.e.
11375 identifier :
11376 case constant-expression :
11377 default :
11379 GNU Extension:
11380 case constant-expression ... constant-expression : statement
11382 When a label is parsed without errors, the label is added to the
11383 parse tree by the finish_* functions, so this function doesn't
11384 have to return the label. */
11386 static void
11387 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11389 cp_token *token;
11390 tree label = NULL_TREE;
11391 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11393 /* The next token should be an identifier. */
11394 token = cp_lexer_peek_token (parser->lexer);
11395 if (token->type != CPP_NAME
11396 && token->type != CPP_KEYWORD)
11398 cp_parser_error (parser, "expected labeled-statement");
11399 return;
11402 /* Remember whether this case or a user-defined label is allowed to fall
11403 through to. */
11404 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11406 parser->colon_corrects_to_scope_p = false;
11407 switch (token->keyword)
11409 case RID_CASE:
11411 tree expr, expr_hi;
11412 cp_token *ellipsis;
11414 /* Consume the `case' token. */
11415 cp_lexer_consume_token (parser->lexer);
11416 /* Parse the constant-expression. */
11417 expr = cp_parser_constant_expression (parser);
11418 if (check_for_bare_parameter_packs (expr))
11419 expr = error_mark_node;
11421 ellipsis = cp_lexer_peek_token (parser->lexer);
11422 if (ellipsis->type == CPP_ELLIPSIS)
11424 /* Consume the `...' token. */
11425 cp_lexer_consume_token (parser->lexer);
11426 expr_hi = cp_parser_constant_expression (parser);
11427 if (check_for_bare_parameter_packs (expr_hi))
11428 expr_hi = error_mark_node;
11430 /* We don't need to emit warnings here, as the common code
11431 will do this for us. */
11433 else
11434 expr_hi = NULL_TREE;
11436 if (parser->in_switch_statement_p)
11438 tree l = finish_case_label (token->location, expr, expr_hi);
11439 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11441 label = CASE_LABEL (l);
11442 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11445 else
11446 error_at (token->location,
11447 "case label %qE not within a switch statement",
11448 expr);
11450 break;
11452 case RID_DEFAULT:
11453 /* Consume the `default' token. */
11454 cp_lexer_consume_token (parser->lexer);
11456 if (parser->in_switch_statement_p)
11458 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11459 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11461 label = CASE_LABEL (l);
11462 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11465 else
11466 error_at (token->location, "case label not within a switch statement");
11467 break;
11469 default:
11470 /* Anything else must be an ordinary label. */
11471 label = finish_label_stmt (cp_parser_identifier (parser));
11472 if (label && TREE_CODE (label) == LABEL_DECL)
11473 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11474 break;
11477 /* Require the `:' token. */
11478 cp_parser_require (parser, CPP_COLON, RT_COLON);
11480 /* An ordinary label may optionally be followed by attributes.
11481 However, this is only permitted if the attributes are then
11482 followed by a semicolon. This is because, for backward
11483 compatibility, when parsing
11484 lab: __attribute__ ((unused)) int i;
11485 we want the attribute to attach to "i", not "lab". */
11486 if (label != NULL_TREE
11487 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11489 tree attrs;
11490 cp_parser_parse_tentatively (parser);
11491 attrs = cp_parser_gnu_attributes_opt (parser);
11492 if (attrs == NULL_TREE
11493 /* And fallthrough always binds to the expression-statement. */
11494 || attribute_fallthrough_p (attrs)
11495 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11496 cp_parser_abort_tentative_parse (parser);
11497 else if (!cp_parser_parse_definitely (parser))
11499 else
11500 attributes = attr_chainon (attributes, attrs);
11503 if (attributes != NULL_TREE)
11504 cplus_decl_attributes (&label, attributes, 0);
11506 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11509 /* Parse an expression-statement.
11511 expression-statement:
11512 expression [opt] ;
11514 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11515 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11516 indicates whether this expression-statement is part of an
11517 expression statement. */
11519 static tree
11520 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11522 tree statement = NULL_TREE;
11523 cp_token *token = cp_lexer_peek_token (parser->lexer);
11524 location_t loc = token->location;
11526 /* There might be attribute fallthrough. */
11527 tree attr = cp_parser_gnu_attributes_opt (parser);
11529 /* If the next token is a ';', then there is no expression
11530 statement. */
11531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11533 statement = cp_parser_expression (parser);
11534 if (statement == error_mark_node
11535 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11537 cp_parser_skip_to_end_of_block_or_statement (parser);
11538 return error_mark_node;
11542 /* Handle [[fallthrough]];. */
11543 if (attribute_fallthrough_p (attr))
11545 /* The next token after the fallthrough attribute is ';'. */
11546 if (statement == NULL_TREE)
11547 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11548 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11549 void_type_node, 0);
11550 else
11551 warning_at (loc, OPT_Wattributes,
11552 "%<fallthrough%> attribute not followed by %<;%>");
11553 attr = NULL_TREE;
11556 /* Allow "[[fallthrough]];", but warn otherwise. */
11557 if (attr != NULL_TREE)
11558 warning_at (loc, OPT_Wattributes,
11559 "attributes at the beginning of statement are ignored");
11561 /* Give a helpful message for "A<T>::type t;" and the like. */
11562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11563 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11565 if (TREE_CODE (statement) == SCOPE_REF)
11566 error_at (token->location, "need %<typename%> before %qE because "
11567 "%qT is a dependent scope",
11568 statement, TREE_OPERAND (statement, 0));
11569 else if (is_overloaded_fn (statement)
11570 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11572 /* A::A a; */
11573 tree fn = get_first_fn (statement);
11574 error_at (token->location,
11575 "%<%T::%D%> names the constructor, not the type",
11576 DECL_CONTEXT (fn), DECL_NAME (fn));
11580 /* Consume the final `;'. */
11581 cp_parser_consume_semicolon_at_end_of_statement (parser);
11583 if (in_statement_expr
11584 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11585 /* This is the final expression statement of a statement
11586 expression. */
11587 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11588 else if (statement)
11589 statement = finish_expr_stmt (statement);
11591 return statement;
11594 /* Parse a compound-statement.
11596 compound-statement:
11597 { statement-seq [opt] }
11599 GNU extension:
11601 compound-statement:
11602 { label-declaration-seq [opt] statement-seq [opt] }
11604 label-declaration-seq:
11605 label-declaration
11606 label-declaration-seq label-declaration
11608 Returns a tree representing the statement. */
11610 static tree
11611 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11612 int bcs_flags, bool function_body)
11614 tree compound_stmt;
11615 matching_braces braces;
11617 /* Consume the `{'. */
11618 if (!braces.require_open (parser))
11619 return error_mark_node;
11620 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11621 && !function_body && cxx_dialect < cxx14)
11622 pedwarn (input_location, OPT_Wpedantic,
11623 "compound-statement in %<constexpr%> function");
11624 /* Begin the compound-statement. */
11625 compound_stmt = begin_compound_stmt (bcs_flags);
11626 /* If the next keyword is `__label__' we have a label declaration. */
11627 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11628 cp_parser_label_declaration (parser);
11629 /* Parse an (optional) statement-seq. */
11630 cp_parser_statement_seq_opt (parser, in_statement_expr);
11631 /* Finish the compound-statement. */
11632 finish_compound_stmt (compound_stmt);
11633 /* Consume the `}'. */
11634 braces.require_close (parser);
11636 return compound_stmt;
11639 /* Parse an (optional) statement-seq.
11641 statement-seq:
11642 statement
11643 statement-seq [opt] statement */
11645 static void
11646 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11648 /* Scan statements until there aren't any more. */
11649 while (true)
11651 cp_token *token = cp_lexer_peek_token (parser->lexer);
11653 /* If we are looking at a `}', then we have run out of
11654 statements; the same is true if we have reached the end
11655 of file, or have stumbled upon a stray '@end'. */
11656 if (token->type == CPP_CLOSE_BRACE
11657 || token->type == CPP_EOF
11658 || token->type == CPP_PRAGMA_EOL
11659 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11660 break;
11662 /* If we are in a compound statement and find 'else' then
11663 something went wrong. */
11664 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11666 if (parser->in_statement & IN_IF_STMT)
11667 break;
11668 else
11670 token = cp_lexer_consume_token (parser->lexer);
11671 error_at (token->location, "%<else%> without a previous %<if%>");
11675 /* Parse the statement. */
11676 cp_parser_statement (parser, in_statement_expr, true, NULL);
11680 /* Return true if this is the C++20 version of range-based-for with
11681 init-statement. */
11683 static bool
11684 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11686 bool r = false;
11688 /* Save tokens so that we can put them back. */
11689 cp_lexer_save_tokens (parser->lexer);
11691 /* There has to be an unnested ; followed by an unnested :. */
11692 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11693 /*recovering=*/false,
11694 CPP_SEMICOLON,
11695 /*consume_paren=*/false) != -1)
11696 goto out;
11698 /* We found the semicolon, eat it now. */
11699 cp_lexer_consume_token (parser->lexer);
11701 /* Now look for ':' that is not nested in () or {}. */
11702 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11703 /*recovering=*/false,
11704 CPP_COLON,
11705 /*consume_paren=*/false) == -1);
11707 out:
11708 /* Roll back the tokens we skipped. */
11709 cp_lexer_rollback_tokens (parser->lexer);
11711 return r;
11714 /* Return true if we're looking at (init; cond), false otherwise. */
11716 static bool
11717 cp_parser_init_statement_p (cp_parser *parser)
11719 /* Save tokens so that we can put them back. */
11720 cp_lexer_save_tokens (parser->lexer);
11722 /* Look for ';' that is not nested in () or {}. */
11723 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11724 /*recovering=*/false,
11725 CPP_SEMICOLON,
11726 /*consume_paren=*/false);
11728 /* Roll back the tokens we skipped. */
11729 cp_lexer_rollback_tokens (parser->lexer);
11731 return ret == -1;
11734 /* Parse a selection-statement.
11736 selection-statement:
11737 if ( init-statement [opt] condition ) statement
11738 if ( init-statement [opt] condition ) statement else statement
11739 switch ( init-statement [opt] condition ) statement
11741 Returns the new IF_STMT or SWITCH_STMT.
11743 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11744 is a (possibly labeled) if statement which is not enclosed in
11745 braces and has an else clause. This is used to implement
11746 -Wparentheses.
11748 CHAIN is a vector of if-else-if conditions. This is used to implement
11749 -Wduplicated-cond. */
11751 static tree
11752 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11753 vec<tree> *chain)
11755 cp_token *token;
11756 enum rid keyword;
11757 token_indent_info guard_tinfo;
11759 if (if_p != NULL)
11760 *if_p = false;
11762 /* Peek at the next token. */
11763 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11764 guard_tinfo = get_token_indent_info (token);
11766 /* See what kind of keyword it is. */
11767 keyword = token->keyword;
11768 switch (keyword)
11770 case RID_IF:
11771 case RID_SWITCH:
11773 tree statement;
11774 tree condition;
11776 bool cx = false;
11777 if (keyword == RID_IF
11778 && cp_lexer_next_token_is_keyword (parser->lexer,
11779 RID_CONSTEXPR))
11781 cx = true;
11782 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11783 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11784 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11785 "with %<-std=c++17%> or %<-std=gnu++17%>");
11788 /* Look for the `('. */
11789 matching_parens parens;
11790 if (!parens.require_open (parser))
11792 cp_parser_skip_to_end_of_statement (parser);
11793 return error_mark_node;
11796 /* Begin the selection-statement. */
11797 if (keyword == RID_IF)
11799 statement = begin_if_stmt ();
11800 IF_STMT_CONSTEXPR_P (statement) = cx;
11802 else
11803 statement = begin_switch_stmt ();
11805 /* Parse the optional init-statement. */
11806 if (cp_parser_init_statement_p (parser))
11808 tree decl;
11809 if (cxx_dialect < cxx17)
11810 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11811 "init-statement in selection statements only available "
11812 "with %<-std=c++17%> or %<-std=gnu++17%>");
11813 cp_parser_init_statement (parser, &decl);
11816 /* Parse the condition. */
11817 condition = cp_parser_condition (parser);
11818 /* Look for the `)'. */
11819 if (!parens.require_close (parser))
11820 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11821 /*consume_paren=*/true);
11823 if (keyword == RID_IF)
11825 bool nested_if;
11826 unsigned char in_statement;
11828 /* Add the condition. */
11829 condition = finish_if_stmt_cond (condition, statement);
11831 if (warn_duplicated_cond)
11832 warn_duplicated_cond_add_or_warn (token->location, condition,
11833 &chain);
11835 /* Parse the then-clause. */
11836 in_statement = parser->in_statement;
11837 parser->in_statement |= IN_IF_STMT;
11839 /* Outside a template, the non-selected branch of a constexpr
11840 if is a 'discarded statement', i.e. unevaluated. */
11841 bool was_discarded = in_discarded_stmt;
11842 bool discard_then = (cx && !processing_template_decl
11843 && integer_zerop (condition));
11844 if (discard_then)
11846 in_discarded_stmt = true;
11847 ++c_inhibit_evaluation_warnings;
11850 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11851 guard_tinfo);
11853 parser->in_statement = in_statement;
11855 finish_then_clause (statement);
11857 if (discard_then)
11859 THEN_CLAUSE (statement) = NULL_TREE;
11860 in_discarded_stmt = was_discarded;
11861 --c_inhibit_evaluation_warnings;
11864 /* If the next token is `else', parse the else-clause. */
11865 if (cp_lexer_next_token_is_keyword (parser->lexer,
11866 RID_ELSE))
11868 bool discard_else = (cx && !processing_template_decl
11869 && integer_nonzerop (condition));
11870 if (discard_else)
11872 in_discarded_stmt = true;
11873 ++c_inhibit_evaluation_warnings;
11876 guard_tinfo
11877 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11878 /* Consume the `else' keyword. */
11879 cp_lexer_consume_token (parser->lexer);
11880 if (warn_duplicated_cond)
11882 if (cp_lexer_next_token_is_keyword (parser->lexer,
11883 RID_IF)
11884 && chain == NULL)
11886 /* We've got "if (COND) else if (COND2)". Start
11887 the condition chain and add COND as the first
11888 element. */
11889 chain = new vec<tree> ();
11890 if (!CONSTANT_CLASS_P (condition)
11891 && !TREE_SIDE_EFFECTS (condition))
11893 /* Wrap it in a NOP_EXPR so that we can set the
11894 location of the condition. */
11895 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11896 condition);
11897 SET_EXPR_LOCATION (e, token->location);
11898 chain->safe_push (e);
11901 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11902 RID_IF))
11904 /* This is if-else without subsequent if. Zap the
11905 condition chain; we would have already warned at
11906 this point. */
11907 delete chain;
11908 chain = NULL;
11911 begin_else_clause (statement);
11912 /* Parse the else-clause. */
11913 cp_parser_implicitly_scoped_statement (parser, NULL,
11914 guard_tinfo, chain);
11916 finish_else_clause (statement);
11918 /* If we are currently parsing a then-clause, then
11919 IF_P will not be NULL. We set it to true to
11920 indicate that this if statement has an else clause.
11921 This may trigger the Wparentheses warning below
11922 when we get back up to the parent if statement. */
11923 if (if_p != NULL)
11924 *if_p = true;
11926 if (discard_else)
11928 ELSE_CLAUSE (statement) = NULL_TREE;
11929 in_discarded_stmt = was_discarded;
11930 --c_inhibit_evaluation_warnings;
11933 else
11935 /* This if statement does not have an else clause. If
11936 NESTED_IF is true, then the then-clause has an if
11937 statement which does have an else clause. We warn
11938 about the potential ambiguity. */
11939 if (nested_if)
11940 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11941 "suggest explicit braces to avoid ambiguous"
11942 " %<else%>");
11943 if (warn_duplicated_cond)
11945 /* We don't need the condition chain anymore. */
11946 delete chain;
11947 chain = NULL;
11951 /* Now we're all done with the if-statement. */
11952 finish_if_stmt (statement);
11954 else
11956 bool in_switch_statement_p;
11957 unsigned char in_statement;
11959 /* Add the condition. */
11960 finish_switch_cond (condition, statement);
11962 /* Parse the body of the switch-statement. */
11963 in_switch_statement_p = parser->in_switch_statement_p;
11964 in_statement = parser->in_statement;
11965 parser->in_switch_statement_p = true;
11966 parser->in_statement |= IN_SWITCH_STMT;
11967 cp_parser_implicitly_scoped_statement (parser, if_p,
11968 guard_tinfo);
11969 parser->in_switch_statement_p = in_switch_statement_p;
11970 parser->in_statement = in_statement;
11972 /* Now we're all done with the switch-statement. */
11973 finish_switch_stmt (statement);
11976 return statement;
11978 break;
11980 default:
11981 cp_parser_error (parser, "expected selection-statement");
11982 return error_mark_node;
11986 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11987 If we have seen at least one decl-specifier, and the next token
11988 is not a parenthesis, then we must be looking at a declaration.
11989 (After "int (" we might be looking at a functional cast.) */
11991 static void
11992 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11993 bool any_specifiers_p)
11995 if (any_specifiers_p
11996 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11997 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11998 && !cp_parser_error_occurred (parser))
11999 cp_parser_commit_to_tentative_parse (parser);
12002 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12003 The declarator shall not specify a function or an array. Returns
12004 TRUE if the declarator is valid, FALSE otherwise. */
12006 static bool
12007 cp_parser_check_condition_declarator (cp_parser* parser,
12008 cp_declarator *declarator,
12009 location_t loc)
12011 if (declarator == cp_error_declarator
12012 || function_declarator_p (declarator)
12013 || declarator->kind == cdk_array)
12015 if (declarator == cp_error_declarator)
12016 /* Already complained. */;
12017 else if (declarator->kind == cdk_array)
12018 error_at (loc, "condition declares an array");
12019 else
12020 error_at (loc, "condition declares a function");
12021 if (parser->fully_implicit_function_template_p)
12022 abort_fully_implicit_template (parser);
12023 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12024 /*or_comma=*/false,
12025 /*consume_paren=*/false);
12026 return false;
12028 else
12029 return true;
12032 /* Parse a condition.
12034 condition:
12035 expression
12036 type-specifier-seq declarator = initializer-clause
12037 type-specifier-seq declarator braced-init-list
12039 GNU Extension:
12041 condition:
12042 type-specifier-seq declarator asm-specification [opt]
12043 attributes [opt] = assignment-expression
12045 Returns the expression that should be tested. */
12047 static tree
12048 cp_parser_condition (cp_parser* parser)
12050 cp_decl_specifier_seq type_specifiers;
12051 const char *saved_message;
12052 int declares_class_or_enum;
12054 /* Try the declaration first. */
12055 cp_parser_parse_tentatively (parser);
12056 /* New types are not allowed in the type-specifier-seq for a
12057 condition. */
12058 saved_message = parser->type_definition_forbidden_message;
12059 parser->type_definition_forbidden_message
12060 = G_("types may not be defined in conditions");
12061 /* Parse the type-specifier-seq. */
12062 cp_parser_decl_specifier_seq (parser,
12063 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12064 &type_specifiers,
12065 &declares_class_or_enum);
12066 /* Restore the saved message. */
12067 parser->type_definition_forbidden_message = saved_message;
12069 /* Gather the attributes that were provided with the
12070 decl-specifiers. */
12071 tree prefix_attributes = type_specifiers.attributes;
12073 cp_parser_maybe_commit_to_declaration (parser,
12074 type_specifiers.any_specifiers_p);
12076 /* If all is well, we might be looking at a declaration. */
12077 if (!cp_parser_error_occurred (parser))
12079 tree decl;
12080 tree asm_specification;
12081 tree attributes;
12082 cp_declarator *declarator;
12083 tree initializer = NULL_TREE;
12084 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12086 /* Parse the declarator. */
12087 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12088 CP_PARSER_FLAGS_NONE,
12089 /*ctor_dtor_or_conv_p=*/NULL,
12090 /*parenthesized_p=*/NULL,
12091 /*member_p=*/false,
12092 /*friend_p=*/false,
12093 /*static_p=*/false);
12094 /* Parse the attributes. */
12095 attributes = cp_parser_attributes_opt (parser);
12096 /* Parse the asm-specification. */
12097 asm_specification = cp_parser_asm_specification_opt (parser);
12098 /* If the next token is not an `=' or '{', then we might still be
12099 looking at an expression. For example:
12101 if (A(a).x)
12103 looks like a decl-specifier-seq and a declarator -- but then
12104 there is no `=', so this is an expression. */
12105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12106 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12107 cp_parser_simulate_error (parser);
12109 /* If we did see an `=' or '{', then we are looking at a declaration
12110 for sure. */
12111 if (cp_parser_parse_definitely (parser))
12113 tree pushed_scope;
12114 bool non_constant_p = false;
12115 int flags = LOOKUP_ONLYCONVERTING;
12117 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12118 return error_mark_node;
12120 /* Create the declaration. */
12121 decl = start_decl (declarator, &type_specifiers,
12122 /*initialized_p=*/true,
12123 attributes, prefix_attributes,
12124 &pushed_scope);
12126 /* Parse the initializer. */
12127 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12129 initializer = cp_parser_braced_list (parser, &non_constant_p);
12130 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12131 flags = 0;
12133 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12135 /* Consume the `='. */
12136 cp_lexer_consume_token (parser->lexer);
12137 initializer = cp_parser_initializer_clause (parser,
12138 &non_constant_p);
12140 else
12142 cp_parser_error (parser, "expected initializer");
12143 initializer = error_mark_node;
12145 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12146 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12148 /* Process the initializer. */
12149 cp_finish_decl (decl,
12150 initializer, !non_constant_p,
12151 asm_specification,
12152 flags);
12154 if (pushed_scope)
12155 pop_scope (pushed_scope);
12157 return convert_from_reference (decl);
12160 /* If we didn't even get past the declarator successfully, we are
12161 definitely not looking at a declaration. */
12162 else
12163 cp_parser_abort_tentative_parse (parser);
12165 /* Otherwise, we are looking at an expression. */
12166 return cp_parser_expression (parser);
12169 /* Parses a for-statement or range-for-statement until the closing ')',
12170 not included. */
12172 static tree
12173 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12175 tree init, scope, decl;
12176 bool is_range_for;
12178 /* Begin the for-statement. */
12179 scope = begin_for_scope (&init);
12181 /* Parse the initialization. */
12182 is_range_for = cp_parser_init_statement (parser, &decl);
12184 if (is_range_for)
12185 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12186 false);
12187 else
12188 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12191 static tree
12192 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12193 unsigned short unroll)
12195 /* Normal for loop */
12196 tree condition = NULL_TREE;
12197 tree expression = NULL_TREE;
12198 tree stmt;
12200 stmt = begin_for_stmt (scope, init);
12201 /* The init-statement has already been parsed in
12202 cp_parser_init_statement, so no work is needed here. */
12203 finish_init_stmt (stmt);
12205 /* If there's a condition, process it. */
12206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12207 condition = cp_parser_condition (parser);
12208 else if (ivdep)
12210 cp_parser_error (parser, "missing loop condition in loop with "
12211 "%<GCC ivdep%> pragma");
12212 condition = error_mark_node;
12214 else if (unroll)
12216 cp_parser_error (parser, "missing loop condition in loop with "
12217 "%<GCC unroll%> pragma");
12218 condition = error_mark_node;
12220 finish_for_cond (condition, stmt, ivdep, unroll);
12221 /* Look for the `;'. */
12222 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12224 /* If there's an expression, process it. */
12225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12226 expression = cp_parser_expression (parser);
12227 finish_for_expr (expression, stmt);
12229 return stmt;
12232 /* Tries to parse a range-based for-statement:
12234 range-based-for:
12235 decl-specifier-seq declarator : expression
12237 The decl-specifier-seq declarator and the `:' are already parsed by
12238 cp_parser_init_statement. If processing_template_decl it returns a
12239 newly created RANGE_FOR_STMT; if not, it is converted to a
12240 regular FOR_STMT. */
12242 static tree
12243 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12244 bool ivdep, unsigned short unroll, bool is_omp)
12246 tree stmt, range_expr;
12247 auto_vec <cxx_binding *, 16> bindings;
12248 auto_vec <tree, 16> names;
12249 tree decomp_first_name = NULL_TREE;
12250 unsigned int decomp_cnt = 0;
12252 /* Get the range declaration momentarily out of the way so that
12253 the range expression doesn't clash with it. */
12254 if (range_decl != error_mark_node)
12256 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12258 tree v = DECL_VALUE_EXPR (range_decl);
12259 /* For decomposition declaration get all of the corresponding
12260 declarations out of the way. */
12261 if (TREE_CODE (v) == ARRAY_REF
12262 && VAR_P (TREE_OPERAND (v, 0))
12263 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12265 tree d = range_decl;
12266 range_decl = TREE_OPERAND (v, 0);
12267 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12268 decomp_first_name = d;
12269 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12271 tree name = DECL_NAME (d);
12272 names.safe_push (name);
12273 bindings.safe_push (IDENTIFIER_BINDING (name));
12274 IDENTIFIER_BINDING (name)
12275 = IDENTIFIER_BINDING (name)->previous;
12279 if (names.is_empty ())
12281 tree name = DECL_NAME (range_decl);
12282 names.safe_push (name);
12283 bindings.safe_push (IDENTIFIER_BINDING (name));
12284 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12288 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12290 bool expr_non_constant_p;
12291 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12293 else
12294 range_expr = cp_parser_expression (parser);
12296 /* Put the range declaration(s) back into scope. */
12297 for (unsigned int i = 0; i < names.length (); i++)
12299 cxx_binding *binding = bindings[i];
12300 binding->previous = IDENTIFIER_BINDING (names[i]);
12301 IDENTIFIER_BINDING (names[i]) = binding;
12304 /* finish_omp_for has its own code for the following, so just
12305 return the range_expr instead. */
12306 if (is_omp)
12307 return range_expr;
12309 /* If in template, STMT is converted to a normal for-statement
12310 at instantiation. If not, it is done just ahead. */
12311 if (processing_template_decl)
12313 if (check_for_bare_parameter_packs (range_expr))
12314 range_expr = error_mark_node;
12315 stmt = begin_range_for_stmt (scope, init);
12316 if (ivdep)
12317 RANGE_FOR_IVDEP (stmt) = 1;
12318 if (unroll)
12319 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12320 finish_range_for_decl (stmt, range_decl, range_expr);
12321 if (!type_dependent_expression_p (range_expr)
12322 /* do_auto_deduction doesn't mess with template init-lists. */
12323 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12324 do_range_for_auto_deduction (range_decl, range_expr);
12326 else
12328 stmt = begin_for_stmt (scope, init);
12329 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12330 decomp_first_name, decomp_cnt, ivdep,
12331 unroll);
12333 return stmt;
12336 /* Subroutine of cp_convert_range_for: given the initializer expression,
12337 builds up the range temporary. */
12339 static tree
12340 build_range_temp (tree range_expr)
12342 tree range_type, range_temp;
12344 /* Find out the type deduced by the declaration
12345 `auto &&__range = range_expr'. */
12346 range_type = cp_build_reference_type (make_auto (), true);
12347 range_type = do_auto_deduction (range_type, range_expr,
12348 type_uses_auto (range_type));
12350 /* Create the __range variable. */
12351 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12352 range_type);
12353 TREE_USED (range_temp) = 1;
12354 DECL_ARTIFICIAL (range_temp) = 1;
12356 return range_temp;
12359 /* Used by cp_parser_range_for in template context: we aren't going to
12360 do a full conversion yet, but we still need to resolve auto in the
12361 type of the for-range-declaration if present. This is basically
12362 a shortcut version of cp_convert_range_for. */
12364 static void
12365 do_range_for_auto_deduction (tree decl, tree range_expr)
12367 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12368 if (auto_node)
12370 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12371 range_temp = convert_from_reference (build_range_temp (range_expr));
12372 iter_type = (cp_parser_perform_range_for_lookup
12373 (range_temp, &begin_dummy, &end_dummy));
12374 if (iter_type)
12376 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12377 iter_type);
12378 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12379 RO_UNARY_STAR,
12380 tf_warning_or_error);
12381 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12382 iter_decl, auto_node);
12387 /* Converts a range-based for-statement into a normal
12388 for-statement, as per the definition.
12390 for (RANGE_DECL : RANGE_EXPR)
12391 BLOCK
12393 should be equivalent to:
12396 auto &&__range = RANGE_EXPR;
12397 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12398 __begin != __end;
12399 ++__begin)
12401 RANGE_DECL = *__begin;
12402 BLOCK
12406 If RANGE_EXPR is an array:
12407 BEGIN_EXPR = __range
12408 END_EXPR = __range + ARRAY_SIZE(__range)
12409 Else if RANGE_EXPR has a member 'begin' or 'end':
12410 BEGIN_EXPR = __range.begin()
12411 END_EXPR = __range.end()
12412 Else:
12413 BEGIN_EXPR = begin(__range)
12414 END_EXPR = end(__range);
12416 If __range has a member 'begin' but not 'end', or vice versa, we must
12417 still use the second alternative (it will surely fail, however).
12418 When calling begin()/end() in the third alternative we must use
12419 argument dependent lookup, but always considering 'std' as an associated
12420 namespace. */
12422 tree
12423 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12424 tree decomp_first_name, unsigned int decomp_cnt,
12425 bool ivdep, unsigned short unroll)
12427 tree begin, end;
12428 tree iter_type, begin_expr, end_expr;
12429 tree condition, expression;
12431 range_expr = mark_lvalue_use (range_expr);
12433 if (range_decl == error_mark_node || range_expr == error_mark_node)
12434 /* If an error happened previously do nothing or else a lot of
12435 unhelpful errors would be issued. */
12436 begin_expr = end_expr = iter_type = error_mark_node;
12437 else
12439 tree range_temp;
12441 if (VAR_P (range_expr)
12442 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12443 /* Can't bind a reference to an array of runtime bound. */
12444 range_temp = range_expr;
12445 else
12447 range_temp = build_range_temp (range_expr);
12448 pushdecl (range_temp);
12449 cp_finish_decl (range_temp, range_expr,
12450 /*is_constant_init*/false, NULL_TREE,
12451 LOOKUP_ONLYCONVERTING);
12452 range_temp = convert_from_reference (range_temp);
12454 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12455 &begin_expr, &end_expr);
12458 /* The new for initialization statement. */
12459 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12460 iter_type);
12461 TREE_USED (begin) = 1;
12462 DECL_ARTIFICIAL (begin) = 1;
12463 pushdecl (begin);
12464 cp_finish_decl (begin, begin_expr,
12465 /*is_constant_init*/false, NULL_TREE,
12466 LOOKUP_ONLYCONVERTING);
12468 if (cxx_dialect >= cxx17)
12469 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12470 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12471 TREE_USED (end) = 1;
12472 DECL_ARTIFICIAL (end) = 1;
12473 pushdecl (end);
12474 cp_finish_decl (end, end_expr,
12475 /*is_constant_init*/false, NULL_TREE,
12476 LOOKUP_ONLYCONVERTING);
12478 finish_init_stmt (statement);
12480 /* The new for condition. */
12481 condition = build_x_binary_op (input_location, NE_EXPR,
12482 begin, ERROR_MARK,
12483 end, ERROR_MARK,
12484 NULL, tf_warning_or_error);
12485 finish_for_cond (condition, statement, ivdep, unroll);
12487 /* The new increment expression. */
12488 expression = finish_unary_op_expr (input_location,
12489 PREINCREMENT_EXPR, begin,
12490 tf_warning_or_error);
12491 finish_for_expr (expression, statement);
12493 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12494 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12496 /* The declaration is initialized with *__begin inside the loop body. */
12497 cp_finish_decl (range_decl,
12498 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12499 tf_warning_or_error),
12500 /*is_constant_init*/false, NULL_TREE,
12501 LOOKUP_ONLYCONVERTING);
12502 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12503 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12505 return statement;
12508 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12509 We need to solve both at the same time because the method used
12510 depends on the existence of members begin or end.
12511 Returns the type deduced for the iterator expression. */
12513 static tree
12514 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12516 if (error_operand_p (range))
12518 *begin = *end = error_mark_node;
12519 return error_mark_node;
12522 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12524 error ("range-based %<for%> expression of type %qT "
12525 "has incomplete type", TREE_TYPE (range));
12526 *begin = *end = error_mark_node;
12527 return error_mark_node;
12529 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12531 /* If RANGE is an array, we will use pointer arithmetic. */
12532 *begin = decay_conversion (range, tf_warning_or_error);
12533 *end = build_binary_op (input_location, PLUS_EXPR,
12534 range,
12535 array_type_nelts_top (TREE_TYPE (range)),
12536 false);
12537 return TREE_TYPE (*begin);
12539 else
12541 /* If it is not an array, we must do a bit of magic. */
12542 tree id_begin, id_end;
12543 tree member_begin, member_end;
12545 *begin = *end = error_mark_node;
12547 id_begin = get_identifier ("begin");
12548 id_end = get_identifier ("end");
12549 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12550 /*protect=*/2, /*want_type=*/false,
12551 tf_warning_or_error);
12552 member_end = lookup_member (TREE_TYPE (range), id_end,
12553 /*protect=*/2, /*want_type=*/false,
12554 tf_warning_or_error);
12556 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12558 /* Use the member functions. */
12559 *begin = cp_parser_range_for_member_function (range, id_begin);
12560 *end = cp_parser_range_for_member_function (range, id_end);
12562 else
12564 /* Use global functions with ADL. */
12565 releasing_vec vec;
12567 vec_safe_push (vec, range);
12569 member_begin = perform_koenig_lookup (id_begin, vec,
12570 tf_warning_or_error);
12571 *begin = finish_call_expr (member_begin, &vec, false, true,
12572 tf_warning_or_error);
12573 member_end = perform_koenig_lookup (id_end, vec,
12574 tf_warning_or_error);
12575 *end = finish_call_expr (member_end, &vec, false, true,
12576 tf_warning_or_error);
12579 /* Last common checks. */
12580 if (*begin == error_mark_node || *end == error_mark_node)
12582 /* If one of the expressions is an error do no more checks. */
12583 *begin = *end = error_mark_node;
12584 return error_mark_node;
12586 else if (type_dependent_expression_p (*begin)
12587 || type_dependent_expression_p (*end))
12588 /* Can happen, when, eg, in a template context, Koenig lookup
12589 can't resolve begin/end (c++/58503). */
12590 return NULL_TREE;
12591 else
12593 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12594 /* The unqualified type of the __begin and __end temporaries should
12595 be the same, as required by the multiple auto declaration. */
12596 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12598 if (cxx_dialect >= cxx17
12599 && (build_x_binary_op (input_location, NE_EXPR,
12600 *begin, ERROR_MARK,
12601 *end, ERROR_MARK,
12602 NULL, tf_none)
12603 != error_mark_node))
12604 /* P0184R0 allows __begin and __end to have different types,
12605 but make sure they are comparable so we can give a better
12606 diagnostic. */;
12607 else
12608 error ("inconsistent begin/end types in range-based %<for%> "
12609 "statement: %qT and %qT",
12610 TREE_TYPE (*begin), TREE_TYPE (*end));
12612 return iter_type;
12617 /* Helper function for cp_parser_perform_range_for_lookup.
12618 Builds a tree for RANGE.IDENTIFIER(). */
12620 static tree
12621 cp_parser_range_for_member_function (tree range, tree identifier)
12623 tree member, res;
12625 member = finish_class_member_access_expr (range, identifier,
12626 false, tf_warning_or_error);
12627 if (member == error_mark_node)
12628 return error_mark_node;
12630 releasing_vec vec;
12631 res = finish_call_expr (member, &vec,
12632 /*disallow_virtual=*/false,
12633 /*koenig_p=*/false,
12634 tf_warning_or_error);
12635 return res;
12638 /* Parse an iteration-statement.
12640 iteration-statement:
12641 while ( condition ) statement
12642 do statement while ( expression ) ;
12643 for ( init-statement condition [opt] ; expression [opt] )
12644 statement
12646 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12648 static tree
12649 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12650 unsigned short unroll)
12652 cp_token *token;
12653 enum rid keyword;
12654 tree statement;
12655 unsigned char in_statement;
12656 token_indent_info guard_tinfo;
12658 /* Peek at the next token. */
12659 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12660 if (!token)
12661 return error_mark_node;
12663 guard_tinfo = get_token_indent_info (token);
12665 /* Remember whether or not we are already within an iteration
12666 statement. */
12667 in_statement = parser->in_statement;
12669 /* See what kind of keyword it is. */
12670 keyword = token->keyword;
12671 switch (keyword)
12673 case RID_WHILE:
12675 tree condition;
12677 /* Begin the while-statement. */
12678 statement = begin_while_stmt ();
12679 /* Look for the `('. */
12680 matching_parens parens;
12681 parens.require_open (parser);
12682 /* Parse the condition. */
12683 condition = cp_parser_condition (parser);
12684 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12685 /* Look for the `)'. */
12686 parens.require_close (parser);
12687 /* Parse the dependent statement. */
12688 parser->in_statement = IN_ITERATION_STMT;
12689 bool prev = note_iteration_stmt_body_start ();
12690 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12691 note_iteration_stmt_body_end (prev);
12692 parser->in_statement = in_statement;
12693 /* We're done with the while-statement. */
12694 finish_while_stmt (statement);
12696 break;
12698 case RID_DO:
12700 tree expression;
12702 /* Begin the do-statement. */
12703 statement = begin_do_stmt ();
12704 /* Parse the body of the do-statement. */
12705 parser->in_statement = IN_ITERATION_STMT;
12706 bool prev = note_iteration_stmt_body_start ();
12707 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12708 note_iteration_stmt_body_end (prev);
12709 parser->in_statement = in_statement;
12710 finish_do_body (statement);
12711 /* Look for the `while' keyword. */
12712 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12713 /* Look for the `('. */
12714 matching_parens parens;
12715 parens.require_open (parser);
12716 /* Parse the expression. */
12717 expression = cp_parser_expression (parser);
12718 /* We're done with the do-statement. */
12719 finish_do_stmt (expression, statement, ivdep, unroll);
12720 /* Look for the `)'. */
12721 parens.require_close (parser);
12722 /* Look for the `;'. */
12723 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12725 break;
12727 case RID_FOR:
12729 /* Look for the `('. */
12730 matching_parens parens;
12731 parens.require_open (parser);
12733 statement = cp_parser_for (parser, ivdep, unroll);
12735 /* Look for the `)'. */
12736 parens.require_close (parser);
12738 /* Parse the body of the for-statement. */
12739 parser->in_statement = IN_ITERATION_STMT;
12740 bool prev = note_iteration_stmt_body_start ();
12741 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12742 note_iteration_stmt_body_end (prev);
12743 parser->in_statement = in_statement;
12745 /* We're done with the for-statement. */
12746 finish_for_stmt (statement);
12748 break;
12750 default:
12751 cp_parser_error (parser, "expected iteration-statement");
12752 statement = error_mark_node;
12753 break;
12756 return statement;
12759 /* Parse a init-statement or the declarator of a range-based-for.
12760 Returns true if a range-based-for declaration is seen.
12762 init-statement:
12763 expression-statement
12764 simple-declaration */
12766 static bool
12767 cp_parser_init_statement (cp_parser *parser, tree *decl)
12769 /* If the next token is a `;', then we have an empty
12770 expression-statement. Grammatically, this is also a
12771 simple-declaration, but an invalid one, because it does not
12772 declare anything. Therefore, if we did not handle this case
12773 specially, we would issue an error message about an invalid
12774 declaration. */
12775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12777 bool is_range_for = false;
12778 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12780 /* Try to parse the init-statement. */
12781 if (cp_parser_range_based_for_with_init_p (parser))
12783 tree dummy;
12784 cp_parser_parse_tentatively (parser);
12785 /* Parse the declaration. */
12786 cp_parser_simple_declaration (parser,
12787 /*function_definition_allowed_p=*/false,
12788 &dummy);
12789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12790 if (!cp_parser_parse_definitely (parser))
12791 /* That didn't work, try to parse it as an expression-statement. */
12792 cp_parser_expression_statement (parser, NULL_TREE);
12794 if (cxx_dialect < cxx2a)
12796 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12797 "range-based %<for%> loops with initializer only "
12798 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12799 *decl = error_mark_node;
12803 /* A colon is used in range-based for. */
12804 parser->colon_corrects_to_scope_p = false;
12806 /* We're going to speculatively look for a declaration, falling back
12807 to an expression, if necessary. */
12808 cp_parser_parse_tentatively (parser);
12809 /* Parse the declaration. */
12810 cp_parser_simple_declaration (parser,
12811 /*function_definition_allowed_p=*/false,
12812 decl);
12813 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12814 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12816 /* It is a range-for, consume the ':'. */
12817 cp_lexer_consume_token (parser->lexer);
12818 is_range_for = true;
12819 if (cxx_dialect < cxx11)
12820 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12821 "range-based %<for%> loops only available with "
12822 "%<-std=c++11%> or %<-std=gnu++11%>");
12824 else
12825 /* The ';' is not consumed yet because we told
12826 cp_parser_simple_declaration not to. */
12827 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12829 if (cp_parser_parse_definitely (parser))
12830 return is_range_for;
12831 /* If the tentative parse failed, then we shall need to look for an
12832 expression-statement. */
12834 /* If we are here, it is an expression-statement. */
12835 cp_parser_expression_statement (parser, NULL_TREE);
12836 return false;
12839 /* Parse a jump-statement.
12841 jump-statement:
12842 break ;
12843 continue ;
12844 return expression [opt] ;
12845 return braced-init-list ;
12846 goto identifier ;
12848 GNU extension:
12850 jump-statement:
12851 goto * expression ;
12853 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12855 static tree
12856 cp_parser_jump_statement (cp_parser* parser)
12858 tree statement = error_mark_node;
12859 cp_token *token;
12860 enum rid keyword;
12861 unsigned char in_statement;
12863 /* Peek at the next token. */
12864 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12865 if (!token)
12866 return error_mark_node;
12868 /* See what kind of keyword it is. */
12869 keyword = token->keyword;
12870 switch (keyword)
12872 case RID_BREAK:
12873 in_statement = parser->in_statement & ~IN_IF_STMT;
12874 switch (in_statement)
12876 case 0:
12877 error_at (token->location, "break statement not within loop or switch");
12878 break;
12879 default:
12880 gcc_assert ((in_statement & IN_SWITCH_STMT)
12881 || in_statement == IN_ITERATION_STMT);
12882 statement = finish_break_stmt ();
12883 if (in_statement == IN_ITERATION_STMT)
12884 break_maybe_infinite_loop ();
12885 break;
12886 case IN_OMP_BLOCK:
12887 error_at (token->location, "invalid exit from OpenMP structured block");
12888 break;
12889 case IN_OMP_FOR:
12890 error_at (token->location, "break statement used with OpenMP for loop");
12891 break;
12893 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12894 break;
12896 case RID_CONTINUE:
12897 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12899 case 0:
12900 error_at (token->location, "continue statement not within a loop");
12901 break;
12902 /* Fall through. */
12903 case IN_ITERATION_STMT:
12904 case IN_OMP_FOR:
12905 statement = finish_continue_stmt ();
12906 break;
12907 case IN_OMP_BLOCK:
12908 error_at (token->location, "invalid exit from OpenMP structured block");
12909 break;
12910 default:
12911 gcc_unreachable ();
12913 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12914 break;
12916 case RID_RETURN:
12918 tree expr;
12919 bool expr_non_constant_p;
12921 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12923 cp_lexer_set_source_position (parser->lexer);
12924 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12925 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12927 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12928 expr = cp_parser_expression (parser);
12929 else
12930 /* If the next token is a `;', then there is no
12931 expression. */
12932 expr = NULL_TREE;
12933 /* Build the return-statement. */
12934 if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
12935 /* Don't deduce from a discarded return statement. */;
12936 else
12937 statement = finish_return_stmt (expr);
12938 /* Look for the final `;'. */
12939 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12941 break;
12943 case RID_GOTO:
12944 if (parser->in_function_body
12945 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12947 error ("%<goto%> in %<constexpr%> function");
12948 cp_function_chain->invalid_constexpr = true;
12951 /* Create the goto-statement. */
12952 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12954 /* Issue a warning about this use of a GNU extension. */
12955 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12956 /* Consume the '*' token. */
12957 cp_lexer_consume_token (parser->lexer);
12958 /* Parse the dependent expression. */
12959 finish_goto_stmt (cp_parser_expression (parser));
12961 else
12962 finish_goto_stmt (cp_parser_identifier (parser));
12963 /* Look for the final `;'. */
12964 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12965 break;
12967 default:
12968 cp_parser_error (parser, "expected jump-statement");
12969 break;
12972 return statement;
12975 /* Parse a declaration-statement.
12977 declaration-statement:
12978 block-declaration */
12980 static void
12981 cp_parser_declaration_statement (cp_parser* parser)
12983 void *p;
12985 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12986 p = obstack_alloc (&declarator_obstack, 0);
12988 /* Parse the block-declaration. */
12989 cp_parser_block_declaration (parser, /*statement_p=*/true);
12991 /* Free any declarators allocated. */
12992 obstack_free (&declarator_obstack, p);
12995 /* Some dependent statements (like `if (cond) statement'), are
12996 implicitly in their own scope. In other words, if the statement is
12997 a single statement (as opposed to a compound-statement), it is
12998 none-the-less treated as if it were enclosed in braces. Any
12999 declarations appearing in the dependent statement are out of scope
13000 after control passes that point. This function parses a statement,
13001 but ensures that is in its own scope, even if it is not a
13002 compound-statement.
13004 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13005 is a (possibly labeled) if statement which is not enclosed in
13006 braces and has an else clause. This is used to implement
13007 -Wparentheses.
13009 CHAIN is a vector of if-else-if conditions. This is used to implement
13010 -Wduplicated-cond.
13012 Returns the new statement. */
13014 static tree
13015 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13016 const token_indent_info &guard_tinfo,
13017 vec<tree> *chain)
13019 tree statement;
13020 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13021 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13022 token_indent_info body_tinfo
13023 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13025 if (if_p != NULL)
13026 *if_p = false;
13028 /* Mark if () ; with a special NOP_EXPR. */
13029 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13031 cp_lexer_consume_token (parser->lexer);
13032 statement = add_stmt (build_empty_stmt (body_loc));
13034 if (guard_tinfo.keyword == RID_IF
13035 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13036 warning_at (body_loc, OPT_Wempty_body,
13037 "suggest braces around empty body in an %<if%> statement");
13038 else if (guard_tinfo.keyword == RID_ELSE)
13039 warning_at (body_loc, OPT_Wempty_body,
13040 "suggest braces around empty body in an %<else%> statement");
13042 /* if a compound is opened, we simply parse the statement directly. */
13043 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13044 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13045 /* If the token is not a `{', then we must take special action. */
13046 else
13048 /* Create a compound-statement. */
13049 statement = begin_compound_stmt (0);
13050 /* Parse the dependent-statement. */
13051 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13052 &body_loc_after_labels);
13053 /* Finish the dummy compound-statement. */
13054 finish_compound_stmt (statement);
13057 token_indent_info next_tinfo
13058 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13059 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13061 if (body_loc_after_labels != UNKNOWN_LOCATION
13062 && next_tinfo.type != CPP_SEMICOLON)
13063 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13064 guard_tinfo.location, guard_tinfo.keyword);
13066 /* Return the statement. */
13067 return statement;
13070 /* For some dependent statements (like `while (cond) statement'), we
13071 have already created a scope. Therefore, even if the dependent
13072 statement is a compound-statement, we do not want to create another
13073 scope. */
13075 static void
13076 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13077 const token_indent_info &guard_tinfo)
13079 /* If the token is a `{', then we must take special action. */
13080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13082 token_indent_info body_tinfo
13083 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13084 location_t loc_after_labels = UNKNOWN_LOCATION;
13086 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13087 &loc_after_labels);
13088 token_indent_info next_tinfo
13089 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13090 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13092 if (loc_after_labels != UNKNOWN_LOCATION
13093 && next_tinfo.type != CPP_SEMICOLON)
13094 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13095 guard_tinfo.location,
13096 guard_tinfo.keyword);
13098 else
13100 /* Avoid calling cp_parser_compound_statement, so that we
13101 don't create a new scope. Do everything else by hand. */
13102 matching_braces braces;
13103 braces.require_open (parser);
13104 /* If the next keyword is `__label__' we have a label declaration. */
13105 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13106 cp_parser_label_declaration (parser);
13107 /* Parse an (optional) statement-seq. */
13108 cp_parser_statement_seq_opt (parser, NULL_TREE);
13109 braces.require_close (parser);
13113 /* Declarations [gram.dcl.dcl] */
13115 /* Parse an optional declaration-sequence.
13117 declaration-seq:
13118 declaration
13119 declaration-seq declaration */
13121 static void
13122 cp_parser_declaration_seq_opt (cp_parser* parser)
13124 while (true)
13126 cp_token *token = cp_lexer_peek_token (parser->lexer);
13128 if (token->type == CPP_CLOSE_BRACE
13129 || token->type == CPP_EOF)
13130 break;
13131 else
13132 cp_parser_toplevel_declaration (parser);
13136 /* Parse a declaration.
13138 declaration:
13139 block-declaration
13140 function-definition
13141 template-declaration
13142 explicit-instantiation
13143 explicit-specialization
13144 linkage-specification
13145 namespace-definition
13147 C++17:
13148 deduction-guide
13150 GNU extension:
13152 declaration:
13153 __extension__ declaration */
13155 static void
13156 cp_parser_declaration (cp_parser* parser)
13158 cp_token token1;
13159 cp_token token2;
13160 int saved_pedantic;
13161 void *p;
13162 tree attributes = NULL_TREE;
13164 /* Check for the `__extension__' keyword. */
13165 if (cp_parser_extension_opt (parser, &saved_pedantic))
13167 /* Parse the qualified declaration. */
13168 cp_parser_declaration (parser);
13169 /* Restore the PEDANTIC flag. */
13170 pedantic = saved_pedantic;
13172 return;
13175 /* Try to figure out what kind of declaration is present. */
13176 token1 = *cp_lexer_peek_token (parser->lexer);
13178 if (token1.type != CPP_EOF)
13179 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13180 else
13182 token2.type = CPP_EOF;
13183 token2.keyword = RID_MAX;
13186 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13187 p = obstack_alloc (&declarator_obstack, 0);
13189 /* If the next token is `extern' and the following token is a string
13190 literal, then we have a linkage specification. */
13191 if (token1.keyword == RID_EXTERN
13192 && cp_parser_is_pure_string_literal (&token2))
13193 cp_parser_linkage_specification (parser);
13194 /* If the next token is `template', then we have either a template
13195 declaration, an explicit instantiation, or an explicit
13196 specialization. */
13197 else if (token1.keyword == RID_TEMPLATE)
13199 /* `template <>' indicates a template specialization. */
13200 if (token2.type == CPP_LESS
13201 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13202 cp_parser_explicit_specialization (parser);
13203 /* `template <' indicates a template declaration. */
13204 else if (token2.type == CPP_LESS)
13205 cp_parser_template_declaration (parser, /*member_p=*/false);
13206 /* Anything else must be an explicit instantiation. */
13207 else
13208 cp_parser_explicit_instantiation (parser);
13210 /* If the next token is `export', then we have a template
13211 declaration. */
13212 else if (token1.keyword == RID_EXPORT)
13213 cp_parser_template_declaration (parser, /*member_p=*/false);
13214 /* If the next token is `extern', 'static' or 'inline' and the one
13215 after that is `template', we have a GNU extended explicit
13216 instantiation directive. */
13217 else if (cp_parser_allow_gnu_extensions_p (parser)
13218 && (token1.keyword == RID_EXTERN
13219 || token1.keyword == RID_STATIC
13220 || token1.keyword == RID_INLINE)
13221 && token2.keyword == RID_TEMPLATE)
13222 cp_parser_explicit_instantiation (parser);
13223 /* If the next token is `namespace', check for a named or unnamed
13224 namespace definition. */
13225 else if (token1.keyword == RID_NAMESPACE
13226 && (/* A named namespace definition. */
13227 (token2.type == CPP_NAME
13228 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13229 != CPP_EQ))
13230 || (token2.type == CPP_OPEN_SQUARE
13231 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13232 == CPP_OPEN_SQUARE)
13233 /* An unnamed namespace definition. */
13234 || token2.type == CPP_OPEN_BRACE
13235 || token2.keyword == RID_ATTRIBUTE))
13236 cp_parser_namespace_definition (parser);
13237 /* An inline (associated) namespace definition. */
13238 else if (token1.keyword == RID_INLINE
13239 && token2.keyword == RID_NAMESPACE)
13240 cp_parser_namespace_definition (parser);
13241 /* Objective-C++ declaration/definition. */
13242 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13243 cp_parser_objc_declaration (parser, NULL_TREE);
13244 else if (c_dialect_objc ()
13245 && token1.keyword == RID_ATTRIBUTE
13246 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13247 cp_parser_objc_declaration (parser, attributes);
13248 /* At this point we may have a template declared by a concept
13249 introduction. */
13250 else if (flag_concepts
13251 && cp_parser_template_declaration_after_export (parser,
13252 /*member_p=*/false))
13253 /* We did. */;
13254 else
13255 /* Try to parse a block-declaration, or a function-definition. */
13256 cp_parser_block_declaration (parser, /*statement_p=*/false);
13258 /* Free any declarators allocated. */
13259 obstack_free (&declarator_obstack, p);
13262 /* Parse a namespace-scope declaration. */
13264 static void
13265 cp_parser_toplevel_declaration (cp_parser* parser)
13267 cp_token *token = cp_lexer_peek_token (parser->lexer);
13269 if (token->type == CPP_PRAGMA)
13270 /* A top-level declaration can consist solely of a #pragma. A
13271 nested declaration cannot, so this is done here and not in
13272 cp_parser_declaration. (A #pragma at block scope is
13273 handled in cp_parser_statement.) */
13274 cp_parser_pragma (parser, pragma_external, NULL);
13275 else if (token->type == CPP_SEMICOLON)
13277 /* A declaration consisting of a single semicolon is
13278 invalid. Allow it unless we're being pedantic. */
13279 cp_lexer_consume_token (parser->lexer);
13280 if (!in_system_header_at (input_location))
13281 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13283 else
13284 /* Parse the declaration itself. */
13285 cp_parser_declaration (parser);
13288 /* Parse a block-declaration.
13290 block-declaration:
13291 simple-declaration
13292 asm-definition
13293 namespace-alias-definition
13294 using-declaration
13295 using-directive
13297 GNU Extension:
13299 block-declaration:
13300 __extension__ block-declaration
13302 C++0x Extension:
13304 block-declaration:
13305 static_assert-declaration
13307 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13308 part of a declaration-statement. */
13310 static void
13311 cp_parser_block_declaration (cp_parser *parser,
13312 bool statement_p)
13314 cp_token *token1;
13315 int saved_pedantic;
13317 /* Check for the `__extension__' keyword. */
13318 if (cp_parser_extension_opt (parser, &saved_pedantic))
13320 /* Parse the qualified declaration. */
13321 cp_parser_block_declaration (parser, statement_p);
13322 /* Restore the PEDANTIC flag. */
13323 pedantic = saved_pedantic;
13325 return;
13328 /* Peek at the next token to figure out which kind of declaration is
13329 present. */
13330 token1 = cp_lexer_peek_token (parser->lexer);
13332 /* If the next keyword is `asm', we have an asm-definition. */
13333 if (token1->keyword == RID_ASM)
13335 if (statement_p)
13336 cp_parser_commit_to_tentative_parse (parser);
13337 cp_parser_asm_definition (parser);
13339 /* If the next keyword is `namespace', we have a
13340 namespace-alias-definition. */
13341 else if (token1->keyword == RID_NAMESPACE)
13342 cp_parser_namespace_alias_definition (parser);
13343 /* If the next keyword is `using', we have a
13344 using-declaration, a using-directive, or an alias-declaration. */
13345 else if (token1->keyword == RID_USING)
13347 cp_token *token2;
13349 if (statement_p)
13350 cp_parser_commit_to_tentative_parse (parser);
13351 /* If the token after `using' is `namespace', then we have a
13352 using-directive. */
13353 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13354 if (token2->keyword == RID_NAMESPACE)
13355 cp_parser_using_directive (parser);
13356 /* If the second token after 'using' is '=', then we have an
13357 alias-declaration. */
13358 else if (cxx_dialect >= cxx11
13359 && token2->type == CPP_NAME
13360 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13361 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13362 cp_parser_alias_declaration (parser);
13363 /* Otherwise, it's a using-declaration. */
13364 else
13365 cp_parser_using_declaration (parser,
13366 /*access_declaration_p=*/false);
13368 /* If the next keyword is `__label__' we have a misplaced label
13369 declaration. */
13370 else if (token1->keyword == RID_LABEL)
13372 cp_lexer_consume_token (parser->lexer);
13373 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13374 cp_parser_skip_to_end_of_statement (parser);
13375 /* If the next token is now a `;', consume it. */
13376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13377 cp_lexer_consume_token (parser->lexer);
13379 /* If the next token is `static_assert' we have a static assertion. */
13380 else if (token1->keyword == RID_STATIC_ASSERT)
13381 cp_parser_static_assert (parser, /*member_p=*/false);
13382 /* Anything else must be a simple-declaration. */
13383 else
13384 cp_parser_simple_declaration (parser, !statement_p,
13385 /*maybe_range_for_decl*/NULL);
13388 /* Parse a simple-declaration.
13390 simple-declaration:
13391 decl-specifier-seq [opt] init-declarator-list [opt] ;
13392 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13393 brace-or-equal-initializer ;
13395 init-declarator-list:
13396 init-declarator
13397 init-declarator-list , init-declarator
13399 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13400 function-definition as a simple-declaration.
13402 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13403 parsed declaration if it is an uninitialized single declarator not followed
13404 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13405 if present, will not be consumed. */
13407 static void
13408 cp_parser_simple_declaration (cp_parser* parser,
13409 bool function_definition_allowed_p,
13410 tree *maybe_range_for_decl)
13412 cp_decl_specifier_seq decl_specifiers;
13413 int declares_class_or_enum;
13414 bool saw_declarator;
13415 location_t comma_loc = UNKNOWN_LOCATION;
13416 location_t init_loc = UNKNOWN_LOCATION;
13418 if (maybe_range_for_decl)
13419 *maybe_range_for_decl = NULL_TREE;
13421 /* Defer access checks until we know what is being declared; the
13422 checks for names appearing in the decl-specifier-seq should be
13423 done as if we were in the scope of the thing being declared. */
13424 push_deferring_access_checks (dk_deferred);
13426 /* Parse the decl-specifier-seq. We have to keep track of whether
13427 or not the decl-specifier-seq declares a named class or
13428 enumeration type, since that is the only case in which the
13429 init-declarator-list is allowed to be empty.
13431 [dcl.dcl]
13433 In a simple-declaration, the optional init-declarator-list can be
13434 omitted only when declaring a class or enumeration, that is when
13435 the decl-specifier-seq contains either a class-specifier, an
13436 elaborated-type-specifier, or an enum-specifier. */
13437 cp_parser_decl_specifier_seq (parser,
13438 CP_PARSER_FLAGS_OPTIONAL,
13439 &decl_specifiers,
13440 &declares_class_or_enum);
13441 /* We no longer need to defer access checks. */
13442 stop_deferring_access_checks ();
13444 /* In a block scope, a valid declaration must always have a
13445 decl-specifier-seq. By not trying to parse declarators, we can
13446 resolve the declaration/expression ambiguity more quickly. */
13447 if (!function_definition_allowed_p
13448 && !decl_specifiers.any_specifiers_p)
13450 cp_parser_error (parser, "expected declaration");
13451 goto done;
13454 /* If the next two tokens are both identifiers, the code is
13455 erroneous. The usual cause of this situation is code like:
13457 T t;
13459 where "T" should name a type -- but does not. */
13460 if (!decl_specifiers.any_type_specifiers_p
13461 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13463 /* If parsing tentatively, we should commit; we really are
13464 looking at a declaration. */
13465 cp_parser_commit_to_tentative_parse (parser);
13466 /* Give up. */
13467 goto done;
13470 cp_parser_maybe_commit_to_declaration (parser,
13471 decl_specifiers.any_specifiers_p);
13473 /* Look for C++17 decomposition declaration. */
13474 for (size_t n = 1; ; n++)
13475 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13476 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13477 continue;
13478 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13479 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13480 && decl_specifiers.any_specifiers_p)
13482 tree decl
13483 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13484 maybe_range_for_decl,
13485 &init_loc);
13487 /* The next token should be either a `,' or a `;'. */
13488 cp_token *token = cp_lexer_peek_token (parser->lexer);
13489 /* If it's a `;', we are done. */
13490 if (token->type == CPP_SEMICOLON)
13491 goto finish;
13492 else if (maybe_range_for_decl)
13494 if (*maybe_range_for_decl == NULL_TREE)
13495 *maybe_range_for_decl = error_mark_node;
13496 goto finish;
13498 /* Anything else is an error. */
13499 else
13501 /* If we have already issued an error message we don't need
13502 to issue another one. */
13503 if ((decl != error_mark_node
13504 && DECL_INITIAL (decl) != error_mark_node)
13505 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13506 cp_parser_error (parser, "expected %<,%> or %<;%>");
13507 /* Skip tokens until we reach the end of the statement. */
13508 cp_parser_skip_to_end_of_statement (parser);
13509 /* If the next token is now a `;', consume it. */
13510 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13511 cp_lexer_consume_token (parser->lexer);
13512 goto done;
13515 else
13516 break;
13518 tree last_type;
13519 bool auto_specifier_p;
13520 /* NULL_TREE if both variable and function declaration are allowed,
13521 error_mark_node if function declaration are not allowed and
13522 a FUNCTION_DECL that should be diagnosed if it is followed by
13523 variable declarations. */
13524 tree auto_function_declaration;
13526 last_type = NULL_TREE;
13527 auto_specifier_p
13528 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13529 auto_function_declaration = NULL_TREE;
13531 /* Keep going until we hit the `;' at the end of the simple
13532 declaration. */
13533 saw_declarator = false;
13534 while (cp_lexer_next_token_is_not (parser->lexer,
13535 CPP_SEMICOLON))
13537 cp_token *token;
13538 bool function_definition_p;
13539 tree decl;
13540 tree auto_result = NULL_TREE;
13542 if (saw_declarator)
13544 /* If we are processing next declarator, comma is expected */
13545 token = cp_lexer_peek_token (parser->lexer);
13546 gcc_assert (token->type == CPP_COMMA);
13547 cp_lexer_consume_token (parser->lexer);
13548 if (maybe_range_for_decl)
13550 *maybe_range_for_decl = error_mark_node;
13551 if (comma_loc == UNKNOWN_LOCATION)
13552 comma_loc = token->location;
13555 else
13556 saw_declarator = true;
13558 /* Parse the init-declarator. */
13559 decl = cp_parser_init_declarator (parser,
13560 CP_PARSER_FLAGS_NONE,
13561 &decl_specifiers,
13562 /*checks=*/NULL,
13563 function_definition_allowed_p,
13564 /*member_p=*/false,
13565 declares_class_or_enum,
13566 &function_definition_p,
13567 maybe_range_for_decl,
13568 &init_loc,
13569 &auto_result);
13570 /* If an error occurred while parsing tentatively, exit quickly.
13571 (That usually happens when in the body of a function; each
13572 statement is treated as a declaration-statement until proven
13573 otherwise.) */
13574 if (cp_parser_error_occurred (parser))
13575 goto done;
13577 if (auto_specifier_p && cxx_dialect >= cxx14)
13579 /* If the init-declarator-list contains more than one
13580 init-declarator, they shall all form declarations of
13581 variables. */
13582 if (auto_function_declaration == NULL_TREE)
13583 auto_function_declaration
13584 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13585 else if (TREE_CODE (decl) == FUNCTION_DECL
13586 || auto_function_declaration != error_mark_node)
13588 error_at (decl_specifiers.locations[ds_type_spec],
13589 "non-variable %qD in declaration with more than one "
13590 "declarator with placeholder type",
13591 TREE_CODE (decl) == FUNCTION_DECL
13592 ? decl : auto_function_declaration);
13593 auto_function_declaration = error_mark_node;
13597 if (auto_result
13598 && (!processing_template_decl || !type_uses_auto (auto_result)))
13600 if (last_type
13601 && last_type != error_mark_node
13602 && !same_type_p (auto_result, last_type))
13604 /* If the list of declarators contains more than one declarator,
13605 the type of each declared variable is determined as described
13606 above. If the type deduced for the template parameter U is not
13607 the same in each deduction, the program is ill-formed. */
13608 error_at (decl_specifiers.locations[ds_type_spec],
13609 "inconsistent deduction for %qT: %qT and then %qT",
13610 decl_specifiers.type, last_type, auto_result);
13611 last_type = error_mark_node;
13613 else
13614 last_type = auto_result;
13617 /* Handle function definitions specially. */
13618 if (function_definition_p)
13620 /* If the next token is a `,', then we are probably
13621 processing something like:
13623 void f() {}, *p;
13625 which is erroneous. */
13626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13628 cp_token *token = cp_lexer_peek_token (parser->lexer);
13629 error_at (token->location,
13630 "mixing"
13631 " declarations and function-definitions is forbidden");
13633 /* Otherwise, we're done with the list of declarators. */
13634 else
13636 pop_deferring_access_checks ();
13637 return;
13640 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13641 *maybe_range_for_decl = decl;
13642 /* The next token should be either a `,' or a `;'. */
13643 token = cp_lexer_peek_token (parser->lexer);
13644 /* If it's a `,', there are more declarators to come. */
13645 if (token->type == CPP_COMMA)
13646 /* will be consumed next time around */;
13647 /* If it's a `;', we are done. */
13648 else if (token->type == CPP_SEMICOLON)
13649 break;
13650 else if (maybe_range_for_decl)
13652 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13653 permerror (decl_specifiers.locations[ds_type_spec],
13654 "types may not be defined in a for-range-declaration");
13655 break;
13657 /* Anything else is an error. */
13658 else
13660 /* If we have already issued an error message we don't need
13661 to issue another one. */
13662 if ((decl != error_mark_node
13663 && DECL_INITIAL (decl) != error_mark_node)
13664 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13665 cp_parser_error (parser, "expected %<,%> or %<;%>");
13666 /* Skip tokens until we reach the end of the statement. */
13667 cp_parser_skip_to_end_of_statement (parser);
13668 /* If the next token is now a `;', consume it. */
13669 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13670 cp_lexer_consume_token (parser->lexer);
13671 goto done;
13673 /* After the first time around, a function-definition is not
13674 allowed -- even if it was OK at first. For example:
13676 int i, f() {}
13678 is not valid. */
13679 function_definition_allowed_p = false;
13682 /* Issue an error message if no declarators are present, and the
13683 decl-specifier-seq does not itself declare a class or
13684 enumeration: [dcl.dcl]/3. */
13685 if (!saw_declarator)
13687 if (cp_parser_declares_only_class_p (parser))
13689 if (!declares_class_or_enum
13690 && decl_specifiers.type
13691 && OVERLOAD_TYPE_P (decl_specifiers.type))
13692 /* Ensure an error is issued anyway when finish_decltype_type,
13693 called via cp_parser_decl_specifier_seq, returns a class or
13694 an enumeration (c++/51786). */
13695 decl_specifiers.type = NULL_TREE;
13696 shadow_tag (&decl_specifiers);
13698 /* Perform any deferred access checks. */
13699 perform_deferred_access_checks (tf_warning_or_error);
13702 /* Consume the `;'. */
13703 finish:
13704 if (!maybe_range_for_decl)
13705 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13706 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13708 if (init_loc != UNKNOWN_LOCATION)
13709 error_at (init_loc, "initializer in range-based %<for%> loop");
13710 if (comma_loc != UNKNOWN_LOCATION)
13711 error_at (comma_loc,
13712 "multiple declarations in range-based %<for%> loop");
13715 done:
13716 pop_deferring_access_checks ();
13719 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13720 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13721 initializer ; */
13723 static tree
13724 cp_parser_decomposition_declaration (cp_parser *parser,
13725 cp_decl_specifier_seq *decl_specifiers,
13726 tree *maybe_range_for_decl,
13727 location_t *init_loc)
13729 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13730 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13731 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13733 /* Parse the identifier-list. */
13734 auto_vec<cp_expr, 10> v;
13735 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13736 while (true)
13738 cp_expr e = cp_parser_identifier (parser);
13739 if (e.get_value () == error_mark_node)
13740 break;
13741 v.safe_push (e);
13742 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13743 break;
13744 cp_lexer_consume_token (parser->lexer);
13747 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13748 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13750 end_loc = UNKNOWN_LOCATION;
13751 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13752 false);
13753 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13754 cp_lexer_consume_token (parser->lexer);
13755 else
13757 cp_parser_skip_to_end_of_statement (parser);
13758 return error_mark_node;
13762 if (cxx_dialect < cxx17)
13763 pedwarn (loc, 0, "structured bindings only available with "
13764 "%<-std=c++17%> or %<-std=gnu++17%>");
13766 tree pushed_scope;
13767 cp_declarator *declarator = make_declarator (cdk_decomp);
13768 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13769 declarator->id_loc = loc;
13770 if (ref_qual != REF_QUAL_NONE)
13771 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13772 ref_qual == REF_QUAL_RVALUE,
13773 NULL_TREE);
13774 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13775 NULL_TREE, decl_specifiers->attributes,
13776 &pushed_scope);
13777 tree orig_decl = decl;
13779 unsigned int i;
13780 cp_expr e;
13781 cp_decl_specifier_seq decl_specs;
13782 clear_decl_specs (&decl_specs);
13783 decl_specs.type = make_auto ();
13784 tree prev = decl;
13785 FOR_EACH_VEC_ELT (v, i, e)
13787 if (i == 0)
13788 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13789 sfk_none, e.get_location ());
13790 else
13792 declarator->u.id.unqualified_name = e.get_value ();
13793 declarator->id_loc = e.get_location ();
13795 tree elt_pushed_scope;
13796 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13797 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13798 if (decl2 == error_mark_node)
13799 decl = error_mark_node;
13800 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13802 /* Ensure we've diagnosed redeclaration if we aren't creating
13803 a new VAR_DECL. */
13804 gcc_assert (errorcount);
13805 decl = error_mark_node;
13807 else
13808 prev = decl2;
13809 if (elt_pushed_scope)
13810 pop_scope (elt_pushed_scope);
13813 if (v.is_empty ())
13815 error_at (loc, "empty structured binding declaration");
13816 decl = error_mark_node;
13819 if (maybe_range_for_decl == NULL
13820 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13822 bool non_constant_p = false, is_direct_init = false;
13823 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13824 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13825 &non_constant_p);
13826 if (initializer == NULL_TREE
13827 || (TREE_CODE (initializer) == TREE_LIST
13828 && TREE_CHAIN (initializer))
13829 || (is_direct_init
13830 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13831 && CONSTRUCTOR_NELTS (initializer) != 1))
13833 error_at (loc, "invalid initializer for structured binding "
13834 "declaration");
13835 initializer = error_mark_node;
13838 if (decl != error_mark_node)
13840 cp_maybe_mangle_decomp (decl, prev, v.length ());
13841 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13842 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13843 cp_finish_decomp (decl, prev, v.length ());
13846 else if (decl != error_mark_node)
13848 *maybe_range_for_decl = prev;
13849 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13850 the underlying DECL. */
13851 cp_finish_decomp (decl, prev, v.length ());
13854 if (pushed_scope)
13855 pop_scope (pushed_scope);
13857 if (decl == error_mark_node && DECL_P (orig_decl))
13859 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13860 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13863 return decl;
13866 /* Parse a decl-specifier-seq.
13868 decl-specifier-seq:
13869 decl-specifier-seq [opt] decl-specifier
13870 decl-specifier attribute-specifier-seq [opt] (C++11)
13872 decl-specifier:
13873 storage-class-specifier
13874 type-specifier
13875 function-specifier
13876 friend
13877 typedef
13879 GNU Extension:
13881 decl-specifier:
13882 attributes
13884 Concepts Extension:
13886 decl-specifier:
13887 concept
13889 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13891 The parser flags FLAGS is used to control type-specifier parsing.
13893 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13894 flags:
13896 1: one of the decl-specifiers is an elaborated-type-specifier
13897 (i.e., a type declaration)
13898 2: one of the decl-specifiers is an enum-specifier or a
13899 class-specifier (i.e., a type definition)
13903 static void
13904 cp_parser_decl_specifier_seq (cp_parser* parser,
13905 cp_parser_flags flags,
13906 cp_decl_specifier_seq *decl_specs,
13907 int* declares_class_or_enum)
13909 bool constructor_possible_p = !parser->in_declarator_p;
13910 bool found_decl_spec = false;
13911 cp_token *start_token = NULL;
13912 cp_decl_spec ds;
13914 /* Clear DECL_SPECS. */
13915 clear_decl_specs (decl_specs);
13917 /* Assume no class or enumeration type is declared. */
13918 *declares_class_or_enum = 0;
13920 /* Keep reading specifiers until there are no more to read. */
13921 while (true)
13923 bool constructor_p;
13924 cp_token *token;
13925 ds = ds_last;
13927 /* Peek at the next token. */
13928 token = cp_lexer_peek_token (parser->lexer);
13930 /* Save the first token of the decl spec list for error
13931 reporting. */
13932 if (!start_token)
13933 start_token = token;
13934 /* Handle attributes. */
13935 if (cp_next_tokens_can_be_attribute_p (parser))
13937 /* Parse the attributes. */
13938 tree attrs = cp_parser_attributes_opt (parser);
13940 /* In a sequence of declaration specifiers, c++11 attributes
13941 appertain to the type that precede them. In that case
13942 [dcl.spec]/1 says:
13944 The attribute-specifier-seq affects the type only for
13945 the declaration it appears in, not other declarations
13946 involving the same type.
13948 But for now let's force the user to position the
13949 attribute either at the beginning of the declaration or
13950 after the declarator-id, which would clearly mean that it
13951 applies to the declarator. */
13952 if (cxx11_attribute_p (attrs))
13954 if (!found_decl_spec)
13955 /* The c++11 attribute is at the beginning of the
13956 declaration. It appertains to the entity being
13957 declared. */;
13958 else
13960 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13962 /* This is an attribute following a
13963 class-specifier. */
13964 if (decl_specs->type_definition_p)
13965 warn_misplaced_attr_for_class_type (token->location,
13966 decl_specs->type);
13967 attrs = NULL_TREE;
13969 else
13971 decl_specs->std_attributes
13972 = attr_chainon (decl_specs->std_attributes, attrs);
13973 if (decl_specs->locations[ds_std_attribute] == 0)
13974 decl_specs->locations[ds_std_attribute] = token->location;
13976 continue;
13980 decl_specs->attributes
13981 = attr_chainon (decl_specs->attributes, attrs);
13982 if (decl_specs->locations[ds_attribute] == 0)
13983 decl_specs->locations[ds_attribute] = token->location;
13984 continue;
13986 /* Assume we will find a decl-specifier keyword. */
13987 found_decl_spec = true;
13988 /* If the next token is an appropriate keyword, we can simply
13989 add it to the list. */
13990 switch (token->keyword)
13992 /* decl-specifier:
13993 friend
13994 constexpr */
13995 case RID_FRIEND:
13996 if (!at_class_scope_p ())
13998 gcc_rich_location richloc (token->location);
13999 richloc.add_fixit_remove ();
14000 error_at (&richloc, "%<friend%> used outside of class");
14001 cp_lexer_purge_token (parser->lexer);
14003 else
14005 ds = ds_friend;
14006 /* Consume the token. */
14007 cp_lexer_consume_token (parser->lexer);
14009 break;
14011 case RID_CONSTEXPR:
14012 ds = ds_constexpr;
14013 cp_lexer_consume_token (parser->lexer);
14014 break;
14016 case RID_CONCEPT:
14017 ds = ds_concept;
14018 cp_lexer_consume_token (parser->lexer);
14019 /* In C++20 a concept definition is just 'concept name = expr;'
14020 Support that syntax by pretending we've seen 'bool'. */
14021 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14022 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
14024 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14025 token, /*type_definition*/false);
14026 decl_specs->any_type_specifiers_p = true;
14028 break;
14030 /* function-specifier:
14031 inline
14032 virtual
14033 explicit */
14034 case RID_INLINE:
14035 case RID_VIRTUAL:
14036 case RID_EXPLICIT:
14037 cp_parser_function_specifier_opt (parser, decl_specs);
14038 break;
14040 /* decl-specifier:
14041 typedef */
14042 case RID_TYPEDEF:
14043 ds = ds_typedef;
14044 /* Consume the token. */
14045 cp_lexer_consume_token (parser->lexer);
14046 /* A constructor declarator cannot appear in a typedef. */
14047 constructor_possible_p = false;
14048 /* The "typedef" keyword can only occur in a declaration; we
14049 may as well commit at this point. */
14050 cp_parser_commit_to_tentative_parse (parser);
14052 if (decl_specs->storage_class != sc_none)
14053 decl_specs->conflicting_specifiers_p = true;
14054 break;
14056 /* storage-class-specifier:
14057 auto
14058 register
14059 static
14060 extern
14061 mutable
14063 GNU Extension:
14064 thread */
14065 case RID_AUTO:
14066 if (cxx_dialect == cxx98)
14068 /* Consume the token. */
14069 cp_lexer_consume_token (parser->lexer);
14071 /* Complain about `auto' as a storage specifier, if
14072 we're complaining about C++0x compatibility. */
14073 gcc_rich_location richloc (token->location);
14074 richloc.add_fixit_remove ();
14075 warning_at (&richloc, OPT_Wc__11_compat,
14076 "%<auto%> changes meaning in C++11; "
14077 "please remove it");
14079 /* Set the storage class anyway. */
14080 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14081 token);
14083 else
14084 /* C++0x auto type-specifier. */
14085 found_decl_spec = false;
14086 break;
14088 case RID_REGISTER:
14089 case RID_STATIC:
14090 case RID_EXTERN:
14091 case RID_MUTABLE:
14092 /* Consume the token. */
14093 cp_lexer_consume_token (parser->lexer);
14094 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14095 token);
14096 break;
14097 case RID_THREAD:
14098 /* Consume the token. */
14099 ds = ds_thread;
14100 cp_lexer_consume_token (parser->lexer);
14101 break;
14103 default:
14104 /* We did not yet find a decl-specifier yet. */
14105 found_decl_spec = false;
14106 break;
14109 if (found_decl_spec
14110 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14111 && token->keyword != RID_CONSTEXPR)
14112 error ("%<decl-specifier%> invalid in condition");
14114 if (found_decl_spec
14115 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14116 && token->keyword != RID_MUTABLE
14117 && token->keyword != RID_CONSTEXPR)
14118 error_at (token->location, "%qD invalid in lambda",
14119 ridpointers[token->keyword]);
14121 if (ds != ds_last)
14122 set_and_check_decl_spec_loc (decl_specs, ds, token);
14124 /* Constructors are a special case. The `S' in `S()' is not a
14125 decl-specifier; it is the beginning of the declarator. */
14126 constructor_p
14127 = (!found_decl_spec
14128 && constructor_possible_p
14129 && (cp_parser_constructor_declarator_p
14130 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14131 ds_friend))));
14133 /* If we don't have a DECL_SPEC yet, then we must be looking at
14134 a type-specifier. */
14135 if (!found_decl_spec && !constructor_p)
14137 int decl_spec_declares_class_or_enum;
14138 bool is_cv_qualifier;
14139 tree type_spec;
14141 type_spec
14142 = cp_parser_type_specifier (parser, flags,
14143 decl_specs,
14144 /*is_declaration=*/true,
14145 &decl_spec_declares_class_or_enum,
14146 &is_cv_qualifier);
14147 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14149 /* If this type-specifier referenced a user-defined type
14150 (a typedef, class-name, etc.), then we can't allow any
14151 more such type-specifiers henceforth.
14153 [dcl.spec]
14155 The longest sequence of decl-specifiers that could
14156 possibly be a type name is taken as the
14157 decl-specifier-seq of a declaration. The sequence shall
14158 be self-consistent as described below.
14160 [dcl.type]
14162 As a general rule, at most one type-specifier is allowed
14163 in the complete decl-specifier-seq of a declaration. The
14164 only exceptions are the following:
14166 -- const or volatile can be combined with any other
14167 type-specifier.
14169 -- signed or unsigned can be combined with char, long,
14170 short, or int.
14172 -- ..
14174 Example:
14176 typedef char* Pc;
14177 void g (const int Pc);
14179 Here, Pc is *not* part of the decl-specifier seq; it's
14180 the declarator. Therefore, once we see a type-specifier
14181 (other than a cv-qualifier), we forbid any additional
14182 user-defined types. We *do* still allow things like `int
14183 int' to be considered a decl-specifier-seq, and issue the
14184 error message later. */
14185 if (type_spec && !is_cv_qualifier)
14186 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14187 /* A constructor declarator cannot follow a type-specifier. */
14188 if (type_spec)
14190 constructor_possible_p = false;
14191 found_decl_spec = true;
14192 if (!is_cv_qualifier)
14193 decl_specs->any_type_specifiers_p = true;
14195 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14196 error_at (token->location, "type-specifier invalid in lambda");
14200 /* If we still do not have a DECL_SPEC, then there are no more
14201 decl-specifiers. */
14202 if (!found_decl_spec)
14203 break;
14205 decl_specs->any_specifiers_p = true;
14206 /* After we see one decl-specifier, further decl-specifiers are
14207 always optional. */
14208 flags |= CP_PARSER_FLAGS_OPTIONAL;
14211 /* Don't allow a friend specifier with a class definition. */
14212 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14213 && (*declares_class_or_enum & 2))
14214 error_at (decl_specs->locations[ds_friend],
14215 "class definition may not be declared a friend");
14218 /* Parse an (optional) storage-class-specifier.
14220 storage-class-specifier:
14221 auto
14222 register
14223 static
14224 extern
14225 mutable
14227 GNU Extension:
14229 storage-class-specifier:
14230 thread
14232 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14234 static tree
14235 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14237 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14239 case RID_AUTO:
14240 if (cxx_dialect != cxx98)
14241 return NULL_TREE;
14242 /* Fall through for C++98. */
14243 gcc_fallthrough ();
14245 case RID_REGISTER:
14246 case RID_STATIC:
14247 case RID_EXTERN:
14248 case RID_MUTABLE:
14249 case RID_THREAD:
14250 /* Consume the token. */
14251 return cp_lexer_consume_token (parser->lexer)->u.value;
14253 default:
14254 return NULL_TREE;
14258 /* Parse an (optional) function-specifier.
14260 function-specifier:
14261 inline
14262 virtual
14263 explicit
14265 C++2A Extension:
14266 explicit(constant-expression)
14268 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14269 Updates DECL_SPECS, if it is non-NULL. */
14271 static tree
14272 cp_parser_function_specifier_opt (cp_parser* parser,
14273 cp_decl_specifier_seq *decl_specs)
14275 cp_token *token = cp_lexer_peek_token (parser->lexer);
14276 switch (token->keyword)
14278 case RID_INLINE:
14279 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14280 break;
14282 case RID_VIRTUAL:
14283 /* 14.5.2.3 [temp.mem]
14285 A member function template shall not be virtual. */
14286 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14287 && current_class_type)
14288 error_at (token->location, "templates may not be %<virtual%>");
14289 else
14290 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14291 break;
14293 case RID_EXPLICIT:
14295 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14296 /* If we see '(', it's C++20 explicit(bool). */
14297 tree expr;
14298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14300 matching_parens parens;
14301 parens.consume_open (parser);
14303 /* New types are not allowed in an explicit-specifier. */
14304 const char *saved_message
14305 = parser->type_definition_forbidden_message;
14306 parser->type_definition_forbidden_message
14307 = G_("types may not be defined in explicit-specifier");
14309 if (cxx_dialect < cxx2a)
14310 pedwarn (token->location, 0,
14311 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14312 "or %<-std=gnu++2a%>");
14314 /* Parse the constant-expression. */
14315 expr = cp_parser_constant_expression (parser);
14317 /* Restore the saved message. */
14318 parser->type_definition_forbidden_message = saved_message;
14319 parens.require_close (parser);
14321 else
14322 /* The explicit-specifier explicit without a constant-expression is
14323 equivalent to the explicit-specifier explicit(true). */
14324 expr = boolean_true_node;
14326 /* [dcl.fct.spec]
14327 "the constant-expression, if supplied, shall be a contextually
14328 converted constant expression of type bool." */
14329 expr = build_explicit_specifier (expr, tf_warning_or_error);
14330 /* We could evaluate it -- mark the decl as appropriate. */
14331 if (expr == boolean_true_node)
14332 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14333 else if (expr == boolean_false_node)
14334 /* Don't mark the decl as explicit. */;
14335 else if (decl_specs)
14336 /* The expression was value-dependent. Remember it so that we can
14337 substitute it later. */
14338 decl_specs->explicit_specifier = expr;
14339 return id;
14342 default:
14343 return NULL_TREE;
14346 /* Consume the token. */
14347 return cp_lexer_consume_token (parser->lexer)->u.value;
14350 /* Parse a linkage-specification.
14352 linkage-specification:
14353 extern string-literal { declaration-seq [opt] }
14354 extern string-literal declaration */
14356 static void
14357 cp_parser_linkage_specification (cp_parser* parser)
14359 tree linkage;
14361 /* Look for the `extern' keyword. */
14362 cp_token *extern_token
14363 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14365 /* Look for the string-literal. */
14366 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14367 linkage = cp_parser_string_literal (parser, false, false);
14369 /* Transform the literal into an identifier. If the literal is a
14370 wide-character string, or contains embedded NULs, then we can't
14371 handle it as the user wants. */
14372 if (strlen (TREE_STRING_POINTER (linkage))
14373 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14375 cp_parser_error (parser, "invalid linkage-specification");
14376 /* Assume C++ linkage. */
14377 linkage = lang_name_cplusplus;
14379 else
14380 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14382 /* We're now using the new linkage. */
14383 push_lang_context (linkage);
14385 /* Preserve the location of the the innermost linkage specification,
14386 tracking the locations of nested specifications via a local. */
14387 location_t saved_location
14388 = parser->innermost_linkage_specification_location;
14389 /* Construct a location ranging from the start of the "extern" to
14390 the end of the string-literal, with the caret at the start, e.g.:
14391 extern "C" {
14392 ^~~~~~~~~~
14394 parser->innermost_linkage_specification_location
14395 = make_location (extern_token->location,
14396 extern_token->location,
14397 get_finish (string_token->location));
14399 /* If the next token is a `{', then we're using the first
14400 production. */
14401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14403 cp_ensure_no_omp_declare_simd (parser);
14404 cp_ensure_no_oacc_routine (parser);
14406 /* Consume the `{' token. */
14407 matching_braces braces;
14408 braces.consume_open (parser);
14409 /* Parse the declarations. */
14410 cp_parser_declaration_seq_opt (parser);
14411 /* Look for the closing `}'. */
14412 braces.require_close (parser);
14414 /* Otherwise, there's just one declaration. */
14415 else
14417 bool saved_in_unbraced_linkage_specification_p;
14419 saved_in_unbraced_linkage_specification_p
14420 = parser->in_unbraced_linkage_specification_p;
14421 parser->in_unbraced_linkage_specification_p = true;
14422 cp_parser_declaration (parser);
14423 parser->in_unbraced_linkage_specification_p
14424 = saved_in_unbraced_linkage_specification_p;
14427 /* We're done with the linkage-specification. */
14428 pop_lang_context ();
14430 /* Restore location of parent linkage specification, if any. */
14431 parser->innermost_linkage_specification_location = saved_location;
14434 /* Parse a static_assert-declaration.
14436 static_assert-declaration:
14437 static_assert ( constant-expression , string-literal ) ;
14438 static_assert ( constant-expression ) ; (C++17)
14440 If MEMBER_P, this static_assert is a class member. */
14442 static void
14443 cp_parser_static_assert(cp_parser *parser, bool member_p)
14445 cp_expr condition;
14446 location_t token_loc;
14447 tree message;
14448 bool dummy;
14450 /* Peek at the `static_assert' token so we can keep track of exactly
14451 where the static assertion started. */
14452 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14454 /* Look for the `static_assert' keyword. */
14455 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14456 RT_STATIC_ASSERT))
14457 return;
14459 /* We know we are in a static assertion; commit to any tentative
14460 parse. */
14461 if (cp_parser_parsing_tentatively (parser))
14462 cp_parser_commit_to_tentative_parse (parser);
14464 /* Parse the `(' starting the static assertion condition. */
14465 matching_parens parens;
14466 parens.require_open (parser);
14468 /* Parse the constant-expression. Allow a non-constant expression
14469 here in order to give better diagnostics in finish_static_assert. */
14470 condition =
14471 cp_parser_constant_expression (parser,
14472 /*allow_non_constant_p=*/true,
14473 /*non_constant_p=*/&dummy);
14475 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14477 if (cxx_dialect < cxx17)
14478 pedwarn (input_location, OPT_Wpedantic,
14479 "%<static_assert%> without a message "
14480 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14481 /* Eat the ')' */
14482 cp_lexer_consume_token (parser->lexer);
14483 message = build_string (1, "");
14484 TREE_TYPE (message) = char_array_type_node;
14485 fix_string_type (message);
14487 else
14489 /* Parse the separating `,'. */
14490 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14492 /* Parse the string-literal message. */
14493 message = cp_parser_string_literal (parser,
14494 /*translate=*/false,
14495 /*wide_ok=*/true);
14497 /* A `)' completes the static assertion. */
14498 if (!parens.require_close (parser))
14499 cp_parser_skip_to_closing_parenthesis (parser,
14500 /*recovering=*/true,
14501 /*or_comma=*/false,
14502 /*consume_paren=*/true);
14505 /* A semicolon terminates the declaration. */
14506 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14508 /* Get the location for the static assertion. Use that of the
14509 condition if available, otherwise, use that of the "static_assert"
14510 token. */
14511 location_t assert_loc = condition.get_location ();
14512 if (assert_loc == UNKNOWN_LOCATION)
14513 assert_loc = token_loc;
14515 /* Complete the static assertion, which may mean either processing
14516 the static assert now or saving it for template instantiation. */
14517 finish_static_assert (condition, message, assert_loc, member_p);
14520 /* Parse the expression in decltype ( expression ). */
14522 static tree
14523 cp_parser_decltype_expr (cp_parser *parser,
14524 bool &id_expression_or_member_access_p)
14526 cp_token *id_expr_start_token;
14527 tree expr;
14529 /* Since we're going to preserve any side-effects from this parse, set up a
14530 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14531 in the expression. */
14532 tentative_firewall firewall (parser);
14534 /* First, try parsing an id-expression. */
14535 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14536 cp_parser_parse_tentatively (parser);
14537 expr = cp_parser_id_expression (parser,
14538 /*template_keyword_p=*/false,
14539 /*check_dependency_p=*/true,
14540 /*template_p=*/NULL,
14541 /*declarator_p=*/false,
14542 /*optional_p=*/false);
14544 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14546 bool non_integral_constant_expression_p = false;
14547 tree id_expression = expr;
14548 cp_id_kind idk;
14549 const char *error_msg;
14551 if (identifier_p (expr))
14552 /* Lookup the name we got back from the id-expression. */
14553 expr = cp_parser_lookup_name_simple (parser, expr,
14554 id_expr_start_token->location);
14556 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14557 /* A template without args is not a complete id-expression. */
14558 expr = error_mark_node;
14560 if (expr
14561 && expr != error_mark_node
14562 && TREE_CODE (expr) != TYPE_DECL
14563 && (TREE_CODE (expr) != BIT_NOT_EXPR
14564 || !TYPE_P (TREE_OPERAND (expr, 0)))
14565 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14567 /* Complete lookup of the id-expression. */
14568 expr = (finish_id_expression
14569 (id_expression, expr, parser->scope, &idk,
14570 /*integral_constant_expression_p=*/false,
14571 /*allow_non_integral_constant_expression_p=*/true,
14572 &non_integral_constant_expression_p,
14573 /*template_p=*/false,
14574 /*done=*/true,
14575 /*address_p=*/false,
14576 /*template_arg_p=*/false,
14577 &error_msg,
14578 id_expr_start_token->location));
14580 if (expr == error_mark_node)
14581 /* We found an id-expression, but it was something that we
14582 should not have found. This is an error, not something
14583 we can recover from, so note that we found an
14584 id-expression and we'll recover as gracefully as
14585 possible. */
14586 id_expression_or_member_access_p = true;
14589 if (expr
14590 && expr != error_mark_node
14591 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14592 /* We have an id-expression. */
14593 id_expression_or_member_access_p = true;
14596 if (!id_expression_or_member_access_p)
14598 /* Abort the id-expression parse. */
14599 cp_parser_abort_tentative_parse (parser);
14601 /* Parsing tentatively, again. */
14602 cp_parser_parse_tentatively (parser);
14604 /* Parse a class member access. */
14605 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14606 /*cast_p=*/false, /*decltype*/true,
14607 /*member_access_only_p=*/true, NULL);
14609 if (expr
14610 && expr != error_mark_node
14611 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14612 /* We have an id-expression. */
14613 id_expression_or_member_access_p = true;
14616 if (id_expression_or_member_access_p)
14617 /* We have parsed the complete id-expression or member access. */
14618 cp_parser_parse_definitely (parser);
14619 else
14621 /* Abort our attempt to parse an id-expression or member access
14622 expression. */
14623 cp_parser_abort_tentative_parse (parser);
14625 /* Commit to the tentative_firewall so we get syntax errors. */
14626 cp_parser_commit_to_tentative_parse (parser);
14628 /* Parse a full expression. */
14629 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14630 /*decltype_p=*/true);
14633 return expr;
14636 /* Parse a `decltype' type. Returns the type.
14638 simple-type-specifier:
14639 decltype ( expression )
14640 C++14 proposal:
14641 decltype ( auto ) */
14643 static tree
14644 cp_parser_decltype (cp_parser *parser)
14646 bool id_expression_or_member_access_p = false;
14647 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14649 if (start_token->type == CPP_DECLTYPE)
14651 /* Already parsed. */
14652 cp_lexer_consume_token (parser->lexer);
14653 return saved_checks_value (start_token->u.tree_check_value);
14656 /* Look for the `decltype' token. */
14657 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14658 return error_mark_node;
14660 /* Parse the opening `('. */
14661 matching_parens parens;
14662 if (!parens.require_open (parser))
14663 return error_mark_node;
14665 push_deferring_access_checks (dk_deferred);
14667 tree expr = NULL_TREE;
14669 if (cxx_dialect >= cxx14
14670 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14671 /* decltype (auto) */
14672 cp_lexer_consume_token (parser->lexer);
14673 else
14675 /* decltype (expression) */
14677 /* Types cannot be defined in a `decltype' expression. Save away the
14678 old message and set the new one. */
14679 const char *saved_message = parser->type_definition_forbidden_message;
14680 parser->type_definition_forbidden_message
14681 = G_("types may not be defined in %<decltype%> expressions");
14683 /* The restrictions on constant-expressions do not apply inside
14684 decltype expressions. */
14685 bool saved_integral_constant_expression_p
14686 = parser->integral_constant_expression_p;
14687 bool saved_non_integral_constant_expression_p
14688 = parser->non_integral_constant_expression_p;
14689 parser->integral_constant_expression_p = false;
14691 /* Within a parenthesized expression, a `>' token is always
14692 the greater-than operator. */
14693 bool saved_greater_than_is_operator_p
14694 = parser->greater_than_is_operator_p;
14695 parser->greater_than_is_operator_p = true;
14697 /* Do not actually evaluate the expression. */
14698 ++cp_unevaluated_operand;
14700 /* Do not warn about problems with the expression. */
14701 ++c_inhibit_evaluation_warnings;
14703 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14704 STRIP_ANY_LOCATION_WRAPPER (expr);
14706 /* Go back to evaluating expressions. */
14707 --cp_unevaluated_operand;
14708 --c_inhibit_evaluation_warnings;
14710 /* The `>' token might be the end of a template-id or
14711 template-parameter-list now. */
14712 parser->greater_than_is_operator_p
14713 = saved_greater_than_is_operator_p;
14715 /* Restore the old message and the integral constant expression
14716 flags. */
14717 parser->type_definition_forbidden_message = saved_message;
14718 parser->integral_constant_expression_p
14719 = saved_integral_constant_expression_p;
14720 parser->non_integral_constant_expression_p
14721 = saved_non_integral_constant_expression_p;
14724 /* Parse to the closing `)'. */
14725 if (!parens.require_close (parser))
14727 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14728 /*consume_paren=*/true);
14729 pop_deferring_access_checks ();
14730 return error_mark_node;
14733 if (!expr)
14735 /* Build auto. */
14736 expr = make_decltype_auto ();
14737 AUTO_IS_DECLTYPE (expr) = true;
14739 else
14740 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14741 tf_warning_or_error);
14743 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14744 it again. */
14745 start_token->type = CPP_DECLTYPE;
14746 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14747 start_token->u.tree_check_value->value = expr;
14748 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14749 start_token->keyword = RID_MAX;
14750 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14752 pop_to_parent_deferring_access_checks ();
14754 return expr;
14757 /* Special member functions [gram.special] */
14759 /* Parse a conversion-function-id.
14761 conversion-function-id:
14762 operator conversion-type-id
14764 Returns an IDENTIFIER_NODE representing the operator. */
14766 static tree
14767 cp_parser_conversion_function_id (cp_parser* parser)
14769 tree type;
14770 tree saved_scope;
14771 tree saved_qualifying_scope;
14772 tree saved_object_scope;
14773 tree pushed_scope = NULL_TREE;
14775 /* Look for the `operator' token. */
14776 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14777 return error_mark_node;
14778 /* When we parse the conversion-type-id, the current scope will be
14779 reset. However, we need that information in able to look up the
14780 conversion function later, so we save it here. */
14781 saved_scope = parser->scope;
14782 saved_qualifying_scope = parser->qualifying_scope;
14783 saved_object_scope = parser->object_scope;
14784 /* We must enter the scope of the class so that the names of
14785 entities declared within the class are available in the
14786 conversion-type-id. For example, consider:
14788 struct S {
14789 typedef int I;
14790 operator I();
14793 S::operator I() { ... }
14795 In order to see that `I' is a type-name in the definition, we
14796 must be in the scope of `S'. */
14797 if (saved_scope)
14798 pushed_scope = push_scope (saved_scope);
14799 /* Parse the conversion-type-id. */
14800 type = cp_parser_conversion_type_id (parser);
14801 /* Leave the scope of the class, if any. */
14802 if (pushed_scope)
14803 pop_scope (pushed_scope);
14804 /* Restore the saved scope. */
14805 parser->scope = saved_scope;
14806 parser->qualifying_scope = saved_qualifying_scope;
14807 parser->object_scope = saved_object_scope;
14808 /* If the TYPE is invalid, indicate failure. */
14809 if (type == error_mark_node)
14810 return error_mark_node;
14811 return make_conv_op_name (type);
14814 /* Parse a conversion-type-id:
14816 conversion-type-id:
14817 type-specifier-seq conversion-declarator [opt]
14819 Returns the TYPE specified. */
14821 static tree
14822 cp_parser_conversion_type_id (cp_parser* parser)
14824 tree attributes;
14825 cp_decl_specifier_seq type_specifiers;
14826 cp_declarator *declarator;
14827 tree type_specified;
14828 const char *saved_message;
14830 /* Parse the attributes. */
14831 attributes = cp_parser_attributes_opt (parser);
14833 saved_message = parser->type_definition_forbidden_message;
14834 parser->type_definition_forbidden_message
14835 = G_("types may not be defined in a conversion-type-id");
14837 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
14838 optional in conversion-type-id. */
14839 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
14840 /*is_declaration=*/false,
14841 /*is_trailing_return=*/false,
14842 &type_specifiers);
14844 parser->type_definition_forbidden_message = saved_message;
14846 /* If that didn't work, stop. */
14847 if (type_specifiers.type == error_mark_node)
14848 return error_mark_node;
14849 /* Parse the conversion-declarator. */
14850 declarator = cp_parser_conversion_declarator_opt (parser);
14852 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14853 /*initialized=*/0, &attributes);
14854 if (attributes)
14855 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14857 /* Don't give this error when parsing tentatively. This happens to
14858 work because we always parse this definitively once. */
14859 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14860 && type_uses_auto (type_specified))
14862 if (cxx_dialect < cxx14)
14864 error ("invalid use of %<auto%> in conversion operator");
14865 return error_mark_node;
14867 else if (template_parm_scope_p ())
14868 warning (0, "use of %<auto%> in member template "
14869 "conversion operator can never be deduced");
14872 return type_specified;
14875 /* Parse an (optional) conversion-declarator.
14877 conversion-declarator:
14878 ptr-operator conversion-declarator [opt]
14882 static cp_declarator *
14883 cp_parser_conversion_declarator_opt (cp_parser* parser)
14885 enum tree_code code;
14886 tree class_type, std_attributes = NULL_TREE;
14887 cp_cv_quals cv_quals;
14889 /* We don't know if there's a ptr-operator next, or not. */
14890 cp_parser_parse_tentatively (parser);
14891 /* Try the ptr-operator. */
14892 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14893 &std_attributes);
14894 /* If it worked, look for more conversion-declarators. */
14895 if (cp_parser_parse_definitely (parser))
14897 cp_declarator *declarator;
14899 /* Parse another optional declarator. */
14900 declarator = cp_parser_conversion_declarator_opt (parser);
14902 declarator = cp_parser_make_indirect_declarator
14903 (code, class_type, cv_quals, declarator, std_attributes);
14905 return declarator;
14908 return NULL;
14911 /* Parse an (optional) ctor-initializer.
14913 ctor-initializer:
14914 : mem-initializer-list */
14916 static void
14917 cp_parser_ctor_initializer_opt (cp_parser* parser)
14919 /* If the next token is not a `:', then there is no
14920 ctor-initializer. */
14921 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14923 /* Do default initialization of any bases and members. */
14924 if (DECL_CONSTRUCTOR_P (current_function_decl))
14925 finish_mem_initializers (NULL_TREE);
14926 return;
14929 /* Consume the `:' token. */
14930 cp_lexer_consume_token (parser->lexer);
14931 /* And the mem-initializer-list. */
14932 cp_parser_mem_initializer_list (parser);
14935 /* Parse a mem-initializer-list.
14937 mem-initializer-list:
14938 mem-initializer ... [opt]
14939 mem-initializer ... [opt] , mem-initializer-list */
14941 static void
14942 cp_parser_mem_initializer_list (cp_parser* parser)
14944 tree mem_initializer_list = NULL_TREE;
14945 tree target_ctor = error_mark_node;
14946 cp_token *token = cp_lexer_peek_token (parser->lexer);
14948 /* Let the semantic analysis code know that we are starting the
14949 mem-initializer-list. */
14950 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14951 error_at (token->location,
14952 "only constructors take member initializers");
14954 /* Loop through the list. */
14955 while (true)
14957 tree mem_initializer;
14959 token = cp_lexer_peek_token (parser->lexer);
14960 /* Parse the mem-initializer. */
14961 mem_initializer = cp_parser_mem_initializer (parser);
14962 /* If the next token is a `...', we're expanding member initializers. */
14963 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14964 if (ellipsis
14965 || (mem_initializer != error_mark_node
14966 && check_for_bare_parameter_packs (TREE_PURPOSE
14967 (mem_initializer))))
14969 /* Consume the `...'. */
14970 if (ellipsis)
14971 cp_lexer_consume_token (parser->lexer);
14973 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14974 can be expanded but members cannot. */
14975 if (mem_initializer != error_mark_node
14976 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14978 error_at (token->location,
14979 "cannot expand initializer for member %qD",
14980 TREE_PURPOSE (mem_initializer));
14981 mem_initializer = error_mark_node;
14984 /* Construct the pack expansion type. */
14985 if (mem_initializer != error_mark_node)
14986 mem_initializer = make_pack_expansion (mem_initializer);
14988 if (target_ctor != error_mark_node
14989 && mem_initializer != error_mark_node)
14991 error ("mem-initializer for %qD follows constructor delegation",
14992 TREE_PURPOSE (mem_initializer));
14993 mem_initializer = error_mark_node;
14995 /* Look for a target constructor. */
14996 if (mem_initializer != error_mark_node
14997 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14998 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15000 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15001 if (mem_initializer_list)
15003 error ("constructor delegation follows mem-initializer for %qD",
15004 TREE_PURPOSE (mem_initializer_list));
15005 mem_initializer = error_mark_node;
15007 target_ctor = mem_initializer;
15009 /* Add it to the list, unless it was erroneous. */
15010 if (mem_initializer != error_mark_node)
15012 TREE_CHAIN (mem_initializer) = mem_initializer_list;
15013 mem_initializer_list = mem_initializer;
15015 /* If the next token is not a `,', we're done. */
15016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15017 break;
15018 /* Consume the `,' token. */
15019 cp_lexer_consume_token (parser->lexer);
15022 /* Perform semantic analysis. */
15023 if (DECL_CONSTRUCTOR_P (current_function_decl))
15024 finish_mem_initializers (mem_initializer_list);
15027 /* Parse a mem-initializer.
15029 mem-initializer:
15030 mem-initializer-id ( expression-list [opt] )
15031 mem-initializer-id braced-init-list
15033 GNU extension:
15035 mem-initializer:
15036 ( expression-list [opt] )
15038 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15039 class) or FIELD_DECL (for a non-static data member) to initialize;
15040 the TREE_VALUE is the expression-list. An empty initialization
15041 list is represented by void_list_node. */
15043 static tree
15044 cp_parser_mem_initializer (cp_parser* parser)
15046 tree mem_initializer_id;
15047 tree expression_list;
15048 tree member;
15049 cp_token *token = cp_lexer_peek_token (parser->lexer);
15051 /* Find out what is being initialized. */
15052 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15054 permerror (token->location,
15055 "anachronistic old-style base class initializer");
15056 mem_initializer_id = NULL_TREE;
15058 else
15060 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15061 if (mem_initializer_id == error_mark_node)
15062 return mem_initializer_id;
15064 member = expand_member_init (mem_initializer_id);
15065 if (member && !DECL_P (member))
15066 in_base_initializer = 1;
15068 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15070 bool expr_non_constant_p;
15071 cp_lexer_set_source_position (parser->lexer);
15072 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15073 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15074 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15075 expression_list = build_tree_list (NULL_TREE, expression_list);
15077 else
15079 vec<tree, va_gc> *vec;
15080 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15081 /*cast_p=*/false,
15082 /*allow_expansion_p=*/true,
15083 /*non_constant_p=*/NULL,
15084 /*close_paren_loc=*/NULL,
15085 /*wrap_locations_p=*/true);
15086 if (vec == NULL)
15087 return error_mark_node;
15088 expression_list = build_tree_list_vec (vec);
15089 release_tree_vector (vec);
15092 if (expression_list == error_mark_node)
15093 return error_mark_node;
15094 if (!expression_list)
15095 expression_list = void_type_node;
15097 in_base_initializer = 0;
15099 return member ? build_tree_list (member, expression_list) : error_mark_node;
15102 /* Parse a mem-initializer-id.
15104 mem-initializer-id:
15105 :: [opt] nested-name-specifier [opt] class-name
15106 decltype-specifier (C++11)
15107 identifier
15109 Returns a TYPE indicating the class to be initialized for the first
15110 production (and the second in C++11). Returns an IDENTIFIER_NODE
15111 indicating the data member to be initialized for the last production. */
15113 static tree
15114 cp_parser_mem_initializer_id (cp_parser* parser)
15116 bool global_scope_p;
15117 bool nested_name_specifier_p;
15118 bool template_p = false;
15119 tree id;
15121 cp_token *token = cp_lexer_peek_token (parser->lexer);
15123 /* `typename' is not allowed in this context ([temp.res]). */
15124 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15126 error_at (token->location,
15127 "keyword %<typename%> not allowed in this context (a qualified "
15128 "member initializer is implicitly a type)");
15129 cp_lexer_consume_token (parser->lexer);
15131 /* Look for the optional `::' operator. */
15132 global_scope_p
15133 = (cp_parser_global_scope_opt (parser,
15134 /*current_scope_valid_p=*/false)
15135 != NULL_TREE);
15136 /* Look for the optional nested-name-specifier. The simplest way to
15137 implement:
15139 [temp.res]
15141 The keyword `typename' is not permitted in a base-specifier or
15142 mem-initializer; in these contexts a qualified name that
15143 depends on a template-parameter is implicitly assumed to be a
15144 type name.
15146 is to assume that we have seen the `typename' keyword at this
15147 point. */
15148 nested_name_specifier_p
15149 = (cp_parser_nested_name_specifier_opt (parser,
15150 /*typename_keyword_p=*/true,
15151 /*check_dependency_p=*/true,
15152 /*type_p=*/true,
15153 /*is_declaration=*/true)
15154 != NULL_TREE);
15155 if (nested_name_specifier_p)
15156 template_p = cp_parser_optional_template_keyword (parser);
15157 /* If there is a `::' operator or a nested-name-specifier, then we
15158 are definitely looking for a class-name. */
15159 if (global_scope_p || nested_name_specifier_p)
15160 return cp_parser_class_name (parser,
15161 /*typename_keyword_p=*/true,
15162 /*template_keyword_p=*/template_p,
15163 typename_type,
15164 /*check_dependency_p=*/true,
15165 /*class_head_p=*/false,
15166 /*is_declaration=*/true);
15167 /* Otherwise, we could also be looking for an ordinary identifier. */
15168 cp_parser_parse_tentatively (parser);
15169 if (cp_lexer_next_token_is_decltype (parser->lexer))
15170 /* Try a decltype-specifier. */
15171 id = cp_parser_decltype (parser);
15172 else
15173 /* Otherwise, try a class-name. */
15174 id = cp_parser_class_name (parser,
15175 /*typename_keyword_p=*/true,
15176 /*template_keyword_p=*/false,
15177 none_type,
15178 /*check_dependency_p=*/true,
15179 /*class_head_p=*/false,
15180 /*is_declaration=*/true);
15181 /* If we found one, we're done. */
15182 if (cp_parser_parse_definitely (parser))
15183 return id;
15184 /* Otherwise, look for an ordinary identifier. */
15185 return cp_parser_identifier (parser);
15188 /* Overloading [gram.over] */
15190 /* Parse an operator-function-id.
15192 operator-function-id:
15193 operator operator
15195 Returns an IDENTIFIER_NODE for the operator which is a
15196 human-readable spelling of the identifier, e.g., `operator +'. */
15198 static cp_expr
15199 cp_parser_operator_function_id (cp_parser* parser)
15201 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15202 /* Look for the `operator' keyword. */
15203 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15204 return error_mark_node;
15205 /* And then the name of the operator itself. */
15206 return cp_parser_operator (parser, start_loc);
15209 /* Return an identifier node for a user-defined literal operator.
15210 The suffix identifier is chained to the operator name identifier. */
15212 tree
15213 cp_literal_operator_id (const char* name)
15215 tree identifier;
15216 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15217 + strlen (name) + 10);
15218 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15219 identifier = get_identifier (buffer);
15221 return identifier;
15224 /* Parse an operator.
15226 operator:
15227 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15228 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15229 || ++ -- , ->* -> () []
15231 GNU Extensions:
15233 operator:
15234 <? >? <?= >?=
15236 Returns an IDENTIFIER_NODE for the operator which is a
15237 human-readable spelling of the identifier, e.g., `operator +'. */
15239 static cp_expr
15240 cp_parser_operator (cp_parser* parser, location_t start_loc)
15242 tree id = NULL_TREE;
15243 cp_token *token;
15244 bool utf8 = false;
15246 /* Peek at the next token. */
15247 token = cp_lexer_peek_token (parser->lexer);
15249 location_t end_loc = token->location;
15251 /* Figure out which operator we have. */
15252 enum tree_code op = ERROR_MARK;
15253 bool assop = false;
15254 bool consumed = false;
15255 switch (token->type)
15257 case CPP_KEYWORD:
15259 /* The keyword should be either `new' or `delete'. */
15260 if (token->keyword == RID_NEW)
15261 op = NEW_EXPR;
15262 else if (token->keyword == RID_DELETE)
15263 op = DELETE_EXPR;
15264 else
15265 break;
15267 /* Consume the `new' or `delete' token. */
15268 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15270 /* Peek at the next token. */
15271 token = cp_lexer_peek_token (parser->lexer);
15272 /* If it's a `[' token then this is the array variant of the
15273 operator. */
15274 if (token->type == CPP_OPEN_SQUARE)
15276 /* Consume the `[' token. */
15277 cp_lexer_consume_token (parser->lexer);
15278 /* Look for the `]' token. */
15279 if (cp_token *close_token
15280 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15281 end_loc = close_token->location;
15282 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15284 consumed = true;
15285 break;
15288 case CPP_PLUS:
15289 op = PLUS_EXPR;
15290 break;
15292 case CPP_MINUS:
15293 op = MINUS_EXPR;
15294 break;
15296 case CPP_MULT:
15297 op = MULT_EXPR;
15298 break;
15300 case CPP_DIV:
15301 op = TRUNC_DIV_EXPR;
15302 break;
15304 case CPP_MOD:
15305 op = TRUNC_MOD_EXPR;
15306 break;
15308 case CPP_XOR:
15309 op = BIT_XOR_EXPR;
15310 break;
15312 case CPP_AND:
15313 op = BIT_AND_EXPR;
15314 break;
15316 case CPP_OR:
15317 op = BIT_IOR_EXPR;
15318 break;
15320 case CPP_COMPL:
15321 op = BIT_NOT_EXPR;
15322 break;
15324 case CPP_NOT:
15325 op = TRUTH_NOT_EXPR;
15326 break;
15328 case CPP_EQ:
15329 assop = true;
15330 op = NOP_EXPR;
15331 break;
15333 case CPP_LESS:
15334 op = LT_EXPR;
15335 break;
15337 case CPP_GREATER:
15338 op = GT_EXPR;
15339 break;
15341 case CPP_PLUS_EQ:
15342 assop = true;
15343 op = PLUS_EXPR;
15344 break;
15346 case CPP_MINUS_EQ:
15347 assop = true;
15348 op = MINUS_EXPR;
15349 break;
15351 case CPP_MULT_EQ:
15352 assop = true;
15353 op = MULT_EXPR;
15354 break;
15356 case CPP_DIV_EQ:
15357 assop = true;
15358 op = TRUNC_DIV_EXPR;
15359 break;
15361 case CPP_MOD_EQ:
15362 assop = true;
15363 op = TRUNC_MOD_EXPR;
15364 break;
15366 case CPP_XOR_EQ:
15367 assop = true;
15368 op = BIT_XOR_EXPR;
15369 break;
15371 case CPP_AND_EQ:
15372 assop = true;
15373 op = BIT_AND_EXPR;
15374 break;
15376 case CPP_OR_EQ:
15377 assop = true;
15378 op = BIT_IOR_EXPR;
15379 break;
15381 case CPP_LSHIFT:
15382 op = LSHIFT_EXPR;
15383 break;
15385 case CPP_RSHIFT:
15386 op = RSHIFT_EXPR;
15387 break;
15389 case CPP_LSHIFT_EQ:
15390 assop = true;
15391 op = LSHIFT_EXPR;
15392 break;
15394 case CPP_RSHIFT_EQ:
15395 assop = true;
15396 op = RSHIFT_EXPR;
15397 break;
15399 case CPP_EQ_EQ:
15400 op = EQ_EXPR;
15401 break;
15403 case CPP_NOT_EQ:
15404 op = NE_EXPR;
15405 break;
15407 case CPP_LESS_EQ:
15408 op = LE_EXPR;
15409 break;
15411 case CPP_GREATER_EQ:
15412 op = GE_EXPR;
15413 break;
15415 case CPP_AND_AND:
15416 op = TRUTH_ANDIF_EXPR;
15417 break;
15419 case CPP_OR_OR:
15420 op = TRUTH_ORIF_EXPR;
15421 break;
15423 case CPP_PLUS_PLUS:
15424 op = POSTINCREMENT_EXPR;
15425 break;
15427 case CPP_MINUS_MINUS:
15428 op = PREDECREMENT_EXPR;
15429 break;
15431 case CPP_COMMA:
15432 op = COMPOUND_EXPR;
15433 break;
15435 case CPP_DEREF_STAR:
15436 op = MEMBER_REF;
15437 break;
15439 case CPP_DEREF:
15440 op = COMPONENT_REF;
15441 break;
15443 case CPP_OPEN_PAREN:
15445 /* Consume the `('. */
15446 matching_parens parens;
15447 parens.consume_open (parser);
15448 /* Look for the matching `)'. */
15449 token = parens.require_close (parser);
15450 if (token)
15451 end_loc = token->location;
15452 op = CALL_EXPR;
15453 consumed = true;
15454 break;
15457 case CPP_OPEN_SQUARE:
15458 /* Consume the `['. */
15459 cp_lexer_consume_token (parser->lexer);
15460 /* Look for the matching `]'. */
15461 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15462 if (token)
15463 end_loc = token->location;
15464 op = ARRAY_REF;
15465 consumed = true;
15466 break;
15468 case CPP_UTF8STRING:
15469 case CPP_UTF8STRING_USERDEF:
15470 utf8 = true;
15471 /* FALLTHRU */
15472 case CPP_STRING:
15473 case CPP_WSTRING:
15474 case CPP_STRING16:
15475 case CPP_STRING32:
15476 case CPP_STRING_USERDEF:
15477 case CPP_WSTRING_USERDEF:
15478 case CPP_STRING16_USERDEF:
15479 case CPP_STRING32_USERDEF:
15481 cp_expr str;
15482 tree string_tree;
15483 int sz, len;
15485 if (cxx_dialect == cxx98)
15486 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15488 /* Consume the string. */
15489 str = cp_parser_string_literal (parser, /*translate=*/true,
15490 /*wide_ok=*/true, /*lookup_udlit=*/false);
15491 if (str == error_mark_node)
15492 return error_mark_node;
15493 else if (TREE_CODE (str) == USERDEF_LITERAL)
15495 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15496 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15497 end_loc = str.get_location ();
15499 else
15501 string_tree = str;
15502 /* Look for the suffix identifier. */
15503 token = cp_lexer_peek_token (parser->lexer);
15504 if (token->type == CPP_NAME)
15506 id = cp_parser_identifier (parser);
15507 end_loc = token->location;
15509 else if (token->type == CPP_KEYWORD)
15511 error ("unexpected keyword;"
15512 " remove space between quotes and suffix identifier");
15513 return error_mark_node;
15515 else
15517 error ("expected suffix identifier");
15518 return error_mark_node;
15521 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15522 (TREE_TYPE (TREE_TYPE (string_tree))));
15523 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15524 if (len != 0)
15526 error ("expected empty string after %<operator%> keyword");
15527 return error_mark_node;
15529 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15530 != char_type_node)
15532 error ("invalid encoding prefix in literal operator");
15533 return error_mark_node;
15535 if (id != error_mark_node)
15537 const char *name = IDENTIFIER_POINTER (id);
15538 id = cp_literal_operator_id (name);
15540 /* Generate a location of the form:
15541 "" _suffix_identifier
15542 ^~~~~~~~~~~~~~~~~~~~~
15543 with caret == start at the start token, finish at the end of the
15544 suffix identifier. */
15545 location_t combined_loc
15546 = make_location (start_loc, start_loc, parser->lexer);
15547 return cp_expr (id, combined_loc);
15550 default:
15551 /* Anything else is an error. */
15552 break;
15555 /* If we have selected an identifier, we need to consume the
15556 operator token. */
15557 if (op != ERROR_MARK)
15559 id = ovl_op_identifier (assop, op);
15560 if (!consumed)
15561 cp_lexer_consume_token (parser->lexer);
15563 /* Otherwise, no valid operator name was present. */
15564 else
15566 cp_parser_error (parser, "expected operator");
15567 id = error_mark_node;
15570 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15571 return cp_expr (id, start_loc);
15574 /* Parse a template-declaration.
15576 template-declaration:
15577 export [opt] template < template-parameter-list > declaration
15579 If MEMBER_P is TRUE, this template-declaration occurs within a
15580 class-specifier.
15582 The grammar rule given by the standard isn't correct. What
15583 is really meant is:
15585 template-declaration:
15586 export [opt] template-parameter-list-seq
15587 decl-specifier-seq [opt] init-declarator [opt] ;
15588 export [opt] template-parameter-list-seq
15589 function-definition
15591 template-parameter-list-seq:
15592 template-parameter-list-seq [opt]
15593 template < template-parameter-list >
15595 Concept Extensions:
15597 template-parameter-list-seq:
15598 template < template-parameter-list > requires-clause [opt]
15600 requires-clause:
15601 requires logical-or-expression */
15603 static void
15604 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15606 /* Check for `export'. */
15607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15609 /* Consume the `export' token. */
15610 cp_lexer_consume_token (parser->lexer);
15611 /* Warn that we do not support `export'. */
15612 warning (0, "keyword %<export%> not implemented, and will be ignored");
15615 cp_parser_template_declaration_after_export (parser, member_p);
15618 /* Parse a template-parameter-list.
15620 template-parameter-list:
15621 template-parameter
15622 template-parameter-list , template-parameter
15624 Returns a TREE_LIST. Each node represents a template parameter.
15625 The nodes are connected via their TREE_CHAINs. */
15627 static tree
15628 cp_parser_template_parameter_list (cp_parser* parser)
15630 tree parameter_list = NULL_TREE;
15632 /* Don't create wrapper nodes within a template-parameter-list,
15633 since we don't want to have different types based on the
15634 spelling location of constants and decls within them. */
15635 auto_suppress_location_wrappers sentinel;
15637 begin_template_parm_list ();
15639 /* The loop below parses the template parms. We first need to know
15640 the total number of template parms to be able to compute proper
15641 canonical types of each dependent type. So after the loop, when
15642 we know the total number of template parms,
15643 end_template_parm_list computes the proper canonical types and
15644 fixes up the dependent types accordingly. */
15645 while (true)
15647 tree parameter;
15648 bool is_non_type;
15649 bool is_parameter_pack;
15650 location_t parm_loc;
15652 /* Parse the template-parameter. */
15653 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15654 parameter = cp_parser_template_parameter (parser,
15655 &is_non_type,
15656 &is_parameter_pack);
15657 /* Add it to the list. */
15658 if (parameter != error_mark_node)
15659 parameter_list = process_template_parm (parameter_list,
15660 parm_loc,
15661 parameter,
15662 is_non_type,
15663 is_parameter_pack);
15664 else
15666 tree err_parm = build_tree_list (parameter, parameter);
15667 parameter_list = chainon (parameter_list, err_parm);
15670 /* If the next token is not a `,', we're done. */
15671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15672 break;
15673 /* Otherwise, consume the `,' token. */
15674 cp_lexer_consume_token (parser->lexer);
15677 return end_template_parm_list (parameter_list);
15680 /* Parse a introduction-list.
15682 introduction-list:
15683 introduced-parameter
15684 introduction-list , introduced-parameter
15686 introduced-parameter:
15687 ...[opt] identifier
15689 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15690 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15691 WILDCARD_DECL will also have DECL_NAME set and token location in
15692 DECL_SOURCE_LOCATION. */
15694 static tree
15695 cp_parser_introduction_list (cp_parser *parser)
15697 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15699 while (true)
15701 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15702 if (is_pack)
15703 cp_lexer_consume_token (parser->lexer);
15705 tree identifier = cp_parser_identifier (parser);
15706 if (identifier == error_mark_node)
15707 break;
15709 /* Build placeholder. */
15710 tree parm = build_nt (WILDCARD_DECL);
15711 DECL_SOURCE_LOCATION (parm)
15712 = cp_lexer_peek_token (parser->lexer)->location;
15713 DECL_NAME (parm) = identifier;
15714 WILDCARD_PACK_P (parm) = is_pack;
15715 vec_safe_push (introduction_vec, parm);
15717 /* If the next token is not a `,', we're done. */
15718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15719 break;
15720 /* Otherwise, consume the `,' token. */
15721 cp_lexer_consume_token (parser->lexer);
15724 /* Convert the vec into a TREE_VEC. */
15725 tree introduction_list = make_tree_vec (introduction_vec->length ());
15726 unsigned int n;
15727 tree parm;
15728 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15729 TREE_VEC_ELT (introduction_list, n) = parm;
15731 release_tree_vector (introduction_vec);
15732 return introduction_list;
15735 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15736 is an abstract declarator. */
15738 static inline cp_declarator*
15739 get_id_declarator (cp_declarator *declarator)
15741 cp_declarator *d = declarator;
15742 while (d && d->kind != cdk_id)
15743 d = d->declarator;
15744 return d;
15747 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15748 is an abstract declarator. */
15750 static inline tree
15751 get_unqualified_id (cp_declarator *declarator)
15753 declarator = get_id_declarator (declarator);
15754 if (declarator)
15755 return declarator->u.id.unqualified_name;
15756 else
15757 return NULL_TREE;
15760 /* Returns true if DECL represents a constrained-parameter. */
15762 static inline bool
15763 is_constrained_parameter (tree decl)
15765 return (decl
15766 && TREE_CODE (decl) == TYPE_DECL
15767 && CONSTRAINED_PARM_CONCEPT (decl)
15768 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15771 /* Returns true if PARM declares a constrained-parameter. */
15773 static inline bool
15774 is_constrained_parameter (cp_parameter_declarator *parm)
15776 return is_constrained_parameter (parm->decl_specifiers.type);
15779 /* Check that the type parameter is only a declarator-id, and that its
15780 type is not cv-qualified. */
15782 bool
15783 cp_parser_check_constrained_type_parm (cp_parser *parser,
15784 cp_parameter_declarator *parm)
15786 if (!parm->declarator)
15787 return true;
15789 if (parm->declarator->kind != cdk_id)
15791 cp_parser_error (parser, "invalid constrained type parameter");
15792 return false;
15795 /* Don't allow cv-qualified type parameters. */
15796 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15797 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15799 cp_parser_error (parser, "cv-qualified type parameter");
15800 return false;
15803 return true;
15806 /* Finish parsing/processing a template type parameter and checking
15807 various restrictions. */
15809 static inline tree
15810 cp_parser_constrained_type_template_parm (cp_parser *parser,
15811 tree id,
15812 cp_parameter_declarator* parmdecl)
15814 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15815 return finish_template_type_parm (class_type_node, id);
15816 else
15817 return error_mark_node;
15820 static tree
15821 finish_constrained_template_template_parm (tree proto, tree id)
15823 /* FIXME: This should probably be copied, and we may need to adjust
15824 the template parameter depths. */
15825 tree saved_parms = current_template_parms;
15826 begin_template_parm_list ();
15827 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15828 end_template_parm_list ();
15830 tree parm = finish_template_template_parm (class_type_node, id);
15831 current_template_parms = saved_parms;
15833 return parm;
15836 /* Finish parsing/processing a template template parameter by borrowing
15837 the template parameter list from the prototype parameter. */
15839 static tree
15840 cp_parser_constrained_template_template_parm (cp_parser *parser,
15841 tree proto,
15842 tree id,
15843 cp_parameter_declarator *parmdecl)
15845 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15846 return error_mark_node;
15847 return finish_constrained_template_template_parm (proto, id);
15850 /* Create a new non-type template parameter from the given PARM
15851 declarator. */
15853 static tree
15854 constrained_non_type_template_parm (bool *is_non_type,
15855 cp_parameter_declarator *parm)
15857 *is_non_type = true;
15858 cp_declarator *decl = parm->declarator;
15859 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15860 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15861 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15864 /* Build a constrained template parameter based on the PARMDECL
15865 declarator. The type of PARMDECL is the constrained type, which
15866 refers to the prototype template parameter that ultimately
15867 specifies the type of the declared parameter. */
15869 static tree
15870 finish_constrained_parameter (cp_parser *parser,
15871 cp_parameter_declarator *parmdecl,
15872 bool *is_non_type,
15873 bool *is_parameter_pack)
15875 tree decl = parmdecl->decl_specifiers.type;
15876 tree id = get_unqualified_id (parmdecl->declarator);
15877 tree def = parmdecl->default_argument;
15878 tree proto = DECL_INITIAL (decl);
15880 /* A template parameter constrained by a variadic concept shall also
15881 be declared as a template parameter pack. */
15882 bool is_variadic = template_parameter_pack_p (proto);
15883 if (is_variadic && !*is_parameter_pack)
15884 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15886 /* Build the parameter. Return an error if the declarator was invalid. */
15887 tree parm;
15888 if (TREE_CODE (proto) == TYPE_DECL)
15889 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15890 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15891 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15892 parmdecl);
15893 else
15894 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15895 if (parm == error_mark_node)
15896 return error_mark_node;
15898 /* Finish the parameter decl and create a node attaching the
15899 default argument and constraint. */
15900 parm = build_tree_list (def, parm);
15901 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15903 return parm;
15906 /* Returns true if the parsed type actually represents the declaration
15907 of a type template-parameter. */
15909 static inline bool
15910 declares_constrained_type_template_parameter (tree type)
15912 return (is_constrained_parameter (type)
15913 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15917 /* Returns true if the parsed type actually represents the declaration of
15918 a template template-parameter. */
15920 static bool
15921 declares_constrained_template_template_parameter (tree type)
15923 return (is_constrained_parameter (type)
15924 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15927 /* Parse a default argument for a type template-parameter.
15928 Note that diagnostics are handled in cp_parser_template_parameter. */
15930 static tree
15931 cp_parser_default_type_template_argument (cp_parser *parser)
15933 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15935 /* Consume the `=' token. */
15936 cp_lexer_consume_token (parser->lexer);
15938 cp_token *token = cp_lexer_peek_token (parser->lexer);
15940 /* Parse the default-argument. */
15941 push_deferring_access_checks (dk_no_deferred);
15942 tree default_argument = cp_parser_type_id (parser,
15943 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15944 NULL);
15945 pop_deferring_access_checks ();
15947 if (flag_concepts && type_uses_auto (default_argument))
15949 error_at (token->location,
15950 "invalid use of %<auto%> in default template argument");
15951 return error_mark_node;
15954 return default_argument;
15957 /* Parse a default argument for a template template-parameter. */
15959 static tree
15960 cp_parser_default_template_template_argument (cp_parser *parser)
15962 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15964 bool is_template;
15966 /* Consume the `='. */
15967 cp_lexer_consume_token (parser->lexer);
15968 /* Parse the id-expression. */
15969 push_deferring_access_checks (dk_no_deferred);
15970 /* save token before parsing the id-expression, for error
15971 reporting */
15972 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15973 tree default_argument
15974 = cp_parser_id_expression (parser,
15975 /*template_keyword_p=*/false,
15976 /*check_dependency_p=*/true,
15977 /*template_p=*/&is_template,
15978 /*declarator_p=*/false,
15979 /*optional_p=*/false);
15980 if (TREE_CODE (default_argument) == TYPE_DECL)
15981 /* If the id-expression was a template-id that refers to
15982 a template-class, we already have the declaration here,
15983 so no further lookup is needed. */
15985 else
15986 /* Look up the name. */
15987 default_argument
15988 = cp_parser_lookup_name (parser, default_argument,
15989 none_type,
15990 /*is_template=*/is_template,
15991 /*is_namespace=*/false,
15992 /*check_dependency=*/true,
15993 /*ambiguous_decls=*/NULL,
15994 token->location);
15995 /* See if the default argument is valid. */
15996 default_argument = check_template_template_default_arg (default_argument);
15997 pop_deferring_access_checks ();
15998 return default_argument;
16001 /* Parse a template-parameter.
16003 template-parameter:
16004 type-parameter
16005 parameter-declaration
16007 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16008 the parameter. The TREE_PURPOSE is the default value, if any.
16009 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16010 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16011 set to true iff this parameter is a parameter pack. */
16013 static tree
16014 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16015 bool *is_parameter_pack)
16017 cp_token *token;
16018 cp_parameter_declarator *parameter_declarator;
16019 tree parm;
16021 /* Assume it is a type parameter or a template parameter. */
16022 *is_non_type = false;
16023 /* Assume it not a parameter pack. */
16024 *is_parameter_pack = false;
16025 /* Peek at the next token. */
16026 token = cp_lexer_peek_token (parser->lexer);
16027 /* If it is `template', we have a type-parameter. */
16028 if (token->keyword == RID_TEMPLATE)
16029 return cp_parser_type_parameter (parser, is_parameter_pack);
16030 /* If it is `class' or `typename' we do not know yet whether it is a
16031 type parameter or a non-type parameter. Consider:
16033 template <typename T, typename T::X X> ...
16037 template <class C, class D*> ...
16039 Here, the first parameter is a type parameter, and the second is
16040 a non-type parameter. We can tell by looking at the token after
16041 the identifier -- if it is a `,', `=', or `>' then we have a type
16042 parameter. */
16043 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16045 /* Peek at the token after `class' or `typename'. */
16046 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16047 /* If it's an ellipsis, we have a template type parameter
16048 pack. */
16049 if (token->type == CPP_ELLIPSIS)
16050 return cp_parser_type_parameter (parser, is_parameter_pack);
16051 /* If it's an identifier, skip it. */
16052 if (token->type == CPP_NAME)
16053 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16054 /* Now, see if the token looks like the end of a template
16055 parameter. */
16056 if (token->type == CPP_COMMA
16057 || token->type == CPP_EQ
16058 || token->type == CPP_GREATER)
16059 return cp_parser_type_parameter (parser, is_parameter_pack);
16062 /* Otherwise, it is a non-type parameter or a constrained parameter.
16064 [temp.param]
16066 When parsing a default template-argument for a non-type
16067 template-parameter, the first non-nested `>' is taken as the end
16068 of the template parameter-list rather than a greater-than
16069 operator. */
16070 parameter_declarator
16071 = cp_parser_parameter_declaration (parser,
16072 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16073 /*template_parm_p=*/true,
16074 /*parenthesized_p=*/NULL);
16076 if (!parameter_declarator)
16077 return error_mark_node;
16079 /* If the parameter declaration is marked as a parameter pack, set
16080 *IS_PARAMETER_PACK to notify the caller. */
16081 if (parameter_declarator->template_parameter_pack_p)
16082 *is_parameter_pack = true;
16084 if (parameter_declarator->default_argument)
16086 /* Can happen in some cases of erroneous input (c++/34892). */
16087 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16088 /* Consume the `...' for better error recovery. */
16089 cp_lexer_consume_token (parser->lexer);
16092 // The parameter may have been constrained.
16093 if (is_constrained_parameter (parameter_declarator))
16094 return finish_constrained_parameter (parser,
16095 parameter_declarator,
16096 is_non_type,
16097 is_parameter_pack);
16099 // Now we're sure that the parameter is a non-type parameter.
16100 *is_non_type = true;
16102 parm = grokdeclarator (parameter_declarator->declarator,
16103 &parameter_declarator->decl_specifiers,
16104 TPARM, /*initialized=*/0,
16105 /*attrlist=*/NULL);
16106 if (parm == error_mark_node)
16107 return error_mark_node;
16109 return build_tree_list (parameter_declarator->default_argument, parm);
16112 /* Parse a type-parameter.
16114 type-parameter:
16115 class identifier [opt]
16116 class identifier [opt] = type-id
16117 typename identifier [opt]
16118 typename identifier [opt] = type-id
16119 template < template-parameter-list > class identifier [opt]
16120 template < template-parameter-list > class identifier [opt]
16121 = id-expression
16123 GNU Extension (variadic templates):
16125 type-parameter:
16126 class ... identifier [opt]
16127 typename ... identifier [opt]
16129 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16130 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16131 the declaration of the parameter.
16133 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16135 static tree
16136 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16138 cp_token *token;
16139 tree parameter;
16141 /* Look for a keyword to tell us what kind of parameter this is. */
16142 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16143 if (!token)
16144 return error_mark_node;
16146 switch (token->keyword)
16148 case RID_CLASS:
16149 case RID_TYPENAME:
16151 tree identifier;
16152 tree default_argument;
16154 /* If the next token is an ellipsis, we have a template
16155 argument pack. */
16156 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16158 /* Consume the `...' token. */
16159 cp_lexer_consume_token (parser->lexer);
16160 maybe_warn_variadic_templates ();
16162 *is_parameter_pack = true;
16165 /* If the next token is an identifier, then it names the
16166 parameter. */
16167 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16168 identifier = cp_parser_identifier (parser);
16169 else
16170 identifier = NULL_TREE;
16172 /* Create the parameter. */
16173 parameter = finish_template_type_parm (class_type_node, identifier);
16175 /* If the next token is an `=', we have a default argument. */
16176 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16178 default_argument
16179 = cp_parser_default_type_template_argument (parser);
16181 /* Template parameter packs cannot have default
16182 arguments. */
16183 if (*is_parameter_pack)
16185 if (identifier)
16186 error_at (token->location,
16187 "template parameter pack %qD cannot have a "
16188 "default argument", identifier);
16189 else
16190 error_at (token->location,
16191 "template parameter packs cannot have "
16192 "default arguments");
16193 default_argument = NULL_TREE;
16195 else if (check_for_bare_parameter_packs (default_argument))
16196 default_argument = error_mark_node;
16198 else
16199 default_argument = NULL_TREE;
16201 /* Create the combined representation of the parameter and the
16202 default argument. */
16203 parameter = build_tree_list (default_argument, parameter);
16205 break;
16207 case RID_TEMPLATE:
16209 tree identifier;
16210 tree default_argument;
16212 /* Look for the `<'. */
16213 cp_parser_require (parser, CPP_LESS, RT_LESS);
16214 /* Parse the template-parameter-list. */
16215 cp_parser_template_parameter_list (parser);
16216 /* Look for the `>'. */
16217 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16219 // If template requirements are present, parse them.
16220 if (flag_concepts)
16222 tree reqs = get_shorthand_constraints (current_template_parms);
16223 if (tree r = cp_parser_requires_clause_opt (parser))
16224 reqs = conjoin_constraints (reqs, normalize_expression (r));
16225 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16228 /* Look for the `class' or 'typename' keywords. */
16229 cp_parser_type_parameter_key (parser);
16230 /* If the next token is an ellipsis, we have a template
16231 argument pack. */
16232 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16234 /* Consume the `...' token. */
16235 cp_lexer_consume_token (parser->lexer);
16236 maybe_warn_variadic_templates ();
16238 *is_parameter_pack = true;
16240 /* If the next token is an `=', then there is a
16241 default-argument. If the next token is a `>', we are at
16242 the end of the parameter-list. If the next token is a `,',
16243 then we are at the end of this parameter. */
16244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16245 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16246 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16248 identifier = cp_parser_identifier (parser);
16249 /* Treat invalid names as if the parameter were nameless. */
16250 if (identifier == error_mark_node)
16251 identifier = NULL_TREE;
16253 else
16254 identifier = NULL_TREE;
16256 /* Create the template parameter. */
16257 parameter = finish_template_template_parm (class_type_node,
16258 identifier);
16260 /* If the next token is an `=', then there is a
16261 default-argument. */
16262 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16264 default_argument
16265 = cp_parser_default_template_template_argument (parser);
16267 /* Template parameter packs cannot have default
16268 arguments. */
16269 if (*is_parameter_pack)
16271 if (identifier)
16272 error_at (token->location,
16273 "template parameter pack %qD cannot "
16274 "have a default argument",
16275 identifier);
16276 else
16277 error_at (token->location, "template parameter packs cannot "
16278 "have default arguments");
16279 default_argument = NULL_TREE;
16282 else
16283 default_argument = NULL_TREE;
16285 /* Create the combined representation of the parameter and the
16286 default argument. */
16287 parameter = build_tree_list (default_argument, parameter);
16289 break;
16291 default:
16292 gcc_unreachable ();
16293 break;
16296 return parameter;
16299 /* Parse a template-id.
16301 template-id:
16302 template-name < template-argument-list [opt] >
16304 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16305 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16306 returned. Otherwise, if the template-name names a function, or set
16307 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16308 names a class, returns a TYPE_DECL for the specialization.
16310 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16311 uninstantiated templates. */
16313 static tree
16314 cp_parser_template_id (cp_parser *parser,
16315 bool template_keyword_p,
16316 bool check_dependency_p,
16317 enum tag_types tag_type,
16318 bool is_declaration)
16320 tree templ;
16321 tree arguments;
16322 tree template_id;
16323 cp_token_position start_of_id = 0;
16324 cp_token *next_token = NULL, *next_token_2 = NULL;
16325 bool is_identifier;
16327 /* If the next token corresponds to a template-id, there is no need
16328 to reparse it. */
16329 cp_token *token = cp_lexer_peek_token (parser->lexer);
16330 if (token->type == CPP_TEMPLATE_ID)
16332 cp_lexer_consume_token (parser->lexer);
16333 return saved_checks_value (token->u.tree_check_value);
16336 /* Avoid performing name lookup if there is no possibility of
16337 finding a template-id. */
16338 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16339 || (token->type == CPP_NAME
16340 && !cp_parser_nth_token_starts_template_argument_list_p
16341 (parser, 2)))
16343 cp_parser_error (parser, "expected template-id");
16344 return error_mark_node;
16347 /* Remember where the template-id starts. */
16348 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16349 start_of_id = cp_lexer_token_position (parser->lexer, false);
16351 push_deferring_access_checks (dk_deferred);
16353 /* Parse the template-name. */
16354 is_identifier = false;
16355 templ = cp_parser_template_name (parser, template_keyword_p,
16356 check_dependency_p,
16357 is_declaration,
16358 tag_type,
16359 &is_identifier);
16361 /* Push any access checks inside the firewall we're about to create. */
16362 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16363 pop_deferring_access_checks ();
16364 if (templ == error_mark_node || is_identifier)
16365 return templ;
16367 /* Since we're going to preserve any side-effects from this parse, set up a
16368 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16369 in the template arguments. */
16370 tentative_firewall firewall (parser);
16371 reopen_deferring_access_checks (checks);
16373 /* If we find the sequence `[:' after a template-name, it's probably
16374 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16375 parse correctly the argument list. */
16376 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16377 == CPP_OPEN_SQUARE)
16378 && next_token->flags & DIGRAPH
16379 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16380 == CPP_COLON)
16381 && !(next_token_2->flags & PREV_WHITE))
16383 cp_parser_parse_tentatively (parser);
16384 /* Change `:' into `::'. */
16385 next_token_2->type = CPP_SCOPE;
16386 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16387 CPP_LESS. */
16388 cp_lexer_consume_token (parser->lexer);
16390 /* Parse the arguments. */
16391 arguments = cp_parser_enclosed_template_argument_list (parser);
16392 if (!cp_parser_parse_definitely (parser))
16394 /* If we couldn't parse an argument list, then we revert our changes
16395 and return simply an error. Maybe this is not a template-id
16396 after all. */
16397 next_token_2->type = CPP_COLON;
16398 cp_parser_error (parser, "expected %<<%>");
16399 pop_deferring_access_checks ();
16400 return error_mark_node;
16402 /* Otherwise, emit an error about the invalid digraph, but continue
16403 parsing because we got our argument list. */
16404 if (permerror (next_token->location,
16405 "%<<::%> cannot begin a template-argument list"))
16407 static bool hint = false;
16408 inform (next_token->location,
16409 "%<<:%> is an alternate spelling for %<[%>."
16410 " Insert whitespace between %<<%> and %<::%>");
16411 if (!hint && !flag_permissive)
16413 inform (next_token->location, "(if you use %<-fpermissive%> "
16414 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16415 "accept your code)");
16416 hint = true;
16420 else
16422 /* Look for the `<' that starts the template-argument-list. */
16423 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16425 pop_deferring_access_checks ();
16426 return error_mark_node;
16428 /* Parse the arguments. */
16429 arguments = cp_parser_enclosed_template_argument_list (parser);
16431 if ((cxx_dialect > cxx17)
16432 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16433 && !template_keyword_p
16434 && (cp_parser_error_occurred (parser)
16435 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16437 /* This didn't go well. */
16438 if (TREE_CODE (templ) == FUNCTION_DECL)
16440 /* C++2A says that "function-name < a;" is now ill-formed. */
16441 if (cp_parser_error_occurred (parser))
16443 error_at (token->location, "invalid template-argument-list");
16444 inform (token->location, "function name as the left hand "
16445 "operand of %<<%> is ill-formed in C++2a; wrap the "
16446 "function name in %<()%>");
16448 else
16449 /* We expect "f<targs>" to be followed by "(args)". */
16450 error_at (cp_lexer_peek_token (parser->lexer)->location,
16451 "expected %<(%> after template-argument-list");
16452 if (start_of_id)
16453 /* Purge all subsequent tokens. */
16454 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16456 else
16457 cp_parser_simulate_error (parser);
16458 pop_deferring_access_checks ();
16459 return error_mark_node;
16463 /* Set the location to be of the form:
16464 template-name < template-argument-list [opt] >
16465 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16466 with caret == start at the start of the template-name,
16467 ranging until the closing '>'. */
16468 location_t combined_loc
16469 = make_location (token->location, token->location, parser->lexer);
16471 /* Check for concepts autos where they don't belong. We could
16472 identify types in some cases of idnetifier TEMPL, looking ahead
16473 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16474 types. We reject them in functions, but if what we have is an
16475 identifier, even with none_type we can't conclude it's NOT a
16476 type, we have to wait for template substitution. */
16477 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16478 template_id = error_mark_node;
16479 /* Build a representation of the specialization. */
16480 else if (identifier_p (templ))
16481 template_id = build_min_nt_loc (combined_loc,
16482 TEMPLATE_ID_EXPR,
16483 templ, arguments);
16484 else if (DECL_TYPE_TEMPLATE_P (templ)
16485 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16487 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16488 template (rather than some instantiation thereof) only if
16489 is not nested within some other construct. For example, in
16490 "template <typename T> void f(T) { A<T>::", A<T> is just an
16491 instantiation of A. */
16492 bool entering_scope
16493 = (template_parm_scope_p ()
16494 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16495 template_id
16496 = finish_template_type (templ, arguments, entering_scope);
16498 /* A template-like identifier may be a partial concept id. */
16499 else if (flag_concepts
16500 && (template_id = (cp_parser_maybe_partial_concept_id
16501 (parser, templ, arguments))))
16502 return template_id;
16503 else if (variable_template_p (templ))
16505 template_id = lookup_template_variable (templ, arguments);
16506 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16507 SET_EXPR_LOCATION (template_id, combined_loc);
16509 else
16511 /* If it's not a class-template or a template-template, it should be
16512 a function-template. */
16513 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16515 template_id = lookup_template_function (templ, arguments);
16516 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16517 SET_EXPR_LOCATION (template_id, combined_loc);
16520 /* If parsing tentatively, replace the sequence of tokens that makes
16521 up the template-id with a CPP_TEMPLATE_ID token. That way,
16522 should we re-parse the token stream, we will not have to repeat
16523 the effort required to do the parse, nor will we issue duplicate
16524 error messages about problems during instantiation of the
16525 template. */
16526 if (start_of_id
16527 /* Don't do this if we had a parse error in a declarator; re-parsing
16528 might succeed if a name changes meaning (60361). */
16529 && !(cp_parser_error_occurred (parser)
16530 && cp_parser_parsing_tentatively (parser)
16531 && parser->in_declarator_p))
16533 /* Reset the contents of the START_OF_ID token. */
16534 token->type = CPP_TEMPLATE_ID;
16535 token->location = combined_loc;
16537 /* Retrieve any deferred checks. Do not pop this access checks yet
16538 so the memory will not be reclaimed during token replacing below. */
16539 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16540 token->u.tree_check_value->value = template_id;
16541 token->u.tree_check_value->checks = get_deferred_access_checks ();
16542 token->keyword = RID_MAX;
16544 /* Purge all subsequent tokens. */
16545 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16547 /* ??? Can we actually assume that, if template_id ==
16548 error_mark_node, we will have issued a diagnostic to the
16549 user, as opposed to simply marking the tentative parse as
16550 failed? */
16551 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16552 error_at (token->location, "parse error in template argument list");
16555 pop_to_parent_deferring_access_checks ();
16556 return template_id;
16559 /* Parse a template-name.
16561 template-name:
16562 identifier
16564 The standard should actually say:
16566 template-name:
16567 identifier
16568 operator-function-id
16570 A defect report has been filed about this issue.
16572 A conversion-function-id cannot be a template name because they cannot
16573 be part of a template-id. In fact, looking at this code:
16575 a.operator K<int>()
16577 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16578 It is impossible to call a templated conversion-function-id with an
16579 explicit argument list, since the only allowed template parameter is
16580 the type to which it is converting.
16582 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16583 `template' keyword, in a construction like:
16585 T::template f<3>()
16587 In that case `f' is taken to be a template-name, even though there
16588 is no way of knowing for sure.
16590 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16591 name refers to a set of overloaded functions, at least one of which
16592 is a template, or an IDENTIFIER_NODE with the name of the template,
16593 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16594 names are looked up inside uninstantiated templates. */
16596 static tree
16597 cp_parser_template_name (cp_parser* parser,
16598 bool template_keyword_p,
16599 bool check_dependency_p,
16600 bool is_declaration,
16601 enum tag_types tag_type,
16602 bool *is_identifier)
16604 tree identifier;
16605 tree decl;
16606 cp_token *token = cp_lexer_peek_token (parser->lexer);
16608 /* If the next token is `operator', then we have either an
16609 operator-function-id or a conversion-function-id. */
16610 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16612 /* We don't know whether we're looking at an
16613 operator-function-id or a conversion-function-id. */
16614 cp_parser_parse_tentatively (parser);
16615 /* Try an operator-function-id. */
16616 identifier = cp_parser_operator_function_id (parser);
16617 /* If that didn't work, try a conversion-function-id. */
16618 if (!cp_parser_parse_definitely (parser))
16620 cp_parser_error (parser, "expected template-name");
16621 return error_mark_node;
16624 /* Look for the identifier. */
16625 else
16626 identifier = cp_parser_identifier (parser);
16628 /* If we didn't find an identifier, we don't have a template-id. */
16629 if (identifier == error_mark_node)
16630 return error_mark_node;
16632 /* If the name immediately followed the `template' keyword, then it
16633 is a template-name. However, if the next token is not `<', then
16634 we do not treat it as a template-name, since it is not being used
16635 as part of a template-id. This enables us to handle constructs
16636 like:
16638 template <typename T> struct S { S(); };
16639 template <typename T> S<T>::S();
16641 correctly. We would treat `S' as a template -- if it were `S<T>'
16642 -- but we do not if there is no `<'. */
16644 if (processing_template_decl
16645 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16647 /* In a declaration, in a dependent context, we pretend that the
16648 "template" keyword was present in order to improve error
16649 recovery. For example, given:
16651 template <typename T> void f(T::X<int>);
16653 we want to treat "X<int>" as a template-id. */
16654 if (is_declaration
16655 && !template_keyword_p
16656 && parser->scope && TYPE_P (parser->scope)
16657 && check_dependency_p
16658 && dependent_scope_p (parser->scope)
16659 /* Do not do this for dtors (or ctors), since they never
16660 need the template keyword before their name. */
16661 && !constructor_name_p (identifier, parser->scope))
16663 cp_token_position start = 0;
16665 /* Explain what went wrong. */
16666 error_at (token->location, "non-template %qD used as template",
16667 identifier);
16668 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16669 parser->scope, identifier);
16670 /* If parsing tentatively, find the location of the "<" token. */
16671 if (cp_parser_simulate_error (parser))
16672 start = cp_lexer_token_position (parser->lexer, true);
16673 /* Parse the template arguments so that we can issue error
16674 messages about them. */
16675 cp_lexer_consume_token (parser->lexer);
16676 cp_parser_enclosed_template_argument_list (parser);
16677 /* Skip tokens until we find a good place from which to
16678 continue parsing. */
16679 cp_parser_skip_to_closing_parenthesis (parser,
16680 /*recovering=*/true,
16681 /*or_comma=*/true,
16682 /*consume_paren=*/false);
16683 /* If parsing tentatively, permanently remove the
16684 template argument list. That will prevent duplicate
16685 error messages from being issued about the missing
16686 "template" keyword. */
16687 if (start)
16688 cp_lexer_purge_tokens_after (parser->lexer, start);
16689 if (is_identifier)
16690 *is_identifier = true;
16691 parser->context->object_type = NULL_TREE;
16692 return identifier;
16695 /* If the "template" keyword is present, then there is generally
16696 no point in doing name-lookup, so we just return IDENTIFIER.
16697 But, if the qualifying scope is non-dependent then we can
16698 (and must) do name-lookup normally. */
16699 if (template_keyword_p)
16701 tree scope = (parser->scope ? parser->scope
16702 : parser->context->object_type);
16703 if (scope && TYPE_P (scope)
16704 && (!CLASS_TYPE_P (scope)
16705 || (check_dependency_p && dependent_type_p (scope))))
16707 /* We're optimizing away the call to cp_parser_lookup_name, but
16708 we still need to do this. */
16709 parser->context->object_type = NULL_TREE;
16710 return identifier;
16715 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16716 const bool scoped_p = ((parser->scope ? parser->scope
16717 : parser->context->object_type) != NULL_TREE);
16719 /* Look up the name. */
16720 decl = cp_parser_lookup_name (parser, identifier,
16721 tag_type,
16722 /*is_template=*/true,
16723 /*is_namespace=*/false,
16724 check_dependency_p,
16725 /*ambiguous_decls=*/NULL,
16726 token->location);
16728 decl = strip_using_decl (decl);
16730 /* If DECL is a template, then the name was a template-name. */
16731 if (TREE_CODE (decl) == TEMPLATE_DECL)
16733 if (TREE_DEPRECATED (decl)
16734 && deprecated_state != DEPRECATED_SUPPRESS)
16735 warn_deprecated_use (decl, NULL_TREE);
16737 else
16739 /* The standard does not explicitly indicate whether a name that
16740 names a set of overloaded declarations, some of which are
16741 templates, is a template-name. However, such a name should
16742 be a template-name; otherwise, there is no way to form a
16743 template-id for the overloaded templates. */
16744 bool found = false;
16746 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16747 !found && iter; ++iter)
16748 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16749 found = true;
16751 if (!found
16752 && (cxx_dialect > cxx17)
16753 && !scoped_p
16754 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16755 && tag_type == none_type)
16757 /* [temp.names] says "A name is also considered to refer to a template
16758 if it is an unqualified-id followed by a < and name lookup finds
16759 either one or more functions or finds nothing." */
16761 /* The "more functions" case. Just use the OVERLOAD as normally.
16762 We don't use is_overloaded_fn here to avoid considering
16763 BASELINKs. */
16764 if (TREE_CODE (decl) == OVERLOAD
16765 /* Name lookup found one function. */
16766 || TREE_CODE (decl) == FUNCTION_DECL)
16767 found = true;
16768 /* Name lookup found nothing. */
16769 else if (decl == error_mark_node)
16770 return identifier;
16773 if (!found)
16775 /* The name does not name a template. */
16776 cp_parser_error (parser, "expected template-name");
16777 return error_mark_node;
16781 return decl;
16784 /* Parse a template-argument-list.
16786 template-argument-list:
16787 template-argument ... [opt]
16788 template-argument-list , template-argument ... [opt]
16790 Returns a TREE_VEC containing the arguments. */
16792 static tree
16793 cp_parser_template_argument_list (cp_parser* parser)
16795 tree fixed_args[10];
16796 unsigned n_args = 0;
16797 unsigned alloced = 10;
16798 tree *arg_ary = fixed_args;
16799 tree vec;
16800 bool saved_in_template_argument_list_p;
16801 bool saved_ice_p;
16802 bool saved_non_ice_p;
16804 /* Don't create location wrapper nodes within a template-argument-list. */
16805 auto_suppress_location_wrappers sentinel;
16807 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16808 parser->in_template_argument_list_p = true;
16809 /* Even if the template-id appears in an integral
16810 constant-expression, the contents of the argument list do
16811 not. */
16812 saved_ice_p = parser->integral_constant_expression_p;
16813 parser->integral_constant_expression_p = false;
16814 saved_non_ice_p = parser->non_integral_constant_expression_p;
16815 parser->non_integral_constant_expression_p = false;
16817 /* Parse the arguments. */
16820 tree argument;
16822 if (n_args)
16823 /* Consume the comma. */
16824 cp_lexer_consume_token (parser->lexer);
16826 /* Parse the template-argument. */
16827 argument = cp_parser_template_argument (parser);
16829 /* If the next token is an ellipsis, we're expanding a template
16830 argument pack. */
16831 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16833 if (argument == error_mark_node)
16835 cp_token *token = cp_lexer_peek_token (parser->lexer);
16836 error_at (token->location,
16837 "expected parameter pack before %<...%>");
16839 /* Consume the `...' token. */
16840 cp_lexer_consume_token (parser->lexer);
16842 /* Make the argument into a TYPE_PACK_EXPANSION or
16843 EXPR_PACK_EXPANSION. */
16844 argument = make_pack_expansion (argument);
16847 if (n_args == alloced)
16849 alloced *= 2;
16851 if (arg_ary == fixed_args)
16853 arg_ary = XNEWVEC (tree, alloced);
16854 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16856 else
16857 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16859 arg_ary[n_args++] = argument;
16861 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16863 vec = make_tree_vec (n_args);
16865 while (n_args--)
16866 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16868 if (arg_ary != fixed_args)
16869 free (arg_ary);
16870 parser->non_integral_constant_expression_p = saved_non_ice_p;
16871 parser->integral_constant_expression_p = saved_ice_p;
16872 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16873 if (CHECKING_P)
16874 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16875 return vec;
16878 /* Parse a template-argument.
16880 template-argument:
16881 assignment-expression
16882 type-id
16883 id-expression
16885 The representation is that of an assignment-expression, type-id, or
16886 id-expression -- except that the qualified id-expression is
16887 evaluated, so that the value returned is either a DECL or an
16888 OVERLOAD.
16890 Although the standard says "assignment-expression", it forbids
16891 throw-expressions or assignments in the template argument.
16892 Therefore, we use "conditional-expression" instead. */
16894 static tree
16895 cp_parser_template_argument (cp_parser* parser)
16897 tree argument;
16898 bool template_p;
16899 bool address_p;
16900 bool maybe_type_id = false;
16901 cp_token *token = NULL, *argument_start_token = NULL;
16902 location_t loc = 0;
16903 cp_id_kind idk;
16905 /* There's really no way to know what we're looking at, so we just
16906 try each alternative in order.
16908 [temp.arg]
16910 In a template-argument, an ambiguity between a type-id and an
16911 expression is resolved to a type-id, regardless of the form of
16912 the corresponding template-parameter.
16914 Therefore, we try a type-id first. */
16915 cp_parser_parse_tentatively (parser);
16916 argument = cp_parser_template_type_arg (parser);
16917 /* If there was no error parsing the type-id but the next token is a
16918 '>>', our behavior depends on which dialect of C++ we're
16919 parsing. In C++98, we probably found a typo for '> >'. But there
16920 are type-id which are also valid expressions. For instance:
16922 struct X { int operator >> (int); };
16923 template <int V> struct Foo {};
16924 Foo<X () >> 5> r;
16926 Here 'X()' is a valid type-id of a function type, but the user just
16927 wanted to write the expression "X() >> 5". Thus, we remember that we
16928 found a valid type-id, but we still try to parse the argument as an
16929 expression to see what happens.
16931 In C++0x, the '>>' will be considered two separate '>'
16932 tokens. */
16933 if (!cp_parser_error_occurred (parser)
16934 && cxx_dialect == cxx98
16935 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16937 maybe_type_id = true;
16938 cp_parser_abort_tentative_parse (parser);
16940 else
16942 /* If the next token isn't a `,' or a `>', then this argument wasn't
16943 really finished. This means that the argument is not a valid
16944 type-id. */
16945 if (!cp_parser_next_token_ends_template_argument_p (parser))
16946 cp_parser_error (parser, "expected template-argument");
16947 /* If that worked, we're done. */
16948 if (cp_parser_parse_definitely (parser))
16949 return argument;
16951 /* We're still not sure what the argument will be. */
16952 cp_parser_parse_tentatively (parser);
16953 /* Try a template. */
16954 argument_start_token = cp_lexer_peek_token (parser->lexer);
16955 argument = cp_parser_id_expression (parser,
16956 /*template_keyword_p=*/false,
16957 /*check_dependency_p=*/true,
16958 &template_p,
16959 /*declarator_p=*/false,
16960 /*optional_p=*/false);
16961 /* If the next token isn't a `,' or a `>', then this argument wasn't
16962 really finished. */
16963 if (!cp_parser_next_token_ends_template_argument_p (parser))
16964 cp_parser_error (parser, "expected template-argument");
16965 if (!cp_parser_error_occurred (parser))
16967 /* Figure out what is being referred to. If the id-expression
16968 was for a class template specialization, then we will have a
16969 TYPE_DECL at this point. There is no need to do name lookup
16970 at this point in that case. */
16971 if (TREE_CODE (argument) != TYPE_DECL)
16972 argument = cp_parser_lookup_name (parser, argument,
16973 none_type,
16974 /*is_template=*/template_p,
16975 /*is_namespace=*/false,
16976 /*check_dependency=*/true,
16977 /*ambiguous_decls=*/NULL,
16978 argument_start_token->location);
16979 /* Handle a constrained-type-specifier for a non-type template
16980 parameter. */
16981 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16982 argument = decl;
16983 else if (TREE_CODE (argument) != TEMPLATE_DECL
16984 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16985 cp_parser_error (parser, "expected template-name");
16987 if (cp_parser_parse_definitely (parser))
16989 if (TREE_DEPRECATED (argument))
16990 warn_deprecated_use (argument, NULL_TREE);
16991 return argument;
16993 /* It must be a non-type argument. In C++17 any constant-expression is
16994 allowed. */
16995 if (cxx_dialect > cxx14)
16996 goto general_expr;
16998 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17000 -- an integral constant-expression of integral or enumeration
17001 type; or
17003 -- the name of a non-type template-parameter; or
17005 -- the name of an object or function with external linkage...
17007 -- the address of an object or function with external linkage...
17009 -- a pointer to member... */
17010 /* Look for a non-type template parameter. */
17011 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17013 cp_parser_parse_tentatively (parser);
17014 argument = cp_parser_primary_expression (parser,
17015 /*address_p=*/false,
17016 /*cast_p=*/false,
17017 /*template_arg_p=*/true,
17018 &idk);
17019 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17020 || !cp_parser_next_token_ends_template_argument_p (parser))
17021 cp_parser_simulate_error (parser);
17022 if (cp_parser_parse_definitely (parser))
17023 return argument;
17026 /* If the next token is "&", the argument must be the address of an
17027 object or function with external linkage. */
17028 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17029 if (address_p)
17031 loc = cp_lexer_peek_token (parser->lexer)->location;
17032 cp_lexer_consume_token (parser->lexer);
17034 /* See if we might have an id-expression. */
17035 token = cp_lexer_peek_token (parser->lexer);
17036 if (token->type == CPP_NAME
17037 || token->keyword == RID_OPERATOR
17038 || token->type == CPP_SCOPE
17039 || token->type == CPP_TEMPLATE_ID
17040 || token->type == CPP_NESTED_NAME_SPECIFIER)
17042 cp_parser_parse_tentatively (parser);
17043 argument = cp_parser_primary_expression (parser,
17044 address_p,
17045 /*cast_p=*/false,
17046 /*template_arg_p=*/true,
17047 &idk);
17048 if (cp_parser_error_occurred (parser)
17049 || !cp_parser_next_token_ends_template_argument_p (parser))
17050 cp_parser_abort_tentative_parse (parser);
17051 else
17053 tree probe;
17055 if (INDIRECT_REF_P (argument))
17057 /* Strip the dereference temporarily. */
17058 gcc_assert (REFERENCE_REF_P (argument));
17059 argument = TREE_OPERAND (argument, 0);
17062 /* If we're in a template, we represent a qualified-id referring
17063 to a static data member as a SCOPE_REF even if the scope isn't
17064 dependent so that we can check access control later. */
17065 probe = argument;
17066 if (TREE_CODE (probe) == SCOPE_REF)
17067 probe = TREE_OPERAND (probe, 1);
17068 if (VAR_P (probe))
17070 /* A variable without external linkage might still be a
17071 valid constant-expression, so no error is issued here
17072 if the external-linkage check fails. */
17073 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17074 cp_parser_simulate_error (parser);
17076 else if (is_overloaded_fn (argument))
17077 /* All overloaded functions are allowed; if the external
17078 linkage test does not pass, an error will be issued
17079 later. */
17081 else if (address_p
17082 && (TREE_CODE (argument) == OFFSET_REF
17083 || TREE_CODE (argument) == SCOPE_REF))
17084 /* A pointer-to-member. */
17086 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17088 else
17089 cp_parser_simulate_error (parser);
17091 if (cp_parser_parse_definitely (parser))
17093 if (address_p)
17094 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17095 tf_warning_or_error);
17096 else
17097 argument = convert_from_reference (argument);
17098 return argument;
17102 /* If the argument started with "&", there are no other valid
17103 alternatives at this point. */
17104 if (address_p)
17106 cp_parser_error (parser, "invalid non-type template argument");
17107 return error_mark_node;
17110 general_expr:
17111 /* If the argument wasn't successfully parsed as a type-id followed
17112 by '>>', the argument can only be a constant expression now.
17113 Otherwise, we try parsing the constant-expression tentatively,
17114 because the argument could really be a type-id. */
17115 if (maybe_type_id)
17116 cp_parser_parse_tentatively (parser);
17118 if (cxx_dialect <= cxx14)
17119 argument = cp_parser_constant_expression (parser);
17120 else
17122 /* In C++20, we can encounter a braced-init-list. */
17123 if (cxx_dialect >= cxx2a
17124 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17126 bool expr_non_constant_p;
17127 return cp_parser_braced_list (parser, &expr_non_constant_p);
17130 /* With C++17 generalized non-type template arguments we need to handle
17131 lvalue constant expressions, too. */
17132 argument = cp_parser_assignment_expression (parser);
17133 require_potential_constant_expression (argument);
17136 if (!maybe_type_id)
17137 return argument;
17138 if (!cp_parser_next_token_ends_template_argument_p (parser))
17139 cp_parser_error (parser, "expected template-argument");
17140 if (cp_parser_parse_definitely (parser))
17141 return argument;
17142 /* We did our best to parse the argument as a non type-id, but that
17143 was the only alternative that matched (albeit with a '>' after
17144 it). We can assume it's just a typo from the user, and a
17145 diagnostic will then be issued. */
17146 return cp_parser_template_type_arg (parser);
17149 /* Parse an explicit-instantiation.
17151 explicit-instantiation:
17152 template declaration
17154 Although the standard says `declaration', what it really means is:
17156 explicit-instantiation:
17157 template decl-specifier-seq [opt] declarator [opt] ;
17159 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17160 supposed to be allowed. A defect report has been filed about this
17161 issue.
17163 GNU Extension:
17165 explicit-instantiation:
17166 storage-class-specifier template
17167 decl-specifier-seq [opt] declarator [opt] ;
17168 function-specifier template
17169 decl-specifier-seq [opt] declarator [opt] ; */
17171 static void
17172 cp_parser_explicit_instantiation (cp_parser* parser)
17174 int declares_class_or_enum;
17175 cp_decl_specifier_seq decl_specifiers;
17176 tree extension_specifier = NULL_TREE;
17178 timevar_push (TV_TEMPLATE_INST);
17180 /* Look for an (optional) storage-class-specifier or
17181 function-specifier. */
17182 if (cp_parser_allow_gnu_extensions_p (parser))
17184 extension_specifier
17185 = cp_parser_storage_class_specifier_opt (parser);
17186 if (!extension_specifier)
17187 extension_specifier
17188 = cp_parser_function_specifier_opt (parser,
17189 /*decl_specs=*/NULL);
17192 /* Look for the `template' keyword. */
17193 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17194 /* Let the front end know that we are processing an explicit
17195 instantiation. */
17196 begin_explicit_instantiation ();
17197 /* [temp.explicit] says that we are supposed to ignore access
17198 control while processing explicit instantiation directives. */
17199 push_deferring_access_checks (dk_no_check);
17200 /* Parse a decl-specifier-seq. */
17201 cp_parser_decl_specifier_seq (parser,
17202 CP_PARSER_FLAGS_OPTIONAL,
17203 &decl_specifiers,
17204 &declares_class_or_enum);
17205 /* If there was exactly one decl-specifier, and it declared a class,
17206 and there's no declarator, then we have an explicit type
17207 instantiation. */
17208 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17210 tree type;
17212 type = check_tag_decl (&decl_specifiers,
17213 /*explicit_type_instantiation_p=*/true);
17214 /* Turn access control back on for names used during
17215 template instantiation. */
17216 pop_deferring_access_checks ();
17217 if (type)
17218 do_type_instantiation (type, extension_specifier,
17219 /*complain=*/tf_error);
17221 else
17223 cp_declarator *declarator;
17224 tree decl;
17226 /* Parse the declarator. */
17227 declarator
17228 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17229 CP_PARSER_FLAGS_NONE,
17230 /*ctor_dtor_or_conv_p=*/NULL,
17231 /*parenthesized_p=*/NULL,
17232 /*member_p=*/false,
17233 /*friend_p=*/false,
17234 /*static_p=*/false);
17235 if (declares_class_or_enum & 2)
17236 cp_parser_check_for_definition_in_return_type (declarator,
17237 decl_specifiers.type,
17238 decl_specifiers.locations[ds_type_spec]);
17239 if (declarator != cp_error_declarator)
17241 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17242 permerror (decl_specifiers.locations[ds_inline],
17243 "explicit instantiation shall not use"
17244 " %<inline%> specifier");
17245 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17246 permerror (decl_specifiers.locations[ds_constexpr],
17247 "explicit instantiation shall not use"
17248 " %<constexpr%> specifier");
17250 decl = grokdeclarator (declarator, &decl_specifiers,
17251 NORMAL, 0, &decl_specifiers.attributes);
17252 /* Turn access control back on for names used during
17253 template instantiation. */
17254 pop_deferring_access_checks ();
17255 /* Do the explicit instantiation. */
17256 do_decl_instantiation (decl, extension_specifier);
17258 else
17260 pop_deferring_access_checks ();
17261 /* Skip the body of the explicit instantiation. */
17262 cp_parser_skip_to_end_of_statement (parser);
17265 /* We're done with the instantiation. */
17266 end_explicit_instantiation ();
17268 cp_parser_consume_semicolon_at_end_of_statement (parser);
17270 timevar_pop (TV_TEMPLATE_INST);
17273 /* Parse an explicit-specialization.
17275 explicit-specialization:
17276 template < > declaration
17278 Although the standard says `declaration', what it really means is:
17280 explicit-specialization:
17281 template <> decl-specifier [opt] init-declarator [opt] ;
17282 template <> function-definition
17283 template <> explicit-specialization
17284 template <> template-declaration */
17286 static void
17287 cp_parser_explicit_specialization (cp_parser* parser)
17289 bool need_lang_pop;
17290 cp_token *token = cp_lexer_peek_token (parser->lexer);
17292 /* Look for the `template' keyword. */
17293 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17294 /* Look for the `<'. */
17295 cp_parser_require (parser, CPP_LESS, RT_LESS);
17296 /* Look for the `>'. */
17297 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17298 /* We have processed another parameter list. */
17299 ++parser->num_template_parameter_lists;
17300 /* [temp]
17302 A template ... explicit specialization ... shall not have C
17303 linkage. */
17304 if (current_lang_name == lang_name_c)
17306 error_at (token->location, "template specialization with C linkage");
17307 maybe_show_extern_c_location ();
17308 /* Give it C++ linkage to avoid confusing other parts of the
17309 front end. */
17310 push_lang_context (lang_name_cplusplus);
17311 need_lang_pop = true;
17313 else
17314 need_lang_pop = false;
17315 /* Let the front end know that we are beginning a specialization. */
17316 if (!begin_specialization ())
17318 end_specialization ();
17319 return;
17322 /* If the next keyword is `template', we need to figure out whether
17323 or not we're looking a template-declaration. */
17324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17326 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17327 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17328 cp_parser_template_declaration_after_export (parser,
17329 /*member_p=*/false);
17330 else
17331 cp_parser_explicit_specialization (parser);
17333 else
17334 /* Parse the dependent declaration. */
17335 cp_parser_single_declaration (parser,
17336 /*checks=*/NULL,
17337 /*member_p=*/false,
17338 /*explicit_specialization_p=*/true,
17339 /*friend_p=*/NULL);
17340 /* We're done with the specialization. */
17341 end_specialization ();
17342 /* For the erroneous case of a template with C linkage, we pushed an
17343 implicit C++ linkage scope; exit that scope now. */
17344 if (need_lang_pop)
17345 pop_lang_context ();
17346 /* We're done with this parameter list. */
17347 --parser->num_template_parameter_lists;
17350 /* Parse a type-specifier.
17352 type-specifier:
17353 simple-type-specifier
17354 class-specifier
17355 enum-specifier
17356 elaborated-type-specifier
17357 cv-qualifier
17359 GNU Extension:
17361 type-specifier:
17362 __complex__
17364 Returns a representation of the type-specifier. For a
17365 class-specifier, enum-specifier, or elaborated-type-specifier, a
17366 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17368 The parser flags FLAGS is used to control type-specifier parsing.
17370 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17371 in a decl-specifier-seq.
17373 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17374 class-specifier, enum-specifier, or elaborated-type-specifier, then
17375 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17376 if a type is declared; 2 if it is defined. Otherwise, it is set to
17377 zero.
17379 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17380 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17381 is set to FALSE. */
17383 static tree
17384 cp_parser_type_specifier (cp_parser* parser,
17385 cp_parser_flags flags,
17386 cp_decl_specifier_seq *decl_specs,
17387 bool is_declaration,
17388 int* declares_class_or_enum,
17389 bool* is_cv_qualifier)
17391 tree type_spec = NULL_TREE;
17392 cp_token *token;
17393 enum rid keyword;
17394 cp_decl_spec ds = ds_last;
17396 /* Assume this type-specifier does not declare a new type. */
17397 if (declares_class_or_enum)
17398 *declares_class_or_enum = 0;
17399 /* And that it does not specify a cv-qualifier. */
17400 if (is_cv_qualifier)
17401 *is_cv_qualifier = false;
17402 /* Peek at the next token. */
17403 token = cp_lexer_peek_token (parser->lexer);
17405 /* If we're looking at a keyword, we can use that to guide the
17406 production we choose. */
17407 keyword = token->keyword;
17408 switch (keyword)
17410 case RID_ENUM:
17411 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17412 goto elaborated_type_specifier;
17414 /* Look for the enum-specifier. */
17415 type_spec = cp_parser_enum_specifier (parser);
17416 /* If that worked, we're done. */
17417 if (type_spec)
17419 if (declares_class_or_enum)
17420 *declares_class_or_enum = 2;
17421 if (decl_specs)
17422 cp_parser_set_decl_spec_type (decl_specs,
17423 type_spec,
17424 token,
17425 /*type_definition_p=*/true);
17426 return type_spec;
17428 else
17429 goto elaborated_type_specifier;
17431 /* Any of these indicate either a class-specifier, or an
17432 elaborated-type-specifier. */
17433 case RID_CLASS:
17434 case RID_STRUCT:
17435 case RID_UNION:
17436 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17437 goto elaborated_type_specifier;
17439 /* Parse tentatively so that we can back up if we don't find a
17440 class-specifier. */
17441 cp_parser_parse_tentatively (parser);
17442 /* Look for the class-specifier. */
17443 type_spec = cp_parser_class_specifier (parser);
17444 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17445 /* If that worked, we're done. */
17446 if (cp_parser_parse_definitely (parser))
17448 if (declares_class_or_enum)
17449 *declares_class_or_enum = 2;
17450 if (decl_specs)
17451 cp_parser_set_decl_spec_type (decl_specs,
17452 type_spec,
17453 token,
17454 /*type_definition_p=*/true);
17455 return type_spec;
17458 /* Fall through. */
17459 elaborated_type_specifier:
17460 /* We're declaring (not defining) a class or enum. */
17461 if (declares_class_or_enum)
17462 *declares_class_or_enum = 1;
17464 /* Fall through. */
17465 case RID_TYPENAME:
17466 /* Look for an elaborated-type-specifier. */
17467 type_spec
17468 = (cp_parser_elaborated_type_specifier
17469 (parser,
17470 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17471 is_declaration));
17472 if (decl_specs)
17473 cp_parser_set_decl_spec_type (decl_specs,
17474 type_spec,
17475 token,
17476 /*type_definition_p=*/false);
17477 return type_spec;
17479 case RID_CONST:
17480 ds = ds_const;
17481 if (is_cv_qualifier)
17482 *is_cv_qualifier = true;
17483 break;
17485 case RID_VOLATILE:
17486 ds = ds_volatile;
17487 if (is_cv_qualifier)
17488 *is_cv_qualifier = true;
17489 break;
17491 case RID_RESTRICT:
17492 ds = ds_restrict;
17493 if (is_cv_qualifier)
17494 *is_cv_qualifier = true;
17495 break;
17497 case RID_COMPLEX:
17498 /* The `__complex__' keyword is a GNU extension. */
17499 ds = ds_complex;
17500 break;
17502 default:
17503 break;
17506 /* Handle simple keywords. */
17507 if (ds != ds_last)
17509 if (decl_specs)
17511 set_and_check_decl_spec_loc (decl_specs, ds, token);
17512 decl_specs->any_specifiers_p = true;
17514 return cp_lexer_consume_token (parser->lexer)->u.value;
17517 /* If we do not already have a type-specifier, assume we are looking
17518 at a simple-type-specifier. */
17519 type_spec = cp_parser_simple_type_specifier (parser,
17520 decl_specs,
17521 flags);
17523 /* If we didn't find a type-specifier, and a type-specifier was not
17524 optional in this context, issue an error message. */
17525 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17527 cp_parser_error (parser, "expected type specifier");
17528 return error_mark_node;
17531 return type_spec;
17534 /* Parse a simple-type-specifier.
17536 simple-type-specifier:
17537 :: [opt] nested-name-specifier [opt] type-name
17538 :: [opt] nested-name-specifier template template-id
17539 char
17540 wchar_t
17541 bool
17542 short
17544 long
17545 signed
17546 unsigned
17547 float
17548 double
17549 void
17551 C++11 Extension:
17553 simple-type-specifier:
17554 auto
17555 decltype ( expression )
17556 char16_t
17557 char32_t
17558 __underlying_type ( type-id )
17560 C++17 extension:
17562 nested-name-specifier(opt) template-name
17564 GNU Extension:
17566 simple-type-specifier:
17567 __int128
17568 __typeof__ unary-expression
17569 __typeof__ ( type-id )
17570 __typeof__ ( type-id ) { initializer-list , [opt] }
17572 Concepts Extension:
17574 simple-type-specifier:
17575 constrained-type-specifier
17577 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17578 appropriately updated. */
17580 static tree
17581 cp_parser_simple_type_specifier (cp_parser* parser,
17582 cp_decl_specifier_seq *decl_specs,
17583 cp_parser_flags flags)
17585 tree type = NULL_TREE;
17586 cp_token *token;
17587 int idx;
17589 /* Peek at the next token. */
17590 token = cp_lexer_peek_token (parser->lexer);
17592 /* If we're looking at a keyword, things are easy. */
17593 switch (token->keyword)
17595 case RID_CHAR:
17596 if (decl_specs)
17597 decl_specs->explicit_char_p = true;
17598 type = char_type_node;
17599 break;
17600 case RID_CHAR8:
17601 type = char8_type_node;
17602 break;
17603 case RID_CHAR16:
17604 type = char16_type_node;
17605 break;
17606 case RID_CHAR32:
17607 type = char32_type_node;
17608 break;
17609 case RID_WCHAR:
17610 type = wchar_type_node;
17611 break;
17612 case RID_BOOL:
17613 type = boolean_type_node;
17614 break;
17615 case RID_SHORT:
17616 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17617 type = short_integer_type_node;
17618 break;
17619 case RID_INT:
17620 if (decl_specs)
17621 decl_specs->explicit_int_p = true;
17622 type = integer_type_node;
17623 break;
17624 case RID_INT_N_0:
17625 case RID_INT_N_1:
17626 case RID_INT_N_2:
17627 case RID_INT_N_3:
17628 idx = token->keyword - RID_INT_N_0;
17629 if (! int_n_enabled_p [idx])
17630 break;
17631 if (decl_specs)
17633 decl_specs->explicit_intN_p = true;
17634 decl_specs->int_n_idx = idx;
17635 /* Check if the alternate "__intN__" form has been used instead of
17636 "__intN". */
17637 if (strncmp (IDENTIFIER_POINTER (token->u.value)
17638 + (IDENTIFIER_LENGTH (token->u.value) - 2),
17639 "__", 2) == 0)
17640 decl_specs->int_n_alt = true;
17642 type = int_n_trees [idx].signed_type;
17643 break;
17644 case RID_LONG:
17645 if (decl_specs)
17646 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17647 type = long_integer_type_node;
17648 break;
17649 case RID_SIGNED:
17650 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17651 type = integer_type_node;
17652 break;
17653 case RID_UNSIGNED:
17654 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17655 type = unsigned_type_node;
17656 break;
17657 case RID_FLOAT:
17658 type = float_type_node;
17659 break;
17660 case RID_DOUBLE:
17661 type = double_type_node;
17662 break;
17663 case RID_VOID:
17664 type = void_type_node;
17665 break;
17667 case RID_AUTO:
17668 maybe_warn_cpp0x (CPP0X_AUTO);
17669 if (parser->auto_is_implicit_function_template_parm_p)
17671 /* The 'auto' might be the placeholder return type for a function decl
17672 with trailing return type. */
17673 bool have_trailing_return_fn_decl = false;
17675 cp_parser_parse_tentatively (parser);
17676 cp_lexer_consume_token (parser->lexer);
17677 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17678 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17679 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17680 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17684 cp_lexer_consume_token (parser->lexer);
17685 cp_parser_skip_to_closing_parenthesis (parser,
17686 /*recovering*/false,
17687 /*or_comma*/false,
17688 /*consume_paren*/true);
17689 continue;
17692 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17694 have_trailing_return_fn_decl = true;
17695 break;
17698 cp_lexer_consume_token (parser->lexer);
17700 cp_parser_abort_tentative_parse (parser);
17702 if (have_trailing_return_fn_decl)
17704 type = make_auto ();
17705 break;
17708 if (cxx_dialect >= cxx14)
17710 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17711 type = TREE_TYPE (type);
17713 else
17714 type = error_mark_node;
17716 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17718 if (cxx_dialect < cxx14)
17719 error_at (token->location,
17720 "use of %<auto%> in lambda parameter declaration "
17721 "only available with "
17722 "%<-std=c++14%> or %<-std=gnu++14%>");
17724 else if (cxx_dialect < cxx14)
17725 error_at (token->location,
17726 "use of %<auto%> in parameter declaration "
17727 "only available with "
17728 "%<-std=c++14%> or %<-std=gnu++14%>");
17729 else if (!flag_concepts)
17730 pedwarn (token->location, 0,
17731 "use of %<auto%> in parameter declaration "
17732 "only available with %<-fconcepts%>");
17734 else
17735 type = make_auto ();
17736 break;
17738 case RID_DECLTYPE:
17739 /* Since DR 743, decltype can either be a simple-type-specifier by
17740 itself or begin a nested-name-specifier. Parsing it will replace
17741 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17742 handling below decide what to do. */
17743 cp_parser_decltype (parser);
17744 cp_lexer_set_token_position (parser->lexer, token);
17745 break;
17747 case RID_TYPEOF:
17748 /* Consume the `typeof' token. */
17749 cp_lexer_consume_token (parser->lexer);
17750 /* Parse the operand to `typeof'. */
17751 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17752 /* If it is not already a TYPE, take its type. */
17753 if (!TYPE_P (type))
17754 type = finish_typeof (type);
17756 if (decl_specs)
17757 cp_parser_set_decl_spec_type (decl_specs, type,
17758 token,
17759 /*type_definition_p=*/false);
17761 return type;
17763 case RID_UNDERLYING_TYPE:
17764 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17765 if (decl_specs)
17766 cp_parser_set_decl_spec_type (decl_specs, type,
17767 token,
17768 /*type_definition_p=*/false);
17770 return type;
17772 case RID_BASES:
17773 case RID_DIRECT_BASES:
17774 type = cp_parser_trait_expr (parser, token->keyword);
17775 if (decl_specs)
17776 cp_parser_set_decl_spec_type (decl_specs, type,
17777 token,
17778 /*type_definition_p=*/false);
17779 return type;
17780 default:
17781 break;
17784 /* If token is an already-parsed decltype not followed by ::,
17785 it's a simple-type-specifier. */
17786 if (token->type == CPP_DECLTYPE
17787 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17789 type = saved_checks_value (token->u.tree_check_value);
17790 if (decl_specs)
17792 cp_parser_set_decl_spec_type (decl_specs, type,
17793 token,
17794 /*type_definition_p=*/false);
17795 /* Remember that we are handling a decltype in order to
17796 implement the resolution of DR 1510 when the argument
17797 isn't instantiation dependent. */
17798 decl_specs->decltype_p = true;
17800 cp_lexer_consume_token (parser->lexer);
17801 return type;
17804 /* If the type-specifier was for a built-in type, we're done. */
17805 if (type)
17807 /* Record the type. */
17808 if (decl_specs
17809 && (token->keyword != RID_SIGNED
17810 && token->keyword != RID_UNSIGNED
17811 && token->keyword != RID_SHORT
17812 && token->keyword != RID_LONG))
17813 cp_parser_set_decl_spec_type (decl_specs,
17814 type,
17815 token,
17816 /*type_definition_p=*/false);
17817 if (decl_specs)
17818 decl_specs->any_specifiers_p = true;
17820 /* Consume the token. */
17821 cp_lexer_consume_token (parser->lexer);
17823 if (type == error_mark_node)
17824 return error_mark_node;
17826 /* There is no valid C++ program where a non-template type is
17827 followed by a "<". That usually indicates that the user thought
17828 that the type was a template. */
17829 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17830 token->location);
17832 return TYPE_NAME (type);
17835 /* The type-specifier must be a user-defined type. */
17836 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17838 bool qualified_p;
17839 bool global_p;
17840 const bool typename_p = (cxx_dialect >= cxx2a
17841 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17843 /* Don't gobble tokens or issue error messages if this is an
17844 optional type-specifier. */
17845 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17846 cp_parser_parse_tentatively (parser);
17848 token = cp_lexer_peek_token (parser->lexer);
17850 /* Look for the optional `::' operator. */
17851 global_p
17852 = (cp_parser_global_scope_opt (parser,
17853 /*current_scope_valid_p=*/false)
17854 != NULL_TREE);
17855 /* Look for the nested-name specifier. */
17856 qualified_p
17857 = (cp_parser_nested_name_specifier_opt (parser,
17858 /*typename_keyword_p=*/false,
17859 /*check_dependency_p=*/true,
17860 /*type_p=*/false,
17861 /*is_declaration=*/false)
17862 != NULL_TREE);
17863 /* If we have seen a nested-name-specifier, and the next token
17864 is `template', then we are using the template-id production. */
17865 if (parser->scope
17866 && cp_parser_optional_template_keyword (parser))
17868 /* Look for the template-id. */
17869 type = cp_parser_template_id (parser,
17870 /*template_keyword_p=*/true,
17871 /*check_dependency_p=*/true,
17872 none_type,
17873 /*is_declaration=*/false);
17874 /* If the template-id did not name a type, we are out of
17875 luck. */
17876 if (TREE_CODE (type) != TYPE_DECL)
17878 /* ...unless we pretend we have seen 'typename'. */
17879 if (typename_p)
17880 type = cp_parser_make_typename_type (parser, type,
17881 token->location);
17882 else
17884 cp_parser_error (parser, "expected template-id for type");
17885 type = error_mark_node;
17890 /* Otherwise, look for a type-name. */
17891 if (!type)
17893 if (cxx_dialect >= cxx17)
17894 cp_parser_parse_tentatively (parser);
17896 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17898 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
17899 type = NULL_TREE;
17902 if (!type && cxx_dialect >= cxx17)
17904 /* Try class template argument deduction. */
17905 tree name = cp_parser_identifier (parser);
17906 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17907 && parser->scope != error_mark_node)
17909 tree tmpl = cp_parser_lookup_name (parser, name,
17910 none_type,
17911 /*is_template=*/false,
17912 /*is_namespace=*/false,
17913 /*check_dependency=*/true,
17914 /*ambiguous_decls=*/NULL,
17915 token->location);
17916 if (tmpl && tmpl != error_mark_node
17917 && (DECL_CLASS_TEMPLATE_P (tmpl)
17918 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17919 type = make_template_placeholder (tmpl);
17920 else
17922 type = error_mark_node;
17923 if (!cp_parser_simulate_error (parser))
17924 cp_parser_name_lookup_error (parser, name, tmpl,
17925 NLE_TYPE, token->location);
17928 else
17929 type = error_mark_node;
17932 /* If it didn't work out, we don't have a TYPE. */
17933 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17934 && !cp_parser_parse_definitely (parser))
17935 type = NULL_TREE;
17937 /* Keep track of all name-lookups performed in class scopes. */
17938 if (type
17939 && !global_p
17940 && !qualified_p
17941 && TREE_CODE (type) == TYPE_DECL
17942 && identifier_p (DECL_NAME (type)))
17943 maybe_note_name_used_in_class (DECL_NAME (type), type);
17945 if (type && decl_specs)
17946 cp_parser_set_decl_spec_type (decl_specs, type,
17947 token,
17948 /*type_definition_p=*/false);
17951 /* If we didn't get a type-name, issue an error message. */
17952 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17954 cp_parser_error (parser, "expected type-name");
17955 return error_mark_node;
17958 if (type && type != error_mark_node)
17960 /* See if TYPE is an Objective-C type, and if so, parse and
17961 accept any protocol references following it. Do this before
17962 the cp_parser_check_for_invalid_template_id() call, because
17963 Objective-C types can be followed by '<...>' which would
17964 enclose protocol names rather than template arguments, and so
17965 everything is fine. */
17966 if (c_dialect_objc () && !parser->scope
17967 && (objc_is_id (type) || objc_is_class_name (type)))
17969 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17970 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17972 /* Clobber the "unqualified" type previously entered into
17973 DECL_SPECS with the new, improved protocol-qualified version. */
17974 if (decl_specs)
17975 decl_specs->type = qual_type;
17977 return qual_type;
17980 /* There is no valid C++ program where a non-template type is
17981 followed by a "<". That usually indicates that the user
17982 thought that the type was a template. */
17983 cp_parser_check_for_invalid_template_id (parser, type,
17984 none_type,
17985 token->location);
17988 return type;
17991 /* Parse a type-name.
17993 type-name:
17994 class-name
17995 enum-name
17996 typedef-name
17997 simple-template-id [in c++0x]
17999 enum-name:
18000 identifier
18002 typedef-name:
18003 identifier
18005 Concepts:
18007 type-name:
18008 concept-name
18009 partial-concept-id
18011 concept-name:
18012 identifier
18014 Returns a TYPE_DECL for the type. */
18016 static tree
18017 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18019 tree type_decl;
18021 /* We can't know yet whether it is a class-name or not. */
18022 cp_parser_parse_tentatively (parser);
18023 /* Try a class-name. */
18024 type_decl = cp_parser_class_name (parser,
18025 typename_keyword_p,
18026 /*template_keyword_p=*/false,
18027 none_type,
18028 /*check_dependency_p=*/true,
18029 /*class_head_p=*/false,
18030 /*is_declaration=*/false);
18031 /* If it's not a class-name, keep looking. */
18032 if (!cp_parser_parse_definitely (parser))
18034 if (cxx_dialect < cxx11)
18035 /* It must be a typedef-name or an enum-name. */
18036 return cp_parser_nonclass_name (parser);
18038 cp_parser_parse_tentatively (parser);
18039 /* It is either a simple-template-id representing an
18040 instantiation of an alias template... */
18041 type_decl = cp_parser_template_id (parser,
18042 /*template_keyword_p=*/false,
18043 /*check_dependency_p=*/true,
18044 none_type,
18045 /*is_declaration=*/false);
18046 /* Note that this must be an instantiation of an alias template
18047 because [temp.names]/6 says:
18049 A template-id that names an alias template specialization
18050 is a type-name.
18052 Whereas [temp.names]/7 says:
18054 A simple-template-id that names a class template
18055 specialization is a class-name.
18057 With concepts, this could also be a partial-concept-id that
18058 declares a non-type template parameter. */
18059 if (type_decl != NULL_TREE
18060 && TREE_CODE (type_decl) == TYPE_DECL
18061 && TYPE_DECL_ALIAS_P (type_decl))
18062 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18063 else if (is_constrained_parameter (type_decl))
18064 /* Don't do anything. */ ;
18065 else
18066 cp_parser_simulate_error (parser);
18068 if (!cp_parser_parse_definitely (parser))
18069 /* ... Or a typedef-name or an enum-name. */
18070 return cp_parser_nonclass_name (parser);
18073 return type_decl;
18076 /* Check if DECL and ARGS can form a constrained-type-specifier.
18077 If ARGS is non-null, we try to form a concept check of the
18078 form DECL<?, ARGS> where ? is a wildcard that matches any
18079 kind of template argument. If ARGS is NULL, then we try to
18080 form a concept check of the form DECL<?>. */
18082 static tree
18083 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18084 tree decl, tree args)
18086 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18088 /* If we a constrained-type-specifier cannot be deduced. */
18089 if (parser->prevent_constrained_type_specifiers)
18090 return NULL_TREE;
18092 /* A constrained type specifier can only be found in an
18093 overload set or as a reference to a template declaration.
18095 FIXME: This might be masking a bug. It's possible that
18096 that the deduction below is causing template specializations
18097 to be formed with the wildcard as an argument. */
18098 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18099 return NULL_TREE;
18101 /* Try to build a call expression that evaluates the
18102 concept. This can fail if the overload set refers
18103 only to non-templates. */
18104 tree placeholder = build_nt (WILDCARD_DECL);
18105 tree check = build_concept_check (decl, placeholder, args);
18106 if (check == error_mark_node)
18107 return NULL_TREE;
18109 /* Deduce the checked constraint and the prototype parameter.
18111 FIXME: In certain cases, failure to deduce should be a
18112 diagnosable error. */
18113 tree conc;
18114 tree proto;
18115 if (!deduce_constrained_parameter (check, conc, proto))
18116 return NULL_TREE;
18118 /* In template parameter scope, this results in a constrained
18119 parameter. Return a descriptor of that parm. */
18120 if (processing_template_parmlist)
18121 return build_constrained_parameter (conc, proto, args);
18123 /* In a parameter-declaration-clause, constrained-type
18124 specifiers result in invented template parameters. */
18125 if (parser->auto_is_implicit_function_template_parm_p)
18127 tree x = build_constrained_parameter (conc, proto, args);
18128 return synthesize_implicit_template_parm (parser, x);
18130 else
18132 /* Otherwise, we're in a context where the constrained
18133 type name is deduced and the constraint applies
18134 after deduction. */
18135 return make_constrained_auto (conc, args);
18138 return NULL_TREE;
18141 /* If DECL refers to a concept, return a TYPE_DECL representing
18142 the result of using the constrained type specifier in the
18143 current context. DECL refers to a concept if
18145 - it is an overload set containing a function concept taking a single
18146 type argument, or
18148 - it is a variable concept taking a single type argument. */
18150 static tree
18151 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18153 if (flag_concepts
18154 && (TREE_CODE (decl) == OVERLOAD
18155 || BASELINK_P (decl)
18156 || variable_concept_p (decl)))
18157 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18158 else
18159 return NULL_TREE;
18162 /* Check if DECL and ARGS form a partial-concept-id. If so,
18163 assign ID to the resulting constrained placeholder.
18165 Returns true if the partial-concept-id designates a placeholder
18166 and false otherwise. Note that *id is set to NULL_TREE in
18167 this case. */
18169 static tree
18170 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18172 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18175 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18176 or a concept-name.
18178 enum-name:
18179 identifier
18181 typedef-name:
18182 identifier
18184 concept-name:
18185 identifier
18187 Returns a TYPE_DECL for the type. */
18189 static tree
18190 cp_parser_nonclass_name (cp_parser* parser)
18192 tree type_decl;
18193 tree identifier;
18195 cp_token *token = cp_lexer_peek_token (parser->lexer);
18196 identifier = cp_parser_identifier (parser);
18197 if (identifier == error_mark_node)
18198 return error_mark_node;
18200 /* Look up the type-name. */
18201 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18203 type_decl = strip_using_decl (type_decl);
18205 /* If we found an overload set, then it may refer to a concept-name. */
18206 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18207 type_decl = decl;
18209 if (TREE_CODE (type_decl) != TYPE_DECL
18210 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18212 /* See if this is an Objective-C type. */
18213 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18214 tree type = objc_get_protocol_qualified_type (identifier, protos);
18215 if (type)
18216 type_decl = TYPE_NAME (type);
18219 /* Issue an error if we did not find a type-name. */
18220 if (TREE_CODE (type_decl) != TYPE_DECL
18221 /* In Objective-C, we have the complication that class names are
18222 normally type names and start declarations (eg, the
18223 "NSObject" in "NSObject *object;"), but can be used in an
18224 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18225 is an expression. So, a classname followed by a dot is not a
18226 valid type-name. */
18227 || (objc_is_class_name (TREE_TYPE (type_decl))
18228 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18230 if (!cp_parser_simulate_error (parser))
18231 cp_parser_name_lookup_error (parser, identifier, type_decl,
18232 NLE_TYPE, token->location);
18233 return error_mark_node;
18235 /* Remember that the name was used in the definition of the
18236 current class so that we can check later to see if the
18237 meaning would have been different after the class was
18238 entirely defined. */
18239 else if (type_decl != error_mark_node
18240 && !parser->scope)
18241 maybe_note_name_used_in_class (identifier, type_decl);
18243 return type_decl;
18246 /* Parse an elaborated-type-specifier. Note that the grammar given
18247 here incorporates the resolution to DR68.
18249 elaborated-type-specifier:
18250 class-key :: [opt] nested-name-specifier [opt] identifier
18251 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18252 enum-key :: [opt] nested-name-specifier [opt] identifier
18253 typename :: [opt] nested-name-specifier identifier
18254 typename :: [opt] nested-name-specifier template [opt]
18255 template-id
18257 GNU extension:
18259 elaborated-type-specifier:
18260 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18261 class-key attributes :: [opt] nested-name-specifier [opt]
18262 template [opt] template-id
18263 enum attributes :: [opt] nested-name-specifier [opt] identifier
18265 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18266 declared `friend'. If IS_DECLARATION is TRUE, then this
18267 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18268 something is being declared.
18270 Returns the TYPE specified. */
18272 static tree
18273 cp_parser_elaborated_type_specifier (cp_parser* parser,
18274 bool is_friend,
18275 bool is_declaration)
18277 enum tag_types tag_type;
18278 tree identifier;
18279 tree type = NULL_TREE;
18280 tree attributes = NULL_TREE;
18281 tree globalscope;
18282 cp_token *token = NULL;
18284 /* See if we're looking at the `enum' keyword. */
18285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18287 /* Consume the `enum' token. */
18288 cp_lexer_consume_token (parser->lexer);
18289 /* Remember that it's an enumeration type. */
18290 tag_type = enum_type;
18291 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18292 enums) is used here. */
18293 cp_token *token = cp_lexer_peek_token (parser->lexer);
18294 if (cp_parser_is_keyword (token, RID_CLASS)
18295 || cp_parser_is_keyword (token, RID_STRUCT))
18297 gcc_rich_location richloc (token->location);
18298 richloc.add_range (input_location);
18299 richloc.add_fixit_remove ();
18300 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18301 "a scoped enum must not use the %qD keyword",
18302 token->u.value);
18303 /* Consume the `struct' or `class' and parse it anyway. */
18304 cp_lexer_consume_token (parser->lexer);
18306 /* Parse the attributes. */
18307 attributes = cp_parser_attributes_opt (parser);
18309 /* Or, it might be `typename'. */
18310 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18311 RID_TYPENAME))
18313 /* Consume the `typename' token. */
18314 cp_lexer_consume_token (parser->lexer);
18315 /* Remember that it's a `typename' type. */
18316 tag_type = typename_type;
18318 /* Otherwise it must be a class-key. */
18319 else
18321 tag_type = cp_parser_class_key (parser);
18322 if (tag_type == none_type)
18323 return error_mark_node;
18324 /* Parse the attributes. */
18325 attributes = cp_parser_attributes_opt (parser);
18328 /* Look for the `::' operator. */
18329 globalscope = cp_parser_global_scope_opt (parser,
18330 /*current_scope_valid_p=*/false);
18331 /* Look for the nested-name-specifier. */
18332 tree nested_name_specifier;
18333 if (tag_type == typename_type && !globalscope)
18335 nested_name_specifier
18336 = cp_parser_nested_name_specifier (parser,
18337 /*typename_keyword_p=*/true,
18338 /*check_dependency_p=*/true,
18339 /*type_p=*/true,
18340 is_declaration);
18341 if (!nested_name_specifier)
18342 return error_mark_node;
18344 else
18345 /* Even though `typename' is not present, the proposed resolution
18346 to Core Issue 180 says that in `class A<T>::B', `B' should be
18347 considered a type-name, even if `A<T>' is dependent. */
18348 nested_name_specifier
18349 = cp_parser_nested_name_specifier_opt (parser,
18350 /*typename_keyword_p=*/true,
18351 /*check_dependency_p=*/true,
18352 /*type_p=*/true,
18353 is_declaration);
18354 /* For everything but enumeration types, consider a template-id.
18355 For an enumeration type, consider only a plain identifier. */
18356 if (tag_type != enum_type)
18358 bool template_p = false;
18359 tree decl;
18361 /* Allow the `template' keyword. */
18362 template_p = cp_parser_optional_template_keyword (parser);
18363 /* If we didn't see `template', we don't know if there's a
18364 template-id or not. */
18365 if (!template_p)
18366 cp_parser_parse_tentatively (parser);
18367 /* The `template' keyword must follow a nested-name-specifier. */
18368 else if (!nested_name_specifier)
18370 cp_parser_error (parser, "%<template%> must follow a nested-"
18371 "name-specifier");
18372 return error_mark_node;
18375 /* Parse the template-id. */
18376 token = cp_lexer_peek_token (parser->lexer);
18377 decl = cp_parser_template_id (parser, template_p,
18378 /*check_dependency_p=*/true,
18379 tag_type,
18380 is_declaration);
18381 /* If we didn't find a template-id, look for an ordinary
18382 identifier. */
18383 if (!template_p && !cp_parser_parse_definitely (parser))
18385 /* We can get here when cp_parser_template_id, called by
18386 cp_parser_class_name with tag_type == none_type, succeeds
18387 and caches a BASELINK. Then, when called again here,
18388 instead of failing and returning an error_mark_node
18389 returns it (see template/typename17.C in C++11).
18390 ??? Could we diagnose this earlier? */
18391 else if (tag_type == typename_type && BASELINK_P (decl))
18393 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18394 type = error_mark_node;
18396 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18397 in effect, then we must assume that, upon instantiation, the
18398 template will correspond to a class. */
18399 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18400 && tag_type == typename_type)
18401 type = make_typename_type (parser->scope, decl,
18402 typename_type,
18403 /*complain=*/tf_error);
18404 /* If the `typename' keyword is in effect and DECL is not a type
18405 decl, then type is non existent. */
18406 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18408 else if (TREE_CODE (decl) == TYPE_DECL)
18410 type = check_elaborated_type_specifier (tag_type, decl,
18411 /*allow_template_p=*/true);
18413 /* If the next token is a semicolon, this must be a specialization,
18414 instantiation, or friend declaration. Check the scope while we
18415 still know whether or not we had a nested-name-specifier. */
18416 if (type != error_mark_node
18417 && !nested_name_specifier && !is_friend
18418 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18419 check_unqualified_spec_or_inst (type, token->location);
18421 else if (decl == error_mark_node)
18422 type = error_mark_node;
18425 if (!type)
18427 token = cp_lexer_peek_token (parser->lexer);
18428 identifier = cp_parser_identifier (parser);
18430 if (identifier == error_mark_node)
18432 parser->scope = NULL_TREE;
18433 return error_mark_node;
18436 /* For a `typename', we needn't call xref_tag. */
18437 if (tag_type == typename_type
18438 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18439 return cp_parser_make_typename_type (parser, identifier,
18440 token->location);
18442 /* Template parameter lists apply only if we are not within a
18443 function parameter list. */
18444 bool template_parm_lists_apply
18445 = parser->num_template_parameter_lists;
18446 if (template_parm_lists_apply)
18447 for (cp_binding_level *s = current_binding_level;
18448 s && s->kind != sk_template_parms;
18449 s = s->level_chain)
18450 if (s->kind == sk_function_parms)
18451 template_parm_lists_apply = false;
18453 /* Look up a qualified name in the usual way. */
18454 if (parser->scope)
18456 tree decl;
18457 tree ambiguous_decls;
18459 decl = cp_parser_lookup_name (parser, identifier,
18460 tag_type,
18461 /*is_template=*/false,
18462 /*is_namespace=*/false,
18463 /*check_dependency=*/true,
18464 &ambiguous_decls,
18465 token->location);
18467 /* If the lookup was ambiguous, an error will already have been
18468 issued. */
18469 if (ambiguous_decls)
18470 return error_mark_node;
18472 /* If we are parsing friend declaration, DECL may be a
18473 TEMPLATE_DECL tree node here. However, we need to check
18474 whether this TEMPLATE_DECL results in valid code. Consider
18475 the following example:
18477 namespace N {
18478 template <class T> class C {};
18480 class X {
18481 template <class T> friend class N::C; // #1, valid code
18483 template <class T> class Y {
18484 friend class N::C; // #2, invalid code
18487 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18488 name lookup of `N::C'. We see that friend declaration must
18489 be template for the code to be valid. Note that
18490 processing_template_decl does not work here since it is
18491 always 1 for the above two cases. */
18493 decl = (cp_parser_maybe_treat_template_as_class
18494 (decl, /*tag_name_p=*/is_friend
18495 && template_parm_lists_apply));
18497 if (TREE_CODE (decl) != TYPE_DECL)
18499 cp_parser_diagnose_invalid_type_name (parser,
18500 identifier,
18501 token->location);
18502 return error_mark_node;
18505 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18507 bool allow_template = (template_parm_lists_apply
18508 || DECL_SELF_REFERENCE_P (decl));
18509 type = check_elaborated_type_specifier (tag_type, decl,
18510 allow_template);
18512 if (type == error_mark_node)
18513 return error_mark_node;
18516 /* Forward declarations of nested types, such as
18518 class C1::C2;
18519 class C1::C2::C3;
18521 are invalid unless all components preceding the final '::'
18522 are complete. If all enclosing types are complete, these
18523 declarations become merely pointless.
18525 Invalid forward declarations of nested types are errors
18526 caught elsewhere in parsing. Those that are pointless arrive
18527 here. */
18529 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18530 && !is_friend && !processing_explicit_instantiation)
18531 warning (0, "declaration %qD does not declare anything", decl);
18533 type = TREE_TYPE (decl);
18535 else
18537 /* An elaborated-type-specifier sometimes introduces a new type and
18538 sometimes names an existing type. Normally, the rule is that it
18539 introduces a new type only if there is not an existing type of
18540 the same name already in scope. For example, given:
18542 struct S {};
18543 void f() { struct S s; }
18545 the `struct S' in the body of `f' is the same `struct S' as in
18546 the global scope; the existing definition is used. However, if
18547 there were no global declaration, this would introduce a new
18548 local class named `S'.
18550 An exception to this rule applies to the following code:
18552 namespace N { struct S; }
18554 Here, the elaborated-type-specifier names a new type
18555 unconditionally; even if there is already an `S' in the
18556 containing scope this declaration names a new type.
18557 This exception only applies if the elaborated-type-specifier
18558 forms the complete declaration:
18560 [class.name]
18562 A declaration consisting solely of `class-key identifier ;' is
18563 either a redeclaration of the name in the current scope or a
18564 forward declaration of the identifier as a class name. It
18565 introduces the name into the current scope.
18567 We are in this situation precisely when the next token is a `;'.
18569 An exception to the exception is that a `friend' declaration does
18570 *not* name a new type; i.e., given:
18572 struct S { friend struct T; };
18574 `T' is not a new type in the scope of `S'.
18576 Also, `new struct S' or `sizeof (struct S)' never results in the
18577 definition of a new type; a new type can only be declared in a
18578 declaration context. */
18580 tag_scope ts;
18581 bool template_p;
18583 if (is_friend)
18584 /* Friends have special name lookup rules. */
18585 ts = ts_within_enclosing_non_class;
18586 else if (is_declaration
18587 && cp_lexer_next_token_is (parser->lexer,
18588 CPP_SEMICOLON))
18589 /* This is a `class-key identifier ;' */
18590 ts = ts_current;
18591 else
18592 ts = ts_global;
18594 template_p =
18595 (template_parm_lists_apply
18596 && (cp_parser_next_token_starts_class_definition_p (parser)
18597 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18598 /* An unqualified name was used to reference this type, so
18599 there were no qualifying templates. */
18600 if (template_parm_lists_apply
18601 && !cp_parser_check_template_parameters (parser,
18602 /*num_templates=*/0,
18603 /*template_id*/false,
18604 token->location,
18605 /*declarator=*/NULL))
18606 return error_mark_node;
18607 type = xref_tag (tag_type, identifier, ts, template_p);
18611 if (type == error_mark_node)
18612 return error_mark_node;
18614 /* Allow attributes on forward declarations of classes. */
18615 if (attributes)
18617 if (TREE_CODE (type) == TYPENAME_TYPE)
18618 warning (OPT_Wattributes,
18619 "attributes ignored on uninstantiated type");
18620 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18621 && ! processing_explicit_instantiation)
18622 warning (OPT_Wattributes,
18623 "attributes ignored on template instantiation");
18624 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18625 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18626 else
18627 warning (OPT_Wattributes,
18628 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18631 if (tag_type != enum_type)
18633 /* Indicate whether this class was declared as a `class' or as a
18634 `struct'. */
18635 if (CLASS_TYPE_P (type))
18636 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18637 cp_parser_check_class_key (tag_type, type);
18640 /* A "<" cannot follow an elaborated type specifier. If that
18641 happens, the user was probably trying to form a template-id. */
18642 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18643 token->location);
18645 return type;
18648 /* Parse an enum-specifier.
18650 enum-specifier:
18651 enum-head { enumerator-list [opt] }
18652 enum-head { enumerator-list , } [C++0x]
18654 enum-head:
18655 enum-key identifier [opt] enum-base [opt]
18656 enum-key nested-name-specifier identifier enum-base [opt]
18658 enum-key:
18659 enum
18660 enum class [C++0x]
18661 enum struct [C++0x]
18663 enum-base: [C++0x]
18664 : type-specifier-seq
18666 opaque-enum-specifier:
18667 enum-key identifier enum-base [opt] ;
18669 GNU Extensions:
18670 enum-key attributes[opt] identifier [opt] enum-base [opt]
18671 { enumerator-list [opt] }attributes[opt]
18672 enum-key attributes[opt] identifier [opt] enum-base [opt]
18673 { enumerator-list, }attributes[opt] [C++0x]
18675 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18676 if the token stream isn't an enum-specifier after all. */
18678 static tree
18679 cp_parser_enum_specifier (cp_parser* parser)
18681 tree identifier;
18682 tree type = NULL_TREE;
18683 tree prev_scope;
18684 tree nested_name_specifier = NULL_TREE;
18685 tree attributes;
18686 bool scoped_enum_p = false;
18687 bool has_underlying_type = false;
18688 bool nested_being_defined = false;
18689 bool new_value_list = false;
18690 bool is_new_type = false;
18691 bool is_unnamed = false;
18692 tree underlying_type = NULL_TREE;
18693 cp_token *type_start_token = NULL;
18694 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18696 parser->colon_corrects_to_scope_p = false;
18698 /* Parse tentatively so that we can back up if we don't find a
18699 enum-specifier. */
18700 cp_parser_parse_tentatively (parser);
18702 /* Caller guarantees that the current token is 'enum', an identifier
18703 possibly follows, and the token after that is an opening brace.
18704 If we don't have an identifier, fabricate an anonymous name for
18705 the enumeration being defined. */
18706 cp_lexer_consume_token (parser->lexer);
18708 /* Parse the "class" or "struct", which indicates a scoped
18709 enumeration type in C++0x. */
18710 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18711 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18713 if (cxx_dialect < cxx11)
18714 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18716 /* Consume the `struct' or `class' token. */
18717 cp_lexer_consume_token (parser->lexer);
18719 scoped_enum_p = true;
18722 attributes = cp_parser_attributes_opt (parser);
18724 /* Clear the qualification. */
18725 parser->scope = NULL_TREE;
18726 parser->qualifying_scope = NULL_TREE;
18727 parser->object_scope = NULL_TREE;
18729 /* Figure out in what scope the declaration is being placed. */
18730 prev_scope = current_scope ();
18732 type_start_token = cp_lexer_peek_token (parser->lexer);
18734 push_deferring_access_checks (dk_no_check);
18735 nested_name_specifier
18736 = cp_parser_nested_name_specifier_opt (parser,
18737 /*typename_keyword_p=*/true,
18738 /*check_dependency_p=*/false,
18739 /*type_p=*/false,
18740 /*is_declaration=*/false);
18742 if (nested_name_specifier)
18744 tree name;
18746 identifier = cp_parser_identifier (parser);
18747 name = cp_parser_lookup_name (parser, identifier,
18748 enum_type,
18749 /*is_template=*/false,
18750 /*is_namespace=*/false,
18751 /*check_dependency=*/true,
18752 /*ambiguous_decls=*/NULL,
18753 input_location);
18754 if (name && name != error_mark_node)
18756 type = TREE_TYPE (name);
18757 if (TREE_CODE (type) == TYPENAME_TYPE)
18759 /* Are template enums allowed in ISO? */
18760 if (template_parm_scope_p ())
18761 pedwarn (type_start_token->location, OPT_Wpedantic,
18762 "%qD is an enumeration template", name);
18763 /* ignore a typename reference, for it will be solved by name
18764 in start_enum. */
18765 type = NULL_TREE;
18768 else if (nested_name_specifier == error_mark_node)
18769 /* We already issued an error. */;
18770 else
18772 error_at (type_start_token->location,
18773 "%qD does not name an enumeration in %qT",
18774 identifier, nested_name_specifier);
18775 nested_name_specifier = error_mark_node;
18778 else
18780 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18781 identifier = cp_parser_identifier (parser);
18782 else
18784 identifier = make_anon_name ();
18785 is_unnamed = true;
18786 if (scoped_enum_p)
18787 error_at (type_start_token->location,
18788 "unnamed scoped enum is not allowed");
18791 pop_deferring_access_checks ();
18793 /* Check for the `:' that denotes a specified underlying type in C++0x.
18794 Note that a ':' could also indicate a bitfield width, however. */
18795 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18797 cp_decl_specifier_seq type_specifiers;
18799 /* Consume the `:'. */
18800 cp_lexer_consume_token (parser->lexer);
18802 /* Parse the type-specifier-seq. */
18803 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18804 /*is_declaration=*/false,
18805 /*is_trailing_return=*/false,
18806 &type_specifiers);
18808 /* At this point this is surely not elaborated type specifier. */
18809 if (!cp_parser_parse_definitely (parser))
18810 return NULL_TREE;
18812 if (cxx_dialect < cxx11)
18813 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18815 has_underlying_type = true;
18817 /* If that didn't work, stop. */
18818 if (type_specifiers.type != error_mark_node)
18820 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18821 /*initialized=*/0, NULL);
18822 if (underlying_type == error_mark_node
18823 || check_for_bare_parameter_packs (underlying_type))
18824 underlying_type = NULL_TREE;
18828 /* Look for the `{' but don't consume it yet. */
18829 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18831 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18833 cp_parser_error (parser, "expected %<{%>");
18834 if (has_underlying_type)
18836 type = NULL_TREE;
18837 goto out;
18840 /* An opaque-enum-specifier must have a ';' here. */
18841 if ((scoped_enum_p || underlying_type)
18842 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18844 cp_parser_error (parser, "expected %<;%> or %<{%>");
18845 if (has_underlying_type)
18847 type = NULL_TREE;
18848 goto out;
18853 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18854 return NULL_TREE;
18856 if (nested_name_specifier)
18858 if (CLASS_TYPE_P (nested_name_specifier))
18860 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18861 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18862 push_scope (nested_name_specifier);
18864 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18866 push_nested_namespace (nested_name_specifier);
18870 /* Issue an error message if type-definitions are forbidden here. */
18871 if (!cp_parser_check_type_definition (parser))
18872 type = error_mark_node;
18873 else
18874 /* Create the new type. We do this before consuming the opening
18875 brace so the enum will be recorded as being on the line of its
18876 tag (or the 'enum' keyword, if there is no tag). */
18877 type = start_enum (identifier, type, underlying_type,
18878 attributes, scoped_enum_p, &is_new_type);
18880 /* If the next token is not '{' it is an opaque-enum-specifier or an
18881 elaborated-type-specifier. */
18882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18884 timevar_push (TV_PARSE_ENUM);
18885 if (nested_name_specifier
18886 && nested_name_specifier != error_mark_node)
18888 /* The following catches invalid code such as:
18889 enum class S<int>::E { A, B, C }; */
18890 if (!processing_specialization
18891 && CLASS_TYPE_P (nested_name_specifier)
18892 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18893 error_at (type_start_token->location, "cannot add an enumerator "
18894 "list to a template instantiation");
18896 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18898 error_at (type_start_token->location,
18899 "%<%T::%E%> has not been declared",
18900 TYPE_CONTEXT (nested_name_specifier),
18901 nested_name_specifier);
18902 type = error_mark_node;
18904 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18905 && !CLASS_TYPE_P (nested_name_specifier))
18907 error_at (type_start_token->location, "nested name specifier "
18908 "%qT for enum declaration does not name a class "
18909 "or namespace", nested_name_specifier);
18910 type = error_mark_node;
18912 /* If that scope does not contain the scope in which the
18913 class was originally declared, the program is invalid. */
18914 else if (prev_scope && !is_ancestor (prev_scope,
18915 nested_name_specifier))
18917 if (at_namespace_scope_p ())
18918 error_at (type_start_token->location,
18919 "declaration of %qD in namespace %qD which does not "
18920 "enclose %qD",
18921 type, prev_scope, nested_name_specifier);
18922 else
18923 error_at (type_start_token->location,
18924 "declaration of %qD in %qD which does not "
18925 "enclose %qD",
18926 type, prev_scope, nested_name_specifier);
18927 type = error_mark_node;
18929 /* If that scope is the scope where the declaration is being placed
18930 the program is invalid. */
18931 else if (CLASS_TYPE_P (nested_name_specifier)
18932 && CLASS_TYPE_P (prev_scope)
18933 && same_type_p (nested_name_specifier, prev_scope))
18935 permerror (type_start_token->location,
18936 "extra qualification not allowed");
18937 nested_name_specifier = NULL_TREE;
18941 if (scoped_enum_p)
18942 begin_scope (sk_scoped_enum, type);
18944 /* Consume the opening brace. */
18945 matching_braces braces;
18946 braces.consume_open (parser);
18948 if (type == error_mark_node)
18949 ; /* Nothing to add */
18950 else if (OPAQUE_ENUM_P (type)
18951 || (cxx_dialect > cxx98 && processing_specialization))
18953 new_value_list = true;
18954 SET_OPAQUE_ENUM_P (type, false);
18955 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18957 else
18959 error_at (type_start_token->location,
18960 "multiple definition of %q#T", type);
18961 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18962 "previous definition here");
18963 type = error_mark_node;
18966 if (type == error_mark_node)
18967 cp_parser_skip_to_end_of_block_or_statement (parser);
18968 /* If the next token is not '}', then there are some enumerators. */
18969 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18971 if (is_unnamed && !scoped_enum_p)
18972 pedwarn (type_start_token->location, OPT_Wpedantic,
18973 "ISO C++ forbids empty unnamed enum");
18975 else
18976 cp_parser_enumerator_list (parser, type);
18978 /* Consume the final '}'. */
18979 braces.require_close (parser);
18981 if (scoped_enum_p)
18982 finish_scope ();
18983 timevar_pop (TV_PARSE_ENUM);
18985 else
18987 /* If a ';' follows, then it is an opaque-enum-specifier
18988 and additional restrictions apply. */
18989 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18991 if (is_unnamed)
18992 error_at (type_start_token->location,
18993 "opaque-enum-specifier without name");
18994 else if (nested_name_specifier)
18995 error_at (type_start_token->location,
18996 "opaque-enum-specifier must use a simple identifier");
19000 /* Look for trailing attributes to apply to this enumeration, and
19001 apply them if appropriate. */
19002 if (cp_parser_allow_gnu_extensions_p (parser))
19004 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
19005 cplus_decl_attributes (&type,
19006 trailing_attr,
19007 (int) ATTR_FLAG_TYPE_IN_PLACE);
19010 /* Finish up the enumeration. */
19011 if (type != error_mark_node)
19013 if (new_value_list)
19014 finish_enum_value_list (type);
19015 if (is_new_type)
19016 finish_enum (type);
19019 if (nested_name_specifier)
19021 if (CLASS_TYPE_P (nested_name_specifier))
19023 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19024 pop_scope (nested_name_specifier);
19026 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19028 pop_nested_namespace (nested_name_specifier);
19031 out:
19032 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19033 return type;
19036 /* Parse an enumerator-list. The enumerators all have the indicated
19037 TYPE.
19039 enumerator-list:
19040 enumerator-definition
19041 enumerator-list , enumerator-definition */
19043 static void
19044 cp_parser_enumerator_list (cp_parser* parser, tree type)
19046 while (true)
19048 /* Parse an enumerator-definition. */
19049 cp_parser_enumerator_definition (parser, type);
19051 /* If the next token is not a ',', we've reached the end of
19052 the list. */
19053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19054 break;
19055 /* Otherwise, consume the `,' and keep going. */
19056 cp_lexer_consume_token (parser->lexer);
19057 /* If the next token is a `}', there is a trailing comma. */
19058 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19060 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
19061 pedwarn (input_location, OPT_Wpedantic,
19062 "comma at end of enumerator list");
19063 break;
19068 /* Parse an enumerator-definition. The enumerator has the indicated
19069 TYPE.
19071 enumerator-definition:
19072 enumerator
19073 enumerator = constant-expression
19075 enumerator:
19076 identifier
19078 GNU Extensions:
19080 enumerator-definition:
19081 enumerator attributes [opt]
19082 enumerator attributes [opt] = constant-expression */
19084 static void
19085 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19087 tree identifier;
19088 tree value;
19089 location_t loc;
19091 /* Save the input location because we are interested in the location
19092 of the identifier and not the location of the explicit value. */
19093 loc = cp_lexer_peek_token (parser->lexer)->location;
19095 /* Look for the identifier. */
19096 identifier = cp_parser_identifier (parser);
19097 if (identifier == error_mark_node)
19098 return;
19100 /* Parse any specified attributes. */
19101 tree attrs = cp_parser_attributes_opt (parser);
19103 /* If the next token is an '=', then there is an explicit value. */
19104 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19106 /* Consume the `=' token. */
19107 cp_lexer_consume_token (parser->lexer);
19108 /* Parse the value. */
19109 value = cp_parser_constant_expression (parser);
19111 else
19112 value = NULL_TREE;
19114 /* If we are processing a template, make sure the initializer of the
19115 enumerator doesn't contain any bare template parameter pack. */
19116 if (check_for_bare_parameter_packs (value))
19117 value = error_mark_node;
19119 /* Create the enumerator. */
19120 build_enumerator (identifier, value, type, attrs, loc);
19123 /* Parse a namespace-name.
19125 namespace-name:
19126 original-namespace-name
19127 namespace-alias
19129 Returns the NAMESPACE_DECL for the namespace. */
19131 static tree
19132 cp_parser_namespace_name (cp_parser* parser)
19134 tree identifier;
19135 tree namespace_decl;
19137 cp_token *token = cp_lexer_peek_token (parser->lexer);
19139 /* Get the name of the namespace. */
19140 identifier = cp_parser_identifier (parser);
19141 if (identifier == error_mark_node)
19142 return error_mark_node;
19144 /* Look up the identifier in the currently active scope. Look only
19145 for namespaces, due to:
19147 [basic.lookup.udir]
19149 When looking up a namespace-name in a using-directive or alias
19150 definition, only namespace names are considered.
19152 And:
19154 [basic.lookup.qual]
19156 During the lookup of a name preceding the :: scope resolution
19157 operator, object, function, and enumerator names are ignored.
19159 (Note that cp_parser_qualifying_entity only calls this
19160 function if the token after the name is the scope resolution
19161 operator.) */
19162 namespace_decl = cp_parser_lookup_name (parser, identifier,
19163 none_type,
19164 /*is_template=*/false,
19165 /*is_namespace=*/true,
19166 /*check_dependency=*/true,
19167 /*ambiguous_decls=*/NULL,
19168 token->location);
19169 /* If it's not a namespace, issue an error. */
19170 if (namespace_decl == error_mark_node
19171 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19173 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19175 auto_diagnostic_group d;
19176 name_hint hint;
19177 if (namespace_decl == error_mark_node
19178 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19179 hint = suggest_alternative_in_explicit_scope (token->location,
19180 identifier,
19181 parser->scope);
19182 if (const char *suggestion = hint.suggestion ())
19184 gcc_rich_location richloc (token->location);
19185 richloc.add_fixit_replace (suggestion);
19186 error_at (&richloc,
19187 "%qD is not a namespace-name; did you mean %qs?",
19188 identifier, suggestion);
19190 else
19191 error_at (token->location, "%qD is not a namespace-name",
19192 identifier);
19194 else
19195 cp_parser_error (parser, "expected namespace-name");
19196 namespace_decl = error_mark_node;
19199 return namespace_decl;
19202 /* Parse a namespace-definition.
19204 namespace-definition:
19205 named-namespace-definition
19206 unnamed-namespace-definition
19208 named-namespace-definition:
19209 original-namespace-definition
19210 extension-namespace-definition
19212 original-namespace-definition:
19213 namespace identifier { namespace-body }
19215 extension-namespace-definition:
19216 namespace original-namespace-name { namespace-body }
19218 unnamed-namespace-definition:
19219 namespace { namespace-body } */
19221 static void
19222 cp_parser_namespace_definition (cp_parser* parser)
19224 tree identifier;
19225 int nested_definition_count = 0;
19227 cp_ensure_no_omp_declare_simd (parser);
19228 cp_ensure_no_oacc_routine (parser);
19230 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19231 const bool topmost_inline_p = is_inline;
19233 if (is_inline)
19235 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19236 cp_lexer_consume_token (parser->lexer);
19239 /* Look for the `namespace' keyword. */
19240 cp_token* token
19241 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19243 /* Parse any specified attributes before the identifier. */
19244 tree attribs = cp_parser_attributes_opt (parser);
19246 for (;;)
19248 identifier = NULL_TREE;
19250 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19251 RID_INLINE);
19252 if (nested_inline_p && nested_definition_count != 0)
19254 if (cxx_dialect < cxx2a)
19255 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19256 OPT_Wpedantic, "nested inline namespace definitions only "
19257 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19258 cp_lexer_consume_token (parser->lexer);
19261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19263 identifier = cp_parser_identifier (parser);
19265 if (cp_next_tokens_can_be_std_attribute_p (parser))
19266 pedwarn (input_location, OPT_Wpedantic,
19267 "standard attributes on namespaces must precede "
19268 "the namespace name");
19270 /* Parse any attributes specified after the identifier. */
19271 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19276 /* Don't forget that the innermost namespace might have been
19277 marked as inline. Use |= because we cannot overwrite
19278 IS_INLINE in case the outermost namespace is inline, but
19279 there are no nested inlines. */
19280 is_inline |= nested_inline_p;
19281 break;
19284 if (!nested_definition_count && cxx_dialect < cxx17)
19285 pedwarn (input_location, OPT_Wpedantic,
19286 "nested namespace definitions only available with "
19287 "%<-std=c++17%> or %<-std=gnu++17%>");
19289 /* Nested namespace names can create new namespaces (unlike
19290 other qualified-ids). */
19291 if (int count = (identifier
19292 ? push_namespace (identifier, nested_inline_p)
19293 : 0))
19294 nested_definition_count += count;
19295 else
19296 cp_parser_error (parser, "nested namespace name required");
19297 cp_lexer_consume_token (parser->lexer);
19300 if (nested_definition_count && !identifier)
19301 cp_parser_error (parser, "namespace name required");
19303 if (nested_definition_count && attribs)
19304 error_at (token->location,
19305 "a nested namespace definition cannot have attributes");
19306 if (nested_definition_count && topmost_inline_p)
19307 error_at (token->location,
19308 "a nested namespace definition cannot be inline");
19310 /* Start the namespace. */
19311 nested_definition_count += push_namespace (identifier, is_inline);
19313 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19315 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19317 /* Look for the `{' to validate starting the namespace. */
19318 matching_braces braces;
19319 if (braces.require_open (parser))
19321 /* Parse the body of the namespace. */
19322 cp_parser_namespace_body (parser);
19324 /* Look for the final `}'. */
19325 braces.require_close (parser);
19328 if (has_visibility)
19329 pop_visibility (1);
19331 /* Pop the nested namespace definitions. */
19332 while (nested_definition_count--)
19333 pop_namespace ();
19336 /* Parse a namespace-body.
19338 namespace-body:
19339 declaration-seq [opt] */
19341 static void
19342 cp_parser_namespace_body (cp_parser* parser)
19344 cp_parser_declaration_seq_opt (parser);
19347 /* Parse a namespace-alias-definition.
19349 namespace-alias-definition:
19350 namespace identifier = qualified-namespace-specifier ; */
19352 static void
19353 cp_parser_namespace_alias_definition (cp_parser* parser)
19355 tree identifier;
19356 tree namespace_specifier;
19358 cp_token *token = cp_lexer_peek_token (parser->lexer);
19360 /* Look for the `namespace' keyword. */
19361 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19362 /* Look for the identifier. */
19363 identifier = cp_parser_identifier (parser);
19364 if (identifier == error_mark_node)
19365 return;
19366 /* Look for the `=' token. */
19367 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19368 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19370 error_at (token->location, "%<namespace%> definition is not allowed here");
19371 /* Skip the definition. */
19372 cp_lexer_consume_token (parser->lexer);
19373 if (cp_parser_skip_to_closing_brace (parser))
19374 cp_lexer_consume_token (parser->lexer);
19375 return;
19377 cp_parser_require (parser, CPP_EQ, RT_EQ);
19378 /* Look for the qualified-namespace-specifier. */
19379 namespace_specifier
19380 = cp_parser_qualified_namespace_specifier (parser);
19381 /* Look for the `;' token. */
19382 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19384 /* Register the alias in the symbol table. */
19385 do_namespace_alias (identifier, namespace_specifier);
19388 /* Parse a qualified-namespace-specifier.
19390 qualified-namespace-specifier:
19391 :: [opt] nested-name-specifier [opt] namespace-name
19393 Returns a NAMESPACE_DECL corresponding to the specified
19394 namespace. */
19396 static tree
19397 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19399 /* Look for the optional `::'. */
19400 cp_parser_global_scope_opt (parser,
19401 /*current_scope_valid_p=*/false);
19403 /* Look for the optional nested-name-specifier. */
19404 cp_parser_nested_name_specifier_opt (parser,
19405 /*typename_keyword_p=*/false,
19406 /*check_dependency_p=*/true,
19407 /*type_p=*/false,
19408 /*is_declaration=*/true);
19410 return cp_parser_namespace_name (parser);
19413 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19414 access declaration.
19416 using-declaration:
19417 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19418 using :: unqualified-id ;
19420 access-declaration:
19421 qualified-id ;
19425 static bool
19426 cp_parser_using_declaration (cp_parser* parser,
19427 bool access_declaration_p)
19429 cp_token *token;
19430 bool typename_p = false;
19431 bool global_scope_p;
19432 tree decl;
19433 tree identifier;
19434 tree qscope;
19435 int oldcount = errorcount;
19436 cp_token *diag_token = NULL;
19438 if (access_declaration_p)
19440 diag_token = cp_lexer_peek_token (parser->lexer);
19441 cp_parser_parse_tentatively (parser);
19443 else
19445 /* Look for the `using' keyword. */
19446 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19448 again:
19449 /* Peek at the next token. */
19450 token = cp_lexer_peek_token (parser->lexer);
19451 /* See if it's `typename'. */
19452 if (token->keyword == RID_TYPENAME)
19454 /* Remember that we've seen it. */
19455 typename_p = true;
19456 /* Consume the `typename' token. */
19457 cp_lexer_consume_token (parser->lexer);
19461 /* Look for the optional global scope qualification. */
19462 global_scope_p
19463 = (cp_parser_global_scope_opt (parser,
19464 /*current_scope_valid_p=*/false)
19465 != NULL_TREE);
19467 /* If we saw `typename', or didn't see `::', then there must be a
19468 nested-name-specifier present. */
19469 if (typename_p || !global_scope_p)
19471 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19472 /*check_dependency_p=*/true,
19473 /*type_p=*/false,
19474 /*is_declaration=*/true);
19475 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19477 cp_parser_skip_to_end_of_block_or_statement (parser);
19478 return false;
19481 /* Otherwise, we could be in either of the two productions. In that
19482 case, treat the nested-name-specifier as optional. */
19483 else
19484 qscope = cp_parser_nested_name_specifier_opt (parser,
19485 /*typename_keyword_p=*/false,
19486 /*check_dependency_p=*/true,
19487 /*type_p=*/false,
19488 /*is_declaration=*/true);
19489 if (!qscope)
19490 qscope = global_namespace;
19491 else if (UNSCOPED_ENUM_P (qscope)
19492 && !TYPE_FUNCTION_SCOPE_P (qscope))
19493 qscope = CP_TYPE_CONTEXT (qscope);
19495 if (access_declaration_p && cp_parser_error_occurred (parser))
19496 /* Something has already gone wrong; there's no need to parse
19497 further. Since an error has occurred, the return value of
19498 cp_parser_parse_definitely will be false, as required. */
19499 return cp_parser_parse_definitely (parser);
19501 token = cp_lexer_peek_token (parser->lexer);
19502 /* Parse the unqualified-id. */
19503 identifier = cp_parser_unqualified_id (parser,
19504 /*template_keyword_p=*/false,
19505 /*check_dependency_p=*/true,
19506 /*declarator_p=*/true,
19507 /*optional_p=*/false);
19509 if (access_declaration_p)
19511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19512 cp_parser_simulate_error (parser);
19513 if (!cp_parser_parse_definitely (parser))
19514 return false;
19516 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19518 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19519 if (cxx_dialect < cxx17
19520 && !in_system_header_at (ell->location))
19521 pedwarn (ell->location, 0,
19522 "pack expansion in using-declaration only available "
19523 "with %<-std=c++17%> or %<-std=gnu++17%>");
19524 qscope = make_pack_expansion (qscope);
19527 /* The function we call to handle a using-declaration is different
19528 depending on what scope we are in. */
19529 if (qscope == error_mark_node || identifier == error_mark_node)
19531 else if (!identifier_p (identifier)
19532 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19533 /* [namespace.udecl]
19535 A using declaration shall not name a template-id. */
19536 error_at (token->location,
19537 "a template-id may not appear in a using-declaration");
19538 else
19540 if (at_class_scope_p ())
19542 /* Create the USING_DECL. */
19543 decl = do_class_using_decl (qscope, identifier);
19545 if (decl && typename_p)
19546 USING_DECL_TYPENAME_P (decl) = 1;
19548 if (check_for_bare_parameter_packs (decl))
19550 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19551 return false;
19553 else
19554 /* Add it to the list of members in this class. */
19555 finish_member_declaration (decl);
19557 else
19558 finish_nonmember_using_decl (qscope, identifier);
19561 if (!access_declaration_p
19562 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19564 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19565 if (cxx_dialect < cxx17)
19566 pedwarn (comma->location, 0,
19567 "comma-separated list in using-declaration only available "
19568 "with %<-std=c++17%> or %<-std=gnu++17%>");
19569 goto again;
19572 /* Look for the final `;'. */
19573 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19575 if (access_declaration_p && errorcount == oldcount)
19576 warning_at (diag_token->location, OPT_Wdeprecated,
19577 "access declarations are deprecated "
19578 "in favour of using-declarations; "
19579 "suggestion: add the %<using%> keyword");
19581 return true;
19584 /* Parse an alias-declaration.
19586 alias-declaration:
19587 using identifier attribute-specifier-seq [opt] = type-id */
19589 static tree
19590 cp_parser_alias_declaration (cp_parser* parser)
19592 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19593 location_t id_location, type_location;
19594 cp_declarator *declarator;
19595 cp_decl_specifier_seq decl_specs;
19596 bool member_p;
19597 const char *saved_message = NULL;
19599 /* Look for the `using' keyword. */
19600 cp_token *using_token
19601 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19602 if (using_token == NULL)
19603 return error_mark_node;
19605 id_location = cp_lexer_peek_token (parser->lexer)->location;
19606 id = cp_parser_identifier (parser);
19607 if (id == error_mark_node)
19608 return error_mark_node;
19610 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19611 attributes = cp_parser_attributes_opt (parser);
19612 if (attributes == error_mark_node)
19613 return error_mark_node;
19615 cp_parser_require (parser, CPP_EQ, RT_EQ);
19617 if (cp_parser_error_occurred (parser))
19618 return error_mark_node;
19620 cp_parser_commit_to_tentative_parse (parser);
19622 /* Now we are going to parse the type-id of the declaration. */
19625 [dcl.type]/3 says:
19627 "A type-specifier-seq shall not define a class or enumeration
19628 unless it appears in the type-id of an alias-declaration (7.1.3) that
19629 is not the declaration of a template-declaration."
19631 In other words, if we currently are in an alias template, the
19632 type-id should not define a type.
19634 So let's set parser->type_definition_forbidden_message in that
19635 case; cp_parser_check_type_definition (called by
19636 cp_parser_class_specifier) will then emit an error if a type is
19637 defined in the type-id. */
19638 if (parser->num_template_parameter_lists)
19640 saved_message = parser->type_definition_forbidden_message;
19641 parser->type_definition_forbidden_message =
19642 G_("types may not be defined in alias template declarations");
19645 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19646 &type_location);
19648 /* Restore the error message if need be. */
19649 if (parser->num_template_parameter_lists)
19650 parser->type_definition_forbidden_message = saved_message;
19652 if (type == error_mark_node
19653 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19655 cp_parser_skip_to_end_of_block_or_statement (parser);
19656 return error_mark_node;
19659 /* A typedef-name can also be introduced by an alias-declaration. The
19660 identifier following the using keyword becomes a typedef-name. It has
19661 the same semantics as if it were introduced by the typedef
19662 specifier. In particular, it does not define a new type and it shall
19663 not appear in the type-id. */
19665 clear_decl_specs (&decl_specs);
19666 decl_specs.type = type;
19667 if (attributes != NULL_TREE)
19669 decl_specs.attributes = attributes;
19670 set_and_check_decl_spec_loc (&decl_specs,
19671 ds_attribute,
19672 attrs_token);
19674 set_and_check_decl_spec_loc (&decl_specs,
19675 ds_typedef,
19676 using_token);
19677 set_and_check_decl_spec_loc (&decl_specs,
19678 ds_alias,
19679 using_token);
19680 decl_specs.locations[ds_type_spec] = type_location;
19682 if (parser->num_template_parameter_lists
19683 && !cp_parser_check_template_parameters (parser,
19684 /*num_templates=*/0,
19685 /*template_id*/false,
19686 id_location,
19687 /*declarator=*/NULL))
19688 return error_mark_node;
19690 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19692 member_p = at_class_scope_p ();
19693 if (member_p)
19694 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19695 NULL_TREE, attributes);
19696 else
19697 decl = start_decl (declarator, &decl_specs, 0,
19698 attributes, NULL_TREE, &pushed_scope);
19699 if (decl == error_mark_node)
19700 return decl;
19702 // Attach constraints to the alias declaration.
19703 if (flag_concepts && current_template_parms)
19705 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19706 tree constr = build_constraints (reqs, NULL_TREE);
19707 set_constraints (decl, constr);
19710 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19712 if (pushed_scope)
19713 pop_scope (pushed_scope);
19715 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19716 added into the symbol table; otherwise, return the TYPE_DECL. */
19717 if (DECL_LANG_SPECIFIC (decl)
19718 && DECL_TEMPLATE_INFO (decl)
19719 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19721 decl = DECL_TI_TEMPLATE (decl);
19722 if (member_p)
19723 check_member_template (decl);
19726 return decl;
19729 /* Parse a using-directive.
19731 using-directive:
19732 using namespace :: [opt] nested-name-specifier [opt]
19733 namespace-name ; */
19735 static void
19736 cp_parser_using_directive (cp_parser* parser)
19738 tree namespace_decl;
19739 tree attribs;
19741 /* Look for the `using' keyword. */
19742 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19743 /* And the `namespace' keyword. */
19744 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19745 /* Look for the optional `::' operator. */
19746 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19747 /* And the optional nested-name-specifier. */
19748 cp_parser_nested_name_specifier_opt (parser,
19749 /*typename_keyword_p=*/false,
19750 /*check_dependency_p=*/true,
19751 /*type_p=*/false,
19752 /*is_declaration=*/true);
19753 /* Get the namespace being used. */
19754 namespace_decl = cp_parser_namespace_name (parser);
19755 /* And any specified attributes. */
19756 attribs = cp_parser_attributes_opt (parser);
19758 /* Update the symbol table. */
19759 finish_using_directive (namespace_decl, attribs);
19761 /* Look for the final `;'. */
19762 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19765 /* Parse an asm-definition.
19767 asm-qualifier:
19768 volatile
19769 inline
19770 goto
19772 asm-qualifier-list:
19773 asm-qualifier
19774 asm-qualifier-list asm-qualifier
19776 asm-definition:
19777 asm ( string-literal ) ;
19779 GNU Extension:
19781 asm-definition:
19782 asm asm-qualifier-list [opt] ( string-literal ) ;
19783 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19784 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19785 : asm-operand-list [opt] ) ;
19786 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19787 : asm-operand-list [opt]
19788 : asm-clobber-list [opt] ) ;
19789 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19790 : asm-clobber-list [opt]
19791 : asm-goto-list ) ;
19793 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19794 contains goto, and is the only allowed form in that case. No duplicates are
19795 allowed in an asm-qualifier-list. */
19797 static void
19798 cp_parser_asm_definition (cp_parser* parser)
19800 tree string;
19801 tree outputs = NULL_TREE;
19802 tree inputs = NULL_TREE;
19803 tree clobbers = NULL_TREE;
19804 tree labels = NULL_TREE;
19805 tree asm_stmt;
19806 bool extended_p = false;
19807 bool invalid_inputs_p = false;
19808 bool invalid_outputs_p = false;
19809 required_token missing = RT_NONE;
19810 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
19812 /* Look for the `asm' keyword. */
19813 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19815 /* In C++2a, unevaluated inline assembly is permitted in constexpr
19816 functions. */
19817 if (parser->in_function_body
19818 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
19819 && (cxx_dialect < cxx2a))
19820 pedwarn (asm_loc, 0, "%<asm%> in %<constexpr%> function only available "
19821 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
19823 /* Handle the asm-qualifier-list. */
19824 location_t volatile_loc = UNKNOWN_LOCATION;
19825 location_t inline_loc = UNKNOWN_LOCATION;
19826 location_t goto_loc = UNKNOWN_LOCATION;
19827 location_t first_loc = UNKNOWN_LOCATION;
19829 if (cp_parser_allow_gnu_extensions_p (parser))
19830 for (;;)
19832 cp_token *token = cp_lexer_peek_token (parser->lexer);
19833 location_t loc = token->location;
19834 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19836 case RID_VOLATILE:
19837 if (volatile_loc)
19839 error_at (loc, "duplicate %<asm%> qualifier %qT",
19840 token->u.value);
19841 inform (volatile_loc, "first seen here");
19843 else
19845 if (!parser->in_function_body)
19846 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
19847 "outside of function body", token->u.value);
19848 volatile_loc = loc;
19850 cp_lexer_consume_token (parser->lexer);
19851 continue;
19853 case RID_INLINE:
19854 if (inline_loc)
19856 error_at (loc, "duplicate %<asm%> qualifier %qT",
19857 token->u.value);
19858 inform (inline_loc, "first seen here");
19860 else
19861 inline_loc = loc;
19862 if (!first_loc)
19863 first_loc = loc;
19864 cp_lexer_consume_token (parser->lexer);
19865 continue;
19867 case RID_GOTO:
19868 if (goto_loc)
19870 error_at (loc, "duplicate %<asm%> qualifier %qT",
19871 token->u.value);
19872 inform (goto_loc, "first seen here");
19874 else
19875 goto_loc = loc;
19876 if (!first_loc)
19877 first_loc = loc;
19878 cp_lexer_consume_token (parser->lexer);
19879 continue;
19881 case RID_CONST:
19882 case RID_RESTRICT:
19883 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
19884 cp_lexer_consume_token (parser->lexer);
19885 continue;
19887 default:
19888 break;
19890 break;
19893 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19894 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19895 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19897 if (!parser->in_function_body && (inline_p || goto_p))
19899 error_at (first_loc, "%<asm%> qualifier outside of function body");
19900 inline_p = goto_p = false;
19903 /* Look for the opening `('. */
19904 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19905 return;
19906 /* Look for the string. */
19907 string = cp_parser_string_literal (parser, false, false);
19908 if (string == error_mark_node)
19910 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19911 /*consume_paren=*/true);
19912 return;
19915 /* If we're allowing GNU extensions, check for the extended assembly
19916 syntax. Unfortunately, the `:' tokens need not be separated by
19917 a space in C, and so, for compatibility, we tolerate that here
19918 too. Doing that means that we have to treat the `::' operator as
19919 two `:' tokens. */
19920 if (cp_parser_allow_gnu_extensions_p (parser)
19921 && parser->in_function_body
19922 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19923 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19925 bool inputs_p = false;
19926 bool clobbers_p = false;
19927 bool labels_p = false;
19929 /* The extended syntax was used. */
19930 extended_p = true;
19932 /* Look for outputs. */
19933 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19935 /* Consume the `:'. */
19936 cp_lexer_consume_token (parser->lexer);
19937 /* Parse the output-operands. */
19938 if (cp_lexer_next_token_is_not (parser->lexer,
19939 CPP_COLON)
19940 && cp_lexer_next_token_is_not (parser->lexer,
19941 CPP_SCOPE)
19942 && cp_lexer_next_token_is_not (parser->lexer,
19943 CPP_CLOSE_PAREN)
19944 && !goto_p)
19946 outputs = cp_parser_asm_operand_list (parser);
19947 if (outputs == error_mark_node)
19948 invalid_outputs_p = true;
19951 /* If the next token is `::', there are no outputs, and the
19952 next token is the beginning of the inputs. */
19953 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19954 /* The inputs are coming next. */
19955 inputs_p = true;
19957 /* Look for inputs. */
19958 if (inputs_p
19959 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19961 /* Consume the `:' or `::'. */
19962 cp_lexer_consume_token (parser->lexer);
19963 /* Parse the output-operands. */
19964 if (cp_lexer_next_token_is_not (parser->lexer,
19965 CPP_COLON)
19966 && cp_lexer_next_token_is_not (parser->lexer,
19967 CPP_SCOPE)
19968 && cp_lexer_next_token_is_not (parser->lexer,
19969 CPP_CLOSE_PAREN))
19971 inputs = cp_parser_asm_operand_list (parser);
19972 if (inputs == error_mark_node)
19973 invalid_inputs_p = true;
19976 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19977 /* The clobbers are coming next. */
19978 clobbers_p = true;
19980 /* Look for clobbers. */
19981 if (clobbers_p
19982 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19984 clobbers_p = true;
19985 /* Consume the `:' or `::'. */
19986 cp_lexer_consume_token (parser->lexer);
19987 /* Parse the clobbers. */
19988 if (cp_lexer_next_token_is_not (parser->lexer,
19989 CPP_COLON)
19990 && cp_lexer_next_token_is_not (parser->lexer,
19991 CPP_CLOSE_PAREN))
19992 clobbers = cp_parser_asm_clobber_list (parser);
19994 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19995 /* The labels are coming next. */
19996 labels_p = true;
19998 /* Look for labels. */
19999 if (labels_p
20000 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
20002 labels_p = true;
20003 /* Consume the `:' or `::'. */
20004 cp_lexer_consume_token (parser->lexer);
20005 /* Parse the labels. */
20006 labels = cp_parser_asm_label_list (parser);
20009 if (goto_p && !labels_p)
20010 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20012 else if (goto_p)
20013 missing = RT_COLON_SCOPE;
20015 /* Look for the closing `)'. */
20016 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20017 missing ? missing : RT_CLOSE_PAREN))
20018 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20019 /*consume_paren=*/true);
20020 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20022 if (!invalid_inputs_p && !invalid_outputs_p)
20024 /* Create the ASM_EXPR. */
20025 if (parser->in_function_body)
20027 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
20028 inputs, clobbers, labels, inline_p);
20029 /* If the extended syntax was not used, mark the ASM_EXPR. */
20030 if (!extended_p)
20032 tree temp = asm_stmt;
20033 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20034 temp = TREE_OPERAND (temp, 0);
20036 ASM_INPUT_P (temp) = 1;
20039 else
20040 symtab->finalize_toplevel_asm (string);
20044 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20045 type that comes from the decl-specifier-seq. */
20047 static tree
20048 strip_declarator_types (tree type, cp_declarator *declarator)
20050 for (cp_declarator *d = declarator; d;)
20051 switch (d->kind)
20053 case cdk_id:
20054 case cdk_decomp:
20055 case cdk_error:
20056 d = NULL;
20057 break;
20059 default:
20060 if (TYPE_PTRMEMFUNC_P (type))
20061 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20062 type = TREE_TYPE (type);
20063 d = d->declarator;
20064 break;
20067 return type;
20070 /* Declarators [gram.dcl.decl] */
20072 /* Parse an init-declarator.
20074 init-declarator:
20075 declarator initializer [opt]
20077 GNU Extension:
20079 init-declarator:
20080 declarator asm-specification [opt] attributes [opt] initializer [opt]
20082 function-definition:
20083 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20084 function-body
20085 decl-specifier-seq [opt] declarator function-try-block
20087 GNU Extension:
20089 function-definition:
20090 __extension__ function-definition
20092 TM Extension:
20094 function-definition:
20095 decl-specifier-seq [opt] declarator function-transaction-block
20097 The parser flags FLAGS is used to control type-specifier parsing.
20099 The DECL_SPECIFIERS apply to this declarator. Returns a
20100 representation of the entity declared. If MEMBER_P is TRUE, then
20101 this declarator appears in a class scope. The new DECL created by
20102 this declarator is returned.
20104 The CHECKS are access checks that should be performed once we know
20105 what entity is being declared (and, therefore, what classes have
20106 befriended it).
20108 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20109 for a function-definition here as well. If the declarator is a
20110 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20111 be TRUE upon return. By that point, the function-definition will
20112 have been completely parsed.
20114 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20115 is FALSE.
20117 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20118 parsed declaration if it is an uninitialized single declarator not followed
20119 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20120 if present, will not be consumed. If returned, this declarator will be
20121 created with SD_INITIALIZED but will not call cp_finish_decl.
20123 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20124 and there is an initializer, the pointed location_t is set to the
20125 location of the '=' or `(', or '{' in C++11 token introducing the
20126 initializer. */
20128 static tree
20129 cp_parser_init_declarator (cp_parser* parser,
20130 cp_parser_flags flags,
20131 cp_decl_specifier_seq *decl_specifiers,
20132 vec<deferred_access_check, va_gc> *checks,
20133 bool function_definition_allowed_p,
20134 bool member_p,
20135 int declares_class_or_enum,
20136 bool* function_definition_p,
20137 tree* maybe_range_for_decl,
20138 location_t* init_loc,
20139 tree* auto_result)
20141 cp_token *token = NULL, *asm_spec_start_token = NULL,
20142 *attributes_start_token = NULL;
20143 cp_declarator *declarator;
20144 tree prefix_attributes;
20145 tree attributes = NULL;
20146 tree asm_specification;
20147 tree initializer;
20148 tree decl = NULL_TREE;
20149 tree scope;
20150 int is_initialized;
20151 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20152 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20153 "(...)". */
20154 enum cpp_ttype initialization_kind;
20155 bool is_direct_init = false;
20156 bool is_non_constant_init;
20157 int ctor_dtor_or_conv_p;
20158 bool friend_p = cp_parser_friend_p (decl_specifiers);
20159 tree pushed_scope = NULL_TREE;
20160 bool range_for_decl_p = false;
20161 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20162 location_t tmp_init_loc = UNKNOWN_LOCATION;
20164 /* Gather the attributes that were provided with the
20165 decl-specifiers. */
20166 prefix_attributes = decl_specifiers->attributes;
20168 /* Assume that this is not the declarator for a function
20169 definition. */
20170 if (function_definition_p)
20171 *function_definition_p = false;
20173 /* Default arguments are only permitted for function parameters. */
20174 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20175 parser->default_arg_ok_p = false;
20177 /* Defer access checks while parsing the declarator; we cannot know
20178 what names are accessible until we know what is being
20179 declared. */
20180 resume_deferring_access_checks ();
20182 token = cp_lexer_peek_token (parser->lexer);
20184 /* Parse the declarator. */
20185 declarator
20186 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20187 flags, &ctor_dtor_or_conv_p,
20188 /*parenthesized_p=*/NULL,
20189 member_p, friend_p, /*static_p=*/false);
20190 /* Gather up the deferred checks. */
20191 stop_deferring_access_checks ();
20193 parser->default_arg_ok_p = saved_default_arg_ok_p;
20195 /* If the DECLARATOR was erroneous, there's no need to go
20196 further. */
20197 if (declarator == cp_error_declarator)
20198 return error_mark_node;
20200 /* Check that the number of template-parameter-lists is OK. */
20201 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20202 token->location))
20203 return error_mark_node;
20205 if (declares_class_or_enum & 2)
20206 cp_parser_check_for_definition_in_return_type (declarator,
20207 decl_specifiers->type,
20208 decl_specifiers->locations[ds_type_spec]);
20210 /* Figure out what scope the entity declared by the DECLARATOR is
20211 located in. `grokdeclarator' sometimes changes the scope, so
20212 we compute it now. */
20213 scope = get_scope_of_declarator (declarator);
20215 /* Perform any lookups in the declared type which were thought to be
20216 dependent, but are not in the scope of the declarator. */
20217 decl_specifiers->type
20218 = maybe_update_decl_type (decl_specifiers->type, scope);
20220 /* If we're allowing GNU extensions, look for an
20221 asm-specification. */
20222 if (cp_parser_allow_gnu_extensions_p (parser))
20224 /* Look for an asm-specification. */
20225 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20226 asm_specification = cp_parser_asm_specification_opt (parser);
20228 else
20229 asm_specification = NULL_TREE;
20231 /* Look for attributes. */
20232 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20233 attributes = cp_parser_attributes_opt (parser);
20235 /* Peek at the next token. */
20236 token = cp_lexer_peek_token (parser->lexer);
20238 bool bogus_implicit_tmpl = false;
20240 if (function_declarator_p (declarator))
20242 /* Handle C++17 deduction guides. */
20243 if (!decl_specifiers->type
20244 && ctor_dtor_or_conv_p <= 0
20245 && cxx_dialect >= cxx17)
20247 cp_declarator *id = get_id_declarator (declarator);
20248 tree name = id->u.id.unqualified_name;
20249 parser->scope = id->u.id.qualifying_scope;
20250 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20251 if (tmpl
20252 && (DECL_CLASS_TEMPLATE_P (tmpl)
20253 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20255 id->u.id.unqualified_name = dguide_name (tmpl);
20256 id->u.id.sfk = sfk_deduction_guide;
20257 ctor_dtor_or_conv_p = 1;
20261 /* Check to see if the token indicates the start of a
20262 function-definition. */
20263 if (cp_parser_token_starts_function_definition_p (token))
20265 if (!function_definition_allowed_p)
20267 /* If a function-definition should not appear here, issue an
20268 error message. */
20269 cp_parser_error (parser,
20270 "a function-definition is not allowed here");
20271 return error_mark_node;
20274 location_t func_brace_location
20275 = cp_lexer_peek_token (parser->lexer)->location;
20277 /* Neither attributes nor an asm-specification are allowed
20278 on a function-definition. */
20279 if (asm_specification)
20280 error_at (asm_spec_start_token->location,
20281 "an %<asm%> specification is not allowed "
20282 "on a function-definition");
20283 if (attributes)
20284 error_at (attributes_start_token->location,
20285 "attributes are not allowed "
20286 "on a function-definition");
20287 /* This is a function-definition. */
20288 *function_definition_p = true;
20290 /* Parse the function definition. */
20291 if (member_p)
20292 decl = cp_parser_save_member_function_body (parser,
20293 decl_specifiers,
20294 declarator,
20295 prefix_attributes);
20296 else
20297 decl =
20298 (cp_parser_function_definition_from_specifiers_and_declarator
20299 (parser, decl_specifiers, prefix_attributes, declarator));
20301 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20303 /* This is where the prologue starts... */
20304 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20305 = func_brace_location;
20308 return decl;
20311 else if (parser->fully_implicit_function_template_p)
20313 /* A non-template declaration involving a function parameter list
20314 containing an implicit template parameter will be made into a
20315 template. If the resulting declaration is not going to be an
20316 actual function then finish the template scope here to prevent it.
20317 An error message will be issued once we have a decl to talk about.
20319 FIXME probably we should do type deduction rather than create an
20320 implicit template, but the standard currently doesn't allow it. */
20321 bogus_implicit_tmpl = true;
20322 finish_fully_implicit_template (parser, NULL_TREE);
20325 /* [dcl.dcl]
20327 Only in function declarations for constructors, destructors, type
20328 conversions, and deduction guides can the decl-specifier-seq be omitted.
20330 We explicitly postpone this check past the point where we handle
20331 function-definitions because we tolerate function-definitions
20332 that are missing their return types in some modes. */
20333 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20335 cp_parser_error (parser,
20336 "expected constructor, destructor, or type conversion");
20337 return error_mark_node;
20340 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20341 if (token->type == CPP_EQ
20342 || token->type == CPP_OPEN_PAREN
20343 || token->type == CPP_OPEN_BRACE)
20345 is_initialized = SD_INITIALIZED;
20346 initialization_kind = token->type;
20347 if (maybe_range_for_decl)
20348 *maybe_range_for_decl = error_mark_node;
20349 tmp_init_loc = token->location;
20350 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20351 *init_loc = tmp_init_loc;
20353 if (token->type == CPP_EQ
20354 && function_declarator_p (declarator))
20356 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20357 if (t2->keyword == RID_DEFAULT)
20358 is_initialized = SD_DEFAULTED;
20359 else if (t2->keyword == RID_DELETE)
20360 is_initialized = SD_DELETED;
20363 else
20365 /* If the init-declarator isn't initialized and isn't followed by a
20366 `,' or `;', it's not a valid init-declarator. */
20367 if (token->type != CPP_COMMA
20368 && token->type != CPP_SEMICOLON)
20370 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20371 range_for_decl_p = true;
20372 else
20374 if (!maybe_range_for_decl)
20375 cp_parser_error (parser, "expected initializer");
20376 return error_mark_node;
20379 is_initialized = SD_UNINITIALIZED;
20380 initialization_kind = CPP_EOF;
20383 /* Because start_decl has side-effects, we should only call it if we
20384 know we're going ahead. By this point, we know that we cannot
20385 possibly be looking at any other construct. */
20386 cp_parser_commit_to_tentative_parse (parser);
20388 /* Enter the newly declared entry in the symbol table. If we're
20389 processing a declaration in a class-specifier, we wait until
20390 after processing the initializer. */
20391 if (!member_p)
20393 if (parser->in_unbraced_linkage_specification_p)
20394 decl_specifiers->storage_class = sc_extern;
20395 decl = start_decl (declarator, decl_specifiers,
20396 range_for_decl_p? SD_INITIALIZED : is_initialized,
20397 attributes, prefix_attributes, &pushed_scope);
20398 cp_finalize_omp_declare_simd (parser, decl);
20399 cp_finalize_oacc_routine (parser, decl, false);
20400 /* Adjust location of decl if declarator->id_loc is more appropriate:
20401 set, and decl wasn't merged with another decl, in which case its
20402 location would be different from input_location, and more accurate. */
20403 if (DECL_P (decl)
20404 && declarator->id_loc != UNKNOWN_LOCATION
20405 && DECL_SOURCE_LOCATION (decl) == input_location)
20406 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20408 else if (scope)
20409 /* Enter the SCOPE. That way unqualified names appearing in the
20410 initializer will be looked up in SCOPE. */
20411 pushed_scope = push_scope (scope);
20413 /* Perform deferred access control checks, now that we know in which
20414 SCOPE the declared entity resides. */
20415 if (!member_p && decl)
20417 tree saved_current_function_decl = NULL_TREE;
20419 /* If the entity being declared is a function, pretend that we
20420 are in its scope. If it is a `friend', it may have access to
20421 things that would not otherwise be accessible. */
20422 if (TREE_CODE (decl) == FUNCTION_DECL)
20424 saved_current_function_decl = current_function_decl;
20425 current_function_decl = decl;
20428 /* Perform access checks for template parameters. */
20429 cp_parser_perform_template_parameter_access_checks (checks);
20431 /* Perform the access control checks for the declarator and the
20432 decl-specifiers. */
20433 perform_deferred_access_checks (tf_warning_or_error);
20435 /* Restore the saved value. */
20436 if (TREE_CODE (decl) == FUNCTION_DECL)
20437 current_function_decl = saved_current_function_decl;
20440 /* Parse the initializer. */
20441 initializer = NULL_TREE;
20442 is_direct_init = false;
20443 is_non_constant_init = true;
20444 if (is_initialized)
20446 if (function_declarator_p (declarator))
20448 if (initialization_kind == CPP_EQ)
20449 initializer = cp_parser_pure_specifier (parser);
20450 else
20452 /* If the declaration was erroneous, we don't really
20453 know what the user intended, so just silently
20454 consume the initializer. */
20455 if (decl != error_mark_node)
20456 error_at (tmp_init_loc, "initializer provided for function");
20457 cp_parser_skip_to_closing_parenthesis (parser,
20458 /*recovering=*/true,
20459 /*or_comma=*/false,
20460 /*consume_paren=*/true);
20463 else
20465 /* We want to record the extra mangling scope for in-class
20466 initializers of class members and initializers of static data
20467 member templates. The former involves deferring
20468 parsing of the initializer until end of class as with default
20469 arguments. So right here we only handle the latter. */
20470 if (!member_p && processing_template_decl && decl != error_mark_node)
20471 start_lambda_scope (decl);
20472 initializer = cp_parser_initializer (parser,
20473 &is_direct_init,
20474 &is_non_constant_init);
20475 if (!member_p && processing_template_decl && decl != error_mark_node)
20476 finish_lambda_scope ();
20477 if (initializer == error_mark_node)
20478 cp_parser_skip_to_end_of_statement (parser);
20482 /* The old parser allows attributes to appear after a parenthesized
20483 initializer. Mark Mitchell proposed removing this functionality
20484 on the GCC mailing lists on 2002-08-13. This parser accepts the
20485 attributes -- but ignores them. Made a permerror in GCC 8. */
20486 if (cp_parser_allow_gnu_extensions_p (parser)
20487 && initialization_kind == CPP_OPEN_PAREN
20488 && cp_parser_attributes_opt (parser)
20489 && permerror (input_location,
20490 "attributes after parenthesized initializer ignored"))
20492 static bool hint;
20493 if (flag_permissive && !hint)
20495 hint = true;
20496 inform (input_location,
20497 "this flexibility is deprecated and will be removed");
20501 /* And now complain about a non-function implicit template. */
20502 if (bogus_implicit_tmpl && decl != error_mark_node)
20503 error_at (DECL_SOURCE_LOCATION (decl),
20504 "non-function %qD declared as implicit template", decl);
20506 /* For an in-class declaration, use `grokfield' to create the
20507 declaration. */
20508 if (member_p)
20510 if (pushed_scope)
20512 pop_scope (pushed_scope);
20513 pushed_scope = NULL_TREE;
20515 decl = grokfield (declarator, decl_specifiers,
20516 initializer, !is_non_constant_init,
20517 /*asmspec=*/NULL_TREE,
20518 attr_chainon (attributes, prefix_attributes));
20519 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20520 cp_parser_save_default_args (parser, decl);
20521 cp_finalize_omp_declare_simd (parser, decl);
20522 cp_finalize_oacc_routine (parser, decl, false);
20525 /* Finish processing the declaration. But, skip member
20526 declarations. */
20527 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20529 cp_finish_decl (decl,
20530 initializer, !is_non_constant_init,
20531 asm_specification,
20532 /* If the initializer is in parentheses, then this is
20533 a direct-initialization, which means that an
20534 `explicit' constructor is OK. Otherwise, an
20535 `explicit' constructor cannot be used. */
20536 ((is_direct_init || !is_initialized)
20537 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20539 else if ((cxx_dialect != cxx98) && friend_p
20540 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20541 /* Core issue #226 (C++0x only): A default template-argument
20542 shall not be specified in a friend class template
20543 declaration. */
20544 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20545 /*is_partial=*/false, /*is_friend_decl=*/1);
20547 if (!friend_p && pushed_scope)
20548 pop_scope (pushed_scope);
20550 if (function_declarator_p (declarator)
20551 && parser->fully_implicit_function_template_p)
20553 if (member_p)
20554 decl = finish_fully_implicit_template (parser, decl);
20555 else
20556 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20559 if (auto_result && is_initialized && decl_specifiers->type
20560 && type_uses_auto (decl_specifiers->type))
20561 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20563 return decl;
20566 /* Parse a declarator.
20568 declarator:
20569 direct-declarator
20570 ptr-operator declarator
20572 abstract-declarator:
20573 ptr-operator abstract-declarator [opt]
20574 direct-abstract-declarator
20576 GNU Extensions:
20578 declarator:
20579 attributes [opt] direct-declarator
20580 attributes [opt] ptr-operator declarator
20582 abstract-declarator:
20583 attributes [opt] ptr-operator abstract-declarator [opt]
20584 attributes [opt] direct-abstract-declarator
20586 The parser flags FLAGS is used to control type-specifier parsing.
20588 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20589 detect constructors, destructors, deduction guides, or conversion operators.
20590 It is set to -1 if the declarator is a name, and +1 if it is a
20591 function. Otherwise it is set to zero. Usually you just want to
20592 test for >0, but internally the negative value is used.
20594 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20595 a decl-specifier-seq unless it declares a constructor, destructor,
20596 or conversion. It might seem that we could check this condition in
20597 semantic analysis, rather than parsing, but that makes it difficult
20598 to handle something like `f()'. We want to notice that there are
20599 no decl-specifiers, and therefore realize that this is an
20600 expression, not a declaration.)
20602 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20603 the declarator is a direct-declarator of the form "(...)".
20605 MEMBER_P is true iff this declarator is a member-declarator.
20607 FRIEND_P is true iff this declarator is a friend.
20609 STATIC_P is true iff the keyword static was seen. */
20611 static cp_declarator *
20612 cp_parser_declarator (cp_parser* parser,
20613 cp_parser_declarator_kind dcl_kind,
20614 cp_parser_flags flags,
20615 int* ctor_dtor_or_conv_p,
20616 bool* parenthesized_p,
20617 bool member_p, bool friend_p, bool static_p)
20619 cp_declarator *declarator;
20620 enum tree_code code;
20621 cp_cv_quals cv_quals;
20622 tree class_type;
20623 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20625 /* Assume this is not a constructor, destructor, or type-conversion
20626 operator. */
20627 if (ctor_dtor_or_conv_p)
20628 *ctor_dtor_or_conv_p = 0;
20630 if (cp_parser_allow_gnu_extensions_p (parser))
20631 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20633 /* Check for the ptr-operator production. */
20634 cp_parser_parse_tentatively (parser);
20635 /* Parse the ptr-operator. */
20636 code = cp_parser_ptr_operator (parser,
20637 &class_type,
20638 &cv_quals,
20639 &std_attributes);
20641 /* If that worked, then we have a ptr-operator. */
20642 if (cp_parser_parse_definitely (parser))
20644 /* If a ptr-operator was found, then this declarator was not
20645 parenthesized. */
20646 if (parenthesized_p)
20647 *parenthesized_p = true;
20648 /* The dependent declarator is optional if we are parsing an
20649 abstract-declarator. */
20650 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20651 cp_parser_parse_tentatively (parser);
20653 /* Parse the dependent declarator. */
20654 declarator = cp_parser_declarator (parser, dcl_kind,
20655 CP_PARSER_FLAGS_NONE,
20656 /*ctor_dtor_or_conv_p=*/NULL,
20657 /*parenthesized_p=*/NULL,
20658 /*member_p=*/false,
20659 friend_p, /*static_p=*/false);
20661 /* If we are parsing an abstract-declarator, we must handle the
20662 case where the dependent declarator is absent. */
20663 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20664 && !cp_parser_parse_definitely (parser))
20665 declarator = NULL;
20667 declarator = cp_parser_make_indirect_declarator
20668 (code, class_type, cv_quals, declarator, std_attributes);
20670 /* Everything else is a direct-declarator. */
20671 else
20673 if (parenthesized_p)
20674 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20675 CPP_OPEN_PAREN);
20676 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20677 flags, ctor_dtor_or_conv_p,
20678 member_p, friend_p, static_p);
20681 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20682 declarator->attributes = gnu_attributes;
20683 return declarator;
20686 /* Parse a direct-declarator or direct-abstract-declarator.
20688 direct-declarator:
20689 declarator-id
20690 direct-declarator ( parameter-declaration-clause )
20691 cv-qualifier-seq [opt]
20692 ref-qualifier [opt]
20693 exception-specification [opt]
20694 direct-declarator [ constant-expression [opt] ]
20695 ( declarator )
20697 direct-abstract-declarator:
20698 direct-abstract-declarator [opt]
20699 ( parameter-declaration-clause )
20700 cv-qualifier-seq [opt]
20701 ref-qualifier [opt]
20702 exception-specification [opt]
20703 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20704 ( abstract-declarator )
20706 Returns a representation of the declarator. DCL_KIND is
20707 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20708 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20709 we are parsing a direct-declarator. It is
20710 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20711 of ambiguity we prefer an abstract declarator, as per
20712 [dcl.ambig.res].
20713 The parser flags FLAGS is used to control type-specifier parsing.
20714 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20715 as for cp_parser_declarator. */
20717 static cp_declarator *
20718 cp_parser_direct_declarator (cp_parser* parser,
20719 cp_parser_declarator_kind dcl_kind,
20720 cp_parser_flags flags,
20721 int* ctor_dtor_or_conv_p,
20722 bool member_p, bool friend_p, bool static_p)
20724 cp_token *token;
20725 cp_declarator *declarator = NULL;
20726 tree scope = NULL_TREE;
20727 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20728 bool saved_in_declarator_p = parser->in_declarator_p;
20729 bool first = true;
20730 tree pushed_scope = NULL_TREE;
20731 cp_token *open_paren = NULL, *close_paren = NULL;
20733 while (true)
20735 /* Peek at the next token. */
20736 token = cp_lexer_peek_token (parser->lexer);
20737 if (token->type == CPP_OPEN_PAREN)
20739 /* This is either a parameter-declaration-clause, or a
20740 parenthesized declarator. When we know we are parsing a
20741 named declarator, it must be a parenthesized declarator
20742 if FIRST is true. For instance, `(int)' is a
20743 parameter-declaration-clause, with an omitted
20744 direct-abstract-declarator. But `((*))', is a
20745 parenthesized abstract declarator. Finally, when T is a
20746 template parameter `(T)' is a
20747 parameter-declaration-clause, and not a parenthesized
20748 named declarator.
20750 We first try and parse a parameter-declaration-clause,
20751 and then try a nested declarator (if FIRST is true).
20753 It is not an error for it not to be a
20754 parameter-declaration-clause, even when FIRST is
20755 false. Consider,
20757 int i (int);
20758 int i (3);
20760 The first is the declaration of a function while the
20761 second is the definition of a variable, including its
20762 initializer.
20764 Having seen only the parenthesis, we cannot know which of
20765 these two alternatives should be selected. Even more
20766 complex are examples like:
20768 int i (int (a));
20769 int i (int (3));
20771 The former is a function-declaration; the latter is a
20772 variable initialization.
20774 Thus again, we try a parameter-declaration-clause, and if
20775 that fails, we back out and return. */
20777 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20779 tree params;
20780 bool is_declarator = false;
20782 open_paren = NULL;
20784 /* In a member-declarator, the only valid interpretation
20785 of a parenthesis is the start of a
20786 parameter-declaration-clause. (It is invalid to
20787 initialize a static data member with a parenthesized
20788 initializer; only the "=" form of initialization is
20789 permitted.) */
20790 if (!member_p)
20791 cp_parser_parse_tentatively (parser);
20793 /* Consume the `('. */
20794 matching_parens parens;
20795 parens.consume_open (parser);
20796 if (first)
20798 /* If this is going to be an abstract declarator, we're
20799 in a declarator and we can't have default args. */
20800 parser->default_arg_ok_p = false;
20801 parser->in_declarator_p = true;
20804 begin_scope (sk_function_parms, NULL_TREE);
20806 /* Parse the parameter-declaration-clause. */
20807 params
20808 = cp_parser_parameter_declaration_clause (parser, flags);
20810 /* Consume the `)'. */
20811 parens.require_close (parser);
20813 /* If all went well, parse the cv-qualifier-seq,
20814 ref-qualifier and the exception-specification. */
20815 if (member_p || cp_parser_parse_definitely (parser))
20817 cp_cv_quals cv_quals;
20818 cp_virt_specifiers virt_specifiers;
20819 cp_ref_qualifier ref_qual;
20820 tree exception_specification;
20821 tree late_return;
20822 tree attrs;
20823 bool memfn = (member_p || (pushed_scope
20824 && CLASS_TYPE_P (pushed_scope)));
20825 unsigned char local_variables_forbidden_p
20826 = parser->local_variables_forbidden_p;
20827 /* 'this' is not allowed in static member functions. */
20828 if (static_p || friend_p)
20829 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20831 is_declarator = true;
20833 if (ctor_dtor_or_conv_p)
20834 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20835 first = false;
20837 /* Parse the cv-qualifier-seq. */
20838 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20839 /* Parse the ref-qualifier. */
20840 ref_qual = cp_parser_ref_qualifier_opt (parser);
20841 /* Parse the tx-qualifier. */
20842 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20843 /* And the exception-specification. */
20844 exception_specification
20845 = cp_parser_exception_specification_opt (parser, friend_p);
20847 attrs = cp_parser_std_attribute_spec_seq (parser);
20849 /* In here, we handle cases where attribute is used after
20850 the function declaration. For example:
20851 void func (int x) __attribute__((vector(..))); */
20852 tree gnu_attrs = NULL_TREE;
20853 tree requires_clause = NULL_TREE;
20854 late_return = (cp_parser_late_return_type_opt
20855 (parser, declarator, requires_clause,
20856 memfn ? cv_quals : -1));
20858 /* Parse the virt-specifier-seq. */
20859 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20861 /* Create the function-declarator. */
20862 declarator = make_call_declarator (declarator,
20863 params,
20864 cv_quals,
20865 virt_specifiers,
20866 ref_qual,
20867 tx_qual,
20868 exception_specification,
20869 late_return,
20870 requires_clause);
20871 declarator->std_attributes = attrs;
20872 declarator->attributes = gnu_attrs;
20873 /* Any subsequent parameter lists are to do with
20874 return type, so are not those of the declared
20875 function. */
20876 parser->default_arg_ok_p = false;
20878 /* Restore the state of local_variables_forbidden_p. */
20879 parser->local_variables_forbidden_p
20880 = local_variables_forbidden_p;
20883 /* Remove the function parms from scope. */
20884 pop_bindings_and_leave_scope ();
20886 if (is_declarator)
20887 /* Repeat the main loop. */
20888 continue;
20891 /* If this is the first, we can try a parenthesized
20892 declarator. */
20893 if (first)
20895 bool saved_in_type_id_in_expr_p;
20897 parser->default_arg_ok_p = saved_default_arg_ok_p;
20898 parser->in_declarator_p = saved_in_declarator_p;
20900 open_paren = token;
20901 /* Consume the `('. */
20902 matching_parens parens;
20903 parens.consume_open (parser);
20904 /* Parse the nested declarator. */
20905 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20906 parser->in_type_id_in_expr_p = true;
20907 declarator
20908 = cp_parser_declarator (parser, dcl_kind, flags,
20909 ctor_dtor_or_conv_p,
20910 /*parenthesized_p=*/NULL,
20911 member_p, friend_p,
20912 /*static_p=*/false);
20913 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20914 first = false;
20915 /* Expect a `)'. */
20916 close_paren = cp_lexer_peek_token (parser->lexer);
20917 if (!parens.require_close (parser))
20918 declarator = cp_error_declarator;
20919 if (declarator == cp_error_declarator)
20920 break;
20922 goto handle_declarator;
20924 /* Otherwise, we must be done. */
20925 else
20926 break;
20928 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20929 && token->type == CPP_OPEN_SQUARE
20930 && !cp_next_tokens_can_be_attribute_p (parser))
20932 /* Parse an array-declarator. */
20933 tree bounds, attrs;
20935 if (ctor_dtor_or_conv_p)
20936 *ctor_dtor_or_conv_p = 0;
20938 open_paren = NULL;
20939 first = false;
20940 parser->default_arg_ok_p = false;
20941 parser->in_declarator_p = true;
20942 /* Consume the `['. */
20943 cp_lexer_consume_token (parser->lexer);
20944 /* Peek at the next token. */
20945 token = cp_lexer_peek_token (parser->lexer);
20946 /* If the next token is `]', then there is no
20947 constant-expression. */
20948 if (token->type != CPP_CLOSE_SQUARE)
20950 bool non_constant_p;
20951 bounds
20952 = cp_parser_constant_expression (parser,
20953 /*allow_non_constant=*/true,
20954 &non_constant_p);
20955 if (!non_constant_p)
20956 /* OK */;
20957 else if (error_operand_p (bounds))
20958 /* Already gave an error. */;
20959 else if (!parser->in_function_body
20960 || current_binding_level->kind == sk_function_parms)
20962 /* Normally, the array bound must be an integral constant
20963 expression. However, as an extension, we allow VLAs
20964 in function scopes as long as they aren't part of a
20965 parameter declaration. */
20966 cp_parser_error (parser,
20967 "array bound is not an integer constant");
20968 bounds = error_mark_node;
20970 else if (processing_template_decl
20971 && !type_dependent_expression_p (bounds))
20973 /* Remember this wasn't a constant-expression. */
20974 bounds = build_nop (TREE_TYPE (bounds), bounds);
20975 TREE_SIDE_EFFECTS (bounds) = 1;
20978 else
20979 bounds = NULL_TREE;
20980 /* Look for the closing `]'. */
20981 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20983 declarator = cp_error_declarator;
20984 break;
20987 attrs = cp_parser_std_attribute_spec_seq (parser);
20988 declarator = make_array_declarator (declarator, bounds);
20989 declarator->std_attributes = attrs;
20991 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20994 tree qualifying_scope;
20995 tree unqualified_name;
20996 tree attrs;
20997 special_function_kind sfk;
20998 bool abstract_ok;
20999 bool pack_expansion_p = false;
21000 cp_token *declarator_id_start_token;
21002 /* Parse a declarator-id */
21003 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21004 if (abstract_ok)
21006 cp_parser_parse_tentatively (parser);
21008 /* If we see an ellipsis, we should be looking at a
21009 parameter pack. */
21010 if (token->type == CPP_ELLIPSIS)
21012 /* Consume the `...' */
21013 cp_lexer_consume_token (parser->lexer);
21015 pack_expansion_p = true;
21019 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21020 unqualified_name
21021 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21022 qualifying_scope = parser->scope;
21023 if (abstract_ok)
21025 bool okay = false;
21027 if (!unqualified_name && pack_expansion_p)
21029 /* Check whether an error occurred. */
21030 okay = !cp_parser_error_occurred (parser);
21032 /* We already consumed the ellipsis to mark a
21033 parameter pack, but we have no way to report it,
21034 so abort the tentative parse. We will be exiting
21035 immediately anyway. */
21036 cp_parser_abort_tentative_parse (parser);
21038 else
21039 okay = cp_parser_parse_definitely (parser);
21041 if (!okay)
21042 unqualified_name = error_mark_node;
21043 else if (unqualified_name
21044 && (qualifying_scope
21045 || (!identifier_p (unqualified_name))))
21047 cp_parser_error (parser, "expected unqualified-id");
21048 unqualified_name = error_mark_node;
21052 if (!unqualified_name)
21053 return NULL;
21054 if (unqualified_name == error_mark_node)
21056 declarator = cp_error_declarator;
21057 pack_expansion_p = false;
21058 declarator->parameter_pack_p = false;
21059 break;
21062 attrs = cp_parser_std_attribute_spec_seq (parser);
21064 if (qualifying_scope && at_namespace_scope_p ()
21065 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21067 /* In the declaration of a member of a template class
21068 outside of the class itself, the SCOPE will sometimes
21069 be a TYPENAME_TYPE. For example, given:
21071 template <typename T>
21072 int S<T>::R::i = 3;
21074 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21075 this context, we must resolve S<T>::R to an ordinary
21076 type, rather than a typename type.
21078 The reason we normally avoid resolving TYPENAME_TYPEs
21079 is that a specialization of `S' might render
21080 `S<T>::R' not a type. However, if `S' is
21081 specialized, then this `i' will not be used, so there
21082 is no harm in resolving the types here. */
21083 tree type;
21085 /* Resolve the TYPENAME_TYPE. */
21086 type = resolve_typename_type (qualifying_scope,
21087 /*only_current_p=*/false);
21088 /* If that failed, the declarator is invalid. */
21089 if (TREE_CODE (type) == TYPENAME_TYPE)
21091 if (typedef_variant_p (type))
21092 error_at (declarator_id_start_token->location,
21093 "cannot define member of dependent typedef "
21094 "%qT", type);
21095 else
21096 error_at (declarator_id_start_token->location,
21097 "%<%T::%E%> is not a type",
21098 TYPE_CONTEXT (qualifying_scope),
21099 TYPE_IDENTIFIER (qualifying_scope));
21101 qualifying_scope = type;
21104 sfk = sfk_none;
21106 if (unqualified_name)
21108 tree class_type;
21110 if (qualifying_scope
21111 && CLASS_TYPE_P (qualifying_scope))
21112 class_type = qualifying_scope;
21113 else
21114 class_type = current_class_type;
21116 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21118 tree name_type = TREE_TYPE (unqualified_name);
21120 if (!class_type || !same_type_p (name_type, class_type))
21122 /* We do not attempt to print the declarator
21123 here because we do not have enough
21124 information about its original syntactic
21125 form. */
21126 cp_parser_error (parser, "invalid declarator");
21127 declarator = cp_error_declarator;
21128 break;
21130 else if (qualifying_scope
21131 && CLASSTYPE_USE_TEMPLATE (name_type))
21133 error_at (declarator_id_start_token->location,
21134 "invalid use of constructor as a template");
21135 inform (declarator_id_start_token->location,
21136 "use %<%T::%D%> instead of %<%T::%D%> to "
21137 "name the constructor in a qualified name",
21138 class_type,
21139 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21140 class_type, name_type);
21141 declarator = cp_error_declarator;
21142 break;
21144 unqualified_name = constructor_name (class_type);
21147 if (class_type)
21149 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21150 sfk = sfk_destructor;
21151 else if (identifier_p (unqualified_name)
21152 && IDENTIFIER_CONV_OP_P (unqualified_name))
21153 sfk = sfk_conversion;
21154 else if (/* There's no way to declare a constructor
21155 for an unnamed type, even if the type
21156 got a name for linkage purposes. */
21157 !TYPE_WAS_UNNAMED (class_type)
21158 /* Handle correctly (c++/19200):
21160 struct S {
21161 struct T{};
21162 friend void S(T);
21165 and also:
21167 namespace N {
21168 void S();
21171 struct S {
21172 friend void N::S();
21173 }; */
21174 && (!friend_p || class_type == qualifying_scope)
21175 && constructor_name_p (unqualified_name,
21176 class_type))
21177 sfk = sfk_constructor;
21178 else if (is_overloaded_fn (unqualified_name)
21179 && DECL_CONSTRUCTOR_P (get_first_fn
21180 (unqualified_name)))
21181 sfk = sfk_constructor;
21183 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21184 *ctor_dtor_or_conv_p = -1;
21187 declarator = make_id_declarator (qualifying_scope,
21188 unqualified_name,
21189 sfk, token->location);
21190 declarator->std_attributes = attrs;
21191 declarator->parameter_pack_p = pack_expansion_p;
21193 if (pack_expansion_p)
21194 maybe_warn_variadic_templates ();
21196 /* We're looking for this case in [temp.res]:
21197 A qualified-id is assumed to name a type if [...]
21198 - it is a decl-specifier of the decl-specifier-seq of a
21199 parameter-declaration in a declarator of a function or
21200 function template declaration, ... */
21201 if (cxx_dialect >= cxx2a
21202 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21203 && declarator->kind == cdk_id
21204 && !at_class_scope_p ()
21205 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21207 /* ...whose declarator-id is qualified. If it isn't, never
21208 assume the parameters to refer to types. */
21209 if (qualifying_scope == NULL_TREE)
21210 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21211 else
21213 /* Now we have something like
21214 template <typename T> int C::x(S::p);
21215 which can be a function template declaration or a
21216 variable template definition. If name lookup for
21217 the declarator-id C::x finds one or more function
21218 templates, assume S::p to name a type. Otherwise,
21219 don't. */
21220 tree decl
21221 = cp_parser_lookup_name_simple (parser, unqualified_name,
21222 token->location);
21223 if (!is_overloaded_fn (decl)
21224 /* Allow
21225 template<typename T>
21226 A<T>::A(T::type) { } */
21227 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21228 && constructor_name_p (unqualified_name,
21229 qualifying_scope)))
21230 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21235 handle_declarator:;
21236 scope = get_scope_of_declarator (declarator);
21237 if (scope)
21239 /* Any names that appear after the declarator-id for a
21240 member are looked up in the containing scope. */
21241 if (at_function_scope_p ())
21243 /* But declarations with qualified-ids can't appear in a
21244 function. */
21245 cp_parser_error (parser, "qualified-id in declaration");
21246 declarator = cp_error_declarator;
21247 break;
21249 pushed_scope = push_scope (scope);
21251 parser->in_declarator_p = true;
21252 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21253 || (declarator && declarator->kind == cdk_id))
21254 /* Default args are only allowed on function
21255 declarations. */
21256 parser->default_arg_ok_p = saved_default_arg_ok_p;
21257 else
21258 parser->default_arg_ok_p = false;
21260 first = false;
21262 /* We're done. */
21263 else
21264 break;
21267 /* For an abstract declarator, we might wind up with nothing at this
21268 point. That's an error; the declarator is not optional. */
21269 if (!declarator)
21270 cp_parser_error (parser, "expected declarator");
21271 else if (open_paren)
21273 /* Record overly parenthesized declarator so we can give a
21274 diagnostic about confusing decl/expr disambiguation. */
21275 if (declarator->kind == cdk_array)
21277 /* If the open and close parens are on different lines, this
21278 is probably a formatting thing, so ignore. */
21279 expanded_location open = expand_location (open_paren->location);
21280 expanded_location close = expand_location (close_paren->location);
21281 if (open.line != close.line || open.file != close.file)
21282 open_paren = NULL;
21284 if (open_paren)
21285 declarator->parenthesized = open_paren->location;
21288 /* If we entered a scope, we must exit it now. */
21289 if (pushed_scope)
21290 pop_scope (pushed_scope);
21292 parser->default_arg_ok_p = saved_default_arg_ok_p;
21293 parser->in_declarator_p = saved_in_declarator_p;
21295 return declarator;
21298 /* Parse a ptr-operator.
21300 ptr-operator:
21301 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21302 * cv-qualifier-seq [opt]
21304 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21305 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21307 GNU Extension:
21309 ptr-operator:
21310 & cv-qualifier-seq [opt]
21312 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21313 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21314 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21315 filled in with the TYPE containing the member. *CV_QUALS is
21316 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21317 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21318 Note that the tree codes returned by this function have nothing
21319 to do with the types of trees that will be eventually be created
21320 to represent the pointer or reference type being parsed. They are
21321 just constants with suggestive names. */
21322 static enum tree_code
21323 cp_parser_ptr_operator (cp_parser* parser,
21324 tree* type,
21325 cp_cv_quals *cv_quals,
21326 tree *attributes)
21328 enum tree_code code = ERROR_MARK;
21329 cp_token *token;
21330 tree attrs = NULL_TREE;
21332 /* Assume that it's not a pointer-to-member. */
21333 *type = NULL_TREE;
21334 /* And that there are no cv-qualifiers. */
21335 *cv_quals = TYPE_UNQUALIFIED;
21337 /* Peek at the next token. */
21338 token = cp_lexer_peek_token (parser->lexer);
21340 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21341 if (token->type == CPP_MULT)
21342 code = INDIRECT_REF;
21343 else if (token->type == CPP_AND)
21344 code = ADDR_EXPR;
21345 else if ((cxx_dialect != cxx98) &&
21346 token->type == CPP_AND_AND) /* C++0x only */
21347 code = NON_LVALUE_EXPR;
21349 if (code != ERROR_MARK)
21351 /* Consume the `*', `&' or `&&'. */
21352 cp_lexer_consume_token (parser->lexer);
21354 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21355 `&', if we are allowing GNU extensions. (The only qualifier
21356 that can legally appear after `&' is `restrict', but that is
21357 enforced during semantic analysis. */
21358 if (code == INDIRECT_REF
21359 || cp_parser_allow_gnu_extensions_p (parser))
21360 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21362 attrs = cp_parser_std_attribute_spec_seq (parser);
21363 if (attributes != NULL)
21364 *attributes = attrs;
21366 else
21368 /* Try the pointer-to-member case. */
21369 cp_parser_parse_tentatively (parser);
21370 /* Look for the optional `::' operator. */
21371 cp_parser_global_scope_opt (parser,
21372 /*current_scope_valid_p=*/false);
21373 /* Look for the nested-name specifier. */
21374 token = cp_lexer_peek_token (parser->lexer);
21375 cp_parser_nested_name_specifier (parser,
21376 /*typename_keyword_p=*/false,
21377 /*check_dependency_p=*/true,
21378 /*type_p=*/false,
21379 /*is_declaration=*/false);
21380 /* If we found it, and the next token is a `*', then we are
21381 indeed looking at a pointer-to-member operator. */
21382 if (!cp_parser_error_occurred (parser)
21383 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21385 /* Indicate that the `*' operator was used. */
21386 code = INDIRECT_REF;
21388 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21389 error_at (token->location, "%qD is a namespace", parser->scope);
21390 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21391 error_at (token->location, "cannot form pointer to member of "
21392 "non-class %q#T", parser->scope);
21393 else
21395 /* The type of which the member is a member is given by the
21396 current SCOPE. */
21397 *type = parser->scope;
21398 /* The next name will not be qualified. */
21399 parser->scope = NULL_TREE;
21400 parser->qualifying_scope = NULL_TREE;
21401 parser->object_scope = NULL_TREE;
21402 /* Look for optional c++11 attributes. */
21403 attrs = cp_parser_std_attribute_spec_seq (parser);
21404 if (attributes != NULL)
21405 *attributes = attrs;
21406 /* Look for the optional cv-qualifier-seq. */
21407 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21410 /* If that didn't work we don't have a ptr-operator. */
21411 if (!cp_parser_parse_definitely (parser))
21412 cp_parser_error (parser, "expected ptr-operator");
21415 return code;
21418 /* Parse an (optional) cv-qualifier-seq.
21420 cv-qualifier-seq:
21421 cv-qualifier cv-qualifier-seq [opt]
21423 cv-qualifier:
21424 const
21425 volatile
21427 GNU Extension:
21429 cv-qualifier:
21430 __restrict__
21432 Returns a bitmask representing the cv-qualifiers. */
21434 static cp_cv_quals
21435 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21437 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21439 while (true)
21441 cp_token *token;
21442 cp_cv_quals cv_qualifier;
21444 /* Peek at the next token. */
21445 token = cp_lexer_peek_token (parser->lexer);
21446 /* See if it's a cv-qualifier. */
21447 switch (token->keyword)
21449 case RID_CONST:
21450 cv_qualifier = TYPE_QUAL_CONST;
21451 break;
21453 case RID_VOLATILE:
21454 cv_qualifier = TYPE_QUAL_VOLATILE;
21455 break;
21457 case RID_RESTRICT:
21458 cv_qualifier = TYPE_QUAL_RESTRICT;
21459 break;
21461 default:
21462 cv_qualifier = TYPE_UNQUALIFIED;
21463 break;
21466 if (!cv_qualifier)
21467 break;
21469 if (cv_quals & cv_qualifier)
21471 gcc_rich_location richloc (token->location);
21472 richloc.add_fixit_remove ();
21473 error_at (&richloc, "duplicate cv-qualifier");
21474 cp_lexer_purge_token (parser->lexer);
21476 else
21478 cp_lexer_consume_token (parser->lexer);
21479 cv_quals |= cv_qualifier;
21483 return cv_quals;
21486 /* Parse an (optional) ref-qualifier
21488 ref-qualifier:
21492 Returns cp_ref_qualifier representing ref-qualifier. */
21494 static cp_ref_qualifier
21495 cp_parser_ref_qualifier_opt (cp_parser* parser)
21497 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21499 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21500 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21501 return ref_qual;
21503 while (true)
21505 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21506 cp_token *token = cp_lexer_peek_token (parser->lexer);
21508 switch (token->type)
21510 case CPP_AND:
21511 curr_ref_qual = REF_QUAL_LVALUE;
21512 break;
21514 case CPP_AND_AND:
21515 curr_ref_qual = REF_QUAL_RVALUE;
21516 break;
21518 default:
21519 curr_ref_qual = REF_QUAL_NONE;
21520 break;
21523 if (!curr_ref_qual)
21524 break;
21525 else if (ref_qual)
21527 error_at (token->location, "multiple ref-qualifiers");
21528 cp_lexer_purge_token (parser->lexer);
21530 else
21532 ref_qual = curr_ref_qual;
21533 cp_lexer_consume_token (parser->lexer);
21537 return ref_qual;
21540 /* Parse an optional tx-qualifier.
21542 tx-qualifier:
21543 transaction_safe
21544 transaction_safe_dynamic */
21546 static tree
21547 cp_parser_tx_qualifier_opt (cp_parser *parser)
21549 cp_token *token = cp_lexer_peek_token (parser->lexer);
21550 if (token->type == CPP_NAME)
21552 tree name = token->u.value;
21553 const char *p = IDENTIFIER_POINTER (name);
21554 const int len = strlen ("transaction_safe");
21555 if (!strncmp (p, "transaction_safe", len))
21557 p += len;
21558 if (*p == '\0'
21559 || !strcmp (p, "_dynamic"))
21561 cp_lexer_consume_token (parser->lexer);
21562 if (!flag_tm)
21564 error ("%qE requires %<-fgnu-tm%>", name);
21565 return NULL_TREE;
21567 else
21568 return name;
21572 return NULL_TREE;
21575 /* Parse an (optional) virt-specifier-seq.
21577 virt-specifier-seq:
21578 virt-specifier virt-specifier-seq [opt]
21580 virt-specifier:
21581 override
21582 final
21584 Returns a bitmask representing the virt-specifiers. */
21586 static cp_virt_specifiers
21587 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21589 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21591 while (true)
21593 cp_token *token;
21594 cp_virt_specifiers virt_specifier;
21596 /* Peek at the next token. */
21597 token = cp_lexer_peek_token (parser->lexer);
21598 /* See if it's a virt-specifier-qualifier. */
21599 if (token->type != CPP_NAME)
21600 break;
21601 if (id_equal (token->u.value, "override"))
21603 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21604 virt_specifier = VIRT_SPEC_OVERRIDE;
21606 else if (id_equal (token->u.value, "final"))
21608 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21609 virt_specifier = VIRT_SPEC_FINAL;
21611 else if (id_equal (token->u.value, "__final"))
21613 virt_specifier = VIRT_SPEC_FINAL;
21615 else
21616 break;
21618 if (virt_specifiers & virt_specifier)
21620 gcc_rich_location richloc (token->location);
21621 richloc.add_fixit_remove ();
21622 error_at (&richloc, "duplicate virt-specifier");
21623 cp_lexer_purge_token (parser->lexer);
21625 else
21627 cp_lexer_consume_token (parser->lexer);
21628 virt_specifiers |= virt_specifier;
21631 return virt_specifiers;
21634 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21635 is in scope even though it isn't real. */
21637 void
21638 inject_this_parameter (tree ctype, cp_cv_quals quals)
21640 tree this_parm;
21642 if (current_class_ptr)
21644 /* We don't clear this between NSDMIs. Is it already what we want? */
21645 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21646 if (DECL_P (current_class_ptr)
21647 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21648 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21649 && cp_type_quals (type) == quals)
21650 return;
21653 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21654 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21655 current_class_ptr = NULL_TREE;
21656 current_class_ref
21657 = cp_build_fold_indirect_ref (this_parm);
21658 current_class_ptr = this_parm;
21661 /* Return true iff our current scope is a non-static data member
21662 initializer. */
21664 bool
21665 parsing_nsdmi (void)
21667 /* We recognize NSDMI context by the context-less 'this' pointer set up
21668 by the function above. */
21669 if (current_class_ptr
21670 && TREE_CODE (current_class_ptr) == PARM_DECL
21671 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21672 return true;
21673 return false;
21676 /* Parse a late-specified return type, if any. This is not a separate
21677 non-terminal, but part of a function declarator, which looks like
21679 -> trailing-type-specifier-seq abstract-declarator(opt)
21681 Returns the type indicated by the type-id.
21683 In addition to this, parse any queued up #pragma omp declare simd
21684 clauses, and #pragma acc routine clauses.
21686 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21687 function. */
21689 static tree
21690 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21691 tree& requires_clause, cp_cv_quals quals)
21693 cp_token *token;
21694 tree type = NULL_TREE;
21695 bool declare_simd_p = (parser->omp_declare_simd
21696 && declarator
21697 && declarator->kind == cdk_id);
21699 bool oacc_routine_p = (parser->oacc_routine
21700 && declarator
21701 && declarator->kind == cdk_id);
21703 /* Peek at the next token. */
21704 token = cp_lexer_peek_token (parser->lexer);
21705 /* A late-specified return type is indicated by an initial '->'. */
21706 if (token->type != CPP_DEREF
21707 && token->keyword != RID_REQUIRES
21708 && !(token->type == CPP_NAME
21709 && token->u.value == ridpointers[RID_REQUIRES])
21710 && !(declare_simd_p || oacc_routine_p))
21711 return NULL_TREE;
21713 tree save_ccp = current_class_ptr;
21714 tree save_ccr = current_class_ref;
21715 if (quals >= 0)
21717 /* DR 1207: 'this' is in scope in the trailing return type. */
21718 inject_this_parameter (current_class_type, quals);
21721 if (token->type == CPP_DEREF)
21723 /* Consume the ->. */
21724 cp_lexer_consume_token (parser->lexer);
21726 type = cp_parser_trailing_type_id (parser);
21729 /* Function declarations may be followed by a trailing
21730 requires-clause. */
21731 requires_clause = cp_parser_requires_clause_opt (parser);
21733 if (declare_simd_p)
21734 declarator->attributes
21735 = cp_parser_late_parsing_omp_declare_simd (parser,
21736 declarator->attributes);
21737 if (oacc_routine_p)
21738 declarator->attributes
21739 = cp_parser_late_parsing_oacc_routine (parser,
21740 declarator->attributes);
21742 if (quals >= 0)
21744 current_class_ptr = save_ccp;
21745 current_class_ref = save_ccr;
21748 return type;
21751 /* Parse a declarator-id.
21753 declarator-id:
21754 id-expression
21755 :: [opt] nested-name-specifier [opt] type-name
21757 In the `id-expression' case, the value returned is as for
21758 cp_parser_id_expression if the id-expression was an unqualified-id.
21759 If the id-expression was a qualified-id, then a SCOPE_REF is
21760 returned. The first operand is the scope (either a NAMESPACE_DECL
21761 or TREE_TYPE), but the second is still just a representation of an
21762 unqualified-id. */
21764 static tree
21765 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21767 tree id;
21768 /* The expression must be an id-expression. Assume that qualified
21769 names are the names of types so that:
21771 template <class T>
21772 int S<T>::R::i = 3;
21774 will work; we must treat `S<T>::R' as the name of a type.
21775 Similarly, assume that qualified names are templates, where
21776 required, so that:
21778 template <class T>
21779 int S<T>::R<T>::i = 3;
21781 will work, too. */
21782 id = cp_parser_id_expression (parser,
21783 /*template_keyword_p=*/false,
21784 /*check_dependency_p=*/false,
21785 /*template_p=*/NULL,
21786 /*declarator_p=*/true,
21787 optional_p);
21788 if (id && BASELINK_P (id))
21789 id = BASELINK_FUNCTIONS (id);
21790 return id;
21793 /* Parse a type-id.
21795 type-id:
21796 type-specifier-seq abstract-declarator [opt]
21798 The parser flags FLAGS is used to control type-specifier parsing.
21800 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21802 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21803 i.e. we've just seen "->".
21805 Returns the TYPE specified. */
21807 static tree
21808 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21809 bool is_template_arg, bool is_trailing_return,
21810 location_t *type_location)
21812 cp_decl_specifier_seq type_specifier_seq;
21813 cp_declarator *abstract_declarator;
21815 /* Parse the type-specifier-seq. */
21816 cp_parser_type_specifier_seq (parser, flags,
21817 /*is_declaration=*/false,
21818 is_trailing_return,
21819 &type_specifier_seq);
21820 if (type_location)
21821 *type_location = type_specifier_seq.locations[ds_type_spec];
21823 if (is_template_arg && type_specifier_seq.type
21824 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21825 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21826 /* A bare template name as a template argument is a template template
21827 argument, not a placeholder, so fail parsing it as a type argument. */
21829 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21830 cp_parser_simulate_error (parser);
21831 return error_mark_node;
21833 if (type_specifier_seq.type == error_mark_node)
21834 return error_mark_node;
21836 /* There might or might not be an abstract declarator. */
21837 cp_parser_parse_tentatively (parser);
21838 /* Look for the declarator. */
21839 abstract_declarator
21840 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21841 CP_PARSER_FLAGS_NONE, NULL,
21842 /*parenthesized_p=*/NULL,
21843 /*member_p=*/false,
21844 /*friend_p=*/false,
21845 /*static_p=*/false);
21846 /* Check to see if there really was a declarator. */
21847 if (!cp_parser_parse_definitely (parser))
21848 abstract_declarator = NULL;
21850 if (type_specifier_seq.type
21851 /* The concepts TS allows 'auto' as a type-id. */
21852 && (!flag_concepts || parser->in_type_id_in_expr_p)
21853 /* None of the valid uses of 'auto' in C++14 involve the type-id
21854 nonterminal, but it is valid in a trailing-return-type. */
21855 && !(cxx_dialect >= cxx14 && is_trailing_return))
21856 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21858 /* A type-id with type 'auto' is only ok if the abstract declarator
21859 is a function declarator with a late-specified return type.
21861 A type-id with 'auto' is also valid in a trailing-return-type
21862 in a compound-requirement. */
21863 if (abstract_declarator
21864 && abstract_declarator->kind == cdk_function
21865 && abstract_declarator->u.function.late_return_type)
21866 /* OK */;
21867 else if (parser->in_result_type_constraint_p)
21868 /* OK */;
21869 else
21871 location_t loc = type_specifier_seq.locations[ds_type_spec];
21872 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21874 error_at (loc, "missing template arguments after %qT",
21875 auto_node);
21876 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21877 tmpl);
21879 else
21880 error_at (loc, "invalid use of %qT", auto_node);
21881 return error_mark_node;
21885 return groktypename (&type_specifier_seq, abstract_declarator,
21886 is_template_arg);
21889 /* Wrapper for cp_parser_type_id_1. */
21891 static tree
21892 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21893 location_t *type_location)
21895 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21898 /* Wrapper for cp_parser_type_id_1. */
21900 static tree
21901 cp_parser_template_type_arg (cp_parser *parser)
21903 tree r;
21904 const char *saved_message = parser->type_definition_forbidden_message;
21905 parser->type_definition_forbidden_message
21906 = G_("types may not be defined in template arguments");
21907 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21908 parser->type_definition_forbidden_message = saved_message;
21909 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21911 error ("invalid use of %<auto%> in template argument");
21912 r = error_mark_node;
21914 return r;
21917 /* Wrapper for cp_parser_type_id_1. */
21919 static tree
21920 cp_parser_trailing_type_id (cp_parser *parser)
21922 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21923 false, true, NULL);
21926 /* Parse a type-specifier-seq.
21928 type-specifier-seq:
21929 type-specifier type-specifier-seq [opt]
21931 GNU extension:
21933 type-specifier-seq:
21934 attributes type-specifier-seq [opt]
21936 The parser flags FLAGS is used to control type-specifier parsing.
21938 If IS_DECLARATION is true, we are at the start of a "condition" or
21939 exception-declaration, so we might be followed by a declarator-id.
21941 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21942 i.e. we've just seen "->".
21944 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21946 static void
21947 cp_parser_type_specifier_seq (cp_parser* parser,
21948 cp_parser_flags flags,
21949 bool is_declaration,
21950 bool is_trailing_return,
21951 cp_decl_specifier_seq *type_specifier_seq)
21953 bool seen_type_specifier = false;
21954 cp_token *start_token = NULL;
21956 /* Clear the TYPE_SPECIFIER_SEQ. */
21957 clear_decl_specs (type_specifier_seq);
21959 flags |= CP_PARSER_FLAGS_OPTIONAL;
21960 /* In the context of a trailing return type, enum E { } is an
21961 elaborated-type-specifier followed by a function-body, not an
21962 enum-specifier. */
21963 if (is_trailing_return)
21964 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21966 /* Parse the type-specifiers and attributes. */
21967 while (true)
21969 tree type_specifier;
21970 bool is_cv_qualifier;
21972 /* Check for attributes first. */
21973 if (cp_next_tokens_can_be_attribute_p (parser))
21975 type_specifier_seq->attributes
21976 = attr_chainon (type_specifier_seq->attributes,
21977 cp_parser_attributes_opt (parser));
21978 continue;
21981 /* record the token of the beginning of the type specifier seq,
21982 for error reporting purposes*/
21983 if (!start_token)
21984 start_token = cp_lexer_peek_token (parser->lexer);
21986 /* Look for the type-specifier. */
21987 type_specifier = cp_parser_type_specifier (parser,
21988 flags,
21989 type_specifier_seq,
21990 /*is_declaration=*/false,
21991 NULL,
21992 &is_cv_qualifier);
21993 if (!type_specifier)
21995 /* If the first type-specifier could not be found, this is not a
21996 type-specifier-seq at all. */
21997 if (!seen_type_specifier)
21999 /* Set in_declarator_p to avoid skipping to the semicolon. */
22000 int in_decl = parser->in_declarator_p;
22001 parser->in_declarator_p = true;
22003 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22004 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22005 cp_parser_error (parser, "expected type-specifier");
22007 parser->in_declarator_p = in_decl;
22009 type_specifier_seq->type = error_mark_node;
22010 return;
22012 /* If subsequent type-specifiers could not be found, the
22013 type-specifier-seq is complete. */
22014 break;
22017 seen_type_specifier = true;
22018 /* The standard says that a condition can be:
22020 type-specifier-seq declarator = assignment-expression
22022 However, given:
22024 struct S {};
22025 if (int S = ...)
22027 we should treat the "S" as a declarator, not as a
22028 type-specifier. The standard doesn't say that explicitly for
22029 type-specifier-seq, but it does say that for
22030 decl-specifier-seq in an ordinary declaration. Perhaps it
22031 would be clearer just to allow a decl-specifier-seq here, and
22032 then add a semantic restriction that if any decl-specifiers
22033 that are not type-specifiers appear, the program is invalid. */
22034 if (is_declaration && !is_cv_qualifier)
22035 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22039 /* Return whether the function currently being declared has an associated
22040 template parameter list. */
22042 static bool
22043 function_being_declared_is_template_p (cp_parser* parser)
22045 if (!current_template_parms || processing_template_parmlist)
22046 return false;
22048 if (parser->implicit_template_scope)
22049 return true;
22051 if (at_class_scope_p ()
22052 && TYPE_BEING_DEFINED (current_class_type))
22053 return parser->num_template_parameter_lists != 0;
22055 return ((int) parser->num_template_parameter_lists > template_class_depth
22056 (current_class_type));
22059 /* Parse a parameter-declaration-clause.
22061 parameter-declaration-clause:
22062 parameter-declaration-list [opt] ... [opt]
22063 parameter-declaration-list , ...
22065 The parser flags FLAGS is used to control type-specifier parsing.
22067 Returns a representation for the parameter declarations. A return
22068 value of NULL indicates a parameter-declaration-clause consisting
22069 only of an ellipsis. */
22071 static tree
22072 cp_parser_parameter_declaration_clause (cp_parser* parser,
22073 cp_parser_flags flags)
22075 tree parameters;
22076 cp_token *token;
22077 bool ellipsis_p;
22079 temp_override<bool> cleanup
22080 (parser->auto_is_implicit_function_template_parm_p);
22082 if (!processing_specialization
22083 && !processing_template_parmlist
22084 && !processing_explicit_instantiation
22085 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22086 actual function or a random abstract declarator. */
22087 && parser->default_arg_ok_p)
22088 if (!current_function_decl
22089 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22090 parser->auto_is_implicit_function_template_parm_p = true;
22092 /* Peek at the next token. */
22093 token = cp_lexer_peek_token (parser->lexer);
22094 /* Check for trivial parameter-declaration-clauses. */
22095 if (token->type == CPP_ELLIPSIS)
22097 /* Consume the `...' token. */
22098 cp_lexer_consume_token (parser->lexer);
22099 return NULL_TREE;
22101 else if (token->type == CPP_CLOSE_PAREN)
22102 /* There are no parameters. */
22103 return void_list_node;
22104 /* Check for `(void)', too, which is a special case. */
22105 else if (token->keyword == RID_VOID
22106 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22107 == CPP_CLOSE_PAREN))
22109 /* Consume the `void' token. */
22110 cp_lexer_consume_token (parser->lexer);
22111 /* There are no parameters. */
22112 return void_list_node;
22115 /* Parse the parameter-declaration-list. */
22116 parameters = cp_parser_parameter_declaration_list (parser, flags);
22117 /* If a parse error occurred while parsing the
22118 parameter-declaration-list, then the entire
22119 parameter-declaration-clause is erroneous. */
22120 if (parameters == error_mark_node)
22121 return NULL_TREE;
22123 /* Peek at the next token. */
22124 token = cp_lexer_peek_token (parser->lexer);
22125 /* If it's a `,', the clause should terminate with an ellipsis. */
22126 if (token->type == CPP_COMMA)
22128 /* Consume the `,'. */
22129 cp_lexer_consume_token (parser->lexer);
22130 /* Expect an ellipsis. */
22131 ellipsis_p
22132 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22134 /* It might also be `...' if the optional trailing `,' was
22135 omitted. */
22136 else if (token->type == CPP_ELLIPSIS)
22138 /* Consume the `...' token. */
22139 cp_lexer_consume_token (parser->lexer);
22140 /* And remember that we saw it. */
22141 ellipsis_p = true;
22143 else
22144 ellipsis_p = false;
22146 /* Finish the parameter list. */
22147 if (!ellipsis_p)
22148 parameters = chainon (parameters, void_list_node);
22150 return parameters;
22153 /* Parse a parameter-declaration-list.
22155 parameter-declaration-list:
22156 parameter-declaration
22157 parameter-declaration-list , parameter-declaration
22159 The parser flags FLAGS is used to control type-specifier parsing.
22161 Returns a representation of the parameter-declaration-list, as for
22162 cp_parser_parameter_declaration_clause. However, the
22163 `void_list_node' is never appended to the list. */
22165 static tree
22166 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22168 tree parameters = NULL_TREE;
22169 tree *tail = &parameters;
22170 bool saved_in_unbraced_linkage_specification_p;
22171 int index = 0;
22173 /* The special considerations that apply to a function within an
22174 unbraced linkage specifications do not apply to the parameters
22175 to the function. */
22176 saved_in_unbraced_linkage_specification_p
22177 = parser->in_unbraced_linkage_specification_p;
22178 parser->in_unbraced_linkage_specification_p = false;
22180 /* Look for more parameters. */
22181 while (true)
22183 cp_parameter_declarator *parameter;
22184 tree decl = error_mark_node;
22185 bool parenthesized_p = false;
22187 /* Parse the parameter. */
22188 parameter
22189 = cp_parser_parameter_declaration (parser, flags,
22190 /*template_parm_p=*/false,
22191 &parenthesized_p);
22193 /* We don't know yet if the enclosing context is deprecated, so wait
22194 and warn in grokparms if appropriate. */
22195 deprecated_state = DEPRECATED_SUPPRESS;
22197 if (parameter)
22199 decl = grokdeclarator (parameter->declarator,
22200 &parameter->decl_specifiers,
22201 PARM,
22202 parameter->default_argument != NULL_TREE,
22203 &parameter->decl_specifiers.attributes);
22204 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22205 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22208 deprecated_state = DEPRECATED_NORMAL;
22210 /* If a parse error occurred parsing the parameter declaration,
22211 then the entire parameter-declaration-list is erroneous. */
22212 if (decl == error_mark_node)
22214 parameters = error_mark_node;
22215 break;
22218 if (parameter->decl_specifiers.attributes)
22219 cplus_decl_attributes (&decl,
22220 parameter->decl_specifiers.attributes,
22222 if (DECL_NAME (decl))
22223 decl = pushdecl (decl);
22225 if (decl != error_mark_node)
22227 retrofit_lang_decl (decl);
22228 DECL_PARM_INDEX (decl) = ++index;
22229 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22232 /* Add the new parameter to the list. */
22233 *tail = build_tree_list (parameter->default_argument, decl);
22234 tail = &TREE_CHAIN (*tail);
22236 /* Peek at the next token. */
22237 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22238 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22239 /* These are for Objective-C++ */
22240 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22241 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22242 /* The parameter-declaration-list is complete. */
22243 break;
22244 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22246 cp_token *token;
22248 /* Peek at the next token. */
22249 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22250 /* If it's an ellipsis, then the list is complete. */
22251 if (token->type == CPP_ELLIPSIS)
22252 break;
22253 /* Otherwise, there must be more parameters. Consume the
22254 `,'. */
22255 cp_lexer_consume_token (parser->lexer);
22256 /* When parsing something like:
22258 int i(float f, double d)
22260 we can tell after seeing the declaration for "f" that we
22261 are not looking at an initialization of a variable "i",
22262 but rather at the declaration of a function "i".
22264 Due to the fact that the parsing of template arguments
22265 (as specified to a template-id) requires backtracking we
22266 cannot use this technique when inside a template argument
22267 list. */
22268 if (!parser->in_template_argument_list_p
22269 && !parser->in_type_id_in_expr_p
22270 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22271 /* However, a parameter-declaration of the form
22272 "float(f)" (which is a valid declaration of a
22273 parameter "f") can also be interpreted as an
22274 expression (the conversion of "f" to "float"). */
22275 && !parenthesized_p)
22276 cp_parser_commit_to_tentative_parse (parser);
22278 else
22280 cp_parser_error (parser, "expected %<,%> or %<...%>");
22281 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22282 cp_parser_skip_to_closing_parenthesis (parser,
22283 /*recovering=*/true,
22284 /*or_comma=*/false,
22285 /*consume_paren=*/false);
22286 break;
22290 parser->in_unbraced_linkage_specification_p
22291 = saved_in_unbraced_linkage_specification_p;
22293 /* Reset implicit_template_scope if we are about to leave the function
22294 parameter list that introduced it. Note that for out-of-line member
22295 definitions, there will be one or more class scopes before we get to
22296 the template parameter scope. */
22298 if (cp_binding_level *its = parser->implicit_template_scope)
22299 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22301 while (maybe_its->kind == sk_class)
22302 maybe_its = maybe_its->level_chain;
22303 if (maybe_its == its)
22305 parser->implicit_template_parms = 0;
22306 parser->implicit_template_scope = 0;
22310 return parameters;
22313 /* Parse a parameter declaration.
22315 parameter-declaration:
22316 decl-specifier-seq ... [opt] declarator
22317 decl-specifier-seq declarator = assignment-expression
22318 decl-specifier-seq ... [opt] abstract-declarator [opt]
22319 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22321 The parser flags FLAGS is used to control type-specifier parsing.
22323 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22324 declares a template parameter. (In that case, a non-nested `>'
22325 token encountered during the parsing of the assignment-expression
22326 is not interpreted as a greater-than operator.)
22328 Returns a representation of the parameter, or NULL if an error
22329 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22330 true iff the declarator is of the form "(p)". */
22332 static cp_parameter_declarator *
22333 cp_parser_parameter_declaration (cp_parser *parser,
22334 cp_parser_flags flags,
22335 bool template_parm_p,
22336 bool *parenthesized_p)
22338 int declares_class_or_enum;
22339 cp_decl_specifier_seq decl_specifiers;
22340 cp_declarator *declarator;
22341 tree default_argument;
22342 cp_token *token = NULL, *declarator_token_start = NULL;
22343 const char *saved_message;
22344 bool template_parameter_pack_p = false;
22346 /* In a template parameter, `>' is not an operator.
22348 [temp.param]
22350 When parsing a default template-argument for a non-type
22351 template-parameter, the first non-nested `>' is taken as the end
22352 of the template parameter-list rather than a greater-than
22353 operator. */
22355 /* Type definitions may not appear in parameter types. */
22356 saved_message = parser->type_definition_forbidden_message;
22357 parser->type_definition_forbidden_message
22358 = G_("types may not be defined in parameter types");
22360 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22361 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22362 (current_template_parms)) : 0);
22364 /* Parse the declaration-specifiers. */
22365 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22366 cp_parser_decl_specifier_seq (parser,
22367 flags,
22368 &decl_specifiers,
22369 &declares_class_or_enum);
22371 /* Complain about missing 'typename' or other invalid type names. */
22372 if (!decl_specifiers.any_type_specifiers_p
22373 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22374 decl_specifiers.type = error_mark_node;
22376 /* If an error occurred, there's no reason to attempt to parse the
22377 rest of the declaration. */
22378 if (cp_parser_error_occurred (parser))
22380 parser->type_definition_forbidden_message = saved_message;
22381 return NULL;
22384 /* Peek at the next token. */
22385 token = cp_lexer_peek_token (parser->lexer);
22387 /* If the next token is a `)', `,', `=', `>', or `...', then there
22388 is no declarator. However, when variadic templates are enabled,
22389 there may be a declarator following `...'. */
22390 if (token->type == CPP_CLOSE_PAREN
22391 || token->type == CPP_COMMA
22392 || token->type == CPP_EQ
22393 || token->type == CPP_GREATER)
22395 declarator = NULL;
22396 if (parenthesized_p)
22397 *parenthesized_p = false;
22399 /* Otherwise, there should be a declarator. */
22400 else
22402 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22403 parser->default_arg_ok_p = false;
22405 /* After seeing a decl-specifier-seq, if the next token is not a
22406 "(", there is no possibility that the code is a valid
22407 expression. Therefore, if parsing tentatively, we commit at
22408 this point. */
22409 if (!parser->in_template_argument_list_p
22410 /* In an expression context, having seen:
22412 (int((char ...
22414 we cannot be sure whether we are looking at a
22415 function-type (taking a "char" as a parameter) or a cast
22416 of some object of type "char" to "int". */
22417 && !parser->in_type_id_in_expr_p
22418 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22419 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22420 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22421 cp_parser_commit_to_tentative_parse (parser);
22422 /* Parse the declarator. */
22423 declarator_token_start = token;
22424 declarator = cp_parser_declarator (parser,
22425 CP_PARSER_DECLARATOR_EITHER,
22426 CP_PARSER_FLAGS_NONE,
22427 /*ctor_dtor_or_conv_p=*/NULL,
22428 parenthesized_p,
22429 /*member_p=*/false,
22430 /*friend_p=*/false,
22431 /*static_p=*/false);
22432 parser->default_arg_ok_p = saved_default_arg_ok_p;
22433 /* After the declarator, allow more attributes. */
22434 decl_specifiers.attributes
22435 = attr_chainon (decl_specifiers.attributes,
22436 cp_parser_attributes_opt (parser));
22438 /* If the declarator is a template parameter pack, remember that and
22439 clear the flag in the declarator itself so we don't get errors
22440 from grokdeclarator. */
22441 if (template_parm_p && declarator && declarator->parameter_pack_p)
22443 declarator->parameter_pack_p = false;
22444 template_parameter_pack_p = true;
22448 /* If the next token is an ellipsis, and we have not seen a declarator
22449 name, and if either the type of the declarator contains parameter
22450 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22451 for, eg, abbreviated integral type names), then we actually have a
22452 parameter pack expansion expression. Otherwise, leave the ellipsis
22453 for a C-style variadic function. */
22454 token = cp_lexer_peek_token (parser->lexer);
22456 /* If a function parameter pack was specified and an implicit template
22457 parameter was introduced during cp_parser_parameter_declaration,
22458 change any implicit parameters introduced into packs. */
22459 if (parser->implicit_template_parms
22460 && ((token->type == CPP_ELLIPSIS
22461 && declarator_can_be_parameter_pack (declarator))
22462 || (declarator && declarator->parameter_pack_p)))
22464 int latest_template_parm_idx = TREE_VEC_LENGTH
22465 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22467 if (latest_template_parm_idx != template_parm_idx)
22468 decl_specifiers.type = convert_generic_types_to_packs
22469 (decl_specifiers.type,
22470 template_parm_idx, latest_template_parm_idx);
22473 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22475 tree type = decl_specifiers.type;
22477 if (type && DECL_P (type))
22478 type = TREE_TYPE (type);
22480 if (((type
22481 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22482 && (template_parm_p || uses_parameter_packs (type)))
22483 || (!type && template_parm_p))
22484 && declarator_can_be_parameter_pack (declarator))
22486 /* Consume the `...'. */
22487 cp_lexer_consume_token (parser->lexer);
22488 maybe_warn_variadic_templates ();
22490 /* Build a pack expansion type */
22491 if (template_parm_p)
22492 template_parameter_pack_p = true;
22493 else if (declarator)
22494 declarator->parameter_pack_p = true;
22495 else
22496 decl_specifiers.type = make_pack_expansion (type);
22500 /* The restriction on defining new types applies only to the type
22501 of the parameter, not to the default argument. */
22502 parser->type_definition_forbidden_message = saved_message;
22504 /* If the next token is `=', then process a default argument. */
22505 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22507 tree type = decl_specifiers.type;
22508 token = cp_lexer_peek_token (parser->lexer);
22509 /* If we are defining a class, then the tokens that make up the
22510 default argument must be saved and processed later. */
22511 if (!template_parm_p && at_class_scope_p ()
22512 && TYPE_BEING_DEFINED (current_class_type)
22513 && !LAMBDA_TYPE_P (current_class_type))
22514 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22516 // A constrained-type-specifier may declare a type template-parameter.
22517 else if (declares_constrained_type_template_parameter (type))
22518 default_argument
22519 = cp_parser_default_type_template_argument (parser);
22521 // A constrained-type-specifier may declare a template-template-parameter.
22522 else if (declares_constrained_template_template_parameter (type))
22523 default_argument
22524 = cp_parser_default_template_template_argument (parser);
22526 /* Outside of a class definition, we can just parse the
22527 assignment-expression. */
22528 else
22529 default_argument
22530 = cp_parser_default_argument (parser, template_parm_p);
22532 if (!parser->default_arg_ok_p)
22534 permerror (token->location,
22535 "default arguments are only "
22536 "permitted for function parameters");
22538 else if ((declarator && declarator->parameter_pack_p)
22539 || template_parameter_pack_p
22540 || (decl_specifiers.type
22541 && PACK_EXPANSION_P (decl_specifiers.type)))
22543 /* Find the name of the parameter pack. */
22544 cp_declarator *id_declarator = declarator;
22545 while (id_declarator && id_declarator->kind != cdk_id)
22546 id_declarator = id_declarator->declarator;
22548 if (id_declarator && id_declarator->kind == cdk_id)
22549 error_at (declarator_token_start->location,
22550 template_parm_p
22551 ? G_("template parameter pack %qD "
22552 "cannot have a default argument")
22553 : G_("parameter pack %qD cannot have "
22554 "a default argument"),
22555 id_declarator->u.id.unqualified_name);
22556 else
22557 error_at (declarator_token_start->location,
22558 template_parm_p
22559 ? G_("template parameter pack cannot have "
22560 "a default argument")
22561 : G_("parameter pack cannot have a "
22562 "default argument"));
22564 default_argument = NULL_TREE;
22567 else
22568 default_argument = NULL_TREE;
22570 if (default_argument)
22571 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22573 /* Generate a location for the parameter, ranging from the start of the
22574 initial token to the end of the final token (using input_location for
22575 the latter, set up by cp_lexer_set_source_position_from_token when
22576 consuming tokens).
22578 If we have a identifier, then use it for the caret location, e.g.
22580 extern int callee (int one, int (*two)(int, int), float three);
22581 ~~~~~~^~~~~~~~~~~~~~
22583 otherwise, reuse the start location for the caret location e.g.:
22585 extern int callee (int one, int (*)(int, int), float three);
22586 ^~~~~~~~~~~~~~~~~
22589 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22590 ? declarator->id_loc
22591 : decl_spec_token_start->location);
22592 location_t param_loc = make_location (caret_loc,
22593 decl_spec_token_start->location,
22594 input_location);
22596 return make_parameter_declarator (&decl_specifiers,
22597 declarator,
22598 default_argument,
22599 param_loc,
22600 template_parameter_pack_p);
22603 /* Parse a default argument and return it.
22605 TEMPLATE_PARM_P is true if this is a default argument for a
22606 non-type template parameter. */
22607 static tree
22608 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22610 tree default_argument = NULL_TREE;
22611 bool saved_greater_than_is_operator_p;
22612 unsigned char saved_local_variables_forbidden_p;
22613 bool non_constant_p, is_direct_init;
22615 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22616 set correctly. */
22617 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22618 parser->greater_than_is_operator_p = !template_parm_p;
22619 /* Local variable names (and the `this' keyword) may not
22620 appear in a default argument. */
22621 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22622 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22623 /* Parse the assignment-expression. */
22624 if (template_parm_p)
22625 push_deferring_access_checks (dk_no_deferred);
22626 tree saved_class_ptr = NULL_TREE;
22627 tree saved_class_ref = NULL_TREE;
22628 /* The "this" pointer is not valid in a default argument. */
22629 if (cfun)
22631 saved_class_ptr = current_class_ptr;
22632 cp_function_chain->x_current_class_ptr = NULL_TREE;
22633 saved_class_ref = current_class_ref;
22634 cp_function_chain->x_current_class_ref = NULL_TREE;
22636 default_argument
22637 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22638 /* Restore the "this" pointer. */
22639 if (cfun)
22641 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22642 cp_function_chain->x_current_class_ref = saved_class_ref;
22644 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22646 if (template_parm_p)
22647 pop_deferring_access_checks ();
22648 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22649 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22651 return default_argument;
22654 /* Parse a function-body.
22656 function-body:
22657 compound_statement */
22659 static void
22660 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22662 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22663 ? BCS_TRY_BLOCK : BCS_NORMAL),
22664 true);
22667 /* Parse a ctor-initializer-opt followed by a function-body. Return
22668 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22669 is true we are parsing a function-try-block. */
22671 static void
22672 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22673 bool in_function_try_block)
22675 tree body, list;
22676 const bool check_body_p
22677 = (DECL_CONSTRUCTOR_P (current_function_decl)
22678 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22679 tree last = NULL;
22681 if (in_function_try_block
22682 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22683 && cxx_dialect < cxx2a)
22685 if (DECL_CONSTRUCTOR_P (current_function_decl))
22686 pedwarn (input_location, 0,
22687 "function-try-block body of %<constexpr%> constructor only "
22688 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22689 else
22690 pedwarn (input_location, 0,
22691 "function-try-block body of %<constexpr%> function only "
22692 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22695 /* Begin the function body. */
22696 body = begin_function_body ();
22697 /* Parse the optional ctor-initializer. */
22698 cp_parser_ctor_initializer_opt (parser);
22700 /* If we're parsing a constexpr constructor definition, we need
22701 to check that the constructor body is indeed empty. However,
22702 before we get to cp_parser_function_body lot of junk has been
22703 generated, so we can't just check that we have an empty block.
22704 Rather we take a snapshot of the outermost block, and check whether
22705 cp_parser_function_body changed its state. */
22706 if (check_body_p)
22708 list = cur_stmt_list;
22709 if (STATEMENT_LIST_TAIL (list))
22710 last = STATEMENT_LIST_TAIL (list)->stmt;
22712 /* Parse the function-body. */
22713 cp_parser_function_body (parser, in_function_try_block);
22714 if (check_body_p)
22715 check_constexpr_ctor_body (last, list, /*complain=*/true);
22716 /* Finish the function body. */
22717 finish_function_body (body);
22720 /* Parse an initializer.
22722 initializer:
22723 = initializer-clause
22724 ( expression-list )
22726 Returns an expression representing the initializer. If no
22727 initializer is present, NULL_TREE is returned.
22729 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22730 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22731 set to TRUE if there is no initializer present. If there is an
22732 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22733 is set to true; otherwise it is set to false. */
22735 static tree
22736 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22737 bool* non_constant_p, bool subexpression_p)
22739 cp_token *token;
22740 tree init;
22742 /* Peek at the next token. */
22743 token = cp_lexer_peek_token (parser->lexer);
22745 /* Let our caller know whether or not this initializer was
22746 parenthesized. */
22747 *is_direct_init = (token->type != CPP_EQ);
22748 /* Assume that the initializer is constant. */
22749 *non_constant_p = false;
22751 if (token->type == CPP_EQ)
22753 /* Consume the `='. */
22754 cp_lexer_consume_token (parser->lexer);
22755 /* Parse the initializer-clause. */
22756 init = cp_parser_initializer_clause (parser, non_constant_p);
22758 else if (token->type == CPP_OPEN_PAREN)
22760 vec<tree, va_gc> *vec;
22761 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22762 /*cast_p=*/false,
22763 /*allow_expansion_p=*/true,
22764 non_constant_p);
22765 if (vec == NULL)
22766 return error_mark_node;
22767 init = build_tree_list_vec (vec);
22768 release_tree_vector (vec);
22770 else if (token->type == CPP_OPEN_BRACE)
22772 cp_lexer_set_source_position (parser->lexer);
22773 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22774 init = cp_parser_braced_list (parser, non_constant_p);
22775 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22777 else
22779 /* Anything else is an error. */
22780 cp_parser_error (parser, "expected initializer");
22781 init = error_mark_node;
22784 if (!subexpression_p && check_for_bare_parameter_packs (init))
22785 init = error_mark_node;
22787 return init;
22790 /* Parse an initializer-clause.
22792 initializer-clause:
22793 assignment-expression
22794 braced-init-list
22796 Returns an expression representing the initializer.
22798 If the `assignment-expression' production is used the value
22799 returned is simply a representation for the expression.
22801 Otherwise, calls cp_parser_braced_list. */
22803 static cp_expr
22804 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22806 cp_expr initializer;
22808 /* Assume the expression is constant. */
22809 *non_constant_p = false;
22811 /* If it is not a `{', then we are looking at an
22812 assignment-expression. */
22813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22815 initializer
22816 = cp_parser_constant_expression (parser,
22817 /*allow_non_constant_p=*/true,
22818 non_constant_p);
22820 else
22821 initializer = cp_parser_braced_list (parser, non_constant_p);
22823 return initializer;
22826 /* Parse a brace-enclosed initializer list.
22828 braced-init-list:
22829 { initializer-list , [opt] }
22830 { designated-initializer-list , [opt] }
22833 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22834 the elements of the initializer-list (or NULL, if the last
22835 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22836 NULL_TREE. There is no way to detect whether or not the optional
22837 trailing `,' was provided. NON_CONSTANT_P is as for
22838 cp_parser_initializer. */
22840 static cp_expr
22841 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22843 tree initializer;
22844 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22846 /* Consume the `{' token. */
22847 matching_braces braces;
22848 braces.require_open (parser);
22849 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22850 initializer = make_node (CONSTRUCTOR);
22851 /* If it's not a `}', then there is a non-trivial initializer. */
22852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22854 bool designated;
22855 /* Parse the initializer list. */
22856 CONSTRUCTOR_ELTS (initializer)
22857 = cp_parser_initializer_list (parser, non_constant_p, &designated);
22858 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
22859 /* A trailing `,' token is allowed. */
22860 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22861 cp_lexer_consume_token (parser->lexer);
22863 else
22864 *non_constant_p = false;
22865 /* Now, there should be a trailing `}'. */
22866 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22867 braces.require_close (parser);
22868 TREE_TYPE (initializer) = init_list_type_node;
22870 cp_expr result (initializer);
22871 /* Build a location of the form:
22872 { ... }
22873 ^~~~~~~
22874 with caret==start at the open brace, finish at the close brace. */
22875 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22876 result.set_location (combined_loc);
22877 return result;
22880 /* Consume tokens up to, and including, the next non-nested closing `]'.
22881 Returns true iff we found a closing `]'. */
22883 static bool
22884 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22886 unsigned square_depth = 0;
22888 while (true)
22890 cp_token * token = cp_lexer_peek_token (parser->lexer);
22892 switch (token->type)
22894 case CPP_PRAGMA_EOL:
22895 if (!parser->lexer->in_pragma)
22896 break;
22897 /* FALLTHRU */
22898 case CPP_EOF:
22899 /* If we've run out of tokens, then there is no closing `]'. */
22900 return false;
22902 case CPP_OPEN_SQUARE:
22903 ++square_depth;
22904 break;
22906 case CPP_CLOSE_SQUARE:
22907 if (!square_depth--)
22909 cp_lexer_consume_token (parser->lexer);
22910 return true;
22912 break;
22914 default:
22915 break;
22918 /* Consume the token. */
22919 cp_lexer_consume_token (parser->lexer);
22923 /* Return true if we are looking at an array-designator, false otherwise. */
22925 static bool
22926 cp_parser_array_designator_p (cp_parser *parser)
22928 /* Consume the `['. */
22929 cp_lexer_consume_token (parser->lexer);
22931 cp_lexer_save_tokens (parser->lexer);
22933 /* Skip tokens until the next token is a closing square bracket.
22934 If we find the closing `]', and the next token is a `=', then
22935 we are looking at an array designator. */
22936 bool array_designator_p
22937 = (cp_parser_skip_to_closing_square_bracket (parser)
22938 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22940 /* Roll back the tokens we skipped. */
22941 cp_lexer_rollback_tokens (parser->lexer);
22943 return array_designator_p;
22946 /* Parse an initializer-list.
22948 initializer-list:
22949 initializer-clause ... [opt]
22950 initializer-list , initializer-clause ... [opt]
22952 C++2A Extension:
22954 designated-initializer-list:
22955 designated-initializer-clause
22956 designated-initializer-list , designated-initializer-clause
22958 designated-initializer-clause:
22959 designator brace-or-equal-initializer
22961 designator:
22962 . identifier
22964 GNU Extension:
22966 initializer-list:
22967 designation initializer-clause ...[opt]
22968 initializer-list , designation initializer-clause ...[opt]
22970 designation:
22971 . identifier =
22972 identifier :
22973 [ constant-expression ] =
22975 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22976 for the initializer. If the INDEX of the elt is non-NULL, it is the
22977 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22978 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22979 are any designators. */
22981 static vec<constructor_elt, va_gc> *
22982 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
22983 bool *designated)
22985 vec<constructor_elt, va_gc> *v = NULL;
22986 bool first_p = true;
22987 tree first_designator = NULL_TREE;
22989 /* Assume all of the expressions are constant. */
22990 *non_constant_p = false;
22992 /* Parse the rest of the list. */
22993 while (true)
22995 cp_token *token;
22996 tree designator;
22997 tree initializer;
22998 bool clause_non_constant_p;
22999 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23001 /* Handle the C++2A syntax, '. id ='. */
23002 if ((cxx_dialect >= cxx2a
23003 || cp_parser_allow_gnu_extensions_p (parser))
23004 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23005 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23006 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23007 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23008 == CPP_OPEN_BRACE)))
23010 if (cxx_dialect < cxx2a)
23011 pedwarn (loc, OPT_Wpedantic,
23012 "C++ designated initializers only available with "
23013 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23014 /* Consume the `.'. */
23015 cp_lexer_consume_token (parser->lexer);
23016 /* Consume the identifier. */
23017 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23018 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23019 /* Consume the `='. */
23020 cp_lexer_consume_token (parser->lexer);
23022 /* Also, if the next token is an identifier and the following one is a
23023 colon, we are looking at the GNU designated-initializer
23024 syntax. */
23025 else if (cp_parser_allow_gnu_extensions_p (parser)
23026 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23027 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23028 == CPP_COLON))
23030 /* Warn the user that they are using an extension. */
23031 pedwarn (loc, OPT_Wpedantic,
23032 "ISO C++ does not allow GNU designated initializers");
23033 /* Consume the identifier. */
23034 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23035 /* Consume the `:'. */
23036 cp_lexer_consume_token (parser->lexer);
23038 /* Also handle C99 array designators, '[ const ] ='. */
23039 else if (cp_parser_allow_gnu_extensions_p (parser)
23040 && !c_dialect_objc ()
23041 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23043 /* In C++11, [ could start a lambda-introducer. */
23044 bool non_const = false;
23046 cp_parser_parse_tentatively (parser);
23048 if (!cp_parser_array_designator_p (parser))
23050 cp_parser_simulate_error (parser);
23051 designator = NULL_TREE;
23053 else
23055 designator = cp_parser_constant_expression (parser, true,
23056 &non_const);
23057 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23058 cp_parser_require (parser, CPP_EQ, RT_EQ);
23061 if (!cp_parser_parse_definitely (parser))
23062 designator = NULL_TREE;
23063 else if (non_const
23064 && (!require_potential_rvalue_constant_expression
23065 (designator)))
23066 designator = NULL_TREE;
23067 if (designator)
23068 /* Warn the user that they are using an extension. */
23069 pedwarn (loc, OPT_Wpedantic,
23070 "ISO C++ does not allow C99 designated initializers");
23072 else
23073 designator = NULL_TREE;
23075 if (first_p)
23077 first_designator = designator;
23078 first_p = false;
23080 else if (cxx_dialect >= cxx2a
23081 && first_designator != error_mark_node
23082 && (!first_designator != !designator))
23084 error_at (loc, "either all initializer clauses should be designated "
23085 "or none of them should be");
23086 first_designator = error_mark_node;
23088 else if (cxx_dialect < cxx2a && !first_designator)
23089 first_designator = designator;
23091 /* Parse the initializer. */
23092 initializer = cp_parser_initializer_clause (parser,
23093 &clause_non_constant_p);
23094 /* If any clause is non-constant, so is the entire initializer. */
23095 if (clause_non_constant_p)
23096 *non_constant_p = true;
23098 /* If we have an ellipsis, this is an initializer pack
23099 expansion. */
23100 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23102 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23104 /* Consume the `...'. */
23105 cp_lexer_consume_token (parser->lexer);
23107 if (designator && cxx_dialect >= cxx2a)
23108 error_at (loc,
23109 "%<...%> not allowed in designated initializer list");
23111 /* Turn the initializer into an initializer expansion. */
23112 initializer = make_pack_expansion (initializer);
23115 /* Add it to the vector. */
23116 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23118 /* If the next token is not a comma, we have reached the end of
23119 the list. */
23120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23121 break;
23123 /* Peek at the next token. */
23124 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23125 /* If the next token is a `}', then we're still done. An
23126 initializer-clause can have a trailing `,' after the
23127 initializer-list and before the closing `}'. */
23128 if (token->type == CPP_CLOSE_BRACE)
23129 break;
23131 /* Consume the `,' token. */
23132 cp_lexer_consume_token (parser->lexer);
23135 /* The same identifier shall not appear in multiple designators
23136 of a designated-initializer-list. */
23137 if (first_designator)
23139 unsigned int i;
23140 tree designator, val;
23141 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23142 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23144 if (IDENTIFIER_MARKED (designator))
23146 error_at (cp_expr_loc_or_input_loc (val),
23147 "%<.%s%> designator used multiple times in "
23148 "the same initializer list",
23149 IDENTIFIER_POINTER (designator));
23150 (*v)[i].index = error_mark_node;
23152 else
23153 IDENTIFIER_MARKED (designator) = 1;
23155 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23156 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23157 IDENTIFIER_MARKED (designator) = 0;
23160 *designated = first_designator != NULL_TREE;
23161 return v;
23164 /* Classes [gram.class] */
23166 /* Parse a class-name.
23168 class-name:
23169 identifier
23170 template-id
23172 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23173 to indicate that names looked up in dependent types should be
23174 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23175 keyword has been used to indicate that the name that appears next
23176 is a template. TAG_TYPE indicates the explicit tag given before
23177 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23178 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23179 is the class being defined in a class-head. If ENUM_OK is TRUE,
23180 enum-names are also accepted.
23182 Returns the TYPE_DECL representing the class. */
23184 static tree
23185 cp_parser_class_name (cp_parser *parser,
23186 bool typename_keyword_p,
23187 bool template_keyword_p,
23188 enum tag_types tag_type,
23189 bool check_dependency_p,
23190 bool class_head_p,
23191 bool is_declaration,
23192 bool enum_ok)
23194 tree decl;
23195 tree scope;
23196 bool typename_p;
23197 cp_token *token;
23198 tree identifier = NULL_TREE;
23200 /* All class-names start with an identifier. */
23201 token = cp_lexer_peek_token (parser->lexer);
23202 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23204 cp_parser_error (parser, "expected class-name");
23205 return error_mark_node;
23208 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23209 to a template-id, so we save it here. */
23210 scope = parser->scope;
23211 if (scope == error_mark_node)
23212 return error_mark_node;
23214 /* Any name names a type if we're following the `typename' keyword
23215 in a qualified name where the enclosing scope is type-dependent. */
23216 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23217 && dependent_type_p (scope));
23218 /* Handle the common case (an identifier, but not a template-id)
23219 efficiently. */
23220 if (token->type == CPP_NAME
23221 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23223 cp_token *identifier_token;
23224 bool ambiguous_p;
23226 /* Look for the identifier. */
23227 identifier_token = cp_lexer_peek_token (parser->lexer);
23228 ambiguous_p = identifier_token->error_reported;
23229 identifier = cp_parser_identifier (parser);
23230 /* If the next token isn't an identifier, we are certainly not
23231 looking at a class-name. */
23232 if (identifier == error_mark_node)
23233 decl = error_mark_node;
23234 /* If we know this is a type-name, there's no need to look it
23235 up. */
23236 else if (typename_p)
23237 decl = identifier;
23238 else
23240 tree ambiguous_decls;
23241 /* If we already know that this lookup is ambiguous, then
23242 we've already issued an error message; there's no reason
23243 to check again. */
23244 if (ambiguous_p)
23246 cp_parser_simulate_error (parser);
23247 return error_mark_node;
23249 /* If the next token is a `::', then the name must be a type
23250 name.
23252 [basic.lookup.qual]
23254 During the lookup for a name preceding the :: scope
23255 resolution operator, object, function, and enumerator
23256 names are ignored. */
23257 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23258 tag_type = scope_type;
23259 /* Look up the name. */
23260 decl = cp_parser_lookup_name (parser, identifier,
23261 tag_type,
23262 /*is_template=*/false,
23263 /*is_namespace=*/false,
23264 check_dependency_p,
23265 &ambiguous_decls,
23266 identifier_token->location);
23267 if (ambiguous_decls)
23269 if (cp_parser_parsing_tentatively (parser))
23270 cp_parser_simulate_error (parser);
23271 return error_mark_node;
23275 else
23277 /* Try a template-id. */
23278 decl = cp_parser_template_id (parser, template_keyword_p,
23279 check_dependency_p,
23280 tag_type,
23281 is_declaration);
23282 if (decl == error_mark_node)
23283 return error_mark_node;
23286 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23288 /* If this is a typename, create a TYPENAME_TYPE. */
23289 if (typename_p
23290 && decl != error_mark_node
23291 && !is_overloaded_fn (decl))
23293 decl = make_typename_type (scope, decl, typename_type,
23294 /*complain=*/tf_error);
23295 if (decl != error_mark_node)
23296 decl = TYPE_NAME (decl);
23299 decl = strip_using_decl (decl);
23301 /* Check to see that it is really the name of a class. */
23302 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23303 && identifier_p (TREE_OPERAND (decl, 0))
23304 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23305 /* Situations like this:
23307 template <typename T> struct A {
23308 typename T::template X<int>::I i;
23311 are problematic. Is `T::template X<int>' a class-name? The
23312 standard does not seem to be definitive, but there is no other
23313 valid interpretation of the following `::'. Therefore, those
23314 names are considered class-names. */
23316 decl = make_typename_type (scope, decl, tag_type, tf_error);
23317 if (decl != error_mark_node)
23318 decl = TYPE_NAME (decl);
23320 else if (TREE_CODE (decl) != TYPE_DECL
23321 || TREE_TYPE (decl) == error_mark_node
23322 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23323 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23324 /* In Objective-C 2.0, a classname followed by '.' starts a
23325 dot-syntax expression, and it's not a type-name. */
23326 || (c_dialect_objc ()
23327 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23328 && objc_is_class_name (decl)))
23329 decl = error_mark_node;
23331 if (decl == error_mark_node)
23332 cp_parser_error (parser, "expected class-name");
23333 else if (identifier && !parser->scope)
23334 maybe_note_name_used_in_class (identifier, decl);
23336 return decl;
23339 /* Make sure that any member-function parameters are in scope.
23340 For instance, a function's noexcept-specifier can use the function's
23341 parameters:
23343 struct S {
23344 void fn (int p) noexcept(noexcept(p));
23347 so we need to make sure name lookup can find them. This is used
23348 when we delay parsing of the noexcept-specifier. */
23350 static void
23351 inject_parm_decls (tree decl)
23353 begin_scope (sk_function_parms, decl);
23354 tree args = DECL_ARGUMENTS (decl);
23356 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
23359 /* Undo the effects of inject_parm_decls. */
23361 static void
23362 pop_injected_parms (void)
23364 pop_bindings_and_leave_scope ();
23367 /* Parse a class-specifier.
23369 class-specifier:
23370 class-head { member-specification [opt] }
23372 Returns the TREE_TYPE representing the class. */
23374 static tree
23375 cp_parser_class_specifier_1 (cp_parser* parser)
23377 tree type;
23378 tree attributes = NULL_TREE;
23379 bool nested_name_specifier_p;
23380 unsigned saved_num_template_parameter_lists;
23381 bool saved_in_function_body;
23382 unsigned char in_statement;
23383 bool in_switch_statement_p;
23384 bool saved_in_unbraced_linkage_specification_p;
23385 tree old_scope = NULL_TREE;
23386 tree scope = NULL_TREE;
23387 cp_token *closing_brace;
23389 push_deferring_access_checks (dk_no_deferred);
23391 /* Parse the class-head. */
23392 type = cp_parser_class_head (parser,
23393 &nested_name_specifier_p);
23394 /* If the class-head was a semantic disaster, skip the entire body
23395 of the class. */
23396 if (!type)
23398 cp_parser_skip_to_end_of_block_or_statement (parser);
23399 pop_deferring_access_checks ();
23400 return error_mark_node;
23403 /* Look for the `{'. */
23404 matching_braces braces;
23405 if (!braces.require_open (parser))
23407 pop_deferring_access_checks ();
23408 return error_mark_node;
23411 cp_ensure_no_omp_declare_simd (parser);
23412 cp_ensure_no_oacc_routine (parser);
23414 /* Issue an error message if type-definitions are forbidden here. */
23415 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23416 /* Remember that we are defining one more class. */
23417 ++parser->num_classes_being_defined;
23418 /* Inside the class, surrounding template-parameter-lists do not
23419 apply. */
23420 saved_num_template_parameter_lists
23421 = parser->num_template_parameter_lists;
23422 parser->num_template_parameter_lists = 0;
23423 /* We are not in a function body. */
23424 saved_in_function_body = parser->in_function_body;
23425 parser->in_function_body = false;
23426 /* Or in a loop. */
23427 in_statement = parser->in_statement;
23428 parser->in_statement = 0;
23429 /* Or in a switch. */
23430 in_switch_statement_p = parser->in_switch_statement_p;
23431 parser->in_switch_statement_p = false;
23432 /* We are not immediately inside an extern "lang" block. */
23433 saved_in_unbraced_linkage_specification_p
23434 = parser->in_unbraced_linkage_specification_p;
23435 parser->in_unbraced_linkage_specification_p = false;
23437 // Associate constraints with the type.
23438 if (flag_concepts)
23439 type = associate_classtype_constraints (type);
23441 /* Start the class. */
23442 if (nested_name_specifier_p)
23444 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23445 old_scope = push_inner_scope (scope);
23447 type = begin_class_definition (type);
23449 if (type == error_mark_node)
23450 /* If the type is erroneous, skip the entire body of the class. */
23451 cp_parser_skip_to_closing_brace (parser);
23452 else
23453 /* Parse the member-specification. */
23454 cp_parser_member_specification_opt (parser);
23456 /* Look for the trailing `}'. */
23457 closing_brace = braces.require_close (parser);
23458 /* Look for trailing attributes to apply to this class. */
23459 if (cp_parser_allow_gnu_extensions_p (parser))
23460 attributes = cp_parser_gnu_attributes_opt (parser);
23461 if (type != error_mark_node)
23462 type = finish_struct (type, attributes);
23463 if (nested_name_specifier_p)
23464 pop_inner_scope (old_scope, scope);
23466 /* We've finished a type definition. Check for the common syntax
23467 error of forgetting a semicolon after the definition. We need to
23468 be careful, as we can't just check for not-a-semicolon and be done
23469 with it; the user might have typed:
23471 class X { } c = ...;
23472 class X { } *p = ...;
23474 and so forth. Instead, enumerate all the possible tokens that
23475 might follow this production; if we don't see one of them, then
23476 complain and silently insert the semicolon. */
23478 cp_token *token = cp_lexer_peek_token (parser->lexer);
23479 bool want_semicolon = true;
23481 if (cp_next_tokens_can_be_std_attribute_p (parser))
23482 /* Don't try to parse c++11 attributes here. As per the
23483 grammar, that should be a task for
23484 cp_parser_decl_specifier_seq. */
23485 want_semicolon = false;
23487 switch (token->type)
23489 case CPP_NAME:
23490 case CPP_SEMICOLON:
23491 case CPP_MULT:
23492 case CPP_AND:
23493 case CPP_OPEN_PAREN:
23494 case CPP_CLOSE_PAREN:
23495 case CPP_COMMA:
23496 want_semicolon = false;
23497 break;
23499 /* While it's legal for type qualifiers and storage class
23500 specifiers to follow type definitions in the grammar, only
23501 compiler testsuites contain code like that. Assume that if
23502 we see such code, then what we're really seeing is a case
23503 like:
23505 class X { }
23506 const <type> var = ...;
23510 class Y { }
23511 static <type> func (...) ...
23513 i.e. the qualifier or specifier applies to the next
23514 declaration. To do so, however, we need to look ahead one
23515 more token to see if *that* token is a type specifier.
23517 This code could be improved to handle:
23519 class Z { }
23520 static const <type> var = ...; */
23521 case CPP_KEYWORD:
23522 if (keyword_is_decl_specifier (token->keyword))
23524 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23526 /* Handling user-defined types here would be nice, but very
23527 tricky. */
23528 want_semicolon
23529 = (lookahead->type == CPP_KEYWORD
23530 && keyword_begins_type_specifier (lookahead->keyword));
23532 break;
23533 default:
23534 break;
23537 /* If we don't have a type, then something is very wrong and we
23538 shouldn't try to do anything clever. Likewise for not seeing the
23539 closing brace. */
23540 if (closing_brace && TYPE_P (type) && want_semicolon)
23542 /* Locate the closing brace. */
23543 cp_token_position prev
23544 = cp_lexer_previous_token_position (parser->lexer);
23545 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23546 location_t loc = prev_token->location;
23548 /* We want to suggest insertion of a ';' immediately *after* the
23549 closing brace, so, if we can, offset the location by 1 column. */
23550 location_t next_loc = loc;
23551 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23552 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23554 rich_location richloc (line_table, next_loc);
23556 /* If we successfully offset the location, suggest the fix-it. */
23557 if (next_loc != loc)
23558 richloc.add_fixit_insert_before (next_loc, ";");
23560 if (CLASSTYPE_DECLARED_CLASS (type))
23561 error_at (&richloc,
23562 "expected %<;%> after class definition");
23563 else if (TREE_CODE (type) == RECORD_TYPE)
23564 error_at (&richloc,
23565 "expected %<;%> after struct definition");
23566 else if (TREE_CODE (type) == UNION_TYPE)
23567 error_at (&richloc,
23568 "expected %<;%> after union definition");
23569 else
23570 gcc_unreachable ();
23572 /* Unget one token and smash it to look as though we encountered
23573 a semicolon in the input stream. */
23574 cp_lexer_set_token_position (parser->lexer, prev);
23575 token = cp_lexer_peek_token (parser->lexer);
23576 token->type = CPP_SEMICOLON;
23577 token->keyword = RID_MAX;
23581 /* If this class is not itself within the scope of another class,
23582 then we need to parse the bodies of all of the queued function
23583 definitions. Note that the queued functions defined in a class
23584 are not always processed immediately following the
23585 class-specifier for that class. Consider:
23587 struct A {
23588 struct B { void f() { sizeof (A); } };
23591 If `f' were processed before the processing of `A' were
23592 completed, there would be no way to compute the size of `A'.
23593 Note that the nesting we are interested in here is lexical --
23594 not the semantic nesting given by TYPE_CONTEXT. In particular,
23595 for:
23597 struct A { struct B; };
23598 struct A::B { void f() { } };
23600 there is no need to delay the parsing of `A::B::f'. */
23601 if (--parser->num_classes_being_defined == 0)
23603 tree decl;
23604 tree class_type = NULL_TREE;
23605 tree pushed_scope = NULL_TREE;
23606 unsigned ix;
23607 cp_default_arg_entry *e;
23608 tree save_ccp, save_ccr;
23610 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23612 /* Skip default arguments, NSDMIs, etc, in order to improve
23613 error recovery (c++/71169, c++/71832). */
23614 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23615 vec_safe_truncate (unparsed_nsdmis, 0);
23616 vec_safe_truncate (unparsed_classes, 0);
23617 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23620 /* In a first pass, parse default arguments to the functions.
23621 Then, in a second pass, parse the bodies of the functions.
23622 This two-phased approach handles cases like:
23624 struct S {
23625 void f() { g(); }
23626 void g(int i = 3);
23630 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23632 decl = e->decl;
23633 /* If there are default arguments that have not yet been processed,
23634 take care of them now. */
23635 if (class_type != e->class_type)
23637 if (pushed_scope)
23638 pop_scope (pushed_scope);
23639 class_type = e->class_type;
23640 pushed_scope = push_scope (class_type);
23642 /* Make sure that any template parameters are in scope. */
23643 maybe_begin_member_template_processing (decl);
23644 /* Parse the default argument expressions. */
23645 cp_parser_late_parsing_default_args (parser, decl);
23646 /* Remove any template parameters from the symbol table. */
23647 maybe_end_member_template_processing ();
23649 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23650 /* Now parse any NSDMIs. */
23651 save_ccp = current_class_ptr;
23652 save_ccr = current_class_ref;
23653 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23655 if (class_type != DECL_CONTEXT (decl))
23657 if (pushed_scope)
23658 pop_scope (pushed_scope);
23659 class_type = DECL_CONTEXT (decl);
23660 pushed_scope = push_scope (class_type);
23662 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23663 cp_parser_late_parsing_nsdmi (parser, decl);
23665 vec_safe_truncate (unparsed_nsdmis, 0);
23666 current_class_ptr = save_ccp;
23667 current_class_ref = save_ccr;
23668 if (pushed_scope)
23669 pop_scope (pushed_scope);
23671 /* Now do some post-NSDMI bookkeeping. */
23672 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23673 after_nsdmi_defaulted_late_checks (class_type);
23674 vec_safe_truncate (unparsed_classes, 0);
23675 after_nsdmi_defaulted_late_checks (type);
23677 /* If there are noexcept-specifiers that have not yet been processed,
23678 take care of them now. */
23679 class_type = NULL_TREE;
23680 pushed_scope = NULL_TREE;
23681 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
23683 tree ctx = DECL_CONTEXT (decl);
23684 if (class_type != ctx)
23686 if (pushed_scope)
23687 pop_scope (pushed_scope);
23688 class_type = ctx;
23689 pushed_scope = push_scope (class_type);
23692 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
23693 spec = TREE_PURPOSE (spec);
23695 /* Make sure that any template parameters are in scope. */
23696 maybe_begin_member_template_processing (decl);
23698 /* Make sure that any member-function parameters are in scope. */
23699 inject_parm_decls (decl);
23701 /* 'this' is not allowed in static member functions. */
23702 unsigned char local_variables_forbidden_p
23703 = parser->local_variables_forbidden_p;
23704 if (DECL_THIS_STATIC (decl))
23705 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23707 /* Now we can parse the noexcept-specifier. */
23708 spec = cp_parser_late_noexcept_specifier (parser, spec);
23710 if (spec != error_mark_node)
23711 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
23713 /* Restore the state of local_variables_forbidden_p. */
23714 parser->local_variables_forbidden_p = local_variables_forbidden_p;
23716 /* The finish_struct call above performed various override checking,
23717 but it skipped unparsed noexcept-specifier operands. Now that we
23718 have resolved them, check again. */
23719 noexcept_override_late_checks (type, decl);
23721 /* Remove any member-function parameters from the symbol table. */
23722 pop_injected_parms ();
23724 /* Remove any template parameters from the symbol table. */
23725 maybe_end_member_template_processing ();
23727 vec_safe_truncate (unparsed_noexcepts, 0);
23728 if (pushed_scope)
23729 pop_scope (pushed_scope);
23731 /* Now parse the body of the functions. */
23732 if (flag_openmp)
23734 /* OpenMP UDRs need to be parsed before all other functions. */
23735 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23736 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23737 cp_parser_late_parsing_for_member (parser, decl);
23738 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23739 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23740 cp_parser_late_parsing_for_member (parser, decl);
23742 else
23743 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23744 cp_parser_late_parsing_for_member (parser, decl);
23745 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23747 else
23748 vec_safe_push (unparsed_classes, type);
23750 /* Put back any saved access checks. */
23751 pop_deferring_access_checks ();
23753 /* Restore saved state. */
23754 parser->in_switch_statement_p = in_switch_statement_p;
23755 parser->in_statement = in_statement;
23756 parser->in_function_body = saved_in_function_body;
23757 parser->num_template_parameter_lists
23758 = saved_num_template_parameter_lists;
23759 parser->in_unbraced_linkage_specification_p
23760 = saved_in_unbraced_linkage_specification_p;
23762 return type;
23765 static tree
23766 cp_parser_class_specifier (cp_parser* parser)
23768 tree ret;
23769 timevar_push (TV_PARSE_STRUCT);
23770 ret = cp_parser_class_specifier_1 (parser);
23771 timevar_pop (TV_PARSE_STRUCT);
23772 return ret;
23775 /* Parse a class-head.
23777 class-head:
23778 class-key identifier [opt] base-clause [opt]
23779 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23780 class-key nested-name-specifier [opt] template-id
23781 base-clause [opt]
23783 class-virt-specifier:
23784 final
23786 GNU Extensions:
23787 class-key attributes identifier [opt] base-clause [opt]
23788 class-key attributes nested-name-specifier identifier base-clause [opt]
23789 class-key attributes nested-name-specifier [opt] template-id
23790 base-clause [opt]
23792 Upon return BASES is initialized to the list of base classes (or
23793 NULL, if there are none) in the same form returned by
23794 cp_parser_base_clause.
23796 Returns the TYPE of the indicated class. Sets
23797 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23798 involving a nested-name-specifier was used, and FALSE otherwise.
23800 Returns error_mark_node if this is not a class-head.
23802 Returns NULL_TREE if the class-head is syntactically valid, but
23803 semantically invalid in a way that means we should skip the entire
23804 body of the class. */
23806 static tree
23807 cp_parser_class_head (cp_parser* parser,
23808 bool* nested_name_specifier_p)
23810 tree nested_name_specifier;
23811 enum tag_types class_key;
23812 tree id = NULL_TREE;
23813 tree type = NULL_TREE;
23814 tree attributes;
23815 tree bases;
23816 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23817 bool template_id_p = false;
23818 bool qualified_p = false;
23819 bool invalid_nested_name_p = false;
23820 bool invalid_explicit_specialization_p = false;
23821 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23822 tree pushed_scope = NULL_TREE;
23823 unsigned num_templates;
23824 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23825 /* Assume no nested-name-specifier will be present. */
23826 *nested_name_specifier_p = false;
23827 /* Assume no template parameter lists will be used in defining the
23828 type. */
23829 num_templates = 0;
23830 parser->colon_corrects_to_scope_p = false;
23832 /* Look for the class-key. */
23833 class_key = cp_parser_class_key (parser);
23834 if (class_key == none_type)
23835 return error_mark_node;
23837 location_t class_head_start_location = input_location;
23839 /* Parse the attributes. */
23840 attributes = cp_parser_attributes_opt (parser);
23842 /* If the next token is `::', that is invalid -- but sometimes
23843 people do try to write:
23845 struct ::S {};
23847 Handle this gracefully by accepting the extra qualifier, and then
23848 issuing an error about it later if this really is a
23849 class-head. If it turns out just to be an elaborated type
23850 specifier, remain silent. */
23851 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23852 qualified_p = true;
23854 push_deferring_access_checks (dk_no_check);
23856 /* Determine the name of the class. Begin by looking for an
23857 optional nested-name-specifier. */
23858 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23859 nested_name_specifier
23860 = cp_parser_nested_name_specifier_opt (parser,
23861 /*typename_keyword_p=*/false,
23862 /*check_dependency_p=*/false,
23863 /*type_p=*/true,
23864 /*is_declaration=*/false);
23865 /* If there was a nested-name-specifier, then there *must* be an
23866 identifier. */
23868 cp_token *bad_template_keyword = NULL;
23870 if (nested_name_specifier)
23872 type_start_token = cp_lexer_peek_token (parser->lexer);
23873 /* Although the grammar says `identifier', it really means
23874 `class-name' or `template-name'. You are only allowed to
23875 define a class that has already been declared with this
23876 syntax.
23878 The proposed resolution for Core Issue 180 says that wherever
23879 you see `class T::X' you should treat `X' as a type-name.
23881 It is OK to define an inaccessible class; for example:
23883 class A { class B; };
23884 class A::B {};
23886 We do not know if we will see a class-name, or a
23887 template-name. We look for a class-name first, in case the
23888 class-name is a template-id; if we looked for the
23889 template-name first we would stop after the template-name. */
23890 cp_parser_parse_tentatively (parser);
23891 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23892 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23893 type = cp_parser_class_name (parser,
23894 /*typename_keyword_p=*/false,
23895 /*template_keyword_p=*/false,
23896 class_type,
23897 /*check_dependency_p=*/false,
23898 /*class_head_p=*/true,
23899 /*is_declaration=*/false);
23900 /* If that didn't work, ignore the nested-name-specifier. */
23901 if (!cp_parser_parse_definitely (parser))
23903 invalid_nested_name_p = true;
23904 type_start_token = cp_lexer_peek_token (parser->lexer);
23905 id = cp_parser_identifier (parser);
23906 if (id == error_mark_node)
23907 id = NULL_TREE;
23909 /* If we could not find a corresponding TYPE, treat this
23910 declaration like an unqualified declaration. */
23911 if (type == error_mark_node)
23912 nested_name_specifier = NULL_TREE;
23913 /* Otherwise, count the number of templates used in TYPE and its
23914 containing scopes. */
23915 else
23916 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23918 /* Otherwise, the identifier is optional. */
23919 else
23921 /* We don't know whether what comes next is a template-id,
23922 an identifier, or nothing at all. */
23923 cp_parser_parse_tentatively (parser);
23924 /* Check for a template-id. */
23925 type_start_token = cp_lexer_peek_token (parser->lexer);
23926 id = cp_parser_template_id (parser,
23927 /*template_keyword_p=*/false,
23928 /*check_dependency_p=*/true,
23929 class_key,
23930 /*is_declaration=*/true);
23931 /* If that didn't work, it could still be an identifier. */
23932 if (!cp_parser_parse_definitely (parser))
23934 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23936 type_start_token = cp_lexer_peek_token (parser->lexer);
23937 id = cp_parser_identifier (parser);
23939 else
23940 id = NULL_TREE;
23942 else
23944 template_id_p = true;
23945 ++num_templates;
23949 pop_deferring_access_checks ();
23951 if (id)
23953 cp_parser_check_for_invalid_template_id (parser, id,
23954 class_key,
23955 type_start_token->location);
23957 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23959 /* If it's not a `:' or a `{' then we can't really be looking at a
23960 class-head, since a class-head only appears as part of a
23961 class-specifier. We have to detect this situation before calling
23962 xref_tag, since that has irreversible side-effects. */
23963 if (!cp_parser_next_token_starts_class_definition_p (parser))
23965 cp_parser_error (parser, "expected %<{%> or %<:%>");
23966 type = error_mark_node;
23967 goto out;
23970 /* At this point, we're going ahead with the class-specifier, even
23971 if some other problem occurs. */
23972 cp_parser_commit_to_tentative_parse (parser);
23973 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23975 cp_parser_error (parser,
23976 "cannot specify %<override%> for a class");
23977 type = error_mark_node;
23978 goto out;
23980 /* Issue the error about the overly-qualified name now. */
23981 if (qualified_p)
23983 cp_parser_error (parser,
23984 "global qualification of class name is invalid");
23985 type = error_mark_node;
23986 goto out;
23988 else if (invalid_nested_name_p)
23990 cp_parser_error (parser,
23991 "qualified name does not name a class");
23992 type = error_mark_node;
23993 goto out;
23995 else if (nested_name_specifier)
23997 tree scope;
23999 if (bad_template_keyword)
24000 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24001 keyword template shall not appear at the top level. */
24002 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
24003 "keyword %<template%> not allowed in class-head-name");
24005 /* Reject typedef-names in class heads. */
24006 if (!DECL_IMPLICIT_TYPEDEF_P (type))
24008 error_at (type_start_token->location,
24009 "invalid class name in declaration of %qD",
24010 type);
24011 type = NULL_TREE;
24012 goto done;
24015 /* Figure out in what scope the declaration is being placed. */
24016 scope = current_scope ();
24017 /* If that scope does not contain the scope in which the
24018 class was originally declared, the program is invalid. */
24019 if (scope && !is_ancestor (scope, nested_name_specifier))
24021 if (at_namespace_scope_p ())
24022 error_at (type_start_token->location,
24023 "declaration of %qD in namespace %qD which does not "
24024 "enclose %qD",
24025 type, scope, nested_name_specifier);
24026 else
24027 error_at (type_start_token->location,
24028 "declaration of %qD in %qD which does not enclose %qD",
24029 type, scope, nested_name_specifier);
24030 type = NULL_TREE;
24031 goto done;
24033 /* [dcl.meaning]
24035 A declarator-id shall not be qualified except for the
24036 definition of a ... nested class outside of its class
24037 ... [or] the definition or explicit instantiation of a
24038 class member of a namespace outside of its namespace. */
24039 if (scope == nested_name_specifier)
24041 permerror (nested_name_specifier_token_start->location,
24042 "extra qualification not allowed");
24043 nested_name_specifier = NULL_TREE;
24044 num_templates = 0;
24047 /* An explicit-specialization must be preceded by "template <>". If
24048 it is not, try to recover gracefully. */
24049 if (at_namespace_scope_p ()
24050 && parser->num_template_parameter_lists == 0
24051 && !processing_template_parmlist
24052 && template_id_p)
24054 /* Build a location of this form:
24055 struct typename <ARGS>
24056 ^~~~~~~~~~~~~~~~~~~~~~
24057 with caret==start at the start token, and
24058 finishing at the end of the type. */
24059 location_t reported_loc
24060 = make_location (class_head_start_location,
24061 class_head_start_location,
24062 get_finish (type_start_token->location));
24063 rich_location richloc (line_table, reported_loc);
24064 richloc.add_fixit_insert_before (class_head_start_location,
24065 "template <> ");
24066 error_at (&richloc,
24067 "an explicit specialization must be preceded by"
24068 " %<template <>%>");
24069 invalid_explicit_specialization_p = true;
24070 /* Take the same action that would have been taken by
24071 cp_parser_explicit_specialization. */
24072 ++parser->num_template_parameter_lists;
24073 begin_specialization ();
24075 /* There must be no "return" statements between this point and the
24076 end of this function; set "type "to the correct return value and
24077 use "goto done;" to return. */
24078 /* Make sure that the right number of template parameters were
24079 present. */
24080 if (!cp_parser_check_template_parameters (parser, num_templates,
24081 template_id_p,
24082 type_start_token->location,
24083 /*declarator=*/NULL))
24085 /* If something went wrong, there is no point in even trying to
24086 process the class-definition. */
24087 type = NULL_TREE;
24088 goto done;
24091 /* Look up the type. */
24092 if (template_id_p)
24094 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24095 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24096 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24098 error_at (type_start_token->location,
24099 "function template %qD redeclared as a class template", id);
24100 type = error_mark_node;
24102 else
24104 type = TREE_TYPE (id);
24105 type = maybe_process_partial_specialization (type);
24107 /* Check the scope while we still know whether or not we had a
24108 nested-name-specifier. */
24109 if (type != error_mark_node)
24110 check_unqualified_spec_or_inst (type, type_start_token->location);
24112 if (nested_name_specifier)
24113 pushed_scope = push_scope (nested_name_specifier);
24115 else if (nested_name_specifier)
24117 tree class_type;
24119 /* Given:
24121 template <typename T> struct S { struct T };
24122 template <typename T> struct S<T>::T { };
24124 we will get a TYPENAME_TYPE when processing the definition of
24125 `S::T'. We need to resolve it to the actual type before we
24126 try to define it. */
24127 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24129 class_type = resolve_typename_type (TREE_TYPE (type),
24130 /*only_current_p=*/false);
24131 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24132 type = TYPE_NAME (class_type);
24133 else
24135 cp_parser_error (parser, "could not resolve typename type");
24136 type = error_mark_node;
24140 if (maybe_process_partial_specialization (TREE_TYPE (type))
24141 == error_mark_node)
24143 type = NULL_TREE;
24144 goto done;
24147 class_type = current_class_type;
24148 /* Enter the scope indicated by the nested-name-specifier. */
24149 pushed_scope = push_scope (nested_name_specifier);
24150 /* Get the canonical version of this type. */
24151 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24152 /* Call push_template_decl if it seems like we should be defining a
24153 template either from the template headers or the type we're
24154 defining, so that we diagnose both extra and missing headers. */
24155 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24156 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24157 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24159 type = push_template_decl (type);
24160 if (type == error_mark_node)
24162 type = NULL_TREE;
24163 goto done;
24167 type = TREE_TYPE (type);
24168 *nested_name_specifier_p = true;
24170 else /* The name is not a nested name. */
24172 /* If the class was unnamed, create a dummy name. */
24173 if (!id)
24174 id = make_anon_name ();
24175 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24176 ? ts_within_enclosing_non_class
24177 : ts_current);
24178 type = xref_tag (class_key, id, tag_scope,
24179 parser->num_template_parameter_lists);
24182 /* Indicate whether this class was declared as a `class' or as a
24183 `struct'. */
24184 if (TREE_CODE (type) == RECORD_TYPE)
24185 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24186 cp_parser_check_class_key (class_key, type);
24188 /* If this type was already complete, and we see another definition,
24189 that's an error. Likewise if the type is already being defined:
24190 this can happen, eg, when it's defined from within an expression
24191 (c++/84605). */
24192 if (type != error_mark_node
24193 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24195 error_at (type_start_token->location, "redefinition of %q#T",
24196 type);
24197 inform (location_of (type), "previous definition of %q#T",
24198 type);
24199 type = NULL_TREE;
24200 goto done;
24202 else if (type == error_mark_node)
24203 type = NULL_TREE;
24205 if (type)
24207 /* Apply attributes now, before any use of the class as a template
24208 argument in its base list. */
24209 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24210 fixup_attribute_variants (type);
24213 /* We will have entered the scope containing the class; the names of
24214 base classes should be looked up in that context. For example:
24216 struct A { struct B {}; struct C; };
24217 struct A::C : B {};
24219 is valid. */
24221 /* Get the list of base-classes, if there is one. */
24222 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24224 /* PR59482: enter the class scope so that base-specifiers are looked
24225 up correctly. */
24226 if (type)
24227 pushclass (type);
24228 bases = cp_parser_base_clause (parser);
24229 /* PR59482: get out of the previously pushed class scope so that the
24230 subsequent pops pop the right thing. */
24231 if (type)
24232 popclass ();
24234 else
24235 bases = NULL_TREE;
24237 /* If we're really defining a class, process the base classes.
24238 If they're invalid, fail. */
24239 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24240 xref_basetypes (type, bases);
24242 done:
24243 /* Leave the scope given by the nested-name-specifier. We will
24244 enter the class scope itself while processing the members. */
24245 if (pushed_scope)
24246 pop_scope (pushed_scope);
24248 if (invalid_explicit_specialization_p)
24250 end_specialization ();
24251 --parser->num_template_parameter_lists;
24254 if (type)
24255 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24256 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24257 CLASSTYPE_FINAL (type) = 1;
24258 out:
24259 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24260 return type;
24263 /* Parse a class-key.
24265 class-key:
24266 class
24267 struct
24268 union
24270 Returns the kind of class-key specified, or none_type to indicate
24271 error. */
24273 static enum tag_types
24274 cp_parser_class_key (cp_parser* parser)
24276 cp_token *token;
24277 enum tag_types tag_type;
24279 /* Look for the class-key. */
24280 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24281 if (!token)
24282 return none_type;
24284 /* Check to see if the TOKEN is a class-key. */
24285 tag_type = cp_parser_token_is_class_key (token);
24286 if (!tag_type)
24287 cp_parser_error (parser, "expected class-key");
24288 return tag_type;
24291 /* Parse a type-parameter-key.
24293 type-parameter-key:
24294 class
24295 typename
24298 static void
24299 cp_parser_type_parameter_key (cp_parser* parser)
24301 /* Look for the type-parameter-key. */
24302 enum tag_types tag_type = none_type;
24303 cp_token *token = cp_lexer_peek_token (parser->lexer);
24304 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24306 cp_lexer_consume_token (parser->lexer);
24307 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24308 /* typename is not allowed in a template template parameter
24309 by the standard until C++17. */
24310 pedwarn (token->location, OPT_Wpedantic,
24311 "ISO C++ forbids typename key in template template parameter;"
24312 " use %<-std=c++17%> or %<-std=gnu++17%>");
24314 else
24315 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24317 return;
24320 /* Parse an (optional) member-specification.
24322 member-specification:
24323 member-declaration member-specification [opt]
24324 access-specifier : member-specification [opt] */
24326 static void
24327 cp_parser_member_specification_opt (cp_parser* parser)
24329 while (true)
24331 cp_token *token;
24332 enum rid keyword;
24334 /* Peek at the next token. */
24335 token = cp_lexer_peek_token (parser->lexer);
24336 /* If it's a `}', or EOF then we've seen all the members. */
24337 if (token->type == CPP_CLOSE_BRACE
24338 || token->type == CPP_EOF
24339 || token->type == CPP_PRAGMA_EOL)
24340 break;
24342 /* See if this token is a keyword. */
24343 keyword = token->keyword;
24344 switch (keyword)
24346 case RID_PUBLIC:
24347 case RID_PROTECTED:
24348 case RID_PRIVATE:
24349 /* Consume the access-specifier. */
24350 cp_lexer_consume_token (parser->lexer);
24351 /* Remember which access-specifier is active. */
24352 current_access_specifier = token->u.value;
24353 /* Look for the `:'. */
24354 cp_parser_require (parser, CPP_COLON, RT_COLON);
24355 break;
24357 default:
24358 /* Accept #pragmas at class scope. */
24359 if (token->type == CPP_PRAGMA)
24361 cp_parser_pragma (parser, pragma_member, NULL);
24362 break;
24365 /* Otherwise, the next construction must be a
24366 member-declaration. */
24367 cp_parser_member_declaration (parser);
24372 /* Parse a member-declaration.
24374 member-declaration:
24375 decl-specifier-seq [opt] member-declarator-list [opt] ;
24376 function-definition ; [opt]
24377 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24378 using-declaration
24379 template-declaration
24380 alias-declaration
24382 member-declarator-list:
24383 member-declarator
24384 member-declarator-list , member-declarator
24386 member-declarator:
24387 declarator pure-specifier [opt]
24388 declarator constant-initializer [opt]
24389 identifier [opt] : constant-expression
24391 GNU Extensions:
24393 member-declaration:
24394 __extension__ member-declaration
24396 member-declarator:
24397 declarator attributes [opt] pure-specifier [opt]
24398 declarator attributes [opt] constant-initializer [opt]
24399 identifier [opt] attributes [opt] : constant-expression
24401 C++0x Extensions:
24403 member-declaration:
24404 static_assert-declaration */
24406 static void
24407 cp_parser_member_declaration (cp_parser* parser)
24409 cp_decl_specifier_seq decl_specifiers;
24410 tree prefix_attributes;
24411 tree decl;
24412 int declares_class_or_enum;
24413 bool friend_p;
24414 cp_token *token = NULL;
24415 cp_token *decl_spec_token_start = NULL;
24416 cp_token *initializer_token_start = NULL;
24417 int saved_pedantic;
24418 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24420 /* Check for the `__extension__' keyword. */
24421 if (cp_parser_extension_opt (parser, &saved_pedantic))
24423 /* Recurse. */
24424 cp_parser_member_declaration (parser);
24425 /* Restore the old value of the PEDANTIC flag. */
24426 pedantic = saved_pedantic;
24428 return;
24431 /* Check for a template-declaration. */
24432 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24434 /* An explicit specialization here is an error condition, and we
24435 expect the specialization handler to detect and report this. */
24436 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24437 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24438 cp_parser_explicit_specialization (parser);
24439 else
24440 cp_parser_template_declaration (parser, /*member_p=*/true);
24442 return;
24444 /* Check for a template introduction. */
24445 else if (cp_parser_template_declaration_after_export (parser, true))
24446 return;
24448 /* Check for a using-declaration. */
24449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24451 if (cxx_dialect < cxx11)
24453 /* Parse the using-declaration. */
24454 cp_parser_using_declaration (parser,
24455 /*access_declaration_p=*/false);
24456 return;
24458 else
24460 tree decl;
24461 bool alias_decl_expected;
24462 cp_parser_parse_tentatively (parser);
24463 decl = cp_parser_alias_declaration (parser);
24464 /* Note that if we actually see the '=' token after the
24465 identifier, cp_parser_alias_declaration commits the
24466 tentative parse. In that case, we really expect an
24467 alias-declaration. Otherwise, we expect a using
24468 declaration. */
24469 alias_decl_expected =
24470 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24471 cp_parser_parse_definitely (parser);
24473 if (alias_decl_expected)
24474 finish_member_declaration (decl);
24475 else
24476 cp_parser_using_declaration (parser,
24477 /*access_declaration_p=*/false);
24478 return;
24482 /* Check for @defs. */
24483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24485 tree ivar, member;
24486 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24487 ivar = ivar_chains;
24488 while (ivar)
24490 member = ivar;
24491 ivar = TREE_CHAIN (member);
24492 TREE_CHAIN (member) = NULL_TREE;
24493 finish_member_declaration (member);
24495 return;
24498 /* If the next token is `static_assert' we have a static assertion. */
24499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24501 cp_parser_static_assert (parser, /*member_p=*/true);
24502 return;
24505 parser->colon_corrects_to_scope_p = false;
24507 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24508 goto out;
24510 /* Parse the decl-specifier-seq. */
24511 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24512 cp_parser_decl_specifier_seq (parser,
24513 (CP_PARSER_FLAGS_OPTIONAL
24514 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24515 &decl_specifiers,
24516 &declares_class_or_enum);
24517 /* Check for an invalid type-name. */
24518 if (!decl_specifiers.any_type_specifiers_p
24519 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24520 goto out;
24521 /* If there is no declarator, then the decl-specifier-seq should
24522 specify a type. */
24523 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24525 /* If there was no decl-specifier-seq, and the next token is a
24526 `;', then we have something like:
24528 struct S { ; };
24530 [class.mem]
24532 Each member-declaration shall declare at least one member
24533 name of the class. */
24534 if (!decl_specifiers.any_specifiers_p)
24536 cp_token *token = cp_lexer_peek_token (parser->lexer);
24537 if (!in_system_header_at (token->location))
24539 gcc_rich_location richloc (token->location);
24540 richloc.add_fixit_remove ();
24541 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24544 else
24546 tree type;
24548 /* See if this declaration is a friend. */
24549 friend_p = cp_parser_friend_p (&decl_specifiers);
24550 /* If there were decl-specifiers, check to see if there was
24551 a class-declaration. */
24552 type = check_tag_decl (&decl_specifiers,
24553 /*explicit_type_instantiation_p=*/false);
24554 /* Nested classes have already been added to the class, but
24555 a `friend' needs to be explicitly registered. */
24556 if (friend_p)
24558 /* If the `friend' keyword was present, the friend must
24559 be introduced with a class-key. */
24560 if (!declares_class_or_enum && cxx_dialect < cxx11)
24561 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24562 "in C++03 a class-key must be used "
24563 "when declaring a friend");
24564 /* In this case:
24566 template <typename T> struct A {
24567 friend struct A<T>::B;
24570 A<T>::B will be represented by a TYPENAME_TYPE, and
24571 therefore not recognized by check_tag_decl. */
24572 if (!type)
24574 type = decl_specifiers.type;
24575 if (type && TREE_CODE (type) == TYPE_DECL)
24576 type = TREE_TYPE (type);
24578 if (!type || !TYPE_P (type))
24579 error_at (decl_spec_token_start->location,
24580 "friend declaration does not name a class or "
24581 "function");
24582 else
24583 make_friend_class (current_class_type, type,
24584 /*complain=*/true);
24586 /* If there is no TYPE, an error message will already have
24587 been issued. */
24588 else if (!type || type == error_mark_node)
24590 /* An anonymous aggregate has to be handled specially; such
24591 a declaration really declares a data member (with a
24592 particular type), as opposed to a nested class. */
24593 else if (ANON_AGGR_TYPE_P (type))
24595 /* C++11 9.5/6. */
24596 if (decl_specifiers.storage_class != sc_none)
24597 error_at (decl_spec_token_start->location,
24598 "a storage class on an anonymous aggregate "
24599 "in class scope is not allowed");
24601 /* Remove constructors and such from TYPE, now that we
24602 know it is an anonymous aggregate. */
24603 fixup_anonymous_aggr (type);
24604 /* And make the corresponding data member. */
24605 decl = build_decl (decl_spec_token_start->location,
24606 FIELD_DECL, NULL_TREE, type);
24607 /* Add it to the class. */
24608 finish_member_declaration (decl);
24610 else
24611 cp_parser_check_access_in_redeclaration
24612 (TYPE_NAME (type),
24613 decl_spec_token_start->location);
24616 else
24618 bool assume_semicolon = false;
24620 /* Clear attributes from the decl_specifiers but keep them
24621 around as prefix attributes that apply them to the entity
24622 being declared. */
24623 prefix_attributes = decl_specifiers.attributes;
24624 decl_specifiers.attributes = NULL_TREE;
24626 /* See if these declarations will be friends. */
24627 friend_p = cp_parser_friend_p (&decl_specifiers);
24629 /* Keep going until we hit the `;' at the end of the
24630 declaration. */
24631 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24633 tree attributes = NULL_TREE;
24634 tree first_attribute;
24635 tree initializer;
24636 bool named_bitfld = false;
24638 /* Peek at the next token. */
24639 token = cp_lexer_peek_token (parser->lexer);
24641 /* The following code wants to know early if it is a bit-field
24642 or some other declaration. Attributes can appear before
24643 the `:' token. Skip over them without consuming any tokens
24644 to peek if they are followed by `:'. */
24645 if (cp_next_tokens_can_be_attribute_p (parser)
24646 || (token->type == CPP_NAME
24647 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24648 && (named_bitfld = true)))
24650 size_t n
24651 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24652 token = cp_lexer_peek_nth_token (parser->lexer, n);
24655 /* Check for a bitfield declaration. */
24656 if (token->type == CPP_COLON
24657 || (token->type == CPP_NAME
24658 && token == cp_lexer_peek_token (parser->lexer)
24659 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24660 && (named_bitfld = true)))
24662 tree identifier;
24663 tree width;
24664 tree late_attributes = NULL_TREE;
24665 location_t id_location
24666 = cp_lexer_peek_token (parser->lexer)->location;
24668 if (named_bitfld)
24669 identifier = cp_parser_identifier (parser);
24670 else
24671 identifier = NULL_TREE;
24673 /* Look for attributes that apply to the bitfield. */
24674 attributes = cp_parser_attributes_opt (parser);
24676 /* Consume the `:' token. */
24677 cp_lexer_consume_token (parser->lexer);
24679 /* Get the width of the bitfield. */
24680 width = cp_parser_constant_expression (parser, false, NULL,
24681 cxx_dialect >= cxx11);
24683 /* In C++2A and as extension for C++11 and above we allow
24684 default member initializers for bit-fields. */
24685 initializer = NULL_TREE;
24686 if (cxx_dialect >= cxx11
24687 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24688 || cp_lexer_next_token_is (parser->lexer,
24689 CPP_OPEN_BRACE)))
24691 location_t loc
24692 = cp_lexer_peek_token (parser->lexer)->location;
24693 if (cxx_dialect < cxx2a
24694 && !in_system_header_at (loc)
24695 && identifier != NULL_TREE)
24696 pedwarn (loc, 0,
24697 "default member initializers for bit-fields "
24698 "only available with %<-std=c++2a%> or "
24699 "%<-std=gnu++2a%>");
24701 initializer = cp_parser_save_nsdmi (parser);
24702 if (identifier == NULL_TREE)
24704 error_at (loc, "default member initializer for "
24705 "unnamed bit-field");
24706 initializer = NULL_TREE;
24709 else
24711 /* Look for attributes that apply to the bitfield after
24712 the `:' token and width. This is where GCC used to
24713 parse attributes in the past, pedwarn if there is
24714 a std attribute. */
24715 if (cp_next_tokens_can_be_std_attribute_p (parser))
24716 pedwarn (input_location, OPT_Wpedantic,
24717 "ISO C++ allows bit-field attributes only "
24718 "before the %<:%> token");
24720 late_attributes = cp_parser_attributes_opt (parser);
24723 attributes = attr_chainon (attributes, late_attributes);
24725 /* Remember which attributes are prefix attributes and
24726 which are not. */
24727 first_attribute = attributes;
24728 /* Combine the attributes. */
24729 attributes = attr_chainon (prefix_attributes, attributes);
24731 /* Create the bitfield declaration. */
24732 decl = grokbitfield (identifier
24733 ? make_id_declarator (NULL_TREE,
24734 identifier,
24735 sfk_none,
24736 id_location)
24737 : NULL,
24738 &decl_specifiers,
24739 width, initializer,
24740 attributes);
24742 else
24744 cp_declarator *declarator;
24745 tree asm_specification;
24746 int ctor_dtor_or_conv_p;
24747 bool static_p = (decl_specifiers.storage_class == sc_static);
24749 /* Parse the declarator. */
24750 declarator
24751 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24752 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24753 &ctor_dtor_or_conv_p,
24754 /*parenthesized_p=*/NULL,
24755 /*member_p=*/true,
24756 friend_p, static_p);
24758 /* If something went wrong parsing the declarator, make sure
24759 that we at least consume some tokens. */
24760 if (declarator == cp_error_declarator)
24762 /* Skip to the end of the statement. */
24763 cp_parser_skip_to_end_of_statement (parser);
24764 /* If the next token is not a semicolon, that is
24765 probably because we just skipped over the body of
24766 a function. So, we consume a semicolon if
24767 present, but do not issue an error message if it
24768 is not present. */
24769 if (cp_lexer_next_token_is (parser->lexer,
24770 CPP_SEMICOLON))
24771 cp_lexer_consume_token (parser->lexer);
24772 goto out;
24775 if (declares_class_or_enum & 2)
24776 cp_parser_check_for_definition_in_return_type
24777 (declarator, decl_specifiers.type,
24778 decl_specifiers.locations[ds_type_spec]);
24780 /* Look for an asm-specification. */
24781 asm_specification = cp_parser_asm_specification_opt (parser);
24782 /* Look for attributes that apply to the declaration. */
24783 attributes = cp_parser_attributes_opt (parser);
24784 /* Remember which attributes are prefix attributes and
24785 which are not. */
24786 first_attribute = attributes;
24787 /* Combine the attributes. */
24788 attributes = attr_chainon (prefix_attributes, attributes);
24790 /* If it's an `=', then we have a constant-initializer or a
24791 pure-specifier. It is not correct to parse the
24792 initializer before registering the member declaration
24793 since the member declaration should be in scope while
24794 its initializer is processed. However, the rest of the
24795 front end does not yet provide an interface that allows
24796 us to handle this correctly. */
24797 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24799 /* In [class.mem]:
24801 A pure-specifier shall be used only in the declaration of
24802 a virtual function.
24804 A member-declarator can contain a constant-initializer
24805 only if it declares a static member of integral or
24806 enumeration type.
24808 Therefore, if the DECLARATOR is for a function, we look
24809 for a pure-specifier; otherwise, we look for a
24810 constant-initializer. When we call `grokfield', it will
24811 perform more stringent semantics checks. */
24812 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24813 if (function_declarator_p (declarator)
24814 || (decl_specifiers.type
24815 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24816 && declarator->kind == cdk_id
24817 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24818 == FUNCTION_TYPE)))
24819 initializer = cp_parser_pure_specifier (parser);
24820 else if (decl_specifiers.storage_class != sc_static)
24821 initializer = cp_parser_save_nsdmi (parser);
24822 else if (cxx_dialect >= cxx11)
24824 bool nonconst;
24825 /* Don't require a constant rvalue in C++11, since we
24826 might want a reference constant. We'll enforce
24827 constancy later. */
24828 cp_lexer_consume_token (parser->lexer);
24829 /* Parse the initializer. */
24830 initializer = cp_parser_initializer_clause (parser,
24831 &nonconst);
24833 else
24834 /* Parse the initializer. */
24835 initializer = cp_parser_constant_initializer (parser);
24837 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24838 && !function_declarator_p (declarator))
24840 bool x;
24841 if (decl_specifiers.storage_class != sc_static)
24842 initializer = cp_parser_save_nsdmi (parser);
24843 else
24844 initializer = cp_parser_initializer (parser, &x, &x);
24846 /* Otherwise, there is no initializer. */
24847 else
24848 initializer = NULL_TREE;
24850 /* See if we are probably looking at a function
24851 definition. We are certainly not looking at a
24852 member-declarator. Calling `grokfield' has
24853 side-effects, so we must not do it unless we are sure
24854 that we are looking at a member-declarator. */
24855 if (cp_parser_token_starts_function_definition_p
24856 (cp_lexer_peek_token (parser->lexer)))
24858 /* The grammar does not allow a pure-specifier to be
24859 used when a member function is defined. (It is
24860 possible that this fact is an oversight in the
24861 standard, since a pure function may be defined
24862 outside of the class-specifier. */
24863 if (initializer && initializer_token_start)
24864 error_at (initializer_token_start->location,
24865 "pure-specifier on function-definition");
24866 decl = cp_parser_save_member_function_body (parser,
24867 &decl_specifiers,
24868 declarator,
24869 attributes);
24870 if (parser->fully_implicit_function_template_p)
24871 decl = finish_fully_implicit_template (parser, decl);
24872 /* If the member was not a friend, declare it here. */
24873 if (!friend_p)
24874 finish_member_declaration (decl);
24875 /* Peek at the next token. */
24876 token = cp_lexer_peek_token (parser->lexer);
24877 /* If the next token is a semicolon, consume it. */
24878 if (token->type == CPP_SEMICOLON)
24880 location_t semicolon_loc
24881 = cp_lexer_consume_token (parser->lexer)->location;
24882 gcc_rich_location richloc (semicolon_loc);
24883 richloc.add_fixit_remove ();
24884 warning_at (&richloc, OPT_Wextra_semi,
24885 "extra %<;%> after in-class "
24886 "function definition");
24888 goto out;
24890 else
24891 if (declarator->kind == cdk_function)
24892 declarator->id_loc = token->location;
24893 /* Create the declaration. */
24894 decl = grokfield (declarator, &decl_specifiers,
24895 initializer, /*init_const_expr_p=*/true,
24896 asm_specification, attributes);
24897 if (parser->fully_implicit_function_template_p)
24899 if (friend_p)
24900 finish_fully_implicit_template (parser, 0);
24901 else
24902 decl = finish_fully_implicit_template (parser, decl);
24906 cp_finalize_omp_declare_simd (parser, decl);
24907 cp_finalize_oacc_routine (parser, decl, false);
24909 /* Reset PREFIX_ATTRIBUTES. */
24910 if (attributes != error_mark_node)
24912 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24913 attributes = TREE_CHAIN (attributes);
24914 if (attributes)
24915 TREE_CHAIN (attributes) = NULL_TREE;
24918 /* If there is any qualification still in effect, clear it
24919 now; we will be starting fresh with the next declarator. */
24920 parser->scope = NULL_TREE;
24921 parser->qualifying_scope = NULL_TREE;
24922 parser->object_scope = NULL_TREE;
24923 /* If it's a `,', then there are more declarators. */
24924 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24926 cp_lexer_consume_token (parser->lexer);
24927 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24929 cp_token *token = cp_lexer_previous_token (parser->lexer);
24930 gcc_rich_location richloc (token->location);
24931 richloc.add_fixit_remove ();
24932 error_at (&richloc, "stray %<,%> at end of "
24933 "member declaration");
24936 /* If the next token isn't a `;', then we have a parse error. */
24937 else if (cp_lexer_next_token_is_not (parser->lexer,
24938 CPP_SEMICOLON))
24940 /* The next token might be a ways away from where the
24941 actual semicolon is missing. Find the previous token
24942 and use that for our error position. */
24943 cp_token *token = cp_lexer_previous_token (parser->lexer);
24944 gcc_rich_location richloc (token->location);
24945 richloc.add_fixit_insert_after (";");
24946 error_at (&richloc, "expected %<;%> at end of "
24947 "member declaration");
24949 /* Assume that the user meant to provide a semicolon. If
24950 we were to cp_parser_skip_to_end_of_statement, we might
24951 skip to a semicolon inside a member function definition
24952 and issue nonsensical error messages. */
24953 assume_semicolon = true;
24956 if (decl)
24958 /* Add DECL to the list of members. */
24959 if (!friend_p
24960 /* Explicitly include, eg, NSDMIs, for better error
24961 recovery (c++/58650). */
24962 || !DECL_DECLARES_FUNCTION_P (decl))
24963 finish_member_declaration (decl);
24965 if (TREE_CODE (decl) == FUNCTION_DECL)
24966 cp_parser_save_default_args (parser, decl);
24967 else if (TREE_CODE (decl) == FIELD_DECL
24968 && DECL_INITIAL (decl))
24969 /* Add DECL to the queue of NSDMI to be parsed later. */
24970 vec_safe_push (unparsed_nsdmis, decl);
24973 if (assume_semicolon)
24974 goto out;
24978 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24979 out:
24980 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24983 /* Parse a pure-specifier.
24985 pure-specifier:
24988 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24989 Otherwise, ERROR_MARK_NODE is returned. */
24991 static tree
24992 cp_parser_pure_specifier (cp_parser* parser)
24994 cp_token *token;
24996 /* Look for the `=' token. */
24997 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24998 return error_mark_node;
24999 /* Look for the `0' token. */
25000 token = cp_lexer_peek_token (parser->lexer);
25002 if (token->type == CPP_EOF
25003 || token->type == CPP_PRAGMA_EOL)
25004 return error_mark_node;
25006 cp_lexer_consume_token (parser->lexer);
25008 /* Accept = default or = delete in c++0x mode. */
25009 if (token->keyword == RID_DEFAULT
25010 || token->keyword == RID_DELETE)
25012 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
25013 return token->u.value;
25016 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25017 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
25019 cp_parser_error (parser,
25020 "invalid pure specifier (only %<= 0%> is allowed)");
25021 cp_parser_skip_to_end_of_statement (parser);
25022 return error_mark_node;
25024 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25026 error_at (token->location, "templates may not be %<virtual%>");
25027 return error_mark_node;
25030 return integer_zero_node;
25033 /* Parse a constant-initializer.
25035 constant-initializer:
25036 = constant-expression
25038 Returns a representation of the constant-expression. */
25040 static tree
25041 cp_parser_constant_initializer (cp_parser* parser)
25043 /* Look for the `=' token. */
25044 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25045 return error_mark_node;
25047 /* It is invalid to write:
25049 struct S { static const int i = { 7 }; };
25052 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25054 cp_parser_error (parser,
25055 "a brace-enclosed initializer is not allowed here");
25056 /* Consume the opening brace. */
25057 matching_braces braces;
25058 braces.consume_open (parser);
25059 /* Skip the initializer. */
25060 cp_parser_skip_to_closing_brace (parser);
25061 /* Look for the trailing `}'. */
25062 braces.require_close (parser);
25064 return error_mark_node;
25067 return cp_parser_constant_expression (parser);
25070 /* Derived classes [gram.class.derived] */
25072 /* Parse a base-clause.
25074 base-clause:
25075 : base-specifier-list
25077 base-specifier-list:
25078 base-specifier ... [opt]
25079 base-specifier-list , base-specifier ... [opt]
25081 Returns a TREE_LIST representing the base-classes, in the order in
25082 which they were declared. The representation of each node is as
25083 described by cp_parser_base_specifier.
25085 In the case that no bases are specified, this function will return
25086 NULL_TREE, not ERROR_MARK_NODE. */
25088 static tree
25089 cp_parser_base_clause (cp_parser* parser)
25091 tree bases = NULL_TREE;
25093 /* Look for the `:' that begins the list. */
25094 cp_parser_require (parser, CPP_COLON, RT_COLON);
25096 /* Scan the base-specifier-list. */
25097 while (true)
25099 cp_token *token;
25100 tree base;
25101 bool pack_expansion_p = false;
25103 /* Look for the base-specifier. */
25104 base = cp_parser_base_specifier (parser);
25105 /* Look for the (optional) ellipsis. */
25106 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25108 /* Consume the `...'. */
25109 cp_lexer_consume_token (parser->lexer);
25111 pack_expansion_p = true;
25114 /* Add BASE to the front of the list. */
25115 if (base && base != error_mark_node)
25117 if (pack_expansion_p)
25118 /* Make this a pack expansion type. */
25119 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25121 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25123 TREE_CHAIN (base) = bases;
25124 bases = base;
25127 /* Peek at the next token. */
25128 token = cp_lexer_peek_token (parser->lexer);
25129 /* If it's not a comma, then the list is complete. */
25130 if (token->type != CPP_COMMA)
25131 break;
25132 /* Consume the `,'. */
25133 cp_lexer_consume_token (parser->lexer);
25136 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25137 base class had a qualified name. However, the next name that
25138 appears is certainly not qualified. */
25139 parser->scope = NULL_TREE;
25140 parser->qualifying_scope = NULL_TREE;
25141 parser->object_scope = NULL_TREE;
25143 return nreverse (bases);
25146 /* Parse a base-specifier.
25148 base-specifier:
25149 :: [opt] nested-name-specifier [opt] class-name
25150 virtual access-specifier [opt] :: [opt] nested-name-specifier
25151 [opt] class-name
25152 access-specifier virtual [opt] :: [opt] nested-name-specifier
25153 [opt] class-name
25155 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25156 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25157 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25158 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25160 static tree
25161 cp_parser_base_specifier (cp_parser* parser)
25163 cp_token *token;
25164 bool done = false;
25165 bool virtual_p = false;
25166 bool duplicate_virtual_error_issued_p = false;
25167 bool duplicate_access_error_issued_p = false;
25168 bool class_scope_p, template_p;
25169 tree access = access_default_node;
25170 tree type;
25172 /* Process the optional `virtual' and `access-specifier'. */
25173 while (!done)
25175 /* Peek at the next token. */
25176 token = cp_lexer_peek_token (parser->lexer);
25177 /* Process `virtual'. */
25178 switch (token->keyword)
25180 case RID_VIRTUAL:
25181 /* If `virtual' appears more than once, issue an error. */
25182 if (virtual_p && !duplicate_virtual_error_issued_p)
25184 cp_parser_error (parser,
25185 "%<virtual%> specified more than once in base-specifier");
25186 duplicate_virtual_error_issued_p = true;
25189 virtual_p = true;
25191 /* Consume the `virtual' token. */
25192 cp_lexer_consume_token (parser->lexer);
25194 break;
25196 case RID_PUBLIC:
25197 case RID_PROTECTED:
25198 case RID_PRIVATE:
25199 /* If more than one access specifier appears, issue an
25200 error. */
25201 if (access != access_default_node
25202 && !duplicate_access_error_issued_p)
25204 cp_parser_error (parser,
25205 "more than one access specifier in base-specifier");
25206 duplicate_access_error_issued_p = true;
25209 access = ridpointers[(int) token->keyword];
25211 /* Consume the access-specifier. */
25212 cp_lexer_consume_token (parser->lexer);
25214 break;
25216 default:
25217 done = true;
25218 break;
25221 /* It is not uncommon to see programs mechanically, erroneously, use
25222 the 'typename' keyword to denote (dependent) qualified types
25223 as base classes. */
25224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25226 token = cp_lexer_peek_token (parser->lexer);
25227 if (!processing_template_decl)
25228 error_at (token->location,
25229 "keyword %<typename%> not allowed outside of templates");
25230 else
25231 error_at (token->location,
25232 "keyword %<typename%> not allowed in this context "
25233 "(the base class is implicitly a type)");
25234 cp_lexer_consume_token (parser->lexer);
25237 /* Look for the optional `::' operator. */
25238 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25239 /* Look for the nested-name-specifier. The simplest way to
25240 implement:
25242 [temp.res]
25244 The keyword `typename' is not permitted in a base-specifier or
25245 mem-initializer; in these contexts a qualified name that
25246 depends on a template-parameter is implicitly assumed to be a
25247 type name.
25249 is to pretend that we have seen the `typename' keyword at this
25250 point. */
25251 cp_parser_nested_name_specifier_opt (parser,
25252 /*typename_keyword_p=*/true,
25253 /*check_dependency_p=*/true,
25254 /*type_p=*/true,
25255 /*is_declaration=*/true);
25256 /* If the base class is given by a qualified name, assume that names
25257 we see are type names or templates, as appropriate. */
25258 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25259 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25261 if (!parser->scope
25262 && cp_lexer_next_token_is_decltype (parser->lexer))
25263 /* DR 950 allows decltype as a base-specifier. */
25264 type = cp_parser_decltype (parser);
25265 else
25267 /* Otherwise, look for the class-name. */
25268 type = cp_parser_class_name (parser,
25269 class_scope_p,
25270 template_p,
25271 typename_type,
25272 /*check_dependency_p=*/true,
25273 /*class_head_p=*/false,
25274 /*is_declaration=*/true);
25275 type = TREE_TYPE (type);
25278 if (type == error_mark_node)
25279 return error_mark_node;
25281 return finish_base_specifier (type, access, virtual_p);
25284 /* Exception handling [gram.exception] */
25286 /* Save the tokens that make up the noexcept-specifier for a member-function.
25287 Returns a DEFERRED_PARSE. */
25289 static tree
25290 cp_parser_save_noexcept (cp_parser *parser)
25292 cp_token *first = parser->lexer->next_token;
25293 /* We want everything up to, including, the final ')'. */
25294 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
25295 cp_token *last = parser->lexer->next_token;
25297 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25298 to carry the information we will need. */
25299 tree expr = make_node (DEFERRED_PARSE);
25300 /* Save away the noexcept-specifier; we will process it when the
25301 class is complete. */
25302 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
25303 expr = build_tree_list (expr, NULL_TREE);
25304 return expr;
25307 /* Used for late processing of noexcept-specifiers of member-functions.
25308 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25309 we saved for later; parse it now. */
25311 static tree
25312 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
25314 /* Make sure we've gotten something that hasn't been parsed yet. */
25315 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
25317 push_unparsed_function_queues (parser);
25319 /* Push the saved tokens for the noexcept-specifier onto the parser's
25320 lexer stack. */
25321 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
25322 cp_parser_push_lexer_for_tokens (parser, tokens);
25324 /* Parse the cached noexcept-specifier. */
25325 tree parsed_arg
25326 = cp_parser_noexcept_specification_opt (parser,
25327 /*require_constexpr=*/true,
25328 /*consumed_expr=*/NULL,
25329 /*return_cond=*/false,
25330 /*friend_p=*/false);
25332 /* Revert to the main lexer. */
25333 cp_parser_pop_lexer (parser);
25335 /* Restore the queue. */
25336 pop_unparsed_function_queues (parser);
25338 /* And we're done. */
25339 return parsed_arg;
25342 /* Perform late checking of overriding function with respect to their
25343 noexcept-specifiers. TYPE is the class and FNDECL is the function
25344 that potentially overrides some virtual function with the same
25345 signature. */
25347 static void
25348 noexcept_override_late_checks (tree type, tree fndecl)
25350 tree binfo = TYPE_BINFO (type);
25351 tree base_binfo;
25353 if (DECL_STATIC_FUNCTION_P (fndecl))
25354 return;
25356 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
25358 tree basetype = BINFO_TYPE (base_binfo);
25360 if (!TYPE_POLYMORPHIC_P (basetype))
25361 continue;
25363 tree fn = look_for_overrides_here (basetype, fndecl);
25364 if (fn)
25365 maybe_check_overriding_exception_spec (fndecl, fn);
25369 /* Parse an (optional) noexcept-specification.
25371 noexcept-specification:
25372 noexcept ( constant-expression ) [opt]
25374 If no noexcept-specification is present, returns NULL_TREE.
25375 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25376 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25377 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25378 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25379 in which case a boolean condition is returned instead. If FRIEND_P is true,
25380 the function with this noexcept-specification had the `friend' specifier. */
25382 static tree
25383 cp_parser_noexcept_specification_opt (cp_parser* parser,
25384 bool require_constexpr,
25385 bool* consumed_expr,
25386 bool return_cond,
25387 bool friend_p)
25389 cp_token *token;
25390 const char *saved_message;
25392 /* Peek at the next token. */
25393 token = cp_lexer_peek_token (parser->lexer);
25395 /* Is it a noexcept-specification? */
25396 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25398 tree expr;
25400 /* [class.mem]/6 says that a noexcept-specifer (within the
25401 member-specification of the class) is a complete-class context of
25402 a class. So, if the noexcept-specifier has the optional expression,
25403 just save the tokens, and reparse this after we're done with the
25404 class. */
25405 const bool literal_p
25406 = ((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
25407 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
25408 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN));
25410 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
25411 /* No need to delay parsing for a number literal or true/false. */
25412 && !literal_p
25413 && at_class_scope_p ()
25414 /* Don't delay parsing for friend member functions. */
25415 && !friend_p
25416 && TYPE_BEING_DEFINED (current_class_type)
25417 && !LAMBDA_TYPE_P (current_class_type))
25418 return cp_parser_save_noexcept (parser);
25420 cp_lexer_consume_token (parser->lexer);
25422 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25424 matching_parens parens;
25425 parens.consume_open (parser);
25427 tree save_ccp = current_class_ptr;
25428 tree save_ccr = current_class_ref;
25430 if (current_class_type)
25431 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25433 if (require_constexpr)
25435 /* Types may not be defined in an exception-specification. */
25436 saved_message = parser->type_definition_forbidden_message;
25437 parser->type_definition_forbidden_message
25438 = G_("types may not be defined in an exception-specification");
25440 bool non_constant_p;
25441 expr
25442 = cp_parser_constant_expression (parser,
25443 /*allow_non_constant=*/true,
25444 &non_constant_p);
25445 if (non_constant_p
25446 && !require_potential_rvalue_constant_expression (expr))
25448 expr = NULL_TREE;
25449 return_cond = true;
25452 /* Restore the saved message. */
25453 parser->type_definition_forbidden_message = saved_message;
25455 else
25457 expr = cp_parser_expression (parser);
25458 *consumed_expr = true;
25461 parens.require_close (parser);
25463 current_class_ptr = save_ccp;
25464 current_class_ref = save_ccr;
25466 else
25468 expr = boolean_true_node;
25469 if (!require_constexpr)
25470 *consumed_expr = false;
25473 /* We cannot build a noexcept-spec right away because this will check
25474 that expr is a constexpr. */
25475 if (!return_cond)
25476 return build_noexcept_spec (expr, tf_warning_or_error);
25477 else
25478 return expr;
25480 else
25481 return NULL_TREE;
25484 /* Parse an (optional) exception-specification.
25486 exception-specification:
25487 throw ( type-id-list [opt] )
25489 Returns a TREE_LIST representing the exception-specification. The
25490 TREE_VALUE of each node is a type. If FRIEND_P is true, the function
25491 with this noexcept-specification had the `friend' specifier. */
25493 static tree
25494 cp_parser_exception_specification_opt (cp_parser* parser, bool friend_p)
25496 cp_token *token;
25497 tree type_id_list;
25498 const char *saved_message;
25500 /* Peek at the next token. */
25501 token = cp_lexer_peek_token (parser->lexer);
25503 /* Is it a noexcept-specification? */
25504 type_id_list
25505 = cp_parser_noexcept_specification_opt (parser,
25506 /*require_constexpr=*/true,
25507 /*consumed_expr=*/NULL,
25508 /*return_cond=*/false,
25509 friend_p);
25510 if (type_id_list != NULL_TREE)
25511 return type_id_list;
25513 /* If it's not `throw', then there's no exception-specification. */
25514 if (!cp_parser_is_keyword (token, RID_THROW))
25515 return NULL_TREE;
25517 location_t loc = token->location;
25519 /* Consume the `throw'. */
25520 cp_lexer_consume_token (parser->lexer);
25522 /* Look for the `('. */
25523 matching_parens parens;
25524 parens.require_open (parser);
25526 /* Peek at the next token. */
25527 token = cp_lexer_peek_token (parser->lexer);
25528 /* If it's not a `)', then there is a type-id-list. */
25529 if (token->type != CPP_CLOSE_PAREN)
25531 /* Types may not be defined in an exception-specification. */
25532 saved_message = parser->type_definition_forbidden_message;
25533 parser->type_definition_forbidden_message
25534 = G_("types may not be defined in an exception-specification");
25535 /* Parse the type-id-list. */
25536 type_id_list = cp_parser_type_id_list (parser);
25537 /* Restore the saved message. */
25538 parser->type_definition_forbidden_message = saved_message;
25540 if (cxx_dialect >= cxx17)
25542 error_at (loc, "ISO C++17 does not allow dynamic exception "
25543 "specifications");
25544 type_id_list = NULL_TREE;
25546 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25547 warning_at (loc, OPT_Wdeprecated,
25548 "dynamic exception specifications are deprecated in "
25549 "C++11");
25551 /* In C++17, throw() is equivalent to noexcept (true). throw()
25552 is deprecated in C++11 and above as well, but is still widely used,
25553 so don't warn about it yet. */
25554 else if (cxx_dialect >= cxx17)
25555 type_id_list = noexcept_true_spec;
25556 else
25557 type_id_list = empty_except_spec;
25559 /* Look for the `)'. */
25560 parens.require_close (parser);
25562 return type_id_list;
25565 /* Parse an (optional) type-id-list.
25567 type-id-list:
25568 type-id ... [opt]
25569 type-id-list , type-id ... [opt]
25571 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25572 in the order that the types were presented. */
25574 static tree
25575 cp_parser_type_id_list (cp_parser* parser)
25577 tree types = NULL_TREE;
25579 while (true)
25581 cp_token *token;
25582 tree type;
25584 token = cp_lexer_peek_token (parser->lexer);
25586 /* Get the next type-id. */
25587 type = cp_parser_type_id (parser);
25588 /* Check for invalid 'auto'. */
25589 if (flag_concepts && type_uses_auto (type))
25591 error_at (token->location,
25592 "invalid use of %<auto%> in exception-specification");
25593 type = error_mark_node;
25595 /* Parse the optional ellipsis. */
25596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25598 /* Consume the `...'. */
25599 cp_lexer_consume_token (parser->lexer);
25601 /* Turn the type into a pack expansion expression. */
25602 type = make_pack_expansion (type);
25604 /* Add it to the list. */
25605 types = add_exception_specifier (types, type, /*complain=*/1);
25606 /* Peek at the next token. */
25607 token = cp_lexer_peek_token (parser->lexer);
25608 /* If it is not a `,', we are done. */
25609 if (token->type != CPP_COMMA)
25610 break;
25611 /* Consume the `,'. */
25612 cp_lexer_consume_token (parser->lexer);
25615 return nreverse (types);
25618 /* Parse a try-block.
25620 try-block:
25621 try compound-statement handler-seq */
25623 static tree
25624 cp_parser_try_block (cp_parser* parser)
25626 tree try_block;
25628 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25629 if (parser->in_function_body
25630 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25631 && cxx_dialect < cxx2a)
25632 pedwarn (input_location, 0,
25633 "%<try%> in %<constexpr%> function only "
25634 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25636 try_block = begin_try_block ();
25637 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25638 finish_try_block (try_block);
25639 cp_parser_handler_seq (parser);
25640 finish_handler_sequence (try_block);
25642 return try_block;
25645 /* Parse a function-try-block.
25647 function-try-block:
25648 try ctor-initializer [opt] function-body handler-seq */
25650 static void
25651 cp_parser_function_try_block (cp_parser* parser)
25653 tree compound_stmt;
25654 tree try_block;
25656 /* Look for the `try' keyword. */
25657 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25658 return;
25659 /* Let the rest of the front end know where we are. */
25660 try_block = begin_function_try_block (&compound_stmt);
25661 /* Parse the function-body. */
25662 cp_parser_ctor_initializer_opt_and_function_body
25663 (parser, /*in_function_try_block=*/true);
25664 /* We're done with the `try' part. */
25665 finish_function_try_block (try_block);
25666 /* Parse the handlers. */
25667 cp_parser_handler_seq (parser);
25668 /* We're done with the handlers. */
25669 finish_function_handler_sequence (try_block, compound_stmt);
25672 /* Parse a handler-seq.
25674 handler-seq:
25675 handler handler-seq [opt] */
25677 static void
25678 cp_parser_handler_seq (cp_parser* parser)
25680 while (true)
25682 cp_token *token;
25684 /* Parse the handler. */
25685 cp_parser_handler (parser);
25686 /* Peek at the next token. */
25687 token = cp_lexer_peek_token (parser->lexer);
25688 /* If it's not `catch' then there are no more handlers. */
25689 if (!cp_parser_is_keyword (token, RID_CATCH))
25690 break;
25694 /* Parse a handler.
25696 handler:
25697 catch ( exception-declaration ) compound-statement */
25699 static void
25700 cp_parser_handler (cp_parser* parser)
25702 tree handler;
25703 tree declaration;
25705 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25706 handler = begin_handler ();
25707 matching_parens parens;
25708 parens.require_open (parser);
25709 declaration = cp_parser_exception_declaration (parser);
25710 finish_handler_parms (declaration, handler);
25711 parens.require_close (parser);
25712 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25713 finish_handler (handler);
25716 /* Parse an exception-declaration.
25718 exception-declaration:
25719 type-specifier-seq declarator
25720 type-specifier-seq abstract-declarator
25721 type-specifier-seq
25724 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25725 ellipsis variant is used. */
25727 static tree
25728 cp_parser_exception_declaration (cp_parser* parser)
25730 cp_decl_specifier_seq type_specifiers;
25731 cp_declarator *declarator;
25732 const char *saved_message;
25734 /* If it's an ellipsis, it's easy to handle. */
25735 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25737 /* Consume the `...' token. */
25738 cp_lexer_consume_token (parser->lexer);
25739 return NULL_TREE;
25742 /* Types may not be defined in exception-declarations. */
25743 saved_message = parser->type_definition_forbidden_message;
25744 parser->type_definition_forbidden_message
25745 = G_("types may not be defined in exception-declarations");
25747 /* Parse the type-specifier-seq. */
25748 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25749 /*is_declaration=*/true,
25750 /*is_trailing_return=*/false,
25751 &type_specifiers);
25752 /* If it's a `)', then there is no declarator. */
25753 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25754 declarator = NULL;
25755 else
25756 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25757 CP_PARSER_FLAGS_NONE,
25758 /*ctor_dtor_or_conv_p=*/NULL,
25759 /*parenthesized_p=*/NULL,
25760 /*member_p=*/false,
25761 /*friend_p=*/false,
25762 /*static_p=*/false);
25764 /* Restore the saved message. */
25765 parser->type_definition_forbidden_message = saved_message;
25767 if (!type_specifiers.any_specifiers_p)
25768 return error_mark_node;
25770 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25773 /* Parse a throw-expression.
25775 throw-expression:
25776 throw assignment-expression [opt]
25778 Returns a THROW_EXPR representing the throw-expression. */
25780 static tree
25781 cp_parser_throw_expression (cp_parser* parser)
25783 tree expression;
25784 cp_token* token;
25786 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25787 token = cp_lexer_peek_token (parser->lexer);
25788 /* Figure out whether or not there is an assignment-expression
25789 following the "throw" keyword. */
25790 if (token->type == CPP_COMMA
25791 || token->type == CPP_SEMICOLON
25792 || token->type == CPP_CLOSE_PAREN
25793 || token->type == CPP_CLOSE_SQUARE
25794 || token->type == CPP_CLOSE_BRACE
25795 || token->type == CPP_COLON)
25796 expression = NULL_TREE;
25797 else
25798 expression = cp_parser_assignment_expression (parser);
25800 return build_throw (expression);
25803 /* GNU Extensions */
25805 /* Parse an (optional) asm-specification.
25807 asm-specification:
25808 asm ( string-literal )
25810 If the asm-specification is present, returns a STRING_CST
25811 corresponding to the string-literal. Otherwise, returns
25812 NULL_TREE. */
25814 static tree
25815 cp_parser_asm_specification_opt (cp_parser* parser)
25817 cp_token *token;
25818 tree asm_specification;
25820 /* Peek at the next token. */
25821 token = cp_lexer_peek_token (parser->lexer);
25822 /* If the next token isn't the `asm' keyword, then there's no
25823 asm-specification. */
25824 if (!cp_parser_is_keyword (token, RID_ASM))
25825 return NULL_TREE;
25827 /* Consume the `asm' token. */
25828 cp_lexer_consume_token (parser->lexer);
25829 /* Look for the `('. */
25830 matching_parens parens;
25831 parens.require_open (parser);
25833 /* Look for the string-literal. */
25834 asm_specification = cp_parser_string_literal (parser, false, false);
25836 /* Look for the `)'. */
25837 parens.require_close (parser);
25839 return asm_specification;
25842 /* Parse an asm-operand-list.
25844 asm-operand-list:
25845 asm-operand
25846 asm-operand-list , asm-operand
25848 asm-operand:
25849 string-literal ( expression )
25850 [ string-literal ] string-literal ( expression )
25852 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25853 each node is the expression. The TREE_PURPOSE is itself a
25854 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25855 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25856 is a STRING_CST for the string literal before the parenthesis. Returns
25857 ERROR_MARK_NODE if any of the operands are invalid. */
25859 static tree
25860 cp_parser_asm_operand_list (cp_parser* parser)
25862 tree asm_operands = NULL_TREE;
25863 bool invalid_operands = false;
25865 while (true)
25867 tree string_literal;
25868 tree expression;
25869 tree name;
25871 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25873 /* Consume the `[' token. */
25874 cp_lexer_consume_token (parser->lexer);
25875 /* Read the operand name. */
25876 name = cp_parser_identifier (parser);
25877 if (name != error_mark_node)
25878 name = build_string (IDENTIFIER_LENGTH (name),
25879 IDENTIFIER_POINTER (name));
25880 /* Look for the closing `]'. */
25881 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25883 else
25884 name = NULL_TREE;
25885 /* Look for the string-literal. */
25886 string_literal = cp_parser_string_literal (parser, false, false);
25888 /* Look for the `('. */
25889 matching_parens parens;
25890 parens.require_open (parser);
25891 /* Parse the expression. */
25892 expression = cp_parser_expression (parser);
25893 /* Look for the `)'. */
25894 parens.require_close (parser);
25896 if (name == error_mark_node
25897 || string_literal == error_mark_node
25898 || expression == error_mark_node)
25899 invalid_operands = true;
25901 /* Add this operand to the list. */
25902 asm_operands = tree_cons (build_tree_list (name, string_literal),
25903 expression,
25904 asm_operands);
25905 /* If the next token is not a `,', there are no more
25906 operands. */
25907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25908 break;
25909 /* Consume the `,'. */
25910 cp_lexer_consume_token (parser->lexer);
25913 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25916 /* Parse an asm-clobber-list.
25918 asm-clobber-list:
25919 string-literal
25920 asm-clobber-list , string-literal
25922 Returns a TREE_LIST, indicating the clobbers in the order that they
25923 appeared. The TREE_VALUE of each node is a STRING_CST. */
25925 static tree
25926 cp_parser_asm_clobber_list (cp_parser* parser)
25928 tree clobbers = NULL_TREE;
25930 while (true)
25932 tree string_literal;
25934 /* Look for the string literal. */
25935 string_literal = cp_parser_string_literal (parser, false, false);
25936 /* Add it to the list. */
25937 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25938 /* If the next token is not a `,', then the list is
25939 complete. */
25940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25941 break;
25942 /* Consume the `,' token. */
25943 cp_lexer_consume_token (parser->lexer);
25946 return clobbers;
25949 /* Parse an asm-label-list.
25951 asm-label-list:
25952 identifier
25953 asm-label-list , identifier
25955 Returns a TREE_LIST, indicating the labels in the order that they
25956 appeared. The TREE_VALUE of each node is a label. */
25958 static tree
25959 cp_parser_asm_label_list (cp_parser* parser)
25961 tree labels = NULL_TREE;
25963 while (true)
25965 tree identifier, label, name;
25967 /* Look for the identifier. */
25968 identifier = cp_parser_identifier (parser);
25969 if (!error_operand_p (identifier))
25971 label = lookup_label (identifier);
25972 if (TREE_CODE (label) == LABEL_DECL)
25974 TREE_USED (label) = 1;
25975 check_goto (label);
25976 name = build_string (IDENTIFIER_LENGTH (identifier),
25977 IDENTIFIER_POINTER (identifier));
25978 labels = tree_cons (name, label, labels);
25981 /* If the next token is not a `,', then the list is
25982 complete. */
25983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25984 break;
25985 /* Consume the `,' token. */
25986 cp_lexer_consume_token (parser->lexer);
25989 return nreverse (labels);
25992 /* Return TRUE iff the next tokens in the stream are possibly the
25993 beginning of a GNU extension attribute. */
25995 static bool
25996 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25998 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
26001 /* Return TRUE iff the next tokens in the stream are possibly the
26002 beginning of a standard C++-11 attribute specifier. */
26004 static bool
26005 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
26007 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
26010 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26011 beginning of a standard C++-11 attribute specifier. */
26013 static bool
26014 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
26016 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26018 return (cxx_dialect >= cxx11
26019 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
26020 || (token->type == CPP_OPEN_SQUARE
26021 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
26022 && token->type == CPP_OPEN_SQUARE)));
26025 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26026 beginning of a GNU extension attribute. */
26028 static bool
26029 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
26031 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26033 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
26036 /* Return true iff the next tokens can be the beginning of either a
26037 GNU attribute list, or a standard C++11 attribute sequence. */
26039 static bool
26040 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
26042 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
26043 || cp_next_tokens_can_be_std_attribute_p (parser));
26046 /* Return true iff the next Nth tokens can be the beginning of either
26047 a GNU attribute list, or a standard C++11 attribute sequence. */
26049 static bool
26050 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
26052 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
26053 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
26056 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26057 of GNU attributes, or return NULL. */
26059 static tree
26060 cp_parser_attributes_opt (cp_parser *parser)
26062 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26063 return cp_parser_gnu_attributes_opt (parser);
26064 return cp_parser_std_attribute_spec_seq (parser);
26067 /* Parse an (optional) series of attributes.
26069 attributes:
26070 attributes attribute
26072 attribute:
26073 __attribute__ (( attribute-list [opt] ))
26075 The return value is as for cp_parser_gnu_attribute_list. */
26077 static tree
26078 cp_parser_gnu_attributes_opt (cp_parser* parser)
26080 tree attributes = NULL_TREE;
26082 temp_override<bool> cleanup
26083 (parser->auto_is_implicit_function_template_parm_p, false);
26085 while (true)
26087 cp_token *token;
26088 tree attribute_list;
26089 bool ok = true;
26091 /* Peek at the next token. */
26092 token = cp_lexer_peek_token (parser->lexer);
26093 /* If it's not `__attribute__', then we're done. */
26094 if (token->keyword != RID_ATTRIBUTE)
26095 break;
26097 /* Consume the `__attribute__' keyword. */
26098 cp_lexer_consume_token (parser->lexer);
26099 /* Look for the two `(' tokens. */
26100 matching_parens outer_parens;
26101 if (!outer_parens.require_open (parser))
26102 ok = false;
26103 matching_parens inner_parens;
26104 if (!inner_parens.require_open (parser))
26105 ok = false;
26107 /* Peek at the next token. */
26108 token = cp_lexer_peek_token (parser->lexer);
26109 if (token->type != CPP_CLOSE_PAREN)
26110 /* Parse the attribute-list. */
26111 attribute_list = cp_parser_gnu_attribute_list (parser);
26112 else
26113 /* If the next token is a `)', then there is no attribute
26114 list. */
26115 attribute_list = NULL;
26117 /* Look for the two `)' tokens. */
26118 if (!inner_parens.require_close (parser))
26119 ok = false;
26120 if (!outer_parens.require_close (parser))
26121 ok = false;
26122 if (!ok)
26123 cp_parser_skip_to_end_of_statement (parser);
26125 /* Add these new attributes to the list. */
26126 attributes = attr_chainon (attributes, attribute_list);
26129 return attributes;
26132 /* Parse a GNU attribute-list.
26134 attribute-list:
26135 attribute
26136 attribute-list , attribute
26138 attribute:
26139 identifier
26140 identifier ( identifier )
26141 identifier ( identifier , expression-list )
26142 identifier ( expression-list )
26144 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
26145 to an attribute. The TREE_PURPOSE of each node is the identifier
26146 indicating which attribute is in use. The TREE_VALUE represents
26147 the arguments, if any. */
26149 static tree
26150 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
26152 tree attribute_list = NULL_TREE;
26153 bool save_translate_strings_p = parser->translate_strings_p;
26155 /* Don't create wrapper nodes within attributes: the
26156 handlers don't know how to handle them. */
26157 auto_suppress_location_wrappers sentinel;
26159 parser->translate_strings_p = false;
26160 while (true)
26162 cp_token *token;
26163 tree identifier;
26164 tree attribute;
26166 /* Look for the identifier. We also allow keywords here; for
26167 example `__attribute__ ((const))' is legal. */
26168 token = cp_lexer_peek_token (parser->lexer);
26169 if (token->type == CPP_NAME
26170 || token->type == CPP_KEYWORD)
26172 tree arguments = NULL_TREE;
26174 /* Consume the token, but save it since we need it for the
26175 SIMD enabled function parsing. */
26176 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
26178 /* Save away the identifier that indicates which attribute
26179 this is. */
26180 identifier = (token->type == CPP_KEYWORD)
26181 /* For keywords, use the canonical spelling, not the
26182 parsed identifier. */
26183 ? ridpointers[(int) token->keyword]
26184 : id_token->u.value;
26186 identifier = canonicalize_attr_name (identifier);
26187 attribute = build_tree_list (identifier, NULL_TREE);
26189 /* Peek at the next token. */
26190 token = cp_lexer_peek_token (parser->lexer);
26191 /* If it's an `(', then parse the attribute arguments. */
26192 if (token->type == CPP_OPEN_PAREN)
26194 vec<tree, va_gc> *vec;
26195 int attr_flag = (attribute_takes_identifier_p (identifier)
26196 ? id_attr : normal_attr);
26197 vec = cp_parser_parenthesized_expression_list
26198 (parser, attr_flag, /*cast_p=*/false,
26199 /*allow_expansion_p=*/false,
26200 /*non_constant_p=*/NULL);
26201 if (vec == NULL)
26202 arguments = error_mark_node;
26203 else
26205 arguments = build_tree_list_vec (vec);
26206 release_tree_vector (vec);
26208 /* Save the arguments away. */
26209 TREE_VALUE (attribute) = arguments;
26212 if (arguments != error_mark_node)
26214 /* Add this attribute to the list. */
26215 TREE_CHAIN (attribute) = attribute_list;
26216 attribute_list = attribute;
26219 token = cp_lexer_peek_token (parser->lexer);
26221 /* Unless EXACTLY_ONE is set look for more attributes.
26222 If the next token isn't a `,', we're done. */
26223 if (exactly_one || token->type != CPP_COMMA)
26224 break;
26226 /* Consume the comma and keep going. */
26227 cp_lexer_consume_token (parser->lexer);
26229 parser->translate_strings_p = save_translate_strings_p;
26231 /* We built up the list in reverse order. */
26232 return nreverse (attribute_list);
26235 /* Parse a standard C++11 attribute.
26237 The returned representation is a TREE_LIST which TREE_PURPOSE is
26238 the scoped name of the attribute, and the TREE_VALUE is its
26239 arguments list.
26241 Note that the scoped name of the attribute is itself a TREE_LIST
26242 which TREE_PURPOSE is the namespace of the attribute, and
26243 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26244 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26245 and which TREE_PURPOSE is directly the attribute name.
26247 Clients of the attribute code should use get_attribute_namespace
26248 and get_attribute_name to get the actual namespace and name of
26249 attributes, regardless of their being GNU or C++11 attributes.
26251 attribute:
26252 attribute-token attribute-argument-clause [opt]
26254 attribute-token:
26255 identifier
26256 attribute-scoped-token
26258 attribute-scoped-token:
26259 attribute-namespace :: identifier
26261 attribute-namespace:
26262 identifier
26264 attribute-argument-clause:
26265 ( balanced-token-seq )
26267 balanced-token-seq:
26268 balanced-token [opt]
26269 balanced-token-seq balanced-token
26271 balanced-token:
26272 ( balanced-token-seq )
26273 [ balanced-token-seq ]
26274 { balanced-token-seq }. */
26276 static tree
26277 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26279 tree attribute, attr_id = NULL_TREE, arguments;
26280 cp_token *token;
26282 temp_override<bool> cleanup
26283 (parser->auto_is_implicit_function_template_parm_p, false);
26285 /* First, parse name of the attribute, a.k.a attribute-token. */
26287 token = cp_lexer_peek_token (parser->lexer);
26288 if (token->type == CPP_NAME)
26289 attr_id = token->u.value;
26290 else if (token->type == CPP_KEYWORD)
26291 attr_id = ridpointers[(int) token->keyword];
26292 else if (token->flags & NAMED_OP)
26293 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26295 if (attr_id == NULL_TREE)
26296 return NULL_TREE;
26298 cp_lexer_consume_token (parser->lexer);
26300 token = cp_lexer_peek_token (parser->lexer);
26301 if (token->type == CPP_SCOPE)
26303 /* We are seeing a scoped attribute token. */
26305 cp_lexer_consume_token (parser->lexer);
26306 if (attr_ns)
26307 error_at (token->location, "attribute using prefix used together "
26308 "with scoped attribute token");
26309 attr_ns = attr_id;
26311 token = cp_lexer_consume_token (parser->lexer);
26312 if (token->type == CPP_NAME)
26313 attr_id = token->u.value;
26314 else if (token->type == CPP_KEYWORD)
26315 attr_id = ridpointers[(int) token->keyword];
26316 else if (token->flags & NAMED_OP)
26317 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26318 else
26320 error_at (token->location,
26321 "expected an identifier for the attribute name");
26322 return error_mark_node;
26325 attr_ns = canonicalize_attr_name (attr_ns);
26326 attr_id = canonicalize_attr_name (attr_id);
26327 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26328 NULL_TREE);
26329 token = cp_lexer_peek_token (parser->lexer);
26331 else if (attr_ns)
26333 attr_ns = canonicalize_attr_name (attr_ns);
26334 attr_id = canonicalize_attr_name (attr_id);
26335 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26336 NULL_TREE);
26338 else
26340 attr_id = canonicalize_attr_name (attr_id);
26341 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26342 NULL_TREE);
26343 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26344 but no longer: we have to be able to tell [[noreturn]] and
26345 __attribute__((noreturn)) apart. */
26346 /* C++14 deprecated attribute is equivalent to GNU's. */
26347 if (is_attribute_p ("deprecated", attr_id))
26348 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26349 /* C++17 fallthrough attribute is equivalent to GNU's. */
26350 else if (is_attribute_p ("fallthrough", attr_id))
26351 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26352 /* Transactional Memory TS optimize_for_synchronized attribute is
26353 equivalent to GNU transaction_callable. */
26354 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26355 TREE_PURPOSE (attribute)
26356 = get_identifier ("transaction_callable");
26357 /* Transactional Memory attributes are GNU attributes. */
26358 else if (tm_attr_to_mask (attr_id))
26359 TREE_PURPOSE (attribute) = attr_id;
26362 /* Now parse the optional argument clause of the attribute. */
26364 if (token->type != CPP_OPEN_PAREN)
26365 return attribute;
26368 vec<tree, va_gc> *vec;
26369 int attr_flag = normal_attr;
26371 /* Maybe we don't expect to see any arguments for this attribute. */
26372 const attribute_spec *as
26373 = lookup_attribute_spec (TREE_PURPOSE (attribute));
26374 if (as && as->max_length == 0)
26376 error_at (token->location, "%qE attribute does not take any arguments",
26377 attr_id);
26378 cp_parser_skip_to_closing_parenthesis (parser,
26379 /*recovering=*/true,
26380 /*or_comma=*/false,
26381 /*consume_paren=*/true);
26382 return error_mark_node;
26385 if (attr_ns == gnu_identifier
26386 && attribute_takes_identifier_p (attr_id))
26387 /* A GNU attribute that takes an identifier in parameter. */
26388 attr_flag = id_attr;
26390 vec = cp_parser_parenthesized_expression_list
26391 (parser, attr_flag, /*cast_p=*/false,
26392 /*allow_expansion_p=*/true,
26393 /*non_constant_p=*/NULL);
26394 if (vec == NULL)
26395 arguments = error_mark_node;
26396 else
26398 arguments = build_tree_list_vec (vec);
26399 release_tree_vector (vec);
26402 if (arguments == error_mark_node)
26403 attribute = error_mark_node;
26404 else
26405 TREE_VALUE (attribute) = arguments;
26408 return attribute;
26411 /* Check that the attribute ATTRIBUTE appears at most once in the
26412 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26413 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26414 isn't implemented yet in GCC. */
26416 static void
26417 cp_parser_check_std_attribute (tree attributes, tree attribute)
26419 if (attributes)
26421 tree name = get_attribute_name (attribute);
26422 if (is_attribute_p ("noreturn", name)
26423 && lookup_attribute ("noreturn", attributes))
26424 error ("attribute %<noreturn%> can appear at most once "
26425 "in an attribute-list");
26426 else if (is_attribute_p ("deprecated", name)
26427 && lookup_attribute ("deprecated", attributes))
26428 error ("attribute %<deprecated%> can appear at most once "
26429 "in an attribute-list");
26433 /* Parse a list of standard C++-11 attributes.
26435 attribute-list:
26436 attribute [opt]
26437 attribute-list , attribute[opt]
26438 attribute ...
26439 attribute-list , attribute ...
26442 static tree
26443 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26445 tree attributes = NULL_TREE, attribute = NULL_TREE;
26446 cp_token *token = NULL;
26448 while (true)
26450 attribute = cp_parser_std_attribute (parser, attr_ns);
26451 if (attribute == error_mark_node)
26452 break;
26453 if (attribute != NULL_TREE)
26455 cp_parser_check_std_attribute (attributes, attribute);
26456 TREE_CHAIN (attribute) = attributes;
26457 attributes = attribute;
26459 token = cp_lexer_peek_token (parser->lexer);
26460 if (token->type == CPP_ELLIPSIS)
26462 cp_lexer_consume_token (parser->lexer);
26463 if (attribute == NULL_TREE)
26464 error_at (token->location,
26465 "expected attribute before %<...%>");
26466 else
26468 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26469 if (pack == error_mark_node)
26470 return error_mark_node;
26471 TREE_VALUE (attribute) = pack;
26473 token = cp_lexer_peek_token (parser->lexer);
26475 if (token->type != CPP_COMMA)
26476 break;
26477 cp_lexer_consume_token (parser->lexer);
26479 attributes = nreverse (attributes);
26480 return attributes;
26483 /* Parse a standard C++-11 attribute specifier.
26485 attribute-specifier:
26486 [ [ attribute-using-prefix [opt] attribute-list ] ]
26487 alignment-specifier
26489 attribute-using-prefix:
26490 using attribute-namespace :
26492 alignment-specifier:
26493 alignas ( type-id ... [opt] )
26494 alignas ( alignment-expression ... [opt] ). */
26496 static tree
26497 cp_parser_std_attribute_spec (cp_parser *parser)
26499 tree attributes = NULL_TREE;
26500 cp_token *token = cp_lexer_peek_token (parser->lexer);
26502 if (token->type == CPP_OPEN_SQUARE
26503 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26505 tree attr_ns = NULL_TREE;
26507 cp_lexer_consume_token (parser->lexer);
26508 cp_lexer_consume_token (parser->lexer);
26510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26512 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26513 if (token->type == CPP_NAME)
26514 attr_ns = token->u.value;
26515 else if (token->type == CPP_KEYWORD)
26516 attr_ns = ridpointers[(int) token->keyword];
26517 else if (token->flags & NAMED_OP)
26518 attr_ns = get_identifier (cpp_type2name (token->type,
26519 token->flags));
26520 if (attr_ns
26521 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26523 if (cxx_dialect < cxx17
26524 && !in_system_header_at (input_location))
26525 pedwarn (input_location, 0,
26526 "attribute using prefix only available "
26527 "with %<-std=c++17%> or %<-std=gnu++17%>");
26529 cp_lexer_consume_token (parser->lexer);
26530 cp_lexer_consume_token (parser->lexer);
26531 cp_lexer_consume_token (parser->lexer);
26533 else
26534 attr_ns = NULL_TREE;
26537 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26539 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26540 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26541 cp_parser_skip_to_end_of_statement (parser);
26542 else
26543 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26544 when we are sure that we have actually parsed them. */
26545 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26547 else
26549 tree alignas_expr;
26551 /* Look for an alignment-specifier. */
26553 token = cp_lexer_peek_token (parser->lexer);
26555 if (token->type != CPP_KEYWORD
26556 || token->keyword != RID_ALIGNAS)
26557 return NULL_TREE;
26559 cp_lexer_consume_token (parser->lexer);
26560 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26562 matching_parens parens;
26563 if (!parens.require_open (parser))
26564 return error_mark_node;
26566 cp_parser_parse_tentatively (parser);
26567 alignas_expr = cp_parser_type_id (parser);
26569 if (!cp_parser_parse_definitely (parser))
26571 alignas_expr = cp_parser_assignment_expression (parser);
26572 if (alignas_expr == error_mark_node)
26573 cp_parser_skip_to_end_of_statement (parser);
26574 if (alignas_expr == NULL_TREE
26575 || alignas_expr == error_mark_node)
26576 return alignas_expr;
26579 alignas_expr = cxx_alignas_expr (alignas_expr);
26580 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26582 /* Handle alignas (pack...). */
26583 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26585 cp_lexer_consume_token (parser->lexer);
26586 alignas_expr = make_pack_expansion (alignas_expr);
26589 /* Something went wrong, so don't build the attribute. */
26590 if (alignas_expr == error_mark_node)
26591 return error_mark_node;
26593 /* Missing ')' means the code cannot possibly be valid; go ahead
26594 and commit to make sure we issue a hard error. */
26595 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
26596 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26597 cp_parser_commit_to_tentative_parse (parser);
26599 if (!parens.require_close (parser))
26600 return error_mark_node;
26602 /* Build the C++-11 representation of an 'aligned'
26603 attribute. */
26604 attributes
26605 = build_tree_list (build_tree_list (gnu_identifier,
26606 aligned_identifier), alignas_expr);
26609 return attributes;
26612 /* Parse a standard C++-11 attribute-specifier-seq.
26614 attribute-specifier-seq:
26615 attribute-specifier-seq [opt] attribute-specifier
26618 static tree
26619 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26621 tree attr_specs = NULL_TREE;
26622 tree attr_last = NULL_TREE;
26624 /* Don't create wrapper nodes within attributes: the
26625 handlers don't know how to handle them. */
26626 auto_suppress_location_wrappers sentinel;
26628 while (true)
26630 tree attr_spec = cp_parser_std_attribute_spec (parser);
26631 if (attr_spec == NULL_TREE)
26632 break;
26633 if (attr_spec == error_mark_node)
26634 return error_mark_node;
26636 if (attr_last)
26637 TREE_CHAIN (attr_last) = attr_spec;
26638 else
26639 attr_specs = attr_last = attr_spec;
26640 attr_last = tree_last (attr_last);
26643 return attr_specs;
26646 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26647 return index of the first token after balanced-token, or N on failure. */
26649 static size_t
26650 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26652 size_t orig_n = n;
26653 int nparens = 0, nbraces = 0, nsquares = 0;
26655 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26657 case CPP_PRAGMA_EOL:
26658 if (!parser->lexer->in_pragma)
26659 break;
26660 /* FALLTHRU */
26661 case CPP_EOF:
26662 /* Ran out of tokens. */
26663 return orig_n;
26664 case CPP_OPEN_PAREN:
26665 ++nparens;
26666 break;
26667 case CPP_OPEN_BRACE:
26668 ++nbraces;
26669 break;
26670 case CPP_OPEN_SQUARE:
26671 ++nsquares;
26672 break;
26673 case CPP_CLOSE_PAREN:
26674 --nparens;
26675 break;
26676 case CPP_CLOSE_BRACE:
26677 --nbraces;
26678 break;
26679 case CPP_CLOSE_SQUARE:
26680 --nsquares;
26681 break;
26682 default:
26683 break;
26685 while (nparens || nbraces || nsquares);
26686 return n;
26689 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26690 return index of the first token after the GNU attribute tokens, or N on
26691 failure. */
26693 static size_t
26694 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26696 while (true)
26698 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26699 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26700 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26701 break;
26703 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26704 if (n2 == n + 2)
26705 break;
26706 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26707 break;
26708 n = n2 + 1;
26710 return n;
26713 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26714 next token), return index of the first token after the standard C++11
26715 attribute tokens, or N on failure. */
26717 static size_t
26718 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26720 while (true)
26722 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26723 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26725 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26726 if (n2 == n + 1)
26727 break;
26728 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26729 break;
26730 n = n2 + 1;
26732 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26733 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26735 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26736 if (n2 == n + 1)
26737 break;
26738 n = n2;
26740 else
26741 break;
26743 return n;
26746 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26747 as the next token), return index of the first token after the attribute
26748 tokens, or N on failure. */
26750 static size_t
26751 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26753 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26754 return cp_parser_skip_gnu_attributes_opt (parser, n);
26755 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26758 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26759 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26760 current value of the PEDANTIC flag, regardless of whether or not
26761 the `__extension__' keyword is present. The caller is responsible
26762 for restoring the value of the PEDANTIC flag. */
26764 static bool
26765 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26767 /* Save the old value of the PEDANTIC flag. */
26768 *saved_pedantic = pedantic;
26770 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26772 /* Consume the `__extension__' token. */
26773 cp_lexer_consume_token (parser->lexer);
26774 /* We're not being pedantic while the `__extension__' keyword is
26775 in effect. */
26776 pedantic = 0;
26778 return true;
26781 return false;
26784 /* Parse a label declaration.
26786 label-declaration:
26787 __label__ label-declarator-seq ;
26789 label-declarator-seq:
26790 identifier , label-declarator-seq
26791 identifier */
26793 static void
26794 cp_parser_label_declaration (cp_parser* parser)
26796 /* Look for the `__label__' keyword. */
26797 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26799 while (true)
26801 tree identifier;
26803 /* Look for an identifier. */
26804 identifier = cp_parser_identifier (parser);
26805 /* If we failed, stop. */
26806 if (identifier == error_mark_node)
26807 break;
26808 /* Declare it as a label. */
26809 finish_label_decl (identifier);
26810 /* If the next token is a `;', stop. */
26811 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26812 break;
26813 /* Look for the `,' separating the label declarations. */
26814 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26817 /* Look for the final `;'. */
26818 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26821 // -------------------------------------------------------------------------- //
26822 // Requires Clause
26824 // Parse a requires clause.
26826 // requires-clause:
26827 // 'requires' logical-or-expression
26829 // The required logical-or-expression must be a constant expression. Note
26830 // that we don't check that the expression is constepxr here. We defer until
26831 // we analyze constraints and then, we only check atomic constraints.
26832 static tree
26833 cp_parser_requires_clause (cp_parser *parser)
26835 // Parse the requires clause so that it is not automatically folded.
26836 ++processing_template_decl;
26837 tree expr = cp_parser_binary_expression (parser, false, false,
26838 PREC_NOT_OPERATOR, NULL);
26839 if (check_for_bare_parameter_packs (expr))
26840 expr = error_mark_node;
26841 --processing_template_decl;
26842 return expr;
26845 // Optionally parse a requires clause:
26846 static tree
26847 cp_parser_requires_clause_opt (cp_parser *parser)
26849 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26850 if (tok->keyword != RID_REQUIRES)
26852 if (!flag_concepts && tok->type == CPP_NAME
26853 && tok->u.value == ridpointers[RID_REQUIRES])
26855 error_at (cp_lexer_peek_token (parser->lexer)->location,
26856 "%<requires%> only available with %<-fconcepts%>");
26857 /* Parse and discard the requires-clause. */
26858 cp_lexer_consume_token (parser->lexer);
26859 cp_parser_requires_clause (parser);
26861 return NULL_TREE;
26863 cp_lexer_consume_token (parser->lexer);
26864 return cp_parser_requires_clause (parser);
26868 /*---------------------------------------------------------------------------
26869 Requires expressions
26870 ---------------------------------------------------------------------------*/
26872 /* Parse a requires expression
26874 requirement-expression:
26875 'requires' requirement-parameter-list [opt] requirement-body */
26876 static tree
26877 cp_parser_requires_expression (cp_parser *parser)
26879 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26880 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26882 /* A requires-expression shall appear only within a concept
26883 definition or a requires-clause.
26885 TODO: Implement this diagnostic correctly. */
26886 if (!processing_template_decl)
26888 error_at (loc, "a requires expression cannot appear outside a template");
26889 cp_parser_skip_to_end_of_statement (parser);
26890 return error_mark_node;
26893 tree parms, reqs;
26895 /* Local parameters are delared as variables within the scope
26896 of the expression. They are not visible past the end of
26897 the expression. Expressions within the requires-expression
26898 are unevaluated. */
26899 struct scope_sentinel
26901 scope_sentinel ()
26903 ++cp_unevaluated_operand;
26904 begin_scope (sk_block, NULL_TREE);
26907 ~scope_sentinel ()
26909 pop_bindings_and_leave_scope ();
26910 --cp_unevaluated_operand;
26912 } s;
26914 /* Parse the optional parameter list. */
26915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26917 parms = cp_parser_requirement_parameter_list (parser);
26918 if (parms == error_mark_node)
26919 return error_mark_node;
26921 else
26922 parms = NULL_TREE;
26924 /* Parse the requirement body. */
26925 reqs = cp_parser_requirement_body (parser);
26926 if (reqs == error_mark_node)
26927 return error_mark_node;
26930 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26931 the parm chain. */
26932 grokparms (parms, &parms);
26933 return finish_requires_expr (parms, reqs);
26936 /* Parse a parameterized requirement.
26938 requirement-parameter-list:
26939 '(' parameter-declaration-clause ')' */
26940 static tree
26941 cp_parser_requirement_parameter_list (cp_parser *parser)
26943 matching_parens parens;
26944 if (!parens.require_open (parser))
26945 return error_mark_node;
26947 tree parms
26948 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26950 if (!parens.require_close (parser))
26951 return error_mark_node;
26953 return parms;
26956 /* Parse the body of a requirement.
26958 requirement-body:
26959 '{' requirement-list '}' */
26960 static tree
26961 cp_parser_requirement_body (cp_parser *parser)
26963 matching_braces braces;
26964 if (!braces.require_open (parser))
26965 return error_mark_node;
26967 tree reqs = cp_parser_requirement_list (parser);
26969 if (!braces.require_close (parser))
26970 return error_mark_node;
26972 return reqs;
26975 /* Parse a list of requirements.
26977 requirement-list:
26978 requirement
26979 requirement-list ';' requirement[opt] */
26980 static tree
26981 cp_parser_requirement_list (cp_parser *parser)
26983 tree result = NULL_TREE;
26984 while (true)
26986 tree req = cp_parser_requirement (parser);
26987 if (req == error_mark_node)
26988 return error_mark_node;
26990 result = tree_cons (NULL_TREE, req, result);
26992 /* If we see a semi-colon, consume it. */
26993 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26994 cp_lexer_consume_token (parser->lexer);
26996 /* Stop processing at the end of the list. */
26997 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26998 break;
27001 /* Reverse the order of requirements so they are analyzed in
27002 declaration order. */
27003 return nreverse (result);
27006 /* Parse a syntactic requirement or type requirement.
27008 requirement:
27009 simple-requirement
27010 compound-requirement
27011 type-requirement
27012 nested-requirement */
27013 static tree
27014 cp_parser_requirement (cp_parser *parser)
27016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27017 return cp_parser_compound_requirement (parser);
27018 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27019 return cp_parser_type_requirement (parser);
27020 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
27021 return cp_parser_nested_requirement (parser);
27022 else
27023 return cp_parser_simple_requirement (parser);
27026 /* Parse a simple requirement.
27028 simple-requirement:
27029 expression ';' */
27030 static tree
27031 cp_parser_simple_requirement (cp_parser *parser)
27033 tree expr = cp_parser_expression (parser, NULL, false, false);
27034 if (!expr || expr == error_mark_node)
27035 return error_mark_node;
27037 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
27038 return error_mark_node;
27040 return finish_simple_requirement (expr);
27043 /* Parse a type requirement
27045 type-requirement
27046 nested-name-specifier [opt] required-type-name ';'
27048 required-type-name:
27049 type-name
27050 'template' [opt] simple-template-id */
27051 static tree
27052 cp_parser_type_requirement (cp_parser *parser)
27054 cp_lexer_consume_token (parser->lexer);
27056 // Save the scope before parsing name specifiers.
27057 tree saved_scope = parser->scope;
27058 tree saved_object_scope = parser->object_scope;
27059 tree saved_qualifying_scope = parser->qualifying_scope;
27060 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
27061 cp_parser_nested_name_specifier_opt (parser,
27062 /*typename_keyword_p=*/true,
27063 /*check_dependency_p=*/false,
27064 /*type_p=*/true,
27065 /*is_declaration=*/false);
27067 tree type;
27068 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27070 cp_lexer_consume_token (parser->lexer);
27071 type = cp_parser_template_id (parser,
27072 /*template_keyword_p=*/true,
27073 /*check_dependency=*/false,
27074 /*tag_type=*/none_type,
27075 /*is_declaration=*/false);
27076 type = make_typename_type (parser->scope, type, typename_type,
27077 /*complain=*/tf_error);
27079 else
27080 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
27082 if (TREE_CODE (type) == TYPE_DECL)
27083 type = TREE_TYPE (type);
27085 parser->scope = saved_scope;
27086 parser->object_scope = saved_object_scope;
27087 parser->qualifying_scope = saved_qualifying_scope;
27089 if (type == error_mark_node)
27090 cp_parser_skip_to_end_of_statement (parser);
27092 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
27093 return error_mark_node;
27094 if (type == error_mark_node)
27095 return error_mark_node;
27097 return finish_type_requirement (type);
27100 /* Parse a compound requirement
27102 compound-requirement:
27103 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
27104 static tree
27105 cp_parser_compound_requirement (cp_parser *parser)
27107 /* Parse an expression enclosed in '{ }'s. */
27108 matching_braces braces;
27109 if (!braces.require_open (parser))
27110 return error_mark_node;
27112 tree expr = cp_parser_expression (parser, NULL, false, false);
27113 if (!expr || expr == error_mark_node)
27114 return error_mark_node;
27116 if (!braces.require_close (parser))
27117 return error_mark_node;
27119 /* Parse the optional noexcept. */
27120 bool noexcept_p = false;
27121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
27123 cp_lexer_consume_token (parser->lexer);
27124 noexcept_p = true;
27127 /* Parse the optional trailing return type. */
27128 tree type = NULL_TREE;
27129 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
27131 cp_lexer_consume_token (parser->lexer);
27132 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
27133 parser->in_result_type_constraint_p = true;
27134 type = cp_parser_trailing_type_id (parser);
27135 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
27136 if (type == error_mark_node)
27137 return error_mark_node;
27140 return finish_compound_requirement (expr, type, noexcept_p);
27143 /* Parse a nested requirement. This is the same as a requires clause.
27145 nested-requirement:
27146 requires-clause */
27147 static tree
27148 cp_parser_nested_requirement (cp_parser *parser)
27150 cp_lexer_consume_token (parser->lexer);
27151 tree req = cp_parser_requires_clause (parser);
27152 if (req == error_mark_node)
27153 return error_mark_node;
27154 return finish_nested_requirement (req);
27157 /* Support Functions */
27159 /* Return the appropriate prefer_type argument for lookup_name_real based on
27160 tag_type and template_mem_access. */
27162 static inline int
27163 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
27165 /* DR 141: When looking in the current enclosing context for a template-name
27166 after -> or ., only consider class templates. */
27167 if (template_mem_access)
27168 return 2;
27169 switch (tag_type)
27171 case none_type: return 0; // No preference.
27172 case scope_type: return 1; // Type or namespace.
27173 default: return 2; // Type only.
27177 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
27178 NAME should have one of the representations used for an
27179 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
27180 is returned. If PARSER->SCOPE is a dependent type, then a
27181 SCOPE_REF is returned.
27183 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
27184 returned; the name was already resolved when the TEMPLATE_ID_EXPR
27185 was formed. Abstractly, such entities should not be passed to this
27186 function, because they do not need to be looked up, but it is
27187 simpler to check for this special case here, rather than at the
27188 call-sites.
27190 In cases not explicitly covered above, this function returns a
27191 DECL, OVERLOAD, or baselink representing the result of the lookup.
27192 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
27193 is returned.
27195 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
27196 (e.g., "struct") that was used. In that case bindings that do not
27197 refer to types are ignored.
27199 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
27200 ignored.
27202 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
27203 are ignored.
27205 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
27206 types.
27208 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
27209 TREE_LIST of candidates if name-lookup results in an ambiguity, and
27210 NULL_TREE otherwise. */
27212 static cp_expr
27213 cp_parser_lookup_name (cp_parser *parser, tree name,
27214 enum tag_types tag_type,
27215 bool is_template,
27216 bool is_namespace,
27217 bool check_dependency,
27218 tree *ambiguous_decls,
27219 location_t name_location)
27221 tree decl;
27222 tree object_type = parser->context->object_type;
27224 /* Assume that the lookup will be unambiguous. */
27225 if (ambiguous_decls)
27226 *ambiguous_decls = NULL_TREE;
27228 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27229 no longer valid. Note that if we are parsing tentatively, and
27230 the parse fails, OBJECT_TYPE will be automatically restored. */
27231 parser->context->object_type = NULL_TREE;
27233 if (name == error_mark_node)
27234 return error_mark_node;
27236 /* A template-id has already been resolved; there is no lookup to
27237 do. */
27238 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
27239 return name;
27240 if (BASELINK_P (name))
27242 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
27243 == TEMPLATE_ID_EXPR);
27244 return name;
27247 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27248 it should already have been checked to make sure that the name
27249 used matches the type being destroyed. */
27250 if (TREE_CODE (name) == BIT_NOT_EXPR)
27252 tree type;
27254 /* Figure out to which type this destructor applies. */
27255 if (parser->scope)
27256 type = parser->scope;
27257 else if (object_type)
27258 type = object_type;
27259 else
27260 type = current_class_type;
27261 /* If that's not a class type, there is no destructor. */
27262 if (!type || !CLASS_TYPE_P (type))
27263 return error_mark_node;
27265 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27266 lazily_declare_fn (sfk_destructor, type);
27268 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27269 return dtor;
27271 return error_mark_node;
27274 /* By this point, the NAME should be an ordinary identifier. If
27275 the id-expression was a qualified name, the qualifying scope is
27276 stored in PARSER->SCOPE at this point. */
27277 gcc_assert (identifier_p (name));
27279 /* Perform the lookup. */
27280 if (parser->scope)
27282 bool dependent_p;
27284 if (parser->scope == error_mark_node)
27285 return error_mark_node;
27287 /* If the SCOPE is dependent, the lookup must be deferred until
27288 the template is instantiated -- unless we are explicitly
27289 looking up names in uninstantiated templates. Even then, we
27290 cannot look up the name if the scope is not a class type; it
27291 might, for example, be a template type parameter. */
27292 dependent_p = (TYPE_P (parser->scope)
27293 && dependent_scope_p (parser->scope));
27294 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27295 && dependent_p)
27296 /* Defer lookup. */
27297 decl = error_mark_node;
27298 else
27300 tree pushed_scope = NULL_TREE;
27302 /* If PARSER->SCOPE is a dependent type, then it must be a
27303 class type, and we must not be checking dependencies;
27304 otherwise, we would have processed this lookup above. So
27305 that PARSER->SCOPE is not considered a dependent base by
27306 lookup_member, we must enter the scope here. */
27307 if (dependent_p)
27308 pushed_scope = push_scope (parser->scope);
27310 /* If the PARSER->SCOPE is a template specialization, it
27311 may be instantiated during name lookup. In that case,
27312 errors may be issued. Even if we rollback the current
27313 tentative parse, those errors are valid. */
27314 decl = lookup_qualified_name (parser->scope, name,
27315 prefer_type_arg (tag_type),
27316 /*complain=*/true);
27318 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27319 lookup result and the nested-name-specifier nominates a class C:
27320 * if the name specified after the nested-name-specifier, when
27321 looked up in C, is the injected-class-name of C (Clause 9), or
27322 * if the name specified after the nested-name-specifier is the
27323 same as the identifier or the simple-template-id's template-
27324 name in the last component of the nested-name-specifier,
27325 the name is instead considered to name the constructor of
27326 class C. [ Note: for example, the constructor is not an
27327 acceptable lookup result in an elaborated-type-specifier so
27328 the constructor would not be used in place of the
27329 injected-class-name. --end note ] Such a constructor name
27330 shall be used only in the declarator-id of a declaration that
27331 names a constructor or in a using-declaration. */
27332 if (tag_type == none_type
27333 && DECL_SELF_REFERENCE_P (decl)
27334 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27335 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27336 prefer_type_arg (tag_type),
27337 /*complain=*/true);
27339 /* If we have a single function from a using decl, pull it out. */
27340 if (TREE_CODE (decl) == OVERLOAD
27341 && !really_overloaded_fn (decl))
27342 decl = OVL_FUNCTION (decl);
27344 if (pushed_scope)
27345 pop_scope (pushed_scope);
27348 /* If the scope is a dependent type and either we deferred lookup or
27349 we did lookup but didn't find the name, rememeber the name. */
27350 if (decl == error_mark_node && TYPE_P (parser->scope)
27351 && dependent_type_p (parser->scope))
27353 if (tag_type)
27355 tree type;
27357 /* The resolution to Core Issue 180 says that `struct
27358 A::B' should be considered a type-name, even if `A'
27359 is dependent. */
27360 type = make_typename_type (parser->scope, name, tag_type,
27361 /*complain=*/tf_error);
27362 if (type != error_mark_node)
27363 decl = TYPE_NAME (type);
27365 else if (is_template
27366 && (cp_parser_next_token_ends_template_argument_p (parser)
27367 || cp_lexer_next_token_is (parser->lexer,
27368 CPP_CLOSE_PAREN)))
27369 decl = make_unbound_class_template (parser->scope,
27370 name, NULL_TREE,
27371 /*complain=*/tf_error);
27372 else
27373 decl = build_qualified_name (/*type=*/NULL_TREE,
27374 parser->scope, name,
27375 is_template);
27377 parser->qualifying_scope = parser->scope;
27378 parser->object_scope = NULL_TREE;
27380 else if (object_type)
27382 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27383 OBJECT_TYPE is not a class. */
27384 if (CLASS_TYPE_P (object_type))
27385 /* If the OBJECT_TYPE is a template specialization, it may
27386 be instantiated during name lookup. In that case, errors
27387 may be issued. Even if we rollback the current tentative
27388 parse, those errors are valid. */
27389 decl = lookup_member (object_type,
27390 name,
27391 /*protect=*/0,
27392 prefer_type_arg (tag_type),
27393 tf_warning_or_error);
27394 else
27395 decl = NULL_TREE;
27397 if (!decl)
27398 /* Look it up in the enclosing context. DR 141: When looking for a
27399 template-name after -> or ., only consider class templates. */
27400 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27401 /*nonclass=*/0,
27402 /*block_p=*/true, is_namespace, 0);
27403 if (object_type == unknown_type_node)
27404 /* The object is type-dependent, so we can't look anything up; we used
27405 this to get the DR 141 behavior. */
27406 object_type = NULL_TREE;
27407 parser->object_scope = object_type;
27408 parser->qualifying_scope = NULL_TREE;
27410 else
27412 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27413 /*nonclass=*/0,
27414 /*block_p=*/true, is_namespace, 0);
27415 parser->qualifying_scope = NULL_TREE;
27416 parser->object_scope = NULL_TREE;
27419 /* If the lookup failed, let our caller know. */
27420 if (!decl || decl == error_mark_node)
27421 return error_mark_node;
27423 /* Pull out the template from an injected-class-name (or multiple). */
27424 if (is_template)
27425 decl = maybe_get_template_decl_from_type_decl (decl);
27427 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27428 if (TREE_CODE (decl) == TREE_LIST)
27430 if (ambiguous_decls)
27431 *ambiguous_decls = decl;
27432 /* The error message we have to print is too complicated for
27433 cp_parser_error, so we incorporate its actions directly. */
27434 if (!cp_parser_simulate_error (parser))
27436 error_at (name_location, "reference to %qD is ambiguous",
27437 name);
27438 print_candidates (decl);
27440 return error_mark_node;
27443 gcc_assert (DECL_P (decl)
27444 || TREE_CODE (decl) == OVERLOAD
27445 || TREE_CODE (decl) == SCOPE_REF
27446 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27447 || BASELINK_P (decl));
27449 /* If we have resolved the name of a member declaration, check to
27450 see if the declaration is accessible. When the name resolves to
27451 set of overloaded functions, accessibility is checked when
27452 overload resolution is done.
27454 During an explicit instantiation, access is not checked at all,
27455 as per [temp.explicit]. */
27456 if (DECL_P (decl))
27457 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27459 maybe_record_typedef_use (decl);
27461 return cp_expr (decl, name_location);
27464 /* Like cp_parser_lookup_name, but for use in the typical case where
27465 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27466 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27468 static tree
27469 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27471 return cp_parser_lookup_name (parser, name,
27472 none_type,
27473 /*is_template=*/false,
27474 /*is_namespace=*/false,
27475 /*check_dependency=*/true,
27476 /*ambiguous_decls=*/NULL,
27477 location);
27480 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27481 the current context, return the TYPE_DECL. If TAG_NAME_P is
27482 true, the DECL indicates the class being defined in a class-head,
27483 or declared in an elaborated-type-specifier.
27485 Otherwise, return DECL. */
27487 static tree
27488 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27490 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27491 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27493 struct A {
27494 template <typename T> struct B;
27497 template <typename T> struct A::B {};
27499 Similarly, in an elaborated-type-specifier:
27501 namespace N { struct X{}; }
27503 struct A {
27504 template <typename T> friend struct N::X;
27507 However, if the DECL refers to a class type, and we are in
27508 the scope of the class, then the name lookup automatically
27509 finds the TYPE_DECL created by build_self_reference rather
27510 than a TEMPLATE_DECL. For example, in:
27512 template <class T> struct S {
27513 S s;
27516 there is no need to handle such case. */
27518 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27519 return DECL_TEMPLATE_RESULT (decl);
27521 return decl;
27524 /* If too many, or too few, template-parameter lists apply to the
27525 declarator, issue an error message. Returns TRUE if all went well,
27526 and FALSE otherwise. */
27528 static bool
27529 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27530 cp_declarator *declarator,
27531 location_t declarator_location)
27533 switch (declarator->kind)
27535 case cdk_id:
27537 unsigned num_templates = 0;
27538 tree scope = declarator->u.id.qualifying_scope;
27539 bool template_id_p = false;
27541 if (scope)
27542 num_templates = num_template_headers_for_class (scope);
27543 else if (TREE_CODE (declarator->u.id.unqualified_name)
27544 == TEMPLATE_ID_EXPR)
27546 /* If the DECLARATOR has the form `X<y>' then it uses one
27547 additional level of template parameters. */
27548 ++num_templates;
27549 template_id_p = true;
27552 return cp_parser_check_template_parameters
27553 (parser, num_templates, template_id_p, declarator_location,
27554 declarator);
27557 case cdk_function:
27558 case cdk_array:
27559 case cdk_pointer:
27560 case cdk_reference:
27561 case cdk_ptrmem:
27562 return (cp_parser_check_declarator_template_parameters
27563 (parser, declarator->declarator, declarator_location));
27565 case cdk_decomp:
27566 case cdk_error:
27567 return true;
27569 default:
27570 gcc_unreachable ();
27572 return false;
27575 /* NUM_TEMPLATES were used in the current declaration. If that is
27576 invalid, return FALSE and issue an error messages. Otherwise,
27577 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27578 declarator and we can print more accurate diagnostics. */
27580 static bool
27581 cp_parser_check_template_parameters (cp_parser* parser,
27582 unsigned num_templates,
27583 bool template_id_p,
27584 location_t location,
27585 cp_declarator *declarator)
27587 /* If there are the same number of template classes and parameter
27588 lists, that's OK. */
27589 if (parser->num_template_parameter_lists == num_templates)
27590 return true;
27591 /* If there are more, but only one more, and the name ends in an identifier,
27592 then we are declaring a primary template. That's OK too. */
27593 if (!template_id_p
27594 && parser->num_template_parameter_lists == num_templates + 1)
27595 return true;
27596 /* If there are more template classes than parameter lists, we have
27597 something like:
27599 template <class T> void S<T>::R<T>::f (); */
27600 if (parser->num_template_parameter_lists < num_templates)
27602 if (declarator && !current_function_decl)
27603 error_at (location, "specializing member %<%T::%E%> "
27604 "requires %<template<>%> syntax",
27605 declarator->u.id.qualifying_scope,
27606 declarator->u.id.unqualified_name);
27607 else if (declarator)
27608 error_at (location, "invalid declaration of %<%T::%E%>",
27609 declarator->u.id.qualifying_scope,
27610 declarator->u.id.unqualified_name);
27611 else
27612 error_at (location, "too few template-parameter-lists");
27613 return false;
27615 /* Otherwise, there are too many template parameter lists. We have
27616 something like:
27618 template <class T> template <class U> void S::f(); */
27619 error_at (location, "too many template-parameter-lists");
27620 return false;
27623 /* Parse an optional `::' token indicating that the following name is
27624 from the global namespace. If so, PARSER->SCOPE is set to the
27625 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27626 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27627 Returns the new value of PARSER->SCOPE, if the `::' token is
27628 present, and NULL_TREE otherwise. */
27630 static tree
27631 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27633 cp_token *token;
27635 /* Peek at the next token. */
27636 token = cp_lexer_peek_token (parser->lexer);
27637 /* If we're looking at a `::' token then we're starting from the
27638 global namespace, not our current location. */
27639 if (token->type == CPP_SCOPE)
27641 /* Consume the `::' token. */
27642 cp_lexer_consume_token (parser->lexer);
27643 /* Set the SCOPE so that we know where to start the lookup. */
27644 parser->scope = global_namespace;
27645 parser->qualifying_scope = global_namespace;
27646 parser->object_scope = NULL_TREE;
27648 return parser->scope;
27650 else if (!current_scope_valid_p)
27652 parser->scope = NULL_TREE;
27653 parser->qualifying_scope = NULL_TREE;
27654 parser->object_scope = NULL_TREE;
27657 return NULL_TREE;
27660 /* Returns TRUE if the upcoming token sequence is the start of a
27661 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27662 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27663 is used to control type-specifier parsing. */
27665 static bool
27666 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
27667 bool friend_p)
27669 bool constructor_p;
27670 bool outside_class_specifier_p;
27671 tree nested_name_specifier;
27672 cp_token *next_token;
27674 /* The common case is that this is not a constructor declarator, so
27675 try to avoid doing lots of work if at all possible. It's not
27676 valid declare a constructor at function scope. */
27677 if (parser->in_function_body)
27678 return false;
27679 /* And only certain tokens can begin a constructor declarator. */
27680 next_token = cp_lexer_peek_token (parser->lexer);
27681 if (next_token->type != CPP_NAME
27682 && next_token->type != CPP_SCOPE
27683 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27684 && next_token->type != CPP_TEMPLATE_ID)
27685 return false;
27687 /* Parse tentatively; we are going to roll back all of the tokens
27688 consumed here. */
27689 cp_parser_parse_tentatively (parser);
27690 /* Assume that we are looking at a constructor declarator. */
27691 constructor_p = true;
27693 /* Look for the optional `::' operator. */
27694 cp_parser_global_scope_opt (parser,
27695 /*current_scope_valid_p=*/false);
27696 /* Look for the nested-name-specifier. */
27697 nested_name_specifier
27698 = (cp_parser_nested_name_specifier_opt (parser,
27699 /*typename_keyword_p=*/false,
27700 /*check_dependency_p=*/false,
27701 /*type_p=*/false,
27702 /*is_declaration=*/false));
27704 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27705 if (nested_name_specifier
27706 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27708 tree s = resolve_typename_type (nested_name_specifier,
27709 /*only_current_p=*/false);
27710 if (TREE_CODE (s) != TYPENAME_TYPE)
27711 nested_name_specifier = s;
27714 outside_class_specifier_p = (!at_class_scope_p ()
27715 || !TYPE_BEING_DEFINED (current_class_type)
27716 || friend_p);
27718 /* Outside of a class-specifier, there must be a
27719 nested-name-specifier. Except in C++17 mode, where we
27720 might be declaring a guiding declaration. */
27721 if (!nested_name_specifier && outside_class_specifier_p
27722 && cxx_dialect < cxx17)
27723 constructor_p = false;
27724 else if (nested_name_specifier == error_mark_node)
27725 constructor_p = false;
27727 /* If we have a class scope, this is easy; DR 147 says that S::S always
27728 names the constructor, and no other qualified name could. */
27729 if (constructor_p && nested_name_specifier
27730 && CLASS_TYPE_P (nested_name_specifier))
27732 tree id = cp_parser_unqualified_id (parser,
27733 /*template_keyword_p=*/false,
27734 /*check_dependency_p=*/false,
27735 /*declarator_p=*/true,
27736 /*optional_p=*/false);
27737 if (is_overloaded_fn (id))
27738 id = DECL_NAME (get_first_fn (id));
27739 if (!constructor_name_p (id, nested_name_specifier))
27740 constructor_p = false;
27742 /* If we still think that this might be a constructor-declarator,
27743 look for a class-name. */
27744 else if (constructor_p)
27746 /* If we have:
27748 template <typename T> struct S {
27749 S();
27752 we must recognize that the nested `S' names a class. */
27753 if (cxx_dialect >= cxx17)
27754 cp_parser_parse_tentatively (parser);
27756 tree type_decl;
27757 type_decl = cp_parser_class_name (parser,
27758 /*typename_keyword_p=*/false,
27759 /*template_keyword_p=*/false,
27760 none_type,
27761 /*check_dependency_p=*/false,
27762 /*class_head_p=*/false,
27763 /*is_declaration=*/false);
27765 if (cxx_dialect >= cxx17
27766 && !cp_parser_parse_definitely (parser))
27768 type_decl = NULL_TREE;
27769 tree tmpl = cp_parser_template_name (parser,
27770 /*template_keyword*/false,
27771 /*check_dependency_p*/false,
27772 /*is_declaration*/false,
27773 none_type,
27774 /*is_identifier*/NULL);
27775 if (DECL_CLASS_TEMPLATE_P (tmpl)
27776 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27777 /* It's a deduction guide, return true. */;
27778 else
27779 cp_parser_simulate_error (parser);
27782 /* If there was no class-name, then this is not a constructor.
27783 Otherwise, if we are in a class-specifier and we aren't
27784 handling a friend declaration, check that its type matches
27785 current_class_type (c++/38313). Note: error_mark_node
27786 is left alone for error recovery purposes. */
27787 constructor_p = (!cp_parser_error_occurred (parser)
27788 && (outside_class_specifier_p
27789 || type_decl == NULL_TREE
27790 || type_decl == error_mark_node
27791 || same_type_p (current_class_type,
27792 TREE_TYPE (type_decl))));
27794 /* If we're still considering a constructor, we have to see a `(',
27795 to begin the parameter-declaration-clause, followed by either a
27796 `)', an `...', or a decl-specifier. We need to check for a
27797 type-specifier to avoid being fooled into thinking that:
27799 S (f) (int);
27801 is a constructor. (It is actually a function named `f' that
27802 takes one parameter (of type `int') and returns a value of type
27803 `S'. */
27804 if (constructor_p
27805 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27806 constructor_p = false;
27808 if (constructor_p
27809 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27810 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27811 /* A parameter declaration begins with a decl-specifier,
27812 which is either the "attribute" keyword, a storage class
27813 specifier, or (usually) a type-specifier. */
27814 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
27815 /* A parameter declaration can also begin with [[attribute]]. */
27816 && !cp_next_tokens_can_be_std_attribute_p (parser))
27818 tree type;
27819 tree pushed_scope = NULL_TREE;
27820 unsigned saved_num_template_parameter_lists;
27822 /* Names appearing in the type-specifier should be looked up
27823 in the scope of the class. */
27824 if (current_class_type)
27825 type = NULL_TREE;
27826 else if (type_decl)
27828 type = TREE_TYPE (type_decl);
27829 if (TREE_CODE (type) == TYPENAME_TYPE)
27831 type = resolve_typename_type (type,
27832 /*only_current_p=*/false);
27833 if (TREE_CODE (type) == TYPENAME_TYPE)
27835 cp_parser_abort_tentative_parse (parser);
27836 return false;
27839 pushed_scope = push_scope (type);
27842 /* Inside the constructor parameter list, surrounding
27843 template-parameter-lists do not apply. */
27844 saved_num_template_parameter_lists
27845 = parser->num_template_parameter_lists;
27846 parser->num_template_parameter_lists = 0;
27848 /* Look for the type-specifier. It's not optional, but its typename
27849 might be. Unless this is a friend declaration; we don't want to
27850 treat
27852 friend S (T::fn)(int);
27854 as a constructor, but with P0634, we might assume a type when
27855 looking for the type-specifier. It is actually a function named
27856 `T::fn' that takes one parameter (of type `int') and returns a
27857 value of type `S'. Constructors can be friends, but they must
27858 use a qualified name. */
27859 cp_parser_type_specifier (parser,
27860 (friend_p ? CP_PARSER_FLAGS_NONE
27861 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
27862 /*decl_specs=*/NULL,
27863 /*is_declarator=*/true,
27864 /*declares_class_or_enum=*/NULL,
27865 /*is_cv_qualifier=*/NULL);
27867 parser->num_template_parameter_lists
27868 = saved_num_template_parameter_lists;
27870 /* Leave the scope of the class. */
27871 if (pushed_scope)
27872 pop_scope (pushed_scope);
27874 constructor_p = !cp_parser_error_occurred (parser);
27878 /* We did not really want to consume any tokens. */
27879 cp_parser_abort_tentative_parse (parser);
27881 return constructor_p;
27884 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27885 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27886 they must be performed once we are in the scope of the function.
27888 Returns the function defined. */
27890 static tree
27891 cp_parser_function_definition_from_specifiers_and_declarator
27892 (cp_parser* parser,
27893 cp_decl_specifier_seq *decl_specifiers,
27894 tree attributes,
27895 const cp_declarator *declarator)
27897 tree fn;
27898 bool success_p;
27900 /* Begin the function-definition. */
27901 success_p = start_function (decl_specifiers, declarator, attributes);
27903 /* The things we're about to see are not directly qualified by any
27904 template headers we've seen thus far. */
27905 reset_specialization ();
27907 /* If there were names looked up in the decl-specifier-seq that we
27908 did not check, check them now. We must wait until we are in the
27909 scope of the function to perform the checks, since the function
27910 might be a friend. */
27911 perform_deferred_access_checks (tf_warning_or_error);
27913 if (success_p)
27915 cp_finalize_omp_declare_simd (parser, current_function_decl);
27916 parser->omp_declare_simd = NULL;
27917 cp_finalize_oacc_routine (parser, current_function_decl, true);
27918 parser->oacc_routine = NULL;
27921 if (!success_p)
27923 /* Skip the entire function. */
27924 cp_parser_skip_to_end_of_block_or_statement (parser);
27925 fn = error_mark_node;
27927 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27929 /* Seen already, skip it. An error message has already been output. */
27930 cp_parser_skip_to_end_of_block_or_statement (parser);
27931 fn = current_function_decl;
27932 current_function_decl = NULL_TREE;
27933 /* If this is a function from a class, pop the nested class. */
27934 if (current_class_name)
27935 pop_nested_class ();
27937 else
27939 timevar_id_t tv;
27940 if (DECL_DECLARED_INLINE_P (current_function_decl))
27941 tv = TV_PARSE_INLINE;
27942 else
27943 tv = TV_PARSE_FUNC;
27944 timevar_push (tv);
27945 fn = cp_parser_function_definition_after_declarator (parser,
27946 /*inline_p=*/false);
27947 timevar_pop (tv);
27950 return fn;
27953 /* Parse the part of a function-definition that follows the
27954 declarator. INLINE_P is TRUE iff this function is an inline
27955 function defined within a class-specifier.
27957 Returns the function defined. */
27959 static tree
27960 cp_parser_function_definition_after_declarator (cp_parser* parser,
27961 bool inline_p)
27963 tree fn;
27964 bool saved_in_unbraced_linkage_specification_p;
27965 bool saved_in_function_body;
27966 unsigned saved_num_template_parameter_lists;
27967 cp_token *token;
27968 bool fully_implicit_function_template_p
27969 = parser->fully_implicit_function_template_p;
27970 parser->fully_implicit_function_template_p = false;
27971 tree implicit_template_parms
27972 = parser->implicit_template_parms;
27973 parser->implicit_template_parms = 0;
27974 cp_binding_level* implicit_template_scope
27975 = parser->implicit_template_scope;
27976 parser->implicit_template_scope = 0;
27978 saved_in_function_body = parser->in_function_body;
27979 parser->in_function_body = true;
27980 /* If the next token is `return', then the code may be trying to
27981 make use of the "named return value" extension that G++ used to
27982 support. */
27983 token = cp_lexer_peek_token (parser->lexer);
27984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27986 /* Consume the `return' keyword. */
27987 cp_lexer_consume_token (parser->lexer);
27988 /* Look for the identifier that indicates what value is to be
27989 returned. */
27990 cp_parser_identifier (parser);
27991 /* Issue an error message. */
27992 error_at (token->location,
27993 "named return values are no longer supported");
27994 /* Skip tokens until we reach the start of the function body. */
27995 while (true)
27997 cp_token *token = cp_lexer_peek_token (parser->lexer);
27998 if (token->type == CPP_OPEN_BRACE
27999 || token->type == CPP_EOF
28000 || token->type == CPP_PRAGMA_EOL)
28001 break;
28002 cp_lexer_consume_token (parser->lexer);
28005 /* The `extern' in `extern "C" void f () { ... }' does not apply to
28006 anything declared inside `f'. */
28007 saved_in_unbraced_linkage_specification_p
28008 = parser->in_unbraced_linkage_specification_p;
28009 parser->in_unbraced_linkage_specification_p = false;
28010 /* Inside the function, surrounding template-parameter-lists do not
28011 apply. */
28012 saved_num_template_parameter_lists
28013 = parser->num_template_parameter_lists;
28014 parser->num_template_parameter_lists = 0;
28016 /* If the next token is `try', `__transaction_atomic', or
28017 `__transaction_relaxed`, then we are looking at either function-try-block
28018 or function-transaction-block. Note that all of these include the
28019 function-body. */
28020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
28021 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
28022 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28023 RID_TRANSACTION_RELAXED))
28024 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
28025 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28026 cp_parser_function_try_block (parser);
28027 else
28028 cp_parser_ctor_initializer_opt_and_function_body
28029 (parser, /*in_function_try_block=*/false);
28031 /* Finish the function. */
28032 fn = finish_function (inline_p);
28033 /* Generate code for it, if necessary. */
28034 expand_or_defer_fn (fn);
28035 /* Restore the saved values. */
28036 parser->in_unbraced_linkage_specification_p
28037 = saved_in_unbraced_linkage_specification_p;
28038 parser->num_template_parameter_lists
28039 = saved_num_template_parameter_lists;
28040 parser->in_function_body = saved_in_function_body;
28042 parser->fully_implicit_function_template_p
28043 = fully_implicit_function_template_p;
28044 parser->implicit_template_parms
28045 = implicit_template_parms;
28046 parser->implicit_template_scope
28047 = implicit_template_scope;
28049 if (parser->fully_implicit_function_template_p)
28050 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
28052 return fn;
28055 /* Parse a template-declaration body (following argument list). */
28057 static void
28058 cp_parser_template_declaration_after_parameters (cp_parser* parser,
28059 tree parameter_list,
28060 bool member_p)
28062 tree decl = NULL_TREE;
28063 bool friend_p = false;
28065 /* We just processed one more parameter list. */
28066 ++parser->num_template_parameter_lists;
28068 /* Get the deferred access checks from the parameter list. These
28069 will be checked once we know what is being declared, as for a
28070 member template the checks must be performed in the scope of the
28071 class containing the member. */
28072 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
28074 /* Tentatively parse for a new template parameter list, which can either be
28075 the template keyword or a template introduction. */
28076 if (cp_parser_template_declaration_after_export (parser, member_p))
28077 /* OK */;
28078 else if (cxx_dialect >= cxx11
28079 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
28080 decl = cp_parser_alias_declaration (parser);
28081 else
28083 /* There are no access checks when parsing a template, as we do not
28084 know if a specialization will be a friend. */
28085 push_deferring_access_checks (dk_no_check);
28086 cp_token *token = cp_lexer_peek_token (parser->lexer);
28087 decl = cp_parser_single_declaration (parser,
28088 checks,
28089 member_p,
28090 /*explicit_specialization_p=*/false,
28091 &friend_p);
28092 pop_deferring_access_checks ();
28094 /* If this is a member template declaration, let the front
28095 end know. */
28096 if (member_p && !friend_p && decl)
28098 if (TREE_CODE (decl) == TYPE_DECL)
28099 cp_parser_check_access_in_redeclaration (decl, token->location);
28101 decl = finish_member_template_decl (decl);
28103 else if (friend_p && decl
28104 && DECL_DECLARES_TYPE_P (decl))
28105 make_friend_class (current_class_type, TREE_TYPE (decl),
28106 /*complain=*/true);
28108 /* We are done with the current parameter list. */
28109 --parser->num_template_parameter_lists;
28111 pop_deferring_access_checks ();
28113 /* Finish up. */
28114 finish_template_decl (parameter_list);
28116 /* Check the template arguments for a literal operator template. */
28117 if (decl
28118 && DECL_DECLARES_FUNCTION_P (decl)
28119 && UDLIT_OPER_P (DECL_NAME (decl)))
28121 bool ok = true;
28122 if (parameter_list == NULL_TREE)
28123 ok = false;
28124 else
28126 int num_parms = TREE_VEC_LENGTH (parameter_list);
28127 if (num_parms == 1)
28129 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
28130 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
28131 if (TREE_CODE (parm) != PARM_DECL)
28132 ok = false;
28133 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
28134 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28135 /* OK, C++20 string literal operator template. We don't need
28136 to warn in lower dialects here because we will have already
28137 warned about the template parameter. */;
28138 else if (TREE_TYPE (parm) != char_type_node
28139 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28140 ok = false;
28142 else if (num_parms == 2 && cxx_dialect >= cxx14)
28144 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
28145 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
28146 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
28147 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
28148 if (TREE_CODE (parm) != PARM_DECL
28149 || TREE_TYPE (parm) != TREE_TYPE (type)
28150 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28151 ok = false;
28152 else
28153 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
28154 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
28155 "ISO C++ did not adopt string literal operator templa"
28156 "tes taking an argument pack of characters");
28158 else
28159 ok = false;
28161 if (!ok)
28163 if (cxx_dialect > cxx17)
28164 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
28165 "template %qD has invalid parameter list; expected "
28166 "non-type template parameter pack %<<char...>%> or "
28167 "single non-type parameter of class type",
28168 decl);
28169 else
28170 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
28171 "template %qD has invalid parameter list; expected "
28172 "non-type template parameter pack %<<char...>%>",
28173 decl);
28177 /* Register member declarations. */
28178 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
28179 finish_member_declaration (decl);
28180 /* If DECL is a function template, we must return to parse it later.
28181 (Even though there is no definition, there might be default
28182 arguments that need handling.) */
28183 if (member_p && decl
28184 && DECL_DECLARES_FUNCTION_P (decl))
28185 vec_safe_push (unparsed_funs_with_definitions, decl);
28188 /* Parse a template introduction header for a template-declaration. Returns
28189 false if tentative parse fails. */
28191 static bool
28192 cp_parser_template_introduction (cp_parser* parser, bool member_p)
28194 cp_parser_parse_tentatively (parser);
28196 tree saved_scope = parser->scope;
28197 tree saved_object_scope = parser->object_scope;
28198 tree saved_qualifying_scope = parser->qualifying_scope;
28200 /* Look for the optional `::' operator. */
28201 cp_parser_global_scope_opt (parser,
28202 /*current_scope_valid_p=*/false);
28203 /* Look for the nested-name-specifier. */
28204 cp_parser_nested_name_specifier_opt (parser,
28205 /*typename_keyword_p=*/false,
28206 /*check_dependency_p=*/true,
28207 /*type_p=*/false,
28208 /*is_declaration=*/false);
28210 cp_token *token = cp_lexer_peek_token (parser->lexer);
28211 tree concept_name = cp_parser_identifier (parser);
28213 /* Look up the concept for which we will be matching
28214 template parameters. */
28215 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
28216 token->location);
28217 parser->scope = saved_scope;
28218 parser->object_scope = saved_object_scope;
28219 parser->qualifying_scope = saved_qualifying_scope;
28221 if (concept_name == error_mark_node)
28222 cp_parser_simulate_error (parser);
28224 /* Look for opening brace for introduction. */
28225 matching_braces braces;
28226 braces.require_open (parser);
28228 if (!cp_parser_parse_definitely (parser))
28229 return false;
28231 push_deferring_access_checks (dk_deferred);
28233 /* Build vector of placeholder parameters and grab
28234 matching identifiers. */
28235 tree introduction_list = cp_parser_introduction_list (parser);
28237 /* Look for closing brace for introduction. */
28238 if (!braces.require_close (parser))
28239 return true;
28241 /* The introduction-list shall not be empty. */
28242 int nargs = TREE_VEC_LENGTH (introduction_list);
28243 if (nargs == 0)
28245 /* In cp_parser_introduction_list we have already issued an error. */
28246 return true;
28249 if (tmpl_decl == error_mark_node)
28251 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
28252 token->location);
28253 return true;
28256 /* Build and associate the constraint. */
28257 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
28258 if (parms && parms != error_mark_node)
28260 cp_parser_template_declaration_after_parameters (parser, parms,
28261 member_p);
28262 return true;
28265 error_at (token->location, "no matching concept for template-introduction");
28266 return true;
28269 /* Parse a normal template-declaration following the template keyword. */
28271 static void
28272 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28274 tree parameter_list;
28275 bool need_lang_pop;
28276 location_t location = input_location;
28278 /* Look for the `<' token. */
28279 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28280 return;
28281 if (at_class_scope_p () && current_function_decl)
28283 /* 14.5.2.2 [temp.mem]
28285 A local class shall not have member templates. */
28286 error_at (location,
28287 "invalid declaration of member template in local class");
28288 cp_parser_skip_to_end_of_block_or_statement (parser);
28289 return;
28291 /* [temp]
28293 A template ... shall not have C linkage. */
28294 if (current_lang_name == lang_name_c)
28296 error_at (location, "template with C linkage");
28297 maybe_show_extern_c_location ();
28298 /* Give it C++ linkage to avoid confusing other parts of the
28299 front end. */
28300 push_lang_context (lang_name_cplusplus);
28301 need_lang_pop = true;
28303 else
28304 need_lang_pop = false;
28306 /* We cannot perform access checks on the template parameter
28307 declarations until we know what is being declared, just as we
28308 cannot check the decl-specifier list. */
28309 push_deferring_access_checks (dk_deferred);
28311 /* If the next token is `>', then we have an invalid
28312 specialization. Rather than complain about an invalid template
28313 parameter, issue an error message here. */
28314 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28316 cp_parser_error (parser, "invalid explicit specialization");
28317 begin_specialization ();
28318 parameter_list = NULL_TREE;
28320 else
28322 /* Parse the template parameters. */
28323 parameter_list = cp_parser_template_parameter_list (parser);
28326 /* Look for the `>'. */
28327 cp_parser_skip_to_end_of_template_parameter_list (parser);
28329 /* Manage template requirements */
28330 if (flag_concepts)
28332 tree reqs = get_shorthand_constraints (current_template_parms);
28333 if (tree r = cp_parser_requires_clause_opt (parser))
28334 reqs = conjoin_constraints (reqs, normalize_expression (r));
28335 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
28338 cp_parser_template_declaration_after_parameters (parser, parameter_list,
28339 member_p);
28341 /* For the erroneous case of a template with C linkage, we pushed an
28342 implicit C++ linkage scope; exit that scope now. */
28343 if (need_lang_pop)
28344 pop_lang_context ();
28347 /* Parse a template-declaration, assuming that the `export' (and
28348 `extern') keywords, if present, has already been scanned. MEMBER_P
28349 is as for cp_parser_template_declaration. */
28351 static bool
28352 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28354 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28356 cp_lexer_consume_token (parser->lexer);
28357 cp_parser_explicit_template_declaration (parser, member_p);
28358 return true;
28360 else if (flag_concepts)
28361 return cp_parser_template_introduction (parser, member_p);
28363 return false;
28366 /* Perform the deferred access checks from a template-parameter-list.
28367 CHECKS is a TREE_LIST of access checks, as returned by
28368 get_deferred_access_checks. */
28370 static void
28371 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28373 ++processing_template_parmlist;
28374 perform_access_checks (checks, tf_warning_or_error);
28375 --processing_template_parmlist;
28378 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28379 `function-definition' sequence that follows a template header.
28380 If MEMBER_P is true, this declaration appears in a class scope.
28382 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28383 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28385 static tree
28386 cp_parser_single_declaration (cp_parser* parser,
28387 vec<deferred_access_check, va_gc> *checks,
28388 bool member_p,
28389 bool explicit_specialization_p,
28390 bool* friend_p)
28392 int declares_class_or_enum;
28393 tree decl = NULL_TREE;
28394 cp_decl_specifier_seq decl_specifiers;
28395 bool function_definition_p = false;
28396 cp_token *decl_spec_token_start;
28398 /* This function is only used when processing a template
28399 declaration. */
28400 gcc_assert (innermost_scope_kind () == sk_template_parms
28401 || innermost_scope_kind () == sk_template_spec);
28403 /* Defer access checks until we know what is being declared. */
28404 push_deferring_access_checks (dk_deferred);
28406 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28407 alternative. */
28408 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28409 cp_parser_decl_specifier_seq (parser,
28410 (CP_PARSER_FLAGS_OPTIONAL
28411 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28412 &decl_specifiers,
28413 &declares_class_or_enum);
28414 if (friend_p)
28415 *friend_p = cp_parser_friend_p (&decl_specifiers);
28417 /* There are no template typedefs. */
28418 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28420 error_at (decl_spec_token_start->location,
28421 "template declaration of %<typedef%>");
28422 decl = error_mark_node;
28425 /* Gather up the access checks that occurred the
28426 decl-specifier-seq. */
28427 stop_deferring_access_checks ();
28429 /* Check for the declaration of a template class. */
28430 if (declares_class_or_enum)
28432 if (cp_parser_declares_only_class_p (parser)
28433 || (declares_class_or_enum & 2))
28435 // If this is a declaration, but not a definition, associate
28436 // any constraints with the type declaration. Constraints
28437 // are associated with definitions in cp_parser_class_specifier.
28438 if (declares_class_or_enum == 1)
28439 associate_classtype_constraints (decl_specifiers.type);
28441 decl = shadow_tag (&decl_specifiers);
28443 /* In this case:
28445 struct C {
28446 friend template <typename T> struct A<T>::B;
28449 A<T>::B will be represented by a TYPENAME_TYPE, and
28450 therefore not recognized by shadow_tag. */
28451 if (friend_p && *friend_p
28452 && !decl
28453 && decl_specifiers.type
28454 && TYPE_P (decl_specifiers.type))
28455 decl = decl_specifiers.type;
28457 if (decl && decl != error_mark_node)
28458 decl = TYPE_NAME (decl);
28459 else
28460 decl = error_mark_node;
28462 /* Perform access checks for template parameters. */
28463 cp_parser_perform_template_parameter_access_checks (checks);
28465 /* Give a helpful diagnostic for
28466 template <class T> struct A { } a;
28467 if we aren't already recovering from an error. */
28468 if (!cp_parser_declares_only_class_p (parser)
28469 && !seen_error ())
28471 error_at (cp_lexer_peek_token (parser->lexer)->location,
28472 "a class template declaration must not declare "
28473 "anything else");
28474 cp_parser_skip_to_end_of_block_or_statement (parser);
28475 goto out;
28480 /* Complain about missing 'typename' or other invalid type names. */
28481 if (!decl_specifiers.any_type_specifiers_p
28482 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28484 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28485 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28486 the rest of this declaration. */
28487 decl = error_mark_node;
28488 goto out;
28491 /* If it's not a template class, try for a template function. If
28492 the next token is a `;', then this declaration does not declare
28493 anything. But, if there were errors in the decl-specifiers, then
28494 the error might well have come from an attempted class-specifier.
28495 In that case, there's no need to warn about a missing declarator. */
28496 if (!decl
28497 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28498 || decl_specifiers.type != error_mark_node))
28500 decl = cp_parser_init_declarator (parser,
28501 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28502 &decl_specifiers,
28503 checks,
28504 /*function_definition_allowed_p=*/true,
28505 member_p,
28506 declares_class_or_enum,
28507 &function_definition_p,
28508 NULL, NULL, NULL);
28510 /* 7.1.1-1 [dcl.stc]
28512 A storage-class-specifier shall not be specified in an explicit
28513 specialization... */
28514 if (decl
28515 && explicit_specialization_p
28516 && decl_specifiers.storage_class != sc_none)
28518 error_at (decl_spec_token_start->location,
28519 "explicit template specialization cannot have a storage class");
28520 decl = error_mark_node;
28523 if (decl && VAR_P (decl))
28524 check_template_variable (decl);
28527 /* Look for a trailing `;' after the declaration. */
28528 if (!function_definition_p
28529 && (decl == error_mark_node
28530 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28531 cp_parser_skip_to_end_of_block_or_statement (parser);
28533 out:
28534 pop_deferring_access_checks ();
28536 /* Clear any current qualification; whatever comes next is the start
28537 of something new. */
28538 parser->scope = NULL_TREE;
28539 parser->qualifying_scope = NULL_TREE;
28540 parser->object_scope = NULL_TREE;
28542 return decl;
28545 /* Parse a cast-expression that is not the operand of a unary "&". */
28547 static cp_expr
28548 cp_parser_simple_cast_expression (cp_parser *parser)
28550 return cp_parser_cast_expression (parser, /*address_p=*/false,
28551 /*cast_p=*/false, /*decltype*/false, NULL);
28554 /* Parse a functional cast to TYPE. Returns an expression
28555 representing the cast. */
28557 static cp_expr
28558 cp_parser_functional_cast (cp_parser* parser, tree type)
28560 vec<tree, va_gc> *vec;
28561 tree expression_list;
28562 cp_expr cast;
28563 bool nonconst_p;
28565 location_t start_loc = input_location;
28567 if (!type)
28568 type = error_mark_node;
28570 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28572 cp_lexer_set_source_position (parser->lexer);
28573 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28574 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28575 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28576 if (TREE_CODE (type) == TYPE_DECL)
28577 type = TREE_TYPE (type);
28579 cast = finish_compound_literal (type, expression_list,
28580 tf_warning_or_error, fcl_functional);
28581 /* Create a location of the form:
28582 type_name{i, f}
28583 ^~~~~~~~~~~~~~~
28584 with caret == start at the start of the type name,
28585 finishing at the closing brace. */
28586 location_t combined_loc = make_location (start_loc, start_loc,
28587 parser->lexer);
28588 cast.set_location (combined_loc);
28589 return cast;
28593 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28594 /*cast_p=*/true,
28595 /*allow_expansion_p=*/true,
28596 /*non_constant_p=*/NULL);
28597 if (vec == NULL)
28598 expression_list = error_mark_node;
28599 else
28601 expression_list = build_tree_list_vec (vec);
28602 release_tree_vector (vec);
28605 cast = build_functional_cast (type, expression_list,
28606 tf_warning_or_error);
28607 /* [expr.const]/1: In an integral constant expression "only type
28608 conversions to integral or enumeration type can be used". */
28609 if (TREE_CODE (type) == TYPE_DECL)
28610 type = TREE_TYPE (type);
28611 if (cast != error_mark_node
28612 && !cast_valid_in_integral_constant_expression_p (type)
28613 && cp_parser_non_integral_constant_expression (parser,
28614 NIC_CONSTRUCTOR))
28615 return error_mark_node;
28617 /* Create a location of the form:
28618 float(i)
28619 ^~~~~~~~
28620 with caret == start at the start of the type name,
28621 finishing at the closing paren. */
28622 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
28623 cast.set_location (combined_loc);
28624 return cast;
28627 /* Save the tokens that make up the body of a member function defined
28628 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28629 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28630 specifiers applied to the declaration. Returns the FUNCTION_DECL
28631 for the member function. */
28633 static tree
28634 cp_parser_save_member_function_body (cp_parser* parser,
28635 cp_decl_specifier_seq *decl_specifiers,
28636 cp_declarator *declarator,
28637 tree attributes)
28639 cp_token *first;
28640 cp_token *last;
28641 tree fn;
28642 bool function_try_block = false;
28644 /* Create the FUNCTION_DECL. */
28645 fn = grokmethod (decl_specifiers, declarator, attributes);
28646 cp_finalize_omp_declare_simd (parser, fn);
28647 cp_finalize_oacc_routine (parser, fn, true);
28648 /* If something went badly wrong, bail out now. */
28649 if (fn == error_mark_node)
28651 /* If there's a function-body, skip it. */
28652 if (cp_parser_token_starts_function_definition_p
28653 (cp_lexer_peek_token (parser->lexer)))
28654 cp_parser_skip_to_end_of_block_or_statement (parser);
28655 return error_mark_node;
28658 /* Remember it, if there are default args to post process. */
28659 cp_parser_save_default_args (parser, fn);
28661 /* Save away the tokens that make up the body of the
28662 function. */
28663 first = parser->lexer->next_token;
28665 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28666 cp_lexer_consume_token (parser->lexer);
28667 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28668 RID_TRANSACTION_ATOMIC))
28670 cp_lexer_consume_token (parser->lexer);
28671 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28672 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28673 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28674 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28675 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28676 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28677 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28679 cp_lexer_consume_token (parser->lexer);
28680 cp_lexer_consume_token (parser->lexer);
28681 cp_lexer_consume_token (parser->lexer);
28682 cp_lexer_consume_token (parser->lexer);
28683 cp_lexer_consume_token (parser->lexer);
28685 else
28686 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28687 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28689 cp_lexer_consume_token (parser->lexer);
28690 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28691 break;
28695 /* Handle function try blocks. */
28696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28698 cp_lexer_consume_token (parser->lexer);
28699 function_try_block = true;
28701 /* We can have braced-init-list mem-initializers before the fn body. */
28702 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28704 cp_lexer_consume_token (parser->lexer);
28705 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28707 /* cache_group will stop after an un-nested { } pair, too. */
28708 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28709 break;
28711 /* variadic mem-inits have ... after the ')'. */
28712 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28713 cp_lexer_consume_token (parser->lexer);
28716 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28717 /* Handle function try blocks. */
28718 if (function_try_block)
28719 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28720 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28721 last = parser->lexer->next_token;
28723 /* Save away the inline definition; we will process it when the
28724 class is complete. */
28725 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28726 DECL_PENDING_INLINE_P (fn) = 1;
28728 /* We need to know that this was defined in the class, so that
28729 friend templates are handled correctly. */
28730 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28732 /* Add FN to the queue of functions to be parsed later. */
28733 vec_safe_push (unparsed_funs_with_definitions, fn);
28735 return fn;
28738 /* Save the tokens that make up the in-class initializer for a non-static
28739 data member. Returns a DEFERRED_PARSE. */
28741 static tree
28742 cp_parser_save_nsdmi (cp_parser* parser)
28744 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28747 /* Parse a template-argument-list, as well as the trailing ">" (but
28748 not the opening "<"). See cp_parser_template_argument_list for the
28749 return value. */
28751 static tree
28752 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28754 tree arguments;
28755 tree saved_scope;
28756 tree saved_qualifying_scope;
28757 tree saved_object_scope;
28758 bool saved_greater_than_is_operator_p;
28760 /* [temp.names]
28762 When parsing a template-id, the first non-nested `>' is taken as
28763 the end of the template-argument-list rather than a greater-than
28764 operator. */
28765 saved_greater_than_is_operator_p
28766 = parser->greater_than_is_operator_p;
28767 parser->greater_than_is_operator_p = false;
28768 /* Parsing the argument list may modify SCOPE, so we save it
28769 here. */
28770 saved_scope = parser->scope;
28771 saved_qualifying_scope = parser->qualifying_scope;
28772 saved_object_scope = parser->object_scope;
28773 /* We need to evaluate the template arguments, even though this
28774 template-id may be nested within a "sizeof". */
28775 cp_evaluated ev;
28776 /* Parse the template-argument-list itself. */
28777 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28778 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28779 arguments = NULL_TREE;
28780 else
28781 arguments = cp_parser_template_argument_list (parser);
28782 /* Look for the `>' that ends the template-argument-list. If we find
28783 a '>>' instead, it's probably just a typo. */
28784 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28786 if (cxx_dialect != cxx98)
28788 /* In C++0x, a `>>' in a template argument list or cast
28789 expression is considered to be two separate `>'
28790 tokens. So, change the current token to a `>', but don't
28791 consume it: it will be consumed later when the outer
28792 template argument list (or cast expression) is parsed.
28793 Note that this replacement of `>' for `>>' is necessary
28794 even if we are parsing tentatively: in the tentative
28795 case, after calling
28796 cp_parser_enclosed_template_argument_list we will always
28797 throw away all of the template arguments and the first
28798 closing `>', either because the template argument list
28799 was erroneous or because we are replacing those tokens
28800 with a CPP_TEMPLATE_ID token. The second `>' (which will
28801 not have been thrown away) is needed either to close an
28802 outer template argument list or to complete a new-style
28803 cast. */
28804 cp_token *token = cp_lexer_peek_token (parser->lexer);
28805 token->type = CPP_GREATER;
28807 else if (!saved_greater_than_is_operator_p)
28809 /* If we're in a nested template argument list, the '>>' has
28810 to be a typo for '> >'. We emit the error message, but we
28811 continue parsing and we push a '>' as next token, so that
28812 the argument list will be parsed correctly. Note that the
28813 global source location is still on the token before the
28814 '>>', so we need to say explicitly where we want it. */
28815 cp_token *token = cp_lexer_peek_token (parser->lexer);
28816 gcc_rich_location richloc (token->location);
28817 richloc.add_fixit_replace ("> >");
28818 error_at (&richloc, "%<>>%> should be %<> >%> "
28819 "within a nested template argument list");
28821 token->type = CPP_GREATER;
28823 else
28825 /* If this is not a nested template argument list, the '>>'
28826 is a typo for '>'. Emit an error message and continue.
28827 Same deal about the token location, but here we can get it
28828 right by consuming the '>>' before issuing the diagnostic. */
28829 cp_token *token = cp_lexer_consume_token (parser->lexer);
28830 error_at (token->location,
28831 "spurious %<>>%>, use %<>%> to terminate "
28832 "a template argument list");
28835 else
28836 cp_parser_skip_to_end_of_template_parameter_list (parser);
28837 /* The `>' token might be a greater-than operator again now. */
28838 parser->greater_than_is_operator_p
28839 = saved_greater_than_is_operator_p;
28840 /* Restore the SAVED_SCOPE. */
28841 parser->scope = saved_scope;
28842 parser->qualifying_scope = saved_qualifying_scope;
28843 parser->object_scope = saved_object_scope;
28845 return arguments;
28848 /* MEMBER_FUNCTION is a member function, or a friend. If default
28849 arguments, or the body of the function have not yet been parsed,
28850 parse them now. */
28852 static void
28853 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28855 timevar_push (TV_PARSE_INMETH);
28856 /* If this member is a template, get the underlying
28857 FUNCTION_DECL. */
28858 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28859 member_function = DECL_TEMPLATE_RESULT (member_function);
28861 /* There should not be any class definitions in progress at this
28862 point; the bodies of members are only parsed outside of all class
28863 definitions. */
28864 gcc_assert (parser->num_classes_being_defined == 0);
28865 /* While we're parsing the member functions we might encounter more
28866 classes. We want to handle them right away, but we don't want
28867 them getting mixed up with functions that are currently in the
28868 queue. */
28869 push_unparsed_function_queues (parser);
28871 /* Make sure that any template parameters are in scope. */
28872 maybe_begin_member_template_processing (member_function);
28874 /* If the body of the function has not yet been parsed, parse it
28875 now. */
28876 if (DECL_PENDING_INLINE_P (member_function))
28878 tree function_scope;
28879 cp_token_cache *tokens;
28881 /* The function is no longer pending; we are processing it. */
28882 tokens = DECL_PENDING_INLINE_INFO (member_function);
28883 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28884 DECL_PENDING_INLINE_P (member_function) = 0;
28886 /* If this is a local class, enter the scope of the containing
28887 function. */
28888 function_scope = current_function_decl;
28889 if (function_scope)
28890 push_function_context ();
28892 /* Push the body of the function onto the lexer stack. */
28893 cp_parser_push_lexer_for_tokens (parser, tokens);
28895 /* Let the front end know that we going to be defining this
28896 function. */
28897 start_preparsed_function (member_function, NULL_TREE,
28898 SF_PRE_PARSED | SF_INCLASS_INLINE);
28900 /* Don't do access checking if it is a templated function. */
28901 if (processing_template_decl)
28902 push_deferring_access_checks (dk_no_check);
28904 /* #pragma omp declare reduction needs special parsing. */
28905 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28907 parser->lexer->in_pragma = true;
28908 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28909 finish_function (/*inline_p=*/true);
28910 cp_check_omp_declare_reduction (member_function);
28912 else
28913 /* Now, parse the body of the function. */
28914 cp_parser_function_definition_after_declarator (parser,
28915 /*inline_p=*/true);
28917 if (processing_template_decl)
28918 pop_deferring_access_checks ();
28920 /* Leave the scope of the containing function. */
28921 if (function_scope)
28922 pop_function_context ();
28923 cp_parser_pop_lexer (parser);
28926 /* Remove any template parameters from the symbol table. */
28927 maybe_end_member_template_processing ();
28929 /* Restore the queue. */
28930 pop_unparsed_function_queues (parser);
28931 timevar_pop (TV_PARSE_INMETH);
28934 /* If DECL contains any default args, remember it on the unparsed
28935 functions queue. */
28937 static void
28938 cp_parser_save_default_args (cp_parser* parser, tree decl)
28940 tree probe;
28942 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28943 probe;
28944 probe = TREE_CHAIN (probe))
28945 if (TREE_PURPOSE (probe))
28947 cp_default_arg_entry entry = {current_class_type, decl};
28948 vec_safe_push (unparsed_funs_with_default_args, entry);
28949 break;
28952 /* Remember if there is a noexcept-specifier to post process. */
28953 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
28954 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
28955 vec_safe_push (unparsed_noexcepts, decl);
28958 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28959 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28960 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28961 from the parameter-type-list. */
28963 static tree
28964 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28965 tree default_arg, tree parmtype)
28967 cp_token_cache *tokens;
28968 tree parsed_arg;
28969 bool dummy;
28971 if (default_arg == error_mark_node)
28972 return error_mark_node;
28974 /* Push the saved tokens for the default argument onto the parser's
28975 lexer stack. */
28976 tokens = DEFPARSE_TOKENS (default_arg);
28977 cp_parser_push_lexer_for_tokens (parser, tokens);
28979 start_lambda_scope (decl);
28981 /* Parse the default argument. */
28982 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28983 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28986 finish_lambda_scope ();
28988 if (parsed_arg == error_mark_node)
28989 cp_parser_skip_to_end_of_statement (parser);
28991 if (!processing_template_decl)
28993 /* In a non-template class, check conversions now. In a template,
28994 we'll wait and instantiate these as needed. */
28995 if (TREE_CODE (decl) == PARM_DECL)
28996 parsed_arg = check_default_argument (parmtype, parsed_arg,
28997 tf_warning_or_error);
28998 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28999 parsed_arg = error_mark_node;
29000 else
29001 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
29004 /* If the token stream has not been completely used up, then
29005 there was extra junk after the end of the default
29006 argument. */
29007 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29009 if (TREE_CODE (decl) == PARM_DECL)
29010 cp_parser_error (parser, "expected %<,%>");
29011 else
29012 cp_parser_error (parser, "expected %<;%>");
29015 /* Revert to the main lexer. */
29016 cp_parser_pop_lexer (parser);
29018 return parsed_arg;
29021 /* FIELD is a non-static data member with an initializer which we saved for
29022 later; parse it now. */
29024 static void
29025 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
29027 tree def;
29029 maybe_begin_member_template_processing (field);
29031 push_unparsed_function_queues (parser);
29032 def = cp_parser_late_parse_one_default_arg (parser, field,
29033 DECL_INITIAL (field),
29034 NULL_TREE);
29035 pop_unparsed_function_queues (parser);
29037 maybe_end_member_template_processing ();
29039 DECL_INITIAL (field) = def;
29042 /* FN is a FUNCTION_DECL which may contains a parameter with an
29043 unparsed DEFERRED_PARSE. Parse the default args now. This function
29044 assumes that the current scope is the scope in which the default
29045 argument should be processed. */
29047 static void
29048 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
29050 unsigned char saved_local_variables_forbidden_p;
29051 tree parm, parmdecl;
29053 /* While we're parsing the default args, we might (due to the
29054 statement expression extension) encounter more classes. We want
29055 to handle them right away, but we don't want them getting mixed
29056 up with default args that are currently in the queue. */
29057 push_unparsed_function_queues (parser);
29059 /* Local variable names (and the `this' keyword) may not appear
29060 in a default argument. */
29061 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
29062 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
29064 push_defarg_context (fn);
29066 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
29067 parmdecl = DECL_ARGUMENTS (fn);
29068 parm && parm != void_list_node;
29069 parm = TREE_CHAIN (parm),
29070 parmdecl = DECL_CHAIN (parmdecl))
29072 tree default_arg = TREE_PURPOSE (parm);
29073 tree parsed_arg;
29074 vec<tree, va_gc> *insts;
29075 tree copy;
29076 unsigned ix;
29078 if (!default_arg)
29079 continue;
29081 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
29082 /* This can happen for a friend declaration for a function
29083 already declared with default arguments. */
29084 continue;
29086 parsed_arg
29087 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
29088 default_arg,
29089 TREE_VALUE (parm));
29090 TREE_PURPOSE (parm) = parsed_arg;
29092 /* Update any instantiations we've already created. */
29093 for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
29094 vec_safe_iterate (insts, ix, &copy); ix++)
29095 TREE_PURPOSE (copy) = parsed_arg;
29098 pop_defarg_context ();
29100 /* Make sure no default arg is missing. */
29101 check_default_args (fn);
29103 /* Restore the state of local_variables_forbidden_p. */
29104 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
29106 /* Restore the queue. */
29107 pop_unparsed_function_queues (parser);
29110 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
29112 sizeof ... ( identifier )
29114 where the 'sizeof' token has already been consumed. */
29116 static tree
29117 cp_parser_sizeof_pack (cp_parser *parser)
29119 /* Consume the `...'. */
29120 cp_lexer_consume_token (parser->lexer);
29121 maybe_warn_variadic_templates ();
29123 matching_parens parens;
29124 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
29125 if (paren)
29126 parens.consume_open (parser);
29127 else
29128 permerror (cp_lexer_peek_token (parser->lexer)->location,
29129 "%<sizeof...%> argument must be surrounded by parentheses");
29131 cp_token *token = cp_lexer_peek_token (parser->lexer);
29132 tree name = cp_parser_identifier (parser);
29133 if (name == error_mark_node)
29134 return error_mark_node;
29135 /* The name is not qualified. */
29136 parser->scope = NULL_TREE;
29137 parser->qualifying_scope = NULL_TREE;
29138 parser->object_scope = NULL_TREE;
29139 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
29140 if (expr == error_mark_node)
29141 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
29142 token->location);
29143 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
29144 expr = TREE_TYPE (expr);
29145 else if (TREE_CODE (expr) == CONST_DECL)
29146 expr = DECL_INITIAL (expr);
29147 expr = make_pack_expansion (expr);
29148 PACK_EXPANSION_SIZEOF_P (expr) = true;
29150 if (paren)
29151 parens.require_close (parser);
29153 return expr;
29156 /* Parse the operand of `sizeof' (or a similar operator). Returns
29157 either a TYPE or an expression, depending on the form of the
29158 input. The KEYWORD indicates which kind of expression we have
29159 encountered. */
29161 static tree
29162 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
29164 tree expr = NULL_TREE;
29165 const char *saved_message;
29166 const char *saved_message_arg;
29167 bool saved_integral_constant_expression_p;
29168 bool saved_non_integral_constant_expression_p;
29170 /* If it's a `...', then we are computing the length of a parameter
29171 pack. */
29172 if (keyword == RID_SIZEOF
29173 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29174 return cp_parser_sizeof_pack (parser);
29176 /* Types cannot be defined in a `sizeof' expression. Save away the
29177 old message. */
29178 saved_message = parser->type_definition_forbidden_message;
29179 saved_message_arg = parser->type_definition_forbidden_message_arg;
29180 parser->type_definition_forbidden_message
29181 = G_("types may not be defined in %qs expressions");
29182 parser->type_definition_forbidden_message_arg
29183 = IDENTIFIER_POINTER (ridpointers[keyword]);
29185 /* The restrictions on constant-expressions do not apply inside
29186 sizeof expressions. */
29187 saved_integral_constant_expression_p
29188 = parser->integral_constant_expression_p;
29189 saved_non_integral_constant_expression_p
29190 = parser->non_integral_constant_expression_p;
29191 parser->integral_constant_expression_p = false;
29193 /* Do not actually evaluate the expression. */
29194 ++cp_unevaluated_operand;
29195 ++c_inhibit_evaluation_warnings;
29196 /* If it's a `(', then we might be looking at the type-id
29197 construction. */
29198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29200 tree type = NULL_TREE;
29202 tentative_firewall firewall (parser);
29204 /* We can't be sure yet whether we're looking at a type-id or an
29205 expression. */
29206 cp_parser_parse_tentatively (parser);
29208 matching_parens parens;
29209 parens.consume_open (parser);
29211 /* Note: as a GNU Extension, compound literals are considered
29212 postfix-expressions as they are in C99, so they are valid
29213 arguments to sizeof. See comment in cp_parser_cast_expression
29214 for details. */
29215 if (cp_parser_compound_literal_p (parser))
29216 cp_parser_simulate_error (parser);
29217 else
29219 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
29220 parser->in_type_id_in_expr_p = true;
29221 /* Look for the type-id. */
29222 type = cp_parser_type_id (parser);
29223 /* Look for the closing `)'. */
29224 parens.require_close (parser);
29225 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
29228 /* If all went well, then we're done. */
29229 if (cp_parser_parse_definitely (parser))
29230 expr = type;
29231 else
29233 /* Commit to the tentative_firewall so we get syntax errors. */
29234 cp_parser_commit_to_tentative_parse (parser);
29236 expr = cp_parser_unary_expression (parser);
29239 else
29240 expr = cp_parser_unary_expression (parser);
29242 /* Go back to evaluating expressions. */
29243 --cp_unevaluated_operand;
29244 --c_inhibit_evaluation_warnings;
29246 /* And restore the old one. */
29247 parser->type_definition_forbidden_message = saved_message;
29248 parser->type_definition_forbidden_message_arg = saved_message_arg;
29249 parser->integral_constant_expression_p
29250 = saved_integral_constant_expression_p;
29251 parser->non_integral_constant_expression_p
29252 = saved_non_integral_constant_expression_p;
29254 return expr;
29257 /* If the current declaration has no declarator, return true. */
29259 static bool
29260 cp_parser_declares_only_class_p (cp_parser *parser)
29262 /* If the next token is a `;' or a `,' then there is no
29263 declarator. */
29264 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29265 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29268 /* Update the DECL_SPECS to reflect the storage class indicated by
29269 KEYWORD. */
29271 static void
29272 cp_parser_set_storage_class (cp_parser *parser,
29273 cp_decl_specifier_seq *decl_specs,
29274 enum rid keyword,
29275 cp_token *token)
29277 cp_storage_class storage_class;
29279 if (parser->in_unbraced_linkage_specification_p)
29281 error_at (token->location, "invalid use of %qD in linkage specification",
29282 ridpointers[keyword]);
29283 return;
29285 else if (decl_specs->storage_class != sc_none)
29287 decl_specs->conflicting_specifiers_p = true;
29288 return;
29291 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29292 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29293 && decl_specs->gnu_thread_keyword_p)
29295 pedwarn (decl_specs->locations[ds_thread], 0,
29296 "%<__thread%> before %qD", ridpointers[keyword]);
29299 switch (keyword)
29301 case RID_AUTO:
29302 storage_class = sc_auto;
29303 break;
29304 case RID_REGISTER:
29305 storage_class = sc_register;
29306 break;
29307 case RID_STATIC:
29308 storage_class = sc_static;
29309 break;
29310 case RID_EXTERN:
29311 storage_class = sc_extern;
29312 break;
29313 case RID_MUTABLE:
29314 storage_class = sc_mutable;
29315 break;
29316 default:
29317 gcc_unreachable ();
29319 decl_specs->storage_class = storage_class;
29320 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29322 /* A storage class specifier cannot be applied alongside a typedef
29323 specifier. If there is a typedef specifier present then set
29324 conflicting_specifiers_p which will trigger an error later
29325 on in grokdeclarator. */
29326 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29327 decl_specs->conflicting_specifiers_p = true;
29330 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29331 is true, the type is a class or enum definition. */
29333 static void
29334 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
29335 tree type_spec,
29336 cp_token *token,
29337 bool type_definition_p)
29339 decl_specs->any_specifiers_p = true;
29341 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29342 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29343 this is what happened. In system headers, we ignore these
29344 declarations so that G++ can work with system headers that are not
29345 C++-safe. */
29346 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29347 && !type_definition_p
29348 && (type_spec == boolean_type_node
29349 || type_spec == char8_type_node
29350 || type_spec == char16_type_node
29351 || type_spec == char32_type_node
29352 || type_spec == wchar_type_node)
29353 && (decl_specs->type
29354 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29355 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29356 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29357 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29359 decl_specs->redefined_builtin_type = type_spec;
29360 set_and_check_decl_spec_loc (decl_specs,
29361 ds_redefined_builtin_type_spec,
29362 token);
29363 if (!decl_specs->type)
29365 decl_specs->type = type_spec;
29366 decl_specs->type_definition_p = false;
29367 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29370 else if (decl_specs->type)
29371 decl_specs->multiple_types_p = true;
29372 else
29374 decl_specs->type = type_spec;
29375 decl_specs->type_definition_p = type_definition_p;
29376 decl_specs->redefined_builtin_type = NULL_TREE;
29377 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29381 /* True iff TOKEN is the GNU keyword __thread. */
29383 static bool
29384 token_is__thread (cp_token *token)
29386 gcc_assert (token->keyword == RID_THREAD);
29387 return id_equal (token->u.value, "__thread");
29390 /* Set the location for a declarator specifier and check if it is
29391 duplicated.
29393 DECL_SPECS is the sequence of declarator specifiers onto which to
29394 set the location.
29396 DS is the single declarator specifier to set which location is to
29397 be set onto the existing sequence of declarators.
29399 LOCATION is the location for the declarator specifier to
29400 consider. */
29402 static void
29403 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29404 cp_decl_spec ds, cp_token *token)
29406 gcc_assert (ds < ds_last);
29408 if (decl_specs == NULL)
29409 return;
29411 location_t location = token->location;
29413 if (decl_specs->locations[ds] == 0)
29415 decl_specs->locations[ds] = location;
29416 if (ds == ds_thread)
29417 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29419 else
29421 if (ds == ds_long)
29423 if (decl_specs->locations[ds_long_long] != 0)
29424 error_at (location,
29425 "%<long long long%> is too long for GCC");
29426 else
29428 decl_specs->locations[ds_long_long] = location;
29429 pedwarn_cxx98 (location,
29430 OPT_Wlong_long,
29431 "ISO C++ 1998 does not support %<long long%>");
29434 else if (ds == ds_thread)
29436 bool gnu = token_is__thread (token);
29437 gcc_rich_location richloc (location);
29438 if (gnu != decl_specs->gnu_thread_keyword_p)
29440 richloc.add_range (decl_specs->locations[ds_thread]);
29441 error_at (&richloc,
29442 "both %<__thread%> and %<thread_local%> specified");
29444 else
29446 richloc.add_fixit_remove ();
29447 error_at (&richloc, "duplicate %qD", token->u.value);
29450 else
29452 static const char *const decl_spec_names[] = {
29453 "signed",
29454 "unsigned",
29455 "short",
29456 "long",
29457 "const",
29458 "volatile",
29459 "restrict",
29460 "inline",
29461 "virtual",
29462 "explicit",
29463 "friend",
29464 "typedef",
29465 "using",
29466 "constexpr",
29467 "__complex"
29469 gcc_rich_location richloc (location);
29470 richloc.add_fixit_remove ();
29471 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29476 /* Return true iff the declarator specifier DS is present in the
29477 sequence of declarator specifiers DECL_SPECS. */
29479 bool
29480 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29481 cp_decl_spec ds)
29483 gcc_assert (ds < ds_last);
29485 if (decl_specs == NULL)
29486 return false;
29488 return decl_specs->locations[ds] != 0;
29491 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29492 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29494 static bool
29495 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29497 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29500 /* Issue an error message indicating that TOKEN_DESC was expected.
29501 If KEYWORD is true, it indicated this function is called by
29502 cp_parser_require_keword and the required token can only be
29503 a indicated keyword.
29505 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29506 within any error as the location of an "opening" token matching
29507 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29508 RT_CLOSE_PAREN). */
29510 static void
29511 cp_parser_required_error (cp_parser *parser,
29512 required_token token_desc,
29513 bool keyword,
29514 location_t matching_location)
29516 if (cp_parser_simulate_error (parser))
29517 return;
29519 const char *gmsgid = NULL;
29520 switch (token_desc)
29522 case RT_NEW:
29523 gmsgid = G_("expected %<new%>");
29524 break;
29525 case RT_DELETE:
29526 gmsgid = G_("expected %<delete%>");
29527 break;
29528 case RT_RETURN:
29529 gmsgid = G_("expected %<return%>");
29530 break;
29531 case RT_WHILE:
29532 gmsgid = G_("expected %<while%>");
29533 break;
29534 case RT_EXTERN:
29535 gmsgid = G_("expected %<extern%>");
29536 break;
29537 case RT_STATIC_ASSERT:
29538 gmsgid = G_("expected %<static_assert%>");
29539 break;
29540 case RT_DECLTYPE:
29541 gmsgid = G_("expected %<decltype%>");
29542 break;
29543 case RT_OPERATOR:
29544 gmsgid = G_("expected %<operator%>");
29545 break;
29546 case RT_CLASS:
29547 gmsgid = G_("expected %<class%>");
29548 break;
29549 case RT_TEMPLATE:
29550 gmsgid = G_("expected %<template%>");
29551 break;
29552 case RT_NAMESPACE:
29553 gmsgid = G_("expected %<namespace%>");
29554 break;
29555 case RT_USING:
29556 gmsgid = G_("expected %<using%>");
29557 break;
29558 case RT_ASM:
29559 gmsgid = G_("expected %<asm%>");
29560 break;
29561 case RT_TRY:
29562 gmsgid = G_("expected %<try%>");
29563 break;
29564 case RT_CATCH:
29565 gmsgid = G_("expected %<catch%>");
29566 break;
29567 case RT_THROW:
29568 gmsgid = G_("expected %<throw%>");
29569 break;
29570 case RT_LABEL:
29571 gmsgid = G_("expected %<__label__%>");
29572 break;
29573 case RT_AT_TRY:
29574 gmsgid = G_("expected %<@try%>");
29575 break;
29576 case RT_AT_SYNCHRONIZED:
29577 gmsgid = G_("expected %<@synchronized%>");
29578 break;
29579 case RT_AT_THROW:
29580 gmsgid = G_("expected %<@throw%>");
29581 break;
29582 case RT_TRANSACTION_ATOMIC:
29583 gmsgid = G_("expected %<__transaction_atomic%>");
29584 break;
29585 case RT_TRANSACTION_RELAXED:
29586 gmsgid = G_("expected %<__transaction_relaxed%>");
29587 break;
29588 default:
29589 break;
29592 if (!gmsgid && !keyword)
29594 switch (token_desc)
29596 case RT_SEMICOLON:
29597 gmsgid = G_("expected %<;%>");
29598 break;
29599 case RT_OPEN_PAREN:
29600 gmsgid = G_("expected %<(%>");
29601 break;
29602 case RT_CLOSE_BRACE:
29603 gmsgid = G_("expected %<}%>");
29604 break;
29605 case RT_OPEN_BRACE:
29606 gmsgid = G_("expected %<{%>");
29607 break;
29608 case RT_CLOSE_SQUARE:
29609 gmsgid = G_("expected %<]%>");
29610 break;
29611 case RT_OPEN_SQUARE:
29612 gmsgid = G_("expected %<[%>");
29613 break;
29614 case RT_COMMA:
29615 gmsgid = G_("expected %<,%>");
29616 break;
29617 case RT_SCOPE:
29618 gmsgid = G_("expected %<::%>");
29619 break;
29620 case RT_LESS:
29621 gmsgid = G_("expected %<<%>");
29622 break;
29623 case RT_GREATER:
29624 gmsgid = G_("expected %<>%>");
29625 break;
29626 case RT_EQ:
29627 gmsgid = G_("expected %<=%>");
29628 break;
29629 case RT_ELLIPSIS:
29630 gmsgid = G_("expected %<...%>");
29631 break;
29632 case RT_MULT:
29633 gmsgid = G_("expected %<*%>");
29634 break;
29635 case RT_COMPL:
29636 gmsgid = G_("expected %<~%>");
29637 break;
29638 case RT_COLON:
29639 gmsgid = G_("expected %<:%>");
29640 break;
29641 case RT_COLON_SCOPE:
29642 gmsgid = G_("expected %<:%> or %<::%>");
29643 break;
29644 case RT_CLOSE_PAREN:
29645 gmsgid = G_("expected %<)%>");
29646 break;
29647 case RT_COMMA_CLOSE_PAREN:
29648 gmsgid = G_("expected %<,%> or %<)%>");
29649 break;
29650 case RT_PRAGMA_EOL:
29651 gmsgid = G_("expected end of line");
29652 break;
29653 case RT_NAME:
29654 gmsgid = G_("expected identifier");
29655 break;
29656 case RT_SELECT:
29657 gmsgid = G_("expected selection-statement");
29658 break;
29659 case RT_ITERATION:
29660 gmsgid = G_("expected iteration-statement");
29661 break;
29662 case RT_JUMP:
29663 gmsgid = G_("expected jump-statement");
29664 break;
29665 case RT_CLASS_KEY:
29666 gmsgid = G_("expected class-key");
29667 break;
29668 case RT_CLASS_TYPENAME_TEMPLATE:
29669 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29670 break;
29671 default:
29672 gcc_unreachable ();
29676 if (gmsgid)
29677 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29681 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29682 issue an error message indicating that TOKEN_DESC was expected.
29684 Returns the token consumed, if the token had the appropriate type.
29685 Otherwise, returns NULL.
29687 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29688 within any error as the location of an "opening" token matching
29689 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29690 RT_CLOSE_PAREN). */
29692 static cp_token *
29693 cp_parser_require (cp_parser* parser,
29694 enum cpp_ttype type,
29695 required_token token_desc,
29696 location_t matching_location)
29698 if (cp_lexer_next_token_is (parser->lexer, type))
29699 return cp_lexer_consume_token (parser->lexer);
29700 else
29702 /* Output the MESSAGE -- unless we're parsing tentatively. */
29703 if (!cp_parser_simulate_error (parser))
29704 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29705 matching_location);
29706 return NULL;
29710 /* An error message is produced if the next token is not '>'.
29711 All further tokens are skipped until the desired token is
29712 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29714 static void
29715 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29717 /* Current level of '< ... >'. */
29718 unsigned level = 0;
29719 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29720 unsigned nesting_depth = 0;
29722 /* Are we ready, yet? If not, issue error message. */
29723 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29724 return;
29726 /* Skip tokens until the desired token is found. */
29727 while (true)
29729 /* Peek at the next token. */
29730 switch (cp_lexer_peek_token (parser->lexer)->type)
29732 case CPP_LESS:
29733 if (!nesting_depth)
29734 ++level;
29735 break;
29737 case CPP_RSHIFT:
29738 if (cxx_dialect == cxx98)
29739 /* C++0x views the `>>' operator as two `>' tokens, but
29740 C++98 does not. */
29741 break;
29742 else if (!nesting_depth && level-- == 0)
29744 /* We've hit a `>>' where the first `>' closes the
29745 template argument list, and the second `>' is
29746 spurious. Just consume the `>>' and stop; we've
29747 already produced at least one error. */
29748 cp_lexer_consume_token (parser->lexer);
29749 return;
29751 /* Fall through for C++0x, so we handle the second `>' in
29752 the `>>'. */
29753 gcc_fallthrough ();
29755 case CPP_GREATER:
29756 if (!nesting_depth && level-- == 0)
29758 /* We've reached the token we want, consume it and stop. */
29759 cp_lexer_consume_token (parser->lexer);
29760 return;
29762 break;
29764 case CPP_OPEN_PAREN:
29765 case CPP_OPEN_SQUARE:
29766 ++nesting_depth;
29767 break;
29769 case CPP_CLOSE_PAREN:
29770 case CPP_CLOSE_SQUARE:
29771 if (nesting_depth-- == 0)
29772 return;
29773 break;
29775 case CPP_EOF:
29776 case CPP_PRAGMA_EOL:
29777 case CPP_SEMICOLON:
29778 case CPP_OPEN_BRACE:
29779 case CPP_CLOSE_BRACE:
29780 /* The '>' was probably forgotten, don't look further. */
29781 return;
29783 default:
29784 break;
29787 /* Consume this token. */
29788 cp_lexer_consume_token (parser->lexer);
29792 /* If the next token is the indicated keyword, consume it. Otherwise,
29793 issue an error message indicating that TOKEN_DESC was expected.
29795 Returns the token consumed, if the token had the appropriate type.
29796 Otherwise, returns NULL. */
29798 static cp_token *
29799 cp_parser_require_keyword (cp_parser* parser,
29800 enum rid keyword,
29801 required_token token_desc)
29803 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29805 if (token && token->keyword != keyword)
29807 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29808 UNKNOWN_LOCATION);
29809 return NULL;
29812 return token;
29815 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29816 function-definition. */
29818 static bool
29819 cp_parser_token_starts_function_definition_p (cp_token* token)
29821 return (/* An ordinary function-body begins with an `{'. */
29822 token->type == CPP_OPEN_BRACE
29823 /* A ctor-initializer begins with a `:'. */
29824 || token->type == CPP_COLON
29825 /* A function-try-block begins with `try'. */
29826 || token->keyword == RID_TRY
29827 /* A function-transaction-block begins with `__transaction_atomic'
29828 or `__transaction_relaxed'. */
29829 || token->keyword == RID_TRANSACTION_ATOMIC
29830 || token->keyword == RID_TRANSACTION_RELAXED
29831 /* The named return value extension begins with `return'. */
29832 || token->keyword == RID_RETURN);
29835 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29836 definition. */
29838 static bool
29839 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29841 cp_token *token;
29843 token = cp_lexer_peek_token (parser->lexer);
29844 return (token->type == CPP_OPEN_BRACE
29845 || (token->type == CPP_COLON
29846 && !parser->colon_doesnt_start_class_def_p));
29849 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29850 C++0x) ending a template-argument. */
29852 static bool
29853 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29855 cp_token *token;
29857 token = cp_lexer_peek_token (parser->lexer);
29858 return (token->type == CPP_COMMA
29859 || token->type == CPP_GREATER
29860 || token->type == CPP_ELLIPSIS
29861 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29864 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29865 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29867 static bool
29868 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29869 size_t n)
29871 cp_token *token;
29873 token = cp_lexer_peek_nth_token (parser->lexer, n);
29874 if (token->type == CPP_LESS)
29875 return true;
29876 /* Check for the sequence `<::' in the original code. It would be lexed as
29877 `[:', where `[' is a digraph, and there is no whitespace before
29878 `:'. */
29879 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29881 cp_token *token2;
29882 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29883 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29884 return true;
29886 return false;
29889 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29890 or none_type otherwise. */
29892 static enum tag_types
29893 cp_parser_token_is_class_key (cp_token* token)
29895 switch (token->keyword)
29897 case RID_CLASS:
29898 return class_type;
29899 case RID_STRUCT:
29900 return record_type;
29901 case RID_UNION:
29902 return union_type;
29904 default:
29905 return none_type;
29909 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29910 or none_type otherwise or if the token is null. */
29912 static enum tag_types
29913 cp_parser_token_is_type_parameter_key (cp_token* token)
29915 if (!token)
29916 return none_type;
29918 switch (token->keyword)
29920 case RID_CLASS:
29921 return class_type;
29922 case RID_TYPENAME:
29923 return typename_type;
29925 default:
29926 return none_type;
29930 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29932 static void
29933 cp_parser_check_class_key (enum tag_types class_key, tree type)
29935 if (type == error_mark_node)
29936 return;
29937 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29939 if (permerror (input_location, "%qs tag used in naming %q#T",
29940 class_key == union_type ? "union"
29941 : class_key == record_type ? "struct" : "class",
29942 type))
29943 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29944 "%q#T was previously declared here", type);
29948 /* Issue an error message if DECL is redeclared with different
29949 access than its original declaration [class.access.spec/3].
29950 This applies to nested classes, nested class templates and
29951 enumerations [class.mem/1]. */
29953 static void
29954 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29956 if (!decl
29957 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29958 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29959 return;
29961 if ((TREE_PRIVATE (decl)
29962 != (current_access_specifier == access_private_node))
29963 || (TREE_PROTECTED (decl)
29964 != (current_access_specifier == access_protected_node)))
29965 error_at (location, "%qD redeclared with different access", decl);
29968 /* Look for the `template' keyword, as a syntactic disambiguator.
29969 Return TRUE iff it is present, in which case it will be
29970 consumed. */
29972 static bool
29973 cp_parser_optional_template_keyword (cp_parser *parser)
29975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29977 /* In C++98 the `template' keyword can only be used within templates;
29978 outside templates the parser can always figure out what is a
29979 template and what is not. In C++11, per the resolution of DR 468,
29980 `template' is allowed in cases where it is not strictly necessary. */
29981 if (!processing_template_decl
29982 && pedantic && cxx_dialect == cxx98)
29984 cp_token *token = cp_lexer_peek_token (parser->lexer);
29985 pedwarn (token->location, OPT_Wpedantic,
29986 "in C++98 %<template%> (as a disambiguator) is only "
29987 "allowed within templates");
29988 /* If this part of the token stream is rescanned, the same
29989 error message would be generated. So, we purge the token
29990 from the stream. */
29991 cp_lexer_purge_token (parser->lexer);
29992 return false;
29994 else
29996 /* Consume the `template' keyword. */
29997 cp_lexer_consume_token (parser->lexer);
29998 return true;
30001 return false;
30004 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
30005 set PARSER->SCOPE, and perform other related actions. */
30007 static void
30008 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
30010 struct tree_check *check_value;
30012 /* Get the stored value. */
30013 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
30014 /* Set the scope from the stored value. */
30015 parser->scope = saved_checks_value (check_value);
30016 parser->qualifying_scope = check_value->qualifying_scope;
30017 parser->object_scope = NULL_TREE;
30020 /* Consume tokens up through a non-nested END token. Returns TRUE if we
30021 encounter the end of a block before what we were looking for. */
30023 static bool
30024 cp_parser_cache_group (cp_parser *parser,
30025 enum cpp_ttype end,
30026 unsigned depth)
30028 while (true)
30030 cp_token *token = cp_lexer_peek_token (parser->lexer);
30032 /* Abort a parenthesized expression if we encounter a semicolon. */
30033 if ((end == CPP_CLOSE_PAREN || depth == 0)
30034 && token->type == CPP_SEMICOLON)
30035 return true;
30036 /* If we've reached the end of the file, stop. */
30037 if (token->type == CPP_EOF
30038 || (end != CPP_PRAGMA_EOL
30039 && token->type == CPP_PRAGMA_EOL))
30040 return true;
30041 if (token->type == CPP_CLOSE_BRACE && depth == 0)
30042 /* We've hit the end of an enclosing block, so there's been some
30043 kind of syntax error. */
30044 return true;
30046 /* Consume the token. */
30047 cp_lexer_consume_token (parser->lexer);
30048 /* See if it starts a new group. */
30049 if (token->type == CPP_OPEN_BRACE)
30051 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
30052 /* In theory this should probably check end == '}', but
30053 cp_parser_save_member_function_body needs it to exit
30054 after either '}' or ')' when called with ')'. */
30055 if (depth == 0)
30056 return false;
30058 else if (token->type == CPP_OPEN_PAREN)
30060 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
30061 if (depth == 0 && end == CPP_CLOSE_PAREN)
30062 return false;
30064 else if (token->type == CPP_PRAGMA)
30065 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
30066 else if (token->type == end)
30067 return false;
30071 /* Like above, for caching a default argument or NSDMI. Both of these are
30072 terminated by a non-nested comma, but it can be unclear whether or not a
30073 comma is nested in a template argument list unless we do more parsing.
30074 In order to handle this ambiguity, when we encounter a ',' after a '<'
30075 we try to parse what follows as a parameter-declaration-list (in the
30076 case of a default argument) or a member-declarator (in the case of an
30077 NSDMI). If that succeeds, then we stop caching. */
30079 static tree
30080 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
30082 unsigned depth = 0;
30083 int maybe_template_id = 0;
30084 cp_token *first_token;
30085 cp_token *token;
30086 tree default_argument;
30088 /* Add tokens until we have processed the entire default
30089 argument. We add the range [first_token, token). */
30090 first_token = cp_lexer_peek_token (parser->lexer);
30091 if (first_token->type == CPP_OPEN_BRACE)
30093 /* For list-initialization, this is straightforward. */
30094 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30095 token = cp_lexer_peek_token (parser->lexer);
30097 else while (true)
30099 bool done = false;
30101 /* Peek at the next token. */
30102 token = cp_lexer_peek_token (parser->lexer);
30103 /* What we do depends on what token we have. */
30104 switch (token->type)
30106 /* In valid code, a default argument must be
30107 immediately followed by a `,' `)', or `...'. */
30108 case CPP_COMMA:
30109 if (depth == 0 && maybe_template_id)
30111 /* If we've seen a '<', we might be in a
30112 template-argument-list. Until Core issue 325 is
30113 resolved, we don't know how this situation ought
30114 to be handled, so try to DTRT. We check whether
30115 what comes after the comma is a valid parameter
30116 declaration list. If it is, then the comma ends
30117 the default argument; otherwise the default
30118 argument continues. */
30119 bool error = false;
30120 cp_token *peek;
30122 /* Set ITALP so cp_parser_parameter_declaration_list
30123 doesn't decide to commit to this parse. */
30124 bool saved_italp = parser->in_template_argument_list_p;
30125 parser->in_template_argument_list_p = true;
30127 cp_parser_parse_tentatively (parser);
30129 if (nsdmi)
30131 /* Parse declarators until we reach a non-comma or
30132 somthing that cannot be an initializer.
30133 Just checking whether we're looking at a single
30134 declarator is insufficient. Consider:
30135 int var = tuple<T,U>::x;
30136 The template parameter 'U' looks exactly like a
30137 declarator. */
30140 int ctor_dtor_or_conv_p;
30141 cp_lexer_consume_token (parser->lexer);
30142 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30143 CP_PARSER_FLAGS_NONE,
30144 &ctor_dtor_or_conv_p,
30145 /*parenthesized_p=*/NULL,
30146 /*member_p=*/true,
30147 /*friend_p=*/false,
30148 /*static_p=*/false);
30149 peek = cp_lexer_peek_token (parser->lexer);
30150 if (cp_parser_error_occurred (parser))
30151 break;
30153 while (peek->type == CPP_COMMA);
30154 /* If we met an '=' or ';' then the original comma
30155 was the end of the NSDMI. Otherwise assume
30156 we're still in the NSDMI. */
30157 error = (peek->type != CPP_EQ
30158 && peek->type != CPP_SEMICOLON);
30160 else
30162 cp_lexer_consume_token (parser->lexer);
30163 begin_scope (sk_function_parms, NULL_TREE);
30164 tree t = cp_parser_parameter_declaration_list
30165 (parser, CP_PARSER_FLAGS_NONE);
30166 if (t == error_mark_node)
30167 error = true;
30168 pop_bindings_and_leave_scope ();
30170 if (!cp_parser_error_occurred (parser) && !error)
30171 done = true;
30172 cp_parser_abort_tentative_parse (parser);
30174 parser->in_template_argument_list_p = saved_italp;
30175 break;
30177 /* FALLTHRU */
30178 case CPP_CLOSE_PAREN:
30179 case CPP_ELLIPSIS:
30180 /* If we run into a non-nested `;', `}', or `]',
30181 then the code is invalid -- but the default
30182 argument is certainly over. */
30183 case CPP_SEMICOLON:
30184 case CPP_CLOSE_BRACE:
30185 case CPP_CLOSE_SQUARE:
30186 if (depth == 0
30187 /* Handle correctly int n = sizeof ... ( p ); */
30188 && token->type != CPP_ELLIPSIS)
30189 done = true;
30190 /* Update DEPTH, if necessary. */
30191 else if (token->type == CPP_CLOSE_PAREN
30192 || token->type == CPP_CLOSE_BRACE
30193 || token->type == CPP_CLOSE_SQUARE)
30194 --depth;
30195 break;
30197 case CPP_OPEN_PAREN:
30198 case CPP_OPEN_SQUARE:
30199 case CPP_OPEN_BRACE:
30200 ++depth;
30201 break;
30203 case CPP_LESS:
30204 if (depth == 0)
30205 /* This might be the comparison operator, or it might
30206 start a template argument list. */
30207 ++maybe_template_id;
30208 break;
30210 case CPP_RSHIFT:
30211 if (cxx_dialect == cxx98)
30212 break;
30213 /* Fall through for C++0x, which treats the `>>'
30214 operator like two `>' tokens in certain
30215 cases. */
30216 gcc_fallthrough ();
30218 case CPP_GREATER:
30219 if (depth == 0)
30221 /* This might be an operator, or it might close a
30222 template argument list. But if a previous '<'
30223 started a template argument list, this will have
30224 closed it, so we can't be in one anymore. */
30225 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
30226 if (maybe_template_id < 0)
30227 maybe_template_id = 0;
30229 break;
30231 /* If we run out of tokens, issue an error message. */
30232 case CPP_EOF:
30233 case CPP_PRAGMA_EOL:
30234 error_at (token->location, "file ends in default argument");
30235 return error_mark_node;
30237 case CPP_NAME:
30238 case CPP_SCOPE:
30239 /* In these cases, we should look for template-ids.
30240 For example, if the default argument is
30241 `X<int, double>()', we need to do name lookup to
30242 figure out whether or not `X' is a template; if
30243 so, the `,' does not end the default argument.
30245 That is not yet done. */
30246 break;
30248 default:
30249 break;
30252 /* If we've reached the end, stop. */
30253 if (done)
30254 break;
30256 /* Add the token to the token block. */
30257 token = cp_lexer_consume_token (parser->lexer);
30260 /* Create a DEFERRED_PARSE to represent the unparsed default
30261 argument. */
30262 default_argument = make_node (DEFERRED_PARSE);
30263 DEFPARSE_TOKENS (default_argument)
30264 = cp_token_cache_new (first_token, token);
30265 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
30267 return default_argument;
30270 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
30272 location_t
30273 defparse_location (tree default_argument)
30275 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
30276 location_t start = tokens->first->location;
30277 location_t end = tokens->last->location;
30278 return make_location (start, start, end);
30281 /* Begin parsing tentatively. We always save tokens while parsing
30282 tentatively so that if the tentative parsing fails we can restore the
30283 tokens. */
30285 static void
30286 cp_parser_parse_tentatively (cp_parser* parser)
30288 /* Enter a new parsing context. */
30289 parser->context = cp_parser_context_new (parser->context);
30290 /* Begin saving tokens. */
30291 cp_lexer_save_tokens (parser->lexer);
30292 /* In order to avoid repetitive access control error messages,
30293 access checks are queued up until we are no longer parsing
30294 tentatively. */
30295 push_deferring_access_checks (dk_deferred);
30298 /* Commit to the currently active tentative parse. */
30300 static void
30301 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30303 cp_parser_context *context;
30304 cp_lexer *lexer;
30306 /* Mark all of the levels as committed. */
30307 lexer = parser->lexer;
30308 for (context = parser->context; context->next; context = context->next)
30310 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30311 break;
30312 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30313 while (!cp_lexer_saving_tokens (lexer))
30314 lexer = lexer->next;
30315 cp_lexer_commit_tokens (lexer);
30319 /* Commit to the topmost currently active tentative parse.
30321 Note that this function shouldn't be called when there are
30322 irreversible side-effects while in a tentative state. For
30323 example, we shouldn't create a permanent entry in the symbol
30324 table, or issue an error message that might not apply if the
30325 tentative parse is aborted. */
30327 static void
30328 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
30330 cp_parser_context *context = parser->context;
30331 cp_lexer *lexer = parser->lexer;
30333 if (context)
30335 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30336 return;
30337 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30339 while (!cp_lexer_saving_tokens (lexer))
30340 lexer = lexer->next;
30341 cp_lexer_commit_tokens (lexer);
30345 /* Abort the currently active tentative parse. All consumed tokens
30346 will be rolled back, and no diagnostics will be issued. */
30348 static void
30349 cp_parser_abort_tentative_parse (cp_parser* parser)
30351 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30352 || errorcount > 0);
30353 cp_parser_simulate_error (parser);
30354 /* Now, pretend that we want to see if the construct was
30355 successfully parsed. */
30356 cp_parser_parse_definitely (parser);
30359 /* Stop parsing tentatively. If a parse error has occurred, restore the
30360 token stream. Otherwise, commit to the tokens we have consumed.
30361 Returns true if no error occurred; false otherwise. */
30363 static bool
30364 cp_parser_parse_definitely (cp_parser* parser)
30366 bool error_occurred;
30367 cp_parser_context *context;
30369 /* Remember whether or not an error occurred, since we are about to
30370 destroy that information. */
30371 error_occurred = cp_parser_error_occurred (parser);
30372 /* Remove the topmost context from the stack. */
30373 context = parser->context;
30374 parser->context = context->next;
30375 /* If no parse errors occurred, commit to the tentative parse. */
30376 if (!error_occurred)
30378 /* Commit to the tokens read tentatively, unless that was
30379 already done. */
30380 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30381 cp_lexer_commit_tokens (parser->lexer);
30383 pop_to_parent_deferring_access_checks ();
30385 /* Otherwise, if errors occurred, roll back our state so that things
30386 are just as they were before we began the tentative parse. */
30387 else
30389 cp_lexer_rollback_tokens (parser->lexer);
30390 pop_deferring_access_checks ();
30392 /* Add the context to the front of the free list. */
30393 context->next = cp_parser_context_free_list;
30394 cp_parser_context_free_list = context;
30396 return !error_occurred;
30399 /* Returns true if we are parsing tentatively and are not committed to
30400 this tentative parse. */
30402 static bool
30403 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30405 return (cp_parser_parsing_tentatively (parser)
30406 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30409 /* Returns nonzero iff an error has occurred during the most recent
30410 tentative parse. */
30412 static bool
30413 cp_parser_error_occurred (cp_parser* parser)
30415 return (cp_parser_parsing_tentatively (parser)
30416 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30419 /* Returns nonzero if GNU extensions are allowed. */
30421 static bool
30422 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30424 return parser->allow_gnu_extensions_p;
30427 /* Objective-C++ Productions */
30430 /* Parse an Objective-C expression, which feeds into a primary-expression
30431 above.
30433 objc-expression:
30434 objc-message-expression
30435 objc-string-literal
30436 objc-encode-expression
30437 objc-protocol-expression
30438 objc-selector-expression
30440 Returns a tree representation of the expression. */
30442 static cp_expr
30443 cp_parser_objc_expression (cp_parser* parser)
30445 /* Try to figure out what kind of declaration is present. */
30446 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30448 switch (kwd->type)
30450 case CPP_OPEN_SQUARE:
30451 return cp_parser_objc_message_expression (parser);
30453 case CPP_OBJC_STRING:
30454 kwd = cp_lexer_consume_token (parser->lexer);
30455 return objc_build_string_object (kwd->u.value);
30457 case CPP_KEYWORD:
30458 switch (kwd->keyword)
30460 case RID_AT_ENCODE:
30461 return cp_parser_objc_encode_expression (parser);
30463 case RID_AT_PROTOCOL:
30464 return cp_parser_objc_protocol_expression (parser);
30466 case RID_AT_SELECTOR:
30467 return cp_parser_objc_selector_expression (parser);
30469 default:
30470 break;
30472 /* FALLTHRU */
30473 default:
30474 error_at (kwd->location,
30475 "misplaced %<@%D%> Objective-C++ construct",
30476 kwd->u.value);
30477 cp_parser_skip_to_end_of_block_or_statement (parser);
30480 return error_mark_node;
30483 /* Parse an Objective-C message expression.
30485 objc-message-expression:
30486 [ objc-message-receiver objc-message-args ]
30488 Returns a representation of an Objective-C message. */
30490 static tree
30491 cp_parser_objc_message_expression (cp_parser* parser)
30493 tree receiver, messageargs;
30495 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30496 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30497 receiver = cp_parser_objc_message_receiver (parser);
30498 messageargs = cp_parser_objc_message_args (parser);
30499 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30500 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30502 tree result = objc_build_message_expr (receiver, messageargs);
30504 /* Construct a location e.g.
30505 [self func1:5]
30506 ^~~~~~~~~~~~~~
30507 ranging from the '[' to the ']', with the caret at the start. */
30508 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30509 protected_set_expr_location (result, combined_loc);
30511 return result;
30514 /* Parse an objc-message-receiver.
30516 objc-message-receiver:
30517 expression
30518 simple-type-specifier
30520 Returns a representation of the type or expression. */
30522 static tree
30523 cp_parser_objc_message_receiver (cp_parser* parser)
30525 tree rcv;
30527 /* An Objective-C message receiver may be either (1) a type
30528 or (2) an expression. */
30529 cp_parser_parse_tentatively (parser);
30530 rcv = cp_parser_expression (parser);
30532 /* If that worked out, fine. */
30533 if (cp_parser_parse_definitely (parser))
30534 return rcv;
30536 cp_parser_parse_tentatively (parser);
30537 rcv = cp_parser_simple_type_specifier (parser,
30538 /*decl_specs=*/NULL,
30539 CP_PARSER_FLAGS_NONE);
30541 if (cp_parser_parse_definitely (parser))
30542 return objc_get_class_reference (rcv);
30544 cp_parser_error (parser, "objective-c++ message receiver expected");
30545 return error_mark_node;
30548 /* Parse the arguments and selectors comprising an Objective-C message.
30550 objc-message-args:
30551 objc-selector
30552 objc-selector-args
30553 objc-selector-args , objc-comma-args
30555 objc-selector-args:
30556 objc-selector [opt] : assignment-expression
30557 objc-selector-args objc-selector [opt] : assignment-expression
30559 objc-comma-args:
30560 assignment-expression
30561 objc-comma-args , assignment-expression
30563 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30564 selector arguments and TREE_VALUE containing a list of comma
30565 arguments. */
30567 static tree
30568 cp_parser_objc_message_args (cp_parser* parser)
30570 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30571 bool maybe_unary_selector_p = true;
30572 cp_token *token = cp_lexer_peek_token (parser->lexer);
30574 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30576 tree selector = NULL_TREE, arg;
30578 if (token->type != CPP_COLON)
30579 selector = cp_parser_objc_selector (parser);
30581 /* Detect if we have a unary selector. */
30582 if (maybe_unary_selector_p
30583 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30584 return build_tree_list (selector, NULL_TREE);
30586 maybe_unary_selector_p = false;
30587 cp_parser_require (parser, CPP_COLON, RT_COLON);
30588 arg = cp_parser_assignment_expression (parser);
30590 sel_args
30591 = chainon (sel_args,
30592 build_tree_list (selector, arg));
30594 token = cp_lexer_peek_token (parser->lexer);
30597 /* Handle non-selector arguments, if any. */
30598 while (token->type == CPP_COMMA)
30600 tree arg;
30602 cp_lexer_consume_token (parser->lexer);
30603 arg = cp_parser_assignment_expression (parser);
30605 addl_args
30606 = chainon (addl_args,
30607 build_tree_list (NULL_TREE, arg));
30609 token = cp_lexer_peek_token (parser->lexer);
30612 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30614 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30615 return build_tree_list (error_mark_node, error_mark_node);
30618 return build_tree_list (sel_args, addl_args);
30621 /* Parse an Objective-C encode expression.
30623 objc-encode-expression:
30624 @encode objc-typename
30626 Returns an encoded representation of the type argument. */
30628 static cp_expr
30629 cp_parser_objc_encode_expression (cp_parser* parser)
30631 tree type;
30632 cp_token *token;
30633 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30635 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30636 matching_parens parens;
30637 parens.require_open (parser);
30638 token = cp_lexer_peek_token (parser->lexer);
30639 type = complete_type (cp_parser_type_id (parser));
30640 parens.require_close (parser);
30642 if (!type)
30644 error_at (token->location,
30645 "%<@encode%> must specify a type as an argument");
30646 return error_mark_node;
30649 /* This happens if we find @encode(T) (where T is a template
30650 typename or something dependent on a template typename) when
30651 parsing a template. In that case, we can't compile it
30652 immediately, but we rather create an AT_ENCODE_EXPR which will
30653 need to be instantiated when the template is used.
30655 if (dependent_type_p (type))
30657 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30658 TREE_READONLY (value) = 1;
30659 return value;
30663 /* Build a location of the form:
30664 @encode(int)
30665 ^~~~~~~~~~~~
30666 with caret==start at the @ token, finishing at the close paren. */
30667 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
30669 return cp_expr (objc_build_encode_expr (type), combined_loc);
30672 /* Parse an Objective-C @defs expression. */
30674 static tree
30675 cp_parser_objc_defs_expression (cp_parser *parser)
30677 tree name;
30679 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30680 matching_parens parens;
30681 parens.require_open (parser);
30682 name = cp_parser_identifier (parser);
30683 parens.require_close (parser);
30685 return objc_get_class_ivars (name);
30688 /* Parse an Objective-C protocol expression.
30690 objc-protocol-expression:
30691 @protocol ( identifier )
30693 Returns a representation of the protocol expression. */
30695 static tree
30696 cp_parser_objc_protocol_expression (cp_parser* parser)
30698 tree proto;
30699 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30701 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30702 matching_parens parens;
30703 parens.require_open (parser);
30704 proto = cp_parser_identifier (parser);
30705 parens.require_close (parser);
30707 /* Build a location of the form:
30708 @protocol(prot)
30709 ^~~~~~~~~~~~~~~
30710 with caret==start at the @ token, finishing at the close paren. */
30711 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
30712 tree result = objc_build_protocol_expr (proto);
30713 protected_set_expr_location (result, combined_loc);
30714 return result;
30717 /* Parse an Objective-C selector expression.
30719 objc-selector-expression:
30720 @selector ( objc-method-signature )
30722 objc-method-signature:
30723 objc-selector
30724 objc-selector-seq
30726 objc-selector-seq:
30727 objc-selector :
30728 objc-selector-seq objc-selector :
30730 Returns a representation of the method selector. */
30732 static tree
30733 cp_parser_objc_selector_expression (cp_parser* parser)
30735 tree sel_seq = NULL_TREE;
30736 bool maybe_unary_selector_p = true;
30737 cp_token *token;
30738 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30740 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30741 matching_parens parens;
30742 parens.require_open (parser);
30743 token = cp_lexer_peek_token (parser->lexer);
30745 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30746 || token->type == CPP_SCOPE)
30748 tree selector = NULL_TREE;
30750 if (token->type != CPP_COLON
30751 || token->type == CPP_SCOPE)
30752 selector = cp_parser_objc_selector (parser);
30754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30755 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30757 /* Detect if we have a unary selector. */
30758 if (maybe_unary_selector_p)
30760 sel_seq = selector;
30761 goto finish_selector;
30763 else
30765 cp_parser_error (parser, "expected %<:%>");
30768 maybe_unary_selector_p = false;
30769 token = cp_lexer_consume_token (parser->lexer);
30771 if (token->type == CPP_SCOPE)
30773 sel_seq
30774 = chainon (sel_seq,
30775 build_tree_list (selector, NULL_TREE));
30776 sel_seq
30777 = chainon (sel_seq,
30778 build_tree_list (NULL_TREE, NULL_TREE));
30780 else
30781 sel_seq
30782 = chainon (sel_seq,
30783 build_tree_list (selector, NULL_TREE));
30785 token = cp_lexer_peek_token (parser->lexer);
30788 finish_selector:
30789 parens.require_close (parser);
30792 /* Build a location of the form:
30793 @selector(func)
30794 ^~~~~~~~~~~~~~~
30795 with caret==start at the @ token, finishing at the close paren. */
30796 location_t combined_loc = make_location (loc, loc, parser->lexer);
30797 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30798 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30799 protected_set_expr_location (result, combined_loc);
30800 return result;
30803 /* Parse a list of identifiers.
30805 objc-identifier-list:
30806 identifier
30807 objc-identifier-list , identifier
30809 Returns a TREE_LIST of identifier nodes. */
30811 static tree
30812 cp_parser_objc_identifier_list (cp_parser* parser)
30814 tree identifier;
30815 tree list;
30816 cp_token *sep;
30818 identifier = cp_parser_identifier (parser);
30819 if (identifier == error_mark_node)
30820 return error_mark_node;
30822 list = build_tree_list (NULL_TREE, identifier);
30823 sep = cp_lexer_peek_token (parser->lexer);
30825 while (sep->type == CPP_COMMA)
30827 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30828 identifier = cp_parser_identifier (parser);
30829 if (identifier == error_mark_node)
30830 return list;
30832 list = chainon (list, build_tree_list (NULL_TREE,
30833 identifier));
30834 sep = cp_lexer_peek_token (parser->lexer);
30837 return list;
30840 /* Parse an Objective-C alias declaration.
30842 objc-alias-declaration:
30843 @compatibility_alias identifier identifier ;
30845 This function registers the alias mapping with the Objective-C front end.
30846 It returns nothing. */
30848 static void
30849 cp_parser_objc_alias_declaration (cp_parser* parser)
30851 tree alias, orig;
30853 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30854 alias = cp_parser_identifier (parser);
30855 orig = cp_parser_identifier (parser);
30856 objc_declare_alias (alias, orig);
30857 cp_parser_consume_semicolon_at_end_of_statement (parser);
30860 /* Parse an Objective-C class forward-declaration.
30862 objc-class-declaration:
30863 @class objc-identifier-list ;
30865 The function registers the forward declarations with the Objective-C
30866 front end. It returns nothing. */
30868 static void
30869 cp_parser_objc_class_declaration (cp_parser* parser)
30871 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30872 while (true)
30874 tree id;
30876 id = cp_parser_identifier (parser);
30877 if (id == error_mark_node)
30878 break;
30880 objc_declare_class (id);
30882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30883 cp_lexer_consume_token (parser->lexer);
30884 else
30885 break;
30887 cp_parser_consume_semicolon_at_end_of_statement (parser);
30890 /* Parse a list of Objective-C protocol references.
30892 objc-protocol-refs-opt:
30893 objc-protocol-refs [opt]
30895 objc-protocol-refs:
30896 < objc-identifier-list >
30898 Returns a TREE_LIST of identifiers, if any. */
30900 static tree
30901 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30903 tree protorefs = NULL_TREE;
30905 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30907 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30908 protorefs = cp_parser_objc_identifier_list (parser);
30909 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30912 return protorefs;
30915 /* Parse a Objective-C visibility specification. */
30917 static void
30918 cp_parser_objc_visibility_spec (cp_parser* parser)
30920 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30922 switch (vis->keyword)
30924 case RID_AT_PRIVATE:
30925 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30926 break;
30927 case RID_AT_PROTECTED:
30928 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30929 break;
30930 case RID_AT_PUBLIC:
30931 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30932 break;
30933 case RID_AT_PACKAGE:
30934 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30935 break;
30936 default:
30937 return;
30940 /* Eat '@private'/'@protected'/'@public'. */
30941 cp_lexer_consume_token (parser->lexer);
30944 /* Parse an Objective-C method type. Return 'true' if it is a class
30945 (+) method, and 'false' if it is an instance (-) method. */
30947 static inline bool
30948 cp_parser_objc_method_type (cp_parser* parser)
30950 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30951 return true;
30952 else
30953 return false;
30956 /* Parse an Objective-C protocol qualifier. */
30958 static tree
30959 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30961 tree quals = NULL_TREE, node;
30962 cp_token *token = cp_lexer_peek_token (parser->lexer);
30964 node = token->u.value;
30966 while (node && identifier_p (node)
30967 && (node == ridpointers [(int) RID_IN]
30968 || node == ridpointers [(int) RID_OUT]
30969 || node == ridpointers [(int) RID_INOUT]
30970 || node == ridpointers [(int) RID_BYCOPY]
30971 || node == ridpointers [(int) RID_BYREF]
30972 || node == ridpointers [(int) RID_ONEWAY]))
30974 quals = tree_cons (NULL_TREE, node, quals);
30975 cp_lexer_consume_token (parser->lexer);
30976 token = cp_lexer_peek_token (parser->lexer);
30977 node = token->u.value;
30980 return quals;
30983 /* Parse an Objective-C typename. */
30985 static tree
30986 cp_parser_objc_typename (cp_parser* parser)
30988 tree type_name = NULL_TREE;
30990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30992 tree proto_quals, cp_type = NULL_TREE;
30994 matching_parens parens;
30995 parens.consume_open (parser); /* Eat '('. */
30996 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30998 /* An ObjC type name may consist of just protocol qualifiers, in which
30999 case the type shall default to 'id'. */
31000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31002 cp_type = cp_parser_type_id (parser);
31004 /* If the type could not be parsed, an error has already
31005 been produced. For error recovery, behave as if it had
31006 not been specified, which will use the default type
31007 'id'. */
31008 if (cp_type == error_mark_node)
31010 cp_type = NULL_TREE;
31011 /* We need to skip to the closing parenthesis as
31012 cp_parser_type_id() does not seem to do it for
31013 us. */
31014 cp_parser_skip_to_closing_parenthesis (parser,
31015 /*recovering=*/true,
31016 /*or_comma=*/false,
31017 /*consume_paren=*/false);
31021 parens.require_close (parser);
31022 type_name = build_tree_list (proto_quals, cp_type);
31025 return type_name;
31028 /* Check to see if TYPE refers to an Objective-C selector name. */
31030 static bool
31031 cp_parser_objc_selector_p (enum cpp_ttype type)
31033 return (type == CPP_NAME || type == CPP_KEYWORD
31034 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
31035 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
31036 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
31037 || type == CPP_XOR || type == CPP_XOR_EQ);
31040 /* Parse an Objective-C selector. */
31042 static tree
31043 cp_parser_objc_selector (cp_parser* parser)
31045 cp_token *token = cp_lexer_consume_token (parser->lexer);
31047 if (!cp_parser_objc_selector_p (token->type))
31049 error_at (token->location, "invalid Objective-C++ selector name");
31050 return error_mark_node;
31053 /* C++ operator names are allowed to appear in ObjC selectors. */
31054 switch (token->type)
31056 case CPP_AND_AND: return get_identifier ("and");
31057 case CPP_AND_EQ: return get_identifier ("and_eq");
31058 case CPP_AND: return get_identifier ("bitand");
31059 case CPP_OR: return get_identifier ("bitor");
31060 case CPP_COMPL: return get_identifier ("compl");
31061 case CPP_NOT: return get_identifier ("not");
31062 case CPP_NOT_EQ: return get_identifier ("not_eq");
31063 case CPP_OR_OR: return get_identifier ("or");
31064 case CPP_OR_EQ: return get_identifier ("or_eq");
31065 case CPP_XOR: return get_identifier ("xor");
31066 case CPP_XOR_EQ: return get_identifier ("xor_eq");
31067 default: return token->u.value;
31071 /* Parse an Objective-C params list. */
31073 static tree
31074 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
31076 tree params = NULL_TREE;
31077 bool maybe_unary_selector_p = true;
31078 cp_token *token = cp_lexer_peek_token (parser->lexer);
31080 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
31082 tree selector = NULL_TREE, type_name, identifier;
31083 tree parm_attr = NULL_TREE;
31085 if (token->keyword == RID_ATTRIBUTE)
31086 break;
31088 if (token->type != CPP_COLON)
31089 selector = cp_parser_objc_selector (parser);
31091 /* Detect if we have a unary selector. */
31092 if (maybe_unary_selector_p
31093 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31095 params = selector; /* Might be followed by attributes. */
31096 break;
31099 maybe_unary_selector_p = false;
31100 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31102 /* Something went quite wrong. There should be a colon
31103 here, but there is not. Stop parsing parameters. */
31104 break;
31106 type_name = cp_parser_objc_typename (parser);
31107 /* New ObjC allows attributes on parameters too. */
31108 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31109 parm_attr = cp_parser_attributes_opt (parser);
31110 identifier = cp_parser_identifier (parser);
31112 params
31113 = chainon (params,
31114 objc_build_keyword_decl (selector,
31115 type_name,
31116 identifier,
31117 parm_attr));
31119 token = cp_lexer_peek_token (parser->lexer);
31122 if (params == NULL_TREE)
31124 cp_parser_error (parser, "objective-c++ method declaration is expected");
31125 return error_mark_node;
31128 /* We allow tail attributes for the method. */
31129 if (token->keyword == RID_ATTRIBUTE)
31131 *attributes = cp_parser_attributes_opt (parser);
31132 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31133 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31134 return params;
31135 cp_parser_error (parser,
31136 "method attributes must be specified at the end");
31137 return error_mark_node;
31140 if (params == NULL_TREE)
31142 cp_parser_error (parser, "objective-c++ method declaration is expected");
31143 return error_mark_node;
31145 return params;
31148 /* Parse the non-keyword Objective-C params. */
31150 static tree
31151 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
31152 tree* attributes)
31154 tree params = make_node (TREE_LIST);
31155 cp_token *token = cp_lexer_peek_token (parser->lexer);
31156 *ellipsisp = false; /* Initially, assume no ellipsis. */
31158 while (token->type == CPP_COMMA)
31160 cp_parameter_declarator *parmdecl;
31161 tree parm;
31163 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31164 token = cp_lexer_peek_token (parser->lexer);
31166 if (token->type == CPP_ELLIPSIS)
31168 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
31169 *ellipsisp = true;
31170 token = cp_lexer_peek_token (parser->lexer);
31171 break;
31174 /* TODO: parse attributes for tail parameters. */
31175 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31176 false, NULL);
31177 parm = grokdeclarator (parmdecl->declarator,
31178 &parmdecl->decl_specifiers,
31179 PARM, /*initialized=*/0,
31180 /*attrlist=*/NULL);
31182 chainon (params, build_tree_list (NULL_TREE, parm));
31183 token = cp_lexer_peek_token (parser->lexer);
31186 /* We allow tail attributes for the method. */
31187 if (token->keyword == RID_ATTRIBUTE)
31189 if (*attributes == NULL_TREE)
31191 *attributes = cp_parser_attributes_opt (parser);
31192 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31193 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31194 return params;
31196 else
31197 /* We have an error, but parse the attributes, so that we can
31198 carry on. */
31199 *attributes = cp_parser_attributes_opt (parser);
31201 cp_parser_error (parser,
31202 "method attributes must be specified at the end");
31203 return error_mark_node;
31206 return params;
31209 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
31211 static void
31212 cp_parser_objc_interstitial_code (cp_parser* parser)
31214 cp_token *token = cp_lexer_peek_token (parser->lexer);
31216 /* If the next token is `extern' and the following token is a string
31217 literal, then we have a linkage specification. */
31218 if (token->keyword == RID_EXTERN
31219 && cp_parser_is_pure_string_literal
31220 (cp_lexer_peek_nth_token (parser->lexer, 2)))
31221 cp_parser_linkage_specification (parser);
31222 /* Handle #pragma, if any. */
31223 else if (token->type == CPP_PRAGMA)
31224 cp_parser_pragma (parser, pragma_objc_icode, NULL);
31225 /* Allow stray semicolons. */
31226 else if (token->type == CPP_SEMICOLON)
31227 cp_lexer_consume_token (parser->lexer);
31228 /* Mark methods as optional or required, when building protocols. */
31229 else if (token->keyword == RID_AT_OPTIONAL)
31231 cp_lexer_consume_token (parser->lexer);
31232 objc_set_method_opt (true);
31234 else if (token->keyword == RID_AT_REQUIRED)
31236 cp_lexer_consume_token (parser->lexer);
31237 objc_set_method_opt (false);
31239 else if (token->keyword == RID_NAMESPACE)
31240 cp_parser_namespace_definition (parser);
31241 /* Other stray characters must generate errors. */
31242 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31244 cp_lexer_consume_token (parser->lexer);
31245 error ("stray %qs between Objective-C++ methods",
31246 token->type == CPP_OPEN_BRACE ? "{" : "}");
31248 /* Finally, try to parse a block-declaration, or a function-definition. */
31249 else
31250 cp_parser_block_declaration (parser, /*statement_p=*/false);
31253 /* Parse a method signature. */
31255 static tree
31256 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31258 tree rettype, kwdparms, optparms;
31259 bool ellipsis = false;
31260 bool is_class_method;
31262 is_class_method = cp_parser_objc_method_type (parser);
31263 rettype = cp_parser_objc_typename (parser);
31264 *attributes = NULL_TREE;
31265 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31266 if (kwdparms == error_mark_node)
31267 return error_mark_node;
31268 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31269 if (optparms == error_mark_node)
31270 return error_mark_node;
31272 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31275 static bool
31276 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31278 tree tattr;
31279 cp_lexer_save_tokens (parser->lexer);
31280 tattr = cp_parser_attributes_opt (parser);
31281 gcc_assert (tattr) ;
31283 /* If the attributes are followed by a method introducer, this is not allowed.
31284 Dump the attributes and flag the situation. */
31285 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31286 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31287 return true;
31289 /* Otherwise, the attributes introduce some interstitial code, possibly so
31290 rewind to allow that check. */
31291 cp_lexer_rollback_tokens (parser->lexer);
31292 return false;
31295 /* Parse an Objective-C method prototype list. */
31297 static void
31298 cp_parser_objc_method_prototype_list (cp_parser* parser)
31300 cp_token *token = cp_lexer_peek_token (parser->lexer);
31302 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31304 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31306 tree attributes, sig;
31307 bool is_class_method;
31308 if (token->type == CPP_PLUS)
31309 is_class_method = true;
31310 else
31311 is_class_method = false;
31312 sig = cp_parser_objc_method_signature (parser, &attributes);
31313 if (sig == error_mark_node)
31315 cp_parser_skip_to_end_of_block_or_statement (parser);
31316 token = cp_lexer_peek_token (parser->lexer);
31317 continue;
31319 objc_add_method_declaration (is_class_method, sig, attributes);
31320 cp_parser_consume_semicolon_at_end_of_statement (parser);
31322 else if (token->keyword == RID_AT_PROPERTY)
31323 cp_parser_objc_at_property_declaration (parser);
31324 else if (token->keyword == RID_ATTRIBUTE
31325 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31326 warning_at (cp_lexer_peek_token (parser->lexer)->location,
31327 OPT_Wattributes,
31328 "prefix attributes are ignored for methods");
31329 else
31330 /* Allow for interspersed non-ObjC++ code. */
31331 cp_parser_objc_interstitial_code (parser);
31333 token = cp_lexer_peek_token (parser->lexer);
31336 if (token->type != CPP_EOF)
31337 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31338 else
31339 cp_parser_error (parser, "expected %<@end%>");
31341 objc_finish_interface ();
31344 /* Parse an Objective-C method definition list. */
31346 static void
31347 cp_parser_objc_method_definition_list (cp_parser* parser)
31349 cp_token *token = cp_lexer_peek_token (parser->lexer);
31351 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31353 tree meth;
31355 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31357 cp_token *ptk;
31358 tree sig, attribute;
31359 bool is_class_method;
31360 if (token->type == CPP_PLUS)
31361 is_class_method = true;
31362 else
31363 is_class_method = false;
31364 push_deferring_access_checks (dk_deferred);
31365 sig = cp_parser_objc_method_signature (parser, &attribute);
31366 if (sig == error_mark_node)
31368 cp_parser_skip_to_end_of_block_or_statement (parser);
31369 token = cp_lexer_peek_token (parser->lexer);
31370 continue;
31372 objc_start_method_definition (is_class_method, sig, attribute,
31373 NULL_TREE);
31375 /* For historical reasons, we accept an optional semicolon. */
31376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31377 cp_lexer_consume_token (parser->lexer);
31379 ptk = cp_lexer_peek_token (parser->lexer);
31380 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31381 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31383 perform_deferred_access_checks (tf_warning_or_error);
31384 stop_deferring_access_checks ();
31385 meth = cp_parser_function_definition_after_declarator (parser,
31386 false);
31387 pop_deferring_access_checks ();
31388 objc_finish_method_definition (meth);
31391 /* The following case will be removed once @synthesize is
31392 completely implemented. */
31393 else if (token->keyword == RID_AT_PROPERTY)
31394 cp_parser_objc_at_property_declaration (parser);
31395 else if (token->keyword == RID_AT_SYNTHESIZE)
31396 cp_parser_objc_at_synthesize_declaration (parser);
31397 else if (token->keyword == RID_AT_DYNAMIC)
31398 cp_parser_objc_at_dynamic_declaration (parser);
31399 else if (token->keyword == RID_ATTRIBUTE
31400 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31401 warning_at (token->location, OPT_Wattributes,
31402 "prefix attributes are ignored for methods");
31403 else
31404 /* Allow for interspersed non-ObjC++ code. */
31405 cp_parser_objc_interstitial_code (parser);
31407 token = cp_lexer_peek_token (parser->lexer);
31410 if (token->type != CPP_EOF)
31411 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31412 else
31413 cp_parser_error (parser, "expected %<@end%>");
31415 objc_finish_implementation ();
31418 /* Parse Objective-C ivars. */
31420 static void
31421 cp_parser_objc_class_ivars (cp_parser* parser)
31423 cp_token *token = cp_lexer_peek_token (parser->lexer);
31425 if (token->type != CPP_OPEN_BRACE)
31426 return; /* No ivars specified. */
31428 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31429 token = cp_lexer_peek_token (parser->lexer);
31431 while (token->type != CPP_CLOSE_BRACE
31432 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31434 cp_decl_specifier_seq declspecs;
31435 int decl_class_or_enum_p;
31436 tree prefix_attributes;
31438 cp_parser_objc_visibility_spec (parser);
31440 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31441 break;
31443 cp_parser_decl_specifier_seq (parser,
31444 CP_PARSER_FLAGS_OPTIONAL,
31445 &declspecs,
31446 &decl_class_or_enum_p);
31448 /* auto, register, static, extern, mutable. */
31449 if (declspecs.storage_class != sc_none)
31451 cp_parser_error (parser, "invalid type for instance variable");
31452 declspecs.storage_class = sc_none;
31455 /* thread_local. */
31456 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31458 cp_parser_error (parser, "invalid type for instance variable");
31459 declspecs.locations[ds_thread] = 0;
31462 /* typedef. */
31463 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31465 cp_parser_error (parser, "invalid type for instance variable");
31466 declspecs.locations[ds_typedef] = 0;
31469 prefix_attributes = declspecs.attributes;
31470 declspecs.attributes = NULL_TREE;
31472 /* Keep going until we hit the `;' at the end of the
31473 declaration. */
31474 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31476 tree width = NULL_TREE, attributes, first_attribute, decl;
31477 cp_declarator *declarator = NULL;
31478 int ctor_dtor_or_conv_p;
31480 /* Check for a (possibly unnamed) bitfield declaration. */
31481 token = cp_lexer_peek_token (parser->lexer);
31482 if (token->type == CPP_COLON)
31483 goto eat_colon;
31485 if (token->type == CPP_NAME
31486 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31487 == CPP_COLON))
31489 /* Get the name of the bitfield. */
31490 declarator = make_id_declarator (NULL_TREE,
31491 cp_parser_identifier (parser),
31492 sfk_none, token->location);
31494 eat_colon:
31495 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31496 /* Get the width of the bitfield. */
31497 width
31498 = cp_parser_constant_expression (parser);
31500 else
31502 /* Parse the declarator. */
31503 declarator
31504 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31505 CP_PARSER_FLAGS_NONE,
31506 &ctor_dtor_or_conv_p,
31507 /*parenthesized_p=*/NULL,
31508 /*member_p=*/false,
31509 /*friend_p=*/false,
31510 /*static_p=*/false);
31513 /* Look for attributes that apply to the ivar. */
31514 attributes = cp_parser_attributes_opt (parser);
31515 /* Remember which attributes are prefix attributes and
31516 which are not. */
31517 first_attribute = attributes;
31518 /* Combine the attributes. */
31519 attributes = attr_chainon (prefix_attributes, attributes);
31521 if (width)
31522 /* Create the bitfield declaration. */
31523 decl = grokbitfield (declarator, &declspecs,
31524 width, NULL_TREE, attributes);
31525 else
31526 decl = grokfield (declarator, &declspecs,
31527 NULL_TREE, /*init_const_expr_p=*/false,
31528 NULL_TREE, attributes);
31530 /* Add the instance variable. */
31531 if (decl != error_mark_node && decl != NULL_TREE)
31532 objc_add_instance_variable (decl);
31534 /* Reset PREFIX_ATTRIBUTES. */
31535 if (attributes != error_mark_node)
31537 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31538 attributes = TREE_CHAIN (attributes);
31539 if (attributes)
31540 TREE_CHAIN (attributes) = NULL_TREE;
31543 token = cp_lexer_peek_token (parser->lexer);
31545 if (token->type == CPP_COMMA)
31547 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31548 continue;
31550 break;
31553 cp_parser_consume_semicolon_at_end_of_statement (parser);
31554 token = cp_lexer_peek_token (parser->lexer);
31557 if (token->keyword == RID_AT_END)
31558 cp_parser_error (parser, "expected %<}%>");
31560 /* Do not consume the RID_AT_END, so it will be read again as terminating
31561 the @interface of @implementation. */
31562 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31563 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31565 /* For historical reasons, we accept an optional semicolon. */
31566 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31567 cp_lexer_consume_token (parser->lexer);
31570 /* Parse an Objective-C protocol declaration. */
31572 static void
31573 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31575 tree proto, protorefs;
31576 cp_token *tok;
31578 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31581 tok = cp_lexer_peek_token (parser->lexer);
31582 error_at (tok->location, "identifier expected after %<@protocol%>");
31583 cp_parser_consume_semicolon_at_end_of_statement (parser);
31584 return;
31587 /* See if we have a forward declaration or a definition. */
31588 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31590 /* Try a forward declaration first. */
31591 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31593 while (true)
31595 tree id;
31597 id = cp_parser_identifier (parser);
31598 if (id == error_mark_node)
31599 break;
31601 objc_declare_protocol (id, attributes);
31603 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31604 cp_lexer_consume_token (parser->lexer);
31605 else
31606 break;
31608 cp_parser_consume_semicolon_at_end_of_statement (parser);
31611 /* Ok, we got a full-fledged definition (or at least should). */
31612 else
31614 proto = cp_parser_identifier (parser);
31615 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31616 objc_start_protocol (proto, protorefs, attributes);
31617 cp_parser_objc_method_prototype_list (parser);
31621 /* Parse an Objective-C superclass or category. */
31623 static void
31624 cp_parser_objc_superclass_or_category (cp_parser *parser,
31625 bool iface_p,
31626 tree *super,
31627 tree *categ, bool *is_class_extension)
31629 cp_token *next = cp_lexer_peek_token (parser->lexer);
31631 *super = *categ = NULL_TREE;
31632 *is_class_extension = false;
31633 if (next->type == CPP_COLON)
31635 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31636 *super = cp_parser_identifier (parser);
31638 else if (next->type == CPP_OPEN_PAREN)
31640 matching_parens parens;
31641 parens.consume_open (parser); /* Eat '('. */
31643 /* If there is no category name, and this is an @interface, we
31644 have a class extension. */
31645 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31647 *categ = NULL_TREE;
31648 *is_class_extension = true;
31650 else
31651 *categ = cp_parser_identifier (parser);
31653 parens.require_close (parser);
31657 /* Parse an Objective-C class interface. */
31659 static void
31660 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31662 tree name, super, categ, protos;
31663 bool is_class_extension;
31665 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31666 name = cp_parser_identifier (parser);
31667 if (name == error_mark_node)
31669 /* It's hard to recover because even if valid @interface stuff
31670 is to follow, we can't compile it (or validate it) if we
31671 don't even know which class it refers to. Let's assume this
31672 was a stray '@interface' token in the stream and skip it.
31674 return;
31676 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31677 &is_class_extension);
31678 protos = cp_parser_objc_protocol_refs_opt (parser);
31680 /* We have either a class or a category on our hands. */
31681 if (categ || is_class_extension)
31682 objc_start_category_interface (name, categ, protos, attributes);
31683 else
31685 objc_start_class_interface (name, super, protos, attributes);
31686 /* Handle instance variable declarations, if any. */
31687 cp_parser_objc_class_ivars (parser);
31688 objc_continue_interface ();
31691 cp_parser_objc_method_prototype_list (parser);
31694 /* Parse an Objective-C class implementation. */
31696 static void
31697 cp_parser_objc_class_implementation (cp_parser* parser)
31699 tree name, super, categ;
31700 bool is_class_extension;
31702 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31703 name = cp_parser_identifier (parser);
31704 if (name == error_mark_node)
31706 /* It's hard to recover because even if valid @implementation
31707 stuff is to follow, we can't compile it (or validate it) if
31708 we don't even know which class it refers to. Let's assume
31709 this was a stray '@implementation' token in the stream and
31710 skip it.
31712 return;
31714 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31715 &is_class_extension);
31717 /* We have either a class or a category on our hands. */
31718 if (categ)
31719 objc_start_category_implementation (name, categ);
31720 else
31722 objc_start_class_implementation (name, super);
31723 /* Handle instance variable declarations, if any. */
31724 cp_parser_objc_class_ivars (parser);
31725 objc_continue_implementation ();
31728 cp_parser_objc_method_definition_list (parser);
31731 /* Consume the @end token and finish off the implementation. */
31733 static void
31734 cp_parser_objc_end_implementation (cp_parser* parser)
31736 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31737 objc_finish_implementation ();
31740 /* Parse an Objective-C declaration. */
31742 static void
31743 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31745 /* Try to figure out what kind of declaration is present. */
31746 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31748 if (attributes)
31749 switch (kwd->keyword)
31751 case RID_AT_ALIAS:
31752 case RID_AT_CLASS:
31753 case RID_AT_END:
31754 error_at (kwd->location, "attributes may not be specified before"
31755 " the %<@%D%> Objective-C++ keyword",
31756 kwd->u.value);
31757 attributes = NULL;
31758 break;
31759 case RID_AT_IMPLEMENTATION:
31760 warning_at (kwd->location, OPT_Wattributes,
31761 "prefix attributes are ignored before %<@%D%>",
31762 kwd->u.value);
31763 attributes = NULL;
31764 default:
31765 break;
31768 switch (kwd->keyword)
31770 case RID_AT_ALIAS:
31771 cp_parser_objc_alias_declaration (parser);
31772 break;
31773 case RID_AT_CLASS:
31774 cp_parser_objc_class_declaration (parser);
31775 break;
31776 case RID_AT_PROTOCOL:
31777 cp_parser_objc_protocol_declaration (parser, attributes);
31778 break;
31779 case RID_AT_INTERFACE:
31780 cp_parser_objc_class_interface (parser, attributes);
31781 break;
31782 case RID_AT_IMPLEMENTATION:
31783 cp_parser_objc_class_implementation (parser);
31784 break;
31785 case RID_AT_END:
31786 cp_parser_objc_end_implementation (parser);
31787 break;
31788 default:
31789 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31790 kwd->u.value);
31791 cp_parser_skip_to_end_of_block_or_statement (parser);
31795 /* Parse an Objective-C try-catch-finally statement.
31797 objc-try-catch-finally-stmt:
31798 @try compound-statement objc-catch-clause-seq [opt]
31799 objc-finally-clause [opt]
31801 objc-catch-clause-seq:
31802 objc-catch-clause objc-catch-clause-seq [opt]
31804 objc-catch-clause:
31805 @catch ( objc-exception-declaration ) compound-statement
31807 objc-finally-clause:
31808 @finally compound-statement
31810 objc-exception-declaration:
31811 parameter-declaration
31812 '...'
31814 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31816 Returns NULL_TREE.
31818 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31819 for C. Keep them in sync. */
31821 static tree
31822 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31824 location_t location;
31825 tree stmt;
31827 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31828 location = cp_lexer_peek_token (parser->lexer)->location;
31829 objc_maybe_warn_exceptions (location);
31830 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31831 node, lest it get absorbed into the surrounding block. */
31832 stmt = push_stmt_list ();
31833 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31834 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31836 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31838 cp_parameter_declarator *parm;
31839 tree parameter_declaration = error_mark_node;
31840 bool seen_open_paren = false;
31841 matching_parens parens;
31843 cp_lexer_consume_token (parser->lexer);
31844 if (parens.require_open (parser))
31845 seen_open_paren = true;
31846 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31848 /* We have "@catch (...)" (where the '...' are literally
31849 what is in the code). Skip the '...'.
31850 parameter_declaration is set to NULL_TREE, and
31851 objc_being_catch_clauses() knows that that means
31852 '...'. */
31853 cp_lexer_consume_token (parser->lexer);
31854 parameter_declaration = NULL_TREE;
31856 else
31858 /* We have "@catch (NSException *exception)" or something
31859 like that. Parse the parameter declaration. */
31860 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31861 false, NULL);
31862 if (parm == NULL)
31863 parameter_declaration = error_mark_node;
31864 else
31865 parameter_declaration = grokdeclarator (parm->declarator,
31866 &parm->decl_specifiers,
31867 PARM, /*initialized=*/0,
31868 /*attrlist=*/NULL);
31870 if (seen_open_paren)
31871 parens.require_close (parser);
31872 else
31874 /* If there was no open parenthesis, we are recovering from
31875 an error, and we are trying to figure out what mistake
31876 the user has made. */
31878 /* If there is an immediate closing parenthesis, the user
31879 probably forgot the opening one (ie, they typed "@catch
31880 NSException *e)". Parse the closing parenthesis and keep
31881 going. */
31882 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31883 cp_lexer_consume_token (parser->lexer);
31885 /* If these is no immediate closing parenthesis, the user
31886 probably doesn't know that parenthesis are required at
31887 all (ie, they typed "@catch NSException *e"). So, just
31888 forget about the closing parenthesis and keep going. */
31890 objc_begin_catch_clause (parameter_declaration);
31891 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31892 objc_finish_catch_clause ();
31894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31896 cp_lexer_consume_token (parser->lexer);
31897 location = cp_lexer_peek_token (parser->lexer)->location;
31898 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31899 node, lest it get absorbed into the surrounding block. */
31900 stmt = push_stmt_list ();
31901 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31902 objc_build_finally_clause (location, pop_stmt_list (stmt));
31905 return objc_finish_try_stmt ();
31908 /* Parse an Objective-C synchronized statement.
31910 objc-synchronized-stmt:
31911 @synchronized ( expression ) compound-statement
31913 Returns NULL_TREE. */
31915 static tree
31916 cp_parser_objc_synchronized_statement (cp_parser *parser)
31918 location_t location;
31919 tree lock, stmt;
31921 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31923 location = cp_lexer_peek_token (parser->lexer)->location;
31924 objc_maybe_warn_exceptions (location);
31925 matching_parens parens;
31926 parens.require_open (parser);
31927 lock = cp_parser_expression (parser);
31928 parens.require_close (parser);
31930 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31931 node, lest it get absorbed into the surrounding block. */
31932 stmt = push_stmt_list ();
31933 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31935 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31938 /* Parse an Objective-C throw statement.
31940 objc-throw-stmt:
31941 @throw assignment-expression [opt] ;
31943 Returns a constructed '@throw' statement. */
31945 static tree
31946 cp_parser_objc_throw_statement (cp_parser *parser)
31948 tree expr = NULL_TREE;
31949 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31951 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31954 expr = cp_parser_expression (parser);
31956 cp_parser_consume_semicolon_at_end_of_statement (parser);
31958 return objc_build_throw_stmt (loc, expr);
31961 /* Parse an Objective-C statement. */
31963 static tree
31964 cp_parser_objc_statement (cp_parser * parser)
31966 /* Try to figure out what kind of declaration is present. */
31967 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31969 switch (kwd->keyword)
31971 case RID_AT_TRY:
31972 return cp_parser_objc_try_catch_finally_statement (parser);
31973 case RID_AT_SYNCHRONIZED:
31974 return cp_parser_objc_synchronized_statement (parser);
31975 case RID_AT_THROW:
31976 return cp_parser_objc_throw_statement (parser);
31977 default:
31978 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31979 kwd->u.value);
31980 cp_parser_skip_to_end_of_block_or_statement (parser);
31983 return error_mark_node;
31986 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31987 look ahead to see if an objc keyword follows the attributes. This
31988 is to detect the use of prefix attributes on ObjC @interface and
31989 @protocol. */
31991 static bool
31992 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31994 cp_lexer_save_tokens (parser->lexer);
31995 *attrib = cp_parser_attributes_opt (parser);
31996 gcc_assert (*attrib);
31997 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31999 cp_lexer_commit_tokens (parser->lexer);
32000 return true;
32002 cp_lexer_rollback_tokens (parser->lexer);
32003 return false;
32006 /* This routine is a minimal replacement for
32007 c_parser_struct_declaration () used when parsing the list of
32008 types/names or ObjC++ properties. For example, when parsing the
32009 code
32011 @property (readonly) int a, b, c;
32013 this function is responsible for parsing "int a, int b, int c" and
32014 returning the declarations as CHAIN of DECLs.
32016 TODO: Share this code with cp_parser_objc_class_ivars. It's very
32017 similar parsing. */
32018 static tree
32019 cp_parser_objc_struct_declaration (cp_parser *parser)
32021 tree decls = NULL_TREE;
32022 cp_decl_specifier_seq declspecs;
32023 int decl_class_or_enum_p;
32024 tree prefix_attributes;
32026 cp_parser_decl_specifier_seq (parser,
32027 CP_PARSER_FLAGS_NONE,
32028 &declspecs,
32029 &decl_class_or_enum_p);
32031 if (declspecs.type == error_mark_node)
32032 return error_mark_node;
32034 /* auto, register, static, extern, mutable. */
32035 if (declspecs.storage_class != sc_none)
32037 cp_parser_error (parser, "invalid type for property");
32038 declspecs.storage_class = sc_none;
32041 /* thread_local. */
32042 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
32044 cp_parser_error (parser, "invalid type for property");
32045 declspecs.locations[ds_thread] = 0;
32048 /* typedef. */
32049 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
32051 cp_parser_error (parser, "invalid type for property");
32052 declspecs.locations[ds_typedef] = 0;
32055 prefix_attributes = declspecs.attributes;
32056 declspecs.attributes = NULL_TREE;
32058 /* Keep going until we hit the `;' at the end of the declaration. */
32059 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32061 tree attributes, first_attribute, decl;
32062 cp_declarator *declarator;
32063 cp_token *token;
32065 /* Parse the declarator. */
32066 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32067 CP_PARSER_FLAGS_NONE,
32068 NULL, NULL, false, false, false);
32070 /* Look for attributes that apply to the ivar. */
32071 attributes = cp_parser_attributes_opt (parser);
32072 /* Remember which attributes are prefix attributes and
32073 which are not. */
32074 first_attribute = attributes;
32075 /* Combine the attributes. */
32076 attributes = attr_chainon (prefix_attributes, attributes);
32078 decl = grokfield (declarator, &declspecs,
32079 NULL_TREE, /*init_const_expr_p=*/false,
32080 NULL_TREE, attributes);
32082 if (decl == error_mark_node || decl == NULL_TREE)
32083 return error_mark_node;
32085 /* Reset PREFIX_ATTRIBUTES. */
32086 if (attributes != error_mark_node)
32088 while (attributes && TREE_CHAIN (attributes) != first_attribute)
32089 attributes = TREE_CHAIN (attributes);
32090 if (attributes)
32091 TREE_CHAIN (attributes) = NULL_TREE;
32094 DECL_CHAIN (decl) = decls;
32095 decls = decl;
32097 token = cp_lexer_peek_token (parser->lexer);
32098 if (token->type == CPP_COMMA)
32100 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32101 continue;
32103 else
32104 break;
32106 return decls;
32109 /* Parse an Objective-C @property declaration. The syntax is:
32111 objc-property-declaration:
32112 '@property' objc-property-attributes[opt] struct-declaration ;
32114 objc-property-attributes:
32115 '(' objc-property-attribute-list ')'
32117 objc-property-attribute-list:
32118 objc-property-attribute
32119 objc-property-attribute-list, objc-property-attribute
32121 objc-property-attribute
32122 'getter' = identifier
32123 'setter' = identifier
32124 'readonly'
32125 'readwrite'
32126 'assign'
32127 'retain'
32128 'copy'
32129 'nonatomic'
32131 For example:
32132 @property NSString *name;
32133 @property (readonly) id object;
32134 @property (retain, nonatomic, getter=getTheName) id name;
32135 @property int a, b, c;
32137 PS: This function is identical to
32138 c_parser_objc_at_property_declaration for C. Keep them in sync. */
32139 static void
32140 cp_parser_objc_at_property_declaration (cp_parser *parser)
32142 /* The following variables hold the attributes of the properties as
32143 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
32144 seen. When we see an attribute, we set them to 'true' (if they
32145 are boolean properties) or to the identifier (if they have an
32146 argument, ie, for getter and setter). Note that here we only
32147 parse the list of attributes, check the syntax and accumulate the
32148 attributes that we find. objc_add_property_declaration() will
32149 then process the information. */
32150 bool property_assign = false;
32151 bool property_copy = false;
32152 tree property_getter_ident = NULL_TREE;
32153 bool property_nonatomic = false;
32154 bool property_readonly = false;
32155 bool property_readwrite = false;
32156 bool property_retain = false;
32157 tree property_setter_ident = NULL_TREE;
32159 /* 'properties' is the list of properties that we read. Usually a
32160 single one, but maybe more (eg, in "@property int a, b, c;" there
32161 are three). */
32162 tree properties;
32163 location_t loc;
32165 loc = cp_lexer_peek_token (parser->lexer)->location;
32167 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
32169 /* Parse the optional attribute list... */
32170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32172 /* Eat the '('. */
32173 matching_parens parens;
32174 parens.consume_open (parser);
32176 while (true)
32178 bool syntax_error = false;
32179 cp_token *token = cp_lexer_peek_token (parser->lexer);
32180 enum rid keyword;
32182 if (token->type != CPP_NAME)
32184 cp_parser_error (parser, "expected identifier");
32185 break;
32187 keyword = C_RID_CODE (token->u.value);
32188 cp_lexer_consume_token (parser->lexer);
32189 switch (keyword)
32191 case RID_ASSIGN: property_assign = true; break;
32192 case RID_COPY: property_copy = true; break;
32193 case RID_NONATOMIC: property_nonatomic = true; break;
32194 case RID_READONLY: property_readonly = true; break;
32195 case RID_READWRITE: property_readwrite = true; break;
32196 case RID_RETAIN: property_retain = true; break;
32198 case RID_GETTER:
32199 case RID_SETTER:
32200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32202 if (keyword == RID_GETTER)
32203 cp_parser_error (parser,
32204 "missing %<=%> (after %<getter%> attribute)");
32205 else
32206 cp_parser_error (parser,
32207 "missing %<=%> (after %<setter%> attribute)");
32208 syntax_error = true;
32209 break;
32211 cp_lexer_consume_token (parser->lexer); /* eat the = */
32212 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
32214 cp_parser_error (parser, "expected identifier");
32215 syntax_error = true;
32216 break;
32218 if (keyword == RID_SETTER)
32220 if (property_setter_ident != NULL_TREE)
32222 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
32223 cp_lexer_consume_token (parser->lexer);
32225 else
32226 property_setter_ident = cp_parser_objc_selector (parser);
32227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32228 cp_parser_error (parser, "setter name must terminate with %<:%>");
32229 else
32230 cp_lexer_consume_token (parser->lexer);
32232 else
32234 if (property_getter_ident != NULL_TREE)
32236 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32237 cp_lexer_consume_token (parser->lexer);
32239 else
32240 property_getter_ident = cp_parser_objc_selector (parser);
32242 break;
32243 default:
32244 cp_parser_error (parser, "unknown property attribute");
32245 syntax_error = true;
32246 break;
32249 if (syntax_error)
32250 break;
32252 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32253 cp_lexer_consume_token (parser->lexer);
32254 else
32255 break;
32258 /* FIXME: "@property (setter, assign);" will generate a spurious
32259 "error: expected ‘)’ before ‘,’ token". This is because
32260 cp_parser_require, unlike the C counterpart, will produce an
32261 error even if we are in error recovery. */
32262 if (!parens.require_close (parser))
32264 cp_parser_skip_to_closing_parenthesis (parser,
32265 /*recovering=*/true,
32266 /*or_comma=*/false,
32267 /*consume_paren=*/true);
32271 /* ... and the property declaration(s). */
32272 properties = cp_parser_objc_struct_declaration (parser);
32274 if (properties == error_mark_node)
32276 cp_parser_skip_to_end_of_statement (parser);
32277 /* If the next token is now a `;', consume it. */
32278 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32279 cp_lexer_consume_token (parser->lexer);
32280 return;
32283 if (properties == NULL_TREE)
32284 cp_parser_error (parser, "expected identifier");
32285 else
32287 /* Comma-separated properties are chained together in
32288 reverse order; add them one by one. */
32289 properties = nreverse (properties);
32291 for (; properties; properties = TREE_CHAIN (properties))
32292 objc_add_property_declaration (loc, copy_node (properties),
32293 property_readonly, property_readwrite,
32294 property_assign, property_retain,
32295 property_copy, property_nonatomic,
32296 property_getter_ident, property_setter_ident);
32299 cp_parser_consume_semicolon_at_end_of_statement (parser);
32302 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32304 objc-synthesize-declaration:
32305 @synthesize objc-synthesize-identifier-list ;
32307 objc-synthesize-identifier-list:
32308 objc-synthesize-identifier
32309 objc-synthesize-identifier-list, objc-synthesize-identifier
32311 objc-synthesize-identifier
32312 identifier
32313 identifier = identifier
32315 For example:
32316 @synthesize MyProperty;
32317 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32319 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32320 for C. Keep them in sync.
32322 static void
32323 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32325 tree list = NULL_TREE;
32326 location_t loc;
32327 loc = cp_lexer_peek_token (parser->lexer)->location;
32329 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
32330 while (true)
32332 tree property, ivar;
32333 property = cp_parser_identifier (parser);
32334 if (property == error_mark_node)
32336 cp_parser_consume_semicolon_at_end_of_statement (parser);
32337 return;
32339 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32341 cp_lexer_consume_token (parser->lexer);
32342 ivar = cp_parser_identifier (parser);
32343 if (ivar == error_mark_node)
32345 cp_parser_consume_semicolon_at_end_of_statement (parser);
32346 return;
32349 else
32350 ivar = NULL_TREE;
32351 list = chainon (list, build_tree_list (ivar, property));
32352 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32353 cp_lexer_consume_token (parser->lexer);
32354 else
32355 break;
32357 cp_parser_consume_semicolon_at_end_of_statement (parser);
32358 objc_add_synthesize_declaration (loc, list);
32361 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32363 objc-dynamic-declaration:
32364 @dynamic identifier-list ;
32366 For example:
32367 @dynamic MyProperty;
32368 @dynamic MyProperty, AnotherProperty;
32370 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32371 for C. Keep them in sync.
32373 static void
32374 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32376 tree list = NULL_TREE;
32377 location_t loc;
32378 loc = cp_lexer_peek_token (parser->lexer)->location;
32380 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32381 while (true)
32383 tree property;
32384 property = cp_parser_identifier (parser);
32385 if (property == error_mark_node)
32387 cp_parser_consume_semicolon_at_end_of_statement (parser);
32388 return;
32390 list = chainon (list, build_tree_list (NULL, property));
32391 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32392 cp_lexer_consume_token (parser->lexer);
32393 else
32394 break;
32396 cp_parser_consume_semicolon_at_end_of_statement (parser);
32397 objc_add_dynamic_declaration (loc, list);
32401 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32403 /* Returns name of the next clause.
32404 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32405 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32406 returned and the token is consumed. */
32408 static pragma_omp_clause
32409 cp_parser_omp_clause_name (cp_parser *parser)
32411 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32414 result = PRAGMA_OACC_CLAUSE_AUTO;
32415 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32416 result = PRAGMA_OMP_CLAUSE_IF;
32417 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32418 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32419 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32420 result = PRAGMA_OACC_CLAUSE_DELETE;
32421 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32422 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32423 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32424 result = PRAGMA_OMP_CLAUSE_FOR;
32425 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32428 const char *p = IDENTIFIER_POINTER (id);
32430 switch (p[0])
32432 case 'a':
32433 if (!strcmp ("aligned", p))
32434 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32435 else if (!strcmp ("async", p))
32436 result = PRAGMA_OACC_CLAUSE_ASYNC;
32437 break;
32438 case 'b':
32439 if (!strcmp ("bind", p))
32440 result = PRAGMA_OMP_CLAUSE_BIND;
32441 break;
32442 case 'c':
32443 if (!strcmp ("collapse", p))
32444 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32445 else if (!strcmp ("copy", p))
32446 result = PRAGMA_OACC_CLAUSE_COPY;
32447 else if (!strcmp ("copyin", p))
32448 result = PRAGMA_OMP_CLAUSE_COPYIN;
32449 else if (!strcmp ("copyout", p))
32450 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32451 else if (!strcmp ("copyprivate", p))
32452 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32453 else if (!strcmp ("create", p))
32454 result = PRAGMA_OACC_CLAUSE_CREATE;
32455 break;
32456 case 'd':
32457 if (!strcmp ("defaultmap", p))
32458 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32459 else if (!strcmp ("depend", p))
32460 result = PRAGMA_OMP_CLAUSE_DEPEND;
32461 else if (!strcmp ("device", p))
32462 result = PRAGMA_OMP_CLAUSE_DEVICE;
32463 else if (!strcmp ("deviceptr", p))
32464 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32465 else if (!strcmp ("device_resident", p))
32466 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32467 else if (!strcmp ("device_type", p))
32468 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
32469 else if (!strcmp ("dist_schedule", p))
32470 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32471 break;
32472 case 'f':
32473 if (!strcmp ("final", p))
32474 result = PRAGMA_OMP_CLAUSE_FINAL;
32475 else if (!strcmp ("finalize", p))
32476 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32477 else if (!strcmp ("firstprivate", p))
32478 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32479 else if (!strcmp ("from", p))
32480 result = PRAGMA_OMP_CLAUSE_FROM;
32481 break;
32482 case 'g':
32483 if (!strcmp ("gang", p))
32484 result = PRAGMA_OACC_CLAUSE_GANG;
32485 else if (!strcmp ("grainsize", p))
32486 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32487 break;
32488 case 'h':
32489 if (!strcmp ("hint", p))
32490 result = PRAGMA_OMP_CLAUSE_HINT;
32491 else if (!strcmp ("host", p))
32492 result = PRAGMA_OACC_CLAUSE_HOST;
32493 break;
32494 case 'i':
32495 if (!strcmp ("if_present", p))
32496 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32497 else if (!strcmp ("in_reduction", p))
32498 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32499 else if (!strcmp ("inbranch", p))
32500 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32501 else if (!strcmp ("independent", p))
32502 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32503 else if (!strcmp ("is_device_ptr", p))
32504 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32505 break;
32506 case 'l':
32507 if (!strcmp ("lastprivate", p))
32508 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32509 else if (!strcmp ("linear", p))
32510 result = PRAGMA_OMP_CLAUSE_LINEAR;
32511 else if (!strcmp ("link", p))
32512 result = PRAGMA_OMP_CLAUSE_LINK;
32513 break;
32514 case 'm':
32515 if (!strcmp ("map", p))
32516 result = PRAGMA_OMP_CLAUSE_MAP;
32517 else if (!strcmp ("mergeable", p))
32518 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32519 break;
32520 case 'n':
32521 if (!strcmp ("nogroup", p))
32522 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32523 else if (!strcmp ("nontemporal", p))
32524 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32525 else if (!strcmp ("notinbranch", p))
32526 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32527 else if (!strcmp ("nowait", p))
32528 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32529 else if (!strcmp ("num_gangs", p))
32530 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32531 else if (!strcmp ("num_tasks", p))
32532 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32533 else if (!strcmp ("num_teams", p))
32534 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32535 else if (!strcmp ("num_threads", p))
32536 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32537 else if (!strcmp ("num_workers", p))
32538 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32539 break;
32540 case 'o':
32541 if (!strcmp ("ordered", p))
32542 result = PRAGMA_OMP_CLAUSE_ORDERED;
32543 else if (!strcmp ("order", p))
32544 result = PRAGMA_OMP_CLAUSE_ORDER;
32545 break;
32546 case 'p':
32547 if (!strcmp ("parallel", p))
32548 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32549 else if (!strcmp ("present", p))
32550 result = PRAGMA_OACC_CLAUSE_PRESENT;
32551 else if (!strcmp ("present_or_copy", p)
32552 || !strcmp ("pcopy", p))
32553 result = PRAGMA_OACC_CLAUSE_COPY;
32554 else if (!strcmp ("present_or_copyin", p)
32555 || !strcmp ("pcopyin", p))
32556 result = PRAGMA_OACC_CLAUSE_COPYIN;
32557 else if (!strcmp ("present_or_copyout", p)
32558 || !strcmp ("pcopyout", p))
32559 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32560 else if (!strcmp ("present_or_create", p)
32561 || !strcmp ("pcreate", p))
32562 result = PRAGMA_OACC_CLAUSE_CREATE;
32563 else if (!strcmp ("priority", p))
32564 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32565 else if (!strcmp ("proc_bind", p))
32566 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32567 break;
32568 case 'r':
32569 if (!strcmp ("reduction", p))
32570 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32571 break;
32572 case 's':
32573 if (!strcmp ("safelen", p))
32574 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32575 else if (!strcmp ("schedule", p))
32576 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32577 else if (!strcmp ("sections", p))
32578 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32579 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32580 result = PRAGMA_OACC_CLAUSE_HOST;
32581 else if (!strcmp ("seq", p))
32582 result = PRAGMA_OACC_CLAUSE_SEQ;
32583 else if (!strcmp ("shared", p))
32584 result = PRAGMA_OMP_CLAUSE_SHARED;
32585 else if (!strcmp ("simd", p))
32586 result = PRAGMA_OMP_CLAUSE_SIMD;
32587 else if (!strcmp ("simdlen", p))
32588 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32589 break;
32590 case 't':
32591 if (!strcmp ("task_reduction", p))
32592 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32593 else if (!strcmp ("taskgroup", p))
32594 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32595 else if (!strcmp ("thread_limit", p))
32596 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32597 else if (!strcmp ("threads", p))
32598 result = PRAGMA_OMP_CLAUSE_THREADS;
32599 else if (!strcmp ("tile", p))
32600 result = PRAGMA_OACC_CLAUSE_TILE;
32601 else if (!strcmp ("to", p))
32602 result = PRAGMA_OMP_CLAUSE_TO;
32603 break;
32604 case 'u':
32605 if (!strcmp ("uniform", p))
32606 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32607 else if (!strcmp ("untied", p))
32608 result = PRAGMA_OMP_CLAUSE_UNTIED;
32609 else if (!strcmp ("use_device", p))
32610 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32611 else if (!strcmp ("use_device_addr", p))
32612 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
32613 else if (!strcmp ("use_device_ptr", p))
32614 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32615 break;
32616 case 'v':
32617 if (!strcmp ("vector", p))
32618 result = PRAGMA_OACC_CLAUSE_VECTOR;
32619 else if (!strcmp ("vector_length", p))
32620 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32621 break;
32622 case 'w':
32623 if (!strcmp ("wait", p))
32624 result = PRAGMA_OACC_CLAUSE_WAIT;
32625 else if (!strcmp ("worker", p))
32626 result = PRAGMA_OACC_CLAUSE_WORKER;
32627 break;
32631 if (result != PRAGMA_OMP_CLAUSE_NONE)
32632 cp_lexer_consume_token (parser->lexer);
32634 return result;
32637 /* Validate that a clause of the given type does not already exist. */
32639 static void
32640 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32641 const char *name, location_t location)
32643 if (omp_find_clause (clauses, code))
32644 error_at (location, "too many %qs clauses", name);
32647 /* OpenMP 2.5:
32648 variable-list:
32649 identifier
32650 variable-list , identifier
32652 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32653 colon). An opening parenthesis will have been consumed by the caller.
32655 If KIND is nonzero, create the appropriate node and install the decl
32656 in OMP_CLAUSE_DECL and add the node to the head of the list.
32658 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32659 return the list created.
32661 COLON can be NULL if only closing parenthesis should end the list,
32662 or pointer to bool which will receive false if the list is terminated
32663 by closing parenthesis or true if the list is terminated by colon. */
32665 static tree
32666 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32667 tree list, bool *colon)
32669 cp_token *token;
32670 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32671 if (colon)
32673 parser->colon_corrects_to_scope_p = false;
32674 *colon = false;
32676 while (1)
32678 tree name, decl;
32680 if (kind == OMP_CLAUSE_DEPEND)
32681 cp_parser_parse_tentatively (parser);
32682 token = cp_lexer_peek_token (parser->lexer);
32683 if (kind != 0
32684 && current_class_ptr
32685 && cp_parser_is_keyword (token, RID_THIS))
32687 decl = finish_this_expr ();
32688 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32689 || CONVERT_EXPR_P (decl))
32690 decl = TREE_OPERAND (decl, 0);
32691 cp_lexer_consume_token (parser->lexer);
32693 else
32695 name = cp_parser_id_expression (parser, /*template_p=*/false,
32696 /*check_dependency_p=*/true,
32697 /*template_p=*/NULL,
32698 /*declarator_p=*/false,
32699 /*optional_p=*/false);
32700 if (name == error_mark_node)
32702 if (kind == OMP_CLAUSE_DEPEND
32703 && cp_parser_simulate_error (parser))
32704 goto depend_lvalue;
32705 goto skip_comma;
32708 if (identifier_p (name))
32709 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32710 else
32711 decl = name;
32712 if (decl == error_mark_node)
32714 if (kind == OMP_CLAUSE_DEPEND
32715 && cp_parser_simulate_error (parser))
32716 goto depend_lvalue;
32717 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32718 token->location);
32721 if (decl == error_mark_node)
32723 else if (kind != 0)
32725 switch (kind)
32727 case OMP_CLAUSE__CACHE_:
32728 /* The OpenACC cache directive explicitly only allows "array
32729 elements or subarrays". */
32730 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32732 error_at (token->location, "expected %<[%>");
32733 decl = error_mark_node;
32734 break;
32736 /* FALLTHROUGH. */
32737 case OMP_CLAUSE_MAP:
32738 case OMP_CLAUSE_FROM:
32739 case OMP_CLAUSE_TO:
32740 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32742 location_t loc
32743 = cp_lexer_peek_token (parser->lexer)->location;
32744 cp_id_kind idk = CP_ID_KIND_NONE;
32745 cp_lexer_consume_token (parser->lexer);
32746 decl = convert_from_reference (decl);
32747 decl
32748 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32749 decl, false,
32750 &idk, loc);
32752 /* FALLTHROUGH. */
32753 case OMP_CLAUSE_DEPEND:
32754 case OMP_CLAUSE_REDUCTION:
32755 case OMP_CLAUSE_IN_REDUCTION:
32756 case OMP_CLAUSE_TASK_REDUCTION:
32757 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32759 tree low_bound = NULL_TREE, length = NULL_TREE;
32761 parser->colon_corrects_to_scope_p = false;
32762 cp_lexer_consume_token (parser->lexer);
32763 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32764 low_bound = cp_parser_expression (parser);
32765 if (!colon)
32766 parser->colon_corrects_to_scope_p
32767 = saved_colon_corrects_to_scope_p;
32768 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32769 length = integer_one_node;
32770 else
32772 /* Look for `:'. */
32773 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32775 if (kind == OMP_CLAUSE_DEPEND
32776 && cp_parser_simulate_error (parser))
32777 goto depend_lvalue;
32778 goto skip_comma;
32780 if (kind == OMP_CLAUSE_DEPEND)
32781 cp_parser_commit_to_tentative_parse (parser);
32782 if (!cp_lexer_next_token_is (parser->lexer,
32783 CPP_CLOSE_SQUARE))
32784 length = cp_parser_expression (parser);
32786 /* Look for the closing `]'. */
32787 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32788 RT_CLOSE_SQUARE))
32790 if (kind == OMP_CLAUSE_DEPEND
32791 && cp_parser_simulate_error (parser))
32792 goto depend_lvalue;
32793 goto skip_comma;
32796 decl = tree_cons (low_bound, length, decl);
32798 break;
32799 default:
32800 break;
32803 if (kind == OMP_CLAUSE_DEPEND)
32805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32806 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32807 && cp_parser_simulate_error (parser))
32809 depend_lvalue:
32810 cp_parser_abort_tentative_parse (parser);
32811 decl = cp_parser_assignment_expression (parser, NULL,
32812 false, false);
32814 else
32815 cp_parser_parse_definitely (parser);
32818 tree u = build_omp_clause (token->location, kind);
32819 OMP_CLAUSE_DECL (u) = decl;
32820 OMP_CLAUSE_CHAIN (u) = list;
32821 list = u;
32823 else
32824 list = tree_cons (decl, NULL_TREE, list);
32826 get_comma:
32827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32828 break;
32829 cp_lexer_consume_token (parser->lexer);
32832 if (colon)
32833 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32835 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32837 *colon = true;
32838 cp_parser_require (parser, CPP_COLON, RT_COLON);
32839 return list;
32842 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32844 int ending;
32846 /* Try to resync to an unnested comma. Copied from
32847 cp_parser_parenthesized_expression_list. */
32848 skip_comma:
32849 if (colon)
32850 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32851 ending = cp_parser_skip_to_closing_parenthesis (parser,
32852 /*recovering=*/true,
32853 /*or_comma=*/true,
32854 /*consume_paren=*/true);
32855 if (ending < 0)
32856 goto get_comma;
32859 return list;
32862 /* Similarly, but expect leading and trailing parenthesis. This is a very
32863 common case for omp clauses. */
32865 static tree
32866 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32868 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32869 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32870 return list;
32873 /* OpenACC 2.0:
32874 copy ( variable-list )
32875 copyin ( variable-list )
32876 copyout ( variable-list )
32877 create ( variable-list )
32878 delete ( variable-list )
32879 present ( variable-list ) */
32881 static tree
32882 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32883 tree list)
32885 enum gomp_map_kind kind;
32886 switch (c_kind)
32888 case PRAGMA_OACC_CLAUSE_COPY:
32889 kind = GOMP_MAP_TOFROM;
32890 break;
32891 case PRAGMA_OACC_CLAUSE_COPYIN:
32892 kind = GOMP_MAP_TO;
32893 break;
32894 case PRAGMA_OACC_CLAUSE_COPYOUT:
32895 kind = GOMP_MAP_FROM;
32896 break;
32897 case PRAGMA_OACC_CLAUSE_CREATE:
32898 kind = GOMP_MAP_ALLOC;
32899 break;
32900 case PRAGMA_OACC_CLAUSE_DELETE:
32901 kind = GOMP_MAP_RELEASE;
32902 break;
32903 case PRAGMA_OACC_CLAUSE_DEVICE:
32904 kind = GOMP_MAP_FORCE_TO;
32905 break;
32906 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32907 kind = GOMP_MAP_DEVICE_RESIDENT;
32908 break;
32909 case PRAGMA_OACC_CLAUSE_HOST:
32910 kind = GOMP_MAP_FORCE_FROM;
32911 break;
32912 case PRAGMA_OACC_CLAUSE_LINK:
32913 kind = GOMP_MAP_LINK;
32914 break;
32915 case PRAGMA_OACC_CLAUSE_PRESENT:
32916 kind = GOMP_MAP_FORCE_PRESENT;
32917 break;
32918 default:
32919 gcc_unreachable ();
32921 tree nl, c;
32922 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32924 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32925 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32927 return nl;
32930 /* OpenACC 2.0:
32931 deviceptr ( variable-list ) */
32933 static tree
32934 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32936 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32937 tree vars, t;
32939 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32940 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32941 variable-list must only allow for pointer variables. */
32942 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32943 for (t = vars; t; t = TREE_CHAIN (t))
32945 tree v = TREE_PURPOSE (t);
32946 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32947 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32948 OMP_CLAUSE_DECL (u) = v;
32949 OMP_CLAUSE_CHAIN (u) = list;
32950 list = u;
32953 return list;
32956 /* OpenACC 2.5:
32957 auto
32958 finalize
32959 independent
32960 nohost
32961 seq */
32963 static tree
32964 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32965 tree list)
32967 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32969 tree c = build_omp_clause (loc, code);
32970 OMP_CLAUSE_CHAIN (c) = list;
32972 return c;
32975 /* OpenACC:
32976 num_gangs ( expression )
32977 num_workers ( expression )
32978 vector_length ( expression ) */
32980 static tree
32981 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32982 const char *str, tree list)
32984 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32986 matching_parens parens;
32987 if (!parens.require_open (parser))
32988 return list;
32990 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32992 if (t == error_mark_node
32993 || !parens.require_close (parser))
32995 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32996 /*or_comma=*/false,
32997 /*consume_paren=*/true);
32998 return list;
33001 check_no_duplicate_clause (list, code, str, loc);
33003 tree c = build_omp_clause (loc, code);
33004 OMP_CLAUSE_OPERAND (c, 0) = t;
33005 OMP_CLAUSE_CHAIN (c) = list;
33006 return c;
33009 /* OpenACC:
33011 gang [( gang-arg-list )]
33012 worker [( [num:] int-expr )]
33013 vector [( [length:] int-expr )]
33015 where gang-arg is one of:
33017 [num:] int-expr
33018 static: size-expr
33020 and size-expr may be:
33023 int-expr
33026 static tree
33027 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
33028 omp_clause_code kind,
33029 const char *str, tree list)
33031 const char *id = "num";
33032 cp_lexer *lexer = parser->lexer;
33033 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
33035 if (kind == OMP_CLAUSE_VECTOR)
33036 id = "length";
33038 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
33040 matching_parens parens;
33041 parens.consume_open (parser);
33045 cp_token *next = cp_lexer_peek_token (lexer);
33046 int idx = 0;
33048 /* Gang static argument. */
33049 if (kind == OMP_CLAUSE_GANG
33050 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
33052 cp_lexer_consume_token (lexer);
33054 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33055 goto cleanup_error;
33057 idx = 1;
33058 if (ops[idx] != NULL)
33060 cp_parser_error (parser, "too many %<static%> arguments");
33061 goto cleanup_error;
33064 /* Check for the '*' argument. */
33065 if (cp_lexer_next_token_is (lexer, CPP_MULT)
33066 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33067 || cp_lexer_nth_token_is (parser->lexer, 2,
33068 CPP_CLOSE_PAREN)))
33070 cp_lexer_consume_token (lexer);
33071 ops[idx] = integer_minus_one_node;
33073 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
33075 cp_lexer_consume_token (lexer);
33076 continue;
33078 else break;
33081 /* Worker num: argument and vector length: arguments. */
33082 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
33083 && id_equal (next->u.value, id)
33084 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
33086 cp_lexer_consume_token (lexer); /* id */
33087 cp_lexer_consume_token (lexer); /* ':' */
33090 /* Now collect the actual argument. */
33091 if (ops[idx] != NULL_TREE)
33093 cp_parser_error (parser, "unexpected argument");
33094 goto cleanup_error;
33097 tree expr = cp_parser_assignment_expression (parser, NULL, false,
33098 false);
33099 if (expr == error_mark_node)
33100 goto cleanup_error;
33102 mark_exp_read (expr);
33103 ops[idx] = expr;
33105 if (kind == OMP_CLAUSE_GANG
33106 && cp_lexer_next_token_is (lexer, CPP_COMMA))
33108 cp_lexer_consume_token (lexer);
33109 continue;
33111 break;
33113 while (1);
33115 if (!parens.require_close (parser))
33116 goto cleanup_error;
33119 check_no_duplicate_clause (list, kind, str, loc);
33121 c = build_omp_clause (loc, kind);
33123 if (ops[1])
33124 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
33126 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
33127 OMP_CLAUSE_CHAIN (c) = list;
33129 return c;
33131 cleanup_error:
33132 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33133 return list;
33136 /* OpenACC 2.0:
33137 tile ( size-expr-list ) */
33139 static tree
33140 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
33142 tree c, expr = error_mark_node;
33143 tree tile = NULL_TREE;
33145 /* Collapse and tile are mutually exclusive. (The spec doesn't say
33146 so, but the spec authors never considered such a case and have
33147 differing opinions on what it might mean, including 'not
33148 allowed'.) */
33149 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
33150 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
33151 clause_loc);
33153 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33154 return list;
33158 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
33159 return list;
33161 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
33162 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33163 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
33165 cp_lexer_consume_token (parser->lexer);
33166 expr = integer_zero_node;
33168 else
33169 expr = cp_parser_constant_expression (parser);
33171 tile = tree_cons (NULL_TREE, expr, tile);
33173 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
33175 /* Consume the trailing ')'. */
33176 cp_lexer_consume_token (parser->lexer);
33178 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
33179 tile = nreverse (tile);
33180 OMP_CLAUSE_TILE_LIST (c) = tile;
33181 OMP_CLAUSE_CHAIN (c) = list;
33182 return c;
33185 /* OpenACC 2.0
33186 Parse wait clause or directive parameters. */
33188 static tree
33189 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
33191 vec<tree, va_gc> *args;
33192 tree t, args_tree;
33194 args = cp_parser_parenthesized_expression_list (parser, non_attr,
33195 /*cast_p=*/false,
33196 /*allow_expansion_p=*/true,
33197 /*non_constant_p=*/NULL);
33199 if (args == NULL || args->length () == 0)
33201 if (args != NULL)
33203 cp_parser_error (parser, "expected integer expression list");
33204 release_tree_vector (args);
33206 return list;
33209 args_tree = build_tree_list_vec (args);
33211 release_tree_vector (args);
33213 for (t = args_tree; t; t = TREE_CHAIN (t))
33215 tree targ = TREE_VALUE (t);
33217 if (targ != error_mark_node)
33219 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
33220 error ("%<wait%> expression must be integral");
33221 else
33223 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
33225 targ = mark_rvalue_use (targ);
33226 OMP_CLAUSE_DECL (c) = targ;
33227 OMP_CLAUSE_CHAIN (c) = list;
33228 list = c;
33233 return list;
33236 /* OpenACC:
33237 wait [( int-expr-list )] */
33239 static tree
33240 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33242 location_t location = cp_lexer_peek_token (parser->lexer)->location;
33244 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33245 list = cp_parser_oacc_wait_list (parser, location, list);
33246 else
33248 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33250 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33251 OMP_CLAUSE_CHAIN (c) = list;
33252 list = c;
33255 return list;
33258 /* OpenMP 3.0:
33259 collapse ( constant-expression ) */
33261 static tree
33262 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33264 tree c, num;
33265 location_t loc;
33266 HOST_WIDE_INT n;
33268 loc = cp_lexer_peek_token (parser->lexer)->location;
33269 matching_parens parens;
33270 if (!parens.require_open (parser))
33271 return list;
33273 num = cp_parser_constant_expression (parser);
33275 if (!parens.require_close (parser))
33276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33280 if (num == error_mark_node)
33281 return list;
33282 num = fold_non_dependent_expr (num);
33283 if (!tree_fits_shwi_p (num)
33284 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33285 || (n = tree_to_shwi (num)) <= 0
33286 || (int) n != n)
33288 error_at (loc, "collapse argument needs positive constant integer expression");
33289 return list;
33292 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33293 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33294 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33295 OMP_CLAUSE_CHAIN (c) = list;
33296 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33298 return c;
33301 /* OpenMP 2.5:
33302 default ( none | shared )
33304 OpenACC:
33305 default ( none | present ) */
33307 static tree
33308 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33309 location_t location, bool is_oacc)
33311 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33312 tree c;
33314 matching_parens parens;
33315 if (!parens.require_open (parser))
33316 return list;
33317 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33319 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33320 const char *p = IDENTIFIER_POINTER (id);
33322 switch (p[0])
33324 case 'n':
33325 if (strcmp ("none", p) != 0)
33326 goto invalid_kind;
33327 kind = OMP_CLAUSE_DEFAULT_NONE;
33328 break;
33330 case 'p':
33331 if (strcmp ("present", p) != 0 || !is_oacc)
33332 goto invalid_kind;
33333 kind = OMP_CLAUSE_DEFAULT_PRESENT;
33334 break;
33336 case 's':
33337 if (strcmp ("shared", p) != 0 || is_oacc)
33338 goto invalid_kind;
33339 kind = OMP_CLAUSE_DEFAULT_SHARED;
33340 break;
33342 default:
33343 goto invalid_kind;
33346 cp_lexer_consume_token (parser->lexer);
33348 else
33350 invalid_kind:
33351 if (is_oacc)
33352 cp_parser_error (parser, "expected %<none%> or %<present%>");
33353 else
33354 cp_parser_error (parser, "expected %<none%> or %<shared%>");
33357 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
33358 || !parens.require_close (parser))
33359 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33360 /*or_comma=*/false,
33361 /*consume_paren=*/true);
33363 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33364 return list;
33366 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33367 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33368 OMP_CLAUSE_CHAIN (c) = list;
33369 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33371 return c;
33374 /* OpenMP 3.1:
33375 final ( expression ) */
33377 static tree
33378 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33380 tree t, c;
33382 matching_parens parens;
33383 if (!parens.require_open (parser))
33384 return list;
33386 t = cp_parser_assignment_expression (parser);
33388 if (t == error_mark_node
33389 || !parens.require_close (parser))
33390 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33391 /*or_comma=*/false,
33392 /*consume_paren=*/true);
33394 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33396 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33397 OMP_CLAUSE_FINAL_EXPR (c) = t;
33398 OMP_CLAUSE_CHAIN (c) = list;
33400 return c;
33403 /* OpenMP 2.5:
33404 if ( expression )
33406 OpenMP 4.5:
33407 if ( directive-name-modifier : expression )
33409 directive-name-modifier:
33410 parallel | task | taskloop | target data | target | target update
33411 | target enter data | target exit data
33413 OpenMP 5.0:
33414 directive-name-modifier:
33415 ... | simd | cancel */
33417 static tree
33418 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33419 bool is_omp)
33421 tree t, c;
33422 enum tree_code if_modifier = ERROR_MARK;
33424 matching_parens parens;
33425 if (!parens.require_open (parser))
33426 return list;
33428 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33430 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33431 const char *p = IDENTIFIER_POINTER (id);
33432 int n = 2;
33434 if (strcmp ("cancel", p) == 0)
33435 if_modifier = VOID_CST;
33436 else if (strcmp ("parallel", p) == 0)
33437 if_modifier = OMP_PARALLEL;
33438 else if (strcmp ("simd", p) == 0)
33439 if_modifier = OMP_SIMD;
33440 else if (strcmp ("task", p) == 0)
33441 if_modifier = OMP_TASK;
33442 else if (strcmp ("taskloop", p) == 0)
33443 if_modifier = OMP_TASKLOOP;
33444 else if (strcmp ("target", p) == 0)
33446 if_modifier = OMP_TARGET;
33447 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33449 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33450 p = IDENTIFIER_POINTER (id);
33451 if (strcmp ("data", p) == 0)
33452 if_modifier = OMP_TARGET_DATA;
33453 else if (strcmp ("update", p) == 0)
33454 if_modifier = OMP_TARGET_UPDATE;
33455 else if (strcmp ("enter", p) == 0)
33456 if_modifier = OMP_TARGET_ENTER_DATA;
33457 else if (strcmp ("exit", p) == 0)
33458 if_modifier = OMP_TARGET_EXIT_DATA;
33459 if (if_modifier != OMP_TARGET)
33460 n = 3;
33461 else
33463 location_t loc
33464 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33465 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33466 "or %<exit%>");
33467 if_modifier = ERROR_MARK;
33469 if (if_modifier == OMP_TARGET_ENTER_DATA
33470 || if_modifier == OMP_TARGET_EXIT_DATA)
33472 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33474 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33475 p = IDENTIFIER_POINTER (id);
33476 if (strcmp ("data", p) == 0)
33477 n = 4;
33479 if (n != 4)
33481 location_t loc
33482 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33483 error_at (loc, "expected %<data%>");
33484 if_modifier = ERROR_MARK;
33489 if (if_modifier != ERROR_MARK)
33491 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33493 while (n-- > 0)
33494 cp_lexer_consume_token (parser->lexer);
33496 else
33498 if (n > 2)
33500 location_t loc
33501 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33502 error_at (loc, "expected %<:%>");
33504 if_modifier = ERROR_MARK;
33509 t = cp_parser_assignment_expression (parser);
33511 if (t == error_mark_node
33512 || !parens.require_close (parser))
33513 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33514 /*or_comma=*/false,
33515 /*consume_paren=*/true);
33517 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33518 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33520 if (if_modifier != ERROR_MARK
33521 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33523 const char *p = NULL;
33524 switch (if_modifier)
33526 case VOID_CST: p = "cancel"; break;
33527 case OMP_PARALLEL: p = "parallel"; break;
33528 case OMP_SIMD: p = "simd"; break;
33529 case OMP_TASK: p = "task"; break;
33530 case OMP_TASKLOOP: p = "taskloop"; break;
33531 case OMP_TARGET_DATA: p = "target data"; break;
33532 case OMP_TARGET: p = "target"; break;
33533 case OMP_TARGET_UPDATE: p = "target update"; break;
33534 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
33535 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
33536 default: gcc_unreachable ();
33538 error_at (location, "too many %<if%> clauses with %qs modifier",
33540 return list;
33542 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33544 if (!is_omp)
33545 error_at (location, "too many %<if%> clauses");
33546 else
33547 error_at (location, "too many %<if%> clauses without modifier");
33548 return list;
33550 else if (if_modifier == ERROR_MARK
33551 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33553 error_at (location, "if any %<if%> clause has modifier, then all "
33554 "%<if%> clauses have to use modifier");
33555 return list;
33559 c = build_omp_clause (location, OMP_CLAUSE_IF);
33560 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33561 OMP_CLAUSE_IF_EXPR (c) = t;
33562 OMP_CLAUSE_CHAIN (c) = list;
33564 return c;
33567 /* OpenMP 3.1:
33568 mergeable */
33570 static tree
33571 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33572 tree list, location_t location)
33574 tree c;
33576 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33577 location);
33579 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33580 OMP_CLAUSE_CHAIN (c) = list;
33581 return c;
33584 /* OpenMP 2.5:
33585 nowait */
33587 static tree
33588 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33589 tree list, location_t location)
33591 tree c;
33593 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33595 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33596 OMP_CLAUSE_CHAIN (c) = list;
33597 return c;
33600 /* OpenMP 2.5:
33601 num_threads ( expression ) */
33603 static tree
33604 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33605 location_t location)
33607 tree t, c;
33609 matching_parens parens;
33610 if (!parens.require_open (parser))
33611 return list;
33613 t = cp_parser_assignment_expression (parser);
33615 if (t == error_mark_node
33616 || !parens.require_close (parser))
33617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33618 /*or_comma=*/false,
33619 /*consume_paren=*/true);
33621 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33622 "num_threads", location);
33624 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33625 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33626 OMP_CLAUSE_CHAIN (c) = list;
33628 return c;
33631 /* OpenMP 4.5:
33632 num_tasks ( expression ) */
33634 static tree
33635 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33636 location_t location)
33638 tree t, c;
33640 matching_parens parens;
33641 if (!parens.require_open (parser))
33642 return list;
33644 t = cp_parser_assignment_expression (parser);
33646 if (t == error_mark_node
33647 || !parens.require_close (parser))
33648 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33649 /*or_comma=*/false,
33650 /*consume_paren=*/true);
33652 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33653 "num_tasks", location);
33655 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33656 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33657 OMP_CLAUSE_CHAIN (c) = list;
33659 return c;
33662 /* OpenMP 4.5:
33663 grainsize ( expression ) */
33665 static tree
33666 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33667 location_t location)
33669 tree t, c;
33671 matching_parens parens;
33672 if (!parens.require_open (parser))
33673 return list;
33675 t = cp_parser_assignment_expression (parser);
33677 if (t == error_mark_node
33678 || !parens.require_close (parser))
33679 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33680 /*or_comma=*/false,
33681 /*consume_paren=*/true);
33683 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33684 "grainsize", location);
33686 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33687 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33688 OMP_CLAUSE_CHAIN (c) = list;
33690 return c;
33693 /* OpenMP 4.5:
33694 priority ( expression ) */
33696 static tree
33697 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33698 location_t location)
33700 tree t, c;
33702 matching_parens parens;
33703 if (!parens.require_open (parser))
33704 return list;
33706 t = cp_parser_assignment_expression (parser);
33708 if (t == error_mark_node
33709 || !parens.require_close (parser))
33710 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33711 /*or_comma=*/false,
33712 /*consume_paren=*/true);
33714 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33715 "priority", location);
33717 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33718 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33719 OMP_CLAUSE_CHAIN (c) = list;
33721 return c;
33724 /* OpenMP 4.5:
33725 hint ( expression ) */
33727 static tree
33728 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33730 tree t, c;
33732 matching_parens parens;
33733 if (!parens.require_open (parser))
33734 return list;
33736 t = cp_parser_assignment_expression (parser);
33738 if (t == error_mark_node
33739 || !parens.require_close (parser))
33740 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33741 /*or_comma=*/false,
33742 /*consume_paren=*/true);
33744 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33746 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33747 OMP_CLAUSE_HINT_EXPR (c) = t;
33748 OMP_CLAUSE_CHAIN (c) = list;
33750 return c;
33753 /* OpenMP 4.5:
33754 defaultmap ( tofrom : scalar )
33756 OpenMP 5.0:
33757 defaultmap ( implicit-behavior [ : variable-category ] ) */
33759 static tree
33760 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33761 location_t location)
33763 tree c, id;
33764 const char *p;
33765 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33766 enum omp_clause_defaultmap_kind category
33767 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33769 matching_parens parens;
33770 if (!parens.require_open (parser))
33771 return list;
33773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33774 p = "default";
33775 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33777 invalid_behavior:
33778 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33779 "%<tofrom%>, %<firstprivate%>, %<none%> "
33780 "or %<default%>");
33781 goto out_err;
33783 else
33785 id = cp_lexer_peek_token (parser->lexer)->u.value;
33786 p = IDENTIFIER_POINTER (id);
33789 switch (p[0])
33791 case 'a':
33792 if (strcmp ("alloc", p) == 0)
33793 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33794 else
33795 goto invalid_behavior;
33796 break;
33798 case 'd':
33799 if (strcmp ("default", p) == 0)
33800 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33801 else
33802 goto invalid_behavior;
33803 break;
33805 case 'f':
33806 if (strcmp ("firstprivate", p) == 0)
33807 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33808 else if (strcmp ("from", p) == 0)
33809 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33810 else
33811 goto invalid_behavior;
33812 break;
33814 case 'n':
33815 if (strcmp ("none", p) == 0)
33816 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33817 else
33818 goto invalid_behavior;
33819 break;
33821 case 't':
33822 if (strcmp ("tofrom", p) == 0)
33823 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33824 else if (strcmp ("to", p) == 0)
33825 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33826 else
33827 goto invalid_behavior;
33828 break;
33830 default:
33831 goto invalid_behavior;
33833 cp_lexer_consume_token (parser->lexer);
33835 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33837 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33838 goto out_err;
33840 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33842 invalid_category:
33843 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33844 "%<pointer%>");
33845 goto out_err;
33847 id = cp_lexer_peek_token (parser->lexer)->u.value;
33848 p = IDENTIFIER_POINTER (id);
33850 switch (p[0])
33852 case 'a':
33853 if (strcmp ("aggregate", p) == 0)
33854 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33855 else
33856 goto invalid_category;
33857 break;
33859 case 'p':
33860 if (strcmp ("pointer", p) == 0)
33861 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33862 else
33863 goto invalid_category;
33864 break;
33866 case 's':
33867 if (strcmp ("scalar", p) == 0)
33868 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33869 else
33870 goto invalid_category;
33871 break;
33873 default:
33874 goto invalid_category;
33877 cp_lexer_consume_token (parser->lexer);
33879 if (!parens.require_close (parser))
33880 goto out_err;
33882 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33883 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33884 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33885 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33886 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33887 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33889 enum omp_clause_defaultmap_kind cat = category;
33890 location_t loc = OMP_CLAUSE_LOCATION (c);
33891 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33892 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33893 p = NULL;
33894 switch (cat)
33896 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33897 p = NULL;
33898 break;
33899 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33900 p = "aggregate";
33901 break;
33902 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33903 p = "pointer";
33904 break;
33905 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33906 p = "scalar";
33907 break;
33908 default:
33909 gcc_unreachable ();
33911 if (p)
33912 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33914 else
33915 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33916 "category");
33917 break;
33920 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33921 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33922 OMP_CLAUSE_CHAIN (c) = list;
33923 return c;
33925 out_err:
33926 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33927 /*or_comma=*/false,
33928 /*consume_paren=*/true);
33929 return list;
33932 /* OpenMP 5.0:
33933 order ( concurrent ) */
33935 static tree
33936 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
33938 tree c, id;
33939 const char *p;
33941 matching_parens parens;
33942 if (!parens.require_open (parser))
33943 return list;
33945 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33947 cp_parser_error (parser, "expected %<concurrent%>");
33948 goto out_err;
33950 else
33952 id = cp_lexer_peek_token (parser->lexer)->u.value;
33953 p = IDENTIFIER_POINTER (id);
33955 if (strcmp (p, "concurrent") != 0)
33957 cp_parser_error (parser, "expected %<concurrent%>");
33958 goto out_err;
33960 cp_lexer_consume_token (parser->lexer);
33961 if (!parens.require_close (parser))
33962 goto out_err;
33964 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
33965 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
33966 OMP_CLAUSE_CHAIN (c) = list;
33967 return c;
33969 out_err:
33970 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33971 /*or_comma=*/false,
33972 /*consume_paren=*/true);
33973 return list;
33976 /* OpenMP 5.0:
33977 bind ( teams | parallel | thread ) */
33979 static tree
33980 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
33981 location_t location)
33983 tree c;
33984 const char *p;
33985 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
33987 matching_parens parens;
33988 if (!parens.require_open (parser))
33989 return list;
33991 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33993 invalid:
33994 cp_parser_error (parser,
33995 "expected %<teams%>, %<parallel%> or %<thread%>");
33996 goto out_err;
33998 else
34000 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34001 p = IDENTIFIER_POINTER (id);
34003 if (strcmp (p, "teams") == 0)
34004 kind = OMP_CLAUSE_BIND_TEAMS;
34005 else if (strcmp (p, "parallel") == 0)
34006 kind = OMP_CLAUSE_BIND_PARALLEL;
34007 else if (strcmp (p, "thread") != 0)
34008 goto invalid;
34009 cp_lexer_consume_token (parser->lexer);
34010 if (!parens.require_close (parser))
34011 goto out_err;
34013 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
34014 c = build_omp_clause (location, OMP_CLAUSE_BIND);
34015 OMP_CLAUSE_BIND_KIND (c) = kind;
34016 OMP_CLAUSE_CHAIN (c) = list;
34017 return c;
34019 out_err:
34020 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34021 /*or_comma=*/false,
34022 /*consume_paren=*/true);
34023 return list;
34026 /* OpenMP 2.5:
34027 ordered
34029 OpenMP 4.5:
34030 ordered ( constant-expression ) */
34032 static tree
34033 cp_parser_omp_clause_ordered (cp_parser *parser,
34034 tree list, location_t location)
34036 tree c, num = NULL_TREE;
34037 HOST_WIDE_INT n;
34039 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
34040 "ordered", location);
34042 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34044 matching_parens parens;
34045 parens.consume_open (parser);
34047 num = cp_parser_constant_expression (parser);
34049 if (!parens.require_close (parser))
34050 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34051 /*or_comma=*/false,
34052 /*consume_paren=*/true);
34054 if (num == error_mark_node)
34055 return list;
34056 num = fold_non_dependent_expr (num);
34057 if (!tree_fits_shwi_p (num)
34058 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
34059 || (n = tree_to_shwi (num)) <= 0
34060 || (int) n != n)
34062 error_at (location,
34063 "ordered argument needs positive constant integer "
34064 "expression");
34065 return list;
34069 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
34070 OMP_CLAUSE_ORDERED_EXPR (c) = num;
34071 OMP_CLAUSE_CHAIN (c) = list;
34072 return c;
34075 /* OpenMP 2.5:
34076 reduction ( reduction-operator : variable-list )
34078 reduction-operator:
34079 One of: + * - & ^ | && ||
34081 OpenMP 3.1:
34083 reduction-operator:
34084 One of: + * - & ^ | && || min max
34086 OpenMP 4.0:
34088 reduction-operator:
34089 One of: + * - & ^ | && ||
34090 id-expression
34092 OpenMP 5.0:
34093 reduction ( reduction-modifier, reduction-operator : variable-list )
34094 in_reduction ( reduction-operator : variable-list )
34095 task_reduction ( reduction-operator : variable-list ) */
34097 static tree
34098 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
34099 bool is_omp, tree list)
34101 enum tree_code code = ERROR_MARK;
34102 tree nlist, c, id = NULL_TREE;
34103 bool task = false;
34104 bool inscan = false;
34106 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34107 return list;
34109 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
34111 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
34112 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34114 cp_lexer_consume_token (parser->lexer);
34115 cp_lexer_consume_token (parser->lexer);
34117 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34118 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34120 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34121 const char *p = IDENTIFIER_POINTER (id);
34122 if (strcmp (p, "task") == 0)
34123 task = true;
34124 else if (strcmp (p, "inscan") == 0)
34125 inscan = true;
34126 if (task || inscan)
34128 cp_lexer_consume_token (parser->lexer);
34129 cp_lexer_consume_token (parser->lexer);
34134 switch (cp_lexer_peek_token (parser->lexer)->type)
34136 case CPP_PLUS: code = PLUS_EXPR; break;
34137 case CPP_MULT: code = MULT_EXPR; break;
34138 case CPP_MINUS: code = MINUS_EXPR; break;
34139 case CPP_AND: code = BIT_AND_EXPR; break;
34140 case CPP_XOR: code = BIT_XOR_EXPR; break;
34141 case CPP_OR: code = BIT_IOR_EXPR; break;
34142 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
34143 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
34144 default: break;
34147 if (code != ERROR_MARK)
34148 cp_lexer_consume_token (parser->lexer);
34149 else
34151 bool saved_colon_corrects_to_scope_p;
34152 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34153 parser->colon_corrects_to_scope_p = false;
34154 id = cp_parser_id_expression (parser, /*template_p=*/false,
34155 /*check_dependency_p=*/true,
34156 /*template_p=*/NULL,
34157 /*declarator_p=*/false,
34158 /*optional_p=*/false);
34159 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34160 if (identifier_p (id))
34162 const char *p = IDENTIFIER_POINTER (id);
34164 if (strcmp (p, "min") == 0)
34165 code = MIN_EXPR;
34166 else if (strcmp (p, "max") == 0)
34167 code = MAX_EXPR;
34168 else if (id == ovl_op_identifier (false, PLUS_EXPR))
34169 code = PLUS_EXPR;
34170 else if (id == ovl_op_identifier (false, MULT_EXPR))
34171 code = MULT_EXPR;
34172 else if (id == ovl_op_identifier (false, MINUS_EXPR))
34173 code = MINUS_EXPR;
34174 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
34175 code = BIT_AND_EXPR;
34176 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
34177 code = BIT_IOR_EXPR;
34178 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
34179 code = BIT_XOR_EXPR;
34180 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
34181 code = TRUTH_ANDIF_EXPR;
34182 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
34183 code = TRUTH_ORIF_EXPR;
34184 id = omp_reduction_id (code, id, NULL_TREE);
34185 tree scope = parser->scope;
34186 if (scope)
34187 id = build_qualified_name (NULL_TREE, scope, id, false);
34188 parser->scope = NULL_TREE;
34189 parser->qualifying_scope = NULL_TREE;
34190 parser->object_scope = NULL_TREE;
34192 else
34194 error ("invalid reduction-identifier");
34195 resync_fail:
34196 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34197 /*or_comma=*/false,
34198 /*consume_paren=*/true);
34199 return list;
34203 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34204 goto resync_fail;
34206 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
34207 NULL);
34208 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34210 OMP_CLAUSE_REDUCTION_CODE (c) = code;
34211 if (task)
34212 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
34213 else if (inscan)
34214 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
34215 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
34218 return nlist;
34221 /* OpenMP 2.5:
34222 schedule ( schedule-kind )
34223 schedule ( schedule-kind , expression )
34225 schedule-kind:
34226 static | dynamic | guided | runtime | auto
34228 OpenMP 4.5:
34229 schedule ( schedule-modifier : schedule-kind )
34230 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
34232 schedule-modifier:
34233 simd
34234 monotonic
34235 nonmonotonic */
34237 static tree
34238 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
34240 tree c, t;
34241 int modifiers = 0, nmodifiers = 0;
34243 matching_parens parens;
34244 if (!parens.require_open (parser))
34245 return list;
34247 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
34249 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34252 const char *p = IDENTIFIER_POINTER (id);
34253 if (strcmp ("simd", p) == 0)
34254 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
34255 else if (strcmp ("monotonic", p) == 0)
34256 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
34257 else if (strcmp ("nonmonotonic", p) == 0)
34258 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
34259 else
34260 break;
34261 cp_lexer_consume_token (parser->lexer);
34262 if (nmodifiers++ == 0
34263 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34264 cp_lexer_consume_token (parser->lexer);
34265 else
34267 cp_parser_require (parser, CPP_COLON, RT_COLON);
34268 break;
34272 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34274 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34275 const char *p = IDENTIFIER_POINTER (id);
34277 switch (p[0])
34279 case 'd':
34280 if (strcmp ("dynamic", p) != 0)
34281 goto invalid_kind;
34282 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
34283 break;
34285 case 'g':
34286 if (strcmp ("guided", p) != 0)
34287 goto invalid_kind;
34288 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
34289 break;
34291 case 'r':
34292 if (strcmp ("runtime", p) != 0)
34293 goto invalid_kind;
34294 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
34295 break;
34297 default:
34298 goto invalid_kind;
34301 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34302 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
34303 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34304 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
34305 else
34306 goto invalid_kind;
34307 cp_lexer_consume_token (parser->lexer);
34309 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
34310 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34311 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
34312 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34314 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
34315 "specified");
34316 modifiers = 0;
34319 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34321 cp_token *token;
34322 cp_lexer_consume_token (parser->lexer);
34324 token = cp_lexer_peek_token (parser->lexer);
34325 t = cp_parser_assignment_expression (parser);
34327 if (t == error_mark_node)
34328 goto resync_fail;
34329 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
34330 error_at (token->location, "schedule %<runtime%> does not take "
34331 "a %<chunk_size%> parameter");
34332 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
34333 error_at (token->location, "schedule %<auto%> does not take "
34334 "a %<chunk_size%> parameter");
34335 else
34336 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
34338 if (!parens.require_close (parser))
34339 goto resync_fail;
34341 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34342 goto resync_fail;
34344 OMP_CLAUSE_SCHEDULE_KIND (c)
34345 = (enum omp_clause_schedule_kind)
34346 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
34348 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
34349 OMP_CLAUSE_CHAIN (c) = list;
34350 return c;
34352 invalid_kind:
34353 cp_parser_error (parser, "invalid schedule kind");
34354 resync_fail:
34355 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34356 /*or_comma=*/false,
34357 /*consume_paren=*/true);
34358 return list;
34361 /* OpenMP 3.0:
34362 untied */
34364 static tree
34365 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
34366 tree list, location_t location)
34368 tree c;
34370 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
34372 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
34373 OMP_CLAUSE_CHAIN (c) = list;
34374 return c;
34377 /* OpenMP 4.0:
34378 inbranch
34379 notinbranch */
34381 static tree
34382 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
34383 tree list, location_t location)
34385 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34386 tree c = build_omp_clause (location, code);
34387 OMP_CLAUSE_CHAIN (c) = list;
34388 return c;
34391 /* OpenMP 4.0:
34392 parallel
34394 sections
34395 taskgroup */
34397 static tree
34398 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
34399 enum omp_clause_code code,
34400 tree list, location_t location)
34402 tree c = build_omp_clause (location, code);
34403 OMP_CLAUSE_CHAIN (c) = list;
34404 return c;
34407 /* OpenMP 4.5:
34408 nogroup */
34410 static tree
34411 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
34412 tree list, location_t location)
34414 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
34415 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
34416 OMP_CLAUSE_CHAIN (c) = list;
34417 return c;
34420 /* OpenMP 4.5:
34421 simd
34422 threads */
34424 static tree
34425 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
34426 enum omp_clause_code code,
34427 tree list, location_t location)
34429 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34430 tree c = build_omp_clause (location, code);
34431 OMP_CLAUSE_CHAIN (c) = list;
34432 return c;
34435 /* OpenMP 4.0:
34436 num_teams ( expression ) */
34438 static tree
34439 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
34440 location_t location)
34442 tree t, c;
34444 matching_parens parens;
34445 if (!parens.require_open (parser))
34446 return list;
34448 t = cp_parser_assignment_expression (parser);
34450 if (t == error_mark_node
34451 || !parens.require_close (parser))
34452 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34453 /*or_comma=*/false,
34454 /*consume_paren=*/true);
34456 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34457 "num_teams", location);
34459 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34460 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34461 OMP_CLAUSE_CHAIN (c) = list;
34463 return c;
34466 /* OpenMP 4.0:
34467 thread_limit ( expression ) */
34469 static tree
34470 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34471 location_t location)
34473 tree t, c;
34475 matching_parens parens;
34476 if (!parens.require_open (parser))
34477 return list;
34479 t = cp_parser_assignment_expression (parser);
34481 if (t == error_mark_node
34482 || !parens.require_close (parser))
34483 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34484 /*or_comma=*/false,
34485 /*consume_paren=*/true);
34487 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34488 "thread_limit", location);
34490 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34491 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34492 OMP_CLAUSE_CHAIN (c) = list;
34494 return c;
34497 /* OpenMP 4.0:
34498 aligned ( variable-list )
34499 aligned ( variable-list : constant-expression ) */
34501 static tree
34502 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34504 tree nlist, c, alignment = NULL_TREE;
34505 bool colon;
34507 matching_parens parens;
34508 if (!parens.require_open (parser))
34509 return list;
34511 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34512 &colon);
34514 if (colon)
34516 alignment = cp_parser_constant_expression (parser);
34518 if (!parens.require_close (parser))
34519 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34520 /*or_comma=*/false,
34521 /*consume_paren=*/true);
34523 if (alignment == error_mark_node)
34524 alignment = NULL_TREE;
34527 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34528 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34530 return nlist;
34533 /* OpenMP 2.5:
34534 lastprivate ( variable-list )
34536 OpenMP 5.0:
34537 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34539 static tree
34540 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34542 bool conditional = false;
34544 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34545 return list;
34547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34548 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34551 const char *p = IDENTIFIER_POINTER (id);
34553 if (strcmp ("conditional", p) == 0)
34555 conditional = true;
34556 cp_lexer_consume_token (parser->lexer);
34557 cp_lexer_consume_token (parser->lexer);
34561 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34562 list, NULL);
34564 if (conditional)
34565 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34566 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34567 return nlist;
34570 /* OpenMP 4.0:
34571 linear ( variable-list )
34572 linear ( variable-list : expression )
34574 OpenMP 4.5:
34575 linear ( modifier ( variable-list ) )
34576 linear ( modifier ( variable-list ) : expression ) */
34578 static tree
34579 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34580 bool declare_simd)
34582 tree nlist, c, step = integer_one_node;
34583 bool colon;
34584 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34586 matching_parens parens;
34587 if (!parens.require_open (parser))
34588 return list;
34590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34592 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34593 const char *p = IDENTIFIER_POINTER (id);
34595 if (strcmp ("ref", p) == 0)
34596 kind = OMP_CLAUSE_LINEAR_REF;
34597 else if (strcmp ("val", p) == 0)
34598 kind = OMP_CLAUSE_LINEAR_VAL;
34599 else if (strcmp ("uval", p) == 0)
34600 kind = OMP_CLAUSE_LINEAR_UVAL;
34601 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34602 cp_lexer_consume_token (parser->lexer);
34603 else
34604 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34607 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34608 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34609 &colon);
34610 else
34612 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34613 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34614 if (colon)
34615 cp_parser_require (parser, CPP_COLON, RT_COLON);
34616 else if (!parens.require_close (parser))
34617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34618 /*or_comma=*/false,
34619 /*consume_paren=*/true);
34622 if (colon)
34624 step = NULL_TREE;
34625 if (declare_simd
34626 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34627 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34629 cp_token *token = cp_lexer_peek_token (parser->lexer);
34630 cp_parser_parse_tentatively (parser);
34631 step = cp_parser_id_expression (parser, /*template_p=*/false,
34632 /*check_dependency_p=*/true,
34633 /*template_p=*/NULL,
34634 /*declarator_p=*/false,
34635 /*optional_p=*/false);
34636 if (step != error_mark_node)
34637 step = cp_parser_lookup_name_simple (parser, step, token->location);
34638 if (step == error_mark_node)
34640 step = NULL_TREE;
34641 cp_parser_abort_tentative_parse (parser);
34643 else if (!cp_parser_parse_definitely (parser))
34644 step = NULL_TREE;
34646 if (!step)
34647 step = cp_parser_assignment_expression (parser);
34649 if (!parens.require_close (parser))
34650 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34651 /*or_comma=*/false,
34652 /*consume_paren=*/true);
34654 if (step == error_mark_node)
34655 return list;
34658 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34660 OMP_CLAUSE_LINEAR_STEP (c) = step;
34661 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34664 return nlist;
34667 /* OpenMP 4.0:
34668 safelen ( constant-expression ) */
34670 static tree
34671 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34672 location_t location)
34674 tree t, c;
34676 matching_parens parens;
34677 if (!parens.require_open (parser))
34678 return list;
34680 t = cp_parser_constant_expression (parser);
34682 if (t == error_mark_node
34683 || !parens.require_close (parser))
34684 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34685 /*or_comma=*/false,
34686 /*consume_paren=*/true);
34688 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34690 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34691 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34692 OMP_CLAUSE_CHAIN (c) = list;
34694 return c;
34697 /* OpenMP 4.0:
34698 simdlen ( constant-expression ) */
34700 static tree
34701 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34702 location_t location)
34704 tree t, c;
34706 matching_parens parens;
34707 if (!parens.require_open (parser))
34708 return list;
34710 t = cp_parser_constant_expression (parser);
34712 if (t == error_mark_node
34713 || !parens.require_close (parser))
34714 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34715 /*or_comma=*/false,
34716 /*consume_paren=*/true);
34718 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34720 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34721 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34722 OMP_CLAUSE_CHAIN (c) = list;
34724 return c;
34727 /* OpenMP 4.5:
34728 vec:
34729 identifier [+/- integer]
34730 vec , identifier [+/- integer]
34733 static tree
34734 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34735 tree list)
34737 tree vec = NULL;
34739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34741 cp_parser_error (parser, "expected identifier");
34742 return list;
34745 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34747 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34748 tree t, identifier = cp_parser_identifier (parser);
34749 tree addend = NULL;
34751 if (identifier == error_mark_node)
34752 t = error_mark_node;
34753 else
34755 t = cp_parser_lookup_name_simple
34756 (parser, identifier,
34757 cp_lexer_peek_token (parser->lexer)->location);
34758 if (t == error_mark_node)
34759 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34760 id_loc);
34763 bool neg = false;
34764 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34765 neg = true;
34766 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34768 addend = integer_zero_node;
34769 goto add_to_vector;
34771 cp_lexer_consume_token (parser->lexer);
34773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34775 cp_parser_error (parser, "expected integer");
34776 return list;
34779 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34780 if (TREE_CODE (addend) != INTEGER_CST)
34782 cp_parser_error (parser, "expected integer");
34783 return list;
34785 cp_lexer_consume_token (parser->lexer);
34787 add_to_vector:
34788 if (t != error_mark_node)
34790 vec = tree_cons (addend, t, vec);
34791 if (neg)
34792 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34796 break;
34798 cp_lexer_consume_token (parser->lexer);
34801 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34803 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34804 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34805 OMP_CLAUSE_DECL (u) = nreverse (vec);
34806 OMP_CLAUSE_CHAIN (u) = list;
34807 return u;
34809 return list;
34812 /* OpenMP 5.0:
34813 iterators ( iterators-definition )
34815 iterators-definition:
34816 iterator-specifier
34817 iterator-specifier , iterators-definition
34819 iterator-specifier:
34820 identifier = range-specification
34821 iterator-type identifier = range-specification
34823 range-specification:
34824 begin : end
34825 begin : end : step */
34827 static tree
34828 cp_parser_omp_iterators (cp_parser *parser)
34830 tree ret = NULL_TREE, *last = &ret;
34831 cp_lexer_consume_token (parser->lexer);
34833 matching_parens parens;
34834 if (!parens.require_open (parser))
34835 return error_mark_node;
34837 bool saved_colon_corrects_to_scope_p
34838 = parser->colon_corrects_to_scope_p;
34839 bool saved_colon_doesnt_start_class_def_p
34840 = parser->colon_doesnt_start_class_def_p;
34844 tree iter_type;
34845 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34846 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34847 iter_type = integer_type_node;
34848 else
34850 const char *saved_message
34851 = parser->type_definition_forbidden_message;
34852 parser->type_definition_forbidden_message
34853 = G_("types may not be defined in iterator type");
34855 iter_type = cp_parser_type_id (parser);
34857 parser->type_definition_forbidden_message = saved_message;
34860 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34863 cp_parser_error (parser, "expected identifier");
34864 break;
34867 tree id = cp_parser_identifier (parser);
34868 if (id == error_mark_node)
34869 break;
34871 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34872 break;
34874 parser->colon_corrects_to_scope_p = false;
34875 parser->colon_doesnt_start_class_def_p = true;
34876 tree begin = cp_parser_assignment_expression (parser);
34878 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34879 break;
34881 tree end = cp_parser_assignment_expression (parser);
34883 tree step = integer_one_node;
34884 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34886 cp_lexer_consume_token (parser->lexer);
34887 step = cp_parser_assignment_expression (parser);
34890 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34891 DECL_ARTIFICIAL (iter_var) = 1;
34892 DECL_CONTEXT (iter_var) = current_function_decl;
34893 pushdecl (iter_var);
34895 *last = make_tree_vec (6);
34896 TREE_VEC_ELT (*last, 0) = iter_var;
34897 TREE_VEC_ELT (*last, 1) = begin;
34898 TREE_VEC_ELT (*last, 2) = end;
34899 TREE_VEC_ELT (*last, 3) = step;
34900 last = &TREE_CHAIN (*last);
34902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34904 cp_lexer_consume_token (parser->lexer);
34905 continue;
34907 break;
34909 while (1);
34911 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34912 parser->colon_doesnt_start_class_def_p
34913 = saved_colon_doesnt_start_class_def_p;
34915 if (!parens.require_close (parser))
34916 cp_parser_skip_to_closing_parenthesis (parser,
34917 /*recovering=*/true,
34918 /*or_comma=*/false,
34919 /*consume_paren=*/true);
34921 return ret ? ret : error_mark_node;
34924 /* OpenMP 4.0:
34925 depend ( depend-kind : variable-list )
34927 depend-kind:
34928 in | out | inout
34930 OpenMP 4.5:
34931 depend ( source )
34933 depend ( sink : vec )
34935 OpenMP 5.0:
34936 depend ( depend-modifier , depend-kind: variable-list )
34938 depend-kind:
34939 in | out | inout | mutexinoutset | depobj
34941 depend-modifier:
34942 iterator ( iterators-definition ) */
34944 static tree
34945 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34947 tree nlist, c, iterators = NULL_TREE;
34948 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34950 matching_parens parens;
34951 if (!parens.require_open (parser))
34952 return list;
34956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34957 goto invalid_kind;
34959 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34960 const char *p = IDENTIFIER_POINTER (id);
34962 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34964 begin_scope (sk_omp, NULL);
34965 iterators = cp_parser_omp_iterators (parser);
34966 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34967 continue;
34969 if (strcmp ("in", p) == 0)
34970 kind = OMP_CLAUSE_DEPEND_IN;
34971 else if (strcmp ("inout", p) == 0)
34972 kind = OMP_CLAUSE_DEPEND_INOUT;
34973 else if (strcmp ("mutexinoutset", p) == 0)
34974 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34975 else if (strcmp ("out", p) == 0)
34976 kind = OMP_CLAUSE_DEPEND_OUT;
34977 else if (strcmp ("depobj", p) == 0)
34978 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34979 else if (strcmp ("sink", p) == 0)
34980 kind = OMP_CLAUSE_DEPEND_SINK;
34981 else if (strcmp ("source", p) == 0)
34982 kind = OMP_CLAUSE_DEPEND_SOURCE;
34983 else
34984 goto invalid_kind;
34985 break;
34987 while (1);
34989 cp_lexer_consume_token (parser->lexer);
34991 if (iterators
34992 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34994 poplevel (0, 1, 0);
34995 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34996 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34997 iterators = NULL_TREE;
35000 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35002 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
35003 OMP_CLAUSE_DEPEND_KIND (c) = kind;
35004 OMP_CLAUSE_DECL (c) = NULL_TREE;
35005 OMP_CLAUSE_CHAIN (c) = list;
35006 if (!parens.require_close (parser))
35007 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35008 /*or_comma=*/false,
35009 /*consume_paren=*/true);
35010 return c;
35013 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35014 goto resync_fail;
35016 if (kind == OMP_CLAUSE_DEPEND_SINK)
35017 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
35018 else
35020 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
35021 list, NULL);
35023 if (iterators)
35025 tree block = poplevel (1, 1, 0);
35026 if (iterators == error_mark_node)
35027 iterators = NULL_TREE;
35028 else
35029 TREE_VEC_ELT (iterators, 5) = block;
35032 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35034 OMP_CLAUSE_DEPEND_KIND (c) = kind;
35035 if (iterators)
35036 OMP_CLAUSE_DECL (c)
35037 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
35040 return nlist;
35042 invalid_kind:
35043 cp_parser_error (parser, "invalid depend kind");
35044 resync_fail:
35045 if (iterators)
35046 poplevel (0, 1, 0);
35047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35048 /*or_comma=*/false,
35049 /*consume_paren=*/true);
35050 return list;
35053 /* OpenMP 4.0:
35054 map ( map-kind : variable-list )
35055 map ( variable-list )
35057 map-kind:
35058 alloc | to | from | tofrom
35060 OpenMP 4.5:
35061 map-kind:
35062 alloc | to | from | tofrom | release | delete
35064 map ( always [,] map-kind: variable-list ) */
35066 static tree
35067 cp_parser_omp_clause_map (cp_parser *parser, tree list)
35069 tree nlist, c;
35070 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
35071 bool always = false;
35073 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35074 return list;
35076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35078 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35079 const char *p = IDENTIFIER_POINTER (id);
35081 if (strcmp ("always", p) == 0)
35083 int nth = 2;
35084 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
35085 nth++;
35086 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
35087 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
35088 == RID_DELETE))
35089 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
35090 == CPP_COLON))
35092 always = true;
35093 cp_lexer_consume_token (parser->lexer);
35094 if (nth == 3)
35095 cp_lexer_consume_token (parser->lexer);
35100 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35101 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35103 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35104 const char *p = IDENTIFIER_POINTER (id);
35106 if (strcmp ("alloc", p) == 0)
35107 kind = GOMP_MAP_ALLOC;
35108 else if (strcmp ("to", p) == 0)
35109 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
35110 else if (strcmp ("from", p) == 0)
35111 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
35112 else if (strcmp ("tofrom", p) == 0)
35113 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
35114 else if (strcmp ("release", p) == 0)
35115 kind = GOMP_MAP_RELEASE;
35116 else
35118 cp_parser_error (parser, "invalid map kind");
35119 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35120 /*or_comma=*/false,
35121 /*consume_paren=*/true);
35122 return list;
35124 cp_lexer_consume_token (parser->lexer);
35125 cp_lexer_consume_token (parser->lexer);
35127 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
35128 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35130 kind = GOMP_MAP_DELETE;
35131 cp_lexer_consume_token (parser->lexer);
35132 cp_lexer_consume_token (parser->lexer);
35135 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
35136 NULL);
35138 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35139 OMP_CLAUSE_SET_MAP_KIND (c, kind);
35141 return nlist;
35144 /* OpenMP 4.0:
35145 device ( expression ) */
35147 static tree
35148 cp_parser_omp_clause_device (cp_parser *parser, tree list,
35149 location_t location)
35151 tree t, c;
35153 matching_parens parens;
35154 if (!parens.require_open (parser))
35155 return list;
35157 t = cp_parser_assignment_expression (parser);
35159 if (t == error_mark_node
35160 || !parens.require_close (parser))
35161 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35162 /*or_comma=*/false,
35163 /*consume_paren=*/true);
35165 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
35166 "device", location);
35168 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
35169 OMP_CLAUSE_DEVICE_ID (c) = t;
35170 OMP_CLAUSE_CHAIN (c) = list;
35172 return c;
35175 /* OpenMP 4.0:
35176 dist_schedule ( static )
35177 dist_schedule ( static , expression ) */
35179 static tree
35180 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
35181 location_t location)
35183 tree c, t;
35185 matching_parens parens;
35186 if (!parens.require_open (parser))
35187 return list;
35189 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
35191 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
35192 goto invalid_kind;
35193 cp_lexer_consume_token (parser->lexer);
35195 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35197 cp_lexer_consume_token (parser->lexer);
35199 t = cp_parser_assignment_expression (parser);
35201 if (t == error_mark_node)
35202 goto resync_fail;
35203 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
35205 if (!parens.require_close (parser))
35206 goto resync_fail;
35208 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35209 goto resync_fail;
35211 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
35212 "dist_schedule", location); */
35213 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
35214 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
35215 OMP_CLAUSE_CHAIN (c) = list;
35216 return c;
35218 invalid_kind:
35219 cp_parser_error (parser, "invalid dist_schedule kind");
35220 resync_fail:
35221 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35222 /*or_comma=*/false,
35223 /*consume_paren=*/true);
35224 return list;
35227 /* OpenMP 4.0:
35228 proc_bind ( proc-bind-kind )
35230 proc-bind-kind:
35231 master | close | spread */
35233 static tree
35234 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
35235 location_t location)
35237 tree c;
35238 enum omp_clause_proc_bind_kind kind;
35240 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35241 return list;
35243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35246 const char *p = IDENTIFIER_POINTER (id);
35248 if (strcmp ("master", p) == 0)
35249 kind = OMP_CLAUSE_PROC_BIND_MASTER;
35250 else if (strcmp ("close", p) == 0)
35251 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
35252 else if (strcmp ("spread", p) == 0)
35253 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
35254 else
35255 goto invalid_kind;
35257 else
35258 goto invalid_kind;
35260 cp_lexer_consume_token (parser->lexer);
35261 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35262 goto resync_fail;
35264 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
35265 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
35266 location);
35267 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
35268 OMP_CLAUSE_CHAIN (c) = list;
35269 return c;
35271 invalid_kind:
35272 cp_parser_error (parser, "invalid depend kind");
35273 resync_fail:
35274 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35275 /*or_comma=*/false,
35276 /*consume_paren=*/true);
35277 return list;
35280 /* OpenMP 5.0:
35281 device_type ( host | nohost | any ) */
35283 static tree
35284 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
35285 location_t location)
35287 tree c;
35288 enum omp_clause_device_type_kind kind;
35290 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35291 return list;
35293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35296 const char *p = IDENTIFIER_POINTER (id);
35298 if (strcmp ("host", p) == 0)
35299 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
35300 else if (strcmp ("nohost", p) == 0)
35301 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
35302 else if (strcmp ("any", p) == 0)
35303 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
35304 else
35305 goto invalid_kind;
35307 else
35308 goto invalid_kind;
35310 cp_lexer_consume_token (parser->lexer);
35311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35312 goto resync_fail;
35314 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
35315 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
35316 location); */
35317 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
35318 OMP_CLAUSE_CHAIN (c) = list;
35319 return c;
35321 invalid_kind:
35322 cp_parser_error (parser, "invalid depend kind");
35323 resync_fail:
35324 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35325 /*or_comma=*/false,
35326 /*consume_paren=*/true);
35327 return list;
35330 /* OpenACC:
35331 async [( int-expr )] */
35333 static tree
35334 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
35336 tree c, t;
35337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35339 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
35341 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35343 matching_parens parens;
35344 parens.consume_open (parser);
35346 t = cp_parser_expression (parser);
35347 if (t == error_mark_node
35348 || !parens.require_close (parser))
35349 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35350 /*or_comma=*/false,
35351 /*consume_paren=*/true);
35354 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
35356 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
35357 OMP_CLAUSE_ASYNC_EXPR (c) = t;
35358 OMP_CLAUSE_CHAIN (c) = list;
35359 list = c;
35361 return list;
35364 /* Parse all OpenACC clauses. The set clauses allowed by the directive
35365 is a bitmask in MASK. Return the list of clauses found. */
35367 static tree
35368 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
35369 const char *where, cp_token *pragma_tok,
35370 bool finish_p = true)
35372 tree clauses = NULL;
35373 bool first = true;
35375 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35377 location_t here;
35378 pragma_omp_clause c_kind;
35379 omp_clause_code code;
35380 const char *c_name;
35381 tree prev = clauses;
35383 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35384 cp_lexer_consume_token (parser->lexer);
35386 here = cp_lexer_peek_token (parser->lexer)->location;
35387 c_kind = cp_parser_omp_clause_name (parser);
35389 switch (c_kind)
35391 case PRAGMA_OACC_CLAUSE_ASYNC:
35392 clauses = cp_parser_oacc_clause_async (parser, clauses);
35393 c_name = "async";
35394 break;
35395 case PRAGMA_OACC_CLAUSE_AUTO:
35396 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
35397 clauses);
35398 c_name = "auto";
35399 break;
35400 case PRAGMA_OACC_CLAUSE_COLLAPSE:
35401 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
35402 c_name = "collapse";
35403 break;
35404 case PRAGMA_OACC_CLAUSE_COPY:
35405 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35406 c_name = "copy";
35407 break;
35408 case PRAGMA_OACC_CLAUSE_COPYIN:
35409 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35410 c_name = "copyin";
35411 break;
35412 case PRAGMA_OACC_CLAUSE_COPYOUT:
35413 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35414 c_name = "copyout";
35415 break;
35416 case PRAGMA_OACC_CLAUSE_CREATE:
35417 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35418 c_name = "create";
35419 break;
35420 case PRAGMA_OACC_CLAUSE_DELETE:
35421 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35422 c_name = "delete";
35423 break;
35424 case PRAGMA_OMP_CLAUSE_DEFAULT:
35425 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
35426 c_name = "default";
35427 break;
35428 case PRAGMA_OACC_CLAUSE_DEVICE:
35429 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35430 c_name = "device";
35431 break;
35432 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
35433 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
35434 c_name = "deviceptr";
35435 break;
35436 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35437 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35438 c_name = "device_resident";
35439 break;
35440 case PRAGMA_OACC_CLAUSE_FINALIZE:
35441 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
35442 clauses);
35443 c_name = "finalize";
35444 break;
35445 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
35446 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35447 clauses);
35448 c_name = "firstprivate";
35449 break;
35450 case PRAGMA_OACC_CLAUSE_GANG:
35451 c_name = "gang";
35452 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
35453 c_name, clauses);
35454 break;
35455 case PRAGMA_OACC_CLAUSE_HOST:
35456 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35457 c_name = "host";
35458 break;
35459 case PRAGMA_OACC_CLAUSE_IF:
35460 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
35461 c_name = "if";
35462 break;
35463 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
35464 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
35465 clauses);
35466 c_name = "if_present";
35467 break;
35468 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
35469 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
35470 clauses);
35471 c_name = "independent";
35472 break;
35473 case PRAGMA_OACC_CLAUSE_LINK:
35474 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35475 c_name = "link";
35476 break;
35477 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
35478 code = OMP_CLAUSE_NUM_GANGS;
35479 c_name = "num_gangs";
35480 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35481 clauses);
35482 break;
35483 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
35484 c_name = "num_workers";
35485 code = OMP_CLAUSE_NUM_WORKERS;
35486 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35487 clauses);
35488 break;
35489 case PRAGMA_OACC_CLAUSE_PRESENT:
35490 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35491 c_name = "present";
35492 break;
35493 case PRAGMA_OACC_CLAUSE_PRIVATE:
35494 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35495 clauses);
35496 c_name = "private";
35497 break;
35498 case PRAGMA_OACC_CLAUSE_REDUCTION:
35499 clauses
35500 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35501 false, clauses);
35502 c_name = "reduction";
35503 break;
35504 case PRAGMA_OACC_CLAUSE_SEQ:
35505 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35506 clauses);
35507 c_name = "seq";
35508 break;
35509 case PRAGMA_OACC_CLAUSE_TILE:
35510 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35511 c_name = "tile";
35512 break;
35513 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35514 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35515 clauses);
35516 c_name = "use_device";
35517 break;
35518 case PRAGMA_OACC_CLAUSE_VECTOR:
35519 c_name = "vector";
35520 clauses = cp_parser_oacc_shape_clause (parser, here,
35521 OMP_CLAUSE_VECTOR,
35522 c_name, clauses);
35523 break;
35524 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35525 c_name = "vector_length";
35526 code = OMP_CLAUSE_VECTOR_LENGTH;
35527 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35528 clauses);
35529 break;
35530 case PRAGMA_OACC_CLAUSE_WAIT:
35531 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35532 c_name = "wait";
35533 break;
35534 case PRAGMA_OACC_CLAUSE_WORKER:
35535 c_name = "worker";
35536 clauses = cp_parser_oacc_shape_clause (parser, here,
35537 OMP_CLAUSE_WORKER,
35538 c_name, clauses);
35539 break;
35540 default:
35541 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35542 goto saw_error;
35545 first = false;
35547 if (((mask >> c_kind) & 1) == 0)
35549 /* Remove the invalid clause(s) from the list to avoid
35550 confusing the rest of the compiler. */
35551 clauses = prev;
35552 error_at (here, "%qs is not valid for %qs", c_name, where);
35556 saw_error:
35557 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35559 if (finish_p)
35560 return finish_omp_clauses (clauses, C_ORT_ACC);
35562 return clauses;
35565 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35566 is a bitmask in MASK. Return the list of clauses found; the result
35567 of clause default goes in *pdefault. */
35569 static tree
35570 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35571 const char *where, cp_token *pragma_tok,
35572 bool finish_p = true)
35574 tree clauses = NULL;
35575 bool first = true;
35576 cp_token *token = NULL;
35578 /* Don't create location wrapper nodes within OpenMP clauses. */
35579 auto_suppress_location_wrappers sentinel;
35581 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35583 pragma_omp_clause c_kind;
35584 const char *c_name;
35585 tree prev = clauses;
35587 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35588 cp_lexer_consume_token (parser->lexer);
35590 token = cp_lexer_peek_token (parser->lexer);
35591 c_kind = cp_parser_omp_clause_name (parser);
35593 switch (c_kind)
35595 case PRAGMA_OMP_CLAUSE_BIND:
35596 clauses = cp_parser_omp_clause_bind (parser, clauses,
35597 token->location);
35598 c_name = "bind";
35599 break;
35600 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35601 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35602 token->location);
35603 c_name = "collapse";
35604 break;
35605 case PRAGMA_OMP_CLAUSE_COPYIN:
35606 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35607 c_name = "copyin";
35608 break;
35609 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35610 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35611 clauses);
35612 c_name = "copyprivate";
35613 break;
35614 case PRAGMA_OMP_CLAUSE_DEFAULT:
35615 clauses = cp_parser_omp_clause_default (parser, clauses,
35616 token->location, false);
35617 c_name = "default";
35618 break;
35619 case PRAGMA_OMP_CLAUSE_FINAL:
35620 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35621 c_name = "final";
35622 break;
35623 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35624 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35625 clauses);
35626 c_name = "firstprivate";
35627 break;
35628 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35629 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35630 token->location);
35631 c_name = "grainsize";
35632 break;
35633 case PRAGMA_OMP_CLAUSE_HINT:
35634 clauses = cp_parser_omp_clause_hint (parser, clauses,
35635 token->location);
35636 c_name = "hint";
35637 break;
35638 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35639 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35640 token->location);
35641 c_name = "defaultmap";
35642 break;
35643 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35644 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35645 clauses);
35646 c_name = "use_device_ptr";
35647 break;
35648 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
35649 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
35650 clauses);
35651 c_name = "use_device_addr";
35652 break;
35653 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35654 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35655 clauses);
35656 c_name = "is_device_ptr";
35657 break;
35658 case PRAGMA_OMP_CLAUSE_IF:
35659 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35660 true);
35661 c_name = "if";
35662 break;
35663 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35664 clauses
35665 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35666 true, clauses);
35667 c_name = "in_reduction";
35668 break;
35669 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35670 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35671 c_name = "lastprivate";
35672 break;
35673 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35674 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35675 token->location);
35676 c_name = "mergeable";
35677 break;
35678 case PRAGMA_OMP_CLAUSE_NOWAIT:
35679 clauses = cp_parser_omp_clause_nowait (parser, clauses,
35680 token->location);
35681 c_name = "nowait";
35682 break;
35683 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35684 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35685 token->location);
35686 c_name = "num_tasks";
35687 break;
35688 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35689 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35690 token->location);
35691 c_name = "num_threads";
35692 break;
35693 case PRAGMA_OMP_CLAUSE_ORDER:
35694 clauses = cp_parser_omp_clause_order (parser, clauses,
35695 token->location);
35696 c_name = "order";
35697 break;
35698 case PRAGMA_OMP_CLAUSE_ORDERED:
35699 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35700 token->location);
35701 c_name = "ordered";
35702 break;
35703 case PRAGMA_OMP_CLAUSE_PRIORITY:
35704 clauses = cp_parser_omp_clause_priority (parser, clauses,
35705 token->location);
35706 c_name = "priority";
35707 break;
35708 case PRAGMA_OMP_CLAUSE_PRIVATE:
35709 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35710 clauses);
35711 c_name = "private";
35712 break;
35713 case PRAGMA_OMP_CLAUSE_REDUCTION:
35714 clauses
35715 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35716 true, clauses);
35717 c_name = "reduction";
35718 break;
35719 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35720 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35721 token->location);
35722 c_name = "schedule";
35723 break;
35724 case PRAGMA_OMP_CLAUSE_SHARED:
35725 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35726 clauses);
35727 c_name = "shared";
35728 break;
35729 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35730 clauses
35731 = cp_parser_omp_clause_reduction (parser,
35732 OMP_CLAUSE_TASK_REDUCTION,
35733 true, clauses);
35734 c_name = "task_reduction";
35735 break;
35736 case PRAGMA_OMP_CLAUSE_UNTIED:
35737 clauses = cp_parser_omp_clause_untied (parser, clauses,
35738 token->location);
35739 c_name = "untied";
35740 break;
35741 case PRAGMA_OMP_CLAUSE_INBRANCH:
35742 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35743 clauses, token->location);
35744 c_name = "inbranch";
35745 break;
35746 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35747 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35748 clauses);
35749 c_name = "nontemporal";
35750 break;
35751 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35752 clauses = cp_parser_omp_clause_branch (parser,
35753 OMP_CLAUSE_NOTINBRANCH,
35754 clauses, token->location);
35755 c_name = "notinbranch";
35756 break;
35757 case PRAGMA_OMP_CLAUSE_PARALLEL:
35758 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35759 clauses, token->location);
35760 c_name = "parallel";
35761 if (!first)
35763 clause_not_first:
35764 error_at (token->location, "%qs must be the first clause of %qs",
35765 c_name, where);
35766 clauses = prev;
35768 break;
35769 case PRAGMA_OMP_CLAUSE_FOR:
35770 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35771 clauses, token->location);
35772 c_name = "for";
35773 if (!first)
35774 goto clause_not_first;
35775 break;
35776 case PRAGMA_OMP_CLAUSE_SECTIONS:
35777 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35778 clauses, token->location);
35779 c_name = "sections";
35780 if (!first)
35781 goto clause_not_first;
35782 break;
35783 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35784 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35785 clauses, token->location);
35786 c_name = "taskgroup";
35787 if (!first)
35788 goto clause_not_first;
35789 break;
35790 case PRAGMA_OMP_CLAUSE_LINK:
35791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35792 c_name = "to";
35793 break;
35794 case PRAGMA_OMP_CLAUSE_TO:
35795 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35796 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35797 clauses);
35798 else
35799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35800 c_name = "to";
35801 break;
35802 case PRAGMA_OMP_CLAUSE_FROM:
35803 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35804 c_name = "from";
35805 break;
35806 case PRAGMA_OMP_CLAUSE_UNIFORM:
35807 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35808 clauses);
35809 c_name = "uniform";
35810 break;
35811 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35812 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35813 token->location);
35814 c_name = "num_teams";
35815 break;
35816 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35817 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35818 token->location);
35819 c_name = "thread_limit";
35820 break;
35821 case PRAGMA_OMP_CLAUSE_ALIGNED:
35822 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35823 c_name = "aligned";
35824 break;
35825 case PRAGMA_OMP_CLAUSE_LINEAR:
35827 bool declare_simd = false;
35828 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35829 declare_simd = true;
35830 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35832 c_name = "linear";
35833 break;
35834 case PRAGMA_OMP_CLAUSE_DEPEND:
35835 clauses = cp_parser_omp_clause_depend (parser, clauses,
35836 token->location);
35837 c_name = "depend";
35838 break;
35839 case PRAGMA_OMP_CLAUSE_MAP:
35840 clauses = cp_parser_omp_clause_map (parser, clauses);
35841 c_name = "map";
35842 break;
35843 case PRAGMA_OMP_CLAUSE_DEVICE:
35844 clauses = cp_parser_omp_clause_device (parser, clauses,
35845 token->location);
35846 c_name = "device";
35847 break;
35848 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35849 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35850 token->location);
35851 c_name = "dist_schedule";
35852 break;
35853 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35854 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35855 token->location);
35856 c_name = "proc_bind";
35857 break;
35858 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
35859 clauses = cp_parser_omp_clause_device_type (parser, clauses,
35860 token->location);
35861 c_name = "device_type";
35862 break;
35863 case PRAGMA_OMP_CLAUSE_SAFELEN:
35864 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35865 token->location);
35866 c_name = "safelen";
35867 break;
35868 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35869 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35870 token->location);
35871 c_name = "simdlen";
35872 break;
35873 case PRAGMA_OMP_CLAUSE_NOGROUP:
35874 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35875 token->location);
35876 c_name = "nogroup";
35877 break;
35878 case PRAGMA_OMP_CLAUSE_THREADS:
35879 clauses
35880 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35881 clauses, token->location);
35882 c_name = "threads";
35883 break;
35884 case PRAGMA_OMP_CLAUSE_SIMD:
35885 clauses
35886 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35887 clauses, token->location);
35888 c_name = "simd";
35889 break;
35890 default:
35891 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35892 goto saw_error;
35895 first = false;
35897 if (((mask >> c_kind) & 1) == 0)
35899 /* Remove the invalid clause(s) from the list to avoid
35900 confusing the rest of the compiler. */
35901 clauses = prev;
35902 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35905 saw_error:
35906 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35907 if (finish_p)
35909 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35910 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35911 else
35912 return finish_omp_clauses (clauses, C_ORT_OMP);
35914 return clauses;
35917 /* OpenMP 2.5:
35918 structured-block:
35919 statement
35921 In practice, we're also interested in adding the statement to an
35922 outer node. So it is convenient if we work around the fact that
35923 cp_parser_statement calls add_stmt. */
35925 static unsigned
35926 cp_parser_begin_omp_structured_block (cp_parser *parser)
35928 unsigned save = parser->in_statement;
35930 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35931 This preserves the "not within loop or switch" style error messages
35932 for nonsense cases like
35933 void foo() {
35934 #pragma omp single
35935 break;
35938 if (parser->in_statement)
35939 parser->in_statement = IN_OMP_BLOCK;
35941 return save;
35944 static void
35945 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35947 parser->in_statement = save;
35950 static tree
35951 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35953 tree stmt = begin_omp_structured_block ();
35954 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35956 cp_parser_statement (parser, NULL_TREE, false, if_p);
35958 cp_parser_end_omp_structured_block (parser, save);
35959 return finish_omp_structured_block (stmt);
35962 /* OpenMP 2.5:
35963 # pragma omp atomic new-line
35964 expression-stmt
35966 expression-stmt:
35967 x binop= expr | x++ | ++x | x-- | --x
35968 binop:
35969 +, *, -, /, &, ^, |, <<, >>
35971 where x is an lvalue expression with scalar type.
35973 OpenMP 3.1:
35974 # pragma omp atomic new-line
35975 update-stmt
35977 # pragma omp atomic read new-line
35978 read-stmt
35980 # pragma omp atomic write new-line
35981 write-stmt
35983 # pragma omp atomic update new-line
35984 update-stmt
35986 # pragma omp atomic capture new-line
35987 capture-stmt
35989 # pragma omp atomic capture new-line
35990 capture-block
35992 read-stmt:
35993 v = x
35994 write-stmt:
35995 x = expr
35996 update-stmt:
35997 expression-stmt | x = x binop expr
35998 capture-stmt:
35999 v = expression-stmt
36000 capture-block:
36001 { v = x; update-stmt; } | { update-stmt; v = x; }
36003 OpenMP 4.0:
36004 update-stmt:
36005 expression-stmt | x = x binop expr | x = expr binop x
36006 capture-stmt:
36007 v = update-stmt
36008 capture-block:
36009 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
36011 where x and v are lvalue expressions with scalar type. */
36013 static void
36014 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
36016 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
36017 tree rhs1 = NULL_TREE, orig_lhs;
36018 location_t loc = pragma_tok->location;
36019 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
36020 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
36021 bool structured_block = false;
36022 bool first = true;
36023 tree clauses = NULL_TREE;
36025 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36027 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36028 cp_lexer_consume_token (parser->lexer);
36030 first = false;
36032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36034 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36035 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
36036 const char *p = IDENTIFIER_POINTER (id);
36037 enum tree_code new_code = ERROR_MARK;
36038 enum omp_memory_order new_memory_order
36039 = OMP_MEMORY_ORDER_UNSPECIFIED;
36041 if (!strcmp (p, "read"))
36042 new_code = OMP_ATOMIC_READ;
36043 else if (!strcmp (p, "write"))
36044 new_code = NOP_EXPR;
36045 else if (!strcmp (p, "update"))
36046 new_code = OMP_ATOMIC;
36047 else if (!strcmp (p, "capture"))
36048 new_code = OMP_ATOMIC_CAPTURE_NEW;
36049 else if (!strcmp (p, "seq_cst"))
36050 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36051 else if (!strcmp (p, "acq_rel"))
36052 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
36053 else if (!strcmp (p, "release"))
36054 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
36055 else if (!strcmp (p, "acquire"))
36056 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
36057 else if (!strcmp (p, "relaxed"))
36058 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
36059 else if (!strcmp (p, "hint"))
36061 cp_lexer_consume_token (parser->lexer);
36062 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
36063 continue;
36065 else
36067 p = NULL;
36068 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
36069 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
36070 "%<release%>, %<relaxed%> or %<hint%> clause");
36072 if (p)
36074 if (new_code != ERROR_MARK)
36076 if (code != ERROR_MARK)
36077 error_at (cloc, "too many atomic clauses");
36078 else
36079 code = new_code;
36081 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
36083 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
36084 error_at (cloc, "too many memory order clauses");
36085 else
36086 memory_order = new_memory_order;
36088 cp_lexer_consume_token (parser->lexer);
36089 continue;
36092 break;
36094 cp_parser_require_pragma_eol (parser, pragma_tok);
36096 if (code == ERROR_MARK)
36097 code = OMP_ATOMIC;
36098 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
36100 omp_requires_mask
36101 = (enum omp_requires) (omp_requires_mask
36102 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
36103 switch ((enum omp_memory_order)
36104 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
36106 case OMP_MEMORY_ORDER_UNSPECIFIED:
36107 case OMP_MEMORY_ORDER_RELAXED:
36108 memory_order = OMP_MEMORY_ORDER_RELAXED;
36109 break;
36110 case OMP_MEMORY_ORDER_SEQ_CST:
36111 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36112 break;
36113 case OMP_MEMORY_ORDER_ACQ_REL:
36114 switch (code)
36116 case OMP_ATOMIC_READ:
36117 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
36118 break;
36119 case NOP_EXPR: /* atomic write */
36120 case OMP_ATOMIC:
36121 memory_order = OMP_MEMORY_ORDER_RELEASE;
36122 break;
36123 default:
36124 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
36125 break;
36127 break;
36128 default:
36129 gcc_unreachable ();
36132 else
36133 switch (code)
36135 case OMP_ATOMIC_READ:
36136 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36137 || memory_order == OMP_MEMORY_ORDER_RELEASE)
36139 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
36140 "%<acq_rel%> or %<release%> clauses");
36141 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36143 break;
36144 case NOP_EXPR: /* atomic write */
36145 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36146 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36148 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
36149 "%<acq_rel%> or %<acquire%> clauses");
36150 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36152 break;
36153 case OMP_ATOMIC:
36154 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36155 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36157 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
36158 "%<acq_rel%> or %<acquire%> clauses");
36159 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36161 break;
36162 default:
36163 break;
36166 switch (code)
36168 case OMP_ATOMIC_READ:
36169 case NOP_EXPR: /* atomic write */
36170 v = cp_parser_unary_expression (parser);
36171 if (v == error_mark_node)
36172 goto saw_error;
36173 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36174 goto saw_error;
36175 if (code == NOP_EXPR)
36176 lhs = cp_parser_expression (parser);
36177 else
36178 lhs = cp_parser_unary_expression (parser);
36179 if (lhs == error_mark_node)
36180 goto saw_error;
36181 if (code == NOP_EXPR)
36183 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
36184 opcode. */
36185 code = OMP_ATOMIC;
36186 rhs = lhs;
36187 lhs = v;
36188 v = NULL_TREE;
36190 goto done;
36191 case OMP_ATOMIC_CAPTURE_NEW:
36192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36194 cp_lexer_consume_token (parser->lexer);
36195 structured_block = true;
36197 else
36199 v = cp_parser_unary_expression (parser);
36200 if (v == error_mark_node)
36201 goto saw_error;
36202 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36203 goto saw_error;
36205 default:
36206 break;
36209 restart:
36210 lhs = cp_parser_unary_expression (parser);
36211 orig_lhs = lhs;
36212 switch (TREE_CODE (lhs))
36214 case ERROR_MARK:
36215 goto saw_error;
36217 case POSTINCREMENT_EXPR:
36218 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36219 code = OMP_ATOMIC_CAPTURE_OLD;
36220 /* FALLTHROUGH */
36221 case PREINCREMENT_EXPR:
36222 lhs = TREE_OPERAND (lhs, 0);
36223 opcode = PLUS_EXPR;
36224 rhs = integer_one_node;
36225 break;
36227 case POSTDECREMENT_EXPR:
36228 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36229 code = OMP_ATOMIC_CAPTURE_OLD;
36230 /* FALLTHROUGH */
36231 case PREDECREMENT_EXPR:
36232 lhs = TREE_OPERAND (lhs, 0);
36233 opcode = MINUS_EXPR;
36234 rhs = integer_one_node;
36235 break;
36237 case COMPOUND_EXPR:
36238 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
36239 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
36240 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
36241 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
36242 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
36243 (TREE_OPERAND (lhs, 1), 0), 0)))
36244 == BOOLEAN_TYPE)
36245 /* Undo effects of boolean_increment for post {in,de}crement. */
36246 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
36247 /* FALLTHRU */
36248 case MODIFY_EXPR:
36249 if (TREE_CODE (lhs) == MODIFY_EXPR
36250 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
36252 /* Undo effects of boolean_increment. */
36253 if (integer_onep (TREE_OPERAND (lhs, 1)))
36255 /* This is pre or post increment. */
36256 rhs = TREE_OPERAND (lhs, 1);
36257 lhs = TREE_OPERAND (lhs, 0);
36258 opcode = NOP_EXPR;
36259 if (code == OMP_ATOMIC_CAPTURE_NEW
36260 && !structured_block
36261 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
36262 code = OMP_ATOMIC_CAPTURE_OLD;
36263 break;
36266 /* FALLTHRU */
36267 default:
36268 switch (cp_lexer_peek_token (parser->lexer)->type)
36270 case CPP_MULT_EQ:
36271 opcode = MULT_EXPR;
36272 break;
36273 case CPP_DIV_EQ:
36274 opcode = TRUNC_DIV_EXPR;
36275 break;
36276 case CPP_PLUS_EQ:
36277 opcode = PLUS_EXPR;
36278 break;
36279 case CPP_MINUS_EQ:
36280 opcode = MINUS_EXPR;
36281 break;
36282 case CPP_LSHIFT_EQ:
36283 opcode = LSHIFT_EXPR;
36284 break;
36285 case CPP_RSHIFT_EQ:
36286 opcode = RSHIFT_EXPR;
36287 break;
36288 case CPP_AND_EQ:
36289 opcode = BIT_AND_EXPR;
36290 break;
36291 case CPP_OR_EQ:
36292 opcode = BIT_IOR_EXPR;
36293 break;
36294 case CPP_XOR_EQ:
36295 opcode = BIT_XOR_EXPR;
36296 break;
36297 case CPP_EQ:
36298 enum cp_parser_prec oprec;
36299 cp_token *token;
36300 cp_lexer_consume_token (parser->lexer);
36301 cp_parser_parse_tentatively (parser);
36302 rhs1 = cp_parser_simple_cast_expression (parser);
36303 if (rhs1 == error_mark_node)
36305 cp_parser_abort_tentative_parse (parser);
36306 cp_parser_simple_cast_expression (parser);
36307 goto saw_error;
36309 token = cp_lexer_peek_token (parser->lexer);
36310 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
36312 cp_parser_abort_tentative_parse (parser);
36313 cp_parser_parse_tentatively (parser);
36314 rhs = cp_parser_binary_expression (parser, false, true,
36315 PREC_NOT_OPERATOR, NULL);
36316 if (rhs == error_mark_node)
36318 cp_parser_abort_tentative_parse (parser);
36319 cp_parser_binary_expression (parser, false, true,
36320 PREC_NOT_OPERATOR, NULL);
36321 goto saw_error;
36323 switch (TREE_CODE (rhs))
36325 case MULT_EXPR:
36326 case TRUNC_DIV_EXPR:
36327 case RDIV_EXPR:
36328 case PLUS_EXPR:
36329 case MINUS_EXPR:
36330 case LSHIFT_EXPR:
36331 case RSHIFT_EXPR:
36332 case BIT_AND_EXPR:
36333 case BIT_IOR_EXPR:
36334 case BIT_XOR_EXPR:
36335 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
36337 if (cp_parser_parse_definitely (parser))
36339 opcode = TREE_CODE (rhs);
36340 rhs1 = TREE_OPERAND (rhs, 0);
36341 rhs = TREE_OPERAND (rhs, 1);
36342 goto stmt_done;
36344 else
36345 goto saw_error;
36347 break;
36348 default:
36349 break;
36351 cp_parser_abort_tentative_parse (parser);
36352 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
36354 rhs = cp_parser_expression (parser);
36355 if (rhs == error_mark_node)
36356 goto saw_error;
36357 opcode = NOP_EXPR;
36358 rhs1 = NULL_TREE;
36359 goto stmt_done;
36361 cp_parser_error (parser,
36362 "invalid form of %<#pragma omp atomic%>");
36363 goto saw_error;
36365 if (!cp_parser_parse_definitely (parser))
36366 goto saw_error;
36367 switch (token->type)
36369 case CPP_SEMICOLON:
36370 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36372 code = OMP_ATOMIC_CAPTURE_OLD;
36373 v = lhs;
36374 lhs = NULL_TREE;
36375 lhs1 = rhs1;
36376 rhs1 = NULL_TREE;
36377 cp_lexer_consume_token (parser->lexer);
36378 goto restart;
36380 else if (structured_block)
36382 opcode = NOP_EXPR;
36383 rhs = rhs1;
36384 rhs1 = NULL_TREE;
36385 goto stmt_done;
36387 cp_parser_error (parser,
36388 "invalid form of %<#pragma omp atomic%>");
36389 goto saw_error;
36390 case CPP_MULT:
36391 opcode = MULT_EXPR;
36392 break;
36393 case CPP_DIV:
36394 opcode = TRUNC_DIV_EXPR;
36395 break;
36396 case CPP_PLUS:
36397 opcode = PLUS_EXPR;
36398 break;
36399 case CPP_MINUS:
36400 opcode = MINUS_EXPR;
36401 break;
36402 case CPP_LSHIFT:
36403 opcode = LSHIFT_EXPR;
36404 break;
36405 case CPP_RSHIFT:
36406 opcode = RSHIFT_EXPR;
36407 break;
36408 case CPP_AND:
36409 opcode = BIT_AND_EXPR;
36410 break;
36411 case CPP_OR:
36412 opcode = BIT_IOR_EXPR;
36413 break;
36414 case CPP_XOR:
36415 opcode = BIT_XOR_EXPR;
36416 break;
36417 default:
36418 cp_parser_error (parser,
36419 "invalid operator for %<#pragma omp atomic%>");
36420 goto saw_error;
36422 oprec = TOKEN_PRECEDENCE (token);
36423 gcc_assert (oprec != PREC_NOT_OPERATOR);
36424 if (commutative_tree_code (opcode))
36425 oprec = (enum cp_parser_prec) (oprec - 1);
36426 cp_lexer_consume_token (parser->lexer);
36427 rhs = cp_parser_binary_expression (parser, false, false,
36428 oprec, NULL);
36429 if (rhs == error_mark_node)
36430 goto saw_error;
36431 goto stmt_done;
36432 /* FALLTHROUGH */
36433 default:
36434 cp_parser_error (parser,
36435 "invalid operator for %<#pragma omp atomic%>");
36436 goto saw_error;
36438 cp_lexer_consume_token (parser->lexer);
36440 rhs = cp_parser_expression (parser);
36441 if (rhs == error_mark_node)
36442 goto saw_error;
36443 break;
36445 stmt_done:
36446 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36448 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
36449 goto saw_error;
36450 v = cp_parser_unary_expression (parser);
36451 if (v == error_mark_node)
36452 goto saw_error;
36453 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36454 goto saw_error;
36455 lhs1 = cp_parser_unary_expression (parser);
36456 if (lhs1 == error_mark_node)
36457 goto saw_error;
36459 if (structured_block)
36461 cp_parser_consume_semicolon_at_end_of_statement (parser);
36462 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
36464 done:
36465 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36466 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
36467 rhs1, clauses, memory_order);
36468 if (!structured_block)
36469 cp_parser_consume_semicolon_at_end_of_statement (parser);
36470 return;
36472 saw_error:
36473 cp_parser_skip_to_end_of_block_or_statement (parser);
36474 if (structured_block)
36476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36477 cp_lexer_consume_token (parser->lexer);
36478 else if (code == OMP_ATOMIC_CAPTURE_NEW)
36480 cp_parser_skip_to_end_of_block_or_statement (parser);
36481 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36482 cp_lexer_consume_token (parser->lexer);
36488 /* OpenMP 2.5:
36489 # pragma omp barrier new-line */
36491 static void
36492 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
36494 cp_parser_require_pragma_eol (parser, pragma_tok);
36495 finish_omp_barrier ();
36498 /* OpenMP 2.5:
36499 # pragma omp critical [(name)] new-line
36500 structured-block
36502 OpenMP 4.5:
36503 # pragma omp critical [(name) [hint(expression)]] new-line
36504 structured-block */
36506 #define OMP_CRITICAL_CLAUSE_MASK \
36507 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36509 static tree
36510 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36512 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36516 matching_parens parens;
36517 parens.consume_open (parser);
36519 name = cp_parser_identifier (parser);
36521 if (name == error_mark_node
36522 || !parens.require_close (parser))
36523 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36524 /*or_comma=*/false,
36525 /*consume_paren=*/true);
36526 if (name == error_mark_node)
36527 name = NULL;
36529 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36530 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36531 cp_lexer_consume_token (parser->lexer);
36533 clauses = cp_parser_omp_all_clauses (parser,
36534 OMP_CRITICAL_CLAUSE_MASK,
36535 "#pragma omp critical", pragma_tok);
36537 else
36538 cp_parser_require_pragma_eol (parser, pragma_tok);
36540 stmt = cp_parser_omp_structured_block (parser, if_p);
36541 return c_finish_omp_critical (input_location, stmt, name, clauses);
36544 /* OpenMP 5.0:
36545 # pragma omp depobj ( depobj ) depobj-clause new-line
36547 depobj-clause:
36548 depend (dependence-type : locator)
36549 destroy
36550 update (dependence-type)
36552 dependence-type:
36555 inout
36556 mutexinout */
36558 static void
36559 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36561 location_t loc = pragma_tok->location;
36562 matching_parens parens;
36563 if (!parens.require_open (parser))
36565 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36566 return;
36569 tree depobj = cp_parser_assignment_expression (parser);
36571 if (!parens.require_close (parser))
36572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36573 /*or_comma=*/false,
36574 /*consume_paren=*/true);
36576 tree clause = NULL_TREE;
36577 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36578 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36581 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36582 const char *p = IDENTIFIER_POINTER (id);
36584 cp_lexer_consume_token (parser->lexer);
36585 if (!strcmp ("depend", p))
36587 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36588 if (clause)
36589 clause = finish_omp_clauses (clause, C_ORT_OMP);
36590 if (!clause)
36591 clause = error_mark_node;
36593 else if (!strcmp ("destroy", p))
36594 kind = OMP_CLAUSE_DEPEND_LAST;
36595 else if (!strcmp ("update", p))
36597 matching_parens c_parens;
36598 if (c_parens.require_open (parser))
36600 location_t c2_loc
36601 = cp_lexer_peek_token (parser->lexer)->location;
36602 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36604 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36605 const char *p2 = IDENTIFIER_POINTER (id2);
36607 cp_lexer_consume_token (parser->lexer);
36608 if (!strcmp ("in", p2))
36609 kind = OMP_CLAUSE_DEPEND_IN;
36610 else if (!strcmp ("out", p2))
36611 kind = OMP_CLAUSE_DEPEND_OUT;
36612 else if (!strcmp ("inout", p2))
36613 kind = OMP_CLAUSE_DEPEND_INOUT;
36614 else if (!strcmp ("mutexinoutset", p2))
36615 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36617 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36619 clause = error_mark_node;
36620 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36621 "%<mutexinoutset%>");
36623 if (!c_parens.require_close (parser))
36624 cp_parser_skip_to_closing_parenthesis (parser,
36625 /*recovering=*/true,
36626 /*or_comma=*/false,
36627 /*consume_paren=*/true);
36629 else
36630 clause = error_mark_node;
36633 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36635 clause = error_mark_node;
36636 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36638 cp_parser_require_pragma_eol (parser, pragma_tok);
36640 finish_omp_depobj (loc, depobj, kind, clause);
36644 /* OpenMP 2.5:
36645 # pragma omp flush flush-vars[opt] new-line
36647 flush-vars:
36648 ( variable-list )
36650 OpenMP 5.0:
36651 # pragma omp flush memory-order-clause new-line */
36653 static void
36654 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36656 enum memmodel mo = MEMMODEL_LAST;
36657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36659 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36660 const char *p = IDENTIFIER_POINTER (id);
36661 if (!strcmp (p, "acq_rel"))
36662 mo = MEMMODEL_ACQ_REL;
36663 else if (!strcmp (p, "release"))
36664 mo = MEMMODEL_RELEASE;
36665 else if (!strcmp (p, "acquire"))
36666 mo = MEMMODEL_ACQUIRE;
36667 else
36668 error_at (cp_lexer_peek_token (parser->lexer)->location,
36669 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36670 cp_lexer_consume_token (parser->lexer);
36672 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36674 if (mo != MEMMODEL_LAST)
36675 error_at (cp_lexer_peek_token (parser->lexer)->location,
36676 "%<flush%> list specified together with memory order "
36677 "clause");
36678 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36680 cp_parser_require_pragma_eol (parser, pragma_tok);
36682 finish_omp_flush (mo);
36685 /* Helper function, to parse omp for increment expression. */
36687 static tree
36688 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36690 tree cond = cp_parser_binary_expression (parser, false, true,
36691 PREC_NOT_OPERATOR, NULL);
36692 if (cond == error_mark_node
36693 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36695 cp_parser_skip_to_end_of_statement (parser);
36696 return error_mark_node;
36699 switch (TREE_CODE (cond))
36701 case GT_EXPR:
36702 case GE_EXPR:
36703 case LT_EXPR:
36704 case LE_EXPR:
36705 break;
36706 case NE_EXPR:
36707 if (code != OACC_LOOP)
36708 break;
36709 gcc_fallthrough ();
36710 default:
36711 return error_mark_node;
36714 /* If decl is an iterator, preserve LHS and RHS of the relational
36715 expr until finish_omp_for. */
36716 if (decl
36717 && (type_dependent_expression_p (decl)
36718 || CLASS_TYPE_P (TREE_TYPE (decl))))
36719 return cond;
36721 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
36722 TREE_CODE (cond),
36723 TREE_OPERAND (cond, 0), ERROR_MARK,
36724 TREE_OPERAND (cond, 1), ERROR_MARK,
36725 /*overload=*/NULL, tf_warning_or_error);
36728 /* Helper function, to parse omp for increment expression. */
36730 static tree
36731 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36733 cp_token *token = cp_lexer_peek_token (parser->lexer);
36734 enum tree_code op;
36735 tree lhs, rhs;
36736 cp_id_kind idk;
36737 bool decl_first;
36739 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36741 op = (token->type == CPP_PLUS_PLUS
36742 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36743 cp_lexer_consume_token (parser->lexer);
36744 lhs = cp_parser_simple_cast_expression (parser);
36745 if (lhs != decl
36746 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36747 return error_mark_node;
36748 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36751 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36752 if (lhs != decl
36753 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36754 return error_mark_node;
36756 token = cp_lexer_peek_token (parser->lexer);
36757 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36759 op = (token->type == CPP_PLUS_PLUS
36760 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36761 cp_lexer_consume_token (parser->lexer);
36762 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36765 op = cp_parser_assignment_operator_opt (parser);
36766 if (op == ERROR_MARK)
36767 return error_mark_node;
36769 if (op != NOP_EXPR)
36771 rhs = cp_parser_assignment_expression (parser);
36772 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36773 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36776 lhs = cp_parser_binary_expression (parser, false, false,
36777 PREC_ADDITIVE_EXPRESSION, NULL);
36778 token = cp_lexer_peek_token (parser->lexer);
36779 decl_first = (lhs == decl
36780 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36781 if (decl_first)
36782 lhs = NULL_TREE;
36783 if (token->type != CPP_PLUS
36784 && token->type != CPP_MINUS)
36785 return error_mark_node;
36789 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36790 cp_lexer_consume_token (parser->lexer);
36791 rhs = cp_parser_binary_expression (parser, false, false,
36792 PREC_ADDITIVE_EXPRESSION, NULL);
36793 token = cp_lexer_peek_token (parser->lexer);
36794 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36796 if (lhs == NULL_TREE)
36798 if (op == PLUS_EXPR)
36799 lhs = rhs;
36800 else
36801 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36802 tf_warning_or_error);
36804 else
36805 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36806 ERROR_MARK, NULL, tf_warning_or_error);
36809 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36811 if (!decl_first)
36813 if ((rhs != decl
36814 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36815 || op == MINUS_EXPR)
36816 return error_mark_node;
36817 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36819 else
36820 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36822 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36825 /* Parse the initialization statement of an OpenMP for loop.
36827 Return true if the resulting construct should have an
36828 OMP_CLAUSE_PRIVATE added to it. */
36830 static tree
36831 cp_parser_omp_for_loop_init (cp_parser *parser,
36832 tree &this_pre_body,
36833 releasing_vec &for_block,
36834 tree &init,
36835 tree &orig_init,
36836 tree &decl,
36837 tree &real_decl)
36839 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36840 return NULL_TREE;
36842 tree add_private_clause = NULL_TREE;
36844 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36846 init-expr:
36847 var = lb
36848 integer-type var = lb
36849 random-access-iterator-type var = lb
36850 pointer-type var = lb
36852 cp_decl_specifier_seq type_specifiers;
36854 /* First, try to parse as an initialized declaration. See
36855 cp_parser_condition, from whence the bulk of this is copied. */
36857 cp_parser_parse_tentatively (parser);
36858 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36859 /*is_declaration=*/true,
36860 /*is_trailing_return=*/false,
36861 &type_specifiers);
36862 if (cp_parser_parse_definitely (parser))
36864 /* If parsing a type specifier seq succeeded, then this
36865 MUST be a initialized declaration. */
36866 tree asm_specification, attributes;
36867 cp_declarator *declarator;
36869 declarator = cp_parser_declarator (parser,
36870 CP_PARSER_DECLARATOR_NAMED,
36871 CP_PARSER_FLAGS_NONE,
36872 /*ctor_dtor_or_conv_p=*/NULL,
36873 /*parenthesized_p=*/NULL,
36874 /*member_p=*/false,
36875 /*friend_p=*/false,
36876 /*static_p=*/false);
36877 attributes = cp_parser_attributes_opt (parser);
36878 asm_specification = cp_parser_asm_specification_opt (parser);
36880 if (declarator == cp_error_declarator)
36881 cp_parser_skip_to_end_of_statement (parser);
36883 else
36885 tree pushed_scope, auto_node;
36887 decl = start_decl (declarator, &type_specifiers,
36888 SD_INITIALIZED, attributes,
36889 /*prefix_attributes=*/NULL_TREE,
36890 &pushed_scope);
36892 auto_node = type_uses_auto (TREE_TYPE (decl));
36893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36895 if (cp_lexer_next_token_is (parser->lexer,
36896 CPP_OPEN_PAREN))
36897 error ("parenthesized initialization is not allowed in "
36898 "OpenMP %<for%> loop");
36899 else
36900 /* Trigger an error. */
36901 cp_parser_require (parser, CPP_EQ, RT_EQ);
36903 init = error_mark_node;
36904 cp_parser_skip_to_end_of_statement (parser);
36906 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36907 || type_dependent_expression_p (decl)
36908 || auto_node)
36910 bool is_direct_init, is_non_constant_init;
36912 init = cp_parser_initializer (parser,
36913 &is_direct_init,
36914 &is_non_constant_init);
36916 if (auto_node)
36918 TREE_TYPE (decl)
36919 = do_auto_deduction (TREE_TYPE (decl), init,
36920 auto_node);
36922 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36923 && !type_dependent_expression_p (decl))
36924 goto non_class;
36927 cp_finish_decl (decl, init, !is_non_constant_init,
36928 asm_specification,
36929 LOOKUP_ONLYCONVERTING);
36930 orig_init = init;
36931 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36933 vec_safe_push (for_block, this_pre_body);
36934 init = NULL_TREE;
36936 else
36938 init = pop_stmt_list (this_pre_body);
36939 if (init && TREE_CODE (init) == STATEMENT_LIST)
36941 tree_stmt_iterator i = tsi_start (init);
36942 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36943 while (!tsi_end_p (i))
36945 tree t = tsi_stmt (i);
36946 if (TREE_CODE (t) == DECL_EXPR
36947 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36949 tsi_delink (&i);
36950 vec_safe_push (for_block, t);
36951 continue;
36953 break;
36955 if (tsi_one_before_end_p (i))
36957 tree t = tsi_stmt (i);
36958 tsi_delink (&i);
36959 free_stmt_list (init);
36960 init = t;
36964 this_pre_body = NULL_TREE;
36966 else
36968 /* Consume '='. */
36969 cp_lexer_consume_token (parser->lexer);
36970 init = cp_parser_assignment_expression (parser);
36972 non_class:
36973 if (TYPE_REF_P (TREE_TYPE (decl)))
36974 init = error_mark_node;
36975 else
36976 cp_finish_decl (decl, NULL_TREE,
36977 /*init_const_expr_p=*/false,
36978 asm_specification,
36979 LOOKUP_ONLYCONVERTING);
36982 if (pushed_scope)
36983 pop_scope (pushed_scope);
36986 else
36988 cp_id_kind idk;
36989 /* If parsing a type specifier sequence failed, then
36990 this MUST be a simple expression. */
36991 cp_parser_parse_tentatively (parser);
36992 decl = cp_parser_primary_expression (parser, false, false,
36993 false, &idk);
36994 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36995 if (!cp_parser_error_occurred (parser)
36996 && decl
36997 && (TREE_CODE (decl) == COMPONENT_REF
36998 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
37000 cp_parser_abort_tentative_parse (parser);
37001 cp_parser_parse_tentatively (parser);
37002 cp_token *token = cp_lexer_peek_token (parser->lexer);
37003 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
37004 /*check_dependency_p=*/true,
37005 /*template_p=*/NULL,
37006 /*declarator_p=*/false,
37007 /*optional_p=*/false);
37008 if (name != error_mark_node
37009 && last_tok == cp_lexer_peek_token (parser->lexer))
37011 decl = cp_parser_lookup_name_simple (parser, name,
37012 token->location);
37013 if (TREE_CODE (decl) == FIELD_DECL)
37014 add_private_clause = omp_privatize_field (decl, false);
37016 cp_parser_abort_tentative_parse (parser);
37017 cp_parser_parse_tentatively (parser);
37018 decl = cp_parser_primary_expression (parser, false, false,
37019 false, &idk);
37021 if (!cp_parser_error_occurred (parser)
37022 && decl
37023 && DECL_P (decl)
37024 && CLASS_TYPE_P (TREE_TYPE (decl)))
37026 tree rhs;
37028 cp_parser_parse_definitely (parser);
37029 cp_parser_require (parser, CPP_EQ, RT_EQ);
37030 rhs = cp_parser_assignment_expression (parser);
37031 orig_init = rhs;
37032 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
37033 decl, NOP_EXPR,
37034 rhs,
37035 tf_warning_or_error));
37036 if (!add_private_clause)
37037 add_private_clause = decl;
37039 else
37041 decl = NULL;
37042 cp_parser_abort_tentative_parse (parser);
37043 init = cp_parser_expression (parser);
37044 if (init)
37046 if (TREE_CODE (init) == MODIFY_EXPR
37047 || TREE_CODE (init) == MODOP_EXPR)
37048 real_decl = TREE_OPERAND (init, 0);
37052 return add_private_clause;
37055 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
37057 void
37058 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
37059 tree &decl, tree &orig_decl, tree &init,
37060 tree &orig_init, tree &cond, tree &incr)
37062 tree begin, end, range_temp_decl = NULL_TREE;
37063 tree iter_type, begin_expr, end_expr;
37065 if (processing_template_decl)
37067 if (check_for_bare_parameter_packs (init))
37068 init = error_mark_node;
37069 if (!type_dependent_expression_p (init)
37070 /* do_auto_deduction doesn't mess with template init-lists. */
37071 && !BRACE_ENCLOSED_INITIALIZER_P (init))
37073 tree d = decl;
37074 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
37076 tree v = DECL_VALUE_EXPR (decl);
37077 if (TREE_CODE (v) == ARRAY_REF
37078 && VAR_P (TREE_OPERAND (v, 0))
37079 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
37080 d = TREE_OPERAND (v, 0);
37082 do_range_for_auto_deduction (d, init);
37084 cond = global_namespace;
37085 incr = NULL_TREE;
37086 orig_init = init;
37087 if (this_pre_body)
37088 this_pre_body = pop_stmt_list (this_pre_body);
37089 return;
37092 init = mark_lvalue_use (init);
37094 if (decl == error_mark_node || init == error_mark_node)
37095 /* If an error happened previously do nothing or else a lot of
37096 unhelpful errors would be issued. */
37097 begin_expr = end_expr = iter_type = error_mark_node;
37098 else
37100 tree range_temp;
37102 if (VAR_P (init)
37103 && array_of_runtime_bound_p (TREE_TYPE (init)))
37104 /* Can't bind a reference to an array of runtime bound. */
37105 range_temp = init;
37106 else
37108 range_temp = build_range_temp (init);
37109 DECL_NAME (range_temp) = NULL_TREE;
37110 pushdecl (range_temp);
37111 cp_finish_decl (range_temp, init,
37112 /*is_constant_init*/false, NULL_TREE,
37113 LOOKUP_ONLYCONVERTING);
37114 range_temp_decl = range_temp;
37115 range_temp = convert_from_reference (range_temp);
37117 iter_type = cp_parser_perform_range_for_lookup (range_temp,
37118 &begin_expr, &end_expr);
37121 tree end_iter_type = iter_type;
37122 if (cxx_dialect >= cxx17)
37123 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
37124 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
37125 TREE_USED (end) = 1;
37126 DECL_ARTIFICIAL (end) = 1;
37127 pushdecl (end);
37128 cp_finish_decl (end, end_expr,
37129 /*is_constant_init*/false, NULL_TREE,
37130 LOOKUP_ONLYCONVERTING);
37132 /* The new for initialization statement. */
37133 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
37134 TREE_USED (begin) = 1;
37135 DECL_ARTIFICIAL (begin) = 1;
37136 pushdecl (begin);
37137 orig_init = init;
37138 if (CLASS_TYPE_P (iter_type))
37139 init = NULL_TREE;
37140 else
37142 init = begin_expr;
37143 begin_expr = NULL_TREE;
37145 cp_finish_decl (begin, begin_expr,
37146 /*is_constant_init*/false, NULL_TREE,
37147 LOOKUP_ONLYCONVERTING);
37149 /* The new for condition. */
37150 if (CLASS_TYPE_P (iter_type))
37151 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
37152 else
37153 cond = build_x_binary_op (input_location, NE_EXPR,
37154 begin, ERROR_MARK,
37155 end, ERROR_MARK,
37156 NULL, tf_warning_or_error);
37158 /* The new increment expression. */
37159 if (CLASS_TYPE_P (iter_type))
37160 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
37161 else
37162 incr = finish_unary_op_expr (input_location,
37163 PREINCREMENT_EXPR, begin,
37164 tf_warning_or_error);
37166 orig_decl = decl;
37167 decl = begin;
37168 if (for_block)
37170 vec_safe_push (for_block, this_pre_body);
37171 this_pre_body = NULL_TREE;
37174 tree decomp_first_name = NULL_TREE;
37175 unsigned decomp_cnt = 0;
37176 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
37178 tree v = DECL_VALUE_EXPR (orig_decl);
37179 if (TREE_CODE (v) == ARRAY_REF
37180 && VAR_P (TREE_OPERAND (v, 0))
37181 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
37183 tree d = orig_decl;
37184 orig_decl = TREE_OPERAND (v, 0);
37185 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
37186 decomp_first_name = d;
37190 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
37191 if (auto_node)
37193 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37194 tf_none);
37195 if (!error_operand_p (t))
37196 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
37197 t, auto_node);
37200 tree v = make_tree_vec (decomp_cnt + 3);
37201 TREE_VEC_ELT (v, 0) = range_temp_decl;
37202 TREE_VEC_ELT (v, 1) = end;
37203 TREE_VEC_ELT (v, 2) = orig_decl;
37204 for (unsigned i = 0; i < decomp_cnt; i++)
37206 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
37207 decomp_first_name = DECL_CHAIN (decomp_first_name);
37209 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
37212 /* Helper for cp_parser_omp_for_loop, finalize part of range for
37213 inside of the collapsed body. */
37215 void
37216 cp_finish_omp_range_for (tree orig, tree begin)
37218 gcc_assert (TREE_CODE (orig) == TREE_LIST
37219 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
37220 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
37221 tree decomp_first_name = NULL_TREE;
37222 unsigned int decomp_cnt = 0;
37224 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37226 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
37227 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
37228 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
37231 /* The declaration is initialized with *__begin inside the loop body. */
37232 cp_finish_decl (decl,
37233 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37234 tf_warning_or_error),
37235 /*is_constant_init*/false, NULL_TREE,
37236 LOOKUP_ONLYCONVERTING);
37237 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37238 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
37241 /* OpenMP 5.0:
37243 scan-loop-body:
37244 { structured-block scan-directive structured-block } */
37246 static void
37247 cp_parser_omp_scan_loop_body (cp_parser *parser)
37249 tree substmt, clauses = NULL_TREE;
37251 matching_braces braces;
37252 if (!braces.require_open (parser))
37253 return;
37255 substmt = cp_parser_omp_structured_block (parser, NULL);
37256 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
37257 add_stmt (substmt);
37259 cp_token *tok = cp_lexer_peek_token (parser->lexer);
37260 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
37262 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
37264 cp_lexer_consume_token (parser->lexer);
37266 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37268 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37269 const char *p = IDENTIFIER_POINTER (id);
37270 if (strcmp (p, "inclusive") == 0)
37271 clause = OMP_CLAUSE_INCLUSIVE;
37272 else if (strcmp (p, "exclusive") == 0)
37273 clause = OMP_CLAUSE_EXCLUSIVE;
37275 if (clause != OMP_CLAUSE_ERROR)
37277 cp_lexer_consume_token (parser->lexer);
37278 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
37280 else
37281 cp_parser_error (parser, "expected %<inclusive%> or "
37282 "%<exclusive%> clause");
37284 cp_parser_require_pragma_eol (parser, tok);
37286 else
37287 error ("expected %<#pragma omp scan%>");
37289 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37290 substmt = cp_parser_omp_structured_block (parser, NULL);
37291 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
37292 clauses);
37293 add_stmt (substmt);
37295 braces.require_close (parser);
37298 /* Parse the restricted form of the for statement allowed by OpenMP. */
37300 static tree
37301 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
37302 tree *cclauses, bool *if_p)
37304 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
37305 tree orig_decl;
37306 tree real_decl, initv, condv, incrv, declv, orig_declv;
37307 tree this_pre_body, cl, ordered_cl = NULL_TREE;
37308 location_t loc_first;
37309 bool collapse_err = false;
37310 int i, collapse = 1, ordered = 0, count, nbraces = 0;
37311 releasing_vec for_block;
37312 auto_vec<tree, 4> orig_inits;
37313 bool tiling = false;
37314 bool inscan = false;
37316 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
37317 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
37318 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
37319 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
37321 tiling = true;
37322 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
37324 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
37325 && OMP_CLAUSE_ORDERED_EXPR (cl))
37327 ordered_cl = cl;
37328 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
37330 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
37331 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
37332 && (code == OMP_SIMD || code == OMP_FOR))
37333 inscan = true;
37335 if (ordered && ordered < collapse)
37337 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
37338 "%<ordered%> clause parameter is less than %<collapse%>");
37339 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
37340 = build_int_cst (NULL_TREE, collapse);
37341 ordered = collapse;
37343 if (ordered)
37345 for (tree *pc = &clauses; *pc; )
37346 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
37348 error_at (OMP_CLAUSE_LOCATION (*pc),
37349 "%<linear%> clause may not be specified together "
37350 "with %<ordered%> clause with a parameter");
37351 *pc = OMP_CLAUSE_CHAIN (*pc);
37353 else
37354 pc = &OMP_CLAUSE_CHAIN (*pc);
37357 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
37358 count = ordered ? ordered : collapse;
37360 declv = make_tree_vec (count);
37361 initv = make_tree_vec (count);
37362 condv = make_tree_vec (count);
37363 incrv = make_tree_vec (count);
37364 orig_declv = NULL_TREE;
37366 loc_first = cp_lexer_peek_token (parser->lexer)->location;
37368 for (i = 0; i < count; i++)
37370 int bracecount = 0;
37371 tree add_private_clause = NULL_TREE;
37372 location_t loc;
37374 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37376 if (!collapse_err)
37377 cp_parser_error (parser, "for statement expected");
37378 return NULL;
37380 loc = cp_lexer_consume_token (parser->lexer)->location;
37382 /* Don't create location wrapper nodes within an OpenMP "for"
37383 statement. */
37384 auto_suppress_location_wrappers sentinel;
37386 matching_parens parens;
37387 if (!parens.require_open (parser))
37388 return NULL;
37390 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
37391 this_pre_body = push_stmt_list ();
37393 if (code != OACC_LOOP && cxx_dialect >= cxx11)
37395 /* Save tokens so that we can put them back. */
37396 cp_lexer_save_tokens (parser->lexer);
37398 /* Look for ':' that is not nested in () or {}. */
37399 bool is_range_for
37400 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
37401 /*recovering=*/false,
37402 CPP_COLON,
37403 /*consume_paren=*/
37404 false) == -1);
37406 /* Roll back the tokens we skipped. */
37407 cp_lexer_rollback_tokens (parser->lexer);
37409 if (is_range_for)
37411 bool saved_colon_corrects_to_scope_p
37412 = parser->colon_corrects_to_scope_p;
37414 /* A colon is used in range-based for. */
37415 parser->colon_corrects_to_scope_p = false;
37417 /* Parse the declaration. */
37418 cp_parser_simple_declaration (parser,
37419 /*function_definition_allowed_p=*/
37420 false, &decl);
37421 parser->colon_corrects_to_scope_p
37422 = saved_colon_corrects_to_scope_p;
37424 cp_parser_require (parser, CPP_COLON, RT_COLON);
37426 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
37427 false, 0, true);
37429 cp_convert_omp_range_for (this_pre_body, for_block, decl,
37430 orig_decl, init, orig_init,
37431 cond, incr);
37432 if (this_pre_body)
37434 if (pre_body)
37436 tree t = pre_body;
37437 pre_body = push_stmt_list ();
37438 add_stmt (t);
37439 add_stmt (this_pre_body);
37440 pre_body = pop_stmt_list (pre_body);
37442 else
37443 pre_body = this_pre_body;
37446 if (ordered_cl)
37447 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
37448 "%<ordered%> clause with parameter on "
37449 "range-based %<for%> loop");
37451 goto parse_close_paren;
37455 add_private_clause
37456 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
37457 init, orig_init, decl, real_decl);
37459 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37460 if (this_pre_body)
37462 this_pre_body = pop_stmt_list (this_pre_body);
37463 if (pre_body)
37465 tree t = pre_body;
37466 pre_body = push_stmt_list ();
37467 add_stmt (t);
37468 add_stmt (this_pre_body);
37469 pre_body = pop_stmt_list (pre_body);
37471 else
37472 pre_body = this_pre_body;
37475 if (decl)
37476 real_decl = decl;
37477 if (cclauses != NULL
37478 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
37479 && real_decl != NULL_TREE
37480 && code != OMP_LOOP)
37482 tree *c;
37483 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
37484 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
37485 && OMP_CLAUSE_DECL (*c) == real_decl)
37487 error_at (loc, "iteration variable %qD"
37488 " should not be firstprivate", real_decl);
37489 *c = OMP_CLAUSE_CHAIN (*c);
37491 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
37492 && OMP_CLAUSE_DECL (*c) == real_decl)
37494 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
37495 tree l = *c;
37496 *c = OMP_CLAUSE_CHAIN (*c);
37497 if (code == OMP_SIMD)
37499 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37500 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
37502 else
37504 OMP_CLAUSE_CHAIN (l) = clauses;
37505 clauses = l;
37507 add_private_clause = NULL_TREE;
37509 else
37511 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
37512 && OMP_CLAUSE_DECL (*c) == real_decl)
37513 add_private_clause = NULL_TREE;
37514 c = &OMP_CLAUSE_CHAIN (*c);
37518 if (add_private_clause)
37520 tree c;
37521 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
37523 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
37524 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
37525 && OMP_CLAUSE_DECL (c) == decl)
37526 break;
37527 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
37528 && OMP_CLAUSE_DECL (c) == decl)
37529 error_at (loc, "iteration variable %qD "
37530 "should not be firstprivate",
37531 decl);
37532 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
37533 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
37534 && OMP_CLAUSE_DECL (c) == decl)
37535 error_at (loc, "iteration variable %qD should not be reduction",
37536 decl);
37538 if (c == NULL)
37540 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
37541 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
37542 else if (code != OMP_SIMD)
37543 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
37544 else
37545 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37546 OMP_CLAUSE_DECL (c) = add_private_clause;
37547 c = finish_omp_clauses (c, C_ORT_OMP);
37548 if (c)
37550 OMP_CLAUSE_CHAIN (c) = clauses;
37551 clauses = c;
37552 /* For linear, signal that we need to fill up
37553 the so far unknown linear step. */
37554 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
37555 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
37560 cond = NULL;
37561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37562 cond = cp_parser_omp_for_cond (parser, decl, code);
37563 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37565 incr = NULL;
37566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
37568 /* If decl is an iterator, preserve the operator on decl
37569 until finish_omp_for. */
37570 if (real_decl
37571 && ((processing_template_decl
37572 && (TREE_TYPE (real_decl) == NULL_TREE
37573 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37574 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37575 incr = cp_parser_omp_for_incr (parser, real_decl);
37576 else
37577 incr = cp_parser_expression (parser);
37578 if (!EXPR_HAS_LOCATION (incr))
37579 protected_set_expr_location (incr, input_location);
37582 parse_close_paren:
37583 if (!parens.require_close (parser))
37584 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37585 /*or_comma=*/false,
37586 /*consume_paren=*/true);
37588 TREE_VEC_ELT (declv, i) = decl;
37589 TREE_VEC_ELT (initv, i) = init;
37590 TREE_VEC_ELT (condv, i) = cond;
37591 TREE_VEC_ELT (incrv, i) = incr;
37592 if (orig_init)
37594 orig_inits.safe_grow_cleared (i + 1);
37595 orig_inits[i] = orig_init;
37597 if (orig_decl)
37599 if (!orig_declv)
37600 orig_declv = copy_node (declv);
37601 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37603 else if (orig_declv)
37604 TREE_VEC_ELT (orig_declv, i) = decl;
37606 if (i == count - 1)
37607 break;
37609 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37610 in between the collapsed for loops to be still considered perfectly
37611 nested. Hopefully the final version clarifies this.
37612 For now handle (multiple) {'s and empty statements. */
37613 cp_parser_parse_tentatively (parser);
37614 for (;;)
37616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37617 break;
37618 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37620 cp_lexer_consume_token (parser->lexer);
37621 bracecount++;
37623 else if (bracecount
37624 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37625 cp_lexer_consume_token (parser->lexer);
37626 else
37628 loc = cp_lexer_peek_token (parser->lexer)->location;
37629 error_at (loc, "not enough for loops to collapse");
37630 collapse_err = true;
37631 cp_parser_abort_tentative_parse (parser);
37632 declv = NULL_TREE;
37633 break;
37637 if (declv)
37639 cp_parser_parse_definitely (parser);
37640 nbraces += bracecount;
37644 if (nbraces)
37645 if_p = NULL;
37647 /* Note that we saved the original contents of this flag when we entered
37648 the structured block, and so we don't need to re-save it here. */
37649 parser->in_statement = IN_OMP_FOR;
37651 /* Note that the grammar doesn't call for a structured block here,
37652 though the loop as a whole is a structured block. */
37653 if (orig_declv)
37655 body = begin_omp_structured_block ();
37656 for (i = 0; i < count; i++)
37657 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37658 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37659 TREE_VEC_ELT (declv, i));
37661 else
37662 body = push_stmt_list ();
37663 if (inscan)
37664 cp_parser_omp_scan_loop_body (parser);
37665 else
37666 cp_parser_statement (parser, NULL_TREE, false, if_p);
37667 if (orig_declv)
37668 body = finish_omp_structured_block (body);
37669 else
37670 body = pop_stmt_list (body);
37672 if (declv == NULL_TREE)
37673 ret = NULL_TREE;
37674 else
37675 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37676 incrv, body, pre_body, &orig_inits, clauses);
37678 while (nbraces)
37680 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37682 cp_lexer_consume_token (parser->lexer);
37683 nbraces--;
37685 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37686 cp_lexer_consume_token (parser->lexer);
37687 else
37689 if (!collapse_err)
37691 error_at (cp_lexer_peek_token (parser->lexer)->location,
37692 "collapsed loops not perfectly nested");
37694 collapse_err = true;
37695 cp_parser_statement_seq_opt (parser, NULL);
37696 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37697 break;
37701 while (!for_block->is_empty ())
37703 tree t = for_block->pop ();
37704 if (TREE_CODE (t) == STATEMENT_LIST)
37705 add_stmt (pop_stmt_list (t));
37706 else
37707 add_stmt (t);
37710 return ret;
37713 /* Helper function for OpenMP parsing, split clauses and call
37714 finish_omp_clauses on each of the set of clauses afterwards. */
37716 static void
37717 cp_omp_split_clauses (location_t loc, enum tree_code code,
37718 omp_clause_mask mask, tree clauses, tree *cclauses)
37720 int i;
37721 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37722 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37723 if (cclauses[i])
37724 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37727 /* OpenMP 5.0:
37728 #pragma omp loop loop-clause[optseq] new-line
37729 for-loop */
37731 #define OMP_LOOP_CLAUSE_MASK \
37732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
37737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
37739 static tree
37740 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
37741 char *p_name, omp_clause_mask mask, tree *cclauses,
37742 bool *if_p)
37744 tree clauses, sb, ret;
37745 unsigned int save;
37746 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37748 strcat (p_name, " loop");
37749 mask |= OMP_LOOP_CLAUSE_MASK;
37751 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37752 cclauses == NULL);
37753 if (cclauses)
37755 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
37756 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
37759 keep_next_level (true);
37760 sb = begin_omp_structured_block ();
37761 save = cp_parser_begin_omp_structured_block (parser);
37763 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
37765 cp_parser_end_omp_structured_block (parser, save);
37766 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37768 return ret;
37771 /* OpenMP 4.0:
37772 #pragma omp simd simd-clause[optseq] new-line
37773 for-loop */
37775 #define OMP_SIMD_CLAUSE_MASK \
37776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
37786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
37788 static tree
37789 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37790 char *p_name, omp_clause_mask mask, tree *cclauses,
37791 bool *if_p)
37793 tree clauses, sb, ret;
37794 unsigned int save;
37795 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37797 strcat (p_name, " simd");
37798 mask |= OMP_SIMD_CLAUSE_MASK;
37800 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37801 cclauses == NULL);
37802 if (cclauses)
37804 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37805 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37806 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37807 OMP_CLAUSE_ORDERED);
37808 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37810 error_at (OMP_CLAUSE_LOCATION (c),
37811 "%<ordered%> clause with parameter may not be specified "
37812 "on %qs construct", p_name);
37813 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37817 keep_next_level (true);
37818 sb = begin_omp_structured_block ();
37819 save = cp_parser_begin_omp_structured_block (parser);
37821 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37823 cp_parser_end_omp_structured_block (parser, save);
37824 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37826 return ret;
37829 /* OpenMP 2.5:
37830 #pragma omp for for-clause[optseq] new-line
37831 for-loop
37833 OpenMP 4.0:
37834 #pragma omp for simd for-simd-clause[optseq] new-line
37835 for-loop */
37837 #define OMP_FOR_CLAUSE_MASK \
37838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
37849 static tree
37850 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37851 char *p_name, omp_clause_mask mask, tree *cclauses,
37852 bool *if_p)
37854 tree clauses, sb, ret;
37855 unsigned int save;
37856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37858 strcat (p_name, " for");
37859 mask |= OMP_FOR_CLAUSE_MASK;
37860 /* parallel for{, simd} disallows nowait clause, but for
37861 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37862 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37863 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37864 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37865 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37866 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37870 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37871 const char *p = IDENTIFIER_POINTER (id);
37873 if (strcmp (p, "simd") == 0)
37875 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37876 if (cclauses == NULL)
37877 cclauses = cclauses_buf;
37879 cp_lexer_consume_token (parser->lexer);
37880 if (!flag_openmp) /* flag_openmp_simd */
37881 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37882 cclauses, if_p);
37883 sb = begin_omp_structured_block ();
37884 save = cp_parser_begin_omp_structured_block (parser);
37885 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37886 cclauses, if_p);
37887 cp_parser_end_omp_structured_block (parser, save);
37888 tree body = finish_omp_structured_block (sb);
37889 if (ret == NULL)
37890 return ret;
37891 ret = make_node (OMP_FOR);
37892 TREE_TYPE (ret) = void_type_node;
37893 OMP_FOR_BODY (ret) = body;
37894 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37895 SET_EXPR_LOCATION (ret, loc);
37896 add_stmt (ret);
37897 return ret;
37900 if (!flag_openmp) /* flag_openmp_simd */
37902 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37903 return NULL_TREE;
37906 /* Composite distribute parallel for disallows linear clause. */
37907 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37908 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37910 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37911 cclauses == NULL);
37912 if (cclauses)
37914 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37915 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37918 keep_next_level (true);
37919 sb = begin_omp_structured_block ();
37920 save = cp_parser_begin_omp_structured_block (parser);
37922 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37924 cp_parser_end_omp_structured_block (parser, save);
37925 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37927 return ret;
37930 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37931 omp_clause_mask, tree *, bool *);
37933 /* OpenMP 2.5:
37934 # pragma omp master new-line
37935 structured-block */
37937 static tree
37938 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37939 char *p_name, omp_clause_mask mask, tree *cclauses,
37940 bool *if_p)
37942 tree clauses, sb, ret;
37943 unsigned int save;
37944 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37946 strcat (p_name, " master");
37948 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37950 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37951 const char *p = IDENTIFIER_POINTER (id);
37953 if (strcmp (p, "taskloop") == 0)
37955 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37956 if (cclauses == NULL)
37957 cclauses = cclauses_buf;
37959 cp_lexer_consume_token (parser->lexer);
37960 if (!flag_openmp) /* flag_openmp_simd */
37961 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37962 cclauses, if_p);
37963 sb = begin_omp_structured_block ();
37964 save = cp_parser_begin_omp_structured_block (parser);
37965 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37966 cclauses, if_p);
37967 cp_parser_end_omp_structured_block (parser, save);
37968 tree body = finish_omp_structured_block (sb);
37969 if (ret == NULL)
37970 return ret;
37971 return c_finish_omp_master (loc, body);
37974 if (!flag_openmp) /* flag_openmp_simd */
37976 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37977 return NULL_TREE;
37980 if (cclauses)
37982 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37983 false);
37984 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37986 else
37987 cp_parser_require_pragma_eol (parser, pragma_tok);
37989 return c_finish_omp_master (loc,
37990 cp_parser_omp_structured_block (parser, if_p));
37993 /* OpenMP 2.5:
37994 # pragma omp ordered new-line
37995 structured-block
37997 OpenMP 4.5:
37998 # pragma omp ordered ordered-clauses new-line
37999 structured-block */
38001 #define OMP_ORDERED_CLAUSE_MASK \
38002 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
38003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
38005 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
38006 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
38008 static bool
38009 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
38010 enum pragma_context context, bool *if_p)
38012 location_t loc = pragma_tok->location;
38014 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38016 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38017 const char *p = IDENTIFIER_POINTER (id);
38019 if (strcmp (p, "depend") == 0)
38021 if (!flag_openmp) /* flag_openmp_simd */
38023 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38024 return false;
38026 if (context == pragma_stmt)
38028 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
38029 "%<depend%> clause may only be used in compound "
38030 "statements");
38031 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38032 return false;
38034 tree clauses
38035 = cp_parser_omp_all_clauses (parser,
38036 OMP_ORDERED_DEPEND_CLAUSE_MASK,
38037 "#pragma omp ordered", pragma_tok);
38038 c_finish_omp_ordered (loc, clauses, NULL_TREE);
38039 return false;
38043 tree clauses
38044 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
38045 "#pragma omp ordered", pragma_tok);
38047 if (!flag_openmp /* flag_openmp_simd */
38048 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
38049 return false;
38051 c_finish_omp_ordered (loc, clauses,
38052 cp_parser_omp_structured_block (parser, if_p));
38053 return true;
38056 /* OpenMP 2.5:
38058 section-scope:
38059 { section-sequence }
38061 section-sequence:
38062 section-directive[opt] structured-block
38063 section-sequence section-directive structured-block */
38065 static tree
38066 cp_parser_omp_sections_scope (cp_parser *parser)
38068 tree stmt, substmt;
38069 bool error_suppress = false;
38070 cp_token *tok;
38072 matching_braces braces;
38073 if (!braces.require_open (parser))
38074 return NULL_TREE;
38076 stmt = push_stmt_list ();
38078 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
38079 != PRAGMA_OMP_SECTION)
38081 substmt = cp_parser_omp_structured_block (parser, NULL);
38082 substmt = build1 (OMP_SECTION, void_type_node, substmt);
38083 add_stmt (substmt);
38086 while (1)
38088 tok = cp_lexer_peek_token (parser->lexer);
38089 if (tok->type == CPP_CLOSE_BRACE)
38090 break;
38091 if (tok->type == CPP_EOF)
38092 break;
38094 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
38096 cp_lexer_consume_token (parser->lexer);
38097 cp_parser_require_pragma_eol (parser, tok);
38098 error_suppress = false;
38100 else if (!error_suppress)
38102 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
38103 error_suppress = true;
38106 substmt = cp_parser_omp_structured_block (parser, NULL);
38107 substmt = build1 (OMP_SECTION, void_type_node, substmt);
38108 add_stmt (substmt);
38110 braces.require_close (parser);
38112 substmt = pop_stmt_list (stmt);
38114 stmt = make_node (OMP_SECTIONS);
38115 TREE_TYPE (stmt) = void_type_node;
38116 OMP_SECTIONS_BODY (stmt) = substmt;
38118 add_stmt (stmt);
38119 return stmt;
38122 /* OpenMP 2.5:
38123 # pragma omp sections sections-clause[optseq] newline
38124 sections-scope */
38126 #define OMP_SECTIONS_CLAUSE_MASK \
38127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38133 static tree
38134 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
38135 char *p_name, omp_clause_mask mask, tree *cclauses)
38137 tree clauses, ret;
38138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38140 strcat (p_name, " sections");
38141 mask |= OMP_SECTIONS_CLAUSE_MASK;
38142 if (cclauses)
38143 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
38145 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38146 cclauses == NULL);
38147 if (cclauses)
38149 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
38150 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
38153 ret = cp_parser_omp_sections_scope (parser);
38154 if (ret)
38155 OMP_SECTIONS_CLAUSES (ret) = clauses;
38157 return ret;
38160 /* OpenMP 2.5:
38161 # pragma omp parallel parallel-clause[optseq] new-line
38162 structured-block
38163 # pragma omp parallel for parallel-for-clause[optseq] new-line
38164 structured-block
38165 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
38166 structured-block
38168 OpenMP 4.0:
38169 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
38170 structured-block */
38172 #define OMP_PARALLEL_CLAUSE_MASK \
38173 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
38179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
38181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
38183 static tree
38184 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
38185 char *p_name, omp_clause_mask mask, tree *cclauses,
38186 bool *if_p)
38188 tree stmt, clauses, block;
38189 unsigned int save;
38190 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38192 strcat (p_name, " parallel");
38193 mask |= OMP_PARALLEL_CLAUSE_MASK;
38194 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
38195 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
38196 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
38197 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
38199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38201 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38202 if (cclauses == NULL)
38203 cclauses = cclauses_buf;
38205 cp_lexer_consume_token (parser->lexer);
38206 if (!flag_openmp) /* flag_openmp_simd */
38207 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38208 if_p);
38209 block = begin_omp_parallel ();
38210 save = cp_parser_begin_omp_structured_block (parser);
38211 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38212 if_p);
38213 cp_parser_end_omp_structured_block (parser, save);
38214 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38215 block);
38216 if (ret == NULL_TREE)
38217 return ret;
38218 OMP_PARALLEL_COMBINED (stmt) = 1;
38219 return stmt;
38221 /* When combined with distribute, parallel has to be followed by for.
38222 #pragma omp target parallel is allowed though. */
38223 else if (cclauses
38224 && (mask & (OMP_CLAUSE_MASK_1
38225 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38227 error_at (loc, "expected %<for%> after %qs", p_name);
38228 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38229 return NULL_TREE;
38231 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38233 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38234 const char *p = IDENTIFIER_POINTER (id);
38235 if (cclauses == NULL && strcmp (p, "master") == 0)
38237 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38238 cclauses = cclauses_buf;
38240 cp_lexer_consume_token (parser->lexer);
38241 block = begin_omp_parallel ();
38242 save = cp_parser_begin_omp_structured_block (parser);
38243 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
38244 cclauses, if_p);
38245 cp_parser_end_omp_structured_block (parser, save);
38246 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38247 block);
38248 OMP_PARALLEL_COMBINED (stmt) = 1;
38249 if (ret == NULL_TREE)
38250 return ret;
38251 return stmt;
38253 else if (strcmp (p, "loop") == 0)
38255 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38256 if (cclauses == NULL)
38257 cclauses = cclauses_buf;
38259 cp_lexer_consume_token (parser->lexer);
38260 if (!flag_openmp) /* flag_openmp_simd */
38261 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38262 cclauses, if_p);
38263 block = begin_omp_parallel ();
38264 save = cp_parser_begin_omp_structured_block (parser);
38265 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38266 cclauses, if_p);
38267 cp_parser_end_omp_structured_block (parser, save);
38268 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38269 block);
38270 if (ret == NULL_TREE)
38271 return ret;
38272 OMP_PARALLEL_COMBINED (stmt) = 1;
38273 return stmt;
38275 else if (!flag_openmp) /* flag_openmp_simd */
38277 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38278 return NULL_TREE;
38280 else if (cclauses == NULL && strcmp (p, "sections") == 0)
38282 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38283 cclauses = cclauses_buf;
38285 cp_lexer_consume_token (parser->lexer);
38286 block = begin_omp_parallel ();
38287 save = cp_parser_begin_omp_structured_block (parser);
38288 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
38289 cp_parser_end_omp_structured_block (parser, save);
38290 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38291 block);
38292 OMP_PARALLEL_COMBINED (stmt) = 1;
38293 return stmt;
38296 else if (!flag_openmp) /* flag_openmp_simd */
38298 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38299 return NULL_TREE;
38302 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38303 cclauses == NULL);
38304 if (cclauses)
38306 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
38307 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
38310 block = begin_omp_parallel ();
38311 save = cp_parser_begin_omp_structured_block (parser);
38312 cp_parser_statement (parser, NULL_TREE, false, if_p);
38313 cp_parser_end_omp_structured_block (parser, save);
38314 stmt = finish_omp_parallel (clauses, block);
38315 return stmt;
38318 /* OpenMP 2.5:
38319 # pragma omp single single-clause[optseq] new-line
38320 structured-block */
38322 #define OMP_SINGLE_CLAUSE_MASK \
38323 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
38326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38328 static tree
38329 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38331 tree stmt = make_node (OMP_SINGLE);
38332 TREE_TYPE (stmt) = void_type_node;
38333 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38335 OMP_SINGLE_CLAUSES (stmt)
38336 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
38337 "#pragma omp single", pragma_tok);
38338 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38340 return add_stmt (stmt);
38343 /* OpenMP 3.0:
38344 # pragma omp task task-clause[optseq] new-line
38345 structured-block */
38347 #define OMP_TASK_CLAUSE_MASK \
38348 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
38350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
38355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
38356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
38358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
38360 static tree
38361 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38363 tree clauses, block;
38364 unsigned int save;
38366 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
38367 "#pragma omp task", pragma_tok);
38368 block = begin_omp_task ();
38369 save = cp_parser_begin_omp_structured_block (parser);
38370 cp_parser_statement (parser, NULL_TREE, false, if_p);
38371 cp_parser_end_omp_structured_block (parser, save);
38372 return finish_omp_task (clauses, block);
38375 /* OpenMP 3.0:
38376 # pragma omp taskwait new-line
38378 OpenMP 5.0:
38379 # pragma omp taskwait taskwait-clause[opt] new-line */
38381 #define OMP_TASKWAIT_CLAUSE_MASK \
38382 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
38384 static void
38385 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
38387 tree clauses
38388 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
38389 "#pragma omp taskwait", pragma_tok);
38391 if (clauses)
38393 tree stmt = make_node (OMP_TASK);
38394 TREE_TYPE (stmt) = void_node;
38395 OMP_TASK_CLAUSES (stmt) = clauses;
38396 OMP_TASK_BODY (stmt) = NULL_TREE;
38397 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38398 add_stmt (stmt);
38400 else
38401 finish_omp_taskwait ();
38404 /* OpenMP 3.1:
38405 # pragma omp taskyield new-line */
38407 static void
38408 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
38410 cp_parser_require_pragma_eol (parser, pragma_tok);
38411 finish_omp_taskyield ();
38414 /* OpenMP 4.0:
38415 # pragma omp taskgroup new-line
38416 structured-block
38418 OpenMP 5.0:
38419 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
38421 #define OMP_TASKGROUP_CLAUSE_MASK \
38422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
38424 static tree
38425 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38427 tree clauses
38428 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
38429 "#pragma omp taskgroup", pragma_tok);
38430 return c_finish_omp_taskgroup (input_location,
38431 cp_parser_omp_structured_block (parser,
38432 if_p),
38433 clauses);
38437 /* OpenMP 2.5:
38438 # pragma omp threadprivate (variable-list) */
38440 static void
38441 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
38443 tree vars;
38445 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38446 cp_parser_require_pragma_eol (parser, pragma_tok);
38448 finish_omp_threadprivate (vars);
38451 /* OpenMP 4.0:
38452 # pragma omp cancel cancel-clause[optseq] new-line */
38454 #define OMP_CANCEL_CLAUSE_MASK \
38455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
38456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
38457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
38458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
38459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
38461 static void
38462 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
38464 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
38465 "#pragma omp cancel", pragma_tok);
38466 finish_omp_cancel (clauses);
38469 /* OpenMP 4.0:
38470 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
38472 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
38473 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
38474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
38475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
38476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
38478 static void
38479 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
38480 enum pragma_context context)
38482 tree clauses;
38483 bool point_seen = false;
38485 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38487 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38488 const char *p = IDENTIFIER_POINTER (id);
38490 if (strcmp (p, "point") == 0)
38492 cp_lexer_consume_token (parser->lexer);
38493 point_seen = true;
38496 if (!point_seen)
38498 cp_parser_error (parser, "expected %<point%>");
38499 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38500 return;
38503 if (context != pragma_compound)
38505 if (context == pragma_stmt)
38506 error_at (pragma_tok->location,
38507 "%<#pragma %s%> may only be used in compound statements",
38508 "omp cancellation point");
38509 else
38510 cp_parser_error (parser, "expected declaration specifiers");
38511 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38512 return;
38515 clauses = cp_parser_omp_all_clauses (parser,
38516 OMP_CANCELLATION_POINT_CLAUSE_MASK,
38517 "#pragma omp cancellation point",
38518 pragma_tok);
38519 finish_omp_cancellation_point (clauses);
38522 /* OpenMP 4.0:
38523 #pragma omp distribute distribute-clause[optseq] new-line
38524 for-loop */
38526 #define OMP_DISTRIBUTE_CLAUSE_MASK \
38527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
38531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
38533 static tree
38534 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
38535 char *p_name, omp_clause_mask mask, tree *cclauses,
38536 bool *if_p)
38538 tree clauses, sb, ret;
38539 unsigned int save;
38540 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38542 strcat (p_name, " distribute");
38543 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
38545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38548 const char *p = IDENTIFIER_POINTER (id);
38549 bool simd = false;
38550 bool parallel = false;
38552 if (strcmp (p, "simd") == 0)
38553 simd = true;
38554 else
38555 parallel = strcmp (p, "parallel") == 0;
38556 if (parallel || simd)
38558 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38559 if (cclauses == NULL)
38560 cclauses = cclauses_buf;
38561 cp_lexer_consume_token (parser->lexer);
38562 if (!flag_openmp) /* flag_openmp_simd */
38564 if (simd)
38565 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38566 cclauses, if_p);
38567 else
38568 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38569 cclauses, if_p);
38571 sb = begin_omp_structured_block ();
38572 save = cp_parser_begin_omp_structured_block (parser);
38573 if (simd)
38574 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38575 cclauses, if_p);
38576 else
38577 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38578 cclauses, if_p);
38579 cp_parser_end_omp_structured_block (parser, save);
38580 tree body = finish_omp_structured_block (sb);
38581 if (ret == NULL)
38582 return ret;
38583 ret = make_node (OMP_DISTRIBUTE);
38584 TREE_TYPE (ret) = void_type_node;
38585 OMP_FOR_BODY (ret) = body;
38586 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38587 SET_EXPR_LOCATION (ret, loc);
38588 add_stmt (ret);
38589 return ret;
38592 if (!flag_openmp) /* flag_openmp_simd */
38594 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38595 return NULL_TREE;
38598 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38599 cclauses == NULL);
38600 if (cclauses)
38602 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
38603 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38606 keep_next_level (true);
38607 sb = begin_omp_structured_block ();
38608 save = cp_parser_begin_omp_structured_block (parser);
38610 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
38612 cp_parser_end_omp_structured_block (parser, save);
38613 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38615 return ret;
38618 /* OpenMP 4.0:
38619 # pragma omp teams teams-clause[optseq] new-line
38620 structured-block */
38622 #define OMP_TEAMS_CLAUSE_MASK \
38623 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38631 static tree
38632 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
38633 char *p_name, omp_clause_mask mask, tree *cclauses,
38634 bool *if_p)
38636 tree clauses, sb, ret;
38637 unsigned int save;
38638 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38640 strcat (p_name, " teams");
38641 mask |= OMP_TEAMS_CLAUSE_MASK;
38643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38645 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38646 const char *p = IDENTIFIER_POINTER (id);
38647 if (strcmp (p, "distribute") == 0)
38649 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38650 if (cclauses == NULL)
38651 cclauses = cclauses_buf;
38653 cp_lexer_consume_token (parser->lexer);
38654 if (!flag_openmp) /* flag_openmp_simd */
38655 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38656 cclauses, if_p);
38657 keep_next_level (true);
38658 sb = begin_omp_structured_block ();
38659 save = cp_parser_begin_omp_structured_block (parser);
38660 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38661 cclauses, if_p);
38662 cp_parser_end_omp_structured_block (parser, save);
38663 tree body = finish_omp_structured_block (sb);
38664 if (ret == NULL)
38665 return ret;
38666 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38667 ret = make_node (OMP_TEAMS);
38668 TREE_TYPE (ret) = void_type_node;
38669 OMP_TEAMS_CLAUSES (ret) = clauses;
38670 OMP_TEAMS_BODY (ret) = body;
38671 OMP_TEAMS_COMBINED (ret) = 1;
38672 SET_EXPR_LOCATION (ret, loc);
38673 return add_stmt (ret);
38675 else if (strcmp (p, "loop") == 0)
38677 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38678 if (cclauses == NULL)
38679 cclauses = cclauses_buf;
38681 cp_lexer_consume_token (parser->lexer);
38682 if (!flag_openmp) /* flag_openmp_simd */
38683 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38684 cclauses, if_p);
38685 keep_next_level (true);
38686 sb = begin_omp_structured_block ();
38687 save = cp_parser_begin_omp_structured_block (parser);
38688 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
38689 cclauses, if_p);
38690 cp_parser_end_omp_structured_block (parser, save);
38691 tree body = finish_omp_structured_block (sb);
38692 if (ret == NULL)
38693 return ret;
38694 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38695 ret = make_node (OMP_TEAMS);
38696 TREE_TYPE (ret) = void_type_node;
38697 OMP_TEAMS_CLAUSES (ret) = clauses;
38698 OMP_TEAMS_BODY (ret) = body;
38699 OMP_TEAMS_COMBINED (ret) = 1;
38700 SET_EXPR_LOCATION (ret, loc);
38701 return add_stmt (ret);
38704 if (!flag_openmp) /* flag_openmp_simd */
38706 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38707 return NULL_TREE;
38710 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38711 cclauses == NULL);
38712 if (cclauses)
38714 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38715 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38718 tree stmt = make_node (OMP_TEAMS);
38719 TREE_TYPE (stmt) = void_type_node;
38720 OMP_TEAMS_CLAUSES (stmt) = clauses;
38721 keep_next_level (true);
38722 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38723 SET_EXPR_LOCATION (stmt, loc);
38725 return add_stmt (stmt);
38728 /* OpenMP 4.0:
38729 # pragma omp target data target-data-clause[optseq] new-line
38730 structured-block */
38732 #define OMP_TARGET_DATA_CLAUSE_MASK \
38733 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
38737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
38739 static tree
38740 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38742 tree clauses
38743 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38744 "#pragma omp target data", pragma_tok);
38745 int map_seen = 0;
38746 for (tree *pc = &clauses; *pc;)
38748 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38749 switch (OMP_CLAUSE_MAP_KIND (*pc))
38751 case GOMP_MAP_TO:
38752 case GOMP_MAP_ALWAYS_TO:
38753 case GOMP_MAP_FROM:
38754 case GOMP_MAP_ALWAYS_FROM:
38755 case GOMP_MAP_TOFROM:
38756 case GOMP_MAP_ALWAYS_TOFROM:
38757 case GOMP_MAP_ALLOC:
38758 map_seen = 3;
38759 break;
38760 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38761 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38762 case GOMP_MAP_ALWAYS_POINTER:
38763 break;
38764 default:
38765 map_seen |= 1;
38766 error_at (OMP_CLAUSE_LOCATION (*pc),
38767 "%<#pragma omp target data%> with map-type other "
38768 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38769 "on %<map%> clause");
38770 *pc = OMP_CLAUSE_CHAIN (*pc);
38771 continue;
38773 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
38774 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
38775 map_seen = 3;
38776 pc = &OMP_CLAUSE_CHAIN (*pc);
38779 if (map_seen != 3)
38781 if (map_seen == 0)
38782 error_at (pragma_tok->location,
38783 "%<#pragma omp target data%> must contain at least "
38784 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
38785 "clause");
38786 return NULL_TREE;
38789 tree stmt = make_node (OMP_TARGET_DATA);
38790 TREE_TYPE (stmt) = void_type_node;
38791 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38793 keep_next_level (true);
38794 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38796 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38797 return add_stmt (stmt);
38800 /* OpenMP 4.5:
38801 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38802 structured-block */
38804 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38805 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38811 static tree
38812 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38813 enum pragma_context context)
38815 bool data_seen = false;
38816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38818 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38819 const char *p = IDENTIFIER_POINTER (id);
38821 if (strcmp (p, "data") == 0)
38823 cp_lexer_consume_token (parser->lexer);
38824 data_seen = true;
38827 if (!data_seen)
38829 cp_parser_error (parser, "expected %<data%>");
38830 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38831 return NULL_TREE;
38834 if (context == pragma_stmt)
38836 error_at (pragma_tok->location,
38837 "%<#pragma %s%> may only be used in compound statements",
38838 "omp target enter data");
38839 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38840 return NULL_TREE;
38843 tree clauses
38844 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38845 "#pragma omp target enter data", pragma_tok);
38846 int map_seen = 0;
38847 for (tree *pc = &clauses; *pc;)
38849 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38850 switch (OMP_CLAUSE_MAP_KIND (*pc))
38852 case GOMP_MAP_TO:
38853 case GOMP_MAP_ALWAYS_TO:
38854 case GOMP_MAP_ALLOC:
38855 map_seen = 3;
38856 break;
38857 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38858 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38859 case GOMP_MAP_ALWAYS_POINTER:
38860 break;
38861 default:
38862 map_seen |= 1;
38863 error_at (OMP_CLAUSE_LOCATION (*pc),
38864 "%<#pragma omp target enter data%> with map-type other "
38865 "than %<to%> or %<alloc%> on %<map%> clause");
38866 *pc = OMP_CLAUSE_CHAIN (*pc);
38867 continue;
38869 pc = &OMP_CLAUSE_CHAIN (*pc);
38872 if (map_seen != 3)
38874 if (map_seen == 0)
38875 error_at (pragma_tok->location,
38876 "%<#pragma omp target enter data%> must contain at least "
38877 "one %<map%> clause");
38878 return NULL_TREE;
38881 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38882 TREE_TYPE (stmt) = void_type_node;
38883 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38884 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38885 return add_stmt (stmt);
38888 /* OpenMP 4.5:
38889 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38890 structured-block */
38892 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38893 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38899 static tree
38900 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38901 enum pragma_context context)
38903 bool data_seen = false;
38904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38906 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38907 const char *p = IDENTIFIER_POINTER (id);
38909 if (strcmp (p, "data") == 0)
38911 cp_lexer_consume_token (parser->lexer);
38912 data_seen = true;
38915 if (!data_seen)
38917 cp_parser_error (parser, "expected %<data%>");
38918 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38919 return NULL_TREE;
38922 if (context == pragma_stmt)
38924 error_at (pragma_tok->location,
38925 "%<#pragma %s%> may only be used in compound statements",
38926 "omp target exit data");
38927 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38928 return NULL_TREE;
38931 tree clauses
38932 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38933 "#pragma omp target exit data", pragma_tok);
38934 int map_seen = 0;
38935 for (tree *pc = &clauses; *pc;)
38937 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38938 switch (OMP_CLAUSE_MAP_KIND (*pc))
38940 case GOMP_MAP_FROM:
38941 case GOMP_MAP_ALWAYS_FROM:
38942 case GOMP_MAP_RELEASE:
38943 case GOMP_MAP_DELETE:
38944 map_seen = 3;
38945 break;
38946 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38947 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38948 case GOMP_MAP_ALWAYS_POINTER:
38949 break;
38950 default:
38951 map_seen |= 1;
38952 error_at (OMP_CLAUSE_LOCATION (*pc),
38953 "%<#pragma omp target exit data%> with map-type other "
38954 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38955 " clause");
38956 *pc = OMP_CLAUSE_CHAIN (*pc);
38957 continue;
38959 pc = &OMP_CLAUSE_CHAIN (*pc);
38962 if (map_seen != 3)
38964 if (map_seen == 0)
38965 error_at (pragma_tok->location,
38966 "%<#pragma omp target exit data%> must contain at least "
38967 "one %<map%> clause");
38968 return NULL_TREE;
38971 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38972 TREE_TYPE (stmt) = void_type_node;
38973 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38974 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38975 return add_stmt (stmt);
38978 /* OpenMP 4.0:
38979 # pragma omp target update target-update-clause[optseq] new-line */
38981 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38989 static bool
38990 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38991 enum pragma_context context)
38993 if (context == pragma_stmt)
38995 error_at (pragma_tok->location,
38996 "%<#pragma %s%> may only be used in compound statements",
38997 "omp target update");
38998 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38999 return false;
39002 tree clauses
39003 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
39004 "#pragma omp target update", pragma_tok);
39005 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
39006 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
39008 error_at (pragma_tok->location,
39009 "%<#pragma omp target update%> must contain at least one "
39010 "%<from%> or %<to%> clauses");
39011 return false;
39014 tree stmt = make_node (OMP_TARGET_UPDATE);
39015 TREE_TYPE (stmt) = void_type_node;
39016 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
39017 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39018 add_stmt (stmt);
39019 return false;
39022 /* OpenMP 4.0:
39023 # pragma omp target target-clause[optseq] new-line
39024 structured-block */
39026 #define OMP_TARGET_CLAUSE_MASK \
39027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
39035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
39037 static bool
39038 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
39039 enum pragma_context context, bool *if_p)
39041 tree *pc = NULL, stmt;
39043 if (flag_openmp)
39044 omp_requires_mask
39045 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
39047 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39049 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39050 const char *p = IDENTIFIER_POINTER (id);
39051 enum tree_code ccode = ERROR_MARK;
39053 if (strcmp (p, "teams") == 0)
39054 ccode = OMP_TEAMS;
39055 else if (strcmp (p, "parallel") == 0)
39056 ccode = OMP_PARALLEL;
39057 else if (strcmp (p, "simd") == 0)
39058 ccode = OMP_SIMD;
39059 if (ccode != ERROR_MARK)
39061 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
39062 char p_name[sizeof ("#pragma omp target teams distribute "
39063 "parallel for simd")];
39065 cp_lexer_consume_token (parser->lexer);
39066 strcpy (p_name, "#pragma omp target");
39067 if (!flag_openmp) /* flag_openmp_simd */
39069 tree stmt;
39070 switch (ccode)
39072 case OMP_TEAMS:
39073 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
39074 OMP_TARGET_CLAUSE_MASK,
39075 cclauses, if_p);
39076 break;
39077 case OMP_PARALLEL:
39078 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
39079 OMP_TARGET_CLAUSE_MASK,
39080 cclauses, if_p);
39081 break;
39082 case OMP_SIMD:
39083 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
39084 OMP_TARGET_CLAUSE_MASK,
39085 cclauses, if_p);
39086 break;
39087 default:
39088 gcc_unreachable ();
39090 return stmt != NULL_TREE;
39092 keep_next_level (true);
39093 tree sb = begin_omp_structured_block (), ret;
39094 unsigned save = cp_parser_begin_omp_structured_block (parser);
39095 switch (ccode)
39097 case OMP_TEAMS:
39098 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
39099 OMP_TARGET_CLAUSE_MASK, cclauses,
39100 if_p);
39101 break;
39102 case OMP_PARALLEL:
39103 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
39104 OMP_TARGET_CLAUSE_MASK, cclauses,
39105 if_p);
39106 break;
39107 case OMP_SIMD:
39108 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
39109 OMP_TARGET_CLAUSE_MASK, cclauses,
39110 if_p);
39111 break;
39112 default:
39113 gcc_unreachable ();
39115 cp_parser_end_omp_structured_block (parser, save);
39116 tree body = finish_omp_structured_block (sb);
39117 if (ret == NULL_TREE)
39118 return false;
39119 if (ccode == OMP_TEAMS && !processing_template_decl)
39121 /* For combined target teams, ensure the num_teams and
39122 thread_limit clause expressions are evaluated on the host,
39123 before entering the target construct. */
39124 tree c;
39125 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
39126 c; c = OMP_CLAUSE_CHAIN (c))
39127 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
39128 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
39129 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
39131 tree expr = OMP_CLAUSE_OPERAND (c, 0);
39132 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
39133 if (expr == error_mark_node)
39134 continue;
39135 tree tmp = TARGET_EXPR_SLOT (expr);
39136 add_stmt (expr);
39137 OMP_CLAUSE_OPERAND (c, 0) = expr;
39138 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
39139 OMP_CLAUSE_FIRSTPRIVATE);
39140 OMP_CLAUSE_DECL (tc) = tmp;
39141 OMP_CLAUSE_CHAIN (tc)
39142 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
39143 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
39146 tree stmt = make_node (OMP_TARGET);
39147 TREE_TYPE (stmt) = void_type_node;
39148 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
39149 OMP_TARGET_BODY (stmt) = body;
39150 OMP_TARGET_COMBINED (stmt) = 1;
39151 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39152 add_stmt (stmt);
39153 pc = &OMP_TARGET_CLAUSES (stmt);
39154 goto check_clauses;
39156 else if (!flag_openmp) /* flag_openmp_simd */
39158 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39159 return false;
39161 else if (strcmp (p, "data") == 0)
39163 cp_lexer_consume_token (parser->lexer);
39164 cp_parser_omp_target_data (parser, pragma_tok, if_p);
39165 return true;
39167 else if (strcmp (p, "enter") == 0)
39169 cp_lexer_consume_token (parser->lexer);
39170 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
39171 return false;
39173 else if (strcmp (p, "exit") == 0)
39175 cp_lexer_consume_token (parser->lexer);
39176 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
39177 return false;
39179 else if (strcmp (p, "update") == 0)
39181 cp_lexer_consume_token (parser->lexer);
39182 return cp_parser_omp_target_update (parser, pragma_tok, context);
39185 if (!flag_openmp) /* flag_openmp_simd */
39187 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39188 return false;
39191 stmt = make_node (OMP_TARGET);
39192 TREE_TYPE (stmt) = void_type_node;
39194 OMP_TARGET_CLAUSES (stmt)
39195 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
39196 "#pragma omp target", pragma_tok);
39197 pc = &OMP_TARGET_CLAUSES (stmt);
39198 keep_next_level (true);
39199 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
39201 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39202 add_stmt (stmt);
39204 check_clauses:
39205 while (*pc)
39207 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
39208 switch (OMP_CLAUSE_MAP_KIND (*pc))
39210 case GOMP_MAP_TO:
39211 case GOMP_MAP_ALWAYS_TO:
39212 case GOMP_MAP_FROM:
39213 case GOMP_MAP_ALWAYS_FROM:
39214 case GOMP_MAP_TOFROM:
39215 case GOMP_MAP_ALWAYS_TOFROM:
39216 case GOMP_MAP_ALLOC:
39217 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39218 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
39219 case GOMP_MAP_ALWAYS_POINTER:
39220 break;
39221 default:
39222 error_at (OMP_CLAUSE_LOCATION (*pc),
39223 "%<#pragma omp target%> with map-type other "
39224 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39225 "on %<map%> clause");
39226 *pc = OMP_CLAUSE_CHAIN (*pc);
39227 continue;
39229 pc = &OMP_CLAUSE_CHAIN (*pc);
39231 return true;
39234 /* OpenACC 2.0:
39235 # pragma acc cache (variable-list) new-line
39238 static tree
39239 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
39241 tree stmt, clauses;
39243 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
39244 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39246 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
39248 stmt = make_node (OACC_CACHE);
39249 TREE_TYPE (stmt) = void_type_node;
39250 OACC_CACHE_CLAUSES (stmt) = clauses;
39251 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39252 add_stmt (stmt);
39254 return stmt;
39257 /* OpenACC 2.0:
39258 # pragma acc data oacc-data-clause[optseq] new-line
39259 structured-block */
39261 #define OACC_DATA_CLAUSE_MASK \
39262 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
39270 static tree
39271 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39273 tree stmt, clauses, block;
39274 unsigned int save;
39276 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
39277 "#pragma acc data", pragma_tok);
39279 block = begin_omp_parallel ();
39280 save = cp_parser_begin_omp_structured_block (parser);
39281 cp_parser_statement (parser, NULL_TREE, false, if_p);
39282 cp_parser_end_omp_structured_block (parser, save);
39283 stmt = finish_oacc_data (clauses, block);
39284 return stmt;
39287 /* OpenACC 2.0:
39288 # pragma acc host_data <clauses> new-line
39289 structured-block */
39291 #define OACC_HOST_DATA_CLAUSE_MASK \
39292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
39294 static tree
39295 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39297 tree stmt, clauses, block;
39298 unsigned int save;
39300 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
39301 "#pragma acc host_data", pragma_tok);
39303 block = begin_omp_parallel ();
39304 save = cp_parser_begin_omp_structured_block (parser);
39305 cp_parser_statement (parser, NULL_TREE, false, if_p);
39306 cp_parser_end_omp_structured_block (parser, save);
39307 stmt = finish_oacc_host_data (clauses, block);
39308 return stmt;
39311 /* OpenACC 2.0:
39312 # pragma acc declare oacc-data-clause[optseq] new-line
39315 #define OACC_DECLARE_CLAUSE_MASK \
39316 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
39322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
39323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
39325 static tree
39326 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
39328 tree clauses, stmt;
39329 bool error = false;
39331 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
39332 "#pragma acc declare", pragma_tok, true);
39335 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39337 error_at (pragma_tok->location,
39338 "no valid clauses specified in %<#pragma acc declare%>");
39339 return NULL_TREE;
39342 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
39344 location_t loc = OMP_CLAUSE_LOCATION (t);
39345 tree decl = OMP_CLAUSE_DECL (t);
39346 if (!DECL_P (decl))
39348 error_at (loc, "array section in %<#pragma acc declare%>");
39349 error = true;
39350 continue;
39352 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
39353 switch (OMP_CLAUSE_MAP_KIND (t))
39355 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39356 case GOMP_MAP_ALLOC:
39357 case GOMP_MAP_TO:
39358 case GOMP_MAP_FORCE_DEVICEPTR:
39359 case GOMP_MAP_DEVICE_RESIDENT:
39360 break;
39362 case GOMP_MAP_LINK:
39363 if (!global_bindings_p ()
39364 && (TREE_STATIC (decl)
39365 || !DECL_EXTERNAL (decl)))
39367 error_at (loc,
39368 "%qD must be a global variable in "
39369 "%<#pragma acc declare link%>",
39370 decl);
39371 error = true;
39372 continue;
39374 break;
39376 default:
39377 if (global_bindings_p ())
39379 error_at (loc, "invalid OpenACC clause at file scope");
39380 error = true;
39381 continue;
39383 if (DECL_EXTERNAL (decl))
39385 error_at (loc,
39386 "invalid use of %<extern%> variable %qD "
39387 "in %<#pragma acc declare%>", decl);
39388 error = true;
39389 continue;
39391 else if (TREE_PUBLIC (decl))
39393 error_at (loc,
39394 "invalid use of %<global%> variable %qD "
39395 "in %<#pragma acc declare%>", decl);
39396 error = true;
39397 continue;
39399 break;
39402 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
39403 || lookup_attribute ("omp declare target link",
39404 DECL_ATTRIBUTES (decl)))
39406 error_at (loc, "variable %qD used more than once with "
39407 "%<#pragma acc declare%>", decl);
39408 error = true;
39409 continue;
39412 if (!error)
39414 tree id;
39416 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
39417 id = get_identifier ("omp declare target link");
39418 else
39419 id = get_identifier ("omp declare target");
39421 DECL_ATTRIBUTES (decl)
39422 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
39423 if (global_bindings_p ())
39425 symtab_node *node = symtab_node::get (decl);
39426 if (node != NULL)
39428 node->offloadable = 1;
39429 if (ENABLE_OFFLOADING)
39431 g->have_offload = true;
39432 if (is_a <varpool_node *> (node))
39433 vec_safe_push (offload_vars, decl);
39440 if (error || global_bindings_p ())
39441 return NULL_TREE;
39443 stmt = make_node (OACC_DECLARE);
39444 TREE_TYPE (stmt) = void_type_node;
39445 OACC_DECLARE_CLAUSES (stmt) = clauses;
39446 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39448 add_stmt (stmt);
39450 return NULL_TREE;
39453 /* OpenACC 2.0:
39454 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
39458 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
39460 LOC is the location of the #pragma token.
39463 #define OACC_ENTER_DATA_CLAUSE_MASK \
39464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39470 #define OACC_EXIT_DATA_CLAUSE_MASK \
39471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
39475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
39476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39478 static tree
39479 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
39480 bool enter)
39482 location_t loc = pragma_tok->location;
39483 tree stmt, clauses;
39484 const char *p = "";
39486 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39487 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39489 if (strcmp (p, "data") != 0)
39491 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
39492 enter ? "enter" : "exit");
39493 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39494 return NULL_TREE;
39497 cp_lexer_consume_token (parser->lexer);
39499 if (enter)
39500 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
39501 "#pragma acc enter data", pragma_tok);
39502 else
39503 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
39504 "#pragma acc exit data", pragma_tok);
39506 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39508 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
39509 enter ? "enter" : "exit");
39510 return NULL_TREE;
39513 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
39514 TREE_TYPE (stmt) = void_type_node;
39515 OMP_STANDALONE_CLAUSES (stmt) = clauses;
39516 SET_EXPR_LOCATION (stmt, loc);
39517 add_stmt (stmt);
39518 return stmt;
39521 /* OpenACC 2.0:
39522 # pragma acc loop oacc-loop-clause[optseq] new-line
39523 structured-block */
39525 #define OACC_LOOP_CLAUSE_MASK \
39526 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
39527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
39533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
39534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
39535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
39537 static tree
39538 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
39539 omp_clause_mask mask, tree *cclauses, bool *if_p)
39541 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
39543 strcat (p_name, " loop");
39544 mask |= OACC_LOOP_CLAUSE_MASK;
39546 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
39547 cclauses == NULL);
39548 if (cclauses)
39550 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
39551 if (*cclauses)
39552 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
39553 if (clauses)
39554 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39557 tree block = begin_omp_structured_block ();
39558 int save = cp_parser_begin_omp_structured_block (parser);
39559 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
39560 cp_parser_end_omp_structured_block (parser, save);
39561 add_stmt (finish_omp_structured_block (block));
39563 return stmt;
39566 /* OpenACC 2.0:
39567 # pragma acc kernels oacc-kernels-clause[optseq] new-line
39568 structured-block
39572 # pragma acc parallel oacc-parallel-clause[optseq] new-line
39573 structured-block
39576 #define OACC_KERNELS_CLAUSE_MASK \
39577 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39591 #define OACC_PARALLEL_CLAUSE_MASK \
39592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39609 static tree
39610 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
39611 char *p_name, bool *if_p)
39613 omp_clause_mask mask;
39614 enum tree_code code;
39615 switch (cp_parser_pragma_kind (pragma_tok))
39617 case PRAGMA_OACC_KERNELS:
39618 strcat (p_name, " kernels");
39619 mask = OACC_KERNELS_CLAUSE_MASK;
39620 code = OACC_KERNELS;
39621 break;
39622 case PRAGMA_OACC_PARALLEL:
39623 strcat (p_name, " parallel");
39624 mask = OACC_PARALLEL_CLAUSE_MASK;
39625 code = OACC_PARALLEL;
39626 break;
39627 default:
39628 gcc_unreachable ();
39631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39633 const char *p
39634 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39635 if (strcmp (p, "loop") == 0)
39637 cp_lexer_consume_token (parser->lexer);
39638 tree block = begin_omp_parallel ();
39639 tree clauses;
39640 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
39641 &clauses, if_p);
39642 protected_set_expr_location (stmt, pragma_tok->location);
39643 return finish_omp_construct (code, block, clauses);
39647 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
39649 tree block = begin_omp_parallel ();
39650 unsigned int save = cp_parser_begin_omp_structured_block (parser);
39651 cp_parser_statement (parser, NULL_TREE, false, if_p);
39652 cp_parser_end_omp_structured_block (parser, save);
39653 return finish_omp_construct (code, block, clauses);
39656 /* OpenACC 2.0:
39657 # pragma acc update oacc-update-clause[optseq] new-line
39660 #define OACC_UPDATE_CLAUSE_MASK \
39661 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39668 static tree
39669 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
39671 tree stmt, clauses;
39673 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39674 "#pragma acc update", pragma_tok);
39676 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39678 error_at (pragma_tok->location,
39679 "%<#pragma acc update%> must contain at least one "
39680 "%<device%> or %<host%> or %<self%> clause");
39681 return NULL_TREE;
39684 stmt = make_node (OACC_UPDATE);
39685 TREE_TYPE (stmt) = void_type_node;
39686 OACC_UPDATE_CLAUSES (stmt) = clauses;
39687 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39688 add_stmt (stmt);
39689 return stmt;
39692 /* OpenACC 2.0:
39693 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39695 LOC is the location of the #pragma token.
39698 #define OACC_WAIT_CLAUSE_MASK \
39699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39701 static tree
39702 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39704 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39705 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39707 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39708 list = cp_parser_oacc_wait_list (parser, loc, list);
39710 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39711 "#pragma acc wait", pragma_tok);
39713 stmt = c_finish_oacc_wait (loc, list, clauses);
39714 stmt = finish_expr_stmt (stmt);
39716 return stmt;
39719 /* OpenMP 4.0:
39720 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39722 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39730 static void
39731 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39732 enum pragma_context context)
39734 bool first_p = parser->omp_declare_simd == NULL;
39735 cp_omp_declare_simd_data data;
39736 if (first_p)
39738 data.error_seen = false;
39739 data.fndecl_seen = false;
39740 data.tokens = vNULL;
39741 data.clauses = NULL_TREE;
39742 /* It is safe to take the address of a local variable; it will only be
39743 used while this scope is live. */
39744 parser->omp_declare_simd = &data;
39747 /* Store away all pragma tokens. */
39748 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39749 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39750 cp_lexer_consume_token (parser->lexer);
39751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39752 parser->omp_declare_simd->error_seen = true;
39753 cp_parser_require_pragma_eol (parser, pragma_tok);
39754 struct cp_token_cache *cp
39755 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39756 parser->omp_declare_simd->tokens.safe_push (cp);
39758 if (first_p)
39760 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39761 cp_parser_pragma (parser, context, NULL);
39762 switch (context)
39764 case pragma_external:
39765 cp_parser_declaration (parser);
39766 break;
39767 case pragma_member:
39768 cp_parser_member_declaration (parser);
39769 break;
39770 case pragma_objc_icode:
39771 cp_parser_block_declaration (parser, /*statement_p=*/false);
39772 break;
39773 default:
39774 cp_parser_declaration_statement (parser);
39775 break;
39777 if (parser->omp_declare_simd
39778 && !parser->omp_declare_simd->error_seen
39779 && !parser->omp_declare_simd->fndecl_seen)
39780 error_at (pragma_tok->location,
39781 "%<#pragma omp declare simd%> not immediately followed by "
39782 "function declaration or definition");
39783 data.tokens.release ();
39784 parser->omp_declare_simd = NULL;
39788 /* Finalize #pragma omp declare simd clauses after direct declarator has
39789 been parsed, and put that into "omp declare simd" attribute. */
39791 static tree
39792 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39794 struct cp_token_cache *ce;
39795 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39796 int i;
39798 if (!data->error_seen && data->fndecl_seen)
39800 error ("%<#pragma omp declare simd%> not immediately followed by "
39801 "a single function declaration or definition");
39802 data->error_seen = true;
39804 if (data->error_seen)
39805 return attrs;
39807 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39809 tree c, cl;
39811 cp_parser_push_lexer_for_tokens (parser, ce);
39812 parser->lexer->in_pragma = true;
39813 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39814 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39815 cp_lexer_consume_token (parser->lexer);
39816 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39817 "#pragma omp declare simd", pragma_tok);
39818 cp_parser_pop_lexer (parser);
39819 if (cl)
39820 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39821 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39822 TREE_CHAIN (c) = attrs;
39823 if (processing_template_decl)
39824 ATTR_IS_DEPENDENT (c) = 1;
39825 attrs = c;
39828 data->fndecl_seen = true;
39829 return attrs;
39833 /* OpenMP 4.0:
39834 # pragma omp declare target new-line
39835 declarations and definitions
39836 # pragma omp end declare target new-line
39838 OpenMP 4.5:
39839 # pragma omp declare target ( extended-list ) new-line
39841 # pragma omp declare target declare-target-clauses[seq] new-line */
39843 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
39846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
39848 static void
39849 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39851 tree clauses = NULL_TREE;
39852 int device_type = 0;
39853 bool only_device_type = true;
39854 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39855 clauses
39856 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39857 "#pragma omp declare target", pragma_tok);
39858 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39860 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39861 clauses);
39862 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39863 cp_parser_require_pragma_eol (parser, pragma_tok);
39865 else
39867 cp_parser_require_pragma_eol (parser, pragma_tok);
39868 scope_chain->omp_declare_target_attribute++;
39869 return;
39871 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39872 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
39873 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
39874 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39876 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
39877 continue;
39878 tree t = OMP_CLAUSE_DECL (c), id;
39879 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39880 tree at2 = lookup_attribute ("omp declare target link",
39881 DECL_ATTRIBUTES (t));
39882 only_device_type = false;
39883 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39885 id = get_identifier ("omp declare target link");
39886 std::swap (at1, at2);
39888 else
39889 id = get_identifier ("omp declare target");
39890 if (at2)
39892 error_at (OMP_CLAUSE_LOCATION (c),
39893 "%qD specified both in declare target %<link%> and %<to%>"
39894 " clauses", t);
39895 continue;
39897 if (!at1)
39899 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39900 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39901 continue;
39903 symtab_node *node = symtab_node::get (t);
39904 if (node != NULL)
39906 node->offloadable = 1;
39907 if (ENABLE_OFFLOADING)
39909 g->have_offload = true;
39910 if (is_a <varpool_node *> (node))
39911 vec_safe_push (offload_vars, t);
39915 if (TREE_CODE (t) != FUNCTION_DECL)
39916 continue;
39917 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
39919 tree at3 = lookup_attribute ("omp declare target host",
39920 DECL_ATTRIBUTES (t));
39921 if (at3 == NULL_TREE)
39923 id = get_identifier ("omp declare target host");
39924 DECL_ATTRIBUTES (t)
39925 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39928 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
39930 tree at3 = lookup_attribute ("omp declare target nohost",
39931 DECL_ATTRIBUTES (t));
39932 if (at3 == NULL_TREE)
39934 id = get_identifier ("omp declare target nohost");
39935 DECL_ATTRIBUTES (t)
39936 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39940 if (device_type && only_device_type)
39941 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
39942 "directive with only %<device_type%> clauses ignored");
39945 static void
39946 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39948 const char *p = "";
39949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39952 p = IDENTIFIER_POINTER (id);
39954 if (strcmp (p, "declare") == 0)
39956 cp_lexer_consume_token (parser->lexer);
39957 p = "";
39958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39960 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39961 p = IDENTIFIER_POINTER (id);
39963 if (strcmp (p, "target") == 0)
39964 cp_lexer_consume_token (parser->lexer);
39965 else
39967 cp_parser_error (parser, "expected %<target%>");
39968 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39969 return;
39972 else
39974 cp_parser_error (parser, "expected %<declare%>");
39975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39976 return;
39978 cp_parser_require_pragma_eol (parser, pragma_tok);
39979 if (!scope_chain->omp_declare_target_attribute)
39980 error_at (pragma_tok->location,
39981 "%<#pragma omp end declare target%> without corresponding "
39982 "%<#pragma omp declare target%>");
39983 else
39984 scope_chain->omp_declare_target_attribute--;
39987 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39988 expression and optional initializer clause of
39989 #pragma omp declare reduction. We store the expression(s) as
39990 either 3, 6 or 7 special statements inside of the artificial function's
39991 body. The first two statements are DECL_EXPRs for the artificial
39992 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39993 expression that uses those variables.
39994 If there was any INITIALIZER clause, this is followed by further statements,
39995 the fourth and fifth statements are DECL_EXPRs for the artificial
39996 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39997 constructor variant (first token after open paren is not omp_priv),
39998 then the sixth statement is a statement with the function call expression
39999 that uses the OMP_PRIV and optionally OMP_ORIG variable.
40000 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
40001 to initialize the OMP_PRIV artificial variable and there is seventh
40002 statement, a DECL_EXPR of the OMP_PRIV statement again. */
40004 static bool
40005 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
40007 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
40008 gcc_assert (TYPE_REF_P (type));
40009 type = TREE_TYPE (type);
40010 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
40011 DECL_ARTIFICIAL (omp_out) = 1;
40012 pushdecl (omp_out);
40013 add_decl_expr (omp_out);
40014 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
40015 DECL_ARTIFICIAL (omp_in) = 1;
40016 pushdecl (omp_in);
40017 add_decl_expr (omp_in);
40018 tree combiner;
40019 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
40021 keep_next_level (true);
40022 tree block = begin_omp_structured_block ();
40023 combiner = cp_parser_expression (parser);
40024 finish_expr_stmt (combiner);
40025 block = finish_omp_structured_block (block);
40026 add_stmt (block);
40028 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
40029 return false;
40031 const char *p = "";
40032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40034 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40035 p = IDENTIFIER_POINTER (id);
40038 if (strcmp (p, "initializer") == 0)
40040 cp_lexer_consume_token (parser->lexer);
40041 matching_parens parens;
40042 if (!parens.require_open (parser))
40043 return false;
40045 p = "";
40046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40048 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40049 p = IDENTIFIER_POINTER (id);
40052 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
40053 DECL_ARTIFICIAL (omp_priv) = 1;
40054 pushdecl (omp_priv);
40055 add_decl_expr (omp_priv);
40056 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
40057 DECL_ARTIFICIAL (omp_orig) = 1;
40058 pushdecl (omp_orig);
40059 add_decl_expr (omp_orig);
40061 keep_next_level (true);
40062 block = begin_omp_structured_block ();
40064 bool ctor = false;
40065 if (strcmp (p, "omp_priv") == 0)
40067 bool is_direct_init, is_non_constant_init;
40068 ctor = true;
40069 cp_lexer_consume_token (parser->lexer);
40070 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
40071 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
40072 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
40073 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
40074 == CPP_CLOSE_PAREN
40075 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
40076 == CPP_CLOSE_PAREN))
40078 finish_omp_structured_block (block);
40079 error ("invalid initializer clause");
40080 return false;
40082 initializer = cp_parser_initializer (parser, &is_direct_init,
40083 &is_non_constant_init);
40084 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
40085 NULL_TREE, LOOKUP_ONLYCONVERTING);
40087 else
40089 cp_parser_parse_tentatively (parser);
40090 /* Don't create location wrapper nodes here. */
40091 auto_suppress_location_wrappers sentinel;
40092 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
40093 /*check_dependency_p=*/true,
40094 /*template_p=*/NULL,
40095 /*declarator_p=*/false,
40096 /*optional_p=*/false);
40097 vec<tree, va_gc> *args;
40098 if (fn_name == error_mark_node
40099 || cp_parser_error_occurred (parser)
40100 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
40101 || ((args = cp_parser_parenthesized_expression_list
40102 (parser, non_attr, /*cast_p=*/false,
40103 /*allow_expansion_p=*/true,
40104 /*non_constant_p=*/NULL)),
40105 cp_parser_error_occurred (parser)))
40107 finish_omp_structured_block (block);
40108 cp_parser_abort_tentative_parse (parser);
40109 cp_parser_error (parser, "expected id-expression (arguments)");
40110 return false;
40112 unsigned int i;
40113 tree arg;
40114 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
40115 if (arg == omp_priv
40116 || (TREE_CODE (arg) == ADDR_EXPR
40117 && TREE_OPERAND (arg, 0) == omp_priv))
40118 break;
40119 cp_parser_abort_tentative_parse (parser);
40120 if (arg == NULL_TREE)
40121 error ("one of the initializer call arguments should be %<omp_priv%>"
40122 " or %<&omp_priv%>");
40123 initializer = cp_parser_postfix_expression (parser, false, false, false,
40124 false, NULL);
40125 finish_expr_stmt (initializer);
40128 block = finish_omp_structured_block (block);
40129 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
40130 add_stmt (block);
40132 if (ctor)
40133 add_decl_expr (omp_orig);
40135 if (!parens.require_close (parser))
40136 return false;
40139 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
40140 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
40141 UNKNOWN_LOCATION);
40143 return true;
40146 /* OpenMP 4.0
40147 #pragma omp declare reduction (reduction-id : typename-list : expression) \
40148 initializer-clause[opt] new-line
40150 initializer-clause:
40151 initializer (omp_priv initializer)
40152 initializer (function-name (argument-list)) */
40154 static void
40155 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
40156 enum pragma_context)
40158 auto_vec<tree> types;
40159 enum tree_code reduc_code = ERROR_MARK;
40160 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
40161 unsigned int i;
40162 cp_token *first_token;
40163 cp_token_cache *cp;
40164 int errs;
40165 void *p;
40167 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
40168 p = obstack_alloc (&declarator_obstack, 0);
40170 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40171 goto fail;
40173 switch (cp_lexer_peek_token (parser->lexer)->type)
40175 case CPP_PLUS:
40176 reduc_code = PLUS_EXPR;
40177 break;
40178 case CPP_MULT:
40179 reduc_code = MULT_EXPR;
40180 break;
40181 case CPP_MINUS:
40182 reduc_code = MINUS_EXPR;
40183 break;
40184 case CPP_AND:
40185 reduc_code = BIT_AND_EXPR;
40186 break;
40187 case CPP_XOR:
40188 reduc_code = BIT_XOR_EXPR;
40189 break;
40190 case CPP_OR:
40191 reduc_code = BIT_IOR_EXPR;
40192 break;
40193 case CPP_AND_AND:
40194 reduc_code = TRUTH_ANDIF_EXPR;
40195 break;
40196 case CPP_OR_OR:
40197 reduc_code = TRUTH_ORIF_EXPR;
40198 break;
40199 case CPP_NAME:
40200 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
40201 break;
40202 default:
40203 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
40204 "%<|%>, %<&&%>, %<||%> or identifier");
40205 goto fail;
40208 if (reduc_code != ERROR_MARK)
40209 cp_lexer_consume_token (parser->lexer);
40211 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
40212 if (reduc_id == error_mark_node)
40213 goto fail;
40215 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40216 goto fail;
40218 /* Types may not be defined in declare reduction type list. */
40219 const char *saved_message;
40220 saved_message = parser->type_definition_forbidden_message;
40221 parser->type_definition_forbidden_message
40222 = G_("types may not be defined in declare reduction type list");
40223 bool saved_colon_corrects_to_scope_p;
40224 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40225 parser->colon_corrects_to_scope_p = false;
40226 bool saved_colon_doesnt_start_class_def_p;
40227 saved_colon_doesnt_start_class_def_p
40228 = parser->colon_doesnt_start_class_def_p;
40229 parser->colon_doesnt_start_class_def_p = true;
40231 while (true)
40233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40234 type = cp_parser_type_id (parser);
40235 if (type == error_mark_node)
40237 else if (ARITHMETIC_TYPE_P (type)
40238 && (orig_reduc_id == NULL_TREE
40239 || (TREE_CODE (type) != COMPLEX_TYPE
40240 && (id_equal (orig_reduc_id, "min")
40241 || id_equal (orig_reduc_id, "max")))))
40242 error_at (loc, "predeclared arithmetic type %qT in "
40243 "%<#pragma omp declare reduction%>", type);
40244 else if (FUNC_OR_METHOD_TYPE_P (type)
40245 || TREE_CODE (type) == ARRAY_TYPE)
40246 error_at (loc, "function or array type %qT in "
40247 "%<#pragma omp declare reduction%>", type);
40248 else if (TYPE_REF_P (type))
40249 error_at (loc, "reference type %qT in "
40250 "%<#pragma omp declare reduction%>", type);
40251 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
40252 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
40253 "type %qT in %<#pragma omp declare reduction%>", type);
40254 else
40255 types.safe_push (type);
40257 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40258 cp_lexer_consume_token (parser->lexer);
40259 else
40260 break;
40263 /* Restore the saved message. */
40264 parser->type_definition_forbidden_message = saved_message;
40265 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40266 parser->colon_doesnt_start_class_def_p
40267 = saved_colon_doesnt_start_class_def_p;
40269 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
40270 || types.is_empty ())
40272 fail:
40273 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40274 goto done;
40277 first_token = cp_lexer_peek_token (parser->lexer);
40278 cp = NULL;
40279 errs = errorcount;
40280 FOR_EACH_VEC_ELT (types, i, type)
40282 tree fntype
40283 = build_function_type_list (void_type_node,
40284 cp_build_reference_type (type, false),
40285 NULL_TREE);
40286 tree this_reduc_id = reduc_id;
40287 if (!dependent_type_p (type))
40288 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
40289 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
40290 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
40291 DECL_ARTIFICIAL (fndecl) = 1;
40292 DECL_EXTERNAL (fndecl) = 1;
40293 DECL_DECLARED_INLINE_P (fndecl) = 1;
40294 DECL_IGNORED_P (fndecl) = 1;
40295 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
40296 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
40297 DECL_ATTRIBUTES (fndecl)
40298 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
40299 DECL_ATTRIBUTES (fndecl));
40300 if (processing_template_decl)
40301 fndecl = push_template_decl (fndecl);
40302 bool block_scope = false;
40303 tree block = NULL_TREE;
40304 if (current_function_decl)
40306 block_scope = true;
40307 DECL_CONTEXT (fndecl) = global_namespace;
40308 if (!processing_template_decl)
40309 pushdecl (fndecl);
40311 else if (current_class_type)
40313 if (cp == NULL)
40315 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40316 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40317 cp_lexer_consume_token (parser->lexer);
40318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40319 goto fail;
40320 cp = cp_token_cache_new (first_token,
40321 cp_lexer_peek_nth_token (parser->lexer,
40322 2));
40324 DECL_STATIC_FUNCTION_P (fndecl) = 1;
40325 finish_member_declaration (fndecl);
40326 DECL_PENDING_INLINE_INFO (fndecl) = cp;
40327 DECL_PENDING_INLINE_P (fndecl) = 1;
40328 vec_safe_push (unparsed_funs_with_definitions, fndecl);
40329 continue;
40331 else
40333 DECL_CONTEXT (fndecl) = current_namespace;
40334 pushdecl (fndecl);
40336 if (!block_scope)
40337 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
40338 else
40339 block = begin_omp_structured_block ();
40340 if (cp)
40342 cp_parser_push_lexer_for_tokens (parser, cp);
40343 parser->lexer->in_pragma = true;
40345 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
40347 if (!block_scope)
40348 finish_function (/*inline_p=*/false);
40349 else
40350 DECL_CONTEXT (fndecl) = current_function_decl;
40351 if (cp)
40352 cp_parser_pop_lexer (parser);
40353 goto fail;
40355 if (cp)
40356 cp_parser_pop_lexer (parser);
40357 if (!block_scope)
40358 finish_function (/*inline_p=*/false);
40359 else
40361 DECL_CONTEXT (fndecl) = current_function_decl;
40362 block = finish_omp_structured_block (block);
40363 if (TREE_CODE (block) == BIND_EXPR)
40364 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
40365 else if (TREE_CODE (block) == STATEMENT_LIST)
40366 DECL_SAVED_TREE (fndecl) = block;
40367 if (processing_template_decl)
40368 add_decl_expr (fndecl);
40370 cp_check_omp_declare_reduction (fndecl);
40371 if (cp == NULL && types.length () > 1)
40372 cp = cp_token_cache_new (first_token,
40373 cp_lexer_peek_nth_token (parser->lexer, 2));
40374 if (errs != errorcount)
40375 break;
40378 cp_parser_require_pragma_eol (parser, pragma_tok);
40380 done:
40381 /* Free any declarators allocated. */
40382 obstack_free (&declarator_obstack, p);
40385 /* OpenMP 4.0
40386 #pragma omp declare simd declare-simd-clauses[optseq] new-line
40387 #pragma omp declare reduction (reduction-id : typename-list : expression) \
40388 initializer-clause[opt] new-line
40389 #pragma omp declare target new-line */
40391 static bool
40392 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
40393 enum pragma_context context)
40395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40397 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40398 const char *p = IDENTIFIER_POINTER (id);
40400 if (strcmp (p, "simd") == 0)
40402 cp_lexer_consume_token (parser->lexer);
40403 cp_parser_omp_declare_simd (parser, pragma_tok,
40404 context);
40405 return true;
40407 cp_ensure_no_omp_declare_simd (parser);
40408 if (strcmp (p, "reduction") == 0)
40410 cp_lexer_consume_token (parser->lexer);
40411 cp_parser_omp_declare_reduction (parser, pragma_tok,
40412 context);
40413 return false;
40415 if (!flag_openmp) /* flag_openmp_simd */
40417 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40418 return false;
40420 if (strcmp (p, "target") == 0)
40422 cp_lexer_consume_token (parser->lexer);
40423 cp_parser_omp_declare_target (parser, pragma_tok);
40424 return false;
40427 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
40428 "or %<target%>");
40429 cp_parser_require_pragma_eol (parser, pragma_tok);
40430 return false;
40433 /* OpenMP 5.0
40434 #pragma omp requires clauses[optseq] new-line */
40436 static bool
40437 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
40439 bool first = true;
40440 enum omp_requires new_req = (enum omp_requires) 0;
40442 location_t loc = pragma_tok->location;
40443 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40445 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40446 cp_lexer_consume_token (parser->lexer);
40448 first = false;
40450 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40452 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40453 const char *p = IDENTIFIER_POINTER (id);
40454 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40455 enum omp_requires this_req = (enum omp_requires) 0;
40457 if (!strcmp (p, "unified_address"))
40458 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
40459 else if (!strcmp (p, "unified_shared_memory"))
40460 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
40461 else if (!strcmp (p, "dynamic_allocators"))
40462 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
40463 else if (!strcmp (p, "reverse_offload"))
40464 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
40465 else if (!strcmp (p, "atomic_default_mem_order"))
40467 cp_lexer_consume_token (parser->lexer);
40469 matching_parens parens;
40470 if (parens.require_open (parser))
40472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40474 id = cp_lexer_peek_token (parser->lexer)->u.value;
40475 p = IDENTIFIER_POINTER (id);
40477 if (!strcmp (p, "seq_cst"))
40478 this_req
40479 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
40480 else if (!strcmp (p, "relaxed"))
40481 this_req
40482 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
40483 else if (!strcmp (p, "acq_rel"))
40484 this_req
40485 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
40487 if (this_req == 0)
40489 error_at (cp_lexer_peek_token (parser->lexer)->location,
40490 "expected %<seq_cst%>, %<relaxed%> or "
40491 "%<acq_rel%>");
40492 if (cp_lexer_nth_token_is (parser->lexer, 2,
40493 CPP_CLOSE_PAREN))
40494 cp_lexer_consume_token (parser->lexer);
40496 else
40497 cp_lexer_consume_token (parser->lexer);
40499 if (!parens.require_close (parser))
40500 cp_parser_skip_to_closing_parenthesis (parser,
40501 /*recovering=*/true,
40502 /*or_comma=*/false,
40503 /*consume_paren=*/
40504 true);
40506 if (this_req == 0)
40508 cp_parser_require_pragma_eol (parser, pragma_tok);
40509 return false;
40512 p = NULL;
40514 else
40516 error_at (cloc, "expected %<unified_address%>, "
40517 "%<unified_shared_memory%>, "
40518 "%<dynamic_allocators%>, "
40519 "%<reverse_offload%> "
40520 "or %<atomic_default_mem_order%> clause");
40521 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40522 return false;
40524 if (p)
40525 sorry_at (cloc, "%qs clause on %<requires%> directive not "
40526 "supported yet", p);
40527 if (p)
40528 cp_lexer_consume_token (parser->lexer);
40529 if (this_req)
40531 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40533 if ((this_req & new_req) != 0)
40534 error_at (cloc, "too many %qs clauses", p);
40535 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
40536 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
40537 error_at (cloc, "%qs clause used lexically after first "
40538 "target construct or offloading API", p);
40540 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40542 error_at (cloc, "too many %qs clauses",
40543 "atomic_default_mem_order");
40544 this_req = (enum omp_requires) 0;
40546 else if ((omp_requires_mask
40547 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40549 error_at (cloc, "more than one %<atomic_default_mem_order%>"
40550 " clause in a single compilation unit");
40551 this_req
40552 = (enum omp_requires)
40553 (omp_requires_mask
40554 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
40556 else if ((omp_requires_mask
40557 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
40558 error_at (cloc, "%<atomic_default_mem_order%> clause used "
40559 "lexically after first %<atomic%> construct "
40560 "without memory order clause");
40561 new_req = (enum omp_requires) (new_req | this_req);
40562 omp_requires_mask
40563 = (enum omp_requires) (omp_requires_mask | this_req);
40564 continue;
40567 break;
40569 cp_parser_require_pragma_eol (parser, pragma_tok);
40571 if (new_req == 0)
40572 error_at (loc, "%<pragma omp requires%> requires at least one clause");
40573 return false;
40577 /* OpenMP 4.5:
40578 #pragma omp taskloop taskloop-clause[optseq] new-line
40579 for-loop
40581 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
40582 for-loop */
40584 #define OMP_TASKLOOP_CLAUSE_MASK \
40585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
40591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
40592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
40598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40602 static tree
40603 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
40604 char *p_name, omp_clause_mask mask, tree *cclauses,
40605 bool *if_p)
40607 tree clauses, sb, ret;
40608 unsigned int save;
40609 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40611 strcat (p_name, " taskloop");
40612 mask |= OMP_TASKLOOP_CLAUSE_MASK;
40613 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40614 clause. */
40615 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
40616 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
40618 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40620 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40621 const char *p = IDENTIFIER_POINTER (id);
40623 if (strcmp (p, "simd") == 0)
40625 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40626 if (cclauses == NULL)
40627 cclauses = cclauses_buf;
40629 cp_lexer_consume_token (parser->lexer);
40630 if (!flag_openmp) /* flag_openmp_simd */
40631 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40632 cclauses, if_p);
40633 sb = begin_omp_structured_block ();
40634 save = cp_parser_begin_omp_structured_block (parser);
40635 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40636 cclauses, if_p);
40637 cp_parser_end_omp_structured_block (parser, save);
40638 tree body = finish_omp_structured_block (sb);
40639 if (ret == NULL)
40640 return ret;
40641 ret = make_node (OMP_TASKLOOP);
40642 TREE_TYPE (ret) = void_type_node;
40643 OMP_FOR_BODY (ret) = body;
40644 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40645 SET_EXPR_LOCATION (ret, loc);
40646 add_stmt (ret);
40647 return ret;
40650 if (!flag_openmp) /* flag_openmp_simd */
40652 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40653 return NULL_TREE;
40656 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40657 cclauses == NULL);
40658 if (cclauses)
40660 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
40661 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40664 keep_next_level (true);
40665 sb = begin_omp_structured_block ();
40666 save = cp_parser_begin_omp_structured_block (parser);
40668 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
40669 if_p);
40671 cp_parser_end_omp_structured_block (parser, save);
40672 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40674 return ret;
40678 /* OpenACC 2.0:
40679 # pragma acc routine oacc-routine-clause[optseq] new-line
40680 function-definition
40682 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40685 #define OACC_ROUTINE_CLAUSE_MASK \
40686 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40692 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40693 component, which must resolve to a declared namespace-scope
40694 function. The clauses are either processed directly (for a named
40695 function), or defered until the immediatley following declaration
40696 is parsed. */
40698 static void
40699 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
40700 enum pragma_context context)
40702 gcc_checking_assert (context == pragma_external);
40703 /* The checking for "another pragma following this one" in the "no optional
40704 '( name )'" case makes sure that we dont re-enter. */
40705 gcc_checking_assert (parser->oacc_routine == NULL);
40707 cp_oacc_routine_data data;
40708 data.error_seen = false;
40709 data.fndecl_seen = false;
40710 data.tokens = vNULL;
40711 data.clauses = NULL_TREE;
40712 data.loc = pragma_tok->location;
40713 /* It is safe to take the address of a local variable; it will only be
40714 used while this scope is live. */
40715 parser->oacc_routine = &data;
40717 /* Look for optional '( name )'. */
40718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40720 matching_parens parens;
40721 parens.consume_open (parser); /* '(' */
40723 /* We parse the name as an id-expression. If it resolves to
40724 anything other than a non-overloaded function at namespace
40725 scope, it's an error. */
40726 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40727 tree name = cp_parser_id_expression (parser,
40728 /*template_keyword_p=*/false,
40729 /*check_dependency_p=*/false,
40730 /*template_p=*/NULL,
40731 /*declarator_p=*/false,
40732 /*optional_p=*/false);
40733 tree decl = (identifier_p (name)
40734 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40735 : name);
40736 if (name != error_mark_node && decl == error_mark_node)
40737 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40739 if (decl == error_mark_node
40740 || !parens.require_close (parser))
40742 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40743 parser->oacc_routine = NULL;
40744 return;
40747 data.clauses
40748 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40749 "#pragma acc routine",
40750 cp_lexer_peek_token (parser->lexer));
40751 /* The clauses are in reverse order; fix that to make later diagnostic
40752 emission easier. */
40753 data.clauses = nreverse (data.clauses);
40755 if (decl && is_overloaded_fn (decl)
40756 && (TREE_CODE (decl) != FUNCTION_DECL
40757 || DECL_FUNCTION_TEMPLATE_P (decl)))
40759 error_at (name_loc,
40760 "%<#pragma acc routine%> names a set of overloads");
40761 parser->oacc_routine = NULL;
40762 return;
40765 /* Perhaps we should use the same rule as declarations in different
40766 namespaces? */
40767 if (!DECL_NAMESPACE_SCOPE_P (decl))
40769 error_at (name_loc,
40770 "%qD does not refer to a namespace scope function", decl);
40771 parser->oacc_routine = NULL;
40772 return;
40775 if (TREE_CODE (decl) != FUNCTION_DECL)
40777 error_at (name_loc, "%qD does not refer to a function", decl);
40778 parser->oacc_routine = NULL;
40779 return;
40782 cp_finalize_oacc_routine (parser, decl, false);
40783 parser->oacc_routine = NULL;
40785 else /* No optional '( name )'. */
40787 /* Store away all pragma tokens. */
40788 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40789 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40790 cp_lexer_consume_token (parser->lexer);
40791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40792 parser->oacc_routine->error_seen = true;
40793 cp_parser_require_pragma_eol (parser, pragma_tok);
40794 struct cp_token_cache *cp
40795 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40796 parser->oacc_routine->tokens.safe_push (cp);
40798 /* Emit a helpful diagnostic if there's another pragma following this
40799 one. */
40800 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40802 cp_ensure_no_oacc_routine (parser);
40803 data.tokens.release ();
40804 /* ..., and then just keep going. */
40805 return;
40808 /* We only have to consider the pragma_external case here. */
40809 cp_parser_declaration (parser);
40810 if (parser->oacc_routine
40811 && !parser->oacc_routine->fndecl_seen)
40812 cp_ensure_no_oacc_routine (parser);
40813 else
40814 parser->oacc_routine = NULL;
40815 data.tokens.release ();
40819 /* Finalize #pragma acc routine clauses after direct declarator has
40820 been parsed. */
40822 static tree
40823 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40825 struct cp_token_cache *ce;
40826 cp_oacc_routine_data *data = parser->oacc_routine;
40828 if (!data->error_seen && data->fndecl_seen)
40830 error_at (data->loc,
40831 "%<#pragma acc routine%> not immediately followed by "
40832 "a single function declaration or definition");
40833 data->error_seen = true;
40835 if (data->error_seen)
40836 return attrs;
40838 gcc_checking_assert (data->tokens.length () == 1);
40839 ce = data->tokens[0];
40841 cp_parser_push_lexer_for_tokens (parser, ce);
40842 parser->lexer->in_pragma = true;
40843 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40845 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40846 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40847 parser->oacc_routine->clauses
40848 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40849 "#pragma acc routine", pragma_tok);
40850 /* The clauses are in reverse order; fix that to make later diagnostic
40851 emission easier. */
40852 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
40853 cp_parser_pop_lexer (parser);
40854 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40855 fndecl_seen. */
40857 return attrs;
40860 /* Apply any saved OpenACC routine clauses to a just-parsed
40861 declaration. */
40863 static void
40864 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40866 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40868 /* Keep going if we're in error reporting mode. */
40869 if (parser->oacc_routine->error_seen
40870 || fndecl == error_mark_node)
40871 return;
40873 if (parser->oacc_routine->fndecl_seen)
40875 error_at (parser->oacc_routine->loc,
40876 "%<#pragma acc routine%> not immediately followed by"
40877 " a single function declaration or definition");
40878 parser->oacc_routine = NULL;
40879 return;
40881 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40883 cp_ensure_no_oacc_routine (parser);
40884 return;
40887 int compatible
40888 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
40889 parser->oacc_routine->loc,
40890 "#pragma acc routine");
40891 if (compatible < 0)
40893 parser->oacc_routine = NULL;
40894 return;
40896 if (compatible > 0)
40899 else
40901 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40903 error_at (parser->oacc_routine->loc,
40904 TREE_USED (fndecl)
40905 ? G_("%<#pragma acc routine%> must be applied before"
40906 " use")
40907 : G_("%<#pragma acc routine%> must be applied before"
40908 " definition"));
40909 parser->oacc_routine = NULL;
40910 return;
40913 /* Set the routine's level of parallelism. */
40914 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40915 oacc_replace_fn_attrib (fndecl, dims);
40917 /* Add an "omp declare target" attribute. */
40918 DECL_ATTRIBUTES (fndecl)
40919 = tree_cons (get_identifier ("omp declare target"),
40920 parser->oacc_routine->clauses,
40921 DECL_ATTRIBUTES (fndecl));
40924 /* Don't unset parser->oacc_routine here: we may still need it to
40925 diagnose wrong usage. But, remember that we've used this "#pragma acc
40926 routine". */
40927 parser->oacc_routine->fndecl_seen = true;
40931 /* Main entry point to OpenMP statement pragmas. */
40933 static void
40934 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40936 tree stmt;
40937 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40938 omp_clause_mask mask (0);
40940 switch (cp_parser_pragma_kind (pragma_tok))
40942 case PRAGMA_OACC_ATOMIC:
40943 cp_parser_omp_atomic (parser, pragma_tok);
40944 return;
40945 case PRAGMA_OACC_CACHE:
40946 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40947 break;
40948 case PRAGMA_OACC_DATA:
40949 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40950 break;
40951 case PRAGMA_OACC_ENTER_DATA:
40952 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40953 break;
40954 case PRAGMA_OACC_EXIT_DATA:
40955 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40956 break;
40957 case PRAGMA_OACC_HOST_DATA:
40958 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40959 break;
40960 case PRAGMA_OACC_KERNELS:
40961 case PRAGMA_OACC_PARALLEL:
40962 strcpy (p_name, "#pragma acc");
40963 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40964 if_p);
40965 break;
40966 case PRAGMA_OACC_LOOP:
40967 strcpy (p_name, "#pragma acc");
40968 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40969 if_p);
40970 break;
40971 case PRAGMA_OACC_UPDATE:
40972 stmt = cp_parser_oacc_update (parser, pragma_tok);
40973 break;
40974 case PRAGMA_OACC_WAIT:
40975 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40976 break;
40977 case PRAGMA_OMP_ATOMIC:
40978 cp_parser_omp_atomic (parser, pragma_tok);
40979 return;
40980 case PRAGMA_OMP_CRITICAL:
40981 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40982 break;
40983 case PRAGMA_OMP_DISTRIBUTE:
40984 strcpy (p_name, "#pragma omp");
40985 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40986 if_p);
40987 break;
40988 case PRAGMA_OMP_FOR:
40989 strcpy (p_name, "#pragma omp");
40990 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40991 if_p);
40992 break;
40993 case PRAGMA_OMP_LOOP:
40994 strcpy (p_name, "#pragma omp");
40995 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
40996 if_p);
40997 break;
40998 case PRAGMA_OMP_MASTER:
40999 strcpy (p_name, "#pragma omp");
41000 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
41001 if_p);
41002 break;
41003 case PRAGMA_OMP_PARALLEL:
41004 strcpy (p_name, "#pragma omp");
41005 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
41006 if_p);
41007 break;
41008 case PRAGMA_OMP_SECTIONS:
41009 strcpy (p_name, "#pragma omp");
41010 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
41011 break;
41012 case PRAGMA_OMP_SIMD:
41013 strcpy (p_name, "#pragma omp");
41014 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
41015 if_p);
41016 break;
41017 case PRAGMA_OMP_SINGLE:
41018 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
41019 break;
41020 case PRAGMA_OMP_TASK:
41021 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
41022 break;
41023 case PRAGMA_OMP_TASKGROUP:
41024 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
41025 break;
41026 case PRAGMA_OMP_TASKLOOP:
41027 strcpy (p_name, "#pragma omp");
41028 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
41029 if_p);
41030 break;
41031 case PRAGMA_OMP_TEAMS:
41032 strcpy (p_name, "#pragma omp");
41033 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
41034 if_p);
41035 break;
41036 default:
41037 gcc_unreachable ();
41040 protected_set_expr_location (stmt, pragma_tok->location);
41043 /* Transactional Memory parsing routines. */
41045 /* Parse a transaction attribute.
41047 txn-attribute:
41048 attribute
41049 [ [ identifier ] ]
41051 We use this instead of cp_parser_attributes_opt for transactions to avoid
41052 the pedwarn in C++98 mode. */
41054 static tree
41055 cp_parser_txn_attribute_opt (cp_parser *parser)
41057 cp_token *token;
41058 tree attr_name, attr = NULL;
41060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
41061 return cp_parser_attributes_opt (parser);
41063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
41064 return NULL_TREE;
41065 cp_lexer_consume_token (parser->lexer);
41066 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
41067 goto error1;
41069 token = cp_lexer_peek_token (parser->lexer);
41070 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
41072 token = cp_lexer_consume_token (parser->lexer);
41074 attr_name = (token->type == CPP_KEYWORD
41075 /* For keywords, use the canonical spelling,
41076 not the parsed identifier. */
41077 ? ridpointers[(int) token->keyword]
41078 : token->u.value);
41079 attr = build_tree_list (attr_name, NULL_TREE);
41081 else
41082 cp_parser_error (parser, "expected identifier");
41084 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
41085 error1:
41086 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
41087 return attr;
41090 /* Parse a __transaction_atomic or __transaction_relaxed statement.
41092 transaction-statement:
41093 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
41094 compound-statement
41095 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
41098 static tree
41099 cp_parser_transaction (cp_parser *parser, cp_token *token)
41101 unsigned char old_in = parser->in_transaction;
41102 unsigned char this_in = 1, new_in;
41103 enum rid keyword = token->keyword;
41104 tree stmt, attrs, noex;
41106 cp_lexer_consume_token (parser->lexer);
41108 if (keyword == RID_TRANSACTION_RELAXED
41109 || keyword == RID_SYNCHRONIZED)
41110 this_in |= TM_STMT_ATTR_RELAXED;
41111 else
41113 attrs = cp_parser_txn_attribute_opt (parser);
41114 if (attrs)
41115 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
41118 /* Parse a noexcept specification. */
41119 if (keyword == RID_ATOMIC_NOEXCEPT)
41120 noex = boolean_true_node;
41121 else if (keyword == RID_ATOMIC_CANCEL)
41123 /* cancel-and-throw is unimplemented. */
41124 sorry ("%<atomic_cancel%>");
41125 noex = NULL_TREE;
41127 else
41128 noex = cp_parser_noexcept_specification_opt (parser,
41129 /*require_constexpr=*/true,
41130 /*consumed_expr=*/NULL,
41131 /*return_cond=*/true,
41132 /*friend_p=*/false);
41134 /* Keep track if we're in the lexical scope of an outer transaction. */
41135 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
41137 stmt = begin_transaction_stmt (token->location, NULL, this_in);
41139 parser->in_transaction = new_in;
41140 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
41141 parser->in_transaction = old_in;
41143 finish_transaction_stmt (stmt, NULL, this_in, noex);
41145 return stmt;
41148 /* Parse a __transaction_atomic or __transaction_relaxed expression.
41150 transaction-expression:
41151 __transaction_atomic txn-noexcept-spec[opt] ( expression )
41152 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
41155 static tree
41156 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
41158 unsigned char old_in = parser->in_transaction;
41159 unsigned char this_in = 1;
41160 cp_token *token;
41161 tree expr, noex;
41162 bool noex_expr;
41163 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41165 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
41166 || keyword == RID_TRANSACTION_RELAXED);
41168 if (!flag_tm)
41169 error_at (loc,
41170 keyword == RID_TRANSACTION_RELAXED
41171 ? G_("%<__transaction_relaxed%> without transactional memory "
41172 "support enabled")
41173 : G_("%<__transaction_atomic%> without transactional memory "
41174 "support enabled"));
41176 token = cp_parser_require_keyword (parser, keyword,
41177 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
41178 : RT_TRANSACTION_RELAXED));
41179 gcc_assert (token != NULL);
41181 if (keyword == RID_TRANSACTION_RELAXED)
41182 this_in |= TM_STMT_ATTR_RELAXED;
41184 /* Set this early. This might mean that we allow transaction_cancel in
41185 an expression that we find out later actually has to be a constexpr.
41186 However, we expect that cxx_constant_value will be able to deal with
41187 this; also, if the noexcept has no constexpr, then what we parse next
41188 really is a transaction's body. */
41189 parser->in_transaction = this_in;
41191 /* Parse a noexcept specification. */
41192 noex = cp_parser_noexcept_specification_opt (parser,
41193 /*require_constexpr=*/false,
41194 &noex_expr,
41195 /*return_cond=*/true,
41196 /*friend_p=*/false);
41198 if (!noex || !noex_expr
41199 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41201 matching_parens parens;
41202 parens.require_open (parser);
41204 expr = cp_parser_expression (parser);
41205 expr = finish_parenthesized_expr (expr);
41207 parens.require_close (parser);
41209 else
41211 /* The only expression that is available got parsed for the noexcept
41212 already. noexcept is true then. */
41213 expr = noex;
41214 noex = boolean_true_node;
41217 expr = build_transaction_expr (token->location, expr, this_in, noex);
41218 parser->in_transaction = old_in;
41220 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
41221 return error_mark_node;
41223 return (flag_tm ? expr : error_mark_node);
41226 /* Parse a function-transaction-block.
41228 function-transaction-block:
41229 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
41230 function-body
41231 __transaction_atomic txn-attribute[opt] function-try-block
41232 __transaction_relaxed ctor-initializer[opt] function-body
41233 __transaction_relaxed function-try-block
41236 static void
41237 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
41239 unsigned char old_in = parser->in_transaction;
41240 unsigned char new_in = 1;
41241 tree compound_stmt, stmt, attrs;
41242 cp_token *token;
41244 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
41245 || keyword == RID_TRANSACTION_RELAXED);
41246 token = cp_parser_require_keyword (parser, keyword,
41247 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
41248 : RT_TRANSACTION_RELAXED));
41249 gcc_assert (token != NULL);
41251 if (keyword == RID_TRANSACTION_RELAXED)
41252 new_in |= TM_STMT_ATTR_RELAXED;
41253 else
41255 attrs = cp_parser_txn_attribute_opt (parser);
41256 if (attrs)
41257 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
41260 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
41262 parser->in_transaction = new_in;
41264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
41265 cp_parser_function_try_block (parser);
41266 else
41267 cp_parser_ctor_initializer_opt_and_function_body
41268 (parser, /*in_function_try_block=*/false);
41270 parser->in_transaction = old_in;
41272 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
41275 /* Parse a __transaction_cancel statement.
41277 cancel-statement:
41278 __transaction_cancel txn-attribute[opt] ;
41279 __transaction_cancel txn-attribute[opt] throw-expression ;
41281 ??? Cancel and throw is not yet implemented. */
41283 static tree
41284 cp_parser_transaction_cancel (cp_parser *parser)
41286 cp_token *token;
41287 bool is_outer = false;
41288 tree stmt, attrs;
41290 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
41291 RT_TRANSACTION_CANCEL);
41292 gcc_assert (token != NULL);
41294 attrs = cp_parser_txn_attribute_opt (parser);
41295 if (attrs)
41296 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
41298 /* ??? Parse cancel-and-throw here. */
41300 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
41302 if (!flag_tm)
41304 error_at (token->location, "%<__transaction_cancel%> without "
41305 "transactional memory support enabled");
41306 return error_mark_node;
41308 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
41310 error_at (token->location, "%<__transaction_cancel%> within a "
41311 "%<__transaction_relaxed%>");
41312 return error_mark_node;
41314 else if (is_outer)
41316 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
41317 && !is_tm_may_cancel_outer (current_function_decl))
41319 error_at (token->location, "outer %<__transaction_cancel%> not "
41320 "within outer %<__transaction_atomic%>");
41321 error_at (token->location,
41322 " or a %<transaction_may_cancel_outer%> function");
41323 return error_mark_node;
41326 else if (parser->in_transaction == 0)
41328 error_at (token->location, "%<__transaction_cancel%> not within "
41329 "%<__transaction_atomic%>");
41330 return error_mark_node;
41333 stmt = build_tm_abort_call (token->location, is_outer);
41334 add_stmt (stmt);
41336 return stmt;
41339 /* The parser. */
41341 static GTY (()) cp_parser *the_parser;
41344 /* Special handling for the first token or line in the file. The first
41345 thing in the file might be #pragma GCC pch_preprocess, which loads a
41346 PCH file, which is a GC collection point. So we need to handle this
41347 first pragma without benefit of an existing lexer structure.
41349 Always returns one token to the caller in *FIRST_TOKEN. This is
41350 either the true first token of the file, or the first token after
41351 the initial pragma. */
41353 static void
41354 cp_parser_initial_pragma (cp_token *first_token)
41356 tree name = NULL;
41358 cp_lexer_get_preprocessor_token (NULL, first_token);
41359 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
41360 return;
41362 cp_lexer_get_preprocessor_token (NULL, first_token);
41363 if (first_token->type == CPP_STRING)
41365 name = first_token->u.value;
41367 cp_lexer_get_preprocessor_token (NULL, first_token);
41368 if (first_token->type != CPP_PRAGMA_EOL)
41369 error_at (first_token->location,
41370 "junk at end of %<#pragma GCC pch_preprocess%>");
41372 else
41373 error_at (first_token->location, "expected string literal");
41375 /* Skip to the end of the pragma. */
41376 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
41377 cp_lexer_get_preprocessor_token (NULL, first_token);
41379 /* Now actually load the PCH file. */
41380 if (name)
41381 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
41383 /* Read one more token to return to our caller. We have to do this
41384 after reading the PCH file in, since its pointers have to be
41385 live. */
41386 cp_lexer_get_preprocessor_token (NULL, first_token);
41389 /* Parse a pragma GCC ivdep. */
41391 static bool
41392 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
41394 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41395 return true;
41398 /* Parse a pragma GCC unroll. */
41400 static unsigned short
41401 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
41403 location_t location = cp_lexer_peek_token (parser->lexer)->location;
41404 tree expr = cp_parser_constant_expression (parser);
41405 unsigned short unroll;
41406 expr = maybe_constant_value (expr);
41407 HOST_WIDE_INT lunroll = 0;
41408 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
41409 || TREE_CODE (expr) != INTEGER_CST
41410 || (lunroll = tree_to_shwi (expr)) < 0
41411 || lunroll >= USHRT_MAX)
41413 error_at (location, "%<#pragma GCC unroll%> requires an"
41414 " assignment-expression that evaluates to a non-negative"
41415 " integral constant less than %u", USHRT_MAX);
41416 unroll = 0;
41418 else
41420 unroll = (unsigned short)lunroll;
41421 if (unroll == 0)
41422 unroll = 1;
41424 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41425 return unroll;
41428 /* Normal parsing of a pragma token. Here we can (and must) use the
41429 regular lexer. */
41431 static bool
41432 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
41434 cp_token *pragma_tok;
41435 unsigned int id;
41436 tree stmt;
41437 bool ret;
41439 pragma_tok = cp_lexer_consume_token (parser->lexer);
41440 gcc_assert (pragma_tok->type == CPP_PRAGMA);
41441 parser->lexer->in_pragma = true;
41443 id = cp_parser_pragma_kind (pragma_tok);
41444 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
41445 cp_ensure_no_omp_declare_simd (parser);
41446 switch (id)
41448 case PRAGMA_GCC_PCH_PREPROCESS:
41449 error_at (pragma_tok->location,
41450 "%<#pragma GCC pch_preprocess%> must be first");
41451 break;
41453 case PRAGMA_OMP_BARRIER:
41454 switch (context)
41456 case pragma_compound:
41457 cp_parser_omp_barrier (parser, pragma_tok);
41458 return false;
41459 case pragma_stmt:
41460 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
41461 "used in compound statements", "omp barrier");
41462 break;
41463 default:
41464 goto bad_stmt;
41466 break;
41468 case PRAGMA_OMP_DEPOBJ:
41469 switch (context)
41471 case pragma_compound:
41472 cp_parser_omp_depobj (parser, pragma_tok);
41473 return false;
41474 case pragma_stmt:
41475 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
41476 "used in compound statements", "omp depobj");
41477 break;
41478 default:
41479 goto bad_stmt;
41481 break;
41483 case PRAGMA_OMP_FLUSH:
41484 switch (context)
41486 case pragma_compound:
41487 cp_parser_omp_flush (parser, pragma_tok);
41488 return false;
41489 case pragma_stmt:
41490 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
41491 "used in compound statements", "omp flush");
41492 break;
41493 default:
41494 goto bad_stmt;
41496 break;
41498 case PRAGMA_OMP_TASKWAIT:
41499 switch (context)
41501 case pragma_compound:
41502 cp_parser_omp_taskwait (parser, pragma_tok);
41503 return false;
41504 case pragma_stmt:
41505 error_at (pragma_tok->location,
41506 "%<#pragma %s%> may only be used in compound statements",
41507 "omp taskwait");
41508 break;
41509 default:
41510 goto bad_stmt;
41512 break;
41514 case PRAGMA_OMP_TASKYIELD:
41515 switch (context)
41517 case pragma_compound:
41518 cp_parser_omp_taskyield (parser, pragma_tok);
41519 return false;
41520 case pragma_stmt:
41521 error_at (pragma_tok->location,
41522 "%<#pragma %s%> may only be used in compound statements",
41523 "omp taskyield");
41524 break;
41525 default:
41526 goto bad_stmt;
41528 break;
41530 case PRAGMA_OMP_CANCEL:
41531 switch (context)
41533 case pragma_compound:
41534 cp_parser_omp_cancel (parser, pragma_tok);
41535 return false;
41536 case pragma_stmt:
41537 error_at (pragma_tok->location,
41538 "%<#pragma %s%> may only be used in compound statements",
41539 "omp cancel");
41540 break;
41541 default:
41542 goto bad_stmt;
41544 break;
41546 case PRAGMA_OMP_CANCELLATION_POINT:
41547 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
41548 return false;
41550 case PRAGMA_OMP_THREADPRIVATE:
41551 cp_parser_omp_threadprivate (parser, pragma_tok);
41552 return false;
41554 case PRAGMA_OMP_DECLARE:
41555 return cp_parser_omp_declare (parser, pragma_tok, context);
41557 case PRAGMA_OACC_DECLARE:
41558 cp_parser_oacc_declare (parser, pragma_tok);
41559 return false;
41561 case PRAGMA_OACC_ENTER_DATA:
41562 if (context == pragma_stmt)
41564 error_at (pragma_tok->location,
41565 "%<#pragma %s%> may only be used in compound statements",
41566 "acc enter data");
41567 break;
41569 else if (context != pragma_compound)
41570 goto bad_stmt;
41571 cp_parser_omp_construct (parser, pragma_tok, if_p);
41572 return true;
41574 case PRAGMA_OACC_EXIT_DATA:
41575 if (context == pragma_stmt)
41577 error_at (pragma_tok->location,
41578 "%<#pragma %s%> may only be used in compound statements",
41579 "acc exit data");
41580 break;
41582 else if (context != pragma_compound)
41583 goto bad_stmt;
41584 cp_parser_omp_construct (parser, pragma_tok, if_p);
41585 return true;
41587 case PRAGMA_OACC_ROUTINE:
41588 if (context != pragma_external)
41590 error_at (pragma_tok->location,
41591 "%<#pragma acc routine%> must be at file scope");
41592 break;
41594 cp_parser_oacc_routine (parser, pragma_tok, context);
41595 return false;
41597 case PRAGMA_OACC_UPDATE:
41598 if (context == pragma_stmt)
41600 error_at (pragma_tok->location,
41601 "%<#pragma %s%> may only be used in compound statements",
41602 "acc update");
41603 break;
41605 else if (context != pragma_compound)
41606 goto bad_stmt;
41607 cp_parser_omp_construct (parser, pragma_tok, if_p);
41608 return true;
41610 case PRAGMA_OACC_WAIT:
41611 if (context == pragma_stmt)
41613 error_at (pragma_tok->location,
41614 "%<#pragma %s%> may only be used in compound statements",
41615 "acc wait");
41616 break;
41618 else if (context != pragma_compound)
41619 goto bad_stmt;
41620 cp_parser_omp_construct (parser, pragma_tok, if_p);
41621 return true;
41623 case PRAGMA_OACC_ATOMIC:
41624 case PRAGMA_OACC_CACHE:
41625 case PRAGMA_OACC_DATA:
41626 case PRAGMA_OACC_HOST_DATA:
41627 case PRAGMA_OACC_KERNELS:
41628 case PRAGMA_OACC_PARALLEL:
41629 case PRAGMA_OACC_LOOP:
41630 case PRAGMA_OMP_ATOMIC:
41631 case PRAGMA_OMP_CRITICAL:
41632 case PRAGMA_OMP_DISTRIBUTE:
41633 case PRAGMA_OMP_FOR:
41634 case PRAGMA_OMP_LOOP:
41635 case PRAGMA_OMP_MASTER:
41636 case PRAGMA_OMP_PARALLEL:
41637 case PRAGMA_OMP_SECTIONS:
41638 case PRAGMA_OMP_SIMD:
41639 case PRAGMA_OMP_SINGLE:
41640 case PRAGMA_OMP_TASK:
41641 case PRAGMA_OMP_TASKGROUP:
41642 case PRAGMA_OMP_TASKLOOP:
41643 case PRAGMA_OMP_TEAMS:
41644 if (context != pragma_stmt && context != pragma_compound)
41645 goto bad_stmt;
41646 stmt = push_omp_privatization_clauses (false);
41647 cp_parser_omp_construct (parser, pragma_tok, if_p);
41648 pop_omp_privatization_clauses (stmt);
41649 return true;
41651 case PRAGMA_OMP_REQUIRES:
41652 return cp_parser_omp_requires (parser, pragma_tok);
41654 case PRAGMA_OMP_ORDERED:
41655 if (context != pragma_stmt && context != pragma_compound)
41656 goto bad_stmt;
41657 stmt = push_omp_privatization_clauses (false);
41658 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
41659 pop_omp_privatization_clauses (stmt);
41660 return ret;
41662 case PRAGMA_OMP_TARGET:
41663 if (context != pragma_stmt && context != pragma_compound)
41664 goto bad_stmt;
41665 stmt = push_omp_privatization_clauses (false);
41666 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
41667 pop_omp_privatization_clauses (stmt);
41668 return ret;
41670 case PRAGMA_OMP_END_DECLARE_TARGET:
41671 cp_parser_omp_end_declare_target (parser, pragma_tok);
41672 return false;
41674 case PRAGMA_OMP_SCAN:
41675 error_at (pragma_tok->location,
41676 "%<#pragma omp scan%> may only be used in "
41677 "a loop construct with %<inscan%> %<reduction%> clause");
41678 break;
41680 case PRAGMA_OMP_SECTION:
41681 error_at (pragma_tok->location,
41682 "%<#pragma omp section%> may only be used in "
41683 "%<#pragma omp sections%> construct");
41684 break;
41686 case PRAGMA_IVDEP:
41688 if (context == pragma_external)
41690 error_at (pragma_tok->location,
41691 "%<#pragma GCC ivdep%> must be inside a function");
41692 break;
41694 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
41695 unsigned short unroll;
41696 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41697 if (tok->type == CPP_PRAGMA
41698 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
41700 tok = cp_lexer_consume_token (parser->lexer);
41701 unroll = cp_parser_pragma_unroll (parser, tok);
41702 tok = cp_lexer_peek_token (the_parser->lexer);
41704 else
41705 unroll = 0;
41706 if (tok->type != CPP_KEYWORD
41707 || (tok->keyword != RID_FOR
41708 && tok->keyword != RID_WHILE
41709 && tok->keyword != RID_DO))
41711 cp_parser_error (parser, "for, while or do statement expected");
41712 return false;
41714 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41715 return true;
41718 case PRAGMA_UNROLL:
41720 if (context == pragma_external)
41722 error_at (pragma_tok->location,
41723 "%<#pragma GCC unroll%> must be inside a function");
41724 break;
41726 const unsigned short unroll
41727 = cp_parser_pragma_unroll (parser, pragma_tok);
41728 bool ivdep;
41729 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41730 if (tok->type == CPP_PRAGMA
41731 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
41733 tok = cp_lexer_consume_token (parser->lexer);
41734 ivdep = cp_parser_pragma_ivdep (parser, tok);
41735 tok = cp_lexer_peek_token (the_parser->lexer);
41737 else
41738 ivdep = false;
41739 if (tok->type != CPP_KEYWORD
41740 || (tok->keyword != RID_FOR
41741 && tok->keyword != RID_WHILE
41742 && tok->keyword != RID_DO))
41744 cp_parser_error (parser, "for, while or do statement expected");
41745 return false;
41747 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41748 return true;
41751 default:
41752 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41753 c_invoke_pragma_handler (id);
41754 break;
41756 bad_stmt:
41757 cp_parser_error (parser, "expected declaration specifiers");
41758 break;
41761 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41762 return false;
41765 /* The interface the pragma parsers have to the lexer. */
41767 enum cpp_ttype
41768 pragma_lex (tree *value, location_t *loc)
41770 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41771 enum cpp_ttype ret = tok->type;
41773 *value = tok->u.value;
41774 if (loc)
41775 *loc = tok->location;
41777 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41778 ret = CPP_EOF;
41779 else if (ret == CPP_STRING)
41780 *value = cp_parser_string_literal (the_parser, false, false);
41781 else
41783 if (ret == CPP_KEYWORD)
41784 ret = CPP_NAME;
41785 cp_lexer_consume_token (the_parser->lexer);
41788 return ret;
41792 /* External interface. */
41794 /* Parse one entire translation unit. */
41796 void
41797 c_parse_file (void)
41799 static bool already_called = false;
41801 if (already_called)
41802 fatal_error (input_location,
41803 "inter-module optimizations not implemented for C++");
41804 already_called = true;
41806 the_parser = cp_parser_new ();
41807 push_deferring_access_checks (flag_access_control
41808 ? dk_no_deferred : dk_no_check);
41809 cp_parser_translation_unit (the_parser);
41810 the_parser = NULL;
41812 finish_translation_unit ();
41815 /* Create an identifier for a generic parameter type (a synthesized
41816 template parameter implied by `auto' or a concept identifier). */
41818 static GTY(()) int generic_parm_count;
41819 static tree
41820 make_generic_type_name ()
41822 char buf[32];
41823 sprintf (buf, "auto:%d", ++generic_parm_count);
41824 return get_identifier (buf);
41827 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41828 (creating a new template parameter list if necessary). Returns the newly
41829 created template type parm. */
41831 static tree
41832 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41834 gcc_assert (current_binding_level->kind == sk_function_parms);
41836 /* Before committing to modifying any scope, if we're in an
41837 implicit template scope, and we're trying to synthesize a
41838 constrained parameter, try to find a previous parameter with
41839 the same name. This is the same-type rule for abbreviated
41840 function templates.
41842 NOTE: We can generate implicit parameters when tentatively
41843 parsing a nested name specifier, only to reject that parse
41844 later. However, matching the same template-id as part of a
41845 direct-declarator should generate an identical template
41846 parameter, so this rule will merge them. */
41847 if (parser->implicit_template_scope && constr)
41849 tree t = parser->implicit_template_parms;
41850 while (t)
41852 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41854 tree d = TREE_VALUE (t);
41855 if (TREE_CODE (d) == PARM_DECL)
41856 /* Return the TEMPLATE_PARM_INDEX. */
41857 d = DECL_INITIAL (d);
41858 return d;
41860 t = TREE_CHAIN (t);
41864 /* We are either continuing a function template that already contains implicit
41865 template parameters, creating a new fully-implicit function template, or
41866 extending an existing explicit function template with implicit template
41867 parameters. */
41869 cp_binding_level *const entry_scope = current_binding_level;
41871 bool become_template = false;
41872 cp_binding_level *parent_scope = 0;
41874 if (parser->implicit_template_scope)
41876 gcc_assert (parser->implicit_template_parms);
41878 current_binding_level = parser->implicit_template_scope;
41880 else
41882 /* Roll back to the existing template parameter scope (in the case of
41883 extending an explicit function template) or introduce a new template
41884 parameter scope ahead of the function parameter scope (or class scope
41885 in the case of out-of-line member definitions). The function scope is
41886 added back after template parameter synthesis below. */
41888 cp_binding_level *scope = entry_scope;
41890 while (scope->kind == sk_function_parms)
41892 parent_scope = scope;
41893 scope = scope->level_chain;
41895 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41897 /* If not defining a class, then any class scope is a scope level in
41898 an out-of-line member definition. In this case simply wind back
41899 beyond the first such scope to inject the template parameter list.
41900 Otherwise wind back to the class being defined. The latter can
41901 occur in class member friend declarations such as:
41903 class A {
41904 void foo (auto);
41906 class B {
41907 friend void A::foo (auto);
41910 The template parameter list synthesized for the friend declaration
41911 must be injected in the scope of 'B'. This can also occur in
41912 erroneous cases such as:
41914 struct A {
41915 struct B {
41916 void foo (auto);
41918 void B::foo (auto) {}
41921 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41922 but, nevertheless, the template parameter list synthesized for the
41923 declarator should be injected into the scope of 'A' as if the
41924 ill-formed template was specified explicitly. */
41926 while (scope->kind == sk_class && !scope->defining_class_p)
41928 parent_scope = scope;
41929 scope = scope->level_chain;
41933 current_binding_level = scope;
41935 if (scope->kind != sk_template_parms
41936 || !function_being_declared_is_template_p (parser))
41938 /* Introduce a new template parameter list for implicit template
41939 parameters. */
41941 become_template = true;
41943 parser->implicit_template_scope
41944 = begin_scope (sk_template_parms, NULL);
41946 ++processing_template_decl;
41948 parser->fully_implicit_function_template_p = true;
41949 ++parser->num_template_parameter_lists;
41951 else
41953 /* Synthesize implicit template parameters at the end of the explicit
41954 template parameter list. */
41956 gcc_assert (current_template_parms);
41958 parser->implicit_template_scope = scope;
41960 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41961 parser->implicit_template_parms
41962 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41966 /* Synthesize a new template parameter and track the current template
41967 parameter chain with implicit_template_parms. */
41969 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41970 tree synth_id = make_generic_type_name ();
41971 tree synth_tmpl_parm;
41972 bool non_type = false;
41974 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41975 synth_tmpl_parm
41976 = finish_template_type_parm (class_type_node, synth_id);
41977 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41978 synth_tmpl_parm
41979 = finish_constrained_template_template_parm (proto, synth_id);
41980 else
41982 synth_tmpl_parm = copy_decl (proto);
41983 DECL_NAME (synth_tmpl_parm) = synth_id;
41984 non_type = true;
41987 // Attach the constraint to the parm before processing.
41988 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41989 TREE_TYPE (node) = constr;
41990 tree new_parm
41991 = process_template_parm (parser->implicit_template_parms,
41992 input_location,
41993 node,
41994 /*non_type=*/non_type,
41995 /*param_pack=*/false);
41997 // Chain the new parameter to the list of implicit parameters.
41998 if (parser->implicit_template_parms)
41999 parser->implicit_template_parms
42000 = TREE_CHAIN (parser->implicit_template_parms);
42001 else
42002 parser->implicit_template_parms = new_parm;
42004 tree new_decl = get_local_decls ();
42005 if (non_type)
42006 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
42007 new_decl = DECL_INITIAL (new_decl);
42009 /* If creating a fully implicit function template, start the new implicit
42010 template parameter list with this synthesized type, otherwise grow the
42011 current template parameter list. */
42013 if (become_template)
42015 parent_scope->level_chain = current_binding_level;
42017 tree new_parms = make_tree_vec (1);
42018 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
42019 current_template_parms = tree_cons (size_int (processing_template_decl),
42020 new_parms, current_template_parms);
42022 else
42024 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
42025 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
42026 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
42027 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
42030 // If the new parameter was constrained, we need to add that to the
42031 // constraints in the template parameter list.
42032 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
42034 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
42035 reqs = conjoin_constraints (reqs, req);
42036 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
42039 current_binding_level = entry_scope;
42041 return new_decl;
42044 /* Finish the declaration of a fully implicit function template. Such a
42045 template has no explicit template parameter list so has not been through the
42046 normal template head and tail processing. synthesize_implicit_template_parm
42047 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
42048 provided if the declaration is a class member such that its template
42049 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
42050 form is returned. Otherwise NULL_TREE is returned. */
42052 static tree
42053 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
42055 gcc_assert (parser->fully_implicit_function_template_p);
42057 if (member_decl_opt && member_decl_opt != error_mark_node
42058 && DECL_VIRTUAL_P (member_decl_opt))
42060 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
42061 "implicit templates may not be %<virtual%>");
42062 DECL_VIRTUAL_P (member_decl_opt) = false;
42065 if (member_decl_opt)
42066 member_decl_opt = finish_member_template_decl (member_decl_opt);
42067 end_template_decl ();
42069 parser->fully_implicit_function_template_p = false;
42070 parser->implicit_template_parms = 0;
42071 parser->implicit_template_scope = 0;
42072 --parser->num_template_parameter_lists;
42074 return member_decl_opt;
42077 /* Like finish_fully_implicit_template, but to be used in error
42078 recovery, rearranging scopes so that we restore the state we had
42079 before synthesize_implicit_template_parm inserted the implement
42080 template parms scope. */
42082 static void
42083 abort_fully_implicit_template (cp_parser *parser)
42085 cp_binding_level *return_to_scope = current_binding_level;
42087 if (parser->implicit_template_scope
42088 && return_to_scope != parser->implicit_template_scope)
42090 cp_binding_level *child = return_to_scope;
42091 for (cp_binding_level *scope = child->level_chain;
42092 scope != parser->implicit_template_scope;
42093 scope = child->level_chain)
42094 child = scope;
42095 child->level_chain = parser->implicit_template_scope->level_chain;
42096 parser->implicit_template_scope->level_chain = return_to_scope;
42097 current_binding_level = parser->implicit_template_scope;
42099 else
42100 return_to_scope = return_to_scope->level_chain;
42102 finish_fully_implicit_template (parser, NULL);
42104 gcc_assert (current_binding_level == return_to_scope);
42107 /* Helper function for diagnostics that have complained about things
42108 being used with 'extern "C"' linkage.
42110 Attempt to issue a note showing where the 'extern "C"' linkage began. */
42112 void
42113 maybe_show_extern_c_location (void)
42115 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
42116 inform (the_parser->innermost_linkage_specification_location,
42117 "%<extern \"C\"%> linkage started here");
42120 #include "gt-cp-parser.h"