P0428R2 - familiar template syntax for generic lambdas
[official-gcc.git] / gcc / cp / parser.c
blobb06170482097740901ce8dac6103a08ec0a7bb31
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 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 "cp-cilkplus.h"
45 #include "gcc-rich-location.h"
46 #include "tree-iterator.h"
47 #include "c-family/name-hint.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 *);
251 static void cp_parser_initial_pragma
252 (cp_token *);
254 static void cp_parser_cilk_simd
255 (cp_parser *, cp_token *, bool *);
256 static tree cp_parser_cilk_for
257 (cp_parser *, tree, bool *);
258 static bool cp_parser_omp_declare_reduction_exprs
259 (tree, cp_parser *);
260 static tree cp_parser_cilk_simd_vectorlength
261 (cp_parser *, tree, bool);
262 static void cp_finalize_oacc_routine
263 (cp_parser *, tree, bool);
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
269 /* Variables. */
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream;
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand;
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
284 static void
285 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
286 cp_token *start_token, unsigned num,
287 cp_token *curr_token)
289 unsigned i, nprinted;
290 cp_token *token;
291 bool do_print;
293 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295 if (buffer == NULL)
296 return;
298 if (num == 0)
299 num = buffer->length ();
301 if (start_token == NULL)
302 start_token = buffer->address ();
304 if (start_token > buffer->address ())
306 cp_lexer_print_token (file, &(*buffer)[0]);
307 fprintf (file, " ... ");
310 do_print = false;
311 nprinted = 0;
312 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314 if (token == start_token)
315 do_print = true;
317 if (!do_print)
318 continue;
320 nprinted++;
321 if (token == curr_token)
322 fprintf (file, "[[");
324 cp_lexer_print_token (file, token);
326 if (token == curr_token)
327 fprintf (file, "]]");
329 switch (token->type)
331 case CPP_SEMICOLON:
332 case CPP_OPEN_BRACE:
333 case CPP_CLOSE_BRACE:
334 case CPP_EOF:
335 fputc ('\n', file);
336 break;
338 default:
339 fputc (' ', file);
343 if (i == num && i < buffer->length ())
345 fprintf (file, " ... ");
346 cp_lexer_print_token (file, &buffer->last ());
349 fprintf (file, "\n");
353 /* Dump all tokens in BUFFER to stderr. */
355 void
356 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> &ref)
364 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
367 DEBUG_FUNCTION void
368 debug (vec<cp_token, va_gc> *ptr)
370 if (ptr)
371 debug (*ptr);
372 else
373 fprintf (stderr, "<nil>\n");
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
380 static void
381 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 if (t)
385 fprintf (file, "%s: ", desc);
386 print_node_brief (file, "", t, 0);
391 /* Dump parser context C to FILE. */
393 static void
394 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
398 print_node_brief (file, "", c->object_type, 0);
399 fprintf (file, "}\n");
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
405 static void
406 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 unsigned i;
409 cp_parser_context *c;
411 fprintf (file, "Parsing context stack:\n");
412 for (i = 0, c = first; c; c = c->next, i++)
414 fprintf (file, "\t#%u: ", i);
415 cp_debug_print_context (file, c);
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
422 static void
423 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 if (flag)
426 fprintf (file, "%s: true\n", desc);
430 /* Print an unparsed function entry UF to FILE. */
432 static void
433 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 unsigned i;
436 cp_default_arg_entry *default_arg_fn;
437 tree fn;
439 fprintf (file, "\tFunctions with default args:\n");
440 for (i = 0;
441 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
442 i++)
444 fprintf (file, "\t\tClass type: ");
445 print_node_brief (file, "", default_arg_fn->class_type, 0);
446 fprintf (file, "\t\tDeclaration: ");
447 print_node_brief (file, "", default_arg_fn->decl, 0);
448 fprintf (file, "\n");
451 fprintf (file, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455 print_node_brief (file, "", fn, 0);
456 fprintf (file, " ");
458 fprintf (file, "\n");
460 fprintf (file, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464 print_node_brief (file, "", fn, 0);
465 fprintf (file, " ");
467 fprintf (file, "\n");
471 /* Print the stack of unparsed member functions S to FILE. */
473 static void
474 cp_debug_print_unparsed_queues (FILE *file,
475 vec<cp_unparsed_functions_entry, va_gc> *s)
477 unsigned i;
478 cp_unparsed_functions_entry *uf;
480 fprintf (file, "Unparsed functions\n");
481 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483 fprintf (file, "#%u:\n", i);
484 cp_debug_print_unparsed_function (file, uf);
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
492 static void
493 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 cp_token *next_token, *first_token, *start_token;
497 if (file == NULL)
498 file = stderr;
500 next_token = parser->lexer->next_token;
501 first_token = parser->lexer->buffer->address ();
502 start_token = (next_token > first_token + window_size / 2)
503 ? next_token - window_size / 2
504 : first_token;
505 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
506 next_token);
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
513 void
514 cp_debug_parser (FILE *file, cp_parser *parser)
516 const size_t window_size = 20;
517 cp_token *token;
518 expanded_location eloc;
520 if (file == NULL)
521 file = stderr;
523 fprintf (file, "Parser state\n\n");
524 fprintf (file, "Number of tokens: %u\n",
525 vec_safe_length (parser->lexer->buffer));
526 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
527 cp_debug_print_tree_if_set (file, "Object scope",
528 parser->object_scope);
529 cp_debug_print_tree_if_set (file, "Qualifying scope",
530 parser->qualifying_scope);
531 cp_debug_print_context_stack (file, parser->context);
532 cp_debug_print_flag (file, "Allow GNU extensions",
533 parser->allow_gnu_extensions_p);
534 cp_debug_print_flag (file, "'>' token is greater-than",
535 parser->greater_than_is_operator_p);
536 cp_debug_print_flag (file, "Default args allowed in current "
537 "parameter list", parser->default_arg_ok_p);
538 cp_debug_print_flag (file, "Parsing integral constant-expression",
539 parser->integral_constant_expression_p);
540 cp_debug_print_flag (file, "Allow non-constant expression in current "
541 "constant-expression",
542 parser->allow_non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Seen non-constant expression",
544 parser->non_integral_constant_expression_p);
545 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
546 "current context",
547 parser->local_variables_forbidden_p);
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
561 parser->in_statement & IN_CILK_SIMD_FOR);
562 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
569 parser->implicit_extern_c);
570 cp_debug_print_flag (file, "String expressions should be translated "
571 "to execution character set",
572 parser->translate_strings_p);
573 cp_debug_print_flag (file, "Parsing function body outside of a "
574 "local class", parser->in_function_body);
575 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
576 parser->colon_corrects_to_scope_p);
577 cp_debug_print_flag (file, "Colon doesn't start a class definition",
578 parser->colon_doesnt_start_class_def_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s\n",
581 parser->type_definition_forbidden_message);
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
601 cp_debug_parser (stderr, &ref);
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
615 static cp_lexer *
616 cp_lexer_alloc (void)
618 cp_lexer *lexer;
620 c_common_no_more_pch ();
622 /* Allocate the memory. */
623 lexer = ggc_cleared_alloc<cp_lexer> ();
625 /* Initially we are not debugging. */
626 lexer->debugging_p = false;
628 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630 /* Create the buffer. */
631 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633 return lexer;
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
638 preprocessor. */
640 static cp_lexer *
641 cp_lexer_new_main (void)
643 cp_lexer *lexer;
644 cp_token token;
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token);
651 lexer = cp_lexer_alloc ();
653 /* Put the first token in the buffer. */
654 lexer->buffer->quick_push (token);
656 /* Get the remaining tokens from the preprocessor. */
657 while (token.type != CPP_EOF)
659 cp_lexer_get_preprocessor_token (lexer, &token);
660 vec_safe_push (lexer->buffer, token);
663 lexer->last_token = lexer->buffer->address ()
664 + lexer->buffer->length ()
665 - 1;
666 lexer->next_token = lexer->buffer->length ()
667 ? lexer->buffer->address ()
668 : &eof_token;
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
672 done_lexing = true;
674 gcc_assert (!lexer->next_token->purged_p);
675 return lexer;
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
681 static cp_lexer *
682 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 cp_token *first = cache->first;
685 cp_token *last = cache->last;
686 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688 /* We do not own the buffer. */
689 lexer->buffer = NULL;
690 lexer->next_token = first == last ? &eof_token : first;
691 lexer->last_token = last;
693 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695 /* Initially we are not debugging. */
696 lexer->debugging_p = false;
698 gcc_assert (!lexer->next_token->purged_p);
699 return lexer;
702 /* Frees all resources associated with LEXER. */
704 static void
705 cp_lexer_destroy (cp_lexer *lexer)
707 vec_free (lexer->buffer);
708 lexer->saved_tokens.release ();
709 ggc_free (lexer);
712 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
713 be used. The point of this flag is to help the compiler to fold away calls
714 to cp_lexer_debugging_p within this source file at compile time, when the
715 lexer is not being debugged. */
717 #define LEXER_DEBUGGING_ENABLED_P false
719 /* Returns nonzero if debugging information should be output. */
721 static inline bool
722 cp_lexer_debugging_p (cp_lexer *lexer)
724 if (!LEXER_DEBUGGING_ENABLED_P)
725 return false;
727 return lexer->debugging_p;
731 static inline cp_token_position
732 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
734 gcc_assert (!previous_p || lexer->next_token != &eof_token);
736 return lexer->next_token - previous_p;
739 static inline cp_token *
740 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
742 return pos;
745 static inline void
746 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
748 lexer->next_token = cp_lexer_token_at (lexer, pos);
751 static inline cp_token_position
752 cp_lexer_previous_token_position (cp_lexer *lexer)
754 if (lexer->next_token == &eof_token)
755 return lexer->last_token - 1;
756 else
757 return cp_lexer_token_position (lexer, true);
760 static inline cp_token *
761 cp_lexer_previous_token (cp_lexer *lexer)
763 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765 /* Skip past purged tokens. */
766 while (tp->purged_p)
768 gcc_assert (tp != vec_safe_address (lexer->buffer));
769 tp--;
772 return cp_lexer_token_at (lexer, tp);
775 /* nonzero if we are presently saving tokens. */
777 static inline int
778 cp_lexer_saving_tokens (const cp_lexer* lexer)
780 return lexer->saved_tokens.length () != 0;
783 /* Store the next token from the preprocessor in *TOKEN. Return true
784 if we reach EOF. If LEXER is NULL, assume we are handling an
785 initial #pragma pch_preprocess, and thus want the lexer to return
786 processed strings. */
788 static void
789 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
791 static int is_extern_c = 0;
793 /* Get a new token from the preprocessor. */
794 token->type
795 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
796 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
797 token->keyword = RID_MAX;
798 token->purged_p = false;
799 token->error_reported = false;
801 /* On some systems, some header files are surrounded by an
802 implicit extern "C" block. Set a flag in the token if it
803 comes from such a header. */
804 is_extern_c += pending_lang_change;
805 pending_lang_change = 0;
806 token->implicit_extern_c = is_extern_c > 0;
808 /* Check to see if this token is a keyword. */
809 if (token->type == CPP_NAME)
811 if (IDENTIFIER_KEYWORD_P (token->u.value))
813 /* Mark this token as a keyword. */
814 token->type = CPP_KEYWORD;
815 /* Record which keyword. */
816 token->keyword = C_RID_CODE (token->u.value);
818 else
820 if (warn_cxx11_compat
821 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
822 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
824 /* Warn about the C++0x keyword (but still treat it as
825 an identifier). */
826 warning (OPT_Wc__11_compat,
827 "identifier %qE is a keyword in C++11",
828 token->u.value);
830 /* Clear out the C_RID_CODE so we don't warn about this
831 particular identifier-turned-keyword again. */
832 C_SET_RID_CODE (token->u.value, RID_MAX);
835 token->keyword = RID_MAX;
838 else if (token->type == CPP_AT_NAME)
840 /* This only happens in Objective-C++; it must be a keyword. */
841 token->type = CPP_KEYWORD;
842 switch (C_RID_CODE (token->u.value))
844 /* Replace 'class' with '@class', 'private' with '@private',
845 etc. This prevents confusion with the C++ keyword
846 'class', and makes the tokens consistent with other
847 Objective-C 'AT' keywords. For example '@class' is
848 reported as RID_AT_CLASS which is consistent with
849 '@synchronized', which is reported as
850 RID_AT_SYNCHRONIZED.
852 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
853 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
854 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
855 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
856 case RID_THROW: token->keyword = RID_AT_THROW; break;
857 case RID_TRY: token->keyword = RID_AT_TRY; break;
858 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
859 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
860 default: token->keyword = C_RID_CODE (token->u.value);
865 /* Update the globals input_location and the input file stack from TOKEN. */
866 static inline void
867 cp_lexer_set_source_position_from_token (cp_token *token)
869 if (token->type != CPP_EOF)
871 input_location = token->location;
875 /* Update the globals input_location and the input file stack from LEXER. */
876 static inline void
877 cp_lexer_set_source_position (cp_lexer *lexer)
879 cp_token *token = cp_lexer_peek_token (lexer);
880 cp_lexer_set_source_position_from_token (token);
883 /* Return a pointer to the next token in the token stream, but do not
884 consume it. */
886 static inline cp_token *
887 cp_lexer_peek_token (cp_lexer *lexer)
889 if (cp_lexer_debugging_p (lexer))
891 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
892 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
893 putc ('\n', cp_lexer_debug_stream);
895 return lexer->next_token;
898 /* Return true if the next token has the indicated TYPE. */
900 static inline bool
901 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
903 return cp_lexer_peek_token (lexer)->type == type;
906 /* Return true if the next token does not have the indicated TYPE. */
908 static inline bool
909 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
911 return !cp_lexer_next_token_is (lexer, type);
914 /* Return true if the next token is the indicated KEYWORD. */
916 static inline bool
917 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
919 return cp_lexer_peek_token (lexer)->keyword == keyword;
922 static inline bool
923 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
925 return cp_lexer_peek_nth_token (lexer, n)->type == type;
928 static inline bool
929 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
931 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
934 /* Return true if the next token is not the indicated KEYWORD. */
936 static inline bool
937 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
939 return cp_lexer_peek_token (lexer)->keyword != keyword;
942 /* Return true if KEYWORD can start a decl-specifier. */
944 bool
945 cp_keyword_starts_decl_specifier_p (enum rid keyword)
947 switch (keyword)
949 /* auto specifier: storage-class-specifier in C++,
950 simple-type-specifier in C++0x. */
951 case RID_AUTO:
952 /* Storage classes. */
953 case RID_REGISTER:
954 case RID_STATIC:
955 case RID_EXTERN:
956 case RID_MUTABLE:
957 case RID_THREAD:
958 /* Elaborated type specifiers. */
959 case RID_ENUM:
960 case RID_CLASS:
961 case RID_STRUCT:
962 case RID_UNION:
963 case RID_TYPENAME:
964 /* Simple type specifiers. */
965 case RID_CHAR:
966 case RID_CHAR16:
967 case RID_CHAR32:
968 case RID_WCHAR:
969 case RID_BOOL:
970 case RID_SHORT:
971 case RID_INT:
972 case RID_LONG:
973 case RID_SIGNED:
974 case RID_UNSIGNED:
975 case RID_FLOAT:
976 case RID_DOUBLE:
977 case RID_VOID:
978 /* GNU extensions. */
979 case RID_ATTRIBUTE:
980 case RID_TYPEOF:
981 /* C++0x extensions. */
982 case RID_DECLTYPE:
983 case RID_UNDERLYING_TYPE:
984 case RID_CONSTEXPR:
985 return true;
987 default:
988 if (keyword >= RID_FIRST_INT_N
989 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
996 /* Return true if the next token is a keyword for a decl-specifier. */
998 static bool
999 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1001 cp_token *token;
1003 token = cp_lexer_peek_token (lexer);
1004 return cp_keyword_starts_decl_specifier_p (token->keyword);
1007 /* Returns TRUE iff the token T begins a decltype type. */
1009 static bool
1010 token_is_decltype (cp_token *t)
1012 return (t->keyword == RID_DECLTYPE
1013 || t->type == CPP_DECLTYPE);
1016 /* Returns TRUE iff the next token begins a decltype type. */
1018 static bool
1019 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1021 cp_token *t = cp_lexer_peek_token (lexer);
1022 return token_is_decltype (t);
1025 /* Called when processing a token with tree_check_value; perform or defer the
1026 associated checks and return the value. */
1028 static tree
1029 saved_checks_value (struct tree_check *check_value)
1031 /* Perform any access checks that were deferred. */
1032 vec<deferred_access_check, va_gc> *checks;
1033 deferred_access_check *chk;
1034 checks = check_value->checks;
1035 if (checks)
1037 int i;
1038 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1039 perform_or_defer_access_check (chk->binfo,
1040 chk->decl,
1041 chk->diag_decl, tf_warning_or_error);
1043 /* Return the stored value. */
1044 return check_value->value;
1047 /* Return a pointer to the Nth token in the token stream. If N is 1,
1048 then this is precisely equivalent to cp_lexer_peek_token (except
1049 that it is not inline). One would like to disallow that case, but
1050 there is one case (cp_parser_nth_token_starts_template_id) where
1051 the caller passes a variable for N and it might be 1. */
1053 static cp_token *
1054 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1056 cp_token *token;
1058 /* N is 1-based, not zero-based. */
1059 gcc_assert (n > 0);
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream,
1063 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1065 --n;
1066 token = lexer->next_token;
1067 gcc_assert (!n || token != &eof_token);
1068 while (n != 0)
1070 ++token;
1071 if (token == lexer->last_token)
1073 token = &eof_token;
1074 break;
1077 if (!token->purged_p)
1078 --n;
1081 if (cp_lexer_debugging_p (lexer))
1083 cp_lexer_print_token (cp_lexer_debug_stream, token);
1084 putc ('\n', cp_lexer_debug_stream);
1087 return token;
1090 /* Return the next token, and advance the lexer's next_token pointer
1091 to point to the next non-purged token. */
1093 static cp_token *
1094 cp_lexer_consume_token (cp_lexer* lexer)
1096 cp_token *token = lexer->next_token;
1098 gcc_assert (token != &eof_token);
1099 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1103 lexer->next_token++;
1104 if (lexer->next_token == lexer->last_token)
1106 lexer->next_token = &eof_token;
1107 break;
1111 while (lexer->next_token->purged_p);
1113 cp_lexer_set_source_position_from_token (token);
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1118 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1119 cp_lexer_print_token (cp_lexer_debug_stream, token);
1120 putc ('\n', cp_lexer_debug_stream);
1123 return token;
1126 /* Permanently remove the next token from the token stream, and
1127 advance the next_token pointer to refer to the next non-purged
1128 token. */
1130 static void
1131 cp_lexer_purge_token (cp_lexer *lexer)
1133 cp_token *tok = lexer->next_token;
1135 gcc_assert (tok != &eof_token);
1136 tok->purged_p = true;
1137 tok->location = UNKNOWN_LOCATION;
1138 tok->u.value = NULL_TREE;
1139 tok->keyword = RID_MAX;
1143 tok++;
1144 if (tok == lexer->last_token)
1146 tok = &eof_token;
1147 break;
1150 while (tok->purged_p);
1151 lexer->next_token = tok;
1154 /* Permanently remove all tokens after TOK, up to, but not
1155 including, the token that will be returned next by
1156 cp_lexer_peek_token. */
1158 static void
1159 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1161 cp_token *peek = lexer->next_token;
1163 if (peek == &eof_token)
1164 peek = lexer->last_token;
1166 gcc_assert (tok < peek);
1168 for ( tok += 1; tok != peek; tok += 1)
1170 tok->purged_p = true;
1171 tok->location = UNKNOWN_LOCATION;
1172 tok->u.value = NULL_TREE;
1173 tok->keyword = RID_MAX;
1177 /* Begin saving tokens. All tokens consumed after this point will be
1178 preserved. */
1180 static void
1181 cp_lexer_save_tokens (cp_lexer* lexer)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1187 lexer->saved_tokens.safe_push (lexer->next_token);
1190 /* Commit to the portion of the token stream most recently saved. */
1192 static void
1193 cp_lexer_commit_tokens (cp_lexer* lexer)
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer))
1197 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1199 lexer->saved_tokens.pop ();
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1205 static void
1206 cp_lexer_rollback_tokens (cp_lexer* lexer)
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer))
1210 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1212 lexer->next_token = lexer->saved_tokens.pop ();
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1218 member function. */
1220 struct saved_token_sentinel
1222 cp_lexer *lexer;
1223 unsigned len;
1224 bool commit;
1225 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1227 len = lexer->saved_tokens.length ();
1228 cp_lexer_save_tokens (lexer);
1230 void rollback ()
1232 cp_lexer_rollback_tokens (lexer);
1233 commit = false;
1235 ~saved_token_sentinel()
1237 if (commit)
1238 cp_lexer_commit_tokens (lexer);
1239 gcc_assert (lexer->saved_tokens.length () == len);
1243 /* Print a representation of the TOKEN on the STREAM. */
1245 static void
1246 cp_lexer_print_token (FILE * stream, cp_token *token)
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1254 TTYPE_TABLE
1255 #undef OP
1256 #undef TK
1257 /* C++ parser token types - see "Manifest constants", above. */
1258 "KEYWORD",
1259 "TEMPLATE_ID",
1260 "NESTED_NAME_SPECIFIER",
1263 /* For some tokens, print the associated data. */
1264 switch (token->type)
1266 case CPP_KEYWORD:
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token->u.value))
1270 break;
1271 /* fall through */
1272 case CPP_NAME:
1273 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1274 break;
1276 case CPP_STRING:
1277 case CPP_STRING16:
1278 case CPP_STRING32:
1279 case CPP_WSTRING:
1280 case CPP_UTF8STRING:
1281 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1282 break;
1284 case CPP_NUMBER:
1285 print_generic_expr (stream, token->u.value);
1286 break;
1288 default:
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token->type < ARRAY_SIZE(token_names))
1292 fputs (token_names[token->type], stream);
1293 else
1294 fprintf (stream, "[%d]", token->type);
1295 break;
1299 DEBUG_FUNCTION void
1300 debug (cp_token &ref)
1302 cp_lexer_print_token (stderr, &ref);
1303 fprintf (stderr, "\n");
1306 DEBUG_FUNCTION void
1307 debug (cp_token *ptr)
1309 if (ptr)
1310 debug (*ptr);
1311 else
1312 fprintf (stderr, "<nil>\n");
1316 /* Start emitting debugging information. */
1318 static void
1319 cp_lexer_start_debugging (cp_lexer* lexer)
1321 if (!LEXER_DEBUGGING_ENABLED_P)
1322 fatal_error (input_location,
1323 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325 lexer->debugging_p = true;
1326 cp_lexer_debug_stream = stderr;
1329 /* Stop emitting debugging information. */
1331 static void
1332 cp_lexer_stop_debugging (cp_lexer* lexer)
1334 if (!LEXER_DEBUGGING_ENABLED_P)
1335 fatal_error (input_location,
1336 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1338 lexer->debugging_p = false;
1339 cp_lexer_debug_stream = NULL;
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1344 static cp_token_cache *
1345 cp_token_cache_new (cp_token *first, cp_token *last)
1347 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1348 cache->first = first;
1349 cache->last = last;
1350 return cache;
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1356 static inline void
1357 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1359 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1361 error ("%<#pragma omp declare simd%> not immediately followed by "
1362 "function declaration or definition");
1363 parser->omp_declare_simd = NULL;
1367 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1368 and put that into "omp declare simd" attribute. */
1370 static inline void
1371 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1373 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1375 if (fndecl == error_mark_node)
1377 parser->omp_declare_simd = NULL;
1378 return;
1380 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1382 cp_ensure_no_omp_declare_simd (parser);
1383 return;
1388 /* Diagnose if #pragma acc routine isn't followed immediately by function
1389 declaration or definition. */
1391 static inline void
1392 cp_ensure_no_oacc_routine (cp_parser *parser)
1394 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1396 error_at (parser->oacc_routine->loc,
1397 "%<#pragma acc routine%> not immediately followed by "
1398 "function declaration or definition");
1399 parser->oacc_routine = NULL;
1403 /* Decl-specifiers. */
1405 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1407 static void
1408 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1410 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1413 /* Declarators. */
1415 /* Nothing other than the parser should be creating declarators;
1416 declarators are a semi-syntactic representation of C++ entities.
1417 Other parts of the front end that need to create entities (like
1418 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1420 static cp_declarator *make_call_declarator
1421 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1422 static cp_declarator *make_array_declarator
1423 (cp_declarator *, tree);
1424 static cp_declarator *make_pointer_declarator
1425 (cp_cv_quals, cp_declarator *, tree);
1426 static cp_declarator *make_reference_declarator
1427 (cp_cv_quals, cp_declarator *, bool, tree);
1428 static cp_declarator *make_ptrmem_declarator
1429 (cp_cv_quals, tree, cp_declarator *, tree);
1431 /* An erroneous declarator. */
1432 static cp_declarator *cp_error_declarator;
1434 /* The obstack on which declarators and related data structures are
1435 allocated. */
1436 static struct obstack declarator_obstack;
1438 /* Alloc BYTES from the declarator memory pool. */
1440 static inline void *
1441 alloc_declarator (size_t bytes)
1443 return obstack_alloc (&declarator_obstack, bytes);
1446 /* Allocate a declarator of the indicated KIND. Clear fields that are
1447 common to all declarators. */
1449 static cp_declarator *
1450 make_declarator (cp_declarator_kind kind)
1452 cp_declarator *declarator;
1454 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1455 declarator->kind = kind;
1456 declarator->parenthesized = UNKNOWN_LOCATION;
1457 declarator->attributes = NULL_TREE;
1458 declarator->std_attributes = NULL_TREE;
1459 declarator->declarator = NULL;
1460 declarator->parameter_pack_p = false;
1461 declarator->id_loc = UNKNOWN_LOCATION;
1463 return declarator;
1466 /* Make a declarator for a generalized identifier. If
1467 QUALIFYING_SCOPE is non-NULL, the identifier is
1468 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1469 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1470 is, if any. */
1472 static cp_declarator *
1473 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1474 special_function_kind sfk)
1476 cp_declarator *declarator;
1478 /* It is valid to write:
1480 class C { void f(); };
1481 typedef C D;
1482 void D::f();
1484 The standard is not clear about whether `typedef const C D' is
1485 legal; as of 2002-09-15 the committee is considering that
1486 question. EDG 3.0 allows that syntax. Therefore, we do as
1487 well. */
1488 if (qualifying_scope && TYPE_P (qualifying_scope))
1489 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1491 gcc_assert (identifier_p (unqualified_name)
1492 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1493 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1495 declarator = make_declarator (cdk_id);
1496 declarator->u.id.qualifying_scope = qualifying_scope;
1497 declarator->u.id.unqualified_name = unqualified_name;
1498 declarator->u.id.sfk = sfk;
1500 return declarator;
1503 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1504 of modifiers such as const or volatile to apply to the pointer
1505 type, represented as identifiers. ATTRIBUTES represent the attributes that
1506 appertain to the pointer or reference. */
1508 cp_declarator *
1509 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1510 tree attributes)
1512 cp_declarator *declarator;
1514 declarator = make_declarator (cdk_pointer);
1515 declarator->declarator = target;
1516 declarator->u.pointer.qualifiers = cv_qualifiers;
1517 declarator->u.pointer.class_type = NULL_TREE;
1518 if (target)
1520 declarator->id_loc = target->id_loc;
1521 declarator->parameter_pack_p = target->parameter_pack_p;
1522 target->parameter_pack_p = false;
1524 else
1525 declarator->parameter_pack_p = false;
1527 declarator->std_attributes = attributes;
1529 return declarator;
1532 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1533 represent the attributes that appertain to the pointer or
1534 reference. */
1536 cp_declarator *
1537 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1538 bool rvalue_ref, tree attributes)
1540 cp_declarator *declarator;
1542 declarator = make_declarator (cdk_reference);
1543 declarator->declarator = target;
1544 declarator->u.reference.qualifiers = cv_qualifiers;
1545 declarator->u.reference.rvalue_ref = rvalue_ref;
1546 if (target)
1548 declarator->id_loc = target->id_loc;
1549 declarator->parameter_pack_p = target->parameter_pack_p;
1550 target->parameter_pack_p = false;
1552 else
1553 declarator->parameter_pack_p = false;
1555 declarator->std_attributes = attributes;
1557 return declarator;
1560 /* Like make_pointer_declarator -- but for a pointer to a non-static
1561 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1562 appertain to the pointer or reference. */
1564 cp_declarator *
1565 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1566 cp_declarator *pointee,
1567 tree attributes)
1569 cp_declarator *declarator;
1571 declarator = make_declarator (cdk_ptrmem);
1572 declarator->declarator = pointee;
1573 declarator->u.pointer.qualifiers = cv_qualifiers;
1574 declarator->u.pointer.class_type = class_type;
1576 if (pointee)
1578 declarator->parameter_pack_p = pointee->parameter_pack_p;
1579 pointee->parameter_pack_p = false;
1581 else
1582 declarator->parameter_pack_p = false;
1584 declarator->std_attributes = attributes;
1586 return declarator;
1589 /* Make a declarator for the function given by TARGET, with the
1590 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1591 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1592 indicates what exceptions can be thrown. */
1594 cp_declarator *
1595 make_call_declarator (cp_declarator *target,
1596 tree parms,
1597 cp_cv_quals cv_qualifiers,
1598 cp_virt_specifiers virt_specifiers,
1599 cp_ref_qualifier ref_qualifier,
1600 tree tx_qualifier,
1601 tree exception_specification,
1602 tree late_return_type,
1603 tree requires_clause)
1605 cp_declarator *declarator;
1607 declarator = make_declarator (cdk_function);
1608 declarator->declarator = target;
1609 declarator->u.function.parameters = parms;
1610 declarator->u.function.qualifiers = cv_qualifiers;
1611 declarator->u.function.virt_specifiers = virt_specifiers;
1612 declarator->u.function.ref_qualifier = ref_qualifier;
1613 declarator->u.function.tx_qualifier = tx_qualifier;
1614 declarator->u.function.exception_specification = exception_specification;
1615 declarator->u.function.late_return_type = late_return_type;
1616 declarator->u.function.requires_clause = requires_clause;
1617 if (target)
1619 declarator->id_loc = target->id_loc;
1620 declarator->parameter_pack_p = target->parameter_pack_p;
1621 target->parameter_pack_p = false;
1623 else
1624 declarator->parameter_pack_p = false;
1626 return declarator;
1629 /* Make a declarator for an array of BOUNDS elements, each of which is
1630 defined by ELEMENT. */
1632 cp_declarator *
1633 make_array_declarator (cp_declarator *element, tree bounds)
1635 cp_declarator *declarator;
1637 declarator = make_declarator (cdk_array);
1638 declarator->declarator = element;
1639 declarator->u.array.bounds = bounds;
1640 if (element)
1642 declarator->id_loc = element->id_loc;
1643 declarator->parameter_pack_p = element->parameter_pack_p;
1644 element->parameter_pack_p = false;
1646 else
1647 declarator->parameter_pack_p = false;
1649 return declarator;
1652 /* Determine whether the declarator we've seen so far can be a
1653 parameter pack, when followed by an ellipsis. */
1654 static bool
1655 declarator_can_be_parameter_pack (cp_declarator *declarator)
1657 if (declarator && declarator->parameter_pack_p)
1658 /* We already saw an ellipsis. */
1659 return false;
1661 /* Search for a declarator name, or any other declarator that goes
1662 after the point where the ellipsis could appear in a parameter
1663 pack. If we find any of these, then this declarator can not be
1664 made into a parameter pack. */
1665 bool found = false;
1666 while (declarator && !found)
1668 switch ((int)declarator->kind)
1670 case cdk_id:
1671 case cdk_array:
1672 case cdk_decomp:
1673 found = true;
1674 break;
1676 case cdk_error:
1677 return true;
1679 default:
1680 declarator = declarator->declarator;
1681 break;
1685 return !found;
1688 cp_parameter_declarator *no_parameters;
1690 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1691 DECLARATOR and DEFAULT_ARGUMENT. */
1693 cp_parameter_declarator *
1694 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1695 cp_declarator *declarator,
1696 tree default_argument,
1697 location_t loc,
1698 bool template_parameter_pack_p = false)
1700 cp_parameter_declarator *parameter;
1702 parameter = ((cp_parameter_declarator *)
1703 alloc_declarator (sizeof (cp_parameter_declarator)));
1704 parameter->next = NULL;
1705 if (decl_specifiers)
1706 parameter->decl_specifiers = *decl_specifiers;
1707 else
1708 clear_decl_specs (&parameter->decl_specifiers);
1709 parameter->declarator = declarator;
1710 parameter->default_argument = default_argument;
1711 parameter->template_parameter_pack_p = template_parameter_pack_p;
1712 parameter->loc = loc;
1714 return parameter;
1717 /* Returns true iff DECLARATOR is a declaration for a function. */
1719 static bool
1720 function_declarator_p (const cp_declarator *declarator)
1722 while (declarator)
1724 if (declarator->kind == cdk_function
1725 && declarator->declarator->kind == cdk_id)
1726 return true;
1727 if (declarator->kind == cdk_id
1728 || declarator->kind == cdk_decomp
1729 || declarator->kind == cdk_error)
1730 return false;
1731 declarator = declarator->declarator;
1733 return false;
1736 /* The parser. */
1738 /* Overview
1739 --------
1741 A cp_parser parses the token stream as specified by the C++
1742 grammar. Its job is purely parsing, not semantic analysis. For
1743 example, the parser breaks the token stream into declarators,
1744 expressions, statements, and other similar syntactic constructs.
1745 It does not check that the types of the expressions on either side
1746 of an assignment-statement are compatible, or that a function is
1747 not declared with a parameter of type `void'.
1749 The parser invokes routines elsewhere in the compiler to perform
1750 semantic analysis and to build up the abstract syntax tree for the
1751 code processed.
1753 The parser (and the template instantiation code, which is, in a
1754 way, a close relative of parsing) are the only parts of the
1755 compiler that should be calling push_scope and pop_scope, or
1756 related functions. The parser (and template instantiation code)
1757 keeps track of what scope is presently active; everything else
1758 should simply honor that. (The code that generates static
1759 initializers may also need to set the scope, in order to check
1760 access control correctly when emitting the initializers.)
1762 Methodology
1763 -----------
1765 The parser is of the standard recursive-descent variety. Upcoming
1766 tokens in the token stream are examined in order to determine which
1767 production to use when parsing a non-terminal. Some C++ constructs
1768 require arbitrary look ahead to disambiguate. For example, it is
1769 impossible, in the general case, to tell whether a statement is an
1770 expression or declaration without scanning the entire statement.
1771 Therefore, the parser is capable of "parsing tentatively." When the
1772 parser is not sure what construct comes next, it enters this mode.
1773 Then, while we attempt to parse the construct, the parser queues up
1774 error messages, rather than issuing them immediately, and saves the
1775 tokens it consumes. If the construct is parsed successfully, the
1776 parser "commits", i.e., it issues any queued error messages and
1777 the tokens that were being preserved are permanently discarded.
1778 If, however, the construct is not parsed successfully, the parser
1779 rolls back its state completely so that it can resume parsing using
1780 a different alternative.
1782 Future Improvements
1783 -------------------
1785 The performance of the parser could probably be improved substantially.
1786 We could often eliminate the need to parse tentatively by looking ahead
1787 a little bit. In some places, this approach might not entirely eliminate
1788 the need to parse tentatively, but it might still speed up the average
1789 case. */
1791 /* Flags that are passed to some parsing functions. These values can
1792 be bitwise-ored together. */
1794 enum
1796 /* No flags. */
1797 CP_PARSER_FLAGS_NONE = 0x0,
1798 /* The construct is optional. If it is not present, then no error
1799 should be issued. */
1800 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1801 /* When parsing a type-specifier, treat user-defined type-names
1802 as non-type identifiers. */
1803 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1804 /* When parsing a type-specifier, do not try to parse a class-specifier
1805 or enum-specifier. */
1806 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1807 /* When parsing a decl-specifier-seq, only allow type-specifier or
1808 constexpr. */
1809 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1810 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1811 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1814 /* This type is used for parameters and variables which hold
1815 combinations of the above flags. */
1816 typedef int cp_parser_flags;
1818 /* The different kinds of declarators we want to parse. */
1820 enum cp_parser_declarator_kind
1822 /* We want an abstract declarator. */
1823 CP_PARSER_DECLARATOR_ABSTRACT,
1824 /* We want a named declarator. */
1825 CP_PARSER_DECLARATOR_NAMED,
1826 /* We don't mind, but the name must be an unqualified-id. */
1827 CP_PARSER_DECLARATOR_EITHER
1830 /* The precedence values used to parse binary expressions. The minimum value
1831 of PREC must be 1, because zero is reserved to quickly discriminate
1832 binary operators from other tokens. */
1834 enum cp_parser_prec
1836 PREC_NOT_OPERATOR,
1837 PREC_LOGICAL_OR_EXPRESSION,
1838 PREC_LOGICAL_AND_EXPRESSION,
1839 PREC_INCLUSIVE_OR_EXPRESSION,
1840 PREC_EXCLUSIVE_OR_EXPRESSION,
1841 PREC_AND_EXPRESSION,
1842 PREC_EQUALITY_EXPRESSION,
1843 PREC_RELATIONAL_EXPRESSION,
1844 PREC_SHIFT_EXPRESSION,
1845 PREC_ADDITIVE_EXPRESSION,
1846 PREC_MULTIPLICATIVE_EXPRESSION,
1847 PREC_PM_EXPRESSION,
1848 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1851 /* A mapping from a token type to a corresponding tree node type, with a
1852 precedence value. */
1854 struct cp_parser_binary_operations_map_node
1856 /* The token type. */
1857 enum cpp_ttype token_type;
1858 /* The corresponding tree code. */
1859 enum tree_code tree_type;
1860 /* The precedence of this operator. */
1861 enum cp_parser_prec prec;
1864 struct cp_parser_expression_stack_entry
1866 /* Left hand side of the binary operation we are currently
1867 parsing. */
1868 cp_expr lhs;
1869 /* Original tree code for left hand side, if it was a binary
1870 expression itself (used for -Wparentheses). */
1871 enum tree_code lhs_type;
1872 /* Tree code for the binary operation we are parsing. */
1873 enum tree_code tree_type;
1874 /* Precedence of the binary operation we are parsing. */
1875 enum cp_parser_prec prec;
1876 /* Location of the binary operation we are parsing. */
1877 location_t loc;
1880 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1881 entries because precedence levels on the stack are monotonically
1882 increasing. */
1883 typedef struct cp_parser_expression_stack_entry
1884 cp_parser_expression_stack[NUM_PREC_VALUES];
1886 /* Prototypes. */
1888 /* Constructors and destructors. */
1890 static cp_parser_context *cp_parser_context_new
1891 (cp_parser_context *);
1893 /* Class variables. */
1895 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1897 /* The operator-precedence table used by cp_parser_binary_expression.
1898 Transformed into an associative array (binops_by_token) by
1899 cp_parser_new. */
1901 static const cp_parser_binary_operations_map_node binops[] = {
1902 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1903 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1905 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1906 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1907 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1909 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1910 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1912 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1913 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1915 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1916 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1917 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1918 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1920 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1921 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1923 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1925 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1927 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1929 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1931 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1934 /* The same as binops, but initialized by cp_parser_new so that
1935 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1936 for speed. */
1937 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1939 /* Constructors and destructors. */
1941 /* Construct a new context. The context below this one on the stack
1942 is given by NEXT. */
1944 static cp_parser_context *
1945 cp_parser_context_new (cp_parser_context* next)
1947 cp_parser_context *context;
1949 /* Allocate the storage. */
1950 if (cp_parser_context_free_list != NULL)
1952 /* Pull the first entry from the free list. */
1953 context = cp_parser_context_free_list;
1954 cp_parser_context_free_list = context->next;
1955 memset (context, 0, sizeof (*context));
1957 else
1958 context = ggc_cleared_alloc<cp_parser_context> ();
1960 /* No errors have occurred yet in this context. */
1961 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1962 /* If this is not the bottommost context, copy information that we
1963 need from the previous context. */
1964 if (next)
1966 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1967 expression, then we are parsing one in this context, too. */
1968 context->object_type = next->object_type;
1969 /* Thread the stack. */
1970 context->next = next;
1973 return context;
1976 /* Managing the unparsed function queues. */
1978 #define unparsed_funs_with_default_args \
1979 parser->unparsed_queues->last ().funs_with_default_args
1980 #define unparsed_funs_with_definitions \
1981 parser->unparsed_queues->last ().funs_with_definitions
1982 #define unparsed_nsdmis \
1983 parser->unparsed_queues->last ().nsdmis
1984 #define unparsed_classes \
1985 parser->unparsed_queues->last ().classes
1987 static void
1988 push_unparsed_function_queues (cp_parser *parser)
1990 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1991 vec_safe_push (parser->unparsed_queues, e);
1994 static void
1995 pop_unparsed_function_queues (cp_parser *parser)
1997 release_tree_vector (unparsed_funs_with_definitions);
1998 parser->unparsed_queues->pop ();
2001 /* Prototypes. */
2003 /* Constructors and destructors. */
2005 static cp_parser *cp_parser_new
2006 (void);
2008 /* Routines to parse various constructs.
2010 Those that return `tree' will return the error_mark_node (rather
2011 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2012 Sometimes, they will return an ordinary node if error-recovery was
2013 attempted, even though a parse error occurred. So, to check
2014 whether or not a parse error occurred, you should always use
2015 cp_parser_error_occurred. If the construct is optional (indicated
2016 either by an `_opt' in the name of the function that does the
2017 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2018 the construct is not present. */
2020 /* Lexical conventions [gram.lex] */
2022 static cp_expr cp_parser_identifier
2023 (cp_parser *);
2024 static cp_expr cp_parser_string_literal
2025 (cp_parser *, bool, bool, bool);
2026 static cp_expr cp_parser_userdef_char_literal
2027 (cp_parser *);
2028 static tree cp_parser_userdef_string_literal
2029 (tree);
2030 static cp_expr cp_parser_userdef_numeric_literal
2031 (cp_parser *);
2033 /* Basic concepts [gram.basic] */
2035 static bool cp_parser_translation_unit
2036 (cp_parser *);
2038 /* Expressions [gram.expr] */
2040 static cp_expr cp_parser_primary_expression
2041 (cp_parser *, bool, bool, bool, cp_id_kind *);
2042 static cp_expr cp_parser_id_expression
2043 (cp_parser *, bool, bool, bool *, bool, bool);
2044 static cp_expr cp_parser_unqualified_id
2045 (cp_parser *, bool, bool, bool, bool);
2046 static tree cp_parser_nested_name_specifier_opt
2047 (cp_parser *, bool, bool, bool, bool, bool = false);
2048 static tree cp_parser_nested_name_specifier
2049 (cp_parser *, bool, bool, bool, bool);
2050 static tree cp_parser_qualifying_entity
2051 (cp_parser *, bool, bool, bool, bool, bool);
2052 static cp_expr cp_parser_postfix_expression
2053 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2054 static tree cp_parser_postfix_open_square_expression
2055 (cp_parser *, tree, bool, bool);
2056 static tree cp_parser_postfix_dot_deref_expression
2057 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2058 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2059 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2060 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2061 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2062 static void cp_parser_pseudo_destructor_name
2063 (cp_parser *, tree, tree *, tree *);
2064 static cp_expr cp_parser_unary_expression
2065 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2066 static enum tree_code cp_parser_unary_operator
2067 (cp_token *);
2068 static tree cp_parser_new_expression
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_placement
2071 (cp_parser *);
2072 static tree cp_parser_new_type_id
2073 (cp_parser *, tree *);
2074 static cp_declarator *cp_parser_new_declarator_opt
2075 (cp_parser *);
2076 static cp_declarator *cp_parser_direct_new_declarator
2077 (cp_parser *);
2078 static vec<tree, va_gc> *cp_parser_new_initializer
2079 (cp_parser *);
2080 static tree cp_parser_delete_expression
2081 (cp_parser *);
2082 static cp_expr cp_parser_cast_expression
2083 (cp_parser *, bool, bool, bool, cp_id_kind *);
2084 static cp_expr cp_parser_binary_expression
2085 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2086 static tree cp_parser_question_colon_clause
2087 (cp_parser *, cp_expr);
2088 static cp_expr cp_parser_assignment_expression
2089 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2090 static enum tree_code cp_parser_assignment_operator_opt
2091 (cp_parser *);
2092 static cp_expr cp_parser_expression
2093 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2094 static cp_expr cp_parser_constant_expression
2095 (cp_parser *, bool = false, bool * = NULL, bool = false);
2096 static cp_expr cp_parser_builtin_offsetof
2097 (cp_parser *);
2098 static cp_expr cp_parser_lambda_expression
2099 (cp_parser *);
2100 static void cp_parser_lambda_introducer
2101 (cp_parser *, tree);
2102 static bool cp_parser_lambda_declarator_opt
2103 (cp_parser *, tree);
2104 static void cp_parser_lambda_body
2105 (cp_parser *, tree);
2107 /* Statements [gram.stmt.stmt] */
2109 static void cp_parser_statement
2110 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2111 static void cp_parser_label_for_labeled_statement
2112 (cp_parser *, tree);
2113 static tree cp_parser_expression_statement
2114 (cp_parser *, tree);
2115 static tree cp_parser_compound_statement
2116 (cp_parser *, tree, int, bool);
2117 static void cp_parser_statement_seq_opt
2118 (cp_parser *, tree);
2119 static tree cp_parser_selection_statement
2120 (cp_parser *, bool *, vec<tree> *);
2121 static tree cp_parser_condition
2122 (cp_parser *);
2123 static tree cp_parser_iteration_statement
2124 (cp_parser *, bool *, bool);
2125 static bool cp_parser_init_statement
2126 (cp_parser *, tree *decl);
2127 static tree cp_parser_for
2128 (cp_parser *, bool);
2129 static tree cp_parser_c_for
2130 (cp_parser *, tree, tree, bool);
2131 static tree cp_parser_range_for
2132 (cp_parser *, tree, tree, tree, bool);
2133 static void do_range_for_auto_deduction
2134 (tree, tree);
2135 static tree cp_parser_perform_range_for_lookup
2136 (tree, tree *, tree *);
2137 static tree cp_parser_range_for_member_function
2138 (tree, tree);
2139 static tree cp_parser_jump_statement
2140 (cp_parser *);
2141 static void cp_parser_declaration_statement
2142 (cp_parser *);
2144 static tree cp_parser_implicitly_scoped_statement
2145 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2146 static void cp_parser_already_scoped_statement
2147 (cp_parser *, bool *, const token_indent_info &);
2149 /* Declarations [gram.dcl.dcl] */
2151 static void cp_parser_declaration_seq_opt
2152 (cp_parser *);
2153 static void cp_parser_declaration
2154 (cp_parser *);
2155 static void cp_parser_block_declaration
2156 (cp_parser *, bool);
2157 static void cp_parser_simple_declaration
2158 (cp_parser *, bool, tree *);
2159 static void cp_parser_decl_specifier_seq
2160 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2161 static tree cp_parser_storage_class_specifier_opt
2162 (cp_parser *);
2163 static tree cp_parser_function_specifier_opt
2164 (cp_parser *, cp_decl_specifier_seq *);
2165 static tree cp_parser_type_specifier
2166 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2167 int *, bool *);
2168 static tree cp_parser_simple_type_specifier
2169 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2170 static tree cp_parser_type_name
2171 (cp_parser *, bool);
2172 static tree cp_parser_type_name
2173 (cp_parser *);
2174 static tree cp_parser_nonclass_name
2175 (cp_parser* parser);
2176 static tree cp_parser_elaborated_type_specifier
2177 (cp_parser *, bool, bool);
2178 static tree cp_parser_enum_specifier
2179 (cp_parser *);
2180 static void cp_parser_enumerator_list
2181 (cp_parser *, tree);
2182 static void cp_parser_enumerator_definition
2183 (cp_parser *, tree);
2184 static tree cp_parser_namespace_name
2185 (cp_parser *);
2186 static void cp_parser_namespace_definition
2187 (cp_parser *);
2188 static void cp_parser_namespace_body
2189 (cp_parser *);
2190 static tree cp_parser_qualified_namespace_specifier
2191 (cp_parser *);
2192 static void cp_parser_namespace_alias_definition
2193 (cp_parser *);
2194 static bool cp_parser_using_declaration
2195 (cp_parser *, bool);
2196 static void cp_parser_using_directive
2197 (cp_parser *);
2198 static tree cp_parser_alias_declaration
2199 (cp_parser *);
2200 static void cp_parser_asm_definition
2201 (cp_parser *);
2202 static void cp_parser_linkage_specification
2203 (cp_parser *);
2204 static void cp_parser_static_assert
2205 (cp_parser *, bool);
2206 static tree cp_parser_decltype
2207 (cp_parser *);
2208 static tree cp_parser_decomposition_declaration
2209 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2211 /* Declarators [gram.dcl.decl] */
2213 static tree cp_parser_init_declarator
2214 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2215 bool, bool, int, bool *, tree *, location_t *, tree *);
2216 static cp_declarator *cp_parser_declarator
2217 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2218 static cp_declarator *cp_parser_direct_declarator
2219 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2220 static enum tree_code cp_parser_ptr_operator
2221 (cp_parser *, tree *, cp_cv_quals *, tree *);
2222 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2223 (cp_parser *);
2224 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2225 (cp_parser *);
2226 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2227 (cp_parser *);
2228 static tree cp_parser_tx_qualifier_opt
2229 (cp_parser *);
2230 static tree cp_parser_late_return_type_opt
2231 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2232 static tree cp_parser_declarator_id
2233 (cp_parser *, bool);
2234 static tree cp_parser_type_id
2235 (cp_parser *);
2236 static tree cp_parser_template_type_arg
2237 (cp_parser *);
2238 static tree cp_parser_trailing_type_id (cp_parser *);
2239 static tree cp_parser_type_id_1
2240 (cp_parser *, bool, bool);
2241 static void cp_parser_type_specifier_seq
2242 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2243 static tree cp_parser_parameter_declaration_clause
2244 (cp_parser *);
2245 static tree cp_parser_parameter_declaration_list
2246 (cp_parser *, bool *);
2247 static cp_parameter_declarator *cp_parser_parameter_declaration
2248 (cp_parser *, bool, bool *);
2249 static tree cp_parser_default_argument
2250 (cp_parser *, bool);
2251 static void cp_parser_function_body
2252 (cp_parser *, bool);
2253 static tree cp_parser_initializer
2254 (cp_parser *, bool *, bool *);
2255 static cp_expr cp_parser_initializer_clause
2256 (cp_parser *, bool *);
2257 static cp_expr cp_parser_braced_list
2258 (cp_parser*, bool*);
2259 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2260 (cp_parser *, bool *);
2262 static void cp_parser_ctor_initializer_opt_and_function_body
2263 (cp_parser *, bool);
2265 static tree cp_parser_late_parsing_omp_declare_simd
2266 (cp_parser *, tree);
2268 static tree cp_parser_late_parsing_cilk_simd_fn_info
2269 (cp_parser *, tree);
2271 static tree cp_parser_late_parsing_oacc_routine
2272 (cp_parser *, tree);
2274 static tree synthesize_implicit_template_parm
2275 (cp_parser *, tree);
2276 static tree finish_fully_implicit_template
2277 (cp_parser *, tree);
2279 /* Classes [gram.class] */
2281 static tree cp_parser_class_name
2282 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2283 static tree cp_parser_class_specifier
2284 (cp_parser *);
2285 static tree cp_parser_class_head
2286 (cp_parser *, bool *);
2287 static enum tag_types cp_parser_class_key
2288 (cp_parser *);
2289 static void cp_parser_type_parameter_key
2290 (cp_parser* parser);
2291 static void cp_parser_member_specification_opt
2292 (cp_parser *);
2293 static void cp_parser_member_declaration
2294 (cp_parser *);
2295 static tree cp_parser_pure_specifier
2296 (cp_parser *);
2297 static tree cp_parser_constant_initializer
2298 (cp_parser *);
2300 /* Derived classes [gram.class.derived] */
2302 static tree cp_parser_base_clause
2303 (cp_parser *);
2304 static tree cp_parser_base_specifier
2305 (cp_parser *);
2307 /* Special member functions [gram.special] */
2309 static tree cp_parser_conversion_function_id
2310 (cp_parser *);
2311 static tree cp_parser_conversion_type_id
2312 (cp_parser *);
2313 static cp_declarator *cp_parser_conversion_declarator_opt
2314 (cp_parser *);
2315 static void cp_parser_ctor_initializer_opt
2316 (cp_parser *);
2317 static void cp_parser_mem_initializer_list
2318 (cp_parser *);
2319 static tree cp_parser_mem_initializer
2320 (cp_parser *);
2321 static tree cp_parser_mem_initializer_id
2322 (cp_parser *);
2324 /* Overloading [gram.over] */
2326 static cp_expr cp_parser_operator_function_id
2327 (cp_parser *);
2328 static cp_expr cp_parser_operator
2329 (cp_parser *);
2331 /* Templates [gram.temp] */
2333 static void cp_parser_template_declaration
2334 (cp_parser *, bool);
2335 static tree cp_parser_template_parameter_list
2336 (cp_parser *);
2337 static tree cp_parser_template_parameter
2338 (cp_parser *, bool *, bool *);
2339 static tree cp_parser_type_parameter
2340 (cp_parser *, bool *);
2341 static tree cp_parser_template_id
2342 (cp_parser *, bool, bool, enum tag_types, bool);
2343 static tree cp_parser_template_name
2344 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2345 static tree cp_parser_template_argument_list
2346 (cp_parser *);
2347 static tree cp_parser_template_argument
2348 (cp_parser *);
2349 static void cp_parser_explicit_instantiation
2350 (cp_parser *);
2351 static void cp_parser_explicit_specialization
2352 (cp_parser *);
2354 /* Exception handling [gram.exception] */
2356 static tree cp_parser_try_block
2357 (cp_parser *);
2358 static void cp_parser_function_try_block
2359 (cp_parser *);
2360 static void cp_parser_handler_seq
2361 (cp_parser *);
2362 static void cp_parser_handler
2363 (cp_parser *);
2364 static tree cp_parser_exception_declaration
2365 (cp_parser *);
2366 static tree cp_parser_throw_expression
2367 (cp_parser *);
2368 static tree cp_parser_exception_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_type_id_list
2371 (cp_parser *);
2373 /* GNU Extensions */
2375 static tree cp_parser_asm_specification_opt
2376 (cp_parser *);
2377 static tree cp_parser_asm_operand_list
2378 (cp_parser *);
2379 static tree cp_parser_asm_clobber_list
2380 (cp_parser *);
2381 static tree cp_parser_asm_label_list
2382 (cp_parser *);
2383 static bool cp_next_tokens_can_be_attribute_p
2384 (cp_parser *);
2385 static bool cp_next_tokens_can_be_gnu_attribute_p
2386 (cp_parser *);
2387 static bool cp_next_tokens_can_be_std_attribute_p
2388 (cp_parser *);
2389 static bool cp_nth_tokens_can_be_std_attribute_p
2390 (cp_parser *, size_t);
2391 static bool cp_nth_tokens_can_be_gnu_attribute_p
2392 (cp_parser *, size_t);
2393 static bool cp_nth_tokens_can_be_attribute_p
2394 (cp_parser *, size_t);
2395 static tree cp_parser_attributes_opt
2396 (cp_parser *);
2397 static tree cp_parser_gnu_attributes_opt
2398 (cp_parser *);
2399 static tree cp_parser_gnu_attribute_list
2400 (cp_parser *);
2401 static tree cp_parser_std_attribute
2402 (cp_parser *, tree);
2403 static tree cp_parser_std_attribute_spec
2404 (cp_parser *);
2405 static tree cp_parser_std_attribute_spec_seq
2406 (cp_parser *);
2407 static bool cp_parser_extension_opt
2408 (cp_parser *, int *);
2409 static void cp_parser_label_declaration
2410 (cp_parser *);
2412 /* Concept Extensions */
2414 static tree cp_parser_requires_clause
2415 (cp_parser *);
2416 static tree cp_parser_requires_clause_opt
2417 (cp_parser *);
2418 static tree cp_parser_requires_expression
2419 (cp_parser *);
2420 static tree cp_parser_requirement_parameter_list
2421 (cp_parser *);
2422 static tree cp_parser_requirement_body
2423 (cp_parser *);
2424 static tree cp_parser_requirement_list
2425 (cp_parser *);
2426 static tree cp_parser_requirement
2427 (cp_parser *);
2428 static tree cp_parser_simple_requirement
2429 (cp_parser *);
2430 static tree cp_parser_compound_requirement
2431 (cp_parser *);
2432 static tree cp_parser_type_requirement
2433 (cp_parser *);
2434 static tree cp_parser_nested_requirement
2435 (cp_parser *);
2437 /* Transactional Memory Extensions */
2439 static tree cp_parser_transaction
2440 (cp_parser *, cp_token *);
2441 static tree cp_parser_transaction_expression
2442 (cp_parser *, enum rid);
2443 static void cp_parser_function_transaction
2444 (cp_parser *, enum rid);
2445 static tree cp_parser_transaction_cancel
2446 (cp_parser *);
2448 enum pragma_context {
2449 pragma_external,
2450 pragma_member,
2451 pragma_objc_icode,
2452 pragma_stmt,
2453 pragma_compound
2455 static bool cp_parser_pragma
2456 (cp_parser *, enum pragma_context, bool *);
2458 /* Objective-C++ Productions */
2460 static tree cp_parser_objc_message_receiver
2461 (cp_parser *);
2462 static tree cp_parser_objc_message_args
2463 (cp_parser *);
2464 static tree cp_parser_objc_message_expression
2465 (cp_parser *);
2466 static cp_expr cp_parser_objc_encode_expression
2467 (cp_parser *);
2468 static tree cp_parser_objc_defs_expression
2469 (cp_parser *);
2470 static tree cp_parser_objc_protocol_expression
2471 (cp_parser *);
2472 static tree cp_parser_objc_selector_expression
2473 (cp_parser *);
2474 static cp_expr cp_parser_objc_expression
2475 (cp_parser *);
2476 static bool cp_parser_objc_selector_p
2477 (enum cpp_ttype);
2478 static tree cp_parser_objc_selector
2479 (cp_parser *);
2480 static tree cp_parser_objc_protocol_refs_opt
2481 (cp_parser *);
2482 static void cp_parser_objc_declaration
2483 (cp_parser *, tree);
2484 static tree cp_parser_objc_statement
2485 (cp_parser *);
2486 static bool cp_parser_objc_valid_prefix_attributes
2487 (cp_parser *, tree *);
2488 static void cp_parser_objc_at_property_declaration
2489 (cp_parser *) ;
2490 static void cp_parser_objc_at_synthesize_declaration
2491 (cp_parser *) ;
2492 static void cp_parser_objc_at_dynamic_declaration
2493 (cp_parser *) ;
2494 static tree cp_parser_objc_struct_declaration
2495 (cp_parser *) ;
2497 /* Utility Routines */
2499 static cp_expr cp_parser_lookup_name
2500 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2501 static tree cp_parser_lookup_name_simple
2502 (cp_parser *, tree, location_t);
2503 static tree cp_parser_maybe_treat_template_as_class
2504 (tree, bool);
2505 static bool cp_parser_check_declarator_template_parameters
2506 (cp_parser *, cp_declarator *, location_t);
2507 static bool cp_parser_check_template_parameters
2508 (cp_parser *, unsigned, location_t, cp_declarator *);
2509 static cp_expr cp_parser_simple_cast_expression
2510 (cp_parser *);
2511 static tree cp_parser_global_scope_opt
2512 (cp_parser *, bool);
2513 static bool cp_parser_constructor_declarator_p
2514 (cp_parser *, bool);
2515 static tree cp_parser_function_definition_from_specifiers_and_declarator
2516 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2517 static tree cp_parser_function_definition_after_declarator
2518 (cp_parser *, bool);
2519 static bool cp_parser_template_declaration_after_export
2520 (cp_parser *, bool);
2521 static void cp_parser_perform_template_parameter_access_checks
2522 (vec<deferred_access_check, va_gc> *);
2523 static tree cp_parser_single_declaration
2524 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2525 static cp_expr cp_parser_functional_cast
2526 (cp_parser *, tree);
2527 static tree cp_parser_save_member_function_body
2528 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2529 static tree cp_parser_save_nsdmi
2530 (cp_parser *);
2531 static tree cp_parser_enclosed_template_argument_list
2532 (cp_parser *);
2533 static void cp_parser_save_default_args
2534 (cp_parser *, tree);
2535 static void cp_parser_late_parsing_for_member
2536 (cp_parser *, tree);
2537 static tree cp_parser_late_parse_one_default_arg
2538 (cp_parser *, tree, tree, tree);
2539 static void cp_parser_late_parsing_nsdmi
2540 (cp_parser *, tree);
2541 static void cp_parser_late_parsing_default_args
2542 (cp_parser *, tree);
2543 static tree cp_parser_sizeof_operand
2544 (cp_parser *, enum rid);
2545 static tree cp_parser_trait_expr
2546 (cp_parser *, enum rid);
2547 static bool cp_parser_declares_only_class_p
2548 (cp_parser *);
2549 static void cp_parser_set_storage_class
2550 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2551 static void cp_parser_set_decl_spec_type
2552 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2553 static void set_and_check_decl_spec_loc
2554 (cp_decl_specifier_seq *decl_specs,
2555 cp_decl_spec ds, cp_token *);
2556 static bool cp_parser_friend_p
2557 (const cp_decl_specifier_seq *);
2558 static void cp_parser_required_error
2559 (cp_parser *, required_token, bool, location_t);
2560 static cp_token *cp_parser_require
2561 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2562 static cp_token *cp_parser_require_keyword
2563 (cp_parser *, enum rid, required_token);
2564 static bool cp_parser_token_starts_function_definition_p
2565 (cp_token *);
2566 static bool cp_parser_next_token_starts_class_definition_p
2567 (cp_parser *);
2568 static bool cp_parser_next_token_ends_template_argument_p
2569 (cp_parser *);
2570 static bool cp_parser_nth_token_starts_template_argument_list_p
2571 (cp_parser *, size_t);
2572 static enum tag_types cp_parser_token_is_class_key
2573 (cp_token *);
2574 static enum tag_types cp_parser_token_is_type_parameter_key
2575 (cp_token *);
2576 static void cp_parser_check_class_key
2577 (enum tag_types, tree type);
2578 static void cp_parser_check_access_in_redeclaration
2579 (tree type, location_t location);
2580 static bool cp_parser_optional_template_keyword
2581 (cp_parser *);
2582 static void cp_parser_pre_parsed_nested_name_specifier
2583 (cp_parser *);
2584 static bool cp_parser_cache_group
2585 (cp_parser *, enum cpp_ttype, unsigned);
2586 static tree cp_parser_cache_defarg
2587 (cp_parser *parser, bool nsdmi);
2588 static void cp_parser_parse_tentatively
2589 (cp_parser *);
2590 static void cp_parser_commit_to_tentative_parse
2591 (cp_parser *);
2592 static void cp_parser_commit_to_topmost_tentative_parse
2593 (cp_parser *);
2594 static void cp_parser_abort_tentative_parse
2595 (cp_parser *);
2596 static bool cp_parser_parse_definitely
2597 (cp_parser *);
2598 static inline bool cp_parser_parsing_tentatively
2599 (cp_parser *);
2600 static bool cp_parser_uncommitted_to_tentative_parse_p
2601 (cp_parser *);
2602 static void cp_parser_error
2603 (cp_parser *, const char *);
2604 static void cp_parser_name_lookup_error
2605 (cp_parser *, tree, tree, name_lookup_error, location_t);
2606 static bool cp_parser_simulate_error
2607 (cp_parser *);
2608 static bool cp_parser_check_type_definition
2609 (cp_parser *);
2610 static void cp_parser_check_for_definition_in_return_type
2611 (cp_declarator *, tree, location_t type_location);
2612 static void cp_parser_check_for_invalid_template_id
2613 (cp_parser *, tree, enum tag_types, location_t location);
2614 static bool cp_parser_non_integral_constant_expression
2615 (cp_parser *, non_integral_constant);
2616 static void cp_parser_diagnose_invalid_type_name
2617 (cp_parser *, tree, location_t);
2618 static bool cp_parser_parse_and_diagnose_invalid_type_name
2619 (cp_parser *);
2620 static int cp_parser_skip_to_closing_parenthesis
2621 (cp_parser *, bool, bool, bool);
2622 static void cp_parser_skip_to_end_of_statement
2623 (cp_parser *);
2624 static void cp_parser_consume_semicolon_at_end_of_statement
2625 (cp_parser *);
2626 static void cp_parser_skip_to_end_of_block_or_statement
2627 (cp_parser *);
2628 static bool cp_parser_skip_to_closing_brace
2629 (cp_parser *);
2630 static void cp_parser_skip_to_end_of_template_parameter_list
2631 (cp_parser *);
2632 static void cp_parser_skip_to_pragma_eol
2633 (cp_parser*, cp_token *);
2634 static bool cp_parser_error_occurred
2635 (cp_parser *);
2636 static bool cp_parser_allow_gnu_extensions_p
2637 (cp_parser *);
2638 static bool cp_parser_is_pure_string_literal
2639 (cp_token *);
2640 static bool cp_parser_is_string_literal
2641 (cp_token *);
2642 static bool cp_parser_is_keyword
2643 (cp_token *, enum rid);
2644 static tree cp_parser_make_typename_type
2645 (cp_parser *, tree, location_t location);
2646 static cp_declarator * cp_parser_make_indirect_declarator
2647 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2648 static bool cp_parser_compound_literal_p
2649 (cp_parser *);
2650 static bool cp_parser_array_designator_p
2651 (cp_parser *);
2652 static bool cp_parser_init_statement_p
2653 (cp_parser *);
2654 static bool cp_parser_skip_to_closing_square_bracket
2655 (cp_parser *);
2657 /* Concept-related syntactic transformations */
2659 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2660 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2662 // -------------------------------------------------------------------------- //
2663 // Unevaluated Operand Guard
2665 // Implementation of an RAII helper for unevaluated operand parsing.
2666 cp_unevaluated::cp_unevaluated ()
2668 ++cp_unevaluated_operand;
2669 ++c_inhibit_evaluation_warnings;
2672 cp_unevaluated::~cp_unevaluated ()
2674 --c_inhibit_evaluation_warnings;
2675 --cp_unevaluated_operand;
2678 // -------------------------------------------------------------------------- //
2679 // Tentative Parsing
2681 /* Returns nonzero if we are parsing tentatively. */
2683 static inline bool
2684 cp_parser_parsing_tentatively (cp_parser* parser)
2686 return parser->context->next != NULL;
2689 /* Returns nonzero if TOKEN is a string literal. */
2691 static bool
2692 cp_parser_is_pure_string_literal (cp_token* token)
2694 return (token->type == CPP_STRING ||
2695 token->type == CPP_STRING16 ||
2696 token->type == CPP_STRING32 ||
2697 token->type == CPP_WSTRING ||
2698 token->type == CPP_UTF8STRING);
2701 /* Returns nonzero if TOKEN is a string literal
2702 of a user-defined string literal. */
2704 static bool
2705 cp_parser_is_string_literal (cp_token* token)
2707 return (cp_parser_is_pure_string_literal (token) ||
2708 token->type == CPP_STRING_USERDEF ||
2709 token->type == CPP_STRING16_USERDEF ||
2710 token->type == CPP_STRING32_USERDEF ||
2711 token->type == CPP_WSTRING_USERDEF ||
2712 token->type == CPP_UTF8STRING_USERDEF);
2715 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2717 static bool
2718 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2720 return token->keyword == keyword;
2723 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2724 PRAGMA_NONE. */
2726 static enum pragma_kind
2727 cp_parser_pragma_kind (cp_token *token)
2729 if (token->type != CPP_PRAGMA)
2730 return PRAGMA_NONE;
2731 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2732 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2735 /* Helper function for cp_parser_error.
2736 Having peeked a token of kind TOK1_KIND that might signify
2737 a conflict marker, peek successor tokens to determine
2738 if we actually do have a conflict marker.
2739 Specifically, we consider a run of 7 '<', '=' or '>' characters
2740 at the start of a line as a conflict marker.
2741 These come through the lexer as three pairs and a single,
2742 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2743 If it returns true, *OUT_LOC is written to with the location/range
2744 of the marker. */
2746 static bool
2747 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2748 location_t *out_loc)
2750 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2751 if (token2->type != tok1_kind)
2752 return false;
2753 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2754 if (token3->type != tok1_kind)
2755 return false;
2756 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2757 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2758 return false;
2760 /* It must be at the start of the line. */
2761 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2762 if (LOCATION_COLUMN (start_loc) != 1)
2763 return false;
2765 /* We have a conflict marker. Construct a location of the form:
2766 <<<<<<<
2767 ^~~~~~~
2768 with start == caret, finishing at the end of the marker. */
2769 location_t finish_loc = get_finish (token4->location);
2770 *out_loc = make_location (start_loc, start_loc, finish_loc);
2772 return true;
2775 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2776 RT_CLOSE_PAREN. */
2778 static const char *
2779 get_matching_symbol (required_token token_desc)
2781 switch (token_desc)
2783 default:
2784 gcc_unreachable ();
2785 return "";
2786 case RT_CLOSE_BRACE:
2787 return "{";
2788 case RT_CLOSE_PAREN:
2789 return "(";
2793 /* Attempt to convert TOKEN_DESC from a required_token to an
2794 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2796 static enum cpp_ttype
2797 get_required_cpp_ttype (required_token token_desc)
2799 switch (token_desc)
2801 case RT_SEMICOLON:
2802 return CPP_SEMICOLON;
2803 case RT_OPEN_PAREN:
2804 return CPP_OPEN_PAREN;
2805 case RT_CLOSE_BRACE:
2806 return CPP_CLOSE_BRACE;
2807 case RT_OPEN_BRACE:
2808 return CPP_OPEN_BRACE;
2809 case RT_CLOSE_SQUARE:
2810 return CPP_CLOSE_SQUARE;
2811 case RT_OPEN_SQUARE:
2812 return CPP_OPEN_SQUARE;
2813 case RT_COMMA:
2814 return CPP_COMMA;
2815 case RT_COLON:
2816 return CPP_COLON;
2817 case RT_CLOSE_PAREN:
2818 return CPP_CLOSE_PAREN;
2820 default:
2821 /* Use CPP_EOF as a "no completions possible" code. */
2822 return CPP_EOF;
2827 /* Subroutine of cp_parser_error and cp_parser_required_error.
2829 Issue a diagnostic of the form
2830 FILE:LINE: MESSAGE before TOKEN
2831 where TOKEN is the next token in the input stream. MESSAGE
2832 (specified by the caller) is usually of the form "expected
2833 OTHER-TOKEN".
2835 This bypasses the check for tentative passing, and potentially
2836 adds material needed by cp_parser_required_error.
2838 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2839 suggesting insertion of the missing token.
2841 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2842 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2843 location. */
2845 static void
2846 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2847 required_token missing_token_desc,
2848 location_t matching_location)
2850 cp_token *token = cp_lexer_peek_token (parser->lexer);
2851 /* This diagnostic makes more sense if it is tagged to the line
2852 of the token we just peeked at. */
2853 cp_lexer_set_source_position_from_token (token);
2855 if (token->type == CPP_PRAGMA)
2857 error_at (token->location,
2858 "%<#pragma%> is not allowed here");
2859 cp_parser_skip_to_pragma_eol (parser, token);
2860 return;
2863 /* If this is actually a conflict marker, report it as such. */
2864 if (token->type == CPP_LSHIFT
2865 || token->type == CPP_RSHIFT
2866 || token->type == CPP_EQ_EQ)
2868 location_t loc;
2869 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2871 error_at (loc, "version control conflict marker in file");
2872 return;
2876 gcc_rich_location richloc (input_location);
2878 bool added_matching_location = false;
2880 if (missing_token_desc != RT_NONE)
2882 /* Potentially supply a fix-it hint, suggesting to add the
2883 missing token immediately after the *previous* token.
2884 This may move the primary location within richloc. */
2885 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2886 location_t prev_token_loc
2887 = cp_lexer_previous_token (parser->lexer)->location;
2888 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2890 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2891 Attempt to consolidate diagnostics by printing it as a
2892 secondary range within the main diagnostic. */
2893 if (matching_location != UNKNOWN_LOCATION)
2894 added_matching_location
2895 = richloc.add_location_if_nearby (matching_location);
2898 /* Actually emit the error. */
2899 c_parse_error (gmsgid,
2900 /* Because c_parser_error does not understand
2901 CPP_KEYWORD, keywords are treated like
2902 identifiers. */
2903 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2904 token->u.value, token->flags, &richloc);
2906 if (missing_token_desc != RT_NONE)
2908 /* If we weren't able to consolidate matching_location, then
2909 print it as a secondary diagnostic. */
2910 if (matching_location != UNKNOWN_LOCATION
2911 && !added_matching_location)
2912 inform (matching_location, "to match this %qs",
2913 get_matching_symbol (missing_token_desc));
2917 /* If not parsing tentatively, issue a diagnostic of the form
2918 FILE:LINE: MESSAGE before TOKEN
2919 where TOKEN is the next token in the input stream. MESSAGE
2920 (specified by the caller) is usually of the form "expected
2921 OTHER-TOKEN". */
2923 static void
2924 cp_parser_error (cp_parser* parser, const char* gmsgid)
2926 if (!cp_parser_simulate_error (parser))
2927 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2930 /* Issue an error about name-lookup failing. NAME is the
2931 IDENTIFIER_NODE DECL is the result of
2932 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2933 the thing that we hoped to find. */
2935 static void
2936 cp_parser_name_lookup_error (cp_parser* parser,
2937 tree name,
2938 tree decl,
2939 name_lookup_error desired,
2940 location_t location)
2942 /* If name lookup completely failed, tell the user that NAME was not
2943 declared. */
2944 if (decl == error_mark_node)
2946 if (parser->scope && parser->scope != global_namespace)
2947 error_at (location, "%<%E::%E%> has not been declared",
2948 parser->scope, name);
2949 else if (parser->scope == global_namespace)
2950 error_at (location, "%<::%E%> has not been declared", name);
2951 else if (parser->object_scope
2952 && !CLASS_TYPE_P (parser->object_scope))
2953 error_at (location, "request for member %qE in non-class type %qT",
2954 name, parser->object_scope);
2955 else if (parser->object_scope)
2956 error_at (location, "%<%T::%E%> has not been declared",
2957 parser->object_scope, name);
2958 else
2959 error_at (location, "%qE has not been declared", name);
2961 else if (parser->scope && parser->scope != global_namespace)
2963 switch (desired)
2965 case NLE_TYPE:
2966 error_at (location, "%<%E::%E%> is not a type",
2967 parser->scope, name);
2968 break;
2969 case NLE_CXX98:
2970 error_at (location, "%<%E::%E%> is not a class or namespace",
2971 parser->scope, name);
2972 break;
2973 case NLE_NOT_CXX98:
2974 error_at (location,
2975 "%<%E::%E%> is not a class, namespace, or enumeration",
2976 parser->scope, name);
2977 break;
2978 default:
2979 gcc_unreachable ();
2983 else if (parser->scope == global_namespace)
2985 switch (desired)
2987 case NLE_TYPE:
2988 error_at (location, "%<::%E%> is not a type", name);
2989 break;
2990 case NLE_CXX98:
2991 error_at (location, "%<::%E%> is not a class or namespace", name);
2992 break;
2993 case NLE_NOT_CXX98:
2994 error_at (location,
2995 "%<::%E%> is not a class, namespace, or enumeration",
2996 name);
2997 break;
2998 default:
2999 gcc_unreachable ();
3002 else
3004 switch (desired)
3006 case NLE_TYPE:
3007 error_at (location, "%qE is not a type", name);
3008 break;
3009 case NLE_CXX98:
3010 error_at (location, "%qE is not a class or namespace", name);
3011 break;
3012 case NLE_NOT_CXX98:
3013 error_at (location,
3014 "%qE is not a class, namespace, or enumeration", name);
3015 break;
3016 default:
3017 gcc_unreachable ();
3022 /* If we are parsing tentatively, remember that an error has occurred
3023 during this tentative parse. Returns true if the error was
3024 simulated; false if a message should be issued by the caller. */
3026 static bool
3027 cp_parser_simulate_error (cp_parser* parser)
3029 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3031 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3032 return true;
3034 return false;
3037 /* This function is called when a type is defined. If type
3038 definitions are forbidden at this point, an error message is
3039 issued. */
3041 static bool
3042 cp_parser_check_type_definition (cp_parser* parser)
3044 /* If types are forbidden here, issue a message. */
3045 if (parser->type_definition_forbidden_message)
3047 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3048 in the message need to be interpreted. */
3049 error (parser->type_definition_forbidden_message);
3050 return false;
3052 return true;
3055 /* This function is called when the DECLARATOR is processed. The TYPE
3056 was a type defined in the decl-specifiers. If it is invalid to
3057 define a type in the decl-specifiers for DECLARATOR, an error is
3058 issued. TYPE_LOCATION is the location of TYPE and is used
3059 for error reporting. */
3061 static void
3062 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3063 tree type, location_t type_location)
3065 /* [dcl.fct] forbids type definitions in return types.
3066 Unfortunately, it's not easy to know whether or not we are
3067 processing a return type until after the fact. */
3068 while (declarator
3069 && (declarator->kind == cdk_pointer
3070 || declarator->kind == cdk_reference
3071 || declarator->kind == cdk_ptrmem))
3072 declarator = declarator->declarator;
3073 if (declarator
3074 && declarator->kind == cdk_function)
3076 error_at (type_location,
3077 "new types may not be defined in a return type");
3078 inform (type_location,
3079 "(perhaps a semicolon is missing after the definition of %qT)",
3080 type);
3084 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3085 "<" in any valid C++ program. If the next token is indeed "<",
3086 issue a message warning the user about what appears to be an
3087 invalid attempt to form a template-id. LOCATION is the location
3088 of the type-specifier (TYPE) */
3090 static void
3091 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3092 tree type,
3093 enum tag_types tag_type,
3094 location_t location)
3096 cp_token_position start = 0;
3098 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3100 if (TREE_CODE (type) == TYPE_DECL)
3101 type = TREE_TYPE (type);
3102 if (TYPE_P (type) && !template_placeholder_p (type))
3103 error_at (location, "%qT is not a template", type);
3104 else if (identifier_p (type))
3106 if (tag_type != none_type)
3107 error_at (location, "%qE is not a class template", type);
3108 else
3109 error_at (location, "%qE is not a template", type);
3111 else
3112 error_at (location, "invalid template-id");
3113 /* Remember the location of the invalid "<". */
3114 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3115 start = cp_lexer_token_position (parser->lexer, true);
3116 /* Consume the "<". */
3117 cp_lexer_consume_token (parser->lexer);
3118 /* Parse the template arguments. */
3119 cp_parser_enclosed_template_argument_list (parser);
3120 /* Permanently remove the invalid template arguments so that
3121 this error message is not issued again. */
3122 if (start)
3123 cp_lexer_purge_tokens_after (parser->lexer, start);
3127 /* If parsing an integral constant-expression, issue an error message
3128 about the fact that THING appeared and return true. Otherwise,
3129 return false. In either case, set
3130 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3132 static bool
3133 cp_parser_non_integral_constant_expression (cp_parser *parser,
3134 non_integral_constant thing)
3136 parser->non_integral_constant_expression_p = true;
3137 if (parser->integral_constant_expression_p)
3139 if (!parser->allow_non_integral_constant_expression_p)
3141 const char *msg = NULL;
3142 switch (thing)
3144 case NIC_FLOAT:
3145 pedwarn (input_location, OPT_Wpedantic,
3146 "ISO C++ forbids using a floating-point literal "
3147 "in a constant-expression");
3148 return true;
3149 case NIC_CAST:
3150 error ("a cast to a type other than an integral or "
3151 "enumeration type cannot appear in a "
3152 "constant-expression");
3153 return true;
3154 case NIC_TYPEID:
3155 error ("%<typeid%> operator "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_NCC:
3159 error ("non-constant compound literals "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_FUNC_CALL:
3163 error ("a function call "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_INC:
3167 error ("an increment "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_DEC:
3171 error ("an decrement "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ARRAY_REF:
3175 error ("an array reference "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_ADDR_LABEL:
3179 error ("the address of a label "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_OVERLOADED:
3183 error ("calls to overloaded operators "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_ASSIGNMENT:
3187 error ("an assignment cannot appear in a constant-expression");
3188 return true;
3189 case NIC_COMMA:
3190 error ("a comma operator "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_CONSTRUCTOR:
3194 error ("a call to a constructor "
3195 "cannot appear in a constant-expression");
3196 return true;
3197 case NIC_TRANSACTION:
3198 error ("a transaction expression "
3199 "cannot appear in a constant-expression");
3200 return true;
3201 case NIC_THIS:
3202 msg = "this";
3203 break;
3204 case NIC_FUNC_NAME:
3205 msg = "__FUNCTION__";
3206 break;
3207 case NIC_PRETTY_FUNC:
3208 msg = "__PRETTY_FUNCTION__";
3209 break;
3210 case NIC_C99_FUNC:
3211 msg = "__func__";
3212 break;
3213 case NIC_VA_ARG:
3214 msg = "va_arg";
3215 break;
3216 case NIC_ARROW:
3217 msg = "->";
3218 break;
3219 case NIC_POINT:
3220 msg = ".";
3221 break;
3222 case NIC_STAR:
3223 msg = "*";
3224 break;
3225 case NIC_ADDR:
3226 msg = "&";
3227 break;
3228 case NIC_PREINCREMENT:
3229 msg = "++";
3230 break;
3231 case NIC_PREDECREMENT:
3232 msg = "--";
3233 break;
3234 case NIC_NEW:
3235 msg = "new";
3236 break;
3237 case NIC_DEL:
3238 msg = "delete";
3239 break;
3240 default:
3241 gcc_unreachable ();
3243 if (msg)
3244 error ("%qs cannot appear in a constant-expression", msg);
3245 return true;
3248 return false;
3251 /* Emit a diagnostic for an invalid type name. This function commits
3252 to the current active tentative parse, if any. (Otherwise, the
3253 problematic construct might be encountered again later, resulting
3254 in duplicate error messages.) LOCATION is the location of ID. */
3256 static void
3257 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3258 location_t location)
3260 tree decl, ambiguous_decls;
3261 cp_parser_commit_to_tentative_parse (parser);
3262 /* Try to lookup the identifier. */
3263 decl = cp_parser_lookup_name (parser, id, none_type,
3264 /*is_template=*/false,
3265 /*is_namespace=*/false,
3266 /*check_dependency=*/true,
3267 &ambiguous_decls, location);
3268 if (ambiguous_decls)
3269 /* If the lookup was ambiguous, an error will already have
3270 been issued. */
3271 return;
3272 /* If the lookup found a template-name, it means that the user forgot
3273 to specify an argument list. Emit a useful error message. */
3274 if (DECL_TYPE_TEMPLATE_P (decl))
3276 error_at (location,
3277 "invalid use of template-name %qE without an argument list",
3278 decl);
3279 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3280 inform (location, "class template argument deduction is only available "
3281 "with -std=c++17 or -std=gnu++17");
3282 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3284 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3285 error_at (location, "invalid use of destructor %qD as a type", id);
3286 else if (TREE_CODE (decl) == TYPE_DECL)
3287 /* Something like 'unsigned A a;' */
3288 error_at (location, "invalid combination of multiple type-specifiers");
3289 else if (!parser->scope)
3291 /* Issue an error message. */
3292 name_hint hint;
3293 if (TREE_CODE (id) == IDENTIFIER_NODE)
3294 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3295 if (hint)
3297 gcc_rich_location richloc (location);
3298 richloc.add_fixit_replace (hint.suggestion ());
3299 error_at (&richloc,
3300 "%qE does not name a type; did you mean %qs?",
3301 id, hint.suggestion ());
3303 else
3304 error_at (location, "%qE does not name a type", id);
3305 /* If we're in a template class, it's possible that the user was
3306 referring to a type from a base class. For example:
3308 template <typename T> struct A { typedef T X; };
3309 template <typename T> struct B : public A<T> { X x; };
3311 The user should have said "typename A<T>::X". */
3312 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3313 inform (location, "C++11 %<constexpr%> only available with "
3314 "-std=c++11 or -std=gnu++11");
3315 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3316 inform (location, "C++11 %<noexcept%> only available with "
3317 "-std=c++11 or -std=gnu++11");
3318 else if (cxx_dialect < cxx11
3319 && TREE_CODE (id) == IDENTIFIER_NODE
3320 && id_equal (id, "thread_local"))
3321 inform (location, "C++11 %<thread_local%> only available with "
3322 "-std=c++11 or -std=gnu++11");
3323 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3324 inform (location, "%<concept%> only available with -fconcepts");
3325 else if (processing_template_decl && current_class_type
3326 && TYPE_BINFO (current_class_type))
3328 tree b;
3330 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3332 b = TREE_CHAIN (b))
3334 tree base_type = BINFO_TYPE (b);
3335 if (CLASS_TYPE_P (base_type)
3336 && dependent_type_p (base_type))
3338 tree field;
3339 /* Go from a particular instantiation of the
3340 template (which will have an empty TYPE_FIELDs),
3341 to the main version. */
3342 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3343 for (field = TYPE_FIELDS (base_type);
3344 field;
3345 field = DECL_CHAIN (field))
3346 if (TREE_CODE (field) == TYPE_DECL
3347 && DECL_NAME (field) == id)
3349 inform (location,
3350 "(perhaps %<typename %T::%E%> was intended)",
3351 BINFO_TYPE (b), id);
3352 break;
3354 if (field)
3355 break;
3360 /* Here we diagnose qualified-ids where the scope is actually correct,
3361 but the identifier does not resolve to a valid type name. */
3362 else if (parser->scope != error_mark_node)
3364 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3366 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3367 error_at (location_of (id),
3368 "%qE in namespace %qE does not name a template type",
3369 id, parser->scope);
3370 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3371 error_at (location_of (id),
3372 "%qE in namespace %qE does not name a template type",
3373 TREE_OPERAND (id, 0), parser->scope);
3374 else
3375 error_at (location_of (id),
3376 "%qE in namespace %qE does not name a type",
3377 id, parser->scope);
3378 if (DECL_P (decl))
3379 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3381 else if (CLASS_TYPE_P (parser->scope)
3382 && constructor_name_p (id, parser->scope))
3384 /* A<T>::A<T>() */
3385 error_at (location, "%<%T::%E%> names the constructor, not"
3386 " the type", parser->scope, id);
3387 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3388 error_at (location, "and %qT has no template constructors",
3389 parser->scope);
3391 else if (TYPE_P (parser->scope)
3392 && dependent_scope_p (parser->scope))
3394 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3395 error_at (location,
3396 "need %<typename%> before %<%T::%D::%E%> because "
3397 "%<%T::%D%> is a dependent scope",
3398 TYPE_CONTEXT (parser->scope),
3399 TYPENAME_TYPE_FULLNAME (parser->scope),
3401 TYPE_CONTEXT (parser->scope),
3402 TYPENAME_TYPE_FULLNAME (parser->scope));
3403 else
3404 error_at (location, "need %<typename%> before %<%T::%E%> because "
3405 "%qT is a dependent scope",
3406 parser->scope, id, parser->scope);
3408 else if (TYPE_P (parser->scope))
3410 if (!COMPLETE_TYPE_P (parser->scope))
3411 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3412 parser->scope);
3413 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 id, parser->scope);
3417 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a template type",
3420 TREE_OPERAND (id, 0), parser->scope);
3421 else
3422 error_at (location_of (id),
3423 "%qE in %q#T does not name a type",
3424 id, parser->scope);
3425 if (DECL_P (decl))
3426 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3428 else
3429 gcc_unreachable ();
3433 /* Check for a common situation where a type-name should be present,
3434 but is not, and issue a sensible error message. Returns true if an
3435 invalid type-name was detected.
3437 The situation handled by this function are variable declarations of the
3438 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3439 Usually, `ID' should name a type, but if we got here it means that it
3440 does not. We try to emit the best possible error message depending on
3441 how exactly the id-expression looks like. */
3443 static bool
3444 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3446 tree id;
3447 cp_token *token = cp_lexer_peek_token (parser->lexer);
3449 /* Avoid duplicate error about ambiguous lookup. */
3450 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3452 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3453 if (next->type == CPP_NAME && next->error_reported)
3454 goto out;
3457 cp_parser_parse_tentatively (parser);
3458 id = cp_parser_id_expression (parser,
3459 /*template_keyword_p=*/false,
3460 /*check_dependency_p=*/true,
3461 /*template_p=*/NULL,
3462 /*declarator_p=*/true,
3463 /*optional_p=*/false);
3464 /* If the next token is a (, this is a function with no explicit return
3465 type, i.e. constructor, destructor or conversion op. */
3466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3467 || TREE_CODE (id) == TYPE_DECL)
3469 cp_parser_abort_tentative_parse (parser);
3470 return false;
3472 if (!cp_parser_parse_definitely (parser))
3473 return false;
3475 /* Emit a diagnostic for the invalid type. */
3476 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3477 out:
3478 /* If we aren't in the middle of a declarator (i.e. in a
3479 parameter-declaration-clause), skip to the end of the declaration;
3480 there's no point in trying to process it. */
3481 if (!parser->in_declarator_p)
3482 cp_parser_skip_to_end_of_block_or_statement (parser);
3483 return true;
3486 /* Consume tokens up to, and including, the next non-nested closing `)'.
3487 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3488 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3489 found an unnested token of that type. */
3491 static int
3492 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3493 bool recovering,
3494 cpp_ttype or_ttype,
3495 bool consume_paren)
3497 unsigned paren_depth = 0;
3498 unsigned brace_depth = 0;
3499 unsigned square_depth = 0;
3501 if (recovering && or_ttype == CPP_EOF
3502 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3503 return 0;
3505 while (true)
3507 cp_token * token = cp_lexer_peek_token (parser->lexer);
3509 /* Have we found what we're looking for before the closing paren? */
3510 if (token->type == or_ttype && or_ttype != CPP_EOF
3511 && !brace_depth && !paren_depth && !square_depth)
3512 return -1;
3514 switch (token->type)
3516 case CPP_EOF:
3517 case CPP_PRAGMA_EOL:
3518 /* If we've run out of tokens, then there is no closing `)'. */
3519 return 0;
3521 /* This is good for lambda expression capture-lists. */
3522 case CPP_OPEN_SQUARE:
3523 ++square_depth;
3524 break;
3525 case CPP_CLOSE_SQUARE:
3526 if (!square_depth--)
3527 return 0;
3528 break;
3530 case CPP_SEMICOLON:
3531 /* This matches the processing in skip_to_end_of_statement. */
3532 if (!brace_depth)
3533 return 0;
3534 break;
3536 case CPP_OPEN_BRACE:
3537 ++brace_depth;
3538 break;
3539 case CPP_CLOSE_BRACE:
3540 if (!brace_depth--)
3541 return 0;
3542 break;
3544 case CPP_OPEN_PAREN:
3545 if (!brace_depth)
3546 ++paren_depth;
3547 break;
3549 case CPP_CLOSE_PAREN:
3550 if (!brace_depth && !paren_depth--)
3552 if (consume_paren)
3553 cp_lexer_consume_token (parser->lexer);
3554 return 1;
3556 break;
3558 default:
3559 break;
3562 /* Consume the token. */
3563 cp_lexer_consume_token (parser->lexer);
3567 /* Consume tokens up to, and including, the next non-nested closing `)'.
3568 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3569 are doing error recovery. Returns -1 if OR_COMMA is true and we
3570 found an unnested token of that type. */
3572 static int
3573 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3574 bool recovering,
3575 bool or_comma,
3576 bool consume_paren)
3578 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3579 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3580 ttype, consume_paren);
3583 /* Consume tokens until we reach the end of the current statement.
3584 Normally, that will be just before consuming a `;'. However, if a
3585 non-nested `}' comes first, then we stop before consuming that. */
3587 static void
3588 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3590 unsigned nesting_depth = 0;
3592 /* Unwind generic function template scope if necessary. */
3593 if (parser->fully_implicit_function_template_p)
3594 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3596 while (true)
3598 cp_token *token = cp_lexer_peek_token (parser->lexer);
3600 switch (token->type)
3602 case CPP_EOF:
3603 case CPP_PRAGMA_EOL:
3604 /* If we've run out of tokens, stop. */
3605 return;
3607 case CPP_SEMICOLON:
3608 /* If the next token is a `;', we have reached the end of the
3609 statement. */
3610 if (!nesting_depth)
3611 return;
3612 break;
3614 case CPP_CLOSE_BRACE:
3615 /* If this is a non-nested '}', stop before consuming it.
3616 That way, when confronted with something like:
3618 { 3 + }
3620 we stop before consuming the closing '}', even though we
3621 have not yet reached a `;'. */
3622 if (nesting_depth == 0)
3623 return;
3625 /* If it is the closing '}' for a block that we have
3626 scanned, stop -- but only after consuming the token.
3627 That way given:
3629 void f g () { ... }
3630 typedef int I;
3632 we will stop after the body of the erroneously declared
3633 function, but before consuming the following `typedef'
3634 declaration. */
3635 if (--nesting_depth == 0)
3637 cp_lexer_consume_token (parser->lexer);
3638 return;
3640 break;
3642 case CPP_OPEN_BRACE:
3643 ++nesting_depth;
3644 break;
3646 default:
3647 break;
3650 /* Consume the token. */
3651 cp_lexer_consume_token (parser->lexer);
3655 /* This function is called at the end of a statement or declaration.
3656 If the next token is a semicolon, it is consumed; otherwise, error
3657 recovery is attempted. */
3659 static void
3660 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3662 /* Look for the trailing `;'. */
3663 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3665 /* If there is additional (erroneous) input, skip to the end of
3666 the statement. */
3667 cp_parser_skip_to_end_of_statement (parser);
3668 /* If the next token is now a `;', consume it. */
3669 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3670 cp_lexer_consume_token (parser->lexer);
3674 /* Skip tokens until we have consumed an entire block, or until we
3675 have consumed a non-nested `;'. */
3677 static void
3678 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3680 int nesting_depth = 0;
3682 /* Unwind generic function template scope if necessary. */
3683 if (parser->fully_implicit_function_template_p)
3684 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3686 while (nesting_depth >= 0)
3688 cp_token *token = cp_lexer_peek_token (parser->lexer);
3690 switch (token->type)
3692 case CPP_EOF:
3693 case CPP_PRAGMA_EOL:
3694 /* If we've run out of tokens, stop. */
3695 return;
3697 case CPP_SEMICOLON:
3698 /* Stop if this is an unnested ';'. */
3699 if (!nesting_depth)
3700 nesting_depth = -1;
3701 break;
3703 case CPP_CLOSE_BRACE:
3704 /* Stop if this is an unnested '}', or closes the outermost
3705 nesting level. */
3706 nesting_depth--;
3707 if (nesting_depth < 0)
3708 return;
3709 if (!nesting_depth)
3710 nesting_depth = -1;
3711 break;
3713 case CPP_OPEN_BRACE:
3714 /* Nest. */
3715 nesting_depth++;
3716 break;
3718 default:
3719 break;
3722 /* Consume the token. */
3723 cp_lexer_consume_token (parser->lexer);
3727 /* Skip tokens until a non-nested closing curly brace is the next
3728 token, or there are no more tokens. Return true in the first case,
3729 false otherwise. */
3731 static bool
3732 cp_parser_skip_to_closing_brace (cp_parser *parser)
3734 unsigned nesting_depth = 0;
3736 while (true)
3738 cp_token *token = cp_lexer_peek_token (parser->lexer);
3740 switch (token->type)
3742 case CPP_EOF:
3743 case CPP_PRAGMA_EOL:
3744 /* If we've run out of tokens, stop. */
3745 return false;
3747 case CPP_CLOSE_BRACE:
3748 /* If the next token is a non-nested `}', then we have reached
3749 the end of the current block. */
3750 if (nesting_depth-- == 0)
3751 return true;
3752 break;
3754 case CPP_OPEN_BRACE:
3755 /* If it the next token is a `{', then we are entering a new
3756 block. Consume the entire block. */
3757 ++nesting_depth;
3758 break;
3760 default:
3761 break;
3764 /* Consume the token. */
3765 cp_lexer_consume_token (parser->lexer);
3769 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3770 parameter is the PRAGMA token, allowing us to purge the entire pragma
3771 sequence. */
3773 static void
3774 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3776 cp_token *token;
3778 parser->lexer->in_pragma = false;
3781 token = cp_lexer_consume_token (parser->lexer);
3782 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3784 /* Ensure that the pragma is not parsed again. */
3785 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3788 /* Require pragma end of line, resyncing with it as necessary. The
3789 arguments are as for cp_parser_skip_to_pragma_eol. */
3791 static void
3792 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3794 parser->lexer->in_pragma = false;
3795 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3799 /* This is a simple wrapper around make_typename_type. When the id is
3800 an unresolved identifier node, we can provide a superior diagnostic
3801 using cp_parser_diagnose_invalid_type_name. */
3803 static tree
3804 cp_parser_make_typename_type (cp_parser *parser, tree id,
3805 location_t id_location)
3807 tree result;
3808 if (identifier_p (id))
3810 result = make_typename_type (parser->scope, id, typename_type,
3811 /*complain=*/tf_none);
3812 if (result == error_mark_node)
3813 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3814 return result;
3816 return make_typename_type (parser->scope, id, typename_type, tf_error);
3819 /* This is a wrapper around the
3820 make_{pointer,ptrmem,reference}_declarator functions that decides
3821 which one to call based on the CODE and CLASS_TYPE arguments. The
3822 CODE argument should be one of the values returned by
3823 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3824 appertain to the pointer or reference. */
3826 static cp_declarator *
3827 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3828 cp_cv_quals cv_qualifiers,
3829 cp_declarator *target,
3830 tree attributes)
3832 if (code == ERROR_MARK)
3833 return cp_error_declarator;
3835 if (code == INDIRECT_REF)
3836 if (class_type == NULL_TREE)
3837 return make_pointer_declarator (cv_qualifiers, target, attributes);
3838 else
3839 return make_ptrmem_declarator (cv_qualifiers, class_type,
3840 target, attributes);
3841 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3842 return make_reference_declarator (cv_qualifiers, target,
3843 false, attributes);
3844 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3845 return make_reference_declarator (cv_qualifiers, target,
3846 true, attributes);
3847 gcc_unreachable ();
3850 /* Create a new C++ parser. */
3852 static cp_parser *
3853 cp_parser_new (void)
3855 cp_parser *parser;
3856 cp_lexer *lexer;
3857 unsigned i;
3859 /* cp_lexer_new_main is called before doing GC allocation because
3860 cp_lexer_new_main might load a PCH file. */
3861 lexer = cp_lexer_new_main ();
3863 /* Initialize the binops_by_token so that we can get the tree
3864 directly from the token. */
3865 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3866 binops_by_token[binops[i].token_type] = binops[i];
3868 parser = ggc_cleared_alloc<cp_parser> ();
3869 parser->lexer = lexer;
3870 parser->context = cp_parser_context_new (NULL);
3872 /* For now, we always accept GNU extensions. */
3873 parser->allow_gnu_extensions_p = 1;
3875 /* The `>' token is a greater-than operator, not the end of a
3876 template-id. */
3877 parser->greater_than_is_operator_p = true;
3879 parser->default_arg_ok_p = true;
3881 /* We are not parsing a constant-expression. */
3882 parser->integral_constant_expression_p = false;
3883 parser->allow_non_integral_constant_expression_p = false;
3884 parser->non_integral_constant_expression_p = false;
3886 /* Local variable names are not forbidden. */
3887 parser->local_variables_forbidden_p = false;
3889 /* We are not processing an `extern "C"' declaration. */
3890 parser->in_unbraced_linkage_specification_p = false;
3892 /* We are not processing a declarator. */
3893 parser->in_declarator_p = false;
3895 /* We are not processing a template-argument-list. */
3896 parser->in_template_argument_list_p = false;
3898 /* We are not in an iteration statement. */
3899 parser->in_statement = 0;
3901 /* We are not in a switch statement. */
3902 parser->in_switch_statement_p = false;
3904 /* We are not parsing a type-id inside an expression. */
3905 parser->in_type_id_in_expr_p = false;
3907 /* Declarations aren't implicitly extern "C". */
3908 parser->implicit_extern_c = false;
3910 /* String literals should be translated to the execution character set. */
3911 parser->translate_strings_p = true;
3913 /* We are not parsing a function body. */
3914 parser->in_function_body = false;
3916 /* We can correct until told otherwise. */
3917 parser->colon_corrects_to_scope_p = true;
3919 /* The unparsed function queue is empty. */
3920 push_unparsed_function_queues (parser);
3922 /* There are no classes being defined. */
3923 parser->num_classes_being_defined = 0;
3925 /* No template parameters apply. */
3926 parser->num_template_parameter_lists = 0;
3928 /* Special parsing data structures. */
3929 parser->omp_declare_simd = NULL;
3930 parser->cilk_simd_fn_info = NULL;
3931 parser->oacc_routine = NULL;
3933 /* Not declaring an implicit function template. */
3934 parser->auto_is_implicit_function_template_parm_p = false;
3935 parser->fully_implicit_function_template_p = false;
3936 parser->implicit_template_parms = 0;
3937 parser->implicit_template_scope = 0;
3939 /* Allow constrained-type-specifiers. */
3940 parser->prevent_constrained_type_specifiers = 0;
3942 /* We haven't yet seen an 'extern "C"'. */
3943 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3945 return parser;
3948 /* Create a cp_lexer structure which will emit the tokens in CACHE
3949 and push it onto the parser's lexer stack. This is used for delayed
3950 parsing of in-class method bodies and default arguments, and should
3951 not be confused with tentative parsing. */
3952 static void
3953 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3955 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3956 lexer->next = parser->lexer;
3957 parser->lexer = lexer;
3959 /* Move the current source position to that of the first token in the
3960 new lexer. */
3961 cp_lexer_set_source_position_from_token (lexer->next_token);
3964 /* Pop the top lexer off the parser stack. This is never used for the
3965 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3966 static void
3967 cp_parser_pop_lexer (cp_parser *parser)
3969 cp_lexer *lexer = parser->lexer;
3970 parser->lexer = lexer->next;
3971 cp_lexer_destroy (lexer);
3973 /* Put the current source position back where it was before this
3974 lexer was pushed. */
3975 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3978 /* Lexical conventions [gram.lex] */
3980 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3981 identifier. */
3983 static cp_expr
3984 cp_parser_identifier (cp_parser* parser)
3986 cp_token *token;
3988 /* Look for the identifier. */
3989 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3990 /* Return the value. */
3991 if (token)
3992 return cp_expr (token->u.value, token->location);
3993 else
3994 return error_mark_node;
3997 /* Parse a sequence of adjacent string constants. Returns a
3998 TREE_STRING representing the combined, nul-terminated string
3999 constant. If TRANSLATE is true, translate the string to the
4000 execution character set. If WIDE_OK is true, a wide string is
4001 invalid here.
4003 C++98 [lex.string] says that if a narrow string literal token is
4004 adjacent to a wide string literal token, the behavior is undefined.
4005 However, C99 6.4.5p4 says that this results in a wide string literal.
4006 We follow C99 here, for consistency with the C front end.
4008 This code is largely lifted from lex_string() in c-lex.c.
4010 FUTURE: ObjC++ will need to handle @-strings here. */
4011 static cp_expr
4012 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4013 bool lookup_udlit = true)
4015 tree value;
4016 size_t count;
4017 struct obstack str_ob;
4018 cpp_string str, istr, *strs;
4019 cp_token *tok;
4020 enum cpp_ttype type, curr_type;
4021 int have_suffix_p = 0;
4022 tree string_tree;
4023 tree suffix_id = NULL_TREE;
4024 bool curr_tok_is_userdef_p = false;
4026 tok = cp_lexer_peek_token (parser->lexer);
4027 if (!cp_parser_is_string_literal (tok))
4029 cp_parser_error (parser, "expected string-literal");
4030 return error_mark_node;
4033 location_t loc = tok->location;
4035 if (cpp_userdef_string_p (tok->type))
4037 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4038 curr_type = cpp_userdef_string_remove_type (tok->type);
4039 curr_tok_is_userdef_p = true;
4041 else
4043 string_tree = tok->u.value;
4044 curr_type = tok->type;
4046 type = curr_type;
4048 /* Try to avoid the overhead of creating and destroying an obstack
4049 for the common case of just one string. */
4050 if (!cp_parser_is_string_literal
4051 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4053 cp_lexer_consume_token (parser->lexer);
4055 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4056 str.len = TREE_STRING_LENGTH (string_tree);
4057 count = 1;
4059 if (curr_tok_is_userdef_p)
4061 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4062 have_suffix_p = 1;
4063 curr_type = cpp_userdef_string_remove_type (tok->type);
4065 else
4066 curr_type = tok->type;
4068 strs = &str;
4070 else
4072 location_t last_tok_loc = tok->location;
4073 gcc_obstack_init (&str_ob);
4074 count = 0;
4078 cp_lexer_consume_token (parser->lexer);
4079 count++;
4080 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4081 str.len = TREE_STRING_LENGTH (string_tree);
4083 if (curr_tok_is_userdef_p)
4085 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4086 if (have_suffix_p == 0)
4088 suffix_id = curr_suffix_id;
4089 have_suffix_p = 1;
4091 else if (have_suffix_p == 1
4092 && curr_suffix_id != suffix_id)
4094 error ("inconsistent user-defined literal suffixes"
4095 " %qD and %qD in string literal",
4096 suffix_id, curr_suffix_id);
4097 have_suffix_p = -1;
4099 curr_type = cpp_userdef_string_remove_type (tok->type);
4101 else
4102 curr_type = tok->type;
4104 if (type != curr_type)
4106 if (type == CPP_STRING)
4107 type = curr_type;
4108 else if (curr_type != CPP_STRING)
4110 rich_location rich_loc (line_table, tok->location);
4111 rich_loc.add_range (last_tok_loc, false);
4112 error_at (&rich_loc,
4113 "unsupported non-standard concatenation "
4114 "of string literals");
4118 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4120 last_tok_loc = tok->location;
4122 tok = cp_lexer_peek_token (parser->lexer);
4123 if (cpp_userdef_string_p (tok->type))
4125 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4126 curr_type = cpp_userdef_string_remove_type (tok->type);
4127 curr_tok_is_userdef_p = true;
4129 else
4131 string_tree = tok->u.value;
4132 curr_type = tok->type;
4133 curr_tok_is_userdef_p = false;
4136 while (cp_parser_is_string_literal (tok));
4138 /* A string literal built by concatenation has its caret=start at
4139 the start of the initial string, and its finish at the finish of
4140 the final string literal. */
4141 loc = make_location (loc, loc, get_finish (last_tok_loc));
4143 strs = (cpp_string *) obstack_finish (&str_ob);
4146 if (type != CPP_STRING && !wide_ok)
4148 cp_parser_error (parser, "a wide string is invalid in this context");
4149 type = CPP_STRING;
4152 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4153 (parse_in, strs, count, &istr, type))
4155 value = build_string (istr.len, (const char *)istr.text);
4156 free (CONST_CAST (unsigned char *, istr.text));
4158 switch (type)
4160 default:
4161 case CPP_STRING:
4162 case CPP_UTF8STRING:
4163 TREE_TYPE (value) = char_array_type_node;
4164 break;
4165 case CPP_STRING16:
4166 TREE_TYPE (value) = char16_array_type_node;
4167 break;
4168 case CPP_STRING32:
4169 TREE_TYPE (value) = char32_array_type_node;
4170 break;
4171 case CPP_WSTRING:
4172 TREE_TYPE (value) = wchar_array_type_node;
4173 break;
4176 value = fix_string_type (value);
4178 if (have_suffix_p)
4180 tree literal = build_userdef_literal (suffix_id, value,
4181 OT_NONE, NULL_TREE);
4182 if (lookup_udlit)
4183 value = cp_parser_userdef_string_literal (literal);
4184 else
4185 value = literal;
4188 else
4189 /* cpp_interpret_string has issued an error. */
4190 value = error_mark_node;
4192 if (count > 1)
4193 obstack_free (&str_ob, 0);
4195 return cp_expr (value, loc);
4198 /* Look up a literal operator with the name and the exact arguments. */
4200 static tree
4201 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4203 tree decl;
4204 decl = lookup_name (name);
4205 if (!decl || !is_overloaded_fn (decl))
4206 return error_mark_node;
4208 for (lkp_iterator iter (decl); iter; ++iter)
4210 unsigned int ix;
4211 bool found = true;
4212 tree fn = *iter;
4213 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4214 if (parmtypes != NULL_TREE)
4216 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4217 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4219 tree tparm = TREE_VALUE (parmtypes);
4220 tree targ = TREE_TYPE ((*args)[ix]);
4221 bool ptr = TYPE_PTR_P (tparm);
4222 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4223 if ((ptr || arr || !same_type_p (tparm, targ))
4224 && (!ptr || !arr
4225 || !same_type_p (TREE_TYPE (tparm),
4226 TREE_TYPE (targ))))
4227 found = false;
4229 if (found
4230 && ix == vec_safe_length (args)
4231 /* May be this should be sufficient_parms_p instead,
4232 depending on how exactly should user-defined literals
4233 work in presence of default arguments on the literal
4234 operator parameters. */
4235 && parmtypes == void_list_node)
4236 return decl;
4240 return error_mark_node;
4243 /* Parse a user-defined char constant. Returns a call to a user-defined
4244 literal operator taking the character as an argument. */
4246 static cp_expr
4247 cp_parser_userdef_char_literal (cp_parser *parser)
4249 cp_token *token = cp_lexer_consume_token (parser->lexer);
4250 tree literal = token->u.value;
4251 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4252 tree value = USERDEF_LITERAL_VALUE (literal);
4253 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4254 tree decl, result;
4256 /* Build up a call to the user-defined operator */
4257 /* Lookup the name we got back from the id-expression. */
4258 vec<tree, va_gc> *args = make_tree_vector ();
4259 vec_safe_push (args, value);
4260 decl = lookup_literal_operator (name, args);
4261 if (!decl || decl == error_mark_node)
4263 error ("unable to find character literal operator %qD with %qT argument",
4264 name, TREE_TYPE (value));
4265 release_tree_vector (args);
4266 return error_mark_node;
4268 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4269 release_tree_vector (args);
4270 return result;
4273 /* A subroutine of cp_parser_userdef_numeric_literal to
4274 create a char... template parameter pack from a string node. */
4276 static tree
4277 make_char_string_pack (tree value)
4279 tree charvec;
4280 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4281 const char *str = TREE_STRING_POINTER (value);
4282 int i, len = TREE_STRING_LENGTH (value) - 1;
4283 tree argvec = make_tree_vec (1);
4285 /* Fill in CHARVEC with all of the parameters. */
4286 charvec = make_tree_vec (len);
4287 for (i = 0; i < len; ++i)
4288 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4290 /* Build the argument packs. */
4291 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4293 TREE_VEC_ELT (argvec, 0) = argpack;
4295 return argvec;
4298 /* A subroutine of cp_parser_userdef_numeric_literal to
4299 create a char... template parameter pack from a string node. */
4301 static tree
4302 make_string_pack (tree value)
4304 tree charvec;
4305 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4306 const unsigned char *str
4307 = (const unsigned char *) TREE_STRING_POINTER (value);
4308 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4309 int len = TREE_STRING_LENGTH (value) / sz - 1;
4310 tree argvec = make_tree_vec (2);
4312 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4313 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4315 /* First template parm is character type. */
4316 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4318 /* Fill in CHARVEC with all of the parameters. */
4319 charvec = make_tree_vec (len);
4320 for (int i = 0; i < len; ++i)
4321 TREE_VEC_ELT (charvec, i)
4322 = double_int_to_tree (str_char_type_node,
4323 double_int::from_buffer (str + i * sz, sz));
4325 /* Build the argument packs. */
4326 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4328 TREE_VEC_ELT (argvec, 1) = argpack;
4330 return argvec;
4333 /* Parse a user-defined numeric constant. returns a call to a user-defined
4334 literal operator. */
4336 static cp_expr
4337 cp_parser_userdef_numeric_literal (cp_parser *parser)
4339 cp_token *token = cp_lexer_consume_token (parser->lexer);
4340 tree literal = token->u.value;
4341 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4342 tree value = USERDEF_LITERAL_VALUE (literal);
4343 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4344 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4345 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4346 tree decl, result;
4347 vec<tree, va_gc> *args;
4349 /* Look for a literal operator taking the exact type of numeric argument
4350 as the literal value. */
4351 args = make_tree_vector ();
4352 vec_safe_push (args, value);
4353 decl = lookup_literal_operator (name, args);
4354 if (decl && decl != error_mark_node)
4356 result = finish_call_expr (decl, &args, false, true,
4357 tf_warning_or_error);
4359 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4361 warning_at (token->location, OPT_Woverflow,
4362 "integer literal exceeds range of %qT type",
4363 long_long_unsigned_type_node);
4365 else
4367 if (overflow > 0)
4368 warning_at (token->location, OPT_Woverflow,
4369 "floating literal exceeds range of %qT type",
4370 long_double_type_node);
4371 else if (overflow < 0)
4372 warning_at (token->location, OPT_Woverflow,
4373 "floating literal truncated to zero");
4376 release_tree_vector (args);
4377 return result;
4379 release_tree_vector (args);
4381 /* If the numeric argument didn't work, look for a raw literal
4382 operator taking a const char* argument consisting of the number
4383 in string format. */
4384 args = make_tree_vector ();
4385 vec_safe_push (args, num_string);
4386 decl = lookup_literal_operator (name, args);
4387 if (decl && decl != error_mark_node)
4389 result = finish_call_expr (decl, &args, false, true,
4390 tf_warning_or_error);
4391 release_tree_vector (args);
4392 return result;
4394 release_tree_vector (args);
4396 /* If the raw literal didn't work, look for a non-type template
4397 function with parameter pack char.... Call the function with
4398 template parameter characters representing the number. */
4399 args = make_tree_vector ();
4400 decl = lookup_literal_operator (name, args);
4401 if (decl && decl != error_mark_node)
4403 tree tmpl_args = make_char_string_pack (num_string);
4404 decl = lookup_template_function (decl, tmpl_args);
4405 result = finish_call_expr (decl, &args, false, true,
4406 tf_warning_or_error);
4407 release_tree_vector (args);
4408 return result;
4411 release_tree_vector (args);
4413 error ("unable to find numeric literal operator %qD", name);
4414 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4415 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4416 "to enable more built-in suffixes");
4417 return error_mark_node;
4420 /* Parse a user-defined string constant. Returns a call to a user-defined
4421 literal operator taking a character pointer and the length of the string
4422 as arguments. */
4424 static tree
4425 cp_parser_userdef_string_literal (tree literal)
4427 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4428 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4429 tree value = USERDEF_LITERAL_VALUE (literal);
4430 int len = TREE_STRING_LENGTH (value)
4431 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4432 tree decl, result;
4433 vec<tree, va_gc> *args;
4435 /* Build up a call to the user-defined operator. */
4436 /* Lookup the name we got back from the id-expression. */
4437 args = make_tree_vector ();
4438 vec_safe_push (args, value);
4439 vec_safe_push (args, build_int_cst (size_type_node, len));
4440 decl = lookup_literal_operator (name, args);
4442 if (decl && decl != error_mark_node)
4444 result = finish_call_expr (decl, &args, false, true,
4445 tf_warning_or_error);
4446 release_tree_vector (args);
4447 return result;
4449 release_tree_vector (args);
4451 /* Look for a template function with typename parameter CharT
4452 and parameter pack CharT... Call the function with
4453 template parameter characters representing the string. */
4454 args = make_tree_vector ();
4455 decl = lookup_literal_operator (name, args);
4456 if (decl && decl != error_mark_node)
4458 tree tmpl_args = make_string_pack (value);
4459 decl = lookup_template_function (decl, tmpl_args);
4460 result = finish_call_expr (decl, &args, false, true,
4461 tf_warning_or_error);
4462 release_tree_vector (args);
4463 return result;
4465 release_tree_vector (args);
4467 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4468 name, TREE_TYPE (value), size_type_node);
4469 return error_mark_node;
4473 /* Basic concepts [gram.basic] */
4475 /* Parse a translation-unit.
4477 translation-unit:
4478 declaration-seq [opt]
4480 Returns TRUE if all went well. */
4482 static bool
4483 cp_parser_translation_unit (cp_parser* parser)
4485 /* The address of the first non-permanent object on the declarator
4486 obstack. */
4487 static void *declarator_obstack_base;
4489 bool success;
4491 /* Create the declarator obstack, if necessary. */
4492 if (!cp_error_declarator)
4494 gcc_obstack_init (&declarator_obstack);
4495 /* Create the error declarator. */
4496 cp_error_declarator = make_declarator (cdk_error);
4497 /* Create the empty parameter list. */
4498 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4499 UNKNOWN_LOCATION);
4500 /* Remember where the base of the declarator obstack lies. */
4501 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4504 cp_parser_declaration_seq_opt (parser);
4506 /* If there are no tokens left then all went well. */
4507 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4509 /* Get rid of the token array; we don't need it any more. */
4510 cp_lexer_destroy (parser->lexer);
4511 parser->lexer = NULL;
4513 /* This file might have been a context that's implicitly extern
4514 "C". If so, pop the lang context. (Only relevant for PCH.) */
4515 if (parser->implicit_extern_c)
4517 pop_lang_context ();
4518 parser->implicit_extern_c = false;
4521 /* Finish up. */
4522 finish_translation_unit ();
4524 success = true;
4526 else
4528 cp_parser_error (parser, "expected declaration");
4529 success = false;
4532 /* Make sure the declarator obstack was fully cleaned up. */
4533 gcc_assert (obstack_next_free (&declarator_obstack)
4534 == declarator_obstack_base);
4536 /* All went well. */
4537 return success;
4540 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4541 decltype context. */
4543 static inline tsubst_flags_t
4544 complain_flags (bool decltype_p)
4546 tsubst_flags_t complain = tf_warning_or_error;
4547 if (decltype_p)
4548 complain |= tf_decltype;
4549 return complain;
4552 /* We're about to parse a collection of statements. If we're currently
4553 parsing tentatively, set up a firewall so that any nested
4554 cp_parser_commit_to_tentative_parse won't affect the current context. */
4556 static cp_token_position
4557 cp_parser_start_tentative_firewall (cp_parser *parser)
4559 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4560 return 0;
4562 cp_parser_parse_tentatively (parser);
4563 cp_parser_commit_to_topmost_tentative_parse (parser);
4564 return cp_lexer_token_position (parser->lexer, false);
4567 /* We've finished parsing the collection of statements. Wrap up the
4568 firewall and replace the relevant tokens with the parsed form. */
4570 static void
4571 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4572 tree expr)
4574 if (!start)
4575 return;
4577 /* Finish the firewall level. */
4578 cp_parser_parse_definitely (parser);
4579 /* And remember the result of the parse for when we try again. */
4580 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4581 token->type = CPP_PREPARSED_EXPR;
4582 token->u.value = expr;
4583 token->keyword = RID_MAX;
4584 cp_lexer_purge_tokens_after (parser->lexer, start);
4587 /* Like the above functions, but let the user modify the tokens. Used by
4588 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4589 later parses, so it makes sense to localize the effects of
4590 cp_parser_commit_to_tentative_parse. */
4592 struct tentative_firewall
4594 cp_parser *parser;
4595 bool set;
4597 tentative_firewall (cp_parser *p): parser(p)
4599 /* If we're currently parsing tentatively, start a committed level as a
4600 firewall and then an inner tentative parse. */
4601 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4603 cp_parser_parse_tentatively (parser);
4604 cp_parser_commit_to_topmost_tentative_parse (parser);
4605 cp_parser_parse_tentatively (parser);
4609 ~tentative_firewall()
4611 if (set)
4613 /* Finish the inner tentative parse and the firewall, propagating any
4614 uncommitted error state to the outer tentative parse. */
4615 bool err = cp_parser_error_occurred (parser);
4616 cp_parser_parse_definitely (parser);
4617 cp_parser_parse_definitely (parser);
4618 if (err)
4619 cp_parser_simulate_error (parser);
4624 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4625 This class is for tracking such a matching pair of symbols.
4626 In particular, it tracks the location of the first token,
4627 so that if the second token is missing, we can highlight the
4628 location of the first token when notifying the user about the
4629 problem. */
4631 template <typename traits_t>
4632 class token_pair
4634 public:
4635 /* token_pair's ctor. */
4636 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4638 /* If the next token is the opening symbol for this pair, consume it and
4639 return true.
4640 Otherwise, issue an error and return false.
4641 In either case, record the location of the opening token. */
4643 bool require_open (cp_parser *parser)
4645 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4646 return cp_parser_require (parser, traits_t::open_token_type,
4647 traits_t::required_token_open);
4650 /* Consume the next token from PARSER, recording its location as
4651 that of the opening token within the pair. */
4653 cp_token * consume_open (cp_parser *parser)
4655 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4656 gcc_assert (tok->type == traits_t::open_token_type);
4657 m_open_loc = tok->location;
4658 return tok;
4661 /* If the next token is the closing symbol for this pair, consume it
4662 and return it.
4663 Otherwise, issue an error, highlighting the location of the
4664 corresponding opening token, and return NULL. */
4666 cp_token *require_close (cp_parser *parser) const
4668 return cp_parser_require (parser, traits_t::close_token_type,
4669 traits_t::required_token_close,
4670 m_open_loc);
4673 private:
4674 location_t m_open_loc;
4677 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4679 struct matching_paren_traits
4681 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4682 static const enum required_token required_token_open = RT_OPEN_PAREN;
4683 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4684 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4687 /* "matching_parens" is a token_pair<T> class for tracking matching
4688 pairs of parentheses. */
4690 typedef token_pair<matching_paren_traits> matching_parens;
4692 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4694 struct matching_brace_traits
4696 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4697 static const enum required_token required_token_open = RT_OPEN_BRACE;
4698 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4699 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4702 /* "matching_braces" is a token_pair<T> class for tracking matching
4703 pairs of braces. */
4705 typedef token_pair<matching_brace_traits> matching_braces;
4708 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4709 enclosing parentheses. */
4711 static cp_expr
4712 cp_parser_statement_expr (cp_parser *parser)
4714 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4716 /* Consume the '('. */
4717 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4718 matching_parens parens;
4719 parens.consume_open (parser);
4720 /* Start the statement-expression. */
4721 tree expr = begin_stmt_expr ();
4722 /* Parse the compound-statement. */
4723 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4724 /* Finish up. */
4725 expr = finish_stmt_expr (expr, false);
4726 /* Consume the ')'. */
4727 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4728 if (!parens.require_close (parser))
4729 cp_parser_skip_to_end_of_statement (parser);
4731 cp_parser_end_tentative_firewall (parser, start, expr);
4732 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4733 return cp_expr (expr, combined_loc);
4736 /* Expressions [gram.expr] */
4738 /* Parse a fold-operator.
4740 fold-operator:
4741 - * / % ^ & | = < > << >>
4742 = -= *= /= %= ^= &= |= <<= >>=
4743 == != <= >= && || , .* ->*
4745 This returns the tree code corresponding to the matched operator
4746 as an int. When the current token matches a compound assignment
4747 opertor, the resulting tree code is the negative value of the
4748 non-assignment operator. */
4750 static int
4751 cp_parser_fold_operator (cp_token *token)
4753 switch (token->type)
4755 case CPP_PLUS: return PLUS_EXPR;
4756 case CPP_MINUS: return MINUS_EXPR;
4757 case CPP_MULT: return MULT_EXPR;
4758 case CPP_DIV: return TRUNC_DIV_EXPR;
4759 case CPP_MOD: return TRUNC_MOD_EXPR;
4760 case CPP_XOR: return BIT_XOR_EXPR;
4761 case CPP_AND: return BIT_AND_EXPR;
4762 case CPP_OR: return BIT_IOR_EXPR;
4763 case CPP_LSHIFT: return LSHIFT_EXPR;
4764 case CPP_RSHIFT: return RSHIFT_EXPR;
4766 case CPP_EQ: return -NOP_EXPR;
4767 case CPP_PLUS_EQ: return -PLUS_EXPR;
4768 case CPP_MINUS_EQ: return -MINUS_EXPR;
4769 case CPP_MULT_EQ: return -MULT_EXPR;
4770 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4771 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4772 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4773 case CPP_AND_EQ: return -BIT_AND_EXPR;
4774 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4775 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4776 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4778 case CPP_EQ_EQ: return EQ_EXPR;
4779 case CPP_NOT_EQ: return NE_EXPR;
4780 case CPP_LESS: return LT_EXPR;
4781 case CPP_GREATER: return GT_EXPR;
4782 case CPP_LESS_EQ: return LE_EXPR;
4783 case CPP_GREATER_EQ: return GE_EXPR;
4785 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4786 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4788 case CPP_COMMA: return COMPOUND_EXPR;
4790 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4791 case CPP_DEREF_STAR: return MEMBER_REF;
4793 default: return ERROR_MARK;
4797 /* Returns true if CODE indicates a binary expression, which is not allowed in
4798 the LHS of a fold-expression. More codes will need to be added to use this
4799 function in other contexts. */
4801 static bool
4802 is_binary_op (tree_code code)
4804 switch (code)
4806 case PLUS_EXPR:
4807 case POINTER_PLUS_EXPR:
4808 case MINUS_EXPR:
4809 case MULT_EXPR:
4810 case TRUNC_DIV_EXPR:
4811 case TRUNC_MOD_EXPR:
4812 case BIT_XOR_EXPR:
4813 case BIT_AND_EXPR:
4814 case BIT_IOR_EXPR:
4815 case LSHIFT_EXPR:
4816 case RSHIFT_EXPR:
4818 case MODOP_EXPR:
4820 case EQ_EXPR:
4821 case NE_EXPR:
4822 case LE_EXPR:
4823 case GE_EXPR:
4824 case LT_EXPR:
4825 case GT_EXPR:
4827 case TRUTH_ANDIF_EXPR:
4828 case TRUTH_ORIF_EXPR:
4830 case COMPOUND_EXPR:
4832 case DOTSTAR_EXPR:
4833 case MEMBER_REF:
4834 return true;
4836 default:
4837 return false;
4841 /* If the next token is a suitable fold operator, consume it and return as
4842 the function above. */
4844 static int
4845 cp_parser_fold_operator (cp_parser *parser)
4847 cp_token* token = cp_lexer_peek_token (parser->lexer);
4848 int code = cp_parser_fold_operator (token);
4849 if (code != ERROR_MARK)
4850 cp_lexer_consume_token (parser->lexer);
4851 return code;
4854 /* Parse a fold-expression.
4856 fold-expression:
4857 ( ... folding-operator cast-expression)
4858 ( cast-expression folding-operator ... )
4859 ( cast-expression folding operator ... folding-operator cast-expression)
4861 Note that the '(' and ')' are matched in primary expression. */
4863 static cp_expr
4864 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4866 cp_id_kind pidk;
4868 // Left fold.
4869 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4871 cp_lexer_consume_token (parser->lexer);
4872 int op = cp_parser_fold_operator (parser);
4873 if (op == ERROR_MARK)
4875 cp_parser_error (parser, "expected binary operator");
4876 return error_mark_node;
4879 tree expr = cp_parser_cast_expression (parser, false, false,
4880 false, &pidk);
4881 if (expr == error_mark_node)
4882 return error_mark_node;
4883 return finish_left_unary_fold_expr (expr, op);
4886 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4887 int op = cp_parser_fold_operator (parser);
4888 if (op == ERROR_MARK)
4890 cp_parser_error (parser, "expected binary operator");
4891 return error_mark_node;
4894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4896 cp_parser_error (parser, "expected ...");
4897 return error_mark_node;
4899 cp_lexer_consume_token (parser->lexer);
4901 /* The operands of a fold-expression are cast-expressions, so binary or
4902 conditional expressions are not allowed. We check this here to avoid
4903 tentative parsing. */
4904 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4905 /* OK, the expression was parenthesized. */;
4906 else if (is_binary_op (TREE_CODE (expr1)))
4907 error_at (location_of (expr1),
4908 "binary expression in operand of fold-expression");
4909 else if (TREE_CODE (expr1) == COND_EXPR)
4910 error_at (location_of (expr1),
4911 "conditional expression in operand of fold-expression");
4913 // Right fold.
4914 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4915 return finish_right_unary_fold_expr (expr1, op);
4917 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4919 cp_parser_error (parser, "mismatched operator in fold-expression");
4920 return error_mark_node;
4922 cp_lexer_consume_token (parser->lexer);
4924 // Binary left or right fold.
4925 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4926 if (expr2 == error_mark_node)
4927 return error_mark_node;
4928 return finish_binary_fold_expr (expr1, expr2, op);
4931 /* Parse a primary-expression.
4933 primary-expression:
4934 literal
4935 this
4936 ( expression )
4937 id-expression
4938 lambda-expression (C++11)
4940 GNU Extensions:
4942 primary-expression:
4943 ( compound-statement )
4944 __builtin_va_arg ( assignment-expression , type-id )
4945 __builtin_offsetof ( type-id , offsetof-expression )
4947 C++ Extensions:
4948 __has_nothrow_assign ( type-id )
4949 __has_nothrow_constructor ( type-id )
4950 __has_nothrow_copy ( type-id )
4951 __has_trivial_assign ( type-id )
4952 __has_trivial_constructor ( type-id )
4953 __has_trivial_copy ( type-id )
4954 __has_trivial_destructor ( type-id )
4955 __has_virtual_destructor ( type-id )
4956 __is_abstract ( type-id )
4957 __is_base_of ( type-id , type-id )
4958 __is_class ( type-id )
4959 __is_empty ( type-id )
4960 __is_enum ( type-id )
4961 __is_final ( type-id )
4962 __is_literal_type ( type-id )
4963 __is_pod ( type-id )
4964 __is_polymorphic ( type-id )
4965 __is_std_layout ( type-id )
4966 __is_trivial ( type-id )
4967 __is_union ( type-id )
4969 Objective-C++ Extension:
4971 primary-expression:
4972 objc-expression
4974 literal:
4975 __null
4977 ADDRESS_P is true iff this expression was immediately preceded by
4978 "&" and therefore might denote a pointer-to-member. CAST_P is true
4979 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4980 true iff this expression is a template argument.
4982 Returns a representation of the expression. Upon return, *IDK
4983 indicates what kind of id-expression (if any) was present. */
4985 static cp_expr
4986 cp_parser_primary_expression (cp_parser *parser,
4987 bool address_p,
4988 bool cast_p,
4989 bool template_arg_p,
4990 bool decltype_p,
4991 cp_id_kind *idk)
4993 cp_token *token = NULL;
4995 /* Assume the primary expression is not an id-expression. */
4996 *idk = CP_ID_KIND_NONE;
4998 /* Peek at the next token. */
4999 token = cp_lexer_peek_token (parser->lexer);
5000 switch ((int) token->type)
5002 /* literal:
5003 integer-literal
5004 character-literal
5005 floating-literal
5006 string-literal
5007 boolean-literal
5008 pointer-literal
5009 user-defined-literal */
5010 case CPP_CHAR:
5011 case CPP_CHAR16:
5012 case CPP_CHAR32:
5013 case CPP_WCHAR:
5014 case CPP_UTF8CHAR:
5015 case CPP_NUMBER:
5016 case CPP_PREPARSED_EXPR:
5017 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5018 return cp_parser_userdef_numeric_literal (parser);
5019 token = cp_lexer_consume_token (parser->lexer);
5020 if (TREE_CODE (token->u.value) == FIXED_CST)
5022 error_at (token->location,
5023 "fixed-point types not supported in C++");
5024 return error_mark_node;
5026 /* Floating-point literals are only allowed in an integral
5027 constant expression if they are cast to an integral or
5028 enumeration type. */
5029 if (TREE_CODE (token->u.value) == REAL_CST
5030 && parser->integral_constant_expression_p
5031 && pedantic)
5033 /* CAST_P will be set even in invalid code like "int(2.7 +
5034 ...)". Therefore, we have to check that the next token
5035 is sure to end the cast. */
5036 if (cast_p)
5038 cp_token *next_token;
5040 next_token = cp_lexer_peek_token (parser->lexer);
5041 if (/* The comma at the end of an
5042 enumerator-definition. */
5043 next_token->type != CPP_COMMA
5044 /* The curly brace at the end of an enum-specifier. */
5045 && next_token->type != CPP_CLOSE_BRACE
5046 /* The end of a statement. */
5047 && next_token->type != CPP_SEMICOLON
5048 /* The end of the cast-expression. */
5049 && next_token->type != CPP_CLOSE_PAREN
5050 /* The end of an array bound. */
5051 && next_token->type != CPP_CLOSE_SQUARE
5052 /* The closing ">" in a template-argument-list. */
5053 && (next_token->type != CPP_GREATER
5054 || parser->greater_than_is_operator_p)
5055 /* C++0x only: A ">>" treated like two ">" tokens,
5056 in a template-argument-list. */
5057 && (next_token->type != CPP_RSHIFT
5058 || (cxx_dialect == cxx98)
5059 || parser->greater_than_is_operator_p))
5060 cast_p = false;
5063 /* If we are within a cast, then the constraint that the
5064 cast is to an integral or enumeration type will be
5065 checked at that point. If we are not within a cast, then
5066 this code is invalid. */
5067 if (!cast_p)
5068 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5070 return cp_expr (token->u.value, token->location);
5072 case CPP_CHAR_USERDEF:
5073 case CPP_CHAR16_USERDEF:
5074 case CPP_CHAR32_USERDEF:
5075 case CPP_WCHAR_USERDEF:
5076 case CPP_UTF8CHAR_USERDEF:
5077 return cp_parser_userdef_char_literal (parser);
5079 case CPP_STRING:
5080 case CPP_STRING16:
5081 case CPP_STRING32:
5082 case CPP_WSTRING:
5083 case CPP_UTF8STRING:
5084 case CPP_STRING_USERDEF:
5085 case CPP_STRING16_USERDEF:
5086 case CPP_STRING32_USERDEF:
5087 case CPP_WSTRING_USERDEF:
5088 case CPP_UTF8STRING_USERDEF:
5089 /* ??? Should wide strings be allowed when parser->translate_strings_p
5090 is false (i.e. in attributes)? If not, we can kill the third
5091 argument to cp_parser_string_literal. */
5092 return cp_parser_string_literal (parser,
5093 parser->translate_strings_p,
5094 true);
5096 case CPP_OPEN_PAREN:
5097 /* If we see `( { ' then we are looking at the beginning of
5098 a GNU statement-expression. */
5099 if (cp_parser_allow_gnu_extensions_p (parser)
5100 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5102 /* Statement-expressions are not allowed by the standard. */
5103 pedwarn (token->location, OPT_Wpedantic,
5104 "ISO C++ forbids braced-groups within expressions");
5106 /* And they're not allowed outside of a function-body; you
5107 cannot, for example, write:
5109 int i = ({ int j = 3; j + 1; });
5111 at class or namespace scope. */
5112 if (!parser->in_function_body
5113 || parser->in_template_argument_list_p)
5115 error_at (token->location,
5116 "statement-expressions are not allowed outside "
5117 "functions nor in template-argument lists");
5118 cp_parser_skip_to_end_of_block_or_statement (parser);
5119 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5120 cp_lexer_consume_token (parser->lexer);
5121 return error_mark_node;
5123 else
5124 return cp_parser_statement_expr (parser);
5126 /* Otherwise it's a normal parenthesized expression. */
5128 cp_expr expr;
5129 bool saved_greater_than_is_operator_p;
5131 location_t open_paren_loc = token->location;
5133 /* Consume the `('. */
5134 matching_parens parens;
5135 parens.consume_open (parser);
5136 /* Within a parenthesized expression, a `>' token is always
5137 the greater-than operator. */
5138 saved_greater_than_is_operator_p
5139 = parser->greater_than_is_operator_p;
5140 parser->greater_than_is_operator_p = true;
5142 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5143 /* Left fold expression. */
5144 expr = NULL_TREE;
5145 else
5146 /* Parse the parenthesized expression. */
5147 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5149 token = cp_lexer_peek_token (parser->lexer);
5150 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5152 expr = cp_parser_fold_expression (parser, expr);
5153 if (expr != error_mark_node
5154 && cxx_dialect < cxx17
5155 && !in_system_header_at (input_location))
5156 pedwarn (input_location, 0, "fold-expressions only available "
5157 "with -std=c++17 or -std=gnu++17");
5159 else
5160 /* Let the front end know that this expression was
5161 enclosed in parentheses. This matters in case, for
5162 example, the expression is of the form `A::B', since
5163 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5164 not. */
5165 expr = finish_parenthesized_expr (expr);
5167 /* DR 705: Wrapping an unqualified name in parentheses
5168 suppresses arg-dependent lookup. We want to pass back
5169 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5170 (c++/37862), but none of the others. */
5171 if (*idk != CP_ID_KIND_QUALIFIED)
5172 *idk = CP_ID_KIND_NONE;
5174 /* The `>' token might be the end of a template-id or
5175 template-parameter-list now. */
5176 parser->greater_than_is_operator_p
5177 = saved_greater_than_is_operator_p;
5179 /* Consume the `)'. */
5180 token = cp_lexer_peek_token (parser->lexer);
5181 location_t close_paren_loc = token->location;
5182 expr.set_range (open_paren_loc, close_paren_loc);
5183 if (!parens.require_close (parser)
5184 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5185 cp_parser_skip_to_end_of_statement (parser);
5187 return expr;
5190 case CPP_OPEN_SQUARE:
5192 if (c_dialect_objc ())
5194 /* We might have an Objective-C++ message. */
5195 cp_parser_parse_tentatively (parser);
5196 tree msg = cp_parser_objc_message_expression (parser);
5197 /* If that works out, we're done ... */
5198 if (cp_parser_parse_definitely (parser))
5199 return msg;
5200 /* ... else, fall though to see if it's a lambda. */
5202 cp_expr lam = cp_parser_lambda_expression (parser);
5203 /* Don't warn about a failed tentative parse. */
5204 if (cp_parser_error_occurred (parser))
5205 return error_mark_node;
5206 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5207 return lam;
5210 case CPP_OBJC_STRING:
5211 if (c_dialect_objc ())
5212 /* We have an Objective-C++ string literal. */
5213 return cp_parser_objc_expression (parser);
5214 cp_parser_error (parser, "expected primary-expression");
5215 return error_mark_node;
5217 case CPP_KEYWORD:
5218 switch (token->keyword)
5220 /* These two are the boolean literals. */
5221 case RID_TRUE:
5222 cp_lexer_consume_token (parser->lexer);
5223 return cp_expr (boolean_true_node, token->location);
5224 case RID_FALSE:
5225 cp_lexer_consume_token (parser->lexer);
5226 return cp_expr (boolean_false_node, token->location);
5228 /* The `__null' literal. */
5229 case RID_NULL:
5230 cp_lexer_consume_token (parser->lexer);
5231 return cp_expr (null_node, token->location);
5233 /* The `nullptr' literal. */
5234 case RID_NULLPTR:
5235 cp_lexer_consume_token (parser->lexer);
5236 return cp_expr (nullptr_node, token->location);
5238 /* Recognize the `this' keyword. */
5239 case RID_THIS:
5240 cp_lexer_consume_token (parser->lexer);
5241 if (parser->local_variables_forbidden_p)
5243 error_at (token->location,
5244 "%<this%> may not be used in this context");
5245 return error_mark_node;
5247 /* Pointers cannot appear in constant-expressions. */
5248 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5249 return error_mark_node;
5250 return cp_expr (finish_this_expr (), token->location);
5252 /* The `operator' keyword can be the beginning of an
5253 id-expression. */
5254 case RID_OPERATOR:
5255 goto id_expression;
5257 case RID_FUNCTION_NAME:
5258 case RID_PRETTY_FUNCTION_NAME:
5259 case RID_C99_FUNCTION_NAME:
5261 non_integral_constant name;
5263 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5264 __func__ are the names of variables -- but they are
5265 treated specially. Therefore, they are handled here,
5266 rather than relying on the generic id-expression logic
5267 below. Grammatically, these names are id-expressions.
5269 Consume the token. */
5270 token = cp_lexer_consume_token (parser->lexer);
5272 switch (token->keyword)
5274 case RID_FUNCTION_NAME:
5275 name = NIC_FUNC_NAME;
5276 break;
5277 case RID_PRETTY_FUNCTION_NAME:
5278 name = NIC_PRETTY_FUNC;
5279 break;
5280 case RID_C99_FUNCTION_NAME:
5281 name = NIC_C99_FUNC;
5282 break;
5283 default:
5284 gcc_unreachable ();
5287 if (cp_parser_non_integral_constant_expression (parser, name))
5288 return error_mark_node;
5290 /* Look up the name. */
5291 return finish_fname (token->u.value);
5294 case RID_VA_ARG:
5296 tree expression;
5297 tree type;
5298 source_location type_location;
5299 location_t start_loc
5300 = cp_lexer_peek_token (parser->lexer)->location;
5301 /* The `__builtin_va_arg' construct is used to handle
5302 `va_arg'. Consume the `__builtin_va_arg' token. */
5303 cp_lexer_consume_token (parser->lexer);
5304 /* Look for the opening `('. */
5305 matching_parens parens;
5306 parens.require_open (parser);
5307 /* Now, parse the assignment-expression. */
5308 expression = cp_parser_assignment_expression (parser);
5309 /* Look for the `,'. */
5310 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5311 type_location = cp_lexer_peek_token (parser->lexer)->location;
5312 /* Parse the type-id. */
5314 type_id_in_expr_sentinel s (parser);
5315 type = cp_parser_type_id (parser);
5317 /* Look for the closing `)'. */
5318 location_t finish_loc
5319 = cp_lexer_peek_token (parser->lexer)->location;
5320 parens.require_close (parser);
5321 /* Using `va_arg' in a constant-expression is not
5322 allowed. */
5323 if (cp_parser_non_integral_constant_expression (parser,
5324 NIC_VA_ARG))
5325 return error_mark_node;
5326 /* Construct a location of the form:
5327 __builtin_va_arg (v, int)
5328 ~~~~~~~~~~~~~~~~~~~~~^~~~
5329 with the caret at the type, ranging from the start of the
5330 "__builtin_va_arg" token to the close paren. */
5331 location_t combined_loc
5332 = make_location (type_location, start_loc, finish_loc);
5333 return build_x_va_arg (combined_loc, expression, type);
5336 case RID_OFFSETOF:
5337 return cp_parser_builtin_offsetof (parser);
5339 case RID_HAS_NOTHROW_ASSIGN:
5340 case RID_HAS_NOTHROW_CONSTRUCTOR:
5341 case RID_HAS_NOTHROW_COPY:
5342 case RID_HAS_TRIVIAL_ASSIGN:
5343 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5344 case RID_HAS_TRIVIAL_COPY:
5345 case RID_HAS_TRIVIAL_DESTRUCTOR:
5346 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5347 case RID_HAS_VIRTUAL_DESTRUCTOR:
5348 case RID_IS_ABSTRACT:
5349 case RID_IS_AGGREGATE:
5350 case RID_IS_BASE_OF:
5351 case RID_IS_CLASS:
5352 case RID_IS_EMPTY:
5353 case RID_IS_ENUM:
5354 case RID_IS_FINAL:
5355 case RID_IS_LITERAL_TYPE:
5356 case RID_IS_POD:
5357 case RID_IS_POLYMORPHIC:
5358 case RID_IS_SAME_AS:
5359 case RID_IS_STD_LAYOUT:
5360 case RID_IS_TRIVIAL:
5361 case RID_IS_TRIVIALLY_ASSIGNABLE:
5362 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5363 case RID_IS_TRIVIALLY_COPYABLE:
5364 case RID_IS_UNION:
5365 case RID_IS_ASSIGNABLE:
5366 case RID_IS_CONSTRUCTIBLE:
5367 return cp_parser_trait_expr (parser, token->keyword);
5369 // C++ concepts
5370 case RID_REQUIRES:
5371 return cp_parser_requires_expression (parser);
5373 /* Objective-C++ expressions. */
5374 case RID_AT_ENCODE:
5375 case RID_AT_PROTOCOL:
5376 case RID_AT_SELECTOR:
5377 return cp_parser_objc_expression (parser);
5379 case RID_TEMPLATE:
5380 if (parser->in_function_body
5381 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5382 == CPP_LESS))
5384 error_at (token->location,
5385 "a template declaration cannot appear at block scope");
5386 cp_parser_skip_to_end_of_block_or_statement (parser);
5387 return error_mark_node;
5389 /* FALLTHRU */
5390 default:
5391 cp_parser_error (parser, "expected primary-expression");
5392 return error_mark_node;
5395 /* An id-expression can start with either an identifier, a
5396 `::' as the beginning of a qualified-id, or the "operator"
5397 keyword. */
5398 case CPP_NAME:
5399 case CPP_SCOPE:
5400 case CPP_TEMPLATE_ID:
5401 case CPP_NESTED_NAME_SPECIFIER:
5403 id_expression:
5404 cp_expr id_expression;
5405 cp_expr decl;
5406 const char *error_msg;
5407 bool template_p;
5408 bool done;
5409 cp_token *id_expr_token;
5411 /* Parse the id-expression. */
5412 id_expression
5413 = cp_parser_id_expression (parser,
5414 /*template_keyword_p=*/false,
5415 /*check_dependency_p=*/true,
5416 &template_p,
5417 /*declarator_p=*/false,
5418 /*optional_p=*/false);
5419 if (id_expression == error_mark_node)
5420 return error_mark_node;
5421 id_expr_token = token;
5422 token = cp_lexer_peek_token (parser->lexer);
5423 done = (token->type != CPP_OPEN_SQUARE
5424 && token->type != CPP_OPEN_PAREN
5425 && token->type != CPP_DOT
5426 && token->type != CPP_DEREF
5427 && token->type != CPP_PLUS_PLUS
5428 && token->type != CPP_MINUS_MINUS);
5429 /* If we have a template-id, then no further lookup is
5430 required. If the template-id was for a template-class, we
5431 will sometimes have a TYPE_DECL at this point. */
5432 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5433 || TREE_CODE (id_expression) == TYPE_DECL)
5434 decl = id_expression;
5435 /* Look up the name. */
5436 else
5438 tree ambiguous_decls;
5440 /* If we already know that this lookup is ambiguous, then
5441 we've already issued an error message; there's no reason
5442 to check again. */
5443 if (id_expr_token->type == CPP_NAME
5444 && id_expr_token->error_reported)
5446 cp_parser_simulate_error (parser);
5447 return error_mark_node;
5450 decl = cp_parser_lookup_name (parser, id_expression,
5451 none_type,
5452 template_p,
5453 /*is_namespace=*/false,
5454 /*check_dependency=*/true,
5455 &ambiguous_decls,
5456 id_expr_token->location);
5457 /* If the lookup was ambiguous, an error will already have
5458 been issued. */
5459 if (ambiguous_decls)
5460 return error_mark_node;
5462 /* In Objective-C++, we may have an Objective-C 2.0
5463 dot-syntax for classes here. */
5464 if (c_dialect_objc ()
5465 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5466 && TREE_CODE (decl) == TYPE_DECL
5467 && objc_is_class_name (decl))
5469 tree component;
5470 cp_lexer_consume_token (parser->lexer);
5471 component = cp_parser_identifier (parser);
5472 if (component == error_mark_node)
5473 return error_mark_node;
5475 tree result = objc_build_class_component_ref (id_expression,
5476 component);
5477 /* Build a location of the form:
5478 expr.component
5479 ~~~~~^~~~~~~~~
5480 with caret at the start of the component name (at
5481 input_location), ranging from the start of the id_expression
5482 to the end of the component name. */
5483 location_t combined_loc
5484 = make_location (input_location, id_expression.get_start (),
5485 get_finish (input_location));
5486 protected_set_expr_location (result, combined_loc);
5487 return result;
5490 /* In Objective-C++, an instance variable (ivar) may be preferred
5491 to whatever cp_parser_lookup_name() found.
5492 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5493 rest of c-family, we have to do a little extra work to preserve
5494 any location information in cp_expr "decl". Given that
5495 objc_lookup_ivar is implemented in "c-family" and "objc", we
5496 have a trip through the pure "tree" type, rather than cp_expr.
5497 Naively copying it back to "decl" would implicitly give the
5498 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5499 store an EXPR_LOCATION. Hence we only update "decl" (and
5500 hence its location_t) if we get back a different tree node. */
5501 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5502 id_expression);
5503 if (decl_tree != decl.get_value ())
5504 decl = cp_expr (decl_tree);
5506 /* If name lookup gives us a SCOPE_REF, then the
5507 qualifying scope was dependent. */
5508 if (TREE_CODE (decl) == SCOPE_REF)
5510 /* At this point, we do not know if DECL is a valid
5511 integral constant expression. We assume that it is
5512 in fact such an expression, so that code like:
5514 template <int N> struct A {
5515 int a[B<N>::i];
5518 is accepted. At template-instantiation time, we
5519 will check that B<N>::i is actually a constant. */
5520 return decl;
5522 /* Check to see if DECL is a local variable in a context
5523 where that is forbidden. */
5524 if (parser->local_variables_forbidden_p
5525 && local_variable_p (decl))
5527 /* It might be that we only found DECL because we are
5528 trying to be generous with pre-ISO scoping rules.
5529 For example, consider:
5531 int i;
5532 void g() {
5533 for (int i = 0; i < 10; ++i) {}
5534 extern void f(int j = i);
5537 Here, name look up will originally find the out
5538 of scope `i'. We need to issue a warning message,
5539 but then use the global `i'. */
5540 decl = check_for_out_of_scope_variable (decl);
5541 if (local_variable_p (decl))
5543 error_at (id_expr_token->location,
5544 "local variable %qD may not appear in this context",
5545 decl.get_value ());
5546 return error_mark_node;
5551 decl = (finish_id_expression
5552 (id_expression, decl, parser->scope,
5553 idk,
5554 parser->integral_constant_expression_p,
5555 parser->allow_non_integral_constant_expression_p,
5556 &parser->non_integral_constant_expression_p,
5557 template_p, done, address_p,
5558 template_arg_p,
5559 &error_msg,
5560 id_expression.get_location ()));
5561 if (error_msg)
5562 cp_parser_error (parser, error_msg);
5563 decl.set_location (id_expr_token->location);
5564 return decl;
5567 /* Anything else is an error. */
5568 default:
5569 cp_parser_error (parser, "expected primary-expression");
5570 return error_mark_node;
5574 static inline cp_expr
5575 cp_parser_primary_expression (cp_parser *parser,
5576 bool address_p,
5577 bool cast_p,
5578 bool template_arg_p,
5579 cp_id_kind *idk)
5581 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5582 /*decltype*/false, idk);
5585 /* Parse an id-expression.
5587 id-expression:
5588 unqualified-id
5589 qualified-id
5591 qualified-id:
5592 :: [opt] nested-name-specifier template [opt] unqualified-id
5593 :: identifier
5594 :: operator-function-id
5595 :: template-id
5597 Return a representation of the unqualified portion of the
5598 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5599 a `::' or nested-name-specifier.
5601 Often, if the id-expression was a qualified-id, the caller will
5602 want to make a SCOPE_REF to represent the qualified-id. This
5603 function does not do this in order to avoid wastefully creating
5604 SCOPE_REFs when they are not required.
5606 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5607 `template' keyword.
5609 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5610 uninstantiated templates.
5612 If *TEMPLATE_P is non-NULL, it is set to true iff the
5613 `template' keyword is used to explicitly indicate that the entity
5614 named is a template.
5616 If DECLARATOR_P is true, the id-expression is appearing as part of
5617 a declarator, rather than as part of an expression. */
5619 static cp_expr
5620 cp_parser_id_expression (cp_parser *parser,
5621 bool template_keyword_p,
5622 bool check_dependency_p,
5623 bool *template_p,
5624 bool declarator_p,
5625 bool optional_p)
5627 bool global_scope_p;
5628 bool nested_name_specifier_p;
5630 /* Assume the `template' keyword was not used. */
5631 if (template_p)
5632 *template_p = template_keyword_p;
5634 /* Look for the optional `::' operator. */
5635 global_scope_p
5636 = (!template_keyword_p
5637 && (cp_parser_global_scope_opt (parser,
5638 /*current_scope_valid_p=*/false)
5639 != NULL_TREE));
5641 /* Look for the optional nested-name-specifier. */
5642 nested_name_specifier_p
5643 = (cp_parser_nested_name_specifier_opt (parser,
5644 /*typename_keyword_p=*/false,
5645 check_dependency_p,
5646 /*type_p=*/false,
5647 declarator_p,
5648 template_keyword_p)
5649 != NULL_TREE);
5651 /* If there is a nested-name-specifier, then we are looking at
5652 the first qualified-id production. */
5653 if (nested_name_specifier_p)
5655 tree saved_scope;
5656 tree saved_object_scope;
5657 tree saved_qualifying_scope;
5658 cp_expr unqualified_id;
5659 bool is_template;
5661 /* See if the next token is the `template' keyword. */
5662 if (!template_p)
5663 template_p = &is_template;
5664 *template_p = cp_parser_optional_template_keyword (parser);
5665 /* Name lookup we do during the processing of the
5666 unqualified-id might obliterate SCOPE. */
5667 saved_scope = parser->scope;
5668 saved_object_scope = parser->object_scope;
5669 saved_qualifying_scope = parser->qualifying_scope;
5670 /* Process the final unqualified-id. */
5671 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5672 check_dependency_p,
5673 declarator_p,
5674 /*optional_p=*/false);
5675 /* Restore the SAVED_SCOPE for our caller. */
5676 parser->scope = saved_scope;
5677 parser->object_scope = saved_object_scope;
5678 parser->qualifying_scope = saved_qualifying_scope;
5680 return unqualified_id;
5682 /* Otherwise, if we are in global scope, then we are looking at one
5683 of the other qualified-id productions. */
5684 else if (global_scope_p)
5686 cp_token *token;
5687 tree id;
5689 /* Peek at the next token. */
5690 token = cp_lexer_peek_token (parser->lexer);
5692 /* If it's an identifier, and the next token is not a "<", then
5693 we can avoid the template-id case. This is an optimization
5694 for this common case. */
5695 if (token->type == CPP_NAME
5696 && !cp_parser_nth_token_starts_template_argument_list_p
5697 (parser, 2))
5698 return cp_parser_identifier (parser);
5700 cp_parser_parse_tentatively (parser);
5701 /* Try a template-id. */
5702 id = cp_parser_template_id (parser,
5703 /*template_keyword_p=*/false,
5704 /*check_dependency_p=*/true,
5705 none_type,
5706 declarator_p);
5707 /* If that worked, we're done. */
5708 if (cp_parser_parse_definitely (parser))
5709 return id;
5711 /* Peek at the next token. (Changes in the token buffer may
5712 have invalidated the pointer obtained above.) */
5713 token = cp_lexer_peek_token (parser->lexer);
5715 switch (token->type)
5717 case CPP_NAME:
5718 return cp_parser_identifier (parser);
5720 case CPP_KEYWORD:
5721 if (token->keyword == RID_OPERATOR)
5722 return cp_parser_operator_function_id (parser);
5723 /* Fall through. */
5725 default:
5726 cp_parser_error (parser, "expected id-expression");
5727 return error_mark_node;
5730 else
5731 return cp_parser_unqualified_id (parser, template_keyword_p,
5732 /*check_dependency_p=*/true,
5733 declarator_p,
5734 optional_p);
5737 /* Parse an unqualified-id.
5739 unqualified-id:
5740 identifier
5741 operator-function-id
5742 conversion-function-id
5743 ~ class-name
5744 template-id
5746 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5747 keyword, in a construct like `A::template ...'.
5749 Returns a representation of unqualified-id. For the `identifier'
5750 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5751 production a BIT_NOT_EXPR is returned; the operand of the
5752 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5753 other productions, see the documentation accompanying the
5754 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5755 names are looked up in uninstantiated templates. If DECLARATOR_P
5756 is true, the unqualified-id is appearing as part of a declarator,
5757 rather than as part of an expression. */
5759 static cp_expr
5760 cp_parser_unqualified_id (cp_parser* parser,
5761 bool template_keyword_p,
5762 bool check_dependency_p,
5763 bool declarator_p,
5764 bool optional_p)
5766 cp_token *token;
5768 /* Peek at the next token. */
5769 token = cp_lexer_peek_token (parser->lexer);
5771 switch ((int) token->type)
5773 case CPP_NAME:
5775 tree id;
5777 /* We don't know yet whether or not this will be a
5778 template-id. */
5779 cp_parser_parse_tentatively (parser);
5780 /* Try a template-id. */
5781 id = cp_parser_template_id (parser, template_keyword_p,
5782 check_dependency_p,
5783 none_type,
5784 declarator_p);
5785 /* If it worked, we're done. */
5786 if (cp_parser_parse_definitely (parser))
5787 return id;
5788 /* Otherwise, it's an ordinary identifier. */
5789 return cp_parser_identifier (parser);
5792 case CPP_TEMPLATE_ID:
5793 return cp_parser_template_id (parser, template_keyword_p,
5794 check_dependency_p,
5795 none_type,
5796 declarator_p);
5798 case CPP_COMPL:
5800 tree type_decl;
5801 tree qualifying_scope;
5802 tree object_scope;
5803 tree scope;
5804 bool done;
5806 /* Consume the `~' token. */
5807 cp_lexer_consume_token (parser->lexer);
5808 /* Parse the class-name. The standard, as written, seems to
5809 say that:
5811 template <typename T> struct S { ~S (); };
5812 template <typename T> S<T>::~S() {}
5814 is invalid, since `~' must be followed by a class-name, but
5815 `S<T>' is dependent, and so not known to be a class.
5816 That's not right; we need to look in uninstantiated
5817 templates. A further complication arises from:
5819 template <typename T> void f(T t) {
5820 t.T::~T();
5823 Here, it is not possible to look up `T' in the scope of `T'
5824 itself. We must look in both the current scope, and the
5825 scope of the containing complete expression.
5827 Yet another issue is:
5829 struct S {
5830 int S;
5831 ~S();
5834 S::~S() {}
5836 The standard does not seem to say that the `S' in `~S'
5837 should refer to the type `S' and not the data member
5838 `S::S'. */
5840 /* DR 244 says that we look up the name after the "~" in the
5841 same scope as we looked up the qualifying name. That idea
5842 isn't fully worked out; it's more complicated than that. */
5843 scope = parser->scope;
5844 object_scope = parser->object_scope;
5845 qualifying_scope = parser->qualifying_scope;
5847 /* Check for invalid scopes. */
5848 if (scope == error_mark_node)
5850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5851 cp_lexer_consume_token (parser->lexer);
5852 return error_mark_node;
5854 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5856 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5857 error_at (token->location,
5858 "scope %qT before %<~%> is not a class-name",
5859 scope);
5860 cp_parser_simulate_error (parser);
5861 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5862 cp_lexer_consume_token (parser->lexer);
5863 return error_mark_node;
5865 gcc_assert (!scope || TYPE_P (scope));
5867 /* If the name is of the form "X::~X" it's OK even if X is a
5868 typedef. */
5869 token = cp_lexer_peek_token (parser->lexer);
5870 if (scope
5871 && token->type == CPP_NAME
5872 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5873 != CPP_LESS)
5874 && (token->u.value == TYPE_IDENTIFIER (scope)
5875 || (CLASS_TYPE_P (scope)
5876 && constructor_name_p (token->u.value, scope))))
5878 cp_lexer_consume_token (parser->lexer);
5879 return build_nt (BIT_NOT_EXPR, scope);
5882 /* ~auto means the destructor of whatever the object is. */
5883 if (cp_parser_is_keyword (token, RID_AUTO))
5885 if (cxx_dialect < cxx14)
5886 pedwarn (input_location, 0,
5887 "%<~auto%> only available with "
5888 "-std=c++14 or -std=gnu++14");
5889 cp_lexer_consume_token (parser->lexer);
5890 return build_nt (BIT_NOT_EXPR, make_auto ());
5893 /* If there was an explicit qualification (S::~T), first look
5894 in the scope given by the qualification (i.e., S).
5896 Note: in the calls to cp_parser_class_name below we pass
5897 typename_type so that lookup finds the injected-class-name
5898 rather than the constructor. */
5899 done = false;
5900 type_decl = NULL_TREE;
5901 if (scope)
5903 cp_parser_parse_tentatively (parser);
5904 type_decl = cp_parser_class_name (parser,
5905 /*typename_keyword_p=*/false,
5906 /*template_keyword_p=*/false,
5907 typename_type,
5908 /*check_dependency=*/false,
5909 /*class_head_p=*/false,
5910 declarator_p);
5911 if (cp_parser_parse_definitely (parser))
5912 done = true;
5914 /* In "N::S::~S", look in "N" as well. */
5915 if (!done && scope && qualifying_scope)
5917 cp_parser_parse_tentatively (parser);
5918 parser->scope = qualifying_scope;
5919 parser->object_scope = NULL_TREE;
5920 parser->qualifying_scope = NULL_TREE;
5921 type_decl
5922 = cp_parser_class_name (parser,
5923 /*typename_keyword_p=*/false,
5924 /*template_keyword_p=*/false,
5925 typename_type,
5926 /*check_dependency=*/false,
5927 /*class_head_p=*/false,
5928 declarator_p);
5929 if (cp_parser_parse_definitely (parser))
5930 done = true;
5932 /* In "p->S::~T", look in the scope given by "*p" as well. */
5933 else if (!done && object_scope)
5935 cp_parser_parse_tentatively (parser);
5936 parser->scope = object_scope;
5937 parser->object_scope = NULL_TREE;
5938 parser->qualifying_scope = NULL_TREE;
5939 type_decl
5940 = cp_parser_class_name (parser,
5941 /*typename_keyword_p=*/false,
5942 /*template_keyword_p=*/false,
5943 typename_type,
5944 /*check_dependency=*/false,
5945 /*class_head_p=*/false,
5946 declarator_p);
5947 if (cp_parser_parse_definitely (parser))
5948 done = true;
5950 /* Look in the surrounding context. */
5951 if (!done)
5953 parser->scope = NULL_TREE;
5954 parser->object_scope = NULL_TREE;
5955 parser->qualifying_scope = NULL_TREE;
5956 if (processing_template_decl)
5957 cp_parser_parse_tentatively (parser);
5958 type_decl
5959 = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (processing_template_decl
5967 && ! cp_parser_parse_definitely (parser))
5969 /* We couldn't find a type with this name. If we're parsing
5970 tentatively, fail and try something else. */
5971 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5973 cp_parser_simulate_error (parser);
5974 return error_mark_node;
5976 /* Otherwise, accept it and check for a match at instantiation
5977 time. */
5978 type_decl = cp_parser_identifier (parser);
5979 if (type_decl != error_mark_node)
5980 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5981 return type_decl;
5984 /* If an error occurred, assume that the name of the
5985 destructor is the same as the name of the qualifying
5986 class. That allows us to keep parsing after running
5987 into ill-formed destructor names. */
5988 if (type_decl == error_mark_node && scope)
5989 return build_nt (BIT_NOT_EXPR, scope);
5990 else if (type_decl == error_mark_node)
5991 return error_mark_node;
5993 /* Check that destructor name and scope match. */
5994 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5996 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5997 error_at (token->location,
5998 "declaration of %<~%T%> as member of %qT",
5999 type_decl, scope);
6000 cp_parser_simulate_error (parser);
6001 return error_mark_node;
6004 /* [class.dtor]
6006 A typedef-name that names a class shall not be used as the
6007 identifier in the declarator for a destructor declaration. */
6008 if (declarator_p
6009 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6010 && !DECL_SELF_REFERENCE_P (type_decl)
6011 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6012 error_at (token->location,
6013 "typedef-name %qD used as destructor declarator",
6014 type_decl);
6016 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6019 case CPP_KEYWORD:
6020 if (token->keyword == RID_OPERATOR)
6022 cp_expr id;
6024 /* This could be a template-id, so we try that first. */
6025 cp_parser_parse_tentatively (parser);
6026 /* Try a template-id. */
6027 id = cp_parser_template_id (parser, template_keyword_p,
6028 /*check_dependency_p=*/true,
6029 none_type,
6030 declarator_p);
6031 /* If that worked, we're done. */
6032 if (cp_parser_parse_definitely (parser))
6033 return id;
6034 /* We still don't know whether we're looking at an
6035 operator-function-id or a conversion-function-id. */
6036 cp_parser_parse_tentatively (parser);
6037 /* Try an operator-function-id. */
6038 id = cp_parser_operator_function_id (parser);
6039 /* If that didn't work, try a conversion-function-id. */
6040 if (!cp_parser_parse_definitely (parser))
6041 id = cp_parser_conversion_function_id (parser);
6042 else if (UDLIT_OPER_P (id))
6044 /* 17.6.3.3.5 */
6045 const char *name = UDLIT_OP_SUFFIX (id);
6046 if (name[0] != '_' && !in_system_header_at (input_location)
6047 && declarator_p)
6048 warning (OPT_Wliteral_suffix,
6049 "literal operator suffixes not preceded by %<_%>"
6050 " are reserved for future standardization");
6053 return id;
6055 /* Fall through. */
6057 default:
6058 if (optional_p)
6059 return NULL_TREE;
6060 cp_parser_error (parser, "expected unqualified-id");
6061 return error_mark_node;
6065 /* Parse an (optional) nested-name-specifier.
6067 nested-name-specifier: [C++98]
6068 class-or-namespace-name :: nested-name-specifier [opt]
6069 class-or-namespace-name :: template nested-name-specifier [opt]
6071 nested-name-specifier: [C++0x]
6072 type-name ::
6073 namespace-name ::
6074 nested-name-specifier identifier ::
6075 nested-name-specifier template [opt] simple-template-id ::
6077 PARSER->SCOPE should be set appropriately before this function is
6078 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6079 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6080 in name lookups.
6082 Sets PARSER->SCOPE to the class (TYPE) or namespace
6083 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6084 it unchanged if there is no nested-name-specifier. Returns the new
6085 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6087 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6088 part of a declaration and/or decl-specifier. */
6090 static tree
6091 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6092 bool typename_keyword_p,
6093 bool check_dependency_p,
6094 bool type_p,
6095 bool is_declaration,
6096 bool template_keyword_p /* = false */)
6098 bool success = false;
6099 cp_token_position start = 0;
6100 cp_token *token;
6102 /* Remember where the nested-name-specifier starts. */
6103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6105 start = cp_lexer_token_position (parser->lexer, false);
6106 push_deferring_access_checks (dk_deferred);
6109 while (true)
6111 tree new_scope;
6112 tree old_scope;
6113 tree saved_qualifying_scope;
6115 /* Spot cases that cannot be the beginning of a
6116 nested-name-specifier. */
6117 token = cp_lexer_peek_token (parser->lexer);
6119 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6120 the already parsed nested-name-specifier. */
6121 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6123 /* Grab the nested-name-specifier and continue the loop. */
6124 cp_parser_pre_parsed_nested_name_specifier (parser);
6125 /* If we originally encountered this nested-name-specifier
6126 with IS_DECLARATION set to false, we will not have
6127 resolved TYPENAME_TYPEs, so we must do so here. */
6128 if (is_declaration
6129 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6131 new_scope = resolve_typename_type (parser->scope,
6132 /*only_current_p=*/false);
6133 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6134 parser->scope = new_scope;
6136 success = true;
6137 continue;
6140 /* Spot cases that cannot be the beginning of a
6141 nested-name-specifier. On the second and subsequent times
6142 through the loop, we look for the `template' keyword. */
6143 if (success && token->keyword == RID_TEMPLATE)
6145 /* A template-id can start a nested-name-specifier. */
6146 else if (token->type == CPP_TEMPLATE_ID)
6148 /* DR 743: decltype can be used in a nested-name-specifier. */
6149 else if (token_is_decltype (token))
6151 else
6153 /* If the next token is not an identifier, then it is
6154 definitely not a type-name or namespace-name. */
6155 if (token->type != CPP_NAME)
6156 break;
6157 /* If the following token is neither a `<' (to begin a
6158 template-id), nor a `::', then we are not looking at a
6159 nested-name-specifier. */
6160 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6162 if (token->type == CPP_COLON
6163 && parser->colon_corrects_to_scope_p
6164 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6166 gcc_rich_location richloc (token->location);
6167 richloc.add_fixit_replace ("::");
6168 error_at (&richloc,
6169 "found %<:%> in nested-name-specifier, "
6170 "expected %<::%>");
6171 token->type = CPP_SCOPE;
6174 if (token->type != CPP_SCOPE
6175 && !cp_parser_nth_token_starts_template_argument_list_p
6176 (parser, 2))
6177 break;
6180 /* The nested-name-specifier is optional, so we parse
6181 tentatively. */
6182 cp_parser_parse_tentatively (parser);
6184 /* Look for the optional `template' keyword, if this isn't the
6185 first time through the loop. */
6186 if (success)
6187 template_keyword_p = cp_parser_optional_template_keyword (parser);
6189 /* Save the old scope since the name lookup we are about to do
6190 might destroy it. */
6191 old_scope = parser->scope;
6192 saved_qualifying_scope = parser->qualifying_scope;
6193 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6194 look up names in "X<T>::I" in order to determine that "Y" is
6195 a template. So, if we have a typename at this point, we make
6196 an effort to look through it. */
6197 if (is_declaration
6198 && !typename_keyword_p
6199 && parser->scope
6200 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6201 parser->scope = resolve_typename_type (parser->scope,
6202 /*only_current_p=*/false);
6203 /* Parse the qualifying entity. */
6204 new_scope
6205 = cp_parser_qualifying_entity (parser,
6206 typename_keyword_p,
6207 template_keyword_p,
6208 check_dependency_p,
6209 type_p,
6210 is_declaration);
6211 /* Look for the `::' token. */
6212 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6214 /* If we found what we wanted, we keep going; otherwise, we're
6215 done. */
6216 if (!cp_parser_parse_definitely (parser))
6218 bool error_p = false;
6220 /* Restore the OLD_SCOPE since it was valid before the
6221 failed attempt at finding the last
6222 class-or-namespace-name. */
6223 parser->scope = old_scope;
6224 parser->qualifying_scope = saved_qualifying_scope;
6226 /* If the next token is a decltype, and the one after that is a
6227 `::', then the decltype has failed to resolve to a class or
6228 enumeration type. Give this error even when parsing
6229 tentatively since it can't possibly be valid--and we're going
6230 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6231 won't get another chance.*/
6232 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6233 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6234 == CPP_SCOPE))
6236 token = cp_lexer_consume_token (parser->lexer);
6237 error_at (token->location, "decltype evaluates to %qT, "
6238 "which is not a class or enumeration type",
6239 token->u.tree_check_value->value);
6240 parser->scope = error_mark_node;
6241 error_p = true;
6242 /* As below. */
6243 success = true;
6244 cp_lexer_consume_token (parser->lexer);
6247 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6248 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6250 /* If we have a non-type template-id followed by ::, it can't
6251 possibly be valid. */
6252 token = cp_lexer_peek_token (parser->lexer);
6253 tree tid = token->u.tree_check_value->value;
6254 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6255 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6257 tree tmpl = NULL_TREE;
6258 if (is_overloaded_fn (tid))
6260 tree fns = get_fns (tid);
6261 if (OVL_SINGLE_P (fns))
6262 tmpl = OVL_FIRST (fns);
6263 error_at (token->location, "function template-id %qD "
6264 "in nested-name-specifier", tid);
6266 else
6268 /* Variable template. */
6269 tmpl = TREE_OPERAND (tid, 0);
6270 gcc_assert (variable_template_p (tmpl));
6271 error_at (token->location, "variable template-id %qD "
6272 "in nested-name-specifier", tid);
6274 if (tmpl)
6275 inform (DECL_SOURCE_LOCATION (tmpl),
6276 "%qD declared here", tmpl);
6278 parser->scope = error_mark_node;
6279 error_p = true;
6280 /* As below. */
6281 success = true;
6282 cp_lexer_consume_token (parser->lexer);
6283 cp_lexer_consume_token (parser->lexer);
6287 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6288 break;
6289 /* If the next token is an identifier, and the one after
6290 that is a `::', then any valid interpretation would have
6291 found a class-or-namespace-name. */
6292 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6293 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6294 == CPP_SCOPE)
6295 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6296 != CPP_COMPL))
6298 token = cp_lexer_consume_token (parser->lexer);
6299 if (!error_p)
6301 if (!token->error_reported)
6303 tree decl;
6304 tree ambiguous_decls;
6306 decl = cp_parser_lookup_name (parser, token->u.value,
6307 none_type,
6308 /*is_template=*/false,
6309 /*is_namespace=*/false,
6310 /*check_dependency=*/true,
6311 &ambiguous_decls,
6312 token->location);
6313 if (TREE_CODE (decl) == TEMPLATE_DECL)
6314 error_at (token->location,
6315 "%qD used without template parameters",
6316 decl);
6317 else if (ambiguous_decls)
6319 // cp_parser_lookup_name has the same diagnostic,
6320 // thus make sure to emit it at most once.
6321 if (cp_parser_uncommitted_to_tentative_parse_p
6322 (parser))
6324 error_at (token->location,
6325 "reference to %qD is ambiguous",
6326 token->u.value);
6327 print_candidates (ambiguous_decls);
6329 decl = error_mark_node;
6331 else
6333 if (cxx_dialect != cxx98)
6334 cp_parser_name_lookup_error
6335 (parser, token->u.value, decl, NLE_NOT_CXX98,
6336 token->location);
6337 else
6338 cp_parser_name_lookup_error
6339 (parser, token->u.value, decl, NLE_CXX98,
6340 token->location);
6343 parser->scope = error_mark_node;
6344 error_p = true;
6345 /* Treat this as a successful nested-name-specifier
6346 due to:
6348 [basic.lookup.qual]
6350 If the name found is not a class-name (clause
6351 _class_) or namespace-name (_namespace.def_), the
6352 program is ill-formed. */
6353 success = true;
6355 cp_lexer_consume_token (parser->lexer);
6357 break;
6359 /* We've found one valid nested-name-specifier. */
6360 success = true;
6361 /* Name lookup always gives us a DECL. */
6362 if (TREE_CODE (new_scope) == TYPE_DECL)
6363 new_scope = TREE_TYPE (new_scope);
6364 /* Uses of "template" must be followed by actual templates. */
6365 if (template_keyword_p
6366 && !(CLASS_TYPE_P (new_scope)
6367 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6368 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6369 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6370 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6371 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6372 == TEMPLATE_ID_EXPR)))
6373 permerror (input_location, TYPE_P (new_scope)
6374 ? G_("%qT is not a template")
6375 : G_("%qD is not a template"),
6376 new_scope);
6377 /* If it is a class scope, try to complete it; we are about to
6378 be looking up names inside the class. */
6379 if (TYPE_P (new_scope)
6380 /* Since checking types for dependency can be expensive,
6381 avoid doing it if the type is already complete. */
6382 && !COMPLETE_TYPE_P (new_scope)
6383 /* Do not try to complete dependent types. */
6384 && !dependent_type_p (new_scope))
6386 new_scope = complete_type (new_scope);
6387 /* If it is a typedef to current class, use the current
6388 class instead, as the typedef won't have any names inside
6389 it yet. */
6390 if (!COMPLETE_TYPE_P (new_scope)
6391 && currently_open_class (new_scope))
6392 new_scope = TYPE_MAIN_VARIANT (new_scope);
6394 /* Make sure we look in the right scope the next time through
6395 the loop. */
6396 parser->scope = new_scope;
6399 /* If parsing tentatively, replace the sequence of tokens that makes
6400 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6401 token. That way, should we re-parse the token stream, we will
6402 not have to repeat the effort required to do the parse, nor will
6403 we issue duplicate error messages. */
6404 if (success && start)
6406 cp_token *token;
6408 token = cp_lexer_token_at (parser->lexer, start);
6409 /* Reset the contents of the START token. */
6410 token->type = CPP_NESTED_NAME_SPECIFIER;
6411 /* Retrieve any deferred checks. Do not pop this access checks yet
6412 so the memory will not be reclaimed during token replacing below. */
6413 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6414 token->u.tree_check_value->value = parser->scope;
6415 token->u.tree_check_value->checks = get_deferred_access_checks ();
6416 token->u.tree_check_value->qualifying_scope =
6417 parser->qualifying_scope;
6418 token->keyword = RID_MAX;
6420 /* Purge all subsequent tokens. */
6421 cp_lexer_purge_tokens_after (parser->lexer, start);
6424 if (start)
6425 pop_to_parent_deferring_access_checks ();
6427 return success ? parser->scope : NULL_TREE;
6430 /* Parse a nested-name-specifier. See
6431 cp_parser_nested_name_specifier_opt for details. This function
6432 behaves identically, except that it will an issue an error if no
6433 nested-name-specifier is present. */
6435 static tree
6436 cp_parser_nested_name_specifier (cp_parser *parser,
6437 bool typename_keyword_p,
6438 bool check_dependency_p,
6439 bool type_p,
6440 bool is_declaration)
6442 tree scope;
6444 /* Look for the nested-name-specifier. */
6445 scope = cp_parser_nested_name_specifier_opt (parser,
6446 typename_keyword_p,
6447 check_dependency_p,
6448 type_p,
6449 is_declaration);
6450 /* If it was not present, issue an error message. */
6451 if (!scope)
6453 cp_parser_error (parser, "expected nested-name-specifier");
6454 parser->scope = NULL_TREE;
6457 return scope;
6460 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6461 this is either a class-name or a namespace-name (which corresponds
6462 to the class-or-namespace-name production in the grammar). For
6463 C++0x, it can also be a type-name that refers to an enumeration
6464 type or a simple-template-id.
6466 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6467 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6468 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6469 TYPE_P is TRUE iff the next name should be taken as a class-name,
6470 even the same name is declared to be another entity in the same
6471 scope.
6473 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6474 specified by the class-or-namespace-name. If neither is found the
6475 ERROR_MARK_NODE is returned. */
6477 static tree
6478 cp_parser_qualifying_entity (cp_parser *parser,
6479 bool typename_keyword_p,
6480 bool template_keyword_p,
6481 bool check_dependency_p,
6482 bool type_p,
6483 bool is_declaration)
6485 tree saved_scope;
6486 tree saved_qualifying_scope;
6487 tree saved_object_scope;
6488 tree scope;
6489 bool only_class_p;
6490 bool successful_parse_p;
6492 /* DR 743: decltype can appear in a nested-name-specifier. */
6493 if (cp_lexer_next_token_is_decltype (parser->lexer))
6495 scope = cp_parser_decltype (parser);
6496 if (TREE_CODE (scope) != ENUMERAL_TYPE
6497 && !MAYBE_CLASS_TYPE_P (scope))
6499 cp_parser_simulate_error (parser);
6500 return error_mark_node;
6502 if (TYPE_NAME (scope))
6503 scope = TYPE_NAME (scope);
6504 return scope;
6507 /* Before we try to parse the class-name, we must save away the
6508 current PARSER->SCOPE since cp_parser_class_name will destroy
6509 it. */
6510 saved_scope = parser->scope;
6511 saved_qualifying_scope = parser->qualifying_scope;
6512 saved_object_scope = parser->object_scope;
6513 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6514 there is no need to look for a namespace-name. */
6515 only_class_p = template_keyword_p
6516 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6517 if (!only_class_p)
6518 cp_parser_parse_tentatively (parser);
6519 scope = cp_parser_class_name (parser,
6520 typename_keyword_p,
6521 template_keyword_p,
6522 type_p ? class_type : none_type,
6523 check_dependency_p,
6524 /*class_head_p=*/false,
6525 is_declaration,
6526 /*enum_ok=*/cxx_dialect > cxx98);
6527 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6528 /* If that didn't work, try for a namespace-name. */
6529 if (!only_class_p && !successful_parse_p)
6531 /* Restore the saved scope. */
6532 parser->scope = saved_scope;
6533 parser->qualifying_scope = saved_qualifying_scope;
6534 parser->object_scope = saved_object_scope;
6535 /* If we are not looking at an identifier followed by the scope
6536 resolution operator, then this is not part of a
6537 nested-name-specifier. (Note that this function is only used
6538 to parse the components of a nested-name-specifier.) */
6539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6540 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6541 return error_mark_node;
6542 scope = cp_parser_namespace_name (parser);
6545 return scope;
6548 /* Return true if we are looking at a compound-literal, false otherwise. */
6550 static bool
6551 cp_parser_compound_literal_p (cp_parser *parser)
6553 cp_lexer_save_tokens (parser->lexer);
6555 /* Skip tokens until the next token is a closing parenthesis.
6556 If we find the closing `)', and the next token is a `{', then
6557 we are looking at a compound-literal. */
6558 bool compound_literal_p
6559 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6560 /*consume_paren=*/true)
6561 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6563 /* Roll back the tokens we skipped. */
6564 cp_lexer_rollback_tokens (parser->lexer);
6566 return compound_literal_p;
6569 /* Parse a postfix-expression.
6571 postfix-expression:
6572 primary-expression
6573 postfix-expression [ expression ]
6574 postfix-expression ( expression-list [opt] )
6575 simple-type-specifier ( expression-list [opt] )
6576 typename :: [opt] nested-name-specifier identifier
6577 ( expression-list [opt] )
6578 typename :: [opt] nested-name-specifier template [opt] template-id
6579 ( expression-list [opt] )
6580 postfix-expression . template [opt] id-expression
6581 postfix-expression -> template [opt] id-expression
6582 postfix-expression . pseudo-destructor-name
6583 postfix-expression -> pseudo-destructor-name
6584 postfix-expression ++
6585 postfix-expression --
6586 dynamic_cast < type-id > ( expression )
6587 static_cast < type-id > ( expression )
6588 reinterpret_cast < type-id > ( expression )
6589 const_cast < type-id > ( expression )
6590 typeid ( expression )
6591 typeid ( type-id )
6593 GNU Extension:
6595 postfix-expression:
6596 ( type-id ) { initializer-list , [opt] }
6598 This extension is a GNU version of the C99 compound-literal
6599 construct. (The C99 grammar uses `type-name' instead of `type-id',
6600 but they are essentially the same concept.)
6602 If ADDRESS_P is true, the postfix expression is the operand of the
6603 `&' operator. CAST_P is true if this expression is the target of a
6604 cast.
6606 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6607 class member access expressions [expr.ref].
6609 Returns a representation of the expression. */
6611 static cp_expr
6612 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6613 bool member_access_only_p, bool decltype_p,
6614 cp_id_kind * pidk_return)
6616 cp_token *token;
6617 location_t loc;
6618 enum rid keyword;
6619 cp_id_kind idk = CP_ID_KIND_NONE;
6620 cp_expr postfix_expression = NULL_TREE;
6621 bool is_member_access = false;
6622 int saved_in_statement = -1;
6624 /* Peek at the next token. */
6625 token = cp_lexer_peek_token (parser->lexer);
6626 loc = token->location;
6627 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6629 /* Some of the productions are determined by keywords. */
6630 keyword = token->keyword;
6631 switch (keyword)
6633 case RID_DYNCAST:
6634 case RID_STATCAST:
6635 case RID_REINTCAST:
6636 case RID_CONSTCAST:
6638 tree type;
6639 cp_expr expression;
6640 const char *saved_message;
6641 bool saved_in_type_id_in_expr_p;
6643 /* All of these can be handled in the same way from the point
6644 of view of parsing. Begin by consuming the token
6645 identifying the cast. */
6646 cp_lexer_consume_token (parser->lexer);
6648 /* New types cannot be defined in the cast. */
6649 saved_message = parser->type_definition_forbidden_message;
6650 parser->type_definition_forbidden_message
6651 = G_("types may not be defined in casts");
6653 /* Look for the opening `<'. */
6654 cp_parser_require (parser, CPP_LESS, RT_LESS);
6655 /* Parse the type to which we are casting. */
6656 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6657 parser->in_type_id_in_expr_p = true;
6658 type = cp_parser_type_id (parser);
6659 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6660 /* Look for the closing `>'. */
6661 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6662 /* Restore the old message. */
6663 parser->type_definition_forbidden_message = saved_message;
6665 bool saved_greater_than_is_operator_p
6666 = parser->greater_than_is_operator_p;
6667 parser->greater_than_is_operator_p = true;
6669 /* And the expression which is being cast. */
6670 matching_parens parens;
6671 parens.require_open (parser);
6672 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6673 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6674 RT_CLOSE_PAREN);
6675 location_t end_loc = close_paren ?
6676 close_paren->location : UNKNOWN_LOCATION;
6678 parser->greater_than_is_operator_p
6679 = saved_greater_than_is_operator_p;
6681 /* Only type conversions to integral or enumeration types
6682 can be used in constant-expressions. */
6683 if (!cast_valid_in_integral_constant_expression_p (type)
6684 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6686 postfix_expression = error_mark_node;
6687 break;
6690 switch (keyword)
6692 case RID_DYNCAST:
6693 postfix_expression
6694 = build_dynamic_cast (type, expression, tf_warning_or_error);
6695 break;
6696 case RID_STATCAST:
6697 postfix_expression
6698 = build_static_cast (type, expression, tf_warning_or_error);
6699 break;
6700 case RID_REINTCAST:
6701 postfix_expression
6702 = build_reinterpret_cast (type, expression,
6703 tf_warning_or_error);
6704 break;
6705 case RID_CONSTCAST:
6706 postfix_expression
6707 = build_const_cast (type, expression, tf_warning_or_error);
6708 break;
6709 default:
6710 gcc_unreachable ();
6713 /* Construct a location e.g. :
6714 reinterpret_cast <int *> (expr)
6715 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6716 ranging from the start of the "*_cast" token to the final closing
6717 paren, with the caret at the start. */
6718 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6719 postfix_expression.set_location (cp_cast_loc);
6721 break;
6723 case RID_TYPEID:
6725 tree type;
6726 const char *saved_message;
6727 bool saved_in_type_id_in_expr_p;
6729 /* Consume the `typeid' token. */
6730 cp_lexer_consume_token (parser->lexer);
6731 /* Look for the `(' token. */
6732 matching_parens parens;
6733 parens.require_open (parser);
6734 /* Types cannot be defined in a `typeid' expression. */
6735 saved_message = parser->type_definition_forbidden_message;
6736 parser->type_definition_forbidden_message
6737 = G_("types may not be defined in a %<typeid%> expression");
6738 /* We can't be sure yet whether we're looking at a type-id or an
6739 expression. */
6740 cp_parser_parse_tentatively (parser);
6741 /* Try a type-id first. */
6742 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6743 parser->in_type_id_in_expr_p = true;
6744 type = cp_parser_type_id (parser);
6745 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6746 /* Look for the `)' token. Otherwise, we can't be sure that
6747 we're not looking at an expression: consider `typeid (int
6748 (3))', for example. */
6749 cp_token *close_paren = parens.require_close (parser);
6750 /* If all went well, simply lookup the type-id. */
6751 if (cp_parser_parse_definitely (parser))
6752 postfix_expression = get_typeid (type, tf_warning_or_error);
6753 /* Otherwise, fall back to the expression variant. */
6754 else
6756 tree expression;
6758 /* Look for an expression. */
6759 expression = cp_parser_expression (parser, & idk);
6760 /* Compute its typeid. */
6761 postfix_expression = build_typeid (expression, tf_warning_or_error);
6762 /* Look for the `)' token. */
6763 close_paren = parens.require_close (parser);
6765 /* Restore the saved message. */
6766 parser->type_definition_forbidden_message = saved_message;
6767 /* `typeid' may not appear in an integral constant expression. */
6768 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6769 postfix_expression = error_mark_node;
6771 /* Construct a location e.g. :
6772 typeid (expr)
6773 ^~~~~~~~~~~~~
6774 ranging from the start of the "typeid" token to the final closing
6775 paren, with the caret at the start. */
6776 if (close_paren)
6778 location_t typeid_loc
6779 = make_location (start_loc, start_loc, close_paren->location);
6780 postfix_expression.set_location (typeid_loc);
6783 break;
6785 case RID_TYPENAME:
6787 tree type;
6788 /* The syntax permitted here is the same permitted for an
6789 elaborated-type-specifier. */
6790 ++parser->prevent_constrained_type_specifiers;
6791 type = cp_parser_elaborated_type_specifier (parser,
6792 /*is_friend=*/false,
6793 /*is_declaration=*/false);
6794 --parser->prevent_constrained_type_specifiers;
6795 postfix_expression = cp_parser_functional_cast (parser, type);
6797 break;
6799 case RID_CILK_SPAWN:
6801 location_t cilk_spawn_loc
6802 = cp_lexer_peek_token (parser->lexer)->location;
6803 cp_lexer_consume_token (parser->lexer);
6804 token = cp_lexer_peek_token (parser->lexer);
6805 if (token->type == CPP_SEMICOLON)
6807 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6808 "an expression");
6809 postfix_expression = error_mark_node;
6810 break;
6812 else if (!current_function_decl)
6814 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6815 "inside a function");
6816 postfix_expression = error_mark_node;
6817 break;
6819 else
6821 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6822 saved_in_statement = parser->in_statement;
6823 parser->in_statement |= IN_CILK_SPAWN;
6825 cfun->calls_cilk_spawn = 1;
6826 postfix_expression =
6827 cp_parser_postfix_expression (parser, false, false,
6828 false, false, &idk);
6829 if (!flag_cilkplus)
6831 error_at (token->location, "-fcilkplus must be enabled to use"
6832 " %<_Cilk_spawn%>");
6833 cfun->calls_cilk_spawn = 0;
6835 else if (saved_in_statement & IN_CILK_SPAWN)
6837 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6838 "are not permitted");
6839 postfix_expression = error_mark_node;
6840 cfun->calls_cilk_spawn = 0;
6842 else
6844 location_t loc = postfix_expression.get_location ();
6845 postfix_expression = build_cilk_spawn (token->location,
6846 postfix_expression);
6847 /* Build a location of the form:
6848 _Cilk_spawn expr
6849 ~~~~~~~~~~~~^~~~
6850 with caret at the expr, ranging from the start of the
6851 _Cilk_spawn token to the end of the expression. */
6852 location_t combined_loc =
6853 make_location (loc, cilk_spawn_loc, get_finish (loc));
6854 postfix_expression.set_location (combined_loc);
6855 if (postfix_expression != error_mark_node)
6856 SET_EXPR_LOCATION (postfix_expression, input_location);
6857 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6859 break;
6862 case RID_ADDRESSOF:
6863 case RID_BUILTIN_SHUFFLE:
6864 case RID_BUILTIN_LAUNDER:
6866 vec<tree, va_gc> *vec;
6867 unsigned int i;
6868 tree p;
6870 cp_lexer_consume_token (parser->lexer);
6871 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6872 /*cast_p=*/false, /*allow_expansion_p=*/true,
6873 /*non_constant_p=*/NULL);
6874 if (vec == NULL)
6876 postfix_expression = error_mark_node;
6877 break;
6880 FOR_EACH_VEC_ELT (*vec, i, p)
6881 mark_exp_read (p);
6883 switch (keyword)
6885 case RID_ADDRESSOF:
6886 if (vec->length () == 1)
6887 postfix_expression
6888 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6889 else
6891 error_at (loc, "wrong number of arguments to "
6892 "%<__builtin_addressof%>");
6893 postfix_expression = error_mark_node;
6895 break;
6897 case RID_BUILTIN_LAUNDER:
6898 if (vec->length () == 1)
6899 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6900 tf_warning_or_error);
6901 else
6903 error_at (loc, "wrong number of arguments to "
6904 "%<__builtin_launder%>");
6905 postfix_expression = error_mark_node;
6907 break;
6909 case RID_BUILTIN_SHUFFLE:
6910 if (vec->length () == 2)
6911 postfix_expression
6912 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6913 (*vec)[1], tf_warning_or_error);
6914 else if (vec->length () == 3)
6915 postfix_expression
6916 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6917 (*vec)[2], tf_warning_or_error);
6918 else
6920 error_at (loc, "wrong number of arguments to "
6921 "%<__builtin_shuffle%>");
6922 postfix_expression = error_mark_node;
6924 break;
6926 default:
6927 gcc_unreachable ();
6929 break;
6932 default:
6934 tree type;
6936 /* If the next thing is a simple-type-specifier, we may be
6937 looking at a functional cast. We could also be looking at
6938 an id-expression. So, we try the functional cast, and if
6939 that doesn't work we fall back to the primary-expression. */
6940 cp_parser_parse_tentatively (parser);
6941 /* Look for the simple-type-specifier. */
6942 ++parser->prevent_constrained_type_specifiers;
6943 type = cp_parser_simple_type_specifier (parser,
6944 /*decl_specs=*/NULL,
6945 CP_PARSER_FLAGS_NONE);
6946 --parser->prevent_constrained_type_specifiers;
6947 /* Parse the cast itself. */
6948 if (!cp_parser_error_occurred (parser))
6949 postfix_expression
6950 = cp_parser_functional_cast (parser, type);
6951 /* If that worked, we're done. */
6952 if (cp_parser_parse_definitely (parser))
6953 break;
6955 /* If the functional-cast didn't work out, try a
6956 compound-literal. */
6957 if (cp_parser_allow_gnu_extensions_p (parser)
6958 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6960 cp_expr initializer = NULL_TREE;
6962 cp_parser_parse_tentatively (parser);
6964 matching_parens parens;
6965 parens.consume_open (parser);
6967 /* Avoid calling cp_parser_type_id pointlessly, see comment
6968 in cp_parser_cast_expression about c++/29234. */
6969 if (!cp_parser_compound_literal_p (parser))
6970 cp_parser_simulate_error (parser);
6971 else
6973 /* Parse the type. */
6974 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6975 parser->in_type_id_in_expr_p = true;
6976 type = cp_parser_type_id (parser);
6977 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6978 parens.require_close (parser);
6981 /* If things aren't going well, there's no need to
6982 keep going. */
6983 if (!cp_parser_error_occurred (parser))
6985 bool non_constant_p;
6986 /* Parse the brace-enclosed initializer list. */
6987 initializer = cp_parser_braced_list (parser,
6988 &non_constant_p);
6990 /* If that worked, we're definitely looking at a
6991 compound-literal expression. */
6992 if (cp_parser_parse_definitely (parser))
6994 /* Warn the user that a compound literal is not
6995 allowed in standard C++. */
6996 pedwarn (input_location, OPT_Wpedantic,
6997 "ISO C++ forbids compound-literals");
6998 /* For simplicity, we disallow compound literals in
6999 constant-expressions. We could
7000 allow compound literals of integer type, whose
7001 initializer was a constant, in constant
7002 expressions. Permitting that usage, as a further
7003 extension, would not change the meaning of any
7004 currently accepted programs. (Of course, as
7005 compound literals are not part of ISO C++, the
7006 standard has nothing to say.) */
7007 if (cp_parser_non_integral_constant_expression (parser,
7008 NIC_NCC))
7010 postfix_expression = error_mark_node;
7011 break;
7013 /* Form the representation of the compound-literal. */
7014 postfix_expression
7015 = finish_compound_literal (type, initializer,
7016 tf_warning_or_error, fcl_c99);
7017 postfix_expression.set_location (initializer.get_location ());
7018 break;
7022 /* It must be a primary-expression. */
7023 postfix_expression
7024 = cp_parser_primary_expression (parser, address_p, cast_p,
7025 /*template_arg_p=*/false,
7026 decltype_p,
7027 &idk);
7029 break;
7032 /* Note that we don't need to worry about calling build_cplus_new on a
7033 class-valued CALL_EXPR in decltype when it isn't the end of the
7034 postfix-expression; unary_complex_lvalue will take care of that for
7035 all these cases. */
7037 /* Keep looping until the postfix-expression is complete. */
7038 while (true)
7040 if (idk == CP_ID_KIND_UNQUALIFIED
7041 && identifier_p (postfix_expression)
7042 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7043 /* It is not a Koenig lookup function call. */
7044 postfix_expression
7045 = unqualified_name_lookup_error (postfix_expression);
7047 /* Peek at the next token. */
7048 token = cp_lexer_peek_token (parser->lexer);
7050 switch (token->type)
7052 case CPP_OPEN_SQUARE:
7053 if (cp_next_tokens_can_be_std_attribute_p (parser))
7055 cp_parser_error (parser,
7056 "two consecutive %<[%> shall "
7057 "only introduce an attribute");
7058 return error_mark_node;
7060 postfix_expression
7061 = cp_parser_postfix_open_square_expression (parser,
7062 postfix_expression,
7063 false,
7064 decltype_p);
7065 postfix_expression.set_range (start_loc,
7066 postfix_expression.get_location ());
7068 idk = CP_ID_KIND_NONE;
7069 is_member_access = false;
7070 break;
7072 case CPP_OPEN_PAREN:
7073 /* postfix-expression ( expression-list [opt] ) */
7075 bool koenig_p;
7076 bool is_builtin_constant_p;
7077 bool saved_integral_constant_expression_p = false;
7078 bool saved_non_integral_constant_expression_p = false;
7079 tsubst_flags_t complain = complain_flags (decltype_p);
7080 vec<tree, va_gc> *args;
7081 location_t close_paren_loc = UNKNOWN_LOCATION;
7083 is_member_access = false;
7085 is_builtin_constant_p
7086 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7087 if (is_builtin_constant_p)
7089 /* The whole point of __builtin_constant_p is to allow
7090 non-constant expressions to appear as arguments. */
7091 saved_integral_constant_expression_p
7092 = parser->integral_constant_expression_p;
7093 saved_non_integral_constant_expression_p
7094 = parser->non_integral_constant_expression_p;
7095 parser->integral_constant_expression_p = false;
7097 args = (cp_parser_parenthesized_expression_list
7098 (parser, non_attr,
7099 /*cast_p=*/false, /*allow_expansion_p=*/true,
7100 /*non_constant_p=*/NULL,
7101 /*close_paren_loc=*/&close_paren_loc));
7102 if (is_builtin_constant_p)
7104 parser->integral_constant_expression_p
7105 = saved_integral_constant_expression_p;
7106 parser->non_integral_constant_expression_p
7107 = saved_non_integral_constant_expression_p;
7110 if (args == NULL)
7112 postfix_expression = error_mark_node;
7113 break;
7116 /* Function calls are not permitted in
7117 constant-expressions. */
7118 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7119 && cp_parser_non_integral_constant_expression (parser,
7120 NIC_FUNC_CALL))
7122 postfix_expression = error_mark_node;
7123 release_tree_vector (args);
7124 break;
7127 koenig_p = false;
7128 if (idk == CP_ID_KIND_UNQUALIFIED
7129 || idk == CP_ID_KIND_TEMPLATE_ID)
7131 if (identifier_p (postfix_expression))
7133 if (!args->is_empty ())
7135 koenig_p = true;
7136 if (!any_type_dependent_arguments_p (args))
7137 postfix_expression
7138 = perform_koenig_lookup (postfix_expression, args,
7139 complain);
7141 else
7142 postfix_expression
7143 = unqualified_fn_lookup_error (postfix_expression);
7145 /* We do not perform argument-dependent lookup if
7146 normal lookup finds a non-function, in accordance
7147 with the expected resolution of DR 218. */
7148 else if (!args->is_empty ()
7149 && is_overloaded_fn (postfix_expression))
7151 tree fn = get_first_fn (postfix_expression);
7152 fn = STRIP_TEMPLATE (fn);
7154 /* Do not do argument dependent lookup if regular
7155 lookup finds a member function or a block-scope
7156 function declaration. [basic.lookup.argdep]/3 */
7157 if (!DECL_FUNCTION_MEMBER_P (fn)
7158 && !DECL_LOCAL_FUNCTION_P (fn))
7160 koenig_p = true;
7161 if (!any_type_dependent_arguments_p (args))
7162 postfix_expression
7163 = perform_koenig_lookup (postfix_expression, args,
7164 complain);
7169 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7170 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7171 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7172 && vec_safe_length (args) == 3)
7174 tree arg0 = (*args)[0];
7175 tree arg1 = (*args)[1];
7176 tree arg2 = (*args)[2];
7177 int literal_mask = ((!!integer_zerop (arg1) << 1)
7178 | (!!integer_zerop (arg2) << 2));
7179 if (TREE_CODE (arg2) == CONST_DECL)
7180 arg2 = DECL_INITIAL (arg2);
7181 warn_for_memset (input_location, arg0, arg2, literal_mask);
7184 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7186 tree instance = TREE_OPERAND (postfix_expression, 0);
7187 tree fn = TREE_OPERAND (postfix_expression, 1);
7189 if (processing_template_decl
7190 && (type_dependent_object_expression_p (instance)
7191 || (!BASELINK_P (fn)
7192 && TREE_CODE (fn) != FIELD_DECL)
7193 || type_dependent_expression_p (fn)
7194 || any_type_dependent_arguments_p (args)))
7196 maybe_generic_this_capture (instance, fn);
7197 postfix_expression
7198 = build_min_nt_call_vec (postfix_expression, args);
7199 release_tree_vector (args);
7200 break;
7203 if (BASELINK_P (fn))
7205 postfix_expression
7206 = (build_new_method_call
7207 (instance, fn, &args, NULL_TREE,
7208 (idk == CP_ID_KIND_QUALIFIED
7209 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7210 : LOOKUP_NORMAL),
7211 /*fn_p=*/NULL,
7212 complain));
7214 else
7215 postfix_expression
7216 = finish_call_expr (postfix_expression, &args,
7217 /*disallow_virtual=*/false,
7218 /*koenig_p=*/false,
7219 complain);
7221 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7222 || TREE_CODE (postfix_expression) == MEMBER_REF
7223 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7224 postfix_expression = (build_offset_ref_call_from_tree
7225 (postfix_expression, &args,
7226 complain));
7227 else if (idk == CP_ID_KIND_QUALIFIED)
7228 /* A call to a static class member, or a namespace-scope
7229 function. */
7230 postfix_expression
7231 = finish_call_expr (postfix_expression, &args,
7232 /*disallow_virtual=*/true,
7233 koenig_p,
7234 complain);
7235 else
7236 /* All other function calls. */
7237 postfix_expression
7238 = finish_call_expr (postfix_expression, &args,
7239 /*disallow_virtual=*/false,
7240 koenig_p,
7241 complain);
7243 if (close_paren_loc != UNKNOWN_LOCATION)
7245 location_t combined_loc = make_location (token->location,
7246 start_loc,
7247 close_paren_loc);
7248 postfix_expression.set_location (combined_loc);
7251 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7252 idk = CP_ID_KIND_NONE;
7254 release_tree_vector (args);
7256 break;
7258 case CPP_DOT:
7259 case CPP_DEREF:
7260 /* postfix-expression . template [opt] id-expression
7261 postfix-expression . pseudo-destructor-name
7262 postfix-expression -> template [opt] id-expression
7263 postfix-expression -> pseudo-destructor-name */
7265 /* Consume the `.' or `->' operator. */
7266 cp_lexer_consume_token (parser->lexer);
7268 postfix_expression
7269 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7270 postfix_expression,
7271 false, &idk, loc);
7273 is_member_access = true;
7274 break;
7276 case CPP_PLUS_PLUS:
7277 /* postfix-expression ++ */
7278 /* Consume the `++' token. */
7279 cp_lexer_consume_token (parser->lexer);
7280 /* Generate a representation for the complete expression. */
7281 postfix_expression
7282 = finish_increment_expr (postfix_expression,
7283 POSTINCREMENT_EXPR);
7284 /* Increments may not appear in constant-expressions. */
7285 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7286 postfix_expression = error_mark_node;
7287 idk = CP_ID_KIND_NONE;
7288 is_member_access = false;
7289 break;
7291 case CPP_MINUS_MINUS:
7292 /* postfix-expression -- */
7293 /* Consume the `--' token. */
7294 cp_lexer_consume_token (parser->lexer);
7295 /* Generate a representation for the complete expression. */
7296 postfix_expression
7297 = finish_increment_expr (postfix_expression,
7298 POSTDECREMENT_EXPR);
7299 /* Decrements may not appear in constant-expressions. */
7300 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7301 postfix_expression = error_mark_node;
7302 idk = CP_ID_KIND_NONE;
7303 is_member_access = false;
7304 break;
7306 default:
7307 if (pidk_return != NULL)
7308 * pidk_return = idk;
7309 if (member_access_only_p)
7310 return is_member_access
7311 ? postfix_expression
7312 : cp_expr (error_mark_node);
7313 else
7314 return postfix_expression;
7318 /* We should never get here. */
7319 gcc_unreachable ();
7320 return error_mark_node;
7323 /* This function parses Cilk Plus array notations. If a normal array expr. is
7324 parsed then the array index is passed back to the caller through *INIT_INDEX
7325 and the function returns a NULL_TREE. If array notation expr. is parsed,
7326 then *INIT_INDEX is ignored by the caller and the function returns
7327 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7328 error_mark_node. */
7330 static tree
7331 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7332 tree array_value)
7334 cp_token *token = NULL;
7335 tree length_index, stride = NULL_TREE, value_tree, array_type;
7336 if (!array_value || array_value == error_mark_node)
7338 cp_parser_skip_to_end_of_statement (parser);
7339 return error_mark_node;
7342 array_type = TREE_TYPE (array_value);
7344 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7345 parser->colon_corrects_to_scope_p = false;
7346 token = cp_lexer_peek_token (parser->lexer);
7348 if (!token)
7350 cp_parser_error (parser, "expected %<:%> or numeral");
7351 return error_mark_node;
7353 else if (token->type == CPP_COLON)
7355 /* Consume the ':'. */
7356 cp_lexer_consume_token (parser->lexer);
7358 /* If we are here, then we have a case like this A[:]. */
7359 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7361 cp_parser_error (parser, "expected %<]%>");
7362 cp_parser_skip_to_end_of_statement (parser);
7363 return error_mark_node;
7365 *init_index = NULL_TREE;
7366 stride = NULL_TREE;
7367 length_index = NULL_TREE;
7369 else
7371 /* If we are here, then there are three valid possibilities:
7372 1. ARRAY [ EXP ]
7373 2. ARRAY [ EXP : EXP ]
7374 3. ARRAY [ EXP : EXP : EXP ] */
7376 *init_index = cp_parser_expression (parser);
7377 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7379 /* This indicates that we have a normal array expression. */
7380 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7381 return NULL_TREE;
7384 /* Consume the ':'. */
7385 cp_lexer_consume_token (parser->lexer);
7386 length_index = cp_parser_expression (parser);
7387 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7389 cp_lexer_consume_token (parser->lexer);
7390 stride = cp_parser_expression (parser);
7393 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7395 if (*init_index == error_mark_node || length_index == error_mark_node
7396 || stride == error_mark_node || array_type == error_mark_node)
7398 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7399 cp_lexer_consume_token (parser->lexer);
7400 return error_mark_node;
7402 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7404 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7405 length_index, stride, array_type);
7406 return value_tree;
7409 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7410 by cp_parser_builtin_offsetof. We're looking for
7412 postfix-expression [ expression ]
7413 postfix-expression [ braced-init-list ] (C++11)
7415 FOR_OFFSETOF is set if we're being called in that context, which
7416 changes how we deal with integer constant expressions. */
7418 static tree
7419 cp_parser_postfix_open_square_expression (cp_parser *parser,
7420 tree postfix_expression,
7421 bool for_offsetof,
7422 bool decltype_p)
7424 tree index = NULL_TREE;
7425 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7426 bool saved_greater_than_is_operator_p;
7428 /* Consume the `[' token. */
7429 cp_lexer_consume_token (parser->lexer);
7431 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7432 parser->greater_than_is_operator_p = true;
7434 /* Parse the index expression. */
7435 /* ??? For offsetof, there is a question of what to allow here. If
7436 offsetof is not being used in an integral constant expression context,
7437 then we *could* get the right answer by computing the value at runtime.
7438 If we are in an integral constant expression context, then we might
7439 could accept any constant expression; hard to say without analysis.
7440 Rather than open the barn door too wide right away, allow only integer
7441 constant expressions here. */
7442 if (for_offsetof)
7443 index = cp_parser_constant_expression (parser);
7444 else
7446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7448 bool expr_nonconst_p;
7449 cp_lexer_set_source_position (parser->lexer);
7450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7451 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7452 if (flag_cilkplus
7453 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7455 error_at (cp_lexer_peek_token (parser->lexer)->location,
7456 "braced list index is not allowed with array "
7457 "notation");
7458 cp_parser_skip_to_end_of_statement (parser);
7459 return error_mark_node;
7462 else if (flag_cilkplus)
7464 /* Here are have these two options:
7465 ARRAY[EXP : EXP] - Array notation expr with default
7466 stride of 1.
7467 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7468 stride. */
7469 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7470 postfix_expression);
7471 if (an_exp)
7472 return an_exp;
7474 else
7475 index = cp_parser_expression (parser);
7478 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7480 /* Look for the closing `]'. */
7481 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7483 /* Build the ARRAY_REF. */
7484 postfix_expression = grok_array_decl (loc, postfix_expression,
7485 index, decltype_p);
7487 /* When not doing offsetof, array references are not permitted in
7488 constant-expressions. */
7489 if (!for_offsetof
7490 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7491 postfix_expression = error_mark_node;
7493 return postfix_expression;
7496 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7497 by cp_parser_builtin_offsetof. We're looking for
7499 postfix-expression . template [opt] id-expression
7500 postfix-expression . pseudo-destructor-name
7501 postfix-expression -> template [opt] id-expression
7502 postfix-expression -> pseudo-destructor-name
7504 FOR_OFFSETOF is set if we're being called in that context. That sorta
7505 limits what of the above we'll actually accept, but nevermind.
7506 TOKEN_TYPE is the "." or "->" token, which will already have been
7507 removed from the stream. */
7509 static tree
7510 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7511 enum cpp_ttype token_type,
7512 cp_expr postfix_expression,
7513 bool for_offsetof, cp_id_kind *idk,
7514 location_t location)
7516 tree name;
7517 bool dependent_p;
7518 bool pseudo_destructor_p;
7519 tree scope = NULL_TREE;
7520 location_t start_loc = postfix_expression.get_start ();
7522 /* If this is a `->' operator, dereference the pointer. */
7523 if (token_type == CPP_DEREF)
7524 postfix_expression = build_x_arrow (location, postfix_expression,
7525 tf_warning_or_error);
7526 /* Check to see whether or not the expression is type-dependent and
7527 not the current instantiation. */
7528 dependent_p = type_dependent_object_expression_p (postfix_expression);
7529 /* The identifier following the `->' or `.' is not qualified. */
7530 parser->scope = NULL_TREE;
7531 parser->qualifying_scope = NULL_TREE;
7532 parser->object_scope = NULL_TREE;
7533 *idk = CP_ID_KIND_NONE;
7535 /* Enter the scope corresponding to the type of the object
7536 given by the POSTFIX_EXPRESSION. */
7537 if (!dependent_p)
7539 scope = TREE_TYPE (postfix_expression);
7540 /* According to the standard, no expression should ever have
7541 reference type. Unfortunately, we do not currently match
7542 the standard in this respect in that our internal representation
7543 of an expression may have reference type even when the standard
7544 says it does not. Therefore, we have to manually obtain the
7545 underlying type here. */
7546 scope = non_reference (scope);
7547 /* The type of the POSTFIX_EXPRESSION must be complete. */
7548 /* Unlike the object expression in other contexts, *this is not
7549 required to be of complete type for purposes of class member
7550 access (5.2.5) outside the member function body. */
7551 if (postfix_expression != current_class_ref
7552 && scope != error_mark_node
7553 && !(processing_template_decl
7554 && current_class_type
7555 && (same_type_ignoring_top_level_qualifiers_p
7556 (scope, current_class_type))))
7558 scope = complete_type (scope);
7559 if (!COMPLETE_TYPE_P (scope)
7560 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7561 && EXPR_P (postfix_expression))
7563 /* In a template, be permissive by treating an object expression
7564 of incomplete type as dependent (after a pedwarn). */
7565 diagnostic_t kind = (processing_template_decl
7566 && MAYBE_CLASS_TYPE_P (scope)
7567 ? DK_PEDWARN
7568 : DK_ERROR);
7569 cxx_incomplete_type_diagnostic
7570 (location_of (postfix_expression),
7571 postfix_expression, scope, kind);
7572 if (!MAYBE_CLASS_TYPE_P (scope))
7573 return error_mark_node;
7574 if (processing_template_decl)
7576 dependent_p = true;
7577 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7582 if (!dependent_p)
7584 /* Let the name lookup machinery know that we are processing a
7585 class member access expression. */
7586 parser->context->object_type = scope;
7587 /* If something went wrong, we want to be able to discern that case,
7588 as opposed to the case where there was no SCOPE due to the type
7589 of expression being dependent. */
7590 if (!scope)
7591 scope = error_mark_node;
7592 /* If the SCOPE was erroneous, make the various semantic analysis
7593 functions exit quickly -- and without issuing additional error
7594 messages. */
7595 if (scope == error_mark_node)
7596 postfix_expression = error_mark_node;
7600 if (dependent_p)
7601 /* Tell cp_parser_lookup_name that there was an object, even though it's
7602 type-dependent. */
7603 parser->context->object_type = unknown_type_node;
7605 /* Assume this expression is not a pseudo-destructor access. */
7606 pseudo_destructor_p = false;
7608 /* If the SCOPE is a scalar type, then, if this is a valid program,
7609 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7610 is type dependent, it can be pseudo-destructor-name or something else.
7611 Try to parse it as pseudo-destructor-name first. */
7612 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7614 tree s;
7615 tree type;
7617 cp_parser_parse_tentatively (parser);
7618 /* Parse the pseudo-destructor-name. */
7619 s = NULL_TREE;
7620 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7621 &s, &type);
7622 if (dependent_p
7623 && (cp_parser_error_occurred (parser)
7624 || !SCALAR_TYPE_P (type)))
7625 cp_parser_abort_tentative_parse (parser);
7626 else if (cp_parser_parse_definitely (parser))
7628 pseudo_destructor_p = true;
7629 postfix_expression
7630 = finish_pseudo_destructor_expr (postfix_expression,
7631 s, type, location);
7635 if (!pseudo_destructor_p)
7637 /* If the SCOPE is not a scalar type, we are looking at an
7638 ordinary class member access expression, rather than a
7639 pseudo-destructor-name. */
7640 bool template_p;
7641 cp_token *token = cp_lexer_peek_token (parser->lexer);
7642 /* Parse the id-expression. */
7643 name = (cp_parser_id_expression
7644 (parser,
7645 cp_parser_optional_template_keyword (parser),
7646 /*check_dependency_p=*/true,
7647 &template_p,
7648 /*declarator_p=*/false,
7649 /*optional_p=*/false));
7650 /* In general, build a SCOPE_REF if the member name is qualified.
7651 However, if the name was not dependent and has already been
7652 resolved; there is no need to build the SCOPE_REF. For example;
7654 struct X { void f(); };
7655 template <typename T> void f(T* t) { t->X::f(); }
7657 Even though "t" is dependent, "X::f" is not and has been resolved
7658 to a BASELINK; there is no need to include scope information. */
7660 /* But we do need to remember that there was an explicit scope for
7661 virtual function calls. */
7662 if (parser->scope)
7663 *idk = CP_ID_KIND_QUALIFIED;
7665 /* If the name is a template-id that names a type, we will get a
7666 TYPE_DECL here. That is invalid code. */
7667 if (TREE_CODE (name) == TYPE_DECL)
7669 error_at (token->location, "invalid use of %qD", name);
7670 postfix_expression = error_mark_node;
7672 else
7674 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7676 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7678 error_at (token->location, "%<%D::%D%> is not a class member",
7679 parser->scope, name);
7680 postfix_expression = error_mark_node;
7682 else
7683 name = build_qualified_name (/*type=*/NULL_TREE,
7684 parser->scope,
7685 name,
7686 template_p);
7687 parser->scope = NULL_TREE;
7688 parser->qualifying_scope = NULL_TREE;
7689 parser->object_scope = NULL_TREE;
7691 if (parser->scope && name && BASELINK_P (name))
7692 adjust_result_of_qualified_name_lookup
7693 (name, parser->scope, scope);
7694 postfix_expression
7695 = finish_class_member_access_expr (postfix_expression, name,
7696 template_p,
7697 tf_warning_or_error);
7698 /* Build a location e.g.:
7699 ptr->access_expr
7700 ~~~^~~~~~~~~~~~~
7701 where the caret is at the deref token, ranging from
7702 the start of postfix_expression to the end of the access expr. */
7703 location_t end_loc
7704 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7705 location_t combined_loc
7706 = make_location (input_location, start_loc, end_loc);
7707 protected_set_expr_location (postfix_expression, combined_loc);
7711 /* We no longer need to look up names in the scope of the object on
7712 the left-hand side of the `.' or `->' operator. */
7713 parser->context->object_type = NULL_TREE;
7715 /* Outside of offsetof, these operators may not appear in
7716 constant-expressions. */
7717 if (!for_offsetof
7718 && (cp_parser_non_integral_constant_expression
7719 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7720 postfix_expression = error_mark_node;
7722 return postfix_expression;
7725 /* Parse a parenthesized expression-list.
7727 expression-list:
7728 assignment-expression
7729 expression-list, assignment-expression
7731 attribute-list:
7732 expression-list
7733 identifier
7734 identifier, expression-list
7736 CAST_P is true if this expression is the target of a cast.
7738 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7739 argument pack.
7741 Returns a vector of trees. Each element is a representation of an
7742 assignment-expression. NULL is returned if the ( and or ) are
7743 missing. An empty, but allocated, vector is returned on no
7744 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7745 if we are parsing an attribute list for an attribute that wants a
7746 plain identifier argument, normal_attr for an attribute that wants
7747 an expression, or non_attr if we aren't parsing an attribute list. If
7748 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7749 not all of the expressions in the list were constant.
7750 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7751 will be written to with the location of the closing parenthesis. If
7752 an error occurs, it may or may not be written to. */
7754 static vec<tree, va_gc> *
7755 cp_parser_parenthesized_expression_list (cp_parser* parser,
7756 int is_attribute_list,
7757 bool cast_p,
7758 bool allow_expansion_p,
7759 bool *non_constant_p,
7760 location_t *close_paren_loc)
7762 vec<tree, va_gc> *expression_list;
7763 bool fold_expr_p = is_attribute_list != non_attr;
7764 tree identifier = NULL_TREE;
7765 bool saved_greater_than_is_operator_p;
7767 /* Assume all the expressions will be constant. */
7768 if (non_constant_p)
7769 *non_constant_p = false;
7771 matching_parens parens;
7772 if (!parens.require_open (parser))
7773 return NULL;
7775 expression_list = make_tree_vector ();
7777 /* Within a parenthesized expression, a `>' token is always
7778 the greater-than operator. */
7779 saved_greater_than_is_operator_p
7780 = parser->greater_than_is_operator_p;
7781 parser->greater_than_is_operator_p = true;
7783 /* Consume expressions until there are no more. */
7784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7785 while (true)
7787 tree expr;
7789 /* At the beginning of attribute lists, check to see if the
7790 next token is an identifier. */
7791 if (is_attribute_list == id_attr
7792 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7794 cp_token *token;
7796 /* Consume the identifier. */
7797 token = cp_lexer_consume_token (parser->lexer);
7798 /* Save the identifier. */
7799 identifier = token->u.value;
7801 else
7803 bool expr_non_constant_p;
7805 /* Parse the next assignment-expression. */
7806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7808 /* A braced-init-list. */
7809 cp_lexer_set_source_position (parser->lexer);
7810 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7811 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7812 if (non_constant_p && expr_non_constant_p)
7813 *non_constant_p = true;
7815 else if (non_constant_p)
7817 expr = (cp_parser_constant_expression
7818 (parser, /*allow_non_constant_p=*/true,
7819 &expr_non_constant_p));
7820 if (expr_non_constant_p)
7821 *non_constant_p = true;
7823 else
7824 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7825 cast_p);
7827 if (fold_expr_p)
7828 expr = instantiate_non_dependent_expr (expr);
7830 /* If we have an ellipsis, then this is an expression
7831 expansion. */
7832 if (allow_expansion_p
7833 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7835 /* Consume the `...'. */
7836 cp_lexer_consume_token (parser->lexer);
7838 /* Build the argument pack. */
7839 expr = make_pack_expansion (expr);
7842 /* Add it to the list. We add error_mark_node
7843 expressions to the list, so that we can still tell if
7844 the correct form for a parenthesized expression-list
7845 is found. That gives better errors. */
7846 vec_safe_push (expression_list, expr);
7848 if (expr == error_mark_node)
7849 goto skip_comma;
7852 /* After the first item, attribute lists look the same as
7853 expression lists. */
7854 is_attribute_list = non_attr;
7856 get_comma:;
7857 /* If the next token isn't a `,', then we are done. */
7858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7859 break;
7861 /* Otherwise, consume the `,' and keep going. */
7862 cp_lexer_consume_token (parser->lexer);
7865 if (close_paren_loc)
7866 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7868 if (!parens.require_close (parser))
7870 int ending;
7872 skip_comma:;
7873 /* We try and resync to an unnested comma, as that will give the
7874 user better diagnostics. */
7875 ending = cp_parser_skip_to_closing_parenthesis (parser,
7876 /*recovering=*/true,
7877 /*or_comma=*/true,
7878 /*consume_paren=*/true);
7879 if (ending < 0)
7880 goto get_comma;
7881 if (!ending)
7883 parser->greater_than_is_operator_p
7884 = saved_greater_than_is_operator_p;
7885 return NULL;
7889 parser->greater_than_is_operator_p
7890 = saved_greater_than_is_operator_p;
7892 if (identifier)
7893 vec_safe_insert (expression_list, 0, identifier);
7895 return expression_list;
7898 /* Parse a pseudo-destructor-name.
7900 pseudo-destructor-name:
7901 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7902 :: [opt] nested-name-specifier template template-id :: ~ type-name
7903 :: [opt] nested-name-specifier [opt] ~ type-name
7905 If either of the first two productions is used, sets *SCOPE to the
7906 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7907 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7908 or ERROR_MARK_NODE if the parse fails. */
7910 static void
7911 cp_parser_pseudo_destructor_name (cp_parser* parser,
7912 tree object,
7913 tree* scope,
7914 tree* type)
7916 bool nested_name_specifier_p;
7918 /* Handle ~auto. */
7919 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7920 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7921 && !type_dependent_expression_p (object))
7923 if (cxx_dialect < cxx14)
7924 pedwarn (input_location, 0,
7925 "%<~auto%> only available with "
7926 "-std=c++14 or -std=gnu++14");
7927 cp_lexer_consume_token (parser->lexer);
7928 cp_lexer_consume_token (parser->lexer);
7929 *scope = NULL_TREE;
7930 *type = TREE_TYPE (object);
7931 return;
7934 /* Assume that things will not work out. */
7935 *type = error_mark_node;
7937 /* Look for the optional `::' operator. */
7938 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7939 /* Look for the optional nested-name-specifier. */
7940 nested_name_specifier_p
7941 = (cp_parser_nested_name_specifier_opt (parser,
7942 /*typename_keyword_p=*/false,
7943 /*check_dependency_p=*/true,
7944 /*type_p=*/false,
7945 /*is_declaration=*/false)
7946 != NULL_TREE);
7947 /* Now, if we saw a nested-name-specifier, we might be doing the
7948 second production. */
7949 if (nested_name_specifier_p
7950 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7952 /* Consume the `template' keyword. */
7953 cp_lexer_consume_token (parser->lexer);
7954 /* Parse the template-id. */
7955 cp_parser_template_id (parser,
7956 /*template_keyword_p=*/true,
7957 /*check_dependency_p=*/false,
7958 class_type,
7959 /*is_declaration=*/true);
7960 /* Look for the `::' token. */
7961 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7963 /* If the next token is not a `~', then there might be some
7964 additional qualification. */
7965 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7967 /* At this point, we're looking for "type-name :: ~". The type-name
7968 must not be a class-name, since this is a pseudo-destructor. So,
7969 it must be either an enum-name, or a typedef-name -- both of which
7970 are just identifiers. So, we peek ahead to check that the "::"
7971 and "~" tokens are present; if they are not, then we can avoid
7972 calling type_name. */
7973 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7974 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7975 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7977 cp_parser_error (parser, "non-scalar type");
7978 return;
7981 /* Look for the type-name. */
7982 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7983 if (*scope == error_mark_node)
7984 return;
7986 /* Look for the `::' token. */
7987 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7989 else
7990 *scope = NULL_TREE;
7992 /* Look for the `~'. */
7993 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7995 /* Once we see the ~, this has to be a pseudo-destructor. */
7996 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7997 cp_parser_commit_to_topmost_tentative_parse (parser);
7999 /* Look for the type-name again. We are not responsible for
8000 checking that it matches the first type-name. */
8001 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8004 /* Parse a unary-expression.
8006 unary-expression:
8007 postfix-expression
8008 ++ cast-expression
8009 -- cast-expression
8010 unary-operator cast-expression
8011 sizeof unary-expression
8012 sizeof ( type-id )
8013 alignof ( type-id ) [C++0x]
8014 new-expression
8015 delete-expression
8017 GNU Extensions:
8019 unary-expression:
8020 __extension__ cast-expression
8021 __alignof__ unary-expression
8022 __alignof__ ( type-id )
8023 alignof unary-expression [C++0x]
8024 __real__ cast-expression
8025 __imag__ cast-expression
8026 && identifier
8027 sizeof ( type-id ) { initializer-list , [opt] }
8028 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8029 __alignof__ ( type-id ) { initializer-list , [opt] }
8031 ADDRESS_P is true iff the unary-expression is appearing as the
8032 operand of the `&' operator. CAST_P is true if this expression is
8033 the target of a cast.
8035 Returns a representation of the expression. */
8037 static cp_expr
8038 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8039 bool address_p, bool cast_p, bool decltype_p)
8041 cp_token *token;
8042 enum tree_code unary_operator;
8044 /* Peek at the next token. */
8045 token = cp_lexer_peek_token (parser->lexer);
8046 /* Some keywords give away the kind of expression. */
8047 if (token->type == CPP_KEYWORD)
8049 enum rid keyword = token->keyword;
8051 switch (keyword)
8053 case RID_ALIGNOF:
8054 case RID_SIZEOF:
8056 tree operand, ret;
8057 enum tree_code op;
8058 location_t start_loc = token->location;
8060 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8061 /* Consume the token. */
8062 cp_lexer_consume_token (parser->lexer);
8063 /* Parse the operand. */
8064 operand = cp_parser_sizeof_operand (parser, keyword);
8066 if (TYPE_P (operand))
8067 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8068 else
8070 /* ISO C++ defines alignof only with types, not with
8071 expressions. So pedwarn if alignof is used with a non-
8072 type expression. However, __alignof__ is ok. */
8073 if (id_equal (token->u.value, "alignof"))
8074 pedwarn (token->location, OPT_Wpedantic,
8075 "ISO C++ does not allow %<alignof%> "
8076 "with a non-type");
8078 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8080 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8081 SIZEOF_EXPR with the original operand. */
8082 if (op == SIZEOF_EXPR && ret != error_mark_node)
8084 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8086 if (!processing_template_decl && TYPE_P (operand))
8088 ret = build_min (SIZEOF_EXPR, size_type_node,
8089 build1 (NOP_EXPR, operand,
8090 error_mark_node));
8091 SIZEOF_EXPR_TYPE_P (ret) = 1;
8093 else
8094 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8095 TREE_SIDE_EFFECTS (ret) = 0;
8096 TREE_READONLY (ret) = 1;
8100 /* Construct a location e.g. :
8101 alignof (expr)
8102 ^~~~~~~~~~~~~~
8103 with start == caret at the start of the "alignof"/"sizeof"
8104 token, with the endpoint at the final closing paren. */
8105 location_t finish_loc
8106 = cp_lexer_previous_token (parser->lexer)->location;
8107 location_t compound_loc
8108 = make_location (start_loc, start_loc, finish_loc);
8110 cp_expr ret_expr (ret);
8111 ret_expr.set_location (compound_loc);
8112 return ret_expr;
8115 case RID_NEW:
8116 return cp_parser_new_expression (parser);
8118 case RID_DELETE:
8119 return cp_parser_delete_expression (parser);
8121 case RID_EXTENSION:
8123 /* The saved value of the PEDANTIC flag. */
8124 int saved_pedantic;
8125 tree expr;
8127 /* Save away the PEDANTIC flag. */
8128 cp_parser_extension_opt (parser, &saved_pedantic);
8129 /* Parse the cast-expression. */
8130 expr = cp_parser_simple_cast_expression (parser);
8131 /* Restore the PEDANTIC flag. */
8132 pedantic = saved_pedantic;
8134 return expr;
8137 case RID_REALPART:
8138 case RID_IMAGPART:
8140 tree expression;
8142 /* Consume the `__real__' or `__imag__' token. */
8143 cp_lexer_consume_token (parser->lexer);
8144 /* Parse the cast-expression. */
8145 expression = cp_parser_simple_cast_expression (parser);
8146 /* Create the complete representation. */
8147 return build_x_unary_op (token->location,
8148 (keyword == RID_REALPART
8149 ? REALPART_EXPR : IMAGPART_EXPR),
8150 expression,
8151 tf_warning_or_error);
8153 break;
8155 case RID_TRANSACTION_ATOMIC:
8156 case RID_TRANSACTION_RELAXED:
8157 return cp_parser_transaction_expression (parser, keyword);
8159 case RID_NOEXCEPT:
8161 tree expr;
8162 const char *saved_message;
8163 bool saved_integral_constant_expression_p;
8164 bool saved_non_integral_constant_expression_p;
8165 bool saved_greater_than_is_operator_p;
8167 cp_lexer_consume_token (parser->lexer);
8168 matching_parens parens;
8169 parens.require_open (parser);
8171 saved_message = parser->type_definition_forbidden_message;
8172 parser->type_definition_forbidden_message
8173 = G_("types may not be defined in %<noexcept%> expressions");
8175 saved_integral_constant_expression_p
8176 = parser->integral_constant_expression_p;
8177 saved_non_integral_constant_expression_p
8178 = parser->non_integral_constant_expression_p;
8179 parser->integral_constant_expression_p = false;
8181 saved_greater_than_is_operator_p
8182 = parser->greater_than_is_operator_p;
8183 parser->greater_than_is_operator_p = true;
8185 ++cp_unevaluated_operand;
8186 ++c_inhibit_evaluation_warnings;
8187 ++cp_noexcept_operand;
8188 expr = cp_parser_expression (parser);
8189 --cp_noexcept_operand;
8190 --c_inhibit_evaluation_warnings;
8191 --cp_unevaluated_operand;
8193 parser->greater_than_is_operator_p
8194 = saved_greater_than_is_operator_p;
8196 parser->integral_constant_expression_p
8197 = saved_integral_constant_expression_p;
8198 parser->non_integral_constant_expression_p
8199 = saved_non_integral_constant_expression_p;
8201 parser->type_definition_forbidden_message = saved_message;
8203 parens.require_close (parser);
8204 return finish_noexcept_expr (expr, tf_warning_or_error);
8207 default:
8208 break;
8212 /* Look for the `:: new' and `:: delete', which also signal the
8213 beginning of a new-expression, or delete-expression,
8214 respectively. If the next token is `::', then it might be one of
8215 these. */
8216 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8218 enum rid keyword;
8220 /* See if the token after the `::' is one of the keywords in
8221 which we're interested. */
8222 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8223 /* If it's `new', we have a new-expression. */
8224 if (keyword == RID_NEW)
8225 return cp_parser_new_expression (parser);
8226 /* Similarly, for `delete'. */
8227 else if (keyword == RID_DELETE)
8228 return cp_parser_delete_expression (parser);
8231 /* Look for a unary operator. */
8232 unary_operator = cp_parser_unary_operator (token);
8233 /* The `++' and `--' operators can be handled similarly, even though
8234 they are not technically unary-operators in the grammar. */
8235 if (unary_operator == ERROR_MARK)
8237 if (token->type == CPP_PLUS_PLUS)
8238 unary_operator = PREINCREMENT_EXPR;
8239 else if (token->type == CPP_MINUS_MINUS)
8240 unary_operator = PREDECREMENT_EXPR;
8241 /* Handle the GNU address-of-label extension. */
8242 else if (cp_parser_allow_gnu_extensions_p (parser)
8243 && token->type == CPP_AND_AND)
8245 tree identifier;
8246 tree expression;
8247 location_t start_loc = token->location;
8249 /* Consume the '&&' token. */
8250 cp_lexer_consume_token (parser->lexer);
8251 /* Look for the identifier. */
8252 location_t finish_loc
8253 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8254 identifier = cp_parser_identifier (parser);
8255 /* Construct a location of the form:
8256 &&label
8257 ^~~~~~~
8258 with caret==start at the "&&", finish at the end of the label. */
8259 location_t combined_loc
8260 = make_location (start_loc, start_loc, finish_loc);
8261 /* Create an expression representing the address. */
8262 expression = finish_label_address_expr (identifier, combined_loc);
8263 if (cp_parser_non_integral_constant_expression (parser,
8264 NIC_ADDR_LABEL))
8265 expression = error_mark_node;
8266 return expression;
8269 if (unary_operator != ERROR_MARK)
8271 cp_expr cast_expression;
8272 cp_expr expression = error_mark_node;
8273 non_integral_constant non_constant_p = NIC_NONE;
8274 location_t loc = token->location;
8275 tsubst_flags_t complain = complain_flags (decltype_p);
8277 /* Consume the operator token. */
8278 token = cp_lexer_consume_token (parser->lexer);
8279 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8281 /* Parse the cast-expression. */
8282 cast_expression
8283 = cp_parser_cast_expression (parser,
8284 unary_operator == ADDR_EXPR,
8285 /*cast_p=*/false,
8286 /*decltype*/false,
8287 pidk);
8289 /* Make a location:
8290 OP_TOKEN CAST_EXPRESSION
8291 ^~~~~~~~~~~~~~~~~~~~~~~~~
8292 with start==caret at the operator token, and
8293 extending to the end of the cast_expression. */
8294 loc = make_location (loc, loc, cast_expression.get_finish ());
8296 /* Now, build an appropriate representation. */
8297 switch (unary_operator)
8299 case INDIRECT_REF:
8300 non_constant_p = NIC_STAR;
8301 expression = build_x_indirect_ref (loc, cast_expression,
8302 RO_UNARY_STAR,
8303 complain);
8304 /* TODO: build_x_indirect_ref does not always honor the
8305 location, so ensure it is set. */
8306 expression.set_location (loc);
8307 break;
8309 case ADDR_EXPR:
8310 non_constant_p = NIC_ADDR;
8311 /* Fall through. */
8312 case BIT_NOT_EXPR:
8313 expression = build_x_unary_op (loc, unary_operator,
8314 cast_expression,
8315 complain);
8316 /* TODO: build_x_unary_op does not always honor the location,
8317 so ensure it is set. */
8318 expression.set_location (loc);
8319 break;
8321 case PREINCREMENT_EXPR:
8322 case PREDECREMENT_EXPR:
8323 non_constant_p = unary_operator == PREINCREMENT_EXPR
8324 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8325 /* Fall through. */
8326 case NEGATE_EXPR:
8327 /* Immediately fold negation of a constant, unless the constant is 0
8328 (since -0 == 0) or it would overflow. */
8329 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8330 && CONSTANT_CLASS_P (cast_expression)
8331 && !integer_zerop (cast_expression)
8332 && !TREE_OVERFLOW (cast_expression))
8334 tree folded = fold_build1 (unary_operator,
8335 TREE_TYPE (cast_expression),
8336 cast_expression);
8337 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8339 expression = cp_expr (folded, loc);
8340 break;
8343 /* Fall through. */
8344 case UNARY_PLUS_EXPR:
8345 case TRUTH_NOT_EXPR:
8346 expression = finish_unary_op_expr (loc, unary_operator,
8347 cast_expression, complain);
8348 break;
8350 default:
8351 gcc_unreachable ();
8354 if (non_constant_p != NIC_NONE
8355 && cp_parser_non_integral_constant_expression (parser,
8356 non_constant_p))
8357 expression = error_mark_node;
8359 return expression;
8362 return cp_parser_postfix_expression (parser, address_p, cast_p,
8363 /*member_access_only_p=*/false,
8364 decltype_p,
8365 pidk);
8368 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8369 unary-operator, the corresponding tree code is returned. */
8371 static enum tree_code
8372 cp_parser_unary_operator (cp_token* token)
8374 switch (token->type)
8376 case CPP_MULT:
8377 return INDIRECT_REF;
8379 case CPP_AND:
8380 return ADDR_EXPR;
8382 case CPP_PLUS:
8383 return UNARY_PLUS_EXPR;
8385 case CPP_MINUS:
8386 return NEGATE_EXPR;
8388 case CPP_NOT:
8389 return TRUTH_NOT_EXPR;
8391 case CPP_COMPL:
8392 return BIT_NOT_EXPR;
8394 default:
8395 return ERROR_MARK;
8399 /* Parse a new-expression.
8401 new-expression:
8402 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8403 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8405 Returns a representation of the expression. */
8407 static tree
8408 cp_parser_new_expression (cp_parser* parser)
8410 bool global_scope_p;
8411 vec<tree, va_gc> *placement;
8412 tree type;
8413 vec<tree, va_gc> *initializer;
8414 tree nelts = NULL_TREE;
8415 tree ret;
8417 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8419 /* Look for the optional `::' operator. */
8420 global_scope_p
8421 = (cp_parser_global_scope_opt (parser,
8422 /*current_scope_valid_p=*/false)
8423 != NULL_TREE);
8424 /* Look for the `new' operator. */
8425 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8426 /* There's no easy way to tell a new-placement from the
8427 `( type-id )' construct. */
8428 cp_parser_parse_tentatively (parser);
8429 /* Look for a new-placement. */
8430 placement = cp_parser_new_placement (parser);
8431 /* If that didn't work out, there's no new-placement. */
8432 if (!cp_parser_parse_definitely (parser))
8434 if (placement != NULL)
8435 release_tree_vector (placement);
8436 placement = NULL;
8439 /* If the next token is a `(', then we have a parenthesized
8440 type-id. */
8441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8443 cp_token *token;
8444 const char *saved_message = parser->type_definition_forbidden_message;
8446 /* Consume the `('. */
8447 matching_parens parens;
8448 parens.consume_open (parser);
8450 /* Parse the type-id. */
8451 parser->type_definition_forbidden_message
8452 = G_("types may not be defined in a new-expression");
8454 type_id_in_expr_sentinel s (parser);
8455 type = cp_parser_type_id (parser);
8457 parser->type_definition_forbidden_message = saved_message;
8459 /* Look for the closing `)'. */
8460 parens.require_close (parser);
8461 token = cp_lexer_peek_token (parser->lexer);
8462 /* There should not be a direct-new-declarator in this production,
8463 but GCC used to allowed this, so we check and emit a sensible error
8464 message for this case. */
8465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8467 error_at (token->location,
8468 "array bound forbidden after parenthesized type-id");
8469 inform (token->location,
8470 "try removing the parentheses around the type-id");
8471 cp_parser_direct_new_declarator (parser);
8474 /* Otherwise, there must be a new-type-id. */
8475 else
8476 type = cp_parser_new_type_id (parser, &nelts);
8478 /* If the next token is a `(' or '{', then we have a new-initializer. */
8479 cp_token *token = cp_lexer_peek_token (parser->lexer);
8480 if (token->type == CPP_OPEN_PAREN
8481 || token->type == CPP_OPEN_BRACE)
8482 initializer = cp_parser_new_initializer (parser);
8483 else
8484 initializer = NULL;
8486 /* A new-expression may not appear in an integral constant
8487 expression. */
8488 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8489 ret = error_mark_node;
8490 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8491 of a new-type-id or type-id of a new-expression, the new-expression shall
8492 contain a new-initializer of the form ( assignment-expression )".
8493 Additionally, consistently with the spirit of DR 1467, we want to accept
8494 'new auto { 2 }' too. */
8495 else if ((ret = type_uses_auto (type))
8496 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8497 && (vec_safe_length (initializer) != 1
8498 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8499 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8501 error_at (token->location,
8502 "initialization of new-expression for type %<auto%> "
8503 "requires exactly one element");
8504 ret = error_mark_node;
8506 else
8508 /* Construct a location e.g.:
8509 ptr = new int[100]
8510 ^~~~~~~~~~~~
8511 with caret == start at the start of the "new" token, and the end
8512 at the end of the final token we consumed. */
8513 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8514 location_t end_loc = get_finish (end_tok->location);
8515 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8517 /* Create a representation of the new-expression. */
8518 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8519 tf_warning_or_error);
8520 protected_set_expr_location (ret, combined_loc);
8523 if (placement != NULL)
8524 release_tree_vector (placement);
8525 if (initializer != NULL)
8526 release_tree_vector (initializer);
8528 return ret;
8531 /* Parse a new-placement.
8533 new-placement:
8534 ( expression-list )
8536 Returns the same representation as for an expression-list. */
8538 static vec<tree, va_gc> *
8539 cp_parser_new_placement (cp_parser* parser)
8541 vec<tree, va_gc> *expression_list;
8543 /* Parse the expression-list. */
8544 expression_list = (cp_parser_parenthesized_expression_list
8545 (parser, non_attr, /*cast_p=*/false,
8546 /*allow_expansion_p=*/true,
8547 /*non_constant_p=*/NULL));
8549 if (expression_list && expression_list->is_empty ())
8550 error ("expected expression-list or type-id");
8552 return expression_list;
8555 /* Parse a new-type-id.
8557 new-type-id:
8558 type-specifier-seq new-declarator [opt]
8560 Returns the TYPE allocated. If the new-type-id indicates an array
8561 type, *NELTS is set to the number of elements in the last array
8562 bound; the TYPE will not include the last array bound. */
8564 static tree
8565 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8567 cp_decl_specifier_seq type_specifier_seq;
8568 cp_declarator *new_declarator;
8569 cp_declarator *declarator;
8570 cp_declarator *outer_declarator;
8571 const char *saved_message;
8573 /* The type-specifier sequence must not contain type definitions.
8574 (It cannot contain declarations of new types either, but if they
8575 are not definitions we will catch that because they are not
8576 complete.) */
8577 saved_message = parser->type_definition_forbidden_message;
8578 parser->type_definition_forbidden_message
8579 = G_("types may not be defined in a new-type-id");
8580 /* Parse the type-specifier-seq. */
8581 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8582 /*is_trailing_return=*/false,
8583 &type_specifier_seq);
8584 /* Restore the old message. */
8585 parser->type_definition_forbidden_message = saved_message;
8587 if (type_specifier_seq.type == error_mark_node)
8588 return error_mark_node;
8590 /* Parse the new-declarator. */
8591 new_declarator = cp_parser_new_declarator_opt (parser);
8593 /* Determine the number of elements in the last array dimension, if
8594 any. */
8595 *nelts = NULL_TREE;
8596 /* Skip down to the last array dimension. */
8597 declarator = new_declarator;
8598 outer_declarator = NULL;
8599 while (declarator && (declarator->kind == cdk_pointer
8600 || declarator->kind == cdk_ptrmem))
8602 outer_declarator = declarator;
8603 declarator = declarator->declarator;
8605 while (declarator
8606 && declarator->kind == cdk_array
8607 && declarator->declarator
8608 && declarator->declarator->kind == cdk_array)
8610 outer_declarator = declarator;
8611 declarator = declarator->declarator;
8614 if (declarator && declarator->kind == cdk_array)
8616 *nelts = declarator->u.array.bounds;
8617 if (*nelts == error_mark_node)
8618 *nelts = integer_one_node;
8620 if (outer_declarator)
8621 outer_declarator->declarator = declarator->declarator;
8622 else
8623 new_declarator = NULL;
8626 return groktypename (&type_specifier_seq, new_declarator, false);
8629 /* Parse an (optional) new-declarator.
8631 new-declarator:
8632 ptr-operator new-declarator [opt]
8633 direct-new-declarator
8635 Returns the declarator. */
8637 static cp_declarator *
8638 cp_parser_new_declarator_opt (cp_parser* parser)
8640 enum tree_code code;
8641 tree type, std_attributes = NULL_TREE;
8642 cp_cv_quals cv_quals;
8644 /* We don't know if there's a ptr-operator next, or not. */
8645 cp_parser_parse_tentatively (parser);
8646 /* Look for a ptr-operator. */
8647 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8648 /* If that worked, look for more new-declarators. */
8649 if (cp_parser_parse_definitely (parser))
8651 cp_declarator *declarator;
8653 /* Parse another optional declarator. */
8654 declarator = cp_parser_new_declarator_opt (parser);
8656 declarator = cp_parser_make_indirect_declarator
8657 (code, type, cv_quals, declarator, std_attributes);
8659 return declarator;
8662 /* If the next token is a `[', there is a direct-new-declarator. */
8663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8664 return cp_parser_direct_new_declarator (parser);
8666 return NULL;
8669 /* Parse a direct-new-declarator.
8671 direct-new-declarator:
8672 [ expression ]
8673 direct-new-declarator [constant-expression]
8677 static cp_declarator *
8678 cp_parser_direct_new_declarator (cp_parser* parser)
8680 cp_declarator *declarator = NULL;
8682 while (true)
8684 tree expression;
8685 cp_token *token;
8687 /* Look for the opening `['. */
8688 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8690 token = cp_lexer_peek_token (parser->lexer);
8691 expression = cp_parser_expression (parser);
8692 /* The standard requires that the expression have integral
8693 type. DR 74 adds enumeration types. We believe that the
8694 real intent is that these expressions be handled like the
8695 expression in a `switch' condition, which also allows
8696 classes with a single conversion to integral or
8697 enumeration type. */
8698 if (!processing_template_decl)
8700 expression
8701 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8702 expression,
8703 /*complain=*/true);
8704 if (!expression)
8706 error_at (token->location,
8707 "expression in new-declarator must have integral "
8708 "or enumeration type");
8709 expression = error_mark_node;
8713 /* Look for the closing `]'. */
8714 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8716 /* Add this bound to the declarator. */
8717 declarator = make_array_declarator (declarator, expression);
8719 /* If the next token is not a `[', then there are no more
8720 bounds. */
8721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8722 break;
8725 return declarator;
8728 /* Parse a new-initializer.
8730 new-initializer:
8731 ( expression-list [opt] )
8732 braced-init-list
8734 Returns a representation of the expression-list. */
8736 static vec<tree, va_gc> *
8737 cp_parser_new_initializer (cp_parser* parser)
8739 vec<tree, va_gc> *expression_list;
8741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8743 tree t;
8744 bool expr_non_constant_p;
8745 cp_lexer_set_source_position (parser->lexer);
8746 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8747 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8748 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8749 expression_list = make_tree_vector_single (t);
8751 else
8752 expression_list = (cp_parser_parenthesized_expression_list
8753 (parser, non_attr, /*cast_p=*/false,
8754 /*allow_expansion_p=*/true,
8755 /*non_constant_p=*/NULL));
8757 return expression_list;
8760 /* Parse a delete-expression.
8762 delete-expression:
8763 :: [opt] delete cast-expression
8764 :: [opt] delete [ ] cast-expression
8766 Returns a representation of the expression. */
8768 static tree
8769 cp_parser_delete_expression (cp_parser* parser)
8771 bool global_scope_p;
8772 bool array_p;
8773 tree expression;
8775 /* Look for the optional `::' operator. */
8776 global_scope_p
8777 = (cp_parser_global_scope_opt (parser,
8778 /*current_scope_valid_p=*/false)
8779 != NULL_TREE);
8780 /* Look for the `delete' keyword. */
8781 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8782 /* See if the array syntax is in use. */
8783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8785 /* Consume the `[' token. */
8786 cp_lexer_consume_token (parser->lexer);
8787 /* Look for the `]' token. */
8788 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8789 /* Remember that this is the `[]' construct. */
8790 array_p = true;
8792 else
8793 array_p = false;
8795 /* Parse the cast-expression. */
8796 expression = cp_parser_simple_cast_expression (parser);
8798 /* A delete-expression may not appear in an integral constant
8799 expression. */
8800 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8801 return error_mark_node;
8803 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8804 tf_warning_or_error);
8807 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8808 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8809 0 otherwise. */
8811 static int
8812 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8814 cp_token *token = cp_lexer_peek_token (parser->lexer);
8815 switch (token->type)
8817 case CPP_COMMA:
8818 case CPP_SEMICOLON:
8819 case CPP_QUERY:
8820 case CPP_COLON:
8821 case CPP_CLOSE_SQUARE:
8822 case CPP_CLOSE_PAREN:
8823 case CPP_CLOSE_BRACE:
8824 case CPP_OPEN_BRACE:
8825 case CPP_DOT:
8826 case CPP_DOT_STAR:
8827 case CPP_DEREF:
8828 case CPP_DEREF_STAR:
8829 case CPP_DIV:
8830 case CPP_MOD:
8831 case CPP_LSHIFT:
8832 case CPP_RSHIFT:
8833 case CPP_LESS:
8834 case CPP_GREATER:
8835 case CPP_LESS_EQ:
8836 case CPP_GREATER_EQ:
8837 case CPP_EQ_EQ:
8838 case CPP_NOT_EQ:
8839 case CPP_EQ:
8840 case CPP_MULT_EQ:
8841 case CPP_DIV_EQ:
8842 case CPP_MOD_EQ:
8843 case CPP_PLUS_EQ:
8844 case CPP_MINUS_EQ:
8845 case CPP_RSHIFT_EQ:
8846 case CPP_LSHIFT_EQ:
8847 case CPP_AND_EQ:
8848 case CPP_XOR_EQ:
8849 case CPP_OR_EQ:
8850 case CPP_XOR:
8851 case CPP_OR:
8852 case CPP_OR_OR:
8853 case CPP_EOF:
8854 case CPP_ELLIPSIS:
8855 return 0;
8857 case CPP_OPEN_PAREN:
8858 /* In ((type ()) () the last () isn't a valid cast-expression,
8859 so the whole must be parsed as postfix-expression. */
8860 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8861 != CPP_CLOSE_PAREN;
8863 case CPP_OPEN_SQUARE:
8864 /* '[' may start a primary-expression in obj-c++ and in C++11,
8865 as a lambda-expression, eg, '(void)[]{}'. */
8866 if (cxx_dialect >= cxx11)
8867 return -1;
8868 return c_dialect_objc ();
8870 case CPP_PLUS_PLUS:
8871 case CPP_MINUS_MINUS:
8872 /* '++' and '--' may or may not start a cast-expression:
8874 struct T { void operator++(int); };
8875 void f() { (T())++; }
8879 int a;
8880 (int)++a; */
8881 return -1;
8883 default:
8884 return 1;
8888 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8889 in the order: const_cast, static_cast, reinterpret_cast.
8891 Don't suggest dynamic_cast.
8893 Return the first legal cast kind found, or NULL otherwise. */
8895 static const char *
8896 get_cast_suggestion (tree dst_type, tree orig_expr)
8898 tree trial;
8900 /* Reuse the parser logic by attempting to build the various kinds of
8901 cast, with "complain" disabled.
8902 Identify the first such cast that is valid. */
8904 /* Don't attempt to run such logic within template processing. */
8905 if (processing_template_decl)
8906 return NULL;
8908 /* First try const_cast. */
8909 trial = build_const_cast (dst_type, orig_expr, tf_none);
8910 if (trial != error_mark_node)
8911 return "const_cast";
8913 /* If that fails, try static_cast. */
8914 trial = build_static_cast (dst_type, orig_expr, tf_none);
8915 if (trial != error_mark_node)
8916 return "static_cast";
8918 /* Finally, try reinterpret_cast. */
8919 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8920 if (trial != error_mark_node)
8921 return "reinterpret_cast";
8923 /* No such cast possible. */
8924 return NULL;
8927 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8928 suggesting how to convert a C-style cast of the form:
8930 (DST_TYPE)ORIG_EXPR
8932 to a C++-style cast.
8934 The primary range of RICHLOC is asssumed to be that of the original
8935 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8936 of the parens in the C-style cast. */
8938 static void
8939 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8940 location_t close_paren_loc, tree orig_expr,
8941 tree dst_type)
8943 /* This function is non-trivial, so bail out now if the warning isn't
8944 going to be emitted. */
8945 if (!warn_old_style_cast)
8946 return;
8948 /* Try to find a legal C++ cast, trying them in order:
8949 const_cast, static_cast, reinterpret_cast. */
8950 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8951 if (!cast_suggestion)
8952 return;
8954 /* Replace the open paren with "CAST_SUGGESTION<". */
8955 pretty_printer pp;
8956 pp_printf (&pp, "%s<", cast_suggestion);
8957 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8959 /* Replace the close paren with "> (". */
8960 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8962 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8963 rich_loc->add_fixit_insert_after (")");
8967 /* Parse a cast-expression.
8969 cast-expression:
8970 unary-expression
8971 ( type-id ) cast-expression
8973 ADDRESS_P is true iff the unary-expression is appearing as the
8974 operand of the `&' operator. CAST_P is true if this expression is
8975 the target of a cast.
8977 Returns a representation of the expression. */
8979 static cp_expr
8980 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8981 bool decltype_p, cp_id_kind * pidk)
8983 /* If it's a `(', then we might be looking at a cast. */
8984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8986 tree type = NULL_TREE;
8987 cp_expr expr (NULL_TREE);
8988 int cast_expression = 0;
8989 const char *saved_message;
8991 /* There's no way to know yet whether or not this is a cast.
8992 For example, `(int (3))' is a unary-expression, while `(int)
8993 3' is a cast. So, we resort to parsing tentatively. */
8994 cp_parser_parse_tentatively (parser);
8995 /* Types may not be defined in a cast. */
8996 saved_message = parser->type_definition_forbidden_message;
8997 parser->type_definition_forbidden_message
8998 = G_("types may not be defined in casts");
8999 /* Consume the `('. */
9000 matching_parens parens;
9001 cp_token *open_paren = parens.consume_open (parser);
9002 location_t open_paren_loc = open_paren->location;
9003 location_t close_paren_loc = UNKNOWN_LOCATION;
9005 /* A very tricky bit is that `(struct S) { 3 }' is a
9006 compound-literal (which we permit in C++ as an extension).
9007 But, that construct is not a cast-expression -- it is a
9008 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9009 is legal; if the compound-literal were a cast-expression,
9010 you'd need an extra set of parentheses.) But, if we parse
9011 the type-id, and it happens to be a class-specifier, then we
9012 will commit to the parse at that point, because we cannot
9013 undo the action that is done when creating a new class. So,
9014 then we cannot back up and do a postfix-expression.
9016 Another tricky case is the following (c++/29234):
9018 struct S { void operator () (); };
9020 void foo ()
9022 ( S()() );
9025 As a type-id we parse the parenthesized S()() as a function
9026 returning a function, groktypename complains and we cannot
9027 back up in this case either.
9029 Therefore, we scan ahead to the closing `)', and check to see
9030 if the tokens after the `)' can start a cast-expression. Otherwise
9031 we are dealing with an unary-expression, a postfix-expression
9032 or something else.
9034 Yet another tricky case, in C++11, is the following (c++/54891):
9036 (void)[]{};
9038 The issue is that usually, besides the case of lambda-expressions,
9039 the parenthesized type-id cannot be followed by '[', and, eg, we
9040 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9041 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9042 we don't commit, we try a cast-expression, then an unary-expression.
9044 Save tokens so that we can put them back. */
9045 cp_lexer_save_tokens (parser->lexer);
9047 /* We may be looking at a cast-expression. */
9048 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9049 /*consume_paren=*/true))
9050 cast_expression
9051 = cp_parser_tokens_start_cast_expression (parser);
9053 /* Roll back the tokens we skipped. */
9054 cp_lexer_rollback_tokens (parser->lexer);
9055 /* If we aren't looking at a cast-expression, simulate an error so
9056 that the call to cp_parser_error_occurred below returns true. */
9057 if (!cast_expression)
9058 cp_parser_simulate_error (parser);
9059 else
9061 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9062 parser->in_type_id_in_expr_p = true;
9063 /* Look for the type-id. */
9064 type = cp_parser_type_id (parser);
9065 /* Look for the closing `)'. */
9066 cp_token *close_paren = parens.require_close (parser);
9067 if (close_paren)
9068 close_paren_loc = close_paren->location;
9069 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9072 /* Restore the saved message. */
9073 parser->type_definition_forbidden_message = saved_message;
9075 /* At this point this can only be either a cast or a
9076 parenthesized ctor such as `(T ())' that looks like a cast to
9077 function returning T. */
9078 if (!cp_parser_error_occurred (parser))
9080 /* Only commit if the cast-expression doesn't start with
9081 '++', '--', or '[' in C++11. */
9082 if (cast_expression > 0)
9083 cp_parser_commit_to_topmost_tentative_parse (parser);
9085 expr = cp_parser_cast_expression (parser,
9086 /*address_p=*/false,
9087 /*cast_p=*/true,
9088 /*decltype_p=*/false,
9089 pidk);
9091 if (cp_parser_parse_definitely (parser))
9093 /* Warn about old-style casts, if so requested. */
9094 if (warn_old_style_cast
9095 && !in_system_header_at (input_location)
9096 && !VOID_TYPE_P (type)
9097 && current_lang_name != lang_name_c)
9099 gcc_rich_location rich_loc (input_location);
9100 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9101 expr, type);
9102 warning_at (&rich_loc, OPT_Wold_style_cast,
9103 "use of old-style cast to %q#T", type);
9106 /* Only type conversions to integral or enumeration types
9107 can be used in constant-expressions. */
9108 if (!cast_valid_in_integral_constant_expression_p (type)
9109 && cp_parser_non_integral_constant_expression (parser,
9110 NIC_CAST))
9111 return error_mark_node;
9113 /* Perform the cast. */
9114 /* Make a location:
9115 (TYPE) EXPR
9116 ^~~~~~~~~~~
9117 with start==caret at the open paren, extending to the
9118 end of "expr". */
9119 location_t cast_loc = make_location (open_paren_loc,
9120 open_paren_loc,
9121 expr.get_finish ());
9122 expr = build_c_cast (cast_loc, type, expr);
9123 return expr;
9126 else
9127 cp_parser_abort_tentative_parse (parser);
9130 /* If we get here, then it's not a cast, so it must be a
9131 unary-expression. */
9132 return cp_parser_unary_expression (parser, pidk, address_p,
9133 cast_p, decltype_p);
9136 /* Parse a binary expression of the general form:
9138 pm-expression:
9139 cast-expression
9140 pm-expression .* cast-expression
9141 pm-expression ->* cast-expression
9143 multiplicative-expression:
9144 pm-expression
9145 multiplicative-expression * pm-expression
9146 multiplicative-expression / pm-expression
9147 multiplicative-expression % pm-expression
9149 additive-expression:
9150 multiplicative-expression
9151 additive-expression + multiplicative-expression
9152 additive-expression - multiplicative-expression
9154 shift-expression:
9155 additive-expression
9156 shift-expression << additive-expression
9157 shift-expression >> additive-expression
9159 relational-expression:
9160 shift-expression
9161 relational-expression < shift-expression
9162 relational-expression > shift-expression
9163 relational-expression <= shift-expression
9164 relational-expression >= shift-expression
9166 GNU Extension:
9168 relational-expression:
9169 relational-expression <? shift-expression
9170 relational-expression >? shift-expression
9172 equality-expression:
9173 relational-expression
9174 equality-expression == relational-expression
9175 equality-expression != relational-expression
9177 and-expression:
9178 equality-expression
9179 and-expression & equality-expression
9181 exclusive-or-expression:
9182 and-expression
9183 exclusive-or-expression ^ and-expression
9185 inclusive-or-expression:
9186 exclusive-or-expression
9187 inclusive-or-expression | exclusive-or-expression
9189 logical-and-expression:
9190 inclusive-or-expression
9191 logical-and-expression && inclusive-or-expression
9193 logical-or-expression:
9194 logical-and-expression
9195 logical-or-expression || logical-and-expression
9197 All these are implemented with a single function like:
9199 binary-expression:
9200 simple-cast-expression
9201 binary-expression <token> binary-expression
9203 CAST_P is true if this expression is the target of a cast.
9205 The binops_by_token map is used to get the tree codes for each <token> type.
9206 binary-expressions are associated according to a precedence table. */
9208 #define TOKEN_PRECEDENCE(token) \
9209 (((token->type == CPP_GREATER \
9210 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9211 && !parser->greater_than_is_operator_p) \
9212 ? PREC_NOT_OPERATOR \
9213 : binops_by_token[token->type].prec)
9215 static cp_expr
9216 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9217 bool no_toplevel_fold_p,
9218 bool decltype_p,
9219 enum cp_parser_prec prec,
9220 cp_id_kind * pidk)
9222 cp_parser_expression_stack stack;
9223 cp_parser_expression_stack_entry *sp = &stack[0];
9224 cp_parser_expression_stack_entry current;
9225 cp_expr rhs;
9226 cp_token *token;
9227 enum tree_code rhs_type;
9228 enum cp_parser_prec new_prec, lookahead_prec;
9229 tree overload;
9231 /* Parse the first expression. */
9232 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9233 ? TRUTH_NOT_EXPR : ERROR_MARK);
9234 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9235 cast_p, decltype_p, pidk);
9236 current.prec = prec;
9238 if (cp_parser_error_occurred (parser))
9239 return error_mark_node;
9241 for (;;)
9243 /* Get an operator token. */
9244 token = cp_lexer_peek_token (parser->lexer);
9246 if (warn_cxx11_compat
9247 && token->type == CPP_RSHIFT
9248 && !parser->greater_than_is_operator_p)
9250 if (warning_at (token->location, OPT_Wc__11_compat,
9251 "%<>>%> operator is treated"
9252 " as two right angle brackets in C++11"))
9253 inform (token->location,
9254 "suggest parentheses around %<>>%> expression");
9257 new_prec = TOKEN_PRECEDENCE (token);
9258 if (new_prec != PREC_NOT_OPERATOR
9259 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9260 /* This is a fold-expression; handle it later. */
9261 new_prec = PREC_NOT_OPERATOR;
9263 /* Popping an entry off the stack means we completed a subexpression:
9264 - either we found a token which is not an operator (`>' where it is not
9265 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9266 will happen repeatedly;
9267 - or, we found an operator which has lower priority. This is the case
9268 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9269 parsing `3 * 4'. */
9270 if (new_prec <= current.prec)
9272 if (sp == stack)
9273 break;
9274 else
9275 goto pop;
9278 get_rhs:
9279 current.tree_type = binops_by_token[token->type].tree_type;
9280 current.loc = token->location;
9282 /* We used the operator token. */
9283 cp_lexer_consume_token (parser->lexer);
9285 /* For "false && x" or "true || x", x will never be executed;
9286 disable warnings while evaluating it. */
9287 if (current.tree_type == TRUTH_ANDIF_EXPR)
9288 c_inhibit_evaluation_warnings +=
9289 cp_fully_fold (current.lhs) == truthvalue_false_node;
9290 else if (current.tree_type == TRUTH_ORIF_EXPR)
9291 c_inhibit_evaluation_warnings +=
9292 cp_fully_fold (current.lhs) == truthvalue_true_node;
9294 /* Extract another operand. It may be the RHS of this expression
9295 or the LHS of a new, higher priority expression. */
9296 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9297 ? TRUTH_NOT_EXPR : ERROR_MARK);
9298 rhs = cp_parser_simple_cast_expression (parser);
9300 /* Get another operator token. Look up its precedence to avoid
9301 building a useless (immediately popped) stack entry for common
9302 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9303 token = cp_lexer_peek_token (parser->lexer);
9304 lookahead_prec = TOKEN_PRECEDENCE (token);
9305 if (lookahead_prec != PREC_NOT_OPERATOR
9306 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9307 lookahead_prec = PREC_NOT_OPERATOR;
9308 if (lookahead_prec > new_prec)
9310 /* ... and prepare to parse the RHS of the new, higher priority
9311 expression. Since precedence levels on the stack are
9312 monotonically increasing, we do not have to care about
9313 stack overflows. */
9314 *sp = current;
9315 ++sp;
9316 current.lhs = rhs;
9317 current.lhs_type = rhs_type;
9318 current.prec = new_prec;
9319 new_prec = lookahead_prec;
9320 goto get_rhs;
9322 pop:
9323 lookahead_prec = new_prec;
9324 /* If the stack is not empty, we have parsed into LHS the right side
9325 (`4' in the example above) of an expression we had suspended.
9326 We can use the information on the stack to recover the LHS (`3')
9327 from the stack together with the tree code (`MULT_EXPR'), and
9328 the precedence of the higher level subexpression
9329 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9330 which will be used to actually build the additive expression. */
9331 rhs = current.lhs;
9332 rhs_type = current.lhs_type;
9333 --sp;
9334 current = *sp;
9337 /* Undo the disabling of warnings done above. */
9338 if (current.tree_type == TRUTH_ANDIF_EXPR)
9339 c_inhibit_evaluation_warnings -=
9340 cp_fully_fold (current.lhs) == truthvalue_false_node;
9341 else if (current.tree_type == TRUTH_ORIF_EXPR)
9342 c_inhibit_evaluation_warnings -=
9343 cp_fully_fold (current.lhs) == truthvalue_true_node;
9345 if (warn_logical_not_paren
9346 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9347 && current.lhs_type == TRUTH_NOT_EXPR
9348 /* Avoid warning for !!x == y. */
9349 && (TREE_CODE (current.lhs) != NE_EXPR
9350 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9351 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9352 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9353 /* Avoid warning for !b == y where b is boolean. */
9354 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9355 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9356 != BOOLEAN_TYPE))))
9357 /* Avoid warning for !!b == y where b is boolean. */
9358 && (!DECL_P (current.lhs)
9359 || TREE_TYPE (current.lhs) == NULL_TREE
9360 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9361 warn_logical_not_parentheses (current.loc, current.tree_type,
9362 current.lhs, maybe_constant_value (rhs));
9364 overload = NULL;
9366 location_t combined_loc = make_location (current.loc,
9367 current.lhs.get_start (),
9368 rhs.get_finish ());
9370 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9371 ERROR_MARK for everything that is not a binary expression.
9372 This makes warn_about_parentheses miss some warnings that
9373 involve unary operators. For unary expressions we should
9374 pass the correct tree_code unless the unary expression was
9375 surrounded by parentheses.
9377 if (no_toplevel_fold_p
9378 && lookahead_prec <= current.prec
9379 && sp == stack)
9380 current.lhs = build2_loc (combined_loc,
9381 current.tree_type,
9382 TREE_CODE_CLASS (current.tree_type)
9383 == tcc_comparison
9384 ? boolean_type_node : TREE_TYPE (current.lhs),
9385 current.lhs, rhs);
9386 else
9388 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9389 current.lhs, current.lhs_type,
9390 rhs, rhs_type, &overload,
9391 complain_flags (decltype_p));
9392 /* TODO: build_x_binary_op doesn't always honor the location. */
9393 current.lhs.set_location (combined_loc);
9395 current.lhs_type = current.tree_type;
9397 /* If the binary operator required the use of an overloaded operator,
9398 then this expression cannot be an integral constant-expression.
9399 An overloaded operator can be used even if both operands are
9400 otherwise permissible in an integral constant-expression if at
9401 least one of the operands is of enumeration type. */
9403 if (overload
9404 && cp_parser_non_integral_constant_expression (parser,
9405 NIC_OVERLOADED))
9406 return error_mark_node;
9409 return current.lhs;
9412 static cp_expr
9413 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9414 bool no_toplevel_fold_p,
9415 enum cp_parser_prec prec,
9416 cp_id_kind * pidk)
9418 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9419 /*decltype*/false, prec, pidk);
9422 /* Parse the `? expression : assignment-expression' part of a
9423 conditional-expression. The LOGICAL_OR_EXPR is the
9424 logical-or-expression that started the conditional-expression.
9425 Returns a representation of the entire conditional-expression.
9427 This routine is used by cp_parser_assignment_expression.
9429 ? expression : assignment-expression
9431 GNU Extensions:
9433 ? : assignment-expression */
9435 static tree
9436 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9438 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9439 cp_expr assignment_expr;
9440 struct cp_token *token;
9441 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9443 /* Consume the `?' token. */
9444 cp_lexer_consume_token (parser->lexer);
9445 token = cp_lexer_peek_token (parser->lexer);
9446 if (cp_parser_allow_gnu_extensions_p (parser)
9447 && token->type == CPP_COLON)
9449 pedwarn (token->location, OPT_Wpedantic,
9450 "ISO C++ does not allow ?: with omitted middle operand");
9451 /* Implicit true clause. */
9452 expr = NULL_TREE;
9453 c_inhibit_evaluation_warnings +=
9454 folded_logical_or_expr == truthvalue_true_node;
9455 warn_for_omitted_condop (token->location, logical_or_expr);
9457 else
9459 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9460 parser->colon_corrects_to_scope_p = false;
9461 /* Parse the expression. */
9462 c_inhibit_evaluation_warnings +=
9463 folded_logical_or_expr == truthvalue_false_node;
9464 expr = cp_parser_expression (parser);
9465 c_inhibit_evaluation_warnings +=
9466 ((folded_logical_or_expr == truthvalue_true_node)
9467 - (folded_logical_or_expr == truthvalue_false_node));
9468 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9471 /* The next token should be a `:'. */
9472 cp_parser_require (parser, CPP_COLON, RT_COLON);
9473 /* Parse the assignment-expression. */
9474 assignment_expr = cp_parser_assignment_expression (parser);
9475 c_inhibit_evaluation_warnings -=
9476 folded_logical_or_expr == truthvalue_true_node;
9478 /* Make a location:
9479 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9480 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9481 with the caret at the "?", ranging from the start of
9482 the logical_or_expr to the end of the assignment_expr. */
9483 loc = make_location (loc,
9484 logical_or_expr.get_start (),
9485 assignment_expr.get_finish ());
9487 /* Build the conditional-expression. */
9488 return build_x_conditional_expr (loc, logical_or_expr,
9489 expr,
9490 assignment_expr,
9491 tf_warning_or_error);
9494 /* Parse an assignment-expression.
9496 assignment-expression:
9497 conditional-expression
9498 logical-or-expression assignment-operator assignment_expression
9499 throw-expression
9501 CAST_P is true if this expression is the target of a cast.
9502 DECLTYPE_P is true if this expression is the operand of decltype.
9504 Returns a representation for the expression. */
9506 static cp_expr
9507 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9508 bool cast_p, bool decltype_p)
9510 cp_expr expr;
9512 /* If the next token is the `throw' keyword, then we're looking at
9513 a throw-expression. */
9514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9515 expr = cp_parser_throw_expression (parser);
9516 /* Otherwise, it must be that we are looking at a
9517 logical-or-expression. */
9518 else
9520 /* Parse the binary expressions (logical-or-expression). */
9521 expr = cp_parser_binary_expression (parser, cast_p, false,
9522 decltype_p,
9523 PREC_NOT_OPERATOR, pidk);
9524 /* If the next token is a `?' then we're actually looking at a
9525 conditional-expression. */
9526 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9527 return cp_parser_question_colon_clause (parser, expr);
9528 else
9530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9532 /* If it's an assignment-operator, we're using the second
9533 production. */
9534 enum tree_code assignment_operator
9535 = cp_parser_assignment_operator_opt (parser);
9536 if (assignment_operator != ERROR_MARK)
9538 bool non_constant_p;
9540 /* Parse the right-hand side of the assignment. */
9541 cp_expr rhs = cp_parser_initializer_clause (parser,
9542 &non_constant_p);
9544 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9547 /* An assignment may not appear in a
9548 constant-expression. */
9549 if (cp_parser_non_integral_constant_expression (parser,
9550 NIC_ASSIGNMENT))
9551 return error_mark_node;
9552 /* Build the assignment expression. Its default
9553 location:
9554 LHS = RHS
9555 ~~~~^~~~~
9556 is the location of the '=' token as the
9557 caret, ranging from the start of the lhs to the
9558 end of the rhs. */
9559 loc = make_location (loc,
9560 expr.get_start (),
9561 rhs.get_finish ());
9562 expr = build_x_modify_expr (loc, expr,
9563 assignment_operator,
9564 rhs,
9565 complain_flags (decltype_p));
9566 /* TODO: build_x_modify_expr doesn't honor the location,
9567 so we must set it here. */
9568 expr.set_location (loc);
9573 return expr;
9576 /* Parse an (optional) assignment-operator.
9578 assignment-operator: one of
9579 = *= /= %= += -= >>= <<= &= ^= |=
9581 GNU Extension:
9583 assignment-operator: one of
9584 <?= >?=
9586 If the next token is an assignment operator, the corresponding tree
9587 code is returned, and the token is consumed. For example, for
9588 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9589 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9590 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9591 operator, ERROR_MARK is returned. */
9593 static enum tree_code
9594 cp_parser_assignment_operator_opt (cp_parser* parser)
9596 enum tree_code op;
9597 cp_token *token;
9599 /* Peek at the next token. */
9600 token = cp_lexer_peek_token (parser->lexer);
9602 switch (token->type)
9604 case CPP_EQ:
9605 op = NOP_EXPR;
9606 break;
9608 case CPP_MULT_EQ:
9609 op = MULT_EXPR;
9610 break;
9612 case CPP_DIV_EQ:
9613 op = TRUNC_DIV_EXPR;
9614 break;
9616 case CPP_MOD_EQ:
9617 op = TRUNC_MOD_EXPR;
9618 break;
9620 case CPP_PLUS_EQ:
9621 op = PLUS_EXPR;
9622 break;
9624 case CPP_MINUS_EQ:
9625 op = MINUS_EXPR;
9626 break;
9628 case CPP_RSHIFT_EQ:
9629 op = RSHIFT_EXPR;
9630 break;
9632 case CPP_LSHIFT_EQ:
9633 op = LSHIFT_EXPR;
9634 break;
9636 case CPP_AND_EQ:
9637 op = BIT_AND_EXPR;
9638 break;
9640 case CPP_XOR_EQ:
9641 op = BIT_XOR_EXPR;
9642 break;
9644 case CPP_OR_EQ:
9645 op = BIT_IOR_EXPR;
9646 break;
9648 default:
9649 /* Nothing else is an assignment operator. */
9650 op = ERROR_MARK;
9653 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9654 if (op != ERROR_MARK
9655 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9656 op = ERROR_MARK;
9658 /* If it was an assignment operator, consume it. */
9659 if (op != ERROR_MARK)
9660 cp_lexer_consume_token (parser->lexer);
9662 return op;
9665 /* Parse an expression.
9667 expression:
9668 assignment-expression
9669 expression , assignment-expression
9671 CAST_P is true if this expression is the target of a cast.
9672 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9673 except possibly parenthesized or on the RHS of a comma (N3276).
9675 Returns a representation of the expression. */
9677 static cp_expr
9678 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9679 bool cast_p, bool decltype_p)
9681 cp_expr expression = NULL_TREE;
9682 location_t loc = UNKNOWN_LOCATION;
9684 while (true)
9686 cp_expr assignment_expression;
9688 /* Parse the next assignment-expression. */
9689 assignment_expression
9690 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9692 /* We don't create a temporary for a call that is the immediate operand
9693 of decltype or on the RHS of a comma. But when we see a comma, we
9694 need to create a temporary for a call on the LHS. */
9695 if (decltype_p && !processing_template_decl
9696 && TREE_CODE (assignment_expression) == CALL_EXPR
9697 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9698 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9699 assignment_expression
9700 = build_cplus_new (TREE_TYPE (assignment_expression),
9701 assignment_expression, tf_warning_or_error);
9703 /* If this is the first assignment-expression, we can just
9704 save it away. */
9705 if (!expression)
9706 expression = assignment_expression;
9707 else
9709 /* Create a location with caret at the comma, ranging
9710 from the start of the LHS to the end of the RHS. */
9711 loc = make_location (loc,
9712 expression.get_start (),
9713 assignment_expression.get_finish ());
9714 expression = build_x_compound_expr (loc, expression,
9715 assignment_expression,
9716 complain_flags (decltype_p));
9717 expression.set_location (loc);
9719 /* If the next token is not a comma, or we're in a fold-expression, then
9720 we are done with the expression. */
9721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9722 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9723 break;
9724 /* Consume the `,'. */
9725 loc = cp_lexer_peek_token (parser->lexer)->location;
9726 cp_lexer_consume_token (parser->lexer);
9727 /* A comma operator cannot appear in a constant-expression. */
9728 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9729 expression = error_mark_node;
9732 return expression;
9735 /* Parse a constant-expression.
9737 constant-expression:
9738 conditional-expression
9740 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9741 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9742 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9743 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9744 only parse a conditional-expression, otherwise parse an
9745 assignment-expression. See below for rationale. */
9747 static cp_expr
9748 cp_parser_constant_expression (cp_parser* parser,
9749 bool allow_non_constant_p,
9750 bool *non_constant_p,
9751 bool strict_p)
9753 bool saved_integral_constant_expression_p;
9754 bool saved_allow_non_integral_constant_expression_p;
9755 bool saved_non_integral_constant_expression_p;
9756 cp_expr expression;
9758 /* It might seem that we could simply parse the
9759 conditional-expression, and then check to see if it were
9760 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9761 one that the compiler can figure out is constant, possibly after
9762 doing some simplifications or optimizations. The standard has a
9763 precise definition of constant-expression, and we must honor
9764 that, even though it is somewhat more restrictive.
9766 For example:
9768 int i[(2, 3)];
9770 is not a legal declaration, because `(2, 3)' is not a
9771 constant-expression. The `,' operator is forbidden in a
9772 constant-expression. However, GCC's constant-folding machinery
9773 will fold this operation to an INTEGER_CST for `3'. */
9775 /* Save the old settings. */
9776 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9777 saved_allow_non_integral_constant_expression_p
9778 = parser->allow_non_integral_constant_expression_p;
9779 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9780 /* We are now parsing a constant-expression. */
9781 parser->integral_constant_expression_p = true;
9782 parser->allow_non_integral_constant_expression_p
9783 = (allow_non_constant_p || cxx_dialect >= cxx11);
9784 parser->non_integral_constant_expression_p = false;
9785 /* Although the grammar says "conditional-expression", when not STRICT_P,
9786 we parse an "assignment-expression", which also permits
9787 "throw-expression" and the use of assignment operators. In the case
9788 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9789 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9790 actually essential that we look for an assignment-expression.
9791 For example, cp_parser_initializer_clauses uses this function to
9792 determine whether a particular assignment-expression is in fact
9793 constant. */
9794 if (strict_p)
9796 /* Parse the binary expressions (logical-or-expression). */
9797 expression = cp_parser_binary_expression (parser, false, false, false,
9798 PREC_NOT_OPERATOR, NULL);
9799 /* If the next token is a `?' then we're actually looking at
9800 a conditional-expression; otherwise we're done. */
9801 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9802 expression = cp_parser_question_colon_clause (parser, expression);
9804 else
9805 expression = cp_parser_assignment_expression (parser);
9806 /* Restore the old settings. */
9807 parser->integral_constant_expression_p
9808 = saved_integral_constant_expression_p;
9809 parser->allow_non_integral_constant_expression_p
9810 = saved_allow_non_integral_constant_expression_p;
9811 if (cxx_dialect >= cxx11)
9813 /* Require an rvalue constant expression here; that's what our
9814 callers expect. Reference constant expressions are handled
9815 separately in e.g. cp_parser_template_argument. */
9816 tree decay = expression;
9817 if (TREE_TYPE (expression)
9818 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9819 decay = build_address (expression);
9820 bool is_const = potential_rvalue_constant_expression (decay);
9821 parser->non_integral_constant_expression_p = !is_const;
9822 if (!is_const && !allow_non_constant_p)
9823 require_potential_rvalue_constant_expression (decay);
9825 if (allow_non_constant_p)
9826 *non_constant_p = parser->non_integral_constant_expression_p;
9827 parser->non_integral_constant_expression_p
9828 = saved_non_integral_constant_expression_p;
9830 return expression;
9833 /* Parse __builtin_offsetof.
9835 offsetof-expression:
9836 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9838 offsetof-member-designator:
9839 id-expression
9840 | offsetof-member-designator "." id-expression
9841 | offsetof-member-designator "[" expression "]"
9842 | offsetof-member-designator "->" id-expression */
9844 static cp_expr
9845 cp_parser_builtin_offsetof (cp_parser *parser)
9847 int save_ice_p, save_non_ice_p;
9848 tree type;
9849 cp_expr expr;
9850 cp_id_kind dummy;
9851 cp_token *token;
9852 location_t finish_loc;
9854 /* We're about to accept non-integral-constant things, but will
9855 definitely yield an integral constant expression. Save and
9856 restore these values around our local parsing. */
9857 save_ice_p = parser->integral_constant_expression_p;
9858 save_non_ice_p = parser->non_integral_constant_expression_p;
9860 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9862 /* Consume the "__builtin_offsetof" token. */
9863 cp_lexer_consume_token (parser->lexer);
9864 /* Consume the opening `('. */
9865 matching_parens parens;
9866 parens.require_open (parser);
9867 /* Parse the type-id. */
9868 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9869 type = cp_parser_type_id (parser);
9870 /* Look for the `,'. */
9871 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9872 token = cp_lexer_peek_token (parser->lexer);
9874 /* Build the (type *)null that begins the traditional offsetof macro. */
9875 tree object_ptr
9876 = build_static_cast (build_pointer_type (type), null_pointer_node,
9877 tf_warning_or_error);
9879 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9880 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9881 true, &dummy, token->location);
9882 while (true)
9884 token = cp_lexer_peek_token (parser->lexer);
9885 switch (token->type)
9887 case CPP_OPEN_SQUARE:
9888 /* offsetof-member-designator "[" expression "]" */
9889 expr = cp_parser_postfix_open_square_expression (parser, expr,
9890 true, false);
9891 break;
9893 case CPP_DEREF:
9894 /* offsetof-member-designator "->" identifier */
9895 expr = grok_array_decl (token->location, expr,
9896 integer_zero_node, false);
9897 /* FALLTHRU */
9899 case CPP_DOT:
9900 /* offsetof-member-designator "." identifier */
9901 cp_lexer_consume_token (parser->lexer);
9902 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9903 expr, true, &dummy,
9904 token->location);
9905 break;
9907 case CPP_CLOSE_PAREN:
9908 /* Consume the ")" token. */
9909 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9910 cp_lexer_consume_token (parser->lexer);
9911 goto success;
9913 default:
9914 /* Error. We know the following require will fail, but
9915 that gives the proper error message. */
9916 parens.require_close (parser);
9917 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9918 expr = error_mark_node;
9919 goto failure;
9923 success:
9924 /* Make a location of the form:
9925 __builtin_offsetof (struct s, f)
9926 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9927 with caret at the type-id, ranging from the start of the
9928 "_builtin_offsetof" token to the close paren. */
9929 loc = make_location (loc, start_loc, finish_loc);
9930 /* The result will be an INTEGER_CST, so we need to explicitly
9931 preserve the location. */
9932 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9934 failure:
9935 parser->integral_constant_expression_p = save_ice_p;
9936 parser->non_integral_constant_expression_p = save_non_ice_p;
9938 return expr;
9941 /* Parse a trait expression.
9943 Returns a representation of the expression, the underlying type
9944 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9946 static tree
9947 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9949 cp_trait_kind kind;
9950 tree type1, type2 = NULL_TREE;
9951 bool binary = false;
9952 bool variadic = false;
9954 switch (keyword)
9956 case RID_HAS_NOTHROW_ASSIGN:
9957 kind = CPTK_HAS_NOTHROW_ASSIGN;
9958 break;
9959 case RID_HAS_NOTHROW_CONSTRUCTOR:
9960 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9961 break;
9962 case RID_HAS_NOTHROW_COPY:
9963 kind = CPTK_HAS_NOTHROW_COPY;
9964 break;
9965 case RID_HAS_TRIVIAL_ASSIGN:
9966 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9967 break;
9968 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9969 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9970 break;
9971 case RID_HAS_TRIVIAL_COPY:
9972 kind = CPTK_HAS_TRIVIAL_COPY;
9973 break;
9974 case RID_HAS_TRIVIAL_DESTRUCTOR:
9975 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9976 break;
9977 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9978 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9979 break;
9980 case RID_HAS_VIRTUAL_DESTRUCTOR:
9981 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9982 break;
9983 case RID_IS_ABSTRACT:
9984 kind = CPTK_IS_ABSTRACT;
9985 break;
9986 case RID_IS_AGGREGATE:
9987 kind = CPTK_IS_AGGREGATE;
9988 break;
9989 case RID_IS_BASE_OF:
9990 kind = CPTK_IS_BASE_OF;
9991 binary = true;
9992 break;
9993 case RID_IS_CLASS:
9994 kind = CPTK_IS_CLASS;
9995 break;
9996 case RID_IS_EMPTY:
9997 kind = CPTK_IS_EMPTY;
9998 break;
9999 case RID_IS_ENUM:
10000 kind = CPTK_IS_ENUM;
10001 break;
10002 case RID_IS_FINAL:
10003 kind = CPTK_IS_FINAL;
10004 break;
10005 case RID_IS_LITERAL_TYPE:
10006 kind = CPTK_IS_LITERAL_TYPE;
10007 break;
10008 case RID_IS_POD:
10009 kind = CPTK_IS_POD;
10010 break;
10011 case RID_IS_POLYMORPHIC:
10012 kind = CPTK_IS_POLYMORPHIC;
10013 break;
10014 case RID_IS_SAME_AS:
10015 kind = CPTK_IS_SAME_AS;
10016 binary = true;
10017 break;
10018 case RID_IS_STD_LAYOUT:
10019 kind = CPTK_IS_STD_LAYOUT;
10020 break;
10021 case RID_IS_TRIVIAL:
10022 kind = CPTK_IS_TRIVIAL;
10023 break;
10024 case RID_IS_TRIVIALLY_ASSIGNABLE:
10025 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10026 binary = true;
10027 break;
10028 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10029 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10030 variadic = true;
10031 break;
10032 case RID_IS_TRIVIALLY_COPYABLE:
10033 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10034 break;
10035 case RID_IS_UNION:
10036 kind = CPTK_IS_UNION;
10037 break;
10038 case RID_UNDERLYING_TYPE:
10039 kind = CPTK_UNDERLYING_TYPE;
10040 break;
10041 case RID_BASES:
10042 kind = CPTK_BASES;
10043 break;
10044 case RID_DIRECT_BASES:
10045 kind = CPTK_DIRECT_BASES;
10046 break;
10047 case RID_IS_ASSIGNABLE:
10048 kind = CPTK_IS_ASSIGNABLE;
10049 binary = true;
10050 break;
10051 case RID_IS_CONSTRUCTIBLE:
10052 kind = CPTK_IS_CONSTRUCTIBLE;
10053 variadic = true;
10054 break;
10055 default:
10056 gcc_unreachable ();
10059 /* Consume the token. */
10060 cp_lexer_consume_token (parser->lexer);
10062 matching_parens parens;
10063 parens.require_open (parser);
10066 type_id_in_expr_sentinel s (parser);
10067 type1 = cp_parser_type_id (parser);
10070 if (type1 == error_mark_node)
10071 return error_mark_node;
10073 if (binary)
10075 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10078 type_id_in_expr_sentinel s (parser);
10079 type2 = cp_parser_type_id (parser);
10082 if (type2 == error_mark_node)
10083 return error_mark_node;
10085 else if (variadic)
10087 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10089 cp_lexer_consume_token (parser->lexer);
10090 tree elt = cp_parser_type_id (parser);
10091 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10093 cp_lexer_consume_token (parser->lexer);
10094 elt = make_pack_expansion (elt);
10096 if (elt == error_mark_node)
10097 return error_mark_node;
10098 type2 = tree_cons (NULL_TREE, elt, type2);
10102 parens.require_close (parser);
10104 /* Complete the trait expression, which may mean either processing
10105 the trait expr now or saving it for template instantiation. */
10106 switch (kind)
10108 case CPTK_UNDERLYING_TYPE:
10109 return finish_underlying_type (type1);
10110 case CPTK_BASES:
10111 return finish_bases (type1, false);
10112 case CPTK_DIRECT_BASES:
10113 return finish_bases (type1, true);
10114 default:
10115 return finish_trait_expr (kind, type1, type2);
10119 /* Parse a lambda expression.
10121 lambda-expression:
10122 lambda-introducer lambda-declarator [opt] compound-statement
10124 Returns a representation of the expression. */
10126 static cp_expr
10127 cp_parser_lambda_expression (cp_parser* parser)
10129 tree lambda_expr = build_lambda_expr ();
10130 tree type;
10131 bool ok = true;
10132 cp_token *token = cp_lexer_peek_token (parser->lexer);
10133 cp_token_position start = 0;
10135 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10137 if (cp_unevaluated_operand)
10139 if (!token->error_reported)
10141 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10142 "lambda-expression in unevaluated context");
10143 token->error_reported = true;
10145 ok = false;
10147 else if (parser->in_template_argument_list_p)
10149 if (!token->error_reported)
10151 error_at (token->location, "lambda-expression in template-argument");
10152 token->error_reported = true;
10154 ok = false;
10157 /* We may be in the middle of deferred access check. Disable
10158 it now. */
10159 push_deferring_access_checks (dk_no_deferred);
10161 cp_parser_lambda_introducer (parser, lambda_expr);
10163 type = begin_lambda_type (lambda_expr);
10164 if (type == error_mark_node)
10165 return error_mark_node;
10167 record_lambda_scope (lambda_expr);
10169 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10170 determine_visibility (TYPE_NAME (type));
10172 /* Now that we've started the type, add the capture fields for any
10173 explicit captures. */
10174 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10177 /* Inside the class, surrounding template-parameter-lists do not apply. */
10178 unsigned int saved_num_template_parameter_lists
10179 = parser->num_template_parameter_lists;
10180 unsigned char in_statement = parser->in_statement;
10181 bool in_switch_statement_p = parser->in_switch_statement_p;
10182 bool fully_implicit_function_template_p
10183 = parser->fully_implicit_function_template_p;
10184 tree implicit_template_parms = parser->implicit_template_parms;
10185 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10186 bool auto_is_implicit_function_template_parm_p
10187 = parser->auto_is_implicit_function_template_parm_p;
10189 parser->num_template_parameter_lists = 0;
10190 parser->in_statement = 0;
10191 parser->in_switch_statement_p = false;
10192 parser->fully_implicit_function_template_p = false;
10193 parser->implicit_template_parms = 0;
10194 parser->implicit_template_scope = 0;
10195 parser->auto_is_implicit_function_template_parm_p = false;
10197 /* By virtue of defining a local class, a lambda expression has access to
10198 the private variables of enclosing classes. */
10200 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10202 if (ok && cp_parser_error_occurred (parser))
10203 ok = false;
10205 if (ok)
10207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10208 && cp_parser_start_tentative_firewall (parser))
10209 start = token;
10210 cp_parser_lambda_body (parser, lambda_expr);
10212 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10214 if (cp_parser_skip_to_closing_brace (parser))
10215 cp_lexer_consume_token (parser->lexer);
10218 /* The capture list was built up in reverse order; fix that now. */
10219 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10220 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10222 if (ok)
10223 maybe_add_lambda_conv_op (type);
10225 type = finish_struct (type, /*attributes=*/NULL_TREE);
10227 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10228 parser->in_statement = in_statement;
10229 parser->in_switch_statement_p = in_switch_statement_p;
10230 parser->fully_implicit_function_template_p
10231 = fully_implicit_function_template_p;
10232 parser->implicit_template_parms = implicit_template_parms;
10233 parser->implicit_template_scope = implicit_template_scope;
10234 parser->auto_is_implicit_function_template_parm_p
10235 = auto_is_implicit_function_template_parm_p;
10238 /* This field is only used during parsing of the lambda. */
10239 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10241 /* This lambda shouldn't have any proxies left at this point. */
10242 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10243 /* And now that we're done, push proxies for an enclosing lambda. */
10244 insert_pending_capture_proxies ();
10246 if (ok)
10247 lambda_expr = build_lambda_object (lambda_expr);
10248 else
10249 lambda_expr = error_mark_node;
10251 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10253 pop_deferring_access_checks ();
10255 return lambda_expr;
10258 /* Parse the beginning of a lambda expression.
10260 lambda-introducer:
10261 [ lambda-capture [opt] ]
10263 LAMBDA_EXPR is the current representation of the lambda expression. */
10265 static void
10266 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10268 /* Need commas after the first capture. */
10269 bool first = true;
10271 /* Eat the leading `['. */
10272 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10274 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10275 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10276 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10278 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10279 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10281 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10283 cp_lexer_consume_token (parser->lexer);
10284 first = false;
10287 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10289 cp_token* capture_token;
10290 tree capture_id;
10291 tree capture_init_expr;
10292 cp_id_kind idk = CP_ID_KIND_NONE;
10293 bool explicit_init_p = false;
10295 enum capture_kind_type
10297 BY_COPY,
10298 BY_REFERENCE
10300 enum capture_kind_type capture_kind = BY_COPY;
10302 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10304 error ("expected end of capture-list");
10305 return;
10308 if (first)
10309 first = false;
10310 else
10311 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10313 /* Possibly capture `this'. */
10314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10316 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10317 if (cxx_dialect < cxx2a
10318 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10319 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10320 "with by-copy capture default");
10321 cp_lexer_consume_token (parser->lexer);
10322 add_capture (lambda_expr,
10323 /*id=*/this_identifier,
10324 /*initializer=*/finish_this_expr (),
10325 /*by_reference_p=*/true,
10326 explicit_init_p);
10327 continue;
10330 /* Possibly capture `*this'. */
10331 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10332 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10334 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10335 if (cxx_dialect < cxx17)
10336 pedwarn (loc, 0, "%<*this%> capture only available with "
10337 "-std=c++17 or -std=gnu++17");
10338 cp_lexer_consume_token (parser->lexer);
10339 cp_lexer_consume_token (parser->lexer);
10340 add_capture (lambda_expr,
10341 /*id=*/this_identifier,
10342 /*initializer=*/finish_this_expr (),
10343 /*by_reference_p=*/false,
10344 explicit_init_p);
10345 continue;
10348 /* Remember whether we want to capture as a reference or not. */
10349 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10351 capture_kind = BY_REFERENCE;
10352 cp_lexer_consume_token (parser->lexer);
10355 /* Get the identifier. */
10356 capture_token = cp_lexer_peek_token (parser->lexer);
10357 capture_id = cp_parser_identifier (parser);
10359 if (capture_id == error_mark_node)
10360 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10361 delimiters, but I modified this to stop on unnested ']' as well. It
10362 was already changed to stop on unnested '}', so the
10363 "closing_parenthesis" name is no more misleading with my change. */
10365 cp_parser_skip_to_closing_parenthesis (parser,
10366 /*recovering=*/true,
10367 /*or_comma=*/true,
10368 /*consume_paren=*/true);
10369 break;
10372 /* Find the initializer for this capture. */
10373 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10374 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10375 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10377 bool direct, non_constant;
10378 /* An explicit initializer exists. */
10379 if (cxx_dialect < cxx14)
10380 pedwarn (input_location, 0,
10381 "lambda capture initializers "
10382 "only available with -std=c++14 or -std=gnu++14");
10383 capture_init_expr = cp_parser_initializer (parser, &direct,
10384 &non_constant);
10385 explicit_init_p = true;
10386 if (capture_init_expr == NULL_TREE)
10388 error ("empty initializer for lambda init-capture");
10389 capture_init_expr = error_mark_node;
10392 else
10394 const char* error_msg;
10396 /* Turn the identifier into an id-expression. */
10397 capture_init_expr
10398 = cp_parser_lookup_name_simple (parser, capture_id,
10399 capture_token->location);
10401 if (capture_init_expr == error_mark_node)
10403 unqualified_name_lookup_error (capture_id);
10404 continue;
10406 else if (DECL_P (capture_init_expr)
10407 && (!VAR_P (capture_init_expr)
10408 && TREE_CODE (capture_init_expr) != PARM_DECL))
10410 error_at (capture_token->location,
10411 "capture of non-variable %qD ",
10412 capture_init_expr);
10413 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10414 "%q#D declared here", capture_init_expr);
10415 continue;
10417 if (VAR_P (capture_init_expr)
10418 && decl_storage_duration (capture_init_expr) != dk_auto)
10420 if (pedwarn (capture_token->location, 0, "capture of variable "
10421 "%qD with non-automatic storage duration",
10422 capture_init_expr))
10423 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10424 "%q#D declared here", capture_init_expr);
10425 continue;
10428 capture_init_expr
10429 = finish_id_expression
10430 (capture_id,
10431 capture_init_expr,
10432 parser->scope,
10433 &idk,
10434 /*integral_constant_expression_p=*/false,
10435 /*allow_non_integral_constant_expression_p=*/false,
10436 /*non_integral_constant_expression_p=*/NULL,
10437 /*template_p=*/false,
10438 /*done=*/true,
10439 /*address_p=*/false,
10440 /*template_arg_p=*/false,
10441 &error_msg,
10442 capture_token->location);
10444 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10446 cp_lexer_consume_token (parser->lexer);
10447 capture_init_expr = make_pack_expansion (capture_init_expr);
10449 else
10450 check_for_bare_parameter_packs (capture_init_expr);
10453 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10454 && !explicit_init_p)
10456 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10457 && capture_kind == BY_COPY)
10458 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10459 "of %qD redundant with by-copy capture default",
10460 capture_id);
10461 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10462 && capture_kind == BY_REFERENCE)
10463 pedwarn (capture_token->location, 0, "explicit by-reference "
10464 "capture of %qD redundant with by-reference capture "
10465 "default", capture_id);
10468 add_capture (lambda_expr,
10469 capture_id,
10470 capture_init_expr,
10471 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10472 explicit_init_p);
10475 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10478 /* Parse the (optional) middle of a lambda expression.
10480 lambda-declarator:
10481 < template-parameter-list [opt] >
10482 ( parameter-declaration-clause [opt] )
10483 attribute-specifier [opt]
10484 decl-specifier-seq [opt]
10485 exception-specification [opt]
10486 lambda-return-type-clause [opt]
10488 LAMBDA_EXPR is the current representation of the lambda expression. */
10490 static bool
10491 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10493 /* 5.1.1.4 of the standard says:
10494 If a lambda-expression does not include a lambda-declarator, it is as if
10495 the lambda-declarator were ().
10496 This means an empty parameter list, no attributes, and no exception
10497 specification. */
10498 tree param_list = void_list_node;
10499 tree attributes = NULL_TREE;
10500 tree exception_spec = NULL_TREE;
10501 tree template_param_list = NULL_TREE;
10502 tree tx_qual = NULL_TREE;
10503 tree return_type = NULL_TREE;
10504 cp_decl_specifier_seq lambda_specs;
10505 clear_decl_specs (&lambda_specs);
10507 /* The template-parameter-list is optional, but must begin with
10508 an opening angle if present. */
10509 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10511 if (cxx_dialect < cxx14)
10512 pedwarn (parser->lexer->next_token->location, 0,
10513 "lambda templates are only available with "
10514 "-std=c++14 or -std=gnu++14");
10515 else if (cxx_dialect < cxx2a)
10516 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10517 "lambda templates are only available with "
10518 "-std=c++2a or -std=gnu++2a");
10520 cp_lexer_consume_token (parser->lexer);
10522 template_param_list = cp_parser_template_parameter_list (parser);
10524 cp_parser_skip_to_end_of_template_parameter_list (parser);
10526 /* We just processed one more parameter list. */
10527 ++parser->num_template_parameter_lists;
10530 /* The parameter-declaration-clause is optional (unless
10531 template-parameter-list was given), but must begin with an
10532 opening parenthesis if present. */
10533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10535 matching_parens parens;
10536 parens.consume_open (parser);
10538 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10540 /* Parse parameters. */
10541 param_list = cp_parser_parameter_declaration_clause (parser);
10543 /* Default arguments shall not be specified in the
10544 parameter-declaration-clause of a lambda-declarator. */
10545 if (cxx_dialect < cxx14)
10546 for (tree t = param_list; t; t = TREE_CHAIN (t))
10547 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10548 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10549 "default argument specified for lambda parameter");
10551 parens.require_close (parser);
10553 attributes = cp_parser_attributes_opt (parser);
10555 /* In the decl-specifier-seq of the lambda-declarator, each
10556 decl-specifier shall either be mutable or constexpr. */
10557 int declares_class_or_enum;
10558 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10559 cp_parser_decl_specifier_seq (parser,
10560 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10561 &lambda_specs, &declares_class_or_enum);
10562 if (lambda_specs.storage_class == sc_mutable)
10564 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10565 if (lambda_specs.conflicting_specifiers_p)
10566 error_at (lambda_specs.locations[ds_storage_class],
10567 "duplicate %<mutable%>");
10570 tx_qual = cp_parser_tx_qualifier_opt (parser);
10572 /* Parse optional exception specification. */
10573 exception_spec = cp_parser_exception_specification_opt (parser);
10575 /* Parse optional trailing return type. */
10576 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10578 cp_lexer_consume_token (parser->lexer);
10579 return_type = cp_parser_trailing_type_id (parser);
10582 /* The function parameters must be in scope all the way until after the
10583 trailing-return-type in case of decltype. */
10584 pop_bindings_and_leave_scope ();
10586 else if (template_param_list != NULL_TREE) // generate diagnostic
10587 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10589 /* Create the function call operator.
10591 Messing with declarators like this is no uglier than building up the
10592 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10593 other code. */
10595 cp_decl_specifier_seq return_type_specs;
10596 cp_declarator* declarator;
10597 tree fco;
10598 int quals;
10599 void *p;
10601 clear_decl_specs (&return_type_specs);
10602 if (return_type)
10603 return_type_specs.type = return_type;
10604 else
10605 /* Maybe we will deduce the return type later. */
10606 return_type_specs.type = make_auto ();
10608 if (lambda_specs.locations[ds_constexpr])
10610 if (cxx_dialect >= cxx17)
10611 return_type_specs.locations[ds_constexpr]
10612 = lambda_specs.locations[ds_constexpr];
10613 else
10614 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10615 "lambda only available with -std=c++17 or -std=gnu++17");
10618 p = obstack_alloc (&declarator_obstack, 0);
10620 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10622 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10623 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10624 declarator = make_call_declarator (declarator, param_list, quals,
10625 VIRT_SPEC_UNSPECIFIED,
10626 REF_QUAL_NONE,
10627 tx_qual,
10628 exception_spec,
10629 /*late_return_type=*/NULL_TREE,
10630 /*requires_clause*/NULL_TREE);
10631 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10633 fco = grokmethod (&return_type_specs,
10634 declarator,
10635 attributes);
10636 if (fco != error_mark_node)
10638 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10639 DECL_ARTIFICIAL (fco) = 1;
10640 /* Give the object parameter a different name. */
10641 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10642 if (return_type)
10643 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10645 if (template_param_list)
10647 fco = finish_member_template_decl (fco);
10648 finish_template_decl (template_param_list);
10649 --parser->num_template_parameter_lists;
10651 else if (parser->fully_implicit_function_template_p)
10652 fco = finish_fully_implicit_template (parser, fco);
10654 finish_member_declaration (fco);
10656 obstack_free (&declarator_obstack, p);
10658 return (fco != error_mark_node);
10662 /* Parse the body of a lambda expression, which is simply
10664 compound-statement
10666 but which requires special handling.
10667 LAMBDA_EXPR is the current representation of the lambda expression. */
10669 static void
10670 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10672 bool nested = (current_function_decl != NULL_TREE);
10673 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10674 bool in_function_body = parser->in_function_body;
10675 if (nested)
10676 push_function_context ();
10677 else
10678 /* Still increment function_depth so that we don't GC in the
10679 middle of an expression. */
10680 ++function_depth;
10681 vec<tree> omp_privatization_save;
10682 save_omp_privatization_clauses (omp_privatization_save);
10683 /* Clear this in case we're in the middle of a default argument. */
10684 parser->local_variables_forbidden_p = false;
10685 parser->in_function_body = true;
10687 /* Finish the function call operator
10688 - class_specifier
10689 + late_parsing_for_member
10690 + function_definition_after_declarator
10691 + ctor_initializer_opt_and_function_body */
10693 local_specialization_stack s (lss_copy);
10695 tree fco = lambda_function (lambda_expr);
10696 tree body = start_lambda_function (fco, lambda_expr);
10697 bool done = false;
10698 tree compound_stmt;
10700 matching_braces braces;
10701 if (!braces.require_open (parser))
10702 goto out;
10704 compound_stmt = begin_compound_stmt (0);
10706 /* 5.1.1.4 of the standard says:
10707 If a lambda-expression does not include a trailing-return-type, it
10708 is as if the trailing-return-type denotes the following type:
10709 * if the compound-statement is of the form
10710 { return attribute-specifier [opt] expression ; }
10711 the type of the returned expression after lvalue-to-rvalue
10712 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10713 (_conv.array_ 4.2), and function-to-pointer conversion
10714 (_conv.func_ 4.3);
10715 * otherwise, void. */
10717 /* In a lambda that has neither a lambda-return-type-clause
10718 nor a deducible form, errors should be reported for return statements
10719 in the body. Since we used void as the placeholder return type, parsing
10720 the body as usual will give such desired behavior. */
10721 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10722 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10723 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10725 tree expr = NULL_TREE;
10726 cp_id_kind idk = CP_ID_KIND_NONE;
10728 /* Parse tentatively in case there's more after the initial return
10729 statement. */
10730 cp_parser_parse_tentatively (parser);
10732 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10734 expr = cp_parser_expression (parser, &idk);
10736 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10737 braces.require_close (parser);
10739 if (cp_parser_parse_definitely (parser))
10741 if (!processing_template_decl)
10743 tree type = lambda_return_type (expr);
10744 apply_deduced_return_type (fco, type);
10745 if (type == error_mark_node)
10746 expr = error_mark_node;
10749 /* Will get error here if type not deduced yet. */
10750 finish_return_stmt (expr);
10752 done = true;
10756 if (!done)
10758 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10759 cp_parser_label_declaration (parser);
10760 cp_parser_statement_seq_opt (parser, NULL_TREE);
10761 braces.require_close (parser);
10764 finish_compound_stmt (compound_stmt);
10766 out:
10767 finish_lambda_function (body);
10770 restore_omp_privatization_clauses (omp_privatization_save);
10771 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10772 parser->in_function_body = in_function_body;
10773 if (nested)
10774 pop_function_context();
10775 else
10776 --function_depth;
10779 /* Statements [gram.stmt.stmt] */
10781 /* Parse a statement.
10783 statement:
10784 labeled-statement
10785 expression-statement
10786 compound-statement
10787 selection-statement
10788 iteration-statement
10789 jump-statement
10790 declaration-statement
10791 try-block
10793 C++11:
10795 statement:
10796 labeled-statement
10797 attribute-specifier-seq (opt) expression-statement
10798 attribute-specifier-seq (opt) compound-statement
10799 attribute-specifier-seq (opt) selection-statement
10800 attribute-specifier-seq (opt) iteration-statement
10801 attribute-specifier-seq (opt) jump-statement
10802 declaration-statement
10803 attribute-specifier-seq (opt) try-block
10805 init-statement:
10806 expression-statement
10807 simple-declaration
10809 TM Extension:
10811 statement:
10812 atomic-statement
10814 IN_COMPOUND is true when the statement is nested inside a
10815 cp_parser_compound_statement; this matters for certain pragmas.
10817 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10818 is a (possibly labeled) if statement which is not enclosed in braces
10819 and has an else clause. This is used to implement -Wparentheses.
10821 CHAIN is a vector of if-else-if conditions. */
10823 static void
10824 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10825 bool in_compound, bool *if_p, vec<tree> *chain,
10826 location_t *loc_after_labels)
10828 tree statement, std_attrs = NULL_TREE;
10829 cp_token *token;
10830 location_t statement_location, attrs_location;
10832 restart:
10833 if (if_p != NULL)
10834 *if_p = false;
10835 /* There is no statement yet. */
10836 statement = NULL_TREE;
10838 saved_token_sentinel saved_tokens (parser->lexer);
10839 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10840 if (c_dialect_objc ())
10841 /* In obj-c++, seeing '[[' might be the either the beginning of
10842 c++11 attributes, or a nested objc-message-expression. So
10843 let's parse the c++11 attributes tentatively. */
10844 cp_parser_parse_tentatively (parser);
10845 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10846 if (c_dialect_objc ())
10848 if (!cp_parser_parse_definitely (parser))
10849 std_attrs = NULL_TREE;
10852 /* Peek at the next token. */
10853 token = cp_lexer_peek_token (parser->lexer);
10854 /* Remember the location of the first token in the statement. */
10855 statement_location = token->location;
10856 /* If this is a keyword, then that will often determine what kind of
10857 statement we have. */
10858 if (token->type == CPP_KEYWORD)
10860 enum rid keyword = token->keyword;
10862 switch (keyword)
10864 case RID_CASE:
10865 case RID_DEFAULT:
10866 /* Looks like a labeled-statement with a case label.
10867 Parse the label, and then use tail recursion to parse
10868 the statement. */
10869 cp_parser_label_for_labeled_statement (parser, std_attrs);
10870 in_compound = false;
10871 goto restart;
10873 case RID_IF:
10874 case RID_SWITCH:
10875 statement = cp_parser_selection_statement (parser, if_p, chain);
10876 break;
10878 case RID_WHILE:
10879 case RID_DO:
10880 case RID_FOR:
10881 statement = cp_parser_iteration_statement (parser, if_p, false);
10882 break;
10884 case RID_CILK_FOR:
10885 if (!flag_cilkplus)
10887 error_at (cp_lexer_peek_token (parser->lexer)->location,
10888 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10889 cp_lexer_consume_token (parser->lexer);
10890 statement = error_mark_node;
10892 else
10893 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10894 break;
10896 case RID_BREAK:
10897 case RID_CONTINUE:
10898 case RID_RETURN:
10899 case RID_GOTO:
10900 statement = cp_parser_jump_statement (parser);
10901 break;
10903 case RID_CILK_SYNC:
10904 cp_lexer_consume_token (parser->lexer);
10905 if (flag_cilkplus)
10907 tree sync_expr = build_cilk_sync ();
10908 SET_EXPR_LOCATION (sync_expr,
10909 token->location);
10910 statement = finish_expr_stmt (sync_expr);
10912 else
10914 error_at (token->location, "-fcilkplus must be enabled to use"
10915 " %<_Cilk_sync%>");
10916 statement = error_mark_node;
10918 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10919 break;
10921 /* Objective-C++ exception-handling constructs. */
10922 case RID_AT_TRY:
10923 case RID_AT_CATCH:
10924 case RID_AT_FINALLY:
10925 case RID_AT_SYNCHRONIZED:
10926 case RID_AT_THROW:
10927 statement = cp_parser_objc_statement (parser);
10928 break;
10930 case RID_TRY:
10931 statement = cp_parser_try_block (parser);
10932 break;
10934 case RID_NAMESPACE:
10935 /* This must be a namespace alias definition. */
10936 cp_parser_declaration_statement (parser);
10937 return;
10939 case RID_TRANSACTION_ATOMIC:
10940 case RID_TRANSACTION_RELAXED:
10941 case RID_SYNCHRONIZED:
10942 case RID_ATOMIC_NOEXCEPT:
10943 case RID_ATOMIC_CANCEL:
10944 statement = cp_parser_transaction (parser, token);
10945 break;
10946 case RID_TRANSACTION_CANCEL:
10947 statement = cp_parser_transaction_cancel (parser);
10948 break;
10950 default:
10951 /* It might be a keyword like `int' that can start a
10952 declaration-statement. */
10953 break;
10956 else if (token->type == CPP_NAME)
10958 /* If the next token is a `:', then we are looking at a
10959 labeled-statement. */
10960 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10961 if (token->type == CPP_COLON)
10963 /* Looks like a labeled-statement with an ordinary label.
10964 Parse the label, and then use tail recursion to parse
10965 the statement. */
10967 cp_parser_label_for_labeled_statement (parser, std_attrs);
10968 in_compound = false;
10969 goto restart;
10972 /* Anything that starts with a `{' must be a compound-statement. */
10973 else if (token->type == CPP_OPEN_BRACE)
10974 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10975 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10976 a statement all its own. */
10977 else if (token->type == CPP_PRAGMA)
10979 /* Only certain OpenMP pragmas are attached to statements, and thus
10980 are considered statements themselves. All others are not. In
10981 the context of a compound, accept the pragma as a "statement" and
10982 return so that we can check for a close brace. Otherwise we
10983 require a real statement and must go back and read one. */
10984 if (in_compound)
10985 cp_parser_pragma (parser, pragma_compound, if_p);
10986 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10987 goto restart;
10988 return;
10990 else if (token->type == CPP_EOF)
10992 cp_parser_error (parser, "expected statement");
10993 return;
10996 /* Everything else must be a declaration-statement or an
10997 expression-statement. Try for the declaration-statement
10998 first, unless we are looking at a `;', in which case we know that
10999 we have an expression-statement. */
11000 if (!statement)
11002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11004 if (std_attrs != NULL_TREE)
11006 /* Attributes should be parsed as part of the the
11007 declaration, so let's un-parse them. */
11008 saved_tokens.rollback();
11009 std_attrs = NULL_TREE;
11012 cp_parser_parse_tentatively (parser);
11013 /* Try to parse the declaration-statement. */
11014 cp_parser_declaration_statement (parser);
11015 /* If that worked, we're done. */
11016 if (cp_parser_parse_definitely (parser))
11017 return;
11019 /* All preceding labels have been parsed at this point. */
11020 if (loc_after_labels != NULL)
11021 *loc_after_labels = statement_location;
11023 /* Look for an expression-statement instead. */
11024 statement = cp_parser_expression_statement (parser, in_statement_expr);
11026 /* Handle [[fallthrough]];. */
11027 if (attribute_fallthrough_p (std_attrs))
11029 /* The next token after the fallthrough attribute is ';'. */
11030 if (statement == NULL_TREE)
11032 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11033 statement = build_call_expr_internal_loc (statement_location,
11034 IFN_FALLTHROUGH,
11035 void_type_node, 0);
11036 finish_expr_stmt (statement);
11038 else
11039 warning_at (statement_location, OPT_Wattributes,
11040 "%<fallthrough%> attribute not followed by %<;%>");
11041 std_attrs = NULL_TREE;
11045 /* Set the line number for the statement. */
11046 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11047 SET_EXPR_LOCATION (statement, statement_location);
11049 /* Allow "[[fallthrough]];", but warn otherwise. */
11050 if (std_attrs != NULL_TREE)
11051 warning_at (attrs_location,
11052 OPT_Wattributes,
11053 "attributes at the beginning of statement are ignored");
11056 /* Parse the label for a labeled-statement, i.e.
11058 identifier :
11059 case constant-expression :
11060 default :
11062 GNU Extension:
11063 case constant-expression ... constant-expression : statement
11065 When a label is parsed without errors, the label is added to the
11066 parse tree by the finish_* functions, so this function doesn't
11067 have to return the label. */
11069 static void
11070 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11072 cp_token *token;
11073 tree label = NULL_TREE;
11074 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11076 /* The next token should be an identifier. */
11077 token = cp_lexer_peek_token (parser->lexer);
11078 if (token->type != CPP_NAME
11079 && token->type != CPP_KEYWORD)
11081 cp_parser_error (parser, "expected labeled-statement");
11082 return;
11085 /* Remember whether this case or a user-defined label is allowed to fall
11086 through to. */
11087 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11089 parser->colon_corrects_to_scope_p = false;
11090 switch (token->keyword)
11092 case RID_CASE:
11094 tree expr, expr_hi;
11095 cp_token *ellipsis;
11097 /* Consume the `case' token. */
11098 cp_lexer_consume_token (parser->lexer);
11099 /* Parse the constant-expression. */
11100 expr = cp_parser_constant_expression (parser);
11101 if (check_for_bare_parameter_packs (expr))
11102 expr = error_mark_node;
11104 ellipsis = cp_lexer_peek_token (parser->lexer);
11105 if (ellipsis->type == CPP_ELLIPSIS)
11107 /* Consume the `...' token. */
11108 cp_lexer_consume_token (parser->lexer);
11109 expr_hi = cp_parser_constant_expression (parser);
11110 if (check_for_bare_parameter_packs (expr_hi))
11111 expr_hi = error_mark_node;
11113 /* We don't need to emit warnings here, as the common code
11114 will do this for us. */
11116 else
11117 expr_hi = NULL_TREE;
11119 if (parser->in_switch_statement_p)
11121 tree l = finish_case_label (token->location, expr, expr_hi);
11122 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11123 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11125 else
11126 error_at (token->location,
11127 "case label %qE not within a switch statement",
11128 expr);
11130 break;
11132 case RID_DEFAULT:
11133 /* Consume the `default' token. */
11134 cp_lexer_consume_token (parser->lexer);
11136 if (parser->in_switch_statement_p)
11138 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11139 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11140 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11142 else
11143 error_at (token->location, "case label not within a switch statement");
11144 break;
11146 default:
11147 /* Anything else must be an ordinary label. */
11148 label = finish_label_stmt (cp_parser_identifier (parser));
11149 if (label && TREE_CODE (label) == LABEL_DECL)
11150 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11151 break;
11154 /* Require the `:' token. */
11155 cp_parser_require (parser, CPP_COLON, RT_COLON);
11157 /* An ordinary label may optionally be followed by attributes.
11158 However, this is only permitted if the attributes are then
11159 followed by a semicolon. This is because, for backward
11160 compatibility, when parsing
11161 lab: __attribute__ ((unused)) int i;
11162 we want the attribute to attach to "i", not "lab". */
11163 if (label != NULL_TREE
11164 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11166 tree attrs;
11167 cp_parser_parse_tentatively (parser);
11168 attrs = cp_parser_gnu_attributes_opt (parser);
11169 if (attrs == NULL_TREE
11170 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11171 cp_parser_abort_tentative_parse (parser);
11172 else if (!cp_parser_parse_definitely (parser))
11174 else
11175 attributes = chainon (attributes, attrs);
11178 if (attributes != NULL_TREE)
11179 cplus_decl_attributes (&label, attributes, 0);
11181 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11184 /* Parse an expression-statement.
11186 expression-statement:
11187 expression [opt] ;
11189 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11190 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11191 indicates whether this expression-statement is part of an
11192 expression statement. */
11194 static tree
11195 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11197 tree statement = NULL_TREE;
11198 cp_token *token = cp_lexer_peek_token (parser->lexer);
11199 location_t loc = token->location;
11201 /* There might be attribute fallthrough. */
11202 tree attr = cp_parser_gnu_attributes_opt (parser);
11204 /* If the next token is a ';', then there is no expression
11205 statement. */
11206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11208 statement = cp_parser_expression (parser);
11209 if (statement == error_mark_node
11210 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11212 cp_parser_skip_to_end_of_block_or_statement (parser);
11213 return error_mark_node;
11217 /* Handle [[fallthrough]];. */
11218 if (attribute_fallthrough_p (attr))
11220 /* The next token after the fallthrough attribute is ';'. */
11221 if (statement == NULL_TREE)
11222 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11223 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11224 void_type_node, 0);
11225 else
11226 warning_at (loc, OPT_Wattributes,
11227 "%<fallthrough%> attribute not followed by %<;%>");
11228 attr = NULL_TREE;
11231 /* Allow "[[fallthrough]];", but warn otherwise. */
11232 if (attr != NULL_TREE)
11233 warning_at (loc, OPT_Wattributes,
11234 "attributes at the beginning of statement are ignored");
11236 /* Give a helpful message for "A<T>::type t;" and the like. */
11237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11238 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11240 if (TREE_CODE (statement) == SCOPE_REF)
11241 error_at (token->location, "need %<typename%> before %qE because "
11242 "%qT is a dependent scope",
11243 statement, TREE_OPERAND (statement, 0));
11244 else if (is_overloaded_fn (statement)
11245 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11247 /* A::A a; */
11248 tree fn = get_first_fn (statement);
11249 error_at (token->location,
11250 "%<%T::%D%> names the constructor, not the type",
11251 DECL_CONTEXT (fn), DECL_NAME (fn));
11255 /* Consume the final `;'. */
11256 cp_parser_consume_semicolon_at_end_of_statement (parser);
11258 if (in_statement_expr
11259 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11260 /* This is the final expression statement of a statement
11261 expression. */
11262 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11263 else if (statement)
11264 statement = finish_expr_stmt (statement);
11266 return statement;
11269 /* Parse a compound-statement.
11271 compound-statement:
11272 { statement-seq [opt] }
11274 GNU extension:
11276 compound-statement:
11277 { label-declaration-seq [opt] statement-seq [opt] }
11279 label-declaration-seq:
11280 label-declaration
11281 label-declaration-seq label-declaration
11283 Returns a tree representing the statement. */
11285 static tree
11286 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11287 int bcs_flags, bool function_body)
11289 tree compound_stmt;
11290 matching_braces braces;
11292 /* Consume the `{'. */
11293 if (!braces.require_open (parser))
11294 return error_mark_node;
11295 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11296 && !function_body && cxx_dialect < cxx14)
11297 pedwarn (input_location, OPT_Wpedantic,
11298 "compound-statement in constexpr function");
11299 /* Begin the compound-statement. */
11300 compound_stmt = begin_compound_stmt (bcs_flags);
11301 /* If the next keyword is `__label__' we have a label declaration. */
11302 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11303 cp_parser_label_declaration (parser);
11304 /* Parse an (optional) statement-seq. */
11305 cp_parser_statement_seq_opt (parser, in_statement_expr);
11306 /* Finish the compound-statement. */
11307 finish_compound_stmt (compound_stmt);
11308 /* Consume the `}'. */
11309 braces.require_close (parser);
11311 return compound_stmt;
11314 /* Parse an (optional) statement-seq.
11316 statement-seq:
11317 statement
11318 statement-seq [opt] statement */
11320 static void
11321 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11323 /* Scan statements until there aren't any more. */
11324 while (true)
11326 cp_token *token = cp_lexer_peek_token (parser->lexer);
11328 /* If we are looking at a `}', then we have run out of
11329 statements; the same is true if we have reached the end
11330 of file, or have stumbled upon a stray '@end'. */
11331 if (token->type == CPP_CLOSE_BRACE
11332 || token->type == CPP_EOF
11333 || token->type == CPP_PRAGMA_EOL
11334 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11335 break;
11337 /* If we are in a compound statement and find 'else' then
11338 something went wrong. */
11339 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11341 if (parser->in_statement & IN_IF_STMT)
11342 break;
11343 else
11345 token = cp_lexer_consume_token (parser->lexer);
11346 error_at (token->location, "%<else%> without a previous %<if%>");
11350 /* Parse the statement. */
11351 cp_parser_statement (parser, in_statement_expr, true, NULL);
11355 /* Return true if we're looking at (init; cond), false otherwise. */
11357 static bool
11358 cp_parser_init_statement_p (cp_parser *parser)
11360 /* Save tokens so that we can put them back. */
11361 cp_lexer_save_tokens (parser->lexer);
11363 /* Look for ';' that is not nested in () or {}. */
11364 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11365 /*recovering=*/false,
11366 CPP_SEMICOLON,
11367 /*consume_paren=*/false);
11369 /* Roll back the tokens we skipped. */
11370 cp_lexer_rollback_tokens (parser->lexer);
11372 return ret == -1;
11375 /* Parse a selection-statement.
11377 selection-statement:
11378 if ( init-statement [opt] condition ) statement
11379 if ( init-statement [opt] condition ) statement else statement
11380 switch ( init-statement [opt] condition ) statement
11382 Returns the new IF_STMT or SWITCH_STMT.
11384 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11385 is a (possibly labeled) if statement which is not enclosed in
11386 braces and has an else clause. This is used to implement
11387 -Wparentheses.
11389 CHAIN is a vector of if-else-if conditions. This is used to implement
11390 -Wduplicated-cond. */
11392 static tree
11393 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11394 vec<tree> *chain)
11396 cp_token *token;
11397 enum rid keyword;
11398 token_indent_info guard_tinfo;
11400 if (if_p != NULL)
11401 *if_p = false;
11403 /* Peek at the next token. */
11404 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11405 guard_tinfo = get_token_indent_info (token);
11407 /* See what kind of keyword it is. */
11408 keyword = token->keyword;
11409 switch (keyword)
11411 case RID_IF:
11412 case RID_SWITCH:
11414 tree statement;
11415 tree condition;
11417 bool cx = false;
11418 if (keyword == RID_IF
11419 && cp_lexer_next_token_is_keyword (parser->lexer,
11420 RID_CONSTEXPR))
11422 cx = true;
11423 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11424 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11425 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11426 "with -std=c++17 or -std=gnu++17");
11429 /* Look for the `('. */
11430 matching_parens parens;
11431 if (!parens.require_open (parser))
11433 cp_parser_skip_to_end_of_statement (parser);
11434 return error_mark_node;
11437 /* Begin the selection-statement. */
11438 if (keyword == RID_IF)
11440 statement = begin_if_stmt ();
11441 IF_STMT_CONSTEXPR_P (statement) = cx;
11443 else
11444 statement = begin_switch_stmt ();
11446 /* Parse the optional init-statement. */
11447 if (cp_parser_init_statement_p (parser))
11449 tree decl;
11450 if (cxx_dialect < cxx17)
11451 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11452 "init-statement in selection statements only available "
11453 "with -std=c++17 or -std=gnu++17");
11454 cp_parser_init_statement (parser, &decl);
11457 /* Parse the condition. */
11458 condition = cp_parser_condition (parser);
11459 /* Look for the `)'. */
11460 if (!parens.require_close (parser))
11461 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11462 /*consume_paren=*/true);
11464 if (keyword == RID_IF)
11466 bool nested_if;
11467 unsigned char in_statement;
11469 /* Add the condition. */
11470 condition = finish_if_stmt_cond (condition, statement);
11472 if (warn_duplicated_cond)
11473 warn_duplicated_cond_add_or_warn (token->location, condition,
11474 &chain);
11476 /* Parse the then-clause. */
11477 in_statement = parser->in_statement;
11478 parser->in_statement |= IN_IF_STMT;
11480 /* Outside a template, the non-selected branch of a constexpr
11481 if is a 'discarded statement', i.e. unevaluated. */
11482 bool was_discarded = in_discarded_stmt;
11483 bool discard_then = (cx && !processing_template_decl
11484 && integer_zerop (condition));
11485 if (discard_then)
11487 in_discarded_stmt = true;
11488 ++c_inhibit_evaluation_warnings;
11491 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11492 guard_tinfo);
11494 parser->in_statement = in_statement;
11496 finish_then_clause (statement);
11498 if (discard_then)
11500 THEN_CLAUSE (statement) = NULL_TREE;
11501 in_discarded_stmt = was_discarded;
11502 --c_inhibit_evaluation_warnings;
11505 /* If the next token is `else', parse the else-clause. */
11506 if (cp_lexer_next_token_is_keyword (parser->lexer,
11507 RID_ELSE))
11509 bool discard_else = (cx && !processing_template_decl
11510 && integer_nonzerop (condition));
11511 if (discard_else)
11513 in_discarded_stmt = true;
11514 ++c_inhibit_evaluation_warnings;
11517 guard_tinfo
11518 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11519 /* Consume the `else' keyword. */
11520 cp_lexer_consume_token (parser->lexer);
11521 if (warn_duplicated_cond)
11523 if (cp_lexer_next_token_is_keyword (parser->lexer,
11524 RID_IF)
11525 && chain == NULL)
11527 /* We've got "if (COND) else if (COND2)". Start
11528 the condition chain and add COND as the first
11529 element. */
11530 chain = new vec<tree> ();
11531 if (!CONSTANT_CLASS_P (condition)
11532 && !TREE_SIDE_EFFECTS (condition))
11534 /* Wrap it in a NOP_EXPR so that we can set the
11535 location of the condition. */
11536 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11537 condition);
11538 SET_EXPR_LOCATION (e, token->location);
11539 chain->safe_push (e);
11542 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11543 RID_IF))
11545 /* This is if-else without subsequent if. Zap the
11546 condition chain; we would have already warned at
11547 this point. */
11548 delete chain;
11549 chain = NULL;
11552 begin_else_clause (statement);
11553 /* Parse the else-clause. */
11554 cp_parser_implicitly_scoped_statement (parser, NULL,
11555 guard_tinfo, chain);
11557 finish_else_clause (statement);
11559 /* If we are currently parsing a then-clause, then
11560 IF_P will not be NULL. We set it to true to
11561 indicate that this if statement has an else clause.
11562 This may trigger the Wparentheses warning below
11563 when we get back up to the parent if statement. */
11564 if (if_p != NULL)
11565 *if_p = true;
11567 if (discard_else)
11569 ELSE_CLAUSE (statement) = NULL_TREE;
11570 in_discarded_stmt = was_discarded;
11571 --c_inhibit_evaluation_warnings;
11574 else
11576 /* This if statement does not have an else clause. If
11577 NESTED_IF is true, then the then-clause has an if
11578 statement which does have an else clause. We warn
11579 about the potential ambiguity. */
11580 if (nested_if)
11581 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11582 "suggest explicit braces to avoid ambiguous"
11583 " %<else%>");
11584 if (warn_duplicated_cond)
11586 /* We don't need the condition chain anymore. */
11587 delete chain;
11588 chain = NULL;
11592 /* Now we're all done with the if-statement. */
11593 finish_if_stmt (statement);
11595 else
11597 bool in_switch_statement_p;
11598 unsigned char in_statement;
11600 /* Add the condition. */
11601 finish_switch_cond (condition, statement);
11603 /* Parse the body of the switch-statement. */
11604 in_switch_statement_p = parser->in_switch_statement_p;
11605 in_statement = parser->in_statement;
11606 parser->in_switch_statement_p = true;
11607 parser->in_statement |= IN_SWITCH_STMT;
11608 cp_parser_implicitly_scoped_statement (parser, if_p,
11609 guard_tinfo);
11610 parser->in_switch_statement_p = in_switch_statement_p;
11611 parser->in_statement = in_statement;
11613 /* Now we're all done with the switch-statement. */
11614 finish_switch_stmt (statement);
11617 return statement;
11619 break;
11621 default:
11622 cp_parser_error (parser, "expected selection-statement");
11623 return error_mark_node;
11627 /* Parse a condition.
11629 condition:
11630 expression
11631 type-specifier-seq declarator = initializer-clause
11632 type-specifier-seq declarator braced-init-list
11634 GNU Extension:
11636 condition:
11637 type-specifier-seq declarator asm-specification [opt]
11638 attributes [opt] = assignment-expression
11640 Returns the expression that should be tested. */
11642 static tree
11643 cp_parser_condition (cp_parser* parser)
11645 cp_decl_specifier_seq type_specifiers;
11646 const char *saved_message;
11647 int declares_class_or_enum;
11649 /* Try the declaration first. */
11650 cp_parser_parse_tentatively (parser);
11651 /* New types are not allowed in the type-specifier-seq for a
11652 condition. */
11653 saved_message = parser->type_definition_forbidden_message;
11654 parser->type_definition_forbidden_message
11655 = G_("types may not be defined in conditions");
11656 /* Parse the type-specifier-seq. */
11657 cp_parser_decl_specifier_seq (parser,
11658 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11659 &type_specifiers,
11660 &declares_class_or_enum);
11661 /* Restore the saved message. */
11662 parser->type_definition_forbidden_message = saved_message;
11663 /* If all is well, we might be looking at a declaration. */
11664 if (!cp_parser_error_occurred (parser))
11666 tree decl;
11667 tree asm_specification;
11668 tree attributes;
11669 cp_declarator *declarator;
11670 tree initializer = NULL_TREE;
11672 /* Parse the declarator. */
11673 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11674 /*ctor_dtor_or_conv_p=*/NULL,
11675 /*parenthesized_p=*/NULL,
11676 /*member_p=*/false,
11677 /*friend_p=*/false);
11678 /* Parse the attributes. */
11679 attributes = cp_parser_attributes_opt (parser);
11680 /* Parse the asm-specification. */
11681 asm_specification = cp_parser_asm_specification_opt (parser);
11682 /* If the next token is not an `=' or '{', then we might still be
11683 looking at an expression. For example:
11685 if (A(a).x)
11687 looks like a decl-specifier-seq and a declarator -- but then
11688 there is no `=', so this is an expression. */
11689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11690 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11691 cp_parser_simulate_error (parser);
11693 /* If we did see an `=' or '{', then we are looking at a declaration
11694 for sure. */
11695 if (cp_parser_parse_definitely (parser))
11697 tree pushed_scope;
11698 bool non_constant_p;
11699 int flags = LOOKUP_ONLYCONVERTING;
11701 /* Create the declaration. */
11702 decl = start_decl (declarator, &type_specifiers,
11703 /*initialized_p=*/true,
11704 attributes, /*prefix_attributes=*/NULL_TREE,
11705 &pushed_scope);
11707 /* Parse the initializer. */
11708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11710 initializer = cp_parser_braced_list (parser, &non_constant_p);
11711 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11712 flags = 0;
11714 else
11716 /* Consume the `='. */
11717 cp_parser_require (parser, CPP_EQ, RT_EQ);
11718 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11720 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11721 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11723 /* Process the initializer. */
11724 cp_finish_decl (decl,
11725 initializer, !non_constant_p,
11726 asm_specification,
11727 flags);
11729 if (pushed_scope)
11730 pop_scope (pushed_scope);
11732 return convert_from_reference (decl);
11735 /* If we didn't even get past the declarator successfully, we are
11736 definitely not looking at a declaration. */
11737 else
11738 cp_parser_abort_tentative_parse (parser);
11740 /* Otherwise, we are looking at an expression. */
11741 return cp_parser_expression (parser);
11744 /* Parses a for-statement or range-for-statement until the closing ')',
11745 not included. */
11747 static tree
11748 cp_parser_for (cp_parser *parser, bool ivdep)
11750 tree init, scope, decl;
11751 bool is_range_for;
11753 /* Begin the for-statement. */
11754 scope = begin_for_scope (&init);
11756 /* Parse the initialization. */
11757 is_range_for = cp_parser_init_statement (parser, &decl);
11759 if (is_range_for)
11760 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11761 else
11762 return cp_parser_c_for (parser, scope, init, ivdep);
11765 static tree
11766 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11768 /* Normal for loop */
11769 tree condition = NULL_TREE;
11770 tree expression = NULL_TREE;
11771 tree stmt;
11773 stmt = begin_for_stmt (scope, init);
11774 /* The init-statement has already been parsed in
11775 cp_parser_init_statement, so no work is needed here. */
11776 finish_init_stmt (stmt);
11778 /* If there's a condition, process it. */
11779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11780 condition = cp_parser_condition (parser);
11781 else if (ivdep)
11783 cp_parser_error (parser, "missing loop condition in loop with "
11784 "%<GCC ivdep%> pragma");
11785 condition = error_mark_node;
11787 finish_for_cond (condition, stmt, ivdep);
11788 /* Look for the `;'. */
11789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11791 /* If there's an expression, process it. */
11792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11793 expression = cp_parser_expression (parser);
11794 finish_for_expr (expression, stmt);
11796 return stmt;
11799 /* Tries to parse a range-based for-statement:
11801 range-based-for:
11802 decl-specifier-seq declarator : expression
11804 The decl-specifier-seq declarator and the `:' are already parsed by
11805 cp_parser_init_statement. If processing_template_decl it returns a
11806 newly created RANGE_FOR_STMT; if not, it is converted to a
11807 regular FOR_STMT. */
11809 static tree
11810 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11811 bool ivdep)
11813 tree stmt, range_expr;
11814 auto_vec <cxx_binding *, 16> bindings;
11815 auto_vec <tree, 16> names;
11816 tree decomp_first_name = NULL_TREE;
11817 unsigned int decomp_cnt = 0;
11819 /* Get the range declaration momentarily out of the way so that
11820 the range expression doesn't clash with it. */
11821 if (range_decl != error_mark_node)
11823 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11825 tree v = DECL_VALUE_EXPR (range_decl);
11826 /* For decomposition declaration get all of the corresponding
11827 declarations out of the way. */
11828 if (TREE_CODE (v) == ARRAY_REF
11829 && VAR_P (TREE_OPERAND (v, 0))
11830 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11832 tree d = range_decl;
11833 range_decl = TREE_OPERAND (v, 0);
11834 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11835 decomp_first_name = d;
11836 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11838 tree name = DECL_NAME (d);
11839 names.safe_push (name);
11840 bindings.safe_push (IDENTIFIER_BINDING (name));
11841 IDENTIFIER_BINDING (name)
11842 = IDENTIFIER_BINDING (name)->previous;
11846 if (names.is_empty ())
11848 tree name = DECL_NAME (range_decl);
11849 names.safe_push (name);
11850 bindings.safe_push (IDENTIFIER_BINDING (name));
11851 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11857 bool expr_non_constant_p;
11858 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11860 else
11861 range_expr = cp_parser_expression (parser);
11863 /* Put the range declaration(s) back into scope. */
11864 for (unsigned int i = 0; i < names.length (); i++)
11866 cxx_binding *binding = bindings[i];
11867 binding->previous = IDENTIFIER_BINDING (names[i]);
11868 IDENTIFIER_BINDING (names[i]) = binding;
11871 /* If in template, STMT is converted to a normal for-statement
11872 at instantiation. If not, it is done just ahead. */
11873 if (processing_template_decl)
11875 if (check_for_bare_parameter_packs (range_expr))
11876 range_expr = error_mark_node;
11877 stmt = begin_range_for_stmt (scope, init);
11878 if (ivdep)
11879 RANGE_FOR_IVDEP (stmt) = 1;
11880 finish_range_for_decl (stmt, range_decl, range_expr);
11881 if (!type_dependent_expression_p (range_expr)
11882 /* do_auto_deduction doesn't mess with template init-lists. */
11883 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11884 do_range_for_auto_deduction (range_decl, range_expr);
11886 else
11888 stmt = begin_for_stmt (scope, init);
11889 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11890 decomp_first_name, decomp_cnt, ivdep);
11892 return stmt;
11895 /* Subroutine of cp_convert_range_for: given the initializer expression,
11896 builds up the range temporary. */
11898 static tree
11899 build_range_temp (tree range_expr)
11901 tree range_type, range_temp;
11903 /* Find out the type deduced by the declaration
11904 `auto &&__range = range_expr'. */
11905 range_type = cp_build_reference_type (make_auto (), true);
11906 range_type = do_auto_deduction (range_type, range_expr,
11907 type_uses_auto (range_type));
11909 /* Create the __range variable. */
11910 range_temp = build_decl (input_location, VAR_DECL,
11911 get_identifier ("__for_range"), range_type);
11912 TREE_USED (range_temp) = 1;
11913 DECL_ARTIFICIAL (range_temp) = 1;
11915 return range_temp;
11918 /* Used by cp_parser_range_for in template context: we aren't going to
11919 do a full conversion yet, but we still need to resolve auto in the
11920 type of the for-range-declaration if present. This is basically
11921 a shortcut version of cp_convert_range_for. */
11923 static void
11924 do_range_for_auto_deduction (tree decl, tree range_expr)
11926 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11927 if (auto_node)
11929 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11930 range_temp = convert_from_reference (build_range_temp (range_expr));
11931 iter_type = (cp_parser_perform_range_for_lookup
11932 (range_temp, &begin_dummy, &end_dummy));
11933 if (iter_type)
11935 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11936 iter_type);
11937 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11938 RO_UNARY_STAR,
11939 tf_warning_or_error);
11940 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11941 iter_decl, auto_node);
11946 /* Converts a range-based for-statement into a normal
11947 for-statement, as per the definition.
11949 for (RANGE_DECL : RANGE_EXPR)
11950 BLOCK
11952 should be equivalent to:
11955 auto &&__range = RANGE_EXPR;
11956 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11957 __begin != __end;
11958 ++__begin)
11960 RANGE_DECL = *__begin;
11961 BLOCK
11965 If RANGE_EXPR is an array:
11966 BEGIN_EXPR = __range
11967 END_EXPR = __range + ARRAY_SIZE(__range)
11968 Else if RANGE_EXPR has a member 'begin' or 'end':
11969 BEGIN_EXPR = __range.begin()
11970 END_EXPR = __range.end()
11971 Else:
11972 BEGIN_EXPR = begin(__range)
11973 END_EXPR = end(__range);
11975 If __range has a member 'begin' but not 'end', or vice versa, we must
11976 still use the second alternative (it will surely fail, however).
11977 When calling begin()/end() in the third alternative we must use
11978 argument dependent lookup, but always considering 'std' as an associated
11979 namespace. */
11981 tree
11982 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11983 tree decomp_first_name, unsigned int decomp_cnt,
11984 bool ivdep)
11986 tree begin, end;
11987 tree iter_type, begin_expr, end_expr;
11988 tree condition, expression;
11990 range_expr = mark_lvalue_use (range_expr);
11992 if (range_decl == error_mark_node || range_expr == error_mark_node)
11993 /* If an error happened previously do nothing or else a lot of
11994 unhelpful errors would be issued. */
11995 begin_expr = end_expr = iter_type = error_mark_node;
11996 else
11998 tree range_temp;
12000 if (VAR_P (range_expr)
12001 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12002 /* Can't bind a reference to an array of runtime bound. */
12003 range_temp = range_expr;
12004 else
12006 range_temp = build_range_temp (range_expr);
12007 pushdecl (range_temp);
12008 cp_finish_decl (range_temp, range_expr,
12009 /*is_constant_init*/false, NULL_TREE,
12010 LOOKUP_ONLYCONVERTING);
12011 range_temp = convert_from_reference (range_temp);
12013 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12014 &begin_expr, &end_expr);
12017 /* The new for initialization statement. */
12018 begin = build_decl (input_location, VAR_DECL,
12019 get_identifier ("__for_begin"), iter_type);
12020 TREE_USED (begin) = 1;
12021 DECL_ARTIFICIAL (begin) = 1;
12022 pushdecl (begin);
12023 cp_finish_decl (begin, begin_expr,
12024 /*is_constant_init*/false, NULL_TREE,
12025 LOOKUP_ONLYCONVERTING);
12027 if (cxx_dialect >= cxx17)
12028 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12029 end = build_decl (input_location, VAR_DECL,
12030 get_identifier ("__for_end"), iter_type);
12031 TREE_USED (end) = 1;
12032 DECL_ARTIFICIAL (end) = 1;
12033 pushdecl (end);
12034 cp_finish_decl (end, end_expr,
12035 /*is_constant_init*/false, NULL_TREE,
12036 LOOKUP_ONLYCONVERTING);
12038 finish_init_stmt (statement);
12040 /* The new for condition. */
12041 condition = build_x_binary_op (input_location, NE_EXPR,
12042 begin, ERROR_MARK,
12043 end, ERROR_MARK,
12044 NULL, tf_warning_or_error);
12045 finish_for_cond (condition, statement, ivdep);
12047 /* The new increment expression. */
12048 expression = finish_unary_op_expr (input_location,
12049 PREINCREMENT_EXPR, begin,
12050 tf_warning_or_error);
12051 finish_for_expr (expression, statement);
12053 /* The declaration is initialized with *__begin inside the loop body. */
12054 cp_finish_decl (range_decl,
12055 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12056 tf_warning_or_error),
12057 /*is_constant_init*/false, NULL_TREE,
12058 LOOKUP_ONLYCONVERTING);
12059 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12060 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12062 return statement;
12065 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12066 We need to solve both at the same time because the method used
12067 depends on the existence of members begin or end.
12068 Returns the type deduced for the iterator expression. */
12070 static tree
12071 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12073 if (error_operand_p (range))
12075 *begin = *end = error_mark_node;
12076 return error_mark_node;
12079 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12081 error ("range-based %<for%> expression of type %qT "
12082 "has incomplete type", TREE_TYPE (range));
12083 *begin = *end = error_mark_node;
12084 return error_mark_node;
12086 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12088 /* If RANGE is an array, we will use pointer arithmetic. */
12089 *begin = decay_conversion (range, tf_warning_or_error);
12090 *end = build_binary_op (input_location, PLUS_EXPR,
12091 range,
12092 array_type_nelts_top (TREE_TYPE (range)),
12093 false);
12094 return TREE_TYPE (*begin);
12096 else
12098 /* If it is not an array, we must do a bit of magic. */
12099 tree id_begin, id_end;
12100 tree member_begin, member_end;
12102 *begin = *end = error_mark_node;
12104 id_begin = get_identifier ("begin");
12105 id_end = get_identifier ("end");
12106 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12107 /*protect=*/2, /*want_type=*/false,
12108 tf_warning_or_error);
12109 member_end = lookup_member (TREE_TYPE (range), id_end,
12110 /*protect=*/2, /*want_type=*/false,
12111 tf_warning_or_error);
12113 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12115 /* Use the member functions. */
12116 if (member_begin != NULL_TREE)
12117 *begin = cp_parser_range_for_member_function (range, id_begin);
12118 else
12119 error ("range-based %<for%> expression of type %qT has an "
12120 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12122 if (member_end != NULL_TREE)
12123 *end = cp_parser_range_for_member_function (range, id_end);
12124 else
12125 error ("range-based %<for%> expression of type %qT has a "
12126 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12128 else
12130 /* Use global functions with ADL. */
12131 vec<tree, va_gc> *vec;
12132 vec = make_tree_vector ();
12134 vec_safe_push (vec, range);
12136 member_begin = perform_koenig_lookup (id_begin, vec,
12137 tf_warning_or_error);
12138 *begin = finish_call_expr (member_begin, &vec, false, true,
12139 tf_warning_or_error);
12140 member_end = perform_koenig_lookup (id_end, vec,
12141 tf_warning_or_error);
12142 *end = finish_call_expr (member_end, &vec, false, true,
12143 tf_warning_or_error);
12145 release_tree_vector (vec);
12148 /* Last common checks. */
12149 if (*begin == error_mark_node || *end == error_mark_node)
12151 /* If one of the expressions is an error do no more checks. */
12152 *begin = *end = error_mark_node;
12153 return error_mark_node;
12155 else if (type_dependent_expression_p (*begin)
12156 || type_dependent_expression_p (*end))
12157 /* Can happen, when, eg, in a template context, Koenig lookup
12158 can't resolve begin/end (c++/58503). */
12159 return NULL_TREE;
12160 else
12162 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12163 /* The unqualified type of the __begin and __end temporaries should
12164 be the same, as required by the multiple auto declaration. */
12165 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12167 if (cxx_dialect >= cxx17
12168 && (build_x_binary_op (input_location, NE_EXPR,
12169 *begin, ERROR_MARK,
12170 *end, ERROR_MARK,
12171 NULL, tf_none)
12172 != error_mark_node))
12173 /* P0184R0 allows __begin and __end to have different types,
12174 but make sure they are comparable so we can give a better
12175 diagnostic. */;
12176 else
12177 error ("inconsistent begin/end types in range-based %<for%> "
12178 "statement: %qT and %qT",
12179 TREE_TYPE (*begin), TREE_TYPE (*end));
12181 return iter_type;
12186 /* Helper function for cp_parser_perform_range_for_lookup.
12187 Builds a tree for RANGE.IDENTIFIER(). */
12189 static tree
12190 cp_parser_range_for_member_function (tree range, tree identifier)
12192 tree member, res;
12193 vec<tree, va_gc> *vec;
12195 member = finish_class_member_access_expr (range, identifier,
12196 false, tf_warning_or_error);
12197 if (member == error_mark_node)
12198 return error_mark_node;
12200 vec = make_tree_vector ();
12201 res = finish_call_expr (member, &vec,
12202 /*disallow_virtual=*/false,
12203 /*koenig_p=*/false,
12204 tf_warning_or_error);
12205 release_tree_vector (vec);
12206 return res;
12209 /* Parse an iteration-statement.
12211 iteration-statement:
12212 while ( condition ) statement
12213 do statement while ( expression ) ;
12214 for ( init-statement condition [opt] ; expression [opt] )
12215 statement
12217 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12219 static tree
12220 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12222 cp_token *token;
12223 enum rid keyword;
12224 tree statement;
12225 unsigned char in_statement;
12226 token_indent_info guard_tinfo;
12228 /* Peek at the next token. */
12229 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12230 if (!token)
12231 return error_mark_node;
12233 guard_tinfo = get_token_indent_info (token);
12235 /* Remember whether or not we are already within an iteration
12236 statement. */
12237 in_statement = parser->in_statement;
12239 /* See what kind of keyword it is. */
12240 keyword = token->keyword;
12241 switch (keyword)
12243 case RID_WHILE:
12245 tree condition;
12247 /* Begin the while-statement. */
12248 statement = begin_while_stmt ();
12249 /* Look for the `('. */
12250 matching_parens parens;
12251 parens.require_open (parser);
12252 /* Parse the condition. */
12253 condition = cp_parser_condition (parser);
12254 finish_while_stmt_cond (condition, statement, ivdep);
12255 /* Look for the `)'. */
12256 parens.require_close (parser);
12257 /* Parse the dependent statement. */
12258 parser->in_statement = IN_ITERATION_STMT;
12259 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12260 parser->in_statement = in_statement;
12261 /* We're done with the while-statement. */
12262 finish_while_stmt (statement);
12264 break;
12266 case RID_DO:
12268 tree expression;
12270 /* Begin the do-statement. */
12271 statement = begin_do_stmt ();
12272 /* Parse the body of the do-statement. */
12273 parser->in_statement = IN_ITERATION_STMT;
12274 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12275 parser->in_statement = in_statement;
12276 finish_do_body (statement);
12277 /* Look for the `while' keyword. */
12278 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12279 /* Look for the `('. */
12280 matching_parens parens;
12281 parens.require_open (parser);
12282 /* Parse the expression. */
12283 expression = cp_parser_expression (parser);
12284 /* We're done with the do-statement. */
12285 finish_do_stmt (expression, statement, ivdep);
12286 /* Look for the `)'. */
12287 parens.require_close (parser);
12288 /* Look for the `;'. */
12289 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12291 break;
12293 case RID_FOR:
12295 /* Look for the `('. */
12296 matching_parens parens;
12297 parens.require_open (parser);
12299 statement = cp_parser_for (parser, ivdep);
12301 /* Look for the `)'. */
12302 parens.require_close (parser);
12304 /* Parse the body of the for-statement. */
12305 parser->in_statement = IN_ITERATION_STMT;
12306 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12307 parser->in_statement = in_statement;
12309 /* We're done with the for-statement. */
12310 finish_for_stmt (statement);
12312 break;
12314 default:
12315 cp_parser_error (parser, "expected iteration-statement");
12316 statement = error_mark_node;
12317 break;
12320 return statement;
12323 /* Parse a init-statement or the declarator of a range-based-for.
12324 Returns true if a range-based-for declaration is seen.
12326 init-statement:
12327 expression-statement
12328 simple-declaration */
12330 static bool
12331 cp_parser_init_statement (cp_parser* parser, tree *decl)
12333 /* If the next token is a `;', then we have an empty
12334 expression-statement. Grammatically, this is also a
12335 simple-declaration, but an invalid one, because it does not
12336 declare anything. Therefore, if we did not handle this case
12337 specially, we would issue an error message about an invalid
12338 declaration. */
12339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12341 bool is_range_for = false;
12342 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12344 /* A colon is used in range-based for. */
12345 parser->colon_corrects_to_scope_p = false;
12347 /* We're going to speculatively look for a declaration, falling back
12348 to an expression, if necessary. */
12349 cp_parser_parse_tentatively (parser);
12350 /* Parse the declaration. */
12351 cp_parser_simple_declaration (parser,
12352 /*function_definition_allowed_p=*/false,
12353 decl);
12354 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12355 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12357 /* It is a range-for, consume the ':' */
12358 cp_lexer_consume_token (parser->lexer);
12359 is_range_for = true;
12360 if (cxx_dialect < cxx11)
12362 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12363 "range-based %<for%> loops only available with "
12364 "-std=c++11 or -std=gnu++11");
12365 *decl = error_mark_node;
12368 else
12369 /* The ';' is not consumed yet because we told
12370 cp_parser_simple_declaration not to. */
12371 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12373 if (cp_parser_parse_definitely (parser))
12374 return is_range_for;
12375 /* If the tentative parse failed, then we shall need to look for an
12376 expression-statement. */
12378 /* If we are here, it is an expression-statement. */
12379 cp_parser_expression_statement (parser, NULL_TREE);
12380 return false;
12383 /* Parse a jump-statement.
12385 jump-statement:
12386 break ;
12387 continue ;
12388 return expression [opt] ;
12389 return braced-init-list ;
12390 goto identifier ;
12392 GNU extension:
12394 jump-statement:
12395 goto * expression ;
12397 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12399 static tree
12400 cp_parser_jump_statement (cp_parser* parser)
12402 tree statement = error_mark_node;
12403 cp_token *token;
12404 enum rid keyword;
12405 unsigned char in_statement;
12407 /* Peek at the next token. */
12408 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12409 if (!token)
12410 return error_mark_node;
12412 /* See what kind of keyword it is. */
12413 keyword = token->keyword;
12414 switch (keyword)
12416 case RID_BREAK:
12417 in_statement = parser->in_statement & ~IN_IF_STMT;
12418 switch (in_statement)
12420 case 0:
12421 error_at (token->location, "break statement not within loop or switch");
12422 break;
12423 default:
12424 gcc_assert ((in_statement & IN_SWITCH_STMT)
12425 || in_statement == IN_ITERATION_STMT);
12426 statement = finish_break_stmt ();
12427 if (in_statement == IN_ITERATION_STMT)
12428 break_maybe_infinite_loop ();
12429 break;
12430 case IN_OMP_BLOCK:
12431 error_at (token->location, "invalid exit from OpenMP structured block");
12432 break;
12433 case IN_OMP_FOR:
12434 error_at (token->location, "break statement used with OpenMP for loop");
12435 break;
12436 case IN_CILK_SIMD_FOR:
12437 error_at (token->location, "break statement used with Cilk Plus for loop");
12438 break;
12440 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12441 break;
12443 case RID_CONTINUE:
12444 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12446 case 0:
12447 error_at (token->location, "continue statement not within a loop");
12448 break;
12449 case IN_CILK_SIMD_FOR:
12450 error_at (token->location,
12451 "continue statement within %<#pragma simd%> loop body");
12452 /* Fall through. */
12453 case IN_ITERATION_STMT:
12454 case IN_OMP_FOR:
12455 statement = finish_continue_stmt ();
12456 break;
12457 case IN_OMP_BLOCK:
12458 error_at (token->location, "invalid exit from OpenMP structured block");
12459 break;
12460 default:
12461 gcc_unreachable ();
12463 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12464 break;
12466 case RID_RETURN:
12468 tree expr;
12469 bool expr_non_constant_p;
12471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12473 cp_lexer_set_source_position (parser->lexer);
12474 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12475 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12477 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12478 expr = cp_parser_expression (parser);
12479 else
12480 /* If the next token is a `;', then there is no
12481 expression. */
12482 expr = NULL_TREE;
12483 /* Build the return-statement. */
12484 if (current_function_auto_return_pattern && in_discarded_stmt)
12485 /* Don't deduce from a discarded return statement. */;
12486 else
12487 statement = finish_return_stmt (expr);
12488 /* Look for the final `;'. */
12489 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12491 break;
12493 case RID_GOTO:
12494 if (parser->in_function_body
12495 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12497 error ("%<goto%> in %<constexpr%> function");
12498 cp_function_chain->invalid_constexpr = true;
12501 /* Create the goto-statement. */
12502 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12504 /* Issue a warning about this use of a GNU extension. */
12505 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12506 /* Consume the '*' token. */
12507 cp_lexer_consume_token (parser->lexer);
12508 /* Parse the dependent expression. */
12509 finish_goto_stmt (cp_parser_expression (parser));
12511 else
12512 finish_goto_stmt (cp_parser_identifier (parser));
12513 /* Look for the final `;'. */
12514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12515 break;
12517 default:
12518 cp_parser_error (parser, "expected jump-statement");
12519 break;
12522 return statement;
12525 /* Parse a declaration-statement.
12527 declaration-statement:
12528 block-declaration */
12530 static void
12531 cp_parser_declaration_statement (cp_parser* parser)
12533 void *p;
12535 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12536 p = obstack_alloc (&declarator_obstack, 0);
12538 /* Parse the block-declaration. */
12539 cp_parser_block_declaration (parser, /*statement_p=*/true);
12541 /* Free any declarators allocated. */
12542 obstack_free (&declarator_obstack, p);
12545 /* Some dependent statements (like `if (cond) statement'), are
12546 implicitly in their own scope. In other words, if the statement is
12547 a single statement (as opposed to a compound-statement), it is
12548 none-the-less treated as if it were enclosed in braces. Any
12549 declarations appearing in the dependent statement are out of scope
12550 after control passes that point. This function parses a statement,
12551 but ensures that is in its own scope, even if it is not a
12552 compound-statement.
12554 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12555 is a (possibly labeled) if statement which is not enclosed in
12556 braces and has an else clause. This is used to implement
12557 -Wparentheses.
12559 CHAIN is a vector of if-else-if conditions. This is used to implement
12560 -Wduplicated-cond.
12562 Returns the new statement. */
12564 static tree
12565 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12566 const token_indent_info &guard_tinfo,
12567 vec<tree> *chain)
12569 tree statement;
12570 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12571 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12572 token_indent_info body_tinfo
12573 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12575 if (if_p != NULL)
12576 *if_p = false;
12578 /* Mark if () ; with a special NOP_EXPR. */
12579 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12581 cp_lexer_consume_token (parser->lexer);
12582 statement = add_stmt (build_empty_stmt (body_loc));
12584 if (guard_tinfo.keyword == RID_IF
12585 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12586 warning_at (body_loc, OPT_Wempty_body,
12587 "suggest braces around empty body in an %<if%> statement");
12588 else if (guard_tinfo.keyword == RID_ELSE)
12589 warning_at (body_loc, OPT_Wempty_body,
12590 "suggest braces around empty body in an %<else%> statement");
12592 /* if a compound is opened, we simply parse the statement directly. */
12593 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12594 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12595 /* If the token is not a `{', then we must take special action. */
12596 else
12598 /* Create a compound-statement. */
12599 statement = begin_compound_stmt (0);
12600 /* Parse the dependent-statement. */
12601 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12602 &body_loc_after_labels);
12603 /* Finish the dummy compound-statement. */
12604 finish_compound_stmt (statement);
12607 token_indent_info next_tinfo
12608 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12609 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12611 if (body_loc_after_labels != UNKNOWN_LOCATION
12612 && next_tinfo.type != CPP_SEMICOLON)
12613 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12614 guard_tinfo.location, guard_tinfo.keyword);
12616 /* Return the statement. */
12617 return statement;
12620 /* For some dependent statements (like `while (cond) statement'), we
12621 have already created a scope. Therefore, even if the dependent
12622 statement is a compound-statement, we do not want to create another
12623 scope. */
12625 static void
12626 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12627 const token_indent_info &guard_tinfo)
12629 /* If the token is a `{', then we must take special action. */
12630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12632 token_indent_info body_tinfo
12633 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12634 location_t loc_after_labels = UNKNOWN_LOCATION;
12636 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12637 &loc_after_labels);
12638 token_indent_info next_tinfo
12639 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12640 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12642 if (loc_after_labels != UNKNOWN_LOCATION
12643 && next_tinfo.type != CPP_SEMICOLON)
12644 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12645 guard_tinfo.location,
12646 guard_tinfo.keyword);
12648 else
12650 /* Avoid calling cp_parser_compound_statement, so that we
12651 don't create a new scope. Do everything else by hand. */
12652 matching_braces braces;
12653 braces.require_open (parser);
12654 /* If the next keyword is `__label__' we have a label declaration. */
12655 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12656 cp_parser_label_declaration (parser);
12657 /* Parse an (optional) statement-seq. */
12658 cp_parser_statement_seq_opt (parser, NULL_TREE);
12659 braces.require_close (parser);
12663 /* Declarations [gram.dcl.dcl] */
12665 /* Parse an optional declaration-sequence.
12667 declaration-seq:
12668 declaration
12669 declaration-seq declaration */
12671 static void
12672 cp_parser_declaration_seq_opt (cp_parser* parser)
12674 while (true)
12676 cp_token *token;
12678 token = cp_lexer_peek_token (parser->lexer);
12680 if (token->type == CPP_CLOSE_BRACE
12681 || token->type == CPP_EOF
12682 || token->type == CPP_PRAGMA_EOL)
12683 break;
12685 if (token->type == CPP_SEMICOLON)
12687 /* A declaration consisting of a single semicolon is
12688 invalid. Allow it unless we're being pedantic. */
12689 cp_lexer_consume_token (parser->lexer);
12690 if (!in_system_header_at (input_location))
12691 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12692 continue;
12695 /* If we're entering or exiting a region that's implicitly
12696 extern "C", modify the lang context appropriately. */
12697 if (!parser->implicit_extern_c && token->implicit_extern_c)
12699 push_lang_context (lang_name_c);
12700 parser->implicit_extern_c = true;
12702 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12704 pop_lang_context ();
12705 parser->implicit_extern_c = false;
12708 if (token->type == CPP_PRAGMA)
12710 /* A top-level declaration can consist solely of a #pragma.
12711 A nested declaration cannot, so this is done here and not
12712 in cp_parser_declaration. (A #pragma at block scope is
12713 handled in cp_parser_statement.) */
12714 cp_parser_pragma (parser, pragma_external, NULL);
12715 continue;
12718 /* Parse the declaration itself. */
12719 cp_parser_declaration (parser);
12723 /* Parse a declaration.
12725 declaration:
12726 block-declaration
12727 function-definition
12728 template-declaration
12729 explicit-instantiation
12730 explicit-specialization
12731 linkage-specification
12732 namespace-definition
12734 C++17:
12735 deduction-guide
12737 GNU extension:
12739 declaration:
12740 __extension__ declaration */
12742 static void
12743 cp_parser_declaration (cp_parser* parser)
12745 cp_token token1;
12746 cp_token token2;
12747 int saved_pedantic;
12748 void *p;
12749 tree attributes = NULL_TREE;
12751 /* Check for the `__extension__' keyword. */
12752 if (cp_parser_extension_opt (parser, &saved_pedantic))
12754 /* Parse the qualified declaration. */
12755 cp_parser_declaration (parser);
12756 /* Restore the PEDANTIC flag. */
12757 pedantic = saved_pedantic;
12759 return;
12762 /* Try to figure out what kind of declaration is present. */
12763 token1 = *cp_lexer_peek_token (parser->lexer);
12765 if (token1.type != CPP_EOF)
12766 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12767 else
12769 token2.type = CPP_EOF;
12770 token2.keyword = RID_MAX;
12773 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12774 p = obstack_alloc (&declarator_obstack, 0);
12776 /* If the next token is `extern' and the following token is a string
12777 literal, then we have a linkage specification. */
12778 if (token1.keyword == RID_EXTERN
12779 && cp_parser_is_pure_string_literal (&token2))
12780 cp_parser_linkage_specification (parser);
12781 /* If the next token is `template', then we have either a template
12782 declaration, an explicit instantiation, or an explicit
12783 specialization. */
12784 else if (token1.keyword == RID_TEMPLATE)
12786 /* `template <>' indicates a template specialization. */
12787 if (token2.type == CPP_LESS
12788 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12789 cp_parser_explicit_specialization (parser);
12790 /* `template <' indicates a template declaration. */
12791 else if (token2.type == CPP_LESS)
12792 cp_parser_template_declaration (parser, /*member_p=*/false);
12793 /* Anything else must be an explicit instantiation. */
12794 else
12795 cp_parser_explicit_instantiation (parser);
12797 /* If the next token is `export', then we have a template
12798 declaration. */
12799 else if (token1.keyword == RID_EXPORT)
12800 cp_parser_template_declaration (parser, /*member_p=*/false);
12801 /* If the next token is `extern', 'static' or 'inline' and the one
12802 after that is `template', we have a GNU extended explicit
12803 instantiation directive. */
12804 else if (cp_parser_allow_gnu_extensions_p (parser)
12805 && (token1.keyword == RID_EXTERN
12806 || token1.keyword == RID_STATIC
12807 || token1.keyword == RID_INLINE)
12808 && token2.keyword == RID_TEMPLATE)
12809 cp_parser_explicit_instantiation (parser);
12810 /* If the next token is `namespace', check for a named or unnamed
12811 namespace definition. */
12812 else if (token1.keyword == RID_NAMESPACE
12813 && (/* A named namespace definition. */
12814 (token2.type == CPP_NAME
12815 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12816 != CPP_EQ))
12817 || (token2.type == CPP_OPEN_SQUARE
12818 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12819 == CPP_OPEN_SQUARE)
12820 /* An unnamed namespace definition. */
12821 || token2.type == CPP_OPEN_BRACE
12822 || token2.keyword == RID_ATTRIBUTE))
12823 cp_parser_namespace_definition (parser);
12824 /* An inline (associated) namespace definition. */
12825 else if (token1.keyword == RID_INLINE
12826 && token2.keyword == RID_NAMESPACE)
12827 cp_parser_namespace_definition (parser);
12828 /* Objective-C++ declaration/definition. */
12829 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12830 cp_parser_objc_declaration (parser, NULL_TREE);
12831 else if (c_dialect_objc ()
12832 && token1.keyword == RID_ATTRIBUTE
12833 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12834 cp_parser_objc_declaration (parser, attributes);
12835 /* At this point we may have a template declared by a concept
12836 introduction. */
12837 else if (flag_concepts
12838 && cp_parser_template_declaration_after_export (parser,
12839 /*member_p=*/false))
12840 /* We did. */;
12841 else
12842 /* Try to parse a block-declaration, or a function-definition. */
12843 cp_parser_block_declaration (parser, /*statement_p=*/false);
12845 /* Free any declarators allocated. */
12846 obstack_free (&declarator_obstack, p);
12849 /* Parse a block-declaration.
12851 block-declaration:
12852 simple-declaration
12853 asm-definition
12854 namespace-alias-definition
12855 using-declaration
12856 using-directive
12858 GNU Extension:
12860 block-declaration:
12861 __extension__ block-declaration
12863 C++0x Extension:
12865 block-declaration:
12866 static_assert-declaration
12868 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12869 part of a declaration-statement. */
12871 static void
12872 cp_parser_block_declaration (cp_parser *parser,
12873 bool statement_p)
12875 cp_token *token1;
12876 int saved_pedantic;
12878 /* Check for the `__extension__' keyword. */
12879 if (cp_parser_extension_opt (parser, &saved_pedantic))
12881 /* Parse the qualified declaration. */
12882 cp_parser_block_declaration (parser, statement_p);
12883 /* Restore the PEDANTIC flag. */
12884 pedantic = saved_pedantic;
12886 return;
12889 /* Peek at the next token to figure out which kind of declaration is
12890 present. */
12891 token1 = cp_lexer_peek_token (parser->lexer);
12893 /* If the next keyword is `asm', we have an asm-definition. */
12894 if (token1->keyword == RID_ASM)
12896 if (statement_p)
12897 cp_parser_commit_to_tentative_parse (parser);
12898 cp_parser_asm_definition (parser);
12900 /* If the next keyword is `namespace', we have a
12901 namespace-alias-definition. */
12902 else if (token1->keyword == RID_NAMESPACE)
12903 cp_parser_namespace_alias_definition (parser);
12904 /* If the next keyword is `using', we have a
12905 using-declaration, a using-directive, or an alias-declaration. */
12906 else if (token1->keyword == RID_USING)
12908 cp_token *token2;
12910 if (statement_p)
12911 cp_parser_commit_to_tentative_parse (parser);
12912 /* If the token after `using' is `namespace', then we have a
12913 using-directive. */
12914 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12915 if (token2->keyword == RID_NAMESPACE)
12916 cp_parser_using_directive (parser);
12917 /* If the second token after 'using' is '=', then we have an
12918 alias-declaration. */
12919 else if (cxx_dialect >= cxx11
12920 && token2->type == CPP_NAME
12921 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12922 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12923 cp_parser_alias_declaration (parser);
12924 /* Otherwise, it's a using-declaration. */
12925 else
12926 cp_parser_using_declaration (parser,
12927 /*access_declaration_p=*/false);
12929 /* If the next keyword is `__label__' we have a misplaced label
12930 declaration. */
12931 else if (token1->keyword == RID_LABEL)
12933 cp_lexer_consume_token (parser->lexer);
12934 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12935 cp_parser_skip_to_end_of_statement (parser);
12936 /* If the next token is now a `;', consume it. */
12937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12938 cp_lexer_consume_token (parser->lexer);
12940 /* If the next token is `static_assert' we have a static assertion. */
12941 else if (token1->keyword == RID_STATIC_ASSERT)
12942 cp_parser_static_assert (parser, /*member_p=*/false);
12943 /* Anything else must be a simple-declaration. */
12944 else
12945 cp_parser_simple_declaration (parser, !statement_p,
12946 /*maybe_range_for_decl*/NULL);
12949 /* Parse a simple-declaration.
12951 simple-declaration:
12952 decl-specifier-seq [opt] init-declarator-list [opt] ;
12953 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12954 brace-or-equal-initializer ;
12956 init-declarator-list:
12957 init-declarator
12958 init-declarator-list , init-declarator
12960 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12961 function-definition as a simple-declaration.
12963 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12964 parsed declaration if it is an uninitialized single declarator not followed
12965 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12966 if present, will not be consumed. */
12968 static void
12969 cp_parser_simple_declaration (cp_parser* parser,
12970 bool function_definition_allowed_p,
12971 tree *maybe_range_for_decl)
12973 cp_decl_specifier_seq decl_specifiers;
12974 int declares_class_or_enum;
12975 bool saw_declarator;
12976 location_t comma_loc = UNKNOWN_LOCATION;
12977 location_t init_loc = UNKNOWN_LOCATION;
12979 if (maybe_range_for_decl)
12980 *maybe_range_for_decl = NULL_TREE;
12982 /* Defer access checks until we know what is being declared; the
12983 checks for names appearing in the decl-specifier-seq should be
12984 done as if we were in the scope of the thing being declared. */
12985 push_deferring_access_checks (dk_deferred);
12987 /* Parse the decl-specifier-seq. We have to keep track of whether
12988 or not the decl-specifier-seq declares a named class or
12989 enumeration type, since that is the only case in which the
12990 init-declarator-list is allowed to be empty.
12992 [dcl.dcl]
12994 In a simple-declaration, the optional init-declarator-list can be
12995 omitted only when declaring a class or enumeration, that is when
12996 the decl-specifier-seq contains either a class-specifier, an
12997 elaborated-type-specifier, or an enum-specifier. */
12998 cp_parser_decl_specifier_seq (parser,
12999 CP_PARSER_FLAGS_OPTIONAL,
13000 &decl_specifiers,
13001 &declares_class_or_enum);
13002 /* We no longer need to defer access checks. */
13003 stop_deferring_access_checks ();
13005 /* In a block scope, a valid declaration must always have a
13006 decl-specifier-seq. By not trying to parse declarators, we can
13007 resolve the declaration/expression ambiguity more quickly. */
13008 if (!function_definition_allowed_p
13009 && !decl_specifiers.any_specifiers_p)
13011 cp_parser_error (parser, "expected declaration");
13012 goto done;
13015 /* If the next two tokens are both identifiers, the code is
13016 erroneous. The usual cause of this situation is code like:
13018 T t;
13020 where "T" should name a type -- but does not. */
13021 if (!decl_specifiers.any_type_specifiers_p
13022 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13024 /* If parsing tentatively, we should commit; we really are
13025 looking at a declaration. */
13026 cp_parser_commit_to_tentative_parse (parser);
13027 /* Give up. */
13028 goto done;
13031 /* If we have seen at least one decl-specifier, and the next token
13032 is not a parenthesis, then we must be looking at a declaration.
13033 (After "int (" we might be looking at a functional cast.) */
13034 if (decl_specifiers.any_specifiers_p
13035 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13036 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13037 && !cp_parser_error_occurred (parser))
13038 cp_parser_commit_to_tentative_parse (parser);
13040 /* Look for C++17 decomposition declaration. */
13041 for (size_t n = 1; ; n++)
13042 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13043 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13044 continue;
13045 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13046 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13047 && decl_specifiers.any_specifiers_p)
13049 tree decl
13050 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13051 maybe_range_for_decl,
13052 &init_loc);
13054 /* The next token should be either a `,' or a `;'. */
13055 cp_token *token = cp_lexer_peek_token (parser->lexer);
13056 /* If it's a `;', we are done. */
13057 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
13058 goto finish;
13059 /* Anything else is an error. */
13060 else
13062 /* If we have already issued an error message we don't need
13063 to issue another one. */
13064 if ((decl != error_mark_node
13065 && DECL_INITIAL (decl) != error_mark_node)
13066 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13067 cp_parser_error (parser, "expected %<,%> or %<;%>");
13068 /* Skip tokens until we reach the end of the statement. */
13069 cp_parser_skip_to_end_of_statement (parser);
13070 /* If the next token is now a `;', consume it. */
13071 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13072 cp_lexer_consume_token (parser->lexer);
13073 goto done;
13076 else
13077 break;
13079 tree last_type;
13080 bool auto_specifier_p;
13081 /* NULL_TREE if both variable and function declaration are allowed,
13082 error_mark_node if function declaration are not allowed and
13083 a FUNCTION_DECL that should be diagnosed if it is followed by
13084 variable declarations. */
13085 tree auto_function_declaration;
13087 last_type = NULL_TREE;
13088 auto_specifier_p
13089 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13090 auto_function_declaration = NULL_TREE;
13092 /* Keep going until we hit the `;' at the end of the simple
13093 declaration. */
13094 saw_declarator = false;
13095 while (cp_lexer_next_token_is_not (parser->lexer,
13096 CPP_SEMICOLON))
13098 cp_token *token;
13099 bool function_definition_p;
13100 tree decl;
13101 tree auto_result = NULL_TREE;
13103 if (saw_declarator)
13105 /* If we are processing next declarator, comma is expected */
13106 token = cp_lexer_peek_token (parser->lexer);
13107 gcc_assert (token->type == CPP_COMMA);
13108 cp_lexer_consume_token (parser->lexer);
13109 if (maybe_range_for_decl)
13111 *maybe_range_for_decl = error_mark_node;
13112 if (comma_loc == UNKNOWN_LOCATION)
13113 comma_loc = token->location;
13116 else
13117 saw_declarator = true;
13119 /* Parse the init-declarator. */
13120 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13121 /*checks=*/NULL,
13122 function_definition_allowed_p,
13123 /*member_p=*/false,
13124 declares_class_or_enum,
13125 &function_definition_p,
13126 maybe_range_for_decl,
13127 &init_loc,
13128 &auto_result);
13129 /* If an error occurred while parsing tentatively, exit quickly.
13130 (That usually happens when in the body of a function; each
13131 statement is treated as a declaration-statement until proven
13132 otherwise.) */
13133 if (cp_parser_error_occurred (parser))
13134 goto done;
13136 if (auto_specifier_p && cxx_dialect >= cxx14)
13138 /* If the init-declarator-list contains more than one
13139 init-declarator, they shall all form declarations of
13140 variables. */
13141 if (auto_function_declaration == NULL_TREE)
13142 auto_function_declaration
13143 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13144 else if (TREE_CODE (decl) == FUNCTION_DECL
13145 || auto_function_declaration != error_mark_node)
13147 error_at (decl_specifiers.locations[ds_type_spec],
13148 "non-variable %qD in declaration with more than one "
13149 "declarator with placeholder type",
13150 TREE_CODE (decl) == FUNCTION_DECL
13151 ? decl : auto_function_declaration);
13152 auto_function_declaration = error_mark_node;
13156 if (auto_result
13157 && (!processing_template_decl || !type_uses_auto (auto_result)))
13159 if (last_type
13160 && last_type != error_mark_node
13161 && !same_type_p (auto_result, last_type))
13163 /* If the list of declarators contains more than one declarator,
13164 the type of each declared variable is determined as described
13165 above. If the type deduced for the template parameter U is not
13166 the same in each deduction, the program is ill-formed. */
13167 error_at (decl_specifiers.locations[ds_type_spec],
13168 "inconsistent deduction for %qT: %qT and then %qT",
13169 decl_specifiers.type, last_type, auto_result);
13170 last_type = error_mark_node;
13172 else
13173 last_type = auto_result;
13176 /* Handle function definitions specially. */
13177 if (function_definition_p)
13179 /* If the next token is a `,', then we are probably
13180 processing something like:
13182 void f() {}, *p;
13184 which is erroneous. */
13185 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13187 cp_token *token = cp_lexer_peek_token (parser->lexer);
13188 error_at (token->location,
13189 "mixing"
13190 " declarations and function-definitions is forbidden");
13192 /* Otherwise, we're done with the list of declarators. */
13193 else
13195 pop_deferring_access_checks ();
13196 return;
13199 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13200 *maybe_range_for_decl = decl;
13201 /* The next token should be either a `,' or a `;'. */
13202 token = cp_lexer_peek_token (parser->lexer);
13203 /* If it's a `,', there are more declarators to come. */
13204 if (token->type == CPP_COMMA)
13205 /* will be consumed next time around */;
13206 /* If it's a `;', we are done. */
13207 else if (token->type == CPP_SEMICOLON)
13208 break;
13209 else if (maybe_range_for_decl)
13211 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13212 permerror (decl_specifiers.locations[ds_type_spec],
13213 "types may not be defined in a for-range-declaration");
13214 break;
13216 /* Anything else is an error. */
13217 else
13219 /* If we have already issued an error message we don't need
13220 to issue another one. */
13221 if ((decl != error_mark_node
13222 && DECL_INITIAL (decl) != error_mark_node)
13223 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13224 cp_parser_error (parser, "expected %<,%> or %<;%>");
13225 /* Skip tokens until we reach the end of the statement. */
13226 cp_parser_skip_to_end_of_statement (parser);
13227 /* If the next token is now a `;', consume it. */
13228 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13229 cp_lexer_consume_token (parser->lexer);
13230 goto done;
13232 /* After the first time around, a function-definition is not
13233 allowed -- even if it was OK at first. For example:
13235 int i, f() {}
13237 is not valid. */
13238 function_definition_allowed_p = false;
13241 /* Issue an error message if no declarators are present, and the
13242 decl-specifier-seq does not itself declare a class or
13243 enumeration: [dcl.dcl]/3. */
13244 if (!saw_declarator)
13246 if (cp_parser_declares_only_class_p (parser))
13248 if (!declares_class_or_enum
13249 && decl_specifiers.type
13250 && OVERLOAD_TYPE_P (decl_specifiers.type))
13251 /* Ensure an error is issued anyway when finish_decltype_type,
13252 called via cp_parser_decl_specifier_seq, returns a class or
13253 an enumeration (c++/51786). */
13254 decl_specifiers.type = NULL_TREE;
13255 shadow_tag (&decl_specifiers);
13257 /* Perform any deferred access checks. */
13258 perform_deferred_access_checks (tf_warning_or_error);
13261 /* Consume the `;'. */
13262 finish:
13263 if (!maybe_range_for_decl)
13264 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13265 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13267 if (init_loc != UNKNOWN_LOCATION)
13268 error_at (init_loc, "initializer in range-based %<for%> loop");
13269 if (comma_loc != UNKNOWN_LOCATION)
13270 error_at (comma_loc,
13271 "multiple declarations in range-based %<for%> loop");
13274 done:
13275 pop_deferring_access_checks ();
13278 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13279 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13280 initializer ; */
13282 static tree
13283 cp_parser_decomposition_declaration (cp_parser *parser,
13284 cp_decl_specifier_seq *decl_specifiers,
13285 tree *maybe_range_for_decl,
13286 location_t *init_loc)
13288 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13290 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13292 /* Parse the identifier-list. */
13293 auto_vec<cp_expr, 10> v;
13294 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13295 while (true)
13297 cp_expr e = cp_parser_identifier (parser);
13298 if (e.get_value () == error_mark_node)
13299 break;
13300 v.safe_push (e);
13301 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13302 break;
13303 cp_lexer_consume_token (parser->lexer);
13306 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13307 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13309 end_loc = UNKNOWN_LOCATION;
13310 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13311 false);
13312 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13313 cp_lexer_consume_token (parser->lexer);
13314 else
13316 cp_parser_skip_to_end_of_statement (parser);
13317 return error_mark_node;
13321 if (cxx_dialect < cxx17)
13322 pedwarn (loc, 0, "structured bindings only available with "
13323 "-std=c++17 or -std=gnu++17");
13325 tree pushed_scope;
13326 cp_declarator *declarator = make_declarator (cdk_decomp);
13327 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13328 declarator->id_loc = loc;
13329 if (ref_qual != REF_QUAL_NONE)
13330 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13331 ref_qual == REF_QUAL_RVALUE,
13332 NULL_TREE);
13333 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13334 NULL_TREE, decl_specifiers->attributes,
13335 &pushed_scope);
13336 tree orig_decl = decl;
13338 unsigned int i;
13339 cp_expr e;
13340 cp_decl_specifier_seq decl_specs;
13341 clear_decl_specs (&decl_specs);
13342 decl_specs.type = make_auto ();
13343 tree prev = decl;
13344 FOR_EACH_VEC_ELT (v, i, e)
13346 if (i == 0)
13347 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13348 else
13349 declarator->u.id.unqualified_name = e.get_value ();
13350 declarator->id_loc = e.get_location ();
13351 tree elt_pushed_scope;
13352 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13353 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13354 if (decl2 == error_mark_node)
13355 decl = error_mark_node;
13356 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13358 /* Ensure we've diagnosed redeclaration if we aren't creating
13359 a new VAR_DECL. */
13360 gcc_assert (errorcount);
13361 decl = error_mark_node;
13363 else
13364 prev = decl2;
13365 if (elt_pushed_scope)
13366 pop_scope (elt_pushed_scope);
13369 if (v.is_empty ())
13371 error_at (loc, "empty structured binding declaration");
13372 decl = error_mark_node;
13375 if (maybe_range_for_decl == NULL
13376 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13378 bool non_constant_p = false, is_direct_init = false;
13379 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13380 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13381 &non_constant_p);
13382 if (initializer == NULL_TREE
13383 || (TREE_CODE (initializer) == TREE_LIST
13384 && TREE_CHAIN (initializer))
13385 || (TREE_CODE (initializer) == CONSTRUCTOR
13386 && CONSTRUCTOR_NELTS (initializer) != 1))
13388 error_at (loc, "invalid initializer for structured binding "
13389 "declaration");
13390 initializer = error_mark_node;
13393 if (decl != error_mark_node)
13395 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13396 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13397 cp_finish_decomp (decl, prev, v.length ());
13400 else if (decl != error_mark_node)
13402 *maybe_range_for_decl = prev;
13403 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13404 the underlying DECL. */
13405 cp_finish_decomp (decl, prev, v.length ());
13408 if (pushed_scope)
13409 pop_scope (pushed_scope);
13411 if (decl == error_mark_node && DECL_P (orig_decl))
13413 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13414 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13417 return decl;
13420 /* Parse a decl-specifier-seq.
13422 decl-specifier-seq:
13423 decl-specifier-seq [opt] decl-specifier
13424 decl-specifier attribute-specifier-seq [opt] (C++11)
13426 decl-specifier:
13427 storage-class-specifier
13428 type-specifier
13429 function-specifier
13430 friend
13431 typedef
13433 GNU Extension:
13435 decl-specifier:
13436 attributes
13438 Concepts Extension:
13440 decl-specifier:
13441 concept
13443 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13445 The parser flags FLAGS is used to control type-specifier parsing.
13447 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13448 flags:
13450 1: one of the decl-specifiers is an elaborated-type-specifier
13451 (i.e., a type declaration)
13452 2: one of the decl-specifiers is an enum-specifier or a
13453 class-specifier (i.e., a type definition)
13457 static void
13458 cp_parser_decl_specifier_seq (cp_parser* parser,
13459 cp_parser_flags flags,
13460 cp_decl_specifier_seq *decl_specs,
13461 int* declares_class_or_enum)
13463 bool constructor_possible_p = !parser->in_declarator_p;
13464 bool found_decl_spec = false;
13465 cp_token *start_token = NULL;
13466 cp_decl_spec ds;
13468 /* Clear DECL_SPECS. */
13469 clear_decl_specs (decl_specs);
13471 /* Assume no class or enumeration type is declared. */
13472 *declares_class_or_enum = 0;
13474 /* Keep reading specifiers until there are no more to read. */
13475 while (true)
13477 bool constructor_p;
13478 cp_token *token;
13479 ds = ds_last;
13481 /* Peek at the next token. */
13482 token = cp_lexer_peek_token (parser->lexer);
13484 /* Save the first token of the decl spec list for error
13485 reporting. */
13486 if (!start_token)
13487 start_token = token;
13488 /* Handle attributes. */
13489 if (cp_next_tokens_can_be_attribute_p (parser))
13491 /* Parse the attributes. */
13492 tree attrs = cp_parser_attributes_opt (parser);
13494 /* In a sequence of declaration specifiers, c++11 attributes
13495 appertain to the type that precede them. In that case
13496 [dcl.spec]/1 says:
13498 The attribute-specifier-seq affects the type only for
13499 the declaration it appears in, not other declarations
13500 involving the same type.
13502 But for now let's force the user to position the
13503 attribute either at the beginning of the declaration or
13504 after the declarator-id, which would clearly mean that it
13505 applies to the declarator. */
13506 if (cxx11_attribute_p (attrs))
13508 if (!found_decl_spec)
13509 /* The c++11 attribute is at the beginning of the
13510 declaration. It appertains to the entity being
13511 declared. */;
13512 else
13514 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13516 /* This is an attribute following a
13517 class-specifier. */
13518 if (decl_specs->type_definition_p)
13519 warn_misplaced_attr_for_class_type (token->location,
13520 decl_specs->type);
13521 attrs = NULL_TREE;
13523 else
13525 decl_specs->std_attributes
13526 = chainon (decl_specs->std_attributes,
13527 attrs);
13528 if (decl_specs->locations[ds_std_attribute] == 0)
13529 decl_specs->locations[ds_std_attribute] = token->location;
13531 continue;
13535 decl_specs->attributes
13536 = chainon (decl_specs->attributes,
13537 attrs);
13538 if (decl_specs->locations[ds_attribute] == 0)
13539 decl_specs->locations[ds_attribute] = token->location;
13540 continue;
13542 /* Assume we will find a decl-specifier keyword. */
13543 found_decl_spec = true;
13544 /* If the next token is an appropriate keyword, we can simply
13545 add it to the list. */
13546 switch (token->keyword)
13548 /* decl-specifier:
13549 friend
13550 constexpr */
13551 case RID_FRIEND:
13552 if (!at_class_scope_p ())
13554 gcc_rich_location richloc (token->location);
13555 richloc.add_fixit_remove ();
13556 error_at (&richloc, "%<friend%> used outside of class");
13557 cp_lexer_purge_token (parser->lexer);
13559 else
13561 ds = ds_friend;
13562 /* Consume the token. */
13563 cp_lexer_consume_token (parser->lexer);
13565 break;
13567 case RID_CONSTEXPR:
13568 ds = ds_constexpr;
13569 cp_lexer_consume_token (parser->lexer);
13570 break;
13572 case RID_CONCEPT:
13573 ds = ds_concept;
13574 cp_lexer_consume_token (parser->lexer);
13575 break;
13577 /* function-specifier:
13578 inline
13579 virtual
13580 explicit */
13581 case RID_INLINE:
13582 case RID_VIRTUAL:
13583 case RID_EXPLICIT:
13584 cp_parser_function_specifier_opt (parser, decl_specs);
13585 break;
13587 /* decl-specifier:
13588 typedef */
13589 case RID_TYPEDEF:
13590 ds = ds_typedef;
13591 /* Consume the token. */
13592 cp_lexer_consume_token (parser->lexer);
13593 /* A constructor declarator cannot appear in a typedef. */
13594 constructor_possible_p = false;
13595 /* The "typedef" keyword can only occur in a declaration; we
13596 may as well commit at this point. */
13597 cp_parser_commit_to_tentative_parse (parser);
13599 if (decl_specs->storage_class != sc_none)
13600 decl_specs->conflicting_specifiers_p = true;
13601 break;
13603 /* storage-class-specifier:
13604 auto
13605 register
13606 static
13607 extern
13608 mutable
13610 GNU Extension:
13611 thread */
13612 case RID_AUTO:
13613 if (cxx_dialect == cxx98)
13615 /* Consume the token. */
13616 cp_lexer_consume_token (parser->lexer);
13618 /* Complain about `auto' as a storage specifier, if
13619 we're complaining about C++0x compatibility. */
13620 gcc_rich_location richloc (token->location);
13621 richloc.add_fixit_remove ();
13622 warning_at (&richloc, OPT_Wc__11_compat,
13623 "%<auto%> changes meaning in C++11; "
13624 "please remove it");
13626 /* Set the storage class anyway. */
13627 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13628 token);
13630 else
13631 /* C++0x auto type-specifier. */
13632 found_decl_spec = false;
13633 break;
13635 case RID_REGISTER:
13636 case RID_STATIC:
13637 case RID_EXTERN:
13638 case RID_MUTABLE:
13639 /* Consume the token. */
13640 cp_lexer_consume_token (parser->lexer);
13641 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13642 token);
13643 break;
13644 case RID_THREAD:
13645 /* Consume the token. */
13646 ds = ds_thread;
13647 cp_lexer_consume_token (parser->lexer);
13648 break;
13650 default:
13651 /* We did not yet find a decl-specifier yet. */
13652 found_decl_spec = false;
13653 break;
13656 if (found_decl_spec
13657 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13658 && token->keyword != RID_CONSTEXPR)
13659 error ("decl-specifier invalid in condition");
13661 if (found_decl_spec
13662 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13663 && token->keyword != RID_MUTABLE
13664 && token->keyword != RID_CONSTEXPR)
13665 error_at (token->location, "%qD invalid in lambda",
13666 ridpointers[token->keyword]);
13668 if (ds != ds_last)
13669 set_and_check_decl_spec_loc (decl_specs, ds, token);
13671 /* Constructors are a special case. The `S' in `S()' is not a
13672 decl-specifier; it is the beginning of the declarator. */
13673 constructor_p
13674 = (!found_decl_spec
13675 && constructor_possible_p
13676 && (cp_parser_constructor_declarator_p
13677 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13679 /* If we don't have a DECL_SPEC yet, then we must be looking at
13680 a type-specifier. */
13681 if (!found_decl_spec && !constructor_p)
13683 int decl_spec_declares_class_or_enum;
13684 bool is_cv_qualifier;
13685 tree type_spec;
13687 type_spec
13688 = cp_parser_type_specifier (parser, flags,
13689 decl_specs,
13690 /*is_declaration=*/true,
13691 &decl_spec_declares_class_or_enum,
13692 &is_cv_qualifier);
13693 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13695 /* If this type-specifier referenced a user-defined type
13696 (a typedef, class-name, etc.), then we can't allow any
13697 more such type-specifiers henceforth.
13699 [dcl.spec]
13701 The longest sequence of decl-specifiers that could
13702 possibly be a type name is taken as the
13703 decl-specifier-seq of a declaration. The sequence shall
13704 be self-consistent as described below.
13706 [dcl.type]
13708 As a general rule, at most one type-specifier is allowed
13709 in the complete decl-specifier-seq of a declaration. The
13710 only exceptions are the following:
13712 -- const or volatile can be combined with any other
13713 type-specifier.
13715 -- signed or unsigned can be combined with char, long,
13716 short, or int.
13718 -- ..
13720 Example:
13722 typedef char* Pc;
13723 void g (const int Pc);
13725 Here, Pc is *not* part of the decl-specifier seq; it's
13726 the declarator. Therefore, once we see a type-specifier
13727 (other than a cv-qualifier), we forbid any additional
13728 user-defined types. We *do* still allow things like `int
13729 int' to be considered a decl-specifier-seq, and issue the
13730 error message later. */
13731 if (type_spec && !is_cv_qualifier)
13732 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13733 /* A constructor declarator cannot follow a type-specifier. */
13734 if (type_spec)
13736 constructor_possible_p = false;
13737 found_decl_spec = true;
13738 if (!is_cv_qualifier)
13739 decl_specs->any_type_specifiers_p = true;
13743 /* If we still do not have a DECL_SPEC, then there are no more
13744 decl-specifiers. */
13745 if (!found_decl_spec)
13746 break;
13748 decl_specs->any_specifiers_p = true;
13749 /* After we see one decl-specifier, further decl-specifiers are
13750 always optional. */
13751 flags |= CP_PARSER_FLAGS_OPTIONAL;
13754 /* Don't allow a friend specifier with a class definition. */
13755 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13756 && (*declares_class_or_enum & 2))
13757 error_at (decl_specs->locations[ds_friend],
13758 "class definition may not be declared a friend");
13761 /* Parse an (optional) storage-class-specifier.
13763 storage-class-specifier:
13764 auto
13765 register
13766 static
13767 extern
13768 mutable
13770 GNU Extension:
13772 storage-class-specifier:
13773 thread
13775 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13777 static tree
13778 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13780 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13782 case RID_AUTO:
13783 if (cxx_dialect != cxx98)
13784 return NULL_TREE;
13785 /* Fall through for C++98. */
13786 gcc_fallthrough ();
13788 case RID_REGISTER:
13789 case RID_STATIC:
13790 case RID_EXTERN:
13791 case RID_MUTABLE:
13792 case RID_THREAD:
13793 /* Consume the token. */
13794 return cp_lexer_consume_token (parser->lexer)->u.value;
13796 default:
13797 return NULL_TREE;
13801 /* Parse an (optional) function-specifier.
13803 function-specifier:
13804 inline
13805 virtual
13806 explicit
13808 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13809 Updates DECL_SPECS, if it is non-NULL. */
13811 static tree
13812 cp_parser_function_specifier_opt (cp_parser* parser,
13813 cp_decl_specifier_seq *decl_specs)
13815 cp_token *token = cp_lexer_peek_token (parser->lexer);
13816 switch (token->keyword)
13818 case RID_INLINE:
13819 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13820 break;
13822 case RID_VIRTUAL:
13823 /* 14.5.2.3 [temp.mem]
13825 A member function template shall not be virtual. */
13826 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13827 && current_class_type)
13828 error_at (token->location, "templates may not be %<virtual%>");
13829 else
13830 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13831 break;
13833 case RID_EXPLICIT:
13834 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13835 break;
13837 default:
13838 return NULL_TREE;
13841 /* Consume the token. */
13842 return cp_lexer_consume_token (parser->lexer)->u.value;
13845 /* Parse a linkage-specification.
13847 linkage-specification:
13848 extern string-literal { declaration-seq [opt] }
13849 extern string-literal declaration */
13851 static void
13852 cp_parser_linkage_specification (cp_parser* parser)
13854 tree linkage;
13856 /* Look for the `extern' keyword. */
13857 cp_token *extern_token
13858 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13860 /* Look for the string-literal. */
13861 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13862 linkage = cp_parser_string_literal (parser, false, false);
13864 /* Transform the literal into an identifier. If the literal is a
13865 wide-character string, or contains embedded NULs, then we can't
13866 handle it as the user wants. */
13867 if (strlen (TREE_STRING_POINTER (linkage))
13868 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13870 cp_parser_error (parser, "invalid linkage-specification");
13871 /* Assume C++ linkage. */
13872 linkage = lang_name_cplusplus;
13874 else
13875 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13877 /* We're now using the new linkage. */
13878 push_lang_context (linkage);
13880 /* Preserve the location of the the innermost linkage specification,
13881 tracking the locations of nested specifications via a local. */
13882 location_t saved_location
13883 = parser->innermost_linkage_specification_location;
13884 /* Construct a location ranging from the start of the "extern" to
13885 the end of the string-literal, with the caret at the start, e.g.:
13886 extern "C" {
13887 ^~~~~~~~~~
13889 parser->innermost_linkage_specification_location
13890 = make_location (extern_token->location,
13891 extern_token->location,
13892 get_finish (string_token->location));
13894 /* If the next token is a `{', then we're using the first
13895 production. */
13896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13898 cp_ensure_no_omp_declare_simd (parser);
13899 cp_ensure_no_oacc_routine (parser);
13901 /* Consume the `{' token. */
13902 matching_braces braces;
13903 braces.consume_open (parser)->location;
13904 /* Parse the declarations. */
13905 cp_parser_declaration_seq_opt (parser);
13906 /* Look for the closing `}'. */
13907 braces.require_close (parser);
13909 /* Otherwise, there's just one declaration. */
13910 else
13912 bool saved_in_unbraced_linkage_specification_p;
13914 saved_in_unbraced_linkage_specification_p
13915 = parser->in_unbraced_linkage_specification_p;
13916 parser->in_unbraced_linkage_specification_p = true;
13917 cp_parser_declaration (parser);
13918 parser->in_unbraced_linkage_specification_p
13919 = saved_in_unbraced_linkage_specification_p;
13922 /* We're done with the linkage-specification. */
13923 pop_lang_context ();
13925 /* Restore location of parent linkage specification, if any. */
13926 parser->innermost_linkage_specification_location = saved_location;
13929 /* Parse a static_assert-declaration.
13931 static_assert-declaration:
13932 static_assert ( constant-expression , string-literal ) ;
13933 static_assert ( constant-expression ) ; (C++17)
13935 If MEMBER_P, this static_assert is a class member. */
13937 static void
13938 cp_parser_static_assert(cp_parser *parser, bool member_p)
13940 tree condition;
13941 tree message;
13942 cp_token *token;
13943 location_t saved_loc;
13944 bool dummy;
13946 /* Peek at the `static_assert' token so we can keep track of exactly
13947 where the static assertion started. */
13948 token = cp_lexer_peek_token (parser->lexer);
13949 saved_loc = token->location;
13951 /* Look for the `static_assert' keyword. */
13952 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13953 RT_STATIC_ASSERT))
13954 return;
13956 /* We know we are in a static assertion; commit to any tentative
13957 parse. */
13958 if (cp_parser_parsing_tentatively (parser))
13959 cp_parser_commit_to_tentative_parse (parser);
13961 /* Parse the `(' starting the static assertion condition. */
13962 matching_parens parens;
13963 parens.require_open (parser);
13965 /* Parse the constant-expression. Allow a non-constant expression
13966 here in order to give better diagnostics in finish_static_assert. */
13967 condition =
13968 cp_parser_constant_expression (parser,
13969 /*allow_non_constant_p=*/true,
13970 /*non_constant_p=*/&dummy);
13972 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13974 if (cxx_dialect < cxx17)
13975 pedwarn (input_location, OPT_Wpedantic,
13976 "static_assert without a message "
13977 "only available with -std=c++17 or -std=gnu++17");
13978 /* Eat the ')' */
13979 cp_lexer_consume_token (parser->lexer);
13980 message = build_string (1, "");
13981 TREE_TYPE (message) = char_array_type_node;
13982 fix_string_type (message);
13984 else
13986 /* Parse the separating `,'. */
13987 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13989 /* Parse the string-literal message. */
13990 message = cp_parser_string_literal (parser,
13991 /*translate=*/false,
13992 /*wide_ok=*/true);
13994 /* A `)' completes the static assertion. */
13995 if (!parens.require_close (parser))
13996 cp_parser_skip_to_closing_parenthesis (parser,
13997 /*recovering=*/true,
13998 /*or_comma=*/false,
13999 /*consume_paren=*/true);
14002 /* A semicolon terminates the declaration. */
14003 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14005 /* Complete the static assertion, which may mean either processing
14006 the static assert now or saving it for template instantiation. */
14007 finish_static_assert (condition, message, saved_loc, member_p);
14010 /* Parse the expression in decltype ( expression ). */
14012 static tree
14013 cp_parser_decltype_expr (cp_parser *parser,
14014 bool &id_expression_or_member_access_p)
14016 cp_token *id_expr_start_token;
14017 tree expr;
14019 /* Since we're going to preserve any side-effects from this parse, set up a
14020 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14021 in the expression. */
14022 tentative_firewall firewall (parser);
14024 /* First, try parsing an id-expression. */
14025 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14026 cp_parser_parse_tentatively (parser);
14027 expr = cp_parser_id_expression (parser,
14028 /*template_keyword_p=*/false,
14029 /*check_dependency_p=*/true,
14030 /*template_p=*/NULL,
14031 /*declarator_p=*/false,
14032 /*optional_p=*/false);
14034 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14036 bool non_integral_constant_expression_p = false;
14037 tree id_expression = expr;
14038 cp_id_kind idk;
14039 const char *error_msg;
14041 if (identifier_p (expr))
14042 /* Lookup the name we got back from the id-expression. */
14043 expr = cp_parser_lookup_name_simple (parser, expr,
14044 id_expr_start_token->location);
14046 if (expr
14047 && expr != error_mark_node
14048 && TREE_CODE (expr) != TYPE_DECL
14049 && (TREE_CODE (expr) != BIT_NOT_EXPR
14050 || !TYPE_P (TREE_OPERAND (expr, 0)))
14051 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14053 /* Complete lookup of the id-expression. */
14054 expr = (finish_id_expression
14055 (id_expression, expr, parser->scope, &idk,
14056 /*integral_constant_expression_p=*/false,
14057 /*allow_non_integral_constant_expression_p=*/true,
14058 &non_integral_constant_expression_p,
14059 /*template_p=*/false,
14060 /*done=*/true,
14061 /*address_p=*/false,
14062 /*template_arg_p=*/false,
14063 &error_msg,
14064 id_expr_start_token->location));
14066 if (expr == error_mark_node)
14067 /* We found an id-expression, but it was something that we
14068 should not have found. This is an error, not something
14069 we can recover from, so note that we found an
14070 id-expression and we'll recover as gracefully as
14071 possible. */
14072 id_expression_or_member_access_p = true;
14075 if (expr
14076 && expr != error_mark_node
14077 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14078 /* We have an id-expression. */
14079 id_expression_or_member_access_p = true;
14082 if (!id_expression_or_member_access_p)
14084 /* Abort the id-expression parse. */
14085 cp_parser_abort_tentative_parse (parser);
14087 /* Parsing tentatively, again. */
14088 cp_parser_parse_tentatively (parser);
14090 /* Parse a class member access. */
14091 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14092 /*cast_p=*/false, /*decltype*/true,
14093 /*member_access_only_p=*/true, NULL);
14095 if (expr
14096 && expr != error_mark_node
14097 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14098 /* We have an id-expression. */
14099 id_expression_or_member_access_p = true;
14102 if (id_expression_or_member_access_p)
14103 /* We have parsed the complete id-expression or member access. */
14104 cp_parser_parse_definitely (parser);
14105 else
14107 /* Abort our attempt to parse an id-expression or member access
14108 expression. */
14109 cp_parser_abort_tentative_parse (parser);
14111 /* Parse a full expression. */
14112 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14113 /*decltype_p=*/true);
14116 return expr;
14119 /* Parse a `decltype' type. Returns the type.
14121 simple-type-specifier:
14122 decltype ( expression )
14123 C++14 proposal:
14124 decltype ( auto ) */
14126 static tree
14127 cp_parser_decltype (cp_parser *parser)
14129 tree expr;
14130 bool id_expression_or_member_access_p = false;
14131 const char *saved_message;
14132 bool saved_integral_constant_expression_p;
14133 bool saved_non_integral_constant_expression_p;
14134 bool saved_greater_than_is_operator_p;
14135 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14137 if (start_token->type == CPP_DECLTYPE)
14139 /* Already parsed. */
14140 cp_lexer_consume_token (parser->lexer);
14141 return saved_checks_value (start_token->u.tree_check_value);
14144 /* Look for the `decltype' token. */
14145 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14146 return error_mark_node;
14148 /* Parse the opening `('. */
14149 matching_parens parens;
14150 if (!parens.require_open (parser))
14151 return error_mark_node;
14153 /* decltype (auto) */
14154 if (cxx_dialect >= cxx14
14155 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14157 cp_lexer_consume_token (parser->lexer);
14158 if (!parens.require_close (parser))
14159 return error_mark_node;
14160 expr = make_decltype_auto ();
14161 AUTO_IS_DECLTYPE (expr) = true;
14162 goto rewrite;
14165 /* Types cannot be defined in a `decltype' expression. Save away the
14166 old message. */
14167 saved_message = parser->type_definition_forbidden_message;
14169 /* And create the new one. */
14170 parser->type_definition_forbidden_message
14171 = G_("types may not be defined in %<decltype%> expressions");
14173 /* The restrictions on constant-expressions do not apply inside
14174 decltype expressions. */
14175 saved_integral_constant_expression_p
14176 = parser->integral_constant_expression_p;
14177 saved_non_integral_constant_expression_p
14178 = parser->non_integral_constant_expression_p;
14179 parser->integral_constant_expression_p = false;
14181 /* Within a parenthesized expression, a `>' token is always
14182 the greater-than operator. */
14183 saved_greater_than_is_operator_p
14184 = parser->greater_than_is_operator_p;
14185 parser->greater_than_is_operator_p = true;
14187 /* Do not actually evaluate the expression. */
14188 ++cp_unevaluated_operand;
14190 /* Do not warn about problems with the expression. */
14191 ++c_inhibit_evaluation_warnings;
14193 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14195 /* Go back to evaluating expressions. */
14196 --cp_unevaluated_operand;
14197 --c_inhibit_evaluation_warnings;
14199 /* The `>' token might be the end of a template-id or
14200 template-parameter-list now. */
14201 parser->greater_than_is_operator_p
14202 = saved_greater_than_is_operator_p;
14204 /* Restore the old message and the integral constant expression
14205 flags. */
14206 parser->type_definition_forbidden_message = saved_message;
14207 parser->integral_constant_expression_p
14208 = saved_integral_constant_expression_p;
14209 parser->non_integral_constant_expression_p
14210 = saved_non_integral_constant_expression_p;
14212 /* Parse to the closing `)'. */
14213 if (!parens.require_close (parser))
14215 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14216 /*consume_paren=*/true);
14217 return error_mark_node;
14220 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14221 tf_warning_or_error);
14223 rewrite:
14224 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14225 it again. */
14226 start_token->type = CPP_DECLTYPE;
14227 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14228 start_token->u.tree_check_value->value = expr;
14229 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14230 start_token->keyword = RID_MAX;
14231 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14233 return expr;
14236 /* Special member functions [gram.special] */
14238 /* Parse a conversion-function-id.
14240 conversion-function-id:
14241 operator conversion-type-id
14243 Returns an IDENTIFIER_NODE representing the operator. */
14245 static tree
14246 cp_parser_conversion_function_id (cp_parser* parser)
14248 tree type;
14249 tree saved_scope;
14250 tree saved_qualifying_scope;
14251 tree saved_object_scope;
14252 tree pushed_scope = NULL_TREE;
14254 /* Look for the `operator' token. */
14255 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14256 return error_mark_node;
14257 /* When we parse the conversion-type-id, the current scope will be
14258 reset. However, we need that information in able to look up the
14259 conversion function later, so we save it here. */
14260 saved_scope = parser->scope;
14261 saved_qualifying_scope = parser->qualifying_scope;
14262 saved_object_scope = parser->object_scope;
14263 /* We must enter the scope of the class so that the names of
14264 entities declared within the class are available in the
14265 conversion-type-id. For example, consider:
14267 struct S {
14268 typedef int I;
14269 operator I();
14272 S::operator I() { ... }
14274 In order to see that `I' is a type-name in the definition, we
14275 must be in the scope of `S'. */
14276 if (saved_scope)
14277 pushed_scope = push_scope (saved_scope);
14278 /* Parse the conversion-type-id. */
14279 type = cp_parser_conversion_type_id (parser);
14280 /* Leave the scope of the class, if any. */
14281 if (pushed_scope)
14282 pop_scope (pushed_scope);
14283 /* Restore the saved scope. */
14284 parser->scope = saved_scope;
14285 parser->qualifying_scope = saved_qualifying_scope;
14286 parser->object_scope = saved_object_scope;
14287 /* If the TYPE is invalid, indicate failure. */
14288 if (type == error_mark_node)
14289 return error_mark_node;
14290 return make_conv_op_name (type);
14293 /* Parse a conversion-type-id:
14295 conversion-type-id:
14296 type-specifier-seq conversion-declarator [opt]
14298 Returns the TYPE specified. */
14300 static tree
14301 cp_parser_conversion_type_id (cp_parser* parser)
14303 tree attributes;
14304 cp_decl_specifier_seq type_specifiers;
14305 cp_declarator *declarator;
14306 tree type_specified;
14307 const char *saved_message;
14309 /* Parse the attributes. */
14310 attributes = cp_parser_attributes_opt (parser);
14312 saved_message = parser->type_definition_forbidden_message;
14313 parser->type_definition_forbidden_message
14314 = G_("types may not be defined in a conversion-type-id");
14316 /* Parse the type-specifiers. */
14317 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14318 /*is_trailing_return=*/false,
14319 &type_specifiers);
14321 parser->type_definition_forbidden_message = saved_message;
14323 /* If that didn't work, stop. */
14324 if (type_specifiers.type == error_mark_node)
14325 return error_mark_node;
14326 /* Parse the conversion-declarator. */
14327 declarator = cp_parser_conversion_declarator_opt (parser);
14329 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14330 /*initialized=*/0, &attributes);
14331 if (attributes)
14332 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14334 /* Don't give this error when parsing tentatively. This happens to
14335 work because we always parse this definitively once. */
14336 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14337 && type_uses_auto (type_specified))
14339 if (cxx_dialect < cxx14)
14341 error ("invalid use of %<auto%> in conversion operator");
14342 return error_mark_node;
14344 else if (template_parm_scope_p ())
14345 warning (0, "use of %<auto%> in member template "
14346 "conversion operator can never be deduced");
14349 return type_specified;
14352 /* Parse an (optional) conversion-declarator.
14354 conversion-declarator:
14355 ptr-operator conversion-declarator [opt]
14359 static cp_declarator *
14360 cp_parser_conversion_declarator_opt (cp_parser* parser)
14362 enum tree_code code;
14363 tree class_type, std_attributes = NULL_TREE;
14364 cp_cv_quals cv_quals;
14366 /* We don't know if there's a ptr-operator next, or not. */
14367 cp_parser_parse_tentatively (parser);
14368 /* Try the ptr-operator. */
14369 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14370 &std_attributes);
14371 /* If it worked, look for more conversion-declarators. */
14372 if (cp_parser_parse_definitely (parser))
14374 cp_declarator *declarator;
14376 /* Parse another optional declarator. */
14377 declarator = cp_parser_conversion_declarator_opt (parser);
14379 declarator = cp_parser_make_indirect_declarator
14380 (code, class_type, cv_quals, declarator, std_attributes);
14382 return declarator;
14385 return NULL;
14388 /* Parse an (optional) ctor-initializer.
14390 ctor-initializer:
14391 : mem-initializer-list */
14393 static void
14394 cp_parser_ctor_initializer_opt (cp_parser* parser)
14396 /* If the next token is not a `:', then there is no
14397 ctor-initializer. */
14398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14400 /* Do default initialization of any bases and members. */
14401 if (DECL_CONSTRUCTOR_P (current_function_decl))
14402 finish_mem_initializers (NULL_TREE);
14403 return;
14406 /* Consume the `:' token. */
14407 cp_lexer_consume_token (parser->lexer);
14408 /* And the mem-initializer-list. */
14409 cp_parser_mem_initializer_list (parser);
14412 /* Parse a mem-initializer-list.
14414 mem-initializer-list:
14415 mem-initializer ... [opt]
14416 mem-initializer ... [opt] , mem-initializer-list */
14418 static void
14419 cp_parser_mem_initializer_list (cp_parser* parser)
14421 tree mem_initializer_list = NULL_TREE;
14422 tree target_ctor = error_mark_node;
14423 cp_token *token = cp_lexer_peek_token (parser->lexer);
14425 /* Let the semantic analysis code know that we are starting the
14426 mem-initializer-list. */
14427 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14428 error_at (token->location,
14429 "only constructors take member initializers");
14431 /* Loop through the list. */
14432 while (true)
14434 tree mem_initializer;
14436 token = cp_lexer_peek_token (parser->lexer);
14437 /* Parse the mem-initializer. */
14438 mem_initializer = cp_parser_mem_initializer (parser);
14439 /* If the next token is a `...', we're expanding member initializers. */
14440 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14442 /* Consume the `...'. */
14443 cp_lexer_consume_token (parser->lexer);
14445 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14446 can be expanded but members cannot. */
14447 if (mem_initializer != error_mark_node
14448 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14450 error_at (token->location,
14451 "cannot expand initializer for member %qD",
14452 TREE_PURPOSE (mem_initializer));
14453 mem_initializer = error_mark_node;
14456 /* Construct the pack expansion type. */
14457 if (mem_initializer != error_mark_node)
14458 mem_initializer = make_pack_expansion (mem_initializer);
14460 if (target_ctor != error_mark_node
14461 && mem_initializer != error_mark_node)
14463 error ("mem-initializer for %qD follows constructor delegation",
14464 TREE_PURPOSE (mem_initializer));
14465 mem_initializer = error_mark_node;
14467 /* Look for a target constructor. */
14468 if (mem_initializer != error_mark_node
14469 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14470 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14472 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14473 if (mem_initializer_list)
14475 error ("constructor delegation follows mem-initializer for %qD",
14476 TREE_PURPOSE (mem_initializer_list));
14477 mem_initializer = error_mark_node;
14479 target_ctor = mem_initializer;
14481 /* Add it to the list, unless it was erroneous. */
14482 if (mem_initializer != error_mark_node)
14484 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14485 mem_initializer_list = mem_initializer;
14487 /* If the next token is not a `,', we're done. */
14488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14489 break;
14490 /* Consume the `,' token. */
14491 cp_lexer_consume_token (parser->lexer);
14494 /* Perform semantic analysis. */
14495 if (DECL_CONSTRUCTOR_P (current_function_decl))
14496 finish_mem_initializers (mem_initializer_list);
14499 /* Parse a mem-initializer.
14501 mem-initializer:
14502 mem-initializer-id ( expression-list [opt] )
14503 mem-initializer-id braced-init-list
14505 GNU extension:
14507 mem-initializer:
14508 ( expression-list [opt] )
14510 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14511 class) or FIELD_DECL (for a non-static data member) to initialize;
14512 the TREE_VALUE is the expression-list. An empty initialization
14513 list is represented by void_list_node. */
14515 static tree
14516 cp_parser_mem_initializer (cp_parser* parser)
14518 tree mem_initializer_id;
14519 tree expression_list;
14520 tree member;
14521 cp_token *token = cp_lexer_peek_token (parser->lexer);
14523 /* Find out what is being initialized. */
14524 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14526 permerror (token->location,
14527 "anachronistic old-style base class initializer");
14528 mem_initializer_id = NULL_TREE;
14530 else
14532 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14533 if (mem_initializer_id == error_mark_node)
14534 return mem_initializer_id;
14536 member = expand_member_init (mem_initializer_id);
14537 if (member && !DECL_P (member))
14538 in_base_initializer = 1;
14540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14542 bool expr_non_constant_p;
14543 cp_lexer_set_source_position (parser->lexer);
14544 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14545 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14546 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14547 expression_list = build_tree_list (NULL_TREE, expression_list);
14549 else
14551 vec<tree, va_gc> *vec;
14552 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14553 /*cast_p=*/false,
14554 /*allow_expansion_p=*/true,
14555 /*non_constant_p=*/NULL);
14556 if (vec == NULL)
14557 return error_mark_node;
14558 expression_list = build_tree_list_vec (vec);
14559 release_tree_vector (vec);
14562 if (expression_list == error_mark_node)
14563 return error_mark_node;
14564 if (!expression_list)
14565 expression_list = void_type_node;
14567 in_base_initializer = 0;
14569 return member ? build_tree_list (member, expression_list) : error_mark_node;
14572 /* Parse a mem-initializer-id.
14574 mem-initializer-id:
14575 :: [opt] nested-name-specifier [opt] class-name
14576 decltype-specifier (C++11)
14577 identifier
14579 Returns a TYPE indicating the class to be initialized for the first
14580 production (and the second in C++11). Returns an IDENTIFIER_NODE
14581 indicating the data member to be initialized for the last production. */
14583 static tree
14584 cp_parser_mem_initializer_id (cp_parser* parser)
14586 bool global_scope_p;
14587 bool nested_name_specifier_p;
14588 bool template_p = false;
14589 tree id;
14591 cp_token *token = cp_lexer_peek_token (parser->lexer);
14593 /* `typename' is not allowed in this context ([temp.res]). */
14594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14596 error_at (token->location,
14597 "keyword %<typename%> not allowed in this context (a qualified "
14598 "member initializer is implicitly a type)");
14599 cp_lexer_consume_token (parser->lexer);
14601 /* Look for the optional `::' operator. */
14602 global_scope_p
14603 = (cp_parser_global_scope_opt (parser,
14604 /*current_scope_valid_p=*/false)
14605 != NULL_TREE);
14606 /* Look for the optional nested-name-specifier. The simplest way to
14607 implement:
14609 [temp.res]
14611 The keyword `typename' is not permitted in a base-specifier or
14612 mem-initializer; in these contexts a qualified name that
14613 depends on a template-parameter is implicitly assumed to be a
14614 type name.
14616 is to assume that we have seen the `typename' keyword at this
14617 point. */
14618 nested_name_specifier_p
14619 = (cp_parser_nested_name_specifier_opt (parser,
14620 /*typename_keyword_p=*/true,
14621 /*check_dependency_p=*/true,
14622 /*type_p=*/true,
14623 /*is_declaration=*/true)
14624 != NULL_TREE);
14625 if (nested_name_specifier_p)
14626 template_p = cp_parser_optional_template_keyword (parser);
14627 /* If there is a `::' operator or a nested-name-specifier, then we
14628 are definitely looking for a class-name. */
14629 if (global_scope_p || nested_name_specifier_p)
14630 return cp_parser_class_name (parser,
14631 /*typename_keyword_p=*/true,
14632 /*template_keyword_p=*/template_p,
14633 typename_type,
14634 /*check_dependency_p=*/true,
14635 /*class_head_p=*/false,
14636 /*is_declaration=*/true);
14637 /* Otherwise, we could also be looking for an ordinary identifier. */
14638 cp_parser_parse_tentatively (parser);
14639 if (cp_lexer_next_token_is_decltype (parser->lexer))
14640 /* Try a decltype-specifier. */
14641 id = cp_parser_decltype (parser);
14642 else
14643 /* Otherwise, try a class-name. */
14644 id = cp_parser_class_name (parser,
14645 /*typename_keyword_p=*/true,
14646 /*template_keyword_p=*/false,
14647 none_type,
14648 /*check_dependency_p=*/true,
14649 /*class_head_p=*/false,
14650 /*is_declaration=*/true);
14651 /* If we found one, we're done. */
14652 if (cp_parser_parse_definitely (parser))
14653 return id;
14654 /* Otherwise, look for an ordinary identifier. */
14655 return cp_parser_identifier (parser);
14658 /* Overloading [gram.over] */
14660 /* Parse an operator-function-id.
14662 operator-function-id:
14663 operator operator
14665 Returns an IDENTIFIER_NODE for the operator which is a
14666 human-readable spelling of the identifier, e.g., `operator +'. */
14668 static cp_expr
14669 cp_parser_operator_function_id (cp_parser* parser)
14671 /* Look for the `operator' keyword. */
14672 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14673 return error_mark_node;
14674 /* And then the name of the operator itself. */
14675 return cp_parser_operator (parser);
14678 /* Return an identifier node for a user-defined literal operator.
14679 The suffix identifier is chained to the operator name identifier. */
14681 tree
14682 cp_literal_operator_id (const char* name)
14684 tree identifier;
14685 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14686 + strlen (name) + 10);
14687 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14688 identifier = get_identifier (buffer);
14690 return identifier;
14693 /* Parse an operator.
14695 operator:
14696 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14697 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14698 || ++ -- , ->* -> () []
14700 GNU Extensions:
14702 operator:
14703 <? >? <?= >?=
14705 Returns an IDENTIFIER_NODE for the operator which is a
14706 human-readable spelling of the identifier, e.g., `operator +'. */
14708 static cp_expr
14709 cp_parser_operator (cp_parser* parser)
14711 tree id = NULL_TREE;
14712 cp_token *token;
14713 bool utf8 = false;
14715 /* Peek at the next token. */
14716 token = cp_lexer_peek_token (parser->lexer);
14718 location_t start_loc = token->location;
14720 /* Figure out which operator we have. */
14721 enum tree_code op = ERROR_MARK;
14722 bool assop = false;
14723 bool consumed = false;
14724 switch (token->type)
14726 case CPP_KEYWORD:
14728 /* The keyword should be either `new' or `delete'. */
14729 if (token->keyword == RID_NEW)
14730 op = NEW_EXPR;
14731 else if (token->keyword == RID_DELETE)
14732 op = DELETE_EXPR;
14733 else
14734 break;
14736 /* Consume the `new' or `delete' token. */
14737 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14739 /* Peek at the next token. */
14740 token = cp_lexer_peek_token (parser->lexer);
14741 /* If it's a `[' token then this is the array variant of the
14742 operator. */
14743 if (token->type == CPP_OPEN_SQUARE)
14745 /* Consume the `[' token. */
14746 cp_lexer_consume_token (parser->lexer);
14747 /* Look for the `]' token. */
14748 if (cp_token *close_token
14749 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14750 end_loc = close_token->location;
14751 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14753 start_loc = make_location (start_loc, start_loc, end_loc);
14754 consumed = true;
14755 break;
14758 case CPP_PLUS:
14759 op = PLUS_EXPR;
14760 break;
14762 case CPP_MINUS:
14763 op = MINUS_EXPR;
14764 break;
14766 case CPP_MULT:
14767 op = MULT_EXPR;
14768 break;
14770 case CPP_DIV:
14771 op = TRUNC_DIV_EXPR;
14772 break;
14774 case CPP_MOD:
14775 op = TRUNC_MOD_EXPR;
14776 break;
14778 case CPP_XOR:
14779 op = BIT_XOR_EXPR;
14780 break;
14782 case CPP_AND:
14783 op = BIT_AND_EXPR;
14784 break;
14786 case CPP_OR:
14787 op = BIT_IOR_EXPR;
14788 break;
14790 case CPP_COMPL:
14791 op = BIT_NOT_EXPR;
14792 break;
14794 case CPP_NOT:
14795 op = TRUTH_NOT_EXPR;
14796 break;
14798 case CPP_EQ:
14799 assop = true;
14800 op = NOP_EXPR;
14801 break;
14803 case CPP_LESS:
14804 op = LT_EXPR;
14805 break;
14807 case CPP_GREATER:
14808 op = GT_EXPR;
14809 break;
14811 case CPP_PLUS_EQ:
14812 assop = true;
14813 op = PLUS_EXPR;
14814 break;
14816 case CPP_MINUS_EQ:
14817 assop = true;
14818 op = MINUS_EXPR;
14819 break;
14821 case CPP_MULT_EQ:
14822 assop = true;
14823 op = MULT_EXPR;
14824 break;
14826 case CPP_DIV_EQ:
14827 assop = true;
14828 op = TRUNC_DIV_EXPR;
14829 break;
14831 case CPP_MOD_EQ:
14832 assop = true;
14833 op = TRUNC_MOD_EXPR;
14834 break;
14836 case CPP_XOR_EQ:
14837 assop = true;
14838 op = BIT_XOR_EXPR;
14839 break;
14841 case CPP_AND_EQ:
14842 assop = true;
14843 op = BIT_AND_EXPR;
14844 break;
14846 case CPP_OR_EQ:
14847 assop = true;
14848 op = BIT_IOR_EXPR;
14849 break;
14851 case CPP_LSHIFT:
14852 op = LSHIFT_EXPR;
14853 break;
14855 case CPP_RSHIFT:
14856 op = RSHIFT_EXPR;
14857 break;
14859 case CPP_LSHIFT_EQ:
14860 assop = true;
14861 op = LSHIFT_EXPR;
14862 break;
14864 case CPP_RSHIFT_EQ:
14865 assop = true;
14866 op = RSHIFT_EXPR;
14867 break;
14869 case CPP_EQ_EQ:
14870 op = EQ_EXPR;
14871 break;
14873 case CPP_NOT_EQ:
14874 op = NE_EXPR;
14875 break;
14877 case CPP_LESS_EQ:
14878 op = LE_EXPR;
14879 break;
14881 case CPP_GREATER_EQ:
14882 op = GE_EXPR;
14883 break;
14885 case CPP_AND_AND:
14886 op = TRUTH_ANDIF_EXPR;
14887 break;
14889 case CPP_OR_OR:
14890 op = TRUTH_ORIF_EXPR;
14891 break;
14893 case CPP_PLUS_PLUS:
14894 op = POSTINCREMENT_EXPR;
14895 break;
14897 case CPP_MINUS_MINUS:
14898 op = PREDECREMENT_EXPR;
14899 break;
14901 case CPP_COMMA:
14902 op = COMPOUND_EXPR;
14903 break;
14905 case CPP_DEREF_STAR:
14906 op = MEMBER_REF;
14907 break;
14909 case CPP_DEREF:
14910 op = COMPONENT_REF;
14911 break;
14913 case CPP_OPEN_PAREN:
14915 /* Consume the `('. */
14916 matching_parens parens;
14917 parens.consume_open (parser);
14918 /* Look for the matching `)'. */
14919 parens.require_close (parser);
14920 op = CALL_EXPR;
14921 consumed = true;
14922 break;
14925 case CPP_OPEN_SQUARE:
14926 /* Consume the `['. */
14927 cp_lexer_consume_token (parser->lexer);
14928 /* Look for the matching `]'. */
14929 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14930 op = ARRAY_REF;
14931 consumed = true;
14932 break;
14934 case CPP_UTF8STRING:
14935 case CPP_UTF8STRING_USERDEF:
14936 utf8 = true;
14937 /* FALLTHRU */
14938 case CPP_STRING:
14939 case CPP_WSTRING:
14940 case CPP_STRING16:
14941 case CPP_STRING32:
14942 case CPP_STRING_USERDEF:
14943 case CPP_WSTRING_USERDEF:
14944 case CPP_STRING16_USERDEF:
14945 case CPP_STRING32_USERDEF:
14947 tree str, string_tree;
14948 int sz, len;
14950 if (cxx_dialect == cxx98)
14951 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14953 /* Consume the string. */
14954 str = cp_parser_string_literal (parser, /*translate=*/true,
14955 /*wide_ok=*/true, /*lookup_udlit=*/false);
14956 if (str == error_mark_node)
14957 return error_mark_node;
14958 else if (TREE_CODE (str) == USERDEF_LITERAL)
14960 string_tree = USERDEF_LITERAL_VALUE (str);
14961 id = USERDEF_LITERAL_SUFFIX_ID (str);
14963 else
14965 string_tree = str;
14966 /* Look for the suffix identifier. */
14967 token = cp_lexer_peek_token (parser->lexer);
14968 if (token->type == CPP_NAME)
14969 id = cp_parser_identifier (parser);
14970 else if (token->type == CPP_KEYWORD)
14972 error ("unexpected keyword;"
14973 " remove space between quotes and suffix identifier");
14974 return error_mark_node;
14976 else
14978 error ("expected suffix identifier");
14979 return error_mark_node;
14982 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14983 (TREE_TYPE (TREE_TYPE (string_tree))));
14984 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14985 if (len != 0)
14987 error ("expected empty string after %<operator%> keyword");
14988 return error_mark_node;
14990 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14991 != char_type_node)
14993 error ("invalid encoding prefix in literal operator");
14994 return error_mark_node;
14996 if (id != error_mark_node)
14998 const char *name = IDENTIFIER_POINTER (id);
14999 id = cp_literal_operator_id (name);
15001 return id;
15004 default:
15005 /* Anything else is an error. */
15006 break;
15009 /* If we have selected an identifier, we need to consume the
15010 operator token. */
15011 if (op != ERROR_MARK)
15013 id = ovl_op_identifier (assop, op);
15014 if (!consumed)
15015 cp_lexer_consume_token (parser->lexer);
15017 /* Otherwise, no valid operator name was present. */
15018 else
15020 cp_parser_error (parser, "expected operator");
15021 id = error_mark_node;
15024 return cp_expr (id, start_loc);
15027 /* Parse a template-declaration.
15029 template-declaration:
15030 export [opt] template < template-parameter-list > declaration
15032 If MEMBER_P is TRUE, this template-declaration occurs within a
15033 class-specifier.
15035 The grammar rule given by the standard isn't correct. What
15036 is really meant is:
15038 template-declaration:
15039 export [opt] template-parameter-list-seq
15040 decl-specifier-seq [opt] init-declarator [opt] ;
15041 export [opt] template-parameter-list-seq
15042 function-definition
15044 template-parameter-list-seq:
15045 template-parameter-list-seq [opt]
15046 template < template-parameter-list >
15048 Concept Extensions:
15050 template-parameter-list-seq:
15051 template < template-parameter-list > requires-clause [opt]
15053 requires-clause:
15054 requires logical-or-expression */
15056 static void
15057 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15059 /* Check for `export'. */
15060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15062 /* Consume the `export' token. */
15063 cp_lexer_consume_token (parser->lexer);
15064 /* Warn that we do not support `export'. */
15065 warning (0, "keyword %<export%> not implemented, and will be ignored");
15068 cp_parser_template_declaration_after_export (parser, member_p);
15071 /* Parse a template-parameter-list.
15073 template-parameter-list:
15074 template-parameter
15075 template-parameter-list , template-parameter
15077 Returns a TREE_LIST. Each node represents a template parameter.
15078 The nodes are connected via their TREE_CHAINs. */
15080 static tree
15081 cp_parser_template_parameter_list (cp_parser* parser)
15083 tree parameter_list = NULL_TREE;
15085 begin_template_parm_list ();
15087 /* The loop below parses the template parms. We first need to know
15088 the total number of template parms to be able to compute proper
15089 canonical types of each dependent type. So after the loop, when
15090 we know the total number of template parms,
15091 end_template_parm_list computes the proper canonical types and
15092 fixes up the dependent types accordingly. */
15093 while (true)
15095 tree parameter;
15096 bool is_non_type;
15097 bool is_parameter_pack;
15098 location_t parm_loc;
15100 /* Parse the template-parameter. */
15101 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15102 parameter = cp_parser_template_parameter (parser,
15103 &is_non_type,
15104 &is_parameter_pack);
15105 /* Add it to the list. */
15106 if (parameter != error_mark_node)
15107 parameter_list = process_template_parm (parameter_list,
15108 parm_loc,
15109 parameter,
15110 is_non_type,
15111 is_parameter_pack);
15112 else
15114 tree err_parm = build_tree_list (parameter, parameter);
15115 parameter_list = chainon (parameter_list, err_parm);
15118 /* If the next token is not a `,', we're done. */
15119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15120 break;
15121 /* Otherwise, consume the `,' token. */
15122 cp_lexer_consume_token (parser->lexer);
15125 return end_template_parm_list (parameter_list);
15128 /* Parse a introduction-list.
15130 introduction-list:
15131 introduced-parameter
15132 introduction-list , introduced-parameter
15134 introduced-parameter:
15135 ...[opt] identifier
15137 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15138 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15139 WILDCARD_DECL will also have DECL_NAME set and token location in
15140 DECL_SOURCE_LOCATION. */
15142 static tree
15143 cp_parser_introduction_list (cp_parser *parser)
15145 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15147 while (true)
15149 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15150 if (is_pack)
15151 cp_lexer_consume_token (parser->lexer);
15153 /* Build placeholder. */
15154 tree parm = build_nt (WILDCARD_DECL);
15155 DECL_SOURCE_LOCATION (parm)
15156 = cp_lexer_peek_token (parser->lexer)->location;
15157 DECL_NAME (parm) = cp_parser_identifier (parser);
15158 WILDCARD_PACK_P (parm) = is_pack;
15159 vec_safe_push (introduction_vec, parm);
15161 /* If the next token is not a `,', we're done. */
15162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15163 break;
15164 /* Otherwise, consume the `,' token. */
15165 cp_lexer_consume_token (parser->lexer);
15168 /* Convert the vec into a TREE_VEC. */
15169 tree introduction_list = make_tree_vec (introduction_vec->length ());
15170 unsigned int n;
15171 tree parm;
15172 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15173 TREE_VEC_ELT (introduction_list, n) = parm;
15175 release_tree_vector (introduction_vec);
15176 return introduction_list;
15179 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15180 is an abstract declarator. */
15182 static inline cp_declarator*
15183 get_id_declarator (cp_declarator *declarator)
15185 cp_declarator *d = declarator;
15186 while (d && d->kind != cdk_id)
15187 d = d->declarator;
15188 return d;
15191 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15192 is an abstract declarator. */
15194 static inline tree
15195 get_unqualified_id (cp_declarator *declarator)
15197 declarator = get_id_declarator (declarator);
15198 if (declarator)
15199 return declarator->u.id.unqualified_name;
15200 else
15201 return NULL_TREE;
15204 /* Returns true if DECL represents a constrained-parameter. */
15206 static inline bool
15207 is_constrained_parameter (tree decl)
15209 return (decl
15210 && TREE_CODE (decl) == TYPE_DECL
15211 && CONSTRAINED_PARM_CONCEPT (decl)
15212 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15215 /* Returns true if PARM declares a constrained-parameter. */
15217 static inline bool
15218 is_constrained_parameter (cp_parameter_declarator *parm)
15220 return is_constrained_parameter (parm->decl_specifiers.type);
15223 /* Check that the type parameter is only a declarator-id, and that its
15224 type is not cv-qualified. */
15226 bool
15227 cp_parser_check_constrained_type_parm (cp_parser *parser,
15228 cp_parameter_declarator *parm)
15230 if (!parm->declarator)
15231 return true;
15233 if (parm->declarator->kind != cdk_id)
15235 cp_parser_error (parser, "invalid constrained type parameter");
15236 return false;
15239 /* Don't allow cv-qualified type parameters. */
15240 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15241 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15243 cp_parser_error (parser, "cv-qualified type parameter");
15244 return false;
15247 return true;
15250 /* Finish parsing/processing a template type parameter and checking
15251 various restrictions. */
15253 static inline tree
15254 cp_parser_constrained_type_template_parm (cp_parser *parser,
15255 tree id,
15256 cp_parameter_declarator* parmdecl)
15258 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15259 return finish_template_type_parm (class_type_node, id);
15260 else
15261 return error_mark_node;
15264 static tree
15265 finish_constrained_template_template_parm (tree proto, tree id)
15267 /* FIXME: This should probably be copied, and we may need to adjust
15268 the template parameter depths. */
15269 tree saved_parms = current_template_parms;
15270 begin_template_parm_list ();
15271 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15272 end_template_parm_list ();
15274 tree parm = finish_template_template_parm (class_type_node, id);
15275 current_template_parms = saved_parms;
15277 return parm;
15280 /* Finish parsing/processing a template template parameter by borrowing
15281 the template parameter list from the prototype parameter. */
15283 static tree
15284 cp_parser_constrained_template_template_parm (cp_parser *parser,
15285 tree proto,
15286 tree id,
15287 cp_parameter_declarator *parmdecl)
15289 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15290 return error_mark_node;
15291 return finish_constrained_template_template_parm (proto, id);
15294 /* Create a new non-type template parameter from the given PARM
15295 declarator. */
15297 static tree
15298 constrained_non_type_template_parm (bool *is_non_type,
15299 cp_parameter_declarator *parm)
15301 *is_non_type = true;
15302 cp_declarator *decl = parm->declarator;
15303 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15304 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15305 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15308 /* Build a constrained template parameter based on the PARMDECL
15309 declarator. The type of PARMDECL is the constrained type, which
15310 refers to the prototype template parameter that ultimately
15311 specifies the type of the declared parameter. */
15313 static tree
15314 finish_constrained_parameter (cp_parser *parser,
15315 cp_parameter_declarator *parmdecl,
15316 bool *is_non_type,
15317 bool *is_parameter_pack)
15319 tree decl = parmdecl->decl_specifiers.type;
15320 tree id = get_unqualified_id (parmdecl->declarator);
15321 tree def = parmdecl->default_argument;
15322 tree proto = DECL_INITIAL (decl);
15324 /* A template parameter constrained by a variadic concept shall also
15325 be declared as a template parameter pack. */
15326 bool is_variadic = template_parameter_pack_p (proto);
15327 if (is_variadic && !*is_parameter_pack)
15328 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15330 /* Build the parameter. Return an error if the declarator was invalid. */
15331 tree parm;
15332 if (TREE_CODE (proto) == TYPE_DECL)
15333 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15334 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15335 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15336 parmdecl);
15337 else
15338 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15339 if (parm == error_mark_node)
15340 return error_mark_node;
15342 /* Finish the parameter decl and create a node attaching the
15343 default argument and constraint. */
15344 parm = build_tree_list (def, parm);
15345 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15347 return parm;
15350 /* Returns true if the parsed type actually represents the declaration
15351 of a type template-parameter. */
15353 static inline bool
15354 declares_constrained_type_template_parameter (tree type)
15356 return (is_constrained_parameter (type)
15357 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15361 /* Returns true if the parsed type actually represents the declaration of
15362 a template template-parameter. */
15364 static bool
15365 declares_constrained_template_template_parameter (tree type)
15367 return (is_constrained_parameter (type)
15368 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15371 /* Parse a default argument for a type template-parameter.
15372 Note that diagnostics are handled in cp_parser_template_parameter. */
15374 static tree
15375 cp_parser_default_type_template_argument (cp_parser *parser)
15377 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15379 /* Consume the `=' token. */
15380 cp_lexer_consume_token (parser->lexer);
15382 cp_token *token = cp_lexer_peek_token (parser->lexer);
15384 /* Parse the default-argument. */
15385 push_deferring_access_checks (dk_no_deferred);
15386 tree default_argument = cp_parser_type_id (parser);
15387 pop_deferring_access_checks ();
15389 if (flag_concepts && type_uses_auto (default_argument))
15391 error_at (token->location,
15392 "invalid use of %<auto%> in default template argument");
15393 return error_mark_node;
15396 return default_argument;
15399 /* Parse a default argument for a template template-parameter. */
15401 static tree
15402 cp_parser_default_template_template_argument (cp_parser *parser)
15404 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15406 bool is_template;
15408 /* Consume the `='. */
15409 cp_lexer_consume_token (parser->lexer);
15410 /* Parse the id-expression. */
15411 push_deferring_access_checks (dk_no_deferred);
15412 /* save token before parsing the id-expression, for error
15413 reporting */
15414 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15415 tree default_argument
15416 = cp_parser_id_expression (parser,
15417 /*template_keyword_p=*/false,
15418 /*check_dependency_p=*/true,
15419 /*template_p=*/&is_template,
15420 /*declarator_p=*/false,
15421 /*optional_p=*/false);
15422 if (TREE_CODE (default_argument) == TYPE_DECL)
15423 /* If the id-expression was a template-id that refers to
15424 a template-class, we already have the declaration here,
15425 so no further lookup is needed. */
15427 else
15428 /* Look up the name. */
15429 default_argument
15430 = cp_parser_lookup_name (parser, default_argument,
15431 none_type,
15432 /*is_template=*/is_template,
15433 /*is_namespace=*/false,
15434 /*check_dependency=*/true,
15435 /*ambiguous_decls=*/NULL,
15436 token->location);
15437 /* See if the default argument is valid. */
15438 default_argument = check_template_template_default_arg (default_argument);
15439 pop_deferring_access_checks ();
15440 return default_argument;
15443 /* Parse a template-parameter.
15445 template-parameter:
15446 type-parameter
15447 parameter-declaration
15449 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15450 the parameter. The TREE_PURPOSE is the default value, if any.
15451 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15452 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15453 set to true iff this parameter is a parameter pack. */
15455 static tree
15456 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15457 bool *is_parameter_pack)
15459 cp_token *token;
15460 cp_parameter_declarator *parameter_declarator;
15461 tree parm;
15463 /* Assume it is a type parameter or a template parameter. */
15464 *is_non_type = false;
15465 /* Assume it not a parameter pack. */
15466 *is_parameter_pack = false;
15467 /* Peek at the next token. */
15468 token = cp_lexer_peek_token (parser->lexer);
15469 /* If it is `template', we have a type-parameter. */
15470 if (token->keyword == RID_TEMPLATE)
15471 return cp_parser_type_parameter (parser, is_parameter_pack);
15472 /* If it is `class' or `typename' we do not know yet whether it is a
15473 type parameter or a non-type parameter. Consider:
15475 template <typename T, typename T::X X> ...
15479 template <class C, class D*> ...
15481 Here, the first parameter is a type parameter, and the second is
15482 a non-type parameter. We can tell by looking at the token after
15483 the identifier -- if it is a `,', `=', or `>' then we have a type
15484 parameter. */
15485 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15487 /* Peek at the token after `class' or `typename'. */
15488 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15489 /* If it's an ellipsis, we have a template type parameter
15490 pack. */
15491 if (token->type == CPP_ELLIPSIS)
15492 return cp_parser_type_parameter (parser, is_parameter_pack);
15493 /* If it's an identifier, skip it. */
15494 if (token->type == CPP_NAME)
15495 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15496 /* Now, see if the token looks like the end of a template
15497 parameter. */
15498 if (token->type == CPP_COMMA
15499 || token->type == CPP_EQ
15500 || token->type == CPP_GREATER)
15501 return cp_parser_type_parameter (parser, is_parameter_pack);
15504 /* Otherwise, it is a non-type parameter or a constrained parameter.
15506 [temp.param]
15508 When parsing a default template-argument for a non-type
15509 template-parameter, the first non-nested `>' is taken as the end
15510 of the template parameter-list rather than a greater-than
15511 operator. */
15512 parameter_declarator
15513 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15514 /*parenthesized_p=*/NULL);
15516 if (!parameter_declarator)
15517 return error_mark_node;
15519 /* If the parameter declaration is marked as a parameter pack, set
15520 *IS_PARAMETER_PACK to notify the caller. */
15521 if (parameter_declarator->template_parameter_pack_p)
15522 *is_parameter_pack = true;
15524 if (parameter_declarator->default_argument)
15526 /* Can happen in some cases of erroneous input (c++/34892). */
15527 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15528 /* Consume the `...' for better error recovery. */
15529 cp_lexer_consume_token (parser->lexer);
15532 // The parameter may have been constrained.
15533 if (is_constrained_parameter (parameter_declarator))
15534 return finish_constrained_parameter (parser,
15535 parameter_declarator,
15536 is_non_type,
15537 is_parameter_pack);
15539 // Now we're sure that the parameter is a non-type parameter.
15540 *is_non_type = true;
15542 parm = grokdeclarator (parameter_declarator->declarator,
15543 &parameter_declarator->decl_specifiers,
15544 TPARM, /*initialized=*/0,
15545 /*attrlist=*/NULL);
15546 if (parm == error_mark_node)
15547 return error_mark_node;
15549 return build_tree_list (parameter_declarator->default_argument, parm);
15552 /* Parse a type-parameter.
15554 type-parameter:
15555 class identifier [opt]
15556 class identifier [opt] = type-id
15557 typename identifier [opt]
15558 typename identifier [opt] = type-id
15559 template < template-parameter-list > class identifier [opt]
15560 template < template-parameter-list > class identifier [opt]
15561 = id-expression
15563 GNU Extension (variadic templates):
15565 type-parameter:
15566 class ... identifier [opt]
15567 typename ... identifier [opt]
15569 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15570 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15571 the declaration of the parameter.
15573 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15575 static tree
15576 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15578 cp_token *token;
15579 tree parameter;
15581 /* Look for a keyword to tell us what kind of parameter this is. */
15582 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15583 if (!token)
15584 return error_mark_node;
15586 switch (token->keyword)
15588 case RID_CLASS:
15589 case RID_TYPENAME:
15591 tree identifier;
15592 tree default_argument;
15594 /* If the next token is an ellipsis, we have a template
15595 argument pack. */
15596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15598 /* Consume the `...' token. */
15599 cp_lexer_consume_token (parser->lexer);
15600 maybe_warn_variadic_templates ();
15602 *is_parameter_pack = true;
15605 /* If the next token is an identifier, then it names the
15606 parameter. */
15607 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15608 identifier = cp_parser_identifier (parser);
15609 else
15610 identifier = NULL_TREE;
15612 /* Create the parameter. */
15613 parameter = finish_template_type_parm (class_type_node, identifier);
15615 /* If the next token is an `=', we have a default argument. */
15616 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15618 default_argument
15619 = cp_parser_default_type_template_argument (parser);
15621 /* Template parameter packs cannot have default
15622 arguments. */
15623 if (*is_parameter_pack)
15625 if (identifier)
15626 error_at (token->location,
15627 "template parameter pack %qD cannot have a "
15628 "default argument", identifier);
15629 else
15630 error_at (token->location,
15631 "template parameter packs cannot have "
15632 "default arguments");
15633 default_argument = NULL_TREE;
15635 else if (check_for_bare_parameter_packs (default_argument))
15636 default_argument = error_mark_node;
15638 else
15639 default_argument = NULL_TREE;
15641 /* Create the combined representation of the parameter and the
15642 default argument. */
15643 parameter = build_tree_list (default_argument, parameter);
15645 break;
15647 case RID_TEMPLATE:
15649 tree identifier;
15650 tree default_argument;
15652 /* Look for the `<'. */
15653 cp_parser_require (parser, CPP_LESS, RT_LESS);
15654 /* Parse the template-parameter-list. */
15655 cp_parser_template_parameter_list (parser);
15656 /* Look for the `>'. */
15657 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15659 // If template requirements are present, parse them.
15660 if (flag_concepts)
15662 tree reqs = get_shorthand_constraints (current_template_parms);
15663 if (tree r = cp_parser_requires_clause_opt (parser))
15664 reqs = conjoin_constraints (reqs, normalize_expression (r));
15665 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15668 /* Look for the `class' or 'typename' keywords. */
15669 cp_parser_type_parameter_key (parser);
15670 /* If the next token is an ellipsis, we have a template
15671 argument pack. */
15672 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15674 /* Consume the `...' token. */
15675 cp_lexer_consume_token (parser->lexer);
15676 maybe_warn_variadic_templates ();
15678 *is_parameter_pack = true;
15680 /* If the next token is an `=', then there is a
15681 default-argument. If the next token is a `>', we are at
15682 the end of the parameter-list. If the next token is a `,',
15683 then we are at the end of this parameter. */
15684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15685 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15686 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15688 identifier = cp_parser_identifier (parser);
15689 /* Treat invalid names as if the parameter were nameless. */
15690 if (identifier == error_mark_node)
15691 identifier = NULL_TREE;
15693 else
15694 identifier = NULL_TREE;
15696 /* Create the template parameter. */
15697 parameter = finish_template_template_parm (class_type_node,
15698 identifier);
15700 /* If the next token is an `=', then there is a
15701 default-argument. */
15702 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15704 default_argument
15705 = cp_parser_default_template_template_argument (parser);
15707 /* Template parameter packs cannot have default
15708 arguments. */
15709 if (*is_parameter_pack)
15711 if (identifier)
15712 error_at (token->location,
15713 "template parameter pack %qD cannot "
15714 "have a default argument",
15715 identifier);
15716 else
15717 error_at (token->location, "template parameter packs cannot "
15718 "have default arguments");
15719 default_argument = NULL_TREE;
15722 else
15723 default_argument = NULL_TREE;
15725 /* Create the combined representation of the parameter and the
15726 default argument. */
15727 parameter = build_tree_list (default_argument, parameter);
15729 break;
15731 default:
15732 gcc_unreachable ();
15733 break;
15736 return parameter;
15739 /* Parse a template-id.
15741 template-id:
15742 template-name < template-argument-list [opt] >
15744 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15745 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15746 returned. Otherwise, if the template-name names a function, or set
15747 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15748 names a class, returns a TYPE_DECL for the specialization.
15750 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15751 uninstantiated templates. */
15753 static tree
15754 cp_parser_template_id (cp_parser *parser,
15755 bool template_keyword_p,
15756 bool check_dependency_p,
15757 enum tag_types tag_type,
15758 bool is_declaration)
15760 tree templ;
15761 tree arguments;
15762 tree template_id;
15763 cp_token_position start_of_id = 0;
15764 cp_token *next_token = NULL, *next_token_2 = NULL;
15765 bool is_identifier;
15767 /* If the next token corresponds to a template-id, there is no need
15768 to reparse it. */
15769 cp_token *token = cp_lexer_peek_token (parser->lexer);
15770 if (token->type == CPP_TEMPLATE_ID)
15772 cp_lexer_consume_token (parser->lexer);
15773 return saved_checks_value (token->u.tree_check_value);
15776 /* Avoid performing name lookup if there is no possibility of
15777 finding a template-id. */
15778 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15779 || (token->type == CPP_NAME
15780 && !cp_parser_nth_token_starts_template_argument_list_p
15781 (parser, 2)))
15783 cp_parser_error (parser, "expected template-id");
15784 return error_mark_node;
15787 /* Remember where the template-id starts. */
15788 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15789 start_of_id = cp_lexer_token_position (parser->lexer, false);
15791 push_deferring_access_checks (dk_deferred);
15793 /* Parse the template-name. */
15794 is_identifier = false;
15795 templ = cp_parser_template_name (parser, template_keyword_p,
15796 check_dependency_p,
15797 is_declaration,
15798 tag_type,
15799 &is_identifier);
15800 if (templ == error_mark_node || is_identifier)
15802 pop_deferring_access_checks ();
15803 return templ;
15806 /* Since we're going to preserve any side-effects from this parse, set up a
15807 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15808 in the template arguments. */
15809 tentative_firewall firewall (parser);
15811 /* If we find the sequence `[:' after a template-name, it's probably
15812 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15813 parse correctly the argument list. */
15814 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15815 == CPP_OPEN_SQUARE)
15816 && next_token->flags & DIGRAPH
15817 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15818 == CPP_COLON)
15819 && !(next_token_2->flags & PREV_WHITE))
15821 cp_parser_parse_tentatively (parser);
15822 /* Change `:' into `::'. */
15823 next_token_2->type = CPP_SCOPE;
15824 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15825 CPP_LESS. */
15826 cp_lexer_consume_token (parser->lexer);
15828 /* Parse the arguments. */
15829 arguments = cp_parser_enclosed_template_argument_list (parser);
15830 if (!cp_parser_parse_definitely (parser))
15832 /* If we couldn't parse an argument list, then we revert our changes
15833 and return simply an error. Maybe this is not a template-id
15834 after all. */
15835 next_token_2->type = CPP_COLON;
15836 cp_parser_error (parser, "expected %<<%>");
15837 pop_deferring_access_checks ();
15838 return error_mark_node;
15840 /* Otherwise, emit an error about the invalid digraph, but continue
15841 parsing because we got our argument list. */
15842 if (permerror (next_token->location,
15843 "%<<::%> cannot begin a template-argument list"))
15845 static bool hint = false;
15846 inform (next_token->location,
15847 "%<<:%> is an alternate spelling for %<[%>."
15848 " Insert whitespace between %<<%> and %<::%>");
15849 if (!hint && !flag_permissive)
15851 inform (next_token->location, "(if you use %<-fpermissive%> "
15852 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15853 "accept your code)");
15854 hint = true;
15858 else
15860 /* Look for the `<' that starts the template-argument-list. */
15861 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15863 pop_deferring_access_checks ();
15864 return error_mark_node;
15866 /* Parse the arguments. */
15867 arguments = cp_parser_enclosed_template_argument_list (parser);
15870 /* Set the location to be of the form:
15871 template-name < template-argument-list [opt] >
15872 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15873 with caret == start at the start of the template-name,
15874 ranging until the closing '>'. */
15875 location_t finish_loc
15876 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15877 location_t combined_loc
15878 = make_location (token->location, token->location, finish_loc);
15880 /* Build a representation of the specialization. */
15881 if (identifier_p (templ))
15882 template_id = build_min_nt_loc (combined_loc,
15883 TEMPLATE_ID_EXPR,
15884 templ, arguments);
15885 else if (DECL_TYPE_TEMPLATE_P (templ)
15886 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15888 bool entering_scope;
15889 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15890 template (rather than some instantiation thereof) only if
15891 is not nested within some other construct. For example, in
15892 "template <typename T> void f(T) { A<T>::", A<T> is just an
15893 instantiation of A. */
15894 entering_scope = (template_parm_scope_p ()
15895 && cp_lexer_next_token_is (parser->lexer,
15896 CPP_SCOPE));
15897 template_id
15898 = finish_template_type (templ, arguments, entering_scope);
15900 /* A template-like identifier may be a partial concept id. */
15901 else if (flag_concepts
15902 && (template_id = (cp_parser_maybe_partial_concept_id
15903 (parser, templ, arguments))))
15904 return template_id;
15905 else if (variable_template_p (templ))
15907 template_id = lookup_template_variable (templ, arguments);
15908 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15909 SET_EXPR_LOCATION (template_id, combined_loc);
15911 else
15913 /* If it's not a class-template or a template-template, it should be
15914 a function-template. */
15915 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15916 || TREE_CODE (templ) == OVERLOAD
15917 || BASELINK_P (templ)));
15919 template_id = lookup_template_function (templ, arguments);
15920 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15921 SET_EXPR_LOCATION (template_id, combined_loc);
15924 /* If parsing tentatively, replace the sequence of tokens that makes
15925 up the template-id with a CPP_TEMPLATE_ID token. That way,
15926 should we re-parse the token stream, we will not have to repeat
15927 the effort required to do the parse, nor will we issue duplicate
15928 error messages about problems during instantiation of the
15929 template. */
15930 if (start_of_id
15931 /* Don't do this if we had a parse error in a declarator; re-parsing
15932 might succeed if a name changes meaning (60361). */
15933 && !(cp_parser_error_occurred (parser)
15934 && cp_parser_parsing_tentatively (parser)
15935 && parser->in_declarator_p))
15937 /* Reset the contents of the START_OF_ID token. */
15938 token->type = CPP_TEMPLATE_ID;
15939 token->location = combined_loc;
15941 /* We must mark the lookup as kept, so we don't throw it away on
15942 the first parse. */
15943 if (is_overloaded_fn (template_id))
15944 lookup_keep (get_fns (template_id), true);
15946 /* Retrieve any deferred checks. Do not pop this access checks yet
15947 so the memory will not be reclaimed during token replacing below. */
15948 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15949 token->u.tree_check_value->value = template_id;
15950 token->u.tree_check_value->checks = get_deferred_access_checks ();
15951 token->keyword = RID_MAX;
15953 /* Purge all subsequent tokens. */
15954 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15956 /* ??? Can we actually assume that, if template_id ==
15957 error_mark_node, we will have issued a diagnostic to the
15958 user, as opposed to simply marking the tentative parse as
15959 failed? */
15960 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15961 error_at (token->location, "parse error in template argument list");
15964 pop_to_parent_deferring_access_checks ();
15965 return template_id;
15968 /* Parse a template-name.
15970 template-name:
15971 identifier
15973 The standard should actually say:
15975 template-name:
15976 identifier
15977 operator-function-id
15979 A defect report has been filed about this issue.
15981 A conversion-function-id cannot be a template name because they cannot
15982 be part of a template-id. In fact, looking at this code:
15984 a.operator K<int>()
15986 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15987 It is impossible to call a templated conversion-function-id with an
15988 explicit argument list, since the only allowed template parameter is
15989 the type to which it is converting.
15991 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15992 `template' keyword, in a construction like:
15994 T::template f<3>()
15996 In that case `f' is taken to be a template-name, even though there
15997 is no way of knowing for sure.
15999 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16000 name refers to a set of overloaded functions, at least one of which
16001 is a template, or an IDENTIFIER_NODE with the name of the template,
16002 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16003 names are looked up inside uninstantiated templates. */
16005 static tree
16006 cp_parser_template_name (cp_parser* parser,
16007 bool template_keyword_p,
16008 bool check_dependency_p,
16009 bool is_declaration,
16010 enum tag_types tag_type,
16011 bool *is_identifier)
16013 tree identifier;
16014 tree decl;
16015 cp_token *token = cp_lexer_peek_token (parser->lexer);
16017 /* If the next token is `operator', then we have either an
16018 operator-function-id or a conversion-function-id. */
16019 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16021 /* We don't know whether we're looking at an
16022 operator-function-id or a conversion-function-id. */
16023 cp_parser_parse_tentatively (parser);
16024 /* Try an operator-function-id. */
16025 identifier = cp_parser_operator_function_id (parser);
16026 /* If that didn't work, try a conversion-function-id. */
16027 if (!cp_parser_parse_definitely (parser))
16029 cp_parser_error (parser, "expected template-name");
16030 return error_mark_node;
16033 /* Look for the identifier. */
16034 else
16035 identifier = cp_parser_identifier (parser);
16037 /* If we didn't find an identifier, we don't have a template-id. */
16038 if (identifier == error_mark_node)
16039 return error_mark_node;
16041 /* If the name immediately followed the `template' keyword, then it
16042 is a template-name. However, if the next token is not `<', then
16043 we do not treat it as a template-name, since it is not being used
16044 as part of a template-id. This enables us to handle constructs
16045 like:
16047 template <typename T> struct S { S(); };
16048 template <typename T> S<T>::S();
16050 correctly. We would treat `S' as a template -- if it were `S<T>'
16051 -- but we do not if there is no `<'. */
16053 if (processing_template_decl
16054 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16056 /* In a declaration, in a dependent context, we pretend that the
16057 "template" keyword was present in order to improve error
16058 recovery. For example, given:
16060 template <typename T> void f(T::X<int>);
16062 we want to treat "X<int>" as a template-id. */
16063 if (is_declaration
16064 && !template_keyword_p
16065 && parser->scope && TYPE_P (parser->scope)
16066 && check_dependency_p
16067 && dependent_scope_p (parser->scope)
16068 /* Do not do this for dtors (or ctors), since they never
16069 need the template keyword before their name. */
16070 && !constructor_name_p (identifier, parser->scope))
16072 cp_token_position start = 0;
16074 /* Explain what went wrong. */
16075 error_at (token->location, "non-template %qD used as template",
16076 identifier);
16077 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16078 parser->scope, identifier);
16079 /* If parsing tentatively, find the location of the "<" token. */
16080 if (cp_parser_simulate_error (parser))
16081 start = cp_lexer_token_position (parser->lexer, true);
16082 /* Parse the template arguments so that we can issue error
16083 messages about them. */
16084 cp_lexer_consume_token (parser->lexer);
16085 cp_parser_enclosed_template_argument_list (parser);
16086 /* Skip tokens until we find a good place from which to
16087 continue parsing. */
16088 cp_parser_skip_to_closing_parenthesis (parser,
16089 /*recovering=*/true,
16090 /*or_comma=*/true,
16091 /*consume_paren=*/false);
16092 /* If parsing tentatively, permanently remove the
16093 template argument list. That will prevent duplicate
16094 error messages from being issued about the missing
16095 "template" keyword. */
16096 if (start)
16097 cp_lexer_purge_tokens_after (parser->lexer, start);
16098 if (is_identifier)
16099 *is_identifier = true;
16100 parser->context->object_type = NULL_TREE;
16101 return identifier;
16104 /* If the "template" keyword is present, then there is generally
16105 no point in doing name-lookup, so we just return IDENTIFIER.
16106 But, if the qualifying scope is non-dependent then we can
16107 (and must) do name-lookup normally. */
16108 if (template_keyword_p)
16110 tree scope = (parser->scope ? parser->scope
16111 : parser->context->object_type);
16112 if (scope && TYPE_P (scope)
16113 && (!CLASS_TYPE_P (scope)
16114 || (check_dependency_p && dependent_type_p (scope))))
16116 /* We're optimizing away the call to cp_parser_lookup_name, but
16117 we still need to do this. */
16118 parser->context->object_type = NULL_TREE;
16119 return identifier;
16124 /* Look up the name. */
16125 decl = cp_parser_lookup_name (parser, identifier,
16126 tag_type,
16127 /*is_template=*/true,
16128 /*is_namespace=*/false,
16129 check_dependency_p,
16130 /*ambiguous_decls=*/NULL,
16131 token->location);
16133 decl = strip_using_decl (decl);
16135 /* If DECL is a template, then the name was a template-name. */
16136 if (TREE_CODE (decl) == TEMPLATE_DECL)
16138 if (TREE_DEPRECATED (decl)
16139 && deprecated_state != DEPRECATED_SUPPRESS)
16140 warn_deprecated_use (decl, NULL_TREE);
16142 else
16144 /* The standard does not explicitly indicate whether a name that
16145 names a set of overloaded declarations, some of which are
16146 templates, is a template-name. However, such a name should
16147 be a template-name; otherwise, there is no way to form a
16148 template-id for the overloaded templates. */
16149 bool found = false;
16151 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16152 !found && iter; ++iter)
16153 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16154 found = true;
16156 if (!found)
16158 /* The name does not name a template. */
16159 cp_parser_error (parser, "expected template-name");
16160 return error_mark_node;
16164 /* If DECL is dependent, and refers to a function, then just return
16165 its name; we will look it up again during template instantiation. */
16166 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16168 tree scope = ovl_scope (decl);
16169 if (TYPE_P (scope) && dependent_type_p (scope))
16170 return identifier;
16173 return decl;
16176 /* Parse a template-argument-list.
16178 template-argument-list:
16179 template-argument ... [opt]
16180 template-argument-list , template-argument ... [opt]
16182 Returns a TREE_VEC containing the arguments. */
16184 static tree
16185 cp_parser_template_argument_list (cp_parser* parser)
16187 tree fixed_args[10];
16188 unsigned n_args = 0;
16189 unsigned alloced = 10;
16190 tree *arg_ary = fixed_args;
16191 tree vec;
16192 bool saved_in_template_argument_list_p;
16193 bool saved_ice_p;
16194 bool saved_non_ice_p;
16196 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16197 parser->in_template_argument_list_p = true;
16198 /* Even if the template-id appears in an integral
16199 constant-expression, the contents of the argument list do
16200 not. */
16201 saved_ice_p = parser->integral_constant_expression_p;
16202 parser->integral_constant_expression_p = false;
16203 saved_non_ice_p = parser->non_integral_constant_expression_p;
16204 parser->non_integral_constant_expression_p = false;
16206 /* Parse the arguments. */
16209 tree argument;
16211 if (n_args)
16212 /* Consume the comma. */
16213 cp_lexer_consume_token (parser->lexer);
16215 /* Parse the template-argument. */
16216 argument = cp_parser_template_argument (parser);
16218 /* If the next token is an ellipsis, we're expanding a template
16219 argument pack. */
16220 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16222 if (argument == error_mark_node)
16224 cp_token *token = cp_lexer_peek_token (parser->lexer);
16225 error_at (token->location,
16226 "expected parameter pack before %<...%>");
16228 /* Consume the `...' token. */
16229 cp_lexer_consume_token (parser->lexer);
16231 /* Make the argument into a TYPE_PACK_EXPANSION or
16232 EXPR_PACK_EXPANSION. */
16233 argument = make_pack_expansion (argument);
16236 if (n_args == alloced)
16238 alloced *= 2;
16240 if (arg_ary == fixed_args)
16242 arg_ary = XNEWVEC (tree, alloced);
16243 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16245 else
16246 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16248 arg_ary[n_args++] = argument;
16250 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16252 vec = make_tree_vec (n_args);
16254 while (n_args--)
16255 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16257 if (arg_ary != fixed_args)
16258 free (arg_ary);
16259 parser->non_integral_constant_expression_p = saved_non_ice_p;
16260 parser->integral_constant_expression_p = saved_ice_p;
16261 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16262 if (CHECKING_P)
16263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16264 return vec;
16267 /* Parse a template-argument.
16269 template-argument:
16270 assignment-expression
16271 type-id
16272 id-expression
16274 The representation is that of an assignment-expression, type-id, or
16275 id-expression -- except that the qualified id-expression is
16276 evaluated, so that the value returned is either a DECL or an
16277 OVERLOAD.
16279 Although the standard says "assignment-expression", it forbids
16280 throw-expressions or assignments in the template argument.
16281 Therefore, we use "conditional-expression" instead. */
16283 static tree
16284 cp_parser_template_argument (cp_parser* parser)
16286 tree argument;
16287 bool template_p;
16288 bool address_p;
16289 bool maybe_type_id = false;
16290 cp_token *token = NULL, *argument_start_token = NULL;
16291 location_t loc = 0;
16292 cp_id_kind idk;
16294 /* There's really no way to know what we're looking at, so we just
16295 try each alternative in order.
16297 [temp.arg]
16299 In a template-argument, an ambiguity between a type-id and an
16300 expression is resolved to a type-id, regardless of the form of
16301 the corresponding template-parameter.
16303 Therefore, we try a type-id first. */
16304 cp_parser_parse_tentatively (parser);
16305 argument = cp_parser_template_type_arg (parser);
16306 /* If there was no error parsing the type-id but the next token is a
16307 '>>', our behavior depends on which dialect of C++ we're
16308 parsing. In C++98, we probably found a typo for '> >'. But there
16309 are type-id which are also valid expressions. For instance:
16311 struct X { int operator >> (int); };
16312 template <int V> struct Foo {};
16313 Foo<X () >> 5> r;
16315 Here 'X()' is a valid type-id of a function type, but the user just
16316 wanted to write the expression "X() >> 5". Thus, we remember that we
16317 found a valid type-id, but we still try to parse the argument as an
16318 expression to see what happens.
16320 In C++0x, the '>>' will be considered two separate '>'
16321 tokens. */
16322 if (!cp_parser_error_occurred (parser)
16323 && cxx_dialect == cxx98
16324 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16326 maybe_type_id = true;
16327 cp_parser_abort_tentative_parse (parser);
16329 else
16331 /* If the next token isn't a `,' or a `>', then this argument wasn't
16332 really finished. This means that the argument is not a valid
16333 type-id. */
16334 if (!cp_parser_next_token_ends_template_argument_p (parser))
16335 cp_parser_error (parser, "expected template-argument");
16336 /* If that worked, we're done. */
16337 if (cp_parser_parse_definitely (parser))
16338 return argument;
16340 /* We're still not sure what the argument will be. */
16341 cp_parser_parse_tentatively (parser);
16342 /* Try a template. */
16343 argument_start_token = cp_lexer_peek_token (parser->lexer);
16344 argument = cp_parser_id_expression (parser,
16345 /*template_keyword_p=*/false,
16346 /*check_dependency_p=*/true,
16347 &template_p,
16348 /*declarator_p=*/false,
16349 /*optional_p=*/false);
16350 /* If the next token isn't a `,' or a `>', then this argument wasn't
16351 really finished. */
16352 if (!cp_parser_next_token_ends_template_argument_p (parser))
16353 cp_parser_error (parser, "expected template-argument");
16354 if (!cp_parser_error_occurred (parser))
16356 /* Figure out what is being referred to. If the id-expression
16357 was for a class template specialization, then we will have a
16358 TYPE_DECL at this point. There is no need to do name lookup
16359 at this point in that case. */
16360 if (TREE_CODE (argument) != TYPE_DECL)
16361 argument = cp_parser_lookup_name (parser, argument,
16362 none_type,
16363 /*is_template=*/template_p,
16364 /*is_namespace=*/false,
16365 /*check_dependency=*/true,
16366 /*ambiguous_decls=*/NULL,
16367 argument_start_token->location);
16368 /* Handle a constrained-type-specifier for a non-type template
16369 parameter. */
16370 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16371 argument = decl;
16372 else if (TREE_CODE (argument) != TEMPLATE_DECL
16373 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16374 cp_parser_error (parser, "expected template-name");
16376 if (cp_parser_parse_definitely (parser))
16378 if (TREE_DEPRECATED (argument))
16379 warn_deprecated_use (argument, NULL_TREE);
16380 return argument;
16382 /* It must be a non-type argument. In C++17 any constant-expression is
16383 allowed. */
16384 if (cxx_dialect > cxx14)
16385 goto general_expr;
16387 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16389 -- an integral constant-expression of integral or enumeration
16390 type; or
16392 -- the name of a non-type template-parameter; or
16394 -- the name of an object or function with external linkage...
16396 -- the address of an object or function with external linkage...
16398 -- a pointer to member... */
16399 /* Look for a non-type template parameter. */
16400 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16402 cp_parser_parse_tentatively (parser);
16403 argument = cp_parser_primary_expression (parser,
16404 /*address_p=*/false,
16405 /*cast_p=*/false,
16406 /*template_arg_p=*/true,
16407 &idk);
16408 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16409 || !cp_parser_next_token_ends_template_argument_p (parser))
16410 cp_parser_simulate_error (parser);
16411 if (cp_parser_parse_definitely (parser))
16412 return argument;
16415 /* If the next token is "&", the argument must be the address of an
16416 object or function with external linkage. */
16417 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16418 if (address_p)
16420 loc = cp_lexer_peek_token (parser->lexer)->location;
16421 cp_lexer_consume_token (parser->lexer);
16423 /* See if we might have an id-expression. */
16424 token = cp_lexer_peek_token (parser->lexer);
16425 if (token->type == CPP_NAME
16426 || token->keyword == RID_OPERATOR
16427 || token->type == CPP_SCOPE
16428 || token->type == CPP_TEMPLATE_ID
16429 || token->type == CPP_NESTED_NAME_SPECIFIER)
16431 cp_parser_parse_tentatively (parser);
16432 argument = cp_parser_primary_expression (parser,
16433 address_p,
16434 /*cast_p=*/false,
16435 /*template_arg_p=*/true,
16436 &idk);
16437 if (cp_parser_error_occurred (parser)
16438 || !cp_parser_next_token_ends_template_argument_p (parser))
16439 cp_parser_abort_tentative_parse (parser);
16440 else
16442 tree probe;
16444 if (INDIRECT_REF_P (argument))
16446 /* Strip the dereference temporarily. */
16447 gcc_assert (REFERENCE_REF_P (argument));
16448 argument = TREE_OPERAND (argument, 0);
16451 /* If we're in a template, we represent a qualified-id referring
16452 to a static data member as a SCOPE_REF even if the scope isn't
16453 dependent so that we can check access control later. */
16454 probe = argument;
16455 if (TREE_CODE (probe) == SCOPE_REF)
16456 probe = TREE_OPERAND (probe, 1);
16457 if (VAR_P (probe))
16459 /* A variable without external linkage might still be a
16460 valid constant-expression, so no error is issued here
16461 if the external-linkage check fails. */
16462 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16463 cp_parser_simulate_error (parser);
16465 else if (is_overloaded_fn (argument))
16466 /* All overloaded functions are allowed; if the external
16467 linkage test does not pass, an error will be issued
16468 later. */
16470 else if (address_p
16471 && (TREE_CODE (argument) == OFFSET_REF
16472 || TREE_CODE (argument) == SCOPE_REF))
16473 /* A pointer-to-member. */
16475 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16477 else
16478 cp_parser_simulate_error (parser);
16480 if (cp_parser_parse_definitely (parser))
16482 if (address_p)
16483 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16484 tf_warning_or_error);
16485 else
16486 argument = convert_from_reference (argument);
16487 return argument;
16491 /* If the argument started with "&", there are no other valid
16492 alternatives at this point. */
16493 if (address_p)
16495 cp_parser_error (parser, "invalid non-type template argument");
16496 return error_mark_node;
16499 general_expr:
16500 /* If the argument wasn't successfully parsed as a type-id followed
16501 by '>>', the argument can only be a constant expression now.
16502 Otherwise, we try parsing the constant-expression tentatively,
16503 because the argument could really be a type-id. */
16504 if (maybe_type_id)
16505 cp_parser_parse_tentatively (parser);
16507 if (cxx_dialect <= cxx14)
16508 argument = cp_parser_constant_expression (parser);
16509 else
16511 /* With C++17 generalized non-type template arguments we need to handle
16512 lvalue constant expressions, too. */
16513 argument = cp_parser_assignment_expression (parser);
16514 require_potential_constant_expression (argument);
16517 if (!maybe_type_id)
16518 return argument;
16519 if (!cp_parser_next_token_ends_template_argument_p (parser))
16520 cp_parser_error (parser, "expected template-argument");
16521 if (cp_parser_parse_definitely (parser))
16522 return argument;
16523 /* We did our best to parse the argument as a non type-id, but that
16524 was the only alternative that matched (albeit with a '>' after
16525 it). We can assume it's just a typo from the user, and a
16526 diagnostic will then be issued. */
16527 return cp_parser_template_type_arg (parser);
16530 /* Parse an explicit-instantiation.
16532 explicit-instantiation:
16533 template declaration
16535 Although the standard says `declaration', what it really means is:
16537 explicit-instantiation:
16538 template decl-specifier-seq [opt] declarator [opt] ;
16540 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16541 supposed to be allowed. A defect report has been filed about this
16542 issue.
16544 GNU Extension:
16546 explicit-instantiation:
16547 storage-class-specifier template
16548 decl-specifier-seq [opt] declarator [opt] ;
16549 function-specifier template
16550 decl-specifier-seq [opt] declarator [opt] ; */
16552 static void
16553 cp_parser_explicit_instantiation (cp_parser* parser)
16555 int declares_class_or_enum;
16556 cp_decl_specifier_seq decl_specifiers;
16557 tree extension_specifier = NULL_TREE;
16559 timevar_push (TV_TEMPLATE_INST);
16561 /* Look for an (optional) storage-class-specifier or
16562 function-specifier. */
16563 if (cp_parser_allow_gnu_extensions_p (parser))
16565 extension_specifier
16566 = cp_parser_storage_class_specifier_opt (parser);
16567 if (!extension_specifier)
16568 extension_specifier
16569 = cp_parser_function_specifier_opt (parser,
16570 /*decl_specs=*/NULL);
16573 /* Look for the `template' keyword. */
16574 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16575 /* Let the front end know that we are processing an explicit
16576 instantiation. */
16577 begin_explicit_instantiation ();
16578 /* [temp.explicit] says that we are supposed to ignore access
16579 control while processing explicit instantiation directives. */
16580 push_deferring_access_checks (dk_no_check);
16581 /* Parse a decl-specifier-seq. */
16582 cp_parser_decl_specifier_seq (parser,
16583 CP_PARSER_FLAGS_OPTIONAL,
16584 &decl_specifiers,
16585 &declares_class_or_enum);
16586 /* If there was exactly one decl-specifier, and it declared a class,
16587 and there's no declarator, then we have an explicit type
16588 instantiation. */
16589 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16591 tree type;
16593 type = check_tag_decl (&decl_specifiers,
16594 /*explicit_type_instantiation_p=*/true);
16595 /* Turn access control back on for names used during
16596 template instantiation. */
16597 pop_deferring_access_checks ();
16598 if (type)
16599 do_type_instantiation (type, extension_specifier,
16600 /*complain=*/tf_error);
16602 else
16604 cp_declarator *declarator;
16605 tree decl;
16607 /* Parse the declarator. */
16608 declarator
16609 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16610 /*ctor_dtor_or_conv_p=*/NULL,
16611 /*parenthesized_p=*/NULL,
16612 /*member_p=*/false,
16613 /*friend_p=*/false);
16614 if (declares_class_or_enum & 2)
16615 cp_parser_check_for_definition_in_return_type (declarator,
16616 decl_specifiers.type,
16617 decl_specifiers.locations[ds_type_spec]);
16618 if (declarator != cp_error_declarator)
16620 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16621 permerror (decl_specifiers.locations[ds_inline],
16622 "explicit instantiation shall not use"
16623 " %<inline%> specifier");
16624 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16625 permerror (decl_specifiers.locations[ds_constexpr],
16626 "explicit instantiation shall not use"
16627 " %<constexpr%> specifier");
16629 decl = grokdeclarator (declarator, &decl_specifiers,
16630 NORMAL, 0, &decl_specifiers.attributes);
16631 /* Turn access control back on for names used during
16632 template instantiation. */
16633 pop_deferring_access_checks ();
16634 /* Do the explicit instantiation. */
16635 do_decl_instantiation (decl, extension_specifier);
16637 else
16639 pop_deferring_access_checks ();
16640 /* Skip the body of the explicit instantiation. */
16641 cp_parser_skip_to_end_of_statement (parser);
16644 /* We're done with the instantiation. */
16645 end_explicit_instantiation ();
16647 cp_parser_consume_semicolon_at_end_of_statement (parser);
16649 timevar_pop (TV_TEMPLATE_INST);
16652 /* Parse an explicit-specialization.
16654 explicit-specialization:
16655 template < > declaration
16657 Although the standard says `declaration', what it really means is:
16659 explicit-specialization:
16660 template <> decl-specifier [opt] init-declarator [opt] ;
16661 template <> function-definition
16662 template <> explicit-specialization
16663 template <> template-declaration */
16665 static void
16666 cp_parser_explicit_specialization (cp_parser* parser)
16668 bool need_lang_pop;
16669 cp_token *token = cp_lexer_peek_token (parser->lexer);
16671 /* Look for the `template' keyword. */
16672 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16673 /* Look for the `<'. */
16674 cp_parser_require (parser, CPP_LESS, RT_LESS);
16675 /* Look for the `>'. */
16676 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16677 /* We have processed another parameter list. */
16678 ++parser->num_template_parameter_lists;
16679 /* [temp]
16681 A template ... explicit specialization ... shall not have C
16682 linkage. */
16683 if (current_lang_name == lang_name_c)
16685 error_at (token->location, "template specialization with C linkage");
16686 maybe_show_extern_c_location ();
16687 /* Give it C++ linkage to avoid confusing other parts of the
16688 front end. */
16689 push_lang_context (lang_name_cplusplus);
16690 need_lang_pop = true;
16692 else
16693 need_lang_pop = false;
16694 /* Let the front end know that we are beginning a specialization. */
16695 if (!begin_specialization ())
16697 end_specialization ();
16698 return;
16701 /* If the next keyword is `template', we need to figure out whether
16702 or not we're looking a template-declaration. */
16703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16705 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16706 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16707 cp_parser_template_declaration_after_export (parser,
16708 /*member_p=*/false);
16709 else
16710 cp_parser_explicit_specialization (parser);
16712 else
16713 /* Parse the dependent declaration. */
16714 cp_parser_single_declaration (parser,
16715 /*checks=*/NULL,
16716 /*member_p=*/false,
16717 /*explicit_specialization_p=*/true,
16718 /*friend_p=*/NULL);
16719 /* We're done with the specialization. */
16720 end_specialization ();
16721 /* For the erroneous case of a template with C linkage, we pushed an
16722 implicit C++ linkage scope; exit that scope now. */
16723 if (need_lang_pop)
16724 pop_lang_context ();
16725 /* We're done with this parameter list. */
16726 --parser->num_template_parameter_lists;
16729 /* Parse a type-specifier.
16731 type-specifier:
16732 simple-type-specifier
16733 class-specifier
16734 enum-specifier
16735 elaborated-type-specifier
16736 cv-qualifier
16738 GNU Extension:
16740 type-specifier:
16741 __complex__
16743 Returns a representation of the type-specifier. For a
16744 class-specifier, enum-specifier, or elaborated-type-specifier, a
16745 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16747 The parser flags FLAGS is used to control type-specifier parsing.
16749 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16750 in a decl-specifier-seq.
16752 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16753 class-specifier, enum-specifier, or elaborated-type-specifier, then
16754 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16755 if a type is declared; 2 if it is defined. Otherwise, it is set to
16756 zero.
16758 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16759 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16760 is set to FALSE. */
16762 static tree
16763 cp_parser_type_specifier (cp_parser* parser,
16764 cp_parser_flags flags,
16765 cp_decl_specifier_seq *decl_specs,
16766 bool is_declaration,
16767 int* declares_class_or_enum,
16768 bool* is_cv_qualifier)
16770 tree type_spec = NULL_TREE;
16771 cp_token *token;
16772 enum rid keyword;
16773 cp_decl_spec ds = ds_last;
16775 /* Assume this type-specifier does not declare a new type. */
16776 if (declares_class_or_enum)
16777 *declares_class_or_enum = 0;
16778 /* And that it does not specify a cv-qualifier. */
16779 if (is_cv_qualifier)
16780 *is_cv_qualifier = false;
16781 /* Peek at the next token. */
16782 token = cp_lexer_peek_token (parser->lexer);
16784 /* If we're looking at a keyword, we can use that to guide the
16785 production we choose. */
16786 keyword = token->keyword;
16787 switch (keyword)
16789 case RID_ENUM:
16790 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16791 goto elaborated_type_specifier;
16793 /* Look for the enum-specifier. */
16794 type_spec = cp_parser_enum_specifier (parser);
16795 /* If that worked, we're done. */
16796 if (type_spec)
16798 if (declares_class_or_enum)
16799 *declares_class_or_enum = 2;
16800 if (decl_specs)
16801 cp_parser_set_decl_spec_type (decl_specs,
16802 type_spec,
16803 token,
16804 /*type_definition_p=*/true);
16805 return type_spec;
16807 else
16808 goto elaborated_type_specifier;
16810 /* Any of these indicate either a class-specifier, or an
16811 elaborated-type-specifier. */
16812 case RID_CLASS:
16813 case RID_STRUCT:
16814 case RID_UNION:
16815 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16816 goto elaborated_type_specifier;
16818 /* Parse tentatively so that we can back up if we don't find a
16819 class-specifier. */
16820 cp_parser_parse_tentatively (parser);
16821 /* Look for the class-specifier. */
16822 type_spec = cp_parser_class_specifier (parser);
16823 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16824 /* If that worked, we're done. */
16825 if (cp_parser_parse_definitely (parser))
16827 if (declares_class_or_enum)
16828 *declares_class_or_enum = 2;
16829 if (decl_specs)
16830 cp_parser_set_decl_spec_type (decl_specs,
16831 type_spec,
16832 token,
16833 /*type_definition_p=*/true);
16834 return type_spec;
16837 /* Fall through. */
16838 elaborated_type_specifier:
16839 /* We're declaring (not defining) a class or enum. */
16840 if (declares_class_or_enum)
16841 *declares_class_or_enum = 1;
16843 /* Fall through. */
16844 case RID_TYPENAME:
16845 /* Look for an elaborated-type-specifier. */
16846 type_spec
16847 = (cp_parser_elaborated_type_specifier
16848 (parser,
16849 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16850 is_declaration));
16851 if (decl_specs)
16852 cp_parser_set_decl_spec_type (decl_specs,
16853 type_spec,
16854 token,
16855 /*type_definition_p=*/false);
16856 return type_spec;
16858 case RID_CONST:
16859 ds = ds_const;
16860 if (is_cv_qualifier)
16861 *is_cv_qualifier = true;
16862 break;
16864 case RID_VOLATILE:
16865 ds = ds_volatile;
16866 if (is_cv_qualifier)
16867 *is_cv_qualifier = true;
16868 break;
16870 case RID_RESTRICT:
16871 ds = ds_restrict;
16872 if (is_cv_qualifier)
16873 *is_cv_qualifier = true;
16874 break;
16876 case RID_COMPLEX:
16877 /* The `__complex__' keyword is a GNU extension. */
16878 ds = ds_complex;
16879 break;
16881 default:
16882 break;
16885 /* Handle simple keywords. */
16886 if (ds != ds_last)
16888 if (decl_specs)
16890 set_and_check_decl_spec_loc (decl_specs, ds, token);
16891 decl_specs->any_specifiers_p = true;
16893 return cp_lexer_consume_token (parser->lexer)->u.value;
16896 /* If we do not already have a type-specifier, assume we are looking
16897 at a simple-type-specifier. */
16898 type_spec = cp_parser_simple_type_specifier (parser,
16899 decl_specs,
16900 flags);
16902 /* If we didn't find a type-specifier, and a type-specifier was not
16903 optional in this context, issue an error message. */
16904 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16906 cp_parser_error (parser, "expected type specifier");
16907 return error_mark_node;
16910 return type_spec;
16913 /* Parse a simple-type-specifier.
16915 simple-type-specifier:
16916 :: [opt] nested-name-specifier [opt] type-name
16917 :: [opt] nested-name-specifier template template-id
16918 char
16919 wchar_t
16920 bool
16921 short
16923 long
16924 signed
16925 unsigned
16926 float
16927 double
16928 void
16930 C++11 Extension:
16932 simple-type-specifier:
16933 auto
16934 decltype ( expression )
16935 char16_t
16936 char32_t
16937 __underlying_type ( type-id )
16939 C++17 extension:
16941 nested-name-specifier(opt) template-name
16943 GNU Extension:
16945 simple-type-specifier:
16946 __int128
16947 __typeof__ unary-expression
16948 __typeof__ ( type-id )
16949 __typeof__ ( type-id ) { initializer-list , [opt] }
16951 Concepts Extension:
16953 simple-type-specifier:
16954 constrained-type-specifier
16956 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16957 appropriately updated. */
16959 static tree
16960 cp_parser_simple_type_specifier (cp_parser* parser,
16961 cp_decl_specifier_seq *decl_specs,
16962 cp_parser_flags flags)
16964 tree type = NULL_TREE;
16965 cp_token *token;
16966 int idx;
16968 /* Peek at the next token. */
16969 token = cp_lexer_peek_token (parser->lexer);
16971 /* If we're looking at a keyword, things are easy. */
16972 switch (token->keyword)
16974 case RID_CHAR:
16975 if (decl_specs)
16976 decl_specs->explicit_char_p = true;
16977 type = char_type_node;
16978 break;
16979 case RID_CHAR16:
16980 type = char16_type_node;
16981 break;
16982 case RID_CHAR32:
16983 type = char32_type_node;
16984 break;
16985 case RID_WCHAR:
16986 type = wchar_type_node;
16987 break;
16988 case RID_BOOL:
16989 type = boolean_type_node;
16990 break;
16991 case RID_SHORT:
16992 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16993 type = short_integer_type_node;
16994 break;
16995 case RID_INT:
16996 if (decl_specs)
16997 decl_specs->explicit_int_p = true;
16998 type = integer_type_node;
16999 break;
17000 case RID_INT_N_0:
17001 case RID_INT_N_1:
17002 case RID_INT_N_2:
17003 case RID_INT_N_3:
17004 idx = token->keyword - RID_INT_N_0;
17005 if (! int_n_enabled_p [idx])
17006 break;
17007 if (decl_specs)
17009 decl_specs->explicit_intN_p = true;
17010 decl_specs->int_n_idx = idx;
17012 type = int_n_trees [idx].signed_type;
17013 break;
17014 case RID_LONG:
17015 if (decl_specs)
17016 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17017 type = long_integer_type_node;
17018 break;
17019 case RID_SIGNED:
17020 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17021 type = integer_type_node;
17022 break;
17023 case RID_UNSIGNED:
17024 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17025 type = unsigned_type_node;
17026 break;
17027 case RID_FLOAT:
17028 type = float_type_node;
17029 break;
17030 case RID_DOUBLE:
17031 type = double_type_node;
17032 break;
17033 case RID_VOID:
17034 type = void_type_node;
17035 break;
17037 case RID_AUTO:
17038 maybe_warn_cpp0x (CPP0X_AUTO);
17039 if (parser->auto_is_implicit_function_template_parm_p)
17041 /* The 'auto' might be the placeholder return type for a function decl
17042 with trailing return type. */
17043 bool have_trailing_return_fn_decl = false;
17045 cp_parser_parse_tentatively (parser);
17046 cp_lexer_consume_token (parser->lexer);
17047 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17048 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17049 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17050 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17052 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17054 cp_lexer_consume_token (parser->lexer);
17055 cp_parser_skip_to_closing_parenthesis (parser,
17056 /*recovering*/false,
17057 /*or_comma*/false,
17058 /*consume_paren*/true);
17059 continue;
17062 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17064 have_trailing_return_fn_decl = true;
17065 break;
17068 cp_lexer_consume_token (parser->lexer);
17070 cp_parser_abort_tentative_parse (parser);
17072 if (have_trailing_return_fn_decl)
17074 type = make_auto ();
17075 break;
17078 if (cxx_dialect >= cxx14)
17080 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17081 type = TREE_TYPE (type);
17083 else
17084 type = error_mark_node;
17086 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17088 if (cxx_dialect < cxx14)
17089 error_at (token->location,
17090 "use of %<auto%> in lambda parameter declaration "
17091 "only available with "
17092 "-std=c++14 or -std=gnu++14");
17094 else if (cxx_dialect < cxx14)
17095 error_at (token->location,
17096 "use of %<auto%> in parameter declaration "
17097 "only available with "
17098 "-std=c++14 or -std=gnu++14");
17099 else if (!flag_concepts)
17100 pedwarn (token->location, OPT_Wpedantic,
17101 "ISO C++ forbids use of %<auto%> in parameter "
17102 "declaration");
17104 else
17105 type = make_auto ();
17106 break;
17108 case RID_DECLTYPE:
17109 /* Since DR 743, decltype can either be a simple-type-specifier by
17110 itself or begin a nested-name-specifier. Parsing it will replace
17111 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17112 handling below decide what to do. */
17113 cp_parser_decltype (parser);
17114 cp_lexer_set_token_position (parser->lexer, token);
17115 break;
17117 case RID_TYPEOF:
17118 /* Consume the `typeof' token. */
17119 cp_lexer_consume_token (parser->lexer);
17120 /* Parse the operand to `typeof'. */
17121 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17122 /* If it is not already a TYPE, take its type. */
17123 if (!TYPE_P (type))
17124 type = finish_typeof (type);
17126 if (decl_specs)
17127 cp_parser_set_decl_spec_type (decl_specs, type,
17128 token,
17129 /*type_definition_p=*/false);
17131 return type;
17133 case RID_UNDERLYING_TYPE:
17134 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17135 if (decl_specs)
17136 cp_parser_set_decl_spec_type (decl_specs, type,
17137 token,
17138 /*type_definition_p=*/false);
17140 return type;
17142 case RID_BASES:
17143 case RID_DIRECT_BASES:
17144 type = cp_parser_trait_expr (parser, token->keyword);
17145 if (decl_specs)
17146 cp_parser_set_decl_spec_type (decl_specs, type,
17147 token,
17148 /*type_definition_p=*/false);
17149 return type;
17150 default:
17151 break;
17154 /* If token is an already-parsed decltype not followed by ::,
17155 it's a simple-type-specifier. */
17156 if (token->type == CPP_DECLTYPE
17157 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17159 type = saved_checks_value (token->u.tree_check_value);
17160 if (decl_specs)
17162 cp_parser_set_decl_spec_type (decl_specs, type,
17163 token,
17164 /*type_definition_p=*/false);
17165 /* Remember that we are handling a decltype in order to
17166 implement the resolution of DR 1510 when the argument
17167 isn't instantiation dependent. */
17168 decl_specs->decltype_p = true;
17170 cp_lexer_consume_token (parser->lexer);
17171 return type;
17174 /* If the type-specifier was for a built-in type, we're done. */
17175 if (type)
17177 /* Record the type. */
17178 if (decl_specs
17179 && (token->keyword != RID_SIGNED
17180 && token->keyword != RID_UNSIGNED
17181 && token->keyword != RID_SHORT
17182 && token->keyword != RID_LONG))
17183 cp_parser_set_decl_spec_type (decl_specs,
17184 type,
17185 token,
17186 /*type_definition_p=*/false);
17187 if (decl_specs)
17188 decl_specs->any_specifiers_p = true;
17190 /* Consume the token. */
17191 cp_lexer_consume_token (parser->lexer);
17193 if (type == error_mark_node)
17194 return error_mark_node;
17196 /* There is no valid C++ program where a non-template type is
17197 followed by a "<". That usually indicates that the user thought
17198 that the type was a template. */
17199 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17200 token->location);
17202 return TYPE_NAME (type);
17205 /* The type-specifier must be a user-defined type. */
17206 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17208 bool qualified_p;
17209 bool global_p;
17211 /* Don't gobble tokens or issue error messages if this is an
17212 optional type-specifier. */
17213 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17214 cp_parser_parse_tentatively (parser);
17216 token = cp_lexer_peek_token (parser->lexer);
17218 /* Look for the optional `::' operator. */
17219 global_p
17220 = (cp_parser_global_scope_opt (parser,
17221 /*current_scope_valid_p=*/false)
17222 != NULL_TREE);
17223 /* Look for the nested-name specifier. */
17224 qualified_p
17225 = (cp_parser_nested_name_specifier_opt (parser,
17226 /*typename_keyword_p=*/false,
17227 /*check_dependency_p=*/true,
17228 /*type_p=*/false,
17229 /*is_declaration=*/false)
17230 != NULL_TREE);
17231 /* If we have seen a nested-name-specifier, and the next token
17232 is `template', then we are using the template-id production. */
17233 if (parser->scope
17234 && cp_parser_optional_template_keyword (parser))
17236 /* Look for the template-id. */
17237 type = cp_parser_template_id (parser,
17238 /*template_keyword_p=*/true,
17239 /*check_dependency_p=*/true,
17240 none_type,
17241 /*is_declaration=*/false);
17242 /* If the template-id did not name a type, we are out of
17243 luck. */
17244 if (TREE_CODE (type) != TYPE_DECL)
17246 cp_parser_error (parser, "expected template-id for type");
17247 type = NULL_TREE;
17250 /* Otherwise, look for a type-name. */
17251 else
17252 type = cp_parser_type_name (parser);
17253 /* Keep track of all name-lookups performed in class scopes. */
17254 if (type
17255 && !global_p
17256 && !qualified_p
17257 && TREE_CODE (type) == TYPE_DECL
17258 && identifier_p (DECL_NAME (type)))
17259 maybe_note_name_used_in_class (DECL_NAME (type), type);
17260 /* If it didn't work out, we don't have a TYPE. */
17261 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17262 && !cp_parser_parse_definitely (parser))
17263 type = NULL_TREE;
17264 if (!type && cxx_dialect >= cxx17)
17266 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17267 cp_parser_parse_tentatively (parser);
17269 cp_parser_global_scope_opt (parser,
17270 /*current_scope_valid_p=*/false);
17271 cp_parser_nested_name_specifier_opt (parser,
17272 /*typename_keyword_p=*/false,
17273 /*check_dependency_p=*/true,
17274 /*type_p=*/false,
17275 /*is_declaration=*/false);
17276 tree name = cp_parser_identifier (parser);
17277 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17278 && parser->scope != error_mark_node)
17280 tree tmpl = cp_parser_lookup_name (parser, name,
17281 none_type,
17282 /*is_template=*/false,
17283 /*is_namespace=*/false,
17284 /*check_dependency=*/true,
17285 /*ambiguous_decls=*/NULL,
17286 token->location);
17287 if (tmpl && tmpl != error_mark_node
17288 && (DECL_CLASS_TEMPLATE_P (tmpl)
17289 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17290 type = make_template_placeholder (tmpl);
17291 else
17293 type = error_mark_node;
17294 if (!cp_parser_simulate_error (parser))
17295 cp_parser_name_lookup_error (parser, name, tmpl,
17296 NLE_TYPE, token->location);
17299 else
17300 type = error_mark_node;
17302 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17303 && !cp_parser_parse_definitely (parser))
17304 type = NULL_TREE;
17306 if (type && decl_specs)
17307 cp_parser_set_decl_spec_type (decl_specs, type,
17308 token,
17309 /*type_definition_p=*/false);
17312 /* If we didn't get a type-name, issue an error message. */
17313 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17315 cp_parser_error (parser, "expected type-name");
17316 return error_mark_node;
17319 if (type && type != error_mark_node)
17321 /* See if TYPE is an Objective-C type, and if so, parse and
17322 accept any protocol references following it. Do this before
17323 the cp_parser_check_for_invalid_template_id() call, because
17324 Objective-C types can be followed by '<...>' which would
17325 enclose protocol names rather than template arguments, and so
17326 everything is fine. */
17327 if (c_dialect_objc () && !parser->scope
17328 && (objc_is_id (type) || objc_is_class_name (type)))
17330 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17331 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17333 /* Clobber the "unqualified" type previously entered into
17334 DECL_SPECS with the new, improved protocol-qualified version. */
17335 if (decl_specs)
17336 decl_specs->type = qual_type;
17338 return qual_type;
17341 /* There is no valid C++ program where a non-template type is
17342 followed by a "<". That usually indicates that the user
17343 thought that the type was a template. */
17344 cp_parser_check_for_invalid_template_id (parser, type,
17345 none_type,
17346 token->location);
17349 return type;
17352 /* Parse a type-name.
17354 type-name:
17355 class-name
17356 enum-name
17357 typedef-name
17358 simple-template-id [in c++0x]
17360 enum-name:
17361 identifier
17363 typedef-name:
17364 identifier
17366 Concepts:
17368 type-name:
17369 concept-name
17370 partial-concept-id
17372 concept-name:
17373 identifier
17375 Returns a TYPE_DECL for the type. */
17377 static tree
17378 cp_parser_type_name (cp_parser* parser)
17380 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17383 /* See above. */
17384 static tree
17385 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17387 tree type_decl;
17389 /* We can't know yet whether it is a class-name or not. */
17390 cp_parser_parse_tentatively (parser);
17391 /* Try a class-name. */
17392 type_decl = cp_parser_class_name (parser,
17393 typename_keyword_p,
17394 /*template_keyword_p=*/false,
17395 none_type,
17396 /*check_dependency_p=*/true,
17397 /*class_head_p=*/false,
17398 /*is_declaration=*/false);
17399 /* If it's not a class-name, keep looking. */
17400 if (!cp_parser_parse_definitely (parser))
17402 if (cxx_dialect < cxx11)
17403 /* It must be a typedef-name or an enum-name. */
17404 return cp_parser_nonclass_name (parser);
17406 cp_parser_parse_tentatively (parser);
17407 /* It is either a simple-template-id representing an
17408 instantiation of an alias template... */
17409 type_decl = cp_parser_template_id (parser,
17410 /*template_keyword_p=*/false,
17411 /*check_dependency_p=*/true,
17412 none_type,
17413 /*is_declaration=*/false);
17414 /* Note that this must be an instantiation of an alias template
17415 because [temp.names]/6 says:
17417 A template-id that names an alias template specialization
17418 is a type-name.
17420 Whereas [temp.names]/7 says:
17422 A simple-template-id that names a class template
17423 specialization is a class-name.
17425 With concepts, this could also be a partial-concept-id that
17426 declares a non-type template parameter. */
17427 if (type_decl != NULL_TREE
17428 && TREE_CODE (type_decl) == TYPE_DECL
17429 && TYPE_DECL_ALIAS_P (type_decl))
17430 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17431 else if (is_constrained_parameter (type_decl))
17432 /* Don't do anything. */ ;
17433 else
17434 cp_parser_simulate_error (parser);
17436 if (!cp_parser_parse_definitely (parser))
17437 /* ... Or a typedef-name or an enum-name. */
17438 return cp_parser_nonclass_name (parser);
17441 return type_decl;
17444 /* Check if DECL and ARGS can form a constrained-type-specifier.
17445 If ARGS is non-null, we try to form a concept check of the
17446 form DECL<?, ARGS> where ? is a wildcard that matches any
17447 kind of template argument. If ARGS is NULL, then we try to
17448 form a concept check of the form DECL<?>. */
17450 static tree
17451 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17452 tree decl, tree args)
17454 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17456 /* If we a constrained-type-specifier cannot be deduced. */
17457 if (parser->prevent_constrained_type_specifiers)
17458 return NULL_TREE;
17460 /* A constrained type specifier can only be found in an
17461 overload set or as a reference to a template declaration.
17463 FIXME: This might be masking a bug. It's possible that
17464 that the deduction below is causing template specializations
17465 to be formed with the wildcard as an argument. */
17466 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17467 return NULL_TREE;
17469 /* Try to build a call expression that evaluates the
17470 concept. This can fail if the overload set refers
17471 only to non-templates. */
17472 tree placeholder = build_nt (WILDCARD_DECL);
17473 tree check = build_concept_check (decl, placeholder, args);
17474 if (check == error_mark_node)
17475 return NULL_TREE;
17477 /* Deduce the checked constraint and the prototype parameter.
17479 FIXME: In certain cases, failure to deduce should be a
17480 diagnosable error. */
17481 tree conc;
17482 tree proto;
17483 if (!deduce_constrained_parameter (check, conc, proto))
17484 return NULL_TREE;
17486 /* In template parameter scope, this results in a constrained
17487 parameter. Return a descriptor of that parm. */
17488 if (processing_template_parmlist)
17489 return build_constrained_parameter (conc, proto, args);
17491 /* In a parameter-declaration-clause, constrained-type
17492 specifiers result in invented template parameters. */
17493 if (parser->auto_is_implicit_function_template_parm_p)
17495 tree x = build_constrained_parameter (conc, proto, args);
17496 return synthesize_implicit_template_parm (parser, x);
17498 else
17500 /* Otherwise, we're in a context where the constrained
17501 type name is deduced and the constraint applies
17502 after deduction. */
17503 return make_constrained_auto (conc, args);
17506 return NULL_TREE;
17509 /* If DECL refers to a concept, return a TYPE_DECL representing
17510 the result of using the constrained type specifier in the
17511 current context. DECL refers to a concept if
17513 - it is an overload set containing a function concept taking a single
17514 type argument, or
17516 - it is a variable concept taking a single type argument. */
17518 static tree
17519 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17521 if (flag_concepts
17522 && (TREE_CODE (decl) == OVERLOAD
17523 || BASELINK_P (decl)
17524 || variable_concept_p (decl)))
17525 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17526 else
17527 return NULL_TREE;
17530 /* Check if DECL and ARGS form a partial-concept-id. If so,
17531 assign ID to the resulting constrained placeholder.
17533 Returns true if the partial-concept-id designates a placeholder
17534 and false otherwise. Note that *id is set to NULL_TREE in
17535 this case. */
17537 static tree
17538 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17540 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17543 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17544 or a concept-name.
17546 enum-name:
17547 identifier
17549 typedef-name:
17550 identifier
17552 concept-name:
17553 identifier
17555 Returns a TYPE_DECL for the type. */
17557 static tree
17558 cp_parser_nonclass_name (cp_parser* parser)
17560 tree type_decl;
17561 tree identifier;
17563 cp_token *token = cp_lexer_peek_token (parser->lexer);
17564 identifier = cp_parser_identifier (parser);
17565 if (identifier == error_mark_node)
17566 return error_mark_node;
17568 /* Look up the type-name. */
17569 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17571 type_decl = strip_using_decl (type_decl);
17573 /* If we found an overload set, then it may refer to a concept-name. */
17574 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17575 type_decl = decl;
17577 if (TREE_CODE (type_decl) != TYPE_DECL
17578 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17580 /* See if this is an Objective-C type. */
17581 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17582 tree type = objc_get_protocol_qualified_type (identifier, protos);
17583 if (type)
17584 type_decl = TYPE_NAME (type);
17587 /* Issue an error if we did not find a type-name. */
17588 if (TREE_CODE (type_decl) != TYPE_DECL
17589 /* In Objective-C, we have the complication that class names are
17590 normally type names and start declarations (eg, the
17591 "NSObject" in "NSObject *object;"), but can be used in an
17592 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17593 is an expression. So, a classname followed by a dot is not a
17594 valid type-name. */
17595 || (objc_is_class_name (TREE_TYPE (type_decl))
17596 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17598 if (!cp_parser_simulate_error (parser))
17599 cp_parser_name_lookup_error (parser, identifier, type_decl,
17600 NLE_TYPE, token->location);
17601 return error_mark_node;
17603 /* Remember that the name was used in the definition of the
17604 current class so that we can check later to see if the
17605 meaning would have been different after the class was
17606 entirely defined. */
17607 else if (type_decl != error_mark_node
17608 && !parser->scope)
17609 maybe_note_name_used_in_class (identifier, type_decl);
17611 return type_decl;
17614 /* Parse an elaborated-type-specifier. Note that the grammar given
17615 here incorporates the resolution to DR68.
17617 elaborated-type-specifier:
17618 class-key :: [opt] nested-name-specifier [opt] identifier
17619 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17620 enum-key :: [opt] nested-name-specifier [opt] identifier
17621 typename :: [opt] nested-name-specifier identifier
17622 typename :: [opt] nested-name-specifier template [opt]
17623 template-id
17625 GNU extension:
17627 elaborated-type-specifier:
17628 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17629 class-key attributes :: [opt] nested-name-specifier [opt]
17630 template [opt] template-id
17631 enum attributes :: [opt] nested-name-specifier [opt] identifier
17633 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17634 declared `friend'. If IS_DECLARATION is TRUE, then this
17635 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17636 something is being declared.
17638 Returns the TYPE specified. */
17640 static tree
17641 cp_parser_elaborated_type_specifier (cp_parser* parser,
17642 bool is_friend,
17643 bool is_declaration)
17645 enum tag_types tag_type;
17646 tree identifier;
17647 tree type = NULL_TREE;
17648 tree attributes = NULL_TREE;
17649 tree globalscope;
17650 cp_token *token = NULL;
17652 /* See if we're looking at the `enum' keyword. */
17653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17655 /* Consume the `enum' token. */
17656 cp_lexer_consume_token (parser->lexer);
17657 /* Remember that it's an enumeration type. */
17658 tag_type = enum_type;
17659 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17660 enums) is used here. */
17661 cp_token *token = cp_lexer_peek_token (parser->lexer);
17662 if (cp_parser_is_keyword (token, RID_CLASS)
17663 || cp_parser_is_keyword (token, RID_STRUCT))
17665 gcc_rich_location richloc (token->location);
17666 richloc.add_range (input_location, false);
17667 richloc.add_fixit_remove ();
17668 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17669 "a scoped enum must not use the %qD keyword",
17670 token->u.value);
17671 /* Consume the `struct' or `class' and parse it anyway. */
17672 cp_lexer_consume_token (parser->lexer);
17674 /* Parse the attributes. */
17675 attributes = cp_parser_attributes_opt (parser);
17677 /* Or, it might be `typename'. */
17678 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17679 RID_TYPENAME))
17681 /* Consume the `typename' token. */
17682 cp_lexer_consume_token (parser->lexer);
17683 /* Remember that it's a `typename' type. */
17684 tag_type = typename_type;
17686 /* Otherwise it must be a class-key. */
17687 else
17689 tag_type = cp_parser_class_key (parser);
17690 if (tag_type == none_type)
17691 return error_mark_node;
17692 /* Parse the attributes. */
17693 attributes = cp_parser_attributes_opt (parser);
17696 /* Look for the `::' operator. */
17697 globalscope = cp_parser_global_scope_opt (parser,
17698 /*current_scope_valid_p=*/false);
17699 /* Look for the nested-name-specifier. */
17700 tree nested_name_specifier;
17701 if (tag_type == typename_type && !globalscope)
17703 nested_name_specifier
17704 = cp_parser_nested_name_specifier (parser,
17705 /*typename_keyword_p=*/true,
17706 /*check_dependency_p=*/true,
17707 /*type_p=*/true,
17708 is_declaration);
17709 if (!nested_name_specifier)
17710 return error_mark_node;
17712 else
17713 /* Even though `typename' is not present, the proposed resolution
17714 to Core Issue 180 says that in `class A<T>::B', `B' should be
17715 considered a type-name, even if `A<T>' is dependent. */
17716 nested_name_specifier
17717 = cp_parser_nested_name_specifier_opt (parser,
17718 /*typename_keyword_p=*/true,
17719 /*check_dependency_p=*/true,
17720 /*type_p=*/true,
17721 is_declaration);
17722 /* For everything but enumeration types, consider a template-id.
17723 For an enumeration type, consider only a plain identifier. */
17724 if (tag_type != enum_type)
17726 bool template_p = false;
17727 tree decl;
17729 /* Allow the `template' keyword. */
17730 template_p = cp_parser_optional_template_keyword (parser);
17731 /* If we didn't see `template', we don't know if there's a
17732 template-id or not. */
17733 if (!template_p)
17734 cp_parser_parse_tentatively (parser);
17735 /* Parse the template-id. */
17736 token = cp_lexer_peek_token (parser->lexer);
17737 decl = cp_parser_template_id (parser, template_p,
17738 /*check_dependency_p=*/true,
17739 tag_type,
17740 is_declaration);
17741 /* If we didn't find a template-id, look for an ordinary
17742 identifier. */
17743 if (!template_p && !cp_parser_parse_definitely (parser))
17745 /* We can get here when cp_parser_template_id, called by
17746 cp_parser_class_name with tag_type == none_type, succeeds
17747 and caches a BASELINK. Then, when called again here,
17748 instead of failing and returning an error_mark_node
17749 returns it (see template/typename17.C in C++11).
17750 ??? Could we diagnose this earlier? */
17751 else if (tag_type == typename_type && BASELINK_P (decl))
17753 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17754 type = error_mark_node;
17756 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17757 in effect, then we must assume that, upon instantiation, the
17758 template will correspond to a class. */
17759 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17760 && tag_type == typename_type)
17761 type = make_typename_type (parser->scope, decl,
17762 typename_type,
17763 /*complain=*/tf_error);
17764 /* If the `typename' keyword is in effect and DECL is not a type
17765 decl, then type is non existent. */
17766 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17768 else if (TREE_CODE (decl) == TYPE_DECL)
17770 type = check_elaborated_type_specifier (tag_type, decl,
17771 /*allow_template_p=*/true);
17773 /* If the next token is a semicolon, this must be a specialization,
17774 instantiation, or friend declaration. Check the scope while we
17775 still know whether or not we had a nested-name-specifier. */
17776 if (type != error_mark_node
17777 && !nested_name_specifier && !is_friend
17778 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17779 check_unqualified_spec_or_inst (type, token->location);
17781 else if (decl == error_mark_node)
17782 type = error_mark_node;
17785 if (!type)
17787 token = cp_lexer_peek_token (parser->lexer);
17788 identifier = cp_parser_identifier (parser);
17790 if (identifier == error_mark_node)
17792 parser->scope = NULL_TREE;
17793 return error_mark_node;
17796 /* For a `typename', we needn't call xref_tag. */
17797 if (tag_type == typename_type
17798 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17799 return cp_parser_make_typename_type (parser, identifier,
17800 token->location);
17802 /* Template parameter lists apply only if we are not within a
17803 function parameter list. */
17804 bool template_parm_lists_apply
17805 = parser->num_template_parameter_lists;
17806 if (template_parm_lists_apply)
17807 for (cp_binding_level *s = current_binding_level;
17808 s && s->kind != sk_template_parms;
17809 s = s->level_chain)
17810 if (s->kind == sk_function_parms)
17811 template_parm_lists_apply = false;
17813 /* Look up a qualified name in the usual way. */
17814 if (parser->scope)
17816 tree decl;
17817 tree ambiguous_decls;
17819 decl = cp_parser_lookup_name (parser, identifier,
17820 tag_type,
17821 /*is_template=*/false,
17822 /*is_namespace=*/false,
17823 /*check_dependency=*/true,
17824 &ambiguous_decls,
17825 token->location);
17827 /* If the lookup was ambiguous, an error will already have been
17828 issued. */
17829 if (ambiguous_decls)
17830 return error_mark_node;
17832 /* If we are parsing friend declaration, DECL may be a
17833 TEMPLATE_DECL tree node here. However, we need to check
17834 whether this TEMPLATE_DECL results in valid code. Consider
17835 the following example:
17837 namespace N {
17838 template <class T> class C {};
17840 class X {
17841 template <class T> friend class N::C; // #1, valid code
17843 template <class T> class Y {
17844 friend class N::C; // #2, invalid code
17847 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17848 name lookup of `N::C'. We see that friend declaration must
17849 be template for the code to be valid. Note that
17850 processing_template_decl does not work here since it is
17851 always 1 for the above two cases. */
17853 decl = (cp_parser_maybe_treat_template_as_class
17854 (decl, /*tag_name_p=*/is_friend
17855 && template_parm_lists_apply));
17857 if (TREE_CODE (decl) != TYPE_DECL)
17859 cp_parser_diagnose_invalid_type_name (parser,
17860 identifier,
17861 token->location);
17862 return error_mark_node;
17865 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17867 bool allow_template = (template_parm_lists_apply
17868 || DECL_SELF_REFERENCE_P (decl));
17869 type = check_elaborated_type_specifier (tag_type, decl,
17870 allow_template);
17872 if (type == error_mark_node)
17873 return error_mark_node;
17876 /* Forward declarations of nested types, such as
17878 class C1::C2;
17879 class C1::C2::C3;
17881 are invalid unless all components preceding the final '::'
17882 are complete. If all enclosing types are complete, these
17883 declarations become merely pointless.
17885 Invalid forward declarations of nested types are errors
17886 caught elsewhere in parsing. Those that are pointless arrive
17887 here. */
17889 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17890 && !is_friend && !processing_explicit_instantiation)
17891 warning (0, "declaration %qD does not declare anything", decl);
17893 type = TREE_TYPE (decl);
17895 else
17897 /* An elaborated-type-specifier sometimes introduces a new type and
17898 sometimes names an existing type. Normally, the rule is that it
17899 introduces a new type only if there is not an existing type of
17900 the same name already in scope. For example, given:
17902 struct S {};
17903 void f() { struct S s; }
17905 the `struct S' in the body of `f' is the same `struct S' as in
17906 the global scope; the existing definition is used. However, if
17907 there were no global declaration, this would introduce a new
17908 local class named `S'.
17910 An exception to this rule applies to the following code:
17912 namespace N { struct S; }
17914 Here, the elaborated-type-specifier names a new type
17915 unconditionally; even if there is already an `S' in the
17916 containing scope this declaration names a new type.
17917 This exception only applies if the elaborated-type-specifier
17918 forms the complete declaration:
17920 [class.name]
17922 A declaration consisting solely of `class-key identifier ;' is
17923 either a redeclaration of the name in the current scope or a
17924 forward declaration of the identifier as a class name. It
17925 introduces the name into the current scope.
17927 We are in this situation precisely when the next token is a `;'.
17929 An exception to the exception is that a `friend' declaration does
17930 *not* name a new type; i.e., given:
17932 struct S { friend struct T; };
17934 `T' is not a new type in the scope of `S'.
17936 Also, `new struct S' or `sizeof (struct S)' never results in the
17937 definition of a new type; a new type can only be declared in a
17938 declaration context. */
17940 tag_scope ts;
17941 bool template_p;
17943 if (is_friend)
17944 /* Friends have special name lookup rules. */
17945 ts = ts_within_enclosing_non_class;
17946 else if (is_declaration
17947 && cp_lexer_next_token_is (parser->lexer,
17948 CPP_SEMICOLON))
17949 /* This is a `class-key identifier ;' */
17950 ts = ts_current;
17951 else
17952 ts = ts_global;
17954 template_p =
17955 (template_parm_lists_apply
17956 && (cp_parser_next_token_starts_class_definition_p (parser)
17957 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17958 /* An unqualified name was used to reference this type, so
17959 there were no qualifying templates. */
17960 if (template_parm_lists_apply
17961 && !cp_parser_check_template_parameters (parser,
17962 /*num_templates=*/0,
17963 token->location,
17964 /*declarator=*/NULL))
17965 return error_mark_node;
17966 type = xref_tag (tag_type, identifier, ts, template_p);
17970 if (type == error_mark_node)
17971 return error_mark_node;
17973 /* Allow attributes on forward declarations of classes. */
17974 if (attributes)
17976 if (TREE_CODE (type) == TYPENAME_TYPE)
17977 warning (OPT_Wattributes,
17978 "attributes ignored on uninstantiated type");
17979 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17980 && ! processing_explicit_instantiation)
17981 warning (OPT_Wattributes,
17982 "attributes ignored on template instantiation");
17983 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17984 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17985 else
17986 warning (OPT_Wattributes,
17987 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17990 if (tag_type != enum_type)
17992 /* Indicate whether this class was declared as a `class' or as a
17993 `struct'. */
17994 if (CLASS_TYPE_P (type))
17995 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17996 cp_parser_check_class_key (tag_type, type);
17999 /* A "<" cannot follow an elaborated type specifier. If that
18000 happens, the user was probably trying to form a template-id. */
18001 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18002 token->location);
18004 return type;
18007 /* Parse an enum-specifier.
18009 enum-specifier:
18010 enum-head { enumerator-list [opt] }
18011 enum-head { enumerator-list , } [C++0x]
18013 enum-head:
18014 enum-key identifier [opt] enum-base [opt]
18015 enum-key nested-name-specifier identifier enum-base [opt]
18017 enum-key:
18018 enum
18019 enum class [C++0x]
18020 enum struct [C++0x]
18022 enum-base: [C++0x]
18023 : type-specifier-seq
18025 opaque-enum-specifier:
18026 enum-key identifier enum-base [opt] ;
18028 GNU Extensions:
18029 enum-key attributes[opt] identifier [opt] enum-base [opt]
18030 { enumerator-list [opt] }attributes[opt]
18031 enum-key attributes[opt] identifier [opt] enum-base [opt]
18032 { enumerator-list, }attributes[opt] [C++0x]
18034 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18035 if the token stream isn't an enum-specifier after all. */
18037 static tree
18038 cp_parser_enum_specifier (cp_parser* parser)
18040 tree identifier;
18041 tree type = NULL_TREE;
18042 tree prev_scope;
18043 tree nested_name_specifier = NULL_TREE;
18044 tree attributes;
18045 bool scoped_enum_p = false;
18046 bool has_underlying_type = false;
18047 bool nested_being_defined = false;
18048 bool new_value_list = false;
18049 bool is_new_type = false;
18050 bool is_unnamed = false;
18051 tree underlying_type = NULL_TREE;
18052 cp_token *type_start_token = NULL;
18053 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18055 parser->colon_corrects_to_scope_p = false;
18057 /* Parse tentatively so that we can back up if we don't find a
18058 enum-specifier. */
18059 cp_parser_parse_tentatively (parser);
18061 /* Caller guarantees that the current token is 'enum', an identifier
18062 possibly follows, and the token after that is an opening brace.
18063 If we don't have an identifier, fabricate an anonymous name for
18064 the enumeration being defined. */
18065 cp_lexer_consume_token (parser->lexer);
18067 /* Parse the "class" or "struct", which indicates a scoped
18068 enumeration type in C++0x. */
18069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18070 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18072 if (cxx_dialect < cxx11)
18073 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18075 /* Consume the `struct' or `class' token. */
18076 cp_lexer_consume_token (parser->lexer);
18078 scoped_enum_p = true;
18081 attributes = cp_parser_attributes_opt (parser);
18083 /* Clear the qualification. */
18084 parser->scope = NULL_TREE;
18085 parser->qualifying_scope = NULL_TREE;
18086 parser->object_scope = NULL_TREE;
18088 /* Figure out in what scope the declaration is being placed. */
18089 prev_scope = current_scope ();
18091 type_start_token = cp_lexer_peek_token (parser->lexer);
18093 push_deferring_access_checks (dk_no_check);
18094 nested_name_specifier
18095 = cp_parser_nested_name_specifier_opt (parser,
18096 /*typename_keyword_p=*/true,
18097 /*check_dependency_p=*/false,
18098 /*type_p=*/false,
18099 /*is_declaration=*/false);
18101 if (nested_name_specifier)
18103 tree name;
18105 identifier = cp_parser_identifier (parser);
18106 name = cp_parser_lookup_name (parser, identifier,
18107 enum_type,
18108 /*is_template=*/false,
18109 /*is_namespace=*/false,
18110 /*check_dependency=*/true,
18111 /*ambiguous_decls=*/NULL,
18112 input_location);
18113 if (name && name != error_mark_node)
18115 type = TREE_TYPE (name);
18116 if (TREE_CODE (type) == TYPENAME_TYPE)
18118 /* Are template enums allowed in ISO? */
18119 if (template_parm_scope_p ())
18120 pedwarn (type_start_token->location, OPT_Wpedantic,
18121 "%qD is an enumeration template", name);
18122 /* ignore a typename reference, for it will be solved by name
18123 in start_enum. */
18124 type = NULL_TREE;
18127 else if (nested_name_specifier == error_mark_node)
18128 /* We already issued an error. */;
18129 else
18131 error_at (type_start_token->location,
18132 "%qD does not name an enumeration in %qT",
18133 identifier, nested_name_specifier);
18134 nested_name_specifier = error_mark_node;
18137 else
18139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18140 identifier = cp_parser_identifier (parser);
18141 else
18143 identifier = make_anon_name ();
18144 is_unnamed = true;
18145 if (scoped_enum_p)
18146 error_at (type_start_token->location,
18147 "unnamed scoped enum is not allowed");
18150 pop_deferring_access_checks ();
18152 /* Check for the `:' that denotes a specified underlying type in C++0x.
18153 Note that a ':' could also indicate a bitfield width, however. */
18154 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18156 cp_decl_specifier_seq type_specifiers;
18158 /* Consume the `:'. */
18159 cp_lexer_consume_token (parser->lexer);
18161 /* Parse the type-specifier-seq. */
18162 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18163 /*is_trailing_return=*/false,
18164 &type_specifiers);
18166 /* At this point this is surely not elaborated type specifier. */
18167 if (!cp_parser_parse_definitely (parser))
18168 return NULL_TREE;
18170 if (cxx_dialect < cxx11)
18171 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18173 has_underlying_type = true;
18175 /* If that didn't work, stop. */
18176 if (type_specifiers.type != error_mark_node)
18178 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18179 /*initialized=*/0, NULL);
18180 if (underlying_type == error_mark_node
18181 || check_for_bare_parameter_packs (underlying_type))
18182 underlying_type = NULL_TREE;
18186 /* Look for the `{' but don't consume it yet. */
18187 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18189 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18191 cp_parser_error (parser, "expected %<{%>");
18192 if (has_underlying_type)
18194 type = NULL_TREE;
18195 goto out;
18198 /* An opaque-enum-specifier must have a ';' here. */
18199 if ((scoped_enum_p || underlying_type)
18200 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18202 cp_parser_error (parser, "expected %<;%> or %<{%>");
18203 if (has_underlying_type)
18205 type = NULL_TREE;
18206 goto out;
18211 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18212 return NULL_TREE;
18214 if (nested_name_specifier)
18216 if (CLASS_TYPE_P (nested_name_specifier))
18218 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18219 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18220 push_scope (nested_name_specifier);
18222 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18224 push_nested_namespace (nested_name_specifier);
18228 /* Issue an error message if type-definitions are forbidden here. */
18229 if (!cp_parser_check_type_definition (parser))
18230 type = error_mark_node;
18231 else
18232 /* Create the new type. We do this before consuming the opening
18233 brace so the enum will be recorded as being on the line of its
18234 tag (or the 'enum' keyword, if there is no tag). */
18235 type = start_enum (identifier, type, underlying_type,
18236 attributes, scoped_enum_p, &is_new_type);
18238 /* If the next token is not '{' it is an opaque-enum-specifier or an
18239 elaborated-type-specifier. */
18240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18242 timevar_push (TV_PARSE_ENUM);
18243 if (nested_name_specifier
18244 && nested_name_specifier != error_mark_node)
18246 /* The following catches invalid code such as:
18247 enum class S<int>::E { A, B, C }; */
18248 if (!processing_specialization
18249 && CLASS_TYPE_P (nested_name_specifier)
18250 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18251 error_at (type_start_token->location, "cannot add an enumerator "
18252 "list to a template instantiation");
18254 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18256 error_at (type_start_token->location,
18257 "%<%T::%E%> has not been declared",
18258 TYPE_CONTEXT (nested_name_specifier),
18259 nested_name_specifier);
18260 type = error_mark_node;
18262 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18263 && !CLASS_TYPE_P (nested_name_specifier))
18265 error_at (type_start_token->location, "nested name specifier "
18266 "%qT for enum declaration does not name a class "
18267 "or namespace", nested_name_specifier);
18268 type = error_mark_node;
18270 /* If that scope does not contain the scope in which the
18271 class was originally declared, the program is invalid. */
18272 else if (prev_scope && !is_ancestor (prev_scope,
18273 nested_name_specifier))
18275 if (at_namespace_scope_p ())
18276 error_at (type_start_token->location,
18277 "declaration of %qD in namespace %qD which does not "
18278 "enclose %qD",
18279 type, prev_scope, nested_name_specifier);
18280 else
18281 error_at (type_start_token->location,
18282 "declaration of %qD in %qD which does not "
18283 "enclose %qD",
18284 type, prev_scope, nested_name_specifier);
18285 type = error_mark_node;
18287 /* If that scope is the scope where the declaration is being placed
18288 the program is invalid. */
18289 else if (CLASS_TYPE_P (nested_name_specifier)
18290 && CLASS_TYPE_P (prev_scope)
18291 && same_type_p (nested_name_specifier, prev_scope))
18293 permerror (type_start_token->location,
18294 "extra qualification not allowed");
18295 nested_name_specifier = NULL_TREE;
18299 if (scoped_enum_p)
18300 begin_scope (sk_scoped_enum, type);
18302 /* Consume the opening brace. */
18303 matching_braces braces;
18304 braces.consume_open (parser);
18306 if (type == error_mark_node)
18307 ; /* Nothing to add */
18308 else if (OPAQUE_ENUM_P (type)
18309 || (cxx_dialect > cxx98 && processing_specialization))
18311 new_value_list = true;
18312 SET_OPAQUE_ENUM_P (type, false);
18313 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18315 else
18317 error_at (type_start_token->location,
18318 "multiple definition of %q#T", type);
18319 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18320 "previous definition here");
18321 type = error_mark_node;
18324 if (type == error_mark_node)
18325 cp_parser_skip_to_end_of_block_or_statement (parser);
18326 /* If the next token is not '}', then there are some enumerators. */
18327 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18329 if (is_unnamed && !scoped_enum_p)
18330 pedwarn (type_start_token->location, OPT_Wpedantic,
18331 "ISO C++ forbids empty unnamed enum");
18333 else
18334 cp_parser_enumerator_list (parser, type);
18336 /* Consume the final '}'. */
18337 braces.require_close (parser);
18339 if (scoped_enum_p)
18340 finish_scope ();
18341 timevar_pop (TV_PARSE_ENUM);
18343 else
18345 /* If a ';' follows, then it is an opaque-enum-specifier
18346 and additional restrictions apply. */
18347 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18349 if (is_unnamed)
18350 error_at (type_start_token->location,
18351 "opaque-enum-specifier without name");
18352 else if (nested_name_specifier)
18353 error_at (type_start_token->location,
18354 "opaque-enum-specifier must use a simple identifier");
18358 /* Look for trailing attributes to apply to this enumeration, and
18359 apply them if appropriate. */
18360 if (cp_parser_allow_gnu_extensions_p (parser))
18362 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18363 cplus_decl_attributes (&type,
18364 trailing_attr,
18365 (int) ATTR_FLAG_TYPE_IN_PLACE);
18368 /* Finish up the enumeration. */
18369 if (type != error_mark_node)
18371 if (new_value_list)
18372 finish_enum_value_list (type);
18373 if (is_new_type)
18374 finish_enum (type);
18377 if (nested_name_specifier)
18379 if (CLASS_TYPE_P (nested_name_specifier))
18381 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18382 pop_scope (nested_name_specifier);
18384 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18386 pop_nested_namespace (nested_name_specifier);
18389 out:
18390 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18391 return type;
18394 /* Parse an enumerator-list. The enumerators all have the indicated
18395 TYPE.
18397 enumerator-list:
18398 enumerator-definition
18399 enumerator-list , enumerator-definition */
18401 static void
18402 cp_parser_enumerator_list (cp_parser* parser, tree type)
18404 while (true)
18406 /* Parse an enumerator-definition. */
18407 cp_parser_enumerator_definition (parser, type);
18409 /* If the next token is not a ',', we've reached the end of
18410 the list. */
18411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18412 break;
18413 /* Otherwise, consume the `,' and keep going. */
18414 cp_lexer_consume_token (parser->lexer);
18415 /* If the next token is a `}', there is a trailing comma. */
18416 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18418 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18419 pedwarn (input_location, OPT_Wpedantic,
18420 "comma at end of enumerator list");
18421 break;
18426 /* Parse an enumerator-definition. The enumerator has the indicated
18427 TYPE.
18429 enumerator-definition:
18430 enumerator
18431 enumerator = constant-expression
18433 enumerator:
18434 identifier
18436 GNU Extensions:
18438 enumerator-definition:
18439 enumerator attributes [opt]
18440 enumerator attributes [opt] = constant-expression */
18442 static void
18443 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18445 tree identifier;
18446 tree value;
18447 location_t loc;
18449 /* Save the input location because we are interested in the location
18450 of the identifier and not the location of the explicit value. */
18451 loc = cp_lexer_peek_token (parser->lexer)->location;
18453 /* Look for the identifier. */
18454 identifier = cp_parser_identifier (parser);
18455 if (identifier == error_mark_node)
18456 return;
18458 /* Parse any specified attributes. */
18459 tree attrs = cp_parser_attributes_opt (parser);
18461 /* If the next token is an '=', then there is an explicit value. */
18462 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18464 /* Consume the `=' token. */
18465 cp_lexer_consume_token (parser->lexer);
18466 /* Parse the value. */
18467 value = cp_parser_constant_expression (parser);
18469 else
18470 value = NULL_TREE;
18472 /* If we are processing a template, make sure the initializer of the
18473 enumerator doesn't contain any bare template parameter pack. */
18474 if (check_for_bare_parameter_packs (value))
18475 value = error_mark_node;
18477 /* Create the enumerator. */
18478 build_enumerator (identifier, value, type, attrs, loc);
18481 /* Parse a namespace-name.
18483 namespace-name:
18484 original-namespace-name
18485 namespace-alias
18487 Returns the NAMESPACE_DECL for the namespace. */
18489 static tree
18490 cp_parser_namespace_name (cp_parser* parser)
18492 tree identifier;
18493 tree namespace_decl;
18495 cp_token *token = cp_lexer_peek_token (parser->lexer);
18497 /* Get the name of the namespace. */
18498 identifier = cp_parser_identifier (parser);
18499 if (identifier == error_mark_node)
18500 return error_mark_node;
18502 /* Look up the identifier in the currently active scope. Look only
18503 for namespaces, due to:
18505 [basic.lookup.udir]
18507 When looking up a namespace-name in a using-directive or alias
18508 definition, only namespace names are considered.
18510 And:
18512 [basic.lookup.qual]
18514 During the lookup of a name preceding the :: scope resolution
18515 operator, object, function, and enumerator names are ignored.
18517 (Note that cp_parser_qualifying_entity only calls this
18518 function if the token after the name is the scope resolution
18519 operator.) */
18520 namespace_decl = cp_parser_lookup_name (parser, identifier,
18521 none_type,
18522 /*is_template=*/false,
18523 /*is_namespace=*/true,
18524 /*check_dependency=*/true,
18525 /*ambiguous_decls=*/NULL,
18526 token->location);
18527 /* If it's not a namespace, issue an error. */
18528 if (namespace_decl == error_mark_node
18529 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18531 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18532 error_at (token->location, "%qD is not a namespace-name", identifier);
18533 cp_parser_error (parser, "expected namespace-name");
18534 namespace_decl = error_mark_node;
18537 return namespace_decl;
18540 /* Parse a namespace-definition.
18542 namespace-definition:
18543 named-namespace-definition
18544 unnamed-namespace-definition
18546 named-namespace-definition:
18547 original-namespace-definition
18548 extension-namespace-definition
18550 original-namespace-definition:
18551 namespace identifier { namespace-body }
18553 extension-namespace-definition:
18554 namespace original-namespace-name { namespace-body }
18556 unnamed-namespace-definition:
18557 namespace { namespace-body } */
18559 static void
18560 cp_parser_namespace_definition (cp_parser* parser)
18562 tree identifier;
18563 int nested_definition_count = 0;
18565 cp_ensure_no_omp_declare_simd (parser);
18566 cp_ensure_no_oacc_routine (parser);
18568 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18570 if (is_inline)
18572 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18573 cp_lexer_consume_token (parser->lexer);
18576 /* Look for the `namespace' keyword. */
18577 cp_token* token
18578 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18580 /* Parse any specified attributes before the identifier. */
18581 tree attribs = cp_parser_attributes_opt (parser);
18583 for (;;)
18585 identifier = NULL_TREE;
18587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18589 identifier = cp_parser_identifier (parser);
18591 /* Parse any attributes specified after the identifier. */
18592 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18596 break;
18598 if (!nested_definition_count && cxx_dialect < cxx17)
18599 pedwarn (input_location, OPT_Wpedantic,
18600 "nested namespace definitions only available with "
18601 "-std=c++17 or -std=gnu++17");
18603 /* Nested namespace names can create new namespaces (unlike
18604 other qualified-ids). */
18605 if (int count = identifier ? push_namespace (identifier) : 0)
18606 nested_definition_count += count;
18607 else
18608 cp_parser_error (parser, "nested namespace name required");
18609 cp_lexer_consume_token (parser->lexer);
18612 if (nested_definition_count && !identifier)
18613 cp_parser_error (parser, "namespace name required");
18615 if (nested_definition_count && attribs)
18616 error_at (token->location,
18617 "a nested namespace definition cannot have attributes");
18618 if (nested_definition_count && is_inline)
18619 error_at (token->location,
18620 "a nested namespace definition cannot be inline");
18622 /* Start the namespace. */
18623 nested_definition_count += push_namespace (identifier, is_inline);
18625 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18627 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18629 /* Look for the `{' to validate starting the namespace. */
18630 matching_braces braces;
18631 if (braces.require_open (parser))
18633 /* Parse the body of the namespace. */
18634 cp_parser_namespace_body (parser);
18636 /* Look for the final `}'. */
18637 braces.require_close (parser);
18640 if (has_visibility)
18641 pop_visibility (1);
18643 /* Pop the nested namespace definitions. */
18644 while (nested_definition_count--)
18645 pop_namespace ();
18648 /* Parse a namespace-body.
18650 namespace-body:
18651 declaration-seq [opt] */
18653 static void
18654 cp_parser_namespace_body (cp_parser* parser)
18656 cp_parser_declaration_seq_opt (parser);
18659 /* Parse a namespace-alias-definition.
18661 namespace-alias-definition:
18662 namespace identifier = qualified-namespace-specifier ; */
18664 static void
18665 cp_parser_namespace_alias_definition (cp_parser* parser)
18667 tree identifier;
18668 tree namespace_specifier;
18670 cp_token *token = cp_lexer_peek_token (parser->lexer);
18672 /* Look for the `namespace' keyword. */
18673 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18674 /* Look for the identifier. */
18675 identifier = cp_parser_identifier (parser);
18676 if (identifier == error_mark_node)
18677 return;
18678 /* Look for the `=' token. */
18679 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18680 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18682 error_at (token->location, "%<namespace%> definition is not allowed here");
18683 /* Skip the definition. */
18684 cp_lexer_consume_token (parser->lexer);
18685 if (cp_parser_skip_to_closing_brace (parser))
18686 cp_lexer_consume_token (parser->lexer);
18687 return;
18689 cp_parser_require (parser, CPP_EQ, RT_EQ);
18690 /* Look for the qualified-namespace-specifier. */
18691 namespace_specifier
18692 = cp_parser_qualified_namespace_specifier (parser);
18693 /* Look for the `;' token. */
18694 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18696 /* Register the alias in the symbol table. */
18697 do_namespace_alias (identifier, namespace_specifier);
18700 /* Parse a qualified-namespace-specifier.
18702 qualified-namespace-specifier:
18703 :: [opt] nested-name-specifier [opt] namespace-name
18705 Returns a NAMESPACE_DECL corresponding to the specified
18706 namespace. */
18708 static tree
18709 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18711 /* Look for the optional `::'. */
18712 cp_parser_global_scope_opt (parser,
18713 /*current_scope_valid_p=*/false);
18715 /* Look for the optional nested-name-specifier. */
18716 cp_parser_nested_name_specifier_opt (parser,
18717 /*typename_keyword_p=*/false,
18718 /*check_dependency_p=*/true,
18719 /*type_p=*/false,
18720 /*is_declaration=*/true);
18722 return cp_parser_namespace_name (parser);
18725 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18726 access declaration.
18728 using-declaration:
18729 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18730 using :: unqualified-id ;
18732 access-declaration:
18733 qualified-id ;
18737 static bool
18738 cp_parser_using_declaration (cp_parser* parser,
18739 bool access_declaration_p)
18741 cp_token *token;
18742 bool typename_p = false;
18743 bool global_scope_p;
18744 tree decl;
18745 tree identifier;
18746 tree qscope;
18747 int oldcount = errorcount;
18748 cp_token *diag_token = NULL;
18750 if (access_declaration_p)
18752 diag_token = cp_lexer_peek_token (parser->lexer);
18753 cp_parser_parse_tentatively (parser);
18755 else
18757 /* Look for the `using' keyword. */
18758 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18760 again:
18761 /* Peek at the next token. */
18762 token = cp_lexer_peek_token (parser->lexer);
18763 /* See if it's `typename'. */
18764 if (token->keyword == RID_TYPENAME)
18766 /* Remember that we've seen it. */
18767 typename_p = true;
18768 /* Consume the `typename' token. */
18769 cp_lexer_consume_token (parser->lexer);
18773 /* Look for the optional global scope qualification. */
18774 global_scope_p
18775 = (cp_parser_global_scope_opt (parser,
18776 /*current_scope_valid_p=*/false)
18777 != NULL_TREE);
18779 /* If we saw `typename', or didn't see `::', then there must be a
18780 nested-name-specifier present. */
18781 if (typename_p || !global_scope_p)
18783 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18784 /*check_dependency_p=*/true,
18785 /*type_p=*/false,
18786 /*is_declaration=*/true);
18787 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18789 cp_parser_skip_to_end_of_block_or_statement (parser);
18790 return false;
18793 /* Otherwise, we could be in either of the two productions. In that
18794 case, treat the nested-name-specifier as optional. */
18795 else
18796 qscope = cp_parser_nested_name_specifier_opt (parser,
18797 /*typename_keyword_p=*/false,
18798 /*check_dependency_p=*/true,
18799 /*type_p=*/false,
18800 /*is_declaration=*/true);
18801 if (!qscope)
18802 qscope = global_namespace;
18803 else if (UNSCOPED_ENUM_P (qscope))
18804 qscope = CP_TYPE_CONTEXT (qscope);
18806 if (access_declaration_p && cp_parser_error_occurred (parser))
18807 /* Something has already gone wrong; there's no need to parse
18808 further. Since an error has occurred, the return value of
18809 cp_parser_parse_definitely will be false, as required. */
18810 return cp_parser_parse_definitely (parser);
18812 token = cp_lexer_peek_token (parser->lexer);
18813 /* Parse the unqualified-id. */
18814 identifier = cp_parser_unqualified_id (parser,
18815 /*template_keyword_p=*/false,
18816 /*check_dependency_p=*/true,
18817 /*declarator_p=*/true,
18818 /*optional_p=*/false);
18820 if (access_declaration_p)
18822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18823 cp_parser_simulate_error (parser);
18824 if (!cp_parser_parse_definitely (parser))
18825 return false;
18827 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18829 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18830 if (cxx_dialect < cxx17
18831 && !in_system_header_at (ell->location))
18832 pedwarn (ell->location, 0,
18833 "pack expansion in using-declaration only available "
18834 "with -std=c++17 or -std=gnu++17");
18835 qscope = make_pack_expansion (qscope);
18838 /* The function we call to handle a using-declaration is different
18839 depending on what scope we are in. */
18840 if (qscope == error_mark_node || identifier == error_mark_node)
18842 else if (!identifier_p (identifier)
18843 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18844 /* [namespace.udecl]
18846 A using declaration shall not name a template-id. */
18847 error_at (token->location,
18848 "a template-id may not appear in a using-declaration");
18849 else
18851 if (at_class_scope_p ())
18853 /* Create the USING_DECL. */
18854 decl = do_class_using_decl (qscope, identifier);
18856 if (decl && typename_p)
18857 USING_DECL_TYPENAME_P (decl) = 1;
18859 if (check_for_bare_parameter_packs (decl))
18861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18862 return false;
18864 else
18865 /* Add it to the list of members in this class. */
18866 finish_member_declaration (decl);
18868 else
18870 decl = cp_parser_lookup_name_simple (parser,
18871 identifier,
18872 token->location);
18873 if (decl == error_mark_node)
18874 cp_parser_name_lookup_error (parser, identifier,
18875 decl, NLE_NULL,
18876 token->location);
18877 else if (check_for_bare_parameter_packs (decl))
18879 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18880 return false;
18882 else if (!at_namespace_scope_p ())
18883 finish_local_using_decl (decl, qscope, identifier);
18884 else
18885 finish_namespace_using_decl (decl, qscope, identifier);
18889 if (!access_declaration_p
18890 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18892 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18893 if (cxx_dialect < cxx17)
18894 pedwarn (comma->location, 0,
18895 "comma-separated list in using-declaration only available "
18896 "with -std=c++17 or -std=gnu++17");
18897 goto again;
18900 /* Look for the final `;'. */
18901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18903 if (access_declaration_p && errorcount == oldcount)
18904 warning_at (diag_token->location, OPT_Wdeprecated,
18905 "access declarations are deprecated "
18906 "in favour of using-declarations; "
18907 "suggestion: add the %<using%> keyword");
18909 return true;
18912 /* Parse an alias-declaration.
18914 alias-declaration:
18915 using identifier attribute-specifier-seq [opt] = type-id */
18917 static tree
18918 cp_parser_alias_declaration (cp_parser* parser)
18920 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18921 location_t id_location;
18922 cp_declarator *declarator;
18923 cp_decl_specifier_seq decl_specs;
18924 bool member_p;
18925 const char *saved_message = NULL;
18927 /* Look for the `using' keyword. */
18928 cp_token *using_token
18929 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18930 if (using_token == NULL)
18931 return error_mark_node;
18933 id_location = cp_lexer_peek_token (parser->lexer)->location;
18934 id = cp_parser_identifier (parser);
18935 if (id == error_mark_node)
18936 return error_mark_node;
18938 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18939 attributes = cp_parser_attributes_opt (parser);
18940 if (attributes == error_mark_node)
18941 return error_mark_node;
18943 cp_parser_require (parser, CPP_EQ, RT_EQ);
18945 if (cp_parser_error_occurred (parser))
18946 return error_mark_node;
18948 cp_parser_commit_to_tentative_parse (parser);
18950 /* Now we are going to parse the type-id of the declaration. */
18953 [dcl.type]/3 says:
18955 "A type-specifier-seq shall not define a class or enumeration
18956 unless it appears in the type-id of an alias-declaration (7.1.3) that
18957 is not the declaration of a template-declaration."
18959 In other words, if we currently are in an alias template, the
18960 type-id should not define a type.
18962 So let's set parser->type_definition_forbidden_message in that
18963 case; cp_parser_check_type_definition (called by
18964 cp_parser_class_specifier) will then emit an error if a type is
18965 defined in the type-id. */
18966 if (parser->num_template_parameter_lists)
18968 saved_message = parser->type_definition_forbidden_message;
18969 parser->type_definition_forbidden_message =
18970 G_("types may not be defined in alias template declarations");
18973 type = cp_parser_type_id (parser);
18975 /* Restore the error message if need be. */
18976 if (parser->num_template_parameter_lists)
18977 parser->type_definition_forbidden_message = saved_message;
18979 if (type == error_mark_node
18980 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18982 cp_parser_skip_to_end_of_block_or_statement (parser);
18983 return error_mark_node;
18986 /* A typedef-name can also be introduced by an alias-declaration. The
18987 identifier following the using keyword becomes a typedef-name. It has
18988 the same semantics as if it were introduced by the typedef
18989 specifier. In particular, it does not define a new type and it shall
18990 not appear in the type-id. */
18992 clear_decl_specs (&decl_specs);
18993 decl_specs.type = type;
18994 if (attributes != NULL_TREE)
18996 decl_specs.attributes = attributes;
18997 set_and_check_decl_spec_loc (&decl_specs,
18998 ds_attribute,
18999 attrs_token);
19001 set_and_check_decl_spec_loc (&decl_specs,
19002 ds_typedef,
19003 using_token);
19004 set_and_check_decl_spec_loc (&decl_specs,
19005 ds_alias,
19006 using_token);
19008 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19009 declarator->id_loc = id_location;
19011 member_p = at_class_scope_p ();
19012 if (member_p)
19013 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19014 NULL_TREE, attributes);
19015 else
19016 decl = start_decl (declarator, &decl_specs, 0,
19017 attributes, NULL_TREE, &pushed_scope);
19018 if (decl == error_mark_node)
19019 return decl;
19021 // Attach constraints to the alias declaration.
19022 if (flag_concepts && current_template_parms)
19024 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19025 tree constr = build_constraints (reqs, NULL_TREE);
19026 set_constraints (decl, constr);
19029 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19031 if (pushed_scope)
19032 pop_scope (pushed_scope);
19034 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19035 added into the symbol table; otherwise, return the TYPE_DECL. */
19036 if (DECL_LANG_SPECIFIC (decl)
19037 && DECL_TEMPLATE_INFO (decl)
19038 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19040 decl = DECL_TI_TEMPLATE (decl);
19041 if (member_p)
19042 check_member_template (decl);
19045 return decl;
19048 /* Parse a using-directive.
19050 using-directive:
19051 using namespace :: [opt] nested-name-specifier [opt]
19052 namespace-name ; */
19054 static void
19055 cp_parser_using_directive (cp_parser* parser)
19057 tree namespace_decl;
19058 tree attribs;
19060 /* Look for the `using' keyword. */
19061 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19062 /* And the `namespace' keyword. */
19063 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19064 /* Look for the optional `::' operator. */
19065 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19066 /* And the optional nested-name-specifier. */
19067 cp_parser_nested_name_specifier_opt (parser,
19068 /*typename_keyword_p=*/false,
19069 /*check_dependency_p=*/true,
19070 /*type_p=*/false,
19071 /*is_declaration=*/true);
19072 /* Get the namespace being used. */
19073 namespace_decl = cp_parser_namespace_name (parser);
19074 /* And any specified attributes. */
19075 attribs = cp_parser_attributes_opt (parser);
19077 /* Update the symbol table. */
19078 if (namespace_bindings_p ())
19079 finish_namespace_using_directive (namespace_decl, attribs);
19080 else
19081 finish_local_using_directive (namespace_decl, attribs);
19083 /* Look for the final `;'. */
19084 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19087 /* Parse an asm-definition.
19089 asm-definition:
19090 asm ( string-literal ) ;
19092 GNU Extension:
19094 asm-definition:
19095 asm volatile [opt] ( string-literal ) ;
19096 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19097 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19098 : asm-operand-list [opt] ) ;
19099 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19100 : asm-operand-list [opt]
19101 : asm-clobber-list [opt] ) ;
19102 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19103 : asm-clobber-list [opt]
19104 : asm-goto-list ) ; */
19106 static void
19107 cp_parser_asm_definition (cp_parser* parser)
19109 tree string;
19110 tree outputs = NULL_TREE;
19111 tree inputs = NULL_TREE;
19112 tree clobbers = NULL_TREE;
19113 tree labels = NULL_TREE;
19114 tree asm_stmt;
19115 bool volatile_p = false;
19116 bool extended_p = false;
19117 bool invalid_inputs_p = false;
19118 bool invalid_outputs_p = false;
19119 bool goto_p = false;
19120 required_token missing = RT_NONE;
19122 /* Look for the `asm' keyword. */
19123 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19125 if (parser->in_function_body
19126 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19128 error ("%<asm%> in %<constexpr%> function");
19129 cp_function_chain->invalid_constexpr = true;
19132 /* See if the next token is `volatile'. */
19133 if (cp_parser_allow_gnu_extensions_p (parser)
19134 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19136 /* Remember that we saw the `volatile' keyword. */
19137 volatile_p = true;
19138 /* Consume the token. */
19139 cp_lexer_consume_token (parser->lexer);
19141 if (cp_parser_allow_gnu_extensions_p (parser)
19142 && parser->in_function_body
19143 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19145 /* Remember that we saw the `goto' keyword. */
19146 goto_p = true;
19147 /* Consume the token. */
19148 cp_lexer_consume_token (parser->lexer);
19150 /* Look for the opening `('. */
19151 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19152 return;
19153 /* Look for the string. */
19154 string = cp_parser_string_literal (parser, false, false);
19155 if (string == error_mark_node)
19157 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19158 /*consume_paren=*/true);
19159 return;
19162 /* If we're allowing GNU extensions, check for the extended assembly
19163 syntax. Unfortunately, the `:' tokens need not be separated by
19164 a space in C, and so, for compatibility, we tolerate that here
19165 too. Doing that means that we have to treat the `::' operator as
19166 two `:' tokens. */
19167 if (cp_parser_allow_gnu_extensions_p (parser)
19168 && parser->in_function_body
19169 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19170 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19172 bool inputs_p = false;
19173 bool clobbers_p = false;
19174 bool labels_p = false;
19176 /* The extended syntax was used. */
19177 extended_p = true;
19179 /* Look for outputs. */
19180 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19182 /* Consume the `:'. */
19183 cp_lexer_consume_token (parser->lexer);
19184 /* Parse the output-operands. */
19185 if (cp_lexer_next_token_is_not (parser->lexer,
19186 CPP_COLON)
19187 && cp_lexer_next_token_is_not (parser->lexer,
19188 CPP_SCOPE)
19189 && cp_lexer_next_token_is_not (parser->lexer,
19190 CPP_CLOSE_PAREN)
19191 && !goto_p)
19193 outputs = cp_parser_asm_operand_list (parser);
19194 if (outputs == error_mark_node)
19195 invalid_outputs_p = true;
19198 /* If the next token is `::', there are no outputs, and the
19199 next token is the beginning of the inputs. */
19200 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19201 /* The inputs are coming next. */
19202 inputs_p = true;
19204 /* Look for inputs. */
19205 if (inputs_p
19206 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19208 /* Consume the `:' or `::'. */
19209 cp_lexer_consume_token (parser->lexer);
19210 /* Parse the output-operands. */
19211 if (cp_lexer_next_token_is_not (parser->lexer,
19212 CPP_COLON)
19213 && cp_lexer_next_token_is_not (parser->lexer,
19214 CPP_SCOPE)
19215 && cp_lexer_next_token_is_not (parser->lexer,
19216 CPP_CLOSE_PAREN))
19218 inputs = cp_parser_asm_operand_list (parser);
19219 if (inputs == error_mark_node)
19220 invalid_inputs_p = true;
19223 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19224 /* The clobbers are coming next. */
19225 clobbers_p = true;
19227 /* Look for clobbers. */
19228 if (clobbers_p
19229 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19231 clobbers_p = true;
19232 /* Consume the `:' or `::'. */
19233 cp_lexer_consume_token (parser->lexer);
19234 /* Parse the clobbers. */
19235 if (cp_lexer_next_token_is_not (parser->lexer,
19236 CPP_COLON)
19237 && cp_lexer_next_token_is_not (parser->lexer,
19238 CPP_CLOSE_PAREN))
19239 clobbers = cp_parser_asm_clobber_list (parser);
19241 else if (goto_p
19242 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19243 /* The labels are coming next. */
19244 labels_p = true;
19246 /* Look for labels. */
19247 if (labels_p
19248 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19250 labels_p = true;
19251 /* Consume the `:' or `::'. */
19252 cp_lexer_consume_token (parser->lexer);
19253 /* Parse the labels. */
19254 labels = cp_parser_asm_label_list (parser);
19257 if (goto_p && !labels_p)
19258 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19260 else if (goto_p)
19261 missing = RT_COLON_SCOPE;
19263 /* Look for the closing `)'. */
19264 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19265 missing ? missing : RT_CLOSE_PAREN))
19266 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19267 /*consume_paren=*/true);
19268 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19270 if (!invalid_inputs_p && !invalid_outputs_p)
19272 /* Create the ASM_EXPR. */
19273 if (parser->in_function_body)
19275 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19276 inputs, clobbers, labels);
19277 /* If the extended syntax was not used, mark the ASM_EXPR. */
19278 if (!extended_p)
19280 tree temp = asm_stmt;
19281 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19282 temp = TREE_OPERAND (temp, 0);
19284 ASM_INPUT_P (temp) = 1;
19287 else
19288 symtab->finalize_toplevel_asm (string);
19292 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19293 type that comes from the decl-specifier-seq. */
19295 static tree
19296 strip_declarator_types (tree type, cp_declarator *declarator)
19298 for (cp_declarator *d = declarator; d;)
19299 switch (d->kind)
19301 case cdk_id:
19302 case cdk_decomp:
19303 case cdk_error:
19304 d = NULL;
19305 break;
19307 default:
19308 if (TYPE_PTRMEMFUNC_P (type))
19309 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19310 type = TREE_TYPE (type);
19311 d = d->declarator;
19312 break;
19315 return type;
19318 /* Declarators [gram.dcl.decl] */
19320 /* Parse an init-declarator.
19322 init-declarator:
19323 declarator initializer [opt]
19325 GNU Extension:
19327 init-declarator:
19328 declarator asm-specification [opt] attributes [opt] initializer [opt]
19330 function-definition:
19331 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19332 function-body
19333 decl-specifier-seq [opt] declarator function-try-block
19335 GNU Extension:
19337 function-definition:
19338 __extension__ function-definition
19340 TM Extension:
19342 function-definition:
19343 decl-specifier-seq [opt] declarator function-transaction-block
19345 The DECL_SPECIFIERS apply to this declarator. Returns a
19346 representation of the entity declared. If MEMBER_P is TRUE, then
19347 this declarator appears in a class scope. The new DECL created by
19348 this declarator is returned.
19350 The CHECKS are access checks that should be performed once we know
19351 what entity is being declared (and, therefore, what classes have
19352 befriended it).
19354 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19355 for a function-definition here as well. If the declarator is a
19356 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19357 be TRUE upon return. By that point, the function-definition will
19358 have been completely parsed.
19360 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19361 is FALSE.
19363 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19364 parsed declaration if it is an uninitialized single declarator not followed
19365 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19366 if present, will not be consumed. If returned, this declarator will be
19367 created with SD_INITIALIZED but will not call cp_finish_decl.
19369 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19370 and there is an initializer, the pointed location_t is set to the
19371 location of the '=' or `(', or '{' in C++11 token introducing the
19372 initializer. */
19374 static tree
19375 cp_parser_init_declarator (cp_parser* parser,
19376 cp_decl_specifier_seq *decl_specifiers,
19377 vec<deferred_access_check, va_gc> *checks,
19378 bool function_definition_allowed_p,
19379 bool member_p,
19380 int declares_class_or_enum,
19381 bool* function_definition_p,
19382 tree* maybe_range_for_decl,
19383 location_t* init_loc,
19384 tree* auto_result)
19386 cp_token *token = NULL, *asm_spec_start_token = NULL,
19387 *attributes_start_token = NULL;
19388 cp_declarator *declarator;
19389 tree prefix_attributes;
19390 tree attributes = NULL;
19391 tree asm_specification;
19392 tree initializer;
19393 tree decl = NULL_TREE;
19394 tree scope;
19395 int is_initialized;
19396 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19397 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19398 "(...)". */
19399 enum cpp_ttype initialization_kind;
19400 bool is_direct_init = false;
19401 bool is_non_constant_init;
19402 int ctor_dtor_or_conv_p;
19403 bool friend_p = cp_parser_friend_p (decl_specifiers);
19404 tree pushed_scope = NULL_TREE;
19405 bool range_for_decl_p = false;
19406 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19407 location_t tmp_init_loc = UNKNOWN_LOCATION;
19409 /* Gather the attributes that were provided with the
19410 decl-specifiers. */
19411 prefix_attributes = decl_specifiers->attributes;
19413 /* Assume that this is not the declarator for a function
19414 definition. */
19415 if (function_definition_p)
19416 *function_definition_p = false;
19418 /* Default arguments are only permitted for function parameters. */
19419 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19420 parser->default_arg_ok_p = false;
19422 /* Defer access checks while parsing the declarator; we cannot know
19423 what names are accessible until we know what is being
19424 declared. */
19425 resume_deferring_access_checks ();
19427 token = cp_lexer_peek_token (parser->lexer);
19429 /* Parse the declarator. */
19430 declarator
19431 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19432 &ctor_dtor_or_conv_p,
19433 /*parenthesized_p=*/NULL,
19434 member_p, friend_p);
19435 /* Gather up the deferred checks. */
19436 stop_deferring_access_checks ();
19438 parser->default_arg_ok_p = saved_default_arg_ok_p;
19440 /* If the DECLARATOR was erroneous, there's no need to go
19441 further. */
19442 if (declarator == cp_error_declarator)
19443 return error_mark_node;
19445 /* Check that the number of template-parameter-lists is OK. */
19446 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19447 token->location))
19448 return error_mark_node;
19450 if (declares_class_or_enum & 2)
19451 cp_parser_check_for_definition_in_return_type (declarator,
19452 decl_specifiers->type,
19453 decl_specifiers->locations[ds_type_spec]);
19455 /* Figure out what scope the entity declared by the DECLARATOR is
19456 located in. `grokdeclarator' sometimes changes the scope, so
19457 we compute it now. */
19458 scope = get_scope_of_declarator (declarator);
19460 /* Perform any lookups in the declared type which were thought to be
19461 dependent, but are not in the scope of the declarator. */
19462 decl_specifiers->type
19463 = maybe_update_decl_type (decl_specifiers->type, scope);
19465 /* If we're allowing GNU extensions, look for an
19466 asm-specification. */
19467 if (cp_parser_allow_gnu_extensions_p (parser))
19469 /* Look for an asm-specification. */
19470 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19471 asm_specification = cp_parser_asm_specification_opt (parser);
19473 else
19474 asm_specification = NULL_TREE;
19476 /* Look for attributes. */
19477 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19478 attributes = cp_parser_attributes_opt (parser);
19480 /* Peek at the next token. */
19481 token = cp_lexer_peek_token (parser->lexer);
19483 bool bogus_implicit_tmpl = false;
19485 if (function_declarator_p (declarator))
19487 /* Handle C++17 deduction guides. */
19488 if (!decl_specifiers->type
19489 && ctor_dtor_or_conv_p <= 0
19490 && cxx_dialect >= cxx17)
19492 cp_declarator *id = get_id_declarator (declarator);
19493 tree name = id->u.id.unqualified_name;
19494 parser->scope = id->u.id.qualifying_scope;
19495 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19496 if (tmpl
19497 && (DECL_CLASS_TEMPLATE_P (tmpl)
19498 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19500 id->u.id.unqualified_name = dguide_name (tmpl);
19501 id->u.id.sfk = sfk_deduction_guide;
19502 ctor_dtor_or_conv_p = 1;
19506 /* Check to see if the token indicates the start of a
19507 function-definition. */
19508 if (cp_parser_token_starts_function_definition_p (token))
19510 if (!function_definition_allowed_p)
19512 /* If a function-definition should not appear here, issue an
19513 error message. */
19514 cp_parser_error (parser,
19515 "a function-definition is not allowed here");
19516 return error_mark_node;
19519 location_t func_brace_location
19520 = cp_lexer_peek_token (parser->lexer)->location;
19522 /* Neither attributes nor an asm-specification are allowed
19523 on a function-definition. */
19524 if (asm_specification)
19525 error_at (asm_spec_start_token->location,
19526 "an asm-specification is not allowed "
19527 "on a function-definition");
19528 if (attributes)
19529 error_at (attributes_start_token->location,
19530 "attributes are not allowed "
19531 "on a function-definition");
19532 /* This is a function-definition. */
19533 *function_definition_p = true;
19535 /* Parse the function definition. */
19536 if (member_p)
19537 decl = cp_parser_save_member_function_body (parser,
19538 decl_specifiers,
19539 declarator,
19540 prefix_attributes);
19541 else
19542 decl =
19543 (cp_parser_function_definition_from_specifiers_and_declarator
19544 (parser, decl_specifiers, prefix_attributes, declarator));
19546 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19548 /* This is where the prologue starts... */
19549 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19550 = func_brace_location;
19553 return decl;
19556 else if (parser->fully_implicit_function_template_p)
19558 /* A non-template declaration involving a function parameter list
19559 containing an implicit template parameter will be made into a
19560 template. If the resulting declaration is not going to be an
19561 actual function then finish the template scope here to prevent it.
19562 An error message will be issued once we have a decl to talk about.
19564 FIXME probably we should do type deduction rather than create an
19565 implicit template, but the standard currently doesn't allow it. */
19566 bogus_implicit_tmpl = true;
19567 finish_fully_implicit_template (parser, NULL_TREE);
19570 /* [dcl.dcl]
19572 Only in function declarations for constructors, destructors, type
19573 conversions, and deduction guides can the decl-specifier-seq be omitted.
19575 We explicitly postpone this check past the point where we handle
19576 function-definitions because we tolerate function-definitions
19577 that are missing their return types in some modes. */
19578 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19580 cp_parser_error (parser,
19581 "expected constructor, destructor, or type conversion");
19582 return error_mark_node;
19585 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19586 if (token->type == CPP_EQ
19587 || token->type == CPP_OPEN_PAREN
19588 || token->type == CPP_OPEN_BRACE)
19590 is_initialized = SD_INITIALIZED;
19591 initialization_kind = token->type;
19592 if (maybe_range_for_decl)
19593 *maybe_range_for_decl = error_mark_node;
19594 tmp_init_loc = token->location;
19595 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19596 *init_loc = tmp_init_loc;
19598 if (token->type == CPP_EQ
19599 && function_declarator_p (declarator))
19601 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19602 if (t2->keyword == RID_DEFAULT)
19603 is_initialized = SD_DEFAULTED;
19604 else if (t2->keyword == RID_DELETE)
19605 is_initialized = SD_DELETED;
19608 else
19610 /* If the init-declarator isn't initialized and isn't followed by a
19611 `,' or `;', it's not a valid init-declarator. */
19612 if (token->type != CPP_COMMA
19613 && token->type != CPP_SEMICOLON)
19615 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19616 range_for_decl_p = true;
19617 else
19619 if (!maybe_range_for_decl)
19620 cp_parser_error (parser, "expected initializer");
19621 return error_mark_node;
19624 is_initialized = SD_UNINITIALIZED;
19625 initialization_kind = CPP_EOF;
19628 /* Because start_decl has side-effects, we should only call it if we
19629 know we're going ahead. By this point, we know that we cannot
19630 possibly be looking at any other construct. */
19631 cp_parser_commit_to_tentative_parse (parser);
19633 /* Enter the newly declared entry in the symbol table. If we're
19634 processing a declaration in a class-specifier, we wait until
19635 after processing the initializer. */
19636 if (!member_p)
19638 if (parser->in_unbraced_linkage_specification_p)
19639 decl_specifiers->storage_class = sc_extern;
19640 decl = start_decl (declarator, decl_specifiers,
19641 range_for_decl_p? SD_INITIALIZED : is_initialized,
19642 attributes, prefix_attributes, &pushed_scope);
19643 cp_finalize_omp_declare_simd (parser, decl);
19644 cp_finalize_oacc_routine (parser, decl, false);
19645 /* Adjust location of decl if declarator->id_loc is more appropriate:
19646 set, and decl wasn't merged with another decl, in which case its
19647 location would be different from input_location, and more accurate. */
19648 if (DECL_P (decl)
19649 && declarator->id_loc != UNKNOWN_LOCATION
19650 && DECL_SOURCE_LOCATION (decl) == input_location)
19651 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19653 else if (scope)
19654 /* Enter the SCOPE. That way unqualified names appearing in the
19655 initializer will be looked up in SCOPE. */
19656 pushed_scope = push_scope (scope);
19658 /* Perform deferred access control checks, now that we know in which
19659 SCOPE the declared entity resides. */
19660 if (!member_p && decl)
19662 tree saved_current_function_decl = NULL_TREE;
19664 /* If the entity being declared is a function, pretend that we
19665 are in its scope. If it is a `friend', it may have access to
19666 things that would not otherwise be accessible. */
19667 if (TREE_CODE (decl) == FUNCTION_DECL)
19669 saved_current_function_decl = current_function_decl;
19670 current_function_decl = decl;
19673 /* Perform access checks for template parameters. */
19674 cp_parser_perform_template_parameter_access_checks (checks);
19676 /* Perform the access control checks for the declarator and the
19677 decl-specifiers. */
19678 perform_deferred_access_checks (tf_warning_or_error);
19680 /* Restore the saved value. */
19681 if (TREE_CODE (decl) == FUNCTION_DECL)
19682 current_function_decl = saved_current_function_decl;
19685 /* Parse the initializer. */
19686 initializer = NULL_TREE;
19687 is_direct_init = false;
19688 is_non_constant_init = true;
19689 if (is_initialized)
19691 if (function_declarator_p (declarator))
19693 if (initialization_kind == CPP_EQ)
19694 initializer = cp_parser_pure_specifier (parser);
19695 else
19697 /* If the declaration was erroneous, we don't really
19698 know what the user intended, so just silently
19699 consume the initializer. */
19700 if (decl != error_mark_node)
19701 error_at (tmp_init_loc, "initializer provided for function");
19702 cp_parser_skip_to_closing_parenthesis (parser,
19703 /*recovering=*/true,
19704 /*or_comma=*/false,
19705 /*consume_paren=*/true);
19708 else
19710 /* We want to record the extra mangling scope for in-class
19711 initializers of class members and initializers of static data
19712 member templates. The former involves deferring
19713 parsing of the initializer until end of class as with default
19714 arguments. So right here we only handle the latter. */
19715 if (!member_p && processing_template_decl)
19716 start_lambda_scope (decl);
19717 initializer = cp_parser_initializer (parser,
19718 &is_direct_init,
19719 &is_non_constant_init);
19720 if (!member_p && processing_template_decl)
19721 finish_lambda_scope ();
19722 if (initializer == error_mark_node)
19723 cp_parser_skip_to_end_of_statement (parser);
19727 /* The old parser allows attributes to appear after a parenthesized
19728 initializer. Mark Mitchell proposed removing this functionality
19729 on the GCC mailing lists on 2002-08-13. This parser accepts the
19730 attributes -- but ignores them. */
19731 if (cp_parser_allow_gnu_extensions_p (parser)
19732 && initialization_kind == CPP_OPEN_PAREN)
19733 if (cp_parser_attributes_opt (parser))
19734 warning (OPT_Wattributes,
19735 "attributes after parenthesized initializer ignored");
19737 /* And now complain about a non-function implicit template. */
19738 if (bogus_implicit_tmpl && decl != error_mark_node)
19739 error_at (DECL_SOURCE_LOCATION (decl),
19740 "non-function %qD declared as implicit template", decl);
19742 /* For an in-class declaration, use `grokfield' to create the
19743 declaration. */
19744 if (member_p)
19746 if (pushed_scope)
19748 pop_scope (pushed_scope);
19749 pushed_scope = NULL_TREE;
19751 decl = grokfield (declarator, decl_specifiers,
19752 initializer, !is_non_constant_init,
19753 /*asmspec=*/NULL_TREE,
19754 chainon (attributes, prefix_attributes));
19755 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19756 cp_parser_save_default_args (parser, decl);
19757 cp_finalize_omp_declare_simd (parser, decl);
19758 cp_finalize_oacc_routine (parser, decl, false);
19761 /* Finish processing the declaration. But, skip member
19762 declarations. */
19763 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19765 cp_finish_decl (decl,
19766 initializer, !is_non_constant_init,
19767 asm_specification,
19768 /* If the initializer is in parentheses, then this is
19769 a direct-initialization, which means that an
19770 `explicit' constructor is OK. Otherwise, an
19771 `explicit' constructor cannot be used. */
19772 ((is_direct_init || !is_initialized)
19773 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19775 else if ((cxx_dialect != cxx98) && friend_p
19776 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19777 /* Core issue #226 (C++0x only): A default template-argument
19778 shall not be specified in a friend class template
19779 declaration. */
19780 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19781 /*is_partial=*/false, /*is_friend_decl=*/1);
19783 if (!friend_p && pushed_scope)
19784 pop_scope (pushed_scope);
19786 if (function_declarator_p (declarator)
19787 && parser->fully_implicit_function_template_p)
19789 if (member_p)
19790 decl = finish_fully_implicit_template (parser, decl);
19791 else
19792 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19795 if (auto_result && is_initialized && decl_specifiers->type
19796 && type_uses_auto (decl_specifiers->type))
19797 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19799 return decl;
19802 /* Parse a declarator.
19804 declarator:
19805 direct-declarator
19806 ptr-operator declarator
19808 abstract-declarator:
19809 ptr-operator abstract-declarator [opt]
19810 direct-abstract-declarator
19812 GNU Extensions:
19814 declarator:
19815 attributes [opt] direct-declarator
19816 attributes [opt] ptr-operator declarator
19818 abstract-declarator:
19819 attributes [opt] ptr-operator abstract-declarator [opt]
19820 attributes [opt] direct-abstract-declarator
19822 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19823 detect constructors, destructors, deduction guides, or conversion operators.
19824 It is set to -1 if the declarator is a name, and +1 if it is a
19825 function. Otherwise it is set to zero. Usually you just want to
19826 test for >0, but internally the negative value is used.
19828 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19829 a decl-specifier-seq unless it declares a constructor, destructor,
19830 or conversion. It might seem that we could check this condition in
19831 semantic analysis, rather than parsing, but that makes it difficult
19832 to handle something like `f()'. We want to notice that there are
19833 no decl-specifiers, and therefore realize that this is an
19834 expression, not a declaration.)
19836 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19837 the declarator is a direct-declarator of the form "(...)".
19839 MEMBER_P is true iff this declarator is a member-declarator.
19841 FRIEND_P is true iff this declarator is a friend. */
19843 static cp_declarator *
19844 cp_parser_declarator (cp_parser* parser,
19845 cp_parser_declarator_kind dcl_kind,
19846 int* ctor_dtor_or_conv_p,
19847 bool* parenthesized_p,
19848 bool member_p, bool friend_p)
19850 cp_declarator *declarator;
19851 enum tree_code code;
19852 cp_cv_quals cv_quals;
19853 tree class_type;
19854 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19856 /* Assume this is not a constructor, destructor, or type-conversion
19857 operator. */
19858 if (ctor_dtor_or_conv_p)
19859 *ctor_dtor_or_conv_p = 0;
19861 if (cp_parser_allow_gnu_extensions_p (parser))
19862 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19864 /* Check for the ptr-operator production. */
19865 cp_parser_parse_tentatively (parser);
19866 /* Parse the ptr-operator. */
19867 code = cp_parser_ptr_operator (parser,
19868 &class_type,
19869 &cv_quals,
19870 &std_attributes);
19872 /* If that worked, then we have a ptr-operator. */
19873 if (cp_parser_parse_definitely (parser))
19875 /* If a ptr-operator was found, then this declarator was not
19876 parenthesized. */
19877 if (parenthesized_p)
19878 *parenthesized_p = true;
19879 /* The dependent declarator is optional if we are parsing an
19880 abstract-declarator. */
19881 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19882 cp_parser_parse_tentatively (parser);
19884 /* Parse the dependent declarator. */
19885 declarator = cp_parser_declarator (parser, dcl_kind,
19886 /*ctor_dtor_or_conv_p=*/NULL,
19887 /*parenthesized_p=*/NULL,
19888 /*member_p=*/false,
19889 friend_p);
19891 /* If we are parsing an abstract-declarator, we must handle the
19892 case where the dependent declarator is absent. */
19893 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19894 && !cp_parser_parse_definitely (parser))
19895 declarator = NULL;
19897 declarator = cp_parser_make_indirect_declarator
19898 (code, class_type, cv_quals, declarator, std_attributes);
19900 /* Everything else is a direct-declarator. */
19901 else
19903 if (parenthesized_p)
19904 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19905 CPP_OPEN_PAREN);
19906 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19907 ctor_dtor_or_conv_p,
19908 member_p, friend_p);
19911 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19912 declarator->attributes = gnu_attributes;
19913 return declarator;
19916 /* Parse a direct-declarator or direct-abstract-declarator.
19918 direct-declarator:
19919 declarator-id
19920 direct-declarator ( parameter-declaration-clause )
19921 cv-qualifier-seq [opt]
19922 ref-qualifier [opt]
19923 exception-specification [opt]
19924 direct-declarator [ constant-expression [opt] ]
19925 ( declarator )
19927 direct-abstract-declarator:
19928 direct-abstract-declarator [opt]
19929 ( parameter-declaration-clause )
19930 cv-qualifier-seq [opt]
19931 ref-qualifier [opt]
19932 exception-specification [opt]
19933 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19934 ( abstract-declarator )
19936 Returns a representation of the declarator. DCL_KIND is
19937 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19938 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19939 we are parsing a direct-declarator. It is
19940 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19941 of ambiguity we prefer an abstract declarator, as per
19942 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19943 as for cp_parser_declarator. */
19945 static cp_declarator *
19946 cp_parser_direct_declarator (cp_parser* parser,
19947 cp_parser_declarator_kind dcl_kind,
19948 int* ctor_dtor_or_conv_p,
19949 bool member_p, bool friend_p)
19951 cp_token *token;
19952 cp_declarator *declarator = NULL;
19953 tree scope = NULL_TREE;
19954 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19955 bool saved_in_declarator_p = parser->in_declarator_p;
19956 bool first = true;
19957 tree pushed_scope = NULL_TREE;
19958 cp_token *open_paren = NULL, *close_paren = NULL;
19960 while (true)
19962 /* Peek at the next token. */
19963 token = cp_lexer_peek_token (parser->lexer);
19964 if (token->type == CPP_OPEN_PAREN)
19966 /* This is either a parameter-declaration-clause, or a
19967 parenthesized declarator. When we know we are parsing a
19968 named declarator, it must be a parenthesized declarator
19969 if FIRST is true. For instance, `(int)' is a
19970 parameter-declaration-clause, with an omitted
19971 direct-abstract-declarator. But `((*))', is a
19972 parenthesized abstract declarator. Finally, when T is a
19973 template parameter `(T)' is a
19974 parameter-declaration-clause, and not a parenthesized
19975 named declarator.
19977 We first try and parse a parameter-declaration-clause,
19978 and then try a nested declarator (if FIRST is true).
19980 It is not an error for it not to be a
19981 parameter-declaration-clause, even when FIRST is
19982 false. Consider,
19984 int i (int);
19985 int i (3);
19987 The first is the declaration of a function while the
19988 second is the definition of a variable, including its
19989 initializer.
19991 Having seen only the parenthesis, we cannot know which of
19992 these two alternatives should be selected. Even more
19993 complex are examples like:
19995 int i (int (a));
19996 int i (int (3));
19998 The former is a function-declaration; the latter is a
19999 variable initialization.
20001 Thus again, we try a parameter-declaration-clause, and if
20002 that fails, we back out and return. */
20004 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20006 tree params;
20007 bool is_declarator = false;
20009 open_paren = NULL;
20011 /* In a member-declarator, the only valid interpretation
20012 of a parenthesis is the start of a
20013 parameter-declaration-clause. (It is invalid to
20014 initialize a static data member with a parenthesized
20015 initializer; only the "=" form of initialization is
20016 permitted.) */
20017 if (!member_p)
20018 cp_parser_parse_tentatively (parser);
20020 /* Consume the `('. */
20021 matching_parens parens;
20022 parens.consume_open (parser);
20023 if (first)
20025 /* If this is going to be an abstract declarator, we're
20026 in a declarator and we can't have default args. */
20027 parser->default_arg_ok_p = false;
20028 parser->in_declarator_p = true;
20031 begin_scope (sk_function_parms, NULL_TREE);
20033 /* Parse the parameter-declaration-clause. */
20034 params = cp_parser_parameter_declaration_clause (parser);
20036 /* Consume the `)'. */
20037 parens.require_close (parser);
20039 /* If all went well, parse the cv-qualifier-seq,
20040 ref-qualifier and the exception-specification. */
20041 if (member_p || cp_parser_parse_definitely (parser))
20043 cp_cv_quals cv_quals;
20044 cp_virt_specifiers virt_specifiers;
20045 cp_ref_qualifier ref_qual;
20046 tree exception_specification;
20047 tree late_return;
20048 tree attrs;
20049 bool memfn = (member_p || (pushed_scope
20050 && CLASS_TYPE_P (pushed_scope)));
20052 is_declarator = true;
20054 if (ctor_dtor_or_conv_p)
20055 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20056 first = false;
20058 /* Parse the cv-qualifier-seq. */
20059 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20060 /* Parse the ref-qualifier. */
20061 ref_qual = cp_parser_ref_qualifier_opt (parser);
20062 /* Parse the tx-qualifier. */
20063 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20064 /* And the exception-specification. */
20065 exception_specification
20066 = cp_parser_exception_specification_opt (parser);
20068 attrs = cp_parser_std_attribute_spec_seq (parser);
20070 /* In here, we handle cases where attribute is used after
20071 the function declaration. For example:
20072 void func (int x) __attribute__((vector(..))); */
20073 tree gnu_attrs = NULL_TREE;
20074 if (flag_cilkplus
20075 && cp_next_tokens_can_be_gnu_attribute_p (parser))
20077 cp_parser_parse_tentatively (parser);
20078 tree attr = cp_parser_gnu_attributes_opt (parser);
20079 if (cp_lexer_next_token_is_not (parser->lexer,
20080 CPP_SEMICOLON)
20081 && cp_lexer_next_token_is_not (parser->lexer,
20082 CPP_OPEN_BRACE))
20083 cp_parser_abort_tentative_parse (parser);
20084 else if (!cp_parser_parse_definitely (parser))
20086 else
20087 gnu_attrs = attr;
20089 tree requires_clause = NULL_TREE;
20090 late_return = (cp_parser_late_return_type_opt
20091 (parser, declarator, requires_clause,
20092 memfn ? cv_quals : -1));
20094 /* Parse the virt-specifier-seq. */
20095 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20097 /* Create the function-declarator. */
20098 declarator = make_call_declarator (declarator,
20099 params,
20100 cv_quals,
20101 virt_specifiers,
20102 ref_qual,
20103 tx_qual,
20104 exception_specification,
20105 late_return,
20106 requires_clause);
20107 declarator->std_attributes = attrs;
20108 declarator->attributes = gnu_attrs;
20109 /* Any subsequent parameter lists are to do with
20110 return type, so are not those of the declared
20111 function. */
20112 parser->default_arg_ok_p = false;
20115 /* Remove the function parms from scope. */
20116 pop_bindings_and_leave_scope ();
20118 if (is_declarator)
20119 /* Repeat the main loop. */
20120 continue;
20123 /* If this is the first, we can try a parenthesized
20124 declarator. */
20125 if (first)
20127 bool saved_in_type_id_in_expr_p;
20129 parser->default_arg_ok_p = saved_default_arg_ok_p;
20130 parser->in_declarator_p = saved_in_declarator_p;
20132 open_paren = token;
20133 /* Consume the `('. */
20134 matching_parens parens;
20135 parens.consume_open (parser);
20136 /* Parse the nested declarator. */
20137 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20138 parser->in_type_id_in_expr_p = true;
20139 declarator
20140 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20141 /*parenthesized_p=*/NULL,
20142 member_p, friend_p);
20143 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20144 first = false;
20145 /* Expect a `)'. */
20146 close_paren = cp_lexer_peek_token (parser->lexer);
20147 if (!parens.require_close (parser))
20148 declarator = cp_error_declarator;
20149 if (declarator == cp_error_declarator)
20150 break;
20152 goto handle_declarator;
20154 /* Otherwise, we must be done. */
20155 else
20156 break;
20158 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20159 && token->type == CPP_OPEN_SQUARE
20160 && !cp_next_tokens_can_be_attribute_p (parser))
20162 /* Parse an array-declarator. */
20163 tree bounds, attrs;
20165 if (ctor_dtor_or_conv_p)
20166 *ctor_dtor_or_conv_p = 0;
20168 open_paren = NULL;
20169 first = false;
20170 parser->default_arg_ok_p = false;
20171 parser->in_declarator_p = true;
20172 /* Consume the `['. */
20173 cp_lexer_consume_token (parser->lexer);
20174 /* Peek at the next token. */
20175 token = cp_lexer_peek_token (parser->lexer);
20176 /* If the next token is `]', then there is no
20177 constant-expression. */
20178 if (token->type != CPP_CLOSE_SQUARE)
20180 bool non_constant_p;
20181 bounds
20182 = cp_parser_constant_expression (parser,
20183 /*allow_non_constant=*/true,
20184 &non_constant_p);
20185 if (!non_constant_p)
20186 /* OK */;
20187 else if (error_operand_p (bounds))
20188 /* Already gave an error. */;
20189 else if (!parser->in_function_body
20190 || current_binding_level->kind == sk_function_parms)
20192 /* Normally, the array bound must be an integral constant
20193 expression. However, as an extension, we allow VLAs
20194 in function scopes as long as they aren't part of a
20195 parameter declaration. */
20196 cp_parser_error (parser,
20197 "array bound is not an integer constant");
20198 bounds = error_mark_node;
20200 else if (processing_template_decl
20201 && !type_dependent_expression_p (bounds))
20203 /* Remember this wasn't a constant-expression. */
20204 bounds = build_nop (TREE_TYPE (bounds), bounds);
20205 TREE_SIDE_EFFECTS (bounds) = 1;
20208 else
20209 bounds = NULL_TREE;
20210 /* Look for the closing `]'. */
20211 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20213 declarator = cp_error_declarator;
20214 break;
20217 attrs = cp_parser_std_attribute_spec_seq (parser);
20218 declarator = make_array_declarator (declarator, bounds);
20219 declarator->std_attributes = attrs;
20221 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20224 tree qualifying_scope;
20225 tree unqualified_name;
20226 tree attrs;
20227 special_function_kind sfk;
20228 bool abstract_ok;
20229 bool pack_expansion_p = false;
20230 cp_token *declarator_id_start_token;
20232 /* Parse a declarator-id */
20233 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20234 if (abstract_ok)
20236 cp_parser_parse_tentatively (parser);
20238 /* If we see an ellipsis, we should be looking at a
20239 parameter pack. */
20240 if (token->type == CPP_ELLIPSIS)
20242 /* Consume the `...' */
20243 cp_lexer_consume_token (parser->lexer);
20245 pack_expansion_p = true;
20249 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20250 unqualified_name
20251 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20252 qualifying_scope = parser->scope;
20253 if (abstract_ok)
20255 bool okay = false;
20257 if (!unqualified_name && pack_expansion_p)
20259 /* Check whether an error occurred. */
20260 okay = !cp_parser_error_occurred (parser);
20262 /* We already consumed the ellipsis to mark a
20263 parameter pack, but we have no way to report it,
20264 so abort the tentative parse. We will be exiting
20265 immediately anyway. */
20266 cp_parser_abort_tentative_parse (parser);
20268 else
20269 okay = cp_parser_parse_definitely (parser);
20271 if (!okay)
20272 unqualified_name = error_mark_node;
20273 else if (unqualified_name
20274 && (qualifying_scope
20275 || (!identifier_p (unqualified_name))))
20277 cp_parser_error (parser, "expected unqualified-id");
20278 unqualified_name = error_mark_node;
20282 if (!unqualified_name)
20283 return NULL;
20284 if (unqualified_name == error_mark_node)
20286 declarator = cp_error_declarator;
20287 pack_expansion_p = false;
20288 declarator->parameter_pack_p = false;
20289 break;
20292 attrs = cp_parser_std_attribute_spec_seq (parser);
20294 if (qualifying_scope && at_namespace_scope_p ()
20295 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20297 /* In the declaration of a member of a template class
20298 outside of the class itself, the SCOPE will sometimes
20299 be a TYPENAME_TYPE. For example, given:
20301 template <typename T>
20302 int S<T>::R::i = 3;
20304 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20305 this context, we must resolve S<T>::R to an ordinary
20306 type, rather than a typename type.
20308 The reason we normally avoid resolving TYPENAME_TYPEs
20309 is that a specialization of `S' might render
20310 `S<T>::R' not a type. However, if `S' is
20311 specialized, then this `i' will not be used, so there
20312 is no harm in resolving the types here. */
20313 tree type;
20315 /* Resolve the TYPENAME_TYPE. */
20316 type = resolve_typename_type (qualifying_scope,
20317 /*only_current_p=*/false);
20318 /* If that failed, the declarator is invalid. */
20319 if (TREE_CODE (type) == TYPENAME_TYPE)
20321 if (typedef_variant_p (type))
20322 error_at (declarator_id_start_token->location,
20323 "cannot define member of dependent typedef "
20324 "%qT", type);
20325 else
20326 error_at (declarator_id_start_token->location,
20327 "%<%T::%E%> is not a type",
20328 TYPE_CONTEXT (qualifying_scope),
20329 TYPE_IDENTIFIER (qualifying_scope));
20331 qualifying_scope = type;
20334 sfk = sfk_none;
20336 if (unqualified_name)
20338 tree class_type;
20340 if (qualifying_scope
20341 && CLASS_TYPE_P (qualifying_scope))
20342 class_type = qualifying_scope;
20343 else
20344 class_type = current_class_type;
20346 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20348 tree name_type = TREE_TYPE (unqualified_name);
20350 if (!class_type || !same_type_p (name_type, class_type))
20352 /* We do not attempt to print the declarator
20353 here because we do not have enough
20354 information about its original syntactic
20355 form. */
20356 cp_parser_error (parser, "invalid declarator");
20357 declarator = cp_error_declarator;
20358 break;
20360 else if (qualifying_scope
20361 && CLASSTYPE_USE_TEMPLATE (name_type))
20363 error_at (declarator_id_start_token->location,
20364 "invalid use of constructor as a template");
20365 inform (declarator_id_start_token->location,
20366 "use %<%T::%D%> instead of %<%T::%D%> to "
20367 "name the constructor in a qualified name",
20368 class_type,
20369 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20370 class_type, name_type);
20371 declarator = cp_error_declarator;
20372 break;
20374 unqualified_name = constructor_name (class_type);
20377 if (class_type)
20379 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20380 sfk = sfk_destructor;
20381 else if (identifier_p (unqualified_name)
20382 && IDENTIFIER_CONV_OP_P (unqualified_name))
20383 sfk = sfk_conversion;
20384 else if (/* There's no way to declare a constructor
20385 for an unnamed type, even if the type
20386 got a name for linkage purposes. */
20387 !TYPE_WAS_UNNAMED (class_type)
20388 /* Handle correctly (c++/19200):
20390 struct S {
20391 struct T{};
20392 friend void S(T);
20395 and also:
20397 namespace N {
20398 void S();
20401 struct S {
20402 friend void N::S();
20403 }; */
20404 && (!friend_p || class_type == qualifying_scope)
20405 && constructor_name_p (unqualified_name,
20406 class_type))
20407 sfk = sfk_constructor;
20408 else if (is_overloaded_fn (unqualified_name)
20409 && DECL_CONSTRUCTOR_P (get_first_fn
20410 (unqualified_name)))
20411 sfk = sfk_constructor;
20413 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20414 *ctor_dtor_or_conv_p = -1;
20417 declarator = make_id_declarator (qualifying_scope,
20418 unqualified_name,
20419 sfk);
20420 declarator->std_attributes = attrs;
20421 declarator->id_loc = token->location;
20422 declarator->parameter_pack_p = pack_expansion_p;
20424 if (pack_expansion_p)
20425 maybe_warn_variadic_templates ();
20428 handle_declarator:;
20429 scope = get_scope_of_declarator (declarator);
20430 if (scope)
20432 /* Any names that appear after the declarator-id for a
20433 member are looked up in the containing scope. */
20434 if (at_function_scope_p ())
20436 /* But declarations with qualified-ids can't appear in a
20437 function. */
20438 cp_parser_error (parser, "qualified-id in declaration");
20439 declarator = cp_error_declarator;
20440 break;
20442 pushed_scope = push_scope (scope);
20444 parser->in_declarator_p = true;
20445 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20446 || (declarator && declarator->kind == cdk_id))
20447 /* Default args are only allowed on function
20448 declarations. */
20449 parser->default_arg_ok_p = saved_default_arg_ok_p;
20450 else
20451 parser->default_arg_ok_p = false;
20453 first = false;
20455 /* We're done. */
20456 else
20457 break;
20460 /* For an abstract declarator, we might wind up with nothing at this
20461 point. That's an error; the declarator is not optional. */
20462 if (!declarator)
20463 cp_parser_error (parser, "expected declarator");
20464 else if (open_paren)
20466 /* Record overly parenthesized declarator so we can give a
20467 diagnostic about confusing decl/expr disambiguation. */
20468 if (declarator->kind == cdk_array)
20470 /* If the open and close parens are on different lines, this
20471 is probably a formatting thing, so ignore. */
20472 expanded_location open = expand_location (open_paren->location);
20473 expanded_location close = expand_location (close_paren->location);
20474 if (open.line != close.line || open.file != close.file)
20475 open_paren = NULL;
20477 if (open_paren)
20478 declarator->parenthesized = open_paren->location;
20481 /* If we entered a scope, we must exit it now. */
20482 if (pushed_scope)
20483 pop_scope (pushed_scope);
20485 parser->default_arg_ok_p = saved_default_arg_ok_p;
20486 parser->in_declarator_p = saved_in_declarator_p;
20488 return declarator;
20491 /* Parse a ptr-operator.
20493 ptr-operator:
20494 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20495 * cv-qualifier-seq [opt]
20497 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20498 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20500 GNU Extension:
20502 ptr-operator:
20503 & cv-qualifier-seq [opt]
20505 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20506 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20507 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20508 filled in with the TYPE containing the member. *CV_QUALS is
20509 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20510 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20511 Note that the tree codes returned by this function have nothing
20512 to do with the types of trees that will be eventually be created
20513 to represent the pointer or reference type being parsed. They are
20514 just constants with suggestive names. */
20515 static enum tree_code
20516 cp_parser_ptr_operator (cp_parser* parser,
20517 tree* type,
20518 cp_cv_quals *cv_quals,
20519 tree *attributes)
20521 enum tree_code code = ERROR_MARK;
20522 cp_token *token;
20523 tree attrs = NULL_TREE;
20525 /* Assume that it's not a pointer-to-member. */
20526 *type = NULL_TREE;
20527 /* And that there are no cv-qualifiers. */
20528 *cv_quals = TYPE_UNQUALIFIED;
20530 /* Peek at the next token. */
20531 token = cp_lexer_peek_token (parser->lexer);
20533 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20534 if (token->type == CPP_MULT)
20535 code = INDIRECT_REF;
20536 else if (token->type == CPP_AND)
20537 code = ADDR_EXPR;
20538 else if ((cxx_dialect != cxx98) &&
20539 token->type == CPP_AND_AND) /* C++0x only */
20540 code = NON_LVALUE_EXPR;
20542 if (code != ERROR_MARK)
20544 /* Consume the `*', `&' or `&&'. */
20545 cp_lexer_consume_token (parser->lexer);
20547 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20548 `&', if we are allowing GNU extensions. (The only qualifier
20549 that can legally appear after `&' is `restrict', but that is
20550 enforced during semantic analysis. */
20551 if (code == INDIRECT_REF
20552 || cp_parser_allow_gnu_extensions_p (parser))
20553 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20555 attrs = cp_parser_std_attribute_spec_seq (parser);
20556 if (attributes != NULL)
20557 *attributes = attrs;
20559 else
20561 /* Try the pointer-to-member case. */
20562 cp_parser_parse_tentatively (parser);
20563 /* Look for the optional `::' operator. */
20564 cp_parser_global_scope_opt (parser,
20565 /*current_scope_valid_p=*/false);
20566 /* Look for the nested-name specifier. */
20567 token = cp_lexer_peek_token (parser->lexer);
20568 cp_parser_nested_name_specifier (parser,
20569 /*typename_keyword_p=*/false,
20570 /*check_dependency_p=*/true,
20571 /*type_p=*/false,
20572 /*is_declaration=*/false);
20573 /* If we found it, and the next token is a `*', then we are
20574 indeed looking at a pointer-to-member operator. */
20575 if (!cp_parser_error_occurred (parser)
20576 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20578 /* Indicate that the `*' operator was used. */
20579 code = INDIRECT_REF;
20581 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20582 error_at (token->location, "%qD is a namespace", parser->scope);
20583 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20584 error_at (token->location, "cannot form pointer to member of "
20585 "non-class %q#T", parser->scope);
20586 else
20588 /* The type of which the member is a member is given by the
20589 current SCOPE. */
20590 *type = parser->scope;
20591 /* The next name will not be qualified. */
20592 parser->scope = NULL_TREE;
20593 parser->qualifying_scope = NULL_TREE;
20594 parser->object_scope = NULL_TREE;
20595 /* Look for optional c++11 attributes. */
20596 attrs = cp_parser_std_attribute_spec_seq (parser);
20597 if (attributes != NULL)
20598 *attributes = attrs;
20599 /* Look for the optional cv-qualifier-seq. */
20600 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20603 /* If that didn't work we don't have a ptr-operator. */
20604 if (!cp_parser_parse_definitely (parser))
20605 cp_parser_error (parser, "expected ptr-operator");
20608 return code;
20611 /* Parse an (optional) cv-qualifier-seq.
20613 cv-qualifier-seq:
20614 cv-qualifier cv-qualifier-seq [opt]
20616 cv-qualifier:
20617 const
20618 volatile
20620 GNU Extension:
20622 cv-qualifier:
20623 __restrict__
20625 Returns a bitmask representing the cv-qualifiers. */
20627 static cp_cv_quals
20628 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20630 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20632 while (true)
20634 cp_token *token;
20635 cp_cv_quals cv_qualifier;
20637 /* Peek at the next token. */
20638 token = cp_lexer_peek_token (parser->lexer);
20639 /* See if it's a cv-qualifier. */
20640 switch (token->keyword)
20642 case RID_CONST:
20643 cv_qualifier = TYPE_QUAL_CONST;
20644 break;
20646 case RID_VOLATILE:
20647 cv_qualifier = TYPE_QUAL_VOLATILE;
20648 break;
20650 case RID_RESTRICT:
20651 cv_qualifier = TYPE_QUAL_RESTRICT;
20652 break;
20654 default:
20655 cv_qualifier = TYPE_UNQUALIFIED;
20656 break;
20659 if (!cv_qualifier)
20660 break;
20662 if (cv_quals & cv_qualifier)
20664 gcc_rich_location richloc (token->location);
20665 richloc.add_fixit_remove ();
20666 error_at (&richloc, "duplicate cv-qualifier");
20667 cp_lexer_purge_token (parser->lexer);
20669 else
20671 cp_lexer_consume_token (parser->lexer);
20672 cv_quals |= cv_qualifier;
20676 return cv_quals;
20679 /* Parse an (optional) ref-qualifier
20681 ref-qualifier:
20685 Returns cp_ref_qualifier representing ref-qualifier. */
20687 static cp_ref_qualifier
20688 cp_parser_ref_qualifier_opt (cp_parser* parser)
20690 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20692 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20693 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20694 return ref_qual;
20696 while (true)
20698 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20699 cp_token *token = cp_lexer_peek_token (parser->lexer);
20701 switch (token->type)
20703 case CPP_AND:
20704 curr_ref_qual = REF_QUAL_LVALUE;
20705 break;
20707 case CPP_AND_AND:
20708 curr_ref_qual = REF_QUAL_RVALUE;
20709 break;
20711 default:
20712 curr_ref_qual = REF_QUAL_NONE;
20713 break;
20716 if (!curr_ref_qual)
20717 break;
20718 else if (ref_qual)
20720 error_at (token->location, "multiple ref-qualifiers");
20721 cp_lexer_purge_token (parser->lexer);
20723 else
20725 ref_qual = curr_ref_qual;
20726 cp_lexer_consume_token (parser->lexer);
20730 return ref_qual;
20733 /* Parse an optional tx-qualifier.
20735 tx-qualifier:
20736 transaction_safe
20737 transaction_safe_dynamic */
20739 static tree
20740 cp_parser_tx_qualifier_opt (cp_parser *parser)
20742 cp_token *token = cp_lexer_peek_token (parser->lexer);
20743 if (token->type == CPP_NAME)
20745 tree name = token->u.value;
20746 const char *p = IDENTIFIER_POINTER (name);
20747 const int len = strlen ("transaction_safe");
20748 if (!strncmp (p, "transaction_safe", len))
20750 p += len;
20751 if (*p == '\0'
20752 || !strcmp (p, "_dynamic"))
20754 cp_lexer_consume_token (parser->lexer);
20755 if (!flag_tm)
20757 error ("%qE requires %<-fgnu-tm%>", name);
20758 return NULL_TREE;
20760 else
20761 return name;
20765 return NULL_TREE;
20768 /* Parse an (optional) virt-specifier-seq.
20770 virt-specifier-seq:
20771 virt-specifier virt-specifier-seq [opt]
20773 virt-specifier:
20774 override
20775 final
20777 Returns a bitmask representing the virt-specifiers. */
20779 static cp_virt_specifiers
20780 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20782 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20784 while (true)
20786 cp_token *token;
20787 cp_virt_specifiers virt_specifier;
20789 /* Peek at the next token. */
20790 token = cp_lexer_peek_token (parser->lexer);
20791 /* See if it's a virt-specifier-qualifier. */
20792 if (token->type != CPP_NAME)
20793 break;
20794 if (id_equal (token->u.value, "override"))
20796 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20797 virt_specifier = VIRT_SPEC_OVERRIDE;
20799 else if (id_equal (token->u.value, "final"))
20801 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20802 virt_specifier = VIRT_SPEC_FINAL;
20804 else if (id_equal (token->u.value, "__final"))
20806 virt_specifier = VIRT_SPEC_FINAL;
20808 else
20809 break;
20811 if (virt_specifiers & virt_specifier)
20813 gcc_rich_location richloc (token->location);
20814 richloc.add_fixit_remove ();
20815 error_at (&richloc, "duplicate virt-specifier");
20816 cp_lexer_purge_token (parser->lexer);
20818 else
20820 cp_lexer_consume_token (parser->lexer);
20821 virt_specifiers |= virt_specifier;
20824 return virt_specifiers;
20827 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20828 is in scope even though it isn't real. */
20830 void
20831 inject_this_parameter (tree ctype, cp_cv_quals quals)
20833 tree this_parm;
20835 if (current_class_ptr)
20837 /* We don't clear this between NSDMIs. Is it already what we want? */
20838 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20839 if (DECL_P (current_class_ptr)
20840 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20841 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20842 && cp_type_quals (type) == quals)
20843 return;
20846 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20847 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20848 current_class_ptr = NULL_TREE;
20849 current_class_ref
20850 = cp_build_fold_indirect_ref (this_parm);
20851 current_class_ptr = this_parm;
20854 /* Return true iff our current scope is a non-static data member
20855 initializer. */
20857 bool
20858 parsing_nsdmi (void)
20860 /* We recognize NSDMI context by the context-less 'this' pointer set up
20861 by the function above. */
20862 if (current_class_ptr
20863 && TREE_CODE (current_class_ptr) == PARM_DECL
20864 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20865 return true;
20866 return false;
20869 /* Parse a late-specified return type, if any. This is not a separate
20870 non-terminal, but part of a function declarator, which looks like
20872 -> trailing-type-specifier-seq abstract-declarator(opt)
20874 Returns the type indicated by the type-id.
20876 In addition to this, parse any queued up #pragma omp declare simd
20877 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20878 #pragma acc routine clauses.
20880 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20881 function. */
20883 static tree
20884 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20885 tree& requires_clause, cp_cv_quals quals)
20887 cp_token *token;
20888 tree type = NULL_TREE;
20889 bool declare_simd_p = (parser->omp_declare_simd
20890 && declarator
20891 && declarator->kind == cdk_id);
20893 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20894 && declarator && declarator->kind == cdk_id);
20896 bool oacc_routine_p = (parser->oacc_routine
20897 && declarator
20898 && declarator->kind == cdk_id);
20900 /* Peek at the next token. */
20901 token = cp_lexer_peek_token (parser->lexer);
20902 /* A late-specified return type is indicated by an initial '->'. */
20903 if (token->type != CPP_DEREF
20904 && token->keyword != RID_REQUIRES
20905 && !(token->type == CPP_NAME
20906 && token->u.value == ridpointers[RID_REQUIRES])
20907 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20908 return NULL_TREE;
20910 tree save_ccp = current_class_ptr;
20911 tree save_ccr = current_class_ref;
20912 if (quals >= 0)
20914 /* DR 1207: 'this' is in scope in the trailing return type. */
20915 inject_this_parameter (current_class_type, quals);
20918 if (token->type == CPP_DEREF)
20920 /* Consume the ->. */
20921 cp_lexer_consume_token (parser->lexer);
20923 type = cp_parser_trailing_type_id (parser);
20926 /* Function declarations may be followed by a trailing
20927 requires-clause. */
20928 requires_clause = cp_parser_requires_clause_opt (parser);
20930 if (cilk_simd_fn_vector_p)
20931 declarator->attributes
20932 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20933 declarator->attributes);
20934 if (declare_simd_p)
20935 declarator->attributes
20936 = cp_parser_late_parsing_omp_declare_simd (parser,
20937 declarator->attributes);
20938 if (oacc_routine_p)
20939 declarator->attributes
20940 = cp_parser_late_parsing_oacc_routine (parser,
20941 declarator->attributes);
20943 if (quals >= 0)
20945 current_class_ptr = save_ccp;
20946 current_class_ref = save_ccr;
20949 return type;
20952 /* Parse a declarator-id.
20954 declarator-id:
20955 id-expression
20956 :: [opt] nested-name-specifier [opt] type-name
20958 In the `id-expression' case, the value returned is as for
20959 cp_parser_id_expression if the id-expression was an unqualified-id.
20960 If the id-expression was a qualified-id, then a SCOPE_REF is
20961 returned. The first operand is the scope (either a NAMESPACE_DECL
20962 or TREE_TYPE), but the second is still just a representation of an
20963 unqualified-id. */
20965 static tree
20966 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20968 tree id;
20969 /* The expression must be an id-expression. Assume that qualified
20970 names are the names of types so that:
20972 template <class T>
20973 int S<T>::R::i = 3;
20975 will work; we must treat `S<T>::R' as the name of a type.
20976 Similarly, assume that qualified names are templates, where
20977 required, so that:
20979 template <class T>
20980 int S<T>::R<T>::i = 3;
20982 will work, too. */
20983 id = cp_parser_id_expression (parser,
20984 /*template_keyword_p=*/false,
20985 /*check_dependency_p=*/false,
20986 /*template_p=*/NULL,
20987 /*declarator_p=*/true,
20988 optional_p);
20989 if (id && BASELINK_P (id))
20990 id = BASELINK_FUNCTIONS (id);
20991 return id;
20994 /* Parse a type-id.
20996 type-id:
20997 type-specifier-seq abstract-declarator [opt]
20999 Returns the TYPE specified. */
21001 static tree
21002 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21003 bool is_trailing_return)
21005 cp_decl_specifier_seq type_specifier_seq;
21006 cp_declarator *abstract_declarator;
21008 /* Parse the type-specifier-seq. */
21009 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21010 is_trailing_return,
21011 &type_specifier_seq);
21012 if (is_template_arg && type_specifier_seq.type
21013 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21014 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21015 /* A bare template name as a template argument is a template template
21016 argument, not a placeholder, so fail parsing it as a type argument. */
21018 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21019 cp_parser_simulate_error (parser);
21020 return error_mark_node;
21022 if (type_specifier_seq.type == error_mark_node)
21023 return error_mark_node;
21025 /* There might or might not be an abstract declarator. */
21026 cp_parser_parse_tentatively (parser);
21027 /* Look for the declarator. */
21028 abstract_declarator
21029 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21030 /*parenthesized_p=*/NULL,
21031 /*member_p=*/false,
21032 /*friend_p=*/false);
21033 /* Check to see if there really was a declarator. */
21034 if (!cp_parser_parse_definitely (parser))
21035 abstract_declarator = NULL;
21037 if (type_specifier_seq.type
21038 /* The concepts TS allows 'auto' as a type-id. */
21039 && (!flag_concepts || parser->in_type_id_in_expr_p)
21040 /* None of the valid uses of 'auto' in C++14 involve the type-id
21041 nonterminal, but it is valid in a trailing-return-type. */
21042 && !(cxx_dialect >= cxx14 && is_trailing_return))
21043 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21045 /* A type-id with type 'auto' is only ok if the abstract declarator
21046 is a function declarator with a late-specified return type.
21048 A type-id with 'auto' is also valid in a trailing-return-type
21049 in a compound-requirement. */
21050 if (abstract_declarator
21051 && abstract_declarator->kind == cdk_function
21052 && abstract_declarator->u.function.late_return_type)
21053 /* OK */;
21054 else if (parser->in_result_type_constraint_p)
21055 /* OK */;
21056 else
21058 location_t loc = type_specifier_seq.locations[ds_type_spec];
21059 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21061 error_at (loc, "missing template arguments after %qT",
21062 auto_node);
21063 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21064 tmpl);
21066 else
21067 error_at (loc, "invalid use of %qT", auto_node);
21068 return error_mark_node;
21072 return groktypename (&type_specifier_seq, abstract_declarator,
21073 is_template_arg);
21076 static tree
21077 cp_parser_type_id (cp_parser *parser)
21079 return cp_parser_type_id_1 (parser, false, false);
21082 static tree
21083 cp_parser_template_type_arg (cp_parser *parser)
21085 tree r;
21086 const char *saved_message = parser->type_definition_forbidden_message;
21087 parser->type_definition_forbidden_message
21088 = G_("types may not be defined in template arguments");
21089 r = cp_parser_type_id_1 (parser, true, false);
21090 parser->type_definition_forbidden_message = saved_message;
21091 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21093 error ("invalid use of %<auto%> in template argument");
21094 r = error_mark_node;
21096 return r;
21099 static tree
21100 cp_parser_trailing_type_id (cp_parser *parser)
21102 return cp_parser_type_id_1 (parser, false, true);
21105 /* Parse a type-specifier-seq.
21107 type-specifier-seq:
21108 type-specifier type-specifier-seq [opt]
21110 GNU extension:
21112 type-specifier-seq:
21113 attributes type-specifier-seq [opt]
21115 If IS_DECLARATION is true, we are at the start of a "condition" or
21116 exception-declaration, so we might be followed by a declarator-id.
21118 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21119 i.e. we've just seen "->".
21121 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21123 static void
21124 cp_parser_type_specifier_seq (cp_parser* parser,
21125 bool is_declaration,
21126 bool is_trailing_return,
21127 cp_decl_specifier_seq *type_specifier_seq)
21129 bool seen_type_specifier = false;
21130 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21131 cp_token *start_token = NULL;
21133 /* Clear the TYPE_SPECIFIER_SEQ. */
21134 clear_decl_specs (type_specifier_seq);
21136 /* In the context of a trailing return type, enum E { } is an
21137 elaborated-type-specifier followed by a function-body, not an
21138 enum-specifier. */
21139 if (is_trailing_return)
21140 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21142 /* Parse the type-specifiers and attributes. */
21143 while (true)
21145 tree type_specifier;
21146 bool is_cv_qualifier;
21148 /* Check for attributes first. */
21149 if (cp_next_tokens_can_be_attribute_p (parser))
21151 type_specifier_seq->attributes =
21152 chainon (type_specifier_seq->attributes,
21153 cp_parser_attributes_opt (parser));
21154 continue;
21157 /* record the token of the beginning of the type specifier seq,
21158 for error reporting purposes*/
21159 if (!start_token)
21160 start_token = cp_lexer_peek_token (parser->lexer);
21162 /* Look for the type-specifier. */
21163 type_specifier = cp_parser_type_specifier (parser,
21164 flags,
21165 type_specifier_seq,
21166 /*is_declaration=*/false,
21167 NULL,
21168 &is_cv_qualifier);
21169 if (!type_specifier)
21171 /* If the first type-specifier could not be found, this is not a
21172 type-specifier-seq at all. */
21173 if (!seen_type_specifier)
21175 /* Set in_declarator_p to avoid skipping to the semicolon. */
21176 int in_decl = parser->in_declarator_p;
21177 parser->in_declarator_p = true;
21179 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21180 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21181 cp_parser_error (parser, "expected type-specifier");
21183 parser->in_declarator_p = in_decl;
21185 type_specifier_seq->type = error_mark_node;
21186 return;
21188 /* If subsequent type-specifiers could not be found, the
21189 type-specifier-seq is complete. */
21190 break;
21193 seen_type_specifier = true;
21194 /* The standard says that a condition can be:
21196 type-specifier-seq declarator = assignment-expression
21198 However, given:
21200 struct S {};
21201 if (int S = ...)
21203 we should treat the "S" as a declarator, not as a
21204 type-specifier. The standard doesn't say that explicitly for
21205 type-specifier-seq, but it does say that for
21206 decl-specifier-seq in an ordinary declaration. Perhaps it
21207 would be clearer just to allow a decl-specifier-seq here, and
21208 then add a semantic restriction that if any decl-specifiers
21209 that are not type-specifiers appear, the program is invalid. */
21210 if (is_declaration && !is_cv_qualifier)
21211 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21215 /* Return whether the function currently being declared has an associated
21216 template parameter list. */
21218 static bool
21219 function_being_declared_is_template_p (cp_parser* parser)
21221 if (!current_template_parms || processing_template_parmlist)
21222 return false;
21224 if (parser->implicit_template_scope)
21225 return true;
21227 if (at_class_scope_p ()
21228 && TYPE_BEING_DEFINED (current_class_type))
21229 return parser->num_template_parameter_lists != 0;
21231 return ((int) parser->num_template_parameter_lists > template_class_depth
21232 (current_class_type));
21235 /* Parse a parameter-declaration-clause.
21237 parameter-declaration-clause:
21238 parameter-declaration-list [opt] ... [opt]
21239 parameter-declaration-list , ...
21241 Returns a representation for the parameter declarations. A return
21242 value of NULL indicates a parameter-declaration-clause consisting
21243 only of an ellipsis. */
21245 static tree
21246 cp_parser_parameter_declaration_clause (cp_parser* parser)
21248 tree parameters;
21249 cp_token *token;
21250 bool ellipsis_p;
21251 bool is_error;
21253 struct cleanup {
21254 cp_parser* parser;
21255 int auto_is_implicit_function_template_parm_p;
21256 ~cleanup() {
21257 parser->auto_is_implicit_function_template_parm_p
21258 = auto_is_implicit_function_template_parm_p;
21260 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21262 (void) cleanup;
21264 if (!processing_specialization
21265 && !processing_template_parmlist
21266 && !processing_explicit_instantiation)
21267 if (!current_function_decl
21268 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21269 parser->auto_is_implicit_function_template_parm_p = true;
21271 /* Peek at the next token. */
21272 token = cp_lexer_peek_token (parser->lexer);
21273 /* Check for trivial parameter-declaration-clauses. */
21274 if (token->type == CPP_ELLIPSIS)
21276 /* Consume the `...' token. */
21277 cp_lexer_consume_token (parser->lexer);
21278 return NULL_TREE;
21280 else if (token->type == CPP_CLOSE_PAREN)
21281 /* There are no parameters. */
21283 #ifndef NO_IMPLICIT_EXTERN_C
21284 if (in_system_header_at (input_location)
21285 && current_class_type == NULL
21286 && current_lang_name == lang_name_c)
21287 return NULL_TREE;
21288 else
21289 #endif
21290 return void_list_node;
21292 /* Check for `(void)', too, which is a special case. */
21293 else if (token->keyword == RID_VOID
21294 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21295 == CPP_CLOSE_PAREN))
21297 /* Consume the `void' token. */
21298 cp_lexer_consume_token (parser->lexer);
21299 /* There are no parameters. */
21300 return void_list_node;
21303 /* Parse the parameter-declaration-list. */
21304 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21305 /* If a parse error occurred while parsing the
21306 parameter-declaration-list, then the entire
21307 parameter-declaration-clause is erroneous. */
21308 if (is_error)
21309 return NULL;
21311 /* Peek at the next token. */
21312 token = cp_lexer_peek_token (parser->lexer);
21313 /* If it's a `,', the clause should terminate with an ellipsis. */
21314 if (token->type == CPP_COMMA)
21316 /* Consume the `,'. */
21317 cp_lexer_consume_token (parser->lexer);
21318 /* Expect an ellipsis. */
21319 ellipsis_p
21320 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21322 /* It might also be `...' if the optional trailing `,' was
21323 omitted. */
21324 else if (token->type == CPP_ELLIPSIS)
21326 /* Consume the `...' token. */
21327 cp_lexer_consume_token (parser->lexer);
21328 /* And remember that we saw it. */
21329 ellipsis_p = true;
21331 else
21332 ellipsis_p = false;
21334 /* Finish the parameter list. */
21335 if (!ellipsis_p)
21336 parameters = chainon (parameters, void_list_node);
21338 return parameters;
21341 /* Parse a parameter-declaration-list.
21343 parameter-declaration-list:
21344 parameter-declaration
21345 parameter-declaration-list , parameter-declaration
21347 Returns a representation of the parameter-declaration-list, as for
21348 cp_parser_parameter_declaration_clause. However, the
21349 `void_list_node' is never appended to the list. Upon return,
21350 *IS_ERROR will be true iff an error occurred. */
21352 static tree
21353 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21355 tree parameters = NULL_TREE;
21356 tree *tail = &parameters;
21357 bool saved_in_unbraced_linkage_specification_p;
21358 int index = 0;
21360 /* Assume all will go well. */
21361 *is_error = false;
21362 /* The special considerations that apply to a function within an
21363 unbraced linkage specifications do not apply to the parameters
21364 to the function. */
21365 saved_in_unbraced_linkage_specification_p
21366 = parser->in_unbraced_linkage_specification_p;
21367 parser->in_unbraced_linkage_specification_p = false;
21369 /* Look for more parameters. */
21370 while (true)
21372 cp_parameter_declarator *parameter;
21373 tree decl = error_mark_node;
21374 bool parenthesized_p = false;
21375 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21376 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21377 (current_template_parms)) : 0);
21379 /* Parse the parameter. */
21380 parameter
21381 = cp_parser_parameter_declaration (parser,
21382 /*template_parm_p=*/false,
21383 &parenthesized_p);
21385 /* We don't know yet if the enclosing context is deprecated, so wait
21386 and warn in grokparms if appropriate. */
21387 deprecated_state = DEPRECATED_SUPPRESS;
21389 if (parameter)
21391 /* If a function parameter pack was specified and an implicit template
21392 parameter was introduced during cp_parser_parameter_declaration,
21393 change any implicit parameters introduced into packs. */
21394 if (parser->implicit_template_parms
21395 && parameter->declarator
21396 && parameter->declarator->parameter_pack_p)
21398 int latest_template_parm_idx = TREE_VEC_LENGTH
21399 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21401 if (latest_template_parm_idx != template_parm_idx)
21402 parameter->decl_specifiers.type = convert_generic_types_to_packs
21403 (parameter->decl_specifiers.type,
21404 template_parm_idx, latest_template_parm_idx);
21407 decl = grokdeclarator (parameter->declarator,
21408 &parameter->decl_specifiers,
21409 PARM,
21410 parameter->default_argument != NULL_TREE,
21411 &parameter->decl_specifiers.attributes);
21412 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21413 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21416 deprecated_state = DEPRECATED_NORMAL;
21418 /* If a parse error occurred parsing the parameter declaration,
21419 then the entire parameter-declaration-list is erroneous. */
21420 if (decl == error_mark_node)
21422 *is_error = true;
21423 parameters = error_mark_node;
21424 break;
21427 if (parameter->decl_specifiers.attributes)
21428 cplus_decl_attributes (&decl,
21429 parameter->decl_specifiers.attributes,
21431 if (DECL_NAME (decl))
21432 decl = pushdecl (decl);
21434 if (decl != error_mark_node)
21436 retrofit_lang_decl (decl);
21437 DECL_PARM_INDEX (decl) = ++index;
21438 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21441 /* Add the new parameter to the list. */
21442 *tail = build_tree_list (parameter->default_argument, decl);
21443 tail = &TREE_CHAIN (*tail);
21445 /* Peek at the next token. */
21446 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21447 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21448 /* These are for Objective-C++ */
21449 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21450 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21451 /* The parameter-declaration-list is complete. */
21452 break;
21453 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21455 cp_token *token;
21457 /* Peek at the next token. */
21458 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21459 /* If it's an ellipsis, then the list is complete. */
21460 if (token->type == CPP_ELLIPSIS)
21461 break;
21462 /* Otherwise, there must be more parameters. Consume the
21463 `,'. */
21464 cp_lexer_consume_token (parser->lexer);
21465 /* When parsing something like:
21467 int i(float f, double d)
21469 we can tell after seeing the declaration for "f" that we
21470 are not looking at an initialization of a variable "i",
21471 but rather at the declaration of a function "i".
21473 Due to the fact that the parsing of template arguments
21474 (as specified to a template-id) requires backtracking we
21475 cannot use this technique when inside a template argument
21476 list. */
21477 if (!parser->in_template_argument_list_p
21478 && !parser->in_type_id_in_expr_p
21479 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21480 /* However, a parameter-declaration of the form
21481 "float(f)" (which is a valid declaration of a
21482 parameter "f") can also be interpreted as an
21483 expression (the conversion of "f" to "float"). */
21484 && !parenthesized_p)
21485 cp_parser_commit_to_tentative_parse (parser);
21487 else
21489 cp_parser_error (parser, "expected %<,%> or %<...%>");
21490 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21491 cp_parser_skip_to_closing_parenthesis (parser,
21492 /*recovering=*/true,
21493 /*or_comma=*/false,
21494 /*consume_paren=*/false);
21495 break;
21499 parser->in_unbraced_linkage_specification_p
21500 = saved_in_unbraced_linkage_specification_p;
21502 /* Reset implicit_template_scope if we are about to leave the function
21503 parameter list that introduced it. Note that for out-of-line member
21504 definitions, there will be one or more class scopes before we get to
21505 the template parameter scope. */
21507 if (cp_binding_level *its = parser->implicit_template_scope)
21508 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21510 while (maybe_its->kind == sk_class)
21511 maybe_its = maybe_its->level_chain;
21512 if (maybe_its == its)
21514 parser->implicit_template_parms = 0;
21515 parser->implicit_template_scope = 0;
21519 return parameters;
21522 /* Parse a parameter declaration.
21524 parameter-declaration:
21525 decl-specifier-seq ... [opt] declarator
21526 decl-specifier-seq declarator = assignment-expression
21527 decl-specifier-seq ... [opt] abstract-declarator [opt]
21528 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21530 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21531 declares a template parameter. (In that case, a non-nested `>'
21532 token encountered during the parsing of the assignment-expression
21533 is not interpreted as a greater-than operator.)
21535 Returns a representation of the parameter, or NULL if an error
21536 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21537 true iff the declarator is of the form "(p)". */
21539 static cp_parameter_declarator *
21540 cp_parser_parameter_declaration (cp_parser *parser,
21541 bool template_parm_p,
21542 bool *parenthesized_p)
21544 int declares_class_or_enum;
21545 cp_decl_specifier_seq decl_specifiers;
21546 cp_declarator *declarator;
21547 tree default_argument;
21548 cp_token *token = NULL, *declarator_token_start = NULL;
21549 const char *saved_message;
21550 bool template_parameter_pack_p = false;
21552 /* In a template parameter, `>' is not an operator.
21554 [temp.param]
21556 When parsing a default template-argument for a non-type
21557 template-parameter, the first non-nested `>' is taken as the end
21558 of the template parameter-list rather than a greater-than
21559 operator. */
21561 /* Type definitions may not appear in parameter types. */
21562 saved_message = parser->type_definition_forbidden_message;
21563 parser->type_definition_forbidden_message
21564 = G_("types may not be defined in parameter types");
21566 /* Parse the declaration-specifiers. */
21567 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21568 cp_parser_decl_specifier_seq (parser,
21569 CP_PARSER_FLAGS_NONE,
21570 &decl_specifiers,
21571 &declares_class_or_enum);
21573 /* Complain about missing 'typename' or other invalid type names. */
21574 if (!decl_specifiers.any_type_specifiers_p
21575 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21576 decl_specifiers.type = error_mark_node;
21578 /* If an error occurred, there's no reason to attempt to parse the
21579 rest of the declaration. */
21580 if (cp_parser_error_occurred (parser))
21582 parser->type_definition_forbidden_message = saved_message;
21583 return NULL;
21586 /* Peek at the next token. */
21587 token = cp_lexer_peek_token (parser->lexer);
21589 /* If the next token is a `)', `,', `=', `>', or `...', then there
21590 is no declarator. However, when variadic templates are enabled,
21591 there may be a declarator following `...'. */
21592 if (token->type == CPP_CLOSE_PAREN
21593 || token->type == CPP_COMMA
21594 || token->type == CPP_EQ
21595 || token->type == CPP_GREATER)
21597 declarator = NULL;
21598 if (parenthesized_p)
21599 *parenthesized_p = false;
21601 /* Otherwise, there should be a declarator. */
21602 else
21604 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21605 parser->default_arg_ok_p = false;
21607 /* After seeing a decl-specifier-seq, if the next token is not a
21608 "(", there is no possibility that the code is a valid
21609 expression. Therefore, if parsing tentatively, we commit at
21610 this point. */
21611 if (!parser->in_template_argument_list_p
21612 /* In an expression context, having seen:
21614 (int((char ...
21616 we cannot be sure whether we are looking at a
21617 function-type (taking a "char" as a parameter) or a cast
21618 of some object of type "char" to "int". */
21619 && !parser->in_type_id_in_expr_p
21620 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21621 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21622 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21623 cp_parser_commit_to_tentative_parse (parser);
21624 /* Parse the declarator. */
21625 declarator_token_start = token;
21626 declarator = cp_parser_declarator (parser,
21627 CP_PARSER_DECLARATOR_EITHER,
21628 /*ctor_dtor_or_conv_p=*/NULL,
21629 parenthesized_p,
21630 /*member_p=*/false,
21631 /*friend_p=*/false);
21632 parser->default_arg_ok_p = saved_default_arg_ok_p;
21633 /* After the declarator, allow more attributes. */
21634 decl_specifiers.attributes
21635 = chainon (decl_specifiers.attributes,
21636 cp_parser_attributes_opt (parser));
21638 /* If the declarator is a template parameter pack, remember that and
21639 clear the flag in the declarator itself so we don't get errors
21640 from grokdeclarator. */
21641 if (template_parm_p && declarator && declarator->parameter_pack_p)
21643 declarator->parameter_pack_p = false;
21644 template_parameter_pack_p = true;
21648 /* If the next token is an ellipsis, and we have not seen a declarator
21649 name, and if either the type of the declarator contains parameter
21650 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21651 for, eg, abbreviated integral type names), then we actually have a
21652 parameter pack expansion expression. Otherwise, leave the ellipsis
21653 for a C-style variadic function. */
21654 token = cp_lexer_peek_token (parser->lexer);
21655 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21657 tree type = decl_specifiers.type;
21659 if (type && DECL_P (type))
21660 type = TREE_TYPE (type);
21662 if (((type
21663 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21664 && (template_parm_p || uses_parameter_packs (type)))
21665 || (!type && template_parm_p))
21666 && declarator_can_be_parameter_pack (declarator))
21668 /* Consume the `...'. */
21669 cp_lexer_consume_token (parser->lexer);
21670 maybe_warn_variadic_templates ();
21672 /* Build a pack expansion type */
21673 if (template_parm_p)
21674 template_parameter_pack_p = true;
21675 else if (declarator)
21676 declarator->parameter_pack_p = true;
21677 else
21678 decl_specifiers.type = make_pack_expansion (type);
21682 /* The restriction on defining new types applies only to the type
21683 of the parameter, not to the default argument. */
21684 parser->type_definition_forbidden_message = saved_message;
21686 /* If the next token is `=', then process a default argument. */
21687 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21689 tree type = decl_specifiers.type;
21690 token = cp_lexer_peek_token (parser->lexer);
21691 /* If we are defining a class, then the tokens that make up the
21692 default argument must be saved and processed later. */
21693 if (!template_parm_p && at_class_scope_p ()
21694 && TYPE_BEING_DEFINED (current_class_type)
21695 && !LAMBDA_TYPE_P (current_class_type))
21696 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21698 // A constrained-type-specifier may declare a type template-parameter.
21699 else if (declares_constrained_type_template_parameter (type))
21700 default_argument
21701 = cp_parser_default_type_template_argument (parser);
21703 // A constrained-type-specifier may declare a template-template-parameter.
21704 else if (declares_constrained_template_template_parameter (type))
21705 default_argument
21706 = cp_parser_default_template_template_argument (parser);
21708 /* Outside of a class definition, we can just parse the
21709 assignment-expression. */
21710 else
21711 default_argument
21712 = cp_parser_default_argument (parser, template_parm_p);
21714 if (!parser->default_arg_ok_p)
21716 permerror (token->location,
21717 "default arguments are only "
21718 "permitted for function parameters");
21720 else if ((declarator && declarator->parameter_pack_p)
21721 || template_parameter_pack_p
21722 || (decl_specifiers.type
21723 && PACK_EXPANSION_P (decl_specifiers.type)))
21725 /* Find the name of the parameter pack. */
21726 cp_declarator *id_declarator = declarator;
21727 while (id_declarator && id_declarator->kind != cdk_id)
21728 id_declarator = id_declarator->declarator;
21730 if (id_declarator && id_declarator->kind == cdk_id)
21731 error_at (declarator_token_start->location,
21732 template_parm_p
21733 ? G_("template parameter pack %qD "
21734 "cannot have a default argument")
21735 : G_("parameter pack %qD cannot have "
21736 "a default argument"),
21737 id_declarator->u.id.unqualified_name);
21738 else
21739 error_at (declarator_token_start->location,
21740 template_parm_p
21741 ? G_("template parameter pack cannot have "
21742 "a default argument")
21743 : G_("parameter pack cannot have a "
21744 "default argument"));
21746 default_argument = NULL_TREE;
21749 else
21750 default_argument = NULL_TREE;
21752 /* Generate a location for the parameter, ranging from the start of the
21753 initial token to the end of the final token (using input_location for
21754 the latter, set up by cp_lexer_set_source_position_from_token when
21755 consuming tokens).
21757 If we have a identifier, then use it for the caret location, e.g.
21759 extern int callee (int one, int (*two)(int, int), float three);
21760 ~~~~~~^~~~~~~~~~~~~~
21762 otherwise, reuse the start location for the caret location e.g.:
21764 extern int callee (int one, int (*)(int, int), float three);
21765 ^~~~~~~~~~~~~~~~~
21768 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21769 ? declarator->id_loc
21770 : decl_spec_token_start->location);
21771 location_t param_loc = make_location (caret_loc,
21772 decl_spec_token_start->location,
21773 input_location);
21775 return make_parameter_declarator (&decl_specifiers,
21776 declarator,
21777 default_argument,
21778 param_loc,
21779 template_parameter_pack_p);
21782 /* Parse a default argument and return it.
21784 TEMPLATE_PARM_P is true if this is a default argument for a
21785 non-type template parameter. */
21786 static tree
21787 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21789 tree default_argument = NULL_TREE;
21790 bool saved_greater_than_is_operator_p;
21791 bool saved_local_variables_forbidden_p;
21792 bool non_constant_p, is_direct_init;
21794 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21795 set correctly. */
21796 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21797 parser->greater_than_is_operator_p = !template_parm_p;
21798 /* Local variable names (and the `this' keyword) may not
21799 appear in a default argument. */
21800 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21801 parser->local_variables_forbidden_p = true;
21802 /* Parse the assignment-expression. */
21803 if (template_parm_p)
21804 push_deferring_access_checks (dk_no_deferred);
21805 tree saved_class_ptr = NULL_TREE;
21806 tree saved_class_ref = NULL_TREE;
21807 /* The "this" pointer is not valid in a default argument. */
21808 if (cfun)
21810 saved_class_ptr = current_class_ptr;
21811 cp_function_chain->x_current_class_ptr = NULL_TREE;
21812 saved_class_ref = current_class_ref;
21813 cp_function_chain->x_current_class_ref = NULL_TREE;
21815 default_argument
21816 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21817 /* Restore the "this" pointer. */
21818 if (cfun)
21820 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21821 cp_function_chain->x_current_class_ref = saved_class_ref;
21823 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21824 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21825 if (template_parm_p)
21826 pop_deferring_access_checks ();
21827 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21828 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21830 return default_argument;
21833 /* Parse a function-body.
21835 function-body:
21836 compound_statement */
21838 static void
21839 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21841 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21842 ? BCS_TRY_BLOCK : BCS_NORMAL),
21843 true);
21846 /* Parse a ctor-initializer-opt followed by a function-body. Return
21847 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21848 is true we are parsing a function-try-block. */
21850 static void
21851 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21852 bool in_function_try_block)
21854 tree body, list;
21855 const bool check_body_p =
21856 DECL_CONSTRUCTOR_P (current_function_decl)
21857 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21858 tree last = NULL;
21860 /* Begin the function body. */
21861 body = begin_function_body ();
21862 /* Parse the optional ctor-initializer. */
21863 cp_parser_ctor_initializer_opt (parser);
21865 /* If we're parsing a constexpr constructor definition, we need
21866 to check that the constructor body is indeed empty. However,
21867 before we get to cp_parser_function_body lot of junk has been
21868 generated, so we can't just check that we have an empty block.
21869 Rather we take a snapshot of the outermost block, and check whether
21870 cp_parser_function_body changed its state. */
21871 if (check_body_p)
21873 list = cur_stmt_list;
21874 if (STATEMENT_LIST_TAIL (list))
21875 last = STATEMENT_LIST_TAIL (list)->stmt;
21877 /* Parse the function-body. */
21878 cp_parser_function_body (parser, in_function_try_block);
21879 if (check_body_p)
21880 check_constexpr_ctor_body (last, list, /*complain=*/true);
21881 /* Finish the function body. */
21882 finish_function_body (body);
21885 /* Parse an initializer.
21887 initializer:
21888 = initializer-clause
21889 ( expression-list )
21891 Returns an expression representing the initializer. If no
21892 initializer is present, NULL_TREE is returned.
21894 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21895 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21896 set to TRUE if there is no initializer present. If there is an
21897 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21898 is set to true; otherwise it is set to false. */
21900 static tree
21901 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21902 bool* non_constant_p)
21904 cp_token *token;
21905 tree init;
21907 /* Peek at the next token. */
21908 token = cp_lexer_peek_token (parser->lexer);
21910 /* Let our caller know whether or not this initializer was
21911 parenthesized. */
21912 *is_direct_init = (token->type != CPP_EQ);
21913 /* Assume that the initializer is constant. */
21914 *non_constant_p = false;
21916 if (token->type == CPP_EQ)
21918 /* Consume the `='. */
21919 cp_lexer_consume_token (parser->lexer);
21920 /* Parse the initializer-clause. */
21921 init = cp_parser_initializer_clause (parser, non_constant_p);
21923 else if (token->type == CPP_OPEN_PAREN)
21925 vec<tree, va_gc> *vec;
21926 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21927 /*cast_p=*/false,
21928 /*allow_expansion_p=*/true,
21929 non_constant_p);
21930 if (vec == NULL)
21931 return error_mark_node;
21932 init = build_tree_list_vec (vec);
21933 release_tree_vector (vec);
21935 else if (token->type == CPP_OPEN_BRACE)
21937 cp_lexer_set_source_position (parser->lexer);
21938 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21939 init = cp_parser_braced_list (parser, non_constant_p);
21940 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21942 else
21944 /* Anything else is an error. */
21945 cp_parser_error (parser, "expected initializer");
21946 init = error_mark_node;
21949 if (check_for_bare_parameter_packs (init))
21950 init = error_mark_node;
21952 return init;
21955 /* Parse an initializer-clause.
21957 initializer-clause:
21958 assignment-expression
21959 braced-init-list
21961 Returns an expression representing the initializer.
21963 If the `assignment-expression' production is used the value
21964 returned is simply a representation for the expression.
21966 Otherwise, calls cp_parser_braced_list. */
21968 static cp_expr
21969 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21971 cp_expr initializer;
21973 /* Assume the expression is constant. */
21974 *non_constant_p = false;
21976 /* If it is not a `{', then we are looking at an
21977 assignment-expression. */
21978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21980 initializer
21981 = cp_parser_constant_expression (parser,
21982 /*allow_non_constant_p=*/true,
21983 non_constant_p);
21985 else
21986 initializer = cp_parser_braced_list (parser, non_constant_p);
21988 return initializer;
21991 /* Parse a brace-enclosed initializer list.
21993 braced-init-list:
21994 { initializer-list , [opt] }
21995 { designated-initializer-list , [opt] }
21998 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21999 the elements of the initializer-list (or NULL, if the last
22000 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22001 NULL_TREE. There is no way to detect whether or not the optional
22002 trailing `,' was provided. NON_CONSTANT_P is as for
22003 cp_parser_initializer. */
22005 static cp_expr
22006 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22008 tree initializer;
22009 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22011 /* Consume the `{' token. */
22012 matching_braces braces;
22013 braces.consume_open (parser);
22014 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22015 initializer = make_node (CONSTRUCTOR);
22016 /* If it's not a `}', then there is a non-trivial initializer. */
22017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22019 /* Parse the initializer list. */
22020 CONSTRUCTOR_ELTS (initializer)
22021 = cp_parser_initializer_list (parser, non_constant_p);
22022 /* A trailing `,' token is allowed. */
22023 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22024 cp_lexer_consume_token (parser->lexer);
22026 else
22027 *non_constant_p = false;
22028 /* Now, there should be a trailing `}'. */
22029 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22030 braces.require_close (parser);
22031 TREE_TYPE (initializer) = init_list_type_node;
22033 cp_expr result (initializer);
22034 /* Build a location of the form:
22035 { ... }
22036 ^~~~~~~
22037 with caret==start at the open brace, finish at the close brace. */
22038 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22039 result.set_location (combined_loc);
22040 return result;
22043 /* Consume tokens up to, and including, the next non-nested closing `]'.
22044 Returns true iff we found a closing `]'. */
22046 static bool
22047 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22049 unsigned square_depth = 0;
22051 while (true)
22053 cp_token * token = cp_lexer_peek_token (parser->lexer);
22055 switch (token->type)
22057 case CPP_EOF:
22058 case CPP_PRAGMA_EOL:
22059 /* If we've run out of tokens, then there is no closing `]'. */
22060 return false;
22062 case CPP_OPEN_SQUARE:
22063 ++square_depth;
22064 break;
22066 case CPP_CLOSE_SQUARE:
22067 if (!square_depth--)
22069 cp_lexer_consume_token (parser->lexer);
22070 return true;
22072 break;
22074 default:
22075 break;
22078 /* Consume the token. */
22079 cp_lexer_consume_token (parser->lexer);
22083 /* Return true if we are looking at an array-designator, false otherwise. */
22085 static bool
22086 cp_parser_array_designator_p (cp_parser *parser)
22088 /* Consume the `['. */
22089 cp_lexer_consume_token (parser->lexer);
22091 cp_lexer_save_tokens (parser->lexer);
22093 /* Skip tokens until the next token is a closing square bracket.
22094 If we find the closing `]', and the next token is a `=', then
22095 we are looking at an array designator. */
22096 bool array_designator_p
22097 = (cp_parser_skip_to_closing_square_bracket (parser)
22098 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22100 /* Roll back the tokens we skipped. */
22101 cp_lexer_rollback_tokens (parser->lexer);
22103 return array_designator_p;
22106 /* Parse an initializer-list.
22108 initializer-list:
22109 initializer-clause ... [opt]
22110 initializer-list , initializer-clause ... [opt]
22112 C++2A Extension:
22114 designated-initializer-list:
22115 designated-initializer-clause
22116 designated-initializer-list , designated-initializer-clause
22118 designated-initializer-clause:
22119 designator brace-or-equal-initializer
22121 designator:
22122 . identifier
22124 GNU Extension:
22126 initializer-list:
22127 designation initializer-clause ...[opt]
22128 initializer-list , designation initializer-clause ...[opt]
22130 designation:
22131 . identifier =
22132 identifier :
22133 [ constant-expression ] =
22135 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22136 for the initializer. If the INDEX of the elt is non-NULL, it is the
22137 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22138 as for cp_parser_initializer. */
22140 static vec<constructor_elt, va_gc> *
22141 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22143 vec<constructor_elt, va_gc> *v = NULL;
22144 bool first_p = true;
22145 tree first_designator = NULL_TREE;
22147 /* Assume all of the expressions are constant. */
22148 *non_constant_p = false;
22150 /* Parse the rest of the list. */
22151 while (true)
22153 cp_token *token;
22154 tree designator;
22155 tree initializer;
22156 bool clause_non_constant_p;
22157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22159 /* Handle the C++2A syntax, '. id ='. */
22160 if ((cxx_dialect >= cxx2a
22161 || cp_parser_allow_gnu_extensions_p (parser))
22162 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22163 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22164 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22165 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22166 == CPP_OPEN_BRACE)))
22168 if (cxx_dialect < cxx2a)
22169 pedwarn (loc, OPT_Wpedantic,
22170 "C++ designated initializers only available with "
22171 "-std=c++2a or -std=gnu++2a");
22172 /* Consume the `.'. */
22173 cp_lexer_consume_token (parser->lexer);
22174 /* Consume the identifier. */
22175 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22176 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22177 /* Consume the `='. */
22178 cp_lexer_consume_token (parser->lexer);
22180 /* Also, if the next token is an identifier and the following one is a
22181 colon, we are looking at the GNU designated-initializer
22182 syntax. */
22183 else if (cp_parser_allow_gnu_extensions_p (parser)
22184 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22185 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22186 == CPP_COLON))
22188 /* Warn the user that they are using an extension. */
22189 pedwarn (loc, OPT_Wpedantic,
22190 "ISO C++ does not allow GNU designated initializers");
22191 /* Consume the identifier. */
22192 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22193 /* Consume the `:'. */
22194 cp_lexer_consume_token (parser->lexer);
22196 /* Also handle C99 array designators, '[ const ] ='. */
22197 else if (cp_parser_allow_gnu_extensions_p (parser)
22198 && !c_dialect_objc ()
22199 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22201 /* In C++11, [ could start a lambda-introducer. */
22202 bool non_const = false;
22204 cp_parser_parse_tentatively (parser);
22206 if (!cp_parser_array_designator_p (parser))
22208 cp_parser_simulate_error (parser);
22209 designator = NULL_TREE;
22211 else
22213 designator = cp_parser_constant_expression (parser, true,
22214 &non_const);
22215 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22216 cp_parser_require (parser, CPP_EQ, RT_EQ);
22219 if (!cp_parser_parse_definitely (parser))
22220 designator = NULL_TREE;
22221 else if (non_const)
22222 require_potential_rvalue_constant_expression (designator);
22223 if (designator)
22224 /* Warn the user that they are using an extension. */
22225 pedwarn (loc, OPT_Wpedantic,
22226 "ISO C++ does not allow C99 designated initializers");
22228 else
22229 designator = NULL_TREE;
22231 if (first_p)
22233 first_designator = designator;
22234 first_p = false;
22236 else if (cxx_dialect >= cxx2a
22237 && first_designator != error_mark_node
22238 && (!first_designator != !designator))
22240 error_at (loc, "either all initializer clauses should be designated "
22241 "or none of them should be");
22242 first_designator = error_mark_node;
22244 else if (cxx_dialect < cxx2a && !first_designator)
22245 first_designator = designator;
22247 /* Parse the initializer. */
22248 initializer = cp_parser_initializer_clause (parser,
22249 &clause_non_constant_p);
22250 /* If any clause is non-constant, so is the entire initializer. */
22251 if (clause_non_constant_p)
22252 *non_constant_p = true;
22254 /* If we have an ellipsis, this is an initializer pack
22255 expansion. */
22256 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22258 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22260 /* Consume the `...'. */
22261 cp_lexer_consume_token (parser->lexer);
22263 if (designator && cxx_dialect >= cxx2a)
22264 error_at (loc,
22265 "%<...%> not allowed in designated initializer list");
22267 /* Turn the initializer into an initializer expansion. */
22268 initializer = make_pack_expansion (initializer);
22271 /* Add it to the vector. */
22272 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22274 /* If the next token is not a comma, we have reached the end of
22275 the list. */
22276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22277 break;
22279 /* Peek at the next token. */
22280 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22281 /* If the next token is a `}', then we're still done. An
22282 initializer-clause can have a trailing `,' after the
22283 initializer-list and before the closing `}'. */
22284 if (token->type == CPP_CLOSE_BRACE)
22285 break;
22287 /* Consume the `,' token. */
22288 cp_lexer_consume_token (parser->lexer);
22291 /* The same identifier shall not appear in multiple designators
22292 of a designated-initializer-list. */
22293 if (first_designator)
22295 unsigned int i;
22296 tree designator, val;
22297 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22298 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22300 if (IDENTIFIER_MARKED (designator))
22302 error_at (EXPR_LOC_OR_LOC (val, input_location),
22303 "%<.%s%> designator used multiple times in "
22304 "the same initializer list",
22305 IDENTIFIER_POINTER (designator));
22306 (*v)[i].index = NULL_TREE;
22308 else
22309 IDENTIFIER_MARKED (designator) = 1;
22311 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22312 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22313 IDENTIFIER_MARKED (designator) = 0;
22316 return v;
22319 /* Classes [gram.class] */
22321 /* Parse a class-name.
22323 class-name:
22324 identifier
22325 template-id
22327 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22328 to indicate that names looked up in dependent types should be
22329 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22330 keyword has been used to indicate that the name that appears next
22331 is a template. TAG_TYPE indicates the explicit tag given before
22332 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22333 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22334 is the class being defined in a class-head. If ENUM_OK is TRUE,
22335 enum-names are also accepted.
22337 Returns the TYPE_DECL representing the class. */
22339 static tree
22340 cp_parser_class_name (cp_parser *parser,
22341 bool typename_keyword_p,
22342 bool template_keyword_p,
22343 enum tag_types tag_type,
22344 bool check_dependency_p,
22345 bool class_head_p,
22346 bool is_declaration,
22347 bool enum_ok)
22349 tree decl;
22350 tree scope;
22351 bool typename_p;
22352 cp_token *token;
22353 tree identifier = NULL_TREE;
22355 /* All class-names start with an identifier. */
22356 token = cp_lexer_peek_token (parser->lexer);
22357 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22359 cp_parser_error (parser, "expected class-name");
22360 return error_mark_node;
22363 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22364 to a template-id, so we save it here. */
22365 scope = parser->scope;
22366 if (scope == error_mark_node)
22367 return error_mark_node;
22369 /* Any name names a type if we're following the `typename' keyword
22370 in a qualified name where the enclosing scope is type-dependent. */
22371 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22372 && dependent_type_p (scope));
22373 /* Handle the common case (an identifier, but not a template-id)
22374 efficiently. */
22375 if (token->type == CPP_NAME
22376 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22378 cp_token *identifier_token;
22379 bool ambiguous_p;
22381 /* Look for the identifier. */
22382 identifier_token = cp_lexer_peek_token (parser->lexer);
22383 ambiguous_p = identifier_token->error_reported;
22384 identifier = cp_parser_identifier (parser);
22385 /* If the next token isn't an identifier, we are certainly not
22386 looking at a class-name. */
22387 if (identifier == error_mark_node)
22388 decl = error_mark_node;
22389 /* If we know this is a type-name, there's no need to look it
22390 up. */
22391 else if (typename_p)
22392 decl = identifier;
22393 else
22395 tree ambiguous_decls;
22396 /* If we already know that this lookup is ambiguous, then
22397 we've already issued an error message; there's no reason
22398 to check again. */
22399 if (ambiguous_p)
22401 cp_parser_simulate_error (parser);
22402 return error_mark_node;
22404 /* If the next token is a `::', then the name must be a type
22405 name.
22407 [basic.lookup.qual]
22409 During the lookup for a name preceding the :: scope
22410 resolution operator, object, function, and enumerator
22411 names are ignored. */
22412 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22413 tag_type = scope_type;
22414 /* Look up the name. */
22415 decl = cp_parser_lookup_name (parser, identifier,
22416 tag_type,
22417 /*is_template=*/false,
22418 /*is_namespace=*/false,
22419 check_dependency_p,
22420 &ambiguous_decls,
22421 identifier_token->location);
22422 if (ambiguous_decls)
22424 if (cp_parser_parsing_tentatively (parser))
22425 cp_parser_simulate_error (parser);
22426 return error_mark_node;
22430 else
22432 /* Try a template-id. */
22433 decl = cp_parser_template_id (parser, template_keyword_p,
22434 check_dependency_p,
22435 tag_type,
22436 is_declaration);
22437 if (decl == error_mark_node)
22438 return error_mark_node;
22441 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22443 /* If this is a typename, create a TYPENAME_TYPE. */
22444 if (typename_p && decl != error_mark_node)
22446 decl = make_typename_type (scope, decl, typename_type,
22447 /*complain=*/tf_error);
22448 if (decl != error_mark_node)
22449 decl = TYPE_NAME (decl);
22452 decl = strip_using_decl (decl);
22454 /* Check to see that it is really the name of a class. */
22455 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22456 && identifier_p (TREE_OPERAND (decl, 0))
22457 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22458 /* Situations like this:
22460 template <typename T> struct A {
22461 typename T::template X<int>::I i;
22464 are problematic. Is `T::template X<int>' a class-name? The
22465 standard does not seem to be definitive, but there is no other
22466 valid interpretation of the following `::'. Therefore, those
22467 names are considered class-names. */
22469 decl = make_typename_type (scope, decl, tag_type, tf_error);
22470 if (decl != error_mark_node)
22471 decl = TYPE_NAME (decl);
22473 else if (TREE_CODE (decl) != TYPE_DECL
22474 || TREE_TYPE (decl) == error_mark_node
22475 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22476 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22477 /* In Objective-C 2.0, a classname followed by '.' starts a
22478 dot-syntax expression, and it's not a type-name. */
22479 || (c_dialect_objc ()
22480 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22481 && objc_is_class_name (decl)))
22482 decl = error_mark_node;
22484 if (decl == error_mark_node)
22485 cp_parser_error (parser, "expected class-name");
22486 else if (identifier && !parser->scope)
22487 maybe_note_name_used_in_class (identifier, decl);
22489 return decl;
22492 /* Parse a class-specifier.
22494 class-specifier:
22495 class-head { member-specification [opt] }
22497 Returns the TREE_TYPE representing the class. */
22499 static tree
22500 cp_parser_class_specifier_1 (cp_parser* parser)
22502 tree type;
22503 tree attributes = NULL_TREE;
22504 bool nested_name_specifier_p;
22505 unsigned saved_num_template_parameter_lists;
22506 bool saved_in_function_body;
22507 unsigned char in_statement;
22508 bool in_switch_statement_p;
22509 bool saved_in_unbraced_linkage_specification_p;
22510 tree old_scope = NULL_TREE;
22511 tree scope = NULL_TREE;
22512 cp_token *closing_brace;
22514 push_deferring_access_checks (dk_no_deferred);
22516 /* Parse the class-head. */
22517 type = cp_parser_class_head (parser,
22518 &nested_name_specifier_p);
22519 /* If the class-head was a semantic disaster, skip the entire body
22520 of the class. */
22521 if (!type)
22523 cp_parser_skip_to_end_of_block_or_statement (parser);
22524 pop_deferring_access_checks ();
22525 return error_mark_node;
22528 /* Look for the `{'. */
22529 matching_braces braces;
22530 if (!braces.require_open (parser))
22532 pop_deferring_access_checks ();
22533 return error_mark_node;
22536 cp_ensure_no_omp_declare_simd (parser);
22537 cp_ensure_no_oacc_routine (parser);
22539 /* Issue an error message if type-definitions are forbidden here. */
22540 cp_parser_check_type_definition (parser);
22541 /* Remember that we are defining one more class. */
22542 ++parser->num_classes_being_defined;
22543 /* Inside the class, surrounding template-parameter-lists do not
22544 apply. */
22545 saved_num_template_parameter_lists
22546 = parser->num_template_parameter_lists;
22547 parser->num_template_parameter_lists = 0;
22548 /* We are not in a function body. */
22549 saved_in_function_body = parser->in_function_body;
22550 parser->in_function_body = false;
22551 /* Or in a loop. */
22552 in_statement = parser->in_statement;
22553 parser->in_statement = 0;
22554 /* Or in a switch. */
22555 in_switch_statement_p = parser->in_switch_statement_p;
22556 parser->in_switch_statement_p = false;
22557 /* We are not immediately inside an extern "lang" block. */
22558 saved_in_unbraced_linkage_specification_p
22559 = parser->in_unbraced_linkage_specification_p;
22560 parser->in_unbraced_linkage_specification_p = false;
22562 // Associate constraints with the type.
22563 if (flag_concepts)
22564 type = associate_classtype_constraints (type);
22566 /* Start the class. */
22567 if (nested_name_specifier_p)
22569 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22570 old_scope = push_inner_scope (scope);
22572 type = begin_class_definition (type);
22574 if (type == error_mark_node)
22575 /* If the type is erroneous, skip the entire body of the class. */
22576 cp_parser_skip_to_closing_brace (parser);
22577 else
22578 /* Parse the member-specification. */
22579 cp_parser_member_specification_opt (parser);
22581 /* Look for the trailing `}'. */
22582 closing_brace = braces.require_close (parser);
22583 /* Look for trailing attributes to apply to this class. */
22584 if (cp_parser_allow_gnu_extensions_p (parser))
22585 attributes = cp_parser_gnu_attributes_opt (parser);
22586 if (type != error_mark_node)
22587 type = finish_struct (type, attributes);
22588 if (nested_name_specifier_p)
22589 pop_inner_scope (old_scope, scope);
22591 /* We've finished a type definition. Check for the common syntax
22592 error of forgetting a semicolon after the definition. We need to
22593 be careful, as we can't just check for not-a-semicolon and be done
22594 with it; the user might have typed:
22596 class X { } c = ...;
22597 class X { } *p = ...;
22599 and so forth. Instead, enumerate all the possible tokens that
22600 might follow this production; if we don't see one of them, then
22601 complain and silently insert the semicolon. */
22603 cp_token *token = cp_lexer_peek_token (parser->lexer);
22604 bool want_semicolon = true;
22606 if (cp_next_tokens_can_be_std_attribute_p (parser))
22607 /* Don't try to parse c++11 attributes here. As per the
22608 grammar, that should be a task for
22609 cp_parser_decl_specifier_seq. */
22610 want_semicolon = false;
22612 switch (token->type)
22614 case CPP_NAME:
22615 case CPP_SEMICOLON:
22616 case CPP_MULT:
22617 case CPP_AND:
22618 case CPP_OPEN_PAREN:
22619 case CPP_CLOSE_PAREN:
22620 case CPP_COMMA:
22621 want_semicolon = false;
22622 break;
22624 /* While it's legal for type qualifiers and storage class
22625 specifiers to follow type definitions in the grammar, only
22626 compiler testsuites contain code like that. Assume that if
22627 we see such code, then what we're really seeing is a case
22628 like:
22630 class X { }
22631 const <type> var = ...;
22635 class Y { }
22636 static <type> func (...) ...
22638 i.e. the qualifier or specifier applies to the next
22639 declaration. To do so, however, we need to look ahead one
22640 more token to see if *that* token is a type specifier.
22642 This code could be improved to handle:
22644 class Z { }
22645 static const <type> var = ...; */
22646 case CPP_KEYWORD:
22647 if (keyword_is_decl_specifier (token->keyword))
22649 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22651 /* Handling user-defined types here would be nice, but very
22652 tricky. */
22653 want_semicolon
22654 = (lookahead->type == CPP_KEYWORD
22655 && keyword_begins_type_specifier (lookahead->keyword));
22657 break;
22658 default:
22659 break;
22662 /* If we don't have a type, then something is very wrong and we
22663 shouldn't try to do anything clever. Likewise for not seeing the
22664 closing brace. */
22665 if (closing_brace && TYPE_P (type) && want_semicolon)
22667 /* Locate the closing brace. */
22668 cp_token_position prev
22669 = cp_lexer_previous_token_position (parser->lexer);
22670 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22671 location_t loc = prev_token->location;
22673 /* We want to suggest insertion of a ';' immediately *after* the
22674 closing brace, so, if we can, offset the location by 1 column. */
22675 location_t next_loc = loc;
22676 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22677 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22679 rich_location richloc (line_table, next_loc);
22681 /* If we successfully offset the location, suggest the fix-it. */
22682 if (next_loc != loc)
22683 richloc.add_fixit_insert_before (next_loc, ";");
22685 if (CLASSTYPE_DECLARED_CLASS (type))
22686 error_at (&richloc,
22687 "expected %<;%> after class definition");
22688 else if (TREE_CODE (type) == RECORD_TYPE)
22689 error_at (&richloc,
22690 "expected %<;%> after struct definition");
22691 else if (TREE_CODE (type) == UNION_TYPE)
22692 error_at (&richloc,
22693 "expected %<;%> after union definition");
22694 else
22695 gcc_unreachable ();
22697 /* Unget one token and smash it to look as though we encountered
22698 a semicolon in the input stream. */
22699 cp_lexer_set_token_position (parser->lexer, prev);
22700 token = cp_lexer_peek_token (parser->lexer);
22701 token->type = CPP_SEMICOLON;
22702 token->keyword = RID_MAX;
22706 /* If this class is not itself within the scope of another class,
22707 then we need to parse the bodies of all of the queued function
22708 definitions. Note that the queued functions defined in a class
22709 are not always processed immediately following the
22710 class-specifier for that class. Consider:
22712 struct A {
22713 struct B { void f() { sizeof (A); } };
22716 If `f' were processed before the processing of `A' were
22717 completed, there would be no way to compute the size of `A'.
22718 Note that the nesting we are interested in here is lexical --
22719 not the semantic nesting given by TYPE_CONTEXT. In particular,
22720 for:
22722 struct A { struct B; };
22723 struct A::B { void f() { } };
22725 there is no need to delay the parsing of `A::B::f'. */
22726 if (--parser->num_classes_being_defined == 0)
22728 tree decl;
22729 tree class_type = NULL_TREE;
22730 tree pushed_scope = NULL_TREE;
22731 unsigned ix;
22732 cp_default_arg_entry *e;
22733 tree save_ccp, save_ccr;
22735 /* In a first pass, parse default arguments to the functions.
22736 Then, in a second pass, parse the bodies of the functions.
22737 This two-phased approach handles cases like:
22739 struct S {
22740 void f() { g(); }
22741 void g(int i = 3);
22745 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22747 decl = e->decl;
22748 /* If there are default arguments that have not yet been processed,
22749 take care of them now. */
22750 if (class_type != e->class_type)
22752 if (pushed_scope)
22753 pop_scope (pushed_scope);
22754 class_type = e->class_type;
22755 pushed_scope = push_scope (class_type);
22757 /* Make sure that any template parameters are in scope. */
22758 maybe_begin_member_template_processing (decl);
22759 /* Parse the default argument expressions. */
22760 cp_parser_late_parsing_default_args (parser, decl);
22761 /* Remove any template parameters from the symbol table. */
22762 maybe_end_member_template_processing ();
22764 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22765 /* Now parse any NSDMIs. */
22766 save_ccp = current_class_ptr;
22767 save_ccr = current_class_ref;
22768 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22770 if (class_type != DECL_CONTEXT (decl))
22772 if (pushed_scope)
22773 pop_scope (pushed_scope);
22774 class_type = DECL_CONTEXT (decl);
22775 pushed_scope = push_scope (class_type);
22777 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22778 cp_parser_late_parsing_nsdmi (parser, decl);
22780 vec_safe_truncate (unparsed_nsdmis, 0);
22781 current_class_ptr = save_ccp;
22782 current_class_ref = save_ccr;
22783 if (pushed_scope)
22784 pop_scope (pushed_scope);
22786 /* Now do some post-NSDMI bookkeeping. */
22787 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22788 after_nsdmi_defaulted_late_checks (class_type);
22789 vec_safe_truncate (unparsed_classes, 0);
22790 after_nsdmi_defaulted_late_checks (type);
22792 /* Now parse the body of the functions. */
22793 if (flag_openmp)
22795 /* OpenMP UDRs need to be parsed before all other functions. */
22796 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22797 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22798 cp_parser_late_parsing_for_member (parser, decl);
22799 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22800 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22801 cp_parser_late_parsing_for_member (parser, decl);
22803 else
22804 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22805 cp_parser_late_parsing_for_member (parser, decl);
22806 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22808 else
22809 vec_safe_push (unparsed_classes, type);
22811 /* Put back any saved access checks. */
22812 pop_deferring_access_checks ();
22814 /* Restore saved state. */
22815 parser->in_switch_statement_p = in_switch_statement_p;
22816 parser->in_statement = in_statement;
22817 parser->in_function_body = saved_in_function_body;
22818 parser->num_template_parameter_lists
22819 = saved_num_template_parameter_lists;
22820 parser->in_unbraced_linkage_specification_p
22821 = saved_in_unbraced_linkage_specification_p;
22823 return type;
22826 static tree
22827 cp_parser_class_specifier (cp_parser* parser)
22829 tree ret;
22830 timevar_push (TV_PARSE_STRUCT);
22831 ret = cp_parser_class_specifier_1 (parser);
22832 timevar_pop (TV_PARSE_STRUCT);
22833 return ret;
22836 /* Parse a class-head.
22838 class-head:
22839 class-key identifier [opt] base-clause [opt]
22840 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22841 class-key nested-name-specifier [opt] template-id
22842 base-clause [opt]
22844 class-virt-specifier:
22845 final
22847 GNU Extensions:
22848 class-key attributes identifier [opt] base-clause [opt]
22849 class-key attributes nested-name-specifier identifier base-clause [opt]
22850 class-key attributes nested-name-specifier [opt] template-id
22851 base-clause [opt]
22853 Upon return BASES is initialized to the list of base classes (or
22854 NULL, if there are none) in the same form returned by
22855 cp_parser_base_clause.
22857 Returns the TYPE of the indicated class. Sets
22858 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22859 involving a nested-name-specifier was used, and FALSE otherwise.
22861 Returns error_mark_node if this is not a class-head.
22863 Returns NULL_TREE if the class-head is syntactically valid, but
22864 semantically invalid in a way that means we should skip the entire
22865 body of the class. */
22867 static tree
22868 cp_parser_class_head (cp_parser* parser,
22869 bool* nested_name_specifier_p)
22871 tree nested_name_specifier;
22872 enum tag_types class_key;
22873 tree id = NULL_TREE;
22874 tree type = NULL_TREE;
22875 tree attributes;
22876 tree bases;
22877 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22878 bool template_id_p = false;
22879 bool qualified_p = false;
22880 bool invalid_nested_name_p = false;
22881 bool invalid_explicit_specialization_p = false;
22882 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22883 tree pushed_scope = NULL_TREE;
22884 unsigned num_templates;
22885 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22886 /* Assume no nested-name-specifier will be present. */
22887 *nested_name_specifier_p = false;
22888 /* Assume no template parameter lists will be used in defining the
22889 type. */
22890 num_templates = 0;
22891 parser->colon_corrects_to_scope_p = false;
22893 /* Look for the class-key. */
22894 class_key = cp_parser_class_key (parser);
22895 if (class_key == none_type)
22896 return error_mark_node;
22898 location_t class_head_start_location = input_location;
22900 /* Parse the attributes. */
22901 attributes = cp_parser_attributes_opt (parser);
22903 /* If the next token is `::', that is invalid -- but sometimes
22904 people do try to write:
22906 struct ::S {};
22908 Handle this gracefully by accepting the extra qualifier, and then
22909 issuing an error about it later if this really is a
22910 class-head. If it turns out just to be an elaborated type
22911 specifier, remain silent. */
22912 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22913 qualified_p = true;
22915 push_deferring_access_checks (dk_no_check);
22917 /* Determine the name of the class. Begin by looking for an
22918 optional nested-name-specifier. */
22919 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22920 nested_name_specifier
22921 = cp_parser_nested_name_specifier_opt (parser,
22922 /*typename_keyword_p=*/false,
22923 /*check_dependency_p=*/false,
22924 /*type_p=*/true,
22925 /*is_declaration=*/false);
22926 /* If there was a nested-name-specifier, then there *must* be an
22927 identifier. */
22929 cp_token *bad_template_keyword = NULL;
22931 if (nested_name_specifier)
22933 type_start_token = cp_lexer_peek_token (parser->lexer);
22934 /* Although the grammar says `identifier', it really means
22935 `class-name' or `template-name'. You are only allowed to
22936 define a class that has already been declared with this
22937 syntax.
22939 The proposed resolution for Core Issue 180 says that wherever
22940 you see `class T::X' you should treat `X' as a type-name.
22942 It is OK to define an inaccessible class; for example:
22944 class A { class B; };
22945 class A::B {};
22947 We do not know if we will see a class-name, or a
22948 template-name. We look for a class-name first, in case the
22949 class-name is a template-id; if we looked for the
22950 template-name first we would stop after the template-name. */
22951 cp_parser_parse_tentatively (parser);
22952 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22953 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22954 type = cp_parser_class_name (parser,
22955 /*typename_keyword_p=*/false,
22956 /*template_keyword_p=*/false,
22957 class_type,
22958 /*check_dependency_p=*/false,
22959 /*class_head_p=*/true,
22960 /*is_declaration=*/false);
22961 /* If that didn't work, ignore the nested-name-specifier. */
22962 if (!cp_parser_parse_definitely (parser))
22964 invalid_nested_name_p = true;
22965 type_start_token = cp_lexer_peek_token (parser->lexer);
22966 id = cp_parser_identifier (parser);
22967 if (id == error_mark_node)
22968 id = NULL_TREE;
22970 /* If we could not find a corresponding TYPE, treat this
22971 declaration like an unqualified declaration. */
22972 if (type == error_mark_node)
22973 nested_name_specifier = NULL_TREE;
22974 /* Otherwise, count the number of templates used in TYPE and its
22975 containing scopes. */
22976 else
22978 tree scope;
22980 for (scope = TREE_TYPE (type);
22981 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22982 scope = get_containing_scope (scope))
22983 if (TYPE_P (scope)
22984 && CLASS_TYPE_P (scope)
22985 && CLASSTYPE_TEMPLATE_INFO (scope)
22986 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22987 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22988 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22989 ++num_templates;
22992 /* Otherwise, the identifier is optional. */
22993 else
22995 /* We don't know whether what comes next is a template-id,
22996 an identifier, or nothing at all. */
22997 cp_parser_parse_tentatively (parser);
22998 /* Check for a template-id. */
22999 type_start_token = cp_lexer_peek_token (parser->lexer);
23000 id = cp_parser_template_id (parser,
23001 /*template_keyword_p=*/false,
23002 /*check_dependency_p=*/true,
23003 class_key,
23004 /*is_declaration=*/true);
23005 /* If that didn't work, it could still be an identifier. */
23006 if (!cp_parser_parse_definitely (parser))
23008 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23010 type_start_token = cp_lexer_peek_token (parser->lexer);
23011 id = cp_parser_identifier (parser);
23013 else
23014 id = NULL_TREE;
23016 else
23018 template_id_p = true;
23019 ++num_templates;
23023 pop_deferring_access_checks ();
23025 if (id)
23027 cp_parser_check_for_invalid_template_id (parser, id,
23028 class_key,
23029 type_start_token->location);
23031 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23033 /* If it's not a `:' or a `{' then we can't really be looking at a
23034 class-head, since a class-head only appears as part of a
23035 class-specifier. We have to detect this situation before calling
23036 xref_tag, since that has irreversible side-effects. */
23037 if (!cp_parser_next_token_starts_class_definition_p (parser))
23039 cp_parser_error (parser, "expected %<{%> or %<:%>");
23040 type = error_mark_node;
23041 goto out;
23044 /* At this point, we're going ahead with the class-specifier, even
23045 if some other problem occurs. */
23046 cp_parser_commit_to_tentative_parse (parser);
23047 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23049 cp_parser_error (parser,
23050 "cannot specify %<override%> for a class");
23051 type = error_mark_node;
23052 goto out;
23054 /* Issue the error about the overly-qualified name now. */
23055 if (qualified_p)
23057 cp_parser_error (parser,
23058 "global qualification of class name is invalid");
23059 type = error_mark_node;
23060 goto out;
23062 else if (invalid_nested_name_p)
23064 cp_parser_error (parser,
23065 "qualified name does not name a class");
23066 type = error_mark_node;
23067 goto out;
23069 else if (nested_name_specifier)
23071 tree scope;
23073 if (bad_template_keyword)
23074 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23075 keyword template shall not appear at the top level. */
23076 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23077 "keyword %<template%> not allowed in class-head-name");
23079 /* Reject typedef-names in class heads. */
23080 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23082 error_at (type_start_token->location,
23083 "invalid class name in declaration of %qD",
23084 type);
23085 type = NULL_TREE;
23086 goto done;
23089 /* Figure out in what scope the declaration is being placed. */
23090 scope = current_scope ();
23091 /* If that scope does not contain the scope in which the
23092 class was originally declared, the program is invalid. */
23093 if (scope && !is_ancestor (scope, nested_name_specifier))
23095 if (at_namespace_scope_p ())
23096 error_at (type_start_token->location,
23097 "declaration of %qD in namespace %qD which does not "
23098 "enclose %qD",
23099 type, scope, nested_name_specifier);
23100 else
23101 error_at (type_start_token->location,
23102 "declaration of %qD in %qD which does not enclose %qD",
23103 type, scope, nested_name_specifier);
23104 type = NULL_TREE;
23105 goto done;
23107 /* [dcl.meaning]
23109 A declarator-id shall not be qualified except for the
23110 definition of a ... nested class outside of its class
23111 ... [or] the definition or explicit instantiation of a
23112 class member of a namespace outside of its namespace. */
23113 if (scope == nested_name_specifier)
23115 permerror (nested_name_specifier_token_start->location,
23116 "extra qualification not allowed");
23117 nested_name_specifier = NULL_TREE;
23118 num_templates = 0;
23121 /* An explicit-specialization must be preceded by "template <>". If
23122 it is not, try to recover gracefully. */
23123 if (at_namespace_scope_p ()
23124 && parser->num_template_parameter_lists == 0
23125 && !processing_template_parmlist
23126 && template_id_p)
23128 /* Build a location of this form:
23129 struct typename <ARGS>
23130 ^~~~~~~~~~~~~~~~~~~~~~
23131 with caret==start at the start token, and
23132 finishing at the end of the type. */
23133 location_t reported_loc
23134 = make_location (class_head_start_location,
23135 class_head_start_location,
23136 get_finish (type_start_token->location));
23137 rich_location richloc (line_table, reported_loc);
23138 richloc.add_fixit_insert_before (class_head_start_location,
23139 "template <> ");
23140 error_at (&richloc,
23141 "an explicit specialization must be preceded by"
23142 " %<template <>%>");
23143 invalid_explicit_specialization_p = true;
23144 /* Take the same action that would have been taken by
23145 cp_parser_explicit_specialization. */
23146 ++parser->num_template_parameter_lists;
23147 begin_specialization ();
23149 /* There must be no "return" statements between this point and the
23150 end of this function; set "type "to the correct return value and
23151 use "goto done;" to return. */
23152 /* Make sure that the right number of template parameters were
23153 present. */
23154 if (!cp_parser_check_template_parameters (parser, num_templates,
23155 type_start_token->location,
23156 /*declarator=*/NULL))
23158 /* If something went wrong, there is no point in even trying to
23159 process the class-definition. */
23160 type = NULL_TREE;
23161 goto done;
23164 /* Look up the type. */
23165 if (template_id_p)
23167 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23168 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23169 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23171 error_at (type_start_token->location,
23172 "function template %qD redeclared as a class template", id);
23173 type = error_mark_node;
23175 else
23177 type = TREE_TYPE (id);
23178 type = maybe_process_partial_specialization (type);
23180 /* Check the scope while we still know whether or not we had a
23181 nested-name-specifier. */
23182 if (type != error_mark_node)
23183 check_unqualified_spec_or_inst (type, type_start_token->location);
23185 if (nested_name_specifier)
23186 pushed_scope = push_scope (nested_name_specifier);
23188 else if (nested_name_specifier)
23190 tree class_type;
23192 /* Given:
23194 template <typename T> struct S { struct T };
23195 template <typename T> struct S<T>::T { };
23197 we will get a TYPENAME_TYPE when processing the definition of
23198 `S::T'. We need to resolve it to the actual type before we
23199 try to define it. */
23200 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23202 class_type = resolve_typename_type (TREE_TYPE (type),
23203 /*only_current_p=*/false);
23204 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23205 type = TYPE_NAME (class_type);
23206 else
23208 cp_parser_error (parser, "could not resolve typename type");
23209 type = error_mark_node;
23213 if (maybe_process_partial_specialization (TREE_TYPE (type))
23214 == error_mark_node)
23216 type = NULL_TREE;
23217 goto done;
23220 class_type = current_class_type;
23221 /* Enter the scope indicated by the nested-name-specifier. */
23222 pushed_scope = push_scope (nested_name_specifier);
23223 /* Get the canonical version of this type. */
23224 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23225 /* Call push_template_decl if it seems like we should be defining a
23226 template either from the template headers or the type we're
23227 defining, so that we diagnose both extra and missing headers. */
23228 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23229 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23230 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23232 type = push_template_decl (type);
23233 if (type == error_mark_node)
23235 type = NULL_TREE;
23236 goto done;
23240 type = TREE_TYPE (type);
23241 *nested_name_specifier_p = true;
23243 else /* The name is not a nested name. */
23245 /* If the class was unnamed, create a dummy name. */
23246 if (!id)
23247 id = make_anon_name ();
23248 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23249 ? ts_within_enclosing_non_class
23250 : ts_current);
23251 type = xref_tag (class_key, id, tag_scope,
23252 parser->num_template_parameter_lists);
23255 /* Indicate whether this class was declared as a `class' or as a
23256 `struct'. */
23257 if (TREE_CODE (type) == RECORD_TYPE)
23258 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23259 cp_parser_check_class_key (class_key, type);
23261 /* If this type was already complete, and we see another definition,
23262 that's an error. */
23263 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23265 error_at (type_start_token->location, "redefinition of %q#T",
23266 type);
23267 inform (location_of (type), "previous definition of %q#T",
23268 type);
23269 type = NULL_TREE;
23270 goto done;
23272 else if (type == error_mark_node)
23273 type = NULL_TREE;
23275 if (type)
23277 /* Apply attributes now, before any use of the class as a template
23278 argument in its base list. */
23279 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23280 fixup_attribute_variants (type);
23283 /* We will have entered the scope containing the class; the names of
23284 base classes should be looked up in that context. For example:
23286 struct A { struct B {}; struct C; };
23287 struct A::C : B {};
23289 is valid. */
23291 /* Get the list of base-classes, if there is one. */
23292 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23294 /* PR59482: enter the class scope so that base-specifiers are looked
23295 up correctly. */
23296 if (type)
23297 pushclass (type);
23298 bases = cp_parser_base_clause (parser);
23299 /* PR59482: get out of the previously pushed class scope so that the
23300 subsequent pops pop the right thing. */
23301 if (type)
23302 popclass ();
23304 else
23305 bases = NULL_TREE;
23307 /* If we're really defining a class, process the base classes.
23308 If they're invalid, fail. */
23309 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23310 xref_basetypes (type, bases);
23312 done:
23313 /* Leave the scope given by the nested-name-specifier. We will
23314 enter the class scope itself while processing the members. */
23315 if (pushed_scope)
23316 pop_scope (pushed_scope);
23318 if (invalid_explicit_specialization_p)
23320 end_specialization ();
23321 --parser->num_template_parameter_lists;
23324 if (type)
23325 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23326 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23327 CLASSTYPE_FINAL (type) = 1;
23328 out:
23329 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23330 return type;
23333 /* Parse a class-key.
23335 class-key:
23336 class
23337 struct
23338 union
23340 Returns the kind of class-key specified, or none_type to indicate
23341 error. */
23343 static enum tag_types
23344 cp_parser_class_key (cp_parser* parser)
23346 cp_token *token;
23347 enum tag_types tag_type;
23349 /* Look for the class-key. */
23350 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23351 if (!token)
23352 return none_type;
23354 /* Check to see if the TOKEN is a class-key. */
23355 tag_type = cp_parser_token_is_class_key (token);
23356 if (!tag_type)
23357 cp_parser_error (parser, "expected class-key");
23358 return tag_type;
23361 /* Parse a type-parameter-key.
23363 type-parameter-key:
23364 class
23365 typename
23368 static void
23369 cp_parser_type_parameter_key (cp_parser* parser)
23371 /* Look for the type-parameter-key. */
23372 enum tag_types tag_type = none_type;
23373 cp_token *token = cp_lexer_peek_token (parser->lexer);
23374 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23376 cp_lexer_consume_token (parser->lexer);
23377 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23378 /* typename is not allowed in a template template parameter
23379 by the standard until C++17. */
23380 pedwarn (token->location, OPT_Wpedantic,
23381 "ISO C++ forbids typename key in template template parameter;"
23382 " use -std=c++17 or -std=gnu++17");
23384 else
23385 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23387 return;
23390 /* Parse an (optional) member-specification.
23392 member-specification:
23393 member-declaration member-specification [opt]
23394 access-specifier : member-specification [opt] */
23396 static void
23397 cp_parser_member_specification_opt (cp_parser* parser)
23399 while (true)
23401 cp_token *token;
23402 enum rid keyword;
23404 /* Peek at the next token. */
23405 token = cp_lexer_peek_token (parser->lexer);
23406 /* If it's a `}', or EOF then we've seen all the members. */
23407 if (token->type == CPP_CLOSE_BRACE
23408 || token->type == CPP_EOF
23409 || token->type == CPP_PRAGMA_EOL)
23410 break;
23412 /* See if this token is a keyword. */
23413 keyword = token->keyword;
23414 switch (keyword)
23416 case RID_PUBLIC:
23417 case RID_PROTECTED:
23418 case RID_PRIVATE:
23419 /* Consume the access-specifier. */
23420 cp_lexer_consume_token (parser->lexer);
23421 /* Remember which access-specifier is active. */
23422 current_access_specifier = token->u.value;
23423 /* Look for the `:'. */
23424 cp_parser_require (parser, CPP_COLON, RT_COLON);
23425 break;
23427 default:
23428 /* Accept #pragmas at class scope. */
23429 if (token->type == CPP_PRAGMA)
23431 cp_parser_pragma (parser, pragma_member, NULL);
23432 break;
23435 /* Otherwise, the next construction must be a
23436 member-declaration. */
23437 cp_parser_member_declaration (parser);
23442 /* Parse a member-declaration.
23444 member-declaration:
23445 decl-specifier-seq [opt] member-declarator-list [opt] ;
23446 function-definition ; [opt]
23447 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23448 using-declaration
23449 template-declaration
23450 alias-declaration
23452 member-declarator-list:
23453 member-declarator
23454 member-declarator-list , member-declarator
23456 member-declarator:
23457 declarator pure-specifier [opt]
23458 declarator constant-initializer [opt]
23459 identifier [opt] : constant-expression
23461 GNU Extensions:
23463 member-declaration:
23464 __extension__ member-declaration
23466 member-declarator:
23467 declarator attributes [opt] pure-specifier [opt]
23468 declarator attributes [opt] constant-initializer [opt]
23469 identifier [opt] attributes [opt] : constant-expression
23471 C++0x Extensions:
23473 member-declaration:
23474 static_assert-declaration */
23476 static void
23477 cp_parser_member_declaration (cp_parser* parser)
23479 cp_decl_specifier_seq decl_specifiers;
23480 tree prefix_attributes;
23481 tree decl;
23482 int declares_class_or_enum;
23483 bool friend_p;
23484 cp_token *token = NULL;
23485 cp_token *decl_spec_token_start = NULL;
23486 cp_token *initializer_token_start = NULL;
23487 int saved_pedantic;
23488 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23490 /* Check for the `__extension__' keyword. */
23491 if (cp_parser_extension_opt (parser, &saved_pedantic))
23493 /* Recurse. */
23494 cp_parser_member_declaration (parser);
23495 /* Restore the old value of the PEDANTIC flag. */
23496 pedantic = saved_pedantic;
23498 return;
23501 /* Check for a template-declaration. */
23502 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23504 /* An explicit specialization here is an error condition, and we
23505 expect the specialization handler to detect and report this. */
23506 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23507 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23508 cp_parser_explicit_specialization (parser);
23509 else
23510 cp_parser_template_declaration (parser, /*member_p=*/true);
23512 return;
23514 /* Check for a template introduction. */
23515 else if (cp_parser_template_declaration_after_export (parser, true))
23516 return;
23518 /* Check for a using-declaration. */
23519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23521 if (cxx_dialect < cxx11)
23523 /* Parse the using-declaration. */
23524 cp_parser_using_declaration (parser,
23525 /*access_declaration_p=*/false);
23526 return;
23528 else
23530 tree decl;
23531 bool alias_decl_expected;
23532 cp_parser_parse_tentatively (parser);
23533 decl = cp_parser_alias_declaration (parser);
23534 /* Note that if we actually see the '=' token after the
23535 identifier, cp_parser_alias_declaration commits the
23536 tentative parse. In that case, we really expect an
23537 alias-declaration. Otherwise, we expect a using
23538 declaration. */
23539 alias_decl_expected =
23540 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23541 cp_parser_parse_definitely (parser);
23543 if (alias_decl_expected)
23544 finish_member_declaration (decl);
23545 else
23546 cp_parser_using_declaration (parser,
23547 /*access_declaration_p=*/false);
23548 return;
23552 /* Check for @defs. */
23553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23555 tree ivar, member;
23556 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23557 ivar = ivar_chains;
23558 while (ivar)
23560 member = ivar;
23561 ivar = TREE_CHAIN (member);
23562 TREE_CHAIN (member) = NULL_TREE;
23563 finish_member_declaration (member);
23565 return;
23568 /* If the next token is `static_assert' we have a static assertion. */
23569 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23571 cp_parser_static_assert (parser, /*member_p=*/true);
23572 return;
23575 parser->colon_corrects_to_scope_p = false;
23577 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23578 goto out;
23580 /* Parse the decl-specifier-seq. */
23581 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23582 cp_parser_decl_specifier_seq (parser,
23583 CP_PARSER_FLAGS_OPTIONAL,
23584 &decl_specifiers,
23585 &declares_class_or_enum);
23586 /* Check for an invalid type-name. */
23587 if (!decl_specifiers.any_type_specifiers_p
23588 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23589 goto out;
23590 /* If there is no declarator, then the decl-specifier-seq should
23591 specify a type. */
23592 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23594 /* If there was no decl-specifier-seq, and the next token is a
23595 `;', then we have something like:
23597 struct S { ; };
23599 [class.mem]
23601 Each member-declaration shall declare at least one member
23602 name of the class. */
23603 if (!decl_specifiers.any_specifiers_p)
23605 cp_token *token = cp_lexer_peek_token (parser->lexer);
23606 if (!in_system_header_at (token->location))
23608 gcc_rich_location richloc (token->location);
23609 richloc.add_fixit_remove ();
23610 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23613 else
23615 tree type;
23617 /* See if this declaration is a friend. */
23618 friend_p = cp_parser_friend_p (&decl_specifiers);
23619 /* If there were decl-specifiers, check to see if there was
23620 a class-declaration. */
23621 type = check_tag_decl (&decl_specifiers,
23622 /*explicit_type_instantiation_p=*/false);
23623 /* Nested classes have already been added to the class, but
23624 a `friend' needs to be explicitly registered. */
23625 if (friend_p)
23627 /* If the `friend' keyword was present, the friend must
23628 be introduced with a class-key. */
23629 if (!declares_class_or_enum && cxx_dialect < cxx11)
23630 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23631 "in C++03 a class-key must be used "
23632 "when declaring a friend");
23633 /* In this case:
23635 template <typename T> struct A {
23636 friend struct A<T>::B;
23639 A<T>::B will be represented by a TYPENAME_TYPE, and
23640 therefore not recognized by check_tag_decl. */
23641 if (!type)
23643 type = decl_specifiers.type;
23644 if (type && TREE_CODE (type) == TYPE_DECL)
23645 type = TREE_TYPE (type);
23647 if (!type || !TYPE_P (type))
23648 error_at (decl_spec_token_start->location,
23649 "friend declaration does not name a class or "
23650 "function");
23651 else
23652 make_friend_class (current_class_type, type,
23653 /*complain=*/true);
23655 /* If there is no TYPE, an error message will already have
23656 been issued. */
23657 else if (!type || type == error_mark_node)
23659 /* An anonymous aggregate has to be handled specially; such
23660 a declaration really declares a data member (with a
23661 particular type), as opposed to a nested class. */
23662 else if (ANON_AGGR_TYPE_P (type))
23664 /* C++11 9.5/6. */
23665 if (decl_specifiers.storage_class != sc_none)
23666 error_at (decl_spec_token_start->location,
23667 "a storage class on an anonymous aggregate "
23668 "in class scope is not allowed");
23670 /* Remove constructors and such from TYPE, now that we
23671 know it is an anonymous aggregate. */
23672 fixup_anonymous_aggr (type);
23673 /* And make the corresponding data member. */
23674 decl = build_decl (decl_spec_token_start->location,
23675 FIELD_DECL, NULL_TREE, type);
23676 /* Add it to the class. */
23677 finish_member_declaration (decl);
23679 else
23680 cp_parser_check_access_in_redeclaration
23681 (TYPE_NAME (type),
23682 decl_spec_token_start->location);
23685 else
23687 bool assume_semicolon = false;
23689 /* Clear attributes from the decl_specifiers but keep them
23690 around as prefix attributes that apply them to the entity
23691 being declared. */
23692 prefix_attributes = decl_specifiers.attributes;
23693 decl_specifiers.attributes = NULL_TREE;
23695 /* See if these declarations will be friends. */
23696 friend_p = cp_parser_friend_p (&decl_specifiers);
23698 /* Keep going until we hit the `;' at the end of the
23699 declaration. */
23700 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23702 tree attributes = NULL_TREE;
23703 tree first_attribute;
23704 tree initializer;
23705 bool is_bitfld = false;
23706 bool named_bitfld = false;
23708 /* Peek at the next token. */
23709 token = cp_lexer_peek_token (parser->lexer);
23711 /* The following code wants to know early if it is a bit-field
23712 or some other declaration. Attributes can appear before
23713 the `:' token, but are hopefully rare enough that the
23714 simplicity of the tentative lookup pays off. */
23715 if (cp_next_tokens_can_be_attribute_p (parser)
23716 || (token->type == CPP_NAME
23717 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23718 && (named_bitfld = true)))
23720 cp_parser_parse_tentatively (parser);
23721 if (named_bitfld)
23722 cp_lexer_consume_token (parser->lexer);
23723 cp_parser_attributes_opt (parser);
23724 token = cp_lexer_peek_token (parser->lexer);
23725 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23726 cp_parser_abort_tentative_parse (parser);
23729 /* Check for a bitfield declaration. */
23730 if (is_bitfld
23731 || token->type == CPP_COLON
23732 || (token->type == CPP_NAME
23733 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23734 && (named_bitfld = true)))
23736 tree identifier;
23737 tree width;
23738 tree late_attributes = NULL_TREE;
23740 if (named_bitfld)
23741 identifier = cp_parser_identifier (parser);
23742 else
23743 identifier = NULL_TREE;
23745 /* Look for attributes that apply to the bitfield. */
23746 attributes = cp_parser_attributes_opt (parser);
23748 /* Consume the `:' token. */
23749 cp_lexer_consume_token (parser->lexer);
23751 /* Get the width of the bitfield. */
23752 width = cp_parser_constant_expression (parser, false, NULL,
23753 cxx_dialect >= cxx11);
23755 /* In C++2A and as extension for C++11 and above we allow
23756 default member initializers for bit-fields. */
23757 initializer = NULL_TREE;
23758 if (cxx_dialect >= cxx11
23759 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23760 || cp_lexer_next_token_is (parser->lexer,
23761 CPP_OPEN_BRACE)))
23763 location_t loc
23764 = cp_lexer_peek_token (parser->lexer)->location;
23765 if (cxx_dialect < cxx2a
23766 && !in_system_header_at (loc)
23767 && identifier != NULL_TREE)
23768 pedwarn (loc, 0,
23769 "default member initializers for bit-fields "
23770 "only available with -std=c++2a or "
23771 "-std=gnu++2a");
23773 initializer = cp_parser_save_nsdmi (parser);
23774 if (identifier == NULL_TREE)
23776 error_at (loc, "default member initializer for "
23777 "unnamed bit-field");
23778 initializer = NULL_TREE;
23781 else
23783 /* Look for attributes that apply to the bitfield after
23784 the `:' token and width. This is where GCC used to
23785 parse attributes in the past, pedwarn if there is
23786 a std attribute. */
23787 if (cp_next_tokens_can_be_std_attribute_p (parser))
23788 pedwarn (input_location, OPT_Wpedantic,
23789 "ISO C++ allows bit-field attributes only "
23790 "before the %<:%> token");
23792 late_attributes = cp_parser_attributes_opt (parser);
23795 attributes = chainon (attributes, late_attributes);
23797 /* Remember which attributes are prefix attributes and
23798 which are not. */
23799 first_attribute = attributes;
23800 /* Combine the attributes. */
23801 attributes = chainon (prefix_attributes, attributes);
23803 /* Create the bitfield declaration. */
23804 decl = grokbitfield (identifier
23805 ? make_id_declarator (NULL_TREE,
23806 identifier,
23807 sfk_none)
23808 : NULL,
23809 &decl_specifiers,
23810 width, initializer,
23811 attributes);
23813 else
23815 cp_declarator *declarator;
23816 tree asm_specification;
23817 int ctor_dtor_or_conv_p;
23819 /* Parse the declarator. */
23820 declarator
23821 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23822 &ctor_dtor_or_conv_p,
23823 /*parenthesized_p=*/NULL,
23824 /*member_p=*/true,
23825 friend_p);
23827 /* If something went wrong parsing the declarator, make sure
23828 that we at least consume some tokens. */
23829 if (declarator == cp_error_declarator)
23831 /* Skip to the end of the statement. */
23832 cp_parser_skip_to_end_of_statement (parser);
23833 /* If the next token is not a semicolon, that is
23834 probably because we just skipped over the body of
23835 a function. So, we consume a semicolon if
23836 present, but do not issue an error message if it
23837 is not present. */
23838 if (cp_lexer_next_token_is (parser->lexer,
23839 CPP_SEMICOLON))
23840 cp_lexer_consume_token (parser->lexer);
23841 goto out;
23844 if (declares_class_or_enum & 2)
23845 cp_parser_check_for_definition_in_return_type
23846 (declarator, decl_specifiers.type,
23847 decl_specifiers.locations[ds_type_spec]);
23849 /* Look for an asm-specification. */
23850 asm_specification = cp_parser_asm_specification_opt (parser);
23851 /* Look for attributes that apply to the declaration. */
23852 attributes = cp_parser_attributes_opt (parser);
23853 /* Remember which attributes are prefix attributes and
23854 which are not. */
23855 first_attribute = attributes;
23856 /* Combine the attributes. */
23857 attributes = chainon (prefix_attributes, attributes);
23859 /* If it's an `=', then we have a constant-initializer or a
23860 pure-specifier. It is not correct to parse the
23861 initializer before registering the member declaration
23862 since the member declaration should be in scope while
23863 its initializer is processed. However, the rest of the
23864 front end does not yet provide an interface that allows
23865 us to handle this correctly. */
23866 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23868 /* In [class.mem]:
23870 A pure-specifier shall be used only in the declaration of
23871 a virtual function.
23873 A member-declarator can contain a constant-initializer
23874 only if it declares a static member of integral or
23875 enumeration type.
23877 Therefore, if the DECLARATOR is for a function, we look
23878 for a pure-specifier; otherwise, we look for a
23879 constant-initializer. When we call `grokfield', it will
23880 perform more stringent semantics checks. */
23881 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23882 if (function_declarator_p (declarator)
23883 || (decl_specifiers.type
23884 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23885 && declarator->kind == cdk_id
23886 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23887 == FUNCTION_TYPE)))
23888 initializer = cp_parser_pure_specifier (parser);
23889 else if (decl_specifiers.storage_class != sc_static)
23890 initializer = cp_parser_save_nsdmi (parser);
23891 else if (cxx_dialect >= cxx11)
23893 bool nonconst;
23894 /* Don't require a constant rvalue in C++11, since we
23895 might want a reference constant. We'll enforce
23896 constancy later. */
23897 cp_lexer_consume_token (parser->lexer);
23898 /* Parse the initializer. */
23899 initializer = cp_parser_initializer_clause (parser,
23900 &nonconst);
23902 else
23903 /* Parse the initializer. */
23904 initializer = cp_parser_constant_initializer (parser);
23906 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23907 && !function_declarator_p (declarator))
23909 bool x;
23910 if (decl_specifiers.storage_class != sc_static)
23911 initializer = cp_parser_save_nsdmi (parser);
23912 else
23913 initializer = cp_parser_initializer (parser, &x, &x);
23915 /* Otherwise, there is no initializer. */
23916 else
23917 initializer = NULL_TREE;
23919 /* See if we are probably looking at a function
23920 definition. We are certainly not looking at a
23921 member-declarator. Calling `grokfield' has
23922 side-effects, so we must not do it unless we are sure
23923 that we are looking at a member-declarator. */
23924 if (cp_parser_token_starts_function_definition_p
23925 (cp_lexer_peek_token (parser->lexer)))
23927 /* The grammar does not allow a pure-specifier to be
23928 used when a member function is defined. (It is
23929 possible that this fact is an oversight in the
23930 standard, since a pure function may be defined
23931 outside of the class-specifier. */
23932 if (initializer && initializer_token_start)
23933 error_at (initializer_token_start->location,
23934 "pure-specifier on function-definition");
23935 decl = cp_parser_save_member_function_body (parser,
23936 &decl_specifiers,
23937 declarator,
23938 attributes);
23939 if (parser->fully_implicit_function_template_p)
23940 decl = finish_fully_implicit_template (parser, decl);
23941 /* If the member was not a friend, declare it here. */
23942 if (!friend_p)
23943 finish_member_declaration (decl);
23944 /* Peek at the next token. */
23945 token = cp_lexer_peek_token (parser->lexer);
23946 /* If the next token is a semicolon, consume it. */
23947 if (token->type == CPP_SEMICOLON)
23949 location_t semicolon_loc
23950 = cp_lexer_consume_token (parser->lexer)->location;
23951 gcc_rich_location richloc (semicolon_loc);
23952 richloc.add_fixit_remove ();
23953 warning_at (&richloc, OPT_Wextra_semi,
23954 "extra %<;%> after in-class "
23955 "function definition");
23957 goto out;
23959 else
23960 if (declarator->kind == cdk_function)
23961 declarator->id_loc = token->location;
23962 /* Create the declaration. */
23963 decl = grokfield (declarator, &decl_specifiers,
23964 initializer, /*init_const_expr_p=*/true,
23965 asm_specification, attributes);
23966 if (parser->fully_implicit_function_template_p)
23968 if (friend_p)
23969 finish_fully_implicit_template (parser, 0);
23970 else
23971 decl = finish_fully_implicit_template (parser, decl);
23975 cp_finalize_omp_declare_simd (parser, decl);
23976 cp_finalize_oacc_routine (parser, decl, false);
23978 /* Reset PREFIX_ATTRIBUTES. */
23979 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23980 attributes = TREE_CHAIN (attributes);
23981 if (attributes)
23982 TREE_CHAIN (attributes) = NULL_TREE;
23984 /* If there is any qualification still in effect, clear it
23985 now; we will be starting fresh with the next declarator. */
23986 parser->scope = NULL_TREE;
23987 parser->qualifying_scope = NULL_TREE;
23988 parser->object_scope = NULL_TREE;
23989 /* If it's a `,', then there are more declarators. */
23990 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23992 cp_lexer_consume_token (parser->lexer);
23993 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23995 cp_token *token = cp_lexer_previous_token (parser->lexer);
23996 gcc_rich_location richloc (token->location);
23997 richloc.add_fixit_remove ();
23998 error_at (&richloc, "stray %<,%> at end of "
23999 "member declaration");
24002 /* If the next token isn't a `;', then we have a parse error. */
24003 else if (cp_lexer_next_token_is_not (parser->lexer,
24004 CPP_SEMICOLON))
24006 /* The next token might be a ways away from where the
24007 actual semicolon is missing. Find the previous token
24008 and use that for our error position. */
24009 cp_token *token = cp_lexer_previous_token (parser->lexer);
24010 gcc_rich_location richloc (token->location);
24011 richloc.add_fixit_insert_after (";");
24012 error_at (&richloc, "expected %<;%> at end of "
24013 "member declaration");
24015 /* Assume that the user meant to provide a semicolon. If
24016 we were to cp_parser_skip_to_end_of_statement, we might
24017 skip to a semicolon inside a member function definition
24018 and issue nonsensical error messages. */
24019 assume_semicolon = true;
24022 if (decl)
24024 /* Add DECL to the list of members. */
24025 if (!friend_p
24026 /* Explicitly include, eg, NSDMIs, for better error
24027 recovery (c++/58650). */
24028 || !DECL_DECLARES_FUNCTION_P (decl))
24029 finish_member_declaration (decl);
24031 if (TREE_CODE (decl) == FUNCTION_DECL)
24032 cp_parser_save_default_args (parser, decl);
24033 else if (TREE_CODE (decl) == FIELD_DECL
24034 && DECL_INITIAL (decl))
24035 /* Add DECL to the queue of NSDMI to be parsed later. */
24036 vec_safe_push (unparsed_nsdmis, decl);
24039 if (assume_semicolon)
24040 goto out;
24044 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24045 out:
24046 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24049 /* Parse a pure-specifier.
24051 pure-specifier:
24054 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24055 Otherwise, ERROR_MARK_NODE is returned. */
24057 static tree
24058 cp_parser_pure_specifier (cp_parser* parser)
24060 cp_token *token;
24062 /* Look for the `=' token. */
24063 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24064 return error_mark_node;
24065 /* Look for the `0' token. */
24066 token = cp_lexer_peek_token (parser->lexer);
24068 if (token->type == CPP_EOF
24069 || token->type == CPP_PRAGMA_EOL)
24070 return error_mark_node;
24072 cp_lexer_consume_token (parser->lexer);
24074 /* Accept = default or = delete in c++0x mode. */
24075 if (token->keyword == RID_DEFAULT
24076 || token->keyword == RID_DELETE)
24078 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24079 return token->u.value;
24082 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24083 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24085 cp_parser_error (parser,
24086 "invalid pure specifier (only %<= 0%> is allowed)");
24087 cp_parser_skip_to_end_of_statement (parser);
24088 return error_mark_node;
24090 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24092 error_at (token->location, "templates may not be %<virtual%>");
24093 return error_mark_node;
24096 return integer_zero_node;
24099 /* Parse a constant-initializer.
24101 constant-initializer:
24102 = constant-expression
24104 Returns a representation of the constant-expression. */
24106 static tree
24107 cp_parser_constant_initializer (cp_parser* parser)
24109 /* Look for the `=' token. */
24110 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24111 return error_mark_node;
24113 /* It is invalid to write:
24115 struct S { static const int i = { 7 }; };
24118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24120 cp_parser_error (parser,
24121 "a brace-enclosed initializer is not allowed here");
24122 /* Consume the opening brace. */
24123 matching_braces braces;
24124 braces.consume_open (parser);
24125 /* Skip the initializer. */
24126 cp_parser_skip_to_closing_brace (parser);
24127 /* Look for the trailing `}'. */
24128 braces.require_close (parser);
24130 return error_mark_node;
24133 return cp_parser_constant_expression (parser);
24136 /* Derived classes [gram.class.derived] */
24138 /* Parse a base-clause.
24140 base-clause:
24141 : base-specifier-list
24143 base-specifier-list:
24144 base-specifier ... [opt]
24145 base-specifier-list , base-specifier ... [opt]
24147 Returns a TREE_LIST representing the base-classes, in the order in
24148 which they were declared. The representation of each node is as
24149 described by cp_parser_base_specifier.
24151 In the case that no bases are specified, this function will return
24152 NULL_TREE, not ERROR_MARK_NODE. */
24154 static tree
24155 cp_parser_base_clause (cp_parser* parser)
24157 tree bases = NULL_TREE;
24159 /* Look for the `:' that begins the list. */
24160 cp_parser_require (parser, CPP_COLON, RT_COLON);
24162 /* Scan the base-specifier-list. */
24163 while (true)
24165 cp_token *token;
24166 tree base;
24167 bool pack_expansion_p = false;
24169 /* Look for the base-specifier. */
24170 base = cp_parser_base_specifier (parser);
24171 /* Look for the (optional) ellipsis. */
24172 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24174 /* Consume the `...'. */
24175 cp_lexer_consume_token (parser->lexer);
24177 pack_expansion_p = true;
24180 /* Add BASE to the front of the list. */
24181 if (base && base != error_mark_node)
24183 if (pack_expansion_p)
24184 /* Make this a pack expansion type. */
24185 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24187 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24189 TREE_CHAIN (base) = bases;
24190 bases = base;
24193 /* Peek at the next token. */
24194 token = cp_lexer_peek_token (parser->lexer);
24195 /* If it's not a comma, then the list is complete. */
24196 if (token->type != CPP_COMMA)
24197 break;
24198 /* Consume the `,'. */
24199 cp_lexer_consume_token (parser->lexer);
24202 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24203 base class had a qualified name. However, the next name that
24204 appears is certainly not qualified. */
24205 parser->scope = NULL_TREE;
24206 parser->qualifying_scope = NULL_TREE;
24207 parser->object_scope = NULL_TREE;
24209 return nreverse (bases);
24212 /* Parse a base-specifier.
24214 base-specifier:
24215 :: [opt] nested-name-specifier [opt] class-name
24216 virtual access-specifier [opt] :: [opt] nested-name-specifier
24217 [opt] class-name
24218 access-specifier virtual [opt] :: [opt] nested-name-specifier
24219 [opt] class-name
24221 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24222 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24223 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24224 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24226 static tree
24227 cp_parser_base_specifier (cp_parser* parser)
24229 cp_token *token;
24230 bool done = false;
24231 bool virtual_p = false;
24232 bool duplicate_virtual_error_issued_p = false;
24233 bool duplicate_access_error_issued_p = false;
24234 bool class_scope_p, template_p;
24235 tree access = access_default_node;
24236 tree type;
24238 /* Process the optional `virtual' and `access-specifier'. */
24239 while (!done)
24241 /* Peek at the next token. */
24242 token = cp_lexer_peek_token (parser->lexer);
24243 /* Process `virtual'. */
24244 switch (token->keyword)
24246 case RID_VIRTUAL:
24247 /* If `virtual' appears more than once, issue an error. */
24248 if (virtual_p && !duplicate_virtual_error_issued_p)
24250 cp_parser_error (parser,
24251 "%<virtual%> specified more than once in base-specifier");
24252 duplicate_virtual_error_issued_p = true;
24255 virtual_p = true;
24257 /* Consume the `virtual' token. */
24258 cp_lexer_consume_token (parser->lexer);
24260 break;
24262 case RID_PUBLIC:
24263 case RID_PROTECTED:
24264 case RID_PRIVATE:
24265 /* If more than one access specifier appears, issue an
24266 error. */
24267 if (access != access_default_node
24268 && !duplicate_access_error_issued_p)
24270 cp_parser_error (parser,
24271 "more than one access specifier in base-specifier");
24272 duplicate_access_error_issued_p = true;
24275 access = ridpointers[(int) token->keyword];
24277 /* Consume the access-specifier. */
24278 cp_lexer_consume_token (parser->lexer);
24280 break;
24282 default:
24283 done = true;
24284 break;
24287 /* It is not uncommon to see programs mechanically, erroneously, use
24288 the 'typename' keyword to denote (dependent) qualified types
24289 as base classes. */
24290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24292 token = cp_lexer_peek_token (parser->lexer);
24293 if (!processing_template_decl)
24294 error_at (token->location,
24295 "keyword %<typename%> not allowed outside of templates");
24296 else
24297 error_at (token->location,
24298 "keyword %<typename%> not allowed in this context "
24299 "(the base class is implicitly a type)");
24300 cp_lexer_consume_token (parser->lexer);
24303 /* Look for the optional `::' operator. */
24304 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24305 /* Look for the nested-name-specifier. The simplest way to
24306 implement:
24308 [temp.res]
24310 The keyword `typename' is not permitted in a base-specifier or
24311 mem-initializer; in these contexts a qualified name that
24312 depends on a template-parameter is implicitly assumed to be a
24313 type name.
24315 is to pretend that we have seen the `typename' keyword at this
24316 point. */
24317 cp_parser_nested_name_specifier_opt (parser,
24318 /*typename_keyword_p=*/true,
24319 /*check_dependency_p=*/true,
24320 /*type_p=*/true,
24321 /*is_declaration=*/true);
24322 /* If the base class is given by a qualified name, assume that names
24323 we see are type names or templates, as appropriate. */
24324 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24325 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24327 if (!parser->scope
24328 && cp_lexer_next_token_is_decltype (parser->lexer))
24329 /* DR 950 allows decltype as a base-specifier. */
24330 type = cp_parser_decltype (parser);
24331 else
24333 /* Otherwise, look for the class-name. */
24334 type = cp_parser_class_name (parser,
24335 class_scope_p,
24336 template_p,
24337 typename_type,
24338 /*check_dependency_p=*/true,
24339 /*class_head_p=*/false,
24340 /*is_declaration=*/true);
24341 type = TREE_TYPE (type);
24344 if (type == error_mark_node)
24345 return error_mark_node;
24347 return finish_base_specifier (type, access, virtual_p);
24350 /* Exception handling [gram.exception] */
24352 /* Parse an (optional) noexcept-specification.
24354 noexcept-specification:
24355 noexcept ( constant-expression ) [opt]
24357 If no noexcept-specification is present, returns NULL_TREE.
24358 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24359 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24360 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24361 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24362 in which case a boolean condition is returned instead. */
24364 static tree
24365 cp_parser_noexcept_specification_opt (cp_parser* parser,
24366 bool require_constexpr,
24367 bool* consumed_expr,
24368 bool return_cond)
24370 cp_token *token;
24371 const char *saved_message;
24373 /* Peek at the next token. */
24374 token = cp_lexer_peek_token (parser->lexer);
24376 /* Is it a noexcept-specification? */
24377 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24379 tree expr;
24380 cp_lexer_consume_token (parser->lexer);
24382 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24384 matching_parens parens;
24385 parens.consume_open (parser);
24387 if (require_constexpr)
24389 /* Types may not be defined in an exception-specification. */
24390 saved_message = parser->type_definition_forbidden_message;
24391 parser->type_definition_forbidden_message
24392 = G_("types may not be defined in an exception-specification");
24394 expr = cp_parser_constant_expression (parser);
24396 /* Restore the saved message. */
24397 parser->type_definition_forbidden_message = saved_message;
24399 else
24401 expr = cp_parser_expression (parser);
24402 *consumed_expr = true;
24405 parens.require_close (parser);
24407 else
24409 expr = boolean_true_node;
24410 if (!require_constexpr)
24411 *consumed_expr = false;
24414 /* We cannot build a noexcept-spec right away because this will check
24415 that expr is a constexpr. */
24416 if (!return_cond)
24417 return build_noexcept_spec (expr, tf_warning_or_error);
24418 else
24419 return expr;
24421 else
24422 return NULL_TREE;
24425 /* Parse an (optional) exception-specification.
24427 exception-specification:
24428 throw ( type-id-list [opt] )
24430 Returns a TREE_LIST representing the exception-specification. The
24431 TREE_VALUE of each node is a type. */
24433 static tree
24434 cp_parser_exception_specification_opt (cp_parser* parser)
24436 cp_token *token;
24437 tree type_id_list;
24438 const char *saved_message;
24440 /* Peek at the next token. */
24441 token = cp_lexer_peek_token (parser->lexer);
24443 /* Is it a noexcept-specification? */
24444 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24445 false);
24446 if (type_id_list != NULL_TREE)
24447 return type_id_list;
24449 /* If it's not `throw', then there's no exception-specification. */
24450 if (!cp_parser_is_keyword (token, RID_THROW))
24451 return NULL_TREE;
24453 location_t loc = token->location;
24455 /* Consume the `throw'. */
24456 cp_lexer_consume_token (parser->lexer);
24458 /* Look for the `('. */
24459 matching_parens parens;
24460 parens.require_open (parser);
24462 /* Peek at the next token. */
24463 token = cp_lexer_peek_token (parser->lexer);
24464 /* If it's not a `)', then there is a type-id-list. */
24465 if (token->type != CPP_CLOSE_PAREN)
24467 /* Types may not be defined in an exception-specification. */
24468 saved_message = parser->type_definition_forbidden_message;
24469 parser->type_definition_forbidden_message
24470 = G_("types may not be defined in an exception-specification");
24471 /* Parse the type-id-list. */
24472 type_id_list = cp_parser_type_id_list (parser);
24473 /* Restore the saved message. */
24474 parser->type_definition_forbidden_message = saved_message;
24476 if (cxx_dialect >= cxx17)
24478 error_at (loc, "ISO C++17 does not allow dynamic exception "
24479 "specifications");
24480 type_id_list = NULL_TREE;
24482 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24483 warning_at (loc, OPT_Wdeprecated,
24484 "dynamic exception specifications are deprecated in "
24485 "C++11");
24487 /* In C++17, throw() is equivalent to noexcept (true). throw()
24488 is deprecated in C++11 and above as well, but is still widely used,
24489 so don't warn about it yet. */
24490 else if (cxx_dialect >= cxx17)
24491 type_id_list = noexcept_true_spec;
24492 else
24493 type_id_list = empty_except_spec;
24495 /* Look for the `)'. */
24496 parens.require_close (parser);
24498 return type_id_list;
24501 /* Parse an (optional) type-id-list.
24503 type-id-list:
24504 type-id ... [opt]
24505 type-id-list , type-id ... [opt]
24507 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24508 in the order that the types were presented. */
24510 static tree
24511 cp_parser_type_id_list (cp_parser* parser)
24513 tree types = NULL_TREE;
24515 while (true)
24517 cp_token *token;
24518 tree type;
24520 token = cp_lexer_peek_token (parser->lexer);
24522 /* Get the next type-id. */
24523 type = cp_parser_type_id (parser);
24524 /* Check for invalid 'auto'. */
24525 if (flag_concepts && type_uses_auto (type))
24527 error_at (token->location,
24528 "invalid use of %<auto%> in exception-specification");
24529 type = error_mark_node;
24531 /* Parse the optional ellipsis. */
24532 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24534 /* Consume the `...'. */
24535 cp_lexer_consume_token (parser->lexer);
24537 /* Turn the type into a pack expansion expression. */
24538 type = make_pack_expansion (type);
24540 /* Add it to the list. */
24541 types = add_exception_specifier (types, type, /*complain=*/1);
24542 /* Peek at the next token. */
24543 token = cp_lexer_peek_token (parser->lexer);
24544 /* If it is not a `,', we are done. */
24545 if (token->type != CPP_COMMA)
24546 break;
24547 /* Consume the `,'. */
24548 cp_lexer_consume_token (parser->lexer);
24551 return nreverse (types);
24554 /* Parse a try-block.
24556 try-block:
24557 try compound-statement handler-seq */
24559 static tree
24560 cp_parser_try_block (cp_parser* parser)
24562 tree try_block;
24564 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24565 if (parser->in_function_body
24566 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24567 error ("%<try%> in %<constexpr%> function");
24569 try_block = begin_try_block ();
24570 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24571 finish_try_block (try_block);
24572 cp_parser_handler_seq (parser);
24573 finish_handler_sequence (try_block);
24575 return try_block;
24578 /* Parse a function-try-block.
24580 function-try-block:
24581 try ctor-initializer [opt] function-body handler-seq */
24583 static void
24584 cp_parser_function_try_block (cp_parser* parser)
24586 tree compound_stmt;
24587 tree try_block;
24589 /* Look for the `try' keyword. */
24590 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24591 return;
24592 /* Let the rest of the front end know where we are. */
24593 try_block = begin_function_try_block (&compound_stmt);
24594 /* Parse the function-body. */
24595 cp_parser_ctor_initializer_opt_and_function_body
24596 (parser, /*in_function_try_block=*/true);
24597 /* We're done with the `try' part. */
24598 finish_function_try_block (try_block);
24599 /* Parse the handlers. */
24600 cp_parser_handler_seq (parser);
24601 /* We're done with the handlers. */
24602 finish_function_handler_sequence (try_block, compound_stmt);
24605 /* Parse a handler-seq.
24607 handler-seq:
24608 handler handler-seq [opt] */
24610 static void
24611 cp_parser_handler_seq (cp_parser* parser)
24613 while (true)
24615 cp_token *token;
24617 /* Parse the handler. */
24618 cp_parser_handler (parser);
24619 /* Peek at the next token. */
24620 token = cp_lexer_peek_token (parser->lexer);
24621 /* If it's not `catch' then there are no more handlers. */
24622 if (!cp_parser_is_keyword (token, RID_CATCH))
24623 break;
24627 /* Parse a handler.
24629 handler:
24630 catch ( exception-declaration ) compound-statement */
24632 static void
24633 cp_parser_handler (cp_parser* parser)
24635 tree handler;
24636 tree declaration;
24638 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24639 handler = begin_handler ();
24640 matching_parens parens;
24641 parens.require_open (parser);
24642 declaration = cp_parser_exception_declaration (parser);
24643 finish_handler_parms (declaration, handler);
24644 parens.require_close (parser);
24645 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24646 finish_handler (handler);
24649 /* Parse an exception-declaration.
24651 exception-declaration:
24652 type-specifier-seq declarator
24653 type-specifier-seq abstract-declarator
24654 type-specifier-seq
24657 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24658 ellipsis variant is used. */
24660 static tree
24661 cp_parser_exception_declaration (cp_parser* parser)
24663 cp_decl_specifier_seq type_specifiers;
24664 cp_declarator *declarator;
24665 const char *saved_message;
24667 /* If it's an ellipsis, it's easy to handle. */
24668 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24670 /* Consume the `...' token. */
24671 cp_lexer_consume_token (parser->lexer);
24672 return NULL_TREE;
24675 /* Types may not be defined in exception-declarations. */
24676 saved_message = parser->type_definition_forbidden_message;
24677 parser->type_definition_forbidden_message
24678 = G_("types may not be defined in exception-declarations");
24680 /* Parse the type-specifier-seq. */
24681 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24682 /*is_trailing_return=*/false,
24683 &type_specifiers);
24684 /* If it's a `)', then there is no declarator. */
24685 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24686 declarator = NULL;
24687 else
24688 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24689 /*ctor_dtor_or_conv_p=*/NULL,
24690 /*parenthesized_p=*/NULL,
24691 /*member_p=*/false,
24692 /*friend_p=*/false);
24694 /* Restore the saved message. */
24695 parser->type_definition_forbidden_message = saved_message;
24697 if (!type_specifiers.any_specifiers_p)
24698 return error_mark_node;
24700 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24703 /* Parse a throw-expression.
24705 throw-expression:
24706 throw assignment-expression [opt]
24708 Returns a THROW_EXPR representing the throw-expression. */
24710 static tree
24711 cp_parser_throw_expression (cp_parser* parser)
24713 tree expression;
24714 cp_token* token;
24716 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24717 token = cp_lexer_peek_token (parser->lexer);
24718 /* Figure out whether or not there is an assignment-expression
24719 following the "throw" keyword. */
24720 if (token->type == CPP_COMMA
24721 || token->type == CPP_SEMICOLON
24722 || token->type == CPP_CLOSE_PAREN
24723 || token->type == CPP_CLOSE_SQUARE
24724 || token->type == CPP_CLOSE_BRACE
24725 || token->type == CPP_COLON)
24726 expression = NULL_TREE;
24727 else
24728 expression = cp_parser_assignment_expression (parser);
24730 return build_throw (expression);
24733 /* GNU Extensions */
24735 /* Parse an (optional) asm-specification.
24737 asm-specification:
24738 asm ( string-literal )
24740 If the asm-specification is present, returns a STRING_CST
24741 corresponding to the string-literal. Otherwise, returns
24742 NULL_TREE. */
24744 static tree
24745 cp_parser_asm_specification_opt (cp_parser* parser)
24747 cp_token *token;
24748 tree asm_specification;
24750 /* Peek at the next token. */
24751 token = cp_lexer_peek_token (parser->lexer);
24752 /* If the next token isn't the `asm' keyword, then there's no
24753 asm-specification. */
24754 if (!cp_parser_is_keyword (token, RID_ASM))
24755 return NULL_TREE;
24757 /* Consume the `asm' token. */
24758 cp_lexer_consume_token (parser->lexer);
24759 /* Look for the `('. */
24760 matching_parens parens;
24761 parens.require_open (parser);
24763 /* Look for the string-literal. */
24764 asm_specification = cp_parser_string_literal (parser, false, false);
24766 /* Look for the `)'. */
24767 parens.require_close (parser);
24769 return asm_specification;
24772 /* Parse an asm-operand-list.
24774 asm-operand-list:
24775 asm-operand
24776 asm-operand-list , asm-operand
24778 asm-operand:
24779 string-literal ( expression )
24780 [ string-literal ] string-literal ( expression )
24782 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24783 each node is the expression. The TREE_PURPOSE is itself a
24784 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24785 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24786 is a STRING_CST for the string literal before the parenthesis. Returns
24787 ERROR_MARK_NODE if any of the operands are invalid. */
24789 static tree
24790 cp_parser_asm_operand_list (cp_parser* parser)
24792 tree asm_operands = NULL_TREE;
24793 bool invalid_operands = false;
24795 while (true)
24797 tree string_literal;
24798 tree expression;
24799 tree name;
24801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24803 /* Consume the `[' token. */
24804 cp_lexer_consume_token (parser->lexer);
24805 /* Read the operand name. */
24806 name = cp_parser_identifier (parser);
24807 if (name != error_mark_node)
24808 name = build_string (IDENTIFIER_LENGTH (name),
24809 IDENTIFIER_POINTER (name));
24810 /* Look for the closing `]'. */
24811 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24813 else
24814 name = NULL_TREE;
24815 /* Look for the string-literal. */
24816 string_literal = cp_parser_string_literal (parser, false, false);
24818 /* Look for the `('. */
24819 matching_parens parens;
24820 parens.require_open (parser);
24821 /* Parse the expression. */
24822 expression = cp_parser_expression (parser);
24823 /* Look for the `)'. */
24824 parens.require_close (parser);
24826 if (name == error_mark_node
24827 || string_literal == error_mark_node
24828 || expression == error_mark_node)
24829 invalid_operands = true;
24831 /* Add this operand to the list. */
24832 asm_operands = tree_cons (build_tree_list (name, string_literal),
24833 expression,
24834 asm_operands);
24835 /* If the next token is not a `,', there are no more
24836 operands. */
24837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24838 break;
24839 /* Consume the `,'. */
24840 cp_lexer_consume_token (parser->lexer);
24843 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24846 /* Parse an asm-clobber-list.
24848 asm-clobber-list:
24849 string-literal
24850 asm-clobber-list , string-literal
24852 Returns a TREE_LIST, indicating the clobbers in the order that they
24853 appeared. The TREE_VALUE of each node is a STRING_CST. */
24855 static tree
24856 cp_parser_asm_clobber_list (cp_parser* parser)
24858 tree clobbers = NULL_TREE;
24860 while (true)
24862 tree string_literal;
24864 /* Look for the string literal. */
24865 string_literal = cp_parser_string_literal (parser, false, false);
24866 /* Add it to the list. */
24867 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24868 /* If the next token is not a `,', then the list is
24869 complete. */
24870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24871 break;
24872 /* Consume the `,' token. */
24873 cp_lexer_consume_token (parser->lexer);
24876 return clobbers;
24879 /* Parse an asm-label-list.
24881 asm-label-list:
24882 identifier
24883 asm-label-list , identifier
24885 Returns a TREE_LIST, indicating the labels in the order that they
24886 appeared. The TREE_VALUE of each node is a label. */
24888 static tree
24889 cp_parser_asm_label_list (cp_parser* parser)
24891 tree labels = NULL_TREE;
24893 while (true)
24895 tree identifier, label, name;
24897 /* Look for the identifier. */
24898 identifier = cp_parser_identifier (parser);
24899 if (!error_operand_p (identifier))
24901 label = lookup_label (identifier);
24902 if (TREE_CODE (label) == LABEL_DECL)
24904 TREE_USED (label) = 1;
24905 check_goto (label);
24906 name = build_string (IDENTIFIER_LENGTH (identifier),
24907 IDENTIFIER_POINTER (identifier));
24908 labels = tree_cons (name, label, labels);
24911 /* If the next token is not a `,', then the list is
24912 complete. */
24913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24914 break;
24915 /* Consume the `,' token. */
24916 cp_lexer_consume_token (parser->lexer);
24919 return nreverse (labels);
24922 /* Return TRUE iff the next tokens in the stream are possibly the
24923 beginning of a GNU extension attribute. */
24925 static bool
24926 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24928 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24931 /* Return TRUE iff the next tokens in the stream are possibly the
24932 beginning of a standard C++-11 attribute specifier. */
24934 static bool
24935 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24937 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24940 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24941 beginning of a standard C++-11 attribute specifier. */
24943 static bool
24944 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24946 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24948 return (cxx_dialect >= cxx11
24949 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24950 || (token->type == CPP_OPEN_SQUARE
24951 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24952 && token->type == CPP_OPEN_SQUARE)));
24955 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24956 beginning of a GNU extension attribute. */
24958 static bool
24959 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24961 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24963 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24966 /* Return true iff the next tokens can be the beginning of either a
24967 GNU attribute list, or a standard C++11 attribute sequence. */
24969 static bool
24970 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24972 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24973 || cp_next_tokens_can_be_std_attribute_p (parser));
24976 /* Return true iff the next Nth tokens can be the beginning of either
24977 a GNU attribute list, or a standard C++11 attribute sequence. */
24979 static bool
24980 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24982 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24983 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24986 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24987 of GNU attributes, or return NULL. */
24989 static tree
24990 cp_parser_attributes_opt (cp_parser *parser)
24992 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24993 return cp_parser_gnu_attributes_opt (parser);
24994 return cp_parser_std_attribute_spec_seq (parser);
24997 #define CILK_SIMD_FN_CLAUSE_MASK \
24998 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24999 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
25000 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
25001 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
25002 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
25004 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
25005 vector [(<clauses>)] */
25007 static void
25008 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
25010 bool first_p = parser->cilk_simd_fn_info == NULL;
25011 cp_token *token = v_token;
25012 if (first_p)
25014 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
25015 parser->cilk_simd_fn_info->error_seen = false;
25016 parser->cilk_simd_fn_info->fndecl_seen = false;
25017 parser->cilk_simd_fn_info->tokens = vNULL;
25018 parser->cilk_simd_fn_info->clauses = NULL_TREE;
25020 int paren_scope = 0;
25021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25023 cp_lexer_consume_token (parser->lexer);
25024 v_token = cp_lexer_peek_token (parser->lexer);
25025 paren_scope++;
25027 while (paren_scope > 0)
25029 token = cp_lexer_peek_token (parser->lexer);
25030 if (token->type == CPP_OPEN_PAREN)
25031 paren_scope++;
25032 else if (token->type == CPP_CLOSE_PAREN)
25033 paren_scope--;
25034 /* Do not push the last ')' */
25035 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
25036 cp_lexer_consume_token (parser->lexer);
25039 token->type = CPP_PRAGMA_EOL;
25040 parser->lexer->next_token = token;
25041 cp_lexer_consume_token (parser->lexer);
25043 struct cp_token_cache *cp
25044 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
25045 parser->cilk_simd_fn_info->tokens.safe_push (cp);
25048 /* Parse an (optional) series of attributes.
25050 attributes:
25051 attributes attribute
25053 attribute:
25054 __attribute__ (( attribute-list [opt] ))
25056 The return value is as for cp_parser_gnu_attribute_list. */
25058 static tree
25059 cp_parser_gnu_attributes_opt (cp_parser* parser)
25061 tree attributes = NULL_TREE;
25063 while (true)
25065 cp_token *token;
25066 tree attribute_list;
25067 bool ok = true;
25069 /* Peek at the next token. */
25070 token = cp_lexer_peek_token (parser->lexer);
25071 /* If it's not `__attribute__', then we're done. */
25072 if (token->keyword != RID_ATTRIBUTE)
25073 break;
25075 /* Consume the `__attribute__' keyword. */
25076 cp_lexer_consume_token (parser->lexer);
25077 /* Look for the two `(' tokens. */
25078 matching_parens outer_parens;
25079 outer_parens.require_open (parser);
25080 matching_parens inner_parens;
25081 inner_parens.require_open (parser);
25083 /* Peek at the next token. */
25084 token = cp_lexer_peek_token (parser->lexer);
25085 if (token->type != CPP_CLOSE_PAREN)
25086 /* Parse the attribute-list. */
25087 attribute_list = cp_parser_gnu_attribute_list (parser);
25088 else
25089 /* If the next token is a `)', then there is no attribute
25090 list. */
25091 attribute_list = NULL;
25093 /* Look for the two `)' tokens. */
25094 if (!inner_parens.require_close (parser))
25095 ok = false;
25096 if (!outer_parens.require_close (parser))
25097 ok = false;
25098 if (!ok)
25099 cp_parser_skip_to_end_of_statement (parser);
25101 /* Add these new attributes to the list. */
25102 attributes = chainon (attributes, attribute_list);
25105 return attributes;
25108 /* Parse a GNU attribute-list.
25110 attribute-list:
25111 attribute
25112 attribute-list , attribute
25114 attribute:
25115 identifier
25116 identifier ( identifier )
25117 identifier ( identifier , expression-list )
25118 identifier ( expression-list )
25120 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25121 to an attribute. The TREE_PURPOSE of each node is the identifier
25122 indicating which attribute is in use. The TREE_VALUE represents
25123 the arguments, if any. */
25125 static tree
25126 cp_parser_gnu_attribute_list (cp_parser* parser)
25128 tree attribute_list = NULL_TREE;
25129 bool save_translate_strings_p = parser->translate_strings_p;
25131 parser->translate_strings_p = false;
25132 while (true)
25134 cp_token *token;
25135 tree identifier;
25136 tree attribute;
25138 /* Look for the identifier. We also allow keywords here; for
25139 example `__attribute__ ((const))' is legal. */
25140 token = cp_lexer_peek_token (parser->lexer);
25141 if (token->type == CPP_NAME
25142 || token->type == CPP_KEYWORD)
25144 tree arguments = NULL_TREE;
25146 /* Consume the token, but save it since we need it for the
25147 SIMD enabled function parsing. */
25148 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25150 /* Save away the identifier that indicates which attribute
25151 this is. */
25152 identifier = (token->type == CPP_KEYWORD)
25153 /* For keywords, use the canonical spelling, not the
25154 parsed identifier. */
25155 ? ridpointers[(int) token->keyword]
25156 : id_token->u.value;
25158 identifier = canonicalize_attr_name (identifier);
25159 attribute = build_tree_list (identifier, NULL_TREE);
25161 /* Peek at the next token. */
25162 token = cp_lexer_peek_token (parser->lexer);
25163 /* If it's an `(', then parse the attribute arguments. */
25164 if (token->type == CPP_OPEN_PAREN)
25166 vec<tree, va_gc> *vec;
25167 int attr_flag = (attribute_takes_identifier_p (identifier)
25168 ? id_attr : normal_attr);
25169 if (is_cilkplus_vector_p (identifier))
25171 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25172 continue;
25174 else
25175 vec = cp_parser_parenthesized_expression_list
25176 (parser, attr_flag, /*cast_p=*/false,
25177 /*allow_expansion_p=*/false,
25178 /*non_constant_p=*/NULL);
25179 if (vec == NULL)
25180 arguments = error_mark_node;
25181 else
25183 arguments = build_tree_list_vec (vec);
25184 release_tree_vector (vec);
25186 /* Save the arguments away. */
25187 TREE_VALUE (attribute) = arguments;
25189 else if (is_cilkplus_vector_p (identifier))
25191 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25192 continue;
25195 if (arguments != error_mark_node)
25197 /* Add this attribute to the list. */
25198 TREE_CHAIN (attribute) = attribute_list;
25199 attribute_list = attribute;
25202 token = cp_lexer_peek_token (parser->lexer);
25204 /* Now, look for more attributes. If the next token isn't a
25205 `,', we're done. */
25206 if (token->type != CPP_COMMA)
25207 break;
25209 /* Consume the comma and keep going. */
25210 cp_lexer_consume_token (parser->lexer);
25212 parser->translate_strings_p = save_translate_strings_p;
25214 /* We built up the list in reverse order. */
25215 return nreverse (attribute_list);
25218 /* Parse a standard C++11 attribute.
25220 The returned representation is a TREE_LIST which TREE_PURPOSE is
25221 the scoped name of the attribute, and the TREE_VALUE is its
25222 arguments list.
25224 Note that the scoped name of the attribute is itself a TREE_LIST
25225 which TREE_PURPOSE is the namespace of the attribute, and
25226 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25227 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25228 and which TREE_PURPOSE is directly the attribute name.
25230 Clients of the attribute code should use get_attribute_namespace
25231 and get_attribute_name to get the actual namespace and name of
25232 attributes, regardless of their being GNU or C++11 attributes.
25234 attribute:
25235 attribute-token attribute-argument-clause [opt]
25237 attribute-token:
25238 identifier
25239 attribute-scoped-token
25241 attribute-scoped-token:
25242 attribute-namespace :: identifier
25244 attribute-namespace:
25245 identifier
25247 attribute-argument-clause:
25248 ( balanced-token-seq )
25250 balanced-token-seq:
25251 balanced-token [opt]
25252 balanced-token-seq balanced-token
25254 balanced-token:
25255 ( balanced-token-seq )
25256 [ balanced-token-seq ]
25257 { balanced-token-seq }. */
25259 static tree
25260 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25262 tree attribute, attr_id = NULL_TREE, arguments;
25263 cp_token *token;
25265 /* First, parse name of the attribute, a.k.a attribute-token. */
25267 token = cp_lexer_peek_token (parser->lexer);
25268 if (token->type == CPP_NAME)
25269 attr_id = token->u.value;
25270 else if (token->type == CPP_KEYWORD)
25271 attr_id = ridpointers[(int) token->keyword];
25272 else if (token->flags & NAMED_OP)
25273 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25275 if (attr_id == NULL_TREE)
25276 return NULL_TREE;
25278 cp_lexer_consume_token (parser->lexer);
25280 token = cp_lexer_peek_token (parser->lexer);
25281 if (token->type == CPP_SCOPE)
25283 /* We are seeing a scoped attribute token. */
25285 cp_lexer_consume_token (parser->lexer);
25286 if (attr_ns)
25287 error_at (token->location, "attribute using prefix used together "
25288 "with scoped attribute token");
25289 attr_ns = attr_id;
25291 token = cp_lexer_consume_token (parser->lexer);
25292 if (token->type == CPP_NAME)
25293 attr_id = token->u.value;
25294 else if (token->type == CPP_KEYWORD)
25295 attr_id = ridpointers[(int) token->keyword];
25296 else if (token->flags & NAMED_OP)
25297 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25298 else
25300 error_at (token->location,
25301 "expected an identifier for the attribute name");
25302 return error_mark_node;
25305 attr_id = canonicalize_attr_name (attr_id);
25306 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25307 NULL_TREE);
25308 token = cp_lexer_peek_token (parser->lexer);
25310 else if (attr_ns)
25311 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25312 NULL_TREE);
25313 else
25315 attr_id = canonicalize_attr_name (attr_id);
25316 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25317 NULL_TREE);
25318 /* C++11 noreturn attribute is equivalent to GNU's. */
25319 if (is_attribute_p ("noreturn", attr_id))
25320 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25321 /* C++14 deprecated attribute is equivalent to GNU's. */
25322 else if (is_attribute_p ("deprecated", attr_id))
25323 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25324 /* C++17 fallthrough attribute is equivalent to GNU's. */
25325 else if (is_attribute_p ("fallthrough", attr_id))
25326 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25327 /* Transactional Memory TS optimize_for_synchronized attribute is
25328 equivalent to GNU transaction_callable. */
25329 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25330 TREE_PURPOSE (attribute)
25331 = get_identifier ("transaction_callable");
25332 /* Transactional Memory attributes are GNU attributes. */
25333 else if (tm_attr_to_mask (attr_id))
25334 TREE_PURPOSE (attribute) = attr_id;
25337 /* Now parse the optional argument clause of the attribute. */
25339 if (token->type != CPP_OPEN_PAREN)
25340 return attribute;
25343 vec<tree, va_gc> *vec;
25344 int attr_flag = normal_attr;
25346 if (attr_ns == get_identifier ("gnu")
25347 && attribute_takes_identifier_p (attr_id))
25348 /* A GNU attribute that takes an identifier in parameter. */
25349 attr_flag = id_attr;
25351 vec = cp_parser_parenthesized_expression_list
25352 (parser, attr_flag, /*cast_p=*/false,
25353 /*allow_expansion_p=*/true,
25354 /*non_constant_p=*/NULL);
25355 if (vec == NULL)
25356 arguments = error_mark_node;
25357 else
25359 arguments = build_tree_list_vec (vec);
25360 release_tree_vector (vec);
25363 if (arguments == error_mark_node)
25364 attribute = error_mark_node;
25365 else
25366 TREE_VALUE (attribute) = arguments;
25369 return attribute;
25372 /* Check that the attribute ATTRIBUTE appears at most once in the
25373 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25374 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25375 isn't implemented yet in GCC. */
25377 static void
25378 cp_parser_check_std_attribute (tree attributes, tree attribute)
25380 if (attributes)
25382 tree name = get_attribute_name (attribute);
25383 if (is_attribute_p ("noreturn", name)
25384 && lookup_attribute ("noreturn", attributes))
25385 error ("attribute %<noreturn%> can appear at most once "
25386 "in an attribute-list");
25387 else if (is_attribute_p ("deprecated", name)
25388 && lookup_attribute ("deprecated", attributes))
25389 error ("attribute %<deprecated%> can appear at most once "
25390 "in an attribute-list");
25394 /* Parse a list of standard C++-11 attributes.
25396 attribute-list:
25397 attribute [opt]
25398 attribute-list , attribute[opt]
25399 attribute ...
25400 attribute-list , attribute ...
25403 static tree
25404 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25406 tree attributes = NULL_TREE, attribute = NULL_TREE;
25407 cp_token *token = NULL;
25409 while (true)
25411 attribute = cp_parser_std_attribute (parser, attr_ns);
25412 if (attribute == error_mark_node)
25413 break;
25414 if (attribute != NULL_TREE)
25416 cp_parser_check_std_attribute (attributes, attribute);
25417 TREE_CHAIN (attribute) = attributes;
25418 attributes = attribute;
25420 token = cp_lexer_peek_token (parser->lexer);
25421 if (token->type == CPP_ELLIPSIS)
25423 cp_lexer_consume_token (parser->lexer);
25424 if (attribute == NULL_TREE)
25425 error_at (token->location,
25426 "expected attribute before %<...%>");
25427 else
25429 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25430 if (pack == error_mark_node)
25431 return error_mark_node;
25432 TREE_VALUE (attribute) = pack;
25434 token = cp_lexer_peek_token (parser->lexer);
25436 if (token->type != CPP_COMMA)
25437 break;
25438 cp_lexer_consume_token (parser->lexer);
25440 attributes = nreverse (attributes);
25441 return attributes;
25444 /* Parse a standard C++-11 attribute specifier.
25446 attribute-specifier:
25447 [ [ attribute-using-prefix [opt] attribute-list ] ]
25448 alignment-specifier
25450 attribute-using-prefix:
25451 using attribute-namespace :
25453 alignment-specifier:
25454 alignas ( type-id ... [opt] )
25455 alignas ( alignment-expression ... [opt] ). */
25457 static tree
25458 cp_parser_std_attribute_spec (cp_parser *parser)
25460 tree attributes = NULL_TREE;
25461 cp_token *token = cp_lexer_peek_token (parser->lexer);
25463 if (token->type == CPP_OPEN_SQUARE
25464 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25466 tree attr_ns = NULL_TREE;
25468 cp_lexer_consume_token (parser->lexer);
25469 cp_lexer_consume_token (parser->lexer);
25471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25473 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25474 if (token->type == CPP_NAME)
25475 attr_ns = token->u.value;
25476 else if (token->type == CPP_KEYWORD)
25477 attr_ns = ridpointers[(int) token->keyword];
25478 else if (token->flags & NAMED_OP)
25479 attr_ns = get_identifier (cpp_type2name (token->type,
25480 token->flags));
25481 if (attr_ns
25482 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25484 if (cxx_dialect < cxx17
25485 && !in_system_header_at (input_location))
25486 pedwarn (input_location, 0,
25487 "attribute using prefix only available "
25488 "with -std=c++17 or -std=gnu++17");
25490 cp_lexer_consume_token (parser->lexer);
25491 cp_lexer_consume_token (parser->lexer);
25492 cp_lexer_consume_token (parser->lexer);
25494 else
25495 attr_ns = NULL_TREE;
25498 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25500 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25501 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25502 cp_parser_skip_to_end_of_statement (parser);
25503 else
25504 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25505 when we are sure that we have actually parsed them. */
25506 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25508 else
25510 tree alignas_expr;
25512 /* Look for an alignment-specifier. */
25514 token = cp_lexer_peek_token (parser->lexer);
25516 if (token->type != CPP_KEYWORD
25517 || token->keyword != RID_ALIGNAS)
25518 return NULL_TREE;
25520 cp_lexer_consume_token (parser->lexer);
25521 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25523 matching_parens parens;
25524 if (!parens.require_open (parser))
25526 cp_parser_error (parser, "expected %<(%>");
25527 return error_mark_node;
25530 cp_parser_parse_tentatively (parser);
25531 alignas_expr = cp_parser_type_id (parser);
25533 if (!cp_parser_parse_definitely (parser))
25535 alignas_expr = cp_parser_assignment_expression (parser);
25536 if (alignas_expr == error_mark_node)
25537 cp_parser_skip_to_end_of_statement (parser);
25538 if (alignas_expr == NULL_TREE
25539 || alignas_expr == error_mark_node)
25540 return alignas_expr;
25543 alignas_expr = cxx_alignas_expr (alignas_expr);
25544 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25546 /* Handle alignas (pack...). */
25547 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25549 cp_lexer_consume_token (parser->lexer);
25550 alignas_expr = make_pack_expansion (alignas_expr);
25553 /* Something went wrong, so don't build the attribute. */
25554 if (alignas_expr == error_mark_node)
25555 return error_mark_node;
25557 if (!parens.require_close (parser))
25559 cp_parser_error (parser, "expected %<)%>");
25560 return error_mark_node;
25563 /* Build the C++-11 representation of an 'aligned'
25564 attribute. */
25565 attributes =
25566 build_tree_list (build_tree_list (get_identifier ("gnu"),
25567 get_identifier ("aligned")),
25568 alignas_expr);
25571 return attributes;
25574 /* Parse a standard C++-11 attribute-specifier-seq.
25576 attribute-specifier-seq:
25577 attribute-specifier-seq [opt] attribute-specifier
25580 static tree
25581 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25583 tree attr_specs = NULL_TREE;
25584 tree attr_last = NULL_TREE;
25586 while (true)
25588 tree attr_spec = cp_parser_std_attribute_spec (parser);
25589 if (attr_spec == NULL_TREE)
25590 break;
25591 if (attr_spec == error_mark_node)
25592 return error_mark_node;
25594 if (attr_last)
25595 TREE_CHAIN (attr_last) = attr_spec;
25596 else
25597 attr_specs = attr_last = attr_spec;
25598 attr_last = tree_last (attr_last);
25601 return attr_specs;
25604 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25605 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25606 current value of the PEDANTIC flag, regardless of whether or not
25607 the `__extension__' keyword is present. The caller is responsible
25608 for restoring the value of the PEDANTIC flag. */
25610 static bool
25611 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25613 /* Save the old value of the PEDANTIC flag. */
25614 *saved_pedantic = pedantic;
25616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25618 /* Consume the `__extension__' token. */
25619 cp_lexer_consume_token (parser->lexer);
25620 /* We're not being pedantic while the `__extension__' keyword is
25621 in effect. */
25622 pedantic = 0;
25624 return true;
25627 return false;
25630 /* Parse a label declaration.
25632 label-declaration:
25633 __label__ label-declarator-seq ;
25635 label-declarator-seq:
25636 identifier , label-declarator-seq
25637 identifier */
25639 static void
25640 cp_parser_label_declaration (cp_parser* parser)
25642 /* Look for the `__label__' keyword. */
25643 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25645 while (true)
25647 tree identifier;
25649 /* Look for an identifier. */
25650 identifier = cp_parser_identifier (parser);
25651 /* If we failed, stop. */
25652 if (identifier == error_mark_node)
25653 break;
25654 /* Declare it as a label. */
25655 finish_label_decl (identifier);
25656 /* If the next token is a `;', stop. */
25657 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25658 break;
25659 /* Look for the `,' separating the label declarations. */
25660 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25663 /* Look for the final `;'. */
25664 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25667 // -------------------------------------------------------------------------- //
25668 // Requires Clause
25670 // Parse a requires clause.
25672 // requires-clause:
25673 // 'requires' logical-or-expression
25675 // The required logical-or-expression must be a constant expression. Note
25676 // that we don't check that the expression is constepxr here. We defer until
25677 // we analyze constraints and then, we only check atomic constraints.
25678 static tree
25679 cp_parser_requires_clause (cp_parser *parser)
25681 // Parse the requires clause so that it is not automatically folded.
25682 ++processing_template_decl;
25683 tree expr = cp_parser_binary_expression (parser, false, false,
25684 PREC_NOT_OPERATOR, NULL);
25685 if (check_for_bare_parameter_packs (expr))
25686 expr = error_mark_node;
25687 --processing_template_decl;
25688 return expr;
25691 // Optionally parse a requires clause:
25692 static tree
25693 cp_parser_requires_clause_opt (cp_parser *parser)
25695 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25696 if (tok->keyword != RID_REQUIRES)
25698 if (!flag_concepts && tok->type == CPP_NAME
25699 && tok->u.value == ridpointers[RID_REQUIRES])
25701 error_at (cp_lexer_peek_token (parser->lexer)->location,
25702 "%<requires%> only available with -fconcepts");
25703 /* Parse and discard the requires-clause. */
25704 cp_lexer_consume_token (parser->lexer);
25705 cp_parser_requires_clause (parser);
25707 return NULL_TREE;
25709 cp_lexer_consume_token (parser->lexer);
25710 return cp_parser_requires_clause (parser);
25714 /*---------------------------------------------------------------------------
25715 Requires expressions
25716 ---------------------------------------------------------------------------*/
25718 /* Parse a requires expression
25720 requirement-expression:
25721 'requires' requirement-parameter-list [opt] requirement-body */
25722 static tree
25723 cp_parser_requires_expression (cp_parser *parser)
25725 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25726 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25728 /* A requires-expression shall appear only within a concept
25729 definition or a requires-clause.
25731 TODO: Implement this diagnostic correctly. */
25732 if (!processing_template_decl)
25734 error_at (loc, "a requires expression cannot appear outside a template");
25735 cp_parser_skip_to_end_of_statement (parser);
25736 return error_mark_node;
25739 tree parms, reqs;
25741 /* Local parameters are delared as variables within the scope
25742 of the expression. They are not visible past the end of
25743 the expression. Expressions within the requires-expression
25744 are unevaluated. */
25745 struct scope_sentinel
25747 scope_sentinel ()
25749 ++cp_unevaluated_operand;
25750 begin_scope (sk_block, NULL_TREE);
25753 ~scope_sentinel ()
25755 pop_bindings_and_leave_scope ();
25756 --cp_unevaluated_operand;
25758 } s;
25760 /* Parse the optional parameter list. */
25761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25763 parms = cp_parser_requirement_parameter_list (parser);
25764 if (parms == error_mark_node)
25765 return error_mark_node;
25767 else
25768 parms = NULL_TREE;
25770 /* Parse the requirement body. */
25771 reqs = cp_parser_requirement_body (parser);
25772 if (reqs == error_mark_node)
25773 return error_mark_node;
25776 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25777 the parm chain. */
25778 grokparms (parms, &parms);
25779 return finish_requires_expr (parms, reqs);
25782 /* Parse a parameterized requirement.
25784 requirement-parameter-list:
25785 '(' parameter-declaration-clause ')' */
25786 static tree
25787 cp_parser_requirement_parameter_list (cp_parser *parser)
25789 matching_parens parens;
25790 if (!parens.require_open (parser))
25791 return error_mark_node;
25793 tree parms = cp_parser_parameter_declaration_clause (parser);
25795 if (!parens.require_close (parser))
25796 return error_mark_node;
25798 return parms;
25801 /* Parse the body of a requirement.
25803 requirement-body:
25804 '{' requirement-list '}' */
25805 static tree
25806 cp_parser_requirement_body (cp_parser *parser)
25808 matching_braces braces;
25809 if (!braces.require_open (parser))
25810 return error_mark_node;
25812 tree reqs = cp_parser_requirement_list (parser);
25814 if (!braces.require_close (parser))
25815 return error_mark_node;
25817 return reqs;
25820 /* Parse a list of requirements.
25822 requirement-list:
25823 requirement
25824 requirement-list ';' requirement[opt] */
25825 static tree
25826 cp_parser_requirement_list (cp_parser *parser)
25828 tree result = NULL_TREE;
25829 while (true)
25831 tree req = cp_parser_requirement (parser);
25832 if (req == error_mark_node)
25833 return error_mark_node;
25835 result = tree_cons (NULL_TREE, req, result);
25837 /* If we see a semi-colon, consume it. */
25838 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25839 cp_lexer_consume_token (parser->lexer);
25841 /* Stop processing at the end of the list. */
25842 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25843 break;
25846 /* Reverse the order of requirements so they are analyzed in
25847 declaration order. */
25848 return nreverse (result);
25851 /* Parse a syntactic requirement or type requirement.
25853 requirement:
25854 simple-requirement
25855 compound-requirement
25856 type-requirement
25857 nested-requirement */
25858 static tree
25859 cp_parser_requirement (cp_parser *parser)
25861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25862 return cp_parser_compound_requirement (parser);
25863 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25864 return cp_parser_type_requirement (parser);
25865 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25866 return cp_parser_nested_requirement (parser);
25867 else
25868 return cp_parser_simple_requirement (parser);
25871 /* Parse a simple requirement.
25873 simple-requirement:
25874 expression ';' */
25875 static tree
25876 cp_parser_simple_requirement (cp_parser *parser)
25878 tree expr = cp_parser_expression (parser, NULL, false, false);
25879 if (!expr || expr == error_mark_node)
25880 return error_mark_node;
25882 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25883 return error_mark_node;
25885 return finish_simple_requirement (expr);
25888 /* Parse a type requirement
25890 type-requirement
25891 nested-name-specifier [opt] required-type-name ';'
25893 required-type-name:
25894 type-name
25895 'template' [opt] simple-template-id */
25896 static tree
25897 cp_parser_type_requirement (cp_parser *parser)
25899 cp_lexer_consume_token (parser->lexer);
25901 // Save the scope before parsing name specifiers.
25902 tree saved_scope = parser->scope;
25903 tree saved_object_scope = parser->object_scope;
25904 tree saved_qualifying_scope = parser->qualifying_scope;
25905 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25906 cp_parser_nested_name_specifier_opt (parser,
25907 /*typename_keyword_p=*/true,
25908 /*check_dependency_p=*/false,
25909 /*type_p=*/true,
25910 /*is_declaration=*/false);
25912 tree type;
25913 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25915 cp_lexer_consume_token (parser->lexer);
25916 type = cp_parser_template_id (parser,
25917 /*template_keyword_p=*/true,
25918 /*check_dependency=*/false,
25919 /*tag_type=*/none_type,
25920 /*is_declaration=*/false);
25921 type = make_typename_type (parser->scope, type, typename_type,
25922 /*complain=*/tf_error);
25924 else
25925 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25927 if (TREE_CODE (type) == TYPE_DECL)
25928 type = TREE_TYPE (type);
25930 parser->scope = saved_scope;
25931 parser->object_scope = saved_object_scope;
25932 parser->qualifying_scope = saved_qualifying_scope;
25934 if (type == error_mark_node)
25935 cp_parser_skip_to_end_of_statement (parser);
25937 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25938 return error_mark_node;
25939 if (type == error_mark_node)
25940 return error_mark_node;
25942 return finish_type_requirement (type);
25945 /* Parse a compound requirement
25947 compound-requirement:
25948 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25949 static tree
25950 cp_parser_compound_requirement (cp_parser *parser)
25952 /* Parse an expression enclosed in '{ }'s. */
25953 matching_braces braces;
25954 if (!braces.require_open (parser))
25955 return error_mark_node;
25957 tree expr = cp_parser_expression (parser, NULL, false, false);
25958 if (!expr || expr == error_mark_node)
25959 return error_mark_node;
25961 if (!braces.require_close (parser))
25962 return error_mark_node;
25964 /* Parse the optional noexcept. */
25965 bool noexcept_p = false;
25966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25968 cp_lexer_consume_token (parser->lexer);
25969 noexcept_p = true;
25972 /* Parse the optional trailing return type. */
25973 tree type = NULL_TREE;
25974 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25976 cp_lexer_consume_token (parser->lexer);
25977 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25978 parser->in_result_type_constraint_p = true;
25979 type = cp_parser_trailing_type_id (parser);
25980 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25981 if (type == error_mark_node)
25982 return error_mark_node;
25985 return finish_compound_requirement (expr, type, noexcept_p);
25988 /* Parse a nested requirement. This is the same as a requires clause.
25990 nested-requirement:
25991 requires-clause */
25992 static tree
25993 cp_parser_nested_requirement (cp_parser *parser)
25995 cp_lexer_consume_token (parser->lexer);
25996 tree req = cp_parser_requires_clause (parser);
25997 if (req == error_mark_node)
25998 return error_mark_node;
25999 return finish_nested_requirement (req);
26002 /* Support Functions */
26004 /* Return the appropriate prefer_type argument for lookup_name_real based on
26005 tag_type and template_mem_access. */
26007 static inline int
26008 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26010 /* DR 141: When looking in the current enclosing context for a template-name
26011 after -> or ., only consider class templates. */
26012 if (template_mem_access)
26013 return 2;
26014 switch (tag_type)
26016 case none_type: return 0; // No preference.
26017 case scope_type: return 1; // Type or namespace.
26018 default: return 2; // Type only.
26022 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26023 NAME should have one of the representations used for an
26024 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26025 is returned. If PARSER->SCOPE is a dependent type, then a
26026 SCOPE_REF is returned.
26028 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26029 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26030 was formed. Abstractly, such entities should not be passed to this
26031 function, because they do not need to be looked up, but it is
26032 simpler to check for this special case here, rather than at the
26033 call-sites.
26035 In cases not explicitly covered above, this function returns a
26036 DECL, OVERLOAD, or baselink representing the result of the lookup.
26037 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26038 is returned.
26040 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26041 (e.g., "struct") that was used. In that case bindings that do not
26042 refer to types are ignored.
26044 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26045 ignored.
26047 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26048 are ignored.
26050 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26051 types.
26053 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26054 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26055 NULL_TREE otherwise. */
26057 static cp_expr
26058 cp_parser_lookup_name (cp_parser *parser, tree name,
26059 enum tag_types tag_type,
26060 bool is_template,
26061 bool is_namespace,
26062 bool check_dependency,
26063 tree *ambiguous_decls,
26064 location_t name_location)
26066 tree decl;
26067 tree object_type = parser->context->object_type;
26069 /* Assume that the lookup will be unambiguous. */
26070 if (ambiguous_decls)
26071 *ambiguous_decls = NULL_TREE;
26073 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26074 no longer valid. Note that if we are parsing tentatively, and
26075 the parse fails, OBJECT_TYPE will be automatically restored. */
26076 parser->context->object_type = NULL_TREE;
26078 if (name == error_mark_node)
26079 return error_mark_node;
26081 /* A template-id has already been resolved; there is no lookup to
26082 do. */
26083 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26084 return name;
26085 if (BASELINK_P (name))
26087 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26088 == TEMPLATE_ID_EXPR);
26089 return name;
26092 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26093 it should already have been checked to make sure that the name
26094 used matches the type being destroyed. */
26095 if (TREE_CODE (name) == BIT_NOT_EXPR)
26097 tree type;
26099 /* Figure out to which type this destructor applies. */
26100 if (parser->scope)
26101 type = parser->scope;
26102 else if (object_type)
26103 type = object_type;
26104 else
26105 type = current_class_type;
26106 /* If that's not a class type, there is no destructor. */
26107 if (!type || !CLASS_TYPE_P (type))
26108 return error_mark_node;
26110 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26111 lazily_declare_fn (sfk_destructor, type);
26113 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26114 return dtor;
26116 return error_mark_node;
26119 /* By this point, the NAME should be an ordinary identifier. If
26120 the id-expression was a qualified name, the qualifying scope is
26121 stored in PARSER->SCOPE at this point. */
26122 gcc_assert (identifier_p (name));
26124 /* Perform the lookup. */
26125 if (parser->scope)
26127 bool dependent_p;
26129 if (parser->scope == error_mark_node)
26130 return error_mark_node;
26132 /* If the SCOPE is dependent, the lookup must be deferred until
26133 the template is instantiated -- unless we are explicitly
26134 looking up names in uninstantiated templates. Even then, we
26135 cannot look up the name if the scope is not a class type; it
26136 might, for example, be a template type parameter. */
26137 dependent_p = (TYPE_P (parser->scope)
26138 && dependent_scope_p (parser->scope));
26139 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26140 && dependent_p)
26141 /* Defer lookup. */
26142 decl = error_mark_node;
26143 else
26145 tree pushed_scope = NULL_TREE;
26147 /* If PARSER->SCOPE is a dependent type, then it must be a
26148 class type, and we must not be checking dependencies;
26149 otherwise, we would have processed this lookup above. So
26150 that PARSER->SCOPE is not considered a dependent base by
26151 lookup_member, we must enter the scope here. */
26152 if (dependent_p)
26153 pushed_scope = push_scope (parser->scope);
26155 /* If the PARSER->SCOPE is a template specialization, it
26156 may be instantiated during name lookup. In that case,
26157 errors may be issued. Even if we rollback the current
26158 tentative parse, those errors are valid. */
26159 decl = lookup_qualified_name (parser->scope, name,
26160 prefer_type_arg (tag_type),
26161 /*complain=*/true);
26163 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26164 lookup result and the nested-name-specifier nominates a class C:
26165 * if the name specified after the nested-name-specifier, when
26166 looked up in C, is the injected-class-name of C (Clause 9), or
26167 * if the name specified after the nested-name-specifier is the
26168 same as the identifier or the simple-template-id's template-
26169 name in the last component of the nested-name-specifier,
26170 the name is instead considered to name the constructor of
26171 class C. [ Note: for example, the constructor is not an
26172 acceptable lookup result in an elaborated-type-specifier so
26173 the constructor would not be used in place of the
26174 injected-class-name. --end note ] Such a constructor name
26175 shall be used only in the declarator-id of a declaration that
26176 names a constructor or in a using-declaration. */
26177 if (tag_type == none_type
26178 && DECL_SELF_REFERENCE_P (decl)
26179 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26180 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26181 prefer_type_arg (tag_type),
26182 /*complain=*/true);
26184 /* If we have a single function from a using decl, pull it out. */
26185 if (TREE_CODE (decl) == OVERLOAD
26186 && !really_overloaded_fn (decl))
26187 decl = OVL_FUNCTION (decl);
26189 if (pushed_scope)
26190 pop_scope (pushed_scope);
26193 /* If the scope is a dependent type and either we deferred lookup or
26194 we did lookup but didn't find the name, rememeber the name. */
26195 if (decl == error_mark_node && TYPE_P (parser->scope)
26196 && dependent_type_p (parser->scope))
26198 if (tag_type)
26200 tree type;
26202 /* The resolution to Core Issue 180 says that `struct
26203 A::B' should be considered a type-name, even if `A'
26204 is dependent. */
26205 type = make_typename_type (parser->scope, name, tag_type,
26206 /*complain=*/tf_error);
26207 if (type != error_mark_node)
26208 decl = TYPE_NAME (type);
26210 else if (is_template
26211 && (cp_parser_next_token_ends_template_argument_p (parser)
26212 || cp_lexer_next_token_is (parser->lexer,
26213 CPP_CLOSE_PAREN)))
26214 decl = make_unbound_class_template (parser->scope,
26215 name, NULL_TREE,
26216 /*complain=*/tf_error);
26217 else
26218 decl = build_qualified_name (/*type=*/NULL_TREE,
26219 parser->scope, name,
26220 is_template);
26222 parser->qualifying_scope = parser->scope;
26223 parser->object_scope = NULL_TREE;
26225 else if (object_type)
26227 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26228 OBJECT_TYPE is not a class. */
26229 if (CLASS_TYPE_P (object_type))
26230 /* If the OBJECT_TYPE is a template specialization, it may
26231 be instantiated during name lookup. In that case, errors
26232 may be issued. Even if we rollback the current tentative
26233 parse, those errors are valid. */
26234 decl = lookup_member (object_type,
26235 name,
26236 /*protect=*/0,
26237 prefer_type_arg (tag_type),
26238 tf_warning_or_error);
26239 else
26240 decl = NULL_TREE;
26242 if (!decl)
26243 /* Look it up in the enclosing context. DR 141: When looking for a
26244 template-name after -> or ., only consider class templates. */
26245 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26246 /*nonclass=*/0,
26247 /*block_p=*/true, is_namespace, 0);
26248 if (object_type == unknown_type_node)
26249 /* The object is type-dependent, so we can't look anything up; we used
26250 this to get the DR 141 behavior. */
26251 object_type = NULL_TREE;
26252 parser->object_scope = object_type;
26253 parser->qualifying_scope = NULL_TREE;
26255 else
26257 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26258 /*nonclass=*/0,
26259 /*block_p=*/true, is_namespace, 0);
26260 parser->qualifying_scope = NULL_TREE;
26261 parser->object_scope = NULL_TREE;
26264 /* If the lookup failed, let our caller know. */
26265 if (!decl || decl == error_mark_node)
26266 return error_mark_node;
26268 /* Pull out the template from an injected-class-name (or multiple). */
26269 if (is_template)
26270 decl = maybe_get_template_decl_from_type_decl (decl);
26272 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26273 if (TREE_CODE (decl) == TREE_LIST)
26275 if (ambiguous_decls)
26276 *ambiguous_decls = decl;
26277 /* The error message we have to print is too complicated for
26278 cp_parser_error, so we incorporate its actions directly. */
26279 if (!cp_parser_simulate_error (parser))
26281 error_at (name_location, "reference to %qD is ambiguous",
26282 name);
26283 print_candidates (decl);
26285 return error_mark_node;
26288 gcc_assert (DECL_P (decl)
26289 || TREE_CODE (decl) == OVERLOAD
26290 || TREE_CODE (decl) == SCOPE_REF
26291 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26292 || BASELINK_P (decl));
26294 /* If we have resolved the name of a member declaration, check to
26295 see if the declaration is accessible. When the name resolves to
26296 set of overloaded functions, accessibility is checked when
26297 overload resolution is done.
26299 During an explicit instantiation, access is not checked at all,
26300 as per [temp.explicit]. */
26301 if (DECL_P (decl))
26302 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26304 maybe_record_typedef_use (decl);
26306 return cp_expr (decl, name_location);
26309 /* Like cp_parser_lookup_name, but for use in the typical case where
26310 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26311 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26313 static tree
26314 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26316 return cp_parser_lookup_name (parser, name,
26317 none_type,
26318 /*is_template=*/false,
26319 /*is_namespace=*/false,
26320 /*check_dependency=*/true,
26321 /*ambiguous_decls=*/NULL,
26322 location);
26325 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26326 the current context, return the TYPE_DECL. If TAG_NAME_P is
26327 true, the DECL indicates the class being defined in a class-head,
26328 or declared in an elaborated-type-specifier.
26330 Otherwise, return DECL. */
26332 static tree
26333 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26335 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26336 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26338 struct A {
26339 template <typename T> struct B;
26342 template <typename T> struct A::B {};
26344 Similarly, in an elaborated-type-specifier:
26346 namespace N { struct X{}; }
26348 struct A {
26349 template <typename T> friend struct N::X;
26352 However, if the DECL refers to a class type, and we are in
26353 the scope of the class, then the name lookup automatically
26354 finds the TYPE_DECL created by build_self_reference rather
26355 than a TEMPLATE_DECL. For example, in:
26357 template <class T> struct S {
26358 S s;
26361 there is no need to handle such case. */
26363 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26364 return DECL_TEMPLATE_RESULT (decl);
26366 return decl;
26369 /* If too many, or too few, template-parameter lists apply to the
26370 declarator, issue an error message. Returns TRUE if all went well,
26371 and FALSE otherwise. */
26373 static bool
26374 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26375 cp_declarator *declarator,
26376 location_t declarator_location)
26378 switch (declarator->kind)
26380 case cdk_id:
26382 unsigned num_templates = 0;
26383 tree scope = declarator->u.id.qualifying_scope;
26385 if (scope)
26386 num_templates = num_template_headers_for_class (scope);
26387 else if (TREE_CODE (declarator->u.id.unqualified_name)
26388 == TEMPLATE_ID_EXPR)
26389 /* If the DECLARATOR has the form `X<y>' then it uses one
26390 additional level of template parameters. */
26391 ++num_templates;
26393 return cp_parser_check_template_parameters
26394 (parser, num_templates, declarator_location, declarator);
26397 case cdk_function:
26398 case cdk_array:
26399 case cdk_pointer:
26400 case cdk_reference:
26401 case cdk_ptrmem:
26402 return (cp_parser_check_declarator_template_parameters
26403 (parser, declarator->declarator, declarator_location));
26405 case cdk_decomp:
26406 case cdk_error:
26407 return true;
26409 default:
26410 gcc_unreachable ();
26412 return false;
26415 /* NUM_TEMPLATES were used in the current declaration. If that is
26416 invalid, return FALSE and issue an error messages. Otherwise,
26417 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26418 declarator and we can print more accurate diagnostics. */
26420 static bool
26421 cp_parser_check_template_parameters (cp_parser* parser,
26422 unsigned num_templates,
26423 location_t location,
26424 cp_declarator *declarator)
26426 /* If there are the same number of template classes and parameter
26427 lists, that's OK. */
26428 if (parser->num_template_parameter_lists == num_templates)
26429 return true;
26430 /* If there are more, but only one more, then we are referring to a
26431 member template. That's OK too. */
26432 if (parser->num_template_parameter_lists == num_templates + 1)
26433 return true;
26434 /* If there are more template classes than parameter lists, we have
26435 something like:
26437 template <class T> void S<T>::R<T>::f (); */
26438 if (parser->num_template_parameter_lists < num_templates)
26440 if (declarator && !current_function_decl)
26441 error_at (location, "specializing member %<%T::%E%> "
26442 "requires %<template<>%> syntax",
26443 declarator->u.id.qualifying_scope,
26444 declarator->u.id.unqualified_name);
26445 else if (declarator)
26446 error_at (location, "invalid declaration of %<%T::%E%>",
26447 declarator->u.id.qualifying_scope,
26448 declarator->u.id.unqualified_name);
26449 else
26450 error_at (location, "too few template-parameter-lists");
26451 return false;
26453 /* Otherwise, there are too many template parameter lists. We have
26454 something like:
26456 template <class T> template <class U> void S::f(); */
26457 error_at (location, "too many template-parameter-lists");
26458 return false;
26461 /* Parse an optional `::' token indicating that the following name is
26462 from the global namespace. If so, PARSER->SCOPE is set to the
26463 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26464 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26465 Returns the new value of PARSER->SCOPE, if the `::' token is
26466 present, and NULL_TREE otherwise. */
26468 static tree
26469 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26471 cp_token *token;
26473 /* Peek at the next token. */
26474 token = cp_lexer_peek_token (parser->lexer);
26475 /* If we're looking at a `::' token then we're starting from the
26476 global namespace, not our current location. */
26477 if (token->type == CPP_SCOPE)
26479 /* Consume the `::' token. */
26480 cp_lexer_consume_token (parser->lexer);
26481 /* Set the SCOPE so that we know where to start the lookup. */
26482 parser->scope = global_namespace;
26483 parser->qualifying_scope = global_namespace;
26484 parser->object_scope = NULL_TREE;
26486 return parser->scope;
26488 else if (!current_scope_valid_p)
26490 parser->scope = NULL_TREE;
26491 parser->qualifying_scope = NULL_TREE;
26492 parser->object_scope = NULL_TREE;
26495 return NULL_TREE;
26498 /* Returns TRUE if the upcoming token sequence is the start of a
26499 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26500 declarator is preceded by the `friend' specifier. */
26502 static bool
26503 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26505 bool constructor_p;
26506 bool outside_class_specifier_p;
26507 tree nested_name_specifier;
26508 cp_token *next_token;
26510 /* The common case is that this is not a constructor declarator, so
26511 try to avoid doing lots of work if at all possible. It's not
26512 valid declare a constructor at function scope. */
26513 if (parser->in_function_body)
26514 return false;
26515 /* And only certain tokens can begin a constructor declarator. */
26516 next_token = cp_lexer_peek_token (parser->lexer);
26517 if (next_token->type != CPP_NAME
26518 && next_token->type != CPP_SCOPE
26519 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26520 && next_token->type != CPP_TEMPLATE_ID)
26521 return false;
26523 /* Parse tentatively; we are going to roll back all of the tokens
26524 consumed here. */
26525 cp_parser_parse_tentatively (parser);
26526 /* Assume that we are looking at a constructor declarator. */
26527 constructor_p = true;
26529 /* Look for the optional `::' operator. */
26530 cp_parser_global_scope_opt (parser,
26531 /*current_scope_valid_p=*/false);
26532 /* Look for the nested-name-specifier. */
26533 nested_name_specifier
26534 = (cp_parser_nested_name_specifier_opt (parser,
26535 /*typename_keyword_p=*/false,
26536 /*check_dependency_p=*/false,
26537 /*type_p=*/false,
26538 /*is_declaration=*/false));
26540 outside_class_specifier_p = (!at_class_scope_p ()
26541 || !TYPE_BEING_DEFINED (current_class_type)
26542 || friend_p);
26544 /* Outside of a class-specifier, there must be a
26545 nested-name-specifier. Except in C++17 mode, where we
26546 might be declaring a guiding declaration. */
26547 if (!nested_name_specifier && outside_class_specifier_p
26548 && cxx_dialect < cxx17)
26549 constructor_p = false;
26550 else if (nested_name_specifier == error_mark_node)
26551 constructor_p = false;
26553 /* If we have a class scope, this is easy; DR 147 says that S::S always
26554 names the constructor, and no other qualified name could. */
26555 if (constructor_p && nested_name_specifier
26556 && CLASS_TYPE_P (nested_name_specifier))
26558 tree id = cp_parser_unqualified_id (parser,
26559 /*template_keyword_p=*/false,
26560 /*check_dependency_p=*/false,
26561 /*declarator_p=*/true,
26562 /*optional_p=*/false);
26563 if (is_overloaded_fn (id))
26564 id = DECL_NAME (get_first_fn (id));
26565 if (!constructor_name_p (id, nested_name_specifier))
26566 constructor_p = false;
26568 /* If we still think that this might be a constructor-declarator,
26569 look for a class-name. */
26570 else if (constructor_p)
26572 /* If we have:
26574 template <typename T> struct S {
26575 S();
26578 we must recognize that the nested `S' names a class. */
26579 if (cxx_dialect >= cxx17)
26580 cp_parser_parse_tentatively (parser);
26582 tree type_decl;
26583 type_decl = cp_parser_class_name (parser,
26584 /*typename_keyword_p=*/false,
26585 /*template_keyword_p=*/false,
26586 none_type,
26587 /*check_dependency_p=*/false,
26588 /*class_head_p=*/false,
26589 /*is_declaration=*/false);
26591 if (cxx_dialect >= cxx17
26592 && !cp_parser_parse_definitely (parser))
26594 type_decl = NULL_TREE;
26595 tree tmpl = cp_parser_template_name (parser,
26596 /*template_keyword*/false,
26597 /*check_dependency_p*/false,
26598 /*is_declaration*/false,
26599 none_type,
26600 /*is_identifier*/NULL);
26601 if (DECL_CLASS_TEMPLATE_P (tmpl)
26602 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26603 /* It's a deduction guide, return true. */;
26604 else
26605 cp_parser_simulate_error (parser);
26608 /* If there was no class-name, then this is not a constructor.
26609 Otherwise, if we are in a class-specifier and we aren't
26610 handling a friend declaration, check that its type matches
26611 current_class_type (c++/38313). Note: error_mark_node
26612 is left alone for error recovery purposes. */
26613 constructor_p = (!cp_parser_error_occurred (parser)
26614 && (outside_class_specifier_p
26615 || type_decl == NULL_TREE
26616 || type_decl == error_mark_node
26617 || same_type_p (current_class_type,
26618 TREE_TYPE (type_decl))));
26620 /* If we're still considering a constructor, we have to see a `(',
26621 to begin the parameter-declaration-clause, followed by either a
26622 `)', an `...', or a decl-specifier. We need to check for a
26623 type-specifier to avoid being fooled into thinking that:
26625 S (f) (int);
26627 is a constructor. (It is actually a function named `f' that
26628 takes one parameter (of type `int') and returns a value of type
26629 `S'. */
26630 if (constructor_p
26631 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26632 constructor_p = false;
26634 if (constructor_p
26635 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26636 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26637 /* A parameter declaration begins with a decl-specifier,
26638 which is either the "attribute" keyword, a storage class
26639 specifier, or (usually) a type-specifier. */
26640 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26642 tree type;
26643 tree pushed_scope = NULL_TREE;
26644 unsigned saved_num_template_parameter_lists;
26646 /* Names appearing in the type-specifier should be looked up
26647 in the scope of the class. */
26648 if (current_class_type)
26649 type = NULL_TREE;
26650 else if (type_decl)
26652 type = TREE_TYPE (type_decl);
26653 if (TREE_CODE (type) == TYPENAME_TYPE)
26655 type = resolve_typename_type (type,
26656 /*only_current_p=*/false);
26657 if (TREE_CODE (type) == TYPENAME_TYPE)
26659 cp_parser_abort_tentative_parse (parser);
26660 return false;
26663 pushed_scope = push_scope (type);
26666 /* Inside the constructor parameter list, surrounding
26667 template-parameter-lists do not apply. */
26668 saved_num_template_parameter_lists
26669 = parser->num_template_parameter_lists;
26670 parser->num_template_parameter_lists = 0;
26672 /* Look for the type-specifier. */
26673 cp_parser_type_specifier (parser,
26674 CP_PARSER_FLAGS_NONE,
26675 /*decl_specs=*/NULL,
26676 /*is_declarator=*/true,
26677 /*declares_class_or_enum=*/NULL,
26678 /*is_cv_qualifier=*/NULL);
26680 parser->num_template_parameter_lists
26681 = saved_num_template_parameter_lists;
26683 /* Leave the scope of the class. */
26684 if (pushed_scope)
26685 pop_scope (pushed_scope);
26687 constructor_p = !cp_parser_error_occurred (parser);
26691 /* We did not really want to consume any tokens. */
26692 cp_parser_abort_tentative_parse (parser);
26694 return constructor_p;
26697 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26698 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26699 they must be performed once we are in the scope of the function.
26701 Returns the function defined. */
26703 static tree
26704 cp_parser_function_definition_from_specifiers_and_declarator
26705 (cp_parser* parser,
26706 cp_decl_specifier_seq *decl_specifiers,
26707 tree attributes,
26708 const cp_declarator *declarator)
26710 tree fn;
26711 bool success_p;
26713 /* Begin the function-definition. */
26714 success_p = start_function (decl_specifiers, declarator, attributes);
26716 /* The things we're about to see are not directly qualified by any
26717 template headers we've seen thus far. */
26718 reset_specialization ();
26720 /* If there were names looked up in the decl-specifier-seq that we
26721 did not check, check them now. We must wait until we are in the
26722 scope of the function to perform the checks, since the function
26723 might be a friend. */
26724 perform_deferred_access_checks (tf_warning_or_error);
26726 if (success_p)
26728 cp_finalize_omp_declare_simd (parser, current_function_decl);
26729 parser->omp_declare_simd = NULL;
26730 cp_finalize_oacc_routine (parser, current_function_decl, true);
26731 parser->oacc_routine = NULL;
26734 if (!success_p)
26736 /* Skip the entire function. */
26737 cp_parser_skip_to_end_of_block_or_statement (parser);
26738 fn = error_mark_node;
26740 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26742 /* Seen already, skip it. An error message has already been output. */
26743 cp_parser_skip_to_end_of_block_or_statement (parser);
26744 fn = current_function_decl;
26745 current_function_decl = NULL_TREE;
26746 /* If this is a function from a class, pop the nested class. */
26747 if (current_class_name)
26748 pop_nested_class ();
26750 else
26752 timevar_id_t tv;
26753 if (DECL_DECLARED_INLINE_P (current_function_decl))
26754 tv = TV_PARSE_INLINE;
26755 else
26756 tv = TV_PARSE_FUNC;
26757 timevar_push (tv);
26758 fn = cp_parser_function_definition_after_declarator (parser,
26759 /*inline_p=*/false);
26760 timevar_pop (tv);
26763 return fn;
26766 /* Parse the part of a function-definition that follows the
26767 declarator. INLINE_P is TRUE iff this function is an inline
26768 function defined within a class-specifier.
26770 Returns the function defined. */
26772 static tree
26773 cp_parser_function_definition_after_declarator (cp_parser* parser,
26774 bool inline_p)
26776 tree fn;
26777 bool saved_in_unbraced_linkage_specification_p;
26778 bool saved_in_function_body;
26779 unsigned saved_num_template_parameter_lists;
26780 cp_token *token;
26781 bool fully_implicit_function_template_p
26782 = parser->fully_implicit_function_template_p;
26783 parser->fully_implicit_function_template_p = false;
26784 tree implicit_template_parms
26785 = parser->implicit_template_parms;
26786 parser->implicit_template_parms = 0;
26787 cp_binding_level* implicit_template_scope
26788 = parser->implicit_template_scope;
26789 parser->implicit_template_scope = 0;
26791 saved_in_function_body = parser->in_function_body;
26792 parser->in_function_body = true;
26793 /* If the next token is `return', then the code may be trying to
26794 make use of the "named return value" extension that G++ used to
26795 support. */
26796 token = cp_lexer_peek_token (parser->lexer);
26797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26799 /* Consume the `return' keyword. */
26800 cp_lexer_consume_token (parser->lexer);
26801 /* Look for the identifier that indicates what value is to be
26802 returned. */
26803 cp_parser_identifier (parser);
26804 /* Issue an error message. */
26805 error_at (token->location,
26806 "named return values are no longer supported");
26807 /* Skip tokens until we reach the start of the function body. */
26808 while (true)
26810 cp_token *token = cp_lexer_peek_token (parser->lexer);
26811 if (token->type == CPP_OPEN_BRACE
26812 || token->type == CPP_EOF
26813 || token->type == CPP_PRAGMA_EOL)
26814 break;
26815 cp_lexer_consume_token (parser->lexer);
26818 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26819 anything declared inside `f'. */
26820 saved_in_unbraced_linkage_specification_p
26821 = parser->in_unbraced_linkage_specification_p;
26822 parser->in_unbraced_linkage_specification_p = false;
26823 /* Inside the function, surrounding template-parameter-lists do not
26824 apply. */
26825 saved_num_template_parameter_lists
26826 = parser->num_template_parameter_lists;
26827 parser->num_template_parameter_lists = 0;
26829 /* If the next token is `try', `__transaction_atomic', or
26830 `__transaction_relaxed`, then we are looking at either function-try-block
26831 or function-transaction-block. Note that all of these include the
26832 function-body. */
26833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26834 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26835 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26836 RID_TRANSACTION_RELAXED))
26837 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26838 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26839 cp_parser_function_try_block (parser);
26840 else
26841 cp_parser_ctor_initializer_opt_and_function_body
26842 (parser, /*in_function_try_block=*/false);
26844 /* Finish the function. */
26845 fn = finish_function (inline_p);
26846 /* Generate code for it, if necessary. */
26847 expand_or_defer_fn (fn);
26848 /* Restore the saved values. */
26849 parser->in_unbraced_linkage_specification_p
26850 = saved_in_unbraced_linkage_specification_p;
26851 parser->num_template_parameter_lists
26852 = saved_num_template_parameter_lists;
26853 parser->in_function_body = saved_in_function_body;
26855 parser->fully_implicit_function_template_p
26856 = fully_implicit_function_template_p;
26857 parser->implicit_template_parms
26858 = implicit_template_parms;
26859 parser->implicit_template_scope
26860 = implicit_template_scope;
26862 if (parser->fully_implicit_function_template_p)
26863 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26865 return fn;
26868 /* Parse a template-declaration body (following argument list). */
26870 static void
26871 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26872 tree parameter_list,
26873 bool member_p)
26875 tree decl = NULL_TREE;
26876 bool friend_p = false;
26878 /* We just processed one more parameter list. */
26879 ++parser->num_template_parameter_lists;
26881 /* Get the deferred access checks from the parameter list. These
26882 will be checked once we know what is being declared, as for a
26883 member template the checks must be performed in the scope of the
26884 class containing the member. */
26885 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26887 /* Tentatively parse for a new template parameter list, which can either be
26888 the template keyword or a template introduction. */
26889 if (cp_parser_template_declaration_after_export (parser, member_p))
26890 /* OK */;
26891 else if (cxx_dialect >= cxx11
26892 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26893 decl = cp_parser_alias_declaration (parser);
26894 else
26896 /* There are no access checks when parsing a template, as we do not
26897 know if a specialization will be a friend. */
26898 push_deferring_access_checks (dk_no_check);
26899 cp_token *token = cp_lexer_peek_token (parser->lexer);
26900 decl = cp_parser_single_declaration (parser,
26901 checks,
26902 member_p,
26903 /*explicit_specialization_p=*/false,
26904 &friend_p);
26905 pop_deferring_access_checks ();
26907 /* If this is a member template declaration, let the front
26908 end know. */
26909 if (member_p && !friend_p && decl)
26911 if (TREE_CODE (decl) == TYPE_DECL)
26912 cp_parser_check_access_in_redeclaration (decl, token->location);
26914 decl = finish_member_template_decl (decl);
26916 else if (friend_p && decl
26917 && DECL_DECLARES_TYPE_P (decl))
26918 make_friend_class (current_class_type, TREE_TYPE (decl),
26919 /*complain=*/true);
26921 /* We are done with the current parameter list. */
26922 --parser->num_template_parameter_lists;
26924 pop_deferring_access_checks ();
26926 /* Finish up. */
26927 finish_template_decl (parameter_list);
26929 /* Check the template arguments for a literal operator template. */
26930 if (decl
26931 && DECL_DECLARES_FUNCTION_P (decl)
26932 && UDLIT_OPER_P (DECL_NAME (decl)))
26934 bool ok = true;
26935 if (parameter_list == NULL_TREE)
26936 ok = false;
26937 else
26939 int num_parms = TREE_VEC_LENGTH (parameter_list);
26940 if (num_parms == 1)
26942 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26943 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26944 if (TREE_TYPE (parm) != char_type_node
26945 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26946 ok = false;
26948 else if (num_parms == 2 && cxx_dialect >= cxx14)
26950 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26951 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26952 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26953 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26954 if (parm == error_mark_node
26955 || TREE_TYPE (parm) != TREE_TYPE (type)
26956 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26957 ok = false;
26959 else
26960 ok = false;
26962 if (!ok)
26964 if (cxx_dialect >= cxx14)
26965 error ("literal operator template %qD has invalid parameter list."
26966 " Expected non-type template argument pack <char...>"
26967 " or <typename CharT, CharT...>",
26968 decl);
26969 else
26970 error ("literal operator template %qD has invalid parameter list."
26971 " Expected non-type template argument pack <char...>",
26972 decl);
26976 /* Register member declarations. */
26977 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26978 finish_member_declaration (decl);
26979 /* If DECL is a function template, we must return to parse it later.
26980 (Even though there is no definition, there might be default
26981 arguments that need handling.) */
26982 if (member_p && decl
26983 && DECL_DECLARES_FUNCTION_P (decl))
26984 vec_safe_push (unparsed_funs_with_definitions, decl);
26987 /* Parse a template introduction header for a template-declaration. Returns
26988 false if tentative parse fails. */
26990 static bool
26991 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26993 cp_parser_parse_tentatively (parser);
26995 tree saved_scope = parser->scope;
26996 tree saved_object_scope = parser->object_scope;
26997 tree saved_qualifying_scope = parser->qualifying_scope;
26999 /* Look for the optional `::' operator. */
27000 cp_parser_global_scope_opt (parser,
27001 /*current_scope_valid_p=*/false);
27002 /* Look for the nested-name-specifier. */
27003 cp_parser_nested_name_specifier_opt (parser,
27004 /*typename_keyword_p=*/false,
27005 /*check_dependency_p=*/true,
27006 /*type_p=*/false,
27007 /*is_declaration=*/false);
27009 cp_token *token = cp_lexer_peek_token (parser->lexer);
27010 tree concept_name = cp_parser_identifier (parser);
27012 /* Look up the concept for which we will be matching
27013 template parameters. */
27014 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27015 token->location);
27016 parser->scope = saved_scope;
27017 parser->object_scope = saved_object_scope;
27018 parser->qualifying_scope = saved_qualifying_scope;
27020 if (concept_name == error_mark_node)
27021 cp_parser_simulate_error (parser);
27023 /* Look for opening brace for introduction. */
27024 matching_braces braces;
27025 braces.require_open (parser);
27027 if (!cp_parser_parse_definitely (parser))
27028 return false;
27030 push_deferring_access_checks (dk_deferred);
27032 /* Build vector of placeholder parameters and grab
27033 matching identifiers. */
27034 tree introduction_list = cp_parser_introduction_list (parser);
27036 /* The introduction-list shall not be empty. */
27037 int nargs = TREE_VEC_LENGTH (introduction_list);
27038 if (nargs == 0)
27040 error ("empty introduction-list");
27041 return true;
27044 /* Look for closing brace for introduction. */
27045 if (!braces.require_close (parser))
27046 return true;
27048 if (tmpl_decl == error_mark_node)
27050 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27051 token->location);
27052 return true;
27055 /* Build and associate the constraint. */
27056 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27057 if (parms && parms != error_mark_node)
27059 cp_parser_template_declaration_after_parameters (parser, parms,
27060 member_p);
27061 return true;
27064 error_at (token->location, "no matching concept for template-introduction");
27065 return true;
27068 /* Parse a normal template-declaration following the template keyword. */
27070 static void
27071 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27073 tree parameter_list;
27074 bool need_lang_pop;
27075 location_t location = input_location;
27077 /* Look for the `<' token. */
27078 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27079 return;
27080 if (at_class_scope_p () && current_function_decl)
27082 /* 14.5.2.2 [temp.mem]
27084 A local class shall not have member templates. */
27085 error_at (location,
27086 "invalid declaration of member template in local class");
27087 cp_parser_skip_to_end_of_block_or_statement (parser);
27088 return;
27090 /* [temp]
27092 A template ... shall not have C linkage. */
27093 if (current_lang_name == lang_name_c)
27095 error_at (location, "template with C linkage");
27096 maybe_show_extern_c_location ();
27097 /* Give it C++ linkage to avoid confusing other parts of the
27098 front end. */
27099 push_lang_context (lang_name_cplusplus);
27100 need_lang_pop = true;
27102 else
27103 need_lang_pop = false;
27105 /* We cannot perform access checks on the template parameter
27106 declarations until we know what is being declared, just as we
27107 cannot check the decl-specifier list. */
27108 push_deferring_access_checks (dk_deferred);
27110 /* If the next token is `>', then we have an invalid
27111 specialization. Rather than complain about an invalid template
27112 parameter, issue an error message here. */
27113 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27115 cp_parser_error (parser, "invalid explicit specialization");
27116 begin_specialization ();
27117 parameter_list = NULL_TREE;
27119 else
27121 /* Parse the template parameters. */
27122 parameter_list = cp_parser_template_parameter_list (parser);
27125 /* Look for the `>'. */
27126 cp_parser_skip_to_end_of_template_parameter_list (parser);
27128 /* Manage template requirements */
27129 if (flag_concepts)
27131 tree reqs = get_shorthand_constraints (current_template_parms);
27132 if (tree r = cp_parser_requires_clause_opt (parser))
27133 reqs = conjoin_constraints (reqs, normalize_expression (r));
27134 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27137 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27138 member_p);
27140 /* For the erroneous case of a template with C linkage, we pushed an
27141 implicit C++ linkage scope; exit that scope now. */
27142 if (need_lang_pop)
27143 pop_lang_context ();
27146 /* Parse a template-declaration, assuming that the `export' (and
27147 `extern') keywords, if present, has already been scanned. MEMBER_P
27148 is as for cp_parser_template_declaration. */
27150 static bool
27151 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27155 cp_lexer_consume_token (parser->lexer);
27156 cp_parser_explicit_template_declaration (parser, member_p);
27157 return true;
27159 else if (flag_concepts)
27160 return cp_parser_template_introduction (parser, member_p);
27162 return false;
27165 /* Perform the deferred access checks from a template-parameter-list.
27166 CHECKS is a TREE_LIST of access checks, as returned by
27167 get_deferred_access_checks. */
27169 static void
27170 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27172 ++processing_template_parmlist;
27173 perform_access_checks (checks, tf_warning_or_error);
27174 --processing_template_parmlist;
27177 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27178 `function-definition' sequence that follows a template header.
27179 If MEMBER_P is true, this declaration appears in a class scope.
27181 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27182 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27184 static tree
27185 cp_parser_single_declaration (cp_parser* parser,
27186 vec<deferred_access_check, va_gc> *checks,
27187 bool member_p,
27188 bool explicit_specialization_p,
27189 bool* friend_p)
27191 int declares_class_or_enum;
27192 tree decl = NULL_TREE;
27193 cp_decl_specifier_seq decl_specifiers;
27194 bool function_definition_p = false;
27195 cp_token *decl_spec_token_start;
27197 /* This function is only used when processing a template
27198 declaration. */
27199 gcc_assert (innermost_scope_kind () == sk_template_parms
27200 || innermost_scope_kind () == sk_template_spec);
27202 /* Defer access checks until we know what is being declared. */
27203 push_deferring_access_checks (dk_deferred);
27205 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27206 alternative. */
27207 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27208 cp_parser_decl_specifier_seq (parser,
27209 CP_PARSER_FLAGS_OPTIONAL,
27210 &decl_specifiers,
27211 &declares_class_or_enum);
27212 if (friend_p)
27213 *friend_p = cp_parser_friend_p (&decl_specifiers);
27215 /* There are no template typedefs. */
27216 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27218 error_at (decl_spec_token_start->location,
27219 "template declaration of %<typedef%>");
27220 decl = error_mark_node;
27223 /* Gather up the access checks that occurred the
27224 decl-specifier-seq. */
27225 stop_deferring_access_checks ();
27227 /* Check for the declaration of a template class. */
27228 if (declares_class_or_enum)
27230 if (cp_parser_declares_only_class_p (parser)
27231 || (declares_class_or_enum & 2))
27233 // If this is a declaration, but not a definition, associate
27234 // any constraints with the type declaration. Constraints
27235 // are associated with definitions in cp_parser_class_specifier.
27236 if (declares_class_or_enum == 1)
27237 associate_classtype_constraints (decl_specifiers.type);
27239 decl = shadow_tag (&decl_specifiers);
27241 /* In this case:
27243 struct C {
27244 friend template <typename T> struct A<T>::B;
27247 A<T>::B will be represented by a TYPENAME_TYPE, and
27248 therefore not recognized by shadow_tag. */
27249 if (friend_p && *friend_p
27250 && !decl
27251 && decl_specifiers.type
27252 && TYPE_P (decl_specifiers.type))
27253 decl = decl_specifiers.type;
27255 if (decl && decl != error_mark_node)
27256 decl = TYPE_NAME (decl);
27257 else
27258 decl = error_mark_node;
27260 /* Perform access checks for template parameters. */
27261 cp_parser_perform_template_parameter_access_checks (checks);
27263 /* Give a helpful diagnostic for
27264 template <class T> struct A { } a;
27265 if we aren't already recovering from an error. */
27266 if (!cp_parser_declares_only_class_p (parser)
27267 && !seen_error ())
27269 error_at (cp_lexer_peek_token (parser->lexer)->location,
27270 "a class template declaration must not declare "
27271 "anything else");
27272 cp_parser_skip_to_end_of_block_or_statement (parser);
27273 goto out;
27278 /* Complain about missing 'typename' or other invalid type names. */
27279 if (!decl_specifiers.any_type_specifiers_p
27280 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27282 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27283 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27284 the rest of this declaration. */
27285 decl = error_mark_node;
27286 goto out;
27289 /* If it's not a template class, try for a template function. If
27290 the next token is a `;', then this declaration does not declare
27291 anything. But, if there were errors in the decl-specifiers, then
27292 the error might well have come from an attempted class-specifier.
27293 In that case, there's no need to warn about a missing declarator. */
27294 if (!decl
27295 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27296 || decl_specifiers.type != error_mark_node))
27298 decl = cp_parser_init_declarator (parser,
27299 &decl_specifiers,
27300 checks,
27301 /*function_definition_allowed_p=*/true,
27302 member_p,
27303 declares_class_or_enum,
27304 &function_definition_p,
27305 NULL, NULL, NULL);
27307 /* 7.1.1-1 [dcl.stc]
27309 A storage-class-specifier shall not be specified in an explicit
27310 specialization... */
27311 if (decl
27312 && explicit_specialization_p
27313 && decl_specifiers.storage_class != sc_none)
27315 error_at (decl_spec_token_start->location,
27316 "explicit template specialization cannot have a storage class");
27317 decl = error_mark_node;
27320 if (decl && VAR_P (decl))
27321 check_template_variable (decl);
27324 /* Look for a trailing `;' after the declaration. */
27325 if (!function_definition_p
27326 && (decl == error_mark_node
27327 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27328 cp_parser_skip_to_end_of_block_or_statement (parser);
27330 out:
27331 pop_deferring_access_checks ();
27333 /* Clear any current qualification; whatever comes next is the start
27334 of something new. */
27335 parser->scope = NULL_TREE;
27336 parser->qualifying_scope = NULL_TREE;
27337 parser->object_scope = NULL_TREE;
27339 return decl;
27342 /* Parse a cast-expression that is not the operand of a unary "&". */
27344 static cp_expr
27345 cp_parser_simple_cast_expression (cp_parser *parser)
27347 return cp_parser_cast_expression (parser, /*address_p=*/false,
27348 /*cast_p=*/false, /*decltype*/false, NULL);
27351 /* Parse a functional cast to TYPE. Returns an expression
27352 representing the cast. */
27354 static cp_expr
27355 cp_parser_functional_cast (cp_parser* parser, tree type)
27357 vec<tree, va_gc> *vec;
27358 tree expression_list;
27359 cp_expr cast;
27360 bool nonconst_p;
27362 location_t start_loc = input_location;
27364 if (!type)
27365 type = error_mark_node;
27367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27369 cp_lexer_set_source_position (parser->lexer);
27370 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27371 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27372 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27373 if (TREE_CODE (type) == TYPE_DECL)
27374 type = TREE_TYPE (type);
27376 cast = finish_compound_literal (type, expression_list,
27377 tf_warning_or_error, fcl_functional);
27378 /* Create a location of the form:
27379 type_name{i, f}
27380 ^~~~~~~~~~~~~~~
27381 with caret == start at the start of the type name,
27382 finishing at the closing brace. */
27383 location_t finish_loc
27384 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27385 location_t combined_loc = make_location (start_loc, start_loc,
27386 finish_loc);
27387 cast.set_location (combined_loc);
27388 return cast;
27392 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27393 /*cast_p=*/true,
27394 /*allow_expansion_p=*/true,
27395 /*non_constant_p=*/NULL);
27396 if (vec == NULL)
27397 expression_list = error_mark_node;
27398 else
27400 expression_list = build_tree_list_vec (vec);
27401 release_tree_vector (vec);
27404 cast = build_functional_cast (type, expression_list,
27405 tf_warning_or_error);
27406 /* [expr.const]/1: In an integral constant expression "only type
27407 conversions to integral or enumeration type can be used". */
27408 if (TREE_CODE (type) == TYPE_DECL)
27409 type = TREE_TYPE (type);
27410 if (cast != error_mark_node
27411 && !cast_valid_in_integral_constant_expression_p (type)
27412 && cp_parser_non_integral_constant_expression (parser,
27413 NIC_CONSTRUCTOR))
27414 return error_mark_node;
27416 /* Create a location of the form:
27417 float(i)
27418 ^~~~~~~~
27419 with caret == start at the start of the type name,
27420 finishing at the closing paren. */
27421 location_t finish_loc
27422 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27423 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27424 cast.set_location (combined_loc);
27425 return cast;
27428 /* Save the tokens that make up the body of a member function defined
27429 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27430 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27431 specifiers applied to the declaration. Returns the FUNCTION_DECL
27432 for the member function. */
27434 static tree
27435 cp_parser_save_member_function_body (cp_parser* parser,
27436 cp_decl_specifier_seq *decl_specifiers,
27437 cp_declarator *declarator,
27438 tree attributes)
27440 cp_token *first;
27441 cp_token *last;
27442 tree fn;
27443 bool function_try_block = false;
27445 /* Create the FUNCTION_DECL. */
27446 fn = grokmethod (decl_specifiers, declarator, attributes);
27447 cp_finalize_omp_declare_simd (parser, fn);
27448 cp_finalize_oacc_routine (parser, fn, true);
27449 /* If something went badly wrong, bail out now. */
27450 if (fn == error_mark_node)
27452 /* If there's a function-body, skip it. */
27453 if (cp_parser_token_starts_function_definition_p
27454 (cp_lexer_peek_token (parser->lexer)))
27455 cp_parser_skip_to_end_of_block_or_statement (parser);
27456 return error_mark_node;
27459 /* Remember it, if there default args to post process. */
27460 cp_parser_save_default_args (parser, fn);
27462 /* Save away the tokens that make up the body of the
27463 function. */
27464 first = parser->lexer->next_token;
27466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27467 cp_lexer_consume_token (parser->lexer);
27468 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27469 RID_TRANSACTION_ATOMIC))
27471 cp_lexer_consume_token (parser->lexer);
27472 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27474 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27475 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27476 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27477 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27478 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27480 cp_lexer_consume_token (parser->lexer);
27481 cp_lexer_consume_token (parser->lexer);
27482 cp_lexer_consume_token (parser->lexer);
27483 cp_lexer_consume_token (parser->lexer);
27484 cp_lexer_consume_token (parser->lexer);
27486 else
27487 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27488 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27490 cp_lexer_consume_token (parser->lexer);
27491 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27492 break;
27496 /* Handle function try blocks. */
27497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27499 cp_lexer_consume_token (parser->lexer);
27500 function_try_block = true;
27502 /* We can have braced-init-list mem-initializers before the fn body. */
27503 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27505 cp_lexer_consume_token (parser->lexer);
27506 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27508 /* cache_group will stop after an un-nested { } pair, too. */
27509 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27510 break;
27512 /* variadic mem-inits have ... after the ')'. */
27513 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27514 cp_lexer_consume_token (parser->lexer);
27517 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27518 /* Handle function try blocks. */
27519 if (function_try_block)
27520 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27521 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27522 last = parser->lexer->next_token;
27524 /* Save away the inline definition; we will process it when the
27525 class is complete. */
27526 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27527 DECL_PENDING_INLINE_P (fn) = 1;
27529 /* We need to know that this was defined in the class, so that
27530 friend templates are handled correctly. */
27531 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27533 /* Add FN to the queue of functions to be parsed later. */
27534 vec_safe_push (unparsed_funs_with_definitions, fn);
27536 return fn;
27539 /* Save the tokens that make up the in-class initializer for a non-static
27540 data member. Returns a DEFAULT_ARG. */
27542 static tree
27543 cp_parser_save_nsdmi (cp_parser* parser)
27545 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27548 /* Parse a template-argument-list, as well as the trailing ">" (but
27549 not the opening "<"). See cp_parser_template_argument_list for the
27550 return value. */
27552 static tree
27553 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27555 tree arguments;
27556 tree saved_scope;
27557 tree saved_qualifying_scope;
27558 tree saved_object_scope;
27559 bool saved_greater_than_is_operator_p;
27560 int saved_unevaluated_operand;
27561 int saved_inhibit_evaluation_warnings;
27563 /* [temp.names]
27565 When parsing a template-id, the first non-nested `>' is taken as
27566 the end of the template-argument-list rather than a greater-than
27567 operator. */
27568 saved_greater_than_is_operator_p
27569 = parser->greater_than_is_operator_p;
27570 parser->greater_than_is_operator_p = false;
27571 /* Parsing the argument list may modify SCOPE, so we save it
27572 here. */
27573 saved_scope = parser->scope;
27574 saved_qualifying_scope = parser->qualifying_scope;
27575 saved_object_scope = parser->object_scope;
27576 /* We need to evaluate the template arguments, even though this
27577 template-id may be nested within a "sizeof". */
27578 saved_unevaluated_operand = cp_unevaluated_operand;
27579 cp_unevaluated_operand = 0;
27580 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27581 c_inhibit_evaluation_warnings = 0;
27582 /* Parse the template-argument-list itself. */
27583 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27584 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27585 arguments = NULL_TREE;
27586 else
27587 arguments = cp_parser_template_argument_list (parser);
27588 /* Look for the `>' that ends the template-argument-list. If we find
27589 a '>>' instead, it's probably just a typo. */
27590 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27592 if (cxx_dialect != cxx98)
27594 /* In C++0x, a `>>' in a template argument list or cast
27595 expression is considered to be two separate `>'
27596 tokens. So, change the current token to a `>', but don't
27597 consume it: it will be consumed later when the outer
27598 template argument list (or cast expression) is parsed.
27599 Note that this replacement of `>' for `>>' is necessary
27600 even if we are parsing tentatively: in the tentative
27601 case, after calling
27602 cp_parser_enclosed_template_argument_list we will always
27603 throw away all of the template arguments and the first
27604 closing `>', either because the template argument list
27605 was erroneous or because we are replacing those tokens
27606 with a CPP_TEMPLATE_ID token. The second `>' (which will
27607 not have been thrown away) is needed either to close an
27608 outer template argument list or to complete a new-style
27609 cast. */
27610 cp_token *token = cp_lexer_peek_token (parser->lexer);
27611 token->type = CPP_GREATER;
27613 else if (!saved_greater_than_is_operator_p)
27615 /* If we're in a nested template argument list, the '>>' has
27616 to be a typo for '> >'. We emit the error message, but we
27617 continue parsing and we push a '>' as next token, so that
27618 the argument list will be parsed correctly. Note that the
27619 global source location is still on the token before the
27620 '>>', so we need to say explicitly where we want it. */
27621 cp_token *token = cp_lexer_peek_token (parser->lexer);
27622 gcc_rich_location richloc (token->location);
27623 richloc.add_fixit_replace ("> >");
27624 error_at (&richloc, "%<>>%> should be %<> >%> "
27625 "within a nested template argument list");
27627 token->type = CPP_GREATER;
27629 else
27631 /* If this is not a nested template argument list, the '>>'
27632 is a typo for '>'. Emit an error message and continue.
27633 Same deal about the token location, but here we can get it
27634 right by consuming the '>>' before issuing the diagnostic. */
27635 cp_token *token = cp_lexer_consume_token (parser->lexer);
27636 error_at (token->location,
27637 "spurious %<>>%>, use %<>%> to terminate "
27638 "a template argument list");
27641 else
27642 cp_parser_skip_to_end_of_template_parameter_list (parser);
27643 /* The `>' token might be a greater-than operator again now. */
27644 parser->greater_than_is_operator_p
27645 = saved_greater_than_is_operator_p;
27646 /* Restore the SAVED_SCOPE. */
27647 parser->scope = saved_scope;
27648 parser->qualifying_scope = saved_qualifying_scope;
27649 parser->object_scope = saved_object_scope;
27650 cp_unevaluated_operand = saved_unevaluated_operand;
27651 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27653 return arguments;
27656 /* MEMBER_FUNCTION is a member function, or a friend. If default
27657 arguments, or the body of the function have not yet been parsed,
27658 parse them now. */
27660 static void
27661 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27663 timevar_push (TV_PARSE_INMETH);
27664 /* If this member is a template, get the underlying
27665 FUNCTION_DECL. */
27666 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27667 member_function = DECL_TEMPLATE_RESULT (member_function);
27669 /* There should not be any class definitions in progress at this
27670 point; the bodies of members are only parsed outside of all class
27671 definitions. */
27672 gcc_assert (parser->num_classes_being_defined == 0);
27673 /* While we're parsing the member functions we might encounter more
27674 classes. We want to handle them right away, but we don't want
27675 them getting mixed up with functions that are currently in the
27676 queue. */
27677 push_unparsed_function_queues (parser);
27679 /* Make sure that any template parameters are in scope. */
27680 maybe_begin_member_template_processing (member_function);
27682 /* If the body of the function has not yet been parsed, parse it
27683 now. */
27684 if (DECL_PENDING_INLINE_P (member_function))
27686 tree function_scope;
27687 cp_token_cache *tokens;
27689 /* The function is no longer pending; we are processing it. */
27690 tokens = DECL_PENDING_INLINE_INFO (member_function);
27691 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27692 DECL_PENDING_INLINE_P (member_function) = 0;
27694 /* If this is a local class, enter the scope of the containing
27695 function. */
27696 function_scope = current_function_decl;
27697 if (function_scope)
27698 push_function_context ();
27700 /* Push the body of the function onto the lexer stack. */
27701 cp_parser_push_lexer_for_tokens (parser, tokens);
27703 /* Let the front end know that we going to be defining this
27704 function. */
27705 start_preparsed_function (member_function, NULL_TREE,
27706 SF_PRE_PARSED | SF_INCLASS_INLINE);
27708 /* Don't do access checking if it is a templated function. */
27709 if (processing_template_decl)
27710 push_deferring_access_checks (dk_no_check);
27712 /* #pragma omp declare reduction needs special parsing. */
27713 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27715 parser->lexer->in_pragma = true;
27716 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27717 finish_function (/*inline_p=*/true);
27718 cp_check_omp_declare_reduction (member_function);
27720 else
27721 /* Now, parse the body of the function. */
27722 cp_parser_function_definition_after_declarator (parser,
27723 /*inline_p=*/true);
27725 if (processing_template_decl)
27726 pop_deferring_access_checks ();
27728 /* Leave the scope of the containing function. */
27729 if (function_scope)
27730 pop_function_context ();
27731 cp_parser_pop_lexer (parser);
27734 /* Remove any template parameters from the symbol table. */
27735 maybe_end_member_template_processing ();
27737 /* Restore the queue. */
27738 pop_unparsed_function_queues (parser);
27739 timevar_pop (TV_PARSE_INMETH);
27742 /* If DECL contains any default args, remember it on the unparsed
27743 functions queue. */
27745 static void
27746 cp_parser_save_default_args (cp_parser* parser, tree decl)
27748 tree probe;
27750 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27751 probe;
27752 probe = TREE_CHAIN (probe))
27753 if (TREE_PURPOSE (probe))
27755 cp_default_arg_entry entry = {current_class_type, decl};
27756 vec_safe_push (unparsed_funs_with_default_args, entry);
27757 break;
27761 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27762 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27763 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27764 from the parameter-type-list. */
27766 static tree
27767 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27768 tree default_arg, tree parmtype)
27770 cp_token_cache *tokens;
27771 tree parsed_arg;
27772 bool dummy;
27774 if (default_arg == error_mark_node)
27775 return error_mark_node;
27777 /* Push the saved tokens for the default argument onto the parser's
27778 lexer stack. */
27779 tokens = DEFARG_TOKENS (default_arg);
27780 cp_parser_push_lexer_for_tokens (parser, tokens);
27782 start_lambda_scope (decl);
27784 /* Parse the default argument. */
27785 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27786 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27787 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27789 finish_lambda_scope ();
27791 if (parsed_arg == error_mark_node)
27792 cp_parser_skip_to_end_of_statement (parser);
27794 if (!processing_template_decl)
27796 /* In a non-template class, check conversions now. In a template,
27797 we'll wait and instantiate these as needed. */
27798 if (TREE_CODE (decl) == PARM_DECL)
27799 parsed_arg = check_default_argument (parmtype, parsed_arg,
27800 tf_warning_or_error);
27801 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27802 parsed_arg = error_mark_node;
27803 else
27804 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27807 /* If the token stream has not been completely used up, then
27808 there was extra junk after the end of the default
27809 argument. */
27810 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27812 if (TREE_CODE (decl) == PARM_DECL)
27813 cp_parser_error (parser, "expected %<,%>");
27814 else
27815 cp_parser_error (parser, "expected %<;%>");
27818 /* Revert to the main lexer. */
27819 cp_parser_pop_lexer (parser);
27821 return parsed_arg;
27824 /* FIELD is a non-static data member with an initializer which we saved for
27825 later; parse it now. */
27827 static void
27828 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27830 tree def;
27832 maybe_begin_member_template_processing (field);
27834 push_unparsed_function_queues (parser);
27835 def = cp_parser_late_parse_one_default_arg (parser, field,
27836 DECL_INITIAL (field),
27837 NULL_TREE);
27838 pop_unparsed_function_queues (parser);
27840 maybe_end_member_template_processing ();
27842 DECL_INITIAL (field) = def;
27845 /* FN is a FUNCTION_DECL which may contains a parameter with an
27846 unparsed DEFAULT_ARG. Parse the default args now. This function
27847 assumes that the current scope is the scope in which the default
27848 argument should be processed. */
27850 static void
27851 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27853 bool saved_local_variables_forbidden_p;
27854 tree parm, parmdecl;
27856 /* While we're parsing the default args, we might (due to the
27857 statement expression extension) encounter more classes. We want
27858 to handle them right away, but we don't want them getting mixed
27859 up with default args that are currently in the queue. */
27860 push_unparsed_function_queues (parser);
27862 /* Local variable names (and the `this' keyword) may not appear
27863 in a default argument. */
27864 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27865 parser->local_variables_forbidden_p = true;
27867 push_defarg_context (fn);
27869 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27870 parmdecl = DECL_ARGUMENTS (fn);
27871 parm && parm != void_list_node;
27872 parm = TREE_CHAIN (parm),
27873 parmdecl = DECL_CHAIN (parmdecl))
27875 tree default_arg = TREE_PURPOSE (parm);
27876 tree parsed_arg;
27877 vec<tree, va_gc> *insts;
27878 tree copy;
27879 unsigned ix;
27881 if (!default_arg)
27882 continue;
27884 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27885 /* This can happen for a friend declaration for a function
27886 already declared with default arguments. */
27887 continue;
27889 parsed_arg
27890 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27891 default_arg,
27892 TREE_VALUE (parm));
27893 TREE_PURPOSE (parm) = parsed_arg;
27895 /* Update any instantiations we've already created. */
27896 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27897 vec_safe_iterate (insts, ix, &copy); ix++)
27898 TREE_PURPOSE (copy) = parsed_arg;
27901 pop_defarg_context ();
27903 /* Make sure no default arg is missing. */
27904 check_default_args (fn);
27906 /* Restore the state of local_variables_forbidden_p. */
27907 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27909 /* Restore the queue. */
27910 pop_unparsed_function_queues (parser);
27913 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27915 sizeof ... ( identifier )
27917 where the 'sizeof' token has already been consumed. */
27919 static tree
27920 cp_parser_sizeof_pack (cp_parser *parser)
27922 /* Consume the `...'. */
27923 cp_lexer_consume_token (parser->lexer);
27924 maybe_warn_variadic_templates ();
27926 matching_parens parens;
27927 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27928 if (paren)
27929 parens.consume_open (parser);
27930 else
27931 permerror (cp_lexer_peek_token (parser->lexer)->location,
27932 "%<sizeof...%> argument must be surrounded by parentheses");
27934 cp_token *token = cp_lexer_peek_token (parser->lexer);
27935 tree name = cp_parser_identifier (parser);
27936 if (name == error_mark_node)
27937 return error_mark_node;
27938 /* The name is not qualified. */
27939 parser->scope = NULL_TREE;
27940 parser->qualifying_scope = NULL_TREE;
27941 parser->object_scope = NULL_TREE;
27942 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27943 if (expr == error_mark_node)
27944 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27945 token->location);
27946 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27947 expr = TREE_TYPE (expr);
27948 else if (TREE_CODE (expr) == CONST_DECL)
27949 expr = DECL_INITIAL (expr);
27950 expr = make_pack_expansion (expr);
27951 PACK_EXPANSION_SIZEOF_P (expr) = true;
27953 if (paren)
27954 parens.require_close (parser);
27956 return expr;
27959 /* Parse the operand of `sizeof' (or a similar operator). Returns
27960 either a TYPE or an expression, depending on the form of the
27961 input. The KEYWORD indicates which kind of expression we have
27962 encountered. */
27964 static tree
27965 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27967 tree expr = NULL_TREE;
27968 const char *saved_message;
27969 char *tmp;
27970 bool saved_integral_constant_expression_p;
27971 bool saved_non_integral_constant_expression_p;
27973 /* If it's a `...', then we are computing the length of a parameter
27974 pack. */
27975 if (keyword == RID_SIZEOF
27976 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27977 return cp_parser_sizeof_pack (parser);
27979 /* Types cannot be defined in a `sizeof' expression. Save away the
27980 old message. */
27981 saved_message = parser->type_definition_forbidden_message;
27982 /* And create the new one. */
27983 tmp = concat ("types may not be defined in %<",
27984 IDENTIFIER_POINTER (ridpointers[keyword]),
27985 "%> expressions", NULL);
27986 parser->type_definition_forbidden_message = tmp;
27988 /* The restrictions on constant-expressions do not apply inside
27989 sizeof expressions. */
27990 saved_integral_constant_expression_p
27991 = parser->integral_constant_expression_p;
27992 saved_non_integral_constant_expression_p
27993 = parser->non_integral_constant_expression_p;
27994 parser->integral_constant_expression_p = false;
27996 /* Do not actually evaluate the expression. */
27997 ++cp_unevaluated_operand;
27998 ++c_inhibit_evaluation_warnings;
27999 /* If it's a `(', then we might be looking at the type-id
28000 construction. */
28001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28003 tree type = NULL_TREE;
28005 /* We can't be sure yet whether we're looking at a type-id or an
28006 expression. */
28007 cp_parser_parse_tentatively (parser);
28009 matching_parens parens;
28010 parens.consume_open (parser);
28012 /* Note: as a GNU Extension, compound literals are considered
28013 postfix-expressions as they are in C99, so they are valid
28014 arguments to sizeof. See comment in cp_parser_cast_expression
28015 for details. */
28016 if (cp_parser_compound_literal_p (parser))
28017 cp_parser_simulate_error (parser);
28018 else
28020 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28021 parser->in_type_id_in_expr_p = true;
28022 /* Look for the type-id. */
28023 type = cp_parser_type_id (parser);
28024 /* Look for the closing `)'. */
28025 parens.require_close (parser);
28026 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28029 /* If all went well, then we're done. */
28030 if (cp_parser_parse_definitely (parser))
28032 cp_decl_specifier_seq decl_specs;
28034 /* Build a trivial decl-specifier-seq. */
28035 clear_decl_specs (&decl_specs);
28036 decl_specs.type = type;
28038 /* Call grokdeclarator to figure out what type this is. */
28039 expr = grokdeclarator (NULL,
28040 &decl_specs,
28041 TYPENAME,
28042 /*initialized=*/0,
28043 /*attrlist=*/NULL);
28047 /* If the type-id production did not work out, then we must be
28048 looking at the unary-expression production. */
28049 if (!expr)
28050 expr = cp_parser_unary_expression (parser);
28052 /* Go back to evaluating expressions. */
28053 --cp_unevaluated_operand;
28054 --c_inhibit_evaluation_warnings;
28056 /* Free the message we created. */
28057 free (tmp);
28058 /* And restore the old one. */
28059 parser->type_definition_forbidden_message = saved_message;
28060 parser->integral_constant_expression_p
28061 = saved_integral_constant_expression_p;
28062 parser->non_integral_constant_expression_p
28063 = saved_non_integral_constant_expression_p;
28065 return expr;
28068 /* If the current declaration has no declarator, return true. */
28070 static bool
28071 cp_parser_declares_only_class_p (cp_parser *parser)
28073 /* If the next token is a `;' or a `,' then there is no
28074 declarator. */
28075 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28076 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28079 /* Update the DECL_SPECS to reflect the storage class indicated by
28080 KEYWORD. */
28082 static void
28083 cp_parser_set_storage_class (cp_parser *parser,
28084 cp_decl_specifier_seq *decl_specs,
28085 enum rid keyword,
28086 cp_token *token)
28088 cp_storage_class storage_class;
28090 if (parser->in_unbraced_linkage_specification_p)
28092 error_at (token->location, "invalid use of %qD in linkage specification",
28093 ridpointers[keyword]);
28094 return;
28096 else if (decl_specs->storage_class != sc_none)
28098 decl_specs->conflicting_specifiers_p = true;
28099 return;
28102 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28103 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28104 && decl_specs->gnu_thread_keyword_p)
28106 pedwarn (decl_specs->locations[ds_thread], 0,
28107 "%<__thread%> before %qD", ridpointers[keyword]);
28110 switch (keyword)
28112 case RID_AUTO:
28113 storage_class = sc_auto;
28114 break;
28115 case RID_REGISTER:
28116 storage_class = sc_register;
28117 break;
28118 case RID_STATIC:
28119 storage_class = sc_static;
28120 break;
28121 case RID_EXTERN:
28122 storage_class = sc_extern;
28123 break;
28124 case RID_MUTABLE:
28125 storage_class = sc_mutable;
28126 break;
28127 default:
28128 gcc_unreachable ();
28130 decl_specs->storage_class = storage_class;
28131 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28133 /* A storage class specifier cannot be applied alongside a typedef
28134 specifier. If there is a typedef specifier present then set
28135 conflicting_specifiers_p which will trigger an error later
28136 on in grokdeclarator. */
28137 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28138 decl_specs->conflicting_specifiers_p = true;
28141 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28142 is true, the type is a class or enum definition. */
28144 static void
28145 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28146 tree type_spec,
28147 cp_token *token,
28148 bool type_definition_p)
28150 decl_specs->any_specifiers_p = true;
28152 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28153 (with, for example, in "typedef int wchar_t;") we remember that
28154 this is what happened. In system headers, we ignore these
28155 declarations so that G++ can work with system headers that are not
28156 C++-safe. */
28157 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28158 && !type_definition_p
28159 && (type_spec == boolean_type_node
28160 || type_spec == char16_type_node
28161 || type_spec == char32_type_node
28162 || type_spec == wchar_type_node)
28163 && (decl_specs->type
28164 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28165 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28166 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28167 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28169 decl_specs->redefined_builtin_type = type_spec;
28170 set_and_check_decl_spec_loc (decl_specs,
28171 ds_redefined_builtin_type_spec,
28172 token);
28173 if (!decl_specs->type)
28175 decl_specs->type = type_spec;
28176 decl_specs->type_definition_p = false;
28177 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28180 else if (decl_specs->type)
28181 decl_specs->multiple_types_p = true;
28182 else
28184 decl_specs->type = type_spec;
28185 decl_specs->type_definition_p = type_definition_p;
28186 decl_specs->redefined_builtin_type = NULL_TREE;
28187 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28191 /* True iff TOKEN is the GNU keyword __thread. */
28193 static bool
28194 token_is__thread (cp_token *token)
28196 gcc_assert (token->keyword == RID_THREAD);
28197 return id_equal (token->u.value, "__thread");
28200 /* Set the location for a declarator specifier and check if it is
28201 duplicated.
28203 DECL_SPECS is the sequence of declarator specifiers onto which to
28204 set the location.
28206 DS is the single declarator specifier to set which location is to
28207 be set onto the existing sequence of declarators.
28209 LOCATION is the location for the declarator specifier to
28210 consider. */
28212 static void
28213 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28214 cp_decl_spec ds, cp_token *token)
28216 gcc_assert (ds < ds_last);
28218 if (decl_specs == NULL)
28219 return;
28221 source_location location = token->location;
28223 if (decl_specs->locations[ds] == 0)
28225 decl_specs->locations[ds] = location;
28226 if (ds == ds_thread)
28227 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28229 else
28231 if (ds == ds_long)
28233 if (decl_specs->locations[ds_long_long] != 0)
28234 error_at (location,
28235 "%<long long long%> is too long for GCC");
28236 else
28238 decl_specs->locations[ds_long_long] = location;
28239 pedwarn_cxx98 (location,
28240 OPT_Wlong_long,
28241 "ISO C++ 1998 does not support %<long long%>");
28244 else if (ds == ds_thread)
28246 bool gnu = token_is__thread (token);
28247 if (gnu != decl_specs->gnu_thread_keyword_p)
28248 error_at (location,
28249 "both %<__thread%> and %<thread_local%> specified");
28250 else
28252 gcc_rich_location richloc (location);
28253 richloc.add_fixit_remove ();
28254 error_at (&richloc, "duplicate %qD", token->u.value);
28257 else
28259 static const char *const decl_spec_names[] = {
28260 "signed",
28261 "unsigned",
28262 "short",
28263 "long",
28264 "const",
28265 "volatile",
28266 "restrict",
28267 "inline",
28268 "virtual",
28269 "explicit",
28270 "friend",
28271 "typedef",
28272 "using",
28273 "constexpr",
28274 "__complex"
28276 gcc_rich_location richloc (location);
28277 richloc.add_fixit_remove ();
28278 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28283 /* Return true iff the declarator specifier DS is present in the
28284 sequence of declarator specifiers DECL_SPECS. */
28286 bool
28287 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28288 cp_decl_spec ds)
28290 gcc_assert (ds < ds_last);
28292 if (decl_specs == NULL)
28293 return false;
28295 return decl_specs->locations[ds] != 0;
28298 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28299 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28301 static bool
28302 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28304 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28307 /* Issue an error message indicating that TOKEN_DESC was expected.
28308 If KEYWORD is true, it indicated this function is called by
28309 cp_parser_require_keword and the required token can only be
28310 a indicated keyword.
28312 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28313 within any error as the location of an "opening" token matching
28314 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28315 RT_CLOSE_PAREN). */
28317 static void
28318 cp_parser_required_error (cp_parser *parser,
28319 required_token token_desc,
28320 bool keyword,
28321 location_t matching_location)
28323 if (cp_parser_simulate_error (parser))
28324 return;
28326 const char *gmsgid = NULL;
28327 switch (token_desc)
28329 case RT_NEW:
28330 gmsgid = G_("expected %<new%>");
28331 break;
28332 case RT_DELETE:
28333 gmsgid = G_("expected %<delete%>");
28334 break;
28335 case RT_RETURN:
28336 gmsgid = G_("expected %<return%>");
28337 break;
28338 case RT_WHILE:
28339 gmsgid = G_("expected %<while%>");
28340 break;
28341 case RT_EXTERN:
28342 gmsgid = G_("expected %<extern%>");
28343 break;
28344 case RT_STATIC_ASSERT:
28345 gmsgid = G_("expected %<static_assert%>");
28346 break;
28347 case RT_DECLTYPE:
28348 gmsgid = G_("expected %<decltype%>");
28349 break;
28350 case RT_OPERATOR:
28351 gmsgid = G_("expected %<operator%>");
28352 break;
28353 case RT_CLASS:
28354 gmsgid = G_("expected %<class%>");
28355 break;
28356 case RT_TEMPLATE:
28357 gmsgid = G_("expected %<template%>");
28358 break;
28359 case RT_NAMESPACE:
28360 gmsgid = G_("expected %<namespace%>");
28361 break;
28362 case RT_USING:
28363 gmsgid = G_("expected %<using%>");
28364 break;
28365 case RT_ASM:
28366 gmsgid = G_("expected %<asm%>");
28367 break;
28368 case RT_TRY:
28369 gmsgid = G_("expected %<try%>");
28370 break;
28371 case RT_CATCH:
28372 gmsgid = G_("expected %<catch%>");
28373 break;
28374 case RT_THROW:
28375 gmsgid = G_("expected %<throw%>");
28376 break;
28377 case RT_LABEL:
28378 gmsgid = G_("expected %<__label__%>");
28379 break;
28380 case RT_AT_TRY:
28381 gmsgid = G_("expected %<@try%>");
28382 break;
28383 case RT_AT_SYNCHRONIZED:
28384 gmsgid = G_("expected %<@synchronized%>");
28385 break;
28386 case RT_AT_THROW:
28387 gmsgid = G_("expected %<@throw%>");
28388 break;
28389 case RT_TRANSACTION_ATOMIC:
28390 gmsgid = G_("expected %<__transaction_atomic%>");
28391 break;
28392 case RT_TRANSACTION_RELAXED:
28393 gmsgid = G_("expected %<__transaction_relaxed%>");
28394 break;
28395 default:
28396 break;
28399 if (!gmsgid && !keyword)
28401 switch (token_desc)
28403 case RT_SEMICOLON:
28404 gmsgid = G_("expected %<;%>");
28405 break;
28406 case RT_OPEN_PAREN:
28407 gmsgid = G_("expected %<(%>");
28408 break;
28409 case RT_CLOSE_BRACE:
28410 gmsgid = G_("expected %<}%>");
28411 break;
28412 case RT_OPEN_BRACE:
28413 gmsgid = G_("expected %<{%>");
28414 break;
28415 case RT_CLOSE_SQUARE:
28416 gmsgid = G_("expected %<]%>");
28417 break;
28418 case RT_OPEN_SQUARE:
28419 gmsgid = G_("expected %<[%>");
28420 break;
28421 case RT_COMMA:
28422 gmsgid = G_("expected %<,%>");
28423 break;
28424 case RT_SCOPE:
28425 gmsgid = G_("expected %<::%>");
28426 break;
28427 case RT_LESS:
28428 gmsgid = G_("expected %<<%>");
28429 break;
28430 case RT_GREATER:
28431 gmsgid = G_("expected %<>%>");
28432 break;
28433 case RT_EQ:
28434 gmsgid = G_("expected %<=%>");
28435 break;
28436 case RT_ELLIPSIS:
28437 gmsgid = G_("expected %<...%>");
28438 break;
28439 case RT_MULT:
28440 gmsgid = G_("expected %<*%>");
28441 break;
28442 case RT_COMPL:
28443 gmsgid = G_("expected %<~%>");
28444 break;
28445 case RT_COLON:
28446 gmsgid = G_("expected %<:%>");
28447 break;
28448 case RT_COLON_SCOPE:
28449 gmsgid = G_("expected %<:%> or %<::%>");
28450 break;
28451 case RT_CLOSE_PAREN:
28452 gmsgid = G_("expected %<)%>");
28453 break;
28454 case RT_COMMA_CLOSE_PAREN:
28455 gmsgid = G_("expected %<,%> or %<)%>");
28456 break;
28457 case RT_PRAGMA_EOL:
28458 gmsgid = G_("expected end of line");
28459 break;
28460 case RT_NAME:
28461 gmsgid = G_("expected identifier");
28462 break;
28463 case RT_SELECT:
28464 gmsgid = G_("expected selection-statement");
28465 break;
28466 case RT_ITERATION:
28467 gmsgid = G_("expected iteration-statement");
28468 break;
28469 case RT_JUMP:
28470 gmsgid = G_("expected jump-statement");
28471 break;
28472 case RT_CLASS_KEY:
28473 gmsgid = G_("expected class-key");
28474 break;
28475 case RT_CLASS_TYPENAME_TEMPLATE:
28476 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28477 break;
28478 default:
28479 gcc_unreachable ();
28483 if (gmsgid)
28484 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28488 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28489 issue an error message indicating that TOKEN_DESC was expected.
28491 Returns the token consumed, if the token had the appropriate type.
28492 Otherwise, returns NULL.
28494 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28495 within any error as the location of an "opening" token matching
28496 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28497 RT_CLOSE_PAREN). */
28499 static cp_token *
28500 cp_parser_require (cp_parser* parser,
28501 enum cpp_ttype type,
28502 required_token token_desc,
28503 location_t matching_location)
28505 if (cp_lexer_next_token_is (parser->lexer, type))
28506 return cp_lexer_consume_token (parser->lexer);
28507 else
28509 /* Output the MESSAGE -- unless we're parsing tentatively. */
28510 if (!cp_parser_simulate_error (parser))
28511 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28512 matching_location);
28513 return NULL;
28517 /* An error message is produced if the next token is not '>'.
28518 All further tokens are skipped until the desired token is
28519 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28521 static void
28522 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28524 /* Current level of '< ... >'. */
28525 unsigned level = 0;
28526 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28527 unsigned nesting_depth = 0;
28529 /* Are we ready, yet? If not, issue error message. */
28530 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28531 return;
28533 /* Skip tokens until the desired token is found. */
28534 while (true)
28536 /* Peek at the next token. */
28537 switch (cp_lexer_peek_token (parser->lexer)->type)
28539 case CPP_LESS:
28540 if (!nesting_depth)
28541 ++level;
28542 break;
28544 case CPP_RSHIFT:
28545 if (cxx_dialect == cxx98)
28546 /* C++0x views the `>>' operator as two `>' tokens, but
28547 C++98 does not. */
28548 break;
28549 else if (!nesting_depth && level-- == 0)
28551 /* We've hit a `>>' where the first `>' closes the
28552 template argument list, and the second `>' is
28553 spurious. Just consume the `>>' and stop; we've
28554 already produced at least one error. */
28555 cp_lexer_consume_token (parser->lexer);
28556 return;
28558 /* Fall through for C++0x, so we handle the second `>' in
28559 the `>>'. */
28560 gcc_fallthrough ();
28562 case CPP_GREATER:
28563 if (!nesting_depth && level-- == 0)
28565 /* We've reached the token we want, consume it and stop. */
28566 cp_lexer_consume_token (parser->lexer);
28567 return;
28569 break;
28571 case CPP_OPEN_PAREN:
28572 case CPP_OPEN_SQUARE:
28573 ++nesting_depth;
28574 break;
28576 case CPP_CLOSE_PAREN:
28577 case CPP_CLOSE_SQUARE:
28578 if (nesting_depth-- == 0)
28579 return;
28580 break;
28582 case CPP_EOF:
28583 case CPP_PRAGMA_EOL:
28584 case CPP_SEMICOLON:
28585 case CPP_OPEN_BRACE:
28586 case CPP_CLOSE_BRACE:
28587 /* The '>' was probably forgotten, don't look further. */
28588 return;
28590 default:
28591 break;
28594 /* Consume this token. */
28595 cp_lexer_consume_token (parser->lexer);
28599 /* If the next token is the indicated keyword, consume it. Otherwise,
28600 issue an error message indicating that TOKEN_DESC was expected.
28602 Returns the token consumed, if the token had the appropriate type.
28603 Otherwise, returns NULL. */
28605 static cp_token *
28606 cp_parser_require_keyword (cp_parser* parser,
28607 enum rid keyword,
28608 required_token token_desc)
28610 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28612 if (token && token->keyword != keyword)
28614 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28615 UNKNOWN_LOCATION);
28616 return NULL;
28619 return token;
28622 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28623 function-definition. */
28625 static bool
28626 cp_parser_token_starts_function_definition_p (cp_token* token)
28628 return (/* An ordinary function-body begins with an `{'. */
28629 token->type == CPP_OPEN_BRACE
28630 /* A ctor-initializer begins with a `:'. */
28631 || token->type == CPP_COLON
28632 /* A function-try-block begins with `try'. */
28633 || token->keyword == RID_TRY
28634 /* A function-transaction-block begins with `__transaction_atomic'
28635 or `__transaction_relaxed'. */
28636 || token->keyword == RID_TRANSACTION_ATOMIC
28637 || token->keyword == RID_TRANSACTION_RELAXED
28638 /* The named return value extension begins with `return'. */
28639 || token->keyword == RID_RETURN);
28642 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28643 definition. */
28645 static bool
28646 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28648 cp_token *token;
28650 token = cp_lexer_peek_token (parser->lexer);
28651 return (token->type == CPP_OPEN_BRACE
28652 || (token->type == CPP_COLON
28653 && !parser->colon_doesnt_start_class_def_p));
28656 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28657 C++0x) ending a template-argument. */
28659 static bool
28660 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28662 cp_token *token;
28664 token = cp_lexer_peek_token (parser->lexer);
28665 return (token->type == CPP_COMMA
28666 || token->type == CPP_GREATER
28667 || token->type == CPP_ELLIPSIS
28668 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28671 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28672 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28674 static bool
28675 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28676 size_t n)
28678 cp_token *token;
28680 token = cp_lexer_peek_nth_token (parser->lexer, n);
28681 if (token->type == CPP_LESS)
28682 return true;
28683 /* Check for the sequence `<::' in the original code. It would be lexed as
28684 `[:', where `[' is a digraph, and there is no whitespace before
28685 `:'. */
28686 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28688 cp_token *token2;
28689 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28690 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28691 return true;
28693 return false;
28696 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28697 or none_type otherwise. */
28699 static enum tag_types
28700 cp_parser_token_is_class_key (cp_token* token)
28702 switch (token->keyword)
28704 case RID_CLASS:
28705 return class_type;
28706 case RID_STRUCT:
28707 return record_type;
28708 case RID_UNION:
28709 return union_type;
28711 default:
28712 return none_type;
28716 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28717 or none_type otherwise or if the token is null. */
28719 static enum tag_types
28720 cp_parser_token_is_type_parameter_key (cp_token* token)
28722 if (!token)
28723 return none_type;
28725 switch (token->keyword)
28727 case RID_CLASS:
28728 return class_type;
28729 case RID_TYPENAME:
28730 return typename_type;
28732 default:
28733 return none_type;
28737 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28739 static void
28740 cp_parser_check_class_key (enum tag_types class_key, tree type)
28742 if (type == error_mark_node)
28743 return;
28744 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28746 if (permerror (input_location, "%qs tag used in naming %q#T",
28747 class_key == union_type ? "union"
28748 : class_key == record_type ? "struct" : "class",
28749 type))
28750 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28751 "%q#T was previously declared here", type);
28755 /* Issue an error message if DECL is redeclared with different
28756 access than its original declaration [class.access.spec/3].
28757 This applies to nested classes, nested class templates and
28758 enumerations [class.mem/1]. */
28760 static void
28761 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28763 if (!decl
28764 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28765 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28766 return;
28768 if ((TREE_PRIVATE (decl)
28769 != (current_access_specifier == access_private_node))
28770 || (TREE_PROTECTED (decl)
28771 != (current_access_specifier == access_protected_node)))
28772 error_at (location, "%qD redeclared with different access", decl);
28775 /* Look for the `template' keyword, as a syntactic disambiguator.
28776 Return TRUE iff it is present, in which case it will be
28777 consumed. */
28779 static bool
28780 cp_parser_optional_template_keyword (cp_parser *parser)
28782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28784 /* In C++98 the `template' keyword can only be used within templates;
28785 outside templates the parser can always figure out what is a
28786 template and what is not. In C++11, per the resolution of DR 468,
28787 `template' is allowed in cases where it is not strictly necessary. */
28788 if (!processing_template_decl
28789 && pedantic && cxx_dialect == cxx98)
28791 cp_token *token = cp_lexer_peek_token (parser->lexer);
28792 pedwarn (token->location, OPT_Wpedantic,
28793 "in C++98 %<template%> (as a disambiguator) is only "
28794 "allowed within templates");
28795 /* If this part of the token stream is rescanned, the same
28796 error message would be generated. So, we purge the token
28797 from the stream. */
28798 cp_lexer_purge_token (parser->lexer);
28799 return false;
28801 else
28803 /* Consume the `template' keyword. */
28804 cp_lexer_consume_token (parser->lexer);
28805 return true;
28808 return false;
28811 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28812 set PARSER->SCOPE, and perform other related actions. */
28814 static void
28815 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28817 struct tree_check *check_value;
28819 /* Get the stored value. */
28820 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28821 /* Set the scope from the stored value. */
28822 parser->scope = saved_checks_value (check_value);
28823 parser->qualifying_scope = check_value->qualifying_scope;
28824 parser->object_scope = NULL_TREE;
28827 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28828 encounter the end of a block before what we were looking for. */
28830 static bool
28831 cp_parser_cache_group (cp_parser *parser,
28832 enum cpp_ttype end,
28833 unsigned depth)
28835 while (true)
28837 cp_token *token = cp_lexer_peek_token (parser->lexer);
28839 /* Abort a parenthesized expression if we encounter a semicolon. */
28840 if ((end == CPP_CLOSE_PAREN || depth == 0)
28841 && token->type == CPP_SEMICOLON)
28842 return true;
28843 /* If we've reached the end of the file, stop. */
28844 if (token->type == CPP_EOF
28845 || (end != CPP_PRAGMA_EOL
28846 && token->type == CPP_PRAGMA_EOL))
28847 return true;
28848 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28849 /* We've hit the end of an enclosing block, so there's been some
28850 kind of syntax error. */
28851 return true;
28853 /* Consume the token. */
28854 cp_lexer_consume_token (parser->lexer);
28855 /* See if it starts a new group. */
28856 if (token->type == CPP_OPEN_BRACE)
28858 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28859 /* In theory this should probably check end == '}', but
28860 cp_parser_save_member_function_body needs it to exit
28861 after either '}' or ')' when called with ')'. */
28862 if (depth == 0)
28863 return false;
28865 else if (token->type == CPP_OPEN_PAREN)
28867 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28868 if (depth == 0 && end == CPP_CLOSE_PAREN)
28869 return false;
28871 else if (token->type == CPP_PRAGMA)
28872 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28873 else if (token->type == end)
28874 return false;
28878 /* Like above, for caching a default argument or NSDMI. Both of these are
28879 terminated by a non-nested comma, but it can be unclear whether or not a
28880 comma is nested in a template argument list unless we do more parsing.
28881 In order to handle this ambiguity, when we encounter a ',' after a '<'
28882 we try to parse what follows as a parameter-declaration-list (in the
28883 case of a default argument) or a member-declarator (in the case of an
28884 NSDMI). If that succeeds, then we stop caching. */
28886 static tree
28887 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28889 unsigned depth = 0;
28890 int maybe_template_id = 0;
28891 cp_token *first_token;
28892 cp_token *token;
28893 tree default_argument;
28895 /* Add tokens until we have processed the entire default
28896 argument. We add the range [first_token, token). */
28897 first_token = cp_lexer_peek_token (parser->lexer);
28898 if (first_token->type == CPP_OPEN_BRACE)
28900 /* For list-initialization, this is straightforward. */
28901 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28902 token = cp_lexer_peek_token (parser->lexer);
28904 else while (true)
28906 bool done = false;
28908 /* Peek at the next token. */
28909 token = cp_lexer_peek_token (parser->lexer);
28910 /* What we do depends on what token we have. */
28911 switch (token->type)
28913 /* In valid code, a default argument must be
28914 immediately followed by a `,' `)', or `...'. */
28915 case CPP_COMMA:
28916 if (depth == 0 && maybe_template_id)
28918 /* If we've seen a '<', we might be in a
28919 template-argument-list. Until Core issue 325 is
28920 resolved, we don't know how this situation ought
28921 to be handled, so try to DTRT. We check whether
28922 what comes after the comma is a valid parameter
28923 declaration list. If it is, then the comma ends
28924 the default argument; otherwise the default
28925 argument continues. */
28926 bool error = false;
28927 cp_token *peek;
28929 /* Set ITALP so cp_parser_parameter_declaration_list
28930 doesn't decide to commit to this parse. */
28931 bool saved_italp = parser->in_template_argument_list_p;
28932 parser->in_template_argument_list_p = true;
28934 cp_parser_parse_tentatively (parser);
28936 if (nsdmi)
28938 /* Parse declarators until we reach a non-comma or
28939 somthing that cannot be an initializer.
28940 Just checking whether we're looking at a single
28941 declarator is insufficient. Consider:
28942 int var = tuple<T,U>::x;
28943 The template parameter 'U' looks exactly like a
28944 declarator. */
28947 int ctor_dtor_or_conv_p;
28948 cp_lexer_consume_token (parser->lexer);
28949 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28950 &ctor_dtor_or_conv_p,
28951 /*parenthesized_p=*/NULL,
28952 /*member_p=*/true,
28953 /*friend_p=*/false);
28954 peek = cp_lexer_peek_token (parser->lexer);
28955 if (cp_parser_error_occurred (parser))
28956 break;
28958 while (peek->type == CPP_COMMA);
28959 /* If we met an '=' or ';' then the original comma
28960 was the end of the NSDMI. Otherwise assume
28961 we're still in the NSDMI. */
28962 error = (peek->type != CPP_EQ
28963 && peek->type != CPP_SEMICOLON);
28965 else
28967 cp_lexer_consume_token (parser->lexer);
28968 begin_scope (sk_function_parms, NULL_TREE);
28969 cp_parser_parameter_declaration_list (parser, &error);
28970 pop_bindings_and_leave_scope ();
28972 if (!cp_parser_error_occurred (parser) && !error)
28973 done = true;
28974 cp_parser_abort_tentative_parse (parser);
28976 parser->in_template_argument_list_p = saved_italp;
28977 break;
28979 /* FALLTHRU */
28980 case CPP_CLOSE_PAREN:
28981 case CPP_ELLIPSIS:
28982 /* If we run into a non-nested `;', `}', or `]',
28983 then the code is invalid -- but the default
28984 argument is certainly over. */
28985 case CPP_SEMICOLON:
28986 case CPP_CLOSE_BRACE:
28987 case CPP_CLOSE_SQUARE:
28988 if (depth == 0
28989 /* Handle correctly int n = sizeof ... ( p ); */
28990 && token->type != CPP_ELLIPSIS)
28991 done = true;
28992 /* Update DEPTH, if necessary. */
28993 else if (token->type == CPP_CLOSE_PAREN
28994 || token->type == CPP_CLOSE_BRACE
28995 || token->type == CPP_CLOSE_SQUARE)
28996 --depth;
28997 break;
28999 case CPP_OPEN_PAREN:
29000 case CPP_OPEN_SQUARE:
29001 case CPP_OPEN_BRACE:
29002 ++depth;
29003 break;
29005 case CPP_LESS:
29006 if (depth == 0)
29007 /* This might be the comparison operator, or it might
29008 start a template argument list. */
29009 ++maybe_template_id;
29010 break;
29012 case CPP_RSHIFT:
29013 if (cxx_dialect == cxx98)
29014 break;
29015 /* Fall through for C++0x, which treats the `>>'
29016 operator like two `>' tokens in certain
29017 cases. */
29018 gcc_fallthrough ();
29020 case CPP_GREATER:
29021 if (depth == 0)
29023 /* This might be an operator, or it might close a
29024 template argument list. But if a previous '<'
29025 started a template argument list, this will have
29026 closed it, so we can't be in one anymore. */
29027 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29028 if (maybe_template_id < 0)
29029 maybe_template_id = 0;
29031 break;
29033 /* If we run out of tokens, issue an error message. */
29034 case CPP_EOF:
29035 case CPP_PRAGMA_EOL:
29036 error_at (token->location, "file ends in default argument");
29037 return error_mark_node;
29039 case CPP_NAME:
29040 case CPP_SCOPE:
29041 /* In these cases, we should look for template-ids.
29042 For example, if the default argument is
29043 `X<int, double>()', we need to do name lookup to
29044 figure out whether or not `X' is a template; if
29045 so, the `,' does not end the default argument.
29047 That is not yet done. */
29048 break;
29050 default:
29051 break;
29054 /* If we've reached the end, stop. */
29055 if (done)
29056 break;
29058 /* Add the token to the token block. */
29059 token = cp_lexer_consume_token (parser->lexer);
29062 /* Create a DEFAULT_ARG to represent the unparsed default
29063 argument. */
29064 default_argument = make_node (DEFAULT_ARG);
29065 DEFARG_TOKENS (default_argument)
29066 = cp_token_cache_new (first_token, token);
29067 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29069 return default_argument;
29072 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29074 location_t
29075 defarg_location (tree default_argument)
29077 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29078 location_t start = tokens->first->location;
29079 location_t end = tokens->last->location;
29080 return make_location (start, start, end);
29083 /* Begin parsing tentatively. We always save tokens while parsing
29084 tentatively so that if the tentative parsing fails we can restore the
29085 tokens. */
29087 static void
29088 cp_parser_parse_tentatively (cp_parser* parser)
29090 /* Enter a new parsing context. */
29091 parser->context = cp_parser_context_new (parser->context);
29092 /* Begin saving tokens. */
29093 cp_lexer_save_tokens (parser->lexer);
29094 /* In order to avoid repetitive access control error messages,
29095 access checks are queued up until we are no longer parsing
29096 tentatively. */
29097 push_deferring_access_checks (dk_deferred);
29100 /* Commit to the currently active tentative parse. */
29102 static void
29103 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29105 cp_parser_context *context;
29106 cp_lexer *lexer;
29108 /* Mark all of the levels as committed. */
29109 lexer = parser->lexer;
29110 for (context = parser->context; context->next; context = context->next)
29112 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29113 break;
29114 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29115 while (!cp_lexer_saving_tokens (lexer))
29116 lexer = lexer->next;
29117 cp_lexer_commit_tokens (lexer);
29121 /* Commit to the topmost currently active tentative parse.
29123 Note that this function shouldn't be called when there are
29124 irreversible side-effects while in a tentative state. For
29125 example, we shouldn't create a permanent entry in the symbol
29126 table, or issue an error message that might not apply if the
29127 tentative parse is aborted. */
29129 static void
29130 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29132 cp_parser_context *context = parser->context;
29133 cp_lexer *lexer = parser->lexer;
29135 if (context)
29137 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29138 return;
29139 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29141 while (!cp_lexer_saving_tokens (lexer))
29142 lexer = lexer->next;
29143 cp_lexer_commit_tokens (lexer);
29147 /* Abort the currently active tentative parse. All consumed tokens
29148 will be rolled back, and no diagnostics will be issued. */
29150 static void
29151 cp_parser_abort_tentative_parse (cp_parser* parser)
29153 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29154 || errorcount > 0);
29155 cp_parser_simulate_error (parser);
29156 /* Now, pretend that we want to see if the construct was
29157 successfully parsed. */
29158 cp_parser_parse_definitely (parser);
29161 /* Stop parsing tentatively. If a parse error has occurred, restore the
29162 token stream. Otherwise, commit to the tokens we have consumed.
29163 Returns true if no error occurred; false otherwise. */
29165 static bool
29166 cp_parser_parse_definitely (cp_parser* parser)
29168 bool error_occurred;
29169 cp_parser_context *context;
29171 /* Remember whether or not an error occurred, since we are about to
29172 destroy that information. */
29173 error_occurred = cp_parser_error_occurred (parser);
29174 /* Remove the topmost context from the stack. */
29175 context = parser->context;
29176 parser->context = context->next;
29177 /* If no parse errors occurred, commit to the tentative parse. */
29178 if (!error_occurred)
29180 /* Commit to the tokens read tentatively, unless that was
29181 already done. */
29182 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29183 cp_lexer_commit_tokens (parser->lexer);
29185 pop_to_parent_deferring_access_checks ();
29187 /* Otherwise, if errors occurred, roll back our state so that things
29188 are just as they were before we began the tentative parse. */
29189 else
29191 cp_lexer_rollback_tokens (parser->lexer);
29192 pop_deferring_access_checks ();
29194 /* Add the context to the front of the free list. */
29195 context->next = cp_parser_context_free_list;
29196 cp_parser_context_free_list = context;
29198 return !error_occurred;
29201 /* Returns true if we are parsing tentatively and are not committed to
29202 this tentative parse. */
29204 static bool
29205 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29207 return (cp_parser_parsing_tentatively (parser)
29208 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29211 /* Returns nonzero iff an error has occurred during the most recent
29212 tentative parse. */
29214 static bool
29215 cp_parser_error_occurred (cp_parser* parser)
29217 return (cp_parser_parsing_tentatively (parser)
29218 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29221 /* Returns nonzero if GNU extensions are allowed. */
29223 static bool
29224 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29226 return parser->allow_gnu_extensions_p;
29229 /* Objective-C++ Productions */
29232 /* Parse an Objective-C expression, which feeds into a primary-expression
29233 above.
29235 objc-expression:
29236 objc-message-expression
29237 objc-string-literal
29238 objc-encode-expression
29239 objc-protocol-expression
29240 objc-selector-expression
29242 Returns a tree representation of the expression. */
29244 static cp_expr
29245 cp_parser_objc_expression (cp_parser* parser)
29247 /* Try to figure out what kind of declaration is present. */
29248 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29250 switch (kwd->type)
29252 case CPP_OPEN_SQUARE:
29253 return cp_parser_objc_message_expression (parser);
29255 case CPP_OBJC_STRING:
29256 kwd = cp_lexer_consume_token (parser->lexer);
29257 return objc_build_string_object (kwd->u.value);
29259 case CPP_KEYWORD:
29260 switch (kwd->keyword)
29262 case RID_AT_ENCODE:
29263 return cp_parser_objc_encode_expression (parser);
29265 case RID_AT_PROTOCOL:
29266 return cp_parser_objc_protocol_expression (parser);
29268 case RID_AT_SELECTOR:
29269 return cp_parser_objc_selector_expression (parser);
29271 default:
29272 break;
29274 default:
29275 error_at (kwd->location,
29276 "misplaced %<@%D%> Objective-C++ construct",
29277 kwd->u.value);
29278 cp_parser_skip_to_end_of_block_or_statement (parser);
29281 return error_mark_node;
29284 /* Parse an Objective-C message expression.
29286 objc-message-expression:
29287 [ objc-message-receiver objc-message-args ]
29289 Returns a representation of an Objective-C message. */
29291 static tree
29292 cp_parser_objc_message_expression (cp_parser* parser)
29294 tree receiver, messageargs;
29296 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29297 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29298 receiver = cp_parser_objc_message_receiver (parser);
29299 messageargs = cp_parser_objc_message_args (parser);
29300 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29301 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29303 tree result = objc_build_message_expr (receiver, messageargs);
29305 /* Construct a location e.g.
29306 [self func1:5]
29307 ^~~~~~~~~~~~~~
29308 ranging from the '[' to the ']', with the caret at the start. */
29309 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29310 protected_set_expr_location (result, combined_loc);
29312 return result;
29315 /* Parse an objc-message-receiver.
29317 objc-message-receiver:
29318 expression
29319 simple-type-specifier
29321 Returns a representation of the type or expression. */
29323 static tree
29324 cp_parser_objc_message_receiver (cp_parser* parser)
29326 tree rcv;
29328 /* An Objective-C message receiver may be either (1) a type
29329 or (2) an expression. */
29330 cp_parser_parse_tentatively (parser);
29331 rcv = cp_parser_expression (parser);
29333 /* If that worked out, fine. */
29334 if (cp_parser_parse_definitely (parser))
29335 return rcv;
29337 cp_parser_parse_tentatively (parser);
29338 rcv = cp_parser_simple_type_specifier (parser,
29339 /*decl_specs=*/NULL,
29340 CP_PARSER_FLAGS_NONE);
29342 if (cp_parser_parse_definitely (parser))
29343 return objc_get_class_reference (rcv);
29345 cp_parser_error (parser, "objective-c++ message receiver expected");
29346 return error_mark_node;
29349 /* Parse the arguments and selectors comprising an Objective-C message.
29351 objc-message-args:
29352 objc-selector
29353 objc-selector-args
29354 objc-selector-args , objc-comma-args
29356 objc-selector-args:
29357 objc-selector [opt] : assignment-expression
29358 objc-selector-args objc-selector [opt] : assignment-expression
29360 objc-comma-args:
29361 assignment-expression
29362 objc-comma-args , assignment-expression
29364 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29365 selector arguments and TREE_VALUE containing a list of comma
29366 arguments. */
29368 static tree
29369 cp_parser_objc_message_args (cp_parser* parser)
29371 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29372 bool maybe_unary_selector_p = true;
29373 cp_token *token = cp_lexer_peek_token (parser->lexer);
29375 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29377 tree selector = NULL_TREE, arg;
29379 if (token->type != CPP_COLON)
29380 selector = cp_parser_objc_selector (parser);
29382 /* Detect if we have a unary selector. */
29383 if (maybe_unary_selector_p
29384 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29385 return build_tree_list (selector, NULL_TREE);
29387 maybe_unary_selector_p = false;
29388 cp_parser_require (parser, CPP_COLON, RT_COLON);
29389 arg = cp_parser_assignment_expression (parser);
29391 sel_args
29392 = chainon (sel_args,
29393 build_tree_list (selector, arg));
29395 token = cp_lexer_peek_token (parser->lexer);
29398 /* Handle non-selector arguments, if any. */
29399 while (token->type == CPP_COMMA)
29401 tree arg;
29403 cp_lexer_consume_token (parser->lexer);
29404 arg = cp_parser_assignment_expression (parser);
29406 addl_args
29407 = chainon (addl_args,
29408 build_tree_list (NULL_TREE, arg));
29410 token = cp_lexer_peek_token (parser->lexer);
29413 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29415 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29416 return build_tree_list (error_mark_node, error_mark_node);
29419 return build_tree_list (sel_args, addl_args);
29422 /* Parse an Objective-C encode expression.
29424 objc-encode-expression:
29425 @encode objc-typename
29427 Returns an encoded representation of the type argument. */
29429 static cp_expr
29430 cp_parser_objc_encode_expression (cp_parser* parser)
29432 tree type;
29433 cp_token *token;
29434 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29436 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29437 matching_parens parens;
29438 parens.require_open (parser);
29439 token = cp_lexer_peek_token (parser->lexer);
29440 type = complete_type (cp_parser_type_id (parser));
29441 parens.require_close (parser);
29443 if (!type)
29445 error_at (token->location,
29446 "%<@encode%> must specify a type as an argument");
29447 return error_mark_node;
29450 /* This happens if we find @encode(T) (where T is a template
29451 typename or something dependent on a template typename) when
29452 parsing a template. In that case, we can't compile it
29453 immediately, but we rather create an AT_ENCODE_EXPR which will
29454 need to be instantiated when the template is used.
29456 if (dependent_type_p (type))
29458 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29459 TREE_READONLY (value) = 1;
29460 return value;
29464 /* Build a location of the form:
29465 @encode(int)
29466 ^~~~~~~~~~~~
29467 with caret==start at the @ token, finishing at the close paren. */
29468 location_t combined_loc
29469 = make_location (start_loc, start_loc,
29470 cp_lexer_previous_token (parser->lexer)->location);
29472 return cp_expr (objc_build_encode_expr (type), combined_loc);
29475 /* Parse an Objective-C @defs expression. */
29477 static tree
29478 cp_parser_objc_defs_expression (cp_parser *parser)
29480 tree name;
29482 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29483 matching_parens parens;
29484 parens.require_open (parser);
29485 name = cp_parser_identifier (parser);
29486 parens.require_close (parser);
29488 return objc_get_class_ivars (name);
29491 /* Parse an Objective-C protocol expression.
29493 objc-protocol-expression:
29494 @protocol ( identifier )
29496 Returns a representation of the protocol expression. */
29498 static tree
29499 cp_parser_objc_protocol_expression (cp_parser* parser)
29501 tree proto;
29502 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29504 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29505 matching_parens parens;
29506 parens.require_open (parser);
29507 proto = cp_parser_identifier (parser);
29508 parens.require_close (parser);
29510 /* Build a location of the form:
29511 @protocol(prot)
29512 ^~~~~~~~~~~~~~~
29513 with caret==start at the @ token, finishing at the close paren. */
29514 location_t combined_loc
29515 = make_location (start_loc, start_loc,
29516 cp_lexer_previous_token (parser->lexer)->location);
29517 tree result = objc_build_protocol_expr (proto);
29518 protected_set_expr_location (result, combined_loc);
29519 return result;
29522 /* Parse an Objective-C selector expression.
29524 objc-selector-expression:
29525 @selector ( objc-method-signature )
29527 objc-method-signature:
29528 objc-selector
29529 objc-selector-seq
29531 objc-selector-seq:
29532 objc-selector :
29533 objc-selector-seq objc-selector :
29535 Returns a representation of the method selector. */
29537 static tree
29538 cp_parser_objc_selector_expression (cp_parser* parser)
29540 tree sel_seq = NULL_TREE;
29541 bool maybe_unary_selector_p = true;
29542 cp_token *token;
29543 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29545 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29546 matching_parens parens;
29547 parens.require_open (parser);
29548 token = cp_lexer_peek_token (parser->lexer);
29550 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29551 || token->type == CPP_SCOPE)
29553 tree selector = NULL_TREE;
29555 if (token->type != CPP_COLON
29556 || token->type == CPP_SCOPE)
29557 selector = cp_parser_objc_selector (parser);
29559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29560 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29562 /* Detect if we have a unary selector. */
29563 if (maybe_unary_selector_p)
29565 sel_seq = selector;
29566 goto finish_selector;
29568 else
29570 cp_parser_error (parser, "expected %<:%>");
29573 maybe_unary_selector_p = false;
29574 token = cp_lexer_consume_token (parser->lexer);
29576 if (token->type == CPP_SCOPE)
29578 sel_seq
29579 = chainon (sel_seq,
29580 build_tree_list (selector, NULL_TREE));
29581 sel_seq
29582 = chainon (sel_seq,
29583 build_tree_list (NULL_TREE, NULL_TREE));
29585 else
29586 sel_seq
29587 = chainon (sel_seq,
29588 build_tree_list (selector, NULL_TREE));
29590 token = cp_lexer_peek_token (parser->lexer);
29593 finish_selector:
29594 parens.require_close (parser);
29597 /* Build a location of the form:
29598 @selector(func)
29599 ^~~~~~~~~~~~~~~
29600 with caret==start at the @ token, finishing at the close paren. */
29601 location_t combined_loc
29602 = make_location (loc, loc,
29603 cp_lexer_previous_token (parser->lexer)->location);
29604 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29605 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29606 protected_set_expr_location (result, combined_loc);
29607 return result;
29610 /* Parse a list of identifiers.
29612 objc-identifier-list:
29613 identifier
29614 objc-identifier-list , identifier
29616 Returns a TREE_LIST of identifier nodes. */
29618 static tree
29619 cp_parser_objc_identifier_list (cp_parser* parser)
29621 tree identifier;
29622 tree list;
29623 cp_token *sep;
29625 identifier = cp_parser_identifier (parser);
29626 if (identifier == error_mark_node)
29627 return error_mark_node;
29629 list = build_tree_list (NULL_TREE, identifier);
29630 sep = cp_lexer_peek_token (parser->lexer);
29632 while (sep->type == CPP_COMMA)
29634 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29635 identifier = cp_parser_identifier (parser);
29636 if (identifier == error_mark_node)
29637 return list;
29639 list = chainon (list, build_tree_list (NULL_TREE,
29640 identifier));
29641 sep = cp_lexer_peek_token (parser->lexer);
29644 return list;
29647 /* Parse an Objective-C alias declaration.
29649 objc-alias-declaration:
29650 @compatibility_alias identifier identifier ;
29652 This function registers the alias mapping with the Objective-C front end.
29653 It returns nothing. */
29655 static void
29656 cp_parser_objc_alias_declaration (cp_parser* parser)
29658 tree alias, orig;
29660 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29661 alias = cp_parser_identifier (parser);
29662 orig = cp_parser_identifier (parser);
29663 objc_declare_alias (alias, orig);
29664 cp_parser_consume_semicolon_at_end_of_statement (parser);
29667 /* Parse an Objective-C class forward-declaration.
29669 objc-class-declaration:
29670 @class objc-identifier-list ;
29672 The function registers the forward declarations with the Objective-C
29673 front end. It returns nothing. */
29675 static void
29676 cp_parser_objc_class_declaration (cp_parser* parser)
29678 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29679 while (true)
29681 tree id;
29683 id = cp_parser_identifier (parser);
29684 if (id == error_mark_node)
29685 break;
29687 objc_declare_class (id);
29689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29690 cp_lexer_consume_token (parser->lexer);
29691 else
29692 break;
29694 cp_parser_consume_semicolon_at_end_of_statement (parser);
29697 /* Parse a list of Objective-C protocol references.
29699 objc-protocol-refs-opt:
29700 objc-protocol-refs [opt]
29702 objc-protocol-refs:
29703 < objc-identifier-list >
29705 Returns a TREE_LIST of identifiers, if any. */
29707 static tree
29708 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29710 tree protorefs = NULL_TREE;
29712 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29714 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29715 protorefs = cp_parser_objc_identifier_list (parser);
29716 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29719 return protorefs;
29722 /* Parse a Objective-C visibility specification. */
29724 static void
29725 cp_parser_objc_visibility_spec (cp_parser* parser)
29727 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29729 switch (vis->keyword)
29731 case RID_AT_PRIVATE:
29732 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29733 break;
29734 case RID_AT_PROTECTED:
29735 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29736 break;
29737 case RID_AT_PUBLIC:
29738 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29739 break;
29740 case RID_AT_PACKAGE:
29741 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29742 break;
29743 default:
29744 return;
29747 /* Eat '@private'/'@protected'/'@public'. */
29748 cp_lexer_consume_token (parser->lexer);
29751 /* Parse an Objective-C method type. Return 'true' if it is a class
29752 (+) method, and 'false' if it is an instance (-) method. */
29754 static inline bool
29755 cp_parser_objc_method_type (cp_parser* parser)
29757 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29758 return true;
29759 else
29760 return false;
29763 /* Parse an Objective-C protocol qualifier. */
29765 static tree
29766 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29768 tree quals = NULL_TREE, node;
29769 cp_token *token = cp_lexer_peek_token (parser->lexer);
29771 node = token->u.value;
29773 while (node && identifier_p (node)
29774 && (node == ridpointers [(int) RID_IN]
29775 || node == ridpointers [(int) RID_OUT]
29776 || node == ridpointers [(int) RID_INOUT]
29777 || node == ridpointers [(int) RID_BYCOPY]
29778 || node == ridpointers [(int) RID_BYREF]
29779 || node == ridpointers [(int) RID_ONEWAY]))
29781 quals = tree_cons (NULL_TREE, node, quals);
29782 cp_lexer_consume_token (parser->lexer);
29783 token = cp_lexer_peek_token (parser->lexer);
29784 node = token->u.value;
29787 return quals;
29790 /* Parse an Objective-C typename. */
29792 static tree
29793 cp_parser_objc_typename (cp_parser* parser)
29795 tree type_name = NULL_TREE;
29797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29799 tree proto_quals, cp_type = NULL_TREE;
29801 matching_parens parens;
29802 parens.consume_open (parser); /* Eat '('. */
29803 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29805 /* An ObjC type name may consist of just protocol qualifiers, in which
29806 case the type shall default to 'id'. */
29807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29809 cp_type = cp_parser_type_id (parser);
29811 /* If the type could not be parsed, an error has already
29812 been produced. For error recovery, behave as if it had
29813 not been specified, which will use the default type
29814 'id'. */
29815 if (cp_type == error_mark_node)
29817 cp_type = NULL_TREE;
29818 /* We need to skip to the closing parenthesis as
29819 cp_parser_type_id() does not seem to do it for
29820 us. */
29821 cp_parser_skip_to_closing_parenthesis (parser,
29822 /*recovering=*/true,
29823 /*or_comma=*/false,
29824 /*consume_paren=*/false);
29828 parens.require_close (parser);
29829 type_name = build_tree_list (proto_quals, cp_type);
29832 return type_name;
29835 /* Check to see if TYPE refers to an Objective-C selector name. */
29837 static bool
29838 cp_parser_objc_selector_p (enum cpp_ttype type)
29840 return (type == CPP_NAME || type == CPP_KEYWORD
29841 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29842 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29843 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29844 || type == CPP_XOR || type == CPP_XOR_EQ);
29847 /* Parse an Objective-C selector. */
29849 static tree
29850 cp_parser_objc_selector (cp_parser* parser)
29852 cp_token *token = cp_lexer_consume_token (parser->lexer);
29854 if (!cp_parser_objc_selector_p (token->type))
29856 error_at (token->location, "invalid Objective-C++ selector name");
29857 return error_mark_node;
29860 /* C++ operator names are allowed to appear in ObjC selectors. */
29861 switch (token->type)
29863 case CPP_AND_AND: return get_identifier ("and");
29864 case CPP_AND_EQ: return get_identifier ("and_eq");
29865 case CPP_AND: return get_identifier ("bitand");
29866 case CPP_OR: return get_identifier ("bitor");
29867 case CPP_COMPL: return get_identifier ("compl");
29868 case CPP_NOT: return get_identifier ("not");
29869 case CPP_NOT_EQ: return get_identifier ("not_eq");
29870 case CPP_OR_OR: return get_identifier ("or");
29871 case CPP_OR_EQ: return get_identifier ("or_eq");
29872 case CPP_XOR: return get_identifier ("xor");
29873 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29874 default: return token->u.value;
29878 /* Parse an Objective-C params list. */
29880 static tree
29881 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29883 tree params = NULL_TREE;
29884 bool maybe_unary_selector_p = true;
29885 cp_token *token = cp_lexer_peek_token (parser->lexer);
29887 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29889 tree selector = NULL_TREE, type_name, identifier;
29890 tree parm_attr = NULL_TREE;
29892 if (token->keyword == RID_ATTRIBUTE)
29893 break;
29895 if (token->type != CPP_COLON)
29896 selector = cp_parser_objc_selector (parser);
29898 /* Detect if we have a unary selector. */
29899 if (maybe_unary_selector_p
29900 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29902 params = selector; /* Might be followed by attributes. */
29903 break;
29906 maybe_unary_selector_p = false;
29907 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29909 /* Something went quite wrong. There should be a colon
29910 here, but there is not. Stop parsing parameters. */
29911 break;
29913 type_name = cp_parser_objc_typename (parser);
29914 /* New ObjC allows attributes on parameters too. */
29915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29916 parm_attr = cp_parser_attributes_opt (parser);
29917 identifier = cp_parser_identifier (parser);
29919 params
29920 = chainon (params,
29921 objc_build_keyword_decl (selector,
29922 type_name,
29923 identifier,
29924 parm_attr));
29926 token = cp_lexer_peek_token (parser->lexer);
29929 if (params == NULL_TREE)
29931 cp_parser_error (parser, "objective-c++ method declaration is expected");
29932 return error_mark_node;
29935 /* We allow tail attributes for the method. */
29936 if (token->keyword == RID_ATTRIBUTE)
29938 *attributes = cp_parser_attributes_opt (parser);
29939 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29940 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29941 return params;
29942 cp_parser_error (parser,
29943 "method attributes must be specified at the end");
29944 return error_mark_node;
29947 if (params == NULL_TREE)
29949 cp_parser_error (parser, "objective-c++ method declaration is expected");
29950 return error_mark_node;
29952 return params;
29955 /* Parse the non-keyword Objective-C params. */
29957 static tree
29958 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29959 tree* attributes)
29961 tree params = make_node (TREE_LIST);
29962 cp_token *token = cp_lexer_peek_token (parser->lexer);
29963 *ellipsisp = false; /* Initially, assume no ellipsis. */
29965 while (token->type == CPP_COMMA)
29967 cp_parameter_declarator *parmdecl;
29968 tree parm;
29970 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29971 token = cp_lexer_peek_token (parser->lexer);
29973 if (token->type == CPP_ELLIPSIS)
29975 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29976 *ellipsisp = true;
29977 token = cp_lexer_peek_token (parser->lexer);
29978 break;
29981 /* TODO: parse attributes for tail parameters. */
29982 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29983 parm = grokdeclarator (parmdecl->declarator,
29984 &parmdecl->decl_specifiers,
29985 PARM, /*initialized=*/0,
29986 /*attrlist=*/NULL);
29988 chainon (params, build_tree_list (NULL_TREE, parm));
29989 token = cp_lexer_peek_token (parser->lexer);
29992 /* We allow tail attributes for the method. */
29993 if (token->keyword == RID_ATTRIBUTE)
29995 if (*attributes == NULL_TREE)
29997 *attributes = cp_parser_attributes_opt (parser);
29998 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29999 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30000 return params;
30002 else
30003 /* We have an error, but parse the attributes, so that we can
30004 carry on. */
30005 *attributes = cp_parser_attributes_opt (parser);
30007 cp_parser_error (parser,
30008 "method attributes must be specified at the end");
30009 return error_mark_node;
30012 return params;
30015 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30017 static void
30018 cp_parser_objc_interstitial_code (cp_parser* parser)
30020 cp_token *token = cp_lexer_peek_token (parser->lexer);
30022 /* If the next token is `extern' and the following token is a string
30023 literal, then we have a linkage specification. */
30024 if (token->keyword == RID_EXTERN
30025 && cp_parser_is_pure_string_literal
30026 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30027 cp_parser_linkage_specification (parser);
30028 /* Handle #pragma, if any. */
30029 else if (token->type == CPP_PRAGMA)
30030 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30031 /* Allow stray semicolons. */
30032 else if (token->type == CPP_SEMICOLON)
30033 cp_lexer_consume_token (parser->lexer);
30034 /* Mark methods as optional or required, when building protocols. */
30035 else if (token->keyword == RID_AT_OPTIONAL)
30037 cp_lexer_consume_token (parser->lexer);
30038 objc_set_method_opt (true);
30040 else if (token->keyword == RID_AT_REQUIRED)
30042 cp_lexer_consume_token (parser->lexer);
30043 objc_set_method_opt (false);
30045 else if (token->keyword == RID_NAMESPACE)
30046 cp_parser_namespace_definition (parser);
30047 /* Other stray characters must generate errors. */
30048 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30050 cp_lexer_consume_token (parser->lexer);
30051 error ("stray %qs between Objective-C++ methods",
30052 token->type == CPP_OPEN_BRACE ? "{" : "}");
30054 /* Finally, try to parse a block-declaration, or a function-definition. */
30055 else
30056 cp_parser_block_declaration (parser, /*statement_p=*/false);
30059 /* Parse a method signature. */
30061 static tree
30062 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30064 tree rettype, kwdparms, optparms;
30065 bool ellipsis = false;
30066 bool is_class_method;
30068 is_class_method = cp_parser_objc_method_type (parser);
30069 rettype = cp_parser_objc_typename (parser);
30070 *attributes = NULL_TREE;
30071 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30072 if (kwdparms == error_mark_node)
30073 return error_mark_node;
30074 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30075 if (optparms == error_mark_node)
30076 return error_mark_node;
30078 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30081 static bool
30082 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30084 tree tattr;
30085 cp_lexer_save_tokens (parser->lexer);
30086 tattr = cp_parser_attributes_opt (parser);
30087 gcc_assert (tattr) ;
30089 /* If the attributes are followed by a method introducer, this is not allowed.
30090 Dump the attributes and flag the situation. */
30091 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30092 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30093 return true;
30095 /* Otherwise, the attributes introduce some interstitial code, possibly so
30096 rewind to allow that check. */
30097 cp_lexer_rollback_tokens (parser->lexer);
30098 return false;
30101 /* Parse an Objective-C method prototype list. */
30103 static void
30104 cp_parser_objc_method_prototype_list (cp_parser* parser)
30106 cp_token *token = cp_lexer_peek_token (parser->lexer);
30108 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30110 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30112 tree attributes, sig;
30113 bool is_class_method;
30114 if (token->type == CPP_PLUS)
30115 is_class_method = true;
30116 else
30117 is_class_method = false;
30118 sig = cp_parser_objc_method_signature (parser, &attributes);
30119 if (sig == error_mark_node)
30121 cp_parser_skip_to_end_of_block_or_statement (parser);
30122 token = cp_lexer_peek_token (parser->lexer);
30123 continue;
30125 objc_add_method_declaration (is_class_method, sig, attributes);
30126 cp_parser_consume_semicolon_at_end_of_statement (parser);
30128 else if (token->keyword == RID_AT_PROPERTY)
30129 cp_parser_objc_at_property_declaration (parser);
30130 else if (token->keyword == RID_ATTRIBUTE
30131 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30132 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30133 OPT_Wattributes,
30134 "prefix attributes are ignored for methods");
30135 else
30136 /* Allow for interspersed non-ObjC++ code. */
30137 cp_parser_objc_interstitial_code (parser);
30139 token = cp_lexer_peek_token (parser->lexer);
30142 if (token->type != CPP_EOF)
30143 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30144 else
30145 cp_parser_error (parser, "expected %<@end%>");
30147 objc_finish_interface ();
30150 /* Parse an Objective-C method definition list. */
30152 static void
30153 cp_parser_objc_method_definition_list (cp_parser* parser)
30155 cp_token *token = cp_lexer_peek_token (parser->lexer);
30157 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30159 tree meth;
30161 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30163 cp_token *ptk;
30164 tree sig, attribute;
30165 bool is_class_method;
30166 if (token->type == CPP_PLUS)
30167 is_class_method = true;
30168 else
30169 is_class_method = false;
30170 push_deferring_access_checks (dk_deferred);
30171 sig = cp_parser_objc_method_signature (parser, &attribute);
30172 if (sig == error_mark_node)
30174 cp_parser_skip_to_end_of_block_or_statement (parser);
30175 token = cp_lexer_peek_token (parser->lexer);
30176 continue;
30178 objc_start_method_definition (is_class_method, sig, attribute,
30179 NULL_TREE);
30181 /* For historical reasons, we accept an optional semicolon. */
30182 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30183 cp_lexer_consume_token (parser->lexer);
30185 ptk = cp_lexer_peek_token (parser->lexer);
30186 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30187 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30189 perform_deferred_access_checks (tf_warning_or_error);
30190 stop_deferring_access_checks ();
30191 meth = cp_parser_function_definition_after_declarator (parser,
30192 false);
30193 pop_deferring_access_checks ();
30194 objc_finish_method_definition (meth);
30197 /* The following case will be removed once @synthesize is
30198 completely implemented. */
30199 else if (token->keyword == RID_AT_PROPERTY)
30200 cp_parser_objc_at_property_declaration (parser);
30201 else if (token->keyword == RID_AT_SYNTHESIZE)
30202 cp_parser_objc_at_synthesize_declaration (parser);
30203 else if (token->keyword == RID_AT_DYNAMIC)
30204 cp_parser_objc_at_dynamic_declaration (parser);
30205 else if (token->keyword == RID_ATTRIBUTE
30206 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30207 warning_at (token->location, OPT_Wattributes,
30208 "prefix attributes are ignored for methods");
30209 else
30210 /* Allow for interspersed non-ObjC++ code. */
30211 cp_parser_objc_interstitial_code (parser);
30213 token = cp_lexer_peek_token (parser->lexer);
30216 if (token->type != CPP_EOF)
30217 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30218 else
30219 cp_parser_error (parser, "expected %<@end%>");
30221 objc_finish_implementation ();
30224 /* Parse Objective-C ivars. */
30226 static void
30227 cp_parser_objc_class_ivars (cp_parser* parser)
30229 cp_token *token = cp_lexer_peek_token (parser->lexer);
30231 if (token->type != CPP_OPEN_BRACE)
30232 return; /* No ivars specified. */
30234 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30235 token = cp_lexer_peek_token (parser->lexer);
30237 while (token->type != CPP_CLOSE_BRACE
30238 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30240 cp_decl_specifier_seq declspecs;
30241 int decl_class_or_enum_p;
30242 tree prefix_attributes;
30244 cp_parser_objc_visibility_spec (parser);
30246 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30247 break;
30249 cp_parser_decl_specifier_seq (parser,
30250 CP_PARSER_FLAGS_OPTIONAL,
30251 &declspecs,
30252 &decl_class_or_enum_p);
30254 /* auto, register, static, extern, mutable. */
30255 if (declspecs.storage_class != sc_none)
30257 cp_parser_error (parser, "invalid type for instance variable");
30258 declspecs.storage_class = sc_none;
30261 /* thread_local. */
30262 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30264 cp_parser_error (parser, "invalid type for instance variable");
30265 declspecs.locations[ds_thread] = 0;
30268 /* typedef. */
30269 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30271 cp_parser_error (parser, "invalid type for instance variable");
30272 declspecs.locations[ds_typedef] = 0;
30275 prefix_attributes = declspecs.attributes;
30276 declspecs.attributes = NULL_TREE;
30278 /* Keep going until we hit the `;' at the end of the
30279 declaration. */
30280 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30282 tree width = NULL_TREE, attributes, first_attribute, decl;
30283 cp_declarator *declarator = NULL;
30284 int ctor_dtor_or_conv_p;
30286 /* Check for a (possibly unnamed) bitfield declaration. */
30287 token = cp_lexer_peek_token (parser->lexer);
30288 if (token->type == CPP_COLON)
30289 goto eat_colon;
30291 if (token->type == CPP_NAME
30292 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30293 == CPP_COLON))
30295 /* Get the name of the bitfield. */
30296 declarator = make_id_declarator (NULL_TREE,
30297 cp_parser_identifier (parser),
30298 sfk_none);
30300 eat_colon:
30301 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30302 /* Get the width of the bitfield. */
30303 width
30304 = cp_parser_constant_expression (parser);
30306 else
30308 /* Parse the declarator. */
30309 declarator
30310 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30311 &ctor_dtor_or_conv_p,
30312 /*parenthesized_p=*/NULL,
30313 /*member_p=*/false,
30314 /*friend_p=*/false);
30317 /* Look for attributes that apply to the ivar. */
30318 attributes = cp_parser_attributes_opt (parser);
30319 /* Remember which attributes are prefix attributes and
30320 which are not. */
30321 first_attribute = attributes;
30322 /* Combine the attributes. */
30323 attributes = chainon (prefix_attributes, attributes);
30325 if (width)
30326 /* Create the bitfield declaration. */
30327 decl = grokbitfield (declarator, &declspecs,
30328 width, NULL_TREE, attributes);
30329 else
30330 decl = grokfield (declarator, &declspecs,
30331 NULL_TREE, /*init_const_expr_p=*/false,
30332 NULL_TREE, attributes);
30334 /* Add the instance variable. */
30335 if (decl != error_mark_node && decl != NULL_TREE)
30336 objc_add_instance_variable (decl);
30338 /* Reset PREFIX_ATTRIBUTES. */
30339 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30340 attributes = TREE_CHAIN (attributes);
30341 if (attributes)
30342 TREE_CHAIN (attributes) = NULL_TREE;
30344 token = cp_lexer_peek_token (parser->lexer);
30346 if (token->type == CPP_COMMA)
30348 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30349 continue;
30351 break;
30354 cp_parser_consume_semicolon_at_end_of_statement (parser);
30355 token = cp_lexer_peek_token (parser->lexer);
30358 if (token->keyword == RID_AT_END)
30359 cp_parser_error (parser, "expected %<}%>");
30361 /* Do not consume the RID_AT_END, so it will be read again as terminating
30362 the @interface of @implementation. */
30363 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30364 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30366 /* For historical reasons, we accept an optional semicolon. */
30367 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30368 cp_lexer_consume_token (parser->lexer);
30371 /* Parse an Objective-C protocol declaration. */
30373 static void
30374 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30376 tree proto, protorefs;
30377 cp_token *tok;
30379 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30382 tok = cp_lexer_peek_token (parser->lexer);
30383 error_at (tok->location, "identifier expected after %<@protocol%>");
30384 cp_parser_consume_semicolon_at_end_of_statement (parser);
30385 return;
30388 /* See if we have a forward declaration or a definition. */
30389 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30391 /* Try a forward declaration first. */
30392 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30394 while (true)
30396 tree id;
30398 id = cp_parser_identifier (parser);
30399 if (id == error_mark_node)
30400 break;
30402 objc_declare_protocol (id, attributes);
30404 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30405 cp_lexer_consume_token (parser->lexer);
30406 else
30407 break;
30409 cp_parser_consume_semicolon_at_end_of_statement (parser);
30412 /* Ok, we got a full-fledged definition (or at least should). */
30413 else
30415 proto = cp_parser_identifier (parser);
30416 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30417 objc_start_protocol (proto, protorefs, attributes);
30418 cp_parser_objc_method_prototype_list (parser);
30422 /* Parse an Objective-C superclass or category. */
30424 static void
30425 cp_parser_objc_superclass_or_category (cp_parser *parser,
30426 bool iface_p,
30427 tree *super,
30428 tree *categ, bool *is_class_extension)
30430 cp_token *next = cp_lexer_peek_token (parser->lexer);
30432 *super = *categ = NULL_TREE;
30433 *is_class_extension = false;
30434 if (next->type == CPP_COLON)
30436 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30437 *super = cp_parser_identifier (parser);
30439 else if (next->type == CPP_OPEN_PAREN)
30441 matching_parens parens;
30442 parens.consume_open (parser); /* Eat '('. */
30444 /* If there is no category name, and this is an @interface, we
30445 have a class extension. */
30446 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30448 *categ = NULL_TREE;
30449 *is_class_extension = true;
30451 else
30452 *categ = cp_parser_identifier (parser);
30454 parens.require_close (parser);
30458 /* Parse an Objective-C class interface. */
30460 static void
30461 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30463 tree name, super, categ, protos;
30464 bool is_class_extension;
30466 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30467 name = cp_parser_identifier (parser);
30468 if (name == error_mark_node)
30470 /* It's hard to recover because even if valid @interface stuff
30471 is to follow, we can't compile it (or validate it) if we
30472 don't even know which class it refers to. Let's assume this
30473 was a stray '@interface' token in the stream and skip it.
30475 return;
30477 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30478 &is_class_extension);
30479 protos = cp_parser_objc_protocol_refs_opt (parser);
30481 /* We have either a class or a category on our hands. */
30482 if (categ || is_class_extension)
30483 objc_start_category_interface (name, categ, protos, attributes);
30484 else
30486 objc_start_class_interface (name, super, protos, attributes);
30487 /* Handle instance variable declarations, if any. */
30488 cp_parser_objc_class_ivars (parser);
30489 objc_continue_interface ();
30492 cp_parser_objc_method_prototype_list (parser);
30495 /* Parse an Objective-C class implementation. */
30497 static void
30498 cp_parser_objc_class_implementation (cp_parser* parser)
30500 tree name, super, categ;
30501 bool is_class_extension;
30503 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30504 name = cp_parser_identifier (parser);
30505 if (name == error_mark_node)
30507 /* It's hard to recover because even if valid @implementation
30508 stuff is to follow, we can't compile it (or validate it) if
30509 we don't even know which class it refers to. Let's assume
30510 this was a stray '@implementation' token in the stream and
30511 skip it.
30513 return;
30515 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30516 &is_class_extension);
30518 /* We have either a class or a category on our hands. */
30519 if (categ)
30520 objc_start_category_implementation (name, categ);
30521 else
30523 objc_start_class_implementation (name, super);
30524 /* Handle instance variable declarations, if any. */
30525 cp_parser_objc_class_ivars (parser);
30526 objc_continue_implementation ();
30529 cp_parser_objc_method_definition_list (parser);
30532 /* Consume the @end token and finish off the implementation. */
30534 static void
30535 cp_parser_objc_end_implementation (cp_parser* parser)
30537 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30538 objc_finish_implementation ();
30541 /* Parse an Objective-C declaration. */
30543 static void
30544 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30546 /* Try to figure out what kind of declaration is present. */
30547 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30549 if (attributes)
30550 switch (kwd->keyword)
30552 case RID_AT_ALIAS:
30553 case RID_AT_CLASS:
30554 case RID_AT_END:
30555 error_at (kwd->location, "attributes may not be specified before"
30556 " the %<@%D%> Objective-C++ keyword",
30557 kwd->u.value);
30558 attributes = NULL;
30559 break;
30560 case RID_AT_IMPLEMENTATION:
30561 warning_at (kwd->location, OPT_Wattributes,
30562 "prefix attributes are ignored before %<@%D%>",
30563 kwd->u.value);
30564 attributes = NULL;
30565 default:
30566 break;
30569 switch (kwd->keyword)
30571 case RID_AT_ALIAS:
30572 cp_parser_objc_alias_declaration (parser);
30573 break;
30574 case RID_AT_CLASS:
30575 cp_parser_objc_class_declaration (parser);
30576 break;
30577 case RID_AT_PROTOCOL:
30578 cp_parser_objc_protocol_declaration (parser, attributes);
30579 break;
30580 case RID_AT_INTERFACE:
30581 cp_parser_objc_class_interface (parser, attributes);
30582 break;
30583 case RID_AT_IMPLEMENTATION:
30584 cp_parser_objc_class_implementation (parser);
30585 break;
30586 case RID_AT_END:
30587 cp_parser_objc_end_implementation (parser);
30588 break;
30589 default:
30590 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30591 kwd->u.value);
30592 cp_parser_skip_to_end_of_block_or_statement (parser);
30596 /* Parse an Objective-C try-catch-finally statement.
30598 objc-try-catch-finally-stmt:
30599 @try compound-statement objc-catch-clause-seq [opt]
30600 objc-finally-clause [opt]
30602 objc-catch-clause-seq:
30603 objc-catch-clause objc-catch-clause-seq [opt]
30605 objc-catch-clause:
30606 @catch ( objc-exception-declaration ) compound-statement
30608 objc-finally-clause:
30609 @finally compound-statement
30611 objc-exception-declaration:
30612 parameter-declaration
30613 '...'
30615 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30617 Returns NULL_TREE.
30619 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30620 for C. Keep them in sync. */
30622 static tree
30623 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30625 location_t location;
30626 tree stmt;
30628 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30629 location = cp_lexer_peek_token (parser->lexer)->location;
30630 objc_maybe_warn_exceptions (location);
30631 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30632 node, lest it get absorbed into the surrounding block. */
30633 stmt = push_stmt_list ();
30634 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30635 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30637 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30639 cp_parameter_declarator *parm;
30640 tree parameter_declaration = error_mark_node;
30641 bool seen_open_paren = false;
30642 matching_parens parens;
30644 cp_lexer_consume_token (parser->lexer);
30645 if (parens.require_open (parser))
30646 seen_open_paren = true;
30647 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30649 /* We have "@catch (...)" (where the '...' are literally
30650 what is in the code). Skip the '...'.
30651 parameter_declaration is set to NULL_TREE, and
30652 objc_being_catch_clauses() knows that that means
30653 '...'. */
30654 cp_lexer_consume_token (parser->lexer);
30655 parameter_declaration = NULL_TREE;
30657 else
30659 /* We have "@catch (NSException *exception)" or something
30660 like that. Parse the parameter declaration. */
30661 parm = cp_parser_parameter_declaration (parser, false, NULL);
30662 if (parm == NULL)
30663 parameter_declaration = error_mark_node;
30664 else
30665 parameter_declaration = grokdeclarator (parm->declarator,
30666 &parm->decl_specifiers,
30667 PARM, /*initialized=*/0,
30668 /*attrlist=*/NULL);
30670 if (seen_open_paren)
30671 parens.require_close (parser);
30672 else
30674 /* If there was no open parenthesis, we are recovering from
30675 an error, and we are trying to figure out what mistake
30676 the user has made. */
30678 /* If there is an immediate closing parenthesis, the user
30679 probably forgot the opening one (ie, they typed "@catch
30680 NSException *e)". Parse the closing parenthesis and keep
30681 going. */
30682 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30683 cp_lexer_consume_token (parser->lexer);
30685 /* If these is no immediate closing parenthesis, the user
30686 probably doesn't know that parenthesis are required at
30687 all (ie, they typed "@catch NSException *e"). So, just
30688 forget about the closing parenthesis and keep going. */
30690 objc_begin_catch_clause (parameter_declaration);
30691 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30692 objc_finish_catch_clause ();
30694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30696 cp_lexer_consume_token (parser->lexer);
30697 location = cp_lexer_peek_token (parser->lexer)->location;
30698 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30699 node, lest it get absorbed into the surrounding block. */
30700 stmt = push_stmt_list ();
30701 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30702 objc_build_finally_clause (location, pop_stmt_list (stmt));
30705 return objc_finish_try_stmt ();
30708 /* Parse an Objective-C synchronized statement.
30710 objc-synchronized-stmt:
30711 @synchronized ( expression ) compound-statement
30713 Returns NULL_TREE. */
30715 static tree
30716 cp_parser_objc_synchronized_statement (cp_parser *parser)
30718 location_t location;
30719 tree lock, stmt;
30721 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30723 location = cp_lexer_peek_token (parser->lexer)->location;
30724 objc_maybe_warn_exceptions (location);
30725 matching_parens parens;
30726 parens.require_open (parser);
30727 lock = cp_parser_expression (parser);
30728 parens.require_close (parser);
30730 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30731 node, lest it get absorbed into the surrounding block. */
30732 stmt = push_stmt_list ();
30733 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30735 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30738 /* Parse an Objective-C throw statement.
30740 objc-throw-stmt:
30741 @throw assignment-expression [opt] ;
30743 Returns a constructed '@throw' statement. */
30745 static tree
30746 cp_parser_objc_throw_statement (cp_parser *parser)
30748 tree expr = NULL_TREE;
30749 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30751 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30754 expr = cp_parser_expression (parser);
30756 cp_parser_consume_semicolon_at_end_of_statement (parser);
30758 return objc_build_throw_stmt (loc, expr);
30761 /* Parse an Objective-C statement. */
30763 static tree
30764 cp_parser_objc_statement (cp_parser * parser)
30766 /* Try to figure out what kind of declaration is present. */
30767 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30769 switch (kwd->keyword)
30771 case RID_AT_TRY:
30772 return cp_parser_objc_try_catch_finally_statement (parser);
30773 case RID_AT_SYNCHRONIZED:
30774 return cp_parser_objc_synchronized_statement (parser);
30775 case RID_AT_THROW:
30776 return cp_parser_objc_throw_statement (parser);
30777 default:
30778 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30779 kwd->u.value);
30780 cp_parser_skip_to_end_of_block_or_statement (parser);
30783 return error_mark_node;
30786 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30787 look ahead to see if an objc keyword follows the attributes. This
30788 is to detect the use of prefix attributes on ObjC @interface and
30789 @protocol. */
30791 static bool
30792 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30794 cp_lexer_save_tokens (parser->lexer);
30795 *attrib = cp_parser_attributes_opt (parser);
30796 gcc_assert (*attrib);
30797 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30799 cp_lexer_commit_tokens (parser->lexer);
30800 return true;
30802 cp_lexer_rollback_tokens (parser->lexer);
30803 return false;
30806 /* This routine is a minimal replacement for
30807 c_parser_struct_declaration () used when parsing the list of
30808 types/names or ObjC++ properties. For example, when parsing the
30809 code
30811 @property (readonly) int a, b, c;
30813 this function is responsible for parsing "int a, int b, int c" and
30814 returning the declarations as CHAIN of DECLs.
30816 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30817 similar parsing. */
30818 static tree
30819 cp_parser_objc_struct_declaration (cp_parser *parser)
30821 tree decls = NULL_TREE;
30822 cp_decl_specifier_seq declspecs;
30823 int decl_class_or_enum_p;
30824 tree prefix_attributes;
30826 cp_parser_decl_specifier_seq (parser,
30827 CP_PARSER_FLAGS_NONE,
30828 &declspecs,
30829 &decl_class_or_enum_p);
30831 if (declspecs.type == error_mark_node)
30832 return error_mark_node;
30834 /* auto, register, static, extern, mutable. */
30835 if (declspecs.storage_class != sc_none)
30837 cp_parser_error (parser, "invalid type for property");
30838 declspecs.storage_class = sc_none;
30841 /* thread_local. */
30842 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30844 cp_parser_error (parser, "invalid type for property");
30845 declspecs.locations[ds_thread] = 0;
30848 /* typedef. */
30849 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30851 cp_parser_error (parser, "invalid type for property");
30852 declspecs.locations[ds_typedef] = 0;
30855 prefix_attributes = declspecs.attributes;
30856 declspecs.attributes = NULL_TREE;
30858 /* Keep going until we hit the `;' at the end of the declaration. */
30859 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30861 tree attributes, first_attribute, decl;
30862 cp_declarator *declarator;
30863 cp_token *token;
30865 /* Parse the declarator. */
30866 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30867 NULL, NULL, false, false);
30869 /* Look for attributes that apply to the ivar. */
30870 attributes = cp_parser_attributes_opt (parser);
30871 /* Remember which attributes are prefix attributes and
30872 which are not. */
30873 first_attribute = attributes;
30874 /* Combine the attributes. */
30875 attributes = chainon (prefix_attributes, attributes);
30877 decl = grokfield (declarator, &declspecs,
30878 NULL_TREE, /*init_const_expr_p=*/false,
30879 NULL_TREE, attributes);
30881 if (decl == error_mark_node || decl == NULL_TREE)
30882 return error_mark_node;
30884 /* Reset PREFIX_ATTRIBUTES. */
30885 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30886 attributes = TREE_CHAIN (attributes);
30887 if (attributes)
30888 TREE_CHAIN (attributes) = NULL_TREE;
30890 DECL_CHAIN (decl) = decls;
30891 decls = decl;
30893 token = cp_lexer_peek_token (parser->lexer);
30894 if (token->type == CPP_COMMA)
30896 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30897 continue;
30899 else
30900 break;
30902 return decls;
30905 /* Parse an Objective-C @property declaration. The syntax is:
30907 objc-property-declaration:
30908 '@property' objc-property-attributes[opt] struct-declaration ;
30910 objc-property-attributes:
30911 '(' objc-property-attribute-list ')'
30913 objc-property-attribute-list:
30914 objc-property-attribute
30915 objc-property-attribute-list, objc-property-attribute
30917 objc-property-attribute
30918 'getter' = identifier
30919 'setter' = identifier
30920 'readonly'
30921 'readwrite'
30922 'assign'
30923 'retain'
30924 'copy'
30925 'nonatomic'
30927 For example:
30928 @property NSString *name;
30929 @property (readonly) id object;
30930 @property (retain, nonatomic, getter=getTheName) id name;
30931 @property int a, b, c;
30933 PS: This function is identical to
30934 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30935 static void
30936 cp_parser_objc_at_property_declaration (cp_parser *parser)
30938 /* The following variables hold the attributes of the properties as
30939 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30940 seen. When we see an attribute, we set them to 'true' (if they
30941 are boolean properties) or to the identifier (if they have an
30942 argument, ie, for getter and setter). Note that here we only
30943 parse the list of attributes, check the syntax and accumulate the
30944 attributes that we find. objc_add_property_declaration() will
30945 then process the information. */
30946 bool property_assign = false;
30947 bool property_copy = false;
30948 tree property_getter_ident = NULL_TREE;
30949 bool property_nonatomic = false;
30950 bool property_readonly = false;
30951 bool property_readwrite = false;
30952 bool property_retain = false;
30953 tree property_setter_ident = NULL_TREE;
30955 /* 'properties' is the list of properties that we read. Usually a
30956 single one, but maybe more (eg, in "@property int a, b, c;" there
30957 are three). */
30958 tree properties;
30959 location_t loc;
30961 loc = cp_lexer_peek_token (parser->lexer)->location;
30963 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30965 /* Parse the optional attribute list... */
30966 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30968 /* Eat the '('. */
30969 matching_parens parens;
30970 parens.consume_open (parser);
30972 while (true)
30974 bool syntax_error = false;
30975 cp_token *token = cp_lexer_peek_token (parser->lexer);
30976 enum rid keyword;
30978 if (token->type != CPP_NAME)
30980 cp_parser_error (parser, "expected identifier");
30981 break;
30983 keyword = C_RID_CODE (token->u.value);
30984 cp_lexer_consume_token (parser->lexer);
30985 switch (keyword)
30987 case RID_ASSIGN: property_assign = true; break;
30988 case RID_COPY: property_copy = true; break;
30989 case RID_NONATOMIC: property_nonatomic = true; break;
30990 case RID_READONLY: property_readonly = true; break;
30991 case RID_READWRITE: property_readwrite = true; break;
30992 case RID_RETAIN: property_retain = true; break;
30994 case RID_GETTER:
30995 case RID_SETTER:
30996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30998 if (keyword == RID_GETTER)
30999 cp_parser_error (parser,
31000 "missing %<=%> (after %<getter%> attribute)");
31001 else
31002 cp_parser_error (parser,
31003 "missing %<=%> (after %<setter%> attribute)");
31004 syntax_error = true;
31005 break;
31007 cp_lexer_consume_token (parser->lexer); /* eat the = */
31008 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31010 cp_parser_error (parser, "expected identifier");
31011 syntax_error = true;
31012 break;
31014 if (keyword == RID_SETTER)
31016 if (property_setter_ident != NULL_TREE)
31018 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31019 cp_lexer_consume_token (parser->lexer);
31021 else
31022 property_setter_ident = cp_parser_objc_selector (parser);
31023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31024 cp_parser_error (parser, "setter name must terminate with %<:%>");
31025 else
31026 cp_lexer_consume_token (parser->lexer);
31028 else
31030 if (property_getter_ident != NULL_TREE)
31032 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31033 cp_lexer_consume_token (parser->lexer);
31035 else
31036 property_getter_ident = cp_parser_objc_selector (parser);
31038 break;
31039 default:
31040 cp_parser_error (parser, "unknown property attribute");
31041 syntax_error = true;
31042 break;
31045 if (syntax_error)
31046 break;
31048 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31049 cp_lexer_consume_token (parser->lexer);
31050 else
31051 break;
31054 /* FIXME: "@property (setter, assign);" will generate a spurious
31055 "error: expected ‘)’ before ‘,’ token". This is because
31056 cp_parser_require, unlike the C counterpart, will produce an
31057 error even if we are in error recovery. */
31058 if (!parens.require_close (parser))
31060 cp_parser_skip_to_closing_parenthesis (parser,
31061 /*recovering=*/true,
31062 /*or_comma=*/false,
31063 /*consume_paren=*/true);
31067 /* ... and the property declaration(s). */
31068 properties = cp_parser_objc_struct_declaration (parser);
31070 if (properties == error_mark_node)
31072 cp_parser_skip_to_end_of_statement (parser);
31073 /* If the next token is now a `;', consume it. */
31074 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31075 cp_lexer_consume_token (parser->lexer);
31076 return;
31079 if (properties == NULL_TREE)
31080 cp_parser_error (parser, "expected identifier");
31081 else
31083 /* Comma-separated properties are chained together in
31084 reverse order; add them one by one. */
31085 properties = nreverse (properties);
31087 for (; properties; properties = TREE_CHAIN (properties))
31088 objc_add_property_declaration (loc, copy_node (properties),
31089 property_readonly, property_readwrite,
31090 property_assign, property_retain,
31091 property_copy, property_nonatomic,
31092 property_getter_ident, property_setter_ident);
31095 cp_parser_consume_semicolon_at_end_of_statement (parser);
31098 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31100 objc-synthesize-declaration:
31101 @synthesize objc-synthesize-identifier-list ;
31103 objc-synthesize-identifier-list:
31104 objc-synthesize-identifier
31105 objc-synthesize-identifier-list, objc-synthesize-identifier
31107 objc-synthesize-identifier
31108 identifier
31109 identifier = identifier
31111 For example:
31112 @synthesize MyProperty;
31113 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31115 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31116 for C. Keep them in sync.
31118 static void
31119 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31121 tree list = NULL_TREE;
31122 location_t loc;
31123 loc = cp_lexer_peek_token (parser->lexer)->location;
31125 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31126 while (true)
31128 tree property, ivar;
31129 property = cp_parser_identifier (parser);
31130 if (property == error_mark_node)
31132 cp_parser_consume_semicolon_at_end_of_statement (parser);
31133 return;
31135 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31137 cp_lexer_consume_token (parser->lexer);
31138 ivar = cp_parser_identifier (parser);
31139 if (ivar == error_mark_node)
31141 cp_parser_consume_semicolon_at_end_of_statement (parser);
31142 return;
31145 else
31146 ivar = NULL_TREE;
31147 list = chainon (list, build_tree_list (ivar, property));
31148 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31149 cp_lexer_consume_token (parser->lexer);
31150 else
31151 break;
31153 cp_parser_consume_semicolon_at_end_of_statement (parser);
31154 objc_add_synthesize_declaration (loc, list);
31157 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31159 objc-dynamic-declaration:
31160 @dynamic identifier-list ;
31162 For example:
31163 @dynamic MyProperty;
31164 @dynamic MyProperty, AnotherProperty;
31166 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31167 for C. Keep them in sync.
31169 static void
31170 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31172 tree list = NULL_TREE;
31173 location_t loc;
31174 loc = cp_lexer_peek_token (parser->lexer)->location;
31176 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31177 while (true)
31179 tree property;
31180 property = cp_parser_identifier (parser);
31181 if (property == error_mark_node)
31183 cp_parser_consume_semicolon_at_end_of_statement (parser);
31184 return;
31186 list = chainon (list, build_tree_list (NULL, property));
31187 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31188 cp_lexer_consume_token (parser->lexer);
31189 else
31190 break;
31192 cp_parser_consume_semicolon_at_end_of_statement (parser);
31193 objc_add_dynamic_declaration (loc, list);
31197 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31199 /* Returns name of the next clause.
31200 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31201 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31202 returned and the token is consumed. */
31204 static pragma_omp_clause
31205 cp_parser_omp_clause_name (cp_parser *parser)
31207 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31210 result = PRAGMA_OACC_CLAUSE_AUTO;
31211 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31212 result = PRAGMA_OMP_CLAUSE_IF;
31213 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31214 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31215 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31216 result = PRAGMA_OACC_CLAUSE_DELETE;
31217 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31218 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31219 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31220 result = PRAGMA_OMP_CLAUSE_FOR;
31221 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31223 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31224 const char *p = IDENTIFIER_POINTER (id);
31226 switch (p[0])
31228 case 'a':
31229 if (!strcmp ("aligned", p))
31230 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31231 else if (!strcmp ("async", p))
31232 result = PRAGMA_OACC_CLAUSE_ASYNC;
31233 break;
31234 case 'c':
31235 if (!strcmp ("collapse", p))
31236 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31237 else if (!strcmp ("copy", p))
31238 result = PRAGMA_OACC_CLAUSE_COPY;
31239 else if (!strcmp ("copyin", p))
31240 result = PRAGMA_OMP_CLAUSE_COPYIN;
31241 else if (!strcmp ("copyout", p))
31242 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31243 else if (!strcmp ("copyprivate", p))
31244 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31245 else if (!strcmp ("create", p))
31246 result = PRAGMA_OACC_CLAUSE_CREATE;
31247 break;
31248 case 'd':
31249 if (!strcmp ("defaultmap", p))
31250 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31251 else if (!strcmp ("depend", p))
31252 result = PRAGMA_OMP_CLAUSE_DEPEND;
31253 else if (!strcmp ("device", p))
31254 result = PRAGMA_OMP_CLAUSE_DEVICE;
31255 else if (!strcmp ("deviceptr", p))
31256 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31257 else if (!strcmp ("device_resident", p))
31258 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31259 else if (!strcmp ("dist_schedule", p))
31260 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31261 break;
31262 case 'f':
31263 if (!strcmp ("final", p))
31264 result = PRAGMA_OMP_CLAUSE_FINAL;
31265 else if (!strcmp ("firstprivate", p))
31266 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31267 else if (!strcmp ("from", p))
31268 result = PRAGMA_OMP_CLAUSE_FROM;
31269 break;
31270 case 'g':
31271 if (!strcmp ("gang", p))
31272 result = PRAGMA_OACC_CLAUSE_GANG;
31273 else if (!strcmp ("grainsize", p))
31274 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31275 break;
31276 case 'h':
31277 if (!strcmp ("hint", p))
31278 result = PRAGMA_OMP_CLAUSE_HINT;
31279 else if (!strcmp ("host", p))
31280 result = PRAGMA_OACC_CLAUSE_HOST;
31281 break;
31282 case 'i':
31283 if (!strcmp ("inbranch", p))
31284 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31285 else if (!strcmp ("independent", p))
31286 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31287 else if (!strcmp ("is_device_ptr", p))
31288 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31289 break;
31290 case 'l':
31291 if (!strcmp ("lastprivate", p))
31292 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31293 else if (!strcmp ("linear", p))
31294 result = PRAGMA_OMP_CLAUSE_LINEAR;
31295 else if (!strcmp ("link", p))
31296 result = PRAGMA_OMP_CLAUSE_LINK;
31297 break;
31298 case 'm':
31299 if (!strcmp ("map", p))
31300 result = PRAGMA_OMP_CLAUSE_MAP;
31301 else if (!strcmp ("mergeable", p))
31302 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31303 else if (flag_cilkplus && !strcmp ("mask", p))
31304 result = PRAGMA_CILK_CLAUSE_MASK;
31305 break;
31306 case 'n':
31307 if (!strcmp ("nogroup", p))
31308 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31309 else if (!strcmp ("notinbranch", p))
31310 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31311 else if (!strcmp ("nowait", p))
31312 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31313 else if (flag_cilkplus && !strcmp ("nomask", p))
31314 result = PRAGMA_CILK_CLAUSE_NOMASK;
31315 else if (!strcmp ("num_gangs", p))
31316 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31317 else if (!strcmp ("num_tasks", p))
31318 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31319 else if (!strcmp ("num_teams", p))
31320 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31321 else if (!strcmp ("num_threads", p))
31322 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31323 else if (!strcmp ("num_workers", p))
31324 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31325 break;
31326 case 'o':
31327 if (!strcmp ("ordered", p))
31328 result = PRAGMA_OMP_CLAUSE_ORDERED;
31329 break;
31330 case 'p':
31331 if (!strcmp ("parallel", p))
31332 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31333 else if (!strcmp ("present", p))
31334 result = PRAGMA_OACC_CLAUSE_PRESENT;
31335 else if (!strcmp ("present_or_copy", p)
31336 || !strcmp ("pcopy", p))
31337 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31338 else if (!strcmp ("present_or_copyin", p)
31339 || !strcmp ("pcopyin", p))
31340 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31341 else if (!strcmp ("present_or_copyout", p)
31342 || !strcmp ("pcopyout", p))
31343 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31344 else if (!strcmp ("present_or_create", p)
31345 || !strcmp ("pcreate", p))
31346 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31347 else if (!strcmp ("priority", p))
31348 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31349 else if (!strcmp ("proc_bind", p))
31350 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31351 break;
31352 case 'r':
31353 if (!strcmp ("reduction", p))
31354 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31355 break;
31356 case 's':
31357 if (!strcmp ("safelen", p))
31358 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31359 else if (!strcmp ("schedule", p))
31360 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31361 else if (!strcmp ("sections", p))
31362 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31363 else if (!strcmp ("self", p))
31364 result = PRAGMA_OACC_CLAUSE_SELF;
31365 else if (!strcmp ("seq", p))
31366 result = PRAGMA_OACC_CLAUSE_SEQ;
31367 else if (!strcmp ("shared", p))
31368 result = PRAGMA_OMP_CLAUSE_SHARED;
31369 else if (!strcmp ("simd", p))
31370 result = PRAGMA_OMP_CLAUSE_SIMD;
31371 else if (!strcmp ("simdlen", p))
31372 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31373 break;
31374 case 't':
31375 if (!strcmp ("taskgroup", p))
31376 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31377 else if (!strcmp ("thread_limit", p))
31378 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31379 else if (!strcmp ("threads", p))
31380 result = PRAGMA_OMP_CLAUSE_THREADS;
31381 else if (!strcmp ("tile", p))
31382 result = PRAGMA_OACC_CLAUSE_TILE;
31383 else if (!strcmp ("to", p))
31384 result = PRAGMA_OMP_CLAUSE_TO;
31385 break;
31386 case 'u':
31387 if (!strcmp ("uniform", p))
31388 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31389 else if (!strcmp ("untied", p))
31390 result = PRAGMA_OMP_CLAUSE_UNTIED;
31391 else if (!strcmp ("use_device", p))
31392 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31393 else if (!strcmp ("use_device_ptr", p))
31394 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31395 break;
31396 case 'v':
31397 if (!strcmp ("vector", p))
31398 result = PRAGMA_OACC_CLAUSE_VECTOR;
31399 else if (!strcmp ("vector_length", p))
31400 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31401 else if (flag_cilkplus && !strcmp ("vectorlength", p))
31402 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31403 break;
31404 case 'w':
31405 if (!strcmp ("wait", p))
31406 result = PRAGMA_OACC_CLAUSE_WAIT;
31407 else if (!strcmp ("worker", p))
31408 result = PRAGMA_OACC_CLAUSE_WORKER;
31409 break;
31413 if (result != PRAGMA_OMP_CLAUSE_NONE)
31414 cp_lexer_consume_token (parser->lexer);
31416 return result;
31419 /* Validate that a clause of the given type does not already exist. */
31421 static void
31422 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31423 const char *name, location_t location)
31425 tree c;
31427 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31428 if (OMP_CLAUSE_CODE (c) == code)
31430 error_at (location, "too many %qs clauses", name);
31431 break;
31435 /* OpenMP 2.5:
31436 variable-list:
31437 identifier
31438 variable-list , identifier
31440 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31441 colon). An opening parenthesis will have been consumed by the caller.
31443 If KIND is nonzero, create the appropriate node and install the decl
31444 in OMP_CLAUSE_DECL and add the node to the head of the list.
31446 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31447 return the list created.
31449 COLON can be NULL if only closing parenthesis should end the list,
31450 or pointer to bool which will receive false if the list is terminated
31451 by closing parenthesis or true if the list is terminated by colon. */
31453 static tree
31454 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31455 tree list, bool *colon)
31457 cp_token *token;
31458 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31459 if (colon)
31461 parser->colon_corrects_to_scope_p = false;
31462 *colon = false;
31464 while (1)
31466 tree name, decl;
31468 token = cp_lexer_peek_token (parser->lexer);
31469 if (kind != 0
31470 && current_class_ptr
31471 && cp_parser_is_keyword (token, RID_THIS))
31473 decl = finish_this_expr ();
31474 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31475 || CONVERT_EXPR_P (decl))
31476 decl = TREE_OPERAND (decl, 0);
31477 cp_lexer_consume_token (parser->lexer);
31479 else
31481 name = cp_parser_id_expression (parser, /*template_p=*/false,
31482 /*check_dependency_p=*/true,
31483 /*template_p=*/NULL,
31484 /*declarator_p=*/false,
31485 /*optional_p=*/false);
31486 if (name == error_mark_node)
31487 goto skip_comma;
31489 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31490 if (decl == error_mark_node)
31491 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31492 token->location);
31494 if (decl == error_mark_node)
31496 else if (kind != 0)
31498 switch (kind)
31500 case OMP_CLAUSE__CACHE_:
31501 /* The OpenACC cache directive explicitly only allows "array
31502 elements or subarrays". */
31503 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31505 error_at (token->location, "expected %<[%>");
31506 decl = error_mark_node;
31507 break;
31509 /* FALLTHROUGH. */
31510 case OMP_CLAUSE_MAP:
31511 case OMP_CLAUSE_FROM:
31512 case OMP_CLAUSE_TO:
31513 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31515 location_t loc
31516 = cp_lexer_peek_token (parser->lexer)->location;
31517 cp_id_kind idk = CP_ID_KIND_NONE;
31518 cp_lexer_consume_token (parser->lexer);
31519 decl = convert_from_reference (decl);
31520 decl
31521 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31522 decl, false,
31523 &idk, loc);
31525 /* FALLTHROUGH. */
31526 case OMP_CLAUSE_DEPEND:
31527 case OMP_CLAUSE_REDUCTION:
31528 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31530 tree low_bound = NULL_TREE, length = NULL_TREE;
31532 parser->colon_corrects_to_scope_p = false;
31533 cp_lexer_consume_token (parser->lexer);
31534 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31535 low_bound = cp_parser_expression (parser);
31536 if (!colon)
31537 parser->colon_corrects_to_scope_p
31538 = saved_colon_corrects_to_scope_p;
31539 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31540 length = integer_one_node;
31541 else
31543 /* Look for `:'. */
31544 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31545 goto skip_comma;
31546 if (!cp_lexer_next_token_is (parser->lexer,
31547 CPP_CLOSE_SQUARE))
31548 length = cp_parser_expression (parser);
31550 /* Look for the closing `]'. */
31551 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31552 RT_CLOSE_SQUARE))
31553 goto skip_comma;
31555 decl = tree_cons (low_bound, length, decl);
31557 break;
31558 default:
31559 break;
31562 tree u = build_omp_clause (token->location, kind);
31563 OMP_CLAUSE_DECL (u) = decl;
31564 OMP_CLAUSE_CHAIN (u) = list;
31565 list = u;
31567 else
31568 list = tree_cons (decl, NULL_TREE, list);
31570 get_comma:
31571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31572 break;
31573 cp_lexer_consume_token (parser->lexer);
31576 if (colon)
31577 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31579 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31581 *colon = true;
31582 cp_parser_require (parser, CPP_COLON, RT_COLON);
31583 return list;
31586 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31588 int ending;
31590 /* Try to resync to an unnested comma. Copied from
31591 cp_parser_parenthesized_expression_list. */
31592 skip_comma:
31593 if (colon)
31594 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31595 ending = cp_parser_skip_to_closing_parenthesis (parser,
31596 /*recovering=*/true,
31597 /*or_comma=*/true,
31598 /*consume_paren=*/true);
31599 if (ending < 0)
31600 goto get_comma;
31603 return list;
31606 /* Similarly, but expect leading and trailing parenthesis. This is a very
31607 common case for omp clauses. */
31609 static tree
31610 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31612 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31613 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31614 return list;
31617 /* OpenACC 2.0:
31618 copy ( variable-list )
31619 copyin ( variable-list )
31620 copyout ( variable-list )
31621 create ( variable-list )
31622 delete ( variable-list )
31623 present ( variable-list )
31624 present_or_copy ( variable-list )
31625 pcopy ( variable-list )
31626 present_or_copyin ( variable-list )
31627 pcopyin ( variable-list )
31628 present_or_copyout ( variable-list )
31629 pcopyout ( variable-list )
31630 present_or_create ( variable-list )
31631 pcreate ( variable-list ) */
31633 static tree
31634 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31635 tree list)
31637 enum gomp_map_kind kind;
31638 switch (c_kind)
31640 case PRAGMA_OACC_CLAUSE_COPY:
31641 kind = GOMP_MAP_FORCE_TOFROM;
31642 break;
31643 case PRAGMA_OACC_CLAUSE_COPYIN:
31644 kind = GOMP_MAP_FORCE_TO;
31645 break;
31646 case PRAGMA_OACC_CLAUSE_COPYOUT:
31647 kind = GOMP_MAP_FORCE_FROM;
31648 break;
31649 case PRAGMA_OACC_CLAUSE_CREATE:
31650 kind = GOMP_MAP_FORCE_ALLOC;
31651 break;
31652 case PRAGMA_OACC_CLAUSE_DELETE:
31653 kind = GOMP_MAP_DELETE;
31654 break;
31655 case PRAGMA_OACC_CLAUSE_DEVICE:
31656 kind = GOMP_MAP_FORCE_TO;
31657 break;
31658 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31659 kind = GOMP_MAP_DEVICE_RESIDENT;
31660 break;
31661 case PRAGMA_OACC_CLAUSE_HOST:
31662 case PRAGMA_OACC_CLAUSE_SELF:
31663 kind = GOMP_MAP_FORCE_FROM;
31664 break;
31665 case PRAGMA_OACC_CLAUSE_LINK:
31666 kind = GOMP_MAP_LINK;
31667 break;
31668 case PRAGMA_OACC_CLAUSE_PRESENT:
31669 kind = GOMP_MAP_FORCE_PRESENT;
31670 break;
31671 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31672 kind = GOMP_MAP_TOFROM;
31673 break;
31674 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31675 kind = GOMP_MAP_TO;
31676 break;
31677 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31678 kind = GOMP_MAP_FROM;
31679 break;
31680 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31681 kind = GOMP_MAP_ALLOC;
31682 break;
31683 default:
31684 gcc_unreachable ();
31686 tree nl, c;
31687 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31689 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31690 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31692 return nl;
31695 /* OpenACC 2.0:
31696 deviceptr ( variable-list ) */
31698 static tree
31699 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31701 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31702 tree vars, t;
31704 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31705 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31706 variable-list must only allow for pointer variables. */
31707 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31708 for (t = vars; t; t = TREE_CHAIN (t))
31710 tree v = TREE_PURPOSE (t);
31711 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31712 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31713 OMP_CLAUSE_DECL (u) = v;
31714 OMP_CLAUSE_CHAIN (u) = list;
31715 list = u;
31718 return list;
31721 /* OpenACC 2.0:
31722 auto
31723 independent
31724 nohost
31725 seq */
31727 static tree
31728 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31729 enum omp_clause_code code,
31730 tree list, location_t location)
31732 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31733 tree c = build_omp_clause (location, code);
31734 OMP_CLAUSE_CHAIN (c) = list;
31735 return c;
31738 /* OpenACC:
31739 num_gangs ( expression )
31740 num_workers ( expression )
31741 vector_length ( expression ) */
31743 static tree
31744 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31745 const char *str, tree list)
31747 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31749 matching_parens parens;
31750 if (!parens.require_open (parser))
31751 return list;
31753 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31755 if (t == error_mark_node
31756 || !parens.require_close (parser))
31758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31759 /*or_comma=*/false,
31760 /*consume_paren=*/true);
31761 return list;
31764 check_no_duplicate_clause (list, code, str, loc);
31766 tree c = build_omp_clause (loc, code);
31767 OMP_CLAUSE_OPERAND (c, 0) = t;
31768 OMP_CLAUSE_CHAIN (c) = list;
31769 return c;
31772 /* OpenACC:
31774 gang [( gang-arg-list )]
31775 worker [( [num:] int-expr )]
31776 vector [( [length:] int-expr )]
31778 where gang-arg is one of:
31780 [num:] int-expr
31781 static: size-expr
31783 and size-expr may be:
31786 int-expr
31789 static tree
31790 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31791 const char *str, tree list)
31793 const char *id = "num";
31794 cp_lexer *lexer = parser->lexer;
31795 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31796 location_t loc = cp_lexer_peek_token (lexer)->location;
31798 if (kind == OMP_CLAUSE_VECTOR)
31799 id = "length";
31801 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31803 matching_parens parens;
31804 parens.consume_open (parser);
31808 cp_token *next = cp_lexer_peek_token (lexer);
31809 int idx = 0;
31811 /* Gang static argument. */
31812 if (kind == OMP_CLAUSE_GANG
31813 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31815 cp_lexer_consume_token (lexer);
31817 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31818 goto cleanup_error;
31820 idx = 1;
31821 if (ops[idx] != NULL)
31823 cp_parser_error (parser, "too many %<static%> arguments");
31824 goto cleanup_error;
31827 /* Check for the '*' argument. */
31828 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31829 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31830 || cp_lexer_nth_token_is (parser->lexer, 2,
31831 CPP_CLOSE_PAREN)))
31833 cp_lexer_consume_token (lexer);
31834 ops[idx] = integer_minus_one_node;
31836 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31838 cp_lexer_consume_token (lexer);
31839 continue;
31841 else break;
31844 /* Worker num: argument and vector length: arguments. */
31845 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31846 && id_equal (next->u.value, id)
31847 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31849 cp_lexer_consume_token (lexer); /* id */
31850 cp_lexer_consume_token (lexer); /* ':' */
31853 /* Now collect the actual argument. */
31854 if (ops[idx] != NULL_TREE)
31856 cp_parser_error (parser, "unexpected argument");
31857 goto cleanup_error;
31860 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31861 false);
31862 if (expr == error_mark_node)
31863 goto cleanup_error;
31865 mark_exp_read (expr);
31866 ops[idx] = expr;
31868 if (kind == OMP_CLAUSE_GANG
31869 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31871 cp_lexer_consume_token (lexer);
31872 continue;
31874 break;
31876 while (1);
31878 if (!parens.require_close (parser))
31879 goto cleanup_error;
31882 check_no_duplicate_clause (list, kind, str, loc);
31884 c = build_omp_clause (loc, kind);
31886 if (ops[1])
31887 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31889 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31890 OMP_CLAUSE_CHAIN (c) = list;
31892 return c;
31894 cleanup_error:
31895 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31896 return list;
31899 /* OpenACC 2.0:
31900 tile ( size-expr-list ) */
31902 static tree
31903 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31905 tree c, expr = error_mark_node;
31906 tree tile = NULL_TREE;
31908 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31909 so, but the spec authors never considered such a case and have
31910 differing opinions on what it might mean, including 'not
31911 allowed'.) */
31912 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31913 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31914 clause_loc);
31916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31917 return list;
31921 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31922 return list;
31924 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31925 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31926 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31928 cp_lexer_consume_token (parser->lexer);
31929 expr = integer_zero_node;
31931 else
31932 expr = cp_parser_constant_expression (parser);
31934 tile = tree_cons (NULL_TREE, expr, tile);
31936 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31938 /* Consume the trailing ')'. */
31939 cp_lexer_consume_token (parser->lexer);
31941 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31942 tile = nreverse (tile);
31943 OMP_CLAUSE_TILE_LIST (c) = tile;
31944 OMP_CLAUSE_CHAIN (c) = list;
31945 return c;
31948 /* OpenACC 2.0
31949 Parse wait clause or directive parameters. */
31951 static tree
31952 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31954 vec<tree, va_gc> *args;
31955 tree t, args_tree;
31957 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31958 /*cast_p=*/false,
31959 /*allow_expansion_p=*/true,
31960 /*non_constant_p=*/NULL);
31962 if (args == NULL || args->length () == 0)
31964 cp_parser_error (parser, "expected integer expression before ')'");
31965 if (args != NULL)
31966 release_tree_vector (args);
31967 return list;
31970 args_tree = build_tree_list_vec (args);
31972 release_tree_vector (args);
31974 for (t = args_tree; t; t = TREE_CHAIN (t))
31976 tree targ = TREE_VALUE (t);
31978 if (targ != error_mark_node)
31980 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31981 error ("%<wait%> expression must be integral");
31982 else
31984 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31986 targ = mark_rvalue_use (targ);
31987 OMP_CLAUSE_DECL (c) = targ;
31988 OMP_CLAUSE_CHAIN (c) = list;
31989 list = c;
31994 return list;
31997 /* OpenACC:
31998 wait ( int-expr-list ) */
32000 static tree
32001 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32003 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32005 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32006 return list;
32008 list = cp_parser_oacc_wait_list (parser, location, list);
32010 return list;
32013 /* OpenMP 3.0:
32014 collapse ( constant-expression ) */
32016 static tree
32017 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32019 tree c, num;
32020 location_t loc;
32021 HOST_WIDE_INT n;
32023 loc = cp_lexer_peek_token (parser->lexer)->location;
32024 matching_parens parens;
32025 if (!parens.require_open (parser))
32026 return list;
32028 num = cp_parser_constant_expression (parser);
32030 if (!parens.require_close (parser))
32031 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32032 /*or_comma=*/false,
32033 /*consume_paren=*/true);
32035 if (num == error_mark_node)
32036 return list;
32037 num = fold_non_dependent_expr (num);
32038 if (!tree_fits_shwi_p (num)
32039 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32040 || (n = tree_to_shwi (num)) <= 0
32041 || (int) n != n)
32043 error_at (loc, "collapse argument needs positive constant integer expression");
32044 return list;
32047 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32048 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32049 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32050 OMP_CLAUSE_CHAIN (c) = list;
32051 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32053 return c;
32056 /* OpenMP 2.5:
32057 default ( none | shared )
32059 OpenACC:
32060 default ( none | present ) */
32062 static tree
32063 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32064 location_t location, bool is_oacc)
32066 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32067 tree c;
32069 matching_parens parens;
32070 if (!parens.require_open (parser))
32071 return list;
32072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32074 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32075 const char *p = IDENTIFIER_POINTER (id);
32077 switch (p[0])
32079 case 'n':
32080 if (strcmp ("none", p) != 0)
32081 goto invalid_kind;
32082 kind = OMP_CLAUSE_DEFAULT_NONE;
32083 break;
32085 case 'p':
32086 if (strcmp ("present", p) != 0 || !is_oacc)
32087 goto invalid_kind;
32088 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32089 break;
32091 case 's':
32092 if (strcmp ("shared", p) != 0 || is_oacc)
32093 goto invalid_kind;
32094 kind = OMP_CLAUSE_DEFAULT_SHARED;
32095 break;
32097 default:
32098 goto invalid_kind;
32101 cp_lexer_consume_token (parser->lexer);
32103 else
32105 invalid_kind:
32106 if (is_oacc)
32107 cp_parser_error (parser, "expected %<none%> or %<present%>");
32108 else
32109 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32112 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32113 || !parens.require_close (parser))
32114 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32115 /*or_comma=*/false,
32116 /*consume_paren=*/true);
32118 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32119 return list;
32121 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32122 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32123 OMP_CLAUSE_CHAIN (c) = list;
32124 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32126 return c;
32129 /* OpenMP 3.1:
32130 final ( expression ) */
32132 static tree
32133 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32135 tree t, c;
32137 matching_parens parens;
32138 if (!parens.require_open (parser))
32139 return list;
32141 t = cp_parser_condition (parser);
32143 if (t == error_mark_node
32144 || !parens.require_close (parser))
32145 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32146 /*or_comma=*/false,
32147 /*consume_paren=*/true);
32149 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32151 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32152 OMP_CLAUSE_FINAL_EXPR (c) = t;
32153 OMP_CLAUSE_CHAIN (c) = list;
32155 return c;
32158 /* OpenMP 2.5:
32159 if ( expression )
32161 OpenMP 4.5:
32162 if ( directive-name-modifier : expression )
32164 directive-name-modifier:
32165 parallel | task | taskloop | target data | target | target update
32166 | target enter data | target exit data */
32168 static tree
32169 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32170 bool is_omp)
32172 tree t, c;
32173 enum tree_code if_modifier = ERROR_MARK;
32175 matching_parens parens;
32176 if (!parens.require_open (parser))
32177 return list;
32179 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32181 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32182 const char *p = IDENTIFIER_POINTER (id);
32183 int n = 2;
32185 if (strcmp ("parallel", p) == 0)
32186 if_modifier = OMP_PARALLEL;
32187 else if (strcmp ("task", p) == 0)
32188 if_modifier = OMP_TASK;
32189 else if (strcmp ("taskloop", p) == 0)
32190 if_modifier = OMP_TASKLOOP;
32191 else if (strcmp ("target", p) == 0)
32193 if_modifier = OMP_TARGET;
32194 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32196 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32197 p = IDENTIFIER_POINTER (id);
32198 if (strcmp ("data", p) == 0)
32199 if_modifier = OMP_TARGET_DATA;
32200 else if (strcmp ("update", p) == 0)
32201 if_modifier = OMP_TARGET_UPDATE;
32202 else if (strcmp ("enter", p) == 0)
32203 if_modifier = OMP_TARGET_ENTER_DATA;
32204 else if (strcmp ("exit", p) == 0)
32205 if_modifier = OMP_TARGET_EXIT_DATA;
32206 if (if_modifier != OMP_TARGET)
32207 n = 3;
32208 else
32210 location_t loc
32211 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32212 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32213 "or %<exit%>");
32214 if_modifier = ERROR_MARK;
32216 if (if_modifier == OMP_TARGET_ENTER_DATA
32217 || if_modifier == OMP_TARGET_EXIT_DATA)
32219 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32221 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32222 p = IDENTIFIER_POINTER (id);
32223 if (strcmp ("data", p) == 0)
32224 n = 4;
32226 if (n != 4)
32228 location_t loc
32229 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32230 error_at (loc, "expected %<data%>");
32231 if_modifier = ERROR_MARK;
32236 if (if_modifier != ERROR_MARK)
32238 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32240 while (n-- > 0)
32241 cp_lexer_consume_token (parser->lexer);
32243 else
32245 if (n > 2)
32247 location_t loc
32248 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32249 error_at (loc, "expected %<:%>");
32251 if_modifier = ERROR_MARK;
32256 t = cp_parser_condition (parser);
32258 if (t == error_mark_node
32259 || !parens.require_close (parser))
32260 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32261 /*or_comma=*/false,
32262 /*consume_paren=*/true);
32264 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32265 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32267 if (if_modifier != ERROR_MARK
32268 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32270 const char *p = NULL;
32271 switch (if_modifier)
32273 case OMP_PARALLEL: p = "parallel"; break;
32274 case OMP_TASK: p = "task"; break;
32275 case OMP_TASKLOOP: p = "taskloop"; break;
32276 case OMP_TARGET_DATA: p = "target data"; break;
32277 case OMP_TARGET: p = "target"; break;
32278 case OMP_TARGET_UPDATE: p = "target update"; break;
32279 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32280 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32281 default: gcc_unreachable ();
32283 error_at (location, "too many %<if%> clauses with %qs modifier",
32285 return list;
32287 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32289 if (!is_omp)
32290 error_at (location, "too many %<if%> clauses");
32291 else
32292 error_at (location, "too many %<if%> clauses without modifier");
32293 return list;
32295 else if (if_modifier == ERROR_MARK
32296 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32298 error_at (location, "if any %<if%> clause has modifier, then all "
32299 "%<if%> clauses have to use modifier");
32300 return list;
32304 c = build_omp_clause (location, OMP_CLAUSE_IF);
32305 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32306 OMP_CLAUSE_IF_EXPR (c) = t;
32307 OMP_CLAUSE_CHAIN (c) = list;
32309 return c;
32312 /* OpenMP 3.1:
32313 mergeable */
32315 static tree
32316 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32317 tree list, location_t location)
32319 tree c;
32321 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32322 location);
32324 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32325 OMP_CLAUSE_CHAIN (c) = list;
32326 return c;
32329 /* OpenMP 2.5:
32330 nowait */
32332 static tree
32333 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32334 tree list, location_t location)
32336 tree c;
32338 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32340 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32341 OMP_CLAUSE_CHAIN (c) = list;
32342 return c;
32345 /* OpenMP 2.5:
32346 num_threads ( expression ) */
32348 static tree
32349 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32350 location_t location)
32352 tree t, c;
32354 matching_parens parens;
32355 if (!parens.require_open (parser))
32356 return list;
32358 t = cp_parser_expression (parser);
32360 if (t == error_mark_node
32361 || !parens.require_close (parser))
32362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32363 /*or_comma=*/false,
32364 /*consume_paren=*/true);
32366 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32367 "num_threads", location);
32369 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32370 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32371 OMP_CLAUSE_CHAIN (c) = list;
32373 return c;
32376 /* OpenMP 4.5:
32377 num_tasks ( expression ) */
32379 static tree
32380 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32381 location_t location)
32383 tree t, c;
32385 matching_parens parens;
32386 if (!parens.require_open (parser))
32387 return list;
32389 t = cp_parser_expression (parser);
32391 if (t == error_mark_node
32392 || !parens.require_close (parser))
32393 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32394 /*or_comma=*/false,
32395 /*consume_paren=*/true);
32397 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32398 "num_tasks", location);
32400 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32401 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32402 OMP_CLAUSE_CHAIN (c) = list;
32404 return c;
32407 /* OpenMP 4.5:
32408 grainsize ( expression ) */
32410 static tree
32411 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32412 location_t location)
32414 tree t, c;
32416 matching_parens parens;
32417 if (!parens.require_open (parser))
32418 return list;
32420 t = cp_parser_expression (parser);
32422 if (t == error_mark_node
32423 || !parens.require_close (parser))
32424 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32425 /*or_comma=*/false,
32426 /*consume_paren=*/true);
32428 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32429 "grainsize", location);
32431 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32432 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32433 OMP_CLAUSE_CHAIN (c) = list;
32435 return c;
32438 /* OpenMP 4.5:
32439 priority ( expression ) */
32441 static tree
32442 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32443 location_t location)
32445 tree t, c;
32447 matching_parens parens;
32448 if (!parens.require_open (parser))
32449 return list;
32451 t = cp_parser_expression (parser);
32453 if (t == error_mark_node
32454 || !parens.require_close (parser))
32455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32456 /*or_comma=*/false,
32457 /*consume_paren=*/true);
32459 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32460 "priority", location);
32462 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32463 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32464 OMP_CLAUSE_CHAIN (c) = list;
32466 return c;
32469 /* OpenMP 4.5:
32470 hint ( expression ) */
32472 static tree
32473 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32474 location_t location)
32476 tree t, c;
32478 matching_parens parens;
32479 if (!parens.require_open (parser))
32480 return list;
32482 t = cp_parser_expression (parser);
32484 if (t == error_mark_node
32485 || !parens.require_close (parser))
32486 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32487 /*or_comma=*/false,
32488 /*consume_paren=*/true);
32490 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32492 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32493 OMP_CLAUSE_HINT_EXPR (c) = t;
32494 OMP_CLAUSE_CHAIN (c) = list;
32496 return c;
32499 /* OpenMP 4.5:
32500 defaultmap ( tofrom : scalar ) */
32502 static tree
32503 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32504 location_t location)
32506 tree c, id;
32507 const char *p;
32509 matching_parens parens;
32510 if (!parens.require_open (parser))
32511 return list;
32513 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32515 cp_parser_error (parser, "expected %<tofrom%>");
32516 goto out_err;
32518 id = cp_lexer_peek_token (parser->lexer)->u.value;
32519 p = IDENTIFIER_POINTER (id);
32520 if (strcmp (p, "tofrom") != 0)
32522 cp_parser_error (parser, "expected %<tofrom%>");
32523 goto out_err;
32525 cp_lexer_consume_token (parser->lexer);
32526 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32527 goto out_err;
32529 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32531 cp_parser_error (parser, "expected %<scalar%>");
32532 goto out_err;
32534 id = cp_lexer_peek_token (parser->lexer)->u.value;
32535 p = IDENTIFIER_POINTER (id);
32536 if (strcmp (p, "scalar") != 0)
32538 cp_parser_error (parser, "expected %<scalar%>");
32539 goto out_err;
32541 cp_lexer_consume_token (parser->lexer);
32542 if (!parens.require_close (parser))
32543 goto out_err;
32545 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32546 location);
32548 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32549 OMP_CLAUSE_CHAIN (c) = list;
32550 return c;
32552 out_err:
32553 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32554 /*or_comma=*/false,
32555 /*consume_paren=*/true);
32556 return list;
32559 /* OpenMP 2.5:
32560 ordered
32562 OpenMP 4.5:
32563 ordered ( constant-expression ) */
32565 static tree
32566 cp_parser_omp_clause_ordered (cp_parser *parser,
32567 tree list, location_t location)
32569 tree c, num = NULL_TREE;
32570 HOST_WIDE_INT n;
32572 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32573 "ordered", location);
32575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32577 matching_parens parens;
32578 parens.consume_open (parser);
32580 num = cp_parser_constant_expression (parser);
32582 if (!parens.require_close (parser))
32583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32584 /*or_comma=*/false,
32585 /*consume_paren=*/true);
32587 if (num == error_mark_node)
32588 return list;
32589 num = fold_non_dependent_expr (num);
32590 if (!tree_fits_shwi_p (num)
32591 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32592 || (n = tree_to_shwi (num)) <= 0
32593 || (int) n != n)
32595 error_at (location,
32596 "ordered argument needs positive constant integer "
32597 "expression");
32598 return list;
32602 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32603 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32604 OMP_CLAUSE_CHAIN (c) = list;
32605 return c;
32608 /* OpenMP 2.5:
32609 reduction ( reduction-operator : variable-list )
32611 reduction-operator:
32612 One of: + * - & ^ | && ||
32614 OpenMP 3.1:
32616 reduction-operator:
32617 One of: + * - & ^ | && || min max
32619 OpenMP 4.0:
32621 reduction-operator:
32622 One of: + * - & ^ | && ||
32623 id-expression */
32625 static tree
32626 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32628 enum tree_code code = ERROR_MARK;
32629 tree nlist, c, id = NULL_TREE;
32631 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32632 return list;
32634 switch (cp_lexer_peek_token (parser->lexer)->type)
32636 case CPP_PLUS: code = PLUS_EXPR; break;
32637 case CPP_MULT: code = MULT_EXPR; break;
32638 case CPP_MINUS: code = MINUS_EXPR; break;
32639 case CPP_AND: code = BIT_AND_EXPR; break;
32640 case CPP_XOR: code = BIT_XOR_EXPR; break;
32641 case CPP_OR: code = BIT_IOR_EXPR; break;
32642 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32643 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32644 default: break;
32647 if (code != ERROR_MARK)
32648 cp_lexer_consume_token (parser->lexer);
32649 else
32651 bool saved_colon_corrects_to_scope_p;
32652 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32653 parser->colon_corrects_to_scope_p = false;
32654 id = cp_parser_id_expression (parser, /*template_p=*/false,
32655 /*check_dependency_p=*/true,
32656 /*template_p=*/NULL,
32657 /*declarator_p=*/false,
32658 /*optional_p=*/false);
32659 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32660 if (identifier_p (id))
32662 const char *p = IDENTIFIER_POINTER (id);
32664 if (strcmp (p, "min") == 0)
32665 code = MIN_EXPR;
32666 else if (strcmp (p, "max") == 0)
32667 code = MAX_EXPR;
32668 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32669 code = PLUS_EXPR;
32670 else if (id == ovl_op_identifier (false, MULT_EXPR))
32671 code = MULT_EXPR;
32672 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32673 code = MINUS_EXPR;
32674 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32675 code = BIT_AND_EXPR;
32676 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32677 code = BIT_IOR_EXPR;
32678 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32679 code = BIT_XOR_EXPR;
32680 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32681 code = TRUTH_ANDIF_EXPR;
32682 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32683 code = TRUTH_ORIF_EXPR;
32684 id = omp_reduction_id (code, id, NULL_TREE);
32685 tree scope = parser->scope;
32686 if (scope)
32687 id = build_qualified_name (NULL_TREE, scope, id, false);
32688 parser->scope = NULL_TREE;
32689 parser->qualifying_scope = NULL_TREE;
32690 parser->object_scope = NULL_TREE;
32692 else
32694 error ("invalid reduction-identifier");
32695 resync_fail:
32696 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32697 /*or_comma=*/false,
32698 /*consume_paren=*/true);
32699 return list;
32703 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32704 goto resync_fail;
32706 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32707 NULL);
32708 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32710 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32711 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32714 return nlist;
32717 /* OpenMP 2.5:
32718 schedule ( schedule-kind )
32719 schedule ( schedule-kind , expression )
32721 schedule-kind:
32722 static | dynamic | guided | runtime | auto
32724 OpenMP 4.5:
32725 schedule ( schedule-modifier : schedule-kind )
32726 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32728 schedule-modifier:
32729 simd
32730 monotonic
32731 nonmonotonic */
32733 static tree
32734 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32736 tree c, t;
32737 int modifiers = 0, nmodifiers = 0;
32739 matching_parens parens;
32740 if (!parens.require_open (parser))
32741 return list;
32743 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32745 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32747 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32748 const char *p = IDENTIFIER_POINTER (id);
32749 if (strcmp ("simd", p) == 0)
32750 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32751 else if (strcmp ("monotonic", p) == 0)
32752 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32753 else if (strcmp ("nonmonotonic", p) == 0)
32754 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32755 else
32756 break;
32757 cp_lexer_consume_token (parser->lexer);
32758 if (nmodifiers++ == 0
32759 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32760 cp_lexer_consume_token (parser->lexer);
32761 else
32763 cp_parser_require (parser, CPP_COLON, RT_COLON);
32764 break;
32768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32771 const char *p = IDENTIFIER_POINTER (id);
32773 switch (p[0])
32775 case 'd':
32776 if (strcmp ("dynamic", p) != 0)
32777 goto invalid_kind;
32778 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32779 break;
32781 case 'g':
32782 if (strcmp ("guided", p) != 0)
32783 goto invalid_kind;
32784 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32785 break;
32787 case 'r':
32788 if (strcmp ("runtime", p) != 0)
32789 goto invalid_kind;
32790 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32791 break;
32793 default:
32794 goto invalid_kind;
32797 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32798 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32799 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32800 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32801 else
32802 goto invalid_kind;
32803 cp_lexer_consume_token (parser->lexer);
32805 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32806 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32807 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32808 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32810 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32811 "specified");
32812 modifiers = 0;
32815 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32817 cp_token *token;
32818 cp_lexer_consume_token (parser->lexer);
32820 token = cp_lexer_peek_token (parser->lexer);
32821 t = cp_parser_assignment_expression (parser);
32823 if (t == error_mark_node)
32824 goto resync_fail;
32825 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32826 error_at (token->location, "schedule %<runtime%> does not take "
32827 "a %<chunk_size%> parameter");
32828 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32829 error_at (token->location, "schedule %<auto%> does not take "
32830 "a %<chunk_size%> parameter");
32831 else
32832 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32834 if (!parens.require_close (parser))
32835 goto resync_fail;
32837 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32838 goto resync_fail;
32840 OMP_CLAUSE_SCHEDULE_KIND (c)
32841 = (enum omp_clause_schedule_kind)
32842 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32844 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32845 OMP_CLAUSE_CHAIN (c) = list;
32846 return c;
32848 invalid_kind:
32849 cp_parser_error (parser, "invalid schedule kind");
32850 resync_fail:
32851 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32852 /*or_comma=*/false,
32853 /*consume_paren=*/true);
32854 return list;
32857 /* OpenMP 3.0:
32858 untied */
32860 static tree
32861 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32862 tree list, location_t location)
32864 tree c;
32866 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32868 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32869 OMP_CLAUSE_CHAIN (c) = list;
32870 return c;
32873 /* OpenMP 4.0:
32874 inbranch
32875 notinbranch */
32877 static tree
32878 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32879 tree list, location_t location)
32881 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32882 tree c = build_omp_clause (location, code);
32883 OMP_CLAUSE_CHAIN (c) = list;
32884 return c;
32887 /* OpenMP 4.0:
32888 parallel
32890 sections
32891 taskgroup */
32893 static tree
32894 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32895 enum omp_clause_code code,
32896 tree list, location_t location)
32898 tree c = build_omp_clause (location, code);
32899 OMP_CLAUSE_CHAIN (c) = list;
32900 return c;
32903 /* OpenMP 4.5:
32904 nogroup */
32906 static tree
32907 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32908 tree list, location_t location)
32910 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32911 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32912 OMP_CLAUSE_CHAIN (c) = list;
32913 return c;
32916 /* OpenMP 4.5:
32917 simd
32918 threads */
32920 static tree
32921 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32922 enum omp_clause_code code,
32923 tree list, location_t location)
32925 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32926 tree c = build_omp_clause (location, code);
32927 OMP_CLAUSE_CHAIN (c) = list;
32928 return c;
32931 /* OpenMP 4.0:
32932 num_teams ( expression ) */
32934 static tree
32935 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32936 location_t location)
32938 tree t, c;
32940 matching_parens parens;
32941 if (!parens.require_open (parser))
32942 return list;
32944 t = cp_parser_expression (parser);
32946 if (t == error_mark_node
32947 || !parens.require_close (parser))
32948 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32949 /*or_comma=*/false,
32950 /*consume_paren=*/true);
32952 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32953 "num_teams", location);
32955 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32956 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32957 OMP_CLAUSE_CHAIN (c) = list;
32959 return c;
32962 /* OpenMP 4.0:
32963 thread_limit ( expression ) */
32965 static tree
32966 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32967 location_t location)
32969 tree t, c;
32971 matching_parens parens;
32972 if (!parens.require_open (parser))
32973 return list;
32975 t = cp_parser_expression (parser);
32977 if (t == error_mark_node
32978 || !parens.require_close (parser))
32979 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32980 /*or_comma=*/false,
32981 /*consume_paren=*/true);
32983 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32984 "thread_limit", location);
32986 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32987 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32988 OMP_CLAUSE_CHAIN (c) = list;
32990 return c;
32993 /* OpenMP 4.0:
32994 aligned ( variable-list )
32995 aligned ( variable-list : constant-expression ) */
32997 static tree
32998 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33000 tree nlist, c, alignment = NULL_TREE;
33001 bool colon;
33003 matching_parens parens;
33004 if (!parens.require_open (parser))
33005 return list;
33007 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33008 &colon);
33010 if (colon)
33012 alignment = cp_parser_constant_expression (parser);
33014 if (!parens.require_close (parser))
33015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33016 /*or_comma=*/false,
33017 /*consume_paren=*/true);
33019 if (alignment == error_mark_node)
33020 alignment = NULL_TREE;
33023 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33024 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33026 return nlist;
33029 /* OpenMP 4.0:
33030 linear ( variable-list )
33031 linear ( variable-list : expression )
33033 OpenMP 4.5:
33034 linear ( modifier ( variable-list ) )
33035 linear ( modifier ( variable-list ) : expression ) */
33037 static tree
33038 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33039 bool is_cilk_simd_fn, bool declare_simd)
33041 tree nlist, c, step = integer_one_node;
33042 bool colon;
33043 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33045 matching_parens parens;
33046 if (!parens.require_open (parser))
33047 return list;
33049 if (!is_cilk_simd_fn
33050 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33052 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33053 const char *p = IDENTIFIER_POINTER (id);
33055 if (strcmp ("ref", p) == 0)
33056 kind = OMP_CLAUSE_LINEAR_REF;
33057 else if (strcmp ("val", p) == 0)
33058 kind = OMP_CLAUSE_LINEAR_VAL;
33059 else if (strcmp ("uval", p) == 0)
33060 kind = OMP_CLAUSE_LINEAR_UVAL;
33061 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33062 cp_lexer_consume_token (parser->lexer);
33063 else
33064 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33067 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33068 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33069 &colon);
33070 else
33072 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33073 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33074 if (colon)
33075 cp_parser_require (parser, CPP_COLON, RT_COLON);
33076 else if (!parens.require_close (parser))
33077 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33078 /*or_comma=*/false,
33079 /*consume_paren=*/true);
33082 if (colon)
33084 step = NULL_TREE;
33085 if (declare_simd
33086 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33087 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33089 cp_token *token = cp_lexer_peek_token (parser->lexer);
33090 cp_parser_parse_tentatively (parser);
33091 step = cp_parser_id_expression (parser, /*template_p=*/false,
33092 /*check_dependency_p=*/true,
33093 /*template_p=*/NULL,
33094 /*declarator_p=*/false,
33095 /*optional_p=*/false);
33096 if (step != error_mark_node)
33097 step = cp_parser_lookup_name_simple (parser, step, token->location);
33098 if (step == error_mark_node)
33100 step = NULL_TREE;
33101 cp_parser_abort_tentative_parse (parser);
33103 else if (!cp_parser_parse_definitely (parser))
33104 step = NULL_TREE;
33106 if (!step)
33107 step = cp_parser_expression (parser);
33109 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
33111 sorry ("using parameters for %<linear%> step is not supported yet");
33112 step = integer_one_node;
33114 if (!parens.require_close (parser))
33115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33116 /*or_comma=*/false,
33117 /*consume_paren=*/true);
33119 if (step == error_mark_node)
33120 return list;
33123 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33125 OMP_CLAUSE_LINEAR_STEP (c) = step;
33126 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33129 return nlist;
33132 /* OpenMP 4.0:
33133 safelen ( constant-expression ) */
33135 static tree
33136 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33137 location_t location)
33139 tree t, c;
33141 matching_parens parens;
33142 if (!parens.require_open (parser))
33143 return list;
33145 t = cp_parser_constant_expression (parser);
33147 if (t == error_mark_node
33148 || !parens.require_close (parser))
33149 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33150 /*or_comma=*/false,
33151 /*consume_paren=*/true);
33153 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33155 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33156 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33157 OMP_CLAUSE_CHAIN (c) = list;
33159 return c;
33162 /* OpenMP 4.0:
33163 simdlen ( constant-expression ) */
33165 static tree
33166 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33167 location_t location)
33169 tree t, c;
33171 matching_parens parens;
33172 if (!parens.require_open (parser))
33173 return list;
33175 t = cp_parser_constant_expression (parser);
33177 if (t == error_mark_node
33178 || !parens.require_close (parser))
33179 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33180 /*or_comma=*/false,
33181 /*consume_paren=*/true);
33183 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33185 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33186 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33187 OMP_CLAUSE_CHAIN (c) = list;
33189 return c;
33192 /* OpenMP 4.5:
33193 vec:
33194 identifier [+/- integer]
33195 vec , identifier [+/- integer]
33198 static tree
33199 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33200 tree list)
33202 tree vec = NULL;
33204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33206 cp_parser_error (parser, "expected identifier");
33207 return list;
33210 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33212 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33213 tree t, identifier = cp_parser_identifier (parser);
33214 tree addend = NULL;
33216 if (identifier == error_mark_node)
33217 t = error_mark_node;
33218 else
33220 t = cp_parser_lookup_name_simple
33221 (parser, identifier,
33222 cp_lexer_peek_token (parser->lexer)->location);
33223 if (t == error_mark_node)
33224 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33225 id_loc);
33228 bool neg = false;
33229 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33230 neg = true;
33231 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33233 addend = integer_zero_node;
33234 goto add_to_vector;
33236 cp_lexer_consume_token (parser->lexer);
33238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33240 cp_parser_error (parser, "expected integer");
33241 return list;
33244 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33245 if (TREE_CODE (addend) != INTEGER_CST)
33247 cp_parser_error (parser, "expected integer");
33248 return list;
33250 cp_lexer_consume_token (parser->lexer);
33252 add_to_vector:
33253 if (t != error_mark_node)
33255 vec = tree_cons (addend, t, vec);
33256 if (neg)
33257 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33261 break;
33263 cp_lexer_consume_token (parser->lexer);
33266 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33268 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33269 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33270 OMP_CLAUSE_DECL (u) = nreverse (vec);
33271 OMP_CLAUSE_CHAIN (u) = list;
33272 return u;
33274 return list;
33277 /* OpenMP 4.0:
33278 depend ( depend-kind : variable-list )
33280 depend-kind:
33281 in | out | inout
33283 OpenMP 4.5:
33284 depend ( source )
33286 depend ( sink : vec ) */
33288 static tree
33289 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33291 tree nlist, c;
33292 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33294 matching_parens parens;
33295 if (!parens.require_open (parser))
33296 return list;
33298 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33300 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33301 const char *p = IDENTIFIER_POINTER (id);
33303 if (strcmp ("in", p) == 0)
33304 kind = OMP_CLAUSE_DEPEND_IN;
33305 else if (strcmp ("inout", p) == 0)
33306 kind = OMP_CLAUSE_DEPEND_INOUT;
33307 else if (strcmp ("out", p) == 0)
33308 kind = OMP_CLAUSE_DEPEND_OUT;
33309 else if (strcmp ("source", p) == 0)
33310 kind = OMP_CLAUSE_DEPEND_SOURCE;
33311 else if (strcmp ("sink", p) == 0)
33312 kind = OMP_CLAUSE_DEPEND_SINK;
33313 else
33314 goto invalid_kind;
33316 else
33317 goto invalid_kind;
33319 cp_lexer_consume_token (parser->lexer);
33321 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33323 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33324 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33325 OMP_CLAUSE_DECL (c) = NULL_TREE;
33326 OMP_CLAUSE_CHAIN (c) = list;
33327 if (!parens.require_close (parser))
33328 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33329 /*or_comma=*/false,
33330 /*consume_paren=*/true);
33331 return c;
33334 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33335 goto resync_fail;
33337 if (kind == OMP_CLAUSE_DEPEND_SINK)
33338 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33339 else
33341 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33342 list, NULL);
33344 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33345 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33347 return nlist;
33349 invalid_kind:
33350 cp_parser_error (parser, "invalid depend kind");
33351 resync_fail:
33352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33353 /*or_comma=*/false,
33354 /*consume_paren=*/true);
33355 return list;
33358 /* OpenMP 4.0:
33359 map ( map-kind : variable-list )
33360 map ( variable-list )
33362 map-kind:
33363 alloc | to | from | tofrom
33365 OpenMP 4.5:
33366 map-kind:
33367 alloc | to | from | tofrom | release | delete
33369 map ( always [,] map-kind: variable-list ) */
33371 static tree
33372 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33374 tree nlist, c;
33375 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33376 bool always = false;
33378 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33379 return list;
33381 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33383 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33384 const char *p = IDENTIFIER_POINTER (id);
33386 if (strcmp ("always", p) == 0)
33388 int nth = 2;
33389 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33390 nth++;
33391 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33392 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33393 == RID_DELETE))
33394 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33395 == CPP_COLON))
33397 always = true;
33398 cp_lexer_consume_token (parser->lexer);
33399 if (nth == 3)
33400 cp_lexer_consume_token (parser->lexer);
33405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33406 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33408 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33409 const char *p = IDENTIFIER_POINTER (id);
33411 if (strcmp ("alloc", p) == 0)
33412 kind = GOMP_MAP_ALLOC;
33413 else if (strcmp ("to", p) == 0)
33414 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33415 else if (strcmp ("from", p) == 0)
33416 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33417 else if (strcmp ("tofrom", p) == 0)
33418 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33419 else if (strcmp ("release", p) == 0)
33420 kind = GOMP_MAP_RELEASE;
33421 else
33423 cp_parser_error (parser, "invalid map kind");
33424 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33425 /*or_comma=*/false,
33426 /*consume_paren=*/true);
33427 return list;
33429 cp_lexer_consume_token (parser->lexer);
33430 cp_lexer_consume_token (parser->lexer);
33432 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33433 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33435 kind = GOMP_MAP_DELETE;
33436 cp_lexer_consume_token (parser->lexer);
33437 cp_lexer_consume_token (parser->lexer);
33440 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33441 NULL);
33443 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33444 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33446 return nlist;
33449 /* OpenMP 4.0:
33450 device ( expression ) */
33452 static tree
33453 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33454 location_t location)
33456 tree t, c;
33458 matching_parens parens;
33459 if (!parens.require_open (parser))
33460 return list;
33462 t = cp_parser_expression (parser);
33464 if (t == error_mark_node
33465 || !parens.require_close (parser))
33466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33467 /*or_comma=*/false,
33468 /*consume_paren=*/true);
33470 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33471 "device", location);
33473 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33474 OMP_CLAUSE_DEVICE_ID (c) = t;
33475 OMP_CLAUSE_CHAIN (c) = list;
33477 return c;
33480 /* OpenMP 4.0:
33481 dist_schedule ( static )
33482 dist_schedule ( static , expression ) */
33484 static tree
33485 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33486 location_t location)
33488 tree c, t;
33490 matching_parens parens;
33491 if (!parens.require_open (parser))
33492 return list;
33494 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33496 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33497 goto invalid_kind;
33498 cp_lexer_consume_token (parser->lexer);
33500 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33502 cp_lexer_consume_token (parser->lexer);
33504 t = cp_parser_assignment_expression (parser);
33506 if (t == error_mark_node)
33507 goto resync_fail;
33508 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33510 if (!parens.require_close (parser))
33511 goto resync_fail;
33513 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33514 goto resync_fail;
33516 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33517 location);
33518 OMP_CLAUSE_CHAIN (c) = list;
33519 return c;
33521 invalid_kind:
33522 cp_parser_error (parser, "invalid dist_schedule kind");
33523 resync_fail:
33524 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33525 /*or_comma=*/false,
33526 /*consume_paren=*/true);
33527 return list;
33530 /* OpenMP 4.0:
33531 proc_bind ( proc-bind-kind )
33533 proc-bind-kind:
33534 master | close | spread */
33536 static tree
33537 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33538 location_t location)
33540 tree c;
33541 enum omp_clause_proc_bind_kind kind;
33543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33544 return list;
33546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33548 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33549 const char *p = IDENTIFIER_POINTER (id);
33551 if (strcmp ("master", p) == 0)
33552 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33553 else if (strcmp ("close", p) == 0)
33554 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33555 else if (strcmp ("spread", p) == 0)
33556 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33557 else
33558 goto invalid_kind;
33560 else
33561 goto invalid_kind;
33563 cp_lexer_consume_token (parser->lexer);
33564 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33565 goto resync_fail;
33567 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33568 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33569 location);
33570 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33571 OMP_CLAUSE_CHAIN (c) = list;
33572 return c;
33574 invalid_kind:
33575 cp_parser_error (parser, "invalid depend kind");
33576 resync_fail:
33577 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33578 /*or_comma=*/false,
33579 /*consume_paren=*/true);
33580 return list;
33583 /* OpenACC:
33584 async [( int-expr )] */
33586 static tree
33587 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33589 tree c, t;
33590 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33592 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33594 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33596 matching_parens parens;
33597 parens.consume_open (parser);
33599 t = cp_parser_expression (parser);
33600 if (t == error_mark_node
33601 || !parens.require_close (parser))
33602 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33603 /*or_comma=*/false,
33604 /*consume_paren=*/true);
33607 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33609 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33610 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33611 OMP_CLAUSE_CHAIN (c) = list;
33612 list = c;
33614 return list;
33617 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33618 is a bitmask in MASK. Return the list of clauses found. */
33620 static tree
33621 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33622 const char *where, cp_token *pragma_tok,
33623 bool finish_p = true)
33625 tree clauses = NULL;
33626 bool first = true;
33628 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33630 location_t here;
33631 pragma_omp_clause c_kind;
33632 omp_clause_code code;
33633 const char *c_name;
33634 tree prev = clauses;
33636 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33637 cp_lexer_consume_token (parser->lexer);
33639 here = cp_lexer_peek_token (parser->lexer)->location;
33640 c_kind = cp_parser_omp_clause_name (parser);
33642 switch (c_kind)
33644 case PRAGMA_OACC_CLAUSE_ASYNC:
33645 clauses = cp_parser_oacc_clause_async (parser, clauses);
33646 c_name = "async";
33647 break;
33648 case PRAGMA_OACC_CLAUSE_AUTO:
33649 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33650 clauses, here);
33651 c_name = "auto";
33652 break;
33653 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33654 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33655 c_name = "collapse";
33656 break;
33657 case PRAGMA_OACC_CLAUSE_COPY:
33658 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33659 c_name = "copy";
33660 break;
33661 case PRAGMA_OACC_CLAUSE_COPYIN:
33662 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33663 c_name = "copyin";
33664 break;
33665 case PRAGMA_OACC_CLAUSE_COPYOUT:
33666 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33667 c_name = "copyout";
33668 break;
33669 case PRAGMA_OACC_CLAUSE_CREATE:
33670 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33671 c_name = "create";
33672 break;
33673 case PRAGMA_OACC_CLAUSE_DELETE:
33674 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33675 c_name = "delete";
33676 break;
33677 case PRAGMA_OMP_CLAUSE_DEFAULT:
33678 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33679 c_name = "default";
33680 break;
33681 case PRAGMA_OACC_CLAUSE_DEVICE:
33682 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33683 c_name = "device";
33684 break;
33685 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33686 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33687 c_name = "deviceptr";
33688 break;
33689 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33690 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33691 c_name = "device_resident";
33692 break;
33693 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33694 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33695 clauses);
33696 c_name = "firstprivate";
33697 break;
33698 case PRAGMA_OACC_CLAUSE_GANG:
33699 c_name = "gang";
33700 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33701 c_name, clauses);
33702 break;
33703 case PRAGMA_OACC_CLAUSE_HOST:
33704 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33705 c_name = "host";
33706 break;
33707 case PRAGMA_OACC_CLAUSE_IF:
33708 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33709 c_name = "if";
33710 break;
33711 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33712 clauses = cp_parser_oacc_simple_clause (parser,
33713 OMP_CLAUSE_INDEPENDENT,
33714 clauses, here);
33715 c_name = "independent";
33716 break;
33717 case PRAGMA_OACC_CLAUSE_LINK:
33718 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33719 c_name = "link";
33720 break;
33721 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33722 code = OMP_CLAUSE_NUM_GANGS;
33723 c_name = "num_gangs";
33724 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33725 clauses);
33726 break;
33727 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33728 c_name = "num_workers";
33729 code = OMP_CLAUSE_NUM_WORKERS;
33730 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33731 clauses);
33732 break;
33733 case PRAGMA_OACC_CLAUSE_PRESENT:
33734 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33735 c_name = "present";
33736 break;
33737 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33738 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33739 c_name = "present_or_copy";
33740 break;
33741 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33742 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33743 c_name = "present_or_copyin";
33744 break;
33745 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33746 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33747 c_name = "present_or_copyout";
33748 break;
33749 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33750 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33751 c_name = "present_or_create";
33752 break;
33753 case PRAGMA_OACC_CLAUSE_PRIVATE:
33754 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33755 clauses);
33756 c_name = "private";
33757 break;
33758 case PRAGMA_OACC_CLAUSE_REDUCTION:
33759 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33760 c_name = "reduction";
33761 break;
33762 case PRAGMA_OACC_CLAUSE_SELF:
33763 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33764 c_name = "self";
33765 break;
33766 case PRAGMA_OACC_CLAUSE_SEQ:
33767 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33768 clauses, here);
33769 c_name = "seq";
33770 break;
33771 case PRAGMA_OACC_CLAUSE_TILE:
33772 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33773 c_name = "tile";
33774 break;
33775 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33776 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33777 clauses);
33778 c_name = "use_device";
33779 break;
33780 case PRAGMA_OACC_CLAUSE_VECTOR:
33781 c_name = "vector";
33782 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33783 c_name, clauses);
33784 break;
33785 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33786 c_name = "vector_length";
33787 code = OMP_CLAUSE_VECTOR_LENGTH;
33788 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33789 clauses);
33790 break;
33791 case PRAGMA_OACC_CLAUSE_WAIT:
33792 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33793 c_name = "wait";
33794 break;
33795 case PRAGMA_OACC_CLAUSE_WORKER:
33796 c_name = "worker";
33797 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33798 c_name, clauses);
33799 break;
33800 default:
33801 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33802 goto saw_error;
33805 first = false;
33807 if (((mask >> c_kind) & 1) == 0)
33809 /* Remove the invalid clause(s) from the list to avoid
33810 confusing the rest of the compiler. */
33811 clauses = prev;
33812 error_at (here, "%qs is not valid for %qs", c_name, where);
33816 saw_error:
33817 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33819 if (finish_p)
33820 return finish_omp_clauses (clauses, C_ORT_ACC);
33822 return clauses;
33825 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33826 is a bitmask in MASK. Return the list of clauses found; the result
33827 of clause default goes in *pdefault. */
33829 static tree
33830 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33831 const char *where, cp_token *pragma_tok,
33832 bool finish_p = true)
33834 tree clauses = NULL;
33835 bool first = true;
33836 cp_token *token = NULL;
33838 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33840 pragma_omp_clause c_kind;
33841 const char *c_name;
33842 tree prev = clauses;
33844 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33845 cp_lexer_consume_token (parser->lexer);
33847 token = cp_lexer_peek_token (parser->lexer);
33848 c_kind = cp_parser_omp_clause_name (parser);
33850 switch (c_kind)
33852 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33853 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33854 token->location);
33855 c_name = "collapse";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_COPYIN:
33858 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33859 c_name = "copyin";
33860 break;
33861 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33862 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33863 clauses);
33864 c_name = "copyprivate";
33865 break;
33866 case PRAGMA_OMP_CLAUSE_DEFAULT:
33867 clauses = cp_parser_omp_clause_default (parser, clauses,
33868 token->location, false);
33869 c_name = "default";
33870 break;
33871 case PRAGMA_OMP_CLAUSE_FINAL:
33872 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33873 c_name = "final";
33874 break;
33875 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33876 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33877 clauses);
33878 c_name = "firstprivate";
33879 break;
33880 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33881 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33882 token->location);
33883 c_name = "grainsize";
33884 break;
33885 case PRAGMA_OMP_CLAUSE_HINT:
33886 clauses = cp_parser_omp_clause_hint (parser, clauses,
33887 token->location);
33888 c_name = "hint";
33889 break;
33890 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33891 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33892 token->location);
33893 c_name = "defaultmap";
33894 break;
33895 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33896 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33897 clauses);
33898 c_name = "use_device_ptr";
33899 break;
33900 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33901 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33902 clauses);
33903 c_name = "is_device_ptr";
33904 break;
33905 case PRAGMA_OMP_CLAUSE_IF:
33906 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33907 true);
33908 c_name = "if";
33909 break;
33910 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33911 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33912 clauses);
33913 c_name = "lastprivate";
33914 break;
33915 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33916 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33917 token->location);
33918 c_name = "mergeable";
33919 break;
33920 case PRAGMA_OMP_CLAUSE_NOWAIT:
33921 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33922 c_name = "nowait";
33923 break;
33924 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33925 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33926 token->location);
33927 c_name = "num_tasks";
33928 break;
33929 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33930 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33931 token->location);
33932 c_name = "num_threads";
33933 break;
33934 case PRAGMA_OMP_CLAUSE_ORDERED:
33935 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33936 token->location);
33937 c_name = "ordered";
33938 break;
33939 case PRAGMA_OMP_CLAUSE_PRIORITY:
33940 clauses = cp_parser_omp_clause_priority (parser, clauses,
33941 token->location);
33942 c_name = "priority";
33943 break;
33944 case PRAGMA_OMP_CLAUSE_PRIVATE:
33945 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33946 clauses);
33947 c_name = "private";
33948 break;
33949 case PRAGMA_OMP_CLAUSE_REDUCTION:
33950 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33951 c_name = "reduction";
33952 break;
33953 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33954 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33955 token->location);
33956 c_name = "schedule";
33957 break;
33958 case PRAGMA_OMP_CLAUSE_SHARED:
33959 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33960 clauses);
33961 c_name = "shared";
33962 break;
33963 case PRAGMA_OMP_CLAUSE_UNTIED:
33964 clauses = cp_parser_omp_clause_untied (parser, clauses,
33965 token->location);
33966 c_name = "untied";
33967 break;
33968 case PRAGMA_OMP_CLAUSE_INBRANCH:
33969 case PRAGMA_CILK_CLAUSE_MASK:
33970 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33971 clauses, token->location);
33972 c_name = "inbranch";
33973 break;
33974 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33975 case PRAGMA_CILK_CLAUSE_NOMASK:
33976 clauses = cp_parser_omp_clause_branch (parser,
33977 OMP_CLAUSE_NOTINBRANCH,
33978 clauses, token->location);
33979 c_name = "notinbranch";
33980 break;
33981 case PRAGMA_OMP_CLAUSE_PARALLEL:
33982 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33983 clauses, token->location);
33984 c_name = "parallel";
33985 if (!first)
33987 clause_not_first:
33988 error_at (token->location, "%qs must be the first clause of %qs",
33989 c_name, where);
33990 clauses = prev;
33992 break;
33993 case PRAGMA_OMP_CLAUSE_FOR:
33994 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33995 clauses, token->location);
33996 c_name = "for";
33997 if (!first)
33998 goto clause_not_first;
33999 break;
34000 case PRAGMA_OMP_CLAUSE_SECTIONS:
34001 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34002 clauses, token->location);
34003 c_name = "sections";
34004 if (!first)
34005 goto clause_not_first;
34006 break;
34007 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34008 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34009 clauses, token->location);
34010 c_name = "taskgroup";
34011 if (!first)
34012 goto clause_not_first;
34013 break;
34014 case PRAGMA_OMP_CLAUSE_LINK:
34015 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34016 c_name = "to";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_TO:
34019 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34020 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34021 clauses);
34022 else
34023 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34024 c_name = "to";
34025 break;
34026 case PRAGMA_OMP_CLAUSE_FROM:
34027 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34028 c_name = "from";
34029 break;
34030 case PRAGMA_OMP_CLAUSE_UNIFORM:
34031 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34032 clauses);
34033 c_name = "uniform";
34034 break;
34035 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34036 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34037 token->location);
34038 c_name = "num_teams";
34039 break;
34040 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34041 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34042 token->location);
34043 c_name = "thread_limit";
34044 break;
34045 case PRAGMA_OMP_CLAUSE_ALIGNED:
34046 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34047 c_name = "aligned";
34048 break;
34049 case PRAGMA_OMP_CLAUSE_LINEAR:
34051 bool cilk_simd_fn = false, declare_simd = false;
34052 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
34053 cilk_simd_fn = true;
34054 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34055 declare_simd = true;
34056 clauses = cp_parser_omp_clause_linear (parser, clauses,
34057 cilk_simd_fn, declare_simd);
34059 c_name = "linear";
34060 break;
34061 case PRAGMA_OMP_CLAUSE_DEPEND:
34062 clauses = cp_parser_omp_clause_depend (parser, clauses,
34063 token->location);
34064 c_name = "depend";
34065 break;
34066 case PRAGMA_OMP_CLAUSE_MAP:
34067 clauses = cp_parser_omp_clause_map (parser, clauses);
34068 c_name = "map";
34069 break;
34070 case PRAGMA_OMP_CLAUSE_DEVICE:
34071 clauses = cp_parser_omp_clause_device (parser, clauses,
34072 token->location);
34073 c_name = "device";
34074 break;
34075 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34076 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34077 token->location);
34078 c_name = "dist_schedule";
34079 break;
34080 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34081 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34082 token->location);
34083 c_name = "proc_bind";
34084 break;
34085 case PRAGMA_OMP_CLAUSE_SAFELEN:
34086 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34087 token->location);
34088 c_name = "safelen";
34089 break;
34090 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34091 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34092 token->location);
34093 c_name = "simdlen";
34094 break;
34095 case PRAGMA_OMP_CLAUSE_NOGROUP:
34096 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34097 token->location);
34098 c_name = "nogroup";
34099 break;
34100 case PRAGMA_OMP_CLAUSE_THREADS:
34101 clauses
34102 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34103 clauses, token->location);
34104 c_name = "threads";
34105 break;
34106 case PRAGMA_OMP_CLAUSE_SIMD:
34107 clauses
34108 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34109 clauses, token->location);
34110 c_name = "simd";
34111 break;
34112 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
34113 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
34114 c_name = "simdlen";
34115 break;
34116 default:
34117 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34118 goto saw_error;
34121 first = false;
34123 if (((mask >> c_kind) & 1) == 0)
34125 /* Remove the invalid clause(s) from the list to avoid
34126 confusing the rest of the compiler. */
34127 clauses = prev;
34128 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34131 saw_error:
34132 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
34133 no reason to skip to the end. */
34134 if (!(flag_cilkplus && pragma_tok == NULL))
34135 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34136 if (finish_p)
34138 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34139 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34140 else
34141 return finish_omp_clauses (clauses, C_ORT_OMP);
34143 return clauses;
34146 /* OpenMP 2.5:
34147 structured-block:
34148 statement
34150 In practice, we're also interested in adding the statement to an
34151 outer node. So it is convenient if we work around the fact that
34152 cp_parser_statement calls add_stmt. */
34154 static unsigned
34155 cp_parser_begin_omp_structured_block (cp_parser *parser)
34157 unsigned save = parser->in_statement;
34159 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34160 This preserves the "not within loop or switch" style error messages
34161 for nonsense cases like
34162 void foo() {
34163 #pragma omp single
34164 break;
34167 if (parser->in_statement)
34168 parser->in_statement = IN_OMP_BLOCK;
34170 return save;
34173 static void
34174 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34176 parser->in_statement = save;
34179 static tree
34180 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34182 tree stmt = begin_omp_structured_block ();
34183 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34185 cp_parser_statement (parser, NULL_TREE, false, if_p);
34187 cp_parser_end_omp_structured_block (parser, save);
34188 return finish_omp_structured_block (stmt);
34191 /* OpenMP 2.5:
34192 # pragma omp atomic new-line
34193 expression-stmt
34195 expression-stmt:
34196 x binop= expr | x++ | ++x | x-- | --x
34197 binop:
34198 +, *, -, /, &, ^, |, <<, >>
34200 where x is an lvalue expression with scalar type.
34202 OpenMP 3.1:
34203 # pragma omp atomic new-line
34204 update-stmt
34206 # pragma omp atomic read new-line
34207 read-stmt
34209 # pragma omp atomic write new-line
34210 write-stmt
34212 # pragma omp atomic update new-line
34213 update-stmt
34215 # pragma omp atomic capture new-line
34216 capture-stmt
34218 # pragma omp atomic capture new-line
34219 capture-block
34221 read-stmt:
34222 v = x
34223 write-stmt:
34224 x = expr
34225 update-stmt:
34226 expression-stmt | x = x binop expr
34227 capture-stmt:
34228 v = expression-stmt
34229 capture-block:
34230 { v = x; update-stmt; } | { update-stmt; v = x; }
34232 OpenMP 4.0:
34233 update-stmt:
34234 expression-stmt | x = x binop expr | x = expr binop x
34235 capture-stmt:
34236 v = update-stmt
34237 capture-block:
34238 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34240 where x and v are lvalue expressions with scalar type. */
34242 static void
34243 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34245 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34246 tree rhs1 = NULL_TREE, orig_lhs;
34247 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34248 bool structured_block = false;
34249 bool seq_cst = false;
34251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34253 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34254 const char *p = IDENTIFIER_POINTER (id);
34256 if (!strcmp (p, "seq_cst"))
34258 seq_cst = true;
34259 cp_lexer_consume_token (parser->lexer);
34260 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34261 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34262 cp_lexer_consume_token (parser->lexer);
34265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34267 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34268 const char *p = IDENTIFIER_POINTER (id);
34270 if (!strcmp (p, "read"))
34271 code = OMP_ATOMIC_READ;
34272 else if (!strcmp (p, "write"))
34273 code = NOP_EXPR;
34274 else if (!strcmp (p, "update"))
34275 code = OMP_ATOMIC;
34276 else if (!strcmp (p, "capture"))
34277 code = OMP_ATOMIC_CAPTURE_NEW;
34278 else
34279 p = NULL;
34280 if (p)
34281 cp_lexer_consume_token (parser->lexer);
34283 if (!seq_cst)
34285 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34286 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34287 cp_lexer_consume_token (parser->lexer);
34289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34291 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34292 const char *p = IDENTIFIER_POINTER (id);
34294 if (!strcmp (p, "seq_cst"))
34296 seq_cst = true;
34297 cp_lexer_consume_token (parser->lexer);
34301 cp_parser_require_pragma_eol (parser, pragma_tok);
34303 switch (code)
34305 case OMP_ATOMIC_READ:
34306 case NOP_EXPR: /* atomic write */
34307 v = cp_parser_unary_expression (parser);
34308 if (v == error_mark_node)
34309 goto saw_error;
34310 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34311 goto saw_error;
34312 if (code == NOP_EXPR)
34313 lhs = cp_parser_expression (parser);
34314 else
34315 lhs = cp_parser_unary_expression (parser);
34316 if (lhs == error_mark_node)
34317 goto saw_error;
34318 if (code == NOP_EXPR)
34320 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34321 opcode. */
34322 code = OMP_ATOMIC;
34323 rhs = lhs;
34324 lhs = v;
34325 v = NULL_TREE;
34327 goto done;
34328 case OMP_ATOMIC_CAPTURE_NEW:
34329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34331 cp_lexer_consume_token (parser->lexer);
34332 structured_block = true;
34334 else
34336 v = cp_parser_unary_expression (parser);
34337 if (v == error_mark_node)
34338 goto saw_error;
34339 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34340 goto saw_error;
34342 default:
34343 break;
34346 restart:
34347 lhs = cp_parser_unary_expression (parser);
34348 orig_lhs = lhs;
34349 switch (TREE_CODE (lhs))
34351 case ERROR_MARK:
34352 goto saw_error;
34354 case POSTINCREMENT_EXPR:
34355 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34356 code = OMP_ATOMIC_CAPTURE_OLD;
34357 /* FALLTHROUGH */
34358 case PREINCREMENT_EXPR:
34359 lhs = TREE_OPERAND (lhs, 0);
34360 opcode = PLUS_EXPR;
34361 rhs = integer_one_node;
34362 break;
34364 case POSTDECREMENT_EXPR:
34365 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34366 code = OMP_ATOMIC_CAPTURE_OLD;
34367 /* FALLTHROUGH */
34368 case PREDECREMENT_EXPR:
34369 lhs = TREE_OPERAND (lhs, 0);
34370 opcode = MINUS_EXPR;
34371 rhs = integer_one_node;
34372 break;
34374 case COMPOUND_EXPR:
34375 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34376 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34377 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34378 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34379 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34380 (TREE_OPERAND (lhs, 1), 0), 0)))
34381 == BOOLEAN_TYPE)
34382 /* Undo effects of boolean_increment for post {in,de}crement. */
34383 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34384 /* FALLTHRU */
34385 case MODIFY_EXPR:
34386 if (TREE_CODE (lhs) == MODIFY_EXPR
34387 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34389 /* Undo effects of boolean_increment. */
34390 if (integer_onep (TREE_OPERAND (lhs, 1)))
34392 /* This is pre or post increment. */
34393 rhs = TREE_OPERAND (lhs, 1);
34394 lhs = TREE_OPERAND (lhs, 0);
34395 opcode = NOP_EXPR;
34396 if (code == OMP_ATOMIC_CAPTURE_NEW
34397 && !structured_block
34398 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34399 code = OMP_ATOMIC_CAPTURE_OLD;
34400 break;
34403 /* FALLTHRU */
34404 default:
34405 switch (cp_lexer_peek_token (parser->lexer)->type)
34407 case CPP_MULT_EQ:
34408 opcode = MULT_EXPR;
34409 break;
34410 case CPP_DIV_EQ:
34411 opcode = TRUNC_DIV_EXPR;
34412 break;
34413 case CPP_PLUS_EQ:
34414 opcode = PLUS_EXPR;
34415 break;
34416 case CPP_MINUS_EQ:
34417 opcode = MINUS_EXPR;
34418 break;
34419 case CPP_LSHIFT_EQ:
34420 opcode = LSHIFT_EXPR;
34421 break;
34422 case CPP_RSHIFT_EQ:
34423 opcode = RSHIFT_EXPR;
34424 break;
34425 case CPP_AND_EQ:
34426 opcode = BIT_AND_EXPR;
34427 break;
34428 case CPP_OR_EQ:
34429 opcode = BIT_IOR_EXPR;
34430 break;
34431 case CPP_XOR_EQ:
34432 opcode = BIT_XOR_EXPR;
34433 break;
34434 case CPP_EQ:
34435 enum cp_parser_prec oprec;
34436 cp_token *token;
34437 cp_lexer_consume_token (parser->lexer);
34438 cp_parser_parse_tentatively (parser);
34439 rhs1 = cp_parser_simple_cast_expression (parser);
34440 if (rhs1 == error_mark_node)
34442 cp_parser_abort_tentative_parse (parser);
34443 cp_parser_simple_cast_expression (parser);
34444 goto saw_error;
34446 token = cp_lexer_peek_token (parser->lexer);
34447 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34449 cp_parser_abort_tentative_parse (parser);
34450 cp_parser_parse_tentatively (parser);
34451 rhs = cp_parser_binary_expression (parser, false, true,
34452 PREC_NOT_OPERATOR, NULL);
34453 if (rhs == error_mark_node)
34455 cp_parser_abort_tentative_parse (parser);
34456 cp_parser_binary_expression (parser, false, true,
34457 PREC_NOT_OPERATOR, NULL);
34458 goto saw_error;
34460 switch (TREE_CODE (rhs))
34462 case MULT_EXPR:
34463 case TRUNC_DIV_EXPR:
34464 case RDIV_EXPR:
34465 case PLUS_EXPR:
34466 case MINUS_EXPR:
34467 case LSHIFT_EXPR:
34468 case RSHIFT_EXPR:
34469 case BIT_AND_EXPR:
34470 case BIT_IOR_EXPR:
34471 case BIT_XOR_EXPR:
34472 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34474 if (cp_parser_parse_definitely (parser))
34476 opcode = TREE_CODE (rhs);
34477 rhs1 = TREE_OPERAND (rhs, 0);
34478 rhs = TREE_OPERAND (rhs, 1);
34479 goto stmt_done;
34481 else
34482 goto saw_error;
34484 break;
34485 default:
34486 break;
34488 cp_parser_abort_tentative_parse (parser);
34489 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34491 rhs = cp_parser_expression (parser);
34492 if (rhs == error_mark_node)
34493 goto saw_error;
34494 opcode = NOP_EXPR;
34495 rhs1 = NULL_TREE;
34496 goto stmt_done;
34498 cp_parser_error (parser,
34499 "invalid form of %<#pragma omp atomic%>");
34500 goto saw_error;
34502 if (!cp_parser_parse_definitely (parser))
34503 goto saw_error;
34504 switch (token->type)
34506 case CPP_SEMICOLON:
34507 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34509 code = OMP_ATOMIC_CAPTURE_OLD;
34510 v = lhs;
34511 lhs = NULL_TREE;
34512 lhs1 = rhs1;
34513 rhs1 = NULL_TREE;
34514 cp_lexer_consume_token (parser->lexer);
34515 goto restart;
34517 else if (structured_block)
34519 opcode = NOP_EXPR;
34520 rhs = rhs1;
34521 rhs1 = NULL_TREE;
34522 goto stmt_done;
34524 cp_parser_error (parser,
34525 "invalid form of %<#pragma omp atomic%>");
34526 goto saw_error;
34527 case CPP_MULT:
34528 opcode = MULT_EXPR;
34529 break;
34530 case CPP_DIV:
34531 opcode = TRUNC_DIV_EXPR;
34532 break;
34533 case CPP_PLUS:
34534 opcode = PLUS_EXPR;
34535 break;
34536 case CPP_MINUS:
34537 opcode = MINUS_EXPR;
34538 break;
34539 case CPP_LSHIFT:
34540 opcode = LSHIFT_EXPR;
34541 break;
34542 case CPP_RSHIFT:
34543 opcode = RSHIFT_EXPR;
34544 break;
34545 case CPP_AND:
34546 opcode = BIT_AND_EXPR;
34547 break;
34548 case CPP_OR:
34549 opcode = BIT_IOR_EXPR;
34550 break;
34551 case CPP_XOR:
34552 opcode = BIT_XOR_EXPR;
34553 break;
34554 default:
34555 cp_parser_error (parser,
34556 "invalid operator for %<#pragma omp atomic%>");
34557 goto saw_error;
34559 oprec = TOKEN_PRECEDENCE (token);
34560 gcc_assert (oprec != PREC_NOT_OPERATOR);
34561 if (commutative_tree_code (opcode))
34562 oprec = (enum cp_parser_prec) (oprec - 1);
34563 cp_lexer_consume_token (parser->lexer);
34564 rhs = cp_parser_binary_expression (parser, false, false,
34565 oprec, NULL);
34566 if (rhs == error_mark_node)
34567 goto saw_error;
34568 goto stmt_done;
34569 /* FALLTHROUGH */
34570 default:
34571 cp_parser_error (parser,
34572 "invalid operator for %<#pragma omp atomic%>");
34573 goto saw_error;
34575 cp_lexer_consume_token (parser->lexer);
34577 rhs = cp_parser_expression (parser);
34578 if (rhs == error_mark_node)
34579 goto saw_error;
34580 break;
34582 stmt_done:
34583 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34585 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34586 goto saw_error;
34587 v = cp_parser_unary_expression (parser);
34588 if (v == error_mark_node)
34589 goto saw_error;
34590 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34591 goto saw_error;
34592 lhs1 = cp_parser_unary_expression (parser);
34593 if (lhs1 == error_mark_node)
34594 goto saw_error;
34596 if (structured_block)
34598 cp_parser_consume_semicolon_at_end_of_statement (parser);
34599 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34601 done:
34602 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34603 if (!structured_block)
34604 cp_parser_consume_semicolon_at_end_of_statement (parser);
34605 return;
34607 saw_error:
34608 cp_parser_skip_to_end_of_block_or_statement (parser);
34609 if (structured_block)
34611 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34612 cp_lexer_consume_token (parser->lexer);
34613 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34615 cp_parser_skip_to_end_of_block_or_statement (parser);
34616 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34617 cp_lexer_consume_token (parser->lexer);
34623 /* OpenMP 2.5:
34624 # pragma omp barrier new-line */
34626 static void
34627 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34629 cp_parser_require_pragma_eol (parser, pragma_tok);
34630 finish_omp_barrier ();
34633 /* OpenMP 2.5:
34634 # pragma omp critical [(name)] new-line
34635 structured-block
34637 OpenMP 4.5:
34638 # pragma omp critical [(name) [hint(expression)]] new-line
34639 structured-block */
34641 #define OMP_CRITICAL_CLAUSE_MASK \
34642 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34644 static tree
34645 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34647 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34651 matching_parens parens;
34652 parens.consume_open (parser);
34654 name = cp_parser_identifier (parser);
34656 if (name == error_mark_node
34657 || !parens.require_close (parser))
34658 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34659 /*or_comma=*/false,
34660 /*consume_paren=*/true);
34661 if (name == error_mark_node)
34662 name = NULL;
34664 clauses = cp_parser_omp_all_clauses (parser,
34665 OMP_CRITICAL_CLAUSE_MASK,
34666 "#pragma omp critical", pragma_tok);
34668 else
34669 cp_parser_require_pragma_eol (parser, pragma_tok);
34671 stmt = cp_parser_omp_structured_block (parser, if_p);
34672 return c_finish_omp_critical (input_location, stmt, name, clauses);
34675 /* OpenMP 2.5:
34676 # pragma omp flush flush-vars[opt] new-line
34678 flush-vars:
34679 ( variable-list ) */
34681 static void
34682 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34685 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34686 cp_parser_require_pragma_eol (parser, pragma_tok);
34688 finish_omp_flush ();
34691 /* Helper function, to parse omp for increment expression. */
34693 static tree
34694 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34696 tree cond = cp_parser_binary_expression (parser, false, true,
34697 PREC_NOT_OPERATOR, NULL);
34698 if (cond == error_mark_node
34699 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34701 cp_parser_skip_to_end_of_statement (parser);
34702 return error_mark_node;
34705 switch (TREE_CODE (cond))
34707 case GT_EXPR:
34708 case GE_EXPR:
34709 case LT_EXPR:
34710 case LE_EXPR:
34711 break;
34712 case NE_EXPR:
34713 if (code == CILK_SIMD || code == CILK_FOR)
34714 break;
34715 /* Fall through: OpenMP disallows NE_EXPR. */
34716 gcc_fallthrough ();
34717 default:
34718 return error_mark_node;
34721 /* If decl is an iterator, preserve LHS and RHS of the relational
34722 expr until finish_omp_for. */
34723 if (decl
34724 && (type_dependent_expression_p (decl)
34725 || CLASS_TYPE_P (TREE_TYPE (decl))))
34726 return cond;
34728 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34729 TREE_CODE (cond),
34730 TREE_OPERAND (cond, 0), ERROR_MARK,
34731 TREE_OPERAND (cond, 1), ERROR_MARK,
34732 /*overload=*/NULL, tf_warning_or_error);
34735 /* Helper function, to parse omp for increment expression. */
34737 static tree
34738 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34740 cp_token *token = cp_lexer_peek_token (parser->lexer);
34741 enum tree_code op;
34742 tree lhs, rhs;
34743 cp_id_kind idk;
34744 bool decl_first;
34746 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34748 op = (token->type == CPP_PLUS_PLUS
34749 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34750 cp_lexer_consume_token (parser->lexer);
34751 lhs = cp_parser_simple_cast_expression (parser);
34752 if (lhs != decl
34753 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34754 return error_mark_node;
34755 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34758 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34759 if (lhs != decl
34760 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34761 return error_mark_node;
34763 token = cp_lexer_peek_token (parser->lexer);
34764 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34766 op = (token->type == CPP_PLUS_PLUS
34767 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34768 cp_lexer_consume_token (parser->lexer);
34769 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34772 op = cp_parser_assignment_operator_opt (parser);
34773 if (op == ERROR_MARK)
34774 return error_mark_node;
34776 if (op != NOP_EXPR)
34778 rhs = cp_parser_assignment_expression (parser);
34779 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34780 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34783 lhs = cp_parser_binary_expression (parser, false, false,
34784 PREC_ADDITIVE_EXPRESSION, NULL);
34785 token = cp_lexer_peek_token (parser->lexer);
34786 decl_first = (lhs == decl
34787 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34788 if (decl_first)
34789 lhs = NULL_TREE;
34790 if (token->type != CPP_PLUS
34791 && token->type != CPP_MINUS)
34792 return error_mark_node;
34796 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34797 cp_lexer_consume_token (parser->lexer);
34798 rhs = cp_parser_binary_expression (parser, false, false,
34799 PREC_ADDITIVE_EXPRESSION, NULL);
34800 token = cp_lexer_peek_token (parser->lexer);
34801 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34803 if (lhs == NULL_TREE)
34805 if (op == PLUS_EXPR)
34806 lhs = rhs;
34807 else
34808 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34809 tf_warning_or_error);
34811 else
34812 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34813 ERROR_MARK, NULL, tf_warning_or_error);
34816 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34818 if (!decl_first)
34820 if ((rhs != decl
34821 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34822 || op == MINUS_EXPR)
34823 return error_mark_node;
34824 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34826 else
34827 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34829 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34832 /* Parse the initialization statement of either an OpenMP for loop or
34833 a Cilk Plus for loop.
34835 Return true if the resulting construct should have an
34836 OMP_CLAUSE_PRIVATE added to it. */
34838 static tree
34839 cp_parser_omp_for_loop_init (cp_parser *parser,
34840 enum tree_code code,
34841 tree &this_pre_body,
34842 vec<tree, va_gc> *for_block,
34843 tree &init,
34844 tree &orig_init,
34845 tree &decl,
34846 tree &real_decl)
34848 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34849 return NULL_TREE;
34851 tree add_private_clause = NULL_TREE;
34853 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34855 init-expr:
34856 var = lb
34857 integer-type var = lb
34858 random-access-iterator-type var = lb
34859 pointer-type var = lb
34861 cp_decl_specifier_seq type_specifiers;
34863 /* First, try to parse as an initialized declaration. See
34864 cp_parser_condition, from whence the bulk of this is copied. */
34866 cp_parser_parse_tentatively (parser);
34867 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34868 /*is_trailing_return=*/false,
34869 &type_specifiers);
34870 if (cp_parser_parse_definitely (parser))
34872 /* If parsing a type specifier seq succeeded, then this
34873 MUST be a initialized declaration. */
34874 tree asm_specification, attributes;
34875 cp_declarator *declarator;
34877 declarator = cp_parser_declarator (parser,
34878 CP_PARSER_DECLARATOR_NAMED,
34879 /*ctor_dtor_or_conv_p=*/NULL,
34880 /*parenthesized_p=*/NULL,
34881 /*member_p=*/false,
34882 /*friend_p=*/false);
34883 attributes = cp_parser_attributes_opt (parser);
34884 asm_specification = cp_parser_asm_specification_opt (parser);
34886 if (declarator == cp_error_declarator)
34887 cp_parser_skip_to_end_of_statement (parser);
34889 else
34891 tree pushed_scope, auto_node;
34893 decl = start_decl (declarator, &type_specifiers,
34894 SD_INITIALIZED, attributes,
34895 /*prefix_attributes=*/NULL_TREE,
34896 &pushed_scope);
34898 auto_node = type_uses_auto (TREE_TYPE (decl));
34899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34901 if (cp_lexer_next_token_is (parser->lexer,
34902 CPP_OPEN_PAREN))
34904 if (code != CILK_SIMD && code != CILK_FOR)
34905 error ("parenthesized initialization is not allowed in "
34906 "OpenMP %<for%> loop");
34907 else
34908 error ("parenthesized initialization is "
34909 "not allowed in for-loop");
34911 else
34912 /* Trigger an error. */
34913 cp_parser_require (parser, CPP_EQ, RT_EQ);
34915 init = error_mark_node;
34916 cp_parser_skip_to_end_of_statement (parser);
34918 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34919 || type_dependent_expression_p (decl)
34920 || auto_node)
34922 bool is_direct_init, is_non_constant_init;
34924 init = cp_parser_initializer (parser,
34925 &is_direct_init,
34926 &is_non_constant_init);
34928 if (auto_node)
34930 TREE_TYPE (decl)
34931 = do_auto_deduction (TREE_TYPE (decl), init,
34932 auto_node);
34934 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34935 && !type_dependent_expression_p (decl))
34936 goto non_class;
34939 cp_finish_decl (decl, init, !is_non_constant_init,
34940 asm_specification,
34941 LOOKUP_ONLYCONVERTING);
34942 orig_init = init;
34943 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34945 vec_safe_push (for_block, this_pre_body);
34946 init = NULL_TREE;
34948 else
34950 init = pop_stmt_list (this_pre_body);
34951 if (init && TREE_CODE (init) == STATEMENT_LIST)
34953 tree_stmt_iterator i = tsi_start (init);
34954 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34955 while (!tsi_end_p (i))
34957 tree t = tsi_stmt (i);
34958 if (TREE_CODE (t) == DECL_EXPR
34959 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34961 tsi_delink (&i);
34962 vec_safe_push (for_block, t);
34963 continue;
34965 break;
34967 if (tsi_one_before_end_p (i))
34969 tree t = tsi_stmt (i);
34970 tsi_delink (&i);
34971 free_stmt_list (init);
34972 init = t;
34976 this_pre_body = NULL_TREE;
34978 else
34980 /* Consume '='. */
34981 cp_lexer_consume_token (parser->lexer);
34982 init = cp_parser_assignment_expression (parser);
34984 non_class:
34985 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34986 init = error_mark_node;
34987 else
34988 cp_finish_decl (decl, NULL_TREE,
34989 /*init_const_expr_p=*/false,
34990 asm_specification,
34991 LOOKUP_ONLYCONVERTING);
34994 if (pushed_scope)
34995 pop_scope (pushed_scope);
34998 else
35000 cp_id_kind idk;
35001 /* If parsing a type specifier sequence failed, then
35002 this MUST be a simple expression. */
35003 if (code == CILK_FOR)
35004 error ("%<_Cilk_for%> allows expression instead of declaration only "
35005 "in C, not in C++");
35006 cp_parser_parse_tentatively (parser);
35007 decl = cp_parser_primary_expression (parser, false, false,
35008 false, &idk);
35009 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35010 if (!cp_parser_error_occurred (parser)
35011 && decl
35012 && (TREE_CODE (decl) == COMPONENT_REF
35013 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35015 cp_parser_abort_tentative_parse (parser);
35016 cp_parser_parse_tentatively (parser);
35017 cp_token *token = cp_lexer_peek_token (parser->lexer);
35018 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35019 /*check_dependency_p=*/true,
35020 /*template_p=*/NULL,
35021 /*declarator_p=*/false,
35022 /*optional_p=*/false);
35023 if (name != error_mark_node
35024 && last_tok == cp_lexer_peek_token (parser->lexer))
35026 decl = cp_parser_lookup_name_simple (parser, name,
35027 token->location);
35028 if (TREE_CODE (decl) == FIELD_DECL)
35029 add_private_clause = omp_privatize_field (decl, false);
35031 cp_parser_abort_tentative_parse (parser);
35032 cp_parser_parse_tentatively (parser);
35033 decl = cp_parser_primary_expression (parser, false, false,
35034 false, &idk);
35036 if (!cp_parser_error_occurred (parser)
35037 && decl
35038 && DECL_P (decl)
35039 && CLASS_TYPE_P (TREE_TYPE (decl)))
35041 tree rhs;
35043 cp_parser_parse_definitely (parser);
35044 cp_parser_require (parser, CPP_EQ, RT_EQ);
35045 rhs = cp_parser_assignment_expression (parser);
35046 orig_init = rhs;
35047 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35048 decl, NOP_EXPR,
35049 rhs,
35050 tf_warning_or_error));
35051 if (!add_private_clause)
35052 add_private_clause = decl;
35054 else
35056 decl = NULL;
35057 cp_parser_abort_tentative_parse (parser);
35058 init = cp_parser_expression (parser);
35059 if (init)
35061 if (TREE_CODE (init) == MODIFY_EXPR
35062 || TREE_CODE (init) == MODOP_EXPR)
35063 real_decl = TREE_OPERAND (init, 0);
35067 return add_private_clause;
35070 /* Parse the restricted form of the for statement allowed by OpenMP. */
35072 static tree
35073 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35074 tree *cclauses, bool *if_p)
35076 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35077 tree real_decl, initv, condv, incrv, declv;
35078 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35079 location_t loc_first;
35080 bool collapse_err = false;
35081 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35082 vec<tree, va_gc> *for_block = make_tree_vector ();
35083 auto_vec<tree, 4> orig_inits;
35084 bool tiling = false;
35086 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35087 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35088 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35089 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35091 tiling = true;
35092 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35094 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35095 && OMP_CLAUSE_ORDERED_EXPR (cl))
35097 ordered_cl = cl;
35098 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35101 if (ordered && ordered < collapse)
35103 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35104 "%<ordered%> clause parameter is less than %<collapse%>");
35105 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35106 = build_int_cst (NULL_TREE, collapse);
35107 ordered = collapse;
35109 if (ordered)
35111 for (tree *pc = &clauses; *pc; )
35112 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35114 error_at (OMP_CLAUSE_LOCATION (*pc),
35115 "%<linear%> clause may not be specified together "
35116 "with %<ordered%> clause with a parameter");
35117 *pc = OMP_CLAUSE_CHAIN (*pc);
35119 else
35120 pc = &OMP_CLAUSE_CHAIN (*pc);
35123 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35124 count = ordered ? ordered : collapse;
35126 declv = make_tree_vec (count);
35127 initv = make_tree_vec (count);
35128 condv = make_tree_vec (count);
35129 incrv = make_tree_vec (count);
35131 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35133 for (i = 0; i < count; i++)
35135 int bracecount = 0;
35136 tree add_private_clause = NULL_TREE;
35137 location_t loc;
35139 if (code != CILK_FOR
35140 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35142 if (!collapse_err)
35143 cp_parser_error (parser, "for statement expected");
35144 return NULL;
35146 if (code == CILK_FOR
35147 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
35149 if (!collapse_err)
35150 cp_parser_error (parser, "_Cilk_for statement expected");
35151 return NULL;
35153 loc = cp_lexer_consume_token (parser->lexer)->location;
35155 matching_parens parens;
35156 if (!parens.require_open (parser))
35157 return NULL;
35159 init = orig_init = decl = real_decl = NULL;
35160 this_pre_body = push_stmt_list ();
35162 add_private_clause
35163 = cp_parser_omp_for_loop_init (parser, code,
35164 this_pre_body, for_block,
35165 init, orig_init, decl, real_decl);
35167 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35168 if (this_pre_body)
35170 this_pre_body = pop_stmt_list (this_pre_body);
35171 if (pre_body)
35173 tree t = pre_body;
35174 pre_body = push_stmt_list ();
35175 add_stmt (t);
35176 add_stmt (this_pre_body);
35177 pre_body = pop_stmt_list (pre_body);
35179 else
35180 pre_body = this_pre_body;
35183 if (decl)
35184 real_decl = decl;
35185 if (cclauses != NULL
35186 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35187 && real_decl != NULL_TREE)
35189 tree *c;
35190 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35191 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35192 && OMP_CLAUSE_DECL (*c) == real_decl)
35194 error_at (loc, "iteration variable %qD"
35195 " should not be firstprivate", real_decl);
35196 *c = OMP_CLAUSE_CHAIN (*c);
35198 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35199 && OMP_CLAUSE_DECL (*c) == real_decl)
35201 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35202 tree l = *c;
35203 *c = OMP_CLAUSE_CHAIN (*c);
35204 if (code == OMP_SIMD)
35206 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35207 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35209 else
35211 OMP_CLAUSE_CHAIN (l) = clauses;
35212 clauses = l;
35214 add_private_clause = NULL_TREE;
35216 else
35218 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35219 && OMP_CLAUSE_DECL (*c) == real_decl)
35220 add_private_clause = NULL_TREE;
35221 c = &OMP_CLAUSE_CHAIN (*c);
35225 if (add_private_clause)
35227 tree c;
35228 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35230 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35231 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35232 && OMP_CLAUSE_DECL (c) == decl)
35233 break;
35234 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35235 && OMP_CLAUSE_DECL (c) == decl)
35236 error_at (loc, "iteration variable %qD "
35237 "should not be firstprivate",
35238 decl);
35239 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35240 && OMP_CLAUSE_DECL (c) == decl)
35241 error_at (loc, "iteration variable %qD should not be reduction",
35242 decl);
35244 if (c == NULL)
35246 if (code != OMP_SIMD)
35247 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35248 else if (collapse == 1)
35249 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35250 else
35251 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35252 OMP_CLAUSE_DECL (c) = add_private_clause;
35253 c = finish_omp_clauses (c, C_ORT_OMP);
35254 if (c)
35256 OMP_CLAUSE_CHAIN (c) = clauses;
35257 clauses = c;
35258 /* For linear, signal that we need to fill up
35259 the so far unknown linear step. */
35260 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35261 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35266 cond = NULL;
35267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35268 cond = cp_parser_omp_for_cond (parser, decl, code);
35269 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35271 incr = NULL;
35272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35274 /* If decl is an iterator, preserve the operator on decl
35275 until finish_omp_for. */
35276 if (real_decl
35277 && ((processing_template_decl
35278 && (TREE_TYPE (real_decl) == NULL_TREE
35279 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35280 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35281 incr = cp_parser_omp_for_incr (parser, real_decl);
35282 else
35283 incr = cp_parser_expression (parser);
35284 if (!EXPR_HAS_LOCATION (incr))
35285 protected_set_expr_location (incr, input_location);
35288 if (!parens.require_close (parser))
35289 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35290 /*or_comma=*/false,
35291 /*consume_paren=*/true);
35293 TREE_VEC_ELT (declv, i) = decl;
35294 TREE_VEC_ELT (initv, i) = init;
35295 TREE_VEC_ELT (condv, i) = cond;
35296 TREE_VEC_ELT (incrv, i) = incr;
35297 if (orig_init)
35299 orig_inits.safe_grow_cleared (i + 1);
35300 orig_inits[i] = orig_init;
35303 if (i == count - 1)
35304 break;
35306 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35307 in between the collapsed for loops to be still considered perfectly
35308 nested. Hopefully the final version clarifies this.
35309 For now handle (multiple) {'s and empty statements. */
35310 cp_parser_parse_tentatively (parser);
35311 for (;;)
35313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35314 break;
35315 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35317 cp_lexer_consume_token (parser->lexer);
35318 bracecount++;
35320 else if (bracecount
35321 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35322 cp_lexer_consume_token (parser->lexer);
35323 else
35325 loc = cp_lexer_peek_token (parser->lexer)->location;
35326 error_at (loc, "not enough for loops to collapse");
35327 collapse_err = true;
35328 cp_parser_abort_tentative_parse (parser);
35329 declv = NULL_TREE;
35330 break;
35334 if (declv)
35336 cp_parser_parse_definitely (parser);
35337 nbraces += bracecount;
35341 if (nbraces)
35342 if_p = NULL;
35344 /* Note that we saved the original contents of this flag when we entered
35345 the structured block, and so we don't need to re-save it here. */
35346 if (code == CILK_SIMD || code == CILK_FOR)
35347 parser->in_statement = IN_CILK_SIMD_FOR;
35348 else
35349 parser->in_statement = IN_OMP_FOR;
35351 /* Note that the grammar doesn't call for a structured block here,
35352 though the loop as a whole is a structured block. */
35353 body = push_stmt_list ();
35354 cp_parser_statement (parser, NULL_TREE, false, if_p);
35355 body = pop_stmt_list (body);
35357 if (declv == NULL_TREE)
35358 ret = NULL_TREE;
35359 else
35360 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35361 body, pre_body, &orig_inits, clauses);
35363 while (nbraces)
35365 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35367 cp_lexer_consume_token (parser->lexer);
35368 nbraces--;
35370 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35371 cp_lexer_consume_token (parser->lexer);
35372 else
35374 if (!collapse_err)
35376 error_at (cp_lexer_peek_token (parser->lexer)->location,
35377 "collapsed loops not perfectly nested");
35379 collapse_err = true;
35380 cp_parser_statement_seq_opt (parser, NULL);
35381 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35382 break;
35386 while (!for_block->is_empty ())
35388 tree t = for_block->pop ();
35389 if (TREE_CODE (t) == STATEMENT_LIST)
35390 add_stmt (pop_stmt_list (t));
35391 else
35392 add_stmt (t);
35394 release_tree_vector (for_block);
35396 return ret;
35399 /* Helper function for OpenMP parsing, split clauses and call
35400 finish_omp_clauses on each of the set of clauses afterwards. */
35402 static void
35403 cp_omp_split_clauses (location_t loc, enum tree_code code,
35404 omp_clause_mask mask, tree clauses, tree *cclauses)
35406 int i;
35407 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35408 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35409 if (cclauses[i])
35410 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35413 /* OpenMP 4.0:
35414 #pragma omp simd simd-clause[optseq] new-line
35415 for-loop */
35417 #define OMP_SIMD_CLAUSE_MASK \
35418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35427 static tree
35428 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35429 char *p_name, omp_clause_mask mask, tree *cclauses,
35430 bool *if_p)
35432 tree clauses, sb, ret;
35433 unsigned int save;
35434 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35436 strcat (p_name, " simd");
35437 mask |= OMP_SIMD_CLAUSE_MASK;
35439 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35440 cclauses == NULL);
35441 if (cclauses)
35443 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35444 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35445 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35446 OMP_CLAUSE_ORDERED);
35447 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35449 error_at (OMP_CLAUSE_LOCATION (c),
35450 "%<ordered%> clause with parameter may not be specified "
35451 "on %qs construct", p_name);
35452 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35456 sb = begin_omp_structured_block ();
35457 save = cp_parser_begin_omp_structured_block (parser);
35459 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35461 cp_parser_end_omp_structured_block (parser, save);
35462 add_stmt (finish_omp_structured_block (sb));
35464 return ret;
35467 /* OpenMP 2.5:
35468 #pragma omp for for-clause[optseq] new-line
35469 for-loop
35471 OpenMP 4.0:
35472 #pragma omp for simd for-simd-clause[optseq] new-line
35473 for-loop */
35475 #define OMP_FOR_CLAUSE_MASK \
35476 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35486 static tree
35487 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35488 char *p_name, omp_clause_mask mask, tree *cclauses,
35489 bool *if_p)
35491 tree clauses, sb, ret;
35492 unsigned int save;
35493 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35495 strcat (p_name, " for");
35496 mask |= OMP_FOR_CLAUSE_MASK;
35497 /* parallel for{, simd} disallows nowait clause, but for
35498 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35499 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35500 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35501 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35502 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35503 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35505 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35507 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35508 const char *p = IDENTIFIER_POINTER (id);
35510 if (strcmp (p, "simd") == 0)
35512 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35513 if (cclauses == NULL)
35514 cclauses = cclauses_buf;
35516 cp_lexer_consume_token (parser->lexer);
35517 if (!flag_openmp) /* flag_openmp_simd */
35518 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35519 cclauses, if_p);
35520 sb = begin_omp_structured_block ();
35521 save = cp_parser_begin_omp_structured_block (parser);
35522 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35523 cclauses, if_p);
35524 cp_parser_end_omp_structured_block (parser, save);
35525 tree body = finish_omp_structured_block (sb);
35526 if (ret == NULL)
35527 return ret;
35528 ret = make_node (OMP_FOR);
35529 TREE_TYPE (ret) = void_type_node;
35530 OMP_FOR_BODY (ret) = body;
35531 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35532 SET_EXPR_LOCATION (ret, loc);
35533 add_stmt (ret);
35534 return ret;
35537 if (!flag_openmp) /* flag_openmp_simd */
35539 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35540 return NULL_TREE;
35543 /* Composite distribute parallel for disallows linear clause. */
35544 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35545 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35547 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35548 cclauses == NULL);
35549 if (cclauses)
35551 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35552 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35555 sb = begin_omp_structured_block ();
35556 save = cp_parser_begin_omp_structured_block (parser);
35558 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35560 cp_parser_end_omp_structured_block (parser, save);
35561 add_stmt (finish_omp_structured_block (sb));
35563 return ret;
35566 /* OpenMP 2.5:
35567 # pragma omp master new-line
35568 structured-block */
35570 static tree
35571 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35573 cp_parser_require_pragma_eol (parser, pragma_tok);
35574 return c_finish_omp_master (input_location,
35575 cp_parser_omp_structured_block (parser, if_p));
35578 /* OpenMP 2.5:
35579 # pragma omp ordered new-line
35580 structured-block
35582 OpenMP 4.5:
35583 # pragma omp ordered ordered-clauses new-line
35584 structured-block */
35586 #define OMP_ORDERED_CLAUSE_MASK \
35587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35590 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35591 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35593 static bool
35594 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35595 enum pragma_context context, bool *if_p)
35597 location_t loc = pragma_tok->location;
35599 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35601 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35602 const char *p = IDENTIFIER_POINTER (id);
35604 if (strcmp (p, "depend") == 0)
35606 if (!flag_openmp) /* flag_openmp_simd */
35608 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35609 return false;
35611 if (context == pragma_stmt)
35613 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35614 "%<depend%> clause may only be used in compound "
35615 "statements");
35616 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35617 return false;
35619 tree clauses
35620 = cp_parser_omp_all_clauses (parser,
35621 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35622 "#pragma omp ordered", pragma_tok);
35623 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35624 return false;
35628 tree clauses
35629 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35630 "#pragma omp ordered", pragma_tok);
35632 if (!flag_openmp /* flag_openmp_simd */
35633 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35634 return false;
35636 c_finish_omp_ordered (loc, clauses,
35637 cp_parser_omp_structured_block (parser, if_p));
35638 return true;
35641 /* OpenMP 2.5:
35643 section-scope:
35644 { section-sequence }
35646 section-sequence:
35647 section-directive[opt] structured-block
35648 section-sequence section-directive structured-block */
35650 static tree
35651 cp_parser_omp_sections_scope (cp_parser *parser)
35653 tree stmt, substmt;
35654 bool error_suppress = false;
35655 cp_token *tok;
35657 matching_braces braces;
35658 if (!braces.require_open (parser))
35659 return NULL_TREE;
35661 stmt = push_stmt_list ();
35663 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35664 != PRAGMA_OMP_SECTION)
35666 substmt = cp_parser_omp_structured_block (parser, NULL);
35667 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35668 add_stmt (substmt);
35671 while (1)
35673 tok = cp_lexer_peek_token (parser->lexer);
35674 if (tok->type == CPP_CLOSE_BRACE)
35675 break;
35676 if (tok->type == CPP_EOF)
35677 break;
35679 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35681 cp_lexer_consume_token (parser->lexer);
35682 cp_parser_require_pragma_eol (parser, tok);
35683 error_suppress = false;
35685 else if (!error_suppress)
35687 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35688 error_suppress = true;
35691 substmt = cp_parser_omp_structured_block (parser, NULL);
35692 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35693 add_stmt (substmt);
35695 braces.require_close (parser);
35697 substmt = pop_stmt_list (stmt);
35699 stmt = make_node (OMP_SECTIONS);
35700 TREE_TYPE (stmt) = void_type_node;
35701 OMP_SECTIONS_BODY (stmt) = substmt;
35703 add_stmt (stmt);
35704 return stmt;
35707 /* OpenMP 2.5:
35708 # pragma omp sections sections-clause[optseq] newline
35709 sections-scope */
35711 #define OMP_SECTIONS_CLAUSE_MASK \
35712 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35718 static tree
35719 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35720 char *p_name, omp_clause_mask mask, tree *cclauses)
35722 tree clauses, ret;
35723 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35725 strcat (p_name, " sections");
35726 mask |= OMP_SECTIONS_CLAUSE_MASK;
35727 if (cclauses)
35728 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35730 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35731 cclauses == NULL);
35732 if (cclauses)
35734 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35735 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35738 ret = cp_parser_omp_sections_scope (parser);
35739 if (ret)
35740 OMP_SECTIONS_CLAUSES (ret) = clauses;
35742 return ret;
35745 /* OpenMP 2.5:
35746 # pragma omp parallel parallel-clause[optseq] new-line
35747 structured-block
35748 # pragma omp parallel for parallel-for-clause[optseq] new-line
35749 structured-block
35750 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35751 structured-block
35753 OpenMP 4.0:
35754 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35755 structured-block */
35757 #define OMP_PARALLEL_CLAUSE_MASK \
35758 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35768 static tree
35769 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35770 char *p_name, omp_clause_mask mask, tree *cclauses,
35771 bool *if_p)
35773 tree stmt, clauses, block;
35774 unsigned int save;
35775 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35777 strcat (p_name, " parallel");
35778 mask |= OMP_PARALLEL_CLAUSE_MASK;
35779 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35780 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35781 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35782 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35786 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35787 if (cclauses == NULL)
35788 cclauses = cclauses_buf;
35790 cp_lexer_consume_token (parser->lexer);
35791 if (!flag_openmp) /* flag_openmp_simd */
35792 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35793 if_p);
35794 block = begin_omp_parallel ();
35795 save = cp_parser_begin_omp_structured_block (parser);
35796 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35797 if_p);
35798 cp_parser_end_omp_structured_block (parser, save);
35799 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35800 block);
35801 if (ret == NULL_TREE)
35802 return ret;
35803 OMP_PARALLEL_COMBINED (stmt) = 1;
35804 return stmt;
35806 /* When combined with distribute, parallel has to be followed by for.
35807 #pragma omp target parallel is allowed though. */
35808 else if (cclauses
35809 && (mask & (OMP_CLAUSE_MASK_1
35810 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35812 error_at (loc, "expected %<for%> after %qs", p_name);
35813 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35814 return NULL_TREE;
35816 else if (!flag_openmp) /* flag_openmp_simd */
35818 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35819 return NULL_TREE;
35821 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35823 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35824 const char *p = IDENTIFIER_POINTER (id);
35825 if (strcmp (p, "sections") == 0)
35827 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35828 cclauses = cclauses_buf;
35830 cp_lexer_consume_token (parser->lexer);
35831 block = begin_omp_parallel ();
35832 save = cp_parser_begin_omp_structured_block (parser);
35833 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35834 cp_parser_end_omp_structured_block (parser, save);
35835 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35836 block);
35837 OMP_PARALLEL_COMBINED (stmt) = 1;
35838 return stmt;
35842 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35843 cclauses == NULL);
35844 if (cclauses)
35846 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35847 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35850 block = begin_omp_parallel ();
35851 save = cp_parser_begin_omp_structured_block (parser);
35852 cp_parser_statement (parser, NULL_TREE, false, if_p);
35853 cp_parser_end_omp_structured_block (parser, save);
35854 stmt = finish_omp_parallel (clauses, block);
35855 return stmt;
35858 /* OpenMP 2.5:
35859 # pragma omp single single-clause[optseq] new-line
35860 structured-block */
35862 #define OMP_SINGLE_CLAUSE_MASK \
35863 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35868 static tree
35869 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35871 tree stmt = make_node (OMP_SINGLE);
35872 TREE_TYPE (stmt) = void_type_node;
35874 OMP_SINGLE_CLAUSES (stmt)
35875 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35876 "#pragma omp single", pragma_tok);
35877 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35879 return add_stmt (stmt);
35882 /* OpenMP 3.0:
35883 # pragma omp task task-clause[optseq] new-line
35884 structured-block */
35886 #define OMP_TASK_CLAUSE_MASK \
35887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35898 static tree
35899 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35901 tree clauses, block;
35902 unsigned int save;
35904 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35905 "#pragma omp task", pragma_tok);
35906 block = begin_omp_task ();
35907 save = cp_parser_begin_omp_structured_block (parser);
35908 cp_parser_statement (parser, NULL_TREE, false, if_p);
35909 cp_parser_end_omp_structured_block (parser, save);
35910 return finish_omp_task (clauses, block);
35913 /* OpenMP 3.0:
35914 # pragma omp taskwait new-line */
35916 static void
35917 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35919 cp_parser_require_pragma_eol (parser, pragma_tok);
35920 finish_omp_taskwait ();
35923 /* OpenMP 3.1:
35924 # pragma omp taskyield new-line */
35926 static void
35927 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35929 cp_parser_require_pragma_eol (parser, pragma_tok);
35930 finish_omp_taskyield ();
35933 /* OpenMP 4.0:
35934 # pragma omp taskgroup new-line
35935 structured-block */
35937 static tree
35938 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35940 cp_parser_require_pragma_eol (parser, pragma_tok);
35941 return c_finish_omp_taskgroup (input_location,
35942 cp_parser_omp_structured_block (parser,
35943 if_p));
35947 /* OpenMP 2.5:
35948 # pragma omp threadprivate (variable-list) */
35950 static void
35951 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35953 tree vars;
35955 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35956 cp_parser_require_pragma_eol (parser, pragma_tok);
35958 finish_omp_threadprivate (vars);
35961 /* OpenMP 4.0:
35962 # pragma omp cancel cancel-clause[optseq] new-line */
35964 #define OMP_CANCEL_CLAUSE_MASK \
35965 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35971 static void
35972 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35974 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35975 "#pragma omp cancel", pragma_tok);
35976 finish_omp_cancel (clauses);
35979 /* OpenMP 4.0:
35980 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35982 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35988 static void
35989 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35990 enum pragma_context context)
35992 tree clauses;
35993 bool point_seen = false;
35995 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35997 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35998 const char *p = IDENTIFIER_POINTER (id);
36000 if (strcmp (p, "point") == 0)
36002 cp_lexer_consume_token (parser->lexer);
36003 point_seen = true;
36006 if (!point_seen)
36008 cp_parser_error (parser, "expected %<point%>");
36009 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36010 return;
36013 if (context != pragma_compound)
36015 if (context == pragma_stmt)
36016 error_at (pragma_tok->location,
36017 "%<#pragma %s%> may only be used in compound statements",
36018 "omp cancellation point");
36019 else
36020 cp_parser_error (parser, "expected declaration specifiers");
36021 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36022 return;
36025 clauses = cp_parser_omp_all_clauses (parser,
36026 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36027 "#pragma omp cancellation point",
36028 pragma_tok);
36029 finish_omp_cancellation_point (clauses);
36032 /* OpenMP 4.0:
36033 #pragma omp distribute distribute-clause[optseq] new-line
36034 for-loop */
36036 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36037 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36043 static tree
36044 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36045 char *p_name, omp_clause_mask mask, tree *cclauses,
36046 bool *if_p)
36048 tree clauses, sb, ret;
36049 unsigned int save;
36050 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36052 strcat (p_name, " distribute");
36053 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36057 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36058 const char *p = IDENTIFIER_POINTER (id);
36059 bool simd = false;
36060 bool parallel = false;
36062 if (strcmp (p, "simd") == 0)
36063 simd = true;
36064 else
36065 parallel = strcmp (p, "parallel") == 0;
36066 if (parallel || simd)
36068 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36069 if (cclauses == NULL)
36070 cclauses = cclauses_buf;
36071 cp_lexer_consume_token (parser->lexer);
36072 if (!flag_openmp) /* flag_openmp_simd */
36074 if (simd)
36075 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36076 cclauses, if_p);
36077 else
36078 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36079 cclauses, if_p);
36081 sb = begin_omp_structured_block ();
36082 save = cp_parser_begin_omp_structured_block (parser);
36083 if (simd)
36084 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36085 cclauses, if_p);
36086 else
36087 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36088 cclauses, if_p);
36089 cp_parser_end_omp_structured_block (parser, save);
36090 tree body = finish_omp_structured_block (sb);
36091 if (ret == NULL)
36092 return ret;
36093 ret = make_node (OMP_DISTRIBUTE);
36094 TREE_TYPE (ret) = void_type_node;
36095 OMP_FOR_BODY (ret) = body;
36096 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36097 SET_EXPR_LOCATION (ret, loc);
36098 add_stmt (ret);
36099 return ret;
36102 if (!flag_openmp) /* flag_openmp_simd */
36104 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36105 return NULL_TREE;
36108 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36109 cclauses == NULL);
36110 if (cclauses)
36112 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36113 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36116 sb = begin_omp_structured_block ();
36117 save = cp_parser_begin_omp_structured_block (parser);
36119 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36121 cp_parser_end_omp_structured_block (parser, save);
36122 add_stmt (finish_omp_structured_block (sb));
36124 return ret;
36127 /* OpenMP 4.0:
36128 # pragma omp teams teams-clause[optseq] new-line
36129 structured-block */
36131 #define OMP_TEAMS_CLAUSE_MASK \
36132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36140 static tree
36141 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36142 char *p_name, omp_clause_mask mask, tree *cclauses,
36143 bool *if_p)
36145 tree clauses, sb, ret;
36146 unsigned int save;
36147 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36149 strcat (p_name, " teams");
36150 mask |= OMP_TEAMS_CLAUSE_MASK;
36152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36155 const char *p = IDENTIFIER_POINTER (id);
36156 if (strcmp (p, "distribute") == 0)
36158 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36159 if (cclauses == NULL)
36160 cclauses = cclauses_buf;
36162 cp_lexer_consume_token (parser->lexer);
36163 if (!flag_openmp) /* flag_openmp_simd */
36164 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36165 cclauses, if_p);
36166 sb = begin_omp_structured_block ();
36167 save = cp_parser_begin_omp_structured_block (parser);
36168 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36169 cclauses, if_p);
36170 cp_parser_end_omp_structured_block (parser, save);
36171 tree body = finish_omp_structured_block (sb);
36172 if (ret == NULL)
36173 return ret;
36174 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36175 ret = make_node (OMP_TEAMS);
36176 TREE_TYPE (ret) = void_type_node;
36177 OMP_TEAMS_CLAUSES (ret) = clauses;
36178 OMP_TEAMS_BODY (ret) = body;
36179 OMP_TEAMS_COMBINED (ret) = 1;
36180 SET_EXPR_LOCATION (ret, loc);
36181 return add_stmt (ret);
36184 if (!flag_openmp) /* flag_openmp_simd */
36186 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36187 return NULL_TREE;
36190 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36191 cclauses == NULL);
36192 if (cclauses)
36194 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36195 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36198 tree stmt = make_node (OMP_TEAMS);
36199 TREE_TYPE (stmt) = void_type_node;
36200 OMP_TEAMS_CLAUSES (stmt) = clauses;
36201 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36202 SET_EXPR_LOCATION (stmt, loc);
36204 return add_stmt (stmt);
36207 /* OpenMP 4.0:
36208 # pragma omp target data target-data-clause[optseq] new-line
36209 structured-block */
36211 #define OMP_TARGET_DATA_CLAUSE_MASK \
36212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36217 static tree
36218 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36220 tree clauses
36221 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36222 "#pragma omp target data", pragma_tok);
36223 int map_seen = 0;
36224 for (tree *pc = &clauses; *pc;)
36226 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36227 switch (OMP_CLAUSE_MAP_KIND (*pc))
36229 case GOMP_MAP_TO:
36230 case GOMP_MAP_ALWAYS_TO:
36231 case GOMP_MAP_FROM:
36232 case GOMP_MAP_ALWAYS_FROM:
36233 case GOMP_MAP_TOFROM:
36234 case GOMP_MAP_ALWAYS_TOFROM:
36235 case GOMP_MAP_ALLOC:
36236 map_seen = 3;
36237 break;
36238 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36239 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36240 case GOMP_MAP_ALWAYS_POINTER:
36241 break;
36242 default:
36243 map_seen |= 1;
36244 error_at (OMP_CLAUSE_LOCATION (*pc),
36245 "%<#pragma omp target data%> with map-type other "
36246 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36247 "on %<map%> clause");
36248 *pc = OMP_CLAUSE_CHAIN (*pc);
36249 continue;
36251 pc = &OMP_CLAUSE_CHAIN (*pc);
36254 if (map_seen != 3)
36256 if (map_seen == 0)
36257 error_at (pragma_tok->location,
36258 "%<#pragma omp target data%> must contain at least "
36259 "one %<map%> clause");
36260 return NULL_TREE;
36263 tree stmt = make_node (OMP_TARGET_DATA);
36264 TREE_TYPE (stmt) = void_type_node;
36265 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36267 keep_next_level (true);
36268 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36270 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36271 return add_stmt (stmt);
36274 /* OpenMP 4.5:
36275 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36276 structured-block */
36278 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36279 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36285 static tree
36286 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36287 enum pragma_context context)
36289 bool data_seen = false;
36290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36293 const char *p = IDENTIFIER_POINTER (id);
36295 if (strcmp (p, "data") == 0)
36297 cp_lexer_consume_token (parser->lexer);
36298 data_seen = true;
36301 if (!data_seen)
36303 cp_parser_error (parser, "expected %<data%>");
36304 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36305 return NULL_TREE;
36308 if (context == pragma_stmt)
36310 error_at (pragma_tok->location,
36311 "%<#pragma %s%> may only be used in compound statements",
36312 "omp target enter data");
36313 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36314 return NULL_TREE;
36317 tree clauses
36318 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36319 "#pragma omp target enter data", pragma_tok);
36320 int map_seen = 0;
36321 for (tree *pc = &clauses; *pc;)
36323 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36324 switch (OMP_CLAUSE_MAP_KIND (*pc))
36326 case GOMP_MAP_TO:
36327 case GOMP_MAP_ALWAYS_TO:
36328 case GOMP_MAP_ALLOC:
36329 map_seen = 3;
36330 break;
36331 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36332 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36333 case GOMP_MAP_ALWAYS_POINTER:
36334 break;
36335 default:
36336 map_seen |= 1;
36337 error_at (OMP_CLAUSE_LOCATION (*pc),
36338 "%<#pragma omp target enter data%> with map-type other "
36339 "than %<to%> or %<alloc%> on %<map%> clause");
36340 *pc = OMP_CLAUSE_CHAIN (*pc);
36341 continue;
36343 pc = &OMP_CLAUSE_CHAIN (*pc);
36346 if (map_seen != 3)
36348 if (map_seen == 0)
36349 error_at (pragma_tok->location,
36350 "%<#pragma omp target enter data%> must contain at least "
36351 "one %<map%> clause");
36352 return NULL_TREE;
36355 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36356 TREE_TYPE (stmt) = void_type_node;
36357 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36358 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36359 return add_stmt (stmt);
36362 /* OpenMP 4.5:
36363 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36364 structured-block */
36366 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36367 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36373 static tree
36374 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36375 enum pragma_context context)
36377 bool data_seen = false;
36378 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36380 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36381 const char *p = IDENTIFIER_POINTER (id);
36383 if (strcmp (p, "data") == 0)
36385 cp_lexer_consume_token (parser->lexer);
36386 data_seen = true;
36389 if (!data_seen)
36391 cp_parser_error (parser, "expected %<data%>");
36392 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36393 return NULL_TREE;
36396 if (context == pragma_stmt)
36398 error_at (pragma_tok->location,
36399 "%<#pragma %s%> may only be used in compound statements",
36400 "omp target exit data");
36401 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36402 return NULL_TREE;
36405 tree clauses
36406 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36407 "#pragma omp target exit data", pragma_tok);
36408 int map_seen = 0;
36409 for (tree *pc = &clauses; *pc;)
36411 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36412 switch (OMP_CLAUSE_MAP_KIND (*pc))
36414 case GOMP_MAP_FROM:
36415 case GOMP_MAP_ALWAYS_FROM:
36416 case GOMP_MAP_RELEASE:
36417 case GOMP_MAP_DELETE:
36418 map_seen = 3;
36419 break;
36420 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36421 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36422 case GOMP_MAP_ALWAYS_POINTER:
36423 break;
36424 default:
36425 map_seen |= 1;
36426 error_at (OMP_CLAUSE_LOCATION (*pc),
36427 "%<#pragma omp target exit data%> with map-type other "
36428 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36429 " clause");
36430 *pc = OMP_CLAUSE_CHAIN (*pc);
36431 continue;
36433 pc = &OMP_CLAUSE_CHAIN (*pc);
36436 if (map_seen != 3)
36438 if (map_seen == 0)
36439 error_at (pragma_tok->location,
36440 "%<#pragma omp target exit data%> must contain at least "
36441 "one %<map%> clause");
36442 return NULL_TREE;
36445 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36446 TREE_TYPE (stmt) = void_type_node;
36447 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36448 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36449 return add_stmt (stmt);
36452 /* OpenMP 4.0:
36453 # pragma omp target update target-update-clause[optseq] new-line */
36455 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36463 static bool
36464 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36465 enum pragma_context context)
36467 if (context == pragma_stmt)
36469 error_at (pragma_tok->location,
36470 "%<#pragma %s%> may only be used in compound statements",
36471 "omp target update");
36472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36473 return false;
36476 tree clauses
36477 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36478 "#pragma omp target update", pragma_tok);
36479 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36480 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36482 error_at (pragma_tok->location,
36483 "%<#pragma omp target update%> must contain at least one "
36484 "%<from%> or %<to%> clauses");
36485 return false;
36488 tree stmt = make_node (OMP_TARGET_UPDATE);
36489 TREE_TYPE (stmt) = void_type_node;
36490 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36491 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36492 add_stmt (stmt);
36493 return false;
36496 /* OpenMP 4.0:
36497 # pragma omp target target-clause[optseq] new-line
36498 structured-block */
36500 #define OMP_TARGET_CLAUSE_MASK \
36501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36511 static bool
36512 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36513 enum pragma_context context, bool *if_p)
36515 tree *pc = NULL, stmt;
36517 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36519 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36520 const char *p = IDENTIFIER_POINTER (id);
36521 enum tree_code ccode = ERROR_MARK;
36523 if (strcmp (p, "teams") == 0)
36524 ccode = OMP_TEAMS;
36525 else if (strcmp (p, "parallel") == 0)
36526 ccode = OMP_PARALLEL;
36527 else if (strcmp (p, "simd") == 0)
36528 ccode = OMP_SIMD;
36529 if (ccode != ERROR_MARK)
36531 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36532 char p_name[sizeof ("#pragma omp target teams distribute "
36533 "parallel for simd")];
36535 cp_lexer_consume_token (parser->lexer);
36536 strcpy (p_name, "#pragma omp target");
36537 if (!flag_openmp) /* flag_openmp_simd */
36539 tree stmt;
36540 switch (ccode)
36542 case OMP_TEAMS:
36543 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36544 OMP_TARGET_CLAUSE_MASK,
36545 cclauses, if_p);
36546 break;
36547 case OMP_PARALLEL:
36548 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36549 OMP_TARGET_CLAUSE_MASK,
36550 cclauses, if_p);
36551 break;
36552 case OMP_SIMD:
36553 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36554 OMP_TARGET_CLAUSE_MASK,
36555 cclauses, if_p);
36556 break;
36557 default:
36558 gcc_unreachable ();
36560 return stmt != NULL_TREE;
36562 keep_next_level (true);
36563 tree sb = begin_omp_structured_block (), ret;
36564 unsigned save = cp_parser_begin_omp_structured_block (parser);
36565 switch (ccode)
36567 case OMP_TEAMS:
36568 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36569 OMP_TARGET_CLAUSE_MASK, cclauses,
36570 if_p);
36571 break;
36572 case OMP_PARALLEL:
36573 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36574 OMP_TARGET_CLAUSE_MASK, cclauses,
36575 if_p);
36576 break;
36577 case OMP_SIMD:
36578 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36579 OMP_TARGET_CLAUSE_MASK, cclauses,
36580 if_p);
36581 break;
36582 default:
36583 gcc_unreachable ();
36585 cp_parser_end_omp_structured_block (parser, save);
36586 tree body = finish_omp_structured_block (sb);
36587 if (ret == NULL_TREE)
36588 return false;
36589 if (ccode == OMP_TEAMS && !processing_template_decl)
36591 /* For combined target teams, ensure the num_teams and
36592 thread_limit clause expressions are evaluated on the host,
36593 before entering the target construct. */
36594 tree c;
36595 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36596 c; c = OMP_CLAUSE_CHAIN (c))
36597 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36598 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36599 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36601 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36602 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36603 if (expr == error_mark_node)
36604 continue;
36605 tree tmp = TARGET_EXPR_SLOT (expr);
36606 add_stmt (expr);
36607 OMP_CLAUSE_OPERAND (c, 0) = expr;
36608 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36609 OMP_CLAUSE_FIRSTPRIVATE);
36610 OMP_CLAUSE_DECL (tc) = tmp;
36611 OMP_CLAUSE_CHAIN (tc)
36612 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36613 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36616 tree stmt = make_node (OMP_TARGET);
36617 TREE_TYPE (stmt) = void_type_node;
36618 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36619 OMP_TARGET_BODY (stmt) = body;
36620 OMP_TARGET_COMBINED (stmt) = 1;
36621 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36622 add_stmt (stmt);
36623 pc = &OMP_TARGET_CLAUSES (stmt);
36624 goto check_clauses;
36626 else if (!flag_openmp) /* flag_openmp_simd */
36628 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36629 return false;
36631 else if (strcmp (p, "data") == 0)
36633 cp_lexer_consume_token (parser->lexer);
36634 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36635 return true;
36637 else if (strcmp (p, "enter") == 0)
36639 cp_lexer_consume_token (parser->lexer);
36640 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36641 return false;
36643 else if (strcmp (p, "exit") == 0)
36645 cp_lexer_consume_token (parser->lexer);
36646 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36647 return false;
36649 else if (strcmp (p, "update") == 0)
36651 cp_lexer_consume_token (parser->lexer);
36652 return cp_parser_omp_target_update (parser, pragma_tok, context);
36655 if (!flag_openmp) /* flag_openmp_simd */
36657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36658 return false;
36661 stmt = make_node (OMP_TARGET);
36662 TREE_TYPE (stmt) = void_type_node;
36664 OMP_TARGET_CLAUSES (stmt)
36665 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36666 "#pragma omp target", pragma_tok);
36667 pc = &OMP_TARGET_CLAUSES (stmt);
36668 keep_next_level (true);
36669 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36671 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36672 add_stmt (stmt);
36674 check_clauses:
36675 while (*pc)
36677 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36678 switch (OMP_CLAUSE_MAP_KIND (*pc))
36680 case GOMP_MAP_TO:
36681 case GOMP_MAP_ALWAYS_TO:
36682 case GOMP_MAP_FROM:
36683 case GOMP_MAP_ALWAYS_FROM:
36684 case GOMP_MAP_TOFROM:
36685 case GOMP_MAP_ALWAYS_TOFROM:
36686 case GOMP_MAP_ALLOC:
36687 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36688 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36689 case GOMP_MAP_ALWAYS_POINTER:
36690 break;
36691 default:
36692 error_at (OMP_CLAUSE_LOCATION (*pc),
36693 "%<#pragma omp target%> with map-type other "
36694 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36695 "on %<map%> clause");
36696 *pc = OMP_CLAUSE_CHAIN (*pc);
36697 continue;
36699 pc = &OMP_CLAUSE_CHAIN (*pc);
36701 return true;
36704 /* OpenACC 2.0:
36705 # pragma acc cache (variable-list) new-line
36708 static tree
36709 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36711 tree stmt, clauses;
36713 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36714 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36716 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36718 stmt = make_node (OACC_CACHE);
36719 TREE_TYPE (stmt) = void_type_node;
36720 OACC_CACHE_CLAUSES (stmt) = clauses;
36721 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36722 add_stmt (stmt);
36724 return stmt;
36727 /* OpenACC 2.0:
36728 # pragma acc data oacc-data-clause[optseq] new-line
36729 structured-block */
36731 #define OACC_DATA_CLAUSE_MASK \
36732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36744 static tree
36745 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36747 tree stmt, clauses, block;
36748 unsigned int save;
36750 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36751 "#pragma acc data", pragma_tok);
36753 block = begin_omp_parallel ();
36754 save = cp_parser_begin_omp_structured_block (parser);
36755 cp_parser_statement (parser, NULL_TREE, false, if_p);
36756 cp_parser_end_omp_structured_block (parser, save);
36757 stmt = finish_oacc_data (clauses, block);
36758 return stmt;
36761 /* OpenACC 2.0:
36762 # pragma acc host_data <clauses> new-line
36763 structured-block */
36765 #define OACC_HOST_DATA_CLAUSE_MASK \
36766 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36768 static tree
36769 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36771 tree stmt, clauses, block;
36772 unsigned int save;
36774 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36775 "#pragma acc host_data", pragma_tok);
36777 block = begin_omp_parallel ();
36778 save = cp_parser_begin_omp_structured_block (parser);
36779 cp_parser_statement (parser, NULL_TREE, false, if_p);
36780 cp_parser_end_omp_structured_block (parser, save);
36781 stmt = finish_oacc_host_data (clauses, block);
36782 return stmt;
36785 /* OpenACC 2.0:
36786 # pragma acc declare oacc-data-clause[optseq] new-line
36789 #define OACC_DECLARE_CLAUSE_MASK \
36790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36803 static tree
36804 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36806 tree clauses, stmt;
36807 bool error = false;
36809 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36810 "#pragma acc declare", pragma_tok, true);
36813 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36815 error_at (pragma_tok->location,
36816 "no valid clauses specified in %<#pragma acc declare%>");
36817 return NULL_TREE;
36820 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36822 location_t loc = OMP_CLAUSE_LOCATION (t);
36823 tree decl = OMP_CLAUSE_DECL (t);
36824 if (!DECL_P (decl))
36826 error_at (loc, "array section in %<#pragma acc declare%>");
36827 error = true;
36828 continue;
36830 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36831 switch (OMP_CLAUSE_MAP_KIND (t))
36833 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36834 case GOMP_MAP_FORCE_ALLOC:
36835 case GOMP_MAP_FORCE_TO:
36836 case GOMP_MAP_FORCE_DEVICEPTR:
36837 case GOMP_MAP_DEVICE_RESIDENT:
36838 break;
36840 case GOMP_MAP_LINK:
36841 if (!global_bindings_p ()
36842 && (TREE_STATIC (decl)
36843 || !DECL_EXTERNAL (decl)))
36845 error_at (loc,
36846 "%qD must be a global variable in "
36847 "%<#pragma acc declare link%>",
36848 decl);
36849 error = true;
36850 continue;
36852 break;
36854 default:
36855 if (global_bindings_p ())
36857 error_at (loc, "invalid OpenACC clause at file scope");
36858 error = true;
36859 continue;
36861 if (DECL_EXTERNAL (decl))
36863 error_at (loc,
36864 "invalid use of %<extern%> variable %qD "
36865 "in %<#pragma acc declare%>", decl);
36866 error = true;
36867 continue;
36869 else if (TREE_PUBLIC (decl))
36871 error_at (loc,
36872 "invalid use of %<global%> variable %qD "
36873 "in %<#pragma acc declare%>", decl);
36874 error = true;
36875 continue;
36877 break;
36880 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36881 || lookup_attribute ("omp declare target link",
36882 DECL_ATTRIBUTES (decl)))
36884 error_at (loc, "variable %qD used more than once with "
36885 "%<#pragma acc declare%>", decl);
36886 error = true;
36887 continue;
36890 if (!error)
36892 tree id;
36894 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36895 id = get_identifier ("omp declare target link");
36896 else
36897 id = get_identifier ("omp declare target");
36899 DECL_ATTRIBUTES (decl)
36900 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36901 if (global_bindings_p ())
36903 symtab_node *node = symtab_node::get (decl);
36904 if (node != NULL)
36906 node->offloadable = 1;
36907 if (ENABLE_OFFLOADING)
36909 g->have_offload = true;
36910 if (is_a <varpool_node *> (node))
36911 vec_safe_push (offload_vars, decl);
36918 if (error || global_bindings_p ())
36919 return NULL_TREE;
36921 stmt = make_node (OACC_DECLARE);
36922 TREE_TYPE (stmt) = void_type_node;
36923 OACC_DECLARE_CLAUSES (stmt) = clauses;
36924 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36926 add_stmt (stmt);
36928 return NULL_TREE;
36931 /* OpenACC 2.0:
36932 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36936 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36938 LOC is the location of the #pragma token.
36941 #define OACC_ENTER_DATA_CLAUSE_MASK \
36942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36950 #define OACC_EXIT_DATA_CLAUSE_MASK \
36951 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36957 static tree
36958 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36959 bool enter)
36961 location_t loc = pragma_tok->location;
36962 tree stmt, clauses;
36963 const char *p = "";
36965 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36966 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36968 if (strcmp (p, "data") != 0)
36970 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36971 enter ? "enter" : "exit");
36972 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36973 return NULL_TREE;
36976 cp_lexer_consume_token (parser->lexer);
36978 if (enter)
36979 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36980 "#pragma acc enter data", pragma_tok);
36981 else
36982 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36983 "#pragma acc exit data", pragma_tok);
36985 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36987 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36988 enter ? "enter" : "exit");
36989 return NULL_TREE;
36992 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36993 TREE_TYPE (stmt) = void_type_node;
36994 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36995 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36996 add_stmt (stmt);
36997 return stmt;
37000 /* OpenACC 2.0:
37001 # pragma acc loop oacc-loop-clause[optseq] new-line
37002 structured-block */
37004 #define OACC_LOOP_CLAUSE_MASK \
37005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37016 static tree
37017 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37018 omp_clause_mask mask, tree *cclauses, bool *if_p)
37020 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37022 strcat (p_name, " loop");
37023 mask |= OACC_LOOP_CLAUSE_MASK;
37025 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37026 cclauses == NULL);
37027 if (cclauses)
37029 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37030 if (*cclauses)
37031 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37032 if (clauses)
37033 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37036 tree block = begin_omp_structured_block ();
37037 int save = cp_parser_begin_omp_structured_block (parser);
37038 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37039 cp_parser_end_omp_structured_block (parser, save);
37040 add_stmt (finish_omp_structured_block (block));
37042 return stmt;
37045 /* OpenACC 2.0:
37046 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37047 structured-block
37051 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37052 structured-block
37055 #define OACC_KERNELS_CLAUSE_MASK \
37056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37074 #define OACC_PARALLEL_CLAUSE_MASK \
37075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37096 static tree
37097 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37098 char *p_name, bool *if_p)
37100 omp_clause_mask mask;
37101 enum tree_code code;
37102 switch (cp_parser_pragma_kind (pragma_tok))
37104 case PRAGMA_OACC_KERNELS:
37105 strcat (p_name, " kernels");
37106 mask = OACC_KERNELS_CLAUSE_MASK;
37107 code = OACC_KERNELS;
37108 break;
37109 case PRAGMA_OACC_PARALLEL:
37110 strcat (p_name, " parallel");
37111 mask = OACC_PARALLEL_CLAUSE_MASK;
37112 code = OACC_PARALLEL;
37113 break;
37114 default:
37115 gcc_unreachable ();
37118 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37120 const char *p
37121 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37122 if (strcmp (p, "loop") == 0)
37124 cp_lexer_consume_token (parser->lexer);
37125 tree block = begin_omp_parallel ();
37126 tree clauses;
37127 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37128 if_p);
37129 return finish_omp_construct (code, block, clauses);
37133 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37135 tree block = begin_omp_parallel ();
37136 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37137 cp_parser_statement (parser, NULL_TREE, false, if_p);
37138 cp_parser_end_omp_structured_block (parser, save);
37139 return finish_omp_construct (code, block, clauses);
37142 /* OpenACC 2.0:
37143 # pragma acc update oacc-update-clause[optseq] new-line
37146 #define OACC_UPDATE_CLAUSE_MASK \
37147 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37154 static tree
37155 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37157 tree stmt, clauses;
37159 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37160 "#pragma acc update", pragma_tok);
37162 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37164 error_at (pragma_tok->location,
37165 "%<#pragma acc update%> must contain at least one "
37166 "%<device%> or %<host%> or %<self%> clause");
37167 return NULL_TREE;
37170 stmt = make_node (OACC_UPDATE);
37171 TREE_TYPE (stmt) = void_type_node;
37172 OACC_UPDATE_CLAUSES (stmt) = clauses;
37173 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37174 add_stmt (stmt);
37175 return stmt;
37178 /* OpenACC 2.0:
37179 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37181 LOC is the location of the #pragma token.
37184 #define OACC_WAIT_CLAUSE_MASK \
37185 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37187 static tree
37188 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37190 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37193 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37194 list = cp_parser_oacc_wait_list (parser, loc, list);
37196 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37197 "#pragma acc wait", pragma_tok);
37199 stmt = c_finish_oacc_wait (loc, list, clauses);
37200 stmt = finish_expr_stmt (stmt);
37202 return stmt;
37205 /* OpenMP 4.0:
37206 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37208 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37209 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37216 static void
37217 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37218 enum pragma_context context)
37220 bool first_p = parser->omp_declare_simd == NULL;
37221 cp_omp_declare_simd_data data;
37222 if (first_p)
37224 data.error_seen = false;
37225 data.fndecl_seen = false;
37226 data.tokens = vNULL;
37227 data.clauses = NULL_TREE;
37228 /* It is safe to take the address of a local variable; it will only be
37229 used while this scope is live. */
37230 parser->omp_declare_simd = &data;
37233 /* Store away all pragma tokens. */
37234 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37235 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37236 cp_lexer_consume_token (parser->lexer);
37237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37238 parser->omp_declare_simd->error_seen = true;
37239 cp_parser_require_pragma_eol (parser, pragma_tok);
37240 struct cp_token_cache *cp
37241 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37242 parser->omp_declare_simd->tokens.safe_push (cp);
37244 if (first_p)
37246 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37247 cp_parser_pragma (parser, context, NULL);
37248 switch (context)
37250 case pragma_external:
37251 cp_parser_declaration (parser);
37252 break;
37253 case pragma_member:
37254 cp_parser_member_declaration (parser);
37255 break;
37256 case pragma_objc_icode:
37257 cp_parser_block_declaration (parser, /*statement_p=*/false);
37258 break;
37259 default:
37260 cp_parser_declaration_statement (parser);
37261 break;
37263 if (parser->omp_declare_simd
37264 && !parser->omp_declare_simd->error_seen
37265 && !parser->omp_declare_simd->fndecl_seen)
37266 error_at (pragma_tok->location,
37267 "%<#pragma omp declare simd%> not immediately followed by "
37268 "function declaration or definition");
37269 data.tokens.release ();
37270 parser->omp_declare_simd = NULL;
37274 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
37275 This function is modelled similar to the late parsing of omp declare
37276 simd. */
37278 static tree
37279 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
37281 struct cp_token_cache *ce;
37282 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
37283 int ii = 0;
37285 if (parser->omp_declare_simd != NULL
37286 || lookup_attribute ("simd", attrs))
37288 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
37289 "used in the same function marked as a Cilk Plus SIMD-enabled "
37290 "function");
37291 parser->cilk_simd_fn_info->tokens.release ();
37292 XDELETE (parser->cilk_simd_fn_info);
37293 parser->cilk_simd_fn_info = NULL;
37294 return attrs;
37296 if (!info->error_seen && info->fndecl_seen)
37298 error ("vector attribute not immediately followed by a single function"
37299 " declaration or definition");
37300 info->error_seen = true;
37302 if (info->error_seen)
37303 return attrs;
37305 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
37307 tree c, cl;
37309 cp_parser_push_lexer_for_tokens (parser, ce);
37310 parser->lexer->in_pragma = true;
37311 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
37312 "SIMD-enabled functions attribute",
37313 NULL);
37314 cp_parser_pop_lexer (parser);
37315 if (cl)
37316 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37318 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
37319 TREE_CHAIN (c) = attrs;
37320 attrs = c;
37322 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37323 TREE_CHAIN (c) = attrs;
37324 if (processing_template_decl)
37325 ATTR_IS_DEPENDENT (c) = 1;
37326 attrs = c;
37328 info->fndecl_seen = true;
37329 parser->cilk_simd_fn_info->tokens.release ();
37330 XDELETE (parser->cilk_simd_fn_info);
37331 parser->cilk_simd_fn_info = NULL;
37332 return attrs;
37335 /* Finalize #pragma omp declare simd clauses after direct declarator has
37336 been parsed, and put that into "omp declare simd" attribute. */
37338 static tree
37339 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37341 struct cp_token_cache *ce;
37342 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37343 int i;
37345 if (!data->error_seen && data->fndecl_seen)
37347 error ("%<#pragma omp declare simd%> not immediately followed by "
37348 "a single function declaration or definition");
37349 data->error_seen = true;
37351 if (data->error_seen)
37352 return attrs;
37354 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37356 tree c, cl;
37358 cp_parser_push_lexer_for_tokens (parser, ce);
37359 parser->lexer->in_pragma = true;
37360 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37361 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37362 cp_lexer_consume_token (parser->lexer);
37363 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37364 "#pragma omp declare simd", pragma_tok);
37365 cp_parser_pop_lexer (parser);
37366 if (cl)
37367 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37368 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37369 TREE_CHAIN (c) = attrs;
37370 if (processing_template_decl)
37371 ATTR_IS_DEPENDENT (c) = 1;
37372 attrs = c;
37375 data->fndecl_seen = true;
37376 return attrs;
37380 /* OpenMP 4.0:
37381 # pragma omp declare target new-line
37382 declarations and definitions
37383 # pragma omp end declare target new-line
37385 OpenMP 4.5:
37386 # pragma omp declare target ( extended-list ) new-line
37388 # pragma omp declare target declare-target-clauses[seq] new-line */
37390 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37394 static void
37395 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37397 tree clauses = NULL_TREE;
37398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37399 clauses
37400 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37401 "#pragma omp declare target", pragma_tok);
37402 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37404 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37405 clauses);
37406 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37407 cp_parser_require_pragma_eol (parser, pragma_tok);
37409 else
37411 cp_parser_require_pragma_eol (parser, pragma_tok);
37412 scope_chain->omp_declare_target_attribute++;
37413 return;
37415 if (scope_chain->omp_declare_target_attribute)
37416 error_at (pragma_tok->location,
37417 "%<#pragma omp declare target%> with clauses in between "
37418 "%<#pragma omp declare target%> without clauses and "
37419 "%<#pragma omp end declare target%>");
37420 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37422 tree t = OMP_CLAUSE_DECL (c), id;
37423 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37424 tree at2 = lookup_attribute ("omp declare target link",
37425 DECL_ATTRIBUTES (t));
37426 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37428 id = get_identifier ("omp declare target link");
37429 std::swap (at1, at2);
37431 else
37432 id = get_identifier ("omp declare target");
37433 if (at2)
37435 error_at (OMP_CLAUSE_LOCATION (c),
37436 "%qD specified both in declare target %<link%> and %<to%>"
37437 " clauses", t);
37438 continue;
37440 if (!at1)
37442 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37443 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37444 continue;
37446 symtab_node *node = symtab_node::get (t);
37447 if (node != NULL)
37449 node->offloadable = 1;
37450 if (ENABLE_OFFLOADING)
37452 g->have_offload = true;
37453 if (is_a <varpool_node *> (node))
37454 vec_safe_push (offload_vars, t);
37461 static void
37462 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37464 const char *p = "";
37465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37467 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37468 p = IDENTIFIER_POINTER (id);
37470 if (strcmp (p, "declare") == 0)
37472 cp_lexer_consume_token (parser->lexer);
37473 p = "";
37474 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37476 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37477 p = IDENTIFIER_POINTER (id);
37479 if (strcmp (p, "target") == 0)
37480 cp_lexer_consume_token (parser->lexer);
37481 else
37483 cp_parser_error (parser, "expected %<target%>");
37484 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37485 return;
37488 else
37490 cp_parser_error (parser, "expected %<declare%>");
37491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37492 return;
37494 cp_parser_require_pragma_eol (parser, pragma_tok);
37495 if (!scope_chain->omp_declare_target_attribute)
37496 error_at (pragma_tok->location,
37497 "%<#pragma omp end declare target%> without corresponding "
37498 "%<#pragma omp declare target%>");
37499 else
37500 scope_chain->omp_declare_target_attribute--;
37503 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37504 expression and optional initializer clause of
37505 #pragma omp declare reduction. We store the expression(s) as
37506 either 3, 6 or 7 special statements inside of the artificial function's
37507 body. The first two statements are DECL_EXPRs for the artificial
37508 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37509 expression that uses those variables.
37510 If there was any INITIALIZER clause, this is followed by further statements,
37511 the fourth and fifth statements are DECL_EXPRs for the artificial
37512 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37513 constructor variant (first token after open paren is not omp_priv),
37514 then the sixth statement is a statement with the function call expression
37515 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37516 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37517 to initialize the OMP_PRIV artificial variable and there is seventh
37518 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37520 static bool
37521 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37523 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37524 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37525 type = TREE_TYPE (type);
37526 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37527 DECL_ARTIFICIAL (omp_out) = 1;
37528 pushdecl (omp_out);
37529 add_decl_expr (omp_out);
37530 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37531 DECL_ARTIFICIAL (omp_in) = 1;
37532 pushdecl (omp_in);
37533 add_decl_expr (omp_in);
37534 tree combiner;
37535 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37537 keep_next_level (true);
37538 tree block = begin_omp_structured_block ();
37539 combiner = cp_parser_expression (parser);
37540 finish_expr_stmt (combiner);
37541 block = finish_omp_structured_block (block);
37542 add_stmt (block);
37544 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37545 return false;
37547 const char *p = "";
37548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37551 p = IDENTIFIER_POINTER (id);
37554 if (strcmp (p, "initializer") == 0)
37556 cp_lexer_consume_token (parser->lexer);
37557 matching_parens parens;
37558 if (!parens.require_open (parser))
37559 return false;
37561 p = "";
37562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37564 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37565 p = IDENTIFIER_POINTER (id);
37568 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37569 DECL_ARTIFICIAL (omp_priv) = 1;
37570 pushdecl (omp_priv);
37571 add_decl_expr (omp_priv);
37572 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37573 DECL_ARTIFICIAL (omp_orig) = 1;
37574 pushdecl (omp_orig);
37575 add_decl_expr (omp_orig);
37577 keep_next_level (true);
37578 block = begin_omp_structured_block ();
37580 bool ctor = false;
37581 if (strcmp (p, "omp_priv") == 0)
37583 bool is_direct_init, is_non_constant_init;
37584 ctor = true;
37585 cp_lexer_consume_token (parser->lexer);
37586 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37587 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37588 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37589 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37590 == CPP_CLOSE_PAREN
37591 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37592 == CPP_CLOSE_PAREN))
37594 finish_omp_structured_block (block);
37595 error ("invalid initializer clause");
37596 return false;
37598 initializer = cp_parser_initializer (parser, &is_direct_init,
37599 &is_non_constant_init);
37600 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37601 NULL_TREE, LOOKUP_ONLYCONVERTING);
37603 else
37605 cp_parser_parse_tentatively (parser);
37606 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37607 /*check_dependency_p=*/true,
37608 /*template_p=*/NULL,
37609 /*declarator_p=*/false,
37610 /*optional_p=*/false);
37611 vec<tree, va_gc> *args;
37612 if (fn_name == error_mark_node
37613 || cp_parser_error_occurred (parser)
37614 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37615 || ((args = cp_parser_parenthesized_expression_list
37616 (parser, non_attr, /*cast_p=*/false,
37617 /*allow_expansion_p=*/true,
37618 /*non_constant_p=*/NULL)),
37619 cp_parser_error_occurred (parser)))
37621 finish_omp_structured_block (block);
37622 cp_parser_abort_tentative_parse (parser);
37623 cp_parser_error (parser, "expected id-expression (arguments)");
37624 return false;
37626 unsigned int i;
37627 tree arg;
37628 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37629 if (arg == omp_priv
37630 || (TREE_CODE (arg) == ADDR_EXPR
37631 && TREE_OPERAND (arg, 0) == omp_priv))
37632 break;
37633 cp_parser_abort_tentative_parse (parser);
37634 if (arg == NULL_TREE)
37635 error ("one of the initializer call arguments should be %<omp_priv%>"
37636 " or %<&omp_priv%>");
37637 initializer = cp_parser_postfix_expression (parser, false, false, false,
37638 false, NULL);
37639 finish_expr_stmt (initializer);
37642 block = finish_omp_structured_block (block);
37643 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37644 add_stmt (block);
37646 if (ctor)
37647 add_decl_expr (omp_orig);
37649 if (!parens.require_close (parser))
37650 return false;
37653 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37654 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37655 UNKNOWN_LOCATION);
37657 return true;
37660 /* OpenMP 4.0
37661 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37662 initializer-clause[opt] new-line
37664 initializer-clause:
37665 initializer (omp_priv initializer)
37666 initializer (function-name (argument-list)) */
37668 static void
37669 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37670 enum pragma_context)
37672 auto_vec<tree> types;
37673 enum tree_code reduc_code = ERROR_MARK;
37674 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37675 unsigned int i;
37676 cp_token *first_token;
37677 cp_token_cache *cp;
37678 int errs;
37679 void *p;
37681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37682 p = obstack_alloc (&declarator_obstack, 0);
37684 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37685 goto fail;
37687 switch (cp_lexer_peek_token (parser->lexer)->type)
37689 case CPP_PLUS:
37690 reduc_code = PLUS_EXPR;
37691 break;
37692 case CPP_MULT:
37693 reduc_code = MULT_EXPR;
37694 break;
37695 case CPP_MINUS:
37696 reduc_code = MINUS_EXPR;
37697 break;
37698 case CPP_AND:
37699 reduc_code = BIT_AND_EXPR;
37700 break;
37701 case CPP_XOR:
37702 reduc_code = BIT_XOR_EXPR;
37703 break;
37704 case CPP_OR:
37705 reduc_code = BIT_IOR_EXPR;
37706 break;
37707 case CPP_AND_AND:
37708 reduc_code = TRUTH_ANDIF_EXPR;
37709 break;
37710 case CPP_OR_OR:
37711 reduc_code = TRUTH_ORIF_EXPR;
37712 break;
37713 case CPP_NAME:
37714 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37715 break;
37716 default:
37717 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37718 "%<|%>, %<&&%>, %<||%> or identifier");
37719 goto fail;
37722 if (reduc_code != ERROR_MARK)
37723 cp_lexer_consume_token (parser->lexer);
37725 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37726 if (reduc_id == error_mark_node)
37727 goto fail;
37729 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37730 goto fail;
37732 /* Types may not be defined in declare reduction type list. */
37733 const char *saved_message;
37734 saved_message = parser->type_definition_forbidden_message;
37735 parser->type_definition_forbidden_message
37736 = G_("types may not be defined in declare reduction type list");
37737 bool saved_colon_corrects_to_scope_p;
37738 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37739 parser->colon_corrects_to_scope_p = false;
37740 bool saved_colon_doesnt_start_class_def_p;
37741 saved_colon_doesnt_start_class_def_p
37742 = parser->colon_doesnt_start_class_def_p;
37743 parser->colon_doesnt_start_class_def_p = true;
37745 while (true)
37747 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37748 type = cp_parser_type_id (parser);
37749 if (type == error_mark_node)
37751 else if (ARITHMETIC_TYPE_P (type)
37752 && (orig_reduc_id == NULL_TREE
37753 || (TREE_CODE (type) != COMPLEX_TYPE
37754 && (id_equal (orig_reduc_id, "min")
37755 || id_equal (orig_reduc_id, "max")))))
37756 error_at (loc, "predeclared arithmetic type %qT in "
37757 "%<#pragma omp declare reduction%>", type);
37758 else if (TREE_CODE (type) == FUNCTION_TYPE
37759 || TREE_CODE (type) == METHOD_TYPE
37760 || TREE_CODE (type) == ARRAY_TYPE)
37761 error_at (loc, "function or array type %qT in "
37762 "%<#pragma omp declare reduction%>", type);
37763 else if (TREE_CODE (type) == REFERENCE_TYPE)
37764 error_at (loc, "reference type %qT in "
37765 "%<#pragma omp declare reduction%>", type);
37766 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37767 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37768 "%<#pragma omp declare reduction%>", type);
37769 else
37770 types.safe_push (type);
37772 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37773 cp_lexer_consume_token (parser->lexer);
37774 else
37775 break;
37778 /* Restore the saved message. */
37779 parser->type_definition_forbidden_message = saved_message;
37780 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37781 parser->colon_doesnt_start_class_def_p
37782 = saved_colon_doesnt_start_class_def_p;
37784 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37785 || types.is_empty ())
37787 fail:
37788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37789 goto done;
37792 first_token = cp_lexer_peek_token (parser->lexer);
37793 cp = NULL;
37794 errs = errorcount;
37795 FOR_EACH_VEC_ELT (types, i, type)
37797 tree fntype
37798 = build_function_type_list (void_type_node,
37799 cp_build_reference_type (type, false),
37800 NULL_TREE);
37801 tree this_reduc_id = reduc_id;
37802 if (!dependent_type_p (type))
37803 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37804 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37805 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37806 DECL_ARTIFICIAL (fndecl) = 1;
37807 DECL_EXTERNAL (fndecl) = 1;
37808 DECL_DECLARED_INLINE_P (fndecl) = 1;
37809 DECL_IGNORED_P (fndecl) = 1;
37810 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37811 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37812 DECL_ATTRIBUTES (fndecl)
37813 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37814 DECL_ATTRIBUTES (fndecl));
37815 if (processing_template_decl)
37816 fndecl = push_template_decl (fndecl);
37817 bool block_scope = false;
37818 tree block = NULL_TREE;
37819 if (current_function_decl)
37821 block_scope = true;
37822 DECL_CONTEXT (fndecl) = global_namespace;
37823 if (!processing_template_decl)
37824 pushdecl (fndecl);
37826 else if (current_class_type)
37828 if (cp == NULL)
37830 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37831 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37832 cp_lexer_consume_token (parser->lexer);
37833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37834 goto fail;
37835 cp = cp_token_cache_new (first_token,
37836 cp_lexer_peek_nth_token (parser->lexer,
37837 2));
37839 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37840 finish_member_declaration (fndecl);
37841 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37842 DECL_PENDING_INLINE_P (fndecl) = 1;
37843 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37844 continue;
37846 else
37848 DECL_CONTEXT (fndecl) = current_namespace;
37849 pushdecl (fndecl);
37851 if (!block_scope)
37852 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37853 else
37854 block = begin_omp_structured_block ();
37855 if (cp)
37857 cp_parser_push_lexer_for_tokens (parser, cp);
37858 parser->lexer->in_pragma = true;
37860 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37862 if (!block_scope)
37863 finish_function (/*inline_p=*/false);
37864 else
37865 DECL_CONTEXT (fndecl) = current_function_decl;
37866 if (cp)
37867 cp_parser_pop_lexer (parser);
37868 goto fail;
37870 if (cp)
37871 cp_parser_pop_lexer (parser);
37872 if (!block_scope)
37873 finish_function (/*inline_p=*/false);
37874 else
37876 DECL_CONTEXT (fndecl) = current_function_decl;
37877 block = finish_omp_structured_block (block);
37878 if (TREE_CODE (block) == BIND_EXPR)
37879 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37880 else if (TREE_CODE (block) == STATEMENT_LIST)
37881 DECL_SAVED_TREE (fndecl) = block;
37882 if (processing_template_decl)
37883 add_decl_expr (fndecl);
37885 cp_check_omp_declare_reduction (fndecl);
37886 if (cp == NULL && types.length () > 1)
37887 cp = cp_token_cache_new (first_token,
37888 cp_lexer_peek_nth_token (parser->lexer, 2));
37889 if (errs != errorcount)
37890 break;
37893 cp_parser_require_pragma_eol (parser, pragma_tok);
37895 done:
37896 /* Free any declarators allocated. */
37897 obstack_free (&declarator_obstack, p);
37900 /* OpenMP 4.0
37901 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37902 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37903 initializer-clause[opt] new-line
37904 #pragma omp declare target new-line */
37906 static void
37907 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37908 enum pragma_context context)
37910 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37912 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37913 const char *p = IDENTIFIER_POINTER (id);
37915 if (strcmp (p, "simd") == 0)
37917 cp_lexer_consume_token (parser->lexer);
37918 cp_parser_omp_declare_simd (parser, pragma_tok,
37919 context);
37920 return;
37922 cp_ensure_no_omp_declare_simd (parser);
37923 if (strcmp (p, "reduction") == 0)
37925 cp_lexer_consume_token (parser->lexer);
37926 cp_parser_omp_declare_reduction (parser, pragma_tok,
37927 context);
37928 return;
37930 if (!flag_openmp) /* flag_openmp_simd */
37932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37933 return;
37935 if (strcmp (p, "target") == 0)
37937 cp_lexer_consume_token (parser->lexer);
37938 cp_parser_omp_declare_target (parser, pragma_tok);
37939 return;
37942 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37943 "or %<target%>");
37944 cp_parser_require_pragma_eol (parser, pragma_tok);
37947 /* OpenMP 4.5:
37948 #pragma omp taskloop taskloop-clause[optseq] new-line
37949 for-loop
37951 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37952 for-loop */
37954 #define OMP_TASKLOOP_CLAUSE_MASK \
37955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37970 static tree
37971 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37972 char *p_name, omp_clause_mask mask, tree *cclauses,
37973 bool *if_p)
37975 tree clauses, sb, ret;
37976 unsigned int save;
37977 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37979 strcat (p_name, " taskloop");
37980 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37985 const char *p = IDENTIFIER_POINTER (id);
37987 if (strcmp (p, "simd") == 0)
37989 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37990 if (cclauses == NULL)
37991 cclauses = cclauses_buf;
37993 cp_lexer_consume_token (parser->lexer);
37994 if (!flag_openmp) /* flag_openmp_simd */
37995 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37996 cclauses, if_p);
37997 sb = begin_omp_structured_block ();
37998 save = cp_parser_begin_omp_structured_block (parser);
37999 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38000 cclauses, if_p);
38001 cp_parser_end_omp_structured_block (parser, save);
38002 tree body = finish_omp_structured_block (sb);
38003 if (ret == NULL)
38004 return ret;
38005 ret = make_node (OMP_TASKLOOP);
38006 TREE_TYPE (ret) = void_type_node;
38007 OMP_FOR_BODY (ret) = body;
38008 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38009 SET_EXPR_LOCATION (ret, loc);
38010 add_stmt (ret);
38011 return ret;
38014 if (!flag_openmp) /* flag_openmp_simd */
38016 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38017 return NULL_TREE;
38020 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38021 cclauses == NULL);
38022 if (cclauses)
38024 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38025 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38028 sb = begin_omp_structured_block ();
38029 save = cp_parser_begin_omp_structured_block (parser);
38031 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38032 if_p);
38034 cp_parser_end_omp_structured_block (parser, save);
38035 add_stmt (finish_omp_structured_block (sb));
38037 return ret;
38041 /* OpenACC 2.0:
38042 # pragma acc routine oacc-routine-clause[optseq] new-line
38043 function-definition
38045 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38048 #define OACC_ROUTINE_CLAUSE_MASK \
38049 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38055 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38056 component, which must resolve to a declared namespace-scope
38057 function. The clauses are either processed directly (for a named
38058 function), or defered until the immediatley following declaration
38059 is parsed. */
38061 static void
38062 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38063 enum pragma_context context)
38065 gcc_checking_assert (context == pragma_external);
38066 /* The checking for "another pragma following this one" in the "no optional
38067 '( name )'" case makes sure that we dont re-enter. */
38068 gcc_checking_assert (parser->oacc_routine == NULL);
38070 cp_oacc_routine_data data;
38071 data.error_seen = false;
38072 data.fndecl_seen = false;
38073 data.tokens = vNULL;
38074 data.clauses = NULL_TREE;
38075 data.loc = pragma_tok->location;
38076 /* It is safe to take the address of a local variable; it will only be
38077 used while this scope is live. */
38078 parser->oacc_routine = &data;
38080 /* Look for optional '( name )'. */
38081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38083 matching_parens parens;
38084 parens.consume_open (parser); /* '(' */
38086 /* We parse the name as an id-expression. If it resolves to
38087 anything other than a non-overloaded function at namespace
38088 scope, it's an error. */
38089 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38090 tree name = cp_parser_id_expression (parser,
38091 /*template_keyword_p=*/false,
38092 /*check_dependency_p=*/false,
38093 /*template_p=*/NULL,
38094 /*declarator_p=*/false,
38095 /*optional_p=*/false);
38096 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
38097 if (name != error_mark_node && decl == error_mark_node)
38098 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38100 if (decl == error_mark_node
38101 || !parens.require_close (parser))
38103 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38104 parser->oacc_routine = NULL;
38105 return;
38108 data.clauses
38109 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38110 "#pragma acc routine",
38111 cp_lexer_peek_token (parser->lexer));
38113 if (decl && is_overloaded_fn (decl)
38114 && (TREE_CODE (decl) != FUNCTION_DECL
38115 || DECL_FUNCTION_TEMPLATE_P (decl)))
38117 error_at (name_loc,
38118 "%<#pragma acc routine%> names a set of overloads");
38119 parser->oacc_routine = NULL;
38120 return;
38123 /* Perhaps we should use the same rule as declarations in different
38124 namespaces? */
38125 if (!DECL_NAMESPACE_SCOPE_P (decl))
38127 error_at (name_loc,
38128 "%qD does not refer to a namespace scope function", decl);
38129 parser->oacc_routine = NULL;
38130 return;
38133 if (TREE_CODE (decl) != FUNCTION_DECL)
38135 error_at (name_loc, "%qD does not refer to a function", decl);
38136 parser->oacc_routine = NULL;
38137 return;
38140 cp_finalize_oacc_routine (parser, decl, false);
38141 parser->oacc_routine = NULL;
38143 else /* No optional '( name )'. */
38145 /* Store away all pragma tokens. */
38146 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38147 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38148 cp_lexer_consume_token (parser->lexer);
38149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38150 parser->oacc_routine->error_seen = true;
38151 cp_parser_require_pragma_eol (parser, pragma_tok);
38152 struct cp_token_cache *cp
38153 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38154 parser->oacc_routine->tokens.safe_push (cp);
38156 /* Emit a helpful diagnostic if there's another pragma following this
38157 one. */
38158 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38160 cp_ensure_no_oacc_routine (parser);
38161 data.tokens.release ();
38162 /* ..., and then just keep going. */
38163 return;
38166 /* We only have to consider the pragma_external case here. */
38167 cp_parser_declaration (parser);
38168 if (parser->oacc_routine
38169 && !parser->oacc_routine->fndecl_seen)
38170 cp_ensure_no_oacc_routine (parser);
38171 else
38172 parser->oacc_routine = NULL;
38173 data.tokens.release ();
38177 /* Finalize #pragma acc routine clauses after direct declarator has
38178 been parsed. */
38180 static tree
38181 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38183 struct cp_token_cache *ce;
38184 cp_oacc_routine_data *data = parser->oacc_routine;
38186 if (!data->error_seen && data->fndecl_seen)
38188 error_at (data->loc,
38189 "%<#pragma acc routine%> not immediately followed by "
38190 "a single function declaration or definition");
38191 data->error_seen = true;
38193 if (data->error_seen)
38194 return attrs;
38196 gcc_checking_assert (data->tokens.length () == 1);
38197 ce = data->tokens[0];
38199 cp_parser_push_lexer_for_tokens (parser, ce);
38200 parser->lexer->in_pragma = true;
38201 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38203 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38204 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38205 parser->oacc_routine->clauses
38206 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38207 "#pragma acc routine", pragma_tok);
38208 cp_parser_pop_lexer (parser);
38209 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38210 fndecl_seen. */
38212 return attrs;
38215 /* Apply any saved OpenACC routine clauses to a just-parsed
38216 declaration. */
38218 static void
38219 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38221 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38223 /* Keep going if we're in error reporting mode. */
38224 if (parser->oacc_routine->error_seen
38225 || fndecl == error_mark_node)
38226 return;
38228 if (parser->oacc_routine->fndecl_seen)
38230 error_at (parser->oacc_routine->loc,
38231 "%<#pragma acc routine%> not immediately followed by"
38232 " a single function declaration or definition");
38233 parser->oacc_routine = NULL;
38234 return;
38236 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38238 cp_ensure_no_oacc_routine (parser);
38239 return;
38242 if (oacc_get_fn_attrib (fndecl))
38244 error_at (parser->oacc_routine->loc,
38245 "%<#pragma acc routine%> already applied to %qD", fndecl);
38246 parser->oacc_routine = NULL;
38247 return;
38250 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38252 error_at (parser->oacc_routine->loc,
38253 TREE_USED (fndecl)
38254 ? G_("%<#pragma acc routine%> must be applied before use")
38255 : G_("%<#pragma acc routine%> must be applied before "
38256 "definition"));
38257 parser->oacc_routine = NULL;
38258 return;
38261 /* Process the routine's dimension clauses. */
38262 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38263 oacc_replace_fn_attrib (fndecl, dims);
38265 /* Add an "omp declare target" attribute. */
38266 DECL_ATTRIBUTES (fndecl)
38267 = tree_cons (get_identifier ("omp declare target"),
38268 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38270 /* Don't unset parser->oacc_routine here: we may still need it to
38271 diagnose wrong usage. But, remember that we've used this "#pragma acc
38272 routine". */
38273 parser->oacc_routine->fndecl_seen = true;
38277 /* Main entry point to OpenMP statement pragmas. */
38279 static void
38280 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38282 tree stmt;
38283 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38284 omp_clause_mask mask (0);
38286 switch (cp_parser_pragma_kind (pragma_tok))
38288 case PRAGMA_OACC_ATOMIC:
38289 cp_parser_omp_atomic (parser, pragma_tok);
38290 return;
38291 case PRAGMA_OACC_CACHE:
38292 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38293 break;
38294 case PRAGMA_OACC_DATA:
38295 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38296 break;
38297 case PRAGMA_OACC_ENTER_DATA:
38298 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38299 break;
38300 case PRAGMA_OACC_EXIT_DATA:
38301 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38302 break;
38303 case PRAGMA_OACC_HOST_DATA:
38304 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38305 break;
38306 case PRAGMA_OACC_KERNELS:
38307 case PRAGMA_OACC_PARALLEL:
38308 strcpy (p_name, "#pragma acc");
38309 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38310 if_p);
38311 break;
38312 case PRAGMA_OACC_LOOP:
38313 strcpy (p_name, "#pragma acc");
38314 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38315 if_p);
38316 break;
38317 case PRAGMA_OACC_UPDATE:
38318 stmt = cp_parser_oacc_update (parser, pragma_tok);
38319 break;
38320 case PRAGMA_OACC_WAIT:
38321 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38322 break;
38323 case PRAGMA_OMP_ATOMIC:
38324 cp_parser_omp_atomic (parser, pragma_tok);
38325 return;
38326 case PRAGMA_OMP_CRITICAL:
38327 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38328 break;
38329 case PRAGMA_OMP_DISTRIBUTE:
38330 strcpy (p_name, "#pragma omp");
38331 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38332 if_p);
38333 break;
38334 case PRAGMA_OMP_FOR:
38335 strcpy (p_name, "#pragma omp");
38336 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38337 if_p);
38338 break;
38339 case PRAGMA_OMP_MASTER:
38340 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38341 break;
38342 case PRAGMA_OMP_PARALLEL:
38343 strcpy (p_name, "#pragma omp");
38344 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38345 if_p);
38346 break;
38347 case PRAGMA_OMP_SECTIONS:
38348 strcpy (p_name, "#pragma omp");
38349 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38350 break;
38351 case PRAGMA_OMP_SIMD:
38352 strcpy (p_name, "#pragma omp");
38353 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38354 if_p);
38355 break;
38356 case PRAGMA_OMP_SINGLE:
38357 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38358 break;
38359 case PRAGMA_OMP_TASK:
38360 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38361 break;
38362 case PRAGMA_OMP_TASKGROUP:
38363 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38364 break;
38365 case PRAGMA_OMP_TASKLOOP:
38366 strcpy (p_name, "#pragma omp");
38367 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38368 if_p);
38369 break;
38370 case PRAGMA_OMP_TEAMS:
38371 strcpy (p_name, "#pragma omp");
38372 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38373 if_p);
38374 break;
38375 default:
38376 gcc_unreachable ();
38379 protected_set_expr_location (stmt, pragma_tok->location);
38382 /* Transactional Memory parsing routines. */
38384 /* Parse a transaction attribute.
38386 txn-attribute:
38387 attribute
38388 [ [ identifier ] ]
38390 We use this instead of cp_parser_attributes_opt for transactions to avoid
38391 the pedwarn in C++98 mode. */
38393 static tree
38394 cp_parser_txn_attribute_opt (cp_parser *parser)
38396 cp_token *token;
38397 tree attr_name, attr = NULL;
38399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38400 return cp_parser_attributes_opt (parser);
38402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38403 return NULL_TREE;
38404 cp_lexer_consume_token (parser->lexer);
38405 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38406 goto error1;
38408 token = cp_lexer_peek_token (parser->lexer);
38409 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38411 token = cp_lexer_consume_token (parser->lexer);
38413 attr_name = (token->type == CPP_KEYWORD
38414 /* For keywords, use the canonical spelling,
38415 not the parsed identifier. */
38416 ? ridpointers[(int) token->keyword]
38417 : token->u.value);
38418 attr = build_tree_list (attr_name, NULL_TREE);
38420 else
38421 cp_parser_error (parser, "expected identifier");
38423 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38424 error1:
38425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38426 return attr;
38429 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38431 transaction-statement:
38432 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38433 compound-statement
38434 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38437 static tree
38438 cp_parser_transaction (cp_parser *parser, cp_token *token)
38440 unsigned char old_in = parser->in_transaction;
38441 unsigned char this_in = 1, new_in;
38442 enum rid keyword = token->keyword;
38443 tree stmt, attrs, noex;
38445 cp_lexer_consume_token (parser->lexer);
38447 if (keyword == RID_TRANSACTION_RELAXED
38448 || keyword == RID_SYNCHRONIZED)
38449 this_in |= TM_STMT_ATTR_RELAXED;
38450 else
38452 attrs = cp_parser_txn_attribute_opt (parser);
38453 if (attrs)
38454 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38457 /* Parse a noexcept specification. */
38458 if (keyword == RID_ATOMIC_NOEXCEPT)
38459 noex = boolean_true_node;
38460 else if (keyword == RID_ATOMIC_CANCEL)
38462 /* cancel-and-throw is unimplemented. */
38463 sorry ("atomic_cancel");
38464 noex = NULL_TREE;
38466 else
38467 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38469 /* Keep track if we're in the lexical scope of an outer transaction. */
38470 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38472 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38474 parser->in_transaction = new_in;
38475 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38476 parser->in_transaction = old_in;
38478 finish_transaction_stmt (stmt, NULL, this_in, noex);
38480 return stmt;
38483 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38485 transaction-expression:
38486 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38487 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38490 static tree
38491 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38493 unsigned char old_in = parser->in_transaction;
38494 unsigned char this_in = 1;
38495 cp_token *token;
38496 tree expr, noex;
38497 bool noex_expr;
38498 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38500 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38501 || keyword == RID_TRANSACTION_RELAXED);
38503 if (!flag_tm)
38504 error_at (loc,
38505 keyword == RID_TRANSACTION_RELAXED
38506 ? G_("%<__transaction_relaxed%> without transactional memory "
38507 "support enabled")
38508 : G_("%<__transaction_atomic%> without transactional memory "
38509 "support enabled"));
38511 token = cp_parser_require_keyword (parser, keyword,
38512 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38513 : RT_TRANSACTION_RELAXED));
38514 gcc_assert (token != NULL);
38516 if (keyword == RID_TRANSACTION_RELAXED)
38517 this_in |= TM_STMT_ATTR_RELAXED;
38519 /* Set this early. This might mean that we allow transaction_cancel in
38520 an expression that we find out later actually has to be a constexpr.
38521 However, we expect that cxx_constant_value will be able to deal with
38522 this; also, if the noexcept has no constexpr, then what we parse next
38523 really is a transaction's body. */
38524 parser->in_transaction = this_in;
38526 /* Parse a noexcept specification. */
38527 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38528 true);
38530 if (!noex || !noex_expr
38531 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38533 matching_parens parens;
38534 parens.require_open (parser);
38536 expr = cp_parser_expression (parser);
38537 expr = finish_parenthesized_expr (expr);
38539 parens.require_close (parser);
38541 else
38543 /* The only expression that is available got parsed for the noexcept
38544 already. noexcept is true then. */
38545 expr = noex;
38546 noex = boolean_true_node;
38549 expr = build_transaction_expr (token->location, expr, this_in, noex);
38550 parser->in_transaction = old_in;
38552 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38553 return error_mark_node;
38555 return (flag_tm ? expr : error_mark_node);
38558 /* Parse a function-transaction-block.
38560 function-transaction-block:
38561 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38562 function-body
38563 __transaction_atomic txn-attribute[opt] function-try-block
38564 __transaction_relaxed ctor-initializer[opt] function-body
38565 __transaction_relaxed function-try-block
38568 static void
38569 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38571 unsigned char old_in = parser->in_transaction;
38572 unsigned char new_in = 1;
38573 tree compound_stmt, stmt, attrs;
38574 cp_token *token;
38576 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38577 || keyword == RID_TRANSACTION_RELAXED);
38578 token = cp_parser_require_keyword (parser, keyword,
38579 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38580 : RT_TRANSACTION_RELAXED));
38581 gcc_assert (token != NULL);
38583 if (keyword == RID_TRANSACTION_RELAXED)
38584 new_in |= TM_STMT_ATTR_RELAXED;
38585 else
38587 attrs = cp_parser_txn_attribute_opt (parser);
38588 if (attrs)
38589 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38592 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38594 parser->in_transaction = new_in;
38596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38597 cp_parser_function_try_block (parser);
38598 else
38599 cp_parser_ctor_initializer_opt_and_function_body
38600 (parser, /*in_function_try_block=*/false);
38602 parser->in_transaction = old_in;
38604 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38607 /* Parse a __transaction_cancel statement.
38609 cancel-statement:
38610 __transaction_cancel txn-attribute[opt] ;
38611 __transaction_cancel txn-attribute[opt] throw-expression ;
38613 ??? Cancel and throw is not yet implemented. */
38615 static tree
38616 cp_parser_transaction_cancel (cp_parser *parser)
38618 cp_token *token;
38619 bool is_outer = false;
38620 tree stmt, attrs;
38622 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38623 RT_TRANSACTION_CANCEL);
38624 gcc_assert (token != NULL);
38626 attrs = cp_parser_txn_attribute_opt (parser);
38627 if (attrs)
38628 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38630 /* ??? Parse cancel-and-throw here. */
38632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38634 if (!flag_tm)
38636 error_at (token->location, "%<__transaction_cancel%> without "
38637 "transactional memory support enabled");
38638 return error_mark_node;
38640 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38642 error_at (token->location, "%<__transaction_cancel%> within a "
38643 "%<__transaction_relaxed%>");
38644 return error_mark_node;
38646 else if (is_outer)
38648 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38649 && !is_tm_may_cancel_outer (current_function_decl))
38651 error_at (token->location, "outer %<__transaction_cancel%> not "
38652 "within outer %<__transaction_atomic%>");
38653 error_at (token->location,
38654 " or a %<transaction_may_cancel_outer%> function");
38655 return error_mark_node;
38658 else if (parser->in_transaction == 0)
38660 error_at (token->location, "%<__transaction_cancel%> not within "
38661 "%<__transaction_atomic%>");
38662 return error_mark_node;
38665 stmt = build_tm_abort_call (token->location, is_outer);
38666 add_stmt (stmt);
38668 return stmt;
38671 /* The parser. */
38673 static GTY (()) cp_parser *the_parser;
38676 /* Special handling for the first token or line in the file. The first
38677 thing in the file might be #pragma GCC pch_preprocess, which loads a
38678 PCH file, which is a GC collection point. So we need to handle this
38679 first pragma without benefit of an existing lexer structure.
38681 Always returns one token to the caller in *FIRST_TOKEN. This is
38682 either the true first token of the file, or the first token after
38683 the initial pragma. */
38685 static void
38686 cp_parser_initial_pragma (cp_token *first_token)
38688 tree name = NULL;
38690 cp_lexer_get_preprocessor_token (NULL, first_token);
38691 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38692 return;
38694 cp_lexer_get_preprocessor_token (NULL, first_token);
38695 if (first_token->type == CPP_STRING)
38697 name = first_token->u.value;
38699 cp_lexer_get_preprocessor_token (NULL, first_token);
38700 if (first_token->type != CPP_PRAGMA_EOL)
38701 error_at (first_token->location,
38702 "junk at end of %<#pragma GCC pch_preprocess%>");
38704 else
38705 error_at (first_token->location, "expected string literal");
38707 /* Skip to the end of the pragma. */
38708 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38709 cp_lexer_get_preprocessor_token (NULL, first_token);
38711 /* Now actually load the PCH file. */
38712 if (name)
38713 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38715 /* Read one more token to return to our caller. We have to do this
38716 after reading the PCH file in, since its pointers have to be
38717 live. */
38718 cp_lexer_get_preprocessor_token (NULL, first_token);
38721 /* Parses the grainsize pragma for the _Cilk_for statement.
38722 Syntax:
38723 #pragma cilk grainsize = <VALUE>. */
38725 static void
38726 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38728 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38730 tree exp = cp_parser_binary_expression (parser, false, false,
38731 PREC_NOT_OPERATOR, NULL);
38732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38733 if (!exp || exp == error_mark_node)
38735 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38736 return;
38739 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38741 cp_parser_cilk_for (parser, exp, if_p);
38742 else
38743 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38744 "%<#pragma cilk grainsize%> is not followed by "
38745 "%<_Cilk_for%>");
38746 return;
38748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38751 /* Normal parsing of a pragma token. Here we can (and must) use the
38752 regular lexer. */
38754 static bool
38755 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38757 cp_token *pragma_tok;
38758 unsigned int id;
38759 tree stmt;
38760 bool ret;
38762 pragma_tok = cp_lexer_consume_token (parser->lexer);
38763 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38764 parser->lexer->in_pragma = true;
38766 id = cp_parser_pragma_kind (pragma_tok);
38767 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38768 cp_ensure_no_omp_declare_simd (parser);
38769 switch (id)
38771 case PRAGMA_GCC_PCH_PREPROCESS:
38772 error_at (pragma_tok->location,
38773 "%<#pragma GCC pch_preprocess%> must be first");
38774 break;
38776 case PRAGMA_OMP_BARRIER:
38777 switch (context)
38779 case pragma_compound:
38780 cp_parser_omp_barrier (parser, pragma_tok);
38781 return false;
38782 case pragma_stmt:
38783 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38784 "used in compound statements", "omp barrier");
38785 break;
38786 default:
38787 goto bad_stmt;
38789 break;
38791 case PRAGMA_OMP_FLUSH:
38792 switch (context)
38794 case pragma_compound:
38795 cp_parser_omp_flush (parser, pragma_tok);
38796 return false;
38797 case pragma_stmt:
38798 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38799 "used in compound statements", "omp flush");
38800 break;
38801 default:
38802 goto bad_stmt;
38804 break;
38806 case PRAGMA_OMP_TASKWAIT:
38807 switch (context)
38809 case pragma_compound:
38810 cp_parser_omp_taskwait (parser, pragma_tok);
38811 return false;
38812 case pragma_stmt:
38813 error_at (pragma_tok->location,
38814 "%<#pragma %s%> may only be used in compound statements",
38815 "omp taskwait");
38816 break;
38817 default:
38818 goto bad_stmt;
38820 break;
38822 case PRAGMA_OMP_TASKYIELD:
38823 switch (context)
38825 case pragma_compound:
38826 cp_parser_omp_taskyield (parser, pragma_tok);
38827 return false;
38828 case pragma_stmt:
38829 error_at (pragma_tok->location,
38830 "%<#pragma %s%> may only be used in compound statements",
38831 "omp taskyield");
38832 break;
38833 default:
38834 goto bad_stmt;
38836 break;
38838 case PRAGMA_OMP_CANCEL:
38839 switch (context)
38841 case pragma_compound:
38842 cp_parser_omp_cancel (parser, pragma_tok);
38843 return false;
38844 case pragma_stmt:
38845 error_at (pragma_tok->location,
38846 "%<#pragma %s%> may only be used in compound statements",
38847 "omp cancel");
38848 break;
38849 default:
38850 goto bad_stmt;
38852 break;
38854 case PRAGMA_OMP_CANCELLATION_POINT:
38855 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38856 return false;
38858 case PRAGMA_OMP_THREADPRIVATE:
38859 cp_parser_omp_threadprivate (parser, pragma_tok);
38860 return false;
38862 case PRAGMA_OMP_DECLARE:
38863 cp_parser_omp_declare (parser, pragma_tok, context);
38864 return false;
38866 case PRAGMA_OACC_DECLARE:
38867 cp_parser_oacc_declare (parser, pragma_tok);
38868 return false;
38870 case PRAGMA_OACC_ENTER_DATA:
38871 if (context == pragma_stmt)
38873 error_at (pragma_tok->location,
38874 "%<#pragma %s%> may only be used in compound statements",
38875 "acc enter data");
38876 break;
38878 else if (context != pragma_compound)
38879 goto bad_stmt;
38880 cp_parser_omp_construct (parser, pragma_tok, if_p);
38881 return true;
38883 case PRAGMA_OACC_EXIT_DATA:
38884 if (context == pragma_stmt)
38886 error_at (pragma_tok->location,
38887 "%<#pragma %s%> may only be used in compound statements",
38888 "acc exit data");
38889 break;
38891 else if (context != pragma_compound)
38892 goto bad_stmt;
38893 cp_parser_omp_construct (parser, pragma_tok, if_p);
38894 return true;
38896 case PRAGMA_OACC_ROUTINE:
38897 if (context != pragma_external)
38899 error_at (pragma_tok->location,
38900 "%<#pragma acc routine%> must be at file scope");
38901 break;
38903 cp_parser_oacc_routine (parser, pragma_tok, context);
38904 return false;
38906 case PRAGMA_OACC_UPDATE:
38907 if (context == pragma_stmt)
38909 error_at (pragma_tok->location,
38910 "%<#pragma %s%> may only be used in compound statements",
38911 "acc update");
38912 break;
38914 else if (context != pragma_compound)
38915 goto bad_stmt;
38916 cp_parser_omp_construct (parser, pragma_tok, if_p);
38917 return true;
38919 case PRAGMA_OACC_WAIT:
38920 if (context == pragma_stmt)
38922 error_at (pragma_tok->location,
38923 "%<#pragma %s%> may only be used in compound statements",
38924 "acc wait");
38925 break;
38927 else if (context != pragma_compound)
38928 goto bad_stmt;
38929 cp_parser_omp_construct (parser, pragma_tok, if_p);
38930 return true;
38932 case PRAGMA_OACC_ATOMIC:
38933 case PRAGMA_OACC_CACHE:
38934 case PRAGMA_OACC_DATA:
38935 case PRAGMA_OACC_HOST_DATA:
38936 case PRAGMA_OACC_KERNELS:
38937 case PRAGMA_OACC_PARALLEL:
38938 case PRAGMA_OACC_LOOP:
38939 case PRAGMA_OMP_ATOMIC:
38940 case PRAGMA_OMP_CRITICAL:
38941 case PRAGMA_OMP_DISTRIBUTE:
38942 case PRAGMA_OMP_FOR:
38943 case PRAGMA_OMP_MASTER:
38944 case PRAGMA_OMP_PARALLEL:
38945 case PRAGMA_OMP_SECTIONS:
38946 case PRAGMA_OMP_SIMD:
38947 case PRAGMA_OMP_SINGLE:
38948 case PRAGMA_OMP_TASK:
38949 case PRAGMA_OMP_TASKGROUP:
38950 case PRAGMA_OMP_TASKLOOP:
38951 case PRAGMA_OMP_TEAMS:
38952 if (context != pragma_stmt && context != pragma_compound)
38953 goto bad_stmt;
38954 stmt = push_omp_privatization_clauses (false);
38955 cp_parser_omp_construct (parser, pragma_tok, if_p);
38956 pop_omp_privatization_clauses (stmt);
38957 return true;
38959 case PRAGMA_OMP_ORDERED:
38960 if (context != pragma_stmt && context != pragma_compound)
38961 goto bad_stmt;
38962 stmt = push_omp_privatization_clauses (false);
38963 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38964 pop_omp_privatization_clauses (stmt);
38965 return ret;
38967 case PRAGMA_OMP_TARGET:
38968 if (context != pragma_stmt && context != pragma_compound)
38969 goto bad_stmt;
38970 stmt = push_omp_privatization_clauses (false);
38971 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38972 pop_omp_privatization_clauses (stmt);
38973 return ret;
38975 case PRAGMA_OMP_END_DECLARE_TARGET:
38976 cp_parser_omp_end_declare_target (parser, pragma_tok);
38977 return false;
38979 case PRAGMA_OMP_SECTION:
38980 error_at (pragma_tok->location,
38981 "%<#pragma omp section%> may only be used in "
38982 "%<#pragma omp sections%> construct");
38983 break;
38985 case PRAGMA_IVDEP:
38987 if (context == pragma_external)
38989 error_at (pragma_tok->location,
38990 "%<#pragma GCC ivdep%> must be inside a function");
38991 break;
38993 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38994 cp_token *tok;
38995 tok = cp_lexer_peek_token (the_parser->lexer);
38996 if (tok->type != CPP_KEYWORD
38997 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38998 && tok->keyword != RID_DO))
39000 cp_parser_error (parser, "for, while or do statement expected");
39001 return false;
39003 cp_parser_iteration_statement (parser, if_p, true);
39004 return true;
39007 case PRAGMA_CILK_SIMD:
39008 if (context == pragma_external)
39010 error_at (pragma_tok->location,
39011 "%<#pragma simd%> must be inside a function");
39012 break;
39014 stmt = push_omp_privatization_clauses (false);
39015 cp_parser_cilk_simd (parser, pragma_tok, if_p);
39016 pop_omp_privatization_clauses (stmt);
39017 return true;
39019 case PRAGMA_CILK_GRAINSIZE:
39020 if (context == pragma_external)
39022 error_at (pragma_tok->location,
39023 "%<#pragma cilk grainsize%> must be inside a function");
39024 break;
39027 /* Ignore the pragma if Cilk Plus is not enabled. */
39028 if (flag_cilkplus)
39030 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
39031 return true;
39033 else
39035 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
39036 "%<#pragma cilk grainsize%>");
39037 break;
39040 default:
39041 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39042 c_invoke_pragma_handler (id);
39043 break;
39045 bad_stmt:
39046 cp_parser_error (parser, "expected declaration specifiers");
39047 break;
39050 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39051 return false;
39054 /* The interface the pragma parsers have to the lexer. */
39056 enum cpp_ttype
39057 pragma_lex (tree *value, location_t *loc)
39059 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39060 enum cpp_ttype ret = tok->type;
39062 *value = tok->u.value;
39063 if (loc)
39064 *loc = tok->location;
39066 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39067 ret = CPP_EOF;
39068 else if (ret == CPP_STRING)
39069 *value = cp_parser_string_literal (the_parser, false, false);
39070 else
39072 if (ret == CPP_KEYWORD)
39073 ret = CPP_NAME;
39074 cp_lexer_consume_token (the_parser->lexer);
39077 return ret;
39081 /* External interface. */
39083 /* Parse one entire translation unit. */
39085 void
39086 c_parse_file (void)
39088 static bool already_called = false;
39090 if (already_called)
39091 fatal_error (input_location,
39092 "inter-module optimizations not implemented for C++");
39093 already_called = true;
39095 the_parser = cp_parser_new ();
39096 push_deferring_access_checks (flag_access_control
39097 ? dk_no_deferred : dk_no_check);
39098 cp_parser_translation_unit (the_parser);
39099 the_parser = NULL;
39102 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
39103 vectorlength clause:
39104 Syntax:
39105 vectorlength ( constant-expression ) */
39107 static tree
39108 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
39109 bool is_simd_fn)
39111 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39112 tree expr;
39113 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
39114 safelen clause. Thus, vectorlength is represented as OMP 4.0
39115 safelen. For SIMD-enabled function it is represented by OMP 4.0
39116 simdlen. */
39117 if (!is_simd_fn)
39118 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
39119 loc);
39120 else
39121 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
39122 loc);
39124 matching_parens parens;
39125 if (!parens.require_open (parser))
39126 return error_mark_node;
39128 expr = cp_parser_constant_expression (parser);
39129 expr = maybe_constant_value (expr);
39131 /* If expr == error_mark_node, then don't emit any errors nor
39132 create a clause. if any of the above functions returns
39133 error mark node then they would have emitted an error message. */
39134 if (expr == error_mark_node)
39136 else if (!TREE_TYPE (expr)
39137 || !TREE_CONSTANT (expr)
39138 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
39139 error_at (loc, "vectorlength must be an integer constant");
39140 else if (TREE_CONSTANT (expr)
39141 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
39142 error_at (loc, "vectorlength must be a power of 2");
39143 else
39145 tree c;
39146 if (!is_simd_fn)
39148 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
39149 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
39150 OMP_CLAUSE_CHAIN (c) = clauses;
39151 clauses = c;
39153 else
39155 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
39156 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
39157 OMP_CLAUSE_CHAIN (c) = clauses;
39158 clauses = c;
39162 if (!parens.require_close (parser))
39163 return error_mark_node;
39164 return clauses;
39167 /* Handles the Cilk Plus #pragma simd linear clause.
39168 Syntax:
39169 linear ( simd-linear-variable-list )
39171 simd-linear-variable-list:
39172 simd-linear-variable
39173 simd-linear-variable-list , simd-linear-variable
39175 simd-linear-variable:
39176 id-expression
39177 id-expression : simd-linear-step
39179 simd-linear-step:
39180 conditional-expression */
39182 static tree
39183 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
39185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39187 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39188 return clauses;
39189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39191 cp_parser_error (parser, "expected identifier");
39192 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39193 return error_mark_node;
39196 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39197 parser->colon_corrects_to_scope_p = false;
39198 while (1)
39200 cp_token *token = cp_lexer_peek_token (parser->lexer);
39201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39203 cp_parser_error (parser, "expected variable-name");
39204 clauses = error_mark_node;
39205 break;
39208 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
39209 false, false);
39210 tree decl = cp_parser_lookup_name_simple (parser, var_name,
39211 token->location);
39212 if (decl == error_mark_node)
39214 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
39215 token->location);
39216 clauses = error_mark_node;
39218 else
39220 tree e = NULL_TREE;
39221 tree step_size = integer_one_node;
39223 /* If present, parse the linear step. Otherwise, assume the default
39224 value of 1. */
39225 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
39227 cp_lexer_consume_token (parser->lexer);
39229 e = cp_parser_assignment_expression (parser);
39230 e = maybe_constant_value (e);
39232 if (e == error_mark_node)
39234 /* If an error has occurred, then the whole pragma is
39235 considered ill-formed. Thus, no reason to keep
39236 parsing. */
39237 clauses = error_mark_node;
39238 break;
39240 else if (type_dependent_expression_p (e)
39241 || value_dependent_expression_p (e)
39242 || (TREE_TYPE (e)
39243 && INTEGRAL_TYPE_P (TREE_TYPE (e))
39244 && (TREE_CONSTANT (e)
39245 || DECL_P (e))))
39246 step_size = e;
39247 else
39248 cp_parser_error (parser,
39249 "step size must be an integer constant "
39250 "expression or an integer variable");
39253 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
39254 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39255 OMP_CLAUSE_DECL (l) = decl;
39256 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
39257 OMP_CLAUSE_CHAIN (l) = clauses;
39258 clauses = l;
39260 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39261 cp_lexer_consume_token (parser->lexer);
39262 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39263 break;
39264 else
39266 error_at (cp_lexer_peek_token (parser->lexer)->location,
39267 "expected %<,%> or %<)%> after %qE", decl);
39268 clauses = error_mark_node;
39269 break;
39272 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39273 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39274 return clauses;
39277 /* Returns the name of the next clause. If the clause is not
39278 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
39279 token is not consumed. Otherwise, the appropriate enum from the
39280 pragma_simd_clause is returned and the token is consumed. */
39282 static pragma_omp_clause
39283 cp_parser_cilk_simd_clause_name (cp_parser *parser)
39285 pragma_omp_clause clause_type;
39286 cp_token *token = cp_lexer_peek_token (parser->lexer);
39288 if (token->keyword == RID_PRIVATE)
39289 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
39290 else if (!token->u.value || token->type != CPP_NAME)
39291 return PRAGMA_CILK_CLAUSE_NONE;
39292 else if (id_equal (token->u.value, "vectorlength"))
39293 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
39294 else if (id_equal (token->u.value, "linear"))
39295 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
39296 else if (id_equal (token->u.value, "firstprivate"))
39297 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
39298 else if (id_equal (token->u.value, "lastprivate"))
39299 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
39300 else if (id_equal (token->u.value, "reduction"))
39301 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
39302 else
39303 return PRAGMA_CILK_CLAUSE_NONE;
39305 cp_lexer_consume_token (parser->lexer);
39306 return clause_type;
39309 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39311 static tree
39312 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
39314 tree clauses = NULL_TREE;
39316 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39317 && clauses != error_mark_node)
39319 pragma_omp_clause c_kind;
39320 c_kind = cp_parser_cilk_simd_clause_name (parser);
39321 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
39322 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
39323 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
39324 clauses = cp_parser_cilk_simd_linear (parser, clauses);
39325 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
39326 /* Use the OpenMP 4.0 equivalent function. */
39327 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
39328 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
39329 /* Use the OpenMP 4.0 equivalent function. */
39330 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39331 clauses);
39332 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
39333 /* Use the OMP 4.0 equivalent function. */
39334 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
39335 clauses);
39336 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
39337 /* Use the OMP 4.0 equivalent function. */
39338 clauses = cp_parser_omp_clause_reduction (parser, clauses);
39339 else
39341 clauses = error_mark_node;
39342 cp_parser_error (parser, "expected %<#pragma simd%> clause");
39343 break;
39347 cp_parser_skip_to_pragma_eol (parser, pragma_token);
39349 if (clauses == error_mark_node)
39350 return error_mark_node;
39351 else
39352 return finish_omp_clauses (clauses, C_ORT_CILK);
39355 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39357 static void
39358 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
39360 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
39362 if (clauses == error_mark_node)
39363 return;
39365 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
39367 error_at (cp_lexer_peek_token (parser->lexer)->location,
39368 "for statement expected");
39369 return;
39372 tree sb = begin_omp_structured_block ();
39373 int save = cp_parser_begin_omp_structured_block (parser);
39374 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
39375 if (ret)
39376 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
39377 cp_parser_end_omp_structured_block (parser, save);
39378 add_stmt (finish_omp_structured_block (sb));
39381 /* Main entry-point for parsing Cilk Plus _Cilk_for
39382 loops. The return value is error_mark_node
39383 when errors happen and CILK_FOR tree on success. */
39385 static tree
39386 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
39388 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
39389 gcc_unreachable ();
39391 tree sb = begin_omp_structured_block ();
39392 int save = cp_parser_begin_omp_structured_block (parser);
39394 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
39395 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
39396 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
39397 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
39399 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
39400 if (ret)
39401 cpp_validate_cilk_plus_loop (ret);
39402 else
39403 ret = error_mark_node;
39405 cp_parser_end_omp_structured_block (parser, save);
39406 add_stmt (finish_omp_structured_block (sb));
39407 return ret;
39410 /* Create an identifier for a generic parameter type (a synthesized
39411 template parameter implied by `auto' or a concept identifier). */
39413 static GTY(()) int generic_parm_count;
39414 static tree
39415 make_generic_type_name ()
39417 char buf[32];
39418 sprintf (buf, "auto:%d", ++generic_parm_count);
39419 return get_identifier (buf);
39422 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39423 (creating a new template parameter list if necessary). Returns the newly
39424 created template type parm. */
39426 static tree
39427 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39429 gcc_assert (current_binding_level->kind == sk_function_parms);
39431 /* Before committing to modifying any scope, if we're in an
39432 implicit template scope, and we're trying to synthesize a
39433 constrained parameter, try to find a previous parameter with
39434 the same name. This is the same-type rule for abbreviated
39435 function templates.
39437 NOTE: We can generate implicit parameters when tentatively
39438 parsing a nested name specifier, only to reject that parse
39439 later. However, matching the same template-id as part of a
39440 direct-declarator should generate an identical template
39441 parameter, so this rule will merge them. */
39442 if (parser->implicit_template_scope && constr)
39444 tree t = parser->implicit_template_parms;
39445 while (t)
39447 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39449 tree d = TREE_VALUE (t);
39450 if (TREE_CODE (d) == PARM_DECL)
39451 /* Return the TEMPLATE_PARM_INDEX. */
39452 d = DECL_INITIAL (d);
39453 return d;
39455 t = TREE_CHAIN (t);
39459 /* We are either continuing a function template that already contains implicit
39460 template parameters, creating a new fully-implicit function template, or
39461 extending an existing explicit function template with implicit template
39462 parameters. */
39464 cp_binding_level *const entry_scope = current_binding_level;
39466 bool become_template = false;
39467 cp_binding_level *parent_scope = 0;
39469 if (parser->implicit_template_scope)
39471 gcc_assert (parser->implicit_template_parms);
39473 current_binding_level = parser->implicit_template_scope;
39475 else
39477 /* Roll back to the existing template parameter scope (in the case of
39478 extending an explicit function template) or introduce a new template
39479 parameter scope ahead of the function parameter scope (or class scope
39480 in the case of out-of-line member definitions). The function scope is
39481 added back after template parameter synthesis below. */
39483 cp_binding_level *scope = entry_scope;
39485 while (scope->kind == sk_function_parms)
39487 parent_scope = scope;
39488 scope = scope->level_chain;
39490 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39492 /* If not defining a class, then any class scope is a scope level in
39493 an out-of-line member definition. In this case simply wind back
39494 beyond the first such scope to inject the template parameter list.
39495 Otherwise wind back to the class being defined. The latter can
39496 occur in class member friend declarations such as:
39498 class A {
39499 void foo (auto);
39501 class B {
39502 friend void A::foo (auto);
39505 The template parameter list synthesized for the friend declaration
39506 must be injected in the scope of 'B'. This can also occur in
39507 erroneous cases such as:
39509 struct A {
39510 struct B {
39511 void foo (auto);
39513 void B::foo (auto) {}
39516 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39517 but, nevertheless, the template parameter list synthesized for the
39518 declarator should be injected into the scope of 'A' as if the
39519 ill-formed template was specified explicitly. */
39521 while (scope->kind == sk_class && !scope->defining_class_p)
39523 parent_scope = scope;
39524 scope = scope->level_chain;
39528 current_binding_level = scope;
39530 if (scope->kind != sk_template_parms
39531 || !function_being_declared_is_template_p (parser))
39533 /* Introduce a new template parameter list for implicit template
39534 parameters. */
39536 become_template = true;
39538 parser->implicit_template_scope
39539 = begin_scope (sk_template_parms, NULL);
39541 ++processing_template_decl;
39543 parser->fully_implicit_function_template_p = true;
39544 ++parser->num_template_parameter_lists;
39546 else
39548 /* Synthesize implicit template parameters at the end of the explicit
39549 template parameter list. */
39551 gcc_assert (current_template_parms);
39553 parser->implicit_template_scope = scope;
39555 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39556 parser->implicit_template_parms
39557 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39561 /* Synthesize a new template parameter and track the current template
39562 parameter chain with implicit_template_parms. */
39564 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39565 tree synth_id = make_generic_type_name ();
39566 tree synth_tmpl_parm;
39567 bool non_type = false;
39569 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39570 synth_tmpl_parm
39571 = finish_template_type_parm (class_type_node, synth_id);
39572 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39573 synth_tmpl_parm
39574 = finish_constrained_template_template_parm (proto, synth_id);
39575 else
39577 synth_tmpl_parm = copy_decl (proto);
39578 DECL_NAME (synth_tmpl_parm) = synth_id;
39579 non_type = true;
39582 // Attach the constraint to the parm before processing.
39583 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39584 TREE_TYPE (node) = constr;
39585 tree new_parm
39586 = process_template_parm (parser->implicit_template_parms,
39587 input_location,
39588 node,
39589 /*non_type=*/non_type,
39590 /*param_pack=*/false);
39592 // Chain the new parameter to the list of implicit parameters.
39593 if (parser->implicit_template_parms)
39594 parser->implicit_template_parms
39595 = TREE_CHAIN (parser->implicit_template_parms);
39596 else
39597 parser->implicit_template_parms = new_parm;
39599 tree new_decl = get_local_decls ();
39600 if (non_type)
39601 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39602 new_decl = DECL_INITIAL (new_decl);
39604 /* If creating a fully implicit function template, start the new implicit
39605 template parameter list with this synthesized type, otherwise grow the
39606 current template parameter list. */
39608 if (become_template)
39610 parent_scope->level_chain = current_binding_level;
39612 tree new_parms = make_tree_vec (1);
39613 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39614 current_template_parms = tree_cons (size_int (processing_template_decl),
39615 new_parms, current_template_parms);
39617 else
39619 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39620 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39621 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39622 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39625 // If the new parameter was constrained, we need to add that to the
39626 // constraints in the template parameter list.
39627 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39629 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39630 reqs = conjoin_constraints (reqs, req);
39631 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39634 current_binding_level = entry_scope;
39636 return new_decl;
39639 /* Finish the declaration of a fully implicit function template. Such a
39640 template has no explicit template parameter list so has not been through the
39641 normal template head and tail processing. synthesize_implicit_template_parm
39642 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39643 provided if the declaration is a class member such that its template
39644 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39645 form is returned. Otherwise NULL_TREE is returned. */
39647 static tree
39648 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39650 gcc_assert (parser->fully_implicit_function_template_p);
39652 if (member_decl_opt && member_decl_opt != error_mark_node
39653 && DECL_VIRTUAL_P (member_decl_opt))
39655 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39656 "implicit templates may not be %<virtual%>");
39657 DECL_VIRTUAL_P (member_decl_opt) = false;
39660 if (member_decl_opt)
39661 member_decl_opt = finish_member_template_decl (member_decl_opt);
39662 end_template_decl ();
39664 parser->fully_implicit_function_template_p = false;
39665 --parser->num_template_parameter_lists;
39667 return member_decl_opt;
39670 /* Helper function for diagnostics that have complained about things
39671 being used with 'extern "C"' linkage.
39673 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39675 void
39676 maybe_show_extern_c_location (void)
39678 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39679 inform (the_parser->innermost_linkage_specification_location,
39680 "%<extern \"C\"%> linkage started here");
39683 #include "gt-cp-parser.h"