PR c++/89511 - ICE with using-declaration and unscoped enumerator.
[official-gcc.git] / gcc / cp / parser.c
blob5f69403349654386dd0d135e113df43b341d170d
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
50 /* The lexer. */
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token =
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
203 /* Prototypes. */
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
251 static void cp_parser_initial_pragma
252 (cp_token *);
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
263 /* Variables. */
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
289 if (buffer == NULL)
290 return;
292 if (num == 0)
293 num = buffer->length ();
295 if (start_token == NULL)
296 start_token = buffer->address ();
298 if (start_token > buffer->address ())
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
308 if (token == start_token)
309 do_print = true;
311 if (!do_print)
312 continue;
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
318 cp_lexer_print_token (file, token);
320 if (token == curr_token)
321 fprintf (file, "]]");
323 switch (token->type)
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
332 default:
333 fputc (' ', file);
337 if (i == num && i < buffer->length ())
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
343 fprintf (file, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
377 if (t)
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
385 /* Dump parser context C to FILE. */
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
402 unsigned i;
403 cp_parser_context *c;
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
424 /* Print an unparsed function entry UF to FILE. */
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
452 fprintf (file, "\n");
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
461 fprintf (file, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
489 cp_token *next_token, *first_token, *start_token;
491 if (file == NULL)
492 file = stderr;
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
514 if (file == NULL)
515 file = stderr;
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names forbidden in current context",
540 (parser->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN));
542 cp_debug_print_flag (file, "'this' forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & THIS_FORBIDDEN));
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "String expressions should be translated "
564 "to execution character set",
565 parser->translate_strings_p);
566 cp_debug_print_flag (file, "Parsing function body outside of a "
567 "local class", parser->in_function_body);
568 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
569 parser->colon_corrects_to_scope_p);
570 cp_debug_print_flag (file, "Colon doesn't start a class definition",
571 parser->colon_doesnt_start_class_def_p);
572 if (parser->type_definition_forbidden_message)
573 fprintf (file, "Error message for forbidden type definitions: %s\n",
574 parser->type_definition_forbidden_message);
575 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
576 fprintf (file, "Number of class definitions in progress: %u\n",
577 parser->num_classes_being_defined);
578 fprintf (file, "Number of template parameter lists for the current "
579 "declaration: %u\n", parser->num_template_parameter_lists);
580 cp_debug_parser_tokens (file, parser, window_size);
581 token = parser->lexer->next_token;
582 fprintf (file, "Next token to parse:\n");
583 fprintf (file, "\tToken: ");
584 cp_lexer_print_token (file, token);
585 eloc = expand_location (token->location);
586 fprintf (file, "\n\tFile: %s\n", eloc.file);
587 fprintf (file, "\tLine: %d\n", eloc.line);
588 fprintf (file, "\tColumn: %d\n", eloc.column);
591 DEBUG_FUNCTION void
592 debug (cp_parser &ref)
594 cp_debug_parser (stderr, &ref);
597 DEBUG_FUNCTION void
598 debug (cp_parser *ptr)
600 if (ptr)
601 debug (*ptr);
602 else
603 fprintf (stderr, "<nil>\n");
606 /* Allocate memory for a new lexer object and return it. */
608 static cp_lexer *
609 cp_lexer_alloc (void)
611 cp_lexer *lexer;
613 c_common_no_more_pch ();
615 /* Allocate the memory. */
616 lexer = ggc_cleared_alloc<cp_lexer> ();
618 /* Initially we are not debugging. */
619 lexer->debugging_p = false;
621 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
623 /* Create the buffer. */
624 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
626 return lexer;
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 preprocessor. */
633 static cp_lexer *
634 cp_lexer_new_main (void)
636 cp_lexer *lexer;
637 cp_token token;
639 /* It's possible that parsing the first pragma will load a PCH file,
640 which is a GC collection point. So we have to do that before
641 allocating any memory. */
642 cp_parser_initial_pragma (&token);
644 lexer = cp_lexer_alloc ();
646 /* Put the first token in the buffer. */
647 lexer->buffer->quick_push (token);
649 /* Get the remaining tokens from the preprocessor. */
650 while (token.type != CPP_EOF)
652 cp_lexer_get_preprocessor_token (lexer, &token);
653 vec_safe_push (lexer->buffer, token);
656 lexer->last_token = lexer->buffer->address ()
657 + lexer->buffer->length ()
658 - 1;
659 lexer->next_token = lexer->buffer->length ()
660 ? lexer->buffer->address ()
661 : &eof_token;
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
665 done_lexing = true;
667 gcc_assert (!lexer->next_token->purged_p);
668 return lexer;
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
674 static cp_lexer *
675 cp_lexer_new_from_tokens (cp_token_cache *cache)
677 cp_token *first = cache->first;
678 cp_token *last = cache->last;
679 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
681 /* We do not own the buffer. */
682 lexer->buffer = NULL;
683 lexer->next_token = first == last ? &eof_token : first;
684 lexer->last_token = last;
686 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
688 /* Initially we are not debugging. */
689 lexer->debugging_p = false;
691 gcc_assert (!lexer->next_token->purged_p);
692 return lexer;
695 /* Frees all resources associated with LEXER. */
697 static void
698 cp_lexer_destroy (cp_lexer *lexer)
700 vec_free (lexer->buffer);
701 lexer->saved_tokens.release ();
702 ggc_free (lexer);
705 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
706 be used. The point of this flag is to help the compiler to fold away calls
707 to cp_lexer_debugging_p within this source file at compile time, when the
708 lexer is not being debugged. */
710 #define LEXER_DEBUGGING_ENABLED_P false
712 /* Returns nonzero if debugging information should be output. */
714 static inline bool
715 cp_lexer_debugging_p (cp_lexer *lexer)
717 if (!LEXER_DEBUGGING_ENABLED_P)
718 return false;
720 return lexer->debugging_p;
724 static inline cp_token_position
725 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
727 gcc_assert (!previous_p || lexer->next_token != &eof_token);
729 return lexer->next_token - previous_p;
732 static inline cp_token *
733 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
735 return pos;
738 static inline void
739 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
741 lexer->next_token = cp_lexer_token_at (lexer, pos);
744 static inline cp_token_position
745 cp_lexer_previous_token_position (cp_lexer *lexer)
747 if (lexer->next_token == &eof_token)
748 return lexer->last_token - 1;
749 else
750 return cp_lexer_token_position (lexer, true);
753 static inline cp_token *
754 cp_lexer_previous_token (cp_lexer *lexer)
756 cp_token_position tp = cp_lexer_previous_token_position (lexer);
758 /* Skip past purged tokens. */
759 while (tp->purged_p)
761 gcc_assert (tp != vec_safe_address (lexer->buffer));
762 tp--;
765 return cp_lexer_token_at (lexer, tp);
768 /* nonzero if we are presently saving tokens. */
770 static inline int
771 cp_lexer_saving_tokens (const cp_lexer* lexer)
773 return lexer->saved_tokens.length () != 0;
776 /* Store the next token from the preprocessor in *TOKEN. Return true
777 if we reach EOF. If LEXER is NULL, assume we are handling an
778 initial #pragma pch_preprocess, and thus want the lexer to return
779 processed strings. */
781 static void
782 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
784 static int is_extern_c = 0;
786 /* Get a new token from the preprocessor. */
787 token->type
788 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
789 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
790 token->keyword = RID_MAX;
791 token->purged_p = false;
792 token->error_reported = false;
794 /* On some systems, some header files are surrounded by an
795 implicit extern "C" block. Set a flag in the token if it
796 comes from such a header. */
797 is_extern_c += pending_lang_change;
798 pending_lang_change = 0;
799 token->implicit_extern_c = is_extern_c > 0;
801 /* Check to see if this token is a keyword. */
802 if (token->type == CPP_NAME)
804 if (IDENTIFIER_KEYWORD_P (token->u.value))
806 /* Mark this token as a keyword. */
807 token->type = CPP_KEYWORD;
808 /* Record which keyword. */
809 token->keyword = C_RID_CODE (token->u.value);
811 else
813 if (warn_cxx11_compat
814 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
815 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
817 /* Warn about the C++0x keyword (but still treat it as
818 an identifier). */
819 warning (OPT_Wc__11_compat,
820 "identifier %qE is a keyword in C++11",
821 token->u.value);
823 /* Clear out the C_RID_CODE so we don't warn about this
824 particular identifier-turned-keyword again. */
825 C_SET_RID_CODE (token->u.value, RID_MAX);
828 token->keyword = RID_MAX;
831 else if (token->type == CPP_AT_NAME)
833 /* This only happens in Objective-C++; it must be a keyword. */
834 token->type = CPP_KEYWORD;
835 switch (C_RID_CODE (token->u.value))
837 /* Replace 'class' with '@class', 'private' with '@private',
838 etc. This prevents confusion with the C++ keyword
839 'class', and makes the tokens consistent with other
840 Objective-C 'AT' keywords. For example '@class' is
841 reported as RID_AT_CLASS which is consistent with
842 '@synchronized', which is reported as
843 RID_AT_SYNCHRONIZED.
845 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
846 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
847 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
848 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
849 case RID_THROW: token->keyword = RID_AT_THROW; break;
850 case RID_TRY: token->keyword = RID_AT_TRY; break;
851 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
852 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
853 default: token->keyword = C_RID_CODE (token->u.value);
858 /* Update the globals input_location and the input file stack from TOKEN. */
859 static inline void
860 cp_lexer_set_source_position_from_token (cp_token *token)
862 if (token->type != CPP_EOF)
864 input_location = token->location;
868 /* Update the globals input_location and the input file stack from LEXER. */
869 static inline void
870 cp_lexer_set_source_position (cp_lexer *lexer)
872 cp_token *token = cp_lexer_peek_token (lexer);
873 cp_lexer_set_source_position_from_token (token);
876 /* Return a pointer to the next token in the token stream, but do not
877 consume it. */
879 static inline cp_token *
880 cp_lexer_peek_token (cp_lexer *lexer)
882 if (cp_lexer_debugging_p (lexer))
884 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
885 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
886 putc ('\n', cp_lexer_debug_stream);
888 return lexer->next_token;
891 /* Return true if the next token has the indicated TYPE. */
893 static inline bool
894 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
896 return cp_lexer_peek_token (lexer)->type == type;
899 /* Return true if the next token does not have the indicated TYPE. */
901 static inline bool
902 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
904 return !cp_lexer_next_token_is (lexer, type);
907 /* Return true if the next token is the indicated KEYWORD. */
909 static inline bool
910 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
912 return cp_lexer_peek_token (lexer)->keyword == keyword;
915 static inline bool
916 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
918 return cp_lexer_peek_nth_token (lexer, n)->type == type;
921 static inline bool
922 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
924 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
927 /* Return true if KEYWORD can start a decl-specifier. */
929 bool
930 cp_keyword_starts_decl_specifier_p (enum rid keyword)
932 switch (keyword)
934 /* auto specifier: storage-class-specifier in C++,
935 simple-type-specifier in C++0x. */
936 case RID_AUTO:
937 /* Storage classes. */
938 case RID_REGISTER:
939 case RID_STATIC:
940 case RID_EXTERN:
941 case RID_MUTABLE:
942 case RID_THREAD:
943 /* Elaborated type specifiers. */
944 case RID_ENUM:
945 case RID_CLASS:
946 case RID_STRUCT:
947 case RID_UNION:
948 case RID_TYPENAME:
949 /* Simple type specifiers. */
950 case RID_CHAR:
951 case RID_CHAR8:
952 case RID_CHAR16:
953 case RID_CHAR32:
954 case RID_WCHAR:
955 case RID_BOOL:
956 case RID_SHORT:
957 case RID_INT:
958 case RID_LONG:
959 case RID_SIGNED:
960 case RID_UNSIGNED:
961 case RID_FLOAT:
962 case RID_DOUBLE:
963 case RID_VOID:
964 /* GNU extensions. */
965 case RID_ATTRIBUTE:
966 case RID_TYPEOF:
967 /* C++0x extensions. */
968 case RID_DECLTYPE:
969 case RID_UNDERLYING_TYPE:
970 case RID_CONSTEXPR:
971 return true;
973 default:
974 if (keyword >= RID_FIRST_INT_N
975 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
976 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
977 return true;
978 return false;
982 /* Return true if the next token is a keyword for a decl-specifier. */
984 static bool
985 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
987 cp_token *token;
989 token = cp_lexer_peek_token (lexer);
990 return cp_keyword_starts_decl_specifier_p (token->keyword);
993 /* Returns TRUE iff the token T begins a decltype type. */
995 static bool
996 token_is_decltype (cp_token *t)
998 return (t->keyword == RID_DECLTYPE
999 || t->type == CPP_DECLTYPE);
1002 /* Returns TRUE iff the next token begins a decltype type. */
1004 static bool
1005 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1007 cp_token *t = cp_lexer_peek_token (lexer);
1008 return token_is_decltype (t);
1011 /* Called when processing a token with tree_check_value; perform or defer the
1012 associated checks and return the value. */
1014 static tree
1015 saved_checks_value (struct tree_check *check_value)
1017 /* Perform any access checks that were deferred. */
1018 vec<deferred_access_check, va_gc> *checks;
1019 deferred_access_check *chk;
1020 checks = check_value->checks;
1021 if (checks)
1023 int i;
1024 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1025 perform_or_defer_access_check (chk->binfo,
1026 chk->decl,
1027 chk->diag_decl, tf_warning_or_error);
1029 /* Return the stored value. */
1030 return check_value->value;
1033 /* Return a pointer to the Nth token in the token stream. If N is 1,
1034 then this is precisely equivalent to cp_lexer_peek_token (except
1035 that it is not inline). One would like to disallow that case, but
1036 there is one case (cp_parser_nth_token_starts_template_id) where
1037 the caller passes a variable for N and it might be 1. */
1039 static cp_token *
1040 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1042 cp_token *token;
1044 /* N is 1-based, not zero-based. */
1045 gcc_assert (n > 0);
1047 if (cp_lexer_debugging_p (lexer))
1048 fprintf (cp_lexer_debug_stream,
1049 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1051 --n;
1052 token = lexer->next_token;
1053 gcc_assert (!n || token != &eof_token);
1054 while (n != 0)
1056 ++token;
1057 if (token == lexer->last_token)
1059 token = &eof_token;
1060 break;
1063 if (!token->purged_p)
1064 --n;
1067 if (cp_lexer_debugging_p (lexer))
1069 cp_lexer_print_token (cp_lexer_debug_stream, token);
1070 putc ('\n', cp_lexer_debug_stream);
1073 return token;
1076 /* Return the next token, and advance the lexer's next_token pointer
1077 to point to the next non-purged token. */
1079 static cp_token *
1080 cp_lexer_consume_token (cp_lexer* lexer)
1082 cp_token *token = lexer->next_token;
1084 gcc_assert (token != &eof_token);
1085 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1089 lexer->next_token++;
1090 if (lexer->next_token == lexer->last_token)
1092 lexer->next_token = &eof_token;
1093 break;
1097 while (lexer->next_token->purged_p);
1099 cp_lexer_set_source_position_from_token (token);
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer))
1104 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1105 cp_lexer_print_token (cp_lexer_debug_stream, token);
1106 putc ('\n', cp_lexer_debug_stream);
1109 return token;
1112 /* Permanently remove the next token from the token stream, and
1113 advance the next_token pointer to refer to the next non-purged
1114 token. */
1116 static void
1117 cp_lexer_purge_token (cp_lexer *lexer)
1119 cp_token *tok = lexer->next_token;
1121 gcc_assert (tok != &eof_token);
1122 tok->purged_p = true;
1123 tok->location = UNKNOWN_LOCATION;
1124 tok->u.value = NULL_TREE;
1125 tok->keyword = RID_MAX;
1129 tok++;
1130 if (tok == lexer->last_token)
1132 tok = &eof_token;
1133 break;
1136 while (tok->purged_p);
1137 lexer->next_token = tok;
1140 /* Permanently remove all tokens after TOK, up to, but not
1141 including, the token that will be returned next by
1142 cp_lexer_peek_token. */
1144 static void
1145 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1147 cp_token *peek = lexer->next_token;
1149 if (peek == &eof_token)
1150 peek = lexer->last_token;
1152 gcc_assert (tok < peek);
1154 for ( tok += 1; tok != peek; tok += 1)
1156 tok->purged_p = true;
1157 tok->location = UNKNOWN_LOCATION;
1158 tok->u.value = NULL_TREE;
1159 tok->keyword = RID_MAX;
1163 /* Begin saving tokens. All tokens consumed after this point will be
1164 preserved. */
1166 static void
1167 cp_lexer_save_tokens (cp_lexer* lexer)
1169 /* Provide debugging output. */
1170 if (cp_lexer_debugging_p (lexer))
1171 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1173 lexer->saved_tokens.safe_push (lexer->next_token);
1176 /* Commit to the portion of the token stream most recently saved. */
1178 static void
1179 cp_lexer_commit_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1185 lexer->saved_tokens.pop ();
1188 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1189 to the token stream. Stop saving tokens. */
1191 static void
1192 cp_lexer_rollback_tokens (cp_lexer* lexer)
1194 /* Provide debugging output. */
1195 if (cp_lexer_debugging_p (lexer))
1196 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1198 lexer->next_token = lexer->saved_tokens.pop ();
1201 /* RAII wrapper around the above functions, with sanity checking. Creating
1202 a variable saves tokens, which are committed when the variable is
1203 destroyed unless they are explicitly rolled back by calling the rollback
1204 member function. */
1206 struct saved_token_sentinel
1208 cp_lexer *lexer;
1209 unsigned len;
1210 bool commit;
1211 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1213 len = lexer->saved_tokens.length ();
1214 cp_lexer_save_tokens (lexer);
1216 void rollback ()
1218 cp_lexer_rollback_tokens (lexer);
1219 commit = false;
1221 ~saved_token_sentinel()
1223 if (commit)
1224 cp_lexer_commit_tokens (lexer);
1225 gcc_assert (lexer->saved_tokens.length () == len);
1229 /* Print a representation of the TOKEN on the STREAM. */
1231 static void
1232 cp_lexer_print_token (FILE * stream, cp_token *token)
1234 /* We don't use cpp_type2name here because the parser defines
1235 a few tokens of its own. */
1236 static const char *const token_names[] = {
1237 /* cpplib-defined token types */
1238 #define OP(e, s) #e,
1239 #define TK(e, s) #e,
1240 TTYPE_TABLE
1241 #undef OP
1242 #undef TK
1243 /* C++ parser token types - see "Manifest constants", above. */
1244 "KEYWORD",
1245 "TEMPLATE_ID",
1246 "NESTED_NAME_SPECIFIER",
1249 /* For some tokens, print the associated data. */
1250 switch (token->type)
1252 case CPP_KEYWORD:
1253 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1254 For example, `struct' is mapped to an INTEGER_CST. */
1255 if (!identifier_p (token->u.value))
1256 break;
1257 /* fall through */
1258 case CPP_NAME:
1259 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1260 break;
1262 case CPP_STRING:
1263 case CPP_STRING16:
1264 case CPP_STRING32:
1265 case CPP_WSTRING:
1266 case CPP_UTF8STRING:
1267 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1268 break;
1270 case CPP_NUMBER:
1271 print_generic_expr (stream, token->u.value);
1272 break;
1274 default:
1275 /* If we have a name for the token, print it out. Otherwise, we
1276 simply give the numeric code. */
1277 if (token->type < ARRAY_SIZE(token_names))
1278 fputs (token_names[token->type], stream);
1279 else
1280 fprintf (stream, "[%d]", token->type);
1281 break;
1285 DEBUG_FUNCTION void
1286 debug (cp_token &ref)
1288 cp_lexer_print_token (stderr, &ref);
1289 fprintf (stderr, "\n");
1292 DEBUG_FUNCTION void
1293 debug (cp_token *ptr)
1295 if (ptr)
1296 debug (*ptr);
1297 else
1298 fprintf (stderr, "<nil>\n");
1302 /* Start emitting debugging information. */
1304 static void
1305 cp_lexer_start_debugging (cp_lexer* lexer)
1307 if (!LEXER_DEBUGGING_ENABLED_P)
1308 fatal_error (input_location,
1309 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1311 lexer->debugging_p = true;
1312 cp_lexer_debug_stream = stderr;
1315 /* Stop emitting debugging information. */
1317 static void
1318 cp_lexer_stop_debugging (cp_lexer* lexer)
1320 if (!LEXER_DEBUGGING_ENABLED_P)
1321 fatal_error (input_location,
1322 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1324 lexer->debugging_p = false;
1325 cp_lexer_debug_stream = NULL;
1328 /* Create a new cp_token_cache, representing a range of tokens. */
1330 static cp_token_cache *
1331 cp_token_cache_new (cp_token *first, cp_token *last)
1333 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1334 cache->first = first;
1335 cache->last = last;
1336 return cache;
1339 /* Diagnose if #pragma omp declare simd isn't followed immediately
1340 by function declaration or definition. */
1342 static inline void
1343 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1345 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1347 error ("%<#pragma omp declare simd%> not immediately followed by "
1348 "function declaration or definition");
1349 parser->omp_declare_simd = NULL;
1353 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1354 and put that into "omp declare simd" attribute. */
1356 static inline void
1357 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1359 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1361 if (fndecl == error_mark_node)
1363 parser->omp_declare_simd = NULL;
1364 return;
1366 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1368 cp_ensure_no_omp_declare_simd (parser);
1369 return;
1374 /* Diagnose if #pragma acc routine isn't followed immediately by function
1375 declaration or definition. */
1377 static inline void
1378 cp_ensure_no_oacc_routine (cp_parser *parser)
1380 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1382 error_at (parser->oacc_routine->loc,
1383 "%<#pragma acc routine%> not immediately followed by "
1384 "function declaration or definition");
1385 parser->oacc_routine = NULL;
1389 /* Decl-specifiers. */
1391 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1393 static void
1394 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1396 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1399 /* Declarators. */
1401 /* Nothing other than the parser should be creating declarators;
1402 declarators are a semi-syntactic representation of C++ entities.
1403 Other parts of the front end that need to create entities (like
1404 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1406 static cp_declarator *make_call_declarator
1407 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1408 static cp_declarator *make_array_declarator
1409 (cp_declarator *, tree);
1410 static cp_declarator *make_pointer_declarator
1411 (cp_cv_quals, cp_declarator *, tree);
1412 static cp_declarator *make_reference_declarator
1413 (cp_cv_quals, cp_declarator *, bool, tree);
1414 static cp_declarator *make_ptrmem_declarator
1415 (cp_cv_quals, tree, cp_declarator *, tree);
1417 /* An erroneous declarator. */
1418 static cp_declarator *cp_error_declarator;
1420 /* The obstack on which declarators and related data structures are
1421 allocated. */
1422 static struct obstack declarator_obstack;
1424 /* Alloc BYTES from the declarator memory pool. */
1426 static inline void *
1427 alloc_declarator (size_t bytes)
1429 return obstack_alloc (&declarator_obstack, bytes);
1432 /* Allocate a declarator of the indicated KIND. Clear fields that are
1433 common to all declarators. */
1435 static cp_declarator *
1436 make_declarator (cp_declarator_kind kind)
1438 cp_declarator *declarator;
1440 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1441 declarator->kind = kind;
1442 declarator->parenthesized = UNKNOWN_LOCATION;
1443 declarator->attributes = NULL_TREE;
1444 declarator->std_attributes = NULL_TREE;
1445 declarator->declarator = NULL;
1446 declarator->parameter_pack_p = false;
1447 declarator->id_loc = UNKNOWN_LOCATION;
1449 return declarator;
1452 /* Make a declarator for a generalized identifier. If
1453 QUALIFYING_SCOPE is non-NULL, the identifier is
1454 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1455 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1456 is, if any. */
1458 static cp_declarator *
1459 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1460 special_function_kind sfk, location_t id_location)
1462 cp_declarator *declarator;
1464 /* It is valid to write:
1466 class C { void f(); };
1467 typedef C D;
1468 void D::f();
1470 The standard is not clear about whether `typedef const C D' is
1471 legal; as of 2002-09-15 the committee is considering that
1472 question. EDG 3.0 allows that syntax. Therefore, we do as
1473 well. */
1474 if (qualifying_scope && TYPE_P (qualifying_scope))
1475 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1477 gcc_assert (identifier_p (unqualified_name)
1478 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1479 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1481 declarator = make_declarator (cdk_id);
1482 declarator->u.id.qualifying_scope = qualifying_scope;
1483 declarator->u.id.unqualified_name = unqualified_name;
1484 declarator->u.id.sfk = sfk;
1485 declarator->id_loc = id_location;
1487 return declarator;
1490 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1491 of modifiers such as const or volatile to apply to the pointer
1492 type, represented as identifiers. ATTRIBUTES represent the attributes that
1493 appertain to the pointer or reference. */
1495 cp_declarator *
1496 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1497 tree attributes)
1499 cp_declarator *declarator;
1501 declarator = make_declarator (cdk_pointer);
1502 declarator->declarator = target;
1503 declarator->u.pointer.qualifiers = cv_qualifiers;
1504 declarator->u.pointer.class_type = NULL_TREE;
1505 if (target)
1507 declarator->id_loc = target->id_loc;
1508 declarator->parameter_pack_p = target->parameter_pack_p;
1509 target->parameter_pack_p = false;
1511 else
1512 declarator->parameter_pack_p = false;
1514 declarator->std_attributes = attributes;
1516 return declarator;
1519 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1520 represent the attributes that appertain to the pointer or
1521 reference. */
1523 cp_declarator *
1524 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1525 bool rvalue_ref, tree attributes)
1527 cp_declarator *declarator;
1529 declarator = make_declarator (cdk_reference);
1530 declarator->declarator = target;
1531 declarator->u.reference.qualifiers = cv_qualifiers;
1532 declarator->u.reference.rvalue_ref = rvalue_ref;
1533 if (target)
1535 declarator->id_loc = target->id_loc;
1536 declarator->parameter_pack_p = target->parameter_pack_p;
1537 target->parameter_pack_p = false;
1539 else
1540 declarator->parameter_pack_p = false;
1542 declarator->std_attributes = attributes;
1544 return declarator;
1547 /* Like make_pointer_declarator -- but for a pointer to a non-static
1548 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1549 appertain to the pointer or reference. */
1551 cp_declarator *
1552 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1553 cp_declarator *pointee,
1554 tree attributes)
1556 cp_declarator *declarator;
1558 declarator = make_declarator (cdk_ptrmem);
1559 declarator->declarator = pointee;
1560 declarator->u.pointer.qualifiers = cv_qualifiers;
1561 declarator->u.pointer.class_type = class_type;
1563 if (pointee)
1565 declarator->parameter_pack_p = pointee->parameter_pack_p;
1566 pointee->parameter_pack_p = false;
1568 else
1569 declarator->parameter_pack_p = false;
1571 declarator->std_attributes = attributes;
1573 return declarator;
1576 /* Make a declarator for the function given by TARGET, with the
1577 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1578 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1579 indicates what exceptions can be thrown. */
1581 cp_declarator *
1582 make_call_declarator (cp_declarator *target,
1583 tree parms,
1584 cp_cv_quals cv_qualifiers,
1585 cp_virt_specifiers virt_specifiers,
1586 cp_ref_qualifier ref_qualifier,
1587 tree tx_qualifier,
1588 tree exception_specification,
1589 tree late_return_type,
1590 tree requires_clause)
1592 cp_declarator *declarator;
1594 declarator = make_declarator (cdk_function);
1595 declarator->declarator = target;
1596 declarator->u.function.parameters = parms;
1597 declarator->u.function.qualifiers = cv_qualifiers;
1598 declarator->u.function.virt_specifiers = virt_specifiers;
1599 declarator->u.function.ref_qualifier = ref_qualifier;
1600 declarator->u.function.tx_qualifier = tx_qualifier;
1601 declarator->u.function.exception_specification = exception_specification;
1602 declarator->u.function.late_return_type = late_return_type;
1603 declarator->u.function.requires_clause = requires_clause;
1604 if (target)
1606 declarator->id_loc = target->id_loc;
1607 declarator->parameter_pack_p = target->parameter_pack_p;
1608 target->parameter_pack_p = false;
1610 else
1611 declarator->parameter_pack_p = false;
1613 return declarator;
1616 /* Make a declarator for an array of BOUNDS elements, each of which is
1617 defined by ELEMENT. */
1619 cp_declarator *
1620 make_array_declarator (cp_declarator *element, tree bounds)
1622 cp_declarator *declarator;
1624 declarator = make_declarator (cdk_array);
1625 declarator->declarator = element;
1626 declarator->u.array.bounds = bounds;
1627 if (element)
1629 declarator->id_loc = element->id_loc;
1630 declarator->parameter_pack_p = element->parameter_pack_p;
1631 element->parameter_pack_p = false;
1633 else
1634 declarator->parameter_pack_p = false;
1636 return declarator;
1639 /* Determine whether the declarator we've seen so far can be a
1640 parameter pack, when followed by an ellipsis. */
1641 static bool
1642 declarator_can_be_parameter_pack (cp_declarator *declarator)
1644 if (declarator && declarator->parameter_pack_p)
1645 /* We already saw an ellipsis. */
1646 return false;
1648 /* Search for a declarator name, or any other declarator that goes
1649 after the point where the ellipsis could appear in a parameter
1650 pack. If we find any of these, then this declarator cannot be
1651 made into a parameter pack. */
1652 bool found = false;
1653 while (declarator && !found)
1655 switch ((int)declarator->kind)
1657 case cdk_id:
1658 case cdk_array:
1659 case cdk_decomp:
1660 found = true;
1661 break;
1663 case cdk_error:
1664 return true;
1666 default:
1667 declarator = declarator->declarator;
1668 break;
1672 return !found;
1675 cp_parameter_declarator *no_parameters;
1677 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1678 DECLARATOR and DEFAULT_ARGUMENT. */
1680 cp_parameter_declarator *
1681 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1682 cp_declarator *declarator,
1683 tree default_argument,
1684 location_t loc,
1685 bool template_parameter_pack_p = false)
1687 cp_parameter_declarator *parameter;
1689 parameter = ((cp_parameter_declarator *)
1690 alloc_declarator (sizeof (cp_parameter_declarator)));
1691 parameter->next = NULL;
1692 if (decl_specifiers)
1693 parameter->decl_specifiers = *decl_specifiers;
1694 else
1695 clear_decl_specs (&parameter->decl_specifiers);
1696 parameter->declarator = declarator;
1697 parameter->default_argument = default_argument;
1698 parameter->template_parameter_pack_p = template_parameter_pack_p;
1699 parameter->loc = loc;
1701 return parameter;
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1709 while (declarator)
1711 if (declarator->kind == cdk_function
1712 && declarator->declarator->kind == cdk_id)
1713 return true;
1714 if (declarator->kind == cdk_id
1715 || declarator->kind == cdk_decomp
1716 || declarator->kind == cdk_error)
1717 return false;
1718 declarator = declarator->declarator;
1720 return false;
1723 /* The parser. */
1725 /* Overview
1726 --------
1728 A cp_parser parses the token stream as specified by the C++
1729 grammar. Its job is purely parsing, not semantic analysis. For
1730 example, the parser breaks the token stream into declarators,
1731 expressions, statements, and other similar syntactic constructs.
1732 It does not check that the types of the expressions on either side
1733 of an assignment-statement are compatible, or that a function is
1734 not declared with a parameter of type `void'.
1736 The parser invokes routines elsewhere in the compiler to perform
1737 semantic analysis and to build up the abstract syntax tree for the
1738 code processed.
1740 The parser (and the template instantiation code, which is, in a
1741 way, a close relative of parsing) are the only parts of the
1742 compiler that should be calling push_scope and pop_scope, or
1743 related functions. The parser (and template instantiation code)
1744 keeps track of what scope is presently active; everything else
1745 should simply honor that. (The code that generates static
1746 initializers may also need to set the scope, in order to check
1747 access control correctly when emitting the initializers.)
1749 Methodology
1750 -----------
1752 The parser is of the standard recursive-descent variety. Upcoming
1753 tokens in the token stream are examined in order to determine which
1754 production to use when parsing a non-terminal. Some C++ constructs
1755 require arbitrary look ahead to disambiguate. For example, it is
1756 impossible, in the general case, to tell whether a statement is an
1757 expression or declaration without scanning the entire statement.
1758 Therefore, the parser is capable of "parsing tentatively." When the
1759 parser is not sure what construct comes next, it enters this mode.
1760 Then, while we attempt to parse the construct, the parser queues up
1761 error messages, rather than issuing them immediately, and saves the
1762 tokens it consumes. If the construct is parsed successfully, the
1763 parser "commits", i.e., it issues any queued error messages and
1764 the tokens that were being preserved are permanently discarded.
1765 If, however, the construct is not parsed successfully, the parser
1766 rolls back its state completely so that it can resume parsing using
1767 a different alternative.
1769 Future Improvements
1770 -------------------
1772 The performance of the parser could probably be improved substantially.
1773 We could often eliminate the need to parse tentatively by looking ahead
1774 a little bit. In some places, this approach might not entirely eliminate
1775 the need to parse tentatively, but it might still speed up the average
1776 case. */
1778 /* Flags that are passed to some parsing functions. These values can
1779 be bitwise-ored together. */
1781 enum
1783 /* No flags. */
1784 CP_PARSER_FLAGS_NONE = 0x0,
1785 /* The construct is optional. If it is not present, then no error
1786 should be issued. */
1787 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1788 /* When parsing a type-specifier, treat user-defined type-names
1789 as non-type identifiers. */
1790 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1791 /* When parsing a type-specifier, do not try to parse a class-specifier
1792 or enum-specifier. */
1793 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1794 /* When parsing a decl-specifier-seq, only allow type-specifier or
1795 constexpr. */
1796 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1797 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1798 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1799 /* When parsing a decl-specifier-seq, allow missing typename. */
1800 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1803 /* This type is used for parameters and variables which hold
1804 combinations of the above flags. */
1805 typedef int cp_parser_flags;
1807 /* The different kinds of declarators we want to parse. */
1809 enum cp_parser_declarator_kind
1811 /* We want an abstract declarator. */
1812 CP_PARSER_DECLARATOR_ABSTRACT,
1813 /* We want a named declarator. */
1814 CP_PARSER_DECLARATOR_NAMED,
1815 /* We don't mind, but the name must be an unqualified-id. */
1816 CP_PARSER_DECLARATOR_EITHER
1819 /* The precedence values used to parse binary expressions. The minimum value
1820 of PREC must be 1, because zero is reserved to quickly discriminate
1821 binary operators from other tokens. */
1823 enum cp_parser_prec
1825 PREC_NOT_OPERATOR,
1826 PREC_LOGICAL_OR_EXPRESSION,
1827 PREC_LOGICAL_AND_EXPRESSION,
1828 PREC_INCLUSIVE_OR_EXPRESSION,
1829 PREC_EXCLUSIVE_OR_EXPRESSION,
1830 PREC_AND_EXPRESSION,
1831 PREC_EQUALITY_EXPRESSION,
1832 PREC_RELATIONAL_EXPRESSION,
1833 PREC_SHIFT_EXPRESSION,
1834 PREC_ADDITIVE_EXPRESSION,
1835 PREC_MULTIPLICATIVE_EXPRESSION,
1836 PREC_PM_EXPRESSION,
1837 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 /* A mapping from a token type to a corresponding tree node type, with a
1841 precedence value. */
1843 struct cp_parser_binary_operations_map_node
1845 /* The token type. */
1846 enum cpp_ttype token_type;
1847 /* The corresponding tree code. */
1848 enum tree_code tree_type;
1849 /* The precedence of this operator. */
1850 enum cp_parser_prec prec;
1853 struct cp_parser_expression_stack_entry
1855 /* Left hand side of the binary operation we are currently
1856 parsing. */
1857 cp_expr lhs;
1858 /* Original tree code for left hand side, if it was a binary
1859 expression itself (used for -Wparentheses). */
1860 enum tree_code lhs_type;
1861 /* Tree code for the binary operation we are parsing. */
1862 enum tree_code tree_type;
1863 /* Precedence of the binary operation we are parsing. */
1864 enum cp_parser_prec prec;
1865 /* Location of the binary operation we are parsing. */
1866 location_t loc;
1869 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1870 entries because precedence levels on the stack are monotonically
1871 increasing. */
1872 typedef struct cp_parser_expression_stack_entry
1873 cp_parser_expression_stack[NUM_PREC_VALUES];
1875 /* Prototypes. */
1877 /* Constructors and destructors. */
1879 static cp_parser_context *cp_parser_context_new
1880 (cp_parser_context *);
1882 /* Class variables. */
1884 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1886 /* The operator-precedence table used by cp_parser_binary_expression.
1887 Transformed into an associative array (binops_by_token) by
1888 cp_parser_new. */
1890 static const cp_parser_binary_operations_map_node binops[] = {
1891 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1892 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1894 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1895 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1896 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1899 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1902 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1905 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1906 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1910 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1914 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1916 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1918 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1920 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 /* The same as binops, but initialized by cp_parser_new so that
1924 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1925 for speed. */
1926 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1928 /* Constructors and destructors. */
1930 /* Construct a new context. The context below this one on the stack
1931 is given by NEXT. */
1933 static cp_parser_context *
1934 cp_parser_context_new (cp_parser_context* next)
1936 cp_parser_context *context;
1938 /* Allocate the storage. */
1939 if (cp_parser_context_free_list != NULL)
1941 /* Pull the first entry from the free list. */
1942 context = cp_parser_context_free_list;
1943 cp_parser_context_free_list = context->next;
1944 memset (context, 0, sizeof (*context));
1946 else
1947 context = ggc_cleared_alloc<cp_parser_context> ();
1949 /* No errors have occurred yet in this context. */
1950 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1951 /* If this is not the bottommost context, copy information that we
1952 need from the previous context. */
1953 if (next)
1955 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1956 expression, then we are parsing one in this context, too. */
1957 context->object_type = next->object_type;
1958 /* Thread the stack. */
1959 context->next = next;
1962 return context;
1965 /* Managing the unparsed function queues. */
1967 #define unparsed_funs_with_default_args \
1968 parser->unparsed_queues->last ().funs_with_default_args
1969 #define unparsed_funs_with_definitions \
1970 parser->unparsed_queues->last ().funs_with_definitions
1971 #define unparsed_nsdmis \
1972 parser->unparsed_queues->last ().nsdmis
1973 #define unparsed_classes \
1974 parser->unparsed_queues->last ().classes
1976 static void
1977 push_unparsed_function_queues (cp_parser *parser)
1979 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1980 vec_safe_push (parser->unparsed_queues, e);
1983 static void
1984 pop_unparsed_function_queues (cp_parser *parser)
1986 release_tree_vector (unparsed_funs_with_definitions);
1987 parser->unparsed_queues->pop ();
1990 /* Prototypes. */
1992 /* Constructors and destructors. */
1994 static cp_parser *cp_parser_new
1995 (void);
1997 /* Routines to parse various constructs.
1999 Those that return `tree' will return the error_mark_node (rather
2000 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2001 Sometimes, they will return an ordinary node if error-recovery was
2002 attempted, even though a parse error occurred. So, to check
2003 whether or not a parse error occurred, you should always use
2004 cp_parser_error_occurred. If the construct is optional (indicated
2005 either by an `_opt' in the name of the function that does the
2006 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2007 the construct is not present. */
2009 /* Lexical conventions [gram.lex] */
2011 static cp_expr cp_parser_identifier
2012 (cp_parser *);
2013 static cp_expr cp_parser_string_literal
2014 (cp_parser *, bool, bool, bool);
2015 static cp_expr cp_parser_userdef_char_literal
2016 (cp_parser *);
2017 static tree cp_parser_userdef_string_literal
2018 (tree);
2019 static cp_expr cp_parser_userdef_numeric_literal
2020 (cp_parser *);
2022 /* Basic concepts [gram.basic] */
2024 static void cp_parser_translation_unit (cp_parser *);
2026 /* Expressions [gram.expr] */
2028 static cp_expr cp_parser_primary_expression
2029 (cp_parser *, bool, bool, bool, cp_id_kind *);
2030 static cp_expr cp_parser_id_expression
2031 (cp_parser *, bool, bool, bool *, bool, bool);
2032 static cp_expr cp_parser_unqualified_id
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_nested_name_specifier_opt
2035 (cp_parser *, bool, bool, bool, bool, bool = false);
2036 static tree cp_parser_nested_name_specifier
2037 (cp_parser *, bool, bool, bool, bool);
2038 static tree cp_parser_qualifying_entity
2039 (cp_parser *, bool, bool, bool, bool, bool);
2040 static cp_expr cp_parser_postfix_expression
2041 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2042 static tree cp_parser_postfix_open_square_expression
2043 (cp_parser *, tree, bool, bool);
2044 static tree cp_parser_postfix_dot_deref_expression
2045 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2046 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2047 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2048 bool = false);
2049 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2050 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2051 static void cp_parser_pseudo_destructor_name
2052 (cp_parser *, tree, tree *, tree *);
2053 static cp_expr cp_parser_unary_expression
2054 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2055 static enum tree_code cp_parser_unary_operator
2056 (cp_token *);
2057 static tree cp_parser_has_attribute_expression
2058 (cp_parser *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2098 /* Statements [gram.stmt.stmt] */
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool, unsigned short);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool, unsigned short);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool, unsigned short);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2140 /* Declarations [gram.dcl.dcl] */
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_toplevel_declaration
2147 (cp_parser *);
2148 static void cp_parser_block_declaration
2149 (cp_parser *, bool);
2150 static void cp_parser_simple_declaration
2151 (cp_parser *, bool, tree *);
2152 static void cp_parser_decl_specifier_seq
2153 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2154 static tree cp_parser_storage_class_specifier_opt
2155 (cp_parser *);
2156 static tree cp_parser_function_specifier_opt
2157 (cp_parser *, cp_decl_specifier_seq *);
2158 static tree cp_parser_type_specifier
2159 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2160 int *, bool *);
2161 static tree cp_parser_simple_type_specifier
2162 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2163 static tree cp_parser_type_name
2164 (cp_parser *, bool);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202 /* Declarators [gram.dcl.decl] */
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2206 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2207 location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2210 bool, bool, bool);
2211 static cp_declarator *cp_parser_direct_declarator
2212 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2213 bool);
2214 static enum tree_code cp_parser_ptr_operator
2215 (cp_parser *, tree *, cp_cv_quals *, tree *);
2216 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2217 (cp_parser *);
2218 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2219 (cp_parser *);
2220 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_tx_qualifier_opt
2223 (cp_parser *);
2224 static tree cp_parser_late_return_type_opt
2225 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2226 static tree cp_parser_declarator_id
2227 (cp_parser *, bool);
2228 static tree cp_parser_type_id
2229 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2230 static tree cp_parser_template_type_arg
2231 (cp_parser *);
2232 static tree cp_parser_trailing_type_id (cp_parser *);
2233 static tree cp_parser_type_id_1
2234 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2235 static void cp_parser_type_specifier_seq
2236 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2237 static tree cp_parser_parameter_declaration_clause
2238 (cp_parser *, cp_parser_flags);
2239 static tree cp_parser_parameter_declaration_list
2240 (cp_parser *, cp_parser_flags);
2241 static cp_parameter_declarator *cp_parser_parameter_declaration
2242 (cp_parser *, cp_parser_flags, bool, bool *);
2243 static tree cp_parser_default_argument
2244 (cp_parser *, bool);
2245 static void cp_parser_function_body
2246 (cp_parser *, bool);
2247 static tree cp_parser_initializer
2248 (cp_parser *, bool *, bool *, bool = false);
2249 static cp_expr cp_parser_initializer_clause
2250 (cp_parser *, bool *);
2251 static cp_expr cp_parser_braced_list
2252 (cp_parser*, bool*);
2253 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2254 (cp_parser *, bool *);
2256 static void cp_parser_ctor_initializer_opt_and_function_body
2257 (cp_parser *, bool);
2259 static tree cp_parser_late_parsing_omp_declare_simd
2260 (cp_parser *, tree);
2262 static tree cp_parser_late_parsing_oacc_routine
2263 (cp_parser *, tree);
2265 static tree synthesize_implicit_template_parm
2266 (cp_parser *, tree);
2267 static tree finish_fully_implicit_template
2268 (cp_parser *, tree);
2269 static void abort_fully_implicit_template
2270 (cp_parser *);
2272 /* Classes [gram.class] */
2274 static tree cp_parser_class_name
2275 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2277 (cp_parser *);
2278 static tree cp_parser_class_head
2279 (cp_parser *, bool *);
2280 static enum tag_types cp_parser_class_key
2281 (cp_parser *);
2282 static void cp_parser_type_parameter_key
2283 (cp_parser* parser);
2284 static void cp_parser_member_specification_opt
2285 (cp_parser *);
2286 static void cp_parser_member_declaration
2287 (cp_parser *);
2288 static tree cp_parser_pure_specifier
2289 (cp_parser *);
2290 static tree cp_parser_constant_initializer
2291 (cp_parser *);
2293 /* Derived classes [gram.class.derived] */
2295 static tree cp_parser_base_clause
2296 (cp_parser *);
2297 static tree cp_parser_base_specifier
2298 (cp_parser *);
2300 /* Special member functions [gram.special] */
2302 static tree cp_parser_conversion_function_id
2303 (cp_parser *);
2304 static tree cp_parser_conversion_type_id
2305 (cp_parser *);
2306 static cp_declarator *cp_parser_conversion_declarator_opt
2307 (cp_parser *);
2308 static void cp_parser_ctor_initializer_opt
2309 (cp_parser *);
2310 static void cp_parser_mem_initializer_list
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer_id
2315 (cp_parser *);
2317 /* Overloading [gram.over] */
2319 static cp_expr cp_parser_operator_function_id
2320 (cp_parser *);
2321 static cp_expr cp_parser_operator
2322 (cp_parser *, location_t);
2324 /* Templates [gram.temp] */
2326 static void cp_parser_template_declaration
2327 (cp_parser *, bool);
2328 static tree cp_parser_template_parameter_list
2329 (cp_parser *);
2330 static tree cp_parser_template_parameter
2331 (cp_parser *, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser *, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser *, bool, bool, enum tag_types, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2338 static tree cp_parser_template_argument_list
2339 (cp_parser *);
2340 static tree cp_parser_template_argument
2341 (cp_parser *);
2342 static void cp_parser_explicit_instantiation
2343 (cp_parser *);
2344 static void cp_parser_explicit_specialization
2345 (cp_parser *);
2347 /* Exception handling [gram.exception] */
2349 static tree cp_parser_try_block
2350 (cp_parser *);
2351 static void cp_parser_function_try_block
2352 (cp_parser *);
2353 static void cp_parser_handler_seq
2354 (cp_parser *);
2355 static void cp_parser_handler
2356 (cp_parser *);
2357 static tree cp_parser_exception_declaration
2358 (cp_parser *);
2359 static tree cp_parser_throw_expression
2360 (cp_parser *);
2361 static tree cp_parser_exception_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_type_id_list
2364 (cp_parser *);
2366 /* GNU Extensions */
2368 static tree cp_parser_asm_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_asm_operand_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_clobber_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_label_list
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_std_attribute_p
2381 (cp_parser *);
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser *, size_t);
2388 static tree cp_parser_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser *, bool = false);
2394 static tree cp_parser_std_attribute
2395 (cp_parser *, tree);
2396 static tree cp_parser_std_attribute_spec
2397 (cp_parser *);
2398 static tree cp_parser_std_attribute_spec_seq
2399 (cp_parser *);
2400 static size_t cp_parser_skip_attributes_opt
2401 (cp_parser *, size_t);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static void cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static cp_expr cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool, location_t);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2652 /* Concept-related syntactic transformations */
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2681 return parser->context->next != NULL;
2684 /* Returns nonzero if TOKEN is a string literal. */
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2715 return token->keyword == keyword;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2767 return true;
2770 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2771 RT_CLOSE_PAREN. */
2773 static const char *
2774 get_matching_symbol (required_token token_desc)
2776 switch (token_desc)
2778 default:
2779 gcc_unreachable ();
2780 return "";
2781 case RT_CLOSE_BRACE:
2782 return "{";
2783 case RT_CLOSE_PAREN:
2784 return "(";
2788 /* Attempt to convert TOKEN_DESC from a required_token to an
2789 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2791 static enum cpp_ttype
2792 get_required_cpp_ttype (required_token token_desc)
2794 switch (token_desc)
2796 case RT_SEMICOLON:
2797 return CPP_SEMICOLON;
2798 case RT_OPEN_PAREN:
2799 return CPP_OPEN_PAREN;
2800 case RT_CLOSE_BRACE:
2801 return CPP_CLOSE_BRACE;
2802 case RT_OPEN_BRACE:
2803 return CPP_OPEN_BRACE;
2804 case RT_CLOSE_SQUARE:
2805 return CPP_CLOSE_SQUARE;
2806 case RT_OPEN_SQUARE:
2807 return CPP_OPEN_SQUARE;
2808 case RT_COMMA:
2809 return CPP_COMMA;
2810 case RT_COLON:
2811 return CPP_COLON;
2812 case RT_CLOSE_PAREN:
2813 return CPP_CLOSE_PAREN;
2815 default:
2816 /* Use CPP_EOF as a "no completions possible" code. */
2817 return CPP_EOF;
2822 /* Subroutine of cp_parser_error and cp_parser_required_error.
2824 Issue a diagnostic of the form
2825 FILE:LINE: MESSAGE before TOKEN
2826 where TOKEN is the next token in the input stream. MESSAGE
2827 (specified by the caller) is usually of the form "expected
2828 OTHER-TOKEN".
2830 This bypasses the check for tentative passing, and potentially
2831 adds material needed by cp_parser_required_error.
2833 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2834 suggesting insertion of the missing token.
2836 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2837 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2838 location. */
2840 static void
2841 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2842 required_token missing_token_desc,
2843 location_t matching_location)
2845 cp_token *token = cp_lexer_peek_token (parser->lexer);
2846 /* This diagnostic makes more sense if it is tagged to the line
2847 of the token we just peeked at. */
2848 cp_lexer_set_source_position_from_token (token);
2850 if (token->type == CPP_PRAGMA)
2852 error_at (token->location,
2853 "%<#pragma%> is not allowed here");
2854 cp_parser_skip_to_pragma_eol (parser, token);
2855 return;
2858 /* If this is actually a conflict marker, report it as such. */
2859 if (token->type == CPP_LSHIFT
2860 || token->type == CPP_RSHIFT
2861 || token->type == CPP_EQ_EQ)
2863 location_t loc;
2864 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2866 error_at (loc, "version control conflict marker in file");
2867 expanded_location token_exploc = expand_location (token->location);
2868 /* Consume tokens until the end of the source line. */
2869 while (1)
2871 cp_lexer_consume_token (parser->lexer);
2872 cp_token *next = cp_lexer_peek_token (parser->lexer);
2873 if (next == NULL)
2874 break;
2875 expanded_location next_exploc = expand_location (next->location);
2876 if (next_exploc.file != token_exploc.file)
2877 break;
2878 if (next_exploc.line != token_exploc.line)
2879 break;
2881 return;
2885 gcc_rich_location richloc (input_location);
2887 bool added_matching_location = false;
2889 if (missing_token_desc != RT_NONE)
2891 /* Potentially supply a fix-it hint, suggesting to add the
2892 missing token immediately after the *previous* token.
2893 This may move the primary location within richloc. */
2894 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2895 location_t prev_token_loc
2896 = cp_lexer_previous_token (parser->lexer)->location;
2897 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2899 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2900 Attempt to consolidate diagnostics by printing it as a
2901 secondary range within the main diagnostic. */
2902 if (matching_location != UNKNOWN_LOCATION)
2903 added_matching_location
2904 = richloc.add_location_if_nearby (matching_location);
2907 /* Actually emit the error. */
2908 c_parse_error (gmsgid,
2909 /* Because c_parser_error does not understand
2910 CPP_KEYWORD, keywords are treated like
2911 identifiers. */
2912 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2913 token->u.value, token->flags, &richloc);
2915 if (missing_token_desc != RT_NONE)
2917 /* If we weren't able to consolidate matching_location, then
2918 print it as a secondary diagnostic. */
2919 if (matching_location != UNKNOWN_LOCATION
2920 && !added_matching_location)
2921 inform (matching_location, "to match this %qs",
2922 get_matching_symbol (missing_token_desc));
2926 /* If not parsing tentatively, issue a diagnostic of the form
2927 FILE:LINE: MESSAGE before TOKEN
2928 where TOKEN is the next token in the input stream. MESSAGE
2929 (specified by the caller) is usually of the form "expected
2930 OTHER-TOKEN". */
2932 static void
2933 cp_parser_error (cp_parser* parser, const char* gmsgid)
2935 if (!cp_parser_simulate_error (parser))
2936 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2939 /* Issue an error about name-lookup failing. NAME is the
2940 IDENTIFIER_NODE DECL is the result of
2941 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2942 the thing that we hoped to find. */
2944 static void
2945 cp_parser_name_lookup_error (cp_parser* parser,
2946 tree name,
2947 tree decl,
2948 name_lookup_error desired,
2949 location_t location)
2951 /* If name lookup completely failed, tell the user that NAME was not
2952 declared. */
2953 if (decl == error_mark_node)
2955 if (parser->scope && parser->scope != global_namespace)
2956 error_at (location, "%<%E::%E%> has not been declared",
2957 parser->scope, name);
2958 else if (parser->scope == global_namespace)
2959 error_at (location, "%<::%E%> has not been declared", name);
2960 else if (parser->object_scope
2961 && !CLASS_TYPE_P (parser->object_scope))
2962 error_at (location, "request for member %qE in non-class type %qT",
2963 name, parser->object_scope);
2964 else if (parser->object_scope)
2965 error_at (location, "%<%T::%E%> has not been declared",
2966 parser->object_scope, name);
2967 else
2968 error_at (location, "%qE has not been declared", name);
2970 else if (parser->scope && parser->scope != global_namespace)
2972 switch (desired)
2974 case NLE_TYPE:
2975 error_at (location, "%<%E::%E%> is not a type",
2976 parser->scope, name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<%E::%E%> is not a class or namespace",
2980 parser->scope, name);
2981 break;
2982 case NLE_NOT_CXX98:
2983 error_at (location,
2984 "%<%E::%E%> is not a class, namespace, or enumeration",
2985 parser->scope, name);
2986 break;
2987 default:
2988 gcc_unreachable ();
2992 else if (parser->scope == global_namespace)
2994 switch (desired)
2996 case NLE_TYPE:
2997 error_at (location, "%<::%E%> is not a type", name);
2998 break;
2999 case NLE_CXX98:
3000 error_at (location, "%<::%E%> is not a class or namespace", name);
3001 break;
3002 case NLE_NOT_CXX98:
3003 error_at (location,
3004 "%<::%E%> is not a class, namespace, or enumeration",
3005 name);
3006 break;
3007 default:
3008 gcc_unreachable ();
3011 else
3013 switch (desired)
3015 case NLE_TYPE:
3016 error_at (location, "%qE is not a type", name);
3017 break;
3018 case NLE_CXX98:
3019 error_at (location, "%qE is not a class or namespace", name);
3020 break;
3021 case NLE_NOT_CXX98:
3022 error_at (location,
3023 "%qE is not a class, namespace, or enumeration", name);
3024 break;
3025 default:
3026 gcc_unreachable ();
3031 /* If we are parsing tentatively, remember that an error has occurred
3032 during this tentative parse. Returns true if the error was
3033 simulated; false if a message should be issued by the caller. */
3035 static bool
3036 cp_parser_simulate_error (cp_parser* parser)
3038 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3040 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3041 return true;
3043 return false;
3046 /* This function is called when a type is defined. If type
3047 definitions are forbidden at this point, an error message is
3048 issued. */
3050 static bool
3051 cp_parser_check_type_definition (cp_parser* parser)
3053 /* If types are forbidden here, issue a message. */
3054 if (parser->type_definition_forbidden_message)
3056 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3057 in the message need to be interpreted. */
3058 error (parser->type_definition_forbidden_message);
3059 return false;
3061 return true;
3064 /* This function is called when the DECLARATOR is processed. The TYPE
3065 was a type defined in the decl-specifiers. If it is invalid to
3066 define a type in the decl-specifiers for DECLARATOR, an error is
3067 issued. TYPE_LOCATION is the location of TYPE and is used
3068 for error reporting. */
3070 static void
3071 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3072 tree type, location_t type_location)
3074 /* [dcl.fct] forbids type definitions in return types.
3075 Unfortunately, it's not easy to know whether or not we are
3076 processing a return type until after the fact. */
3077 while (declarator
3078 && (declarator->kind == cdk_pointer
3079 || declarator->kind == cdk_reference
3080 || declarator->kind == cdk_ptrmem))
3081 declarator = declarator->declarator;
3082 if (declarator
3083 && declarator->kind == cdk_function)
3085 error_at (type_location,
3086 "new types may not be defined in a return type");
3087 inform (type_location,
3088 "(perhaps a semicolon is missing after the definition of %qT)",
3089 type);
3093 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3094 "<" in any valid C++ program. If the next token is indeed "<",
3095 issue a message warning the user about what appears to be an
3096 invalid attempt to form a template-id. LOCATION is the location
3097 of the type-specifier (TYPE) */
3099 static void
3100 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3101 tree type,
3102 enum tag_types tag_type,
3103 location_t location)
3105 cp_token_position start = 0;
3107 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3109 if (TREE_CODE (type) == TYPE_DECL)
3110 type = TREE_TYPE (type);
3111 if (TYPE_P (type) && !template_placeholder_p (type))
3112 error_at (location, "%qT is not a template", type);
3113 else if (identifier_p (type))
3115 if (tag_type != none_type)
3116 error_at (location, "%qE is not a class template", type);
3117 else
3118 error_at (location, "%qE is not a template", type);
3120 else
3121 error_at (location, "invalid template-id");
3122 /* Remember the location of the invalid "<". */
3123 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3124 start = cp_lexer_token_position (parser->lexer, true);
3125 /* Consume the "<". */
3126 cp_lexer_consume_token (parser->lexer);
3127 /* Parse the template arguments. */
3128 cp_parser_enclosed_template_argument_list (parser);
3129 /* Permanently remove the invalid template arguments so that
3130 this error message is not issued again. */
3131 if (start)
3132 cp_lexer_purge_tokens_after (parser->lexer, start);
3136 /* If parsing an integral constant-expression, issue an error message
3137 about the fact that THING appeared and return true. Otherwise,
3138 return false. In either case, set
3139 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3141 static bool
3142 cp_parser_non_integral_constant_expression (cp_parser *parser,
3143 non_integral_constant thing)
3145 parser->non_integral_constant_expression_p = true;
3146 if (parser->integral_constant_expression_p)
3148 if (!parser->allow_non_integral_constant_expression_p)
3150 const char *msg = NULL;
3151 switch (thing)
3153 case NIC_FLOAT:
3154 pedwarn (input_location, OPT_Wpedantic,
3155 "ISO C++ forbids using a floating-point literal "
3156 "in a constant-expression");
3157 return true;
3158 case NIC_CAST:
3159 error ("a cast to a type other than an integral or "
3160 "enumeration type cannot appear in a "
3161 "constant-expression");
3162 return true;
3163 case NIC_TYPEID:
3164 error ("%<typeid%> operator "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_NCC:
3168 error ("non-constant compound literals "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_FUNC_CALL:
3172 error ("a function call "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_INC:
3176 error ("an increment "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_DEC:
3180 error ("an decrement "
3181 "cannot appear in a constant-expression");
3182 return true;
3183 case NIC_ARRAY_REF:
3184 error ("an array reference "
3185 "cannot appear in a constant-expression");
3186 return true;
3187 case NIC_ADDR_LABEL:
3188 error ("the address of a label "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_OVERLOADED:
3192 error ("calls to overloaded operators "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_ASSIGNMENT:
3196 error ("an assignment cannot appear in a constant-expression");
3197 return true;
3198 case NIC_COMMA:
3199 error ("a comma operator "
3200 "cannot appear in a constant-expression");
3201 return true;
3202 case NIC_CONSTRUCTOR:
3203 error ("a call to a constructor "
3204 "cannot appear in a constant-expression");
3205 return true;
3206 case NIC_TRANSACTION:
3207 error ("a transaction expression "
3208 "cannot appear in a constant-expression");
3209 return true;
3210 case NIC_THIS:
3211 msg = "this";
3212 break;
3213 case NIC_FUNC_NAME:
3214 msg = "__FUNCTION__";
3215 break;
3216 case NIC_PRETTY_FUNC:
3217 msg = "__PRETTY_FUNCTION__";
3218 break;
3219 case NIC_C99_FUNC:
3220 msg = "__func__";
3221 break;
3222 case NIC_VA_ARG:
3223 msg = "va_arg";
3224 break;
3225 case NIC_ARROW:
3226 msg = "->";
3227 break;
3228 case NIC_POINT:
3229 msg = ".";
3230 break;
3231 case NIC_STAR:
3232 msg = "*";
3233 break;
3234 case NIC_ADDR:
3235 msg = "&";
3236 break;
3237 case NIC_PREINCREMENT:
3238 msg = "++";
3239 break;
3240 case NIC_PREDECREMENT:
3241 msg = "--";
3242 break;
3243 case NIC_NEW:
3244 msg = "new";
3245 break;
3246 case NIC_DEL:
3247 msg = "delete";
3248 break;
3249 default:
3250 gcc_unreachable ();
3252 if (msg)
3253 error ("%qs cannot appear in a constant-expression", msg);
3254 return true;
3257 return false;
3260 /* Emit a diagnostic for an invalid type name. This function commits
3261 to the current active tentative parse, if any. (Otherwise, the
3262 problematic construct might be encountered again later, resulting
3263 in duplicate error messages.) LOCATION is the location of ID. */
3265 static void
3266 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3267 location_t location)
3269 tree decl, ambiguous_decls;
3270 cp_parser_commit_to_tentative_parse (parser);
3271 /* Try to lookup the identifier. */
3272 decl = cp_parser_lookup_name (parser, id, none_type,
3273 /*is_template=*/false,
3274 /*is_namespace=*/false,
3275 /*check_dependency=*/true,
3276 &ambiguous_decls, location);
3277 if (ambiguous_decls)
3278 /* If the lookup was ambiguous, an error will already have
3279 been issued. */
3280 return;
3281 /* If the lookup found a template-name, it means that the user forgot
3282 to specify an argument list. Emit a useful error message. */
3283 if (DECL_TYPE_TEMPLATE_P (decl))
3285 auto_diagnostic_group d;
3286 error_at (location,
3287 "invalid use of template-name %qE without an argument list",
3288 decl);
3289 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3290 inform (location, "class template argument deduction is only available "
3291 "with -std=c++17 or -std=gnu++17");
3292 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3294 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3295 error_at (location, "invalid use of destructor %qD as a type", id);
3296 else if (TREE_CODE (decl) == TYPE_DECL)
3297 /* Something like 'unsigned A a;' */
3298 error_at (location, "invalid combination of multiple type-specifiers");
3299 else if (!parser->scope)
3301 /* Issue an error message. */
3302 auto_diagnostic_group d;
3303 name_hint hint;
3304 if (TREE_CODE (id) == IDENTIFIER_NODE)
3305 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3306 if (const char *suggestion = hint.suggestion ())
3308 gcc_rich_location richloc (location);
3309 richloc.add_fixit_replace (suggestion);
3310 error_at (&richloc,
3311 "%qE does not name a type; did you mean %qs?",
3312 id, suggestion);
3314 else
3315 error_at (location, "%qE does not name a type", id);
3316 /* If we're in a template class, it's possible that the user was
3317 referring to a type from a base class. For example:
3319 template <typename T> struct A { typedef T X; };
3320 template <typename T> struct B : public A<T> { X x; };
3322 The user should have said "typename A<T>::X". */
3323 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3324 inform (location, "C++11 %<constexpr%> only available with "
3325 "-std=c++11 or -std=gnu++11");
3326 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3327 inform (location, "C++11 %<noexcept%> only available with "
3328 "-std=c++11 or -std=gnu++11");
3329 else if (cxx_dialect < cxx11
3330 && TREE_CODE (id) == IDENTIFIER_NODE
3331 && id_equal (id, "thread_local"))
3332 inform (location, "C++11 %<thread_local%> only available with "
3333 "-std=c++11 or -std=gnu++11");
3334 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3335 inform (location, "%<concept%> only available with -fconcepts");
3336 else if (processing_template_decl && current_class_type
3337 && TYPE_BINFO (current_class_type))
3339 tree b;
3341 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3343 b = TREE_CHAIN (b))
3345 tree base_type = BINFO_TYPE (b);
3346 if (CLASS_TYPE_P (base_type)
3347 && dependent_type_p (base_type))
3349 tree field;
3350 /* Go from a particular instantiation of the
3351 template (which will have an empty TYPE_FIELDs),
3352 to the main version. */
3353 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3354 for (field = TYPE_FIELDS (base_type);
3355 field;
3356 field = DECL_CHAIN (field))
3357 if (TREE_CODE (field) == TYPE_DECL
3358 && DECL_NAME (field) == id)
3360 inform (location,
3361 "(perhaps %<typename %T::%E%> was intended)",
3362 BINFO_TYPE (b), id);
3363 break;
3365 if (field)
3366 break;
3371 /* Here we diagnose qualified-ids where the scope is actually correct,
3372 but the identifier does not resolve to a valid type name. */
3373 else if (parser->scope != error_mark_node)
3375 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3377 auto_diagnostic_group d;
3378 name_hint hint;
3379 if (decl == error_mark_node)
3380 hint = suggest_alternative_in_explicit_scope (location, id,
3381 parser->scope);
3382 const char *suggestion = hint.suggestion ();
3383 gcc_rich_location richloc (location_of (id));
3384 if (suggestion)
3385 richloc.add_fixit_replace (suggestion);
3386 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3388 if (suggestion)
3389 error_at (&richloc,
3390 "%qE in namespace %qE does not name a template"
3391 " type; did you mean %qs?",
3392 id, parser->scope, suggestion);
3393 else
3394 error_at (&richloc,
3395 "%qE in namespace %qE does not name a template type",
3396 id, parser->scope);
3398 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3400 if (suggestion)
3401 error_at (&richloc,
3402 "%qE in namespace %qE does not name a template"
3403 " type; did you mean %qs?",
3404 TREE_OPERAND (id, 0), parser->scope, suggestion);
3405 else
3406 error_at (&richloc,
3407 "%qE in namespace %qE does not name a template"
3408 " type",
3409 TREE_OPERAND (id, 0), parser->scope);
3411 else
3413 if (suggestion)
3414 error_at (&richloc,
3415 "%qE in namespace %qE does not name a type"
3416 "; did you mean %qs?",
3417 id, parser->scope, suggestion);
3418 else
3419 error_at (&richloc,
3420 "%qE in namespace %qE does not name a type",
3421 id, parser->scope);
3423 if (DECL_P (decl))
3424 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3426 else if (CLASS_TYPE_P (parser->scope)
3427 && constructor_name_p (id, parser->scope))
3429 /* A<T>::A<T>() */
3430 auto_diagnostic_group d;
3431 error_at (location, "%<%T::%E%> names the constructor, not"
3432 " the type", parser->scope, id);
3433 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3434 error_at (location, "and %qT has no template constructors",
3435 parser->scope);
3437 else if (TYPE_P (parser->scope)
3438 && dependent_scope_p (parser->scope))
3440 gcc_rich_location richloc (location);
3441 richloc.add_fixit_insert_before ("typename ");
3442 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3443 error_at (&richloc,
3444 "need %<typename%> before %<%T::%D::%E%> because "
3445 "%<%T::%D%> is a dependent scope",
3446 TYPE_CONTEXT (parser->scope),
3447 TYPENAME_TYPE_FULLNAME (parser->scope),
3449 TYPE_CONTEXT (parser->scope),
3450 TYPENAME_TYPE_FULLNAME (parser->scope));
3451 else
3452 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3453 "%qT is a dependent scope",
3454 parser->scope, id, parser->scope);
3456 else if (TYPE_P (parser->scope))
3458 auto_diagnostic_group d;
3459 if (!COMPLETE_TYPE_P (parser->scope))
3460 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3461 parser->scope);
3462 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3463 error_at (location_of (id),
3464 "%qE in %q#T does not name a template type",
3465 id, parser->scope);
3466 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3467 error_at (location_of (id),
3468 "%qE in %q#T does not name a template type",
3469 TREE_OPERAND (id, 0), parser->scope);
3470 else
3471 error_at (location_of (id),
3472 "%qE in %q#T does not name a type",
3473 id, parser->scope);
3474 if (DECL_P (decl))
3475 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3477 else
3478 gcc_unreachable ();
3482 /* Check for a common situation where a type-name should be present,
3483 but is not, and issue a sensible error message. Returns true if an
3484 invalid type-name was detected.
3486 The situation handled by this function are variable declarations of the
3487 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3488 Usually, `ID' should name a type, but if we got here it means that it
3489 does not. We try to emit the best possible error message depending on
3490 how exactly the id-expression looks like. */
3492 static bool
3493 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3495 tree id;
3496 cp_token *token = cp_lexer_peek_token (parser->lexer);
3498 /* Avoid duplicate error about ambiguous lookup. */
3499 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3501 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3502 if (next->type == CPP_NAME && next->error_reported)
3503 goto out;
3506 cp_parser_parse_tentatively (parser);
3507 id = cp_parser_id_expression (parser,
3508 /*template_keyword_p=*/false,
3509 /*check_dependency_p=*/true,
3510 /*template_p=*/NULL,
3511 /*declarator_p=*/false,
3512 /*optional_p=*/false);
3513 /* If the next token is a (, this is a function with no explicit return
3514 type, i.e. constructor, destructor or conversion op. */
3515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3516 || TREE_CODE (id) == TYPE_DECL)
3518 cp_parser_abort_tentative_parse (parser);
3519 return false;
3521 if (!cp_parser_parse_definitely (parser))
3522 return false;
3524 /* Emit a diagnostic for the invalid type. */
3525 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3526 out:
3527 /* If we aren't in the middle of a declarator (i.e. in a
3528 parameter-declaration-clause), skip to the end of the declaration;
3529 there's no point in trying to process it. */
3530 if (!parser->in_declarator_p)
3531 cp_parser_skip_to_end_of_block_or_statement (parser);
3532 return true;
3535 /* Consume tokens up to, and including, the next non-nested closing `)'.
3536 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3537 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3538 found an unnested token of that type. */
3540 static int
3541 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3542 bool recovering,
3543 cpp_ttype or_ttype,
3544 bool consume_paren)
3546 unsigned paren_depth = 0;
3547 unsigned brace_depth = 0;
3548 unsigned square_depth = 0;
3549 unsigned condop_depth = 0;
3551 if (recovering && or_ttype == CPP_EOF
3552 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3553 return 0;
3555 while (true)
3557 cp_token * token = cp_lexer_peek_token (parser->lexer);
3559 /* Have we found what we're looking for before the closing paren? */
3560 if (token->type == or_ttype && or_ttype != CPP_EOF
3561 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3562 return -1;
3564 switch (token->type)
3566 case CPP_PRAGMA_EOL:
3567 if (!parser->lexer->in_pragma)
3568 break;
3569 /* FALLTHRU */
3570 case CPP_EOF:
3571 /* If we've run out of tokens, then there is no closing `)'. */
3572 return 0;
3574 /* This is good for lambda expression capture-lists. */
3575 case CPP_OPEN_SQUARE:
3576 ++square_depth;
3577 break;
3578 case CPP_CLOSE_SQUARE:
3579 if (!square_depth--)
3580 return 0;
3581 break;
3583 case CPP_SEMICOLON:
3584 /* This matches the processing in skip_to_end_of_statement. */
3585 if (!brace_depth)
3586 return 0;
3587 break;
3589 case CPP_OPEN_BRACE:
3590 ++brace_depth;
3591 break;
3592 case CPP_CLOSE_BRACE:
3593 if (!brace_depth--)
3594 return 0;
3595 break;
3597 case CPP_OPEN_PAREN:
3598 if (!brace_depth)
3599 ++paren_depth;
3600 break;
3602 case CPP_CLOSE_PAREN:
3603 if (!brace_depth && !paren_depth--)
3605 if (consume_paren)
3606 cp_lexer_consume_token (parser->lexer);
3607 return 1;
3609 break;
3611 case CPP_QUERY:
3612 if (!brace_depth && !paren_depth && !square_depth)
3613 ++condop_depth;
3614 break;
3616 case CPP_COLON:
3617 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3618 condop_depth--;
3619 break;
3621 default:
3622 break;
3625 /* Consume the token. */
3626 cp_lexer_consume_token (parser->lexer);
3630 /* Consume tokens up to, and including, the next non-nested closing `)'.
3631 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3632 are doing error recovery. Returns -1 if OR_COMMA is true and we
3633 found an unnested token of that type. */
3635 static int
3636 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3637 bool recovering,
3638 bool or_comma,
3639 bool consume_paren)
3641 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3642 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3643 ttype, consume_paren);
3646 /* Consume tokens until we reach the end of the current statement.
3647 Normally, that will be just before consuming a `;'. However, if a
3648 non-nested `}' comes first, then we stop before consuming that. */
3650 static void
3651 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3653 unsigned nesting_depth = 0;
3655 /* Unwind generic function template scope if necessary. */
3656 if (parser->fully_implicit_function_template_p)
3657 abort_fully_implicit_template (parser);
3659 while (true)
3661 cp_token *token = cp_lexer_peek_token (parser->lexer);
3663 switch (token->type)
3665 case CPP_PRAGMA_EOL:
3666 if (!parser->lexer->in_pragma)
3667 break;
3668 /* FALLTHRU */
3669 case CPP_EOF:
3670 /* If we've run out of tokens, stop. */
3671 return;
3673 case CPP_SEMICOLON:
3674 /* If the next token is a `;', we have reached the end of the
3675 statement. */
3676 if (!nesting_depth)
3677 return;
3678 break;
3680 case CPP_CLOSE_BRACE:
3681 /* If this is a non-nested '}', stop before consuming it.
3682 That way, when confronted with something like:
3684 { 3 + }
3686 we stop before consuming the closing '}', even though we
3687 have not yet reached a `;'. */
3688 if (nesting_depth == 0)
3689 return;
3691 /* If it is the closing '}' for a block that we have
3692 scanned, stop -- but only after consuming the token.
3693 That way given:
3695 void f g () { ... }
3696 typedef int I;
3698 we will stop after the body of the erroneously declared
3699 function, but before consuming the following `typedef'
3700 declaration. */
3701 if (--nesting_depth == 0)
3703 cp_lexer_consume_token (parser->lexer);
3704 return;
3706 break;
3708 case CPP_OPEN_BRACE:
3709 ++nesting_depth;
3710 break;
3712 default:
3713 break;
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser->lexer);
3721 /* This function is called at the end of a statement or declaration.
3722 If the next token is a semicolon, it is consumed; otherwise, error
3723 recovery is attempted. */
3725 static void
3726 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3728 /* Look for the trailing `;'. */
3729 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3731 /* If there is additional (erroneous) input, skip to the end of
3732 the statement. */
3733 cp_parser_skip_to_end_of_statement (parser);
3734 /* If the next token is now a `;', consume it. */
3735 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3736 cp_lexer_consume_token (parser->lexer);
3740 /* Skip tokens until we have consumed an entire block, or until we
3741 have consumed a non-nested `;'. */
3743 static void
3744 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3746 int nesting_depth = 0;
3748 /* Unwind generic function template scope if necessary. */
3749 if (parser->fully_implicit_function_template_p)
3750 abort_fully_implicit_template (parser);
3752 while (nesting_depth >= 0)
3754 cp_token *token = cp_lexer_peek_token (parser->lexer);
3756 switch (token->type)
3758 case CPP_PRAGMA_EOL:
3759 if (!parser->lexer->in_pragma)
3760 break;
3761 /* FALLTHRU */
3762 case CPP_EOF:
3763 /* If we've run out of tokens, stop. */
3764 return;
3766 case CPP_SEMICOLON:
3767 /* Stop if this is an unnested ';'. */
3768 if (!nesting_depth)
3769 nesting_depth = -1;
3770 break;
3772 case CPP_CLOSE_BRACE:
3773 /* Stop if this is an unnested '}', or closes the outermost
3774 nesting level. */
3775 nesting_depth--;
3776 if (nesting_depth < 0)
3777 return;
3778 if (!nesting_depth)
3779 nesting_depth = -1;
3780 break;
3782 case CPP_OPEN_BRACE:
3783 /* Nest. */
3784 nesting_depth++;
3785 break;
3787 default:
3788 break;
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser->lexer);
3796 /* Skip tokens until a non-nested closing curly brace is the next
3797 token, or there are no more tokens. Return true in the first case,
3798 false otherwise. */
3800 static bool
3801 cp_parser_skip_to_closing_brace (cp_parser *parser)
3803 unsigned nesting_depth = 0;
3805 while (true)
3807 cp_token *token = cp_lexer_peek_token (parser->lexer);
3809 switch (token->type)
3811 case CPP_PRAGMA_EOL:
3812 if (!parser->lexer->in_pragma)
3813 break;
3814 /* FALLTHRU */
3815 case CPP_EOF:
3816 /* If we've run out of tokens, stop. */
3817 return false;
3819 case CPP_CLOSE_BRACE:
3820 /* If the next token is a non-nested `}', then we have reached
3821 the end of the current block. */
3822 if (nesting_depth-- == 0)
3823 return true;
3824 break;
3826 case CPP_OPEN_BRACE:
3827 /* If it the next token is a `{', then we are entering a new
3828 block. Consume the entire block. */
3829 ++nesting_depth;
3830 break;
3832 default:
3833 break;
3836 /* Consume the token. */
3837 cp_lexer_consume_token (parser->lexer);
3841 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3842 parameter is the PRAGMA token, allowing us to purge the entire pragma
3843 sequence. */
3845 static void
3846 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3848 cp_token *token;
3850 parser->lexer->in_pragma = false;
3853 token = cp_lexer_consume_token (parser->lexer);
3854 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3856 /* Ensure that the pragma is not parsed again. */
3857 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3860 /* Require pragma end of line, resyncing with it as necessary. The
3861 arguments are as for cp_parser_skip_to_pragma_eol. */
3863 static void
3864 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3866 parser->lexer->in_pragma = false;
3867 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3871 /* This is a simple wrapper around make_typename_type. When the id is
3872 an unresolved identifier node, we can provide a superior diagnostic
3873 using cp_parser_diagnose_invalid_type_name. */
3875 static tree
3876 cp_parser_make_typename_type (cp_parser *parser, tree id,
3877 location_t id_location)
3879 tree result;
3880 if (identifier_p (id))
3882 result = make_typename_type (parser->scope, id, typename_type,
3883 /*complain=*/tf_none);
3884 if (result == error_mark_node)
3885 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3886 return result;
3888 return make_typename_type (parser->scope, id, typename_type, tf_error);
3891 /* This is a wrapper around the
3892 make_{pointer,ptrmem,reference}_declarator functions that decides
3893 which one to call based on the CODE and CLASS_TYPE arguments. The
3894 CODE argument should be one of the values returned by
3895 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3896 appertain to the pointer or reference. */
3898 static cp_declarator *
3899 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3900 cp_cv_quals cv_qualifiers,
3901 cp_declarator *target,
3902 tree attributes)
3904 if (code == ERROR_MARK || target == cp_error_declarator)
3905 return cp_error_declarator;
3907 if (code == INDIRECT_REF)
3908 if (class_type == NULL_TREE)
3909 return make_pointer_declarator (cv_qualifiers, target, attributes);
3910 else
3911 return make_ptrmem_declarator (cv_qualifiers, class_type,
3912 target, attributes);
3913 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3914 return make_reference_declarator (cv_qualifiers, target,
3915 false, attributes);
3916 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3917 return make_reference_declarator (cv_qualifiers, target,
3918 true, attributes);
3919 gcc_unreachable ();
3922 /* Create a new C++ parser. */
3924 static cp_parser *
3925 cp_parser_new (void)
3927 cp_parser *parser;
3928 cp_lexer *lexer;
3929 unsigned i;
3931 /* cp_lexer_new_main is called before doing GC allocation because
3932 cp_lexer_new_main might load a PCH file. */
3933 lexer = cp_lexer_new_main ();
3935 /* Initialize the binops_by_token so that we can get the tree
3936 directly from the token. */
3937 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3938 binops_by_token[binops[i].token_type] = binops[i];
3940 parser = ggc_cleared_alloc<cp_parser> ();
3941 parser->lexer = lexer;
3942 parser->context = cp_parser_context_new (NULL);
3944 /* For now, we always accept GNU extensions. */
3945 parser->allow_gnu_extensions_p = 1;
3947 /* The `>' token is a greater-than operator, not the end of a
3948 template-id. */
3949 parser->greater_than_is_operator_p = true;
3951 parser->default_arg_ok_p = true;
3953 /* We are not parsing a constant-expression. */
3954 parser->integral_constant_expression_p = false;
3955 parser->allow_non_integral_constant_expression_p = false;
3956 parser->non_integral_constant_expression_p = false;
3958 /* Local variable names are not forbidden. */
3959 parser->local_variables_forbidden_p = 0;
3961 /* We are not processing an `extern "C"' declaration. */
3962 parser->in_unbraced_linkage_specification_p = false;
3964 /* We are not processing a declarator. */
3965 parser->in_declarator_p = false;
3967 /* We are not processing a template-argument-list. */
3968 parser->in_template_argument_list_p = false;
3970 /* We are not in an iteration statement. */
3971 parser->in_statement = 0;
3973 /* We are not in a switch statement. */
3974 parser->in_switch_statement_p = false;
3976 /* We are not parsing a type-id inside an expression. */
3977 parser->in_type_id_in_expr_p = false;
3979 /* String literals should be translated to the execution character set. */
3980 parser->translate_strings_p = true;
3982 /* We are not parsing a function body. */
3983 parser->in_function_body = false;
3985 /* We can correct until told otherwise. */
3986 parser->colon_corrects_to_scope_p = true;
3988 /* The unparsed function queue is empty. */
3989 push_unparsed_function_queues (parser);
3991 /* There are no classes being defined. */
3992 parser->num_classes_being_defined = 0;
3994 /* No template parameters apply. */
3995 parser->num_template_parameter_lists = 0;
3997 /* Special parsing data structures. */
3998 parser->omp_declare_simd = NULL;
3999 parser->oacc_routine = NULL;
4001 /* Not declaring an implicit function template. */
4002 parser->auto_is_implicit_function_template_parm_p = false;
4003 parser->fully_implicit_function_template_p = false;
4004 parser->implicit_template_parms = 0;
4005 parser->implicit_template_scope = 0;
4007 /* Allow constrained-type-specifiers. */
4008 parser->prevent_constrained_type_specifiers = 0;
4010 /* We haven't yet seen an 'extern "C"'. */
4011 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4013 return parser;
4016 /* Create a cp_lexer structure which will emit the tokens in CACHE
4017 and push it onto the parser's lexer stack. This is used for delayed
4018 parsing of in-class method bodies and default arguments, and should
4019 not be confused with tentative parsing. */
4020 static void
4021 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4023 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4024 lexer->next = parser->lexer;
4025 parser->lexer = lexer;
4027 /* Move the current source position to that of the first token in the
4028 new lexer. */
4029 cp_lexer_set_source_position_from_token (lexer->next_token);
4032 /* Pop the top lexer off the parser stack. This is never used for the
4033 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4034 static void
4035 cp_parser_pop_lexer (cp_parser *parser)
4037 cp_lexer *lexer = parser->lexer;
4038 parser->lexer = lexer->next;
4039 cp_lexer_destroy (lexer);
4041 /* Put the current source position back where it was before this
4042 lexer was pushed. */
4043 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4046 /* Lexical conventions [gram.lex] */
4048 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4049 identifier. */
4051 static cp_expr
4052 cp_parser_identifier (cp_parser* parser)
4054 cp_token *token;
4056 /* Look for the identifier. */
4057 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4058 /* Return the value. */
4059 if (token)
4060 return cp_expr (token->u.value, token->location);
4061 else
4062 return error_mark_node;
4065 /* Parse a sequence of adjacent string constants. Returns a
4066 TREE_STRING representing the combined, nul-terminated string
4067 constant. If TRANSLATE is true, translate the string to the
4068 execution character set. If WIDE_OK is true, a wide string is
4069 invalid here.
4071 C++98 [lex.string] says that if a narrow string literal token is
4072 adjacent to a wide string literal token, the behavior is undefined.
4073 However, C99 6.4.5p4 says that this results in a wide string literal.
4074 We follow C99 here, for consistency with the C front end.
4076 This code is largely lifted from lex_string() in c-lex.c.
4078 FUTURE: ObjC++ will need to handle @-strings here. */
4079 static cp_expr
4080 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4081 bool lookup_udlit = true)
4083 tree value;
4084 size_t count;
4085 struct obstack str_ob;
4086 struct obstack loc_ob;
4087 cpp_string str, istr, *strs;
4088 cp_token *tok;
4089 enum cpp_ttype type, curr_type;
4090 int have_suffix_p = 0;
4091 tree string_tree;
4092 tree suffix_id = NULL_TREE;
4093 bool curr_tok_is_userdef_p = false;
4095 tok = cp_lexer_peek_token (parser->lexer);
4096 if (!cp_parser_is_string_literal (tok))
4098 cp_parser_error (parser, "expected string-literal");
4099 return error_mark_node;
4102 location_t loc = tok->location;
4104 if (cpp_userdef_string_p (tok->type))
4106 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4107 curr_type = cpp_userdef_string_remove_type (tok->type);
4108 curr_tok_is_userdef_p = true;
4110 else
4112 string_tree = tok->u.value;
4113 curr_type = tok->type;
4115 type = curr_type;
4117 /* Try to avoid the overhead of creating and destroying an obstack
4118 for the common case of just one string. */
4119 if (!cp_parser_is_string_literal
4120 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4122 cp_lexer_consume_token (parser->lexer);
4124 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4125 str.len = TREE_STRING_LENGTH (string_tree);
4126 count = 1;
4128 if (curr_tok_is_userdef_p)
4130 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4131 have_suffix_p = 1;
4132 curr_type = cpp_userdef_string_remove_type (tok->type);
4134 else
4135 curr_type = tok->type;
4137 strs = &str;
4139 else
4141 location_t last_tok_loc = tok->location;
4142 gcc_obstack_init (&str_ob);
4143 gcc_obstack_init (&loc_ob);
4144 count = 0;
4148 cp_lexer_consume_token (parser->lexer);
4149 count++;
4150 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4151 str.len = TREE_STRING_LENGTH (string_tree);
4153 if (curr_tok_is_userdef_p)
4155 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4156 if (have_suffix_p == 0)
4158 suffix_id = curr_suffix_id;
4159 have_suffix_p = 1;
4161 else if (have_suffix_p == 1
4162 && curr_suffix_id != suffix_id)
4164 error ("inconsistent user-defined literal suffixes"
4165 " %qD and %qD in string literal",
4166 suffix_id, curr_suffix_id);
4167 have_suffix_p = -1;
4169 curr_type = cpp_userdef_string_remove_type (tok->type);
4171 else
4172 curr_type = tok->type;
4174 if (type != curr_type)
4176 if (type == CPP_STRING)
4177 type = curr_type;
4178 else if (curr_type != CPP_STRING)
4180 rich_location rich_loc (line_table, tok->location);
4181 rich_loc.add_range (last_tok_loc);
4182 error_at (&rich_loc,
4183 "unsupported non-standard concatenation "
4184 "of string literals");
4188 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4189 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4191 last_tok_loc = tok->location;
4193 tok = cp_lexer_peek_token (parser->lexer);
4194 if (cpp_userdef_string_p (tok->type))
4196 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4197 curr_type = cpp_userdef_string_remove_type (tok->type);
4198 curr_tok_is_userdef_p = true;
4200 else
4202 string_tree = tok->u.value;
4203 curr_type = tok->type;
4204 curr_tok_is_userdef_p = false;
4207 while (cp_parser_is_string_literal (tok));
4209 /* A string literal built by concatenation has its caret=start at
4210 the start of the initial string, and its finish at the finish of
4211 the final string literal. */
4212 loc = make_location (loc, loc, get_finish (last_tok_loc));
4214 strs = (cpp_string *) obstack_finish (&str_ob);
4217 if (type != CPP_STRING && !wide_ok)
4219 cp_parser_error (parser, "a wide string is invalid in this context");
4220 type = CPP_STRING;
4223 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4224 (parse_in, strs, count, &istr, type))
4226 value = build_string (istr.len, (const char *)istr.text);
4227 free (CONST_CAST (unsigned char *, istr.text));
4228 if (count > 1)
4230 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4231 gcc_assert (g_string_concat_db);
4232 g_string_concat_db->record_string_concatenation (count, locs);
4235 switch (type)
4237 default:
4238 case CPP_STRING:
4239 TREE_TYPE (value) = char_array_type_node;
4240 break;
4241 case CPP_UTF8STRING:
4242 if (flag_char8_t)
4243 TREE_TYPE (value) = char8_array_type_node;
4244 else
4245 TREE_TYPE (value) = char_array_type_node;
4246 break;
4247 case CPP_STRING16:
4248 TREE_TYPE (value) = char16_array_type_node;
4249 break;
4250 case CPP_STRING32:
4251 TREE_TYPE (value) = char32_array_type_node;
4252 break;
4253 case CPP_WSTRING:
4254 TREE_TYPE (value) = wchar_array_type_node;
4255 break;
4258 value = fix_string_type (value);
4260 if (have_suffix_p)
4262 tree literal = build_userdef_literal (suffix_id, value,
4263 OT_NONE, NULL_TREE);
4264 if (lookup_udlit)
4265 value = cp_parser_userdef_string_literal (literal);
4266 else
4267 value = literal;
4270 else
4271 /* cpp_interpret_string has issued an error. */
4272 value = error_mark_node;
4274 if (count > 1)
4276 obstack_free (&str_ob, 0);
4277 obstack_free (&loc_ob, 0);
4280 return cp_expr (value, loc);
4283 /* Look up a literal operator with the name and the exact arguments. */
4285 static tree
4286 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4288 tree decl = lookup_name (name);
4289 if (!decl || !is_overloaded_fn (decl))
4290 return error_mark_node;
4292 for (lkp_iterator iter (decl); iter; ++iter)
4294 tree fn = *iter;
4296 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4298 unsigned int ix;
4299 bool found = true;
4301 for (ix = 0;
4302 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4303 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4305 tree tparm = TREE_VALUE (parmtypes);
4306 tree targ = TREE_TYPE ((*args)[ix]);
4307 bool ptr = TYPE_PTR_P (tparm);
4308 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4309 if ((ptr || arr || !same_type_p (tparm, targ))
4310 && (!ptr || !arr
4311 || !same_type_p (TREE_TYPE (tparm),
4312 TREE_TYPE (targ))))
4313 found = false;
4316 if (found
4317 && ix == vec_safe_length (args)
4318 /* May be this should be sufficient_parms_p instead,
4319 depending on how exactly should user-defined literals
4320 work in presence of default arguments on the literal
4321 operator parameters. */
4322 && parmtypes == void_list_node)
4323 return decl;
4327 return error_mark_node;
4330 /* Parse a user-defined char constant. Returns a call to a user-defined
4331 literal operator taking the character as an argument. */
4333 static cp_expr
4334 cp_parser_userdef_char_literal (cp_parser *parser)
4336 cp_token *token = cp_lexer_consume_token (parser->lexer);
4337 tree literal = token->u.value;
4338 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4339 tree value = USERDEF_LITERAL_VALUE (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4343 /* Build up a call to the user-defined operator */
4344 /* Lookup the name we got back from the id-expression. */
4345 vec<tree, va_gc> *args = make_tree_vector ();
4346 vec_safe_push (args, value);
4347 decl = lookup_literal_operator (name, args);
4348 if (!decl || decl == error_mark_node)
4350 error ("unable to find character literal operator %qD with %qT argument",
4351 name, TREE_TYPE (value));
4352 release_tree_vector (args);
4353 return error_mark_node;
4355 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4356 release_tree_vector (args);
4357 return result;
4360 /* A subroutine of cp_parser_userdef_numeric_literal to
4361 create a char... template parameter pack from a string node. */
4363 static tree
4364 make_char_string_pack (tree value)
4366 tree charvec;
4367 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4368 const char *str = TREE_STRING_POINTER (value);
4369 int i, len = TREE_STRING_LENGTH (value) - 1;
4370 tree argvec = make_tree_vec (1);
4372 /* Fill in CHARVEC with all of the parameters. */
4373 charvec = make_tree_vec (len);
4374 for (i = 0; i < len; ++i)
4376 unsigned char s[3] = { '\'', str[i], '\'' };
4377 cpp_string in = { 3, s };
4378 cpp_string out = { 0, 0 };
4379 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4380 return NULL_TREE;
4381 gcc_assert (out.len == 2);
4382 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4383 out.text[0]);
4386 /* Build the argument packs. */
4387 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4389 TREE_VEC_ELT (argvec, 0) = argpack;
4391 return argvec;
4394 /* A subroutine of cp_parser_userdef_numeric_literal to
4395 create a char... template parameter pack from a string node. */
4397 static tree
4398 make_string_pack (tree value)
4400 tree charvec;
4401 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4402 const unsigned char *str
4403 = (const unsigned char *) TREE_STRING_POINTER (value);
4404 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4405 int len = TREE_STRING_LENGTH (value) / sz - 1;
4406 tree argvec = make_tree_vec (2);
4408 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4409 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4411 /* First template parm is character type. */
4412 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4414 /* Fill in CHARVEC with all of the parameters. */
4415 charvec = make_tree_vec (len);
4416 for (int i = 0; i < len; ++i)
4417 TREE_VEC_ELT (charvec, i)
4418 = double_int_to_tree (str_char_type_node,
4419 double_int::from_buffer (str + i * sz, sz));
4421 /* Build the argument packs. */
4422 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4424 TREE_VEC_ELT (argvec, 1) = argpack;
4426 return argvec;
4429 /* Parse a user-defined numeric constant. returns a call to a user-defined
4430 literal operator. */
4432 static cp_expr
4433 cp_parser_userdef_numeric_literal (cp_parser *parser)
4435 cp_token *token = cp_lexer_consume_token (parser->lexer);
4436 tree literal = token->u.value;
4437 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4438 tree value = USERDEF_LITERAL_VALUE (literal);
4439 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4440 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4441 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4442 tree decl, result;
4443 vec<tree, va_gc> *args;
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4447 args = make_tree_vector ();
4448 vec_safe_push (args, value);
4449 decl = lookup_literal_operator (name, args);
4450 if (decl && decl != error_mark_node)
4452 result = finish_call_expr (decl, &args, false, true,
4453 tf_warning_or_error);
4455 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4457 warning_at (token->location, OPT_Woverflow,
4458 "integer literal exceeds range of %qT type",
4459 long_long_unsigned_type_node);
4461 else
4463 if (overflow > 0)
4464 warning_at (token->location, OPT_Woverflow,
4465 "floating literal exceeds range of %qT type",
4466 long_double_type_node);
4467 else if (overflow < 0)
4468 warning_at (token->location, OPT_Woverflow,
4469 "floating literal truncated to zero");
4472 release_tree_vector (args);
4473 return result;
4475 release_tree_vector (args);
4477 /* If the numeric argument didn't work, look for a raw literal
4478 operator taking a const char* argument consisting of the number
4479 in string format. */
4480 args = make_tree_vector ();
4481 vec_safe_push (args, num_string);
4482 decl = lookup_literal_operator (name, args);
4483 if (decl && decl != error_mark_node)
4485 result = finish_call_expr (decl, &args, false, true,
4486 tf_warning_or_error);
4487 release_tree_vector (args);
4488 return result;
4490 release_tree_vector (args);
4492 /* If the raw literal didn't work, look for a non-type template
4493 function with parameter pack char.... Call the function with
4494 template parameter characters representing the number. */
4495 args = make_tree_vector ();
4496 decl = lookup_literal_operator (name, args);
4497 if (decl && decl != error_mark_node)
4499 tree tmpl_args = make_char_string_pack (num_string);
4500 if (tmpl_args == NULL_TREE)
4502 error ("failed to translate literal to execution character set %qT",
4503 num_string);
4504 return error_mark_node;
4506 decl = lookup_template_function (decl, tmpl_args);
4507 result = finish_call_expr (decl, &args, false, true,
4508 tf_warning_or_error);
4509 release_tree_vector (args);
4510 return result;
4513 release_tree_vector (args);
4515 /* In C++14 the standard library defines complex number suffixes that
4516 conflict with GNU extensions. Prefer them if <complex> is #included. */
4517 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4518 bool i14 = (cxx_dialect > cxx11
4519 && (id_equal (suffix_id, "i")
4520 || id_equal (suffix_id, "if")
4521 || id_equal (suffix_id, "il")));
4522 diagnostic_t kind = DK_ERROR;
4523 int opt = 0;
4525 if (i14 && ext)
4527 tree cxlit = lookup_qualified_name (std_node,
4528 get_identifier ("complex_literals"),
4529 0, false, false);
4530 if (cxlit == error_mark_node)
4532 /* No <complex>, so pedwarn and use GNU semantics. */
4533 kind = DK_PEDWARN;
4534 opt = OPT_Wpedantic;
4538 bool complained
4539 = emit_diagnostic (kind, input_location, opt,
4540 "unable to find numeric literal operator %qD", name);
4542 if (!complained)
4543 /* Don't inform either. */;
4544 else if (i14)
4546 inform (token->location, "add %<using namespace std::complex_literals%> "
4547 "(from <complex>) to enable the C++14 user-defined literal "
4548 "suffixes");
4549 if (ext)
4550 inform (token->location, "or use %<j%> instead of %<i%> for the "
4551 "GNU built-in suffix");
4553 else if (!ext)
4554 inform (token->location, "use -fext-numeric-literals "
4555 "to enable more built-in suffixes");
4557 if (kind == DK_ERROR)
4558 value = error_mark_node;
4559 else
4561 /* Use the built-in semantics. */
4562 tree type;
4563 if (id_equal (suffix_id, "i"))
4565 if (TREE_CODE (value) == INTEGER_CST)
4566 type = integer_type_node;
4567 else
4568 type = double_type_node;
4570 else if (id_equal (suffix_id, "if"))
4571 type = float_type_node;
4572 else /* if (id_equal (suffix_id, "il")) */
4573 type = long_double_type_node;
4575 value = build_complex (build_complex_type (type),
4576 fold_convert (type, integer_zero_node),
4577 fold_convert (type, value));
4580 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4581 /* Avoid repeated diagnostics. */
4582 token->u.value = value;
4583 return value;
4586 /* Parse a user-defined string constant. Returns a call to a user-defined
4587 literal operator taking a character pointer and the length of the string
4588 as arguments. */
4590 static tree
4591 cp_parser_userdef_string_literal (tree literal)
4593 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4594 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4595 tree value = USERDEF_LITERAL_VALUE (literal);
4596 int len = TREE_STRING_LENGTH (value)
4597 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4598 tree decl;
4600 /* Build up a call to the user-defined operator. */
4601 /* Lookup the name we got back from the id-expression. */
4602 releasing_vec rargs;
4603 vec<tree, va_gc> *&args = rargs.get_ref();
4604 vec_safe_push (args, value);
4605 vec_safe_push (args, build_int_cst (size_type_node, len));
4606 decl = lookup_literal_operator (name, args);
4608 if (decl && decl != error_mark_node)
4609 return finish_call_expr (decl, &args, false, true,
4610 tf_warning_or_error);
4612 /* Look for a suitable template function, either (C++20) with a single
4613 parameter of class type, or (N3599) with typename parameter CharT and
4614 parameter pack CharT... */
4615 args->truncate (0);
4616 decl = lookup_literal_operator (name, args);
4617 if (decl && decl != error_mark_node)
4619 /* Use resolve_nondeduced_context to try to choose one form of template
4620 or the other. */
4621 tree tmpl_args = make_tree_vec (1);
4622 TREE_VEC_ELT (tmpl_args, 0) = value;
4623 decl = lookup_template_function (decl, tmpl_args);
4624 tree res = resolve_nondeduced_context (decl, tf_none);
4625 if (DECL_P (res))
4626 decl = res;
4627 else
4629 TREE_OPERAND (decl, 1) = make_string_pack (value);
4630 res = resolve_nondeduced_context (decl, tf_none);
4631 if (DECL_P (res))
4632 decl = res;
4634 if (!DECL_P (decl) && cxx_dialect > cxx17)
4635 TREE_OPERAND (decl, 1) = tmpl_args;
4636 return finish_call_expr (decl, &args, false, true,
4637 tf_warning_or_error);
4640 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4641 name, TREE_TYPE (value), size_type_node);
4642 return error_mark_node;
4646 /* Basic concepts [gram.basic] */
4648 /* Parse a translation-unit.
4650 translation-unit:
4651 declaration-seq [opt] */
4653 static void
4654 cp_parser_translation_unit (cp_parser* parser)
4656 gcc_checking_assert (!cp_error_declarator);
4658 /* Create the declarator obstack. */
4659 gcc_obstack_init (&declarator_obstack);
4660 /* Create the error declarator. */
4661 cp_error_declarator = make_declarator (cdk_error);
4662 /* Create the empty parameter list. */
4663 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4664 UNKNOWN_LOCATION);
4665 /* Remember where the base of the declarator obstack lies. */
4666 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4668 bool implicit_extern_c = false;
4670 for (;;)
4672 cp_token *token = cp_lexer_peek_token (parser->lexer);
4674 /* If we're entering or exiting a region that's implicitly
4675 extern "C", modify the lang context appropriately. */
4676 if (implicit_extern_c
4677 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4679 implicit_extern_c = !implicit_extern_c;
4680 if (implicit_extern_c)
4681 push_lang_context (lang_name_c);
4682 else
4683 pop_lang_context ();
4686 if (token->type == CPP_EOF)
4687 break;
4689 if (token->type == CPP_CLOSE_BRACE)
4691 cp_parser_error (parser, "expected declaration");
4692 cp_lexer_consume_token (parser->lexer);
4693 /* If the next token is now a `;', consume it. */
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4695 cp_lexer_consume_token (parser->lexer);
4697 else
4698 cp_parser_toplevel_declaration (parser);
4701 /* Get rid of the token array; we don't need it any more. */
4702 cp_lexer_destroy (parser->lexer);
4703 parser->lexer = NULL;
4705 /* The EOF should have reset this. */
4706 gcc_checking_assert (!implicit_extern_c);
4708 /* Make sure the declarator obstack was fully cleaned up. */
4709 gcc_assert (obstack_next_free (&declarator_obstack)
4710 == declarator_obstack_base);
4713 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4714 decltype context. */
4716 static inline tsubst_flags_t
4717 complain_flags (bool decltype_p)
4719 tsubst_flags_t complain = tf_warning_or_error;
4720 if (decltype_p)
4721 complain |= tf_decltype;
4722 return complain;
4725 /* We're about to parse a collection of statements. If we're currently
4726 parsing tentatively, set up a firewall so that any nested
4727 cp_parser_commit_to_tentative_parse won't affect the current context. */
4729 static cp_token_position
4730 cp_parser_start_tentative_firewall (cp_parser *parser)
4732 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4733 return 0;
4735 cp_parser_parse_tentatively (parser);
4736 cp_parser_commit_to_topmost_tentative_parse (parser);
4737 return cp_lexer_token_position (parser->lexer, false);
4740 /* We've finished parsing the collection of statements. Wrap up the
4741 firewall and replace the relevant tokens with the parsed form. */
4743 static void
4744 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4745 tree expr)
4747 if (!start)
4748 return;
4750 /* Finish the firewall level. */
4751 cp_parser_parse_definitely (parser);
4752 /* And remember the result of the parse for when we try again. */
4753 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4754 token->type = CPP_PREPARSED_EXPR;
4755 token->u.value = expr;
4756 token->keyword = RID_MAX;
4757 cp_lexer_purge_tokens_after (parser->lexer, start);
4760 /* Like the above functions, but let the user modify the tokens. Used by
4761 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4762 later parses, so it makes sense to localize the effects of
4763 cp_parser_commit_to_tentative_parse. */
4765 struct tentative_firewall
4767 cp_parser *parser;
4768 bool set;
4770 tentative_firewall (cp_parser *p): parser(p)
4772 /* If we're currently parsing tentatively, start a committed level as a
4773 firewall and then an inner tentative parse. */
4774 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4776 cp_parser_parse_tentatively (parser);
4777 cp_parser_commit_to_topmost_tentative_parse (parser);
4778 cp_parser_parse_tentatively (parser);
4782 ~tentative_firewall()
4784 if (set)
4786 /* Finish the inner tentative parse and the firewall, propagating any
4787 uncommitted error state to the outer tentative parse. */
4788 bool err = cp_parser_error_occurred (parser);
4789 cp_parser_parse_definitely (parser);
4790 cp_parser_parse_definitely (parser);
4791 if (err)
4792 cp_parser_simulate_error (parser);
4797 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4798 This class is for tracking such a matching pair of symbols.
4799 In particular, it tracks the location of the first token,
4800 so that if the second token is missing, we can highlight the
4801 location of the first token when notifying the user about the
4802 problem. */
4804 template <typename traits_t>
4805 class token_pair
4807 public:
4808 /* token_pair's ctor. */
4809 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4811 /* If the next token is the opening symbol for this pair, consume it and
4812 return true.
4813 Otherwise, issue an error and return false.
4814 In either case, record the location of the opening token. */
4816 bool require_open (cp_parser *parser)
4818 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4819 return cp_parser_require (parser, traits_t::open_token_type,
4820 traits_t::required_token_open);
4823 /* Consume the next token from PARSER, recording its location as
4824 that of the opening token within the pair. */
4826 cp_token * consume_open (cp_parser *parser)
4828 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4829 gcc_assert (tok->type == traits_t::open_token_type);
4830 m_open_loc = tok->location;
4831 return tok;
4834 /* If the next token is the closing symbol for this pair, consume it
4835 and return it.
4836 Otherwise, issue an error, highlighting the location of the
4837 corresponding opening token, and return NULL. */
4839 cp_token *require_close (cp_parser *parser) const
4841 return cp_parser_require (parser, traits_t::close_token_type,
4842 traits_t::required_token_close,
4843 m_open_loc);
4846 private:
4847 location_t m_open_loc;
4850 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4852 struct matching_paren_traits
4854 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4855 static const enum required_token required_token_open = RT_OPEN_PAREN;
4856 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4857 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4860 /* "matching_parens" is a token_pair<T> class for tracking matching
4861 pairs of parentheses. */
4863 typedef token_pair<matching_paren_traits> matching_parens;
4865 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4867 struct matching_brace_traits
4869 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4870 static const enum required_token required_token_open = RT_OPEN_BRACE;
4871 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4872 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4875 /* "matching_braces" is a token_pair<T> class for tracking matching
4876 pairs of braces. */
4878 typedef token_pair<matching_brace_traits> matching_braces;
4881 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4882 enclosing parentheses. */
4884 static cp_expr
4885 cp_parser_statement_expr (cp_parser *parser)
4887 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4889 /* Consume the '('. */
4890 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4891 matching_parens parens;
4892 parens.consume_open (parser);
4893 /* Start the statement-expression. */
4894 tree expr = begin_stmt_expr ();
4895 /* Parse the compound-statement. */
4896 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4897 /* Finish up. */
4898 expr = finish_stmt_expr (expr, false);
4899 /* Consume the ')'. */
4900 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4901 if (!parens.require_close (parser))
4902 cp_parser_skip_to_end_of_statement (parser);
4904 cp_parser_end_tentative_firewall (parser, start, expr);
4905 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4906 return cp_expr (expr, combined_loc);
4909 /* Expressions [gram.expr] */
4911 /* Parse a fold-operator.
4913 fold-operator:
4914 - * / % ^ & | = < > << >>
4915 = -= *= /= %= ^= &= |= <<= >>=
4916 == != <= >= && || , .* ->*
4918 This returns the tree code corresponding to the matched operator
4919 as an int. When the current token matches a compound assignment
4920 opertor, the resulting tree code is the negative value of the
4921 non-assignment operator. */
4923 static int
4924 cp_parser_fold_operator (cp_token *token)
4926 switch (token->type)
4928 case CPP_PLUS: return PLUS_EXPR;
4929 case CPP_MINUS: return MINUS_EXPR;
4930 case CPP_MULT: return MULT_EXPR;
4931 case CPP_DIV: return TRUNC_DIV_EXPR;
4932 case CPP_MOD: return TRUNC_MOD_EXPR;
4933 case CPP_XOR: return BIT_XOR_EXPR;
4934 case CPP_AND: return BIT_AND_EXPR;
4935 case CPP_OR: return BIT_IOR_EXPR;
4936 case CPP_LSHIFT: return LSHIFT_EXPR;
4937 case CPP_RSHIFT: return RSHIFT_EXPR;
4939 case CPP_EQ: return -NOP_EXPR;
4940 case CPP_PLUS_EQ: return -PLUS_EXPR;
4941 case CPP_MINUS_EQ: return -MINUS_EXPR;
4942 case CPP_MULT_EQ: return -MULT_EXPR;
4943 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4944 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4945 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4946 case CPP_AND_EQ: return -BIT_AND_EXPR;
4947 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4948 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4949 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4951 case CPP_EQ_EQ: return EQ_EXPR;
4952 case CPP_NOT_EQ: return NE_EXPR;
4953 case CPP_LESS: return LT_EXPR;
4954 case CPP_GREATER: return GT_EXPR;
4955 case CPP_LESS_EQ: return LE_EXPR;
4956 case CPP_GREATER_EQ: return GE_EXPR;
4958 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4959 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4961 case CPP_COMMA: return COMPOUND_EXPR;
4963 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4964 case CPP_DEREF_STAR: return MEMBER_REF;
4966 default: return ERROR_MARK;
4970 /* Returns true if CODE indicates a binary expression, which is not allowed in
4971 the LHS of a fold-expression. More codes will need to be added to use this
4972 function in other contexts. */
4974 static bool
4975 is_binary_op (tree_code code)
4977 switch (code)
4979 case PLUS_EXPR:
4980 case POINTER_PLUS_EXPR:
4981 case MINUS_EXPR:
4982 case MULT_EXPR:
4983 case TRUNC_DIV_EXPR:
4984 case TRUNC_MOD_EXPR:
4985 case BIT_XOR_EXPR:
4986 case BIT_AND_EXPR:
4987 case BIT_IOR_EXPR:
4988 case LSHIFT_EXPR:
4989 case RSHIFT_EXPR:
4991 case MODOP_EXPR:
4993 case EQ_EXPR:
4994 case NE_EXPR:
4995 case LE_EXPR:
4996 case GE_EXPR:
4997 case LT_EXPR:
4998 case GT_EXPR:
5000 case TRUTH_ANDIF_EXPR:
5001 case TRUTH_ORIF_EXPR:
5003 case COMPOUND_EXPR:
5005 case DOTSTAR_EXPR:
5006 case MEMBER_REF:
5007 return true;
5009 default:
5010 return false;
5014 /* If the next token is a suitable fold operator, consume it and return as
5015 the function above. */
5017 static int
5018 cp_parser_fold_operator (cp_parser *parser)
5020 cp_token* token = cp_lexer_peek_token (parser->lexer);
5021 int code = cp_parser_fold_operator (token);
5022 if (code != ERROR_MARK)
5023 cp_lexer_consume_token (parser->lexer);
5024 return code;
5027 /* Parse a fold-expression.
5029 fold-expression:
5030 ( ... folding-operator cast-expression)
5031 ( cast-expression folding-operator ... )
5032 ( cast-expression folding operator ... folding-operator cast-expression)
5034 Note that the '(' and ')' are matched in primary expression. */
5036 static cp_expr
5037 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5039 cp_id_kind pidk;
5041 // Left fold.
5042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5044 cp_lexer_consume_token (parser->lexer);
5045 int op = cp_parser_fold_operator (parser);
5046 if (op == ERROR_MARK)
5048 cp_parser_error (parser, "expected binary operator");
5049 return error_mark_node;
5052 tree expr = cp_parser_cast_expression (parser, false, false,
5053 false, &pidk);
5054 if (expr == error_mark_node)
5055 return error_mark_node;
5056 return finish_left_unary_fold_expr (expr, op);
5059 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5060 int op = cp_parser_fold_operator (parser);
5061 if (op == ERROR_MARK)
5063 cp_parser_error (parser, "expected binary operator");
5064 return error_mark_node;
5067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5069 cp_parser_error (parser, "expected ...");
5070 return error_mark_node;
5072 cp_lexer_consume_token (parser->lexer);
5074 /* The operands of a fold-expression are cast-expressions, so binary or
5075 conditional expressions are not allowed. We check this here to avoid
5076 tentative parsing. */
5077 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5078 /* OK, the expression was parenthesized. */;
5079 else if (is_binary_op (TREE_CODE (expr1)))
5080 error_at (location_of (expr1),
5081 "binary expression in operand of fold-expression");
5082 else if (TREE_CODE (expr1) == COND_EXPR
5083 || (REFERENCE_REF_P (expr1)
5084 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5085 error_at (location_of (expr1),
5086 "conditional expression in operand of fold-expression");
5088 // Right fold.
5089 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5090 return finish_right_unary_fold_expr (expr1, op);
5092 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5094 cp_parser_error (parser, "mismatched operator in fold-expression");
5095 return error_mark_node;
5097 cp_lexer_consume_token (parser->lexer);
5099 // Binary left or right fold.
5100 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5101 if (expr2 == error_mark_node)
5102 return error_mark_node;
5103 return finish_binary_fold_expr (expr1, expr2, op);
5106 /* Parse a primary-expression.
5108 primary-expression:
5109 literal
5110 this
5111 ( expression )
5112 id-expression
5113 lambda-expression (C++11)
5115 GNU Extensions:
5117 primary-expression:
5118 ( compound-statement )
5119 __builtin_va_arg ( assignment-expression , type-id )
5120 __builtin_offsetof ( type-id , offsetof-expression )
5122 C++ Extensions:
5123 __has_nothrow_assign ( type-id )
5124 __has_nothrow_constructor ( type-id )
5125 __has_nothrow_copy ( type-id )
5126 __has_trivial_assign ( type-id )
5127 __has_trivial_constructor ( type-id )
5128 __has_trivial_copy ( type-id )
5129 __has_trivial_destructor ( type-id )
5130 __has_virtual_destructor ( type-id )
5131 __is_abstract ( type-id )
5132 __is_base_of ( type-id , type-id )
5133 __is_class ( type-id )
5134 __is_empty ( type-id )
5135 __is_enum ( type-id )
5136 __is_final ( type-id )
5137 __is_literal_type ( type-id )
5138 __is_pod ( type-id )
5139 __is_polymorphic ( type-id )
5140 __is_std_layout ( type-id )
5141 __is_trivial ( type-id )
5142 __is_union ( type-id )
5144 Objective-C++ Extension:
5146 primary-expression:
5147 objc-expression
5149 literal:
5150 __null
5152 ADDRESS_P is true iff this expression was immediately preceded by
5153 "&" and therefore might denote a pointer-to-member. CAST_P is true
5154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5155 true iff this expression is a template argument.
5157 Returns a representation of the expression. Upon return, *IDK
5158 indicates what kind of id-expression (if any) was present. */
5160 static cp_expr
5161 cp_parser_primary_expression (cp_parser *parser,
5162 bool address_p,
5163 bool cast_p,
5164 bool template_arg_p,
5165 bool decltype_p,
5166 cp_id_kind *idk)
5168 cp_token *token = NULL;
5170 /* Assume the primary expression is not an id-expression. */
5171 *idk = CP_ID_KIND_NONE;
5173 /* Peek at the next token. */
5174 token = cp_lexer_peek_token (parser->lexer);
5175 switch ((int) token->type)
5177 /* literal:
5178 integer-literal
5179 character-literal
5180 floating-literal
5181 string-literal
5182 boolean-literal
5183 pointer-literal
5184 user-defined-literal */
5185 case CPP_CHAR:
5186 case CPP_CHAR16:
5187 case CPP_CHAR32:
5188 case CPP_WCHAR:
5189 case CPP_UTF8CHAR:
5190 case CPP_NUMBER:
5191 case CPP_PREPARSED_EXPR:
5192 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5193 return cp_parser_userdef_numeric_literal (parser);
5194 token = cp_lexer_consume_token (parser->lexer);
5195 if (TREE_CODE (token->u.value) == FIXED_CST)
5197 error_at (token->location,
5198 "fixed-point types not supported in C++");
5199 return error_mark_node;
5201 /* Floating-point literals are only allowed in an integral
5202 constant expression if they are cast to an integral or
5203 enumeration type. */
5204 if (TREE_CODE (token->u.value) == REAL_CST
5205 && parser->integral_constant_expression_p
5206 && pedantic)
5208 /* CAST_P will be set even in invalid code like "int(2.7 +
5209 ...)". Therefore, we have to check that the next token
5210 is sure to end the cast. */
5211 if (cast_p)
5213 cp_token *next_token;
5215 next_token = cp_lexer_peek_token (parser->lexer);
5216 if (/* The comma at the end of an
5217 enumerator-definition. */
5218 next_token->type != CPP_COMMA
5219 /* The curly brace at the end of an enum-specifier. */
5220 && next_token->type != CPP_CLOSE_BRACE
5221 /* The end of a statement. */
5222 && next_token->type != CPP_SEMICOLON
5223 /* The end of the cast-expression. */
5224 && next_token->type != CPP_CLOSE_PAREN
5225 /* The end of an array bound. */
5226 && next_token->type != CPP_CLOSE_SQUARE
5227 /* The closing ">" in a template-argument-list. */
5228 && (next_token->type != CPP_GREATER
5229 || parser->greater_than_is_operator_p)
5230 /* C++0x only: A ">>" treated like two ">" tokens,
5231 in a template-argument-list. */
5232 && (next_token->type != CPP_RSHIFT
5233 || (cxx_dialect == cxx98)
5234 || parser->greater_than_is_operator_p))
5235 cast_p = false;
5238 /* If we are within a cast, then the constraint that the
5239 cast is to an integral or enumeration type will be
5240 checked at that point. If we are not within a cast, then
5241 this code is invalid. */
5242 if (!cast_p)
5243 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5245 return (cp_expr (token->u.value, token->location)
5246 .maybe_add_location_wrapper ());
5248 case CPP_CHAR_USERDEF:
5249 case CPP_CHAR16_USERDEF:
5250 case CPP_CHAR32_USERDEF:
5251 case CPP_WCHAR_USERDEF:
5252 case CPP_UTF8CHAR_USERDEF:
5253 return cp_parser_userdef_char_literal (parser);
5255 case CPP_STRING:
5256 case CPP_STRING16:
5257 case CPP_STRING32:
5258 case CPP_WSTRING:
5259 case CPP_UTF8STRING:
5260 case CPP_STRING_USERDEF:
5261 case CPP_STRING16_USERDEF:
5262 case CPP_STRING32_USERDEF:
5263 case CPP_WSTRING_USERDEF:
5264 case CPP_UTF8STRING_USERDEF:
5265 /* ??? Should wide strings be allowed when parser->translate_strings_p
5266 is false (i.e. in attributes)? If not, we can kill the third
5267 argument to cp_parser_string_literal. */
5268 return (cp_parser_string_literal (parser,
5269 parser->translate_strings_p,
5270 true)
5271 .maybe_add_location_wrapper ());
5273 case CPP_OPEN_PAREN:
5274 /* If we see `( { ' then we are looking at the beginning of
5275 a GNU statement-expression. */
5276 if (cp_parser_allow_gnu_extensions_p (parser)
5277 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5279 /* Statement-expressions are not allowed by the standard. */
5280 pedwarn (token->location, OPT_Wpedantic,
5281 "ISO C++ forbids braced-groups within expressions");
5283 /* And they're not allowed outside of a function-body; you
5284 cannot, for example, write:
5286 int i = ({ int j = 3; j + 1; });
5288 at class or namespace scope. */
5289 if (!parser->in_function_body
5290 || parser->in_template_argument_list_p)
5292 error_at (token->location,
5293 "statement-expressions are not allowed outside "
5294 "functions nor in template-argument lists");
5295 cp_parser_skip_to_end_of_block_or_statement (parser);
5296 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5297 cp_lexer_consume_token (parser->lexer);
5298 return error_mark_node;
5300 else
5301 return cp_parser_statement_expr (parser);
5303 /* Otherwise it's a normal parenthesized expression. */
5305 cp_expr expr;
5306 bool saved_greater_than_is_operator_p;
5308 location_t open_paren_loc = token->location;
5310 /* Consume the `('. */
5311 matching_parens parens;
5312 parens.consume_open (parser);
5313 /* Within a parenthesized expression, a `>' token is always
5314 the greater-than operator. */
5315 saved_greater_than_is_operator_p
5316 = parser->greater_than_is_operator_p;
5317 parser->greater_than_is_operator_p = true;
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5320 /* Left fold expression. */
5321 expr = NULL_TREE;
5322 else
5323 /* Parse the parenthesized expression. */
5324 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5326 token = cp_lexer_peek_token (parser->lexer);
5327 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5329 expr = cp_parser_fold_expression (parser, expr);
5330 if (expr != error_mark_node
5331 && cxx_dialect < cxx17
5332 && !in_system_header_at (input_location))
5333 pedwarn (input_location, 0, "fold-expressions only available "
5334 "with -std=c++17 or -std=gnu++17");
5336 else
5337 /* Let the front end know that this expression was
5338 enclosed in parentheses. This matters in case, for
5339 example, the expression is of the form `A::B', since
5340 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5341 not. */
5342 expr = finish_parenthesized_expr (expr);
5344 /* DR 705: Wrapping an unqualified name in parentheses
5345 suppresses arg-dependent lookup. We want to pass back
5346 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5347 (c++/37862), but none of the others. */
5348 if (*idk != CP_ID_KIND_QUALIFIED)
5349 *idk = CP_ID_KIND_NONE;
5351 /* The `>' token might be the end of a template-id or
5352 template-parameter-list now. */
5353 parser->greater_than_is_operator_p
5354 = saved_greater_than_is_operator_p;
5356 /* Consume the `)'. */
5357 token = cp_lexer_peek_token (parser->lexer);
5358 location_t close_paren_loc = token->location;
5359 expr.set_range (open_paren_loc, close_paren_loc);
5360 if (!parens.require_close (parser)
5361 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5362 cp_parser_skip_to_end_of_statement (parser);
5364 return expr;
5367 case CPP_OPEN_SQUARE:
5369 if (c_dialect_objc ())
5371 /* We might have an Objective-C++ message. */
5372 cp_parser_parse_tentatively (parser);
5373 tree msg = cp_parser_objc_message_expression (parser);
5374 /* If that works out, we're done ... */
5375 if (cp_parser_parse_definitely (parser))
5376 return msg;
5377 /* ... else, fall though to see if it's a lambda. */
5379 cp_expr lam = cp_parser_lambda_expression (parser);
5380 /* Don't warn about a failed tentative parse. */
5381 if (cp_parser_error_occurred (parser))
5382 return error_mark_node;
5383 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5384 return lam;
5387 case CPP_OBJC_STRING:
5388 if (c_dialect_objc ())
5389 /* We have an Objective-C++ string literal. */
5390 return cp_parser_objc_expression (parser);
5391 cp_parser_error (parser, "expected primary-expression");
5392 return error_mark_node;
5394 case CPP_KEYWORD:
5395 switch (token->keyword)
5397 /* These two are the boolean literals. */
5398 case RID_TRUE:
5399 cp_lexer_consume_token (parser->lexer);
5400 return cp_expr (boolean_true_node, token->location);
5401 case RID_FALSE:
5402 cp_lexer_consume_token (parser->lexer);
5403 return cp_expr (boolean_false_node, token->location);
5405 /* The `__null' literal. */
5406 case RID_NULL:
5407 cp_lexer_consume_token (parser->lexer);
5408 return cp_expr (null_node, token->location);
5410 /* The `nullptr' literal. */
5411 case RID_NULLPTR:
5412 cp_lexer_consume_token (parser->lexer);
5413 return cp_expr (nullptr_node, token->location);
5415 /* Recognize the `this' keyword. */
5416 case RID_THIS:
5417 cp_lexer_consume_token (parser->lexer);
5418 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5420 error_at (token->location,
5421 "%<this%> may not be used in this context");
5422 return error_mark_node;
5424 /* Pointers cannot appear in constant-expressions. */
5425 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5426 return error_mark_node;
5427 return cp_expr (finish_this_expr (), token->location);
5429 /* The `operator' keyword can be the beginning of an
5430 id-expression. */
5431 case RID_OPERATOR:
5432 goto id_expression;
5434 case RID_FUNCTION_NAME:
5435 case RID_PRETTY_FUNCTION_NAME:
5436 case RID_C99_FUNCTION_NAME:
5438 non_integral_constant name;
5440 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5441 __func__ are the names of variables -- but they are
5442 treated specially. Therefore, they are handled here,
5443 rather than relying on the generic id-expression logic
5444 below. Grammatically, these names are id-expressions.
5446 Consume the token. */
5447 token = cp_lexer_consume_token (parser->lexer);
5449 switch (token->keyword)
5451 case RID_FUNCTION_NAME:
5452 name = NIC_FUNC_NAME;
5453 break;
5454 case RID_PRETTY_FUNCTION_NAME:
5455 name = NIC_PRETTY_FUNC;
5456 break;
5457 case RID_C99_FUNCTION_NAME:
5458 name = NIC_C99_FUNC;
5459 break;
5460 default:
5461 gcc_unreachable ();
5464 if (cp_parser_non_integral_constant_expression (parser, name))
5465 return error_mark_node;
5467 /* Look up the name. */
5468 return finish_fname (token->u.value);
5471 case RID_VA_ARG:
5473 tree expression;
5474 tree type;
5475 location_t type_location;
5476 location_t start_loc
5477 = cp_lexer_peek_token (parser->lexer)->location;
5478 /* The `__builtin_va_arg' construct is used to handle
5479 `va_arg'. Consume the `__builtin_va_arg' token. */
5480 cp_lexer_consume_token (parser->lexer);
5481 /* Look for the opening `('. */
5482 matching_parens parens;
5483 parens.require_open (parser);
5484 /* Now, parse the assignment-expression. */
5485 expression = cp_parser_assignment_expression (parser);
5486 /* Look for the `,'. */
5487 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5488 type_location = cp_lexer_peek_token (parser->lexer)->location;
5489 /* Parse the type-id. */
5491 type_id_in_expr_sentinel s (parser);
5492 type = cp_parser_type_id (parser);
5494 /* Look for the closing `)'. */
5495 location_t finish_loc
5496 = cp_lexer_peek_token (parser->lexer)->location;
5497 parens.require_close (parser);
5498 /* Using `va_arg' in a constant-expression is not
5499 allowed. */
5500 if (cp_parser_non_integral_constant_expression (parser,
5501 NIC_VA_ARG))
5502 return error_mark_node;
5503 /* Construct a location of the form:
5504 __builtin_va_arg (v, int)
5505 ~~~~~~~~~~~~~~~~~~~~~^~~~
5506 with the caret at the type, ranging from the start of the
5507 "__builtin_va_arg" token to the close paren. */
5508 location_t combined_loc
5509 = make_location (type_location, start_loc, finish_loc);
5510 return build_x_va_arg (combined_loc, expression, type);
5513 case RID_OFFSETOF:
5514 return cp_parser_builtin_offsetof (parser);
5516 case RID_HAS_NOTHROW_ASSIGN:
5517 case RID_HAS_NOTHROW_CONSTRUCTOR:
5518 case RID_HAS_NOTHROW_COPY:
5519 case RID_HAS_TRIVIAL_ASSIGN:
5520 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5521 case RID_HAS_TRIVIAL_COPY:
5522 case RID_HAS_TRIVIAL_DESTRUCTOR:
5523 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5524 case RID_HAS_VIRTUAL_DESTRUCTOR:
5525 case RID_IS_ABSTRACT:
5526 case RID_IS_AGGREGATE:
5527 case RID_IS_BASE_OF:
5528 case RID_IS_CLASS:
5529 case RID_IS_EMPTY:
5530 case RID_IS_ENUM:
5531 case RID_IS_FINAL:
5532 case RID_IS_LITERAL_TYPE:
5533 case RID_IS_POD:
5534 case RID_IS_POLYMORPHIC:
5535 case RID_IS_SAME_AS:
5536 case RID_IS_STD_LAYOUT:
5537 case RID_IS_TRIVIAL:
5538 case RID_IS_TRIVIALLY_ASSIGNABLE:
5539 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5540 case RID_IS_TRIVIALLY_COPYABLE:
5541 case RID_IS_UNION:
5542 case RID_IS_ASSIGNABLE:
5543 case RID_IS_CONSTRUCTIBLE:
5544 return cp_parser_trait_expr (parser, token->keyword);
5546 // C++ concepts
5547 case RID_REQUIRES:
5548 return cp_parser_requires_expression (parser);
5550 /* Objective-C++ expressions. */
5551 case RID_AT_ENCODE:
5552 case RID_AT_PROTOCOL:
5553 case RID_AT_SELECTOR:
5554 return cp_parser_objc_expression (parser);
5556 case RID_TEMPLATE:
5557 if (parser->in_function_body
5558 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5559 == CPP_LESS))
5561 error_at (token->location,
5562 "a template declaration cannot appear at block scope");
5563 cp_parser_skip_to_end_of_block_or_statement (parser);
5564 return error_mark_node;
5566 /* FALLTHRU */
5567 default:
5568 cp_parser_error (parser, "expected primary-expression");
5569 return error_mark_node;
5572 /* An id-expression can start with either an identifier, a
5573 `::' as the beginning of a qualified-id, or the "operator"
5574 keyword. */
5575 case CPP_NAME:
5576 case CPP_SCOPE:
5577 case CPP_TEMPLATE_ID:
5578 case CPP_NESTED_NAME_SPECIFIER:
5580 id_expression:
5581 cp_expr id_expression;
5582 cp_expr decl;
5583 const char *error_msg;
5584 bool template_p;
5585 bool done;
5586 cp_token *id_expr_token;
5588 /* Parse the id-expression. */
5589 id_expression
5590 = cp_parser_id_expression (parser,
5591 /*template_keyword_p=*/false,
5592 /*check_dependency_p=*/true,
5593 &template_p,
5594 /*declarator_p=*/false,
5595 /*optional_p=*/false);
5596 if (id_expression == error_mark_node)
5597 return error_mark_node;
5598 id_expr_token = token;
5599 token = cp_lexer_peek_token (parser->lexer);
5600 done = (token->type != CPP_OPEN_SQUARE
5601 && token->type != CPP_OPEN_PAREN
5602 && token->type != CPP_DOT
5603 && token->type != CPP_DEREF
5604 && token->type != CPP_PLUS_PLUS
5605 && token->type != CPP_MINUS_MINUS);
5606 /* If we have a template-id, then no further lookup is
5607 required. If the template-id was for a template-class, we
5608 will sometimes have a TYPE_DECL at this point. */
5609 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5610 || TREE_CODE (id_expression) == TYPE_DECL)
5611 decl = id_expression;
5612 /* Look up the name. */
5613 else
5615 tree ambiguous_decls;
5617 /* If we already know that this lookup is ambiguous, then
5618 we've already issued an error message; there's no reason
5619 to check again. */
5620 if (id_expr_token->type == CPP_NAME
5621 && id_expr_token->error_reported)
5623 cp_parser_simulate_error (parser);
5624 return error_mark_node;
5627 decl = cp_parser_lookup_name (parser, id_expression,
5628 none_type,
5629 template_p,
5630 /*is_namespace=*/false,
5631 /*check_dependency=*/true,
5632 &ambiguous_decls,
5633 id_expression.get_location ());
5634 /* If the lookup was ambiguous, an error will already have
5635 been issued. */
5636 if (ambiguous_decls)
5637 return error_mark_node;
5639 /* In Objective-C++, we may have an Objective-C 2.0
5640 dot-syntax for classes here. */
5641 if (c_dialect_objc ()
5642 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5643 && TREE_CODE (decl) == TYPE_DECL
5644 && objc_is_class_name (decl))
5646 tree component;
5647 cp_lexer_consume_token (parser->lexer);
5648 component = cp_parser_identifier (parser);
5649 if (component == error_mark_node)
5650 return error_mark_node;
5652 tree result = objc_build_class_component_ref (id_expression,
5653 component);
5654 /* Build a location of the form:
5655 expr.component
5656 ~~~~~^~~~~~~~~
5657 with caret at the start of the component name (at
5658 input_location), ranging from the start of the id_expression
5659 to the end of the component name. */
5660 location_t combined_loc
5661 = make_location (input_location, id_expression.get_start (),
5662 get_finish (input_location));
5663 protected_set_expr_location (result, combined_loc);
5664 return result;
5667 /* In Objective-C++, an instance variable (ivar) may be preferred
5668 to whatever cp_parser_lookup_name() found.
5669 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5670 rest of c-family, we have to do a little extra work to preserve
5671 any location information in cp_expr "decl". Given that
5672 objc_lookup_ivar is implemented in "c-family" and "objc", we
5673 have a trip through the pure "tree" type, rather than cp_expr.
5674 Naively copying it back to "decl" would implicitly give the
5675 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5676 store an EXPR_LOCATION. Hence we only update "decl" (and
5677 hence its location_t) if we get back a different tree node. */
5678 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5679 id_expression);
5680 if (decl_tree != decl.get_value ())
5681 decl = cp_expr (decl_tree);
5683 /* If name lookup gives us a SCOPE_REF, then the
5684 qualifying scope was dependent. */
5685 if (TREE_CODE (decl) == SCOPE_REF)
5687 /* At this point, we do not know if DECL is a valid
5688 integral constant expression. We assume that it is
5689 in fact such an expression, so that code like:
5691 template <int N> struct A {
5692 int a[B<N>::i];
5695 is accepted. At template-instantiation time, we
5696 will check that B<N>::i is actually a constant. */
5697 return decl;
5699 /* Check to see if DECL is a local variable in a context
5700 where that is forbidden. */
5701 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5702 && local_variable_p (decl))
5704 error_at (id_expression.get_location (),
5705 "local variable %qD may not appear in this context",
5706 decl.get_value ());
5707 return error_mark_node;
5711 decl = (finish_id_expression
5712 (id_expression, decl, parser->scope,
5713 idk,
5714 parser->integral_constant_expression_p,
5715 parser->allow_non_integral_constant_expression_p,
5716 &parser->non_integral_constant_expression_p,
5717 template_p, done, address_p,
5718 template_arg_p,
5719 &error_msg,
5720 id_expression.get_location ()));
5721 if (error_msg)
5722 cp_parser_error (parser, error_msg);
5723 /* Build a location for an id-expression of the form:
5724 ::ns::id
5725 ~~~~~~^~
5729 i.e. from the start of the first token to the end of the final
5730 token, with the caret at the start of the unqualified-id. */
5731 location_t caret_loc = get_pure_location (id_expression.get_location ());
5732 location_t start_loc = get_start (id_expr_token->location);
5733 location_t finish_loc = get_finish (id_expression.get_location ());
5734 location_t combined_loc
5735 = make_location (caret_loc, start_loc, finish_loc);
5737 decl.set_location (combined_loc);
5738 return decl;
5741 /* Anything else is an error. */
5742 default:
5743 cp_parser_error (parser, "expected primary-expression");
5744 return error_mark_node;
5748 static inline cp_expr
5749 cp_parser_primary_expression (cp_parser *parser,
5750 bool address_p,
5751 bool cast_p,
5752 bool template_arg_p,
5753 cp_id_kind *idk)
5755 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5756 /*decltype*/false, idk);
5759 /* Parse an id-expression.
5761 id-expression:
5762 unqualified-id
5763 qualified-id
5765 qualified-id:
5766 :: [opt] nested-name-specifier template [opt] unqualified-id
5767 :: identifier
5768 :: operator-function-id
5769 :: template-id
5771 Return a representation of the unqualified portion of the
5772 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5773 a `::' or nested-name-specifier.
5775 Often, if the id-expression was a qualified-id, the caller will
5776 want to make a SCOPE_REF to represent the qualified-id. This
5777 function does not do this in order to avoid wastefully creating
5778 SCOPE_REFs when they are not required.
5780 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5781 `template' keyword.
5783 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5784 uninstantiated templates.
5786 If *TEMPLATE_P is non-NULL, it is set to true iff the
5787 `template' keyword is used to explicitly indicate that the entity
5788 named is a template.
5790 If DECLARATOR_P is true, the id-expression is appearing as part of
5791 a declarator, rather than as part of an expression. */
5793 static cp_expr
5794 cp_parser_id_expression (cp_parser *parser,
5795 bool template_keyword_p,
5796 bool check_dependency_p,
5797 bool *template_p,
5798 bool declarator_p,
5799 bool optional_p)
5801 bool global_scope_p;
5802 bool nested_name_specifier_p;
5804 /* Assume the `template' keyword was not used. */
5805 if (template_p)
5806 *template_p = template_keyword_p;
5808 /* Look for the optional `::' operator. */
5809 global_scope_p
5810 = (!template_keyword_p
5811 && (cp_parser_global_scope_opt (parser,
5812 /*current_scope_valid_p=*/false)
5813 != NULL_TREE));
5815 /* Look for the optional nested-name-specifier. */
5816 nested_name_specifier_p
5817 = (cp_parser_nested_name_specifier_opt (parser,
5818 /*typename_keyword_p=*/false,
5819 check_dependency_p,
5820 /*type_p=*/false,
5821 declarator_p,
5822 template_keyword_p)
5823 != NULL_TREE);
5825 /* If there is a nested-name-specifier, then we are looking at
5826 the first qualified-id production. */
5827 if (nested_name_specifier_p)
5829 tree saved_scope;
5830 tree saved_object_scope;
5831 tree saved_qualifying_scope;
5832 cp_expr unqualified_id;
5833 bool is_template;
5835 /* See if the next token is the `template' keyword. */
5836 if (!template_p)
5837 template_p = &is_template;
5838 *template_p = cp_parser_optional_template_keyword (parser);
5839 /* Name lookup we do during the processing of the
5840 unqualified-id might obliterate SCOPE. */
5841 saved_scope = parser->scope;
5842 saved_object_scope = parser->object_scope;
5843 saved_qualifying_scope = parser->qualifying_scope;
5844 /* Process the final unqualified-id. */
5845 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5846 check_dependency_p,
5847 declarator_p,
5848 /*optional_p=*/false);
5849 /* Restore the SAVED_SCOPE for our caller. */
5850 parser->scope = saved_scope;
5851 parser->object_scope = saved_object_scope;
5852 parser->qualifying_scope = saved_qualifying_scope;
5854 return unqualified_id;
5856 /* Otherwise, if we are in global scope, then we are looking at one
5857 of the other qualified-id productions. */
5858 else if (global_scope_p)
5860 cp_token *token;
5861 tree id;
5863 /* Peek at the next token. */
5864 token = cp_lexer_peek_token (parser->lexer);
5866 /* If it's an identifier, and the next token is not a "<", then
5867 we can avoid the template-id case. This is an optimization
5868 for this common case. */
5869 if (token->type == CPP_NAME
5870 && !cp_parser_nth_token_starts_template_argument_list_p
5871 (parser, 2))
5872 return cp_parser_identifier (parser);
5874 cp_parser_parse_tentatively (parser);
5875 /* Try a template-id. */
5876 id = cp_parser_template_id (parser,
5877 /*template_keyword_p=*/false,
5878 /*check_dependency_p=*/true,
5879 none_type,
5880 declarator_p);
5881 /* If that worked, we're done. */
5882 if (cp_parser_parse_definitely (parser))
5883 return id;
5885 /* Peek at the next token. (Changes in the token buffer may
5886 have invalidated the pointer obtained above.) */
5887 token = cp_lexer_peek_token (parser->lexer);
5889 switch (token->type)
5891 case CPP_NAME:
5892 return cp_parser_identifier (parser);
5894 case CPP_KEYWORD:
5895 if (token->keyword == RID_OPERATOR)
5896 return cp_parser_operator_function_id (parser);
5897 /* Fall through. */
5899 default:
5900 cp_parser_error (parser, "expected id-expression");
5901 return error_mark_node;
5904 else
5905 return cp_parser_unqualified_id (parser, template_keyword_p,
5906 /*check_dependency_p=*/true,
5907 declarator_p,
5908 optional_p);
5911 /* Parse an unqualified-id.
5913 unqualified-id:
5914 identifier
5915 operator-function-id
5916 conversion-function-id
5917 ~ class-name
5918 template-id
5920 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5921 keyword, in a construct like `A::template ...'.
5923 Returns a representation of unqualified-id. For the `identifier'
5924 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5925 production a BIT_NOT_EXPR is returned; the operand of the
5926 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5927 other productions, see the documentation accompanying the
5928 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5929 names are looked up in uninstantiated templates. If DECLARATOR_P
5930 is true, the unqualified-id is appearing as part of a declarator,
5931 rather than as part of an expression. */
5933 static cp_expr
5934 cp_parser_unqualified_id (cp_parser* parser,
5935 bool template_keyword_p,
5936 bool check_dependency_p,
5937 bool declarator_p,
5938 bool optional_p)
5940 cp_token *token;
5942 /* Peek at the next token. */
5943 token = cp_lexer_peek_token (parser->lexer);
5945 switch ((int) token->type)
5947 case CPP_NAME:
5949 tree id;
5951 /* We don't know yet whether or not this will be a
5952 template-id. */
5953 cp_parser_parse_tentatively (parser);
5954 /* Try a template-id. */
5955 id = cp_parser_template_id (parser, template_keyword_p,
5956 check_dependency_p,
5957 none_type,
5958 declarator_p);
5959 /* If it worked, we're done. */
5960 if (cp_parser_parse_definitely (parser))
5961 return id;
5962 /* Otherwise, it's an ordinary identifier. */
5963 return cp_parser_identifier (parser);
5966 case CPP_TEMPLATE_ID:
5967 return cp_parser_template_id (parser, template_keyword_p,
5968 check_dependency_p,
5969 none_type,
5970 declarator_p);
5972 case CPP_COMPL:
5974 tree type_decl;
5975 tree qualifying_scope;
5976 tree object_scope;
5977 tree scope;
5978 bool done;
5979 location_t tilde_loc = token->location;
5981 /* Consume the `~' token. */
5982 cp_lexer_consume_token (parser->lexer);
5983 /* Parse the class-name. The standard, as written, seems to
5984 say that:
5986 template <typename T> struct S { ~S (); };
5987 template <typename T> S<T>::~S() {}
5989 is invalid, since `~' must be followed by a class-name, but
5990 `S<T>' is dependent, and so not known to be a class.
5991 That's not right; we need to look in uninstantiated
5992 templates. A further complication arises from:
5994 template <typename T> void f(T t) {
5995 t.T::~T();
5998 Here, it is not possible to look up `T' in the scope of `T'
5999 itself. We must look in both the current scope, and the
6000 scope of the containing complete expression.
6002 Yet another issue is:
6004 struct S {
6005 int S;
6006 ~S();
6009 S::~S() {}
6011 The standard does not seem to say that the `S' in `~S'
6012 should refer to the type `S' and not the data member
6013 `S::S'. */
6015 /* DR 244 says that we look up the name after the "~" in the
6016 same scope as we looked up the qualifying name. That idea
6017 isn't fully worked out; it's more complicated than that. */
6018 scope = parser->scope;
6019 object_scope = parser->object_scope;
6020 qualifying_scope = parser->qualifying_scope;
6022 /* Check for invalid scopes. */
6023 if (scope == error_mark_node)
6025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6026 cp_lexer_consume_token (parser->lexer);
6027 return error_mark_node;
6029 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6031 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6032 error_at (token->location,
6033 "scope %qT before %<~%> is not a class-name",
6034 scope);
6035 cp_parser_simulate_error (parser);
6036 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6037 cp_lexer_consume_token (parser->lexer);
6038 return error_mark_node;
6040 gcc_assert (!scope || TYPE_P (scope));
6042 token = cp_lexer_peek_token (parser->lexer);
6044 /* Create a location with caret == start at the tilde,
6045 finishing at the end of the peeked token, e.g:
6046 ~token
6047 ^~~~~~. */
6048 location_t loc
6049 = make_location (tilde_loc, tilde_loc, token->location);
6051 /* If the name is of the form "X::~X" it's OK even if X is a
6052 typedef. */
6054 if (scope
6055 && token->type == CPP_NAME
6056 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6057 != CPP_LESS)
6058 && (token->u.value == TYPE_IDENTIFIER (scope)
6059 || (CLASS_TYPE_P (scope)
6060 && constructor_name_p (token->u.value, scope))))
6062 cp_lexer_consume_token (parser->lexer);
6063 return cp_expr (build_nt (BIT_NOT_EXPR, scope), loc);
6066 /* ~auto means the destructor of whatever the object is. */
6067 if (cp_parser_is_keyword (token, RID_AUTO))
6069 if (cxx_dialect < cxx14)
6070 pedwarn (loc, 0,
6071 "%<~auto%> only available with "
6072 "-std=c++14 or -std=gnu++14");
6073 cp_lexer_consume_token (parser->lexer);
6074 return cp_expr (build_nt (BIT_NOT_EXPR, make_auto (), loc));
6077 /* If there was an explicit qualification (S::~T), first look
6078 in the scope given by the qualification (i.e., S).
6080 Note: in the calls to cp_parser_class_name below we pass
6081 typename_type so that lookup finds the injected-class-name
6082 rather than the constructor. */
6083 done = false;
6084 type_decl = NULL_TREE;
6085 if (scope)
6087 cp_parser_parse_tentatively (parser);
6088 type_decl = cp_parser_class_name (parser,
6089 /*typename_keyword_p=*/false,
6090 /*template_keyword_p=*/false,
6091 typename_type,
6092 /*check_dependency=*/false,
6093 /*class_head_p=*/false,
6094 declarator_p);
6095 if (cp_parser_parse_definitely (parser))
6096 done = true;
6098 /* In "N::S::~S", look in "N" as well. */
6099 if (!done && scope && qualifying_scope)
6101 cp_parser_parse_tentatively (parser);
6102 parser->scope = qualifying_scope;
6103 parser->object_scope = NULL_TREE;
6104 parser->qualifying_scope = NULL_TREE;
6105 type_decl
6106 = cp_parser_class_name (parser,
6107 /*typename_keyword_p=*/false,
6108 /*template_keyword_p=*/false,
6109 typename_type,
6110 /*check_dependency=*/false,
6111 /*class_head_p=*/false,
6112 declarator_p);
6113 if (cp_parser_parse_definitely (parser))
6114 done = true;
6116 /* In "p->S::~T", look in the scope given by "*p" as well. */
6117 else if (!done && object_scope)
6119 cp_parser_parse_tentatively (parser);
6120 parser->scope = object_scope;
6121 parser->object_scope = NULL_TREE;
6122 parser->qualifying_scope = NULL_TREE;
6123 type_decl
6124 = cp_parser_class_name (parser,
6125 /*typename_keyword_p=*/false,
6126 /*template_keyword_p=*/false,
6127 typename_type,
6128 /*check_dependency=*/false,
6129 /*class_head_p=*/false,
6130 declarator_p);
6131 if (cp_parser_parse_definitely (parser))
6132 done = true;
6134 /* Look in the surrounding context. */
6135 if (!done)
6137 parser->scope = NULL_TREE;
6138 parser->object_scope = NULL_TREE;
6139 parser->qualifying_scope = NULL_TREE;
6140 if (processing_template_decl)
6141 cp_parser_parse_tentatively (parser);
6142 type_decl
6143 = cp_parser_class_name (parser,
6144 /*typename_keyword_p=*/false,
6145 /*template_keyword_p=*/false,
6146 typename_type,
6147 /*check_dependency=*/false,
6148 /*class_head_p=*/false,
6149 declarator_p);
6150 if (processing_template_decl
6151 && ! cp_parser_parse_definitely (parser))
6153 /* We couldn't find a type with this name. If we're parsing
6154 tentatively, fail and try something else. */
6155 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6157 cp_parser_simulate_error (parser);
6158 return error_mark_node;
6160 /* Otherwise, accept it and check for a match at instantiation
6161 time. */
6162 type_decl = cp_parser_identifier (parser);
6163 if (type_decl != error_mark_node)
6164 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6165 return cp_expr (type_decl, loc);
6168 /* If an error occurred, assume that the name of the
6169 destructor is the same as the name of the qualifying
6170 class. That allows us to keep parsing after running
6171 into ill-formed destructor names. */
6172 if (type_decl == error_mark_node && scope)
6173 return build_nt (BIT_NOT_EXPR, scope);
6174 else if (type_decl == error_mark_node)
6175 return error_mark_node;
6177 /* Check that destructor name and scope match. */
6178 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6180 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6181 error_at (loc,
6182 "declaration of %<~%T%> as member of %qT",
6183 type_decl, scope);
6184 cp_parser_simulate_error (parser);
6185 return error_mark_node;
6188 /* [class.dtor]
6190 A typedef-name that names a class shall not be used as the
6191 identifier in the declarator for a destructor declaration. */
6192 if (declarator_p
6193 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6194 && !DECL_SELF_REFERENCE_P (type_decl)
6195 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6196 error_at (loc,
6197 "typedef-name %qD used as destructor declarator",
6198 type_decl);
6200 return cp_expr (build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl), loc));
6203 case CPP_KEYWORD:
6204 if (token->keyword == RID_OPERATOR)
6206 cp_expr id;
6208 /* This could be a template-id, so we try that first. */
6209 cp_parser_parse_tentatively (parser);
6210 /* Try a template-id. */
6211 id = cp_parser_template_id (parser, template_keyword_p,
6212 /*check_dependency_p=*/true,
6213 none_type,
6214 declarator_p);
6215 /* If that worked, we're done. */
6216 if (cp_parser_parse_definitely (parser))
6217 return id;
6218 /* We still don't know whether we're looking at an
6219 operator-function-id or a conversion-function-id. */
6220 cp_parser_parse_tentatively (parser);
6221 /* Try an operator-function-id. */
6222 id = cp_parser_operator_function_id (parser);
6223 /* If that didn't work, try a conversion-function-id. */
6224 if (!cp_parser_parse_definitely (parser))
6225 id = cp_parser_conversion_function_id (parser);
6227 return id;
6229 /* Fall through. */
6231 default:
6232 if (optional_p)
6233 return NULL_TREE;
6234 cp_parser_error (parser, "expected unqualified-id");
6235 return error_mark_node;
6239 /* Parse an (optional) nested-name-specifier.
6241 nested-name-specifier: [C++98]
6242 class-or-namespace-name :: nested-name-specifier [opt]
6243 class-or-namespace-name :: template nested-name-specifier [opt]
6245 nested-name-specifier: [C++0x]
6246 type-name ::
6247 namespace-name ::
6248 nested-name-specifier identifier ::
6249 nested-name-specifier template [opt] simple-template-id ::
6251 PARSER->SCOPE should be set appropriately before this function is
6252 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6253 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6254 in name lookups.
6256 Sets PARSER->SCOPE to the class (TYPE) or namespace
6257 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6258 it unchanged if there is no nested-name-specifier. Returns the new
6259 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6261 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6262 part of a declaration and/or decl-specifier. */
6264 static tree
6265 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6266 bool typename_keyword_p,
6267 bool check_dependency_p,
6268 bool type_p,
6269 bool is_declaration,
6270 bool template_keyword_p /* = false */)
6272 bool success = false;
6273 cp_token_position start = 0;
6274 cp_token *token;
6276 /* Remember where the nested-name-specifier starts. */
6277 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6279 start = cp_lexer_token_position (parser->lexer, false);
6280 push_deferring_access_checks (dk_deferred);
6283 while (true)
6285 tree new_scope;
6286 tree old_scope;
6287 tree saved_qualifying_scope;
6289 /* Spot cases that cannot be the beginning of a
6290 nested-name-specifier. */
6291 token = cp_lexer_peek_token (parser->lexer);
6293 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6294 the already parsed nested-name-specifier. */
6295 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6297 /* Grab the nested-name-specifier and continue the loop. */
6298 cp_parser_pre_parsed_nested_name_specifier (parser);
6299 /* If we originally encountered this nested-name-specifier
6300 with IS_DECLARATION set to false, we will not have
6301 resolved TYPENAME_TYPEs, so we must do so here. */
6302 if (is_declaration
6303 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6305 new_scope = resolve_typename_type (parser->scope,
6306 /*only_current_p=*/false);
6307 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6308 parser->scope = new_scope;
6310 success = true;
6311 continue;
6314 /* Spot cases that cannot be the beginning of a
6315 nested-name-specifier. On the second and subsequent times
6316 through the loop, we look for the `template' keyword. */
6317 if (success && token->keyword == RID_TEMPLATE)
6319 /* A template-id can start a nested-name-specifier. */
6320 else if (token->type == CPP_TEMPLATE_ID)
6322 /* DR 743: decltype can be used in a nested-name-specifier. */
6323 else if (token_is_decltype (token))
6325 else
6327 /* If the next token is not an identifier, then it is
6328 definitely not a type-name or namespace-name. */
6329 if (token->type != CPP_NAME)
6330 break;
6331 /* If the following token is neither a `<' (to begin a
6332 template-id), nor a `::', then we are not looking at a
6333 nested-name-specifier. */
6334 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6336 if (token->type == CPP_COLON
6337 && parser->colon_corrects_to_scope_p
6338 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6340 gcc_rich_location richloc (token->location);
6341 richloc.add_fixit_replace ("::");
6342 error_at (&richloc,
6343 "found %<:%> in nested-name-specifier, "
6344 "expected %<::%>");
6345 token->type = CPP_SCOPE;
6348 if (token->type != CPP_SCOPE
6349 && !cp_parser_nth_token_starts_template_argument_list_p
6350 (parser, 2))
6351 break;
6354 /* The nested-name-specifier is optional, so we parse
6355 tentatively. */
6356 cp_parser_parse_tentatively (parser);
6358 /* Look for the optional `template' keyword, if this isn't the
6359 first time through the loop. */
6360 if (success)
6361 template_keyword_p = cp_parser_optional_template_keyword (parser);
6363 /* Save the old scope since the name lookup we are about to do
6364 might destroy it. */
6365 old_scope = parser->scope;
6366 saved_qualifying_scope = parser->qualifying_scope;
6367 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6368 look up names in "X<T>::I" in order to determine that "Y" is
6369 a template. So, if we have a typename at this point, we make
6370 an effort to look through it. */
6371 if (is_declaration
6372 && !typename_keyword_p
6373 && parser->scope
6374 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6375 parser->scope = resolve_typename_type (parser->scope,
6376 /*only_current_p=*/false);
6377 /* Parse the qualifying entity. */
6378 new_scope
6379 = cp_parser_qualifying_entity (parser,
6380 typename_keyword_p,
6381 template_keyword_p,
6382 check_dependency_p,
6383 type_p,
6384 is_declaration);
6385 /* Look for the `::' token. */
6386 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6388 /* If we found what we wanted, we keep going; otherwise, we're
6389 done. */
6390 if (!cp_parser_parse_definitely (parser))
6392 bool error_p = false;
6394 /* Restore the OLD_SCOPE since it was valid before the
6395 failed attempt at finding the last
6396 class-or-namespace-name. */
6397 parser->scope = old_scope;
6398 parser->qualifying_scope = saved_qualifying_scope;
6400 /* If the next token is a decltype, and the one after that is a
6401 `::', then the decltype has failed to resolve to a class or
6402 enumeration type. Give this error even when parsing
6403 tentatively since it can't possibly be valid--and we're going
6404 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6405 won't get another chance.*/
6406 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6407 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6408 == CPP_SCOPE))
6410 token = cp_lexer_consume_token (parser->lexer);
6411 error_at (token->location, "decltype evaluates to %qT, "
6412 "which is not a class or enumeration type",
6413 token->u.tree_check_value->value);
6414 parser->scope = error_mark_node;
6415 error_p = true;
6416 /* As below. */
6417 success = true;
6418 cp_lexer_consume_token (parser->lexer);
6421 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6422 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6424 /* If we have a non-type template-id followed by ::, it can't
6425 possibly be valid. */
6426 token = cp_lexer_peek_token (parser->lexer);
6427 tree tid = token->u.tree_check_value->value;
6428 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6429 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6431 tree tmpl = NULL_TREE;
6432 if (is_overloaded_fn (tid))
6434 tree fns = get_fns (tid);
6435 if (OVL_SINGLE_P (fns))
6436 tmpl = OVL_FIRST (fns);
6437 error_at (token->location, "function template-id %qD "
6438 "in nested-name-specifier", tid);
6440 else
6442 /* Variable template. */
6443 tmpl = TREE_OPERAND (tid, 0);
6444 gcc_assert (variable_template_p (tmpl));
6445 error_at (token->location, "variable template-id %qD "
6446 "in nested-name-specifier", tid);
6448 if (tmpl)
6449 inform (DECL_SOURCE_LOCATION (tmpl),
6450 "%qD declared here", tmpl);
6452 parser->scope = error_mark_node;
6453 error_p = true;
6454 /* As below. */
6455 success = true;
6456 cp_lexer_consume_token (parser->lexer);
6457 cp_lexer_consume_token (parser->lexer);
6461 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6462 break;
6463 /* If the next token is an identifier, and the one after
6464 that is a `::', then any valid interpretation would have
6465 found a class-or-namespace-name. */
6466 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6467 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6468 == CPP_SCOPE)
6469 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6470 != CPP_COMPL))
6472 token = cp_lexer_consume_token (parser->lexer);
6473 if (!error_p)
6475 if (!token->error_reported)
6477 tree decl;
6478 tree ambiguous_decls;
6480 decl = cp_parser_lookup_name (parser, token->u.value,
6481 none_type,
6482 /*is_template=*/false,
6483 /*is_namespace=*/false,
6484 /*check_dependency=*/true,
6485 &ambiguous_decls,
6486 token->location);
6487 if (TREE_CODE (decl) == TEMPLATE_DECL)
6488 error_at (token->location,
6489 "%qD used without template arguments",
6490 decl);
6491 else if (ambiguous_decls)
6493 // cp_parser_lookup_name has the same diagnostic,
6494 // thus make sure to emit it at most once.
6495 if (cp_parser_uncommitted_to_tentative_parse_p
6496 (parser))
6498 error_at (token->location,
6499 "reference to %qD is ambiguous",
6500 token->u.value);
6501 print_candidates (ambiguous_decls);
6503 decl = error_mark_node;
6505 else
6507 if (cxx_dialect != cxx98)
6508 cp_parser_name_lookup_error
6509 (parser, token->u.value, decl, NLE_NOT_CXX98,
6510 token->location);
6511 else
6512 cp_parser_name_lookup_error
6513 (parser, token->u.value, decl, NLE_CXX98,
6514 token->location);
6517 parser->scope = error_mark_node;
6518 error_p = true;
6519 /* Treat this as a successful nested-name-specifier
6520 due to:
6522 [basic.lookup.qual]
6524 If the name found is not a class-name (clause
6525 _class_) or namespace-name (_namespace.def_), the
6526 program is ill-formed. */
6527 success = true;
6529 cp_lexer_consume_token (parser->lexer);
6531 break;
6533 /* We've found one valid nested-name-specifier. */
6534 success = true;
6535 /* Name lookup always gives us a DECL. */
6536 if (TREE_CODE (new_scope) == TYPE_DECL)
6537 new_scope = TREE_TYPE (new_scope);
6538 /* Uses of "template" must be followed by actual templates. */
6539 if (template_keyword_p
6540 && !(CLASS_TYPE_P (new_scope)
6541 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6542 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6543 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6544 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6545 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6546 == TEMPLATE_ID_EXPR)))
6547 permerror (input_location, TYPE_P (new_scope)
6548 ? G_("%qT is not a template")
6549 : G_("%qD is not a template"),
6550 new_scope);
6551 /* If it is a class scope, try to complete it; we are about to
6552 be looking up names inside the class. */
6553 if (TYPE_P (new_scope)
6554 /* Since checking types for dependency can be expensive,
6555 avoid doing it if the type is already complete. */
6556 && !COMPLETE_TYPE_P (new_scope)
6557 /* Do not try to complete dependent types. */
6558 && !dependent_type_p (new_scope))
6560 new_scope = complete_type (new_scope);
6561 /* If it is a typedef to current class, use the current
6562 class instead, as the typedef won't have any names inside
6563 it yet. */
6564 if (!COMPLETE_TYPE_P (new_scope)
6565 && currently_open_class (new_scope))
6566 new_scope = TYPE_MAIN_VARIANT (new_scope);
6568 /* Make sure we look in the right scope the next time through
6569 the loop. */
6570 parser->scope = new_scope;
6573 /* If parsing tentatively, replace the sequence of tokens that makes
6574 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6575 token. That way, should we re-parse the token stream, we will
6576 not have to repeat the effort required to do the parse, nor will
6577 we issue duplicate error messages. */
6578 if (success && start)
6580 cp_token *token;
6582 token = cp_lexer_token_at (parser->lexer, start);
6583 /* Reset the contents of the START token. */
6584 token->type = CPP_NESTED_NAME_SPECIFIER;
6585 /* Retrieve any deferred checks. Do not pop this access checks yet
6586 so the memory will not be reclaimed during token replacing below. */
6587 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6588 token->u.tree_check_value->value = parser->scope;
6589 token->u.tree_check_value->checks = get_deferred_access_checks ();
6590 token->u.tree_check_value->qualifying_scope =
6591 parser->qualifying_scope;
6592 token->keyword = RID_MAX;
6594 /* Purge all subsequent tokens. */
6595 cp_lexer_purge_tokens_after (parser->lexer, start);
6598 if (start)
6599 pop_to_parent_deferring_access_checks ();
6601 return success ? parser->scope : NULL_TREE;
6604 /* Parse a nested-name-specifier. See
6605 cp_parser_nested_name_specifier_opt for details. This function
6606 behaves identically, except that it will an issue an error if no
6607 nested-name-specifier is present. */
6609 static tree
6610 cp_parser_nested_name_specifier (cp_parser *parser,
6611 bool typename_keyword_p,
6612 bool check_dependency_p,
6613 bool type_p,
6614 bool is_declaration)
6616 tree scope;
6618 /* Look for the nested-name-specifier. */
6619 scope = cp_parser_nested_name_specifier_opt (parser,
6620 typename_keyword_p,
6621 check_dependency_p,
6622 type_p,
6623 is_declaration);
6624 /* If it was not present, issue an error message. */
6625 if (!scope)
6627 cp_parser_error (parser, "expected nested-name-specifier");
6628 parser->scope = NULL_TREE;
6631 return scope;
6634 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6635 this is either a class-name or a namespace-name (which corresponds
6636 to the class-or-namespace-name production in the grammar). For
6637 C++0x, it can also be a type-name that refers to an enumeration
6638 type or a simple-template-id.
6640 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6641 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6642 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6643 TYPE_P is TRUE iff the next name should be taken as a class-name,
6644 even the same name is declared to be another entity in the same
6645 scope.
6647 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6648 specified by the class-or-namespace-name. If neither is found the
6649 ERROR_MARK_NODE is returned. */
6651 static tree
6652 cp_parser_qualifying_entity (cp_parser *parser,
6653 bool typename_keyword_p,
6654 bool template_keyword_p,
6655 bool check_dependency_p,
6656 bool type_p,
6657 bool is_declaration)
6659 tree saved_scope;
6660 tree saved_qualifying_scope;
6661 tree saved_object_scope;
6662 tree scope;
6663 bool only_class_p;
6664 bool successful_parse_p;
6666 /* DR 743: decltype can appear in a nested-name-specifier. */
6667 if (cp_lexer_next_token_is_decltype (parser->lexer))
6669 scope = cp_parser_decltype (parser);
6670 if (TREE_CODE (scope) != ENUMERAL_TYPE
6671 && !MAYBE_CLASS_TYPE_P (scope))
6673 cp_parser_simulate_error (parser);
6674 return error_mark_node;
6676 if (TYPE_NAME (scope))
6677 scope = TYPE_NAME (scope);
6678 return scope;
6681 /* Before we try to parse the class-name, we must save away the
6682 current PARSER->SCOPE since cp_parser_class_name will destroy
6683 it. */
6684 saved_scope = parser->scope;
6685 saved_qualifying_scope = parser->qualifying_scope;
6686 saved_object_scope = parser->object_scope;
6687 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6688 there is no need to look for a namespace-name. */
6689 only_class_p = template_keyword_p
6690 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6691 if (!only_class_p)
6692 cp_parser_parse_tentatively (parser);
6693 scope = cp_parser_class_name (parser,
6694 typename_keyword_p,
6695 template_keyword_p,
6696 type_p ? class_type : none_type,
6697 check_dependency_p,
6698 /*class_head_p=*/false,
6699 is_declaration,
6700 /*enum_ok=*/cxx_dialect > cxx98);
6701 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6702 /* If that didn't work, try for a namespace-name. */
6703 if (!only_class_p && !successful_parse_p)
6705 /* Restore the saved scope. */
6706 parser->scope = saved_scope;
6707 parser->qualifying_scope = saved_qualifying_scope;
6708 parser->object_scope = saved_object_scope;
6709 /* If we are not looking at an identifier followed by the scope
6710 resolution operator, then this is not part of a
6711 nested-name-specifier. (Note that this function is only used
6712 to parse the components of a nested-name-specifier.) */
6713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6714 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6715 return error_mark_node;
6716 scope = cp_parser_namespace_name (parser);
6719 return scope;
6722 /* Return true if we are looking at a compound-literal, false otherwise. */
6724 static bool
6725 cp_parser_compound_literal_p (cp_parser *parser)
6727 cp_lexer_save_tokens (parser->lexer);
6729 /* Skip tokens until the next token is a closing parenthesis.
6730 If we find the closing `)', and the next token is a `{', then
6731 we are looking at a compound-literal. */
6732 bool compound_literal_p
6733 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6734 /*consume_paren=*/true)
6735 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6737 /* Roll back the tokens we skipped. */
6738 cp_lexer_rollback_tokens (parser->lexer);
6740 return compound_literal_p;
6743 /* Return true if EXPR is the integer constant zero or a complex constant
6744 of zero, without any folding, but ignoring location wrappers. */
6746 bool
6747 literal_integer_zerop (const_tree expr)
6749 return (location_wrapper_p (expr)
6750 && integer_zerop (TREE_OPERAND (expr, 0)));
6753 /* Parse a postfix-expression.
6755 postfix-expression:
6756 primary-expression
6757 postfix-expression [ expression ]
6758 postfix-expression ( expression-list [opt] )
6759 simple-type-specifier ( expression-list [opt] )
6760 typename :: [opt] nested-name-specifier identifier
6761 ( expression-list [opt] )
6762 typename :: [opt] nested-name-specifier template [opt] template-id
6763 ( expression-list [opt] )
6764 postfix-expression . template [opt] id-expression
6765 postfix-expression -> template [opt] id-expression
6766 postfix-expression . pseudo-destructor-name
6767 postfix-expression -> pseudo-destructor-name
6768 postfix-expression ++
6769 postfix-expression --
6770 dynamic_cast < type-id > ( expression )
6771 static_cast < type-id > ( expression )
6772 reinterpret_cast < type-id > ( expression )
6773 const_cast < type-id > ( expression )
6774 typeid ( expression )
6775 typeid ( type-id )
6777 GNU Extension:
6779 postfix-expression:
6780 ( type-id ) { initializer-list , [opt] }
6782 This extension is a GNU version of the C99 compound-literal
6783 construct. (The C99 grammar uses `type-name' instead of `type-id',
6784 but they are essentially the same concept.)
6786 If ADDRESS_P is true, the postfix expression is the operand of the
6787 `&' operator. CAST_P is true if this expression is the target of a
6788 cast.
6790 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6791 class member access expressions [expr.ref].
6793 Returns a representation of the expression. */
6795 static cp_expr
6796 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6797 bool member_access_only_p, bool decltype_p,
6798 cp_id_kind * pidk_return)
6800 cp_token *token;
6801 location_t loc;
6802 enum rid keyword;
6803 cp_id_kind idk = CP_ID_KIND_NONE;
6804 cp_expr postfix_expression = NULL_TREE;
6805 bool is_member_access = false;
6807 /* Peek at the next token. */
6808 token = cp_lexer_peek_token (parser->lexer);
6809 loc = token->location;
6810 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6812 /* Some of the productions are determined by keywords. */
6813 keyword = token->keyword;
6814 switch (keyword)
6816 case RID_DYNCAST:
6817 case RID_STATCAST:
6818 case RID_REINTCAST:
6819 case RID_CONSTCAST:
6821 tree type;
6822 cp_expr expression;
6823 const char *saved_message;
6824 bool saved_in_type_id_in_expr_p;
6826 /* All of these can be handled in the same way from the point
6827 of view of parsing. Begin by consuming the token
6828 identifying the cast. */
6829 cp_lexer_consume_token (parser->lexer);
6831 /* New types cannot be defined in the cast. */
6832 saved_message = parser->type_definition_forbidden_message;
6833 parser->type_definition_forbidden_message
6834 = G_("types may not be defined in casts");
6836 /* Look for the opening `<'. */
6837 cp_parser_require (parser, CPP_LESS, RT_LESS);
6838 /* Parse the type to which we are casting. */
6839 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6840 parser->in_type_id_in_expr_p = true;
6841 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6842 NULL);
6843 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6844 /* Look for the closing `>'. */
6845 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6846 /* Restore the old message. */
6847 parser->type_definition_forbidden_message = saved_message;
6849 bool saved_greater_than_is_operator_p
6850 = parser->greater_than_is_operator_p;
6851 parser->greater_than_is_operator_p = true;
6853 /* And the expression which is being cast. */
6854 matching_parens parens;
6855 parens.require_open (parser);
6856 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6857 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6858 RT_CLOSE_PAREN);
6859 location_t end_loc = close_paren ?
6860 close_paren->location : UNKNOWN_LOCATION;
6862 parser->greater_than_is_operator_p
6863 = saved_greater_than_is_operator_p;
6865 /* Only type conversions to integral or enumeration types
6866 can be used in constant-expressions. */
6867 if (!cast_valid_in_integral_constant_expression_p (type)
6868 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6870 postfix_expression = error_mark_node;
6871 break;
6874 switch (keyword)
6876 case RID_DYNCAST:
6877 postfix_expression
6878 = build_dynamic_cast (type, expression, tf_warning_or_error);
6879 break;
6880 case RID_STATCAST:
6881 postfix_expression
6882 = build_static_cast (type, expression, tf_warning_or_error);
6883 break;
6884 case RID_REINTCAST:
6885 postfix_expression
6886 = build_reinterpret_cast (type, expression,
6887 tf_warning_or_error);
6888 break;
6889 case RID_CONSTCAST:
6890 postfix_expression
6891 = build_const_cast (type, expression, tf_warning_or_error);
6892 break;
6893 default:
6894 gcc_unreachable ();
6897 /* Construct a location e.g. :
6898 reinterpret_cast <int *> (expr)
6899 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6900 ranging from the start of the "*_cast" token to the final closing
6901 paren, with the caret at the start. */
6902 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6903 postfix_expression.set_location (cp_cast_loc);
6905 break;
6907 case RID_TYPEID:
6909 tree type;
6910 const char *saved_message;
6911 bool saved_in_type_id_in_expr_p;
6913 /* Consume the `typeid' token. */
6914 cp_lexer_consume_token (parser->lexer);
6915 /* Look for the `(' token. */
6916 matching_parens parens;
6917 parens.require_open (parser);
6918 /* Types cannot be defined in a `typeid' expression. */
6919 saved_message = parser->type_definition_forbidden_message;
6920 parser->type_definition_forbidden_message
6921 = G_("types may not be defined in a %<typeid%> expression");
6922 /* We can't be sure yet whether we're looking at a type-id or an
6923 expression. */
6924 cp_parser_parse_tentatively (parser);
6925 /* Try a type-id first. */
6926 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6927 parser->in_type_id_in_expr_p = true;
6928 type = cp_parser_type_id (parser);
6929 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6930 /* Look for the `)' token. Otherwise, we can't be sure that
6931 we're not looking at an expression: consider `typeid (int
6932 (3))', for example. */
6933 cp_token *close_paren = parens.require_close (parser);
6934 /* If all went well, simply lookup the type-id. */
6935 if (cp_parser_parse_definitely (parser))
6936 postfix_expression = get_typeid (type, tf_warning_or_error);
6937 /* Otherwise, fall back to the expression variant. */
6938 else
6940 tree expression;
6942 /* Look for an expression. */
6943 expression = cp_parser_expression (parser, & idk);
6944 /* Compute its typeid. */
6945 postfix_expression = build_typeid (expression, tf_warning_or_error);
6946 /* Look for the `)' token. */
6947 close_paren = parens.require_close (parser);
6949 /* Restore the saved message. */
6950 parser->type_definition_forbidden_message = saved_message;
6951 /* `typeid' may not appear in an integral constant expression. */
6952 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6953 postfix_expression = error_mark_node;
6955 /* Construct a location e.g. :
6956 typeid (expr)
6957 ^~~~~~~~~~~~~
6958 ranging from the start of the "typeid" token to the final closing
6959 paren, with the caret at the start. */
6960 if (close_paren)
6962 location_t typeid_loc
6963 = make_location (start_loc, start_loc, close_paren->location);
6964 postfix_expression.set_location (typeid_loc);
6965 postfix_expression.maybe_add_location_wrapper ();
6968 break;
6970 case RID_TYPENAME:
6972 tree type;
6973 /* The syntax permitted here is the same permitted for an
6974 elaborated-type-specifier. */
6975 ++parser->prevent_constrained_type_specifiers;
6976 type = cp_parser_elaborated_type_specifier (parser,
6977 /*is_friend=*/false,
6978 /*is_declaration=*/false);
6979 --parser->prevent_constrained_type_specifiers;
6980 postfix_expression = cp_parser_functional_cast (parser, type);
6982 break;
6984 case RID_ADDRESSOF:
6985 case RID_BUILTIN_SHUFFLE:
6986 case RID_BUILTIN_LAUNDER:
6988 vec<tree, va_gc> *vec;
6989 unsigned int i;
6990 tree p;
6992 cp_lexer_consume_token (parser->lexer);
6993 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6994 /*cast_p=*/false, /*allow_expansion_p=*/true,
6995 /*non_constant_p=*/NULL);
6996 if (vec == NULL)
6998 postfix_expression = error_mark_node;
6999 break;
7002 FOR_EACH_VEC_ELT (*vec, i, p)
7003 mark_exp_read (p);
7005 switch (keyword)
7007 case RID_ADDRESSOF:
7008 if (vec->length () == 1)
7009 postfix_expression
7010 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7011 else
7013 error_at (loc, "wrong number of arguments to "
7014 "%<__builtin_addressof%>");
7015 postfix_expression = error_mark_node;
7017 break;
7019 case RID_BUILTIN_LAUNDER:
7020 if (vec->length () == 1)
7021 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7022 tf_warning_or_error);
7023 else
7025 error_at (loc, "wrong number of arguments to "
7026 "%<__builtin_launder%>");
7027 postfix_expression = error_mark_node;
7029 break;
7031 case RID_BUILTIN_SHUFFLE:
7032 if (vec->length () == 2)
7033 postfix_expression
7034 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7035 (*vec)[1], tf_warning_or_error);
7036 else if (vec->length () == 3)
7037 postfix_expression
7038 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7039 (*vec)[2], tf_warning_or_error);
7040 else
7042 error_at (loc, "wrong number of arguments to "
7043 "%<__builtin_shuffle%>");
7044 postfix_expression = error_mark_node;
7046 break;
7048 default:
7049 gcc_unreachable ();
7051 break;
7054 case RID_BUILTIN_CONVERTVECTOR:
7056 tree expression;
7057 tree type;
7058 /* Consume the `__builtin_convertvector' token. */
7059 cp_lexer_consume_token (parser->lexer);
7060 /* Look for the opening `('. */
7061 matching_parens parens;
7062 parens.require_open (parser);
7063 /* Now, parse the assignment-expression. */
7064 expression = cp_parser_assignment_expression (parser);
7065 /* Look for the `,'. */
7066 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7067 location_t type_location
7068 = cp_lexer_peek_token (parser->lexer)->location;
7069 /* Parse the type-id. */
7071 type_id_in_expr_sentinel s (parser);
7072 type = cp_parser_type_id (parser);
7074 /* Look for the closing `)'. */
7075 parens.require_close (parser);
7076 return cp_build_vec_convert (expression, type_location, type,
7077 tf_warning_or_error);
7080 default:
7082 tree type;
7084 /* If the next thing is a simple-type-specifier, we may be
7085 looking at a functional cast. We could also be looking at
7086 an id-expression. So, we try the functional cast, and if
7087 that doesn't work we fall back to the primary-expression. */
7088 cp_parser_parse_tentatively (parser);
7089 /* Look for the simple-type-specifier. */
7090 ++parser->prevent_constrained_type_specifiers;
7091 type = cp_parser_simple_type_specifier (parser,
7092 /*decl_specs=*/NULL,
7093 CP_PARSER_FLAGS_NONE);
7094 --parser->prevent_constrained_type_specifiers;
7095 /* Parse the cast itself. */
7096 if (!cp_parser_error_occurred (parser))
7097 postfix_expression
7098 = cp_parser_functional_cast (parser, type);
7099 /* If that worked, we're done. */
7100 if (cp_parser_parse_definitely (parser))
7101 break;
7103 /* If the functional-cast didn't work out, try a
7104 compound-literal. */
7105 if (cp_parser_allow_gnu_extensions_p (parser)
7106 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7108 cp_expr initializer = NULL_TREE;
7110 cp_parser_parse_tentatively (parser);
7112 matching_parens parens;
7113 parens.consume_open (parser);
7115 /* Avoid calling cp_parser_type_id pointlessly, see comment
7116 in cp_parser_cast_expression about c++/29234. */
7117 if (!cp_parser_compound_literal_p (parser))
7118 cp_parser_simulate_error (parser);
7119 else
7121 /* Parse the type. */
7122 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7123 parser->in_type_id_in_expr_p = true;
7124 type = cp_parser_type_id (parser);
7125 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7126 parens.require_close (parser);
7129 /* If things aren't going well, there's no need to
7130 keep going. */
7131 if (!cp_parser_error_occurred (parser))
7133 bool non_constant_p;
7134 /* Parse the brace-enclosed initializer list. */
7135 initializer = cp_parser_braced_list (parser,
7136 &non_constant_p);
7138 /* If that worked, we're definitely looking at a
7139 compound-literal expression. */
7140 if (cp_parser_parse_definitely (parser))
7142 /* Warn the user that a compound literal is not
7143 allowed in standard C++. */
7144 pedwarn (input_location, OPT_Wpedantic,
7145 "ISO C++ forbids compound-literals");
7146 /* For simplicity, we disallow compound literals in
7147 constant-expressions. We could
7148 allow compound literals of integer type, whose
7149 initializer was a constant, in constant
7150 expressions. Permitting that usage, as a further
7151 extension, would not change the meaning of any
7152 currently accepted programs. (Of course, as
7153 compound literals are not part of ISO C++, the
7154 standard has nothing to say.) */
7155 if (cp_parser_non_integral_constant_expression (parser,
7156 NIC_NCC))
7158 postfix_expression = error_mark_node;
7159 break;
7161 /* Form the representation of the compound-literal. */
7162 postfix_expression
7163 = finish_compound_literal (type, initializer,
7164 tf_warning_or_error, fcl_c99);
7165 postfix_expression.set_location (initializer.get_location ());
7166 break;
7170 /* It must be a primary-expression. */
7171 postfix_expression
7172 = cp_parser_primary_expression (parser, address_p, cast_p,
7173 /*template_arg_p=*/false,
7174 decltype_p,
7175 &idk);
7177 break;
7180 /* Note that we don't need to worry about calling build_cplus_new on a
7181 class-valued CALL_EXPR in decltype when it isn't the end of the
7182 postfix-expression; unary_complex_lvalue will take care of that for
7183 all these cases. */
7185 /* Keep looping until the postfix-expression is complete. */
7186 while (true)
7188 if (idk == CP_ID_KIND_UNQUALIFIED
7189 && identifier_p (postfix_expression)
7190 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7191 /* It is not a Koenig lookup function call. */
7192 postfix_expression
7193 = unqualified_name_lookup_error (postfix_expression);
7195 /* Peek at the next token. */
7196 token = cp_lexer_peek_token (parser->lexer);
7198 switch (token->type)
7200 case CPP_OPEN_SQUARE:
7201 if (cp_next_tokens_can_be_std_attribute_p (parser))
7203 cp_parser_error (parser,
7204 "two consecutive %<[%> shall "
7205 "only introduce an attribute");
7206 return error_mark_node;
7208 postfix_expression
7209 = cp_parser_postfix_open_square_expression (parser,
7210 postfix_expression,
7211 false,
7212 decltype_p);
7213 postfix_expression.set_range (start_loc,
7214 postfix_expression.get_location ());
7216 idk = CP_ID_KIND_NONE;
7217 is_member_access = false;
7218 break;
7220 case CPP_OPEN_PAREN:
7221 /* postfix-expression ( expression-list [opt] ) */
7223 bool koenig_p;
7224 bool is_builtin_constant_p;
7225 bool saved_integral_constant_expression_p = false;
7226 bool saved_non_integral_constant_expression_p = false;
7227 tsubst_flags_t complain = complain_flags (decltype_p);
7228 vec<tree, va_gc> *args;
7229 location_t close_paren_loc = UNKNOWN_LOCATION;
7231 is_member_access = false;
7233 tree stripped_expression
7234 = tree_strip_any_location_wrapper (postfix_expression);
7235 is_builtin_constant_p
7236 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7237 if (is_builtin_constant_p)
7239 /* The whole point of __builtin_constant_p is to allow
7240 non-constant expressions to appear as arguments. */
7241 saved_integral_constant_expression_p
7242 = parser->integral_constant_expression_p;
7243 saved_non_integral_constant_expression_p
7244 = parser->non_integral_constant_expression_p;
7245 parser->integral_constant_expression_p = false;
7247 args = (cp_parser_parenthesized_expression_list
7248 (parser, non_attr,
7249 /*cast_p=*/false, /*allow_expansion_p=*/true,
7250 /*non_constant_p=*/NULL,
7251 /*close_paren_loc=*/&close_paren_loc,
7252 /*wrap_locations_p=*/true));
7253 if (is_builtin_constant_p)
7255 parser->integral_constant_expression_p
7256 = saved_integral_constant_expression_p;
7257 parser->non_integral_constant_expression_p
7258 = saved_non_integral_constant_expression_p;
7261 if (args == NULL)
7263 postfix_expression = error_mark_node;
7264 break;
7267 /* Function calls are not permitted in
7268 constant-expressions. */
7269 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7270 && cp_parser_non_integral_constant_expression (parser,
7271 NIC_FUNC_CALL))
7273 postfix_expression = error_mark_node;
7274 release_tree_vector (args);
7275 break;
7278 koenig_p = false;
7279 if (idk == CP_ID_KIND_UNQUALIFIED
7280 || idk == CP_ID_KIND_TEMPLATE_ID)
7282 if (identifier_p (postfix_expression)
7283 /* In C++2A, we may need to perform ADL for a template
7284 name. */
7285 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7286 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7288 if (!args->is_empty ())
7290 koenig_p = true;
7291 if (!any_type_dependent_arguments_p (args))
7292 postfix_expression
7293 = perform_koenig_lookup (postfix_expression, args,
7294 complain);
7296 else
7297 postfix_expression
7298 = unqualified_fn_lookup_error (postfix_expression);
7300 /* We do not perform argument-dependent lookup if
7301 normal lookup finds a non-function, in accordance
7302 with the expected resolution of DR 218. */
7303 else if (!args->is_empty ()
7304 && is_overloaded_fn (postfix_expression))
7306 /* We only need to look at the first function,
7307 because all the fns share the attribute we're
7308 concerned with (all member fns or all local
7309 fns). */
7310 tree fn = get_first_fn (postfix_expression);
7311 fn = STRIP_TEMPLATE (fn);
7313 /* Do not do argument dependent lookup if regular
7314 lookup finds a member function or a block-scope
7315 function declaration. [basic.lookup.argdep]/3 */
7316 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7317 || DECL_FUNCTION_MEMBER_P (fn)
7318 || DECL_LOCAL_FUNCTION_P (fn)))
7320 koenig_p = true;
7321 if (!any_type_dependent_arguments_p (args))
7322 postfix_expression
7323 = perform_koenig_lookup (postfix_expression, args,
7324 complain);
7329 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7331 tree instance = TREE_OPERAND (postfix_expression, 0);
7332 tree fn = TREE_OPERAND (postfix_expression, 1);
7334 if (processing_template_decl
7335 && (type_dependent_object_expression_p (instance)
7336 || (!BASELINK_P (fn)
7337 && TREE_CODE (fn) != FIELD_DECL)
7338 || type_dependent_expression_p (fn)
7339 || any_type_dependent_arguments_p (args)))
7341 maybe_generic_this_capture (instance, fn);
7342 postfix_expression
7343 = build_min_nt_call_vec (postfix_expression, args);
7344 release_tree_vector (args);
7345 break;
7348 if (BASELINK_P (fn))
7350 postfix_expression
7351 = (build_new_method_call
7352 (instance, fn, &args, NULL_TREE,
7353 (idk == CP_ID_KIND_QUALIFIED
7354 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7355 : LOOKUP_NORMAL),
7356 /*fn_p=*/NULL,
7357 complain));
7359 else
7360 postfix_expression
7361 = finish_call_expr (postfix_expression, &args,
7362 /*disallow_virtual=*/false,
7363 /*koenig_p=*/false,
7364 complain);
7366 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7367 || TREE_CODE (postfix_expression) == MEMBER_REF
7368 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7369 postfix_expression = (build_offset_ref_call_from_tree
7370 (postfix_expression, &args,
7371 complain));
7372 else if (idk == CP_ID_KIND_QUALIFIED)
7373 /* A call to a static class member, or a namespace-scope
7374 function. */
7375 postfix_expression
7376 = finish_call_expr (postfix_expression, &args,
7377 /*disallow_virtual=*/true,
7378 koenig_p,
7379 complain);
7380 else
7381 /* All other function calls. */
7382 postfix_expression
7383 = finish_call_expr (postfix_expression, &args,
7384 /*disallow_virtual=*/false,
7385 koenig_p,
7386 complain);
7388 if (close_paren_loc != UNKNOWN_LOCATION)
7390 location_t combined_loc = make_location (token->location,
7391 start_loc,
7392 close_paren_loc);
7393 postfix_expression.set_location (combined_loc);
7396 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7397 idk = CP_ID_KIND_NONE;
7399 release_tree_vector (args);
7401 break;
7403 case CPP_DOT:
7404 case CPP_DEREF:
7405 /* postfix-expression . template [opt] id-expression
7406 postfix-expression . pseudo-destructor-name
7407 postfix-expression -> template [opt] id-expression
7408 postfix-expression -> pseudo-destructor-name */
7410 /* Consume the `.' or `->' operator. */
7411 cp_lexer_consume_token (parser->lexer);
7413 postfix_expression
7414 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7415 postfix_expression,
7416 false, &idk, loc);
7418 is_member_access = true;
7419 break;
7421 case CPP_PLUS_PLUS:
7422 /* postfix-expression ++ */
7423 /* Consume the `++' token. */
7424 cp_lexer_consume_token (parser->lexer);
7425 /* Generate a representation for the complete expression. */
7426 postfix_expression
7427 = finish_increment_expr (postfix_expression,
7428 POSTINCREMENT_EXPR);
7429 /* Increments may not appear in constant-expressions. */
7430 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7431 postfix_expression = error_mark_node;
7432 idk = CP_ID_KIND_NONE;
7433 is_member_access = false;
7434 break;
7436 case CPP_MINUS_MINUS:
7437 /* postfix-expression -- */
7438 /* Consume the `--' token. */
7439 cp_lexer_consume_token (parser->lexer);
7440 /* Generate a representation for the complete expression. */
7441 postfix_expression
7442 = finish_increment_expr (postfix_expression,
7443 POSTDECREMENT_EXPR);
7444 /* Decrements may not appear in constant-expressions. */
7445 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7446 postfix_expression = error_mark_node;
7447 idk = CP_ID_KIND_NONE;
7448 is_member_access = false;
7449 break;
7451 default:
7452 if (pidk_return != NULL)
7453 * pidk_return = idk;
7454 if (member_access_only_p)
7455 return is_member_access
7456 ? postfix_expression
7457 : cp_expr (error_mark_node);
7458 else
7459 return postfix_expression;
7463 /* We should never get here. */
7464 gcc_unreachable ();
7465 return error_mark_node;
7468 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7469 by cp_parser_builtin_offsetof. We're looking for
7471 postfix-expression [ expression ]
7472 postfix-expression [ braced-init-list ] (C++11)
7474 FOR_OFFSETOF is set if we're being called in that context, which
7475 changes how we deal with integer constant expressions. */
7477 static tree
7478 cp_parser_postfix_open_square_expression (cp_parser *parser,
7479 tree postfix_expression,
7480 bool for_offsetof,
7481 bool decltype_p)
7483 tree index = NULL_TREE;
7484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7485 bool saved_greater_than_is_operator_p;
7487 /* Consume the `[' token. */
7488 cp_lexer_consume_token (parser->lexer);
7490 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7491 parser->greater_than_is_operator_p = true;
7493 /* Parse the index expression. */
7494 /* ??? For offsetof, there is a question of what to allow here. If
7495 offsetof is not being used in an integral constant expression context,
7496 then we *could* get the right answer by computing the value at runtime.
7497 If we are in an integral constant expression context, then we might
7498 could accept any constant expression; hard to say without analysis.
7499 Rather than open the barn door too wide right away, allow only integer
7500 constant expressions here. */
7501 if (for_offsetof)
7502 index = cp_parser_constant_expression (parser);
7503 else
7505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7507 bool expr_nonconst_p;
7508 cp_lexer_set_source_position (parser->lexer);
7509 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7510 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7512 else
7513 index = cp_parser_expression (parser);
7516 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7518 /* Look for the closing `]'. */
7519 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7521 /* Build the ARRAY_REF. */
7522 postfix_expression = grok_array_decl (loc, postfix_expression,
7523 index, decltype_p);
7525 /* When not doing offsetof, array references are not permitted in
7526 constant-expressions. */
7527 if (!for_offsetof
7528 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7529 postfix_expression = error_mark_node;
7531 return postfix_expression;
7534 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7535 dereference of incomplete type, returns true if error_mark_node should
7536 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7537 and *DEPENDENT_P. */
7539 bool
7540 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7541 bool *dependent_p)
7543 /* In a template, be permissive by treating an object expression
7544 of incomplete type as dependent (after a pedwarn). */
7545 diagnostic_t kind = (processing_template_decl
7546 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7548 switch (TREE_CODE (*postfix_expression))
7550 case CAST_EXPR:
7551 case REINTERPRET_CAST_EXPR:
7552 case CONST_CAST_EXPR:
7553 case STATIC_CAST_EXPR:
7554 case DYNAMIC_CAST_EXPR:
7555 case IMPLICIT_CONV_EXPR:
7556 case VIEW_CONVERT_EXPR:
7557 case NON_LVALUE_EXPR:
7558 kind = DK_ERROR;
7559 break;
7560 case OVERLOAD:
7561 /* Don't emit any diagnostic for OVERLOADs. */
7562 kind = DK_IGNORED;
7563 break;
7564 default:
7565 /* Avoid clobbering e.g. DECLs. */
7566 if (!EXPR_P (*postfix_expression))
7567 kind = DK_ERROR;
7568 break;
7571 if (kind == DK_IGNORED)
7572 return false;
7574 location_t exploc = location_of (*postfix_expression);
7575 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7576 if (!MAYBE_CLASS_TYPE_P (*scope))
7577 return true;
7578 if (kind == DK_ERROR)
7579 *scope = *postfix_expression = error_mark_node;
7580 else if (processing_template_decl)
7582 *dependent_p = true;
7583 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7585 return false;
7588 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7589 by cp_parser_builtin_offsetof. We're looking for
7591 postfix-expression . template [opt] id-expression
7592 postfix-expression . pseudo-destructor-name
7593 postfix-expression -> template [opt] id-expression
7594 postfix-expression -> pseudo-destructor-name
7596 FOR_OFFSETOF is set if we're being called in that context. That sorta
7597 limits what of the above we'll actually accept, but nevermind.
7598 TOKEN_TYPE is the "." or "->" token, which will already have been
7599 removed from the stream. */
7601 static tree
7602 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7603 enum cpp_ttype token_type,
7604 cp_expr postfix_expression,
7605 bool for_offsetof, cp_id_kind *idk,
7606 location_t location)
7608 tree name;
7609 bool dependent_p;
7610 bool pseudo_destructor_p;
7611 tree scope = NULL_TREE;
7612 location_t start_loc = postfix_expression.get_start ();
7614 /* If this is a `->' operator, dereference the pointer. */
7615 if (token_type == CPP_DEREF)
7616 postfix_expression = build_x_arrow (location, postfix_expression,
7617 tf_warning_or_error);
7618 /* Check to see whether or not the expression is type-dependent and
7619 not the current instantiation. */
7620 dependent_p = type_dependent_object_expression_p (postfix_expression);
7621 /* The identifier following the `->' or `.' is not qualified. */
7622 parser->scope = NULL_TREE;
7623 parser->qualifying_scope = NULL_TREE;
7624 parser->object_scope = NULL_TREE;
7625 *idk = CP_ID_KIND_NONE;
7627 /* Enter the scope corresponding to the type of the object
7628 given by the POSTFIX_EXPRESSION. */
7629 if (!dependent_p)
7631 scope = TREE_TYPE (postfix_expression);
7632 /* According to the standard, no expression should ever have
7633 reference type. Unfortunately, we do not currently match
7634 the standard in this respect in that our internal representation
7635 of an expression may have reference type even when the standard
7636 says it does not. Therefore, we have to manually obtain the
7637 underlying type here. */
7638 scope = non_reference (scope);
7639 /* The type of the POSTFIX_EXPRESSION must be complete. */
7640 /* Unlike the object expression in other contexts, *this is not
7641 required to be of complete type for purposes of class member
7642 access (5.2.5) outside the member function body. */
7643 if (postfix_expression != current_class_ref
7644 && scope != error_mark_node
7645 && !currently_open_class (scope))
7647 scope = complete_type (scope);
7648 if (!COMPLETE_TYPE_P (scope)
7649 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7650 &dependent_p))
7651 return error_mark_node;
7654 if (!dependent_p)
7656 /* Let the name lookup machinery know that we are processing a
7657 class member access expression. */
7658 parser->context->object_type = scope;
7659 /* If something went wrong, we want to be able to discern that case,
7660 as opposed to the case where there was no SCOPE due to the type
7661 of expression being dependent. */
7662 if (!scope)
7663 scope = error_mark_node;
7664 /* If the SCOPE was erroneous, make the various semantic analysis
7665 functions exit quickly -- and without issuing additional error
7666 messages. */
7667 if (scope == error_mark_node)
7668 postfix_expression = error_mark_node;
7672 if (dependent_p)
7673 /* Tell cp_parser_lookup_name that there was an object, even though it's
7674 type-dependent. */
7675 parser->context->object_type = unknown_type_node;
7677 /* Assume this expression is not a pseudo-destructor access. */
7678 pseudo_destructor_p = false;
7680 /* If the SCOPE is a scalar type, then, if this is a valid program,
7681 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7682 is type dependent, it can be pseudo-destructor-name or something else.
7683 Try to parse it as pseudo-destructor-name first. */
7684 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7686 tree s;
7687 tree type;
7689 cp_parser_parse_tentatively (parser);
7690 /* Parse the pseudo-destructor-name. */
7691 s = NULL_TREE;
7692 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7693 &s, &type);
7694 if (dependent_p
7695 && (cp_parser_error_occurred (parser)
7696 || !SCALAR_TYPE_P (type)))
7697 cp_parser_abort_tentative_parse (parser);
7698 else if (cp_parser_parse_definitely (parser))
7700 pseudo_destructor_p = true;
7701 postfix_expression
7702 = finish_pseudo_destructor_expr (postfix_expression,
7703 s, type, location);
7707 if (!pseudo_destructor_p)
7709 /* If the SCOPE is not a scalar type, we are looking at an
7710 ordinary class member access expression, rather than a
7711 pseudo-destructor-name. */
7712 bool template_p;
7713 cp_token *token = cp_lexer_peek_token (parser->lexer);
7714 /* Parse the id-expression. */
7715 name = (cp_parser_id_expression
7716 (parser,
7717 cp_parser_optional_template_keyword (parser),
7718 /*check_dependency_p=*/true,
7719 &template_p,
7720 /*declarator_p=*/false,
7721 /*optional_p=*/false));
7722 /* In general, build a SCOPE_REF if the member name is qualified.
7723 However, if the name was not dependent and has already been
7724 resolved; there is no need to build the SCOPE_REF. For example;
7726 struct X { void f(); };
7727 template <typename T> void f(T* t) { t->X::f(); }
7729 Even though "t" is dependent, "X::f" is not and has been resolved
7730 to a BASELINK; there is no need to include scope information. */
7732 /* But we do need to remember that there was an explicit scope for
7733 virtual function calls. */
7734 if (parser->scope)
7735 *idk = CP_ID_KIND_QUALIFIED;
7737 /* If the name is a template-id that names a type, we will get a
7738 TYPE_DECL here. That is invalid code. */
7739 if (TREE_CODE (name) == TYPE_DECL)
7741 error_at (token->location, "invalid use of %qD", name);
7742 postfix_expression = error_mark_node;
7744 else
7746 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7748 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7750 error_at (token->location, "%<%D::%D%> is not a class member",
7751 parser->scope, name);
7752 postfix_expression = error_mark_node;
7754 else
7755 name = build_qualified_name (/*type=*/NULL_TREE,
7756 parser->scope,
7757 name,
7758 template_p);
7759 parser->scope = NULL_TREE;
7760 parser->qualifying_scope = NULL_TREE;
7761 parser->object_scope = NULL_TREE;
7763 if (parser->scope && name && BASELINK_P (name))
7764 adjust_result_of_qualified_name_lookup
7765 (name, parser->scope, scope);
7766 postfix_expression
7767 = finish_class_member_access_expr (postfix_expression, name,
7768 template_p,
7769 tf_warning_or_error);
7770 /* Build a location e.g.:
7771 ptr->access_expr
7772 ~~~^~~~~~~~~~~~~
7773 where the caret is at the deref token, ranging from
7774 the start of postfix_expression to the end of the access expr. */
7775 location_t end_loc
7776 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7777 location_t combined_loc
7778 = make_location (input_location, start_loc, end_loc);
7779 protected_set_expr_location (postfix_expression, combined_loc);
7783 /* We no longer need to look up names in the scope of the object on
7784 the left-hand side of the `.' or `->' operator. */
7785 parser->context->object_type = NULL_TREE;
7787 /* Outside of offsetof, these operators may not appear in
7788 constant-expressions. */
7789 if (!for_offsetof
7790 && (cp_parser_non_integral_constant_expression
7791 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7792 postfix_expression = error_mark_node;
7794 return postfix_expression;
7797 /* Parse a parenthesized expression-list.
7799 expression-list:
7800 assignment-expression
7801 expression-list, assignment-expression
7803 attribute-list:
7804 expression-list
7805 identifier
7806 identifier, expression-list
7808 CAST_P is true if this expression is the target of a cast.
7810 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7811 argument pack.
7813 WRAP_LOCATIONS_P is true if expressions within this list for which
7814 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7815 their source locations.
7817 Returns a vector of trees. Each element is a representation of an
7818 assignment-expression. NULL is returned if the ( and or ) are
7819 missing. An empty, but allocated, vector is returned on no
7820 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7821 if we are parsing an attribute list for an attribute that wants a
7822 plain identifier argument, normal_attr for an attribute that wants
7823 an expression, or non_attr if we aren't parsing an attribute list. If
7824 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7825 not all of the expressions in the list were constant.
7826 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7827 will be written to with the location of the closing parenthesis. If
7828 an error occurs, it may or may not be written to. */
7830 static vec<tree, va_gc> *
7831 cp_parser_parenthesized_expression_list (cp_parser* parser,
7832 int is_attribute_list,
7833 bool cast_p,
7834 bool allow_expansion_p,
7835 bool *non_constant_p,
7836 location_t *close_paren_loc,
7837 bool wrap_locations_p)
7839 vec<tree, va_gc> *expression_list;
7840 bool fold_expr_p = is_attribute_list != non_attr;
7841 tree identifier = NULL_TREE;
7842 bool saved_greater_than_is_operator_p;
7844 /* Assume all the expressions will be constant. */
7845 if (non_constant_p)
7846 *non_constant_p = false;
7848 matching_parens parens;
7849 if (!parens.require_open (parser))
7850 return NULL;
7852 expression_list = make_tree_vector ();
7854 /* Within a parenthesized expression, a `>' token is always
7855 the greater-than operator. */
7856 saved_greater_than_is_operator_p
7857 = parser->greater_than_is_operator_p;
7858 parser->greater_than_is_operator_p = true;
7860 cp_expr expr (NULL_TREE);
7862 /* Consume expressions until there are no more. */
7863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7864 while (true)
7866 /* At the beginning of attribute lists, check to see if the
7867 next token is an identifier. */
7868 if (is_attribute_list == id_attr
7869 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7871 cp_token *token;
7873 /* Consume the identifier. */
7874 token = cp_lexer_consume_token (parser->lexer);
7875 /* Save the identifier. */
7876 identifier = token->u.value;
7878 else
7880 bool expr_non_constant_p;
7882 /* Parse the next assignment-expression. */
7883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7885 /* A braced-init-list. */
7886 cp_lexer_set_source_position (parser->lexer);
7887 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7888 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7889 if (non_constant_p && expr_non_constant_p)
7890 *non_constant_p = true;
7892 else if (non_constant_p)
7894 expr = (cp_parser_constant_expression
7895 (parser, /*allow_non_constant_p=*/true,
7896 &expr_non_constant_p));
7897 if (expr_non_constant_p)
7898 *non_constant_p = true;
7900 else
7901 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7902 cast_p);
7904 if (fold_expr_p)
7905 expr = instantiate_non_dependent_expr (expr);
7907 /* If we have an ellipsis, then this is an expression
7908 expansion. */
7909 if (allow_expansion_p
7910 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7912 /* Consume the `...'. */
7913 cp_lexer_consume_token (parser->lexer);
7915 /* Build the argument pack. */
7916 expr = make_pack_expansion (expr);
7919 if (wrap_locations_p)
7920 expr.maybe_add_location_wrapper ();
7922 /* Add it to the list. We add error_mark_node
7923 expressions to the list, so that we can still tell if
7924 the correct form for a parenthesized expression-list
7925 is found. That gives better errors. */
7926 vec_safe_push (expression_list, expr.get_value ());
7928 if (expr == error_mark_node)
7929 goto skip_comma;
7932 /* After the first item, attribute lists look the same as
7933 expression lists. */
7934 is_attribute_list = non_attr;
7936 get_comma:;
7937 /* If the next token isn't a `,', then we are done. */
7938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7939 break;
7941 /* Otherwise, consume the `,' and keep going. */
7942 cp_lexer_consume_token (parser->lexer);
7945 if (close_paren_loc)
7946 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7948 if (!parens.require_close (parser))
7950 int ending;
7952 skip_comma:;
7953 /* We try and resync to an unnested comma, as that will give the
7954 user better diagnostics. */
7955 ending = cp_parser_skip_to_closing_parenthesis (parser,
7956 /*recovering=*/true,
7957 /*or_comma=*/true,
7958 /*consume_paren=*/true);
7959 if (ending < 0)
7960 goto get_comma;
7961 if (!ending)
7963 parser->greater_than_is_operator_p
7964 = saved_greater_than_is_operator_p;
7965 return NULL;
7969 parser->greater_than_is_operator_p
7970 = saved_greater_than_is_operator_p;
7972 if (identifier)
7973 vec_safe_insert (expression_list, 0, identifier);
7975 return expression_list;
7978 /* Parse a pseudo-destructor-name.
7980 pseudo-destructor-name:
7981 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7982 :: [opt] nested-name-specifier template template-id :: ~ type-name
7983 :: [opt] nested-name-specifier [opt] ~ type-name
7985 If either of the first two productions is used, sets *SCOPE to the
7986 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7987 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7988 or ERROR_MARK_NODE if the parse fails. */
7990 static void
7991 cp_parser_pseudo_destructor_name (cp_parser* parser,
7992 tree object,
7993 tree* scope,
7994 tree* type)
7996 bool nested_name_specifier_p;
7998 /* Handle ~auto. */
7999 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8000 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8001 && !type_dependent_expression_p (object))
8003 if (cxx_dialect < cxx14)
8004 pedwarn (input_location, 0,
8005 "%<~auto%> only available with "
8006 "-std=c++14 or -std=gnu++14");
8007 cp_lexer_consume_token (parser->lexer);
8008 cp_lexer_consume_token (parser->lexer);
8009 *scope = NULL_TREE;
8010 *type = TREE_TYPE (object);
8011 return;
8014 /* Assume that things will not work out. */
8015 *type = error_mark_node;
8017 /* Look for the optional `::' operator. */
8018 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8019 /* Look for the optional nested-name-specifier. */
8020 nested_name_specifier_p
8021 = (cp_parser_nested_name_specifier_opt (parser,
8022 /*typename_keyword_p=*/false,
8023 /*check_dependency_p=*/true,
8024 /*type_p=*/false,
8025 /*is_declaration=*/false)
8026 != NULL_TREE);
8027 /* Now, if we saw a nested-name-specifier, we might be doing the
8028 second production. */
8029 if (nested_name_specifier_p
8030 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8032 /* Consume the `template' keyword. */
8033 cp_lexer_consume_token (parser->lexer);
8034 /* Parse the template-id. */
8035 cp_parser_template_id (parser,
8036 /*template_keyword_p=*/true,
8037 /*check_dependency_p=*/false,
8038 class_type,
8039 /*is_declaration=*/true);
8040 /* Look for the `::' token. */
8041 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8043 /* If the next token is not a `~', then there might be some
8044 additional qualification. */
8045 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8047 /* At this point, we're looking for "type-name :: ~". The type-name
8048 must not be a class-name, since this is a pseudo-destructor. So,
8049 it must be either an enum-name, or a typedef-name -- both of which
8050 are just identifiers. So, we peek ahead to check that the "::"
8051 and "~" tokens are present; if they are not, then we can avoid
8052 calling type_name. */
8053 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8054 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8055 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8057 cp_parser_error (parser, "non-scalar type");
8058 return;
8061 /* Look for the type-name. */
8062 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8063 if (*scope == error_mark_node)
8064 return;
8066 /* Look for the `::' token. */
8067 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8069 else
8070 *scope = NULL_TREE;
8072 /* Look for the `~'. */
8073 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8075 /* Once we see the ~, this has to be a pseudo-destructor. */
8076 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8077 cp_parser_commit_to_topmost_tentative_parse (parser);
8079 /* Look for the type-name again. We are not responsible for
8080 checking that it matches the first type-name. */
8081 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8084 /* Parse a unary-expression.
8086 unary-expression:
8087 postfix-expression
8088 ++ cast-expression
8089 -- cast-expression
8090 unary-operator cast-expression
8091 sizeof unary-expression
8092 sizeof ( type-id )
8093 alignof ( type-id ) [C++0x]
8094 new-expression
8095 delete-expression
8097 GNU Extensions:
8099 unary-expression:
8100 __extension__ cast-expression
8101 __alignof__ unary-expression
8102 __alignof__ ( type-id )
8103 alignof unary-expression [C++0x]
8104 __real__ cast-expression
8105 __imag__ cast-expression
8106 && identifier
8107 sizeof ( type-id ) { initializer-list , [opt] }
8108 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8109 __alignof__ ( type-id ) { initializer-list , [opt] }
8111 ADDRESS_P is true iff the unary-expression is appearing as the
8112 operand of the `&' operator. CAST_P is true if this expression is
8113 the target of a cast.
8115 Returns a representation of the expression. */
8117 static cp_expr
8118 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8119 bool address_p, bool cast_p, bool decltype_p)
8121 cp_token *token;
8122 enum tree_code unary_operator;
8124 /* Peek at the next token. */
8125 token = cp_lexer_peek_token (parser->lexer);
8126 /* Some keywords give away the kind of expression. */
8127 if (token->type == CPP_KEYWORD)
8129 enum rid keyword = token->keyword;
8131 switch (keyword)
8133 case RID_ALIGNOF:
8134 case RID_SIZEOF:
8136 tree operand, ret;
8137 enum tree_code op;
8138 location_t start_loc = token->location;
8140 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8141 bool std_alignof = id_equal (token->u.value, "alignof");
8143 /* Consume the token. */
8144 cp_lexer_consume_token (parser->lexer);
8145 /* Parse the operand. */
8146 operand = cp_parser_sizeof_operand (parser, keyword);
8148 if (TYPE_P (operand))
8149 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8150 true);
8151 else
8153 /* ISO C++ defines alignof only with types, not with
8154 expressions. So pedwarn if alignof is used with a non-
8155 type expression. However, __alignof__ is ok. */
8156 if (std_alignof)
8157 pedwarn (token->location, OPT_Wpedantic,
8158 "ISO C++ does not allow %<alignof%> "
8159 "with a non-type");
8161 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8163 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8164 SIZEOF_EXPR with the original operand. */
8165 if (op == SIZEOF_EXPR && ret != error_mark_node)
8167 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8169 if (!processing_template_decl && TYPE_P (operand))
8171 ret = build_min (SIZEOF_EXPR, size_type_node,
8172 build1 (NOP_EXPR, operand,
8173 error_mark_node));
8174 SIZEOF_EXPR_TYPE_P (ret) = 1;
8176 else
8177 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8178 TREE_SIDE_EFFECTS (ret) = 0;
8179 TREE_READONLY (ret) = 1;
8183 /* Construct a location e.g. :
8184 alignof (expr)
8185 ^~~~~~~~~~~~~~
8186 with start == caret at the start of the "alignof"/"sizeof"
8187 token, with the endpoint at the final closing paren. */
8188 location_t finish_loc
8189 = cp_lexer_previous_token (parser->lexer)->location;
8190 location_t compound_loc
8191 = make_location (start_loc, start_loc, finish_loc);
8193 cp_expr ret_expr (ret);
8194 ret_expr.set_location (compound_loc);
8195 ret_expr = ret_expr.maybe_add_location_wrapper ();
8196 return ret_expr;
8199 case RID_BUILTIN_HAS_ATTRIBUTE:
8200 return cp_parser_has_attribute_expression (parser);
8202 case RID_NEW:
8203 return cp_parser_new_expression (parser);
8205 case RID_DELETE:
8206 return cp_parser_delete_expression (parser);
8208 case RID_EXTENSION:
8210 /* The saved value of the PEDANTIC flag. */
8211 int saved_pedantic;
8212 tree expr;
8214 /* Save away the PEDANTIC flag. */
8215 cp_parser_extension_opt (parser, &saved_pedantic);
8216 /* Parse the cast-expression. */
8217 expr = cp_parser_simple_cast_expression (parser);
8218 /* Restore the PEDANTIC flag. */
8219 pedantic = saved_pedantic;
8221 return expr;
8224 case RID_REALPART:
8225 case RID_IMAGPART:
8227 tree expression;
8229 /* Consume the `__real__' or `__imag__' token. */
8230 cp_lexer_consume_token (parser->lexer);
8231 /* Parse the cast-expression. */
8232 expression = cp_parser_simple_cast_expression (parser);
8233 /* Create the complete representation. */
8234 return build_x_unary_op (token->location,
8235 (keyword == RID_REALPART
8236 ? REALPART_EXPR : IMAGPART_EXPR),
8237 expression,
8238 tf_warning_or_error);
8240 break;
8242 case RID_TRANSACTION_ATOMIC:
8243 case RID_TRANSACTION_RELAXED:
8244 return cp_parser_transaction_expression (parser, keyword);
8246 case RID_NOEXCEPT:
8248 tree expr;
8249 const char *saved_message;
8250 bool saved_integral_constant_expression_p;
8251 bool saved_non_integral_constant_expression_p;
8252 bool saved_greater_than_is_operator_p;
8254 location_t start_loc = token->location;
8256 cp_lexer_consume_token (parser->lexer);
8257 matching_parens parens;
8258 parens.require_open (parser);
8260 saved_message = parser->type_definition_forbidden_message;
8261 parser->type_definition_forbidden_message
8262 = G_("types may not be defined in %<noexcept%> expressions");
8264 saved_integral_constant_expression_p
8265 = parser->integral_constant_expression_p;
8266 saved_non_integral_constant_expression_p
8267 = parser->non_integral_constant_expression_p;
8268 parser->integral_constant_expression_p = false;
8270 saved_greater_than_is_operator_p
8271 = parser->greater_than_is_operator_p;
8272 parser->greater_than_is_operator_p = true;
8274 ++cp_unevaluated_operand;
8275 ++c_inhibit_evaluation_warnings;
8276 ++cp_noexcept_operand;
8277 expr = cp_parser_expression (parser);
8278 --cp_noexcept_operand;
8279 --c_inhibit_evaluation_warnings;
8280 --cp_unevaluated_operand;
8282 parser->greater_than_is_operator_p
8283 = saved_greater_than_is_operator_p;
8285 parser->integral_constant_expression_p
8286 = saved_integral_constant_expression_p;
8287 parser->non_integral_constant_expression_p
8288 = saved_non_integral_constant_expression_p;
8290 parser->type_definition_forbidden_message = saved_message;
8292 location_t finish_loc
8293 = cp_lexer_peek_token (parser->lexer)->location;
8294 parens.require_close (parser);
8296 /* Construct a location of the form:
8297 noexcept (expr)
8298 ^~~~~~~~~~~~~~~
8299 with start == caret, finishing at the close-paren. */
8300 location_t noexcept_loc
8301 = make_location (start_loc, start_loc, finish_loc);
8303 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8304 noexcept_loc);
8307 default:
8308 break;
8312 /* Look for the `:: new' and `:: delete', which also signal the
8313 beginning of a new-expression, or delete-expression,
8314 respectively. If the next token is `::', then it might be one of
8315 these. */
8316 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8318 enum rid keyword;
8320 /* See if the token after the `::' is one of the keywords in
8321 which we're interested. */
8322 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8323 /* If it's `new', we have a new-expression. */
8324 if (keyword == RID_NEW)
8325 return cp_parser_new_expression (parser);
8326 /* Similarly, for `delete'. */
8327 else if (keyword == RID_DELETE)
8328 return cp_parser_delete_expression (parser);
8331 /* Look for a unary operator. */
8332 unary_operator = cp_parser_unary_operator (token);
8333 /* The `++' and `--' operators can be handled similarly, even though
8334 they are not technically unary-operators in the grammar. */
8335 if (unary_operator == ERROR_MARK)
8337 if (token->type == CPP_PLUS_PLUS)
8338 unary_operator = PREINCREMENT_EXPR;
8339 else if (token->type == CPP_MINUS_MINUS)
8340 unary_operator = PREDECREMENT_EXPR;
8341 /* Handle the GNU address-of-label extension. */
8342 else if (cp_parser_allow_gnu_extensions_p (parser)
8343 && token->type == CPP_AND_AND)
8345 tree identifier;
8346 tree expression;
8347 location_t start_loc = token->location;
8349 /* Consume the '&&' token. */
8350 cp_lexer_consume_token (parser->lexer);
8351 /* Look for the identifier. */
8352 location_t finish_loc
8353 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8354 identifier = cp_parser_identifier (parser);
8355 /* Construct a location of the form:
8356 &&label
8357 ^~~~~~~
8358 with caret==start at the "&&", finish at the end of the label. */
8359 location_t combined_loc
8360 = make_location (start_loc, start_loc, finish_loc);
8361 /* Create an expression representing the address. */
8362 expression = finish_label_address_expr (identifier, combined_loc);
8363 if (cp_parser_non_integral_constant_expression (parser,
8364 NIC_ADDR_LABEL))
8365 expression = error_mark_node;
8366 return expression;
8369 if (unary_operator != ERROR_MARK)
8371 cp_expr cast_expression;
8372 cp_expr expression = error_mark_node;
8373 non_integral_constant non_constant_p = NIC_NONE;
8374 location_t loc = token->location;
8375 tsubst_flags_t complain = complain_flags (decltype_p);
8377 /* Consume the operator token. */
8378 token = cp_lexer_consume_token (parser->lexer);
8379 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8381 /* Parse the cast-expression. */
8382 cast_expression
8383 = cp_parser_cast_expression (parser,
8384 unary_operator == ADDR_EXPR,
8385 /*cast_p=*/false,
8386 /*decltype*/false,
8387 pidk);
8389 /* Make a location:
8390 OP_TOKEN CAST_EXPRESSION
8391 ^~~~~~~~~~~~~~~~~~~~~~~~~
8392 with start==caret at the operator token, and
8393 extending to the end of the cast_expression. */
8394 loc = make_location (loc, loc, cast_expression.get_finish ());
8396 /* Now, build an appropriate representation. */
8397 switch (unary_operator)
8399 case INDIRECT_REF:
8400 non_constant_p = NIC_STAR;
8401 expression = build_x_indirect_ref (loc, cast_expression,
8402 RO_UNARY_STAR,
8403 complain);
8404 /* TODO: build_x_indirect_ref does not always honor the
8405 location, so ensure it is set. */
8406 expression.set_location (loc);
8407 break;
8409 case ADDR_EXPR:
8410 non_constant_p = NIC_ADDR;
8411 /* Fall through. */
8412 case BIT_NOT_EXPR:
8413 expression = build_x_unary_op (loc, unary_operator,
8414 cast_expression,
8415 complain);
8416 /* TODO: build_x_unary_op does not always honor the location,
8417 so ensure it is set. */
8418 expression.set_location (loc);
8419 break;
8421 case PREINCREMENT_EXPR:
8422 case PREDECREMENT_EXPR:
8423 non_constant_p = unary_operator == PREINCREMENT_EXPR
8424 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8425 /* Fall through. */
8426 case NEGATE_EXPR:
8427 /* Immediately fold negation of a constant, unless the constant is 0
8428 (since -0 == 0) or it would overflow. */
8429 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8431 tree stripped_expr
8432 = tree_strip_any_location_wrapper (cast_expression);
8433 if (CONSTANT_CLASS_P (stripped_expr)
8434 && !integer_zerop (stripped_expr)
8435 && !TREE_OVERFLOW (stripped_expr))
8437 tree folded = fold_build1 (unary_operator,
8438 TREE_TYPE (stripped_expr),
8439 stripped_expr);
8440 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8442 expression = maybe_wrap_with_location (folded, loc);
8443 break;
8447 /* Fall through. */
8448 case UNARY_PLUS_EXPR:
8449 case TRUTH_NOT_EXPR:
8450 expression = finish_unary_op_expr (loc, unary_operator,
8451 cast_expression, complain);
8452 break;
8454 default:
8455 gcc_unreachable ();
8458 if (non_constant_p != NIC_NONE
8459 && cp_parser_non_integral_constant_expression (parser,
8460 non_constant_p))
8461 expression = error_mark_node;
8463 return expression;
8466 return cp_parser_postfix_expression (parser, address_p, cast_p,
8467 /*member_access_only_p=*/false,
8468 decltype_p,
8469 pidk);
8472 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8473 unary-operator, the corresponding tree code is returned. */
8475 static enum tree_code
8476 cp_parser_unary_operator (cp_token* token)
8478 switch (token->type)
8480 case CPP_MULT:
8481 return INDIRECT_REF;
8483 case CPP_AND:
8484 return ADDR_EXPR;
8486 case CPP_PLUS:
8487 return UNARY_PLUS_EXPR;
8489 case CPP_MINUS:
8490 return NEGATE_EXPR;
8492 case CPP_NOT:
8493 return TRUTH_NOT_EXPR;
8495 case CPP_COMPL:
8496 return BIT_NOT_EXPR;
8498 default:
8499 return ERROR_MARK;
8503 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8504 Returns a representation of the expression. */
8506 static tree
8507 cp_parser_has_attribute_expression (cp_parser *parser)
8509 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8511 /* Consume the __builtin_has_attribute token. */
8512 cp_lexer_consume_token (parser->lexer);
8514 matching_parens parens;
8515 if (!parens.require_open (parser))
8516 return error_mark_node;
8518 /* Types cannot be defined in a `sizeof' expression. Save away the
8519 old message. */
8520 const char *saved_message = parser->type_definition_forbidden_message;
8521 /* And create the new one. */
8522 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8523 char *tmp = concat ("types may not be defined in %<",
8524 IDENTIFIER_POINTER (ridpointers[kwd]),
8525 "%> expressions", NULL);
8526 parser->type_definition_forbidden_message = tmp;
8528 /* The restrictions on constant-expressions do not apply inside
8529 sizeof expressions. */
8530 bool saved_integral_constant_expression_p
8531 = parser->integral_constant_expression_p;
8532 bool saved_non_integral_constant_expression_p
8533 = parser->non_integral_constant_expression_p;
8534 parser->integral_constant_expression_p = false;
8536 /* Do not actually evaluate the expression. */
8537 ++cp_unevaluated_operand;
8538 ++c_inhibit_evaluation_warnings;
8540 tree oper = NULL_TREE;
8542 /* We can't be sure yet whether we're looking at a type-id or an
8543 expression. */
8544 cp_parser_parse_tentatively (parser);
8546 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8547 parser->in_type_id_in_expr_p = true;
8548 /* Look for the type-id. */
8549 oper = cp_parser_type_id (parser);
8550 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8552 cp_parser_parse_definitely (parser);
8554 /* If the type-id production did not work out, then we must be
8555 looking at the unary-expression production. */
8556 if (!oper || oper == error_mark_node)
8557 oper = cp_parser_unary_expression (parser);
8559 STRIP_ANY_LOCATION_WRAPPER (oper);
8561 /* Go back to evaluating expressions. */
8562 --cp_unevaluated_operand;
8563 --c_inhibit_evaluation_warnings;
8565 /* Free the message we created. */
8566 free (tmp);
8567 /* And restore the old one. */
8568 parser->type_definition_forbidden_message = saved_message;
8569 parser->integral_constant_expression_p
8570 = saved_integral_constant_expression_p;
8571 parser->non_integral_constant_expression_p
8572 = saved_non_integral_constant_expression_p;
8574 /* Consume the comma if it's there. */
8575 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8577 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8578 /*consume_paren=*/true);
8579 return error_mark_node;
8582 /* Parse the attribute specification. */
8583 bool ret = false;
8584 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8585 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8587 if (oper != error_mark_node)
8589 /* Fold constant expressions used in attributes first. */
8590 cp_check_const_attributes (attr);
8592 /* Finally, see if OPER has been declared with ATTR. */
8593 ret = has_attribute (atloc, oper, attr, default_conversion);
8596 parens.require_close (parser);
8598 else
8600 error_at (atloc, "expected identifier");
8601 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8604 /* Construct a location e.g. :
8605 __builtin_has_attribute (oper, attr)
8606 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8607 with start == caret at the start of the built-in token,
8608 and with the endpoint at the final closing paren. */
8609 location_t finish_loc
8610 = cp_lexer_previous_token (parser->lexer)->location;
8611 location_t compound_loc
8612 = make_location (start_loc, start_loc, finish_loc);
8614 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8615 ret_expr.set_location (compound_loc);
8616 ret_expr = ret_expr.maybe_add_location_wrapper ();
8617 return ret_expr;
8620 /* Parse a new-expression.
8622 new-expression:
8623 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8624 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8626 Returns a representation of the expression. */
8628 static tree
8629 cp_parser_new_expression (cp_parser* parser)
8631 bool global_scope_p;
8632 vec<tree, va_gc> *placement;
8633 tree type;
8634 vec<tree, va_gc> *initializer;
8635 tree nelts = NULL_TREE;
8636 tree ret;
8638 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8640 /* Look for the optional `::' operator. */
8641 global_scope_p
8642 = (cp_parser_global_scope_opt (parser,
8643 /*current_scope_valid_p=*/false)
8644 != NULL_TREE);
8645 /* Look for the `new' operator. */
8646 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8647 /* There's no easy way to tell a new-placement from the
8648 `( type-id )' construct. */
8649 cp_parser_parse_tentatively (parser);
8650 /* Look for a new-placement. */
8651 placement = cp_parser_new_placement (parser);
8652 /* If that didn't work out, there's no new-placement. */
8653 if (!cp_parser_parse_definitely (parser))
8655 if (placement != NULL)
8656 release_tree_vector (placement);
8657 placement = NULL;
8660 /* If the next token is a `(', then we have a parenthesized
8661 type-id. */
8662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8664 cp_token *token;
8665 const char *saved_message = parser->type_definition_forbidden_message;
8667 /* Consume the `('. */
8668 matching_parens parens;
8669 parens.consume_open (parser);
8671 /* Parse the type-id. */
8672 parser->type_definition_forbidden_message
8673 = G_("types may not be defined in a new-expression");
8675 type_id_in_expr_sentinel s (parser);
8676 type = cp_parser_type_id (parser);
8678 parser->type_definition_forbidden_message = saved_message;
8680 /* Look for the closing `)'. */
8681 parens.require_close (parser);
8682 token = cp_lexer_peek_token (parser->lexer);
8683 /* There should not be a direct-new-declarator in this production,
8684 but GCC used to allowed this, so we check and emit a sensible error
8685 message for this case. */
8686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8688 error_at (token->location,
8689 "array bound forbidden after parenthesized type-id");
8690 inform (token->location,
8691 "try removing the parentheses around the type-id");
8692 cp_parser_direct_new_declarator (parser);
8695 /* Otherwise, there must be a new-type-id. */
8696 else
8697 type = cp_parser_new_type_id (parser, &nelts);
8699 /* If the next token is a `(' or '{', then we have a new-initializer. */
8700 cp_token *token = cp_lexer_peek_token (parser->lexer);
8701 if (token->type == CPP_OPEN_PAREN
8702 || token->type == CPP_OPEN_BRACE)
8703 initializer = cp_parser_new_initializer (parser);
8704 else
8705 initializer = NULL;
8707 /* A new-expression may not appear in an integral constant
8708 expression. */
8709 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8710 ret = error_mark_node;
8711 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8712 of a new-type-id or type-id of a new-expression, the new-expression shall
8713 contain a new-initializer of the form ( assignment-expression )".
8714 Additionally, consistently with the spirit of DR 1467, we want to accept
8715 'new auto { 2 }' too. */
8716 else if ((ret = type_uses_auto (type))
8717 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8718 && (vec_safe_length (initializer) != 1
8719 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8720 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8722 error_at (token->location,
8723 "initialization of new-expression for type %<auto%> "
8724 "requires exactly one element");
8725 ret = error_mark_node;
8727 else
8729 /* Construct a location e.g.:
8730 ptr = new int[100]
8731 ^~~~~~~~~~~~
8732 with caret == start at the start of the "new" token, and the end
8733 at the end of the final token we consumed. */
8734 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8735 location_t end_loc = get_finish (end_tok->location);
8736 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8738 /* Create a representation of the new-expression. */
8739 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8740 tf_warning_or_error);
8741 protected_set_expr_location (ret, combined_loc);
8744 if (placement != NULL)
8745 release_tree_vector (placement);
8746 if (initializer != NULL)
8747 release_tree_vector (initializer);
8749 return ret;
8752 /* Parse a new-placement.
8754 new-placement:
8755 ( expression-list )
8757 Returns the same representation as for an expression-list. */
8759 static vec<tree, va_gc> *
8760 cp_parser_new_placement (cp_parser* parser)
8762 vec<tree, va_gc> *expression_list;
8764 /* Parse the expression-list. */
8765 expression_list = (cp_parser_parenthesized_expression_list
8766 (parser, non_attr, /*cast_p=*/false,
8767 /*allow_expansion_p=*/true,
8768 /*non_constant_p=*/NULL));
8770 if (expression_list && expression_list->is_empty ())
8771 error ("expected expression-list or type-id");
8773 return expression_list;
8776 /* Parse a new-type-id.
8778 new-type-id:
8779 type-specifier-seq new-declarator [opt]
8781 Returns the TYPE allocated. If the new-type-id indicates an array
8782 type, *NELTS is set to the number of elements in the last array
8783 bound; the TYPE will not include the last array bound. */
8785 static tree
8786 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8788 cp_decl_specifier_seq type_specifier_seq;
8789 cp_declarator *new_declarator;
8790 cp_declarator *declarator;
8791 cp_declarator *outer_declarator;
8792 const char *saved_message;
8794 /* The type-specifier sequence must not contain type definitions.
8795 (It cannot contain declarations of new types either, but if they
8796 are not definitions we will catch that because they are not
8797 complete.) */
8798 saved_message = parser->type_definition_forbidden_message;
8799 parser->type_definition_forbidden_message
8800 = G_("types may not be defined in a new-type-id");
8801 /* Parse the type-specifier-seq. */
8802 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8803 /*is_declaration=*/false,
8804 /*is_trailing_return=*/false,
8805 &type_specifier_seq);
8806 /* Restore the old message. */
8807 parser->type_definition_forbidden_message = saved_message;
8809 if (type_specifier_seq.type == error_mark_node)
8810 return error_mark_node;
8812 /* Parse the new-declarator. */
8813 new_declarator = cp_parser_new_declarator_opt (parser);
8815 /* Determine the number of elements in the last array dimension, if
8816 any. */
8817 *nelts = NULL_TREE;
8818 /* Skip down to the last array dimension. */
8819 declarator = new_declarator;
8820 outer_declarator = NULL;
8821 while (declarator && (declarator->kind == cdk_pointer
8822 || declarator->kind == cdk_ptrmem))
8824 outer_declarator = declarator;
8825 declarator = declarator->declarator;
8827 while (declarator
8828 && declarator->kind == cdk_array
8829 && declarator->declarator
8830 && declarator->declarator->kind == cdk_array)
8832 outer_declarator = declarator;
8833 declarator = declarator->declarator;
8836 if (declarator && declarator->kind == cdk_array)
8838 *nelts = declarator->u.array.bounds;
8839 if (*nelts == error_mark_node)
8840 *nelts = integer_one_node;
8842 if (outer_declarator)
8843 outer_declarator->declarator = declarator->declarator;
8844 else
8845 new_declarator = NULL;
8848 return groktypename (&type_specifier_seq, new_declarator, false);
8851 /* Parse an (optional) new-declarator.
8853 new-declarator:
8854 ptr-operator new-declarator [opt]
8855 direct-new-declarator
8857 Returns the declarator. */
8859 static cp_declarator *
8860 cp_parser_new_declarator_opt (cp_parser* parser)
8862 enum tree_code code;
8863 tree type, std_attributes = NULL_TREE;
8864 cp_cv_quals cv_quals;
8866 /* We don't know if there's a ptr-operator next, or not. */
8867 cp_parser_parse_tentatively (parser);
8868 /* Look for a ptr-operator. */
8869 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8870 /* If that worked, look for more new-declarators. */
8871 if (cp_parser_parse_definitely (parser))
8873 cp_declarator *declarator;
8875 /* Parse another optional declarator. */
8876 declarator = cp_parser_new_declarator_opt (parser);
8878 declarator = cp_parser_make_indirect_declarator
8879 (code, type, cv_quals, declarator, std_attributes);
8881 return declarator;
8884 /* If the next token is a `[', there is a direct-new-declarator. */
8885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8886 return cp_parser_direct_new_declarator (parser);
8888 return NULL;
8891 /* Parse a direct-new-declarator.
8893 direct-new-declarator:
8894 [ expression ]
8895 direct-new-declarator [constant-expression]
8899 static cp_declarator *
8900 cp_parser_direct_new_declarator (cp_parser* parser)
8902 cp_declarator *declarator = NULL;
8904 while (true)
8906 tree expression;
8907 cp_token *token;
8909 /* Look for the opening `['. */
8910 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8912 token = cp_lexer_peek_token (parser->lexer);
8913 expression = cp_parser_expression (parser);
8914 /* The standard requires that the expression have integral
8915 type. DR 74 adds enumeration types. We believe that the
8916 real intent is that these expressions be handled like the
8917 expression in a `switch' condition, which also allows
8918 classes with a single conversion to integral or
8919 enumeration type. */
8920 if (!processing_template_decl)
8922 expression
8923 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8924 expression,
8925 /*complain=*/true);
8926 if (!expression)
8928 error_at (token->location,
8929 "expression in new-declarator must have integral "
8930 "or enumeration type");
8931 expression = error_mark_node;
8935 /* Look for the closing `]'. */
8936 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8938 /* Add this bound to the declarator. */
8939 declarator = make_array_declarator (declarator, expression);
8941 /* If the next token is not a `[', then there are no more
8942 bounds. */
8943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8944 break;
8947 return declarator;
8950 /* Parse a new-initializer.
8952 new-initializer:
8953 ( expression-list [opt] )
8954 braced-init-list
8956 Returns a representation of the expression-list. */
8958 static vec<tree, va_gc> *
8959 cp_parser_new_initializer (cp_parser* parser)
8961 vec<tree, va_gc> *expression_list;
8963 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8965 tree t;
8966 bool expr_non_constant_p;
8967 cp_lexer_set_source_position (parser->lexer);
8968 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8969 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8970 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8971 expression_list = make_tree_vector_single (t);
8973 else
8974 expression_list = (cp_parser_parenthesized_expression_list
8975 (parser, non_attr, /*cast_p=*/false,
8976 /*allow_expansion_p=*/true,
8977 /*non_constant_p=*/NULL));
8979 return expression_list;
8982 /* Parse a delete-expression.
8984 delete-expression:
8985 :: [opt] delete cast-expression
8986 :: [opt] delete [ ] cast-expression
8988 Returns a representation of the expression. */
8990 static tree
8991 cp_parser_delete_expression (cp_parser* parser)
8993 bool global_scope_p;
8994 bool array_p;
8995 tree expression;
8997 /* Look for the optional `::' operator. */
8998 global_scope_p
8999 = (cp_parser_global_scope_opt (parser,
9000 /*current_scope_valid_p=*/false)
9001 != NULL_TREE);
9002 /* Look for the `delete' keyword. */
9003 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9004 /* See if the array syntax is in use. */
9005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9007 /* Consume the `[' token. */
9008 cp_lexer_consume_token (parser->lexer);
9009 /* Look for the `]' token. */
9010 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9011 /* Remember that this is the `[]' construct. */
9012 array_p = true;
9014 else
9015 array_p = false;
9017 /* Parse the cast-expression. */
9018 expression = cp_parser_simple_cast_expression (parser);
9020 /* A delete-expression may not appear in an integral constant
9021 expression. */
9022 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9023 return error_mark_node;
9025 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9026 tf_warning_or_error);
9029 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9030 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9031 0 otherwise. */
9033 static int
9034 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9036 cp_token *token = cp_lexer_peek_token (parser->lexer);
9037 switch (token->type)
9039 case CPP_COMMA:
9040 case CPP_SEMICOLON:
9041 case CPP_QUERY:
9042 case CPP_COLON:
9043 case CPP_CLOSE_SQUARE:
9044 case CPP_CLOSE_PAREN:
9045 case CPP_CLOSE_BRACE:
9046 case CPP_OPEN_BRACE:
9047 case CPP_DOT:
9048 case CPP_DOT_STAR:
9049 case CPP_DEREF:
9050 case CPP_DEREF_STAR:
9051 case CPP_DIV:
9052 case CPP_MOD:
9053 case CPP_LSHIFT:
9054 case CPP_RSHIFT:
9055 case CPP_LESS:
9056 case CPP_GREATER:
9057 case CPP_LESS_EQ:
9058 case CPP_GREATER_EQ:
9059 case CPP_EQ_EQ:
9060 case CPP_NOT_EQ:
9061 case CPP_EQ:
9062 case CPP_MULT_EQ:
9063 case CPP_DIV_EQ:
9064 case CPP_MOD_EQ:
9065 case CPP_PLUS_EQ:
9066 case CPP_MINUS_EQ:
9067 case CPP_RSHIFT_EQ:
9068 case CPP_LSHIFT_EQ:
9069 case CPP_AND_EQ:
9070 case CPP_XOR_EQ:
9071 case CPP_OR_EQ:
9072 case CPP_XOR:
9073 case CPP_OR:
9074 case CPP_OR_OR:
9075 case CPP_EOF:
9076 case CPP_ELLIPSIS:
9077 return 0;
9079 case CPP_OPEN_PAREN:
9080 /* In ((type ()) () the last () isn't a valid cast-expression,
9081 so the whole must be parsed as postfix-expression. */
9082 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9083 != CPP_CLOSE_PAREN;
9085 case CPP_OPEN_SQUARE:
9086 /* '[' may start a primary-expression in obj-c++ and in C++11,
9087 as a lambda-expression, eg, '(void)[]{}'. */
9088 if (cxx_dialect >= cxx11)
9089 return -1;
9090 return c_dialect_objc ();
9092 case CPP_PLUS_PLUS:
9093 case CPP_MINUS_MINUS:
9094 /* '++' and '--' may or may not start a cast-expression:
9096 struct T { void operator++(int); };
9097 void f() { (T())++; }
9101 int a;
9102 (int)++a; */
9103 return -1;
9105 default:
9106 return 1;
9110 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9111 in the order: const_cast, static_cast, reinterpret_cast.
9113 Don't suggest dynamic_cast.
9115 Return the first legal cast kind found, or NULL otherwise. */
9117 static const char *
9118 get_cast_suggestion (tree dst_type, tree orig_expr)
9120 tree trial;
9122 /* Reuse the parser logic by attempting to build the various kinds of
9123 cast, with "complain" disabled.
9124 Identify the first such cast that is valid. */
9126 /* Don't attempt to run such logic within template processing. */
9127 if (processing_template_decl)
9128 return NULL;
9130 /* First try const_cast. */
9131 trial = build_const_cast (dst_type, orig_expr, tf_none);
9132 if (trial != error_mark_node)
9133 return "const_cast";
9135 /* If that fails, try static_cast. */
9136 trial = build_static_cast (dst_type, orig_expr, tf_none);
9137 if (trial != error_mark_node)
9138 return "static_cast";
9140 /* Finally, try reinterpret_cast. */
9141 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9142 if (trial != error_mark_node)
9143 return "reinterpret_cast";
9145 /* No such cast possible. */
9146 return NULL;
9149 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9150 suggesting how to convert a C-style cast of the form:
9152 (DST_TYPE)ORIG_EXPR
9154 to a C++-style cast.
9156 The primary range of RICHLOC is asssumed to be that of the original
9157 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9158 of the parens in the C-style cast. */
9160 static void
9161 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9162 location_t close_paren_loc, tree orig_expr,
9163 tree dst_type)
9165 /* This function is non-trivial, so bail out now if the warning isn't
9166 going to be emitted. */
9167 if (!warn_old_style_cast)
9168 return;
9170 /* Try to find a legal C++ cast, trying them in order:
9171 const_cast, static_cast, reinterpret_cast. */
9172 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9173 if (!cast_suggestion)
9174 return;
9176 /* Replace the open paren with "CAST_SUGGESTION<". */
9177 pretty_printer pp;
9178 pp_printf (&pp, "%s<", cast_suggestion);
9179 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9181 /* Replace the close paren with "> (". */
9182 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9184 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9185 rich_loc->add_fixit_insert_after (")");
9189 /* Parse a cast-expression.
9191 cast-expression:
9192 unary-expression
9193 ( type-id ) cast-expression
9195 ADDRESS_P is true iff the unary-expression is appearing as the
9196 operand of the `&' operator. CAST_P is true if this expression is
9197 the target of a cast.
9199 Returns a representation of the expression. */
9201 static cp_expr
9202 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9203 bool decltype_p, cp_id_kind * pidk)
9205 /* If it's a `(', then we might be looking at a cast. */
9206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9208 tree type = NULL_TREE;
9209 cp_expr expr (NULL_TREE);
9210 int cast_expression = 0;
9211 const char *saved_message;
9213 /* There's no way to know yet whether or not this is a cast.
9214 For example, `(int (3))' is a unary-expression, while `(int)
9215 3' is a cast. So, we resort to parsing tentatively. */
9216 cp_parser_parse_tentatively (parser);
9217 /* Types may not be defined in a cast. */
9218 saved_message = parser->type_definition_forbidden_message;
9219 parser->type_definition_forbidden_message
9220 = G_("types may not be defined in casts");
9221 /* Consume the `('. */
9222 matching_parens parens;
9223 cp_token *open_paren = parens.consume_open (parser);
9224 location_t open_paren_loc = open_paren->location;
9225 location_t close_paren_loc = UNKNOWN_LOCATION;
9227 /* A very tricky bit is that `(struct S) { 3 }' is a
9228 compound-literal (which we permit in C++ as an extension).
9229 But, that construct is not a cast-expression -- it is a
9230 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9231 is legal; if the compound-literal were a cast-expression,
9232 you'd need an extra set of parentheses.) But, if we parse
9233 the type-id, and it happens to be a class-specifier, then we
9234 will commit to the parse at that point, because we cannot
9235 undo the action that is done when creating a new class. So,
9236 then we cannot back up and do a postfix-expression.
9238 Another tricky case is the following (c++/29234):
9240 struct S { void operator () (); };
9242 void foo ()
9244 ( S()() );
9247 As a type-id we parse the parenthesized S()() as a function
9248 returning a function, groktypename complains and we cannot
9249 back up in this case either.
9251 Therefore, we scan ahead to the closing `)', and check to see
9252 if the tokens after the `)' can start a cast-expression. Otherwise
9253 we are dealing with an unary-expression, a postfix-expression
9254 or something else.
9256 Yet another tricky case, in C++11, is the following (c++/54891):
9258 (void)[]{};
9260 The issue is that usually, besides the case of lambda-expressions,
9261 the parenthesized type-id cannot be followed by '[', and, eg, we
9262 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9263 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9264 we don't commit, we try a cast-expression, then an unary-expression.
9266 Save tokens so that we can put them back. */
9267 cp_lexer_save_tokens (parser->lexer);
9269 /* We may be looking at a cast-expression. */
9270 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9271 /*consume_paren=*/true))
9272 cast_expression
9273 = cp_parser_tokens_start_cast_expression (parser);
9275 /* Roll back the tokens we skipped. */
9276 cp_lexer_rollback_tokens (parser->lexer);
9277 /* If we aren't looking at a cast-expression, simulate an error so
9278 that the call to cp_parser_error_occurred below returns true. */
9279 if (!cast_expression)
9280 cp_parser_simulate_error (parser);
9281 else
9283 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9284 parser->in_type_id_in_expr_p = true;
9285 /* Look for the type-id. */
9286 type = cp_parser_type_id (parser);
9287 /* Look for the closing `)'. */
9288 cp_token *close_paren = parens.require_close (parser);
9289 if (close_paren)
9290 close_paren_loc = close_paren->location;
9291 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9294 /* Restore the saved message. */
9295 parser->type_definition_forbidden_message = saved_message;
9297 /* At this point this can only be either a cast or a
9298 parenthesized ctor such as `(T ())' that looks like a cast to
9299 function returning T. */
9300 if (!cp_parser_error_occurred (parser))
9302 /* Only commit if the cast-expression doesn't start with
9303 '++', '--', or '[' in C++11. */
9304 if (cast_expression > 0)
9305 cp_parser_commit_to_topmost_tentative_parse (parser);
9307 expr = cp_parser_cast_expression (parser,
9308 /*address_p=*/false,
9309 /*cast_p=*/true,
9310 /*decltype_p=*/false,
9311 pidk);
9313 if (cp_parser_parse_definitely (parser))
9315 /* Warn about old-style casts, if so requested. */
9316 if (warn_old_style_cast
9317 && !in_system_header_at (input_location)
9318 && !VOID_TYPE_P (type)
9319 && current_lang_name != lang_name_c)
9321 gcc_rich_location rich_loc (input_location);
9322 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9323 expr, type);
9324 warning_at (&rich_loc, OPT_Wold_style_cast,
9325 "use of old-style cast to %q#T", type);
9328 /* Only type conversions to integral or enumeration types
9329 can be used in constant-expressions. */
9330 if (!cast_valid_in_integral_constant_expression_p (type)
9331 && cp_parser_non_integral_constant_expression (parser,
9332 NIC_CAST))
9333 return error_mark_node;
9335 /* Perform the cast. */
9336 /* Make a location:
9337 (TYPE) EXPR
9338 ^~~~~~~~~~~
9339 with start==caret at the open paren, extending to the
9340 end of "expr". */
9341 location_t cast_loc = make_location (open_paren_loc,
9342 open_paren_loc,
9343 expr.get_finish ());
9344 expr = build_c_cast (cast_loc, type, expr);
9345 return expr;
9348 else
9349 cp_parser_abort_tentative_parse (parser);
9352 /* If we get here, then it's not a cast, so it must be a
9353 unary-expression. */
9354 return cp_parser_unary_expression (parser, pidk, address_p,
9355 cast_p, decltype_p);
9358 /* Parse a binary expression of the general form:
9360 pm-expression:
9361 cast-expression
9362 pm-expression .* cast-expression
9363 pm-expression ->* cast-expression
9365 multiplicative-expression:
9366 pm-expression
9367 multiplicative-expression * pm-expression
9368 multiplicative-expression / pm-expression
9369 multiplicative-expression % pm-expression
9371 additive-expression:
9372 multiplicative-expression
9373 additive-expression + multiplicative-expression
9374 additive-expression - multiplicative-expression
9376 shift-expression:
9377 additive-expression
9378 shift-expression << additive-expression
9379 shift-expression >> additive-expression
9381 relational-expression:
9382 shift-expression
9383 relational-expression < shift-expression
9384 relational-expression > shift-expression
9385 relational-expression <= shift-expression
9386 relational-expression >= shift-expression
9388 GNU Extension:
9390 relational-expression:
9391 relational-expression <? shift-expression
9392 relational-expression >? shift-expression
9394 equality-expression:
9395 relational-expression
9396 equality-expression == relational-expression
9397 equality-expression != relational-expression
9399 and-expression:
9400 equality-expression
9401 and-expression & equality-expression
9403 exclusive-or-expression:
9404 and-expression
9405 exclusive-or-expression ^ and-expression
9407 inclusive-or-expression:
9408 exclusive-or-expression
9409 inclusive-or-expression | exclusive-or-expression
9411 logical-and-expression:
9412 inclusive-or-expression
9413 logical-and-expression && inclusive-or-expression
9415 logical-or-expression:
9416 logical-and-expression
9417 logical-or-expression || logical-and-expression
9419 All these are implemented with a single function like:
9421 binary-expression:
9422 simple-cast-expression
9423 binary-expression <token> binary-expression
9425 CAST_P is true if this expression is the target of a cast.
9427 The binops_by_token map is used to get the tree codes for each <token> type.
9428 binary-expressions are associated according to a precedence table. */
9430 #define TOKEN_PRECEDENCE(token) \
9431 (((token->type == CPP_GREATER \
9432 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9433 && !parser->greater_than_is_operator_p) \
9434 ? PREC_NOT_OPERATOR \
9435 : binops_by_token[token->type].prec)
9437 static cp_expr
9438 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9439 bool no_toplevel_fold_p,
9440 bool decltype_p,
9441 enum cp_parser_prec prec,
9442 cp_id_kind * pidk)
9444 cp_parser_expression_stack stack;
9445 cp_parser_expression_stack_entry *sp = &stack[0];
9446 cp_parser_expression_stack_entry current;
9447 cp_expr rhs;
9448 cp_token *token;
9449 enum tree_code rhs_type;
9450 enum cp_parser_prec new_prec, lookahead_prec;
9451 tree overload;
9453 /* Parse the first expression. */
9454 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9455 ? TRUTH_NOT_EXPR : ERROR_MARK);
9456 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9457 cast_p, decltype_p, pidk);
9458 current.prec = prec;
9460 if (cp_parser_error_occurred (parser))
9461 return error_mark_node;
9463 for (;;)
9465 /* Get an operator token. */
9466 token = cp_lexer_peek_token (parser->lexer);
9468 if (warn_cxx11_compat
9469 && token->type == CPP_RSHIFT
9470 && !parser->greater_than_is_operator_p)
9472 if (warning_at (token->location, OPT_Wc__11_compat,
9473 "%<>>%> operator is treated"
9474 " as two right angle brackets in C++11"))
9475 inform (token->location,
9476 "suggest parentheses around %<>>%> expression");
9479 new_prec = TOKEN_PRECEDENCE (token);
9480 if (new_prec != PREC_NOT_OPERATOR
9481 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9482 /* This is a fold-expression; handle it later. */
9483 new_prec = PREC_NOT_OPERATOR;
9485 /* Popping an entry off the stack means we completed a subexpression:
9486 - either we found a token which is not an operator (`>' where it is not
9487 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9488 will happen repeatedly;
9489 - or, we found an operator which has lower priority. This is the case
9490 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9491 parsing `3 * 4'. */
9492 if (new_prec <= current.prec)
9494 if (sp == stack)
9495 break;
9496 else
9497 goto pop;
9500 get_rhs:
9501 current.tree_type = binops_by_token[token->type].tree_type;
9502 current.loc = token->location;
9504 /* We used the operator token. */
9505 cp_lexer_consume_token (parser->lexer);
9507 /* For "false && x" or "true || x", x will never be executed;
9508 disable warnings while evaluating it. */
9509 if (current.tree_type == TRUTH_ANDIF_EXPR)
9510 c_inhibit_evaluation_warnings +=
9511 cp_fully_fold (current.lhs) == truthvalue_false_node;
9512 else if (current.tree_type == TRUTH_ORIF_EXPR)
9513 c_inhibit_evaluation_warnings +=
9514 cp_fully_fold (current.lhs) == truthvalue_true_node;
9516 /* Extract another operand. It may be the RHS of this expression
9517 or the LHS of a new, higher priority expression. */
9518 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9519 ? TRUTH_NOT_EXPR : ERROR_MARK);
9520 rhs = cp_parser_simple_cast_expression (parser);
9522 /* Get another operator token. Look up its precedence to avoid
9523 building a useless (immediately popped) stack entry for common
9524 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9525 token = cp_lexer_peek_token (parser->lexer);
9526 lookahead_prec = TOKEN_PRECEDENCE (token);
9527 if (lookahead_prec != PREC_NOT_OPERATOR
9528 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9529 lookahead_prec = PREC_NOT_OPERATOR;
9530 if (lookahead_prec > new_prec)
9532 /* ... and prepare to parse the RHS of the new, higher priority
9533 expression. Since precedence levels on the stack are
9534 monotonically increasing, we do not have to care about
9535 stack overflows. */
9536 *sp = current;
9537 ++sp;
9538 current.lhs = rhs;
9539 current.lhs_type = rhs_type;
9540 current.prec = new_prec;
9541 new_prec = lookahead_prec;
9542 goto get_rhs;
9544 pop:
9545 lookahead_prec = new_prec;
9546 /* If the stack is not empty, we have parsed into LHS the right side
9547 (`4' in the example above) of an expression we had suspended.
9548 We can use the information on the stack to recover the LHS (`3')
9549 from the stack together with the tree code (`MULT_EXPR'), and
9550 the precedence of the higher level subexpression
9551 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9552 which will be used to actually build the additive expression. */
9553 rhs = current.lhs;
9554 rhs_type = current.lhs_type;
9555 --sp;
9556 current = *sp;
9559 /* Undo the disabling of warnings done above. */
9560 if (current.tree_type == TRUTH_ANDIF_EXPR)
9561 c_inhibit_evaluation_warnings -=
9562 cp_fully_fold (current.lhs) == truthvalue_false_node;
9563 else if (current.tree_type == TRUTH_ORIF_EXPR)
9564 c_inhibit_evaluation_warnings -=
9565 cp_fully_fold (current.lhs) == truthvalue_true_node;
9567 if (warn_logical_not_paren
9568 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9569 && current.lhs_type == TRUTH_NOT_EXPR
9570 /* Avoid warning for !!x == y. */
9571 && (TREE_CODE (current.lhs) != NE_EXPR
9572 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9573 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9574 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9575 /* Avoid warning for !b == y where b is boolean. */
9576 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9577 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9578 != BOOLEAN_TYPE))))
9579 /* Avoid warning for !!b == y where b is boolean. */
9580 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9581 || TREE_TYPE (current.lhs) == NULL_TREE
9582 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9583 warn_logical_not_parentheses (current.loc, current.tree_type,
9584 current.lhs, maybe_constant_value (rhs));
9586 overload = NULL;
9588 location_t combined_loc = make_location (current.loc,
9589 current.lhs.get_start (),
9590 rhs.get_finish ());
9592 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9593 ERROR_MARK for everything that is not a binary expression.
9594 This makes warn_about_parentheses miss some warnings that
9595 involve unary operators. For unary expressions we should
9596 pass the correct tree_code unless the unary expression was
9597 surrounded by parentheses.
9599 if (no_toplevel_fold_p
9600 && lookahead_prec <= current.prec
9601 && sp == stack)
9603 if (current.lhs == error_mark_node || rhs == error_mark_node)
9604 current.lhs = error_mark_node;
9605 else
9607 current.lhs
9608 = build_min (current.tree_type,
9609 TREE_CODE_CLASS (current.tree_type)
9610 == tcc_comparison
9611 ? boolean_type_node : TREE_TYPE (current.lhs),
9612 current.lhs.get_value (), rhs.get_value ());
9613 SET_EXPR_LOCATION (current.lhs, combined_loc);
9616 else
9618 op_location_t op_loc (current.loc, combined_loc);
9619 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9620 current.lhs, current.lhs_type,
9621 rhs, rhs_type, &overload,
9622 complain_flags (decltype_p));
9623 /* TODO: build_x_binary_op doesn't always honor the location. */
9624 current.lhs.set_location (combined_loc);
9626 current.lhs_type = current.tree_type;
9628 /* If the binary operator required the use of an overloaded operator,
9629 then this expression cannot be an integral constant-expression.
9630 An overloaded operator can be used even if both operands are
9631 otherwise permissible in an integral constant-expression if at
9632 least one of the operands is of enumeration type. */
9634 if (overload
9635 && cp_parser_non_integral_constant_expression (parser,
9636 NIC_OVERLOADED))
9637 return error_mark_node;
9640 return current.lhs;
9643 static cp_expr
9644 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9645 bool no_toplevel_fold_p,
9646 enum cp_parser_prec prec,
9647 cp_id_kind * pidk)
9649 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9650 /*decltype*/false, prec, pidk);
9653 /* Parse the `? expression : assignment-expression' part of a
9654 conditional-expression. The LOGICAL_OR_EXPR is the
9655 logical-or-expression that started the conditional-expression.
9656 Returns a representation of the entire conditional-expression.
9658 This routine is used by cp_parser_assignment_expression.
9660 ? expression : assignment-expression
9662 GNU Extensions:
9664 ? : assignment-expression */
9666 static tree
9667 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9669 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9670 cp_expr assignment_expr;
9671 struct cp_token *token;
9672 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9674 /* Consume the `?' token. */
9675 cp_lexer_consume_token (parser->lexer);
9676 token = cp_lexer_peek_token (parser->lexer);
9677 if (cp_parser_allow_gnu_extensions_p (parser)
9678 && token->type == CPP_COLON)
9680 pedwarn (token->location, OPT_Wpedantic,
9681 "ISO C++ does not allow ?: with omitted middle operand");
9682 /* Implicit true clause. */
9683 expr = NULL_TREE;
9684 c_inhibit_evaluation_warnings +=
9685 folded_logical_or_expr == truthvalue_true_node;
9686 warn_for_omitted_condop (token->location, logical_or_expr);
9688 else
9690 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9691 parser->colon_corrects_to_scope_p = false;
9692 /* Parse the expression. */
9693 c_inhibit_evaluation_warnings +=
9694 folded_logical_or_expr == truthvalue_false_node;
9695 expr = cp_parser_expression (parser);
9696 c_inhibit_evaluation_warnings +=
9697 ((folded_logical_or_expr == truthvalue_true_node)
9698 - (folded_logical_or_expr == truthvalue_false_node));
9699 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9702 /* The next token should be a `:'. */
9703 cp_parser_require (parser, CPP_COLON, RT_COLON);
9704 /* Parse the assignment-expression. */
9705 assignment_expr = cp_parser_assignment_expression (parser);
9706 c_inhibit_evaluation_warnings -=
9707 folded_logical_or_expr == truthvalue_true_node;
9709 /* Make a location:
9710 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9711 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9712 with the caret at the "?", ranging from the start of
9713 the logical_or_expr to the end of the assignment_expr. */
9714 loc = make_location (loc,
9715 logical_or_expr.get_start (),
9716 assignment_expr.get_finish ());
9718 /* Build the conditional-expression. */
9719 return build_x_conditional_expr (loc, logical_or_expr,
9720 expr,
9721 assignment_expr,
9722 tf_warning_or_error);
9725 /* Parse an assignment-expression.
9727 assignment-expression:
9728 conditional-expression
9729 logical-or-expression assignment-operator assignment_expression
9730 throw-expression
9732 CAST_P is true if this expression is the target of a cast.
9733 DECLTYPE_P is true if this expression is the operand of decltype.
9735 Returns a representation for the expression. */
9737 static cp_expr
9738 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9739 bool cast_p, bool decltype_p)
9741 cp_expr expr;
9743 /* If the next token is the `throw' keyword, then we're looking at
9744 a throw-expression. */
9745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9746 expr = cp_parser_throw_expression (parser);
9747 /* Otherwise, it must be that we are looking at a
9748 logical-or-expression. */
9749 else
9751 /* Parse the binary expressions (logical-or-expression). */
9752 expr = cp_parser_binary_expression (parser, cast_p, false,
9753 decltype_p,
9754 PREC_NOT_OPERATOR, pidk);
9755 /* If the next token is a `?' then we're actually looking at a
9756 conditional-expression. */
9757 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9758 return cp_parser_question_colon_clause (parser, expr);
9759 else
9761 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9763 /* If it's an assignment-operator, we're using the second
9764 production. */
9765 enum tree_code assignment_operator
9766 = cp_parser_assignment_operator_opt (parser);
9767 if (assignment_operator != ERROR_MARK)
9769 bool non_constant_p;
9771 /* Parse the right-hand side of the assignment. */
9772 cp_expr rhs = cp_parser_initializer_clause (parser,
9773 &non_constant_p);
9775 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9776 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9778 /* An assignment may not appear in a
9779 constant-expression. */
9780 if (cp_parser_non_integral_constant_expression (parser,
9781 NIC_ASSIGNMENT))
9782 return error_mark_node;
9783 /* Build the assignment expression. Its default
9784 location:
9785 LHS = RHS
9786 ~~~~^~~~~
9787 is the location of the '=' token as the
9788 caret, ranging from the start of the lhs to the
9789 end of the rhs. */
9790 loc = make_location (loc,
9791 expr.get_start (),
9792 rhs.get_finish ());
9793 expr = build_x_modify_expr (loc, expr,
9794 assignment_operator,
9795 rhs,
9796 complain_flags (decltype_p));
9797 /* TODO: build_x_modify_expr doesn't honor the location,
9798 so we must set it here. */
9799 expr.set_location (loc);
9804 return expr;
9807 /* Parse an (optional) assignment-operator.
9809 assignment-operator: one of
9810 = *= /= %= += -= >>= <<= &= ^= |=
9812 GNU Extension:
9814 assignment-operator: one of
9815 <?= >?=
9817 If the next token is an assignment operator, the corresponding tree
9818 code is returned, and the token is consumed. For example, for
9819 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9820 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9821 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9822 operator, ERROR_MARK is returned. */
9824 static enum tree_code
9825 cp_parser_assignment_operator_opt (cp_parser* parser)
9827 enum tree_code op;
9828 cp_token *token;
9830 /* Peek at the next token. */
9831 token = cp_lexer_peek_token (parser->lexer);
9833 switch (token->type)
9835 case CPP_EQ:
9836 op = NOP_EXPR;
9837 break;
9839 case CPP_MULT_EQ:
9840 op = MULT_EXPR;
9841 break;
9843 case CPP_DIV_EQ:
9844 op = TRUNC_DIV_EXPR;
9845 break;
9847 case CPP_MOD_EQ:
9848 op = TRUNC_MOD_EXPR;
9849 break;
9851 case CPP_PLUS_EQ:
9852 op = PLUS_EXPR;
9853 break;
9855 case CPP_MINUS_EQ:
9856 op = MINUS_EXPR;
9857 break;
9859 case CPP_RSHIFT_EQ:
9860 op = RSHIFT_EXPR;
9861 break;
9863 case CPP_LSHIFT_EQ:
9864 op = LSHIFT_EXPR;
9865 break;
9867 case CPP_AND_EQ:
9868 op = BIT_AND_EXPR;
9869 break;
9871 case CPP_XOR_EQ:
9872 op = BIT_XOR_EXPR;
9873 break;
9875 case CPP_OR_EQ:
9876 op = BIT_IOR_EXPR;
9877 break;
9879 default:
9880 /* Nothing else is an assignment operator. */
9881 op = ERROR_MARK;
9884 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9885 if (op != ERROR_MARK
9886 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9887 op = ERROR_MARK;
9889 /* If it was an assignment operator, consume it. */
9890 if (op != ERROR_MARK)
9891 cp_lexer_consume_token (parser->lexer);
9893 return op;
9896 /* Parse an expression.
9898 expression:
9899 assignment-expression
9900 expression , assignment-expression
9902 CAST_P is true if this expression is the target of a cast.
9903 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9904 except possibly parenthesized or on the RHS of a comma (N3276).
9906 Returns a representation of the expression. */
9908 static cp_expr
9909 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9910 bool cast_p, bool decltype_p)
9912 cp_expr expression = NULL_TREE;
9913 location_t loc = UNKNOWN_LOCATION;
9915 while (true)
9917 cp_expr assignment_expression;
9919 /* Parse the next assignment-expression. */
9920 assignment_expression
9921 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9923 /* We don't create a temporary for a call that is the immediate operand
9924 of decltype or on the RHS of a comma. But when we see a comma, we
9925 need to create a temporary for a call on the LHS. */
9926 if (decltype_p && !processing_template_decl
9927 && TREE_CODE (assignment_expression) == CALL_EXPR
9928 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9929 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9930 assignment_expression
9931 = build_cplus_new (TREE_TYPE (assignment_expression),
9932 assignment_expression, tf_warning_or_error);
9934 /* If this is the first assignment-expression, we can just
9935 save it away. */
9936 if (!expression)
9937 expression = assignment_expression;
9938 else
9940 /* Create a location with caret at the comma, ranging
9941 from the start of the LHS to the end of the RHS. */
9942 loc = make_location (loc,
9943 expression.get_start (),
9944 assignment_expression.get_finish ());
9945 expression = build_x_compound_expr (loc, expression,
9946 assignment_expression,
9947 complain_flags (decltype_p));
9948 expression.set_location (loc);
9950 /* If the next token is not a comma, or we're in a fold-expression, then
9951 we are done with the expression. */
9952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9953 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9954 break;
9955 /* Consume the `,'. */
9956 loc = cp_lexer_peek_token (parser->lexer)->location;
9957 cp_lexer_consume_token (parser->lexer);
9958 /* A comma operator cannot appear in a constant-expression. */
9959 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9960 expression = error_mark_node;
9963 return expression;
9966 /* Parse a constant-expression.
9968 constant-expression:
9969 conditional-expression
9971 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9972 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9973 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9974 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9975 only parse a conditional-expression, otherwise parse an
9976 assignment-expression. See below for rationale. */
9978 static cp_expr
9979 cp_parser_constant_expression (cp_parser* parser,
9980 bool allow_non_constant_p,
9981 bool *non_constant_p,
9982 bool strict_p)
9984 bool saved_integral_constant_expression_p;
9985 bool saved_allow_non_integral_constant_expression_p;
9986 bool saved_non_integral_constant_expression_p;
9987 cp_expr expression;
9989 /* It might seem that we could simply parse the
9990 conditional-expression, and then check to see if it were
9991 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9992 one that the compiler can figure out is constant, possibly after
9993 doing some simplifications or optimizations. The standard has a
9994 precise definition of constant-expression, and we must honor
9995 that, even though it is somewhat more restrictive.
9997 For example:
9999 int i[(2, 3)];
10001 is not a legal declaration, because `(2, 3)' is not a
10002 constant-expression. The `,' operator is forbidden in a
10003 constant-expression. However, GCC's constant-folding machinery
10004 will fold this operation to an INTEGER_CST for `3'. */
10006 /* Save the old settings. */
10007 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10008 saved_allow_non_integral_constant_expression_p
10009 = parser->allow_non_integral_constant_expression_p;
10010 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10011 /* We are now parsing a constant-expression. */
10012 parser->integral_constant_expression_p = true;
10013 parser->allow_non_integral_constant_expression_p
10014 = (allow_non_constant_p || cxx_dialect >= cxx11);
10015 parser->non_integral_constant_expression_p = false;
10016 /* Although the grammar says "conditional-expression", when not STRICT_P,
10017 we parse an "assignment-expression", which also permits
10018 "throw-expression" and the use of assignment operators. In the case
10019 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10020 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10021 actually essential that we look for an assignment-expression.
10022 For example, cp_parser_initializer_clauses uses this function to
10023 determine whether a particular assignment-expression is in fact
10024 constant. */
10025 if (strict_p)
10027 /* Parse the binary expressions (logical-or-expression). */
10028 expression = cp_parser_binary_expression (parser, false, false, false,
10029 PREC_NOT_OPERATOR, NULL);
10030 /* If the next token is a `?' then we're actually looking at
10031 a conditional-expression; otherwise we're done. */
10032 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10033 expression = cp_parser_question_colon_clause (parser, expression);
10035 else
10036 expression = cp_parser_assignment_expression (parser);
10037 /* Restore the old settings. */
10038 parser->integral_constant_expression_p
10039 = saved_integral_constant_expression_p;
10040 parser->allow_non_integral_constant_expression_p
10041 = saved_allow_non_integral_constant_expression_p;
10042 if (cxx_dialect >= cxx11)
10044 /* Require an rvalue constant expression here; that's what our
10045 callers expect. Reference constant expressions are handled
10046 separately in e.g. cp_parser_template_argument. */
10047 tree decay = expression;
10048 if (TREE_TYPE (expression)
10049 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10050 decay = build_address (expression);
10051 bool is_const = potential_rvalue_constant_expression (decay);
10052 parser->non_integral_constant_expression_p = !is_const;
10053 if (!is_const && !allow_non_constant_p)
10054 require_potential_rvalue_constant_expression (decay);
10056 if (allow_non_constant_p)
10057 *non_constant_p = parser->non_integral_constant_expression_p;
10058 parser->non_integral_constant_expression_p
10059 = saved_non_integral_constant_expression_p;
10061 return expression;
10064 /* Parse __builtin_offsetof.
10066 offsetof-expression:
10067 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10069 offsetof-member-designator:
10070 id-expression
10071 | offsetof-member-designator "." id-expression
10072 | offsetof-member-designator "[" expression "]"
10073 | offsetof-member-designator "->" id-expression */
10075 static cp_expr
10076 cp_parser_builtin_offsetof (cp_parser *parser)
10078 int save_ice_p, save_non_ice_p;
10079 tree type;
10080 cp_expr expr;
10081 cp_id_kind dummy;
10082 cp_token *token;
10083 location_t finish_loc;
10085 /* We're about to accept non-integral-constant things, but will
10086 definitely yield an integral constant expression. Save and
10087 restore these values around our local parsing. */
10088 save_ice_p = parser->integral_constant_expression_p;
10089 save_non_ice_p = parser->non_integral_constant_expression_p;
10091 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10093 /* Consume the "__builtin_offsetof" token. */
10094 cp_lexer_consume_token (parser->lexer);
10095 /* Consume the opening `('. */
10096 matching_parens parens;
10097 parens.require_open (parser);
10098 /* Parse the type-id. */
10099 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10101 const char *saved_message = parser->type_definition_forbidden_message;
10102 parser->type_definition_forbidden_message
10103 = G_("types may not be defined within __builtin_offsetof");
10104 type = cp_parser_type_id (parser);
10105 parser->type_definition_forbidden_message = saved_message;
10107 /* Look for the `,'. */
10108 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10109 token = cp_lexer_peek_token (parser->lexer);
10111 /* Build the (type *)null that begins the traditional offsetof macro. */
10112 tree object_ptr
10113 = build_static_cast (build_pointer_type (type), null_pointer_node,
10114 tf_warning_or_error);
10116 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10117 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10118 true, &dummy, token->location);
10119 while (true)
10121 token = cp_lexer_peek_token (parser->lexer);
10122 switch (token->type)
10124 case CPP_OPEN_SQUARE:
10125 /* offsetof-member-designator "[" expression "]" */
10126 expr = cp_parser_postfix_open_square_expression (parser, expr,
10127 true, false);
10128 break;
10130 case CPP_DEREF:
10131 /* offsetof-member-designator "->" identifier */
10132 expr = grok_array_decl (token->location, expr,
10133 integer_zero_node, false);
10134 /* FALLTHRU */
10136 case CPP_DOT:
10137 /* offsetof-member-designator "." identifier */
10138 cp_lexer_consume_token (parser->lexer);
10139 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10140 expr, true, &dummy,
10141 token->location);
10142 break;
10144 case CPP_CLOSE_PAREN:
10145 /* Consume the ")" token. */
10146 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10147 cp_lexer_consume_token (parser->lexer);
10148 goto success;
10150 default:
10151 /* Error. We know the following require will fail, but
10152 that gives the proper error message. */
10153 parens.require_close (parser);
10154 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10155 expr = error_mark_node;
10156 goto failure;
10160 success:
10161 /* Make a location of the form:
10162 __builtin_offsetof (struct s, f)
10163 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10164 with caret at the type-id, ranging from the start of the
10165 "_builtin_offsetof" token to the close paren. */
10166 loc = make_location (loc, start_loc, finish_loc);
10167 /* The result will be an INTEGER_CST, so we need to explicitly
10168 preserve the location. */
10169 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10171 failure:
10172 parser->integral_constant_expression_p = save_ice_p;
10173 parser->non_integral_constant_expression_p = save_non_ice_p;
10175 expr = expr.maybe_add_location_wrapper ();
10176 return expr;
10179 /* Parse a trait expression.
10181 Returns a representation of the expression, the underlying type
10182 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10184 static cp_expr
10185 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10187 cp_trait_kind kind;
10188 tree type1, type2 = NULL_TREE;
10189 bool binary = false;
10190 bool variadic = false;
10192 switch (keyword)
10194 case RID_HAS_NOTHROW_ASSIGN:
10195 kind = CPTK_HAS_NOTHROW_ASSIGN;
10196 break;
10197 case RID_HAS_NOTHROW_CONSTRUCTOR:
10198 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10199 break;
10200 case RID_HAS_NOTHROW_COPY:
10201 kind = CPTK_HAS_NOTHROW_COPY;
10202 break;
10203 case RID_HAS_TRIVIAL_ASSIGN:
10204 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10205 break;
10206 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10207 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10208 break;
10209 case RID_HAS_TRIVIAL_COPY:
10210 kind = CPTK_HAS_TRIVIAL_COPY;
10211 break;
10212 case RID_HAS_TRIVIAL_DESTRUCTOR:
10213 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10214 break;
10215 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10216 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10217 break;
10218 case RID_HAS_VIRTUAL_DESTRUCTOR:
10219 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10220 break;
10221 case RID_IS_ABSTRACT:
10222 kind = CPTK_IS_ABSTRACT;
10223 break;
10224 case RID_IS_AGGREGATE:
10225 kind = CPTK_IS_AGGREGATE;
10226 break;
10227 case RID_IS_BASE_OF:
10228 kind = CPTK_IS_BASE_OF;
10229 binary = true;
10230 break;
10231 case RID_IS_CLASS:
10232 kind = CPTK_IS_CLASS;
10233 break;
10234 case RID_IS_EMPTY:
10235 kind = CPTK_IS_EMPTY;
10236 break;
10237 case RID_IS_ENUM:
10238 kind = CPTK_IS_ENUM;
10239 break;
10240 case RID_IS_FINAL:
10241 kind = CPTK_IS_FINAL;
10242 break;
10243 case RID_IS_LITERAL_TYPE:
10244 kind = CPTK_IS_LITERAL_TYPE;
10245 break;
10246 case RID_IS_POD:
10247 kind = CPTK_IS_POD;
10248 break;
10249 case RID_IS_POLYMORPHIC:
10250 kind = CPTK_IS_POLYMORPHIC;
10251 break;
10252 case RID_IS_SAME_AS:
10253 kind = CPTK_IS_SAME_AS;
10254 binary = true;
10255 break;
10256 case RID_IS_STD_LAYOUT:
10257 kind = CPTK_IS_STD_LAYOUT;
10258 break;
10259 case RID_IS_TRIVIAL:
10260 kind = CPTK_IS_TRIVIAL;
10261 break;
10262 case RID_IS_TRIVIALLY_ASSIGNABLE:
10263 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10264 binary = true;
10265 break;
10266 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10267 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10268 variadic = true;
10269 break;
10270 case RID_IS_TRIVIALLY_COPYABLE:
10271 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10272 break;
10273 case RID_IS_UNION:
10274 kind = CPTK_IS_UNION;
10275 break;
10276 case RID_UNDERLYING_TYPE:
10277 kind = CPTK_UNDERLYING_TYPE;
10278 break;
10279 case RID_BASES:
10280 kind = CPTK_BASES;
10281 break;
10282 case RID_DIRECT_BASES:
10283 kind = CPTK_DIRECT_BASES;
10284 break;
10285 case RID_IS_ASSIGNABLE:
10286 kind = CPTK_IS_ASSIGNABLE;
10287 binary = true;
10288 break;
10289 case RID_IS_CONSTRUCTIBLE:
10290 kind = CPTK_IS_CONSTRUCTIBLE;
10291 variadic = true;
10292 break;
10293 default:
10294 gcc_unreachable ();
10297 /* Get location of initial token. */
10298 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10300 /* Consume the token. */
10301 cp_lexer_consume_token (parser->lexer);
10303 matching_parens parens;
10304 parens.require_open (parser);
10307 type_id_in_expr_sentinel s (parser);
10308 type1 = cp_parser_type_id (parser);
10311 if (type1 == error_mark_node)
10312 return error_mark_node;
10314 if (binary)
10316 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10319 type_id_in_expr_sentinel s (parser);
10320 type2 = cp_parser_type_id (parser);
10323 if (type2 == error_mark_node)
10324 return error_mark_node;
10326 else if (variadic)
10328 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10330 cp_lexer_consume_token (parser->lexer);
10331 tree elt = cp_parser_type_id (parser);
10332 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10334 cp_lexer_consume_token (parser->lexer);
10335 elt = make_pack_expansion (elt);
10337 if (elt == error_mark_node)
10338 return error_mark_node;
10339 type2 = tree_cons (NULL_TREE, elt, type2);
10343 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10344 parens.require_close (parser);
10346 /* Construct a location of the form:
10347 __is_trivially_copyable(_Tp)
10348 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10349 with start == caret, finishing at the close-paren. */
10350 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10352 /* Complete the trait expression, which may mean either processing
10353 the trait expr now or saving it for template instantiation. */
10354 switch (kind)
10356 case CPTK_UNDERLYING_TYPE:
10357 return cp_expr (finish_underlying_type (type1), trait_loc);
10358 case CPTK_BASES:
10359 return cp_expr (finish_bases (type1, false), trait_loc);
10360 case CPTK_DIRECT_BASES:
10361 return cp_expr (finish_bases (type1, true), trait_loc);
10362 default:
10363 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10367 /* Parse a lambda expression.
10369 lambda-expression:
10370 lambda-introducer lambda-declarator [opt] compound-statement
10372 Returns a representation of the expression. */
10374 static cp_expr
10375 cp_parser_lambda_expression (cp_parser* parser)
10377 tree lambda_expr = build_lambda_expr ();
10378 tree type;
10379 bool ok = true;
10380 cp_token *token = cp_lexer_peek_token (parser->lexer);
10381 cp_token_position start = 0;
10383 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10385 if (cxx_dialect >= cxx2a)
10386 /* C++20 allows lambdas in unevaluated context. */;
10387 else if (cp_unevaluated_operand)
10389 if (!token->error_reported)
10391 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10392 "lambda-expression in unevaluated context"
10393 " only available with -std=c++2a or -std=gnu++2a");
10394 token->error_reported = true;
10396 ok = false;
10398 else if (parser->in_template_argument_list_p)
10400 if (!token->error_reported)
10402 error_at (token->location, "lambda-expression in template-argument"
10403 " only available with -std=c++2a or -std=gnu++2a");
10404 token->error_reported = true;
10406 ok = false;
10409 /* We may be in the middle of deferred access check. Disable
10410 it now. */
10411 push_deferring_access_checks (dk_no_deferred);
10413 cp_parser_lambda_introducer (parser, lambda_expr);
10414 if (cp_parser_error_occurred (parser))
10415 return error_mark_node;
10417 type = begin_lambda_type (lambda_expr);
10418 if (type == error_mark_node)
10419 return error_mark_node;
10421 record_lambda_scope (lambda_expr);
10423 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10424 determine_visibility (TYPE_NAME (type));
10426 /* Now that we've started the type, add the capture fields for any
10427 explicit captures. */
10428 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10431 /* Inside the class, surrounding template-parameter-lists do not apply. */
10432 unsigned int saved_num_template_parameter_lists
10433 = parser->num_template_parameter_lists;
10434 unsigned char in_statement = parser->in_statement;
10435 bool in_switch_statement_p = parser->in_switch_statement_p;
10436 bool fully_implicit_function_template_p
10437 = parser->fully_implicit_function_template_p;
10438 tree implicit_template_parms = parser->implicit_template_parms;
10439 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10440 bool auto_is_implicit_function_template_parm_p
10441 = parser->auto_is_implicit_function_template_parm_p;
10443 parser->num_template_parameter_lists = 0;
10444 parser->in_statement = 0;
10445 parser->in_switch_statement_p = false;
10446 parser->fully_implicit_function_template_p = false;
10447 parser->implicit_template_parms = 0;
10448 parser->implicit_template_scope = 0;
10449 parser->auto_is_implicit_function_template_parm_p = false;
10451 /* By virtue of defining a local class, a lambda expression has access to
10452 the private variables of enclosing classes. */
10454 if (cp_parser_start_tentative_firewall (parser))
10455 start = token;
10457 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10459 if (ok && cp_parser_error_occurred (parser))
10460 ok = false;
10462 if (ok)
10464 cp_parser_lambda_body (parser, lambda_expr);
10466 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10468 if (cp_parser_skip_to_closing_brace (parser))
10469 cp_lexer_consume_token (parser->lexer);
10472 /* The capture list was built up in reverse order; fix that now. */
10473 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10474 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10476 if (ok)
10477 maybe_add_lambda_conv_op (type);
10479 type = finish_struct (type, /*attributes=*/NULL_TREE);
10481 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10482 parser->in_statement = in_statement;
10483 parser->in_switch_statement_p = in_switch_statement_p;
10484 parser->fully_implicit_function_template_p
10485 = fully_implicit_function_template_p;
10486 parser->implicit_template_parms = implicit_template_parms;
10487 parser->implicit_template_scope = implicit_template_scope;
10488 parser->auto_is_implicit_function_template_parm_p
10489 = auto_is_implicit_function_template_parm_p;
10492 /* This field is only used during parsing of the lambda. */
10493 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10495 /* This lambda shouldn't have any proxies left at this point. */
10496 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10497 /* And now that we're done, push proxies for an enclosing lambda. */
10498 insert_pending_capture_proxies ();
10500 /* Update the lambda expression to a range. */
10501 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10502 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10503 token->location,
10504 end_tok->location);
10506 if (ok)
10507 lambda_expr = build_lambda_object (lambda_expr);
10508 else
10509 lambda_expr = error_mark_node;
10511 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10513 pop_deferring_access_checks ();
10515 return lambda_expr;
10518 /* Parse the beginning of a lambda expression.
10520 lambda-introducer:
10521 [ lambda-capture [opt] ]
10523 LAMBDA_EXPR is the current representation of the lambda expression. */
10525 static void
10526 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10528 /* Need commas after the first capture. */
10529 bool first = true;
10531 /* Eat the leading `['. */
10532 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10534 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10535 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10536 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10537 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10538 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10539 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10541 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10543 cp_lexer_consume_token (parser->lexer);
10544 first = false;
10546 if (!(at_function_scope_p () || parsing_nsdmi ()))
10547 error ("non-local lambda expression cannot have a capture-default");
10550 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10552 cp_token* capture_token;
10553 tree capture_id;
10554 tree capture_init_expr;
10555 cp_id_kind idk = CP_ID_KIND_NONE;
10556 bool explicit_init_p = false;
10558 enum capture_kind_type
10560 BY_COPY,
10561 BY_REFERENCE
10563 enum capture_kind_type capture_kind = BY_COPY;
10565 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10567 error ("expected end of capture-list");
10568 return;
10571 if (first)
10572 first = false;
10573 else
10574 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10576 /* Possibly capture `this'. */
10577 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10579 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10580 if (cxx_dialect < cxx2a
10581 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10582 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10583 "with by-copy capture default");
10584 cp_lexer_consume_token (parser->lexer);
10585 add_capture (lambda_expr,
10586 /*id=*/this_identifier,
10587 /*initializer=*/finish_this_expr (),
10588 /*by_reference_p=*/true,
10589 explicit_init_p);
10590 continue;
10593 /* Possibly capture `*this'. */
10594 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10595 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10597 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10598 if (cxx_dialect < cxx17)
10599 pedwarn (loc, 0, "%<*this%> capture only available with "
10600 "-std=c++17 or -std=gnu++17");
10601 cp_lexer_consume_token (parser->lexer);
10602 cp_lexer_consume_token (parser->lexer);
10603 add_capture (lambda_expr,
10604 /*id=*/this_identifier,
10605 /*initializer=*/finish_this_expr (),
10606 /*by_reference_p=*/false,
10607 explicit_init_p);
10608 continue;
10611 bool init_pack_expansion = false;
10612 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10615 if (cxx_dialect < cxx2a)
10616 pedwarn (loc, 0, "pack init-capture only available with "
10617 "-std=c++2a or -std=gnu++2a");
10618 cp_lexer_consume_token (parser->lexer);
10619 init_pack_expansion = true;
10622 /* Remember whether we want to capture as a reference or not. */
10623 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10625 capture_kind = BY_REFERENCE;
10626 cp_lexer_consume_token (parser->lexer);
10629 /* Get the identifier. */
10630 capture_token = cp_lexer_peek_token (parser->lexer);
10631 capture_id = cp_parser_identifier (parser);
10633 if (capture_id == error_mark_node)
10634 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10635 delimiters, but I modified this to stop on unnested ']' as well. It
10636 was already changed to stop on unnested '}', so the
10637 "closing_parenthesis" name is no more misleading with my change. */
10639 cp_parser_skip_to_closing_parenthesis (parser,
10640 /*recovering=*/true,
10641 /*or_comma=*/true,
10642 /*consume_paren=*/true);
10643 break;
10646 /* Find the initializer for this capture. */
10647 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10648 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10649 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10651 bool direct, non_constant;
10652 /* An explicit initializer exists. */
10653 if (cxx_dialect < cxx14)
10654 pedwarn (input_location, 0,
10655 "lambda capture initializers "
10656 "only available with -std=c++14 or -std=gnu++14");
10657 capture_init_expr = cp_parser_initializer (parser, &direct,
10658 &non_constant, true);
10659 explicit_init_p = true;
10660 if (capture_init_expr == NULL_TREE)
10662 error ("empty initializer for lambda init-capture");
10663 capture_init_expr = error_mark_node;
10665 if (init_pack_expansion)
10666 capture_init_expr = make_pack_expansion (capture_init_expr);
10668 else
10670 const char* error_msg;
10672 /* Turn the identifier into an id-expression. */
10673 capture_init_expr
10674 = cp_parser_lookup_name_simple (parser, capture_id,
10675 capture_token->location);
10677 if (capture_init_expr == error_mark_node)
10679 unqualified_name_lookup_error (capture_id);
10680 continue;
10682 else if (!VAR_P (capture_init_expr)
10683 && TREE_CODE (capture_init_expr) != PARM_DECL)
10685 error_at (capture_token->location,
10686 "capture of non-variable %qE",
10687 capture_init_expr);
10688 if (DECL_P (capture_init_expr))
10689 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10690 "%q#D declared here", capture_init_expr);
10691 continue;
10693 if (VAR_P (capture_init_expr)
10694 && decl_storage_duration (capture_init_expr) != dk_auto)
10696 if (pedwarn (capture_token->location, 0, "capture of variable "
10697 "%qD with non-automatic storage duration",
10698 capture_init_expr))
10699 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10700 "%q#D declared here", capture_init_expr);
10701 continue;
10704 capture_init_expr
10705 = finish_id_expression
10706 (capture_id,
10707 capture_init_expr,
10708 parser->scope,
10709 &idk,
10710 /*integral_constant_expression_p=*/false,
10711 /*allow_non_integral_constant_expression_p=*/false,
10712 /*non_integral_constant_expression_p=*/NULL,
10713 /*template_p=*/false,
10714 /*done=*/true,
10715 /*address_p=*/false,
10716 /*template_arg_p=*/false,
10717 &error_msg,
10718 capture_token->location);
10720 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10722 cp_lexer_consume_token (parser->lexer);
10723 capture_init_expr = make_pack_expansion (capture_init_expr);
10727 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10728 && !explicit_init_p)
10730 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10731 && capture_kind == BY_COPY)
10732 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10733 "of %qD redundant with by-copy capture default",
10734 capture_id);
10735 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10736 && capture_kind == BY_REFERENCE)
10737 pedwarn (capture_token->location, 0, "explicit by-reference "
10738 "capture of %qD redundant with by-reference capture "
10739 "default", capture_id);
10742 add_capture (lambda_expr,
10743 capture_id,
10744 capture_init_expr,
10745 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10746 explicit_init_p);
10748 /* If there is any qualification still in effect, clear it
10749 now; we will be starting fresh with the next capture. */
10750 parser->scope = NULL_TREE;
10751 parser->qualifying_scope = NULL_TREE;
10752 parser->object_scope = NULL_TREE;
10755 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10758 /* Parse the (optional) middle of a lambda expression.
10760 lambda-declarator:
10761 < template-parameter-list [opt] >
10762 ( parameter-declaration-clause [opt] )
10763 attribute-specifier [opt]
10764 decl-specifier-seq [opt]
10765 exception-specification [opt]
10766 lambda-return-type-clause [opt]
10768 LAMBDA_EXPR is the current representation of the lambda expression. */
10770 static bool
10771 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10773 /* 5.1.1.4 of the standard says:
10774 If a lambda-expression does not include a lambda-declarator, it is as if
10775 the lambda-declarator were ().
10776 This means an empty parameter list, no attributes, and no exception
10777 specification. */
10778 tree param_list = void_list_node;
10779 tree attributes = NULL_TREE;
10780 tree exception_spec = NULL_TREE;
10781 tree template_param_list = NULL_TREE;
10782 tree tx_qual = NULL_TREE;
10783 tree return_type = NULL_TREE;
10784 cp_decl_specifier_seq lambda_specs;
10785 clear_decl_specs (&lambda_specs);
10787 /* The template-parameter-list is optional, but must begin with
10788 an opening angle if present. */
10789 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10791 if (cxx_dialect < cxx14)
10792 pedwarn (parser->lexer->next_token->location, 0,
10793 "lambda templates are only available with "
10794 "-std=c++14 or -std=gnu++14");
10795 else if (cxx_dialect < cxx2a)
10796 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10797 "lambda templates are only available with "
10798 "-std=c++2a or -std=gnu++2a");
10800 cp_lexer_consume_token (parser->lexer);
10802 template_param_list = cp_parser_template_parameter_list (parser);
10804 cp_parser_skip_to_end_of_template_parameter_list (parser);
10806 /* We just processed one more parameter list. */
10807 ++parser->num_template_parameter_lists;
10810 /* The parameter-declaration-clause is optional (unless
10811 template-parameter-list was given), but must begin with an
10812 opening parenthesis if present. */
10813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10815 matching_parens parens;
10816 parens.consume_open (parser);
10818 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10820 /* Parse parameters. */
10821 param_list
10822 = cp_parser_parameter_declaration_clause
10823 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10825 /* Default arguments shall not be specified in the
10826 parameter-declaration-clause of a lambda-declarator. */
10827 if (cxx_dialect < cxx14)
10828 for (tree t = param_list; t; t = TREE_CHAIN (t))
10829 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10830 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10831 "default argument specified for lambda parameter");
10833 parens.require_close (parser);
10835 /* In the decl-specifier-seq of the lambda-declarator, each
10836 decl-specifier shall either be mutable or constexpr. */
10837 int declares_class_or_enum;
10838 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10839 cp_parser_decl_specifier_seq (parser,
10840 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10841 &lambda_specs, &declares_class_or_enum);
10842 if (lambda_specs.storage_class == sc_mutable)
10844 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10845 if (lambda_specs.conflicting_specifiers_p)
10846 error_at (lambda_specs.locations[ds_storage_class],
10847 "duplicate %<mutable%>");
10850 tx_qual = cp_parser_tx_qualifier_opt (parser);
10852 /* Parse optional exception specification. */
10853 exception_spec = cp_parser_exception_specification_opt (parser);
10855 attributes = cp_parser_std_attribute_spec_seq (parser);
10857 /* Parse optional trailing return type. */
10858 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10860 cp_lexer_consume_token (parser->lexer);
10861 return_type = cp_parser_trailing_type_id (parser);
10864 /* The function parameters must be in scope all the way until after the
10865 trailing-return-type in case of decltype. */
10866 pop_bindings_and_leave_scope ();
10868 else if (template_param_list != NULL_TREE) // generate diagnostic
10869 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10871 /* Create the function call operator.
10873 Messing with declarators like this is no uglier than building up the
10874 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10875 other code. */
10877 cp_decl_specifier_seq return_type_specs;
10878 cp_declarator* declarator;
10879 tree fco;
10880 int quals;
10881 void *p;
10883 clear_decl_specs (&return_type_specs);
10884 return_type_specs.type = make_auto ();
10886 if (lambda_specs.locations[ds_constexpr])
10888 if (cxx_dialect >= cxx17)
10889 return_type_specs.locations[ds_constexpr]
10890 = lambda_specs.locations[ds_constexpr];
10891 else
10892 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10893 "lambda only available with -std=c++17 or -std=gnu++17");
10896 p = obstack_alloc (&declarator_obstack, 0);
10898 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10899 LAMBDA_EXPR_LOCATION (lambda_expr));
10901 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10902 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10903 declarator = make_call_declarator (declarator, param_list, quals,
10904 VIRT_SPEC_UNSPECIFIED,
10905 REF_QUAL_NONE,
10906 tx_qual,
10907 exception_spec,
10908 return_type,
10909 /*requires_clause*/NULL_TREE);
10910 declarator->std_attributes = attributes;
10912 fco = grokmethod (&return_type_specs,
10913 declarator,
10914 NULL_TREE);
10915 if (fco != error_mark_node)
10917 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10918 DECL_ARTIFICIAL (fco) = 1;
10919 /* Give the object parameter a different name. */
10920 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10921 DECL_LAMBDA_FUNCTION (fco) = 1;
10923 if (template_param_list)
10925 fco = finish_member_template_decl (fco);
10926 finish_template_decl (template_param_list);
10927 --parser->num_template_parameter_lists;
10929 else if (parser->fully_implicit_function_template_p)
10930 fco = finish_fully_implicit_template (parser, fco);
10932 finish_member_declaration (fco);
10934 obstack_free (&declarator_obstack, p);
10936 return (fco != error_mark_node);
10940 /* Parse the body of a lambda expression, which is simply
10942 compound-statement
10944 but which requires special handling.
10945 LAMBDA_EXPR is the current representation of the lambda expression. */
10947 static void
10948 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10950 bool nested = (current_function_decl != NULL_TREE);
10951 unsigned char local_variables_forbidden_p
10952 = parser->local_variables_forbidden_p;
10953 bool in_function_body = parser->in_function_body;
10955 /* The body of a lambda-expression is not a subexpression of the enclosing
10956 expression. */
10957 cp_evaluated ev;
10959 if (nested)
10960 push_function_context ();
10961 else
10962 /* Still increment function_depth so that we don't GC in the
10963 middle of an expression. */
10964 ++function_depth;
10966 vec<tree> omp_privatization_save;
10967 save_omp_privatization_clauses (omp_privatization_save);
10968 /* Clear this in case we're in the middle of a default argument. */
10969 parser->local_variables_forbidden_p = 0;
10970 parser->in_function_body = true;
10973 local_specialization_stack s (lss_copy);
10974 tree fco = lambda_function (lambda_expr);
10975 tree body = start_lambda_function (fco, lambda_expr);
10976 matching_braces braces;
10978 if (braces.require_open (parser))
10980 tree compound_stmt = begin_compound_stmt (0);
10982 /* Originally C++11 required us to peek for 'return expr'; and
10983 process it specially here to deduce the return type. N3638
10984 removed the need for that. */
10986 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10987 cp_parser_label_declaration (parser);
10988 cp_parser_statement_seq_opt (parser, NULL_TREE);
10989 braces.require_close (parser);
10991 finish_compound_stmt (compound_stmt);
10994 finish_lambda_function (body);
10997 restore_omp_privatization_clauses (omp_privatization_save);
10998 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10999 parser->in_function_body = in_function_body;
11000 if (nested)
11001 pop_function_context();
11002 else
11003 --function_depth;
11006 /* Statements [gram.stmt.stmt] */
11008 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11010 static void
11011 add_debug_begin_stmt (location_t loc)
11013 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11014 return;
11015 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11016 /* A concept is never expanded normally. */
11017 return;
11019 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11020 SET_EXPR_LOCATION (stmt, loc);
11021 add_stmt (stmt);
11024 /* Parse a statement.
11026 statement:
11027 labeled-statement
11028 expression-statement
11029 compound-statement
11030 selection-statement
11031 iteration-statement
11032 jump-statement
11033 declaration-statement
11034 try-block
11036 C++11:
11038 statement:
11039 labeled-statement
11040 attribute-specifier-seq (opt) expression-statement
11041 attribute-specifier-seq (opt) compound-statement
11042 attribute-specifier-seq (opt) selection-statement
11043 attribute-specifier-seq (opt) iteration-statement
11044 attribute-specifier-seq (opt) jump-statement
11045 declaration-statement
11046 attribute-specifier-seq (opt) try-block
11048 init-statement:
11049 expression-statement
11050 simple-declaration
11052 TM Extension:
11054 statement:
11055 atomic-statement
11057 IN_COMPOUND is true when the statement is nested inside a
11058 cp_parser_compound_statement; this matters for certain pragmas.
11060 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11061 is a (possibly labeled) if statement which is not enclosed in braces
11062 and has an else clause. This is used to implement -Wparentheses.
11064 CHAIN is a vector of if-else-if conditions. */
11066 static void
11067 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11068 bool in_compound, bool *if_p, vec<tree> *chain,
11069 location_t *loc_after_labels)
11071 tree statement, std_attrs = NULL_TREE;
11072 cp_token *token;
11073 location_t statement_location, attrs_loc;
11075 restart:
11076 if (if_p != NULL)
11077 *if_p = false;
11078 /* There is no statement yet. */
11079 statement = NULL_TREE;
11081 saved_token_sentinel saved_tokens (parser->lexer);
11082 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11083 if (c_dialect_objc ())
11084 /* In obj-c++, seeing '[[' might be the either the beginning of
11085 c++11 attributes, or a nested objc-message-expression. So
11086 let's parse the c++11 attributes tentatively. */
11087 cp_parser_parse_tentatively (parser);
11088 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11089 if (std_attrs)
11091 location_t end_loc
11092 = cp_lexer_previous_token (parser->lexer)->location;
11093 attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11095 if (c_dialect_objc ())
11097 if (!cp_parser_parse_definitely (parser))
11098 std_attrs = NULL_TREE;
11101 /* Peek at the next token. */
11102 token = cp_lexer_peek_token (parser->lexer);
11103 /* Remember the location of the first token in the statement. */
11104 cp_token *statement_token = token;
11105 statement_location = token->location;
11106 add_debug_begin_stmt (statement_location);
11107 /* If this is a keyword, then that will often determine what kind of
11108 statement we have. */
11109 if (token->type == CPP_KEYWORD)
11111 enum rid keyword = token->keyword;
11113 switch (keyword)
11115 case RID_CASE:
11116 case RID_DEFAULT:
11117 /* Looks like a labeled-statement with a case label.
11118 Parse the label, and then use tail recursion to parse
11119 the statement. */
11120 cp_parser_label_for_labeled_statement (parser, std_attrs);
11121 in_compound = false;
11122 goto restart;
11124 case RID_IF:
11125 case RID_SWITCH:
11126 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11127 statement = cp_parser_selection_statement (parser, if_p, chain);
11128 break;
11130 case RID_WHILE:
11131 case RID_DO:
11132 case RID_FOR:
11133 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11134 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11135 break;
11137 case RID_BREAK:
11138 case RID_CONTINUE:
11139 case RID_RETURN:
11140 case RID_GOTO:
11141 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11142 statement = cp_parser_jump_statement (parser);
11143 break;
11145 /* Objective-C++ exception-handling constructs. */
11146 case RID_AT_TRY:
11147 case RID_AT_CATCH:
11148 case RID_AT_FINALLY:
11149 case RID_AT_SYNCHRONIZED:
11150 case RID_AT_THROW:
11151 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11152 statement = cp_parser_objc_statement (parser);
11153 break;
11155 case RID_TRY:
11156 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11157 statement = cp_parser_try_block (parser);
11158 break;
11160 case RID_NAMESPACE:
11161 /* This must be a namespace alias definition. */
11162 if (std_attrs != NULL_TREE)
11164 /* Attributes should be parsed as part of the the
11165 declaration, so let's un-parse them. */
11166 saved_tokens.rollback();
11167 std_attrs = NULL_TREE;
11169 cp_parser_declaration_statement (parser);
11170 return;
11172 case RID_TRANSACTION_ATOMIC:
11173 case RID_TRANSACTION_RELAXED:
11174 case RID_SYNCHRONIZED:
11175 case RID_ATOMIC_NOEXCEPT:
11176 case RID_ATOMIC_CANCEL:
11177 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11178 statement = cp_parser_transaction (parser, token);
11179 break;
11180 case RID_TRANSACTION_CANCEL:
11181 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11182 statement = cp_parser_transaction_cancel (parser);
11183 break;
11185 default:
11186 /* It might be a keyword like `int' that can start a
11187 declaration-statement. */
11188 break;
11191 else if (token->type == CPP_NAME)
11193 /* If the next token is a `:', then we are looking at a
11194 labeled-statement. */
11195 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11196 if (token->type == CPP_COLON)
11198 /* Looks like a labeled-statement with an ordinary label.
11199 Parse the label, and then use tail recursion to parse
11200 the statement. */
11202 cp_parser_label_for_labeled_statement (parser, std_attrs);
11203 in_compound = false;
11204 goto restart;
11207 /* Anything that starts with a `{' must be a compound-statement. */
11208 else if (token->type == CPP_OPEN_BRACE)
11209 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11210 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11211 a statement all its own. */
11212 else if (token->type == CPP_PRAGMA)
11214 /* Only certain OpenMP pragmas are attached to statements, and thus
11215 are considered statements themselves. All others are not. In
11216 the context of a compound, accept the pragma as a "statement" and
11217 return so that we can check for a close brace. Otherwise we
11218 require a real statement and must go back and read one. */
11219 if (in_compound)
11220 cp_parser_pragma (parser, pragma_compound, if_p);
11221 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11222 goto restart;
11223 return;
11225 else if (token->type == CPP_EOF)
11227 cp_parser_error (parser, "expected statement");
11228 return;
11231 /* Everything else must be a declaration-statement or an
11232 expression-statement. Try for the declaration-statement
11233 first, unless we are looking at a `;', in which case we know that
11234 we have an expression-statement. */
11235 if (!statement)
11237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11239 if (std_attrs != NULL_TREE)
11240 /* Attributes should be parsed as part of the declaration,
11241 so let's un-parse them. */
11242 saved_tokens.rollback();
11244 cp_parser_parse_tentatively (parser);
11245 /* Try to parse the declaration-statement. */
11246 cp_parser_declaration_statement (parser);
11247 /* If that worked, we're done. */
11248 if (cp_parser_parse_definitely (parser))
11249 return;
11250 /* It didn't work, restore the post-attribute position. */
11251 if (std_attrs)
11252 cp_lexer_set_token_position (parser->lexer, statement_token);
11254 /* All preceding labels have been parsed at this point. */
11255 if (loc_after_labels != NULL)
11256 *loc_after_labels = statement_location;
11258 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11260 /* Look for an expression-statement instead. */
11261 statement = cp_parser_expression_statement (parser, in_statement_expr);
11263 /* Handle [[fallthrough]];. */
11264 if (attribute_fallthrough_p (std_attrs))
11266 /* The next token after the fallthrough attribute is ';'. */
11267 if (statement == NULL_TREE)
11269 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11270 statement = build_call_expr_internal_loc (statement_location,
11271 IFN_FALLTHROUGH,
11272 void_type_node, 0);
11273 finish_expr_stmt (statement);
11275 else
11276 warning_at (statement_location, OPT_Wattributes,
11277 "%<fallthrough%> attribute not followed by %<;%>");
11278 std_attrs = NULL_TREE;
11282 /* Set the line number for the statement. */
11283 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11284 SET_EXPR_LOCATION (statement, statement_location);
11286 /* Allow "[[fallthrough]];", but warn otherwise. */
11287 if (std_attrs != NULL_TREE)
11288 warning_at (attrs_loc,
11289 OPT_Wattributes,
11290 "attributes at the beginning of statement are ignored");
11293 /* Append ATTR to attribute list ATTRS. */
11295 static tree
11296 attr_chainon (tree attrs, tree attr)
11298 if (attrs == error_mark_node)
11299 return error_mark_node;
11300 if (attr == error_mark_node)
11301 return error_mark_node;
11302 return chainon (attrs, attr);
11305 /* Parse the label for a labeled-statement, i.e.
11307 identifier :
11308 case constant-expression :
11309 default :
11311 GNU Extension:
11312 case constant-expression ... constant-expression : statement
11314 When a label is parsed without errors, the label is added to the
11315 parse tree by the finish_* functions, so this function doesn't
11316 have to return the label. */
11318 static void
11319 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11321 cp_token *token;
11322 tree label = NULL_TREE;
11323 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11325 /* The next token should be an identifier. */
11326 token = cp_lexer_peek_token (parser->lexer);
11327 if (token->type != CPP_NAME
11328 && token->type != CPP_KEYWORD)
11330 cp_parser_error (parser, "expected labeled-statement");
11331 return;
11334 /* Remember whether this case or a user-defined label is allowed to fall
11335 through to. */
11336 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11338 parser->colon_corrects_to_scope_p = false;
11339 switch (token->keyword)
11341 case RID_CASE:
11343 tree expr, expr_hi;
11344 cp_token *ellipsis;
11346 /* Consume the `case' token. */
11347 cp_lexer_consume_token (parser->lexer);
11348 /* Parse the constant-expression. */
11349 expr = cp_parser_constant_expression (parser);
11350 if (check_for_bare_parameter_packs (expr))
11351 expr = error_mark_node;
11353 ellipsis = cp_lexer_peek_token (parser->lexer);
11354 if (ellipsis->type == CPP_ELLIPSIS)
11356 /* Consume the `...' token. */
11357 cp_lexer_consume_token (parser->lexer);
11358 expr_hi = cp_parser_constant_expression (parser);
11359 if (check_for_bare_parameter_packs (expr_hi))
11360 expr_hi = error_mark_node;
11362 /* We don't need to emit warnings here, as the common code
11363 will do this for us. */
11365 else
11366 expr_hi = NULL_TREE;
11368 if (parser->in_switch_statement_p)
11370 tree l = finish_case_label (token->location, expr, expr_hi);
11371 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11373 label = CASE_LABEL (l);
11374 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11377 else
11378 error_at (token->location,
11379 "case label %qE not within a switch statement",
11380 expr);
11382 break;
11384 case RID_DEFAULT:
11385 /* Consume the `default' token. */
11386 cp_lexer_consume_token (parser->lexer);
11388 if (parser->in_switch_statement_p)
11390 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11391 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11393 label = CASE_LABEL (l);
11394 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11397 else
11398 error_at (token->location, "case label not within a switch statement");
11399 break;
11401 default:
11402 /* Anything else must be an ordinary label. */
11403 label = finish_label_stmt (cp_parser_identifier (parser));
11404 if (label && TREE_CODE (label) == LABEL_DECL)
11405 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11406 break;
11409 /* Require the `:' token. */
11410 cp_parser_require (parser, CPP_COLON, RT_COLON);
11412 /* An ordinary label may optionally be followed by attributes.
11413 However, this is only permitted if the attributes are then
11414 followed by a semicolon. This is because, for backward
11415 compatibility, when parsing
11416 lab: __attribute__ ((unused)) int i;
11417 we want the attribute to attach to "i", not "lab". */
11418 if (label != NULL_TREE
11419 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11421 tree attrs;
11422 cp_parser_parse_tentatively (parser);
11423 attrs = cp_parser_gnu_attributes_opt (parser);
11424 if (attrs == NULL_TREE
11425 /* And fallthrough always binds to the expression-statement. */
11426 || attribute_fallthrough_p (attrs)
11427 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11428 cp_parser_abort_tentative_parse (parser);
11429 else if (!cp_parser_parse_definitely (parser))
11431 else
11432 attributes = attr_chainon (attributes, attrs);
11435 if (attributes != NULL_TREE)
11436 cplus_decl_attributes (&label, attributes, 0);
11438 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11441 /* Parse an expression-statement.
11443 expression-statement:
11444 expression [opt] ;
11446 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11447 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11448 indicates whether this expression-statement is part of an
11449 expression statement. */
11451 static tree
11452 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11454 tree statement = NULL_TREE;
11455 cp_token *token = cp_lexer_peek_token (parser->lexer);
11456 location_t loc = token->location;
11458 /* There might be attribute fallthrough. */
11459 tree attr = cp_parser_gnu_attributes_opt (parser);
11461 /* If the next token is a ';', then there is no expression
11462 statement. */
11463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11465 statement = cp_parser_expression (parser);
11466 if (statement == error_mark_node
11467 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11469 cp_parser_skip_to_end_of_block_or_statement (parser);
11470 return error_mark_node;
11474 /* Handle [[fallthrough]];. */
11475 if (attribute_fallthrough_p (attr))
11477 /* The next token after the fallthrough attribute is ';'. */
11478 if (statement == NULL_TREE)
11479 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11480 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11481 void_type_node, 0);
11482 else
11483 warning_at (loc, OPT_Wattributes,
11484 "%<fallthrough%> attribute not followed by %<;%>");
11485 attr = NULL_TREE;
11488 /* Allow "[[fallthrough]];", but warn otherwise. */
11489 if (attr != NULL_TREE)
11490 warning_at (loc, OPT_Wattributes,
11491 "attributes at the beginning of statement are ignored");
11493 /* Give a helpful message for "A<T>::type t;" and the like. */
11494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11495 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11497 if (TREE_CODE (statement) == SCOPE_REF)
11498 error_at (token->location, "need %<typename%> before %qE because "
11499 "%qT is a dependent scope",
11500 statement, TREE_OPERAND (statement, 0));
11501 else if (is_overloaded_fn (statement)
11502 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11504 /* A::A a; */
11505 tree fn = get_first_fn (statement);
11506 error_at (token->location,
11507 "%<%T::%D%> names the constructor, not the type",
11508 DECL_CONTEXT (fn), DECL_NAME (fn));
11512 /* Consume the final `;'. */
11513 cp_parser_consume_semicolon_at_end_of_statement (parser);
11515 if (in_statement_expr
11516 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11517 /* This is the final expression statement of a statement
11518 expression. */
11519 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11520 else if (statement)
11521 statement = finish_expr_stmt (statement);
11523 return statement;
11526 /* Parse a compound-statement.
11528 compound-statement:
11529 { statement-seq [opt] }
11531 GNU extension:
11533 compound-statement:
11534 { label-declaration-seq [opt] statement-seq [opt] }
11536 label-declaration-seq:
11537 label-declaration
11538 label-declaration-seq label-declaration
11540 Returns a tree representing the statement. */
11542 static tree
11543 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11544 int bcs_flags, bool function_body)
11546 tree compound_stmt;
11547 matching_braces braces;
11549 /* Consume the `{'. */
11550 if (!braces.require_open (parser))
11551 return error_mark_node;
11552 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11553 && !function_body && cxx_dialect < cxx14)
11554 pedwarn (input_location, OPT_Wpedantic,
11555 "compound-statement in %<constexpr%> function");
11556 /* Begin the compound-statement. */
11557 compound_stmt = begin_compound_stmt (bcs_flags);
11558 /* If the next keyword is `__label__' we have a label declaration. */
11559 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11560 cp_parser_label_declaration (parser);
11561 /* Parse an (optional) statement-seq. */
11562 cp_parser_statement_seq_opt (parser, in_statement_expr);
11563 /* Finish the compound-statement. */
11564 finish_compound_stmt (compound_stmt);
11565 /* Consume the `}'. */
11566 braces.require_close (parser);
11568 return compound_stmt;
11571 /* Parse an (optional) statement-seq.
11573 statement-seq:
11574 statement
11575 statement-seq [opt] statement */
11577 static void
11578 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11580 /* Scan statements until there aren't any more. */
11581 while (true)
11583 cp_token *token = cp_lexer_peek_token (parser->lexer);
11585 /* If we are looking at a `}', then we have run out of
11586 statements; the same is true if we have reached the end
11587 of file, or have stumbled upon a stray '@end'. */
11588 if (token->type == CPP_CLOSE_BRACE
11589 || token->type == CPP_EOF
11590 || token->type == CPP_PRAGMA_EOL
11591 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11592 break;
11594 /* If we are in a compound statement and find 'else' then
11595 something went wrong. */
11596 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11598 if (parser->in_statement & IN_IF_STMT)
11599 break;
11600 else
11602 token = cp_lexer_consume_token (parser->lexer);
11603 error_at (token->location, "%<else%> without a previous %<if%>");
11607 /* Parse the statement. */
11608 cp_parser_statement (parser, in_statement_expr, true, NULL);
11612 /* Return true if this is the C++20 version of range-based-for with
11613 init-statement. */
11615 static bool
11616 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11618 bool r = false;
11620 /* Save tokens so that we can put them back. */
11621 cp_lexer_save_tokens (parser->lexer);
11623 /* There has to be an unnested ; followed by an unnested :. */
11624 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11625 /*recovering=*/false,
11626 CPP_SEMICOLON,
11627 /*consume_paren=*/false) != -1)
11628 goto out;
11630 /* We found the semicolon, eat it now. */
11631 cp_lexer_consume_token (parser->lexer);
11633 /* Now look for ':' that is not nested in () or {}. */
11634 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11635 /*recovering=*/false,
11636 CPP_COLON,
11637 /*consume_paren=*/false) == -1);
11639 out:
11640 /* Roll back the tokens we skipped. */
11641 cp_lexer_rollback_tokens (parser->lexer);
11643 return r;
11646 /* Return true if we're looking at (init; cond), false otherwise. */
11648 static bool
11649 cp_parser_init_statement_p (cp_parser *parser)
11651 /* Save tokens so that we can put them back. */
11652 cp_lexer_save_tokens (parser->lexer);
11654 /* Look for ';' that is not nested in () or {}. */
11655 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11656 /*recovering=*/false,
11657 CPP_SEMICOLON,
11658 /*consume_paren=*/false);
11660 /* Roll back the tokens we skipped. */
11661 cp_lexer_rollback_tokens (parser->lexer);
11663 return ret == -1;
11666 /* Parse a selection-statement.
11668 selection-statement:
11669 if ( init-statement [opt] condition ) statement
11670 if ( init-statement [opt] condition ) statement else statement
11671 switch ( init-statement [opt] condition ) statement
11673 Returns the new IF_STMT or SWITCH_STMT.
11675 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11676 is a (possibly labeled) if statement which is not enclosed in
11677 braces and has an else clause. This is used to implement
11678 -Wparentheses.
11680 CHAIN is a vector of if-else-if conditions. This is used to implement
11681 -Wduplicated-cond. */
11683 static tree
11684 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11685 vec<tree> *chain)
11687 cp_token *token;
11688 enum rid keyword;
11689 token_indent_info guard_tinfo;
11691 if (if_p != NULL)
11692 *if_p = false;
11694 /* Peek at the next token. */
11695 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11696 guard_tinfo = get_token_indent_info (token);
11698 /* See what kind of keyword it is. */
11699 keyword = token->keyword;
11700 switch (keyword)
11702 case RID_IF:
11703 case RID_SWITCH:
11705 tree statement;
11706 tree condition;
11708 bool cx = false;
11709 if (keyword == RID_IF
11710 && cp_lexer_next_token_is_keyword (parser->lexer,
11711 RID_CONSTEXPR))
11713 cx = true;
11714 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11715 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11716 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11717 "with -std=c++17 or -std=gnu++17");
11720 /* Look for the `('. */
11721 matching_parens parens;
11722 if (!parens.require_open (parser))
11724 cp_parser_skip_to_end_of_statement (parser);
11725 return error_mark_node;
11728 /* Begin the selection-statement. */
11729 if (keyword == RID_IF)
11731 statement = begin_if_stmt ();
11732 IF_STMT_CONSTEXPR_P (statement) = cx;
11734 else
11735 statement = begin_switch_stmt ();
11737 /* Parse the optional init-statement. */
11738 if (cp_parser_init_statement_p (parser))
11740 tree decl;
11741 if (cxx_dialect < cxx17)
11742 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11743 "init-statement in selection statements only available "
11744 "with -std=c++17 or -std=gnu++17");
11745 cp_parser_init_statement (parser, &decl);
11748 /* Parse the condition. */
11749 condition = cp_parser_condition (parser);
11750 /* Look for the `)'. */
11751 if (!parens.require_close (parser))
11752 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11753 /*consume_paren=*/true);
11755 if (keyword == RID_IF)
11757 bool nested_if;
11758 unsigned char in_statement;
11760 /* Add the condition. */
11761 condition = finish_if_stmt_cond (condition, statement);
11763 if (warn_duplicated_cond)
11764 warn_duplicated_cond_add_or_warn (token->location, condition,
11765 &chain);
11767 /* Parse the then-clause. */
11768 in_statement = parser->in_statement;
11769 parser->in_statement |= IN_IF_STMT;
11771 /* Outside a template, the non-selected branch of a constexpr
11772 if is a 'discarded statement', i.e. unevaluated. */
11773 bool was_discarded = in_discarded_stmt;
11774 bool discard_then = (cx && !processing_template_decl
11775 && integer_zerop (condition));
11776 if (discard_then)
11778 in_discarded_stmt = true;
11779 ++c_inhibit_evaluation_warnings;
11782 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11783 guard_tinfo);
11785 parser->in_statement = in_statement;
11787 finish_then_clause (statement);
11789 if (discard_then)
11791 THEN_CLAUSE (statement) = NULL_TREE;
11792 in_discarded_stmt = was_discarded;
11793 --c_inhibit_evaluation_warnings;
11796 /* If the next token is `else', parse the else-clause. */
11797 if (cp_lexer_next_token_is_keyword (parser->lexer,
11798 RID_ELSE))
11800 bool discard_else = (cx && !processing_template_decl
11801 && integer_nonzerop (condition));
11802 if (discard_else)
11804 in_discarded_stmt = true;
11805 ++c_inhibit_evaluation_warnings;
11808 guard_tinfo
11809 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11810 /* Consume the `else' keyword. */
11811 cp_lexer_consume_token (parser->lexer);
11812 if (warn_duplicated_cond)
11814 if (cp_lexer_next_token_is_keyword (parser->lexer,
11815 RID_IF)
11816 && chain == NULL)
11818 /* We've got "if (COND) else if (COND2)". Start
11819 the condition chain and add COND as the first
11820 element. */
11821 chain = new vec<tree> ();
11822 if (!CONSTANT_CLASS_P (condition)
11823 && !TREE_SIDE_EFFECTS (condition))
11825 /* Wrap it in a NOP_EXPR so that we can set the
11826 location of the condition. */
11827 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11828 condition);
11829 SET_EXPR_LOCATION (e, token->location);
11830 chain->safe_push (e);
11833 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11834 RID_IF))
11836 /* This is if-else without subsequent if. Zap the
11837 condition chain; we would have already warned at
11838 this point. */
11839 delete chain;
11840 chain = NULL;
11843 begin_else_clause (statement);
11844 /* Parse the else-clause. */
11845 cp_parser_implicitly_scoped_statement (parser, NULL,
11846 guard_tinfo, chain);
11848 finish_else_clause (statement);
11850 /* If we are currently parsing a then-clause, then
11851 IF_P will not be NULL. We set it to true to
11852 indicate that this if statement has an else clause.
11853 This may trigger the Wparentheses warning below
11854 when we get back up to the parent if statement. */
11855 if (if_p != NULL)
11856 *if_p = true;
11858 if (discard_else)
11860 ELSE_CLAUSE (statement) = NULL_TREE;
11861 in_discarded_stmt = was_discarded;
11862 --c_inhibit_evaluation_warnings;
11865 else
11867 /* This if statement does not have an else clause. If
11868 NESTED_IF is true, then the then-clause has an if
11869 statement which does have an else clause. We warn
11870 about the potential ambiguity. */
11871 if (nested_if)
11872 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11873 "suggest explicit braces to avoid ambiguous"
11874 " %<else%>");
11875 if (warn_duplicated_cond)
11877 /* We don't need the condition chain anymore. */
11878 delete chain;
11879 chain = NULL;
11883 /* Now we're all done with the if-statement. */
11884 finish_if_stmt (statement);
11886 else
11888 bool in_switch_statement_p;
11889 unsigned char in_statement;
11891 /* Add the condition. */
11892 finish_switch_cond (condition, statement);
11894 /* Parse the body of the switch-statement. */
11895 in_switch_statement_p = parser->in_switch_statement_p;
11896 in_statement = parser->in_statement;
11897 parser->in_switch_statement_p = true;
11898 parser->in_statement |= IN_SWITCH_STMT;
11899 cp_parser_implicitly_scoped_statement (parser, if_p,
11900 guard_tinfo);
11901 parser->in_switch_statement_p = in_switch_statement_p;
11902 parser->in_statement = in_statement;
11904 /* Now we're all done with the switch-statement. */
11905 finish_switch_stmt (statement);
11908 return statement;
11910 break;
11912 default:
11913 cp_parser_error (parser, "expected selection-statement");
11914 return error_mark_node;
11918 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11919 If we have seen at least one decl-specifier, and the next token
11920 is not a parenthesis, then we must be looking at a declaration.
11921 (After "int (" we might be looking at a functional cast.) */
11923 static void
11924 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11925 bool any_specifiers_p)
11927 if (any_specifiers_p
11928 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11929 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11930 && !cp_parser_error_occurred (parser))
11931 cp_parser_commit_to_tentative_parse (parser);
11934 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11935 The declarator shall not specify a function or an array. Returns
11936 TRUE if the declarator is valid, FALSE otherwise. */
11938 static bool
11939 cp_parser_check_condition_declarator (cp_parser* parser,
11940 cp_declarator *declarator,
11941 location_t loc)
11943 if (declarator == cp_error_declarator
11944 || function_declarator_p (declarator)
11945 || declarator->kind == cdk_array)
11947 if (declarator == cp_error_declarator)
11948 /* Already complained. */;
11949 else if (declarator->kind == cdk_array)
11950 error_at (loc, "condition declares an array");
11951 else
11952 error_at (loc, "condition declares a function");
11953 if (parser->fully_implicit_function_template_p)
11954 abort_fully_implicit_template (parser);
11955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11956 /*or_comma=*/false,
11957 /*consume_paren=*/false);
11958 return false;
11960 else
11961 return true;
11964 /* Parse a condition.
11966 condition:
11967 expression
11968 type-specifier-seq declarator = initializer-clause
11969 type-specifier-seq declarator braced-init-list
11971 GNU Extension:
11973 condition:
11974 type-specifier-seq declarator asm-specification [opt]
11975 attributes [opt] = assignment-expression
11977 Returns the expression that should be tested. */
11979 static tree
11980 cp_parser_condition (cp_parser* parser)
11982 cp_decl_specifier_seq type_specifiers;
11983 const char *saved_message;
11984 int declares_class_or_enum;
11986 /* Try the declaration first. */
11987 cp_parser_parse_tentatively (parser);
11988 /* New types are not allowed in the type-specifier-seq for a
11989 condition. */
11990 saved_message = parser->type_definition_forbidden_message;
11991 parser->type_definition_forbidden_message
11992 = G_("types may not be defined in conditions");
11993 /* Parse the type-specifier-seq. */
11994 cp_parser_decl_specifier_seq (parser,
11995 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11996 &type_specifiers,
11997 &declares_class_or_enum);
11998 /* Restore the saved message. */
11999 parser->type_definition_forbidden_message = saved_message;
12001 cp_parser_maybe_commit_to_declaration (parser,
12002 type_specifiers.any_specifiers_p);
12004 /* If all is well, we might be looking at a declaration. */
12005 if (!cp_parser_error_occurred (parser))
12007 tree decl;
12008 tree asm_specification;
12009 tree attributes;
12010 cp_declarator *declarator;
12011 tree initializer = NULL_TREE;
12012 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12014 /* Parse the declarator. */
12015 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12016 CP_PARSER_FLAGS_NONE,
12017 /*ctor_dtor_or_conv_p=*/NULL,
12018 /*parenthesized_p=*/NULL,
12019 /*member_p=*/false,
12020 /*friend_p=*/false,
12021 /*static_p=*/false);
12022 /* Parse the attributes. */
12023 attributes = cp_parser_attributes_opt (parser);
12024 /* Parse the asm-specification. */
12025 asm_specification = cp_parser_asm_specification_opt (parser);
12026 /* If the next token is not an `=' or '{', then we might still be
12027 looking at an expression. For example:
12029 if (A(a).x)
12031 looks like a decl-specifier-seq and a declarator -- but then
12032 there is no `=', so this is an expression. */
12033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12034 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12035 cp_parser_simulate_error (parser);
12037 /* If we did see an `=' or '{', then we are looking at a declaration
12038 for sure. */
12039 if (cp_parser_parse_definitely (parser))
12041 tree pushed_scope;
12042 bool non_constant_p = false;
12043 int flags = LOOKUP_ONLYCONVERTING;
12045 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12046 return error_mark_node;
12048 /* Create the declaration. */
12049 decl = start_decl (declarator, &type_specifiers,
12050 /*initialized_p=*/true,
12051 attributes, /*prefix_attributes=*/NULL_TREE,
12052 &pushed_scope);
12054 /* Parse the initializer. */
12055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12057 initializer = cp_parser_braced_list (parser, &non_constant_p);
12058 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12059 flags = 0;
12061 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12063 /* Consume the `='. */
12064 cp_lexer_consume_token (parser->lexer);
12065 initializer = cp_parser_initializer_clause (parser,
12066 &non_constant_p);
12068 else
12070 cp_parser_error (parser, "expected initializer");
12071 initializer = error_mark_node;
12073 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12074 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12076 /* Process the initializer. */
12077 cp_finish_decl (decl,
12078 initializer, !non_constant_p,
12079 asm_specification,
12080 flags);
12082 if (pushed_scope)
12083 pop_scope (pushed_scope);
12085 return convert_from_reference (decl);
12088 /* If we didn't even get past the declarator successfully, we are
12089 definitely not looking at a declaration. */
12090 else
12091 cp_parser_abort_tentative_parse (parser);
12093 /* Otherwise, we are looking at an expression. */
12094 return cp_parser_expression (parser);
12097 /* Parses a for-statement or range-for-statement until the closing ')',
12098 not included. */
12100 static tree
12101 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12103 tree init, scope, decl;
12104 bool is_range_for;
12106 /* Begin the for-statement. */
12107 scope = begin_for_scope (&init);
12109 /* Parse the initialization. */
12110 is_range_for = cp_parser_init_statement (parser, &decl);
12112 if (is_range_for)
12113 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12114 false);
12115 else
12116 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12119 static tree
12120 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12121 unsigned short unroll)
12123 /* Normal for loop */
12124 tree condition = NULL_TREE;
12125 tree expression = NULL_TREE;
12126 tree stmt;
12128 stmt = begin_for_stmt (scope, init);
12129 /* The init-statement has already been parsed in
12130 cp_parser_init_statement, so no work is needed here. */
12131 finish_init_stmt (stmt);
12133 /* If there's a condition, process it. */
12134 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12135 condition = cp_parser_condition (parser);
12136 else if (ivdep)
12138 cp_parser_error (parser, "missing loop condition in loop with "
12139 "%<GCC ivdep%> pragma");
12140 condition = error_mark_node;
12142 else if (unroll)
12144 cp_parser_error (parser, "missing loop condition in loop with "
12145 "%<GCC unroll%> pragma");
12146 condition = error_mark_node;
12148 finish_for_cond (condition, stmt, ivdep, unroll);
12149 /* Look for the `;'. */
12150 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12152 /* If there's an expression, process it. */
12153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12154 expression = cp_parser_expression (parser);
12155 finish_for_expr (expression, stmt);
12157 return stmt;
12160 /* Tries to parse a range-based for-statement:
12162 range-based-for:
12163 decl-specifier-seq declarator : expression
12165 The decl-specifier-seq declarator and the `:' are already parsed by
12166 cp_parser_init_statement. If processing_template_decl it returns a
12167 newly created RANGE_FOR_STMT; if not, it is converted to a
12168 regular FOR_STMT. */
12170 static tree
12171 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12172 bool ivdep, unsigned short unroll, bool is_omp)
12174 tree stmt, range_expr;
12175 auto_vec <cxx_binding *, 16> bindings;
12176 auto_vec <tree, 16> names;
12177 tree decomp_first_name = NULL_TREE;
12178 unsigned int decomp_cnt = 0;
12180 /* Get the range declaration momentarily out of the way so that
12181 the range expression doesn't clash with it. */
12182 if (range_decl != error_mark_node)
12184 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12186 tree v = DECL_VALUE_EXPR (range_decl);
12187 /* For decomposition declaration get all of the corresponding
12188 declarations out of the way. */
12189 if (TREE_CODE (v) == ARRAY_REF
12190 && VAR_P (TREE_OPERAND (v, 0))
12191 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12193 tree d = range_decl;
12194 range_decl = TREE_OPERAND (v, 0);
12195 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12196 decomp_first_name = d;
12197 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12199 tree name = DECL_NAME (d);
12200 names.safe_push (name);
12201 bindings.safe_push (IDENTIFIER_BINDING (name));
12202 IDENTIFIER_BINDING (name)
12203 = IDENTIFIER_BINDING (name)->previous;
12207 if (names.is_empty ())
12209 tree name = DECL_NAME (range_decl);
12210 names.safe_push (name);
12211 bindings.safe_push (IDENTIFIER_BINDING (name));
12212 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12216 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12218 bool expr_non_constant_p;
12219 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12221 else
12222 range_expr = cp_parser_expression (parser);
12224 /* Put the range declaration(s) back into scope. */
12225 for (unsigned int i = 0; i < names.length (); i++)
12227 cxx_binding *binding = bindings[i];
12228 binding->previous = IDENTIFIER_BINDING (names[i]);
12229 IDENTIFIER_BINDING (names[i]) = binding;
12232 /* finish_omp_for has its own code for the following, so just
12233 return the range_expr instead. */
12234 if (is_omp)
12235 return range_expr;
12237 /* If in template, STMT is converted to a normal for-statement
12238 at instantiation. If not, it is done just ahead. */
12239 if (processing_template_decl)
12241 if (check_for_bare_parameter_packs (range_expr))
12242 range_expr = error_mark_node;
12243 stmt = begin_range_for_stmt (scope, init);
12244 if (ivdep)
12245 RANGE_FOR_IVDEP (stmt) = 1;
12246 if (unroll)
12247 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12248 finish_range_for_decl (stmt, range_decl, range_expr);
12249 if (!type_dependent_expression_p (range_expr)
12250 /* do_auto_deduction doesn't mess with template init-lists. */
12251 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12252 do_range_for_auto_deduction (range_decl, range_expr);
12254 else
12256 stmt = begin_for_stmt (scope, init);
12257 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12258 decomp_first_name, decomp_cnt, ivdep,
12259 unroll);
12261 return stmt;
12264 /* Subroutine of cp_convert_range_for: given the initializer expression,
12265 builds up the range temporary. */
12267 static tree
12268 build_range_temp (tree range_expr)
12270 tree range_type, range_temp;
12272 /* Find out the type deduced by the declaration
12273 `auto &&__range = range_expr'. */
12274 range_type = cp_build_reference_type (make_auto (), true);
12275 range_type = do_auto_deduction (range_type, range_expr,
12276 type_uses_auto (range_type));
12278 /* Create the __range variable. */
12279 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12280 range_type);
12281 TREE_USED (range_temp) = 1;
12282 DECL_ARTIFICIAL (range_temp) = 1;
12284 return range_temp;
12287 /* Used by cp_parser_range_for in template context: we aren't going to
12288 do a full conversion yet, but we still need to resolve auto in the
12289 type of the for-range-declaration if present. This is basically
12290 a shortcut version of cp_convert_range_for. */
12292 static void
12293 do_range_for_auto_deduction (tree decl, tree range_expr)
12295 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12296 if (auto_node)
12298 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12299 range_temp = convert_from_reference (build_range_temp (range_expr));
12300 iter_type = (cp_parser_perform_range_for_lookup
12301 (range_temp, &begin_dummy, &end_dummy));
12302 if (iter_type)
12304 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12305 iter_type);
12306 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12307 RO_UNARY_STAR,
12308 tf_warning_or_error);
12309 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12310 iter_decl, auto_node);
12315 /* Converts a range-based for-statement into a normal
12316 for-statement, as per the definition.
12318 for (RANGE_DECL : RANGE_EXPR)
12319 BLOCK
12321 should be equivalent to:
12324 auto &&__range = RANGE_EXPR;
12325 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12326 __begin != __end;
12327 ++__begin)
12329 RANGE_DECL = *__begin;
12330 BLOCK
12334 If RANGE_EXPR is an array:
12335 BEGIN_EXPR = __range
12336 END_EXPR = __range + ARRAY_SIZE(__range)
12337 Else if RANGE_EXPR has a member 'begin' or 'end':
12338 BEGIN_EXPR = __range.begin()
12339 END_EXPR = __range.end()
12340 Else:
12341 BEGIN_EXPR = begin(__range)
12342 END_EXPR = end(__range);
12344 If __range has a member 'begin' but not 'end', or vice versa, we must
12345 still use the second alternative (it will surely fail, however).
12346 When calling begin()/end() in the third alternative we must use
12347 argument dependent lookup, but always considering 'std' as an associated
12348 namespace. */
12350 tree
12351 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12352 tree decomp_first_name, unsigned int decomp_cnt,
12353 bool ivdep, unsigned short unroll)
12355 tree begin, end;
12356 tree iter_type, begin_expr, end_expr;
12357 tree condition, expression;
12359 range_expr = mark_lvalue_use (range_expr);
12361 if (range_decl == error_mark_node || range_expr == error_mark_node)
12362 /* If an error happened previously do nothing or else a lot of
12363 unhelpful errors would be issued. */
12364 begin_expr = end_expr = iter_type = error_mark_node;
12365 else
12367 tree range_temp;
12369 if (VAR_P (range_expr)
12370 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12371 /* Can't bind a reference to an array of runtime bound. */
12372 range_temp = range_expr;
12373 else
12375 range_temp = build_range_temp (range_expr);
12376 pushdecl (range_temp);
12377 cp_finish_decl (range_temp, range_expr,
12378 /*is_constant_init*/false, NULL_TREE,
12379 LOOKUP_ONLYCONVERTING);
12380 range_temp = convert_from_reference (range_temp);
12382 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12383 &begin_expr, &end_expr);
12386 /* The new for initialization statement. */
12387 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12388 iter_type);
12389 TREE_USED (begin) = 1;
12390 DECL_ARTIFICIAL (begin) = 1;
12391 pushdecl (begin);
12392 cp_finish_decl (begin, begin_expr,
12393 /*is_constant_init*/false, NULL_TREE,
12394 LOOKUP_ONLYCONVERTING);
12396 if (cxx_dialect >= cxx17)
12397 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12398 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12399 TREE_USED (end) = 1;
12400 DECL_ARTIFICIAL (end) = 1;
12401 pushdecl (end);
12402 cp_finish_decl (end, end_expr,
12403 /*is_constant_init*/false, NULL_TREE,
12404 LOOKUP_ONLYCONVERTING);
12406 finish_init_stmt (statement);
12408 /* The new for condition. */
12409 condition = build_x_binary_op (input_location, NE_EXPR,
12410 begin, ERROR_MARK,
12411 end, ERROR_MARK,
12412 NULL, tf_warning_or_error);
12413 finish_for_cond (condition, statement, ivdep, unroll);
12415 /* The new increment expression. */
12416 expression = finish_unary_op_expr (input_location,
12417 PREINCREMENT_EXPR, begin,
12418 tf_warning_or_error);
12419 finish_for_expr (expression, statement);
12421 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12422 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12424 /* The declaration is initialized with *__begin inside the loop body. */
12425 cp_finish_decl (range_decl,
12426 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12427 tf_warning_or_error),
12428 /*is_constant_init*/false, NULL_TREE,
12429 LOOKUP_ONLYCONVERTING);
12430 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12431 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12433 return statement;
12436 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12437 We need to solve both at the same time because the method used
12438 depends on the existence of members begin or end.
12439 Returns the type deduced for the iterator expression. */
12441 static tree
12442 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12444 if (error_operand_p (range))
12446 *begin = *end = error_mark_node;
12447 return error_mark_node;
12450 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12452 error ("range-based %<for%> expression of type %qT "
12453 "has incomplete type", TREE_TYPE (range));
12454 *begin = *end = error_mark_node;
12455 return error_mark_node;
12457 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12459 /* If RANGE is an array, we will use pointer arithmetic. */
12460 *begin = decay_conversion (range, tf_warning_or_error);
12461 *end = build_binary_op (input_location, PLUS_EXPR,
12462 range,
12463 array_type_nelts_top (TREE_TYPE (range)),
12464 false);
12465 return TREE_TYPE (*begin);
12467 else
12469 /* If it is not an array, we must do a bit of magic. */
12470 tree id_begin, id_end;
12471 tree member_begin, member_end;
12473 *begin = *end = error_mark_node;
12475 id_begin = get_identifier ("begin");
12476 id_end = get_identifier ("end");
12477 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12478 /*protect=*/2, /*want_type=*/false,
12479 tf_warning_or_error);
12480 member_end = lookup_member (TREE_TYPE (range), id_end,
12481 /*protect=*/2, /*want_type=*/false,
12482 tf_warning_or_error);
12484 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12486 /* Use the member functions. */
12487 *begin = cp_parser_range_for_member_function (range, id_begin);
12488 *end = cp_parser_range_for_member_function (range, id_end);
12490 else
12492 /* Use global functions with ADL. */
12493 vec<tree, va_gc> *vec;
12494 vec = make_tree_vector ();
12496 vec_safe_push (vec, range);
12498 member_begin = perform_koenig_lookup (id_begin, vec,
12499 tf_warning_or_error);
12500 *begin = finish_call_expr (member_begin, &vec, false, true,
12501 tf_warning_or_error);
12502 member_end = perform_koenig_lookup (id_end, vec,
12503 tf_warning_or_error);
12504 *end = finish_call_expr (member_end, &vec, false, true,
12505 tf_warning_or_error);
12507 release_tree_vector (vec);
12510 /* Last common checks. */
12511 if (*begin == error_mark_node || *end == error_mark_node)
12513 /* If one of the expressions is an error do no more checks. */
12514 *begin = *end = error_mark_node;
12515 return error_mark_node;
12517 else if (type_dependent_expression_p (*begin)
12518 || type_dependent_expression_p (*end))
12519 /* Can happen, when, eg, in a template context, Koenig lookup
12520 can't resolve begin/end (c++/58503). */
12521 return NULL_TREE;
12522 else
12524 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12525 /* The unqualified type of the __begin and __end temporaries should
12526 be the same, as required by the multiple auto declaration. */
12527 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12529 if (cxx_dialect >= cxx17
12530 && (build_x_binary_op (input_location, NE_EXPR,
12531 *begin, ERROR_MARK,
12532 *end, ERROR_MARK,
12533 NULL, tf_none)
12534 != error_mark_node))
12535 /* P0184R0 allows __begin and __end to have different types,
12536 but make sure they are comparable so we can give a better
12537 diagnostic. */;
12538 else
12539 error ("inconsistent begin/end types in range-based %<for%> "
12540 "statement: %qT and %qT",
12541 TREE_TYPE (*begin), TREE_TYPE (*end));
12543 return iter_type;
12548 /* Helper function for cp_parser_perform_range_for_lookup.
12549 Builds a tree for RANGE.IDENTIFIER(). */
12551 static tree
12552 cp_parser_range_for_member_function (tree range, tree identifier)
12554 tree member, res;
12555 vec<tree, va_gc> *vec;
12557 member = finish_class_member_access_expr (range, identifier,
12558 false, tf_warning_or_error);
12559 if (member == error_mark_node)
12560 return error_mark_node;
12562 vec = make_tree_vector ();
12563 res = finish_call_expr (member, &vec,
12564 /*disallow_virtual=*/false,
12565 /*koenig_p=*/false,
12566 tf_warning_or_error);
12567 release_tree_vector (vec);
12568 return res;
12571 /* Parse an iteration-statement.
12573 iteration-statement:
12574 while ( condition ) statement
12575 do statement while ( expression ) ;
12576 for ( init-statement condition [opt] ; expression [opt] )
12577 statement
12579 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12581 static tree
12582 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12583 unsigned short unroll)
12585 cp_token *token;
12586 enum rid keyword;
12587 tree statement;
12588 unsigned char in_statement;
12589 token_indent_info guard_tinfo;
12591 /* Peek at the next token. */
12592 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12593 if (!token)
12594 return error_mark_node;
12596 guard_tinfo = get_token_indent_info (token);
12598 /* Remember whether or not we are already within an iteration
12599 statement. */
12600 in_statement = parser->in_statement;
12602 /* See what kind of keyword it is. */
12603 keyword = token->keyword;
12604 switch (keyword)
12606 case RID_WHILE:
12608 tree condition;
12610 /* Begin the while-statement. */
12611 statement = begin_while_stmt ();
12612 /* Look for the `('. */
12613 matching_parens parens;
12614 parens.require_open (parser);
12615 /* Parse the condition. */
12616 condition = cp_parser_condition (parser);
12617 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12618 /* Look for the `)'. */
12619 parens.require_close (parser);
12620 /* Parse the dependent statement. */
12621 parser->in_statement = IN_ITERATION_STMT;
12622 bool prev = note_iteration_stmt_body_start ();
12623 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12624 note_iteration_stmt_body_end (prev);
12625 parser->in_statement = in_statement;
12626 /* We're done with the while-statement. */
12627 finish_while_stmt (statement);
12629 break;
12631 case RID_DO:
12633 tree expression;
12635 /* Begin the do-statement. */
12636 statement = begin_do_stmt ();
12637 /* Parse the body of the do-statement. */
12638 parser->in_statement = IN_ITERATION_STMT;
12639 bool prev = note_iteration_stmt_body_start ();
12640 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12641 note_iteration_stmt_body_end (prev);
12642 parser->in_statement = in_statement;
12643 finish_do_body (statement);
12644 /* Look for the `while' keyword. */
12645 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12646 /* Look for the `('. */
12647 matching_parens parens;
12648 parens.require_open (parser);
12649 /* Parse the expression. */
12650 expression = cp_parser_expression (parser);
12651 /* We're done with the do-statement. */
12652 finish_do_stmt (expression, statement, ivdep, unroll);
12653 /* Look for the `)'. */
12654 parens.require_close (parser);
12655 /* Look for the `;'. */
12656 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12658 break;
12660 case RID_FOR:
12662 /* Look for the `('. */
12663 matching_parens parens;
12664 parens.require_open (parser);
12666 statement = cp_parser_for (parser, ivdep, unroll);
12668 /* Look for the `)'. */
12669 parens.require_close (parser);
12671 /* Parse the body of the for-statement. */
12672 parser->in_statement = IN_ITERATION_STMT;
12673 bool prev = note_iteration_stmt_body_start ();
12674 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12675 note_iteration_stmt_body_end (prev);
12676 parser->in_statement = in_statement;
12678 /* We're done with the for-statement. */
12679 finish_for_stmt (statement);
12681 break;
12683 default:
12684 cp_parser_error (parser, "expected iteration-statement");
12685 statement = error_mark_node;
12686 break;
12689 return statement;
12692 /* Parse a init-statement or the declarator of a range-based-for.
12693 Returns true if a range-based-for declaration is seen.
12695 init-statement:
12696 expression-statement
12697 simple-declaration */
12699 static bool
12700 cp_parser_init_statement (cp_parser *parser, tree *decl)
12702 /* If the next token is a `;', then we have an empty
12703 expression-statement. Grammatically, this is also a
12704 simple-declaration, but an invalid one, because it does not
12705 declare anything. Therefore, if we did not handle this case
12706 specially, we would issue an error message about an invalid
12707 declaration. */
12708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12710 bool is_range_for = false;
12711 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12713 /* Try to parse the init-statement. */
12714 if (cp_parser_range_based_for_with_init_p (parser))
12716 tree dummy;
12717 cp_parser_parse_tentatively (parser);
12718 /* Parse the declaration. */
12719 cp_parser_simple_declaration (parser,
12720 /*function_definition_allowed_p=*/false,
12721 &dummy);
12722 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12723 if (!cp_parser_parse_definitely (parser))
12724 /* That didn't work, try to parse it as an expression-statement. */
12725 cp_parser_expression_statement (parser, NULL_TREE);
12727 if (cxx_dialect < cxx2a)
12729 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12730 "range-based %<for%> loops with initializer only "
12731 "available with -std=c++2a or -std=gnu++2a");
12732 *decl = error_mark_node;
12736 /* A colon is used in range-based for. */
12737 parser->colon_corrects_to_scope_p = false;
12739 /* We're going to speculatively look for a declaration, falling back
12740 to an expression, if necessary. */
12741 cp_parser_parse_tentatively (parser);
12742 /* Parse the declaration. */
12743 cp_parser_simple_declaration (parser,
12744 /*function_definition_allowed_p=*/false,
12745 decl);
12746 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12747 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12749 /* It is a range-for, consume the ':'. */
12750 cp_lexer_consume_token (parser->lexer);
12751 is_range_for = true;
12752 if (cxx_dialect < cxx11)
12753 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12754 "range-based %<for%> loops only available with "
12755 "-std=c++11 or -std=gnu++11");
12757 else
12758 /* The ';' is not consumed yet because we told
12759 cp_parser_simple_declaration not to. */
12760 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12762 if (cp_parser_parse_definitely (parser))
12763 return is_range_for;
12764 /* If the tentative parse failed, then we shall need to look for an
12765 expression-statement. */
12767 /* If we are here, it is an expression-statement. */
12768 cp_parser_expression_statement (parser, NULL_TREE);
12769 return false;
12772 /* Parse a jump-statement.
12774 jump-statement:
12775 break ;
12776 continue ;
12777 return expression [opt] ;
12778 return braced-init-list ;
12779 goto identifier ;
12781 GNU extension:
12783 jump-statement:
12784 goto * expression ;
12786 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12788 static tree
12789 cp_parser_jump_statement (cp_parser* parser)
12791 tree statement = error_mark_node;
12792 cp_token *token;
12793 enum rid keyword;
12794 unsigned char in_statement;
12796 /* Peek at the next token. */
12797 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12798 if (!token)
12799 return error_mark_node;
12801 /* See what kind of keyword it is. */
12802 keyword = token->keyword;
12803 switch (keyword)
12805 case RID_BREAK:
12806 in_statement = parser->in_statement & ~IN_IF_STMT;
12807 switch (in_statement)
12809 case 0:
12810 error_at (token->location, "break statement not within loop or switch");
12811 break;
12812 default:
12813 gcc_assert ((in_statement & IN_SWITCH_STMT)
12814 || in_statement == IN_ITERATION_STMT);
12815 statement = finish_break_stmt ();
12816 if (in_statement == IN_ITERATION_STMT)
12817 break_maybe_infinite_loop ();
12818 break;
12819 case IN_OMP_BLOCK:
12820 error_at (token->location, "invalid exit from OpenMP structured block");
12821 break;
12822 case IN_OMP_FOR:
12823 error_at (token->location, "break statement used with OpenMP for loop");
12824 break;
12826 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12827 break;
12829 case RID_CONTINUE:
12830 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12832 case 0:
12833 error_at (token->location, "continue statement not within a loop");
12834 break;
12835 /* Fall through. */
12836 case IN_ITERATION_STMT:
12837 case IN_OMP_FOR:
12838 statement = finish_continue_stmt ();
12839 break;
12840 case IN_OMP_BLOCK:
12841 error_at (token->location, "invalid exit from OpenMP structured block");
12842 break;
12843 default:
12844 gcc_unreachable ();
12846 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12847 break;
12849 case RID_RETURN:
12851 tree expr;
12852 bool expr_non_constant_p;
12854 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12856 cp_lexer_set_source_position (parser->lexer);
12857 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12858 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12860 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12861 expr = cp_parser_expression (parser);
12862 else
12863 /* If the next token is a `;', then there is no
12864 expression. */
12865 expr = NULL_TREE;
12866 /* Build the return-statement. */
12867 if (current_function_auto_return_pattern && in_discarded_stmt)
12868 /* Don't deduce from a discarded return statement. */;
12869 else
12870 statement = finish_return_stmt (expr);
12871 /* Look for the final `;'. */
12872 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12874 break;
12876 case RID_GOTO:
12877 if (parser->in_function_body
12878 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12880 error ("%<goto%> in %<constexpr%> function");
12881 cp_function_chain->invalid_constexpr = true;
12884 /* Create the goto-statement. */
12885 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12887 /* Issue a warning about this use of a GNU extension. */
12888 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12889 /* Consume the '*' token. */
12890 cp_lexer_consume_token (parser->lexer);
12891 /* Parse the dependent expression. */
12892 finish_goto_stmt (cp_parser_expression (parser));
12894 else
12895 finish_goto_stmt (cp_parser_identifier (parser));
12896 /* Look for the final `;'. */
12897 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12898 break;
12900 default:
12901 cp_parser_error (parser, "expected jump-statement");
12902 break;
12905 return statement;
12908 /* Parse a declaration-statement.
12910 declaration-statement:
12911 block-declaration */
12913 static void
12914 cp_parser_declaration_statement (cp_parser* parser)
12916 void *p;
12918 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12919 p = obstack_alloc (&declarator_obstack, 0);
12921 /* Parse the block-declaration. */
12922 cp_parser_block_declaration (parser, /*statement_p=*/true);
12924 /* Free any declarators allocated. */
12925 obstack_free (&declarator_obstack, p);
12928 /* Some dependent statements (like `if (cond) statement'), are
12929 implicitly in their own scope. In other words, if the statement is
12930 a single statement (as opposed to a compound-statement), it is
12931 none-the-less treated as if it were enclosed in braces. Any
12932 declarations appearing in the dependent statement are out of scope
12933 after control passes that point. This function parses a statement,
12934 but ensures that is in its own scope, even if it is not a
12935 compound-statement.
12937 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12938 is a (possibly labeled) if statement which is not enclosed in
12939 braces and has an else clause. This is used to implement
12940 -Wparentheses.
12942 CHAIN is a vector of if-else-if conditions. This is used to implement
12943 -Wduplicated-cond.
12945 Returns the new statement. */
12947 static tree
12948 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12949 const token_indent_info &guard_tinfo,
12950 vec<tree> *chain)
12952 tree statement;
12953 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12954 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12955 token_indent_info body_tinfo
12956 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12958 if (if_p != NULL)
12959 *if_p = false;
12961 /* Mark if () ; with a special NOP_EXPR. */
12962 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12964 cp_lexer_consume_token (parser->lexer);
12965 statement = add_stmt (build_empty_stmt (body_loc));
12967 if (guard_tinfo.keyword == RID_IF
12968 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12969 warning_at (body_loc, OPT_Wempty_body,
12970 "suggest braces around empty body in an %<if%> statement");
12971 else if (guard_tinfo.keyword == RID_ELSE)
12972 warning_at (body_loc, OPT_Wempty_body,
12973 "suggest braces around empty body in an %<else%> statement");
12975 /* if a compound is opened, we simply parse the statement directly. */
12976 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12977 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12978 /* If the token is not a `{', then we must take special action. */
12979 else
12981 /* Create a compound-statement. */
12982 statement = begin_compound_stmt (0);
12983 /* Parse the dependent-statement. */
12984 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12985 &body_loc_after_labels);
12986 /* Finish the dummy compound-statement. */
12987 finish_compound_stmt (statement);
12990 token_indent_info next_tinfo
12991 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12992 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12994 if (body_loc_after_labels != UNKNOWN_LOCATION
12995 && next_tinfo.type != CPP_SEMICOLON)
12996 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12997 guard_tinfo.location, guard_tinfo.keyword);
12999 /* Return the statement. */
13000 return statement;
13003 /* For some dependent statements (like `while (cond) statement'), we
13004 have already created a scope. Therefore, even if the dependent
13005 statement is a compound-statement, we do not want to create another
13006 scope. */
13008 static void
13009 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13010 const token_indent_info &guard_tinfo)
13012 /* If the token is a `{', then we must take special action. */
13013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13015 token_indent_info body_tinfo
13016 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13017 location_t loc_after_labels = UNKNOWN_LOCATION;
13019 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13020 &loc_after_labels);
13021 token_indent_info next_tinfo
13022 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13023 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13025 if (loc_after_labels != UNKNOWN_LOCATION
13026 && next_tinfo.type != CPP_SEMICOLON)
13027 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13028 guard_tinfo.location,
13029 guard_tinfo.keyword);
13031 else
13033 /* Avoid calling cp_parser_compound_statement, so that we
13034 don't create a new scope. Do everything else by hand. */
13035 matching_braces braces;
13036 braces.require_open (parser);
13037 /* If the next keyword is `__label__' we have a label declaration. */
13038 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13039 cp_parser_label_declaration (parser);
13040 /* Parse an (optional) statement-seq. */
13041 cp_parser_statement_seq_opt (parser, NULL_TREE);
13042 braces.require_close (parser);
13046 /* Declarations [gram.dcl.dcl] */
13048 /* Parse an optional declaration-sequence.
13050 declaration-seq:
13051 declaration
13052 declaration-seq declaration */
13054 static void
13055 cp_parser_declaration_seq_opt (cp_parser* parser)
13057 while (true)
13059 cp_token *token = cp_lexer_peek_token (parser->lexer);
13061 if (token->type == CPP_CLOSE_BRACE
13062 || token->type == CPP_EOF)
13063 break;
13064 else
13065 cp_parser_toplevel_declaration (parser);
13069 /* Parse a declaration.
13071 declaration:
13072 block-declaration
13073 function-definition
13074 template-declaration
13075 explicit-instantiation
13076 explicit-specialization
13077 linkage-specification
13078 namespace-definition
13080 C++17:
13081 deduction-guide
13083 GNU extension:
13085 declaration:
13086 __extension__ declaration */
13088 static void
13089 cp_parser_declaration (cp_parser* parser)
13091 cp_token token1;
13092 cp_token token2;
13093 int saved_pedantic;
13094 void *p;
13095 tree attributes = NULL_TREE;
13097 /* Check for the `__extension__' keyword. */
13098 if (cp_parser_extension_opt (parser, &saved_pedantic))
13100 /* Parse the qualified declaration. */
13101 cp_parser_declaration (parser);
13102 /* Restore the PEDANTIC flag. */
13103 pedantic = saved_pedantic;
13105 return;
13108 /* Try to figure out what kind of declaration is present. */
13109 token1 = *cp_lexer_peek_token (parser->lexer);
13111 if (token1.type != CPP_EOF)
13112 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13113 else
13115 token2.type = CPP_EOF;
13116 token2.keyword = RID_MAX;
13119 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13120 p = obstack_alloc (&declarator_obstack, 0);
13122 /* If the next token is `extern' and the following token is a string
13123 literal, then we have a linkage specification. */
13124 if (token1.keyword == RID_EXTERN
13125 && cp_parser_is_pure_string_literal (&token2))
13126 cp_parser_linkage_specification (parser);
13127 /* If the next token is `template', then we have either a template
13128 declaration, an explicit instantiation, or an explicit
13129 specialization. */
13130 else if (token1.keyword == RID_TEMPLATE)
13132 /* `template <>' indicates a template specialization. */
13133 if (token2.type == CPP_LESS
13134 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13135 cp_parser_explicit_specialization (parser);
13136 /* `template <' indicates a template declaration. */
13137 else if (token2.type == CPP_LESS)
13138 cp_parser_template_declaration (parser, /*member_p=*/false);
13139 /* Anything else must be an explicit instantiation. */
13140 else
13141 cp_parser_explicit_instantiation (parser);
13143 /* If the next token is `export', then we have a template
13144 declaration. */
13145 else if (token1.keyword == RID_EXPORT)
13146 cp_parser_template_declaration (parser, /*member_p=*/false);
13147 /* If the next token is `extern', 'static' or 'inline' and the one
13148 after that is `template', we have a GNU extended explicit
13149 instantiation directive. */
13150 else if (cp_parser_allow_gnu_extensions_p (parser)
13151 && (token1.keyword == RID_EXTERN
13152 || token1.keyword == RID_STATIC
13153 || token1.keyword == RID_INLINE)
13154 && token2.keyword == RID_TEMPLATE)
13155 cp_parser_explicit_instantiation (parser);
13156 /* If the next token is `namespace', check for a named or unnamed
13157 namespace definition. */
13158 else if (token1.keyword == RID_NAMESPACE
13159 && (/* A named namespace definition. */
13160 (token2.type == CPP_NAME
13161 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13162 != CPP_EQ))
13163 || (token2.type == CPP_OPEN_SQUARE
13164 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13165 == CPP_OPEN_SQUARE)
13166 /* An unnamed namespace definition. */
13167 || token2.type == CPP_OPEN_BRACE
13168 || token2.keyword == RID_ATTRIBUTE))
13169 cp_parser_namespace_definition (parser);
13170 /* An inline (associated) namespace definition. */
13171 else if (token1.keyword == RID_INLINE
13172 && token2.keyword == RID_NAMESPACE)
13173 cp_parser_namespace_definition (parser);
13174 /* Objective-C++ declaration/definition. */
13175 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13176 cp_parser_objc_declaration (parser, NULL_TREE);
13177 else if (c_dialect_objc ()
13178 && token1.keyword == RID_ATTRIBUTE
13179 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13180 cp_parser_objc_declaration (parser, attributes);
13181 /* At this point we may have a template declared by a concept
13182 introduction. */
13183 else if (flag_concepts
13184 && cp_parser_template_declaration_after_export (parser,
13185 /*member_p=*/false))
13186 /* We did. */;
13187 else
13188 /* Try to parse a block-declaration, or a function-definition. */
13189 cp_parser_block_declaration (parser, /*statement_p=*/false);
13191 /* Free any declarators allocated. */
13192 obstack_free (&declarator_obstack, p);
13195 /* Parse a namespace-scope declaration. */
13197 static void
13198 cp_parser_toplevel_declaration (cp_parser* parser)
13200 cp_token *token = cp_lexer_peek_token (parser->lexer);
13202 if (token->type == CPP_PRAGMA)
13203 /* A top-level declaration can consist solely of a #pragma. A
13204 nested declaration cannot, so this is done here and not in
13205 cp_parser_declaration. (A #pragma at block scope is
13206 handled in cp_parser_statement.) */
13207 cp_parser_pragma (parser, pragma_external, NULL);
13208 else if (token->type == CPP_SEMICOLON)
13210 /* A declaration consisting of a single semicolon is
13211 invalid. Allow it unless we're being pedantic. */
13212 cp_lexer_consume_token (parser->lexer);
13213 if (!in_system_header_at (input_location))
13214 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13216 else
13217 /* Parse the declaration itself. */
13218 cp_parser_declaration (parser);
13221 /* Parse a block-declaration.
13223 block-declaration:
13224 simple-declaration
13225 asm-definition
13226 namespace-alias-definition
13227 using-declaration
13228 using-directive
13230 GNU Extension:
13232 block-declaration:
13233 __extension__ block-declaration
13235 C++0x Extension:
13237 block-declaration:
13238 static_assert-declaration
13240 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13241 part of a declaration-statement. */
13243 static void
13244 cp_parser_block_declaration (cp_parser *parser,
13245 bool statement_p)
13247 cp_token *token1;
13248 int saved_pedantic;
13250 /* Check for the `__extension__' keyword. */
13251 if (cp_parser_extension_opt (parser, &saved_pedantic))
13253 /* Parse the qualified declaration. */
13254 cp_parser_block_declaration (parser, statement_p);
13255 /* Restore the PEDANTIC flag. */
13256 pedantic = saved_pedantic;
13258 return;
13261 /* Peek at the next token to figure out which kind of declaration is
13262 present. */
13263 token1 = cp_lexer_peek_token (parser->lexer);
13265 /* If the next keyword is `asm', we have an asm-definition. */
13266 if (token1->keyword == RID_ASM)
13268 if (statement_p)
13269 cp_parser_commit_to_tentative_parse (parser);
13270 cp_parser_asm_definition (parser);
13272 /* If the next keyword is `namespace', we have a
13273 namespace-alias-definition. */
13274 else if (token1->keyword == RID_NAMESPACE)
13275 cp_parser_namespace_alias_definition (parser);
13276 /* If the next keyword is `using', we have a
13277 using-declaration, a using-directive, or an alias-declaration. */
13278 else if (token1->keyword == RID_USING)
13280 cp_token *token2;
13282 if (statement_p)
13283 cp_parser_commit_to_tentative_parse (parser);
13284 /* If the token after `using' is `namespace', then we have a
13285 using-directive. */
13286 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13287 if (token2->keyword == RID_NAMESPACE)
13288 cp_parser_using_directive (parser);
13289 /* If the second token after 'using' is '=', then we have an
13290 alias-declaration. */
13291 else if (cxx_dialect >= cxx11
13292 && token2->type == CPP_NAME
13293 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13294 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13295 cp_parser_alias_declaration (parser);
13296 /* Otherwise, it's a using-declaration. */
13297 else
13298 cp_parser_using_declaration (parser,
13299 /*access_declaration_p=*/false);
13301 /* If the next keyword is `__label__' we have a misplaced label
13302 declaration. */
13303 else if (token1->keyword == RID_LABEL)
13305 cp_lexer_consume_token (parser->lexer);
13306 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13307 cp_parser_skip_to_end_of_statement (parser);
13308 /* If the next token is now a `;', consume it. */
13309 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13310 cp_lexer_consume_token (parser->lexer);
13312 /* If the next token is `static_assert' we have a static assertion. */
13313 else if (token1->keyword == RID_STATIC_ASSERT)
13314 cp_parser_static_assert (parser, /*member_p=*/false);
13315 /* Anything else must be a simple-declaration. */
13316 else
13317 cp_parser_simple_declaration (parser, !statement_p,
13318 /*maybe_range_for_decl*/NULL);
13321 /* Parse a simple-declaration.
13323 simple-declaration:
13324 decl-specifier-seq [opt] init-declarator-list [opt] ;
13325 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13326 brace-or-equal-initializer ;
13328 init-declarator-list:
13329 init-declarator
13330 init-declarator-list , init-declarator
13332 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13333 function-definition as a simple-declaration.
13335 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13336 parsed declaration if it is an uninitialized single declarator not followed
13337 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13338 if present, will not be consumed. */
13340 static void
13341 cp_parser_simple_declaration (cp_parser* parser,
13342 bool function_definition_allowed_p,
13343 tree *maybe_range_for_decl)
13345 cp_decl_specifier_seq decl_specifiers;
13346 int declares_class_or_enum;
13347 bool saw_declarator;
13348 location_t comma_loc = UNKNOWN_LOCATION;
13349 location_t init_loc = UNKNOWN_LOCATION;
13351 if (maybe_range_for_decl)
13352 *maybe_range_for_decl = NULL_TREE;
13354 /* Defer access checks until we know what is being declared; the
13355 checks for names appearing in the decl-specifier-seq should be
13356 done as if we were in the scope of the thing being declared. */
13357 push_deferring_access_checks (dk_deferred);
13359 /* Parse the decl-specifier-seq. We have to keep track of whether
13360 or not the decl-specifier-seq declares a named class or
13361 enumeration type, since that is the only case in which the
13362 init-declarator-list is allowed to be empty.
13364 [dcl.dcl]
13366 In a simple-declaration, the optional init-declarator-list can be
13367 omitted only when declaring a class or enumeration, that is when
13368 the decl-specifier-seq contains either a class-specifier, an
13369 elaborated-type-specifier, or an enum-specifier. */
13370 cp_parser_decl_specifier_seq (parser,
13371 CP_PARSER_FLAGS_OPTIONAL,
13372 &decl_specifiers,
13373 &declares_class_or_enum);
13374 /* We no longer need to defer access checks. */
13375 stop_deferring_access_checks ();
13377 /* In a block scope, a valid declaration must always have a
13378 decl-specifier-seq. By not trying to parse declarators, we can
13379 resolve the declaration/expression ambiguity more quickly. */
13380 if (!function_definition_allowed_p
13381 && !decl_specifiers.any_specifiers_p)
13383 cp_parser_error (parser, "expected declaration");
13384 goto done;
13387 /* If the next two tokens are both identifiers, the code is
13388 erroneous. The usual cause of this situation is code like:
13390 T t;
13392 where "T" should name a type -- but does not. */
13393 if (!decl_specifiers.any_type_specifiers_p
13394 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13396 /* If parsing tentatively, we should commit; we really are
13397 looking at a declaration. */
13398 cp_parser_commit_to_tentative_parse (parser);
13399 /* Give up. */
13400 goto done;
13403 cp_parser_maybe_commit_to_declaration (parser,
13404 decl_specifiers.any_specifiers_p);
13406 /* Look for C++17 decomposition declaration. */
13407 for (size_t n = 1; ; n++)
13408 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13409 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13410 continue;
13411 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13412 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13413 && decl_specifiers.any_specifiers_p)
13415 tree decl
13416 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13417 maybe_range_for_decl,
13418 &init_loc);
13420 /* The next token should be either a `,' or a `;'. */
13421 cp_token *token = cp_lexer_peek_token (parser->lexer);
13422 /* If it's a `;', we are done. */
13423 if (token->type == CPP_SEMICOLON)
13424 goto finish;
13425 else if (maybe_range_for_decl)
13427 if (*maybe_range_for_decl == NULL_TREE)
13428 *maybe_range_for_decl = error_mark_node;
13429 goto finish;
13431 /* Anything else is an error. */
13432 else
13434 /* If we have already issued an error message we don't need
13435 to issue another one. */
13436 if ((decl != error_mark_node
13437 && DECL_INITIAL (decl) != error_mark_node)
13438 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13439 cp_parser_error (parser, "expected %<,%> or %<;%>");
13440 /* Skip tokens until we reach the end of the statement. */
13441 cp_parser_skip_to_end_of_statement (parser);
13442 /* If the next token is now a `;', consume it. */
13443 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13444 cp_lexer_consume_token (parser->lexer);
13445 goto done;
13448 else
13449 break;
13451 tree last_type;
13452 bool auto_specifier_p;
13453 /* NULL_TREE if both variable and function declaration are allowed,
13454 error_mark_node if function declaration are not allowed and
13455 a FUNCTION_DECL that should be diagnosed if it is followed by
13456 variable declarations. */
13457 tree auto_function_declaration;
13459 last_type = NULL_TREE;
13460 auto_specifier_p
13461 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13462 auto_function_declaration = NULL_TREE;
13464 /* Keep going until we hit the `;' at the end of the simple
13465 declaration. */
13466 saw_declarator = false;
13467 while (cp_lexer_next_token_is_not (parser->lexer,
13468 CPP_SEMICOLON))
13470 cp_token *token;
13471 bool function_definition_p;
13472 tree decl;
13473 tree auto_result = NULL_TREE;
13475 if (saw_declarator)
13477 /* If we are processing next declarator, comma is expected */
13478 token = cp_lexer_peek_token (parser->lexer);
13479 gcc_assert (token->type == CPP_COMMA);
13480 cp_lexer_consume_token (parser->lexer);
13481 if (maybe_range_for_decl)
13483 *maybe_range_for_decl = error_mark_node;
13484 if (comma_loc == UNKNOWN_LOCATION)
13485 comma_loc = token->location;
13488 else
13489 saw_declarator = true;
13491 /* Parse the init-declarator. */
13492 decl = cp_parser_init_declarator (parser,
13493 CP_PARSER_FLAGS_NONE,
13494 &decl_specifiers,
13495 /*checks=*/NULL,
13496 function_definition_allowed_p,
13497 /*member_p=*/false,
13498 declares_class_or_enum,
13499 &function_definition_p,
13500 maybe_range_for_decl,
13501 &init_loc,
13502 &auto_result);
13503 /* If an error occurred while parsing tentatively, exit quickly.
13504 (That usually happens when in the body of a function; each
13505 statement is treated as a declaration-statement until proven
13506 otherwise.) */
13507 if (cp_parser_error_occurred (parser))
13508 goto done;
13510 if (auto_specifier_p && cxx_dialect >= cxx14)
13512 /* If the init-declarator-list contains more than one
13513 init-declarator, they shall all form declarations of
13514 variables. */
13515 if (auto_function_declaration == NULL_TREE)
13516 auto_function_declaration
13517 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13518 else if (TREE_CODE (decl) == FUNCTION_DECL
13519 || auto_function_declaration != error_mark_node)
13521 error_at (decl_specifiers.locations[ds_type_spec],
13522 "non-variable %qD in declaration with more than one "
13523 "declarator with placeholder type",
13524 TREE_CODE (decl) == FUNCTION_DECL
13525 ? decl : auto_function_declaration);
13526 auto_function_declaration = error_mark_node;
13530 if (auto_result
13531 && (!processing_template_decl || !type_uses_auto (auto_result)))
13533 if (last_type
13534 && last_type != error_mark_node
13535 && !same_type_p (auto_result, last_type))
13537 /* If the list of declarators contains more than one declarator,
13538 the type of each declared variable is determined as described
13539 above. If the type deduced for the template parameter U is not
13540 the same in each deduction, the program is ill-formed. */
13541 error_at (decl_specifiers.locations[ds_type_spec],
13542 "inconsistent deduction for %qT: %qT and then %qT",
13543 decl_specifiers.type, last_type, auto_result);
13544 last_type = error_mark_node;
13546 else
13547 last_type = auto_result;
13550 /* Handle function definitions specially. */
13551 if (function_definition_p)
13553 /* If the next token is a `,', then we are probably
13554 processing something like:
13556 void f() {}, *p;
13558 which is erroneous. */
13559 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13561 cp_token *token = cp_lexer_peek_token (parser->lexer);
13562 error_at (token->location,
13563 "mixing"
13564 " declarations and function-definitions is forbidden");
13566 /* Otherwise, we're done with the list of declarators. */
13567 else
13569 pop_deferring_access_checks ();
13570 return;
13573 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13574 *maybe_range_for_decl = decl;
13575 /* The next token should be either a `,' or a `;'. */
13576 token = cp_lexer_peek_token (parser->lexer);
13577 /* If it's a `,', there are more declarators to come. */
13578 if (token->type == CPP_COMMA)
13579 /* will be consumed next time around */;
13580 /* If it's a `;', we are done. */
13581 else if (token->type == CPP_SEMICOLON)
13582 break;
13583 else if (maybe_range_for_decl)
13585 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13586 permerror (decl_specifiers.locations[ds_type_spec],
13587 "types may not be defined in a for-range-declaration");
13588 break;
13590 /* Anything else is an error. */
13591 else
13593 /* If we have already issued an error message we don't need
13594 to issue another one. */
13595 if ((decl != error_mark_node
13596 && DECL_INITIAL (decl) != error_mark_node)
13597 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13598 cp_parser_error (parser, "expected %<,%> or %<;%>");
13599 /* Skip tokens until we reach the end of the statement. */
13600 cp_parser_skip_to_end_of_statement (parser);
13601 /* If the next token is now a `;', consume it. */
13602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13603 cp_lexer_consume_token (parser->lexer);
13604 goto done;
13606 /* After the first time around, a function-definition is not
13607 allowed -- even if it was OK at first. For example:
13609 int i, f() {}
13611 is not valid. */
13612 function_definition_allowed_p = false;
13615 /* Issue an error message if no declarators are present, and the
13616 decl-specifier-seq does not itself declare a class or
13617 enumeration: [dcl.dcl]/3. */
13618 if (!saw_declarator)
13620 if (cp_parser_declares_only_class_p (parser))
13622 if (!declares_class_or_enum
13623 && decl_specifiers.type
13624 && OVERLOAD_TYPE_P (decl_specifiers.type))
13625 /* Ensure an error is issued anyway when finish_decltype_type,
13626 called via cp_parser_decl_specifier_seq, returns a class or
13627 an enumeration (c++/51786). */
13628 decl_specifiers.type = NULL_TREE;
13629 shadow_tag (&decl_specifiers);
13631 /* Perform any deferred access checks. */
13632 perform_deferred_access_checks (tf_warning_or_error);
13635 /* Consume the `;'. */
13636 finish:
13637 if (!maybe_range_for_decl)
13638 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13639 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13641 if (init_loc != UNKNOWN_LOCATION)
13642 error_at (init_loc, "initializer in range-based %<for%> loop");
13643 if (comma_loc != UNKNOWN_LOCATION)
13644 error_at (comma_loc,
13645 "multiple declarations in range-based %<for%> loop");
13648 done:
13649 pop_deferring_access_checks ();
13652 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13653 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13654 initializer ; */
13656 static tree
13657 cp_parser_decomposition_declaration (cp_parser *parser,
13658 cp_decl_specifier_seq *decl_specifiers,
13659 tree *maybe_range_for_decl,
13660 location_t *init_loc)
13662 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13663 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13664 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13666 /* Parse the identifier-list. */
13667 auto_vec<cp_expr, 10> v;
13668 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13669 while (true)
13671 cp_expr e = cp_parser_identifier (parser);
13672 if (e.get_value () == error_mark_node)
13673 break;
13674 v.safe_push (e);
13675 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13676 break;
13677 cp_lexer_consume_token (parser->lexer);
13680 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13681 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13683 end_loc = UNKNOWN_LOCATION;
13684 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13685 false);
13686 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13687 cp_lexer_consume_token (parser->lexer);
13688 else
13690 cp_parser_skip_to_end_of_statement (parser);
13691 return error_mark_node;
13695 if (cxx_dialect < cxx17)
13696 pedwarn (loc, 0, "structured bindings only available with "
13697 "-std=c++17 or -std=gnu++17");
13699 tree pushed_scope;
13700 cp_declarator *declarator = make_declarator (cdk_decomp);
13701 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13702 declarator->id_loc = loc;
13703 if (ref_qual != REF_QUAL_NONE)
13704 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13705 ref_qual == REF_QUAL_RVALUE,
13706 NULL_TREE);
13707 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13708 NULL_TREE, decl_specifiers->attributes,
13709 &pushed_scope);
13710 tree orig_decl = decl;
13712 unsigned int i;
13713 cp_expr e;
13714 cp_decl_specifier_seq decl_specs;
13715 clear_decl_specs (&decl_specs);
13716 decl_specs.type = make_auto ();
13717 tree prev = decl;
13718 FOR_EACH_VEC_ELT (v, i, e)
13720 if (i == 0)
13721 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13722 sfk_none, e.get_location ());
13723 else
13725 declarator->u.id.unqualified_name = e.get_value ();
13726 declarator->id_loc = e.get_location ();
13728 tree elt_pushed_scope;
13729 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13730 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13731 if (decl2 == error_mark_node)
13732 decl = error_mark_node;
13733 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13735 /* Ensure we've diagnosed redeclaration if we aren't creating
13736 a new VAR_DECL. */
13737 gcc_assert (errorcount);
13738 decl = error_mark_node;
13740 else
13741 prev = decl2;
13742 if (elt_pushed_scope)
13743 pop_scope (elt_pushed_scope);
13746 if (v.is_empty ())
13748 error_at (loc, "empty structured binding declaration");
13749 decl = error_mark_node;
13752 if (maybe_range_for_decl == NULL
13753 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13755 bool non_constant_p = false, is_direct_init = false;
13756 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13757 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13758 &non_constant_p);
13759 if (initializer == NULL_TREE
13760 || (TREE_CODE (initializer) == TREE_LIST
13761 && TREE_CHAIN (initializer))
13762 || (is_direct_init
13763 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13764 && CONSTRUCTOR_NELTS (initializer) != 1))
13766 error_at (loc, "invalid initializer for structured binding "
13767 "declaration");
13768 initializer = error_mark_node;
13771 if (decl != error_mark_node)
13773 cp_maybe_mangle_decomp (decl, prev, v.length ());
13774 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13775 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13776 cp_finish_decomp (decl, prev, v.length ());
13779 else if (decl != error_mark_node)
13781 *maybe_range_for_decl = prev;
13782 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13783 the underlying DECL. */
13784 cp_finish_decomp (decl, prev, v.length ());
13787 if (pushed_scope)
13788 pop_scope (pushed_scope);
13790 if (decl == error_mark_node && DECL_P (orig_decl))
13792 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13793 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13796 return decl;
13799 /* Parse a decl-specifier-seq.
13801 decl-specifier-seq:
13802 decl-specifier-seq [opt] decl-specifier
13803 decl-specifier attribute-specifier-seq [opt] (C++11)
13805 decl-specifier:
13806 storage-class-specifier
13807 type-specifier
13808 function-specifier
13809 friend
13810 typedef
13812 GNU Extension:
13814 decl-specifier:
13815 attributes
13817 Concepts Extension:
13819 decl-specifier:
13820 concept
13822 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13824 The parser flags FLAGS is used to control type-specifier parsing.
13826 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13827 flags:
13829 1: one of the decl-specifiers is an elaborated-type-specifier
13830 (i.e., a type declaration)
13831 2: one of the decl-specifiers is an enum-specifier or a
13832 class-specifier (i.e., a type definition)
13836 static void
13837 cp_parser_decl_specifier_seq (cp_parser* parser,
13838 cp_parser_flags flags,
13839 cp_decl_specifier_seq *decl_specs,
13840 int* declares_class_or_enum)
13842 bool constructor_possible_p = !parser->in_declarator_p;
13843 bool found_decl_spec = false;
13844 cp_token *start_token = NULL;
13845 cp_decl_spec ds;
13847 /* Clear DECL_SPECS. */
13848 clear_decl_specs (decl_specs);
13850 /* Assume no class or enumeration type is declared. */
13851 *declares_class_or_enum = 0;
13853 /* Keep reading specifiers until there are no more to read. */
13854 while (true)
13856 bool constructor_p;
13857 cp_token *token;
13858 ds = ds_last;
13860 /* Peek at the next token. */
13861 token = cp_lexer_peek_token (parser->lexer);
13863 /* Save the first token of the decl spec list for error
13864 reporting. */
13865 if (!start_token)
13866 start_token = token;
13867 /* Handle attributes. */
13868 if (cp_next_tokens_can_be_attribute_p (parser))
13870 /* Parse the attributes. */
13871 tree attrs = cp_parser_attributes_opt (parser);
13873 /* In a sequence of declaration specifiers, c++11 attributes
13874 appertain to the type that precede them. In that case
13875 [dcl.spec]/1 says:
13877 The attribute-specifier-seq affects the type only for
13878 the declaration it appears in, not other declarations
13879 involving the same type.
13881 But for now let's force the user to position the
13882 attribute either at the beginning of the declaration or
13883 after the declarator-id, which would clearly mean that it
13884 applies to the declarator. */
13885 if (cxx11_attribute_p (attrs))
13887 if (!found_decl_spec)
13888 /* The c++11 attribute is at the beginning of the
13889 declaration. It appertains to the entity being
13890 declared. */;
13891 else
13893 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13895 /* This is an attribute following a
13896 class-specifier. */
13897 if (decl_specs->type_definition_p)
13898 warn_misplaced_attr_for_class_type (token->location,
13899 decl_specs->type);
13900 attrs = NULL_TREE;
13902 else
13904 decl_specs->std_attributes
13905 = attr_chainon (decl_specs->std_attributes, attrs);
13906 if (decl_specs->locations[ds_std_attribute] == 0)
13907 decl_specs->locations[ds_std_attribute] = token->location;
13909 continue;
13913 decl_specs->attributes
13914 = attr_chainon (decl_specs->attributes, attrs);
13915 if (decl_specs->locations[ds_attribute] == 0)
13916 decl_specs->locations[ds_attribute] = token->location;
13917 continue;
13919 /* Assume we will find a decl-specifier keyword. */
13920 found_decl_spec = true;
13921 /* If the next token is an appropriate keyword, we can simply
13922 add it to the list. */
13923 switch (token->keyword)
13925 /* decl-specifier:
13926 friend
13927 constexpr */
13928 case RID_FRIEND:
13929 if (!at_class_scope_p ())
13931 gcc_rich_location richloc (token->location);
13932 richloc.add_fixit_remove ();
13933 error_at (&richloc, "%<friend%> used outside of class");
13934 cp_lexer_purge_token (parser->lexer);
13936 else
13938 ds = ds_friend;
13939 /* Consume the token. */
13940 cp_lexer_consume_token (parser->lexer);
13942 break;
13944 case RID_CONSTEXPR:
13945 ds = ds_constexpr;
13946 cp_lexer_consume_token (parser->lexer);
13947 break;
13949 case RID_CONCEPT:
13950 ds = ds_concept;
13951 cp_lexer_consume_token (parser->lexer);
13952 break;
13954 /* function-specifier:
13955 inline
13956 virtual
13957 explicit */
13958 case RID_INLINE:
13959 case RID_VIRTUAL:
13960 case RID_EXPLICIT:
13961 cp_parser_function_specifier_opt (parser, decl_specs);
13962 break;
13964 /* decl-specifier:
13965 typedef */
13966 case RID_TYPEDEF:
13967 ds = ds_typedef;
13968 /* Consume the token. */
13969 cp_lexer_consume_token (parser->lexer);
13970 /* A constructor declarator cannot appear in a typedef. */
13971 constructor_possible_p = false;
13972 /* The "typedef" keyword can only occur in a declaration; we
13973 may as well commit at this point. */
13974 cp_parser_commit_to_tentative_parse (parser);
13976 if (decl_specs->storage_class != sc_none)
13977 decl_specs->conflicting_specifiers_p = true;
13978 break;
13980 /* storage-class-specifier:
13981 auto
13982 register
13983 static
13984 extern
13985 mutable
13987 GNU Extension:
13988 thread */
13989 case RID_AUTO:
13990 if (cxx_dialect == cxx98)
13992 /* Consume the token. */
13993 cp_lexer_consume_token (parser->lexer);
13995 /* Complain about `auto' as a storage specifier, if
13996 we're complaining about C++0x compatibility. */
13997 gcc_rich_location richloc (token->location);
13998 richloc.add_fixit_remove ();
13999 warning_at (&richloc, OPT_Wc__11_compat,
14000 "%<auto%> changes meaning in C++11; "
14001 "please remove it");
14003 /* Set the storage class anyway. */
14004 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14005 token);
14007 else
14008 /* C++0x auto type-specifier. */
14009 found_decl_spec = false;
14010 break;
14012 case RID_REGISTER:
14013 case RID_STATIC:
14014 case RID_EXTERN:
14015 case RID_MUTABLE:
14016 /* Consume the token. */
14017 cp_lexer_consume_token (parser->lexer);
14018 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14019 token);
14020 break;
14021 case RID_THREAD:
14022 /* Consume the token. */
14023 ds = ds_thread;
14024 cp_lexer_consume_token (parser->lexer);
14025 break;
14027 default:
14028 /* We did not yet find a decl-specifier yet. */
14029 found_decl_spec = false;
14030 break;
14033 if (found_decl_spec
14034 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14035 && token->keyword != RID_CONSTEXPR)
14036 error ("decl-specifier invalid in condition");
14038 if (found_decl_spec
14039 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14040 && token->keyword != RID_MUTABLE
14041 && token->keyword != RID_CONSTEXPR)
14042 error_at (token->location, "%qD invalid in lambda",
14043 ridpointers[token->keyword]);
14045 if (ds != ds_last)
14046 set_and_check_decl_spec_loc (decl_specs, ds, token);
14048 /* Constructors are a special case. The `S' in `S()' is not a
14049 decl-specifier; it is the beginning of the declarator. */
14050 constructor_p
14051 = (!found_decl_spec
14052 && constructor_possible_p
14053 && (cp_parser_constructor_declarator_p
14054 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
14056 /* If we don't have a DECL_SPEC yet, then we must be looking at
14057 a type-specifier. */
14058 if (!found_decl_spec && !constructor_p)
14060 int decl_spec_declares_class_or_enum;
14061 bool is_cv_qualifier;
14062 tree type_spec;
14064 type_spec
14065 = cp_parser_type_specifier (parser, flags,
14066 decl_specs,
14067 /*is_declaration=*/true,
14068 &decl_spec_declares_class_or_enum,
14069 &is_cv_qualifier);
14070 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14072 /* If this type-specifier referenced a user-defined type
14073 (a typedef, class-name, etc.), then we can't allow any
14074 more such type-specifiers henceforth.
14076 [dcl.spec]
14078 The longest sequence of decl-specifiers that could
14079 possibly be a type name is taken as the
14080 decl-specifier-seq of a declaration. The sequence shall
14081 be self-consistent as described below.
14083 [dcl.type]
14085 As a general rule, at most one type-specifier is allowed
14086 in the complete decl-specifier-seq of a declaration. The
14087 only exceptions are the following:
14089 -- const or volatile can be combined with any other
14090 type-specifier.
14092 -- signed or unsigned can be combined with char, long,
14093 short, or int.
14095 -- ..
14097 Example:
14099 typedef char* Pc;
14100 void g (const int Pc);
14102 Here, Pc is *not* part of the decl-specifier seq; it's
14103 the declarator. Therefore, once we see a type-specifier
14104 (other than a cv-qualifier), we forbid any additional
14105 user-defined types. We *do* still allow things like `int
14106 int' to be considered a decl-specifier-seq, and issue the
14107 error message later. */
14108 if (type_spec && !is_cv_qualifier)
14109 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14110 /* A constructor declarator cannot follow a type-specifier. */
14111 if (type_spec)
14113 constructor_possible_p = false;
14114 found_decl_spec = true;
14115 if (!is_cv_qualifier)
14116 decl_specs->any_type_specifiers_p = true;
14118 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14119 error_at (token->location, "type-specifier invalid in lambda");
14123 /* If we still do not have a DECL_SPEC, then there are no more
14124 decl-specifiers. */
14125 if (!found_decl_spec)
14126 break;
14128 decl_specs->any_specifiers_p = true;
14129 /* After we see one decl-specifier, further decl-specifiers are
14130 always optional. */
14131 flags |= CP_PARSER_FLAGS_OPTIONAL;
14134 /* Don't allow a friend specifier with a class definition. */
14135 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14136 && (*declares_class_or_enum & 2))
14137 error_at (decl_specs->locations[ds_friend],
14138 "class definition may not be declared a friend");
14141 /* Parse an (optional) storage-class-specifier.
14143 storage-class-specifier:
14144 auto
14145 register
14146 static
14147 extern
14148 mutable
14150 GNU Extension:
14152 storage-class-specifier:
14153 thread
14155 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14157 static tree
14158 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14160 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14162 case RID_AUTO:
14163 if (cxx_dialect != cxx98)
14164 return NULL_TREE;
14165 /* Fall through for C++98. */
14166 gcc_fallthrough ();
14168 case RID_REGISTER:
14169 case RID_STATIC:
14170 case RID_EXTERN:
14171 case RID_MUTABLE:
14172 case RID_THREAD:
14173 /* Consume the token. */
14174 return cp_lexer_consume_token (parser->lexer)->u.value;
14176 default:
14177 return NULL_TREE;
14181 /* Parse an (optional) function-specifier.
14183 function-specifier:
14184 inline
14185 virtual
14186 explicit
14188 C++2A Extension:
14189 explicit(constant-expression)
14191 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14192 Updates DECL_SPECS, if it is non-NULL. */
14194 static tree
14195 cp_parser_function_specifier_opt (cp_parser* parser,
14196 cp_decl_specifier_seq *decl_specs)
14198 cp_token *token = cp_lexer_peek_token (parser->lexer);
14199 switch (token->keyword)
14201 case RID_INLINE:
14202 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14203 break;
14205 case RID_VIRTUAL:
14206 /* 14.5.2.3 [temp.mem]
14208 A member function template shall not be virtual. */
14209 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14210 && current_class_type)
14211 error_at (token->location, "templates may not be %<virtual%>");
14212 else
14213 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14214 break;
14216 case RID_EXPLICIT:
14218 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14219 /* If we see '(', it's C++20 explicit(bool). */
14220 tree expr;
14221 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14223 matching_parens parens;
14224 parens.consume_open (parser);
14226 /* New types are not allowed in an explicit-specifier. */
14227 const char *saved_message
14228 = parser->type_definition_forbidden_message;
14229 parser->type_definition_forbidden_message
14230 = G_("types may not be defined in explicit-specifier");
14232 if (cxx_dialect < cxx2a)
14233 pedwarn (token->location, 0,
14234 "%<explicit(bool)%> only available with -std=c++2a "
14235 "or -std=gnu++2a");
14237 /* Parse the constant-expression. */
14238 expr = cp_parser_constant_expression (parser);
14240 /* Restore the saved message. */
14241 parser->type_definition_forbidden_message = saved_message;
14242 parens.require_close (parser);
14244 else
14245 /* The explicit-specifier explicit without a constant-expression is
14246 equivalent to the explicit-specifier explicit(true). */
14247 expr = boolean_true_node;
14249 /* [dcl.fct.spec]
14250 "the constant-expression, if supplied, shall be a contextually
14251 converted constant expression of type bool." */
14252 expr = build_explicit_specifier (expr, tf_warning_or_error);
14253 /* We could evaluate it -- mark the decl as appropriate. */
14254 if (expr == boolean_true_node)
14255 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14256 else if (expr == boolean_false_node)
14257 /* Don't mark the decl as explicit. */;
14258 else if (decl_specs)
14259 /* The expression was value-dependent. Remember it so that we can
14260 substitute it later. */
14261 decl_specs->explicit_specifier = expr;
14262 return id;
14265 default:
14266 return NULL_TREE;
14269 /* Consume the token. */
14270 return cp_lexer_consume_token (parser->lexer)->u.value;
14273 /* Parse a linkage-specification.
14275 linkage-specification:
14276 extern string-literal { declaration-seq [opt] }
14277 extern string-literal declaration */
14279 static void
14280 cp_parser_linkage_specification (cp_parser* parser)
14282 tree linkage;
14284 /* Look for the `extern' keyword. */
14285 cp_token *extern_token
14286 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14288 /* Look for the string-literal. */
14289 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14290 linkage = cp_parser_string_literal (parser, false, false);
14292 /* Transform the literal into an identifier. If the literal is a
14293 wide-character string, or contains embedded NULs, then we can't
14294 handle it as the user wants. */
14295 if (strlen (TREE_STRING_POINTER (linkage))
14296 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14298 cp_parser_error (parser, "invalid linkage-specification");
14299 /* Assume C++ linkage. */
14300 linkage = lang_name_cplusplus;
14302 else
14303 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14305 /* We're now using the new linkage. */
14306 push_lang_context (linkage);
14308 /* Preserve the location of the the innermost linkage specification,
14309 tracking the locations of nested specifications via a local. */
14310 location_t saved_location
14311 = parser->innermost_linkage_specification_location;
14312 /* Construct a location ranging from the start of the "extern" to
14313 the end of the string-literal, with the caret at the start, e.g.:
14314 extern "C" {
14315 ^~~~~~~~~~
14317 parser->innermost_linkage_specification_location
14318 = make_location (extern_token->location,
14319 extern_token->location,
14320 get_finish (string_token->location));
14322 /* If the next token is a `{', then we're using the first
14323 production. */
14324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14326 cp_ensure_no_omp_declare_simd (parser);
14327 cp_ensure_no_oacc_routine (parser);
14329 /* Consume the `{' token. */
14330 matching_braces braces;
14331 braces.consume_open (parser);
14332 /* Parse the declarations. */
14333 cp_parser_declaration_seq_opt (parser);
14334 /* Look for the closing `}'. */
14335 braces.require_close (parser);
14337 /* Otherwise, there's just one declaration. */
14338 else
14340 bool saved_in_unbraced_linkage_specification_p;
14342 saved_in_unbraced_linkage_specification_p
14343 = parser->in_unbraced_linkage_specification_p;
14344 parser->in_unbraced_linkage_specification_p = true;
14345 cp_parser_declaration (parser);
14346 parser->in_unbraced_linkage_specification_p
14347 = saved_in_unbraced_linkage_specification_p;
14350 /* We're done with the linkage-specification. */
14351 pop_lang_context ();
14353 /* Restore location of parent linkage specification, if any. */
14354 parser->innermost_linkage_specification_location = saved_location;
14357 /* Parse a static_assert-declaration.
14359 static_assert-declaration:
14360 static_assert ( constant-expression , string-literal ) ;
14361 static_assert ( constant-expression ) ; (C++17)
14363 If MEMBER_P, this static_assert is a class member. */
14365 static void
14366 cp_parser_static_assert(cp_parser *parser, bool member_p)
14368 cp_expr condition;
14369 location_t token_loc;
14370 tree message;
14371 bool dummy;
14373 /* Peek at the `static_assert' token so we can keep track of exactly
14374 where the static assertion started. */
14375 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14377 /* Look for the `static_assert' keyword. */
14378 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14379 RT_STATIC_ASSERT))
14380 return;
14382 /* We know we are in a static assertion; commit to any tentative
14383 parse. */
14384 if (cp_parser_parsing_tentatively (parser))
14385 cp_parser_commit_to_tentative_parse (parser);
14387 /* Parse the `(' starting the static assertion condition. */
14388 matching_parens parens;
14389 parens.require_open (parser);
14391 /* Parse the constant-expression. Allow a non-constant expression
14392 here in order to give better diagnostics in finish_static_assert. */
14393 condition =
14394 cp_parser_constant_expression (parser,
14395 /*allow_non_constant_p=*/true,
14396 /*non_constant_p=*/&dummy);
14398 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14400 if (cxx_dialect < cxx17)
14401 pedwarn (input_location, OPT_Wpedantic,
14402 "static_assert without a message "
14403 "only available with -std=c++17 or -std=gnu++17");
14404 /* Eat the ')' */
14405 cp_lexer_consume_token (parser->lexer);
14406 message = build_string (1, "");
14407 TREE_TYPE (message) = char_array_type_node;
14408 fix_string_type (message);
14410 else
14412 /* Parse the separating `,'. */
14413 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14415 /* Parse the string-literal message. */
14416 message = cp_parser_string_literal (parser,
14417 /*translate=*/false,
14418 /*wide_ok=*/true);
14420 /* A `)' completes the static assertion. */
14421 if (!parens.require_close (parser))
14422 cp_parser_skip_to_closing_parenthesis (parser,
14423 /*recovering=*/true,
14424 /*or_comma=*/false,
14425 /*consume_paren=*/true);
14428 /* A semicolon terminates the declaration. */
14429 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14431 /* Get the location for the static assertion. Use that of the
14432 condition if available, otherwise, use that of the "static_assert"
14433 token. */
14434 location_t assert_loc = condition.get_location ();
14435 if (assert_loc == UNKNOWN_LOCATION)
14436 assert_loc = token_loc;
14438 /* Complete the static assertion, which may mean either processing
14439 the static assert now or saving it for template instantiation. */
14440 finish_static_assert (condition, message, assert_loc, member_p);
14443 /* Parse the expression in decltype ( expression ). */
14445 static tree
14446 cp_parser_decltype_expr (cp_parser *parser,
14447 bool &id_expression_or_member_access_p)
14449 cp_token *id_expr_start_token;
14450 tree expr;
14452 /* Since we're going to preserve any side-effects from this parse, set up a
14453 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14454 in the expression. */
14455 tentative_firewall firewall (parser);
14457 /* First, try parsing an id-expression. */
14458 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14459 cp_parser_parse_tentatively (parser);
14460 expr = cp_parser_id_expression (parser,
14461 /*template_keyword_p=*/false,
14462 /*check_dependency_p=*/true,
14463 /*template_p=*/NULL,
14464 /*declarator_p=*/false,
14465 /*optional_p=*/false);
14467 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14469 bool non_integral_constant_expression_p = false;
14470 tree id_expression = expr;
14471 cp_id_kind idk;
14472 const char *error_msg;
14474 if (identifier_p (expr))
14475 /* Lookup the name we got back from the id-expression. */
14476 expr = cp_parser_lookup_name_simple (parser, expr,
14477 id_expr_start_token->location);
14479 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14480 /* A template without args is not a complete id-expression. */
14481 expr = error_mark_node;
14483 if (expr
14484 && expr != error_mark_node
14485 && TREE_CODE (expr) != TYPE_DECL
14486 && (TREE_CODE (expr) != BIT_NOT_EXPR
14487 || !TYPE_P (TREE_OPERAND (expr, 0)))
14488 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14490 /* Complete lookup of the id-expression. */
14491 expr = (finish_id_expression
14492 (id_expression, expr, parser->scope, &idk,
14493 /*integral_constant_expression_p=*/false,
14494 /*allow_non_integral_constant_expression_p=*/true,
14495 &non_integral_constant_expression_p,
14496 /*template_p=*/false,
14497 /*done=*/true,
14498 /*address_p=*/false,
14499 /*template_arg_p=*/false,
14500 &error_msg,
14501 id_expr_start_token->location));
14503 if (expr == error_mark_node)
14504 /* We found an id-expression, but it was something that we
14505 should not have found. This is an error, not something
14506 we can recover from, so note that we found an
14507 id-expression and we'll recover as gracefully as
14508 possible. */
14509 id_expression_or_member_access_p = true;
14512 if (expr
14513 && expr != error_mark_node
14514 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14515 /* We have an id-expression. */
14516 id_expression_or_member_access_p = true;
14519 if (!id_expression_or_member_access_p)
14521 /* Abort the id-expression parse. */
14522 cp_parser_abort_tentative_parse (parser);
14524 /* Parsing tentatively, again. */
14525 cp_parser_parse_tentatively (parser);
14527 /* Parse a class member access. */
14528 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14529 /*cast_p=*/false, /*decltype*/true,
14530 /*member_access_only_p=*/true, NULL);
14532 if (expr
14533 && expr != error_mark_node
14534 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14535 /* We have an id-expression. */
14536 id_expression_or_member_access_p = true;
14539 if (id_expression_or_member_access_p)
14540 /* We have parsed the complete id-expression or member access. */
14541 cp_parser_parse_definitely (parser);
14542 else
14544 /* Abort our attempt to parse an id-expression or member access
14545 expression. */
14546 cp_parser_abort_tentative_parse (parser);
14548 /* Commit to the tentative_firewall so we get syntax errors. */
14549 cp_parser_commit_to_tentative_parse (parser);
14551 /* Parse a full expression. */
14552 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14553 /*decltype_p=*/true);
14556 return expr;
14559 /* Parse a `decltype' type. Returns the type.
14561 simple-type-specifier:
14562 decltype ( expression )
14563 C++14 proposal:
14564 decltype ( auto ) */
14566 static tree
14567 cp_parser_decltype (cp_parser *parser)
14569 bool id_expression_or_member_access_p = false;
14570 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14572 if (start_token->type == CPP_DECLTYPE)
14574 /* Already parsed. */
14575 cp_lexer_consume_token (parser->lexer);
14576 return saved_checks_value (start_token->u.tree_check_value);
14579 /* Look for the `decltype' token. */
14580 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14581 return error_mark_node;
14583 /* Parse the opening `('. */
14584 matching_parens parens;
14585 if (!parens.require_open (parser))
14586 return error_mark_node;
14588 push_deferring_access_checks (dk_deferred);
14590 tree expr = NULL_TREE;
14592 if (cxx_dialect >= cxx14
14593 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14594 /* decltype (auto) */
14595 cp_lexer_consume_token (parser->lexer);
14596 else
14598 /* decltype (expression) */
14600 /* Types cannot be defined in a `decltype' expression. Save away the
14601 old message and set the new one. */
14602 const char *saved_message = parser->type_definition_forbidden_message;
14603 parser->type_definition_forbidden_message
14604 = G_("types may not be defined in %<decltype%> expressions");
14606 /* The restrictions on constant-expressions do not apply inside
14607 decltype expressions. */
14608 bool saved_integral_constant_expression_p
14609 = parser->integral_constant_expression_p;
14610 bool saved_non_integral_constant_expression_p
14611 = parser->non_integral_constant_expression_p;
14612 parser->integral_constant_expression_p = false;
14614 /* Within a parenthesized expression, a `>' token is always
14615 the greater-than operator. */
14616 bool saved_greater_than_is_operator_p
14617 = parser->greater_than_is_operator_p;
14618 parser->greater_than_is_operator_p = true;
14620 /* Do not actually evaluate the expression. */
14621 ++cp_unevaluated_operand;
14623 /* Do not warn about problems with the expression. */
14624 ++c_inhibit_evaluation_warnings;
14626 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14627 STRIP_ANY_LOCATION_WRAPPER (expr);
14629 /* Go back to evaluating expressions. */
14630 --cp_unevaluated_operand;
14631 --c_inhibit_evaluation_warnings;
14633 /* The `>' token might be the end of a template-id or
14634 template-parameter-list now. */
14635 parser->greater_than_is_operator_p
14636 = saved_greater_than_is_operator_p;
14638 /* Restore the old message and the integral constant expression
14639 flags. */
14640 parser->type_definition_forbidden_message = saved_message;
14641 parser->integral_constant_expression_p
14642 = saved_integral_constant_expression_p;
14643 parser->non_integral_constant_expression_p
14644 = saved_non_integral_constant_expression_p;
14647 /* Parse to the closing `)'. */
14648 if (!parens.require_close (parser))
14650 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14651 /*consume_paren=*/true);
14652 pop_deferring_access_checks ();
14653 return error_mark_node;
14656 if (!expr)
14658 /* Build auto. */
14659 expr = make_decltype_auto ();
14660 AUTO_IS_DECLTYPE (expr) = true;
14662 else
14663 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14664 tf_warning_or_error);
14666 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14667 it again. */
14668 start_token->type = CPP_DECLTYPE;
14669 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14670 start_token->u.tree_check_value->value = expr;
14671 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14672 start_token->keyword = RID_MAX;
14673 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14675 pop_to_parent_deferring_access_checks ();
14677 return expr;
14680 /* Special member functions [gram.special] */
14682 /* Parse a conversion-function-id.
14684 conversion-function-id:
14685 operator conversion-type-id
14687 Returns an IDENTIFIER_NODE representing the operator. */
14689 static tree
14690 cp_parser_conversion_function_id (cp_parser* parser)
14692 tree type;
14693 tree saved_scope;
14694 tree saved_qualifying_scope;
14695 tree saved_object_scope;
14696 tree pushed_scope = NULL_TREE;
14698 /* Look for the `operator' token. */
14699 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14700 return error_mark_node;
14701 /* When we parse the conversion-type-id, the current scope will be
14702 reset. However, we need that information in able to look up the
14703 conversion function later, so we save it here. */
14704 saved_scope = parser->scope;
14705 saved_qualifying_scope = parser->qualifying_scope;
14706 saved_object_scope = parser->object_scope;
14707 /* We must enter the scope of the class so that the names of
14708 entities declared within the class are available in the
14709 conversion-type-id. For example, consider:
14711 struct S {
14712 typedef int I;
14713 operator I();
14716 S::operator I() { ... }
14718 In order to see that `I' is a type-name in the definition, we
14719 must be in the scope of `S'. */
14720 if (saved_scope)
14721 pushed_scope = push_scope (saved_scope);
14722 /* Parse the conversion-type-id. */
14723 type = cp_parser_conversion_type_id (parser);
14724 /* Leave the scope of the class, if any. */
14725 if (pushed_scope)
14726 pop_scope (pushed_scope);
14727 /* Restore the saved scope. */
14728 parser->scope = saved_scope;
14729 parser->qualifying_scope = saved_qualifying_scope;
14730 parser->object_scope = saved_object_scope;
14731 /* If the TYPE is invalid, indicate failure. */
14732 if (type == error_mark_node)
14733 return error_mark_node;
14734 return make_conv_op_name (type);
14737 /* Parse a conversion-type-id:
14739 conversion-type-id:
14740 type-specifier-seq conversion-declarator [opt]
14742 Returns the TYPE specified. */
14744 static tree
14745 cp_parser_conversion_type_id (cp_parser* parser)
14747 tree attributes;
14748 cp_decl_specifier_seq type_specifiers;
14749 cp_declarator *declarator;
14750 tree type_specified;
14751 const char *saved_message;
14753 /* Parse the attributes. */
14754 attributes = cp_parser_attributes_opt (parser);
14756 saved_message = parser->type_definition_forbidden_message;
14757 parser->type_definition_forbidden_message
14758 = G_("types may not be defined in a conversion-type-id");
14760 /* Parse the type-specifiers. */
14761 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14762 /*is_declaration=*/false,
14763 /*is_trailing_return=*/false,
14764 &type_specifiers);
14766 parser->type_definition_forbidden_message = saved_message;
14768 /* If that didn't work, stop. */
14769 if (type_specifiers.type == error_mark_node)
14770 return error_mark_node;
14771 /* Parse the conversion-declarator. */
14772 declarator = cp_parser_conversion_declarator_opt (parser);
14774 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14775 /*initialized=*/0, &attributes);
14776 if (attributes)
14777 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14779 /* Don't give this error when parsing tentatively. This happens to
14780 work because we always parse this definitively once. */
14781 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14782 && type_uses_auto (type_specified))
14784 if (cxx_dialect < cxx14)
14786 error ("invalid use of %<auto%> in conversion operator");
14787 return error_mark_node;
14789 else if (template_parm_scope_p ())
14790 warning (0, "use of %<auto%> in member template "
14791 "conversion operator can never be deduced");
14794 return type_specified;
14797 /* Parse an (optional) conversion-declarator.
14799 conversion-declarator:
14800 ptr-operator conversion-declarator [opt]
14804 static cp_declarator *
14805 cp_parser_conversion_declarator_opt (cp_parser* parser)
14807 enum tree_code code;
14808 tree class_type, std_attributes = NULL_TREE;
14809 cp_cv_quals cv_quals;
14811 /* We don't know if there's a ptr-operator next, or not. */
14812 cp_parser_parse_tentatively (parser);
14813 /* Try the ptr-operator. */
14814 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14815 &std_attributes);
14816 /* If it worked, look for more conversion-declarators. */
14817 if (cp_parser_parse_definitely (parser))
14819 cp_declarator *declarator;
14821 /* Parse another optional declarator. */
14822 declarator = cp_parser_conversion_declarator_opt (parser);
14824 declarator = cp_parser_make_indirect_declarator
14825 (code, class_type, cv_quals, declarator, std_attributes);
14827 return declarator;
14830 return NULL;
14833 /* Parse an (optional) ctor-initializer.
14835 ctor-initializer:
14836 : mem-initializer-list */
14838 static void
14839 cp_parser_ctor_initializer_opt (cp_parser* parser)
14841 /* If the next token is not a `:', then there is no
14842 ctor-initializer. */
14843 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14845 /* Do default initialization of any bases and members. */
14846 if (DECL_CONSTRUCTOR_P (current_function_decl))
14847 finish_mem_initializers (NULL_TREE);
14848 return;
14851 /* Consume the `:' token. */
14852 cp_lexer_consume_token (parser->lexer);
14853 /* And the mem-initializer-list. */
14854 cp_parser_mem_initializer_list (parser);
14857 /* Parse a mem-initializer-list.
14859 mem-initializer-list:
14860 mem-initializer ... [opt]
14861 mem-initializer ... [opt] , mem-initializer-list */
14863 static void
14864 cp_parser_mem_initializer_list (cp_parser* parser)
14866 tree mem_initializer_list = NULL_TREE;
14867 tree target_ctor = error_mark_node;
14868 cp_token *token = cp_lexer_peek_token (parser->lexer);
14870 /* Let the semantic analysis code know that we are starting the
14871 mem-initializer-list. */
14872 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14873 error_at (token->location,
14874 "only constructors take member initializers");
14876 /* Loop through the list. */
14877 while (true)
14879 tree mem_initializer;
14881 token = cp_lexer_peek_token (parser->lexer);
14882 /* Parse the mem-initializer. */
14883 mem_initializer = cp_parser_mem_initializer (parser);
14884 /* If the next token is a `...', we're expanding member initializers. */
14885 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14886 if (ellipsis
14887 || (mem_initializer != error_mark_node
14888 && check_for_bare_parameter_packs (TREE_PURPOSE
14889 (mem_initializer))))
14891 /* Consume the `...'. */
14892 if (ellipsis)
14893 cp_lexer_consume_token (parser->lexer);
14895 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14896 can be expanded but members cannot. */
14897 if (mem_initializer != error_mark_node
14898 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14900 error_at (token->location,
14901 "cannot expand initializer for member %qD",
14902 TREE_PURPOSE (mem_initializer));
14903 mem_initializer = error_mark_node;
14906 /* Construct the pack expansion type. */
14907 if (mem_initializer != error_mark_node)
14908 mem_initializer = make_pack_expansion (mem_initializer);
14910 if (target_ctor != error_mark_node
14911 && mem_initializer != error_mark_node)
14913 error ("mem-initializer for %qD follows constructor delegation",
14914 TREE_PURPOSE (mem_initializer));
14915 mem_initializer = error_mark_node;
14917 /* Look for a target constructor. */
14918 if (mem_initializer != error_mark_node
14919 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14920 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14922 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14923 if (mem_initializer_list)
14925 error ("constructor delegation follows mem-initializer for %qD",
14926 TREE_PURPOSE (mem_initializer_list));
14927 mem_initializer = error_mark_node;
14929 target_ctor = mem_initializer;
14931 /* Add it to the list, unless it was erroneous. */
14932 if (mem_initializer != error_mark_node)
14934 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14935 mem_initializer_list = mem_initializer;
14937 /* If the next token is not a `,', we're done. */
14938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14939 break;
14940 /* Consume the `,' token. */
14941 cp_lexer_consume_token (parser->lexer);
14944 /* Perform semantic analysis. */
14945 if (DECL_CONSTRUCTOR_P (current_function_decl))
14946 finish_mem_initializers (mem_initializer_list);
14949 /* Parse a mem-initializer.
14951 mem-initializer:
14952 mem-initializer-id ( expression-list [opt] )
14953 mem-initializer-id braced-init-list
14955 GNU extension:
14957 mem-initializer:
14958 ( expression-list [opt] )
14960 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14961 class) or FIELD_DECL (for a non-static data member) to initialize;
14962 the TREE_VALUE is the expression-list. An empty initialization
14963 list is represented by void_list_node. */
14965 static tree
14966 cp_parser_mem_initializer (cp_parser* parser)
14968 tree mem_initializer_id;
14969 tree expression_list;
14970 tree member;
14971 cp_token *token = cp_lexer_peek_token (parser->lexer);
14973 /* Find out what is being initialized. */
14974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14976 permerror (token->location,
14977 "anachronistic old-style base class initializer");
14978 mem_initializer_id = NULL_TREE;
14980 else
14982 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14983 if (mem_initializer_id == error_mark_node)
14984 return mem_initializer_id;
14986 member = expand_member_init (mem_initializer_id);
14987 if (member && !DECL_P (member))
14988 in_base_initializer = 1;
14990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14992 bool expr_non_constant_p;
14993 cp_lexer_set_source_position (parser->lexer);
14994 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14995 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14996 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14997 expression_list = build_tree_list (NULL_TREE, expression_list);
14999 else
15001 vec<tree, va_gc> *vec;
15002 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15003 /*cast_p=*/false,
15004 /*allow_expansion_p=*/true,
15005 /*non_constant_p=*/NULL,
15006 /*close_paren_loc=*/NULL,
15007 /*wrap_locations_p=*/true);
15008 if (vec == NULL)
15009 return error_mark_node;
15010 expression_list = build_tree_list_vec (vec);
15011 release_tree_vector (vec);
15014 if (expression_list == error_mark_node)
15015 return error_mark_node;
15016 if (!expression_list)
15017 expression_list = void_type_node;
15019 in_base_initializer = 0;
15021 return member ? build_tree_list (member, expression_list) : error_mark_node;
15024 /* Parse a mem-initializer-id.
15026 mem-initializer-id:
15027 :: [opt] nested-name-specifier [opt] class-name
15028 decltype-specifier (C++11)
15029 identifier
15031 Returns a TYPE indicating the class to be initialized for the first
15032 production (and the second in C++11). Returns an IDENTIFIER_NODE
15033 indicating the data member to be initialized for the last production. */
15035 static tree
15036 cp_parser_mem_initializer_id (cp_parser* parser)
15038 bool global_scope_p;
15039 bool nested_name_specifier_p;
15040 bool template_p = false;
15041 tree id;
15043 cp_token *token = cp_lexer_peek_token (parser->lexer);
15045 /* `typename' is not allowed in this context ([temp.res]). */
15046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15048 error_at (token->location,
15049 "keyword %<typename%> not allowed in this context (a qualified "
15050 "member initializer is implicitly a type)");
15051 cp_lexer_consume_token (parser->lexer);
15053 /* Look for the optional `::' operator. */
15054 global_scope_p
15055 = (cp_parser_global_scope_opt (parser,
15056 /*current_scope_valid_p=*/false)
15057 != NULL_TREE);
15058 /* Look for the optional nested-name-specifier. The simplest way to
15059 implement:
15061 [temp.res]
15063 The keyword `typename' is not permitted in a base-specifier or
15064 mem-initializer; in these contexts a qualified name that
15065 depends on a template-parameter is implicitly assumed to be a
15066 type name.
15068 is to assume that we have seen the `typename' keyword at this
15069 point. */
15070 nested_name_specifier_p
15071 = (cp_parser_nested_name_specifier_opt (parser,
15072 /*typename_keyword_p=*/true,
15073 /*check_dependency_p=*/true,
15074 /*type_p=*/true,
15075 /*is_declaration=*/true)
15076 != NULL_TREE);
15077 if (nested_name_specifier_p)
15078 template_p = cp_parser_optional_template_keyword (parser);
15079 /* If there is a `::' operator or a nested-name-specifier, then we
15080 are definitely looking for a class-name. */
15081 if (global_scope_p || nested_name_specifier_p)
15082 return cp_parser_class_name (parser,
15083 /*typename_keyword_p=*/true,
15084 /*template_keyword_p=*/template_p,
15085 typename_type,
15086 /*check_dependency_p=*/true,
15087 /*class_head_p=*/false,
15088 /*is_declaration=*/true);
15089 /* Otherwise, we could also be looking for an ordinary identifier. */
15090 cp_parser_parse_tentatively (parser);
15091 if (cp_lexer_next_token_is_decltype (parser->lexer))
15092 /* Try a decltype-specifier. */
15093 id = cp_parser_decltype (parser);
15094 else
15095 /* Otherwise, try a class-name. */
15096 id = cp_parser_class_name (parser,
15097 /*typename_keyword_p=*/true,
15098 /*template_keyword_p=*/false,
15099 none_type,
15100 /*check_dependency_p=*/true,
15101 /*class_head_p=*/false,
15102 /*is_declaration=*/true);
15103 /* If we found one, we're done. */
15104 if (cp_parser_parse_definitely (parser))
15105 return id;
15106 /* Otherwise, look for an ordinary identifier. */
15107 return cp_parser_identifier (parser);
15110 /* Overloading [gram.over] */
15112 /* Parse an operator-function-id.
15114 operator-function-id:
15115 operator operator
15117 Returns an IDENTIFIER_NODE for the operator which is a
15118 human-readable spelling of the identifier, e.g., `operator +'. */
15120 static cp_expr
15121 cp_parser_operator_function_id (cp_parser* parser)
15123 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15124 /* Look for the `operator' keyword. */
15125 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15126 return error_mark_node;
15127 /* And then the name of the operator itself. */
15128 return cp_parser_operator (parser, start_loc);
15131 /* Return an identifier node for a user-defined literal operator.
15132 The suffix identifier is chained to the operator name identifier. */
15134 tree
15135 cp_literal_operator_id (const char* name)
15137 tree identifier;
15138 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15139 + strlen (name) + 10);
15140 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15141 identifier = get_identifier (buffer);
15143 return identifier;
15146 /* Parse an operator.
15148 operator:
15149 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15150 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15151 || ++ -- , ->* -> () []
15153 GNU Extensions:
15155 operator:
15156 <? >? <?= >?=
15158 Returns an IDENTIFIER_NODE for the operator which is a
15159 human-readable spelling of the identifier, e.g., `operator +'. */
15161 static cp_expr
15162 cp_parser_operator (cp_parser* parser, location_t start_loc)
15164 tree id = NULL_TREE;
15165 cp_token *token;
15166 bool utf8 = false;
15168 /* Peek at the next token. */
15169 token = cp_lexer_peek_token (parser->lexer);
15171 location_t end_loc = token->location;
15173 /* Figure out which operator we have. */
15174 enum tree_code op = ERROR_MARK;
15175 bool assop = false;
15176 bool consumed = false;
15177 switch (token->type)
15179 case CPP_KEYWORD:
15181 /* The keyword should be either `new' or `delete'. */
15182 if (token->keyword == RID_NEW)
15183 op = NEW_EXPR;
15184 else if (token->keyword == RID_DELETE)
15185 op = DELETE_EXPR;
15186 else
15187 break;
15189 /* Consume the `new' or `delete' token. */
15190 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15192 /* Peek at the next token. */
15193 token = cp_lexer_peek_token (parser->lexer);
15194 /* If it's a `[' token then this is the array variant of the
15195 operator. */
15196 if (token->type == CPP_OPEN_SQUARE)
15198 /* Consume the `[' token. */
15199 cp_lexer_consume_token (parser->lexer);
15200 /* Look for the `]' token. */
15201 if (cp_token *close_token
15202 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15203 end_loc = close_token->location;
15204 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15206 consumed = true;
15207 break;
15210 case CPP_PLUS:
15211 op = PLUS_EXPR;
15212 break;
15214 case CPP_MINUS:
15215 op = MINUS_EXPR;
15216 break;
15218 case CPP_MULT:
15219 op = MULT_EXPR;
15220 break;
15222 case CPP_DIV:
15223 op = TRUNC_DIV_EXPR;
15224 break;
15226 case CPP_MOD:
15227 op = TRUNC_MOD_EXPR;
15228 break;
15230 case CPP_XOR:
15231 op = BIT_XOR_EXPR;
15232 break;
15234 case CPP_AND:
15235 op = BIT_AND_EXPR;
15236 break;
15238 case CPP_OR:
15239 op = BIT_IOR_EXPR;
15240 break;
15242 case CPP_COMPL:
15243 op = BIT_NOT_EXPR;
15244 break;
15246 case CPP_NOT:
15247 op = TRUTH_NOT_EXPR;
15248 break;
15250 case CPP_EQ:
15251 assop = true;
15252 op = NOP_EXPR;
15253 break;
15255 case CPP_LESS:
15256 op = LT_EXPR;
15257 break;
15259 case CPP_GREATER:
15260 op = GT_EXPR;
15261 break;
15263 case CPP_PLUS_EQ:
15264 assop = true;
15265 op = PLUS_EXPR;
15266 break;
15268 case CPP_MINUS_EQ:
15269 assop = true;
15270 op = MINUS_EXPR;
15271 break;
15273 case CPP_MULT_EQ:
15274 assop = true;
15275 op = MULT_EXPR;
15276 break;
15278 case CPP_DIV_EQ:
15279 assop = true;
15280 op = TRUNC_DIV_EXPR;
15281 break;
15283 case CPP_MOD_EQ:
15284 assop = true;
15285 op = TRUNC_MOD_EXPR;
15286 break;
15288 case CPP_XOR_EQ:
15289 assop = true;
15290 op = BIT_XOR_EXPR;
15291 break;
15293 case CPP_AND_EQ:
15294 assop = true;
15295 op = BIT_AND_EXPR;
15296 break;
15298 case CPP_OR_EQ:
15299 assop = true;
15300 op = BIT_IOR_EXPR;
15301 break;
15303 case CPP_LSHIFT:
15304 op = LSHIFT_EXPR;
15305 break;
15307 case CPP_RSHIFT:
15308 op = RSHIFT_EXPR;
15309 break;
15311 case CPP_LSHIFT_EQ:
15312 assop = true;
15313 op = LSHIFT_EXPR;
15314 break;
15316 case CPP_RSHIFT_EQ:
15317 assop = true;
15318 op = RSHIFT_EXPR;
15319 break;
15321 case CPP_EQ_EQ:
15322 op = EQ_EXPR;
15323 break;
15325 case CPP_NOT_EQ:
15326 op = NE_EXPR;
15327 break;
15329 case CPP_LESS_EQ:
15330 op = LE_EXPR;
15331 break;
15333 case CPP_GREATER_EQ:
15334 op = GE_EXPR;
15335 break;
15337 case CPP_AND_AND:
15338 op = TRUTH_ANDIF_EXPR;
15339 break;
15341 case CPP_OR_OR:
15342 op = TRUTH_ORIF_EXPR;
15343 break;
15345 case CPP_PLUS_PLUS:
15346 op = POSTINCREMENT_EXPR;
15347 break;
15349 case CPP_MINUS_MINUS:
15350 op = PREDECREMENT_EXPR;
15351 break;
15353 case CPP_COMMA:
15354 op = COMPOUND_EXPR;
15355 break;
15357 case CPP_DEREF_STAR:
15358 op = MEMBER_REF;
15359 break;
15361 case CPP_DEREF:
15362 op = COMPONENT_REF;
15363 break;
15365 case CPP_OPEN_PAREN:
15367 /* Consume the `('. */
15368 matching_parens parens;
15369 parens.consume_open (parser);
15370 /* Look for the matching `)'. */
15371 token = parens.require_close (parser);
15372 if (token)
15373 end_loc = token->location;
15374 op = CALL_EXPR;
15375 consumed = true;
15376 break;
15379 case CPP_OPEN_SQUARE:
15380 /* Consume the `['. */
15381 cp_lexer_consume_token (parser->lexer);
15382 /* Look for the matching `]'. */
15383 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15384 if (token)
15385 end_loc = token->location;
15386 op = ARRAY_REF;
15387 consumed = true;
15388 break;
15390 case CPP_UTF8STRING:
15391 case CPP_UTF8STRING_USERDEF:
15392 utf8 = true;
15393 /* FALLTHRU */
15394 case CPP_STRING:
15395 case CPP_WSTRING:
15396 case CPP_STRING16:
15397 case CPP_STRING32:
15398 case CPP_STRING_USERDEF:
15399 case CPP_WSTRING_USERDEF:
15400 case CPP_STRING16_USERDEF:
15401 case CPP_STRING32_USERDEF:
15403 cp_expr str;
15404 tree string_tree;
15405 int sz, len;
15407 if (cxx_dialect == cxx98)
15408 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15410 /* Consume the string. */
15411 str = cp_parser_string_literal (parser, /*translate=*/true,
15412 /*wide_ok=*/true, /*lookup_udlit=*/false);
15413 if (str == error_mark_node)
15414 return error_mark_node;
15415 else if (TREE_CODE (str) == USERDEF_LITERAL)
15417 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15418 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15419 end_loc = str.get_location ();
15421 else
15423 string_tree = str;
15424 /* Look for the suffix identifier. */
15425 token = cp_lexer_peek_token (parser->lexer);
15426 if (token->type == CPP_NAME)
15428 id = cp_parser_identifier (parser);
15429 end_loc = token->location;
15431 else if (token->type == CPP_KEYWORD)
15433 error ("unexpected keyword;"
15434 " remove space between quotes and suffix identifier");
15435 return error_mark_node;
15437 else
15439 error ("expected suffix identifier");
15440 return error_mark_node;
15443 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15444 (TREE_TYPE (TREE_TYPE (string_tree))));
15445 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15446 if (len != 0)
15448 error ("expected empty string after %<operator%> keyword");
15449 return error_mark_node;
15451 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15452 != char_type_node)
15454 error ("invalid encoding prefix in literal operator");
15455 return error_mark_node;
15457 if (id != error_mark_node)
15459 const char *name = IDENTIFIER_POINTER (id);
15460 id = cp_literal_operator_id (name);
15462 /* Generate a location of the form:
15463 "" _suffix_identifier
15464 ^~~~~~~~~~~~~~~~~~~~~
15465 with caret == start at the start token, finish at the end of the
15466 suffix identifier. */
15467 location_t finish_loc
15468 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15469 location_t combined_loc
15470 = make_location (start_loc, start_loc, finish_loc);
15471 return cp_expr (id, combined_loc);
15474 default:
15475 /* Anything else is an error. */
15476 break;
15479 /* If we have selected an identifier, we need to consume the
15480 operator token. */
15481 if (op != ERROR_MARK)
15483 id = ovl_op_identifier (assop, op);
15484 if (!consumed)
15485 cp_lexer_consume_token (parser->lexer);
15487 /* Otherwise, no valid operator name was present. */
15488 else
15490 cp_parser_error (parser, "expected operator");
15491 id = error_mark_node;
15494 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15495 return cp_expr (id, start_loc);
15498 /* Parse a template-declaration.
15500 template-declaration:
15501 export [opt] template < template-parameter-list > declaration
15503 If MEMBER_P is TRUE, this template-declaration occurs within a
15504 class-specifier.
15506 The grammar rule given by the standard isn't correct. What
15507 is really meant is:
15509 template-declaration:
15510 export [opt] template-parameter-list-seq
15511 decl-specifier-seq [opt] init-declarator [opt] ;
15512 export [opt] template-parameter-list-seq
15513 function-definition
15515 template-parameter-list-seq:
15516 template-parameter-list-seq [opt]
15517 template < template-parameter-list >
15519 Concept Extensions:
15521 template-parameter-list-seq:
15522 template < template-parameter-list > requires-clause [opt]
15524 requires-clause:
15525 requires logical-or-expression */
15527 static void
15528 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15530 /* Check for `export'. */
15531 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15533 /* Consume the `export' token. */
15534 cp_lexer_consume_token (parser->lexer);
15535 /* Warn that we do not support `export'. */
15536 warning (0, "keyword %<export%> not implemented, and will be ignored");
15539 cp_parser_template_declaration_after_export (parser, member_p);
15542 /* Parse a template-parameter-list.
15544 template-parameter-list:
15545 template-parameter
15546 template-parameter-list , template-parameter
15548 Returns a TREE_LIST. Each node represents a template parameter.
15549 The nodes are connected via their TREE_CHAINs. */
15551 static tree
15552 cp_parser_template_parameter_list (cp_parser* parser)
15554 tree parameter_list = NULL_TREE;
15556 /* Don't create wrapper nodes within a template-parameter-list,
15557 since we don't want to have different types based on the
15558 spelling location of constants and decls within them. */
15559 auto_suppress_location_wrappers sentinel;
15561 begin_template_parm_list ();
15563 /* The loop below parses the template parms. We first need to know
15564 the total number of template parms to be able to compute proper
15565 canonical types of each dependent type. So after the loop, when
15566 we know the total number of template parms,
15567 end_template_parm_list computes the proper canonical types and
15568 fixes up the dependent types accordingly. */
15569 while (true)
15571 tree parameter;
15572 bool is_non_type;
15573 bool is_parameter_pack;
15574 location_t parm_loc;
15576 /* Parse the template-parameter. */
15577 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15578 parameter = cp_parser_template_parameter (parser,
15579 &is_non_type,
15580 &is_parameter_pack);
15581 /* Add it to the list. */
15582 if (parameter != error_mark_node)
15583 parameter_list = process_template_parm (parameter_list,
15584 parm_loc,
15585 parameter,
15586 is_non_type,
15587 is_parameter_pack);
15588 else
15590 tree err_parm = build_tree_list (parameter, parameter);
15591 parameter_list = chainon (parameter_list, err_parm);
15594 /* If the next token is not a `,', we're done. */
15595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15596 break;
15597 /* Otherwise, consume the `,' token. */
15598 cp_lexer_consume_token (parser->lexer);
15601 return end_template_parm_list (parameter_list);
15604 /* Parse a introduction-list.
15606 introduction-list:
15607 introduced-parameter
15608 introduction-list , introduced-parameter
15610 introduced-parameter:
15611 ...[opt] identifier
15613 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15614 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15615 WILDCARD_DECL will also have DECL_NAME set and token location in
15616 DECL_SOURCE_LOCATION. */
15618 static tree
15619 cp_parser_introduction_list (cp_parser *parser)
15621 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15623 while (true)
15625 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15626 if (is_pack)
15627 cp_lexer_consume_token (parser->lexer);
15629 tree identifier = cp_parser_identifier (parser);
15630 if (identifier == error_mark_node)
15631 break;
15633 /* Build placeholder. */
15634 tree parm = build_nt (WILDCARD_DECL);
15635 DECL_SOURCE_LOCATION (parm)
15636 = cp_lexer_peek_token (parser->lexer)->location;
15637 DECL_NAME (parm) = identifier;
15638 WILDCARD_PACK_P (parm) = is_pack;
15639 vec_safe_push (introduction_vec, parm);
15641 /* If the next token is not a `,', we're done. */
15642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15643 break;
15644 /* Otherwise, consume the `,' token. */
15645 cp_lexer_consume_token (parser->lexer);
15648 /* Convert the vec into a TREE_VEC. */
15649 tree introduction_list = make_tree_vec (introduction_vec->length ());
15650 unsigned int n;
15651 tree parm;
15652 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15653 TREE_VEC_ELT (introduction_list, n) = parm;
15655 release_tree_vector (introduction_vec);
15656 return introduction_list;
15659 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15660 is an abstract declarator. */
15662 static inline cp_declarator*
15663 get_id_declarator (cp_declarator *declarator)
15665 cp_declarator *d = declarator;
15666 while (d && d->kind != cdk_id)
15667 d = d->declarator;
15668 return d;
15671 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15672 is an abstract declarator. */
15674 static inline tree
15675 get_unqualified_id (cp_declarator *declarator)
15677 declarator = get_id_declarator (declarator);
15678 if (declarator)
15679 return declarator->u.id.unqualified_name;
15680 else
15681 return NULL_TREE;
15684 /* Returns true if DECL represents a constrained-parameter. */
15686 static inline bool
15687 is_constrained_parameter (tree decl)
15689 return (decl
15690 && TREE_CODE (decl) == TYPE_DECL
15691 && CONSTRAINED_PARM_CONCEPT (decl)
15692 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15695 /* Returns true if PARM declares a constrained-parameter. */
15697 static inline bool
15698 is_constrained_parameter (cp_parameter_declarator *parm)
15700 return is_constrained_parameter (parm->decl_specifiers.type);
15703 /* Check that the type parameter is only a declarator-id, and that its
15704 type is not cv-qualified. */
15706 bool
15707 cp_parser_check_constrained_type_parm (cp_parser *parser,
15708 cp_parameter_declarator *parm)
15710 if (!parm->declarator)
15711 return true;
15713 if (parm->declarator->kind != cdk_id)
15715 cp_parser_error (parser, "invalid constrained type parameter");
15716 return false;
15719 /* Don't allow cv-qualified type parameters. */
15720 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15721 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15723 cp_parser_error (parser, "cv-qualified type parameter");
15724 return false;
15727 return true;
15730 /* Finish parsing/processing a template type parameter and checking
15731 various restrictions. */
15733 static inline tree
15734 cp_parser_constrained_type_template_parm (cp_parser *parser,
15735 tree id,
15736 cp_parameter_declarator* parmdecl)
15738 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15739 return finish_template_type_parm (class_type_node, id);
15740 else
15741 return error_mark_node;
15744 static tree
15745 finish_constrained_template_template_parm (tree proto, tree id)
15747 /* FIXME: This should probably be copied, and we may need to adjust
15748 the template parameter depths. */
15749 tree saved_parms = current_template_parms;
15750 begin_template_parm_list ();
15751 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15752 end_template_parm_list ();
15754 tree parm = finish_template_template_parm (class_type_node, id);
15755 current_template_parms = saved_parms;
15757 return parm;
15760 /* Finish parsing/processing a template template parameter by borrowing
15761 the template parameter list from the prototype parameter. */
15763 static tree
15764 cp_parser_constrained_template_template_parm (cp_parser *parser,
15765 tree proto,
15766 tree id,
15767 cp_parameter_declarator *parmdecl)
15769 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15770 return error_mark_node;
15771 return finish_constrained_template_template_parm (proto, id);
15774 /* Create a new non-type template parameter from the given PARM
15775 declarator. */
15777 static tree
15778 constrained_non_type_template_parm (bool *is_non_type,
15779 cp_parameter_declarator *parm)
15781 *is_non_type = true;
15782 cp_declarator *decl = parm->declarator;
15783 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15784 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15785 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15788 /* Build a constrained template parameter based on the PARMDECL
15789 declarator. The type of PARMDECL is the constrained type, which
15790 refers to the prototype template parameter that ultimately
15791 specifies the type of the declared parameter. */
15793 static tree
15794 finish_constrained_parameter (cp_parser *parser,
15795 cp_parameter_declarator *parmdecl,
15796 bool *is_non_type,
15797 bool *is_parameter_pack)
15799 tree decl = parmdecl->decl_specifiers.type;
15800 tree id = get_unqualified_id (parmdecl->declarator);
15801 tree def = parmdecl->default_argument;
15802 tree proto = DECL_INITIAL (decl);
15804 /* A template parameter constrained by a variadic concept shall also
15805 be declared as a template parameter pack. */
15806 bool is_variadic = template_parameter_pack_p (proto);
15807 if (is_variadic && !*is_parameter_pack)
15808 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15810 /* Build the parameter. Return an error if the declarator was invalid. */
15811 tree parm;
15812 if (TREE_CODE (proto) == TYPE_DECL)
15813 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15814 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15815 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15816 parmdecl);
15817 else
15818 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15819 if (parm == error_mark_node)
15820 return error_mark_node;
15822 /* Finish the parameter decl and create a node attaching the
15823 default argument and constraint. */
15824 parm = build_tree_list (def, parm);
15825 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15827 return parm;
15830 /* Returns true if the parsed type actually represents the declaration
15831 of a type template-parameter. */
15833 static inline bool
15834 declares_constrained_type_template_parameter (tree type)
15836 return (is_constrained_parameter (type)
15837 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15841 /* Returns true if the parsed type actually represents the declaration of
15842 a template template-parameter. */
15844 static bool
15845 declares_constrained_template_template_parameter (tree type)
15847 return (is_constrained_parameter (type)
15848 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15851 /* Parse a default argument for a type template-parameter.
15852 Note that diagnostics are handled in cp_parser_template_parameter. */
15854 static tree
15855 cp_parser_default_type_template_argument (cp_parser *parser)
15857 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15859 /* Consume the `=' token. */
15860 cp_lexer_consume_token (parser->lexer);
15862 cp_token *token = cp_lexer_peek_token (parser->lexer);
15864 /* Parse the default-argument. */
15865 push_deferring_access_checks (dk_no_deferred);
15866 tree default_argument = cp_parser_type_id (parser,
15867 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15868 NULL);
15869 pop_deferring_access_checks ();
15871 if (flag_concepts && type_uses_auto (default_argument))
15873 error_at (token->location,
15874 "invalid use of %<auto%> in default template argument");
15875 return error_mark_node;
15878 return default_argument;
15881 /* Parse a default argument for a template template-parameter. */
15883 static tree
15884 cp_parser_default_template_template_argument (cp_parser *parser)
15886 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15888 bool is_template;
15890 /* Consume the `='. */
15891 cp_lexer_consume_token (parser->lexer);
15892 /* Parse the id-expression. */
15893 push_deferring_access_checks (dk_no_deferred);
15894 /* save token before parsing the id-expression, for error
15895 reporting */
15896 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15897 tree default_argument
15898 = cp_parser_id_expression (parser,
15899 /*template_keyword_p=*/false,
15900 /*check_dependency_p=*/true,
15901 /*template_p=*/&is_template,
15902 /*declarator_p=*/false,
15903 /*optional_p=*/false);
15904 if (TREE_CODE (default_argument) == TYPE_DECL)
15905 /* If the id-expression was a template-id that refers to
15906 a template-class, we already have the declaration here,
15907 so no further lookup is needed. */
15909 else
15910 /* Look up the name. */
15911 default_argument
15912 = cp_parser_lookup_name (parser, default_argument,
15913 none_type,
15914 /*is_template=*/is_template,
15915 /*is_namespace=*/false,
15916 /*check_dependency=*/true,
15917 /*ambiguous_decls=*/NULL,
15918 token->location);
15919 /* See if the default argument is valid. */
15920 default_argument = check_template_template_default_arg (default_argument);
15921 pop_deferring_access_checks ();
15922 return default_argument;
15925 /* Parse a template-parameter.
15927 template-parameter:
15928 type-parameter
15929 parameter-declaration
15931 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15932 the parameter. The TREE_PURPOSE is the default value, if any.
15933 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15934 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15935 set to true iff this parameter is a parameter pack. */
15937 static tree
15938 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15939 bool *is_parameter_pack)
15941 cp_token *token;
15942 cp_parameter_declarator *parameter_declarator;
15943 tree parm;
15945 /* Assume it is a type parameter or a template parameter. */
15946 *is_non_type = false;
15947 /* Assume it not a parameter pack. */
15948 *is_parameter_pack = false;
15949 /* Peek at the next token. */
15950 token = cp_lexer_peek_token (parser->lexer);
15951 /* If it is `template', we have a type-parameter. */
15952 if (token->keyword == RID_TEMPLATE)
15953 return cp_parser_type_parameter (parser, is_parameter_pack);
15954 /* If it is `class' or `typename' we do not know yet whether it is a
15955 type parameter or a non-type parameter. Consider:
15957 template <typename T, typename T::X X> ...
15961 template <class C, class D*> ...
15963 Here, the first parameter is a type parameter, and the second is
15964 a non-type parameter. We can tell by looking at the token after
15965 the identifier -- if it is a `,', `=', or `>' then we have a type
15966 parameter. */
15967 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15969 /* Peek at the token after `class' or `typename'. */
15970 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15971 /* If it's an ellipsis, we have a template type parameter
15972 pack. */
15973 if (token->type == CPP_ELLIPSIS)
15974 return cp_parser_type_parameter (parser, is_parameter_pack);
15975 /* If it's an identifier, skip it. */
15976 if (token->type == CPP_NAME)
15977 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15978 /* Now, see if the token looks like the end of a template
15979 parameter. */
15980 if (token->type == CPP_COMMA
15981 || token->type == CPP_EQ
15982 || token->type == CPP_GREATER)
15983 return cp_parser_type_parameter (parser, is_parameter_pack);
15986 /* Otherwise, it is a non-type parameter or a constrained parameter.
15988 [temp.param]
15990 When parsing a default template-argument for a non-type
15991 template-parameter, the first non-nested `>' is taken as the end
15992 of the template parameter-list rather than a greater-than
15993 operator. */
15994 parameter_declarator
15995 = cp_parser_parameter_declaration (parser,
15996 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15997 /*template_parm_p=*/true,
15998 /*parenthesized_p=*/NULL);
16000 if (!parameter_declarator)
16001 return error_mark_node;
16003 /* If the parameter declaration is marked as a parameter pack, set
16004 *IS_PARAMETER_PACK to notify the caller. */
16005 if (parameter_declarator->template_parameter_pack_p)
16006 *is_parameter_pack = true;
16008 if (parameter_declarator->default_argument)
16010 /* Can happen in some cases of erroneous input (c++/34892). */
16011 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16012 /* Consume the `...' for better error recovery. */
16013 cp_lexer_consume_token (parser->lexer);
16016 // The parameter may have been constrained.
16017 if (is_constrained_parameter (parameter_declarator))
16018 return finish_constrained_parameter (parser,
16019 parameter_declarator,
16020 is_non_type,
16021 is_parameter_pack);
16023 // Now we're sure that the parameter is a non-type parameter.
16024 *is_non_type = true;
16026 parm = grokdeclarator (parameter_declarator->declarator,
16027 &parameter_declarator->decl_specifiers,
16028 TPARM, /*initialized=*/0,
16029 /*attrlist=*/NULL);
16030 if (parm == error_mark_node)
16031 return error_mark_node;
16033 return build_tree_list (parameter_declarator->default_argument, parm);
16036 /* Parse a type-parameter.
16038 type-parameter:
16039 class identifier [opt]
16040 class identifier [opt] = type-id
16041 typename identifier [opt]
16042 typename identifier [opt] = type-id
16043 template < template-parameter-list > class identifier [opt]
16044 template < template-parameter-list > class identifier [opt]
16045 = id-expression
16047 GNU Extension (variadic templates):
16049 type-parameter:
16050 class ... identifier [opt]
16051 typename ... identifier [opt]
16053 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16054 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16055 the declaration of the parameter.
16057 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16059 static tree
16060 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16062 cp_token *token;
16063 tree parameter;
16065 /* Look for a keyword to tell us what kind of parameter this is. */
16066 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16067 if (!token)
16068 return error_mark_node;
16070 switch (token->keyword)
16072 case RID_CLASS:
16073 case RID_TYPENAME:
16075 tree identifier;
16076 tree default_argument;
16078 /* If the next token is an ellipsis, we have a template
16079 argument pack. */
16080 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16082 /* Consume the `...' token. */
16083 cp_lexer_consume_token (parser->lexer);
16084 maybe_warn_variadic_templates ();
16086 *is_parameter_pack = true;
16089 /* If the next token is an identifier, then it names the
16090 parameter. */
16091 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16092 identifier = cp_parser_identifier (parser);
16093 else
16094 identifier = NULL_TREE;
16096 /* Create the parameter. */
16097 parameter = finish_template_type_parm (class_type_node, identifier);
16099 /* If the next token is an `=', we have a default argument. */
16100 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16102 default_argument
16103 = cp_parser_default_type_template_argument (parser);
16105 /* Template parameter packs cannot have default
16106 arguments. */
16107 if (*is_parameter_pack)
16109 if (identifier)
16110 error_at (token->location,
16111 "template parameter pack %qD cannot have a "
16112 "default argument", identifier);
16113 else
16114 error_at (token->location,
16115 "template parameter packs cannot have "
16116 "default arguments");
16117 default_argument = NULL_TREE;
16119 else if (check_for_bare_parameter_packs (default_argument))
16120 default_argument = error_mark_node;
16122 else
16123 default_argument = NULL_TREE;
16125 /* Create the combined representation of the parameter and the
16126 default argument. */
16127 parameter = build_tree_list (default_argument, parameter);
16129 break;
16131 case RID_TEMPLATE:
16133 tree identifier;
16134 tree default_argument;
16136 /* Look for the `<'. */
16137 cp_parser_require (parser, CPP_LESS, RT_LESS);
16138 /* Parse the template-parameter-list. */
16139 cp_parser_template_parameter_list (parser);
16140 /* Look for the `>'. */
16141 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16143 // If template requirements are present, parse them.
16144 if (flag_concepts)
16146 tree reqs = get_shorthand_constraints (current_template_parms);
16147 if (tree r = cp_parser_requires_clause_opt (parser))
16148 reqs = conjoin_constraints (reqs, normalize_expression (r));
16149 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16152 /* Look for the `class' or 'typename' keywords. */
16153 cp_parser_type_parameter_key (parser);
16154 /* If the next token is an ellipsis, we have a template
16155 argument pack. */
16156 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16158 /* Consume the `...' token. */
16159 cp_lexer_consume_token (parser->lexer);
16160 maybe_warn_variadic_templates ();
16162 *is_parameter_pack = true;
16164 /* If the next token is an `=', then there is a
16165 default-argument. If the next token is a `>', we are at
16166 the end of the parameter-list. If the next token is a `,',
16167 then we are at the end of this parameter. */
16168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16169 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16170 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16172 identifier = cp_parser_identifier (parser);
16173 /* Treat invalid names as if the parameter were nameless. */
16174 if (identifier == error_mark_node)
16175 identifier = NULL_TREE;
16177 else
16178 identifier = NULL_TREE;
16180 /* Create the template parameter. */
16181 parameter = finish_template_template_parm (class_type_node,
16182 identifier);
16184 /* If the next token is an `=', then there is a
16185 default-argument. */
16186 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16188 default_argument
16189 = cp_parser_default_template_template_argument (parser);
16191 /* Template parameter packs cannot have default
16192 arguments. */
16193 if (*is_parameter_pack)
16195 if (identifier)
16196 error_at (token->location,
16197 "template parameter pack %qD cannot "
16198 "have a default argument",
16199 identifier);
16200 else
16201 error_at (token->location, "template parameter packs cannot "
16202 "have default arguments");
16203 default_argument = NULL_TREE;
16206 else
16207 default_argument = NULL_TREE;
16209 /* Create the combined representation of the parameter and the
16210 default argument. */
16211 parameter = build_tree_list (default_argument, parameter);
16213 break;
16215 default:
16216 gcc_unreachable ();
16217 break;
16220 return parameter;
16223 /* Parse a template-id.
16225 template-id:
16226 template-name < template-argument-list [opt] >
16228 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16229 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16230 returned. Otherwise, if the template-name names a function, or set
16231 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16232 names a class, returns a TYPE_DECL for the specialization.
16234 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16235 uninstantiated templates. */
16237 static tree
16238 cp_parser_template_id (cp_parser *parser,
16239 bool template_keyword_p,
16240 bool check_dependency_p,
16241 enum tag_types tag_type,
16242 bool is_declaration)
16244 tree templ;
16245 tree arguments;
16246 tree template_id;
16247 cp_token_position start_of_id = 0;
16248 cp_token *next_token = NULL, *next_token_2 = NULL;
16249 bool is_identifier;
16251 /* If the next token corresponds to a template-id, there is no need
16252 to reparse it. */
16253 cp_token *token = cp_lexer_peek_token (parser->lexer);
16254 if (token->type == CPP_TEMPLATE_ID)
16256 cp_lexer_consume_token (parser->lexer);
16257 return saved_checks_value (token->u.tree_check_value);
16260 /* Avoid performing name lookup if there is no possibility of
16261 finding a template-id. */
16262 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16263 || (token->type == CPP_NAME
16264 && !cp_parser_nth_token_starts_template_argument_list_p
16265 (parser, 2)))
16267 cp_parser_error (parser, "expected template-id");
16268 return error_mark_node;
16271 /* Remember where the template-id starts. */
16272 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16273 start_of_id = cp_lexer_token_position (parser->lexer, false);
16275 push_deferring_access_checks (dk_deferred);
16277 /* Parse the template-name. */
16278 is_identifier = false;
16279 templ = cp_parser_template_name (parser, template_keyword_p,
16280 check_dependency_p,
16281 is_declaration,
16282 tag_type,
16283 &is_identifier);
16285 /* Push any access checks inside the firewall we're about to create. */
16286 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16287 pop_deferring_access_checks ();
16288 if (templ == error_mark_node || is_identifier)
16289 return templ;
16291 /* Since we're going to preserve any side-effects from this parse, set up a
16292 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16293 in the template arguments. */
16294 tentative_firewall firewall (parser);
16295 reopen_deferring_access_checks (checks);
16297 /* If we find the sequence `[:' after a template-name, it's probably
16298 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16299 parse correctly the argument list. */
16300 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16301 == CPP_OPEN_SQUARE)
16302 && next_token->flags & DIGRAPH
16303 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16304 == CPP_COLON)
16305 && !(next_token_2->flags & PREV_WHITE))
16307 cp_parser_parse_tentatively (parser);
16308 /* Change `:' into `::'. */
16309 next_token_2->type = CPP_SCOPE;
16310 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16311 CPP_LESS. */
16312 cp_lexer_consume_token (parser->lexer);
16314 /* Parse the arguments. */
16315 arguments = cp_parser_enclosed_template_argument_list (parser);
16316 if (!cp_parser_parse_definitely (parser))
16318 /* If we couldn't parse an argument list, then we revert our changes
16319 and return simply an error. Maybe this is not a template-id
16320 after all. */
16321 next_token_2->type = CPP_COLON;
16322 cp_parser_error (parser, "expected %<<%>");
16323 pop_deferring_access_checks ();
16324 return error_mark_node;
16326 /* Otherwise, emit an error about the invalid digraph, but continue
16327 parsing because we got our argument list. */
16328 if (permerror (next_token->location,
16329 "%<<::%> cannot begin a template-argument list"))
16331 static bool hint = false;
16332 inform (next_token->location,
16333 "%<<:%> is an alternate spelling for %<[%>."
16334 " Insert whitespace between %<<%> and %<::%>");
16335 if (!hint && !flag_permissive)
16337 inform (next_token->location, "(if you use %<-fpermissive%> "
16338 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16339 "accept your code)");
16340 hint = true;
16344 else
16346 /* Look for the `<' that starts the template-argument-list. */
16347 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16349 pop_deferring_access_checks ();
16350 return error_mark_node;
16352 /* Parse the arguments. */
16353 arguments = cp_parser_enclosed_template_argument_list (parser);
16355 if ((cxx_dialect > cxx17)
16356 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16357 && !template_keyword_p
16358 && (cp_parser_error_occurred (parser)
16359 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16361 /* This didn't go well. */
16362 if (TREE_CODE (templ) == FUNCTION_DECL)
16364 /* C++2A says that "function-name < a;" is now ill-formed. */
16365 if (cp_parser_error_occurred (parser))
16367 error_at (token->location, "invalid template-argument-list");
16368 inform (token->location, "function name as the left hand "
16369 "operand of %<<%> is ill-formed in C++2a; wrap the "
16370 "function name in %<()%>");
16372 else
16373 /* We expect "f<targs>" to be followed by "(args)". */
16374 error_at (cp_lexer_peek_token (parser->lexer)->location,
16375 "expected %<(%> after template-argument-list");
16376 if (start_of_id)
16377 /* Purge all subsequent tokens. */
16378 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16380 else
16381 cp_parser_simulate_error (parser);
16382 pop_deferring_access_checks ();
16383 return error_mark_node;
16387 /* Set the location to be of the form:
16388 template-name < template-argument-list [opt] >
16389 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16390 with caret == start at the start of the template-name,
16391 ranging until the closing '>'. */
16392 location_t finish_loc
16393 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16394 location_t combined_loc
16395 = make_location (token->location, token->location, finish_loc);
16397 /* Check for concepts autos where they don't belong. We could
16398 identify types in some cases of idnetifier TEMPL, looking ahead
16399 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16400 types. We reject them in functions, but if what we have is an
16401 identifier, even with none_type we can't conclude it's NOT a
16402 type, we have to wait for template substitution. */
16403 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16404 template_id = error_mark_node;
16405 /* Build a representation of the specialization. */
16406 else if (identifier_p (templ))
16407 template_id = build_min_nt_loc (combined_loc,
16408 TEMPLATE_ID_EXPR,
16409 templ, arguments);
16410 else if (DECL_TYPE_TEMPLATE_P (templ)
16411 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16413 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16414 template (rather than some instantiation thereof) only if
16415 is not nested within some other construct. For example, in
16416 "template <typename T> void f(T) { A<T>::", A<T> is just an
16417 instantiation of A. */
16418 bool entering_scope
16419 = (template_parm_scope_p ()
16420 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16421 template_id
16422 = finish_template_type (templ, arguments, entering_scope);
16424 /* A template-like identifier may be a partial concept id. */
16425 else if (flag_concepts
16426 && (template_id = (cp_parser_maybe_partial_concept_id
16427 (parser, templ, arguments))))
16428 return template_id;
16429 else if (variable_template_p (templ))
16431 template_id = lookup_template_variable (templ, arguments);
16432 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16433 SET_EXPR_LOCATION (template_id, combined_loc);
16435 else
16437 /* If it's not a class-template or a template-template, it should be
16438 a function-template. */
16439 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16440 || TREE_CODE (templ) == OVERLOAD
16441 || TREE_CODE (templ) == FUNCTION_DECL
16442 || BASELINK_P (templ)));
16444 template_id = lookup_template_function (templ, arguments);
16445 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16446 SET_EXPR_LOCATION (template_id, combined_loc);
16449 /* If parsing tentatively, replace the sequence of tokens that makes
16450 up the template-id with a CPP_TEMPLATE_ID token. That way,
16451 should we re-parse the token stream, we will not have to repeat
16452 the effort required to do the parse, nor will we issue duplicate
16453 error messages about problems during instantiation of the
16454 template. */
16455 if (start_of_id
16456 /* Don't do this if we had a parse error in a declarator; re-parsing
16457 might succeed if a name changes meaning (60361). */
16458 && !(cp_parser_error_occurred (parser)
16459 && cp_parser_parsing_tentatively (parser)
16460 && parser->in_declarator_p))
16462 /* Reset the contents of the START_OF_ID token. */
16463 token->type = CPP_TEMPLATE_ID;
16464 token->location = combined_loc;
16466 /* Retrieve any deferred checks. Do not pop this access checks yet
16467 so the memory will not be reclaimed during token replacing below. */
16468 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16469 token->u.tree_check_value->value = template_id;
16470 token->u.tree_check_value->checks = get_deferred_access_checks ();
16471 token->keyword = RID_MAX;
16473 /* Purge all subsequent tokens. */
16474 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16476 /* ??? Can we actually assume that, if template_id ==
16477 error_mark_node, we will have issued a diagnostic to the
16478 user, as opposed to simply marking the tentative parse as
16479 failed? */
16480 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16481 error_at (token->location, "parse error in template argument list");
16484 pop_to_parent_deferring_access_checks ();
16485 return template_id;
16488 /* Parse a template-name.
16490 template-name:
16491 identifier
16493 The standard should actually say:
16495 template-name:
16496 identifier
16497 operator-function-id
16499 A defect report has been filed about this issue.
16501 A conversion-function-id cannot be a template name because they cannot
16502 be part of a template-id. In fact, looking at this code:
16504 a.operator K<int>()
16506 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16507 It is impossible to call a templated conversion-function-id with an
16508 explicit argument list, since the only allowed template parameter is
16509 the type to which it is converting.
16511 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16512 `template' keyword, in a construction like:
16514 T::template f<3>()
16516 In that case `f' is taken to be a template-name, even though there
16517 is no way of knowing for sure.
16519 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16520 name refers to a set of overloaded functions, at least one of which
16521 is a template, or an IDENTIFIER_NODE with the name of the template,
16522 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16523 names are looked up inside uninstantiated templates. */
16525 static tree
16526 cp_parser_template_name (cp_parser* parser,
16527 bool template_keyword_p,
16528 bool check_dependency_p,
16529 bool is_declaration,
16530 enum tag_types tag_type,
16531 bool *is_identifier)
16533 tree identifier;
16534 tree decl;
16535 cp_token *token = cp_lexer_peek_token (parser->lexer);
16537 /* If the next token is `operator', then we have either an
16538 operator-function-id or a conversion-function-id. */
16539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16541 /* We don't know whether we're looking at an
16542 operator-function-id or a conversion-function-id. */
16543 cp_parser_parse_tentatively (parser);
16544 /* Try an operator-function-id. */
16545 identifier = cp_parser_operator_function_id (parser);
16546 /* If that didn't work, try a conversion-function-id. */
16547 if (!cp_parser_parse_definitely (parser))
16549 cp_parser_error (parser, "expected template-name");
16550 return error_mark_node;
16553 /* Look for the identifier. */
16554 else
16555 identifier = cp_parser_identifier (parser);
16557 /* If we didn't find an identifier, we don't have a template-id. */
16558 if (identifier == error_mark_node)
16559 return error_mark_node;
16561 /* If the name immediately followed the `template' keyword, then it
16562 is a template-name. However, if the next token is not `<', then
16563 we do not treat it as a template-name, since it is not being used
16564 as part of a template-id. This enables us to handle constructs
16565 like:
16567 template <typename T> struct S { S(); };
16568 template <typename T> S<T>::S();
16570 correctly. We would treat `S' as a template -- if it were `S<T>'
16571 -- but we do not if there is no `<'. */
16573 if (processing_template_decl
16574 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16576 /* In a declaration, in a dependent context, we pretend that the
16577 "template" keyword was present in order to improve error
16578 recovery. For example, given:
16580 template <typename T> void f(T::X<int>);
16582 we want to treat "X<int>" as a template-id. */
16583 if (is_declaration
16584 && !template_keyword_p
16585 && parser->scope && TYPE_P (parser->scope)
16586 && check_dependency_p
16587 && dependent_scope_p (parser->scope)
16588 /* Do not do this for dtors (or ctors), since they never
16589 need the template keyword before their name. */
16590 && !constructor_name_p (identifier, parser->scope))
16592 cp_token_position start = 0;
16594 /* Explain what went wrong. */
16595 error_at (token->location, "non-template %qD used as template",
16596 identifier);
16597 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16598 parser->scope, identifier);
16599 /* If parsing tentatively, find the location of the "<" token. */
16600 if (cp_parser_simulate_error (parser))
16601 start = cp_lexer_token_position (parser->lexer, true);
16602 /* Parse the template arguments so that we can issue error
16603 messages about them. */
16604 cp_lexer_consume_token (parser->lexer);
16605 cp_parser_enclosed_template_argument_list (parser);
16606 /* Skip tokens until we find a good place from which to
16607 continue parsing. */
16608 cp_parser_skip_to_closing_parenthesis (parser,
16609 /*recovering=*/true,
16610 /*or_comma=*/true,
16611 /*consume_paren=*/false);
16612 /* If parsing tentatively, permanently remove the
16613 template argument list. That will prevent duplicate
16614 error messages from being issued about the missing
16615 "template" keyword. */
16616 if (start)
16617 cp_lexer_purge_tokens_after (parser->lexer, start);
16618 if (is_identifier)
16619 *is_identifier = true;
16620 parser->context->object_type = NULL_TREE;
16621 return identifier;
16624 /* If the "template" keyword is present, then there is generally
16625 no point in doing name-lookup, so we just return IDENTIFIER.
16626 But, if the qualifying scope is non-dependent then we can
16627 (and must) do name-lookup normally. */
16628 if (template_keyword_p)
16630 tree scope = (parser->scope ? parser->scope
16631 : parser->context->object_type);
16632 if (scope && TYPE_P (scope)
16633 && (!CLASS_TYPE_P (scope)
16634 || (check_dependency_p && dependent_type_p (scope))))
16636 /* We're optimizing away the call to cp_parser_lookup_name, but
16637 we still need to do this. */
16638 parser->context->object_type = NULL_TREE;
16639 return identifier;
16644 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16645 const bool scoped_p = ((parser->scope ? parser->scope
16646 : parser->context->object_type) != NULL_TREE);
16648 /* Look up the name. */
16649 decl = cp_parser_lookup_name (parser, identifier,
16650 tag_type,
16651 /*is_template=*/true,
16652 /*is_namespace=*/false,
16653 check_dependency_p,
16654 /*ambiguous_decls=*/NULL,
16655 token->location);
16657 decl = strip_using_decl (decl);
16659 /* If DECL is a template, then the name was a template-name. */
16660 if (TREE_CODE (decl) == TEMPLATE_DECL)
16662 if (TREE_DEPRECATED (decl)
16663 && deprecated_state != DEPRECATED_SUPPRESS)
16664 warn_deprecated_use (decl, NULL_TREE);
16666 else
16668 /* The standard does not explicitly indicate whether a name that
16669 names a set of overloaded declarations, some of which are
16670 templates, is a template-name. However, such a name should
16671 be a template-name; otherwise, there is no way to form a
16672 template-id for the overloaded templates. */
16673 bool found = false;
16675 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16676 !found && iter; ++iter)
16677 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16678 found = true;
16680 if (!found
16681 && (cxx_dialect > cxx17)
16682 && !scoped_p
16683 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16684 && tag_type == none_type)
16686 /* [temp.names] says "A name is also considered to refer to a template
16687 if it is an unqualified-id followed by a < and name lookup finds
16688 either one or more functions or finds nothing." */
16690 /* The "more functions" case. Just use the OVERLOAD as normally.
16691 We don't use is_overloaded_fn here to avoid considering
16692 BASELINKs. */
16693 if (TREE_CODE (decl) == OVERLOAD
16694 /* Name lookup found one function. */
16695 || TREE_CODE (decl) == FUNCTION_DECL)
16696 found = true;
16697 /* Name lookup found nothing. */
16698 else if (decl == error_mark_node)
16699 return identifier;
16702 if (!found)
16704 /* The name does not name a template. */
16705 cp_parser_error (parser, "expected template-name");
16706 return error_mark_node;
16710 return decl;
16713 /* Parse a template-argument-list.
16715 template-argument-list:
16716 template-argument ... [opt]
16717 template-argument-list , template-argument ... [opt]
16719 Returns a TREE_VEC containing the arguments. */
16721 static tree
16722 cp_parser_template_argument_list (cp_parser* parser)
16724 tree fixed_args[10];
16725 unsigned n_args = 0;
16726 unsigned alloced = 10;
16727 tree *arg_ary = fixed_args;
16728 tree vec;
16729 bool saved_in_template_argument_list_p;
16730 bool saved_ice_p;
16731 bool saved_non_ice_p;
16733 /* Don't create location wrapper nodes within a template-argument-list. */
16734 auto_suppress_location_wrappers sentinel;
16736 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16737 parser->in_template_argument_list_p = true;
16738 /* Even if the template-id appears in an integral
16739 constant-expression, the contents of the argument list do
16740 not. */
16741 saved_ice_p = parser->integral_constant_expression_p;
16742 parser->integral_constant_expression_p = false;
16743 saved_non_ice_p = parser->non_integral_constant_expression_p;
16744 parser->non_integral_constant_expression_p = false;
16746 /* Parse the arguments. */
16749 tree argument;
16751 if (n_args)
16752 /* Consume the comma. */
16753 cp_lexer_consume_token (parser->lexer);
16755 /* Parse the template-argument. */
16756 argument = cp_parser_template_argument (parser);
16758 /* If the next token is an ellipsis, we're expanding a template
16759 argument pack. */
16760 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16762 if (argument == error_mark_node)
16764 cp_token *token = cp_lexer_peek_token (parser->lexer);
16765 error_at (token->location,
16766 "expected parameter pack before %<...%>");
16768 /* Consume the `...' token. */
16769 cp_lexer_consume_token (parser->lexer);
16771 /* Make the argument into a TYPE_PACK_EXPANSION or
16772 EXPR_PACK_EXPANSION. */
16773 argument = make_pack_expansion (argument);
16776 if (n_args == alloced)
16778 alloced *= 2;
16780 if (arg_ary == fixed_args)
16782 arg_ary = XNEWVEC (tree, alloced);
16783 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16785 else
16786 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16788 arg_ary[n_args++] = argument;
16790 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16792 vec = make_tree_vec (n_args);
16794 while (n_args--)
16795 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16797 if (arg_ary != fixed_args)
16798 free (arg_ary);
16799 parser->non_integral_constant_expression_p = saved_non_ice_p;
16800 parser->integral_constant_expression_p = saved_ice_p;
16801 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16802 if (CHECKING_P)
16803 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16804 return vec;
16807 /* Parse a template-argument.
16809 template-argument:
16810 assignment-expression
16811 type-id
16812 id-expression
16814 The representation is that of an assignment-expression, type-id, or
16815 id-expression -- except that the qualified id-expression is
16816 evaluated, so that the value returned is either a DECL or an
16817 OVERLOAD.
16819 Although the standard says "assignment-expression", it forbids
16820 throw-expressions or assignments in the template argument.
16821 Therefore, we use "conditional-expression" instead. */
16823 static tree
16824 cp_parser_template_argument (cp_parser* parser)
16826 tree argument;
16827 bool template_p;
16828 bool address_p;
16829 bool maybe_type_id = false;
16830 cp_token *token = NULL, *argument_start_token = NULL;
16831 location_t loc = 0;
16832 cp_id_kind idk;
16834 /* There's really no way to know what we're looking at, so we just
16835 try each alternative in order.
16837 [temp.arg]
16839 In a template-argument, an ambiguity between a type-id and an
16840 expression is resolved to a type-id, regardless of the form of
16841 the corresponding template-parameter.
16843 Therefore, we try a type-id first. */
16844 cp_parser_parse_tentatively (parser);
16845 argument = cp_parser_template_type_arg (parser);
16846 /* If there was no error parsing the type-id but the next token is a
16847 '>>', our behavior depends on which dialect of C++ we're
16848 parsing. In C++98, we probably found a typo for '> >'. But there
16849 are type-id which are also valid expressions. For instance:
16851 struct X { int operator >> (int); };
16852 template <int V> struct Foo {};
16853 Foo<X () >> 5> r;
16855 Here 'X()' is a valid type-id of a function type, but the user just
16856 wanted to write the expression "X() >> 5". Thus, we remember that we
16857 found a valid type-id, but we still try to parse the argument as an
16858 expression to see what happens.
16860 In C++0x, the '>>' will be considered two separate '>'
16861 tokens. */
16862 if (!cp_parser_error_occurred (parser)
16863 && cxx_dialect == cxx98
16864 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16866 maybe_type_id = true;
16867 cp_parser_abort_tentative_parse (parser);
16869 else
16871 /* If the next token isn't a `,' or a `>', then this argument wasn't
16872 really finished. This means that the argument is not a valid
16873 type-id. */
16874 if (!cp_parser_next_token_ends_template_argument_p (parser))
16875 cp_parser_error (parser, "expected template-argument");
16876 /* If that worked, we're done. */
16877 if (cp_parser_parse_definitely (parser))
16878 return argument;
16880 /* We're still not sure what the argument will be. */
16881 cp_parser_parse_tentatively (parser);
16882 /* Try a template. */
16883 argument_start_token = cp_lexer_peek_token (parser->lexer);
16884 argument = cp_parser_id_expression (parser,
16885 /*template_keyword_p=*/false,
16886 /*check_dependency_p=*/true,
16887 &template_p,
16888 /*declarator_p=*/false,
16889 /*optional_p=*/false);
16890 /* If the next token isn't a `,' or a `>', then this argument wasn't
16891 really finished. */
16892 if (!cp_parser_next_token_ends_template_argument_p (parser))
16893 cp_parser_error (parser, "expected template-argument");
16894 if (!cp_parser_error_occurred (parser))
16896 /* Figure out what is being referred to. If the id-expression
16897 was for a class template specialization, then we will have a
16898 TYPE_DECL at this point. There is no need to do name lookup
16899 at this point in that case. */
16900 if (TREE_CODE (argument) != TYPE_DECL)
16901 argument = cp_parser_lookup_name (parser, argument,
16902 none_type,
16903 /*is_template=*/template_p,
16904 /*is_namespace=*/false,
16905 /*check_dependency=*/true,
16906 /*ambiguous_decls=*/NULL,
16907 argument_start_token->location);
16908 /* Handle a constrained-type-specifier for a non-type template
16909 parameter. */
16910 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16911 argument = decl;
16912 else if (TREE_CODE (argument) != TEMPLATE_DECL
16913 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16914 cp_parser_error (parser, "expected template-name");
16916 if (cp_parser_parse_definitely (parser))
16918 if (TREE_DEPRECATED (argument))
16919 warn_deprecated_use (argument, NULL_TREE);
16920 return argument;
16922 /* It must be a non-type argument. In C++17 any constant-expression is
16923 allowed. */
16924 if (cxx_dialect > cxx14)
16925 goto general_expr;
16927 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16929 -- an integral constant-expression of integral or enumeration
16930 type; or
16932 -- the name of a non-type template-parameter; or
16934 -- the name of an object or function with external linkage...
16936 -- the address of an object or function with external linkage...
16938 -- a pointer to member... */
16939 /* Look for a non-type template parameter. */
16940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16942 cp_parser_parse_tentatively (parser);
16943 argument = cp_parser_primary_expression (parser,
16944 /*address_p=*/false,
16945 /*cast_p=*/false,
16946 /*template_arg_p=*/true,
16947 &idk);
16948 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16949 || !cp_parser_next_token_ends_template_argument_p (parser))
16950 cp_parser_simulate_error (parser);
16951 if (cp_parser_parse_definitely (parser))
16952 return argument;
16955 /* If the next token is "&", the argument must be the address of an
16956 object or function with external linkage. */
16957 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16958 if (address_p)
16960 loc = cp_lexer_peek_token (parser->lexer)->location;
16961 cp_lexer_consume_token (parser->lexer);
16963 /* See if we might have an id-expression. */
16964 token = cp_lexer_peek_token (parser->lexer);
16965 if (token->type == CPP_NAME
16966 || token->keyword == RID_OPERATOR
16967 || token->type == CPP_SCOPE
16968 || token->type == CPP_TEMPLATE_ID
16969 || token->type == CPP_NESTED_NAME_SPECIFIER)
16971 cp_parser_parse_tentatively (parser);
16972 argument = cp_parser_primary_expression (parser,
16973 address_p,
16974 /*cast_p=*/false,
16975 /*template_arg_p=*/true,
16976 &idk);
16977 if (cp_parser_error_occurred (parser)
16978 || !cp_parser_next_token_ends_template_argument_p (parser))
16979 cp_parser_abort_tentative_parse (parser);
16980 else
16982 tree probe;
16984 if (INDIRECT_REF_P (argument))
16986 /* Strip the dereference temporarily. */
16987 gcc_assert (REFERENCE_REF_P (argument));
16988 argument = TREE_OPERAND (argument, 0);
16991 /* If we're in a template, we represent a qualified-id referring
16992 to a static data member as a SCOPE_REF even if the scope isn't
16993 dependent so that we can check access control later. */
16994 probe = argument;
16995 if (TREE_CODE (probe) == SCOPE_REF)
16996 probe = TREE_OPERAND (probe, 1);
16997 if (VAR_P (probe))
16999 /* A variable without external linkage might still be a
17000 valid constant-expression, so no error is issued here
17001 if the external-linkage check fails. */
17002 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17003 cp_parser_simulate_error (parser);
17005 else if (is_overloaded_fn (argument))
17006 /* All overloaded functions are allowed; if the external
17007 linkage test does not pass, an error will be issued
17008 later. */
17010 else if (address_p
17011 && (TREE_CODE (argument) == OFFSET_REF
17012 || TREE_CODE (argument) == SCOPE_REF))
17013 /* A pointer-to-member. */
17015 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17017 else
17018 cp_parser_simulate_error (parser);
17020 if (cp_parser_parse_definitely (parser))
17022 if (address_p)
17023 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17024 tf_warning_or_error);
17025 else
17026 argument = convert_from_reference (argument);
17027 return argument;
17031 /* If the argument started with "&", there are no other valid
17032 alternatives at this point. */
17033 if (address_p)
17035 cp_parser_error (parser, "invalid non-type template argument");
17036 return error_mark_node;
17039 general_expr:
17040 /* If the argument wasn't successfully parsed as a type-id followed
17041 by '>>', the argument can only be a constant expression now.
17042 Otherwise, we try parsing the constant-expression tentatively,
17043 because the argument could really be a type-id. */
17044 if (maybe_type_id)
17045 cp_parser_parse_tentatively (parser);
17047 if (cxx_dialect <= cxx14)
17048 argument = cp_parser_constant_expression (parser);
17049 else
17051 /* In C++20, we can encounter a braced-init-list. */
17052 if (cxx_dialect >= cxx2a
17053 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17055 bool expr_non_constant_p;
17056 return cp_parser_braced_list (parser, &expr_non_constant_p);
17059 /* With C++17 generalized non-type template arguments we need to handle
17060 lvalue constant expressions, too. */
17061 argument = cp_parser_assignment_expression (parser);
17062 require_potential_constant_expression (argument);
17065 if (!maybe_type_id)
17066 return argument;
17067 if (!cp_parser_next_token_ends_template_argument_p (parser))
17068 cp_parser_error (parser, "expected template-argument");
17069 if (cp_parser_parse_definitely (parser))
17070 return argument;
17071 /* We did our best to parse the argument as a non type-id, but that
17072 was the only alternative that matched (albeit with a '>' after
17073 it). We can assume it's just a typo from the user, and a
17074 diagnostic will then be issued. */
17075 return cp_parser_template_type_arg (parser);
17078 /* Parse an explicit-instantiation.
17080 explicit-instantiation:
17081 template declaration
17083 Although the standard says `declaration', what it really means is:
17085 explicit-instantiation:
17086 template decl-specifier-seq [opt] declarator [opt] ;
17088 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17089 supposed to be allowed. A defect report has been filed about this
17090 issue.
17092 GNU Extension:
17094 explicit-instantiation:
17095 storage-class-specifier template
17096 decl-specifier-seq [opt] declarator [opt] ;
17097 function-specifier template
17098 decl-specifier-seq [opt] declarator [opt] ; */
17100 static void
17101 cp_parser_explicit_instantiation (cp_parser* parser)
17103 int declares_class_or_enum;
17104 cp_decl_specifier_seq decl_specifiers;
17105 tree extension_specifier = NULL_TREE;
17107 timevar_push (TV_TEMPLATE_INST);
17109 /* Look for an (optional) storage-class-specifier or
17110 function-specifier. */
17111 if (cp_parser_allow_gnu_extensions_p (parser))
17113 extension_specifier
17114 = cp_parser_storage_class_specifier_opt (parser);
17115 if (!extension_specifier)
17116 extension_specifier
17117 = cp_parser_function_specifier_opt (parser,
17118 /*decl_specs=*/NULL);
17121 /* Look for the `template' keyword. */
17122 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17123 /* Let the front end know that we are processing an explicit
17124 instantiation. */
17125 begin_explicit_instantiation ();
17126 /* [temp.explicit] says that we are supposed to ignore access
17127 control while processing explicit instantiation directives. */
17128 push_deferring_access_checks (dk_no_check);
17129 /* Parse a decl-specifier-seq. */
17130 cp_parser_decl_specifier_seq (parser,
17131 CP_PARSER_FLAGS_OPTIONAL,
17132 &decl_specifiers,
17133 &declares_class_or_enum);
17134 /* If there was exactly one decl-specifier, and it declared a class,
17135 and there's no declarator, then we have an explicit type
17136 instantiation. */
17137 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17139 tree type;
17141 type = check_tag_decl (&decl_specifiers,
17142 /*explicit_type_instantiation_p=*/true);
17143 /* Turn access control back on for names used during
17144 template instantiation. */
17145 pop_deferring_access_checks ();
17146 if (type)
17147 do_type_instantiation (type, extension_specifier,
17148 /*complain=*/tf_error);
17150 else
17152 cp_declarator *declarator;
17153 tree decl;
17155 /* Parse the declarator. */
17156 declarator
17157 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17158 CP_PARSER_FLAGS_NONE,
17159 /*ctor_dtor_or_conv_p=*/NULL,
17160 /*parenthesized_p=*/NULL,
17161 /*member_p=*/false,
17162 /*friend_p=*/false,
17163 /*static_p=*/false);
17164 if (declares_class_or_enum & 2)
17165 cp_parser_check_for_definition_in_return_type (declarator,
17166 decl_specifiers.type,
17167 decl_specifiers.locations[ds_type_spec]);
17168 if (declarator != cp_error_declarator)
17170 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17171 permerror (decl_specifiers.locations[ds_inline],
17172 "explicit instantiation shall not use"
17173 " %<inline%> specifier");
17174 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17175 permerror (decl_specifiers.locations[ds_constexpr],
17176 "explicit instantiation shall not use"
17177 " %<constexpr%> specifier");
17179 decl = grokdeclarator (declarator, &decl_specifiers,
17180 NORMAL, 0, &decl_specifiers.attributes);
17181 /* Turn access control back on for names used during
17182 template instantiation. */
17183 pop_deferring_access_checks ();
17184 /* Do the explicit instantiation. */
17185 do_decl_instantiation (decl, extension_specifier);
17187 else
17189 pop_deferring_access_checks ();
17190 /* Skip the body of the explicit instantiation. */
17191 cp_parser_skip_to_end_of_statement (parser);
17194 /* We're done with the instantiation. */
17195 end_explicit_instantiation ();
17197 cp_parser_consume_semicolon_at_end_of_statement (parser);
17199 timevar_pop (TV_TEMPLATE_INST);
17202 /* Parse an explicit-specialization.
17204 explicit-specialization:
17205 template < > declaration
17207 Although the standard says `declaration', what it really means is:
17209 explicit-specialization:
17210 template <> decl-specifier [opt] init-declarator [opt] ;
17211 template <> function-definition
17212 template <> explicit-specialization
17213 template <> template-declaration */
17215 static void
17216 cp_parser_explicit_specialization (cp_parser* parser)
17218 bool need_lang_pop;
17219 cp_token *token = cp_lexer_peek_token (parser->lexer);
17221 /* Look for the `template' keyword. */
17222 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17223 /* Look for the `<'. */
17224 cp_parser_require (parser, CPP_LESS, RT_LESS);
17225 /* Look for the `>'. */
17226 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17227 /* We have processed another parameter list. */
17228 ++parser->num_template_parameter_lists;
17229 /* [temp]
17231 A template ... explicit specialization ... shall not have C
17232 linkage. */
17233 if (current_lang_name == lang_name_c)
17235 error_at (token->location, "template specialization with C linkage");
17236 maybe_show_extern_c_location ();
17237 /* Give it C++ linkage to avoid confusing other parts of the
17238 front end. */
17239 push_lang_context (lang_name_cplusplus);
17240 need_lang_pop = true;
17242 else
17243 need_lang_pop = false;
17244 /* Let the front end know that we are beginning a specialization. */
17245 if (!begin_specialization ())
17247 end_specialization ();
17248 return;
17251 /* If the next keyword is `template', we need to figure out whether
17252 or not we're looking a template-declaration. */
17253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17255 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17256 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17257 cp_parser_template_declaration_after_export (parser,
17258 /*member_p=*/false);
17259 else
17260 cp_parser_explicit_specialization (parser);
17262 else
17263 /* Parse the dependent declaration. */
17264 cp_parser_single_declaration (parser,
17265 /*checks=*/NULL,
17266 /*member_p=*/false,
17267 /*explicit_specialization_p=*/true,
17268 /*friend_p=*/NULL);
17269 /* We're done with the specialization. */
17270 end_specialization ();
17271 /* For the erroneous case of a template with C linkage, we pushed an
17272 implicit C++ linkage scope; exit that scope now. */
17273 if (need_lang_pop)
17274 pop_lang_context ();
17275 /* We're done with this parameter list. */
17276 --parser->num_template_parameter_lists;
17279 /* Parse a type-specifier.
17281 type-specifier:
17282 simple-type-specifier
17283 class-specifier
17284 enum-specifier
17285 elaborated-type-specifier
17286 cv-qualifier
17288 GNU Extension:
17290 type-specifier:
17291 __complex__
17293 Returns a representation of the type-specifier. For a
17294 class-specifier, enum-specifier, or elaborated-type-specifier, a
17295 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17297 The parser flags FLAGS is used to control type-specifier parsing.
17299 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17300 in a decl-specifier-seq.
17302 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17303 class-specifier, enum-specifier, or elaborated-type-specifier, then
17304 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17305 if a type is declared; 2 if it is defined. Otherwise, it is set to
17306 zero.
17308 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17309 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17310 is set to FALSE. */
17312 static tree
17313 cp_parser_type_specifier (cp_parser* parser,
17314 cp_parser_flags flags,
17315 cp_decl_specifier_seq *decl_specs,
17316 bool is_declaration,
17317 int* declares_class_or_enum,
17318 bool* is_cv_qualifier)
17320 tree type_spec = NULL_TREE;
17321 cp_token *token;
17322 enum rid keyword;
17323 cp_decl_spec ds = ds_last;
17325 /* Assume this type-specifier does not declare a new type. */
17326 if (declares_class_or_enum)
17327 *declares_class_or_enum = 0;
17328 /* And that it does not specify a cv-qualifier. */
17329 if (is_cv_qualifier)
17330 *is_cv_qualifier = false;
17331 /* Peek at the next token. */
17332 token = cp_lexer_peek_token (parser->lexer);
17334 /* If we're looking at a keyword, we can use that to guide the
17335 production we choose. */
17336 keyword = token->keyword;
17337 switch (keyword)
17339 case RID_ENUM:
17340 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17341 goto elaborated_type_specifier;
17343 /* Look for the enum-specifier. */
17344 type_spec = cp_parser_enum_specifier (parser);
17345 /* If that worked, we're done. */
17346 if (type_spec)
17348 if (declares_class_or_enum)
17349 *declares_class_or_enum = 2;
17350 if (decl_specs)
17351 cp_parser_set_decl_spec_type (decl_specs,
17352 type_spec,
17353 token,
17354 /*type_definition_p=*/true);
17355 return type_spec;
17357 else
17358 goto elaborated_type_specifier;
17360 /* Any of these indicate either a class-specifier, or an
17361 elaborated-type-specifier. */
17362 case RID_CLASS:
17363 case RID_STRUCT:
17364 case RID_UNION:
17365 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17366 goto elaborated_type_specifier;
17368 /* Parse tentatively so that we can back up if we don't find a
17369 class-specifier. */
17370 cp_parser_parse_tentatively (parser);
17371 /* Look for the class-specifier. */
17372 type_spec = cp_parser_class_specifier (parser);
17373 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17374 /* If that worked, we're done. */
17375 if (cp_parser_parse_definitely (parser))
17377 if (declares_class_or_enum)
17378 *declares_class_or_enum = 2;
17379 if (decl_specs)
17380 cp_parser_set_decl_spec_type (decl_specs,
17381 type_spec,
17382 token,
17383 /*type_definition_p=*/true);
17384 return type_spec;
17387 /* Fall through. */
17388 elaborated_type_specifier:
17389 /* We're declaring (not defining) a class or enum. */
17390 if (declares_class_or_enum)
17391 *declares_class_or_enum = 1;
17393 /* Fall through. */
17394 case RID_TYPENAME:
17395 /* Look for an elaborated-type-specifier. */
17396 type_spec
17397 = (cp_parser_elaborated_type_specifier
17398 (parser,
17399 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17400 is_declaration));
17401 if (decl_specs)
17402 cp_parser_set_decl_spec_type (decl_specs,
17403 type_spec,
17404 token,
17405 /*type_definition_p=*/false);
17406 return type_spec;
17408 case RID_CONST:
17409 ds = ds_const;
17410 if (is_cv_qualifier)
17411 *is_cv_qualifier = true;
17412 break;
17414 case RID_VOLATILE:
17415 ds = ds_volatile;
17416 if (is_cv_qualifier)
17417 *is_cv_qualifier = true;
17418 break;
17420 case RID_RESTRICT:
17421 ds = ds_restrict;
17422 if (is_cv_qualifier)
17423 *is_cv_qualifier = true;
17424 break;
17426 case RID_COMPLEX:
17427 /* The `__complex__' keyword is a GNU extension. */
17428 ds = ds_complex;
17429 break;
17431 default:
17432 break;
17435 /* Handle simple keywords. */
17436 if (ds != ds_last)
17438 if (decl_specs)
17440 set_and_check_decl_spec_loc (decl_specs, ds, token);
17441 decl_specs->any_specifiers_p = true;
17443 return cp_lexer_consume_token (parser->lexer)->u.value;
17446 /* If we do not already have a type-specifier, assume we are looking
17447 at a simple-type-specifier. */
17448 type_spec = cp_parser_simple_type_specifier (parser,
17449 decl_specs,
17450 flags);
17452 /* If we didn't find a type-specifier, and a type-specifier was not
17453 optional in this context, issue an error message. */
17454 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17456 cp_parser_error (parser, "expected type specifier");
17457 return error_mark_node;
17460 return type_spec;
17463 /* Parse a simple-type-specifier.
17465 simple-type-specifier:
17466 :: [opt] nested-name-specifier [opt] type-name
17467 :: [opt] nested-name-specifier template template-id
17468 char
17469 wchar_t
17470 bool
17471 short
17473 long
17474 signed
17475 unsigned
17476 float
17477 double
17478 void
17480 C++11 Extension:
17482 simple-type-specifier:
17483 auto
17484 decltype ( expression )
17485 char16_t
17486 char32_t
17487 __underlying_type ( type-id )
17489 C++17 extension:
17491 nested-name-specifier(opt) template-name
17493 GNU Extension:
17495 simple-type-specifier:
17496 __int128
17497 __typeof__ unary-expression
17498 __typeof__ ( type-id )
17499 __typeof__ ( type-id ) { initializer-list , [opt] }
17501 Concepts Extension:
17503 simple-type-specifier:
17504 constrained-type-specifier
17506 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17507 appropriately updated. */
17509 static tree
17510 cp_parser_simple_type_specifier (cp_parser* parser,
17511 cp_decl_specifier_seq *decl_specs,
17512 cp_parser_flags flags)
17514 tree type = NULL_TREE;
17515 cp_token *token;
17516 int idx;
17518 /* Peek at the next token. */
17519 token = cp_lexer_peek_token (parser->lexer);
17521 /* If we're looking at a keyword, things are easy. */
17522 switch (token->keyword)
17524 case RID_CHAR:
17525 if (decl_specs)
17526 decl_specs->explicit_char_p = true;
17527 type = char_type_node;
17528 break;
17529 case RID_CHAR8:
17530 type = char8_type_node;
17531 break;
17532 case RID_CHAR16:
17533 type = char16_type_node;
17534 break;
17535 case RID_CHAR32:
17536 type = char32_type_node;
17537 break;
17538 case RID_WCHAR:
17539 type = wchar_type_node;
17540 break;
17541 case RID_BOOL:
17542 type = boolean_type_node;
17543 break;
17544 case RID_SHORT:
17545 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17546 type = short_integer_type_node;
17547 break;
17548 case RID_INT:
17549 if (decl_specs)
17550 decl_specs->explicit_int_p = true;
17551 type = integer_type_node;
17552 break;
17553 case RID_INT_N_0:
17554 case RID_INT_N_1:
17555 case RID_INT_N_2:
17556 case RID_INT_N_3:
17557 idx = token->keyword - RID_INT_N_0;
17558 if (! int_n_enabled_p [idx])
17559 break;
17560 if (decl_specs)
17562 decl_specs->explicit_intN_p = true;
17563 decl_specs->int_n_idx = idx;
17565 type = int_n_trees [idx].signed_type;
17566 break;
17567 case RID_LONG:
17568 if (decl_specs)
17569 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17570 type = long_integer_type_node;
17571 break;
17572 case RID_SIGNED:
17573 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17574 type = integer_type_node;
17575 break;
17576 case RID_UNSIGNED:
17577 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17578 type = unsigned_type_node;
17579 break;
17580 case RID_FLOAT:
17581 type = float_type_node;
17582 break;
17583 case RID_DOUBLE:
17584 type = double_type_node;
17585 break;
17586 case RID_VOID:
17587 type = void_type_node;
17588 break;
17590 case RID_AUTO:
17591 maybe_warn_cpp0x (CPP0X_AUTO);
17592 if (parser->auto_is_implicit_function_template_parm_p)
17594 /* The 'auto' might be the placeholder return type for a function decl
17595 with trailing return type. */
17596 bool have_trailing_return_fn_decl = false;
17598 cp_parser_parse_tentatively (parser);
17599 cp_lexer_consume_token (parser->lexer);
17600 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17601 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17602 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17603 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17605 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17607 cp_lexer_consume_token (parser->lexer);
17608 cp_parser_skip_to_closing_parenthesis (parser,
17609 /*recovering*/false,
17610 /*or_comma*/false,
17611 /*consume_paren*/true);
17612 continue;
17615 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17617 have_trailing_return_fn_decl = true;
17618 break;
17621 cp_lexer_consume_token (parser->lexer);
17623 cp_parser_abort_tentative_parse (parser);
17625 if (have_trailing_return_fn_decl)
17627 type = make_auto ();
17628 break;
17631 if (cxx_dialect >= cxx14)
17633 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17634 type = TREE_TYPE (type);
17636 else
17637 type = error_mark_node;
17639 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17641 if (cxx_dialect < cxx14)
17642 error_at (token->location,
17643 "use of %<auto%> in lambda parameter declaration "
17644 "only available with "
17645 "-std=c++14 or -std=gnu++14");
17647 else if (cxx_dialect < cxx14)
17648 error_at (token->location,
17649 "use of %<auto%> in parameter declaration "
17650 "only available with "
17651 "-std=c++14 or -std=gnu++14");
17652 else if (!flag_concepts)
17653 pedwarn (token->location, 0,
17654 "use of %<auto%> in parameter declaration "
17655 "only available with -fconcepts");
17657 else
17658 type = make_auto ();
17659 break;
17661 case RID_DECLTYPE:
17662 /* Since DR 743, decltype can either be a simple-type-specifier by
17663 itself or begin a nested-name-specifier. Parsing it will replace
17664 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17665 handling below decide what to do. */
17666 cp_parser_decltype (parser);
17667 cp_lexer_set_token_position (parser->lexer, token);
17668 break;
17670 case RID_TYPEOF:
17671 /* Consume the `typeof' token. */
17672 cp_lexer_consume_token (parser->lexer);
17673 /* Parse the operand to `typeof'. */
17674 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17675 /* If it is not already a TYPE, take its type. */
17676 if (!TYPE_P (type))
17677 type = finish_typeof (type);
17679 if (decl_specs)
17680 cp_parser_set_decl_spec_type (decl_specs, type,
17681 token,
17682 /*type_definition_p=*/false);
17684 return type;
17686 case RID_UNDERLYING_TYPE:
17687 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17688 if (decl_specs)
17689 cp_parser_set_decl_spec_type (decl_specs, type,
17690 token,
17691 /*type_definition_p=*/false);
17693 return type;
17695 case RID_BASES:
17696 case RID_DIRECT_BASES:
17697 type = cp_parser_trait_expr (parser, token->keyword);
17698 if (decl_specs)
17699 cp_parser_set_decl_spec_type (decl_specs, type,
17700 token,
17701 /*type_definition_p=*/false);
17702 return type;
17703 default:
17704 break;
17707 /* If token is an already-parsed decltype not followed by ::,
17708 it's a simple-type-specifier. */
17709 if (token->type == CPP_DECLTYPE
17710 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17712 type = saved_checks_value (token->u.tree_check_value);
17713 if (decl_specs)
17715 cp_parser_set_decl_spec_type (decl_specs, type,
17716 token,
17717 /*type_definition_p=*/false);
17718 /* Remember that we are handling a decltype in order to
17719 implement the resolution of DR 1510 when the argument
17720 isn't instantiation dependent. */
17721 decl_specs->decltype_p = true;
17723 cp_lexer_consume_token (parser->lexer);
17724 return type;
17727 /* If the type-specifier was for a built-in type, we're done. */
17728 if (type)
17730 /* Record the type. */
17731 if (decl_specs
17732 && (token->keyword != RID_SIGNED
17733 && token->keyword != RID_UNSIGNED
17734 && token->keyword != RID_SHORT
17735 && token->keyword != RID_LONG))
17736 cp_parser_set_decl_spec_type (decl_specs,
17737 type,
17738 token,
17739 /*type_definition_p=*/false);
17740 if (decl_specs)
17741 decl_specs->any_specifiers_p = true;
17743 /* Consume the token. */
17744 cp_lexer_consume_token (parser->lexer);
17746 if (type == error_mark_node)
17747 return error_mark_node;
17749 /* There is no valid C++ program where a non-template type is
17750 followed by a "<". That usually indicates that the user thought
17751 that the type was a template. */
17752 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17753 token->location);
17755 return TYPE_NAME (type);
17758 /* The type-specifier must be a user-defined type. */
17759 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17761 bool qualified_p;
17762 bool global_p;
17763 const bool typename_p = (cxx_dialect >= cxx2a
17764 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17766 /* Don't gobble tokens or issue error messages if this is an
17767 optional type-specifier. */
17768 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17769 cp_parser_parse_tentatively (parser);
17771 token = cp_lexer_peek_token (parser->lexer);
17773 /* Look for the optional `::' operator. */
17774 global_p
17775 = (cp_parser_global_scope_opt (parser,
17776 /*current_scope_valid_p=*/false)
17777 != NULL_TREE);
17778 /* Look for the nested-name specifier. */
17779 qualified_p
17780 = (cp_parser_nested_name_specifier_opt (parser,
17781 /*typename_keyword_p=*/false,
17782 /*check_dependency_p=*/true,
17783 /*type_p=*/false,
17784 /*is_declaration=*/false)
17785 != NULL_TREE);
17786 /* If we have seen a nested-name-specifier, and the next token
17787 is `template', then we are using the template-id production. */
17788 if (parser->scope
17789 && cp_parser_optional_template_keyword (parser))
17791 /* Look for the template-id. */
17792 type = cp_parser_template_id (parser,
17793 /*template_keyword_p=*/true,
17794 /*check_dependency_p=*/true,
17795 none_type,
17796 /*is_declaration=*/false);
17797 /* If the template-id did not name a type, we are out of
17798 luck. */
17799 if (TREE_CODE (type) != TYPE_DECL)
17801 /* ...unless we pretend we have seen 'typename'. */
17802 if (typename_p)
17803 type = cp_parser_make_typename_type (parser, type,
17804 token->location);
17805 else
17807 cp_parser_error (parser, "expected template-id for type");
17808 type = NULL_TREE;
17812 /* Otherwise, look for a type-name. */
17813 else
17814 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17816 /* Keep track of all name-lookups performed in class scopes. */
17817 if (type
17818 && !global_p
17819 && !qualified_p
17820 && TREE_CODE (type) == TYPE_DECL
17821 && identifier_p (DECL_NAME (type)))
17822 maybe_note_name_used_in_class (DECL_NAME (type), type);
17823 /* If it didn't work out, we don't have a TYPE. */
17824 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17825 && !cp_parser_parse_definitely (parser))
17826 type = NULL_TREE;
17827 if (!type && cxx_dialect >= cxx17)
17829 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17830 cp_parser_parse_tentatively (parser);
17832 cp_parser_global_scope_opt (parser,
17833 /*current_scope_valid_p=*/false);
17834 cp_parser_nested_name_specifier_opt (parser,
17835 /*typename_keyword_p=*/false,
17836 /*check_dependency_p=*/true,
17837 /*type_p=*/false,
17838 /*is_declaration=*/false);
17839 tree name = cp_parser_identifier (parser);
17840 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17841 && parser->scope != error_mark_node)
17843 tree tmpl = cp_parser_lookup_name (parser, name,
17844 none_type,
17845 /*is_template=*/false,
17846 /*is_namespace=*/false,
17847 /*check_dependency=*/true,
17848 /*ambiguous_decls=*/NULL,
17849 token->location);
17850 if (tmpl && tmpl != error_mark_node
17851 && (DECL_CLASS_TEMPLATE_P (tmpl)
17852 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17853 type = make_template_placeholder (tmpl);
17854 else
17856 type = error_mark_node;
17857 if (!cp_parser_simulate_error (parser))
17858 cp_parser_name_lookup_error (parser, name, tmpl,
17859 NLE_TYPE, token->location);
17862 else
17863 type = error_mark_node;
17865 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17866 && !cp_parser_parse_definitely (parser))
17867 type = NULL_TREE;
17869 if (type && decl_specs)
17870 cp_parser_set_decl_spec_type (decl_specs, type,
17871 token,
17872 /*type_definition_p=*/false);
17875 /* If we didn't get a type-name, issue an error message. */
17876 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17878 cp_parser_error (parser, "expected type-name");
17879 return error_mark_node;
17882 if (type && type != error_mark_node)
17884 /* See if TYPE is an Objective-C type, and if so, parse and
17885 accept any protocol references following it. Do this before
17886 the cp_parser_check_for_invalid_template_id() call, because
17887 Objective-C types can be followed by '<...>' which would
17888 enclose protocol names rather than template arguments, and so
17889 everything is fine. */
17890 if (c_dialect_objc () && !parser->scope
17891 && (objc_is_id (type) || objc_is_class_name (type)))
17893 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17894 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17896 /* Clobber the "unqualified" type previously entered into
17897 DECL_SPECS with the new, improved protocol-qualified version. */
17898 if (decl_specs)
17899 decl_specs->type = qual_type;
17901 return qual_type;
17904 /* There is no valid C++ program where a non-template type is
17905 followed by a "<". That usually indicates that the user
17906 thought that the type was a template. */
17907 cp_parser_check_for_invalid_template_id (parser, type,
17908 none_type,
17909 token->location);
17912 return type;
17915 /* Parse a type-name.
17917 type-name:
17918 class-name
17919 enum-name
17920 typedef-name
17921 simple-template-id [in c++0x]
17923 enum-name:
17924 identifier
17926 typedef-name:
17927 identifier
17929 Concepts:
17931 type-name:
17932 concept-name
17933 partial-concept-id
17935 concept-name:
17936 identifier
17938 Returns a TYPE_DECL for the type. */
17940 static tree
17941 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17943 tree type_decl;
17945 /* We can't know yet whether it is a class-name or not. */
17946 cp_parser_parse_tentatively (parser);
17947 /* Try a class-name. */
17948 type_decl = cp_parser_class_name (parser,
17949 typename_keyword_p,
17950 /*template_keyword_p=*/false,
17951 none_type,
17952 /*check_dependency_p=*/true,
17953 /*class_head_p=*/false,
17954 /*is_declaration=*/false);
17955 /* If it's not a class-name, keep looking. */
17956 if (!cp_parser_parse_definitely (parser))
17958 if (cxx_dialect < cxx11)
17959 /* It must be a typedef-name or an enum-name. */
17960 return cp_parser_nonclass_name (parser);
17962 cp_parser_parse_tentatively (parser);
17963 /* It is either a simple-template-id representing an
17964 instantiation of an alias template... */
17965 type_decl = cp_parser_template_id (parser,
17966 /*template_keyword_p=*/false,
17967 /*check_dependency_p=*/true,
17968 none_type,
17969 /*is_declaration=*/false);
17970 /* Note that this must be an instantiation of an alias template
17971 because [temp.names]/6 says:
17973 A template-id that names an alias template specialization
17974 is a type-name.
17976 Whereas [temp.names]/7 says:
17978 A simple-template-id that names a class template
17979 specialization is a class-name.
17981 With concepts, this could also be a partial-concept-id that
17982 declares a non-type template parameter. */
17983 if (type_decl != NULL_TREE
17984 && TREE_CODE (type_decl) == TYPE_DECL
17985 && TYPE_DECL_ALIAS_P (type_decl))
17986 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17987 else if (is_constrained_parameter (type_decl))
17988 /* Don't do anything. */ ;
17989 else
17990 cp_parser_simulate_error (parser);
17992 if (!cp_parser_parse_definitely (parser))
17993 /* ... Or a typedef-name or an enum-name. */
17994 return cp_parser_nonclass_name (parser);
17997 return type_decl;
18000 /* Check if DECL and ARGS can form a constrained-type-specifier.
18001 If ARGS is non-null, we try to form a concept check of the
18002 form DECL<?, ARGS> where ? is a wildcard that matches any
18003 kind of template argument. If ARGS is NULL, then we try to
18004 form a concept check of the form DECL<?>. */
18006 static tree
18007 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18008 tree decl, tree args)
18010 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18012 /* If we a constrained-type-specifier cannot be deduced. */
18013 if (parser->prevent_constrained_type_specifiers)
18014 return NULL_TREE;
18016 /* A constrained type specifier can only be found in an
18017 overload set or as a reference to a template declaration.
18019 FIXME: This might be masking a bug. It's possible that
18020 that the deduction below is causing template specializations
18021 to be formed with the wildcard as an argument. */
18022 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18023 return NULL_TREE;
18025 /* Try to build a call expression that evaluates the
18026 concept. This can fail if the overload set refers
18027 only to non-templates. */
18028 tree placeholder = build_nt (WILDCARD_DECL);
18029 tree check = build_concept_check (decl, placeholder, args);
18030 if (check == error_mark_node)
18031 return NULL_TREE;
18033 /* Deduce the checked constraint and the prototype parameter.
18035 FIXME: In certain cases, failure to deduce should be a
18036 diagnosable error. */
18037 tree conc;
18038 tree proto;
18039 if (!deduce_constrained_parameter (check, conc, proto))
18040 return NULL_TREE;
18042 /* In template parameter scope, this results in a constrained
18043 parameter. Return a descriptor of that parm. */
18044 if (processing_template_parmlist)
18045 return build_constrained_parameter (conc, proto, args);
18047 /* In a parameter-declaration-clause, constrained-type
18048 specifiers result in invented template parameters. */
18049 if (parser->auto_is_implicit_function_template_parm_p)
18051 tree x = build_constrained_parameter (conc, proto, args);
18052 return synthesize_implicit_template_parm (parser, x);
18054 else
18056 /* Otherwise, we're in a context where the constrained
18057 type name is deduced and the constraint applies
18058 after deduction. */
18059 return make_constrained_auto (conc, args);
18062 return NULL_TREE;
18065 /* If DECL refers to a concept, return a TYPE_DECL representing
18066 the result of using the constrained type specifier in the
18067 current context. DECL refers to a concept if
18069 - it is an overload set containing a function concept taking a single
18070 type argument, or
18072 - it is a variable concept taking a single type argument. */
18074 static tree
18075 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18077 if (flag_concepts
18078 && (TREE_CODE (decl) == OVERLOAD
18079 || BASELINK_P (decl)
18080 || variable_concept_p (decl)))
18081 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18082 else
18083 return NULL_TREE;
18086 /* Check if DECL and ARGS form a partial-concept-id. If so,
18087 assign ID to the resulting constrained placeholder.
18089 Returns true if the partial-concept-id designates a placeholder
18090 and false otherwise. Note that *id is set to NULL_TREE in
18091 this case. */
18093 static tree
18094 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18096 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18099 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18100 or a concept-name.
18102 enum-name:
18103 identifier
18105 typedef-name:
18106 identifier
18108 concept-name:
18109 identifier
18111 Returns a TYPE_DECL for the type. */
18113 static tree
18114 cp_parser_nonclass_name (cp_parser* parser)
18116 tree type_decl;
18117 tree identifier;
18119 cp_token *token = cp_lexer_peek_token (parser->lexer);
18120 identifier = cp_parser_identifier (parser);
18121 if (identifier == error_mark_node)
18122 return error_mark_node;
18124 /* Look up the type-name. */
18125 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18127 type_decl = strip_using_decl (type_decl);
18129 /* If we found an overload set, then it may refer to a concept-name. */
18130 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18131 type_decl = decl;
18133 if (TREE_CODE (type_decl) != TYPE_DECL
18134 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18136 /* See if this is an Objective-C type. */
18137 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18138 tree type = objc_get_protocol_qualified_type (identifier, protos);
18139 if (type)
18140 type_decl = TYPE_NAME (type);
18143 /* Issue an error if we did not find a type-name. */
18144 if (TREE_CODE (type_decl) != TYPE_DECL
18145 /* In Objective-C, we have the complication that class names are
18146 normally type names and start declarations (eg, the
18147 "NSObject" in "NSObject *object;"), but can be used in an
18148 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18149 is an expression. So, a classname followed by a dot is not a
18150 valid type-name. */
18151 || (objc_is_class_name (TREE_TYPE (type_decl))
18152 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18154 if (!cp_parser_simulate_error (parser))
18155 cp_parser_name_lookup_error (parser, identifier, type_decl,
18156 NLE_TYPE, token->location);
18157 return error_mark_node;
18159 /* Remember that the name was used in the definition of the
18160 current class so that we can check later to see if the
18161 meaning would have been different after the class was
18162 entirely defined. */
18163 else if (type_decl != error_mark_node
18164 && !parser->scope)
18165 maybe_note_name_used_in_class (identifier, type_decl);
18167 return type_decl;
18170 /* Parse an elaborated-type-specifier. Note that the grammar given
18171 here incorporates the resolution to DR68.
18173 elaborated-type-specifier:
18174 class-key :: [opt] nested-name-specifier [opt] identifier
18175 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18176 enum-key :: [opt] nested-name-specifier [opt] identifier
18177 typename :: [opt] nested-name-specifier identifier
18178 typename :: [opt] nested-name-specifier template [opt]
18179 template-id
18181 GNU extension:
18183 elaborated-type-specifier:
18184 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18185 class-key attributes :: [opt] nested-name-specifier [opt]
18186 template [opt] template-id
18187 enum attributes :: [opt] nested-name-specifier [opt] identifier
18189 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18190 declared `friend'. If IS_DECLARATION is TRUE, then this
18191 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18192 something is being declared.
18194 Returns the TYPE specified. */
18196 static tree
18197 cp_parser_elaborated_type_specifier (cp_parser* parser,
18198 bool is_friend,
18199 bool is_declaration)
18201 enum tag_types tag_type;
18202 tree identifier;
18203 tree type = NULL_TREE;
18204 tree attributes = NULL_TREE;
18205 tree globalscope;
18206 cp_token *token = NULL;
18208 /* See if we're looking at the `enum' keyword. */
18209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18211 /* Consume the `enum' token. */
18212 cp_lexer_consume_token (parser->lexer);
18213 /* Remember that it's an enumeration type. */
18214 tag_type = enum_type;
18215 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18216 enums) is used here. */
18217 cp_token *token = cp_lexer_peek_token (parser->lexer);
18218 if (cp_parser_is_keyword (token, RID_CLASS)
18219 || cp_parser_is_keyword (token, RID_STRUCT))
18221 gcc_rich_location richloc (token->location);
18222 richloc.add_range (input_location);
18223 richloc.add_fixit_remove ();
18224 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18225 "a scoped enum must not use the %qD keyword",
18226 token->u.value);
18227 /* Consume the `struct' or `class' and parse it anyway. */
18228 cp_lexer_consume_token (parser->lexer);
18230 /* Parse the attributes. */
18231 attributes = cp_parser_attributes_opt (parser);
18233 /* Or, it might be `typename'. */
18234 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18235 RID_TYPENAME))
18237 /* Consume the `typename' token. */
18238 cp_lexer_consume_token (parser->lexer);
18239 /* Remember that it's a `typename' type. */
18240 tag_type = typename_type;
18242 /* Otherwise it must be a class-key. */
18243 else
18245 tag_type = cp_parser_class_key (parser);
18246 if (tag_type == none_type)
18247 return error_mark_node;
18248 /* Parse the attributes. */
18249 attributes = cp_parser_attributes_opt (parser);
18252 /* Look for the `::' operator. */
18253 globalscope = cp_parser_global_scope_opt (parser,
18254 /*current_scope_valid_p=*/false);
18255 /* Look for the nested-name-specifier. */
18256 tree nested_name_specifier;
18257 if (tag_type == typename_type && !globalscope)
18259 nested_name_specifier
18260 = cp_parser_nested_name_specifier (parser,
18261 /*typename_keyword_p=*/true,
18262 /*check_dependency_p=*/true,
18263 /*type_p=*/true,
18264 is_declaration);
18265 if (!nested_name_specifier)
18266 return error_mark_node;
18268 else
18269 /* Even though `typename' is not present, the proposed resolution
18270 to Core Issue 180 says that in `class A<T>::B', `B' should be
18271 considered a type-name, even if `A<T>' is dependent. */
18272 nested_name_specifier
18273 = cp_parser_nested_name_specifier_opt (parser,
18274 /*typename_keyword_p=*/true,
18275 /*check_dependency_p=*/true,
18276 /*type_p=*/true,
18277 is_declaration);
18278 /* For everything but enumeration types, consider a template-id.
18279 For an enumeration type, consider only a plain identifier. */
18280 if (tag_type != enum_type)
18282 bool template_p = false;
18283 tree decl;
18285 /* Allow the `template' keyword. */
18286 template_p = cp_parser_optional_template_keyword (parser);
18287 /* If we didn't see `template', we don't know if there's a
18288 template-id or not. */
18289 if (!template_p)
18290 cp_parser_parse_tentatively (parser);
18291 /* The `template' keyword must follow a nested-name-specifier. */
18292 else if (!nested_name_specifier)
18294 cp_parser_error (parser, "%<template%> must follow a nested-"
18295 "name-specifier");
18296 return error_mark_node;
18299 /* Parse the template-id. */
18300 token = cp_lexer_peek_token (parser->lexer);
18301 decl = cp_parser_template_id (parser, template_p,
18302 /*check_dependency_p=*/true,
18303 tag_type,
18304 is_declaration);
18305 /* If we didn't find a template-id, look for an ordinary
18306 identifier. */
18307 if (!template_p && !cp_parser_parse_definitely (parser))
18309 /* We can get here when cp_parser_template_id, called by
18310 cp_parser_class_name with tag_type == none_type, succeeds
18311 and caches a BASELINK. Then, when called again here,
18312 instead of failing and returning an error_mark_node
18313 returns it (see template/typename17.C in C++11).
18314 ??? Could we diagnose this earlier? */
18315 else if (tag_type == typename_type && BASELINK_P (decl))
18317 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18318 type = error_mark_node;
18320 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18321 in effect, then we must assume that, upon instantiation, the
18322 template will correspond to a class. */
18323 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18324 && tag_type == typename_type)
18325 type = make_typename_type (parser->scope, decl,
18326 typename_type,
18327 /*complain=*/tf_error);
18328 /* If the `typename' keyword is in effect and DECL is not a type
18329 decl, then type is non existent. */
18330 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18332 else if (TREE_CODE (decl) == TYPE_DECL)
18334 type = check_elaborated_type_specifier (tag_type, decl,
18335 /*allow_template_p=*/true);
18337 /* If the next token is a semicolon, this must be a specialization,
18338 instantiation, or friend declaration. Check the scope while we
18339 still know whether or not we had a nested-name-specifier. */
18340 if (type != error_mark_node
18341 && !nested_name_specifier && !is_friend
18342 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18343 check_unqualified_spec_or_inst (type, token->location);
18345 else if (decl == error_mark_node)
18346 type = error_mark_node;
18349 if (!type)
18351 token = cp_lexer_peek_token (parser->lexer);
18352 identifier = cp_parser_identifier (parser);
18354 if (identifier == error_mark_node)
18356 parser->scope = NULL_TREE;
18357 return error_mark_node;
18360 /* For a `typename', we needn't call xref_tag. */
18361 if (tag_type == typename_type
18362 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18363 return cp_parser_make_typename_type (parser, identifier,
18364 token->location);
18366 /* Template parameter lists apply only if we are not within a
18367 function parameter list. */
18368 bool template_parm_lists_apply
18369 = parser->num_template_parameter_lists;
18370 if (template_parm_lists_apply)
18371 for (cp_binding_level *s = current_binding_level;
18372 s && s->kind != sk_template_parms;
18373 s = s->level_chain)
18374 if (s->kind == sk_function_parms)
18375 template_parm_lists_apply = false;
18377 /* Look up a qualified name in the usual way. */
18378 if (parser->scope)
18380 tree decl;
18381 tree ambiguous_decls;
18383 decl = cp_parser_lookup_name (parser, identifier,
18384 tag_type,
18385 /*is_template=*/false,
18386 /*is_namespace=*/false,
18387 /*check_dependency=*/true,
18388 &ambiguous_decls,
18389 token->location);
18391 /* If the lookup was ambiguous, an error will already have been
18392 issued. */
18393 if (ambiguous_decls)
18394 return error_mark_node;
18396 /* If we are parsing friend declaration, DECL may be a
18397 TEMPLATE_DECL tree node here. However, we need to check
18398 whether this TEMPLATE_DECL results in valid code. Consider
18399 the following example:
18401 namespace N {
18402 template <class T> class C {};
18404 class X {
18405 template <class T> friend class N::C; // #1, valid code
18407 template <class T> class Y {
18408 friend class N::C; // #2, invalid code
18411 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18412 name lookup of `N::C'. We see that friend declaration must
18413 be template for the code to be valid. Note that
18414 processing_template_decl does not work here since it is
18415 always 1 for the above two cases. */
18417 decl = (cp_parser_maybe_treat_template_as_class
18418 (decl, /*tag_name_p=*/is_friend
18419 && template_parm_lists_apply));
18421 if (TREE_CODE (decl) != TYPE_DECL)
18423 cp_parser_diagnose_invalid_type_name (parser,
18424 identifier,
18425 token->location);
18426 return error_mark_node;
18429 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18431 bool allow_template = (template_parm_lists_apply
18432 || DECL_SELF_REFERENCE_P (decl));
18433 type = check_elaborated_type_specifier (tag_type, decl,
18434 allow_template);
18436 if (type == error_mark_node)
18437 return error_mark_node;
18440 /* Forward declarations of nested types, such as
18442 class C1::C2;
18443 class C1::C2::C3;
18445 are invalid unless all components preceding the final '::'
18446 are complete. If all enclosing types are complete, these
18447 declarations become merely pointless.
18449 Invalid forward declarations of nested types are errors
18450 caught elsewhere in parsing. Those that are pointless arrive
18451 here. */
18453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18454 && !is_friend && !processing_explicit_instantiation)
18455 warning (0, "declaration %qD does not declare anything", decl);
18457 type = TREE_TYPE (decl);
18459 else
18461 /* An elaborated-type-specifier sometimes introduces a new type and
18462 sometimes names an existing type. Normally, the rule is that it
18463 introduces a new type only if there is not an existing type of
18464 the same name already in scope. For example, given:
18466 struct S {};
18467 void f() { struct S s; }
18469 the `struct S' in the body of `f' is the same `struct S' as in
18470 the global scope; the existing definition is used. However, if
18471 there were no global declaration, this would introduce a new
18472 local class named `S'.
18474 An exception to this rule applies to the following code:
18476 namespace N { struct S; }
18478 Here, the elaborated-type-specifier names a new type
18479 unconditionally; even if there is already an `S' in the
18480 containing scope this declaration names a new type.
18481 This exception only applies if the elaborated-type-specifier
18482 forms the complete declaration:
18484 [class.name]
18486 A declaration consisting solely of `class-key identifier ;' is
18487 either a redeclaration of the name in the current scope or a
18488 forward declaration of the identifier as a class name. It
18489 introduces the name into the current scope.
18491 We are in this situation precisely when the next token is a `;'.
18493 An exception to the exception is that a `friend' declaration does
18494 *not* name a new type; i.e., given:
18496 struct S { friend struct T; };
18498 `T' is not a new type in the scope of `S'.
18500 Also, `new struct S' or `sizeof (struct S)' never results in the
18501 definition of a new type; a new type can only be declared in a
18502 declaration context. */
18504 tag_scope ts;
18505 bool template_p;
18507 if (is_friend)
18508 /* Friends have special name lookup rules. */
18509 ts = ts_within_enclosing_non_class;
18510 else if (is_declaration
18511 && cp_lexer_next_token_is (parser->lexer,
18512 CPP_SEMICOLON))
18513 /* This is a `class-key identifier ;' */
18514 ts = ts_current;
18515 else
18516 ts = ts_global;
18518 template_p =
18519 (template_parm_lists_apply
18520 && (cp_parser_next_token_starts_class_definition_p (parser)
18521 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18522 /* An unqualified name was used to reference this type, so
18523 there were no qualifying templates. */
18524 if (template_parm_lists_apply
18525 && !cp_parser_check_template_parameters (parser,
18526 /*num_templates=*/0,
18527 /*template_id*/false,
18528 token->location,
18529 /*declarator=*/NULL))
18530 return error_mark_node;
18531 type = xref_tag (tag_type, identifier, ts, template_p);
18535 if (type == error_mark_node)
18536 return error_mark_node;
18538 /* Allow attributes on forward declarations of classes. */
18539 if (attributes)
18541 if (TREE_CODE (type) == TYPENAME_TYPE)
18542 warning (OPT_Wattributes,
18543 "attributes ignored on uninstantiated type");
18544 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18545 && ! processing_explicit_instantiation)
18546 warning (OPT_Wattributes,
18547 "attributes ignored on template instantiation");
18548 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18549 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18550 else
18551 warning (OPT_Wattributes,
18552 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18555 if (tag_type != enum_type)
18557 /* Indicate whether this class was declared as a `class' or as a
18558 `struct'. */
18559 if (CLASS_TYPE_P (type))
18560 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18561 cp_parser_check_class_key (tag_type, type);
18564 /* A "<" cannot follow an elaborated type specifier. If that
18565 happens, the user was probably trying to form a template-id. */
18566 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18567 token->location);
18569 return type;
18572 /* Parse an enum-specifier.
18574 enum-specifier:
18575 enum-head { enumerator-list [opt] }
18576 enum-head { enumerator-list , } [C++0x]
18578 enum-head:
18579 enum-key identifier [opt] enum-base [opt]
18580 enum-key nested-name-specifier identifier enum-base [opt]
18582 enum-key:
18583 enum
18584 enum class [C++0x]
18585 enum struct [C++0x]
18587 enum-base: [C++0x]
18588 : type-specifier-seq
18590 opaque-enum-specifier:
18591 enum-key identifier enum-base [opt] ;
18593 GNU Extensions:
18594 enum-key attributes[opt] identifier [opt] enum-base [opt]
18595 { enumerator-list [opt] }attributes[opt]
18596 enum-key attributes[opt] identifier [opt] enum-base [opt]
18597 { enumerator-list, }attributes[opt] [C++0x]
18599 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18600 if the token stream isn't an enum-specifier after all. */
18602 static tree
18603 cp_parser_enum_specifier (cp_parser* parser)
18605 tree identifier;
18606 tree type = NULL_TREE;
18607 tree prev_scope;
18608 tree nested_name_specifier = NULL_TREE;
18609 tree attributes;
18610 bool scoped_enum_p = false;
18611 bool has_underlying_type = false;
18612 bool nested_being_defined = false;
18613 bool new_value_list = false;
18614 bool is_new_type = false;
18615 bool is_unnamed = false;
18616 tree underlying_type = NULL_TREE;
18617 cp_token *type_start_token = NULL;
18618 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18620 parser->colon_corrects_to_scope_p = false;
18622 /* Parse tentatively so that we can back up if we don't find a
18623 enum-specifier. */
18624 cp_parser_parse_tentatively (parser);
18626 /* Caller guarantees that the current token is 'enum', an identifier
18627 possibly follows, and the token after that is an opening brace.
18628 If we don't have an identifier, fabricate an anonymous name for
18629 the enumeration being defined. */
18630 cp_lexer_consume_token (parser->lexer);
18632 /* Parse the "class" or "struct", which indicates a scoped
18633 enumeration type in C++0x. */
18634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18635 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18637 if (cxx_dialect < cxx11)
18638 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18640 /* Consume the `struct' or `class' token. */
18641 cp_lexer_consume_token (parser->lexer);
18643 scoped_enum_p = true;
18646 attributes = cp_parser_attributes_opt (parser);
18648 /* Clear the qualification. */
18649 parser->scope = NULL_TREE;
18650 parser->qualifying_scope = NULL_TREE;
18651 parser->object_scope = NULL_TREE;
18653 /* Figure out in what scope the declaration is being placed. */
18654 prev_scope = current_scope ();
18656 type_start_token = cp_lexer_peek_token (parser->lexer);
18658 push_deferring_access_checks (dk_no_check);
18659 nested_name_specifier
18660 = cp_parser_nested_name_specifier_opt (parser,
18661 /*typename_keyword_p=*/true,
18662 /*check_dependency_p=*/false,
18663 /*type_p=*/false,
18664 /*is_declaration=*/false);
18666 if (nested_name_specifier)
18668 tree name;
18670 identifier = cp_parser_identifier (parser);
18671 name = cp_parser_lookup_name (parser, identifier,
18672 enum_type,
18673 /*is_template=*/false,
18674 /*is_namespace=*/false,
18675 /*check_dependency=*/true,
18676 /*ambiguous_decls=*/NULL,
18677 input_location);
18678 if (name && name != error_mark_node)
18680 type = TREE_TYPE (name);
18681 if (TREE_CODE (type) == TYPENAME_TYPE)
18683 /* Are template enums allowed in ISO? */
18684 if (template_parm_scope_p ())
18685 pedwarn (type_start_token->location, OPT_Wpedantic,
18686 "%qD is an enumeration template", name);
18687 /* ignore a typename reference, for it will be solved by name
18688 in start_enum. */
18689 type = NULL_TREE;
18692 else if (nested_name_specifier == error_mark_node)
18693 /* We already issued an error. */;
18694 else
18696 error_at (type_start_token->location,
18697 "%qD does not name an enumeration in %qT",
18698 identifier, nested_name_specifier);
18699 nested_name_specifier = error_mark_node;
18702 else
18704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18705 identifier = cp_parser_identifier (parser);
18706 else
18708 identifier = make_anon_name ();
18709 is_unnamed = true;
18710 if (scoped_enum_p)
18711 error_at (type_start_token->location,
18712 "unnamed scoped enum is not allowed");
18715 pop_deferring_access_checks ();
18717 /* Check for the `:' that denotes a specified underlying type in C++0x.
18718 Note that a ':' could also indicate a bitfield width, however. */
18719 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18721 cp_decl_specifier_seq type_specifiers;
18723 /* Consume the `:'. */
18724 cp_lexer_consume_token (parser->lexer);
18726 /* Parse the type-specifier-seq. */
18727 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18728 /*is_declaration=*/false,
18729 /*is_trailing_return=*/false,
18730 &type_specifiers);
18732 /* At this point this is surely not elaborated type specifier. */
18733 if (!cp_parser_parse_definitely (parser))
18734 return NULL_TREE;
18736 if (cxx_dialect < cxx11)
18737 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18739 has_underlying_type = true;
18741 /* If that didn't work, stop. */
18742 if (type_specifiers.type != error_mark_node)
18744 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18745 /*initialized=*/0, NULL);
18746 if (underlying_type == error_mark_node
18747 || check_for_bare_parameter_packs (underlying_type))
18748 underlying_type = NULL_TREE;
18752 /* Look for the `{' but don't consume it yet. */
18753 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18755 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18757 cp_parser_error (parser, "expected %<{%>");
18758 if (has_underlying_type)
18760 type = NULL_TREE;
18761 goto out;
18764 /* An opaque-enum-specifier must have a ';' here. */
18765 if ((scoped_enum_p || underlying_type)
18766 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18768 cp_parser_error (parser, "expected %<;%> or %<{%>");
18769 if (has_underlying_type)
18771 type = NULL_TREE;
18772 goto out;
18777 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18778 return NULL_TREE;
18780 if (nested_name_specifier)
18782 if (CLASS_TYPE_P (nested_name_specifier))
18784 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18785 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18786 push_scope (nested_name_specifier);
18788 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18790 push_nested_namespace (nested_name_specifier);
18794 /* Issue an error message if type-definitions are forbidden here. */
18795 if (!cp_parser_check_type_definition (parser))
18796 type = error_mark_node;
18797 else
18798 /* Create the new type. We do this before consuming the opening
18799 brace so the enum will be recorded as being on the line of its
18800 tag (or the 'enum' keyword, if there is no tag). */
18801 type = start_enum (identifier, type, underlying_type,
18802 attributes, scoped_enum_p, &is_new_type);
18804 /* If the next token is not '{' it is an opaque-enum-specifier or an
18805 elaborated-type-specifier. */
18806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18808 timevar_push (TV_PARSE_ENUM);
18809 if (nested_name_specifier
18810 && nested_name_specifier != error_mark_node)
18812 /* The following catches invalid code such as:
18813 enum class S<int>::E { A, B, C }; */
18814 if (!processing_specialization
18815 && CLASS_TYPE_P (nested_name_specifier)
18816 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18817 error_at (type_start_token->location, "cannot add an enumerator "
18818 "list to a template instantiation");
18820 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18822 error_at (type_start_token->location,
18823 "%<%T::%E%> has not been declared",
18824 TYPE_CONTEXT (nested_name_specifier),
18825 nested_name_specifier);
18826 type = error_mark_node;
18828 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18829 && !CLASS_TYPE_P (nested_name_specifier))
18831 error_at (type_start_token->location, "nested name specifier "
18832 "%qT for enum declaration does not name a class "
18833 "or namespace", nested_name_specifier);
18834 type = error_mark_node;
18836 /* If that scope does not contain the scope in which the
18837 class was originally declared, the program is invalid. */
18838 else if (prev_scope && !is_ancestor (prev_scope,
18839 nested_name_specifier))
18841 if (at_namespace_scope_p ())
18842 error_at (type_start_token->location,
18843 "declaration of %qD in namespace %qD which does not "
18844 "enclose %qD",
18845 type, prev_scope, nested_name_specifier);
18846 else
18847 error_at (type_start_token->location,
18848 "declaration of %qD in %qD which does not "
18849 "enclose %qD",
18850 type, prev_scope, nested_name_specifier);
18851 type = error_mark_node;
18853 /* If that scope is the scope where the declaration is being placed
18854 the program is invalid. */
18855 else if (CLASS_TYPE_P (nested_name_specifier)
18856 && CLASS_TYPE_P (prev_scope)
18857 && same_type_p (nested_name_specifier, prev_scope))
18859 permerror (type_start_token->location,
18860 "extra qualification not allowed");
18861 nested_name_specifier = NULL_TREE;
18865 if (scoped_enum_p)
18866 begin_scope (sk_scoped_enum, type);
18868 /* Consume the opening brace. */
18869 matching_braces braces;
18870 braces.consume_open (parser);
18872 if (type == error_mark_node)
18873 ; /* Nothing to add */
18874 else if (OPAQUE_ENUM_P (type)
18875 || (cxx_dialect > cxx98 && processing_specialization))
18877 new_value_list = true;
18878 SET_OPAQUE_ENUM_P (type, false);
18879 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18881 else
18883 error_at (type_start_token->location,
18884 "multiple definition of %q#T", type);
18885 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18886 "previous definition here");
18887 type = error_mark_node;
18890 if (type == error_mark_node)
18891 cp_parser_skip_to_end_of_block_or_statement (parser);
18892 /* If the next token is not '}', then there are some enumerators. */
18893 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18895 if (is_unnamed && !scoped_enum_p)
18896 pedwarn (type_start_token->location, OPT_Wpedantic,
18897 "ISO C++ forbids empty unnamed enum");
18899 else
18900 cp_parser_enumerator_list (parser, type);
18902 /* Consume the final '}'. */
18903 braces.require_close (parser);
18905 if (scoped_enum_p)
18906 finish_scope ();
18907 timevar_pop (TV_PARSE_ENUM);
18909 else
18911 /* If a ';' follows, then it is an opaque-enum-specifier
18912 and additional restrictions apply. */
18913 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18915 if (is_unnamed)
18916 error_at (type_start_token->location,
18917 "opaque-enum-specifier without name");
18918 else if (nested_name_specifier)
18919 error_at (type_start_token->location,
18920 "opaque-enum-specifier must use a simple identifier");
18924 /* Look for trailing attributes to apply to this enumeration, and
18925 apply them if appropriate. */
18926 if (cp_parser_allow_gnu_extensions_p (parser))
18928 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18929 cplus_decl_attributes (&type,
18930 trailing_attr,
18931 (int) ATTR_FLAG_TYPE_IN_PLACE);
18934 /* Finish up the enumeration. */
18935 if (type != error_mark_node)
18937 if (new_value_list)
18938 finish_enum_value_list (type);
18939 if (is_new_type)
18940 finish_enum (type);
18943 if (nested_name_specifier)
18945 if (CLASS_TYPE_P (nested_name_specifier))
18947 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18948 pop_scope (nested_name_specifier);
18950 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18952 pop_nested_namespace (nested_name_specifier);
18955 out:
18956 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18957 return type;
18960 /* Parse an enumerator-list. The enumerators all have the indicated
18961 TYPE.
18963 enumerator-list:
18964 enumerator-definition
18965 enumerator-list , enumerator-definition */
18967 static void
18968 cp_parser_enumerator_list (cp_parser* parser, tree type)
18970 while (true)
18972 /* Parse an enumerator-definition. */
18973 cp_parser_enumerator_definition (parser, type);
18975 /* If the next token is not a ',', we've reached the end of
18976 the list. */
18977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18978 break;
18979 /* Otherwise, consume the `,' and keep going. */
18980 cp_lexer_consume_token (parser->lexer);
18981 /* If the next token is a `}', there is a trailing comma. */
18982 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18984 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18985 pedwarn (input_location, OPT_Wpedantic,
18986 "comma at end of enumerator list");
18987 break;
18992 /* Parse an enumerator-definition. The enumerator has the indicated
18993 TYPE.
18995 enumerator-definition:
18996 enumerator
18997 enumerator = constant-expression
18999 enumerator:
19000 identifier
19002 GNU Extensions:
19004 enumerator-definition:
19005 enumerator attributes [opt]
19006 enumerator attributes [opt] = constant-expression */
19008 static void
19009 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19011 tree identifier;
19012 tree value;
19013 location_t loc;
19015 /* Save the input location because we are interested in the location
19016 of the identifier and not the location of the explicit value. */
19017 loc = cp_lexer_peek_token (parser->lexer)->location;
19019 /* Look for the identifier. */
19020 identifier = cp_parser_identifier (parser);
19021 if (identifier == error_mark_node)
19022 return;
19024 /* Parse any specified attributes. */
19025 tree attrs = cp_parser_attributes_opt (parser);
19027 /* If the next token is an '=', then there is an explicit value. */
19028 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19030 /* Consume the `=' token. */
19031 cp_lexer_consume_token (parser->lexer);
19032 /* Parse the value. */
19033 value = cp_parser_constant_expression (parser);
19035 else
19036 value = NULL_TREE;
19038 /* If we are processing a template, make sure the initializer of the
19039 enumerator doesn't contain any bare template parameter pack. */
19040 if (check_for_bare_parameter_packs (value))
19041 value = error_mark_node;
19043 /* Create the enumerator. */
19044 build_enumerator (identifier, value, type, attrs, loc);
19047 /* Parse a namespace-name.
19049 namespace-name:
19050 original-namespace-name
19051 namespace-alias
19053 Returns the NAMESPACE_DECL for the namespace. */
19055 static tree
19056 cp_parser_namespace_name (cp_parser* parser)
19058 tree identifier;
19059 tree namespace_decl;
19061 cp_token *token = cp_lexer_peek_token (parser->lexer);
19063 /* Get the name of the namespace. */
19064 identifier = cp_parser_identifier (parser);
19065 if (identifier == error_mark_node)
19066 return error_mark_node;
19068 /* Look up the identifier in the currently active scope. Look only
19069 for namespaces, due to:
19071 [basic.lookup.udir]
19073 When looking up a namespace-name in a using-directive or alias
19074 definition, only namespace names are considered.
19076 And:
19078 [basic.lookup.qual]
19080 During the lookup of a name preceding the :: scope resolution
19081 operator, object, function, and enumerator names are ignored.
19083 (Note that cp_parser_qualifying_entity only calls this
19084 function if the token after the name is the scope resolution
19085 operator.) */
19086 namespace_decl = cp_parser_lookup_name (parser, identifier,
19087 none_type,
19088 /*is_template=*/false,
19089 /*is_namespace=*/true,
19090 /*check_dependency=*/true,
19091 /*ambiguous_decls=*/NULL,
19092 token->location);
19093 /* If it's not a namespace, issue an error. */
19094 if (namespace_decl == error_mark_node
19095 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19097 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19099 auto_diagnostic_group d;
19100 name_hint hint;
19101 if (namespace_decl == error_mark_node
19102 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19103 hint = suggest_alternative_in_explicit_scope (token->location,
19104 identifier,
19105 parser->scope);
19106 if (const char *suggestion = hint.suggestion ())
19108 gcc_rich_location richloc (token->location);
19109 richloc.add_fixit_replace (suggestion);
19110 error_at (&richloc,
19111 "%qD is not a namespace-name; did you mean %qs?",
19112 identifier, suggestion);
19114 else
19115 error_at (token->location, "%qD is not a namespace-name",
19116 identifier);
19118 else
19119 cp_parser_error (parser, "expected namespace-name");
19120 namespace_decl = error_mark_node;
19123 return namespace_decl;
19126 /* Parse a namespace-definition.
19128 namespace-definition:
19129 named-namespace-definition
19130 unnamed-namespace-definition
19132 named-namespace-definition:
19133 original-namespace-definition
19134 extension-namespace-definition
19136 original-namespace-definition:
19137 namespace identifier { namespace-body }
19139 extension-namespace-definition:
19140 namespace original-namespace-name { namespace-body }
19142 unnamed-namespace-definition:
19143 namespace { namespace-body } */
19145 static void
19146 cp_parser_namespace_definition (cp_parser* parser)
19148 tree identifier;
19149 int nested_definition_count = 0;
19151 cp_ensure_no_omp_declare_simd (parser);
19152 cp_ensure_no_oacc_routine (parser);
19154 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19155 const bool topmost_inline_p = is_inline;
19157 if (is_inline)
19159 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19160 cp_lexer_consume_token (parser->lexer);
19163 /* Look for the `namespace' keyword. */
19164 cp_token* token
19165 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19167 /* Parse any specified attributes before the identifier. */
19168 tree attribs = cp_parser_attributes_opt (parser);
19170 for (;;)
19172 identifier = NULL_TREE;
19174 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19175 RID_INLINE);
19176 if (nested_inline_p && nested_definition_count != 0)
19178 if (cxx_dialect < cxx2a)
19179 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19180 OPT_Wpedantic, "nested inline namespace definitions only "
19181 "available with -std=c++2a or -std=gnu++2a");
19182 cp_lexer_consume_token (parser->lexer);
19185 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19187 identifier = cp_parser_identifier (parser);
19189 if (cp_next_tokens_can_be_std_attribute_p (parser))
19190 pedwarn (input_location, OPT_Wpedantic,
19191 "standard attributes on namespaces must precede "
19192 "the namespace name");
19194 /* Parse any attributes specified after the identifier. */
19195 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19200 /* Don't forget that the innermost namespace might have been
19201 marked as inline. Use |= because we cannot overwrite
19202 IS_INLINE in case the outermost namespace is inline, but
19203 there are no nested inlines. */
19204 is_inline |= nested_inline_p;
19205 break;
19208 if (!nested_definition_count && cxx_dialect < cxx17)
19209 pedwarn (input_location, OPT_Wpedantic,
19210 "nested namespace definitions only available with "
19211 "-std=c++17 or -std=gnu++17");
19213 /* Nested namespace names can create new namespaces (unlike
19214 other qualified-ids). */
19215 if (int count = (identifier
19216 ? push_namespace (identifier, nested_inline_p)
19217 : 0))
19218 nested_definition_count += count;
19219 else
19220 cp_parser_error (parser, "nested namespace name required");
19221 cp_lexer_consume_token (parser->lexer);
19224 if (nested_definition_count && !identifier)
19225 cp_parser_error (parser, "namespace name required");
19227 if (nested_definition_count && attribs)
19228 error_at (token->location,
19229 "a nested namespace definition cannot have attributes");
19230 if (nested_definition_count && topmost_inline_p)
19231 error_at (token->location,
19232 "a nested namespace definition cannot be inline");
19234 /* Start the namespace. */
19235 nested_definition_count += push_namespace (identifier, is_inline);
19237 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19239 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19241 /* Look for the `{' to validate starting the namespace. */
19242 matching_braces braces;
19243 if (braces.require_open (parser))
19245 /* Parse the body of the namespace. */
19246 cp_parser_namespace_body (parser);
19248 /* Look for the final `}'. */
19249 braces.require_close (parser);
19252 if (has_visibility)
19253 pop_visibility (1);
19255 /* Pop the nested namespace definitions. */
19256 while (nested_definition_count--)
19257 pop_namespace ();
19260 /* Parse a namespace-body.
19262 namespace-body:
19263 declaration-seq [opt] */
19265 static void
19266 cp_parser_namespace_body (cp_parser* parser)
19268 cp_parser_declaration_seq_opt (parser);
19271 /* Parse a namespace-alias-definition.
19273 namespace-alias-definition:
19274 namespace identifier = qualified-namespace-specifier ; */
19276 static void
19277 cp_parser_namespace_alias_definition (cp_parser* parser)
19279 tree identifier;
19280 tree namespace_specifier;
19282 cp_token *token = cp_lexer_peek_token (parser->lexer);
19284 /* Look for the `namespace' keyword. */
19285 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19286 /* Look for the identifier. */
19287 identifier = cp_parser_identifier (parser);
19288 if (identifier == error_mark_node)
19289 return;
19290 /* Look for the `=' token. */
19291 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19292 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19294 error_at (token->location, "%<namespace%> definition is not allowed here");
19295 /* Skip the definition. */
19296 cp_lexer_consume_token (parser->lexer);
19297 if (cp_parser_skip_to_closing_brace (parser))
19298 cp_lexer_consume_token (parser->lexer);
19299 return;
19301 cp_parser_require (parser, CPP_EQ, RT_EQ);
19302 /* Look for the qualified-namespace-specifier. */
19303 namespace_specifier
19304 = cp_parser_qualified_namespace_specifier (parser);
19305 /* Look for the `;' token. */
19306 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19308 /* Register the alias in the symbol table. */
19309 do_namespace_alias (identifier, namespace_specifier);
19312 /* Parse a qualified-namespace-specifier.
19314 qualified-namespace-specifier:
19315 :: [opt] nested-name-specifier [opt] namespace-name
19317 Returns a NAMESPACE_DECL corresponding to the specified
19318 namespace. */
19320 static tree
19321 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19323 /* Look for the optional `::'. */
19324 cp_parser_global_scope_opt (parser,
19325 /*current_scope_valid_p=*/false);
19327 /* Look for the optional nested-name-specifier. */
19328 cp_parser_nested_name_specifier_opt (parser,
19329 /*typename_keyword_p=*/false,
19330 /*check_dependency_p=*/true,
19331 /*type_p=*/false,
19332 /*is_declaration=*/true);
19334 return cp_parser_namespace_name (parser);
19337 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19338 access declaration.
19340 using-declaration:
19341 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19342 using :: unqualified-id ;
19344 access-declaration:
19345 qualified-id ;
19349 static bool
19350 cp_parser_using_declaration (cp_parser* parser,
19351 bool access_declaration_p)
19353 cp_token *token;
19354 bool typename_p = false;
19355 bool global_scope_p;
19356 tree decl;
19357 tree identifier;
19358 tree qscope;
19359 int oldcount = errorcount;
19360 cp_token *diag_token = NULL;
19362 if (access_declaration_p)
19364 diag_token = cp_lexer_peek_token (parser->lexer);
19365 cp_parser_parse_tentatively (parser);
19367 else
19369 /* Look for the `using' keyword. */
19370 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19372 again:
19373 /* Peek at the next token. */
19374 token = cp_lexer_peek_token (parser->lexer);
19375 /* See if it's `typename'. */
19376 if (token->keyword == RID_TYPENAME)
19378 /* Remember that we've seen it. */
19379 typename_p = true;
19380 /* Consume the `typename' token. */
19381 cp_lexer_consume_token (parser->lexer);
19385 /* Look for the optional global scope qualification. */
19386 global_scope_p
19387 = (cp_parser_global_scope_opt (parser,
19388 /*current_scope_valid_p=*/false)
19389 != NULL_TREE);
19391 /* If we saw `typename', or didn't see `::', then there must be a
19392 nested-name-specifier present. */
19393 if (typename_p || !global_scope_p)
19395 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19396 /*check_dependency_p=*/true,
19397 /*type_p=*/false,
19398 /*is_declaration=*/true);
19399 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19401 cp_parser_skip_to_end_of_block_or_statement (parser);
19402 return false;
19405 /* Otherwise, we could be in either of the two productions. In that
19406 case, treat the nested-name-specifier as optional. */
19407 else
19408 qscope = cp_parser_nested_name_specifier_opt (parser,
19409 /*typename_keyword_p=*/false,
19410 /*check_dependency_p=*/true,
19411 /*type_p=*/false,
19412 /*is_declaration=*/true);
19413 if (!qscope)
19414 qscope = global_namespace;
19415 else if (UNSCOPED_ENUM_P (qscope)
19416 && !TYPE_FUNCTION_SCOPE_P (qscope))
19417 qscope = CP_TYPE_CONTEXT (qscope);
19419 if (access_declaration_p && cp_parser_error_occurred (parser))
19420 /* Something has already gone wrong; there's no need to parse
19421 further. Since an error has occurred, the return value of
19422 cp_parser_parse_definitely will be false, as required. */
19423 return cp_parser_parse_definitely (parser);
19425 token = cp_lexer_peek_token (parser->lexer);
19426 /* Parse the unqualified-id. */
19427 identifier = cp_parser_unqualified_id (parser,
19428 /*template_keyword_p=*/false,
19429 /*check_dependency_p=*/true,
19430 /*declarator_p=*/true,
19431 /*optional_p=*/false);
19433 if (access_declaration_p)
19435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19436 cp_parser_simulate_error (parser);
19437 if (!cp_parser_parse_definitely (parser))
19438 return false;
19440 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19442 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19443 if (cxx_dialect < cxx17
19444 && !in_system_header_at (ell->location))
19445 pedwarn (ell->location, 0,
19446 "pack expansion in using-declaration only available "
19447 "with -std=c++17 or -std=gnu++17");
19448 qscope = make_pack_expansion (qscope);
19451 /* The function we call to handle a using-declaration is different
19452 depending on what scope we are in. */
19453 if (qscope == error_mark_node || identifier == error_mark_node)
19455 else if (!identifier_p (identifier)
19456 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19457 /* [namespace.udecl]
19459 A using declaration shall not name a template-id. */
19460 error_at (token->location,
19461 "a template-id may not appear in a using-declaration");
19462 else
19464 if (at_class_scope_p ())
19466 /* Create the USING_DECL. */
19467 decl = do_class_using_decl (qscope, identifier);
19469 if (decl && typename_p)
19470 USING_DECL_TYPENAME_P (decl) = 1;
19472 if (check_for_bare_parameter_packs (decl))
19474 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19475 return false;
19477 else
19478 /* Add it to the list of members in this class. */
19479 finish_member_declaration (decl);
19481 else
19483 decl = cp_parser_lookup_name_simple (parser,
19484 identifier,
19485 token->location);
19486 if (decl == error_mark_node)
19487 cp_parser_name_lookup_error (parser, identifier,
19488 decl, NLE_NULL,
19489 token->location);
19490 else if (check_for_bare_parameter_packs (decl))
19492 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19493 return false;
19495 else if (!at_namespace_scope_p ())
19496 finish_local_using_decl (decl, qscope, identifier);
19497 else
19498 finish_namespace_using_decl (decl, qscope, identifier);
19502 if (!access_declaration_p
19503 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19505 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19506 if (cxx_dialect < cxx17)
19507 pedwarn (comma->location, 0,
19508 "comma-separated list in using-declaration only available "
19509 "with -std=c++17 or -std=gnu++17");
19510 goto again;
19513 /* Look for the final `;'. */
19514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19516 if (access_declaration_p && errorcount == oldcount)
19517 warning_at (diag_token->location, OPT_Wdeprecated,
19518 "access declarations are deprecated "
19519 "in favour of using-declarations; "
19520 "suggestion: add the %<using%> keyword");
19522 return true;
19525 /* Parse an alias-declaration.
19527 alias-declaration:
19528 using identifier attribute-specifier-seq [opt] = type-id */
19530 static tree
19531 cp_parser_alias_declaration (cp_parser* parser)
19533 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19534 location_t id_location, type_location;
19535 cp_declarator *declarator;
19536 cp_decl_specifier_seq decl_specs;
19537 bool member_p;
19538 const char *saved_message = NULL;
19540 /* Look for the `using' keyword. */
19541 cp_token *using_token
19542 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19543 if (using_token == NULL)
19544 return error_mark_node;
19546 id_location = cp_lexer_peek_token (parser->lexer)->location;
19547 id = cp_parser_identifier (parser);
19548 if (id == error_mark_node)
19549 return error_mark_node;
19551 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19552 attributes = cp_parser_attributes_opt (parser);
19553 if (attributes == error_mark_node)
19554 return error_mark_node;
19556 cp_parser_require (parser, CPP_EQ, RT_EQ);
19558 if (cp_parser_error_occurred (parser))
19559 return error_mark_node;
19561 cp_parser_commit_to_tentative_parse (parser);
19563 /* Now we are going to parse the type-id of the declaration. */
19566 [dcl.type]/3 says:
19568 "A type-specifier-seq shall not define a class or enumeration
19569 unless it appears in the type-id of an alias-declaration (7.1.3) that
19570 is not the declaration of a template-declaration."
19572 In other words, if we currently are in an alias template, the
19573 type-id should not define a type.
19575 So let's set parser->type_definition_forbidden_message in that
19576 case; cp_parser_check_type_definition (called by
19577 cp_parser_class_specifier) will then emit an error if a type is
19578 defined in the type-id. */
19579 if (parser->num_template_parameter_lists)
19581 saved_message = parser->type_definition_forbidden_message;
19582 parser->type_definition_forbidden_message =
19583 G_("types may not be defined in alias template declarations");
19586 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19587 &type_location);
19589 /* Restore the error message if need be. */
19590 if (parser->num_template_parameter_lists)
19591 parser->type_definition_forbidden_message = saved_message;
19593 if (type == error_mark_node
19594 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19596 cp_parser_skip_to_end_of_block_or_statement (parser);
19597 return error_mark_node;
19600 /* A typedef-name can also be introduced by an alias-declaration. The
19601 identifier following the using keyword becomes a typedef-name. It has
19602 the same semantics as if it were introduced by the typedef
19603 specifier. In particular, it does not define a new type and it shall
19604 not appear in the type-id. */
19606 clear_decl_specs (&decl_specs);
19607 decl_specs.type = type;
19608 if (attributes != NULL_TREE)
19610 decl_specs.attributes = attributes;
19611 set_and_check_decl_spec_loc (&decl_specs,
19612 ds_attribute,
19613 attrs_token);
19615 set_and_check_decl_spec_loc (&decl_specs,
19616 ds_typedef,
19617 using_token);
19618 set_and_check_decl_spec_loc (&decl_specs,
19619 ds_alias,
19620 using_token);
19621 decl_specs.locations[ds_type_spec] = type_location;
19623 if (parser->num_template_parameter_lists
19624 && !cp_parser_check_template_parameters (parser,
19625 /*num_templates=*/0,
19626 /*template_id*/false,
19627 id_location,
19628 /*declarator=*/NULL))
19629 return error_mark_node;
19631 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19633 member_p = at_class_scope_p ();
19634 if (member_p)
19635 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19636 NULL_TREE, attributes);
19637 else
19638 decl = start_decl (declarator, &decl_specs, 0,
19639 attributes, NULL_TREE, &pushed_scope);
19640 if (decl == error_mark_node)
19641 return decl;
19643 // Attach constraints to the alias declaration.
19644 if (flag_concepts && current_template_parms)
19646 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19647 tree constr = build_constraints (reqs, NULL_TREE);
19648 set_constraints (decl, constr);
19651 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19653 if (pushed_scope)
19654 pop_scope (pushed_scope);
19656 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19657 added into the symbol table; otherwise, return the TYPE_DECL. */
19658 if (DECL_LANG_SPECIFIC (decl)
19659 && DECL_TEMPLATE_INFO (decl)
19660 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19662 decl = DECL_TI_TEMPLATE (decl);
19663 if (member_p)
19664 check_member_template (decl);
19667 return decl;
19670 /* Parse a using-directive.
19672 using-directive:
19673 using namespace :: [opt] nested-name-specifier [opt]
19674 namespace-name ; */
19676 static void
19677 cp_parser_using_directive (cp_parser* parser)
19679 tree namespace_decl;
19680 tree attribs;
19682 /* Look for the `using' keyword. */
19683 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19684 /* And the `namespace' keyword. */
19685 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19686 /* Look for the optional `::' operator. */
19687 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19688 /* And the optional nested-name-specifier. */
19689 cp_parser_nested_name_specifier_opt (parser,
19690 /*typename_keyword_p=*/false,
19691 /*check_dependency_p=*/true,
19692 /*type_p=*/false,
19693 /*is_declaration=*/true);
19694 /* Get the namespace being used. */
19695 namespace_decl = cp_parser_namespace_name (parser);
19696 /* And any specified attributes. */
19697 attribs = cp_parser_attributes_opt (parser);
19699 /* Update the symbol table. */
19700 if (namespace_bindings_p ())
19701 finish_namespace_using_directive (namespace_decl, attribs);
19702 else
19703 finish_local_using_directive (namespace_decl, attribs);
19705 /* Look for the final `;'. */
19706 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19709 /* Parse an asm-definition.
19711 asm-qualifier:
19712 volatile
19713 inline
19714 goto
19716 asm-qualifier-list:
19717 asm-qualifier
19718 asm-qualifier-list asm-qualifier
19720 asm-definition:
19721 asm ( string-literal ) ;
19723 GNU Extension:
19725 asm-definition:
19726 asm asm-qualifier-list [opt] ( string-literal ) ;
19727 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19728 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19729 : asm-operand-list [opt] ) ;
19730 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19731 : asm-operand-list [opt]
19732 : asm-clobber-list [opt] ) ;
19733 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19734 : asm-clobber-list [opt]
19735 : asm-goto-list ) ;
19737 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19738 contains goto, and is the only allowed form in that case. No duplicates are
19739 allowed in an asm-qualifier-list. */
19741 static void
19742 cp_parser_asm_definition (cp_parser* parser)
19744 tree string;
19745 tree outputs = NULL_TREE;
19746 tree inputs = NULL_TREE;
19747 tree clobbers = NULL_TREE;
19748 tree labels = NULL_TREE;
19749 tree asm_stmt;
19750 bool extended_p = false;
19751 bool invalid_inputs_p = false;
19752 bool invalid_outputs_p = false;
19753 required_token missing = RT_NONE;
19755 /* Look for the `asm' keyword. */
19756 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19758 if (parser->in_function_body
19759 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19761 error ("%<asm%> in %<constexpr%> function");
19762 cp_function_chain->invalid_constexpr = true;
19765 /* Handle the asm-qualifier-list. */
19766 location_t volatile_loc = UNKNOWN_LOCATION;
19767 location_t inline_loc = UNKNOWN_LOCATION;
19768 location_t goto_loc = UNKNOWN_LOCATION;
19770 if (cp_parser_allow_gnu_extensions_p (parser) && parser->in_function_body)
19771 for (;;)
19773 cp_token *token = cp_lexer_peek_token (parser->lexer);
19774 location_t loc = token->location;
19775 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19777 case RID_VOLATILE:
19778 if (volatile_loc)
19780 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19781 inform (volatile_loc, "first seen here");
19783 else
19784 volatile_loc = loc;
19785 cp_lexer_consume_token (parser->lexer);
19786 continue;
19788 case RID_INLINE:
19789 if (inline_loc)
19791 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19792 inform (inline_loc, "first seen here");
19794 else
19795 inline_loc = loc;
19796 cp_lexer_consume_token (parser->lexer);
19797 continue;
19799 case RID_GOTO:
19800 if (goto_loc)
19802 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19803 inform (goto_loc, "first seen here");
19805 else
19806 goto_loc = loc;
19807 cp_lexer_consume_token (parser->lexer);
19808 continue;
19810 case RID_CONST:
19811 case RID_RESTRICT:
19812 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19813 cp_lexer_consume_token (parser->lexer);
19814 continue;
19816 default:
19817 break;
19819 break;
19822 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19823 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19824 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19826 /* Look for the opening `('. */
19827 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19828 return;
19829 /* Look for the string. */
19830 string = cp_parser_string_literal (parser, false, false);
19831 if (string == error_mark_node)
19833 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19834 /*consume_paren=*/true);
19835 return;
19838 /* If we're allowing GNU extensions, check for the extended assembly
19839 syntax. Unfortunately, the `:' tokens need not be separated by
19840 a space in C, and so, for compatibility, we tolerate that here
19841 too. Doing that means that we have to treat the `::' operator as
19842 two `:' tokens. */
19843 if (cp_parser_allow_gnu_extensions_p (parser)
19844 && parser->in_function_body
19845 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19846 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19848 bool inputs_p = false;
19849 bool clobbers_p = false;
19850 bool labels_p = false;
19852 /* The extended syntax was used. */
19853 extended_p = true;
19855 /* Look for outputs. */
19856 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19858 /* Consume the `:'. */
19859 cp_lexer_consume_token (parser->lexer);
19860 /* Parse the output-operands. */
19861 if (cp_lexer_next_token_is_not (parser->lexer,
19862 CPP_COLON)
19863 && cp_lexer_next_token_is_not (parser->lexer,
19864 CPP_SCOPE)
19865 && cp_lexer_next_token_is_not (parser->lexer,
19866 CPP_CLOSE_PAREN)
19867 && !goto_p)
19869 outputs = cp_parser_asm_operand_list (parser);
19870 if (outputs == error_mark_node)
19871 invalid_outputs_p = true;
19874 /* If the next token is `::', there are no outputs, and the
19875 next token is the beginning of the inputs. */
19876 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19877 /* The inputs are coming next. */
19878 inputs_p = true;
19880 /* Look for inputs. */
19881 if (inputs_p
19882 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19884 /* Consume the `:' or `::'. */
19885 cp_lexer_consume_token (parser->lexer);
19886 /* Parse the output-operands. */
19887 if (cp_lexer_next_token_is_not (parser->lexer,
19888 CPP_COLON)
19889 && cp_lexer_next_token_is_not (parser->lexer,
19890 CPP_SCOPE)
19891 && cp_lexer_next_token_is_not (parser->lexer,
19892 CPP_CLOSE_PAREN))
19894 inputs = cp_parser_asm_operand_list (parser);
19895 if (inputs == error_mark_node)
19896 invalid_inputs_p = true;
19899 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19900 /* The clobbers are coming next. */
19901 clobbers_p = true;
19903 /* Look for clobbers. */
19904 if (clobbers_p
19905 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19907 clobbers_p = true;
19908 /* Consume the `:' or `::'. */
19909 cp_lexer_consume_token (parser->lexer);
19910 /* Parse the clobbers. */
19911 if (cp_lexer_next_token_is_not (parser->lexer,
19912 CPP_COLON)
19913 && cp_lexer_next_token_is_not (parser->lexer,
19914 CPP_CLOSE_PAREN))
19915 clobbers = cp_parser_asm_clobber_list (parser);
19917 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19918 /* The labels are coming next. */
19919 labels_p = true;
19921 /* Look for labels. */
19922 if (labels_p
19923 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19925 labels_p = true;
19926 /* Consume the `:' or `::'. */
19927 cp_lexer_consume_token (parser->lexer);
19928 /* Parse the labels. */
19929 labels = cp_parser_asm_label_list (parser);
19932 if (goto_p && !labels_p)
19933 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19935 else if (goto_p)
19936 missing = RT_COLON_SCOPE;
19938 /* Look for the closing `)'. */
19939 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19940 missing ? missing : RT_CLOSE_PAREN))
19941 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19942 /*consume_paren=*/true);
19943 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19945 if (!invalid_inputs_p && !invalid_outputs_p)
19947 /* Create the ASM_EXPR. */
19948 if (parser->in_function_body)
19950 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19951 inputs, clobbers, labels, inline_p);
19952 /* If the extended syntax was not used, mark the ASM_EXPR. */
19953 if (!extended_p)
19955 tree temp = asm_stmt;
19956 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19957 temp = TREE_OPERAND (temp, 0);
19959 ASM_INPUT_P (temp) = 1;
19962 else
19963 symtab->finalize_toplevel_asm (string);
19967 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19968 type that comes from the decl-specifier-seq. */
19970 static tree
19971 strip_declarator_types (tree type, cp_declarator *declarator)
19973 for (cp_declarator *d = declarator; d;)
19974 switch (d->kind)
19976 case cdk_id:
19977 case cdk_decomp:
19978 case cdk_error:
19979 d = NULL;
19980 break;
19982 default:
19983 if (TYPE_PTRMEMFUNC_P (type))
19984 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19985 type = TREE_TYPE (type);
19986 d = d->declarator;
19987 break;
19990 return type;
19993 /* Declarators [gram.dcl.decl] */
19995 /* Parse an init-declarator.
19997 init-declarator:
19998 declarator initializer [opt]
20000 GNU Extension:
20002 init-declarator:
20003 declarator asm-specification [opt] attributes [opt] initializer [opt]
20005 function-definition:
20006 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20007 function-body
20008 decl-specifier-seq [opt] declarator function-try-block
20010 GNU Extension:
20012 function-definition:
20013 __extension__ function-definition
20015 TM Extension:
20017 function-definition:
20018 decl-specifier-seq [opt] declarator function-transaction-block
20020 The parser flags FLAGS is used to control type-specifier parsing.
20022 The DECL_SPECIFIERS apply to this declarator. Returns a
20023 representation of the entity declared. If MEMBER_P is TRUE, then
20024 this declarator appears in a class scope. The new DECL created by
20025 this declarator is returned.
20027 The CHECKS are access checks that should be performed once we know
20028 what entity is being declared (and, therefore, what classes have
20029 befriended it).
20031 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20032 for a function-definition here as well. If the declarator is a
20033 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20034 be TRUE upon return. By that point, the function-definition will
20035 have been completely parsed.
20037 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20038 is FALSE.
20040 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20041 parsed declaration if it is an uninitialized single declarator not followed
20042 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20043 if present, will not be consumed. If returned, this declarator will be
20044 created with SD_INITIALIZED but will not call cp_finish_decl.
20046 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20047 and there is an initializer, the pointed location_t is set to the
20048 location of the '=' or `(', or '{' in C++11 token introducing the
20049 initializer. */
20051 static tree
20052 cp_parser_init_declarator (cp_parser* parser,
20053 cp_parser_flags flags,
20054 cp_decl_specifier_seq *decl_specifiers,
20055 vec<deferred_access_check, va_gc> *checks,
20056 bool function_definition_allowed_p,
20057 bool member_p,
20058 int declares_class_or_enum,
20059 bool* function_definition_p,
20060 tree* maybe_range_for_decl,
20061 location_t* init_loc,
20062 tree* auto_result)
20064 cp_token *token = NULL, *asm_spec_start_token = NULL,
20065 *attributes_start_token = NULL;
20066 cp_declarator *declarator;
20067 tree prefix_attributes;
20068 tree attributes = NULL;
20069 tree asm_specification;
20070 tree initializer;
20071 tree decl = NULL_TREE;
20072 tree scope;
20073 int is_initialized;
20074 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20075 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20076 "(...)". */
20077 enum cpp_ttype initialization_kind;
20078 bool is_direct_init = false;
20079 bool is_non_constant_init;
20080 int ctor_dtor_or_conv_p;
20081 bool friend_p = cp_parser_friend_p (decl_specifiers);
20082 tree pushed_scope = NULL_TREE;
20083 bool range_for_decl_p = false;
20084 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20085 location_t tmp_init_loc = UNKNOWN_LOCATION;
20087 /* Gather the attributes that were provided with the
20088 decl-specifiers. */
20089 prefix_attributes = decl_specifiers->attributes;
20091 /* Assume that this is not the declarator for a function
20092 definition. */
20093 if (function_definition_p)
20094 *function_definition_p = false;
20096 /* Default arguments are only permitted for function parameters. */
20097 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20098 parser->default_arg_ok_p = false;
20100 /* Defer access checks while parsing the declarator; we cannot know
20101 what names are accessible until we know what is being
20102 declared. */
20103 resume_deferring_access_checks ();
20105 token = cp_lexer_peek_token (parser->lexer);
20107 /* Parse the declarator. */
20108 declarator
20109 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20110 flags, &ctor_dtor_or_conv_p,
20111 /*parenthesized_p=*/NULL,
20112 member_p, friend_p, /*static_p=*/false);
20113 /* Gather up the deferred checks. */
20114 stop_deferring_access_checks ();
20116 parser->default_arg_ok_p = saved_default_arg_ok_p;
20118 /* If the DECLARATOR was erroneous, there's no need to go
20119 further. */
20120 if (declarator == cp_error_declarator)
20121 return error_mark_node;
20123 /* Check that the number of template-parameter-lists is OK. */
20124 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20125 token->location))
20126 return error_mark_node;
20128 if (declares_class_or_enum & 2)
20129 cp_parser_check_for_definition_in_return_type (declarator,
20130 decl_specifiers->type,
20131 decl_specifiers->locations[ds_type_spec]);
20133 /* Figure out what scope the entity declared by the DECLARATOR is
20134 located in. `grokdeclarator' sometimes changes the scope, so
20135 we compute it now. */
20136 scope = get_scope_of_declarator (declarator);
20138 /* Perform any lookups in the declared type which were thought to be
20139 dependent, but are not in the scope of the declarator. */
20140 decl_specifiers->type
20141 = maybe_update_decl_type (decl_specifiers->type, scope);
20143 /* If we're allowing GNU extensions, look for an
20144 asm-specification. */
20145 if (cp_parser_allow_gnu_extensions_p (parser))
20147 /* Look for an asm-specification. */
20148 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20149 asm_specification = cp_parser_asm_specification_opt (parser);
20151 else
20152 asm_specification = NULL_TREE;
20154 /* Look for attributes. */
20155 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20156 attributes = cp_parser_attributes_opt (parser);
20158 /* Peek at the next token. */
20159 token = cp_lexer_peek_token (parser->lexer);
20161 bool bogus_implicit_tmpl = false;
20163 if (function_declarator_p (declarator))
20165 /* Handle C++17 deduction guides. */
20166 if (!decl_specifiers->type
20167 && ctor_dtor_or_conv_p <= 0
20168 && cxx_dialect >= cxx17)
20170 cp_declarator *id = get_id_declarator (declarator);
20171 tree name = id->u.id.unqualified_name;
20172 parser->scope = id->u.id.qualifying_scope;
20173 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20174 if (tmpl
20175 && (DECL_CLASS_TEMPLATE_P (tmpl)
20176 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20178 id->u.id.unqualified_name = dguide_name (tmpl);
20179 id->u.id.sfk = sfk_deduction_guide;
20180 ctor_dtor_or_conv_p = 1;
20184 /* Check to see if the token indicates the start of a
20185 function-definition. */
20186 if (cp_parser_token_starts_function_definition_p (token))
20188 if (!function_definition_allowed_p)
20190 /* If a function-definition should not appear here, issue an
20191 error message. */
20192 cp_parser_error (parser,
20193 "a function-definition is not allowed here");
20194 return error_mark_node;
20197 location_t func_brace_location
20198 = cp_lexer_peek_token (parser->lexer)->location;
20200 /* Neither attributes nor an asm-specification are allowed
20201 on a function-definition. */
20202 if (asm_specification)
20203 error_at (asm_spec_start_token->location,
20204 "an asm-specification is not allowed "
20205 "on a function-definition");
20206 if (attributes)
20207 error_at (attributes_start_token->location,
20208 "attributes are not allowed "
20209 "on a function-definition");
20210 /* This is a function-definition. */
20211 *function_definition_p = true;
20213 /* Parse the function definition. */
20214 if (member_p)
20215 decl = cp_parser_save_member_function_body (parser,
20216 decl_specifiers,
20217 declarator,
20218 prefix_attributes);
20219 else
20220 decl =
20221 (cp_parser_function_definition_from_specifiers_and_declarator
20222 (parser, decl_specifiers, prefix_attributes, declarator));
20224 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20226 /* This is where the prologue starts... */
20227 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20228 = func_brace_location;
20231 return decl;
20234 else if (parser->fully_implicit_function_template_p)
20236 /* A non-template declaration involving a function parameter list
20237 containing an implicit template parameter will be made into a
20238 template. If the resulting declaration is not going to be an
20239 actual function then finish the template scope here to prevent it.
20240 An error message will be issued once we have a decl to talk about.
20242 FIXME probably we should do type deduction rather than create an
20243 implicit template, but the standard currently doesn't allow it. */
20244 bogus_implicit_tmpl = true;
20245 finish_fully_implicit_template (parser, NULL_TREE);
20248 /* [dcl.dcl]
20250 Only in function declarations for constructors, destructors, type
20251 conversions, and deduction guides can the decl-specifier-seq be omitted.
20253 We explicitly postpone this check past the point where we handle
20254 function-definitions because we tolerate function-definitions
20255 that are missing their return types in some modes. */
20256 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20258 cp_parser_error (parser,
20259 "expected constructor, destructor, or type conversion");
20260 return error_mark_node;
20263 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20264 if (token->type == CPP_EQ
20265 || token->type == CPP_OPEN_PAREN
20266 || token->type == CPP_OPEN_BRACE)
20268 is_initialized = SD_INITIALIZED;
20269 initialization_kind = token->type;
20270 if (maybe_range_for_decl)
20271 *maybe_range_for_decl = error_mark_node;
20272 tmp_init_loc = token->location;
20273 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20274 *init_loc = tmp_init_loc;
20276 if (token->type == CPP_EQ
20277 && function_declarator_p (declarator))
20279 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20280 if (t2->keyword == RID_DEFAULT)
20281 is_initialized = SD_DEFAULTED;
20282 else if (t2->keyword == RID_DELETE)
20283 is_initialized = SD_DELETED;
20286 else
20288 /* If the init-declarator isn't initialized and isn't followed by a
20289 `,' or `;', it's not a valid init-declarator. */
20290 if (token->type != CPP_COMMA
20291 && token->type != CPP_SEMICOLON)
20293 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20294 range_for_decl_p = true;
20295 else
20297 if (!maybe_range_for_decl)
20298 cp_parser_error (parser, "expected initializer");
20299 return error_mark_node;
20302 is_initialized = SD_UNINITIALIZED;
20303 initialization_kind = CPP_EOF;
20306 /* Because start_decl has side-effects, we should only call it if we
20307 know we're going ahead. By this point, we know that we cannot
20308 possibly be looking at any other construct. */
20309 cp_parser_commit_to_tentative_parse (parser);
20311 /* Enter the newly declared entry in the symbol table. If we're
20312 processing a declaration in a class-specifier, we wait until
20313 after processing the initializer. */
20314 if (!member_p)
20316 if (parser->in_unbraced_linkage_specification_p)
20317 decl_specifiers->storage_class = sc_extern;
20318 decl = start_decl (declarator, decl_specifiers,
20319 range_for_decl_p? SD_INITIALIZED : is_initialized,
20320 attributes, prefix_attributes, &pushed_scope);
20321 cp_finalize_omp_declare_simd (parser, decl);
20322 cp_finalize_oacc_routine (parser, decl, false);
20323 /* Adjust location of decl if declarator->id_loc is more appropriate:
20324 set, and decl wasn't merged with another decl, in which case its
20325 location would be different from input_location, and more accurate. */
20326 if (DECL_P (decl)
20327 && declarator->id_loc != UNKNOWN_LOCATION
20328 && DECL_SOURCE_LOCATION (decl) == input_location)
20329 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20331 else if (scope)
20332 /* Enter the SCOPE. That way unqualified names appearing in the
20333 initializer will be looked up in SCOPE. */
20334 pushed_scope = push_scope (scope);
20336 /* Perform deferred access control checks, now that we know in which
20337 SCOPE the declared entity resides. */
20338 if (!member_p && decl)
20340 tree saved_current_function_decl = NULL_TREE;
20342 /* If the entity being declared is a function, pretend that we
20343 are in its scope. If it is a `friend', it may have access to
20344 things that would not otherwise be accessible. */
20345 if (TREE_CODE (decl) == FUNCTION_DECL)
20347 saved_current_function_decl = current_function_decl;
20348 current_function_decl = decl;
20351 /* Perform access checks for template parameters. */
20352 cp_parser_perform_template_parameter_access_checks (checks);
20354 /* Perform the access control checks for the declarator and the
20355 decl-specifiers. */
20356 perform_deferred_access_checks (tf_warning_or_error);
20358 /* Restore the saved value. */
20359 if (TREE_CODE (decl) == FUNCTION_DECL)
20360 current_function_decl = saved_current_function_decl;
20363 /* Parse the initializer. */
20364 initializer = NULL_TREE;
20365 is_direct_init = false;
20366 is_non_constant_init = true;
20367 if (is_initialized)
20369 if (function_declarator_p (declarator))
20371 if (initialization_kind == CPP_EQ)
20372 initializer = cp_parser_pure_specifier (parser);
20373 else
20375 /* If the declaration was erroneous, we don't really
20376 know what the user intended, so just silently
20377 consume the initializer. */
20378 if (decl != error_mark_node)
20379 error_at (tmp_init_loc, "initializer provided for function");
20380 cp_parser_skip_to_closing_parenthesis (parser,
20381 /*recovering=*/true,
20382 /*or_comma=*/false,
20383 /*consume_paren=*/true);
20386 else
20388 /* We want to record the extra mangling scope for in-class
20389 initializers of class members and initializers of static data
20390 member templates. The former involves deferring
20391 parsing of the initializer until end of class as with default
20392 arguments. So right here we only handle the latter. */
20393 if (!member_p && processing_template_decl && decl != error_mark_node)
20394 start_lambda_scope (decl);
20395 initializer = cp_parser_initializer (parser,
20396 &is_direct_init,
20397 &is_non_constant_init);
20398 if (!member_p && processing_template_decl && decl != error_mark_node)
20399 finish_lambda_scope ();
20400 if (initializer == error_mark_node)
20401 cp_parser_skip_to_end_of_statement (parser);
20405 /* The old parser allows attributes to appear after a parenthesized
20406 initializer. Mark Mitchell proposed removing this functionality
20407 on the GCC mailing lists on 2002-08-13. This parser accepts the
20408 attributes -- but ignores them. Made a permerror in GCC 8. */
20409 if (cp_parser_allow_gnu_extensions_p (parser)
20410 && initialization_kind == CPP_OPEN_PAREN
20411 && cp_parser_attributes_opt (parser)
20412 && permerror (input_location,
20413 "attributes after parenthesized initializer ignored"))
20415 static bool hint;
20416 if (flag_permissive && !hint)
20418 hint = true;
20419 inform (input_location,
20420 "this flexibility is deprecated and will be removed");
20424 /* And now complain about a non-function implicit template. */
20425 if (bogus_implicit_tmpl && decl != error_mark_node)
20426 error_at (DECL_SOURCE_LOCATION (decl),
20427 "non-function %qD declared as implicit template", decl);
20429 /* For an in-class declaration, use `grokfield' to create the
20430 declaration. */
20431 if (member_p)
20433 if (pushed_scope)
20435 pop_scope (pushed_scope);
20436 pushed_scope = NULL_TREE;
20438 decl = grokfield (declarator, decl_specifiers,
20439 initializer, !is_non_constant_init,
20440 /*asmspec=*/NULL_TREE,
20441 attr_chainon (attributes, prefix_attributes));
20442 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20443 cp_parser_save_default_args (parser, decl);
20444 cp_finalize_omp_declare_simd (parser, decl);
20445 cp_finalize_oacc_routine (parser, decl, false);
20448 /* Finish processing the declaration. But, skip member
20449 declarations. */
20450 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20452 cp_finish_decl (decl,
20453 initializer, !is_non_constant_init,
20454 asm_specification,
20455 /* If the initializer is in parentheses, then this is
20456 a direct-initialization, which means that an
20457 `explicit' constructor is OK. Otherwise, an
20458 `explicit' constructor cannot be used. */
20459 ((is_direct_init || !is_initialized)
20460 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20462 else if ((cxx_dialect != cxx98) && friend_p
20463 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20464 /* Core issue #226 (C++0x only): A default template-argument
20465 shall not be specified in a friend class template
20466 declaration. */
20467 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20468 /*is_partial=*/false, /*is_friend_decl=*/1);
20470 if (!friend_p && pushed_scope)
20471 pop_scope (pushed_scope);
20473 if (function_declarator_p (declarator)
20474 && parser->fully_implicit_function_template_p)
20476 if (member_p)
20477 decl = finish_fully_implicit_template (parser, decl);
20478 else
20479 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20482 if (auto_result && is_initialized && decl_specifiers->type
20483 && type_uses_auto (decl_specifiers->type))
20484 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20486 return decl;
20489 /* Parse a declarator.
20491 declarator:
20492 direct-declarator
20493 ptr-operator declarator
20495 abstract-declarator:
20496 ptr-operator abstract-declarator [opt]
20497 direct-abstract-declarator
20499 GNU Extensions:
20501 declarator:
20502 attributes [opt] direct-declarator
20503 attributes [opt] ptr-operator declarator
20505 abstract-declarator:
20506 attributes [opt] ptr-operator abstract-declarator [opt]
20507 attributes [opt] direct-abstract-declarator
20509 The parser flags FLAGS is used to control type-specifier parsing.
20511 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20512 detect constructors, destructors, deduction guides, or conversion operators.
20513 It is set to -1 if the declarator is a name, and +1 if it is a
20514 function. Otherwise it is set to zero. Usually you just want to
20515 test for >0, but internally the negative value is used.
20517 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20518 a decl-specifier-seq unless it declares a constructor, destructor,
20519 or conversion. It might seem that we could check this condition in
20520 semantic analysis, rather than parsing, but that makes it difficult
20521 to handle something like `f()'. We want to notice that there are
20522 no decl-specifiers, and therefore realize that this is an
20523 expression, not a declaration.)
20525 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20526 the declarator is a direct-declarator of the form "(...)".
20528 MEMBER_P is true iff this declarator is a member-declarator.
20530 FRIEND_P is true iff this declarator is a friend.
20532 STATIC_P is true iff the keyword static was seen. */
20534 static cp_declarator *
20535 cp_parser_declarator (cp_parser* parser,
20536 cp_parser_declarator_kind dcl_kind,
20537 cp_parser_flags flags,
20538 int* ctor_dtor_or_conv_p,
20539 bool* parenthesized_p,
20540 bool member_p, bool friend_p, bool static_p)
20542 cp_declarator *declarator;
20543 enum tree_code code;
20544 cp_cv_quals cv_quals;
20545 tree class_type;
20546 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20548 /* Assume this is not a constructor, destructor, or type-conversion
20549 operator. */
20550 if (ctor_dtor_or_conv_p)
20551 *ctor_dtor_or_conv_p = 0;
20553 if (cp_parser_allow_gnu_extensions_p (parser))
20554 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20556 /* Check for the ptr-operator production. */
20557 cp_parser_parse_tentatively (parser);
20558 /* Parse the ptr-operator. */
20559 code = cp_parser_ptr_operator (parser,
20560 &class_type,
20561 &cv_quals,
20562 &std_attributes);
20564 /* If that worked, then we have a ptr-operator. */
20565 if (cp_parser_parse_definitely (parser))
20567 /* If a ptr-operator was found, then this declarator was not
20568 parenthesized. */
20569 if (parenthesized_p)
20570 *parenthesized_p = true;
20571 /* The dependent declarator is optional if we are parsing an
20572 abstract-declarator. */
20573 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20574 cp_parser_parse_tentatively (parser);
20576 /* Parse the dependent declarator. */
20577 declarator = cp_parser_declarator (parser, dcl_kind,
20578 CP_PARSER_FLAGS_NONE,
20579 /*ctor_dtor_or_conv_p=*/NULL,
20580 /*parenthesized_p=*/NULL,
20581 /*member_p=*/false,
20582 friend_p, /*static_p=*/false);
20584 /* If we are parsing an abstract-declarator, we must handle the
20585 case where the dependent declarator is absent. */
20586 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20587 && !cp_parser_parse_definitely (parser))
20588 declarator = NULL;
20590 declarator = cp_parser_make_indirect_declarator
20591 (code, class_type, cv_quals, declarator, std_attributes);
20593 /* Everything else is a direct-declarator. */
20594 else
20596 if (parenthesized_p)
20597 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20598 CPP_OPEN_PAREN);
20599 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20600 flags, ctor_dtor_or_conv_p,
20601 member_p, friend_p, static_p);
20604 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20605 declarator->attributes = gnu_attributes;
20606 return declarator;
20609 /* Parse a direct-declarator or direct-abstract-declarator.
20611 direct-declarator:
20612 declarator-id
20613 direct-declarator ( parameter-declaration-clause )
20614 cv-qualifier-seq [opt]
20615 ref-qualifier [opt]
20616 exception-specification [opt]
20617 direct-declarator [ constant-expression [opt] ]
20618 ( declarator )
20620 direct-abstract-declarator:
20621 direct-abstract-declarator [opt]
20622 ( parameter-declaration-clause )
20623 cv-qualifier-seq [opt]
20624 ref-qualifier [opt]
20625 exception-specification [opt]
20626 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20627 ( abstract-declarator )
20629 Returns a representation of the declarator. DCL_KIND is
20630 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20631 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20632 we are parsing a direct-declarator. It is
20633 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20634 of ambiguity we prefer an abstract declarator, as per
20635 [dcl.ambig.res].
20636 The parser flags FLAGS is used to control type-specifier parsing.
20637 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20638 as for cp_parser_declarator. */
20640 static cp_declarator *
20641 cp_parser_direct_declarator (cp_parser* parser,
20642 cp_parser_declarator_kind dcl_kind,
20643 cp_parser_flags flags,
20644 int* ctor_dtor_or_conv_p,
20645 bool member_p, bool friend_p, bool static_p)
20647 cp_token *token;
20648 cp_declarator *declarator = NULL;
20649 tree scope = NULL_TREE;
20650 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20651 bool saved_in_declarator_p = parser->in_declarator_p;
20652 bool first = true;
20653 tree pushed_scope = NULL_TREE;
20654 cp_token *open_paren = NULL, *close_paren = NULL;
20656 while (true)
20658 /* Peek at the next token. */
20659 token = cp_lexer_peek_token (parser->lexer);
20660 if (token->type == CPP_OPEN_PAREN)
20662 /* This is either a parameter-declaration-clause, or a
20663 parenthesized declarator. When we know we are parsing a
20664 named declarator, it must be a parenthesized declarator
20665 if FIRST is true. For instance, `(int)' is a
20666 parameter-declaration-clause, with an omitted
20667 direct-abstract-declarator. But `((*))', is a
20668 parenthesized abstract declarator. Finally, when T is a
20669 template parameter `(T)' is a
20670 parameter-declaration-clause, and not a parenthesized
20671 named declarator.
20673 We first try and parse a parameter-declaration-clause,
20674 and then try a nested declarator (if FIRST is true).
20676 It is not an error for it not to be a
20677 parameter-declaration-clause, even when FIRST is
20678 false. Consider,
20680 int i (int);
20681 int i (3);
20683 The first is the declaration of a function while the
20684 second is the definition of a variable, including its
20685 initializer.
20687 Having seen only the parenthesis, we cannot know which of
20688 these two alternatives should be selected. Even more
20689 complex are examples like:
20691 int i (int (a));
20692 int i (int (3));
20694 The former is a function-declaration; the latter is a
20695 variable initialization.
20697 Thus again, we try a parameter-declaration-clause, and if
20698 that fails, we back out and return. */
20700 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20702 tree params;
20703 bool is_declarator = false;
20705 open_paren = NULL;
20707 /* In a member-declarator, the only valid interpretation
20708 of a parenthesis is the start of a
20709 parameter-declaration-clause. (It is invalid to
20710 initialize a static data member with a parenthesized
20711 initializer; only the "=" form of initialization is
20712 permitted.) */
20713 if (!member_p)
20714 cp_parser_parse_tentatively (parser);
20716 /* Consume the `('. */
20717 matching_parens parens;
20718 parens.consume_open (parser);
20719 if (first)
20721 /* If this is going to be an abstract declarator, we're
20722 in a declarator and we can't have default args. */
20723 parser->default_arg_ok_p = false;
20724 parser->in_declarator_p = true;
20727 begin_scope (sk_function_parms, NULL_TREE);
20729 /* Parse the parameter-declaration-clause. */
20730 params
20731 = cp_parser_parameter_declaration_clause (parser, flags);
20733 /* Consume the `)'. */
20734 parens.require_close (parser);
20736 /* If all went well, parse the cv-qualifier-seq,
20737 ref-qualifier and the exception-specification. */
20738 if (member_p || cp_parser_parse_definitely (parser))
20740 cp_cv_quals cv_quals;
20741 cp_virt_specifiers virt_specifiers;
20742 cp_ref_qualifier ref_qual;
20743 tree exception_specification;
20744 tree late_return;
20745 tree attrs;
20746 bool memfn = (member_p || (pushed_scope
20747 && CLASS_TYPE_P (pushed_scope)));
20748 unsigned char local_variables_forbidden_p
20749 = parser->local_variables_forbidden_p;
20750 /* 'this' is not allowed in static member functions. */
20751 if (static_p || friend_p)
20752 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20754 is_declarator = true;
20756 if (ctor_dtor_or_conv_p)
20757 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20758 first = false;
20760 /* Parse the cv-qualifier-seq. */
20761 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20762 /* Parse the ref-qualifier. */
20763 ref_qual = cp_parser_ref_qualifier_opt (parser);
20764 /* Parse the tx-qualifier. */
20765 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20766 /* And the exception-specification. */
20767 exception_specification
20768 = cp_parser_exception_specification_opt (parser);
20770 attrs = cp_parser_std_attribute_spec_seq (parser);
20772 /* In here, we handle cases where attribute is used after
20773 the function declaration. For example:
20774 void func (int x) __attribute__((vector(..))); */
20775 tree gnu_attrs = NULL_TREE;
20776 tree requires_clause = NULL_TREE;
20777 late_return = (cp_parser_late_return_type_opt
20778 (parser, declarator, requires_clause,
20779 memfn ? cv_quals : -1));
20781 /* Parse the virt-specifier-seq. */
20782 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20784 /* Create the function-declarator. */
20785 declarator = make_call_declarator (declarator,
20786 params,
20787 cv_quals,
20788 virt_specifiers,
20789 ref_qual,
20790 tx_qual,
20791 exception_specification,
20792 late_return,
20793 requires_clause);
20794 declarator->std_attributes = attrs;
20795 declarator->attributes = gnu_attrs;
20796 /* Any subsequent parameter lists are to do with
20797 return type, so are not those of the declared
20798 function. */
20799 parser->default_arg_ok_p = false;
20801 /* Restore the state of local_variables_forbidden_p. */
20802 parser->local_variables_forbidden_p
20803 = local_variables_forbidden_p;
20806 /* Remove the function parms from scope. */
20807 pop_bindings_and_leave_scope ();
20809 if (is_declarator)
20810 /* Repeat the main loop. */
20811 continue;
20814 /* If this is the first, we can try a parenthesized
20815 declarator. */
20816 if (first)
20818 bool saved_in_type_id_in_expr_p;
20820 parser->default_arg_ok_p = saved_default_arg_ok_p;
20821 parser->in_declarator_p = saved_in_declarator_p;
20823 open_paren = token;
20824 /* Consume the `('. */
20825 matching_parens parens;
20826 parens.consume_open (parser);
20827 /* Parse the nested declarator. */
20828 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20829 parser->in_type_id_in_expr_p = true;
20830 declarator
20831 = cp_parser_declarator (parser, dcl_kind, flags,
20832 ctor_dtor_or_conv_p,
20833 /*parenthesized_p=*/NULL,
20834 member_p, friend_p,
20835 /*static_p=*/false);
20836 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20837 first = false;
20838 /* Expect a `)'. */
20839 close_paren = cp_lexer_peek_token (parser->lexer);
20840 if (!parens.require_close (parser))
20841 declarator = cp_error_declarator;
20842 if (declarator == cp_error_declarator)
20843 break;
20845 goto handle_declarator;
20847 /* Otherwise, we must be done. */
20848 else
20849 break;
20851 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20852 && token->type == CPP_OPEN_SQUARE
20853 && !cp_next_tokens_can_be_attribute_p (parser))
20855 /* Parse an array-declarator. */
20856 tree bounds, attrs;
20858 if (ctor_dtor_or_conv_p)
20859 *ctor_dtor_or_conv_p = 0;
20861 open_paren = NULL;
20862 first = false;
20863 parser->default_arg_ok_p = false;
20864 parser->in_declarator_p = true;
20865 /* Consume the `['. */
20866 cp_lexer_consume_token (parser->lexer);
20867 /* Peek at the next token. */
20868 token = cp_lexer_peek_token (parser->lexer);
20869 /* If the next token is `]', then there is no
20870 constant-expression. */
20871 if (token->type != CPP_CLOSE_SQUARE)
20873 bool non_constant_p;
20874 bounds
20875 = cp_parser_constant_expression (parser,
20876 /*allow_non_constant=*/true,
20877 &non_constant_p);
20878 if (!non_constant_p)
20879 /* OK */;
20880 else if (error_operand_p (bounds))
20881 /* Already gave an error. */;
20882 else if (!parser->in_function_body
20883 || current_binding_level->kind == sk_function_parms)
20885 /* Normally, the array bound must be an integral constant
20886 expression. However, as an extension, we allow VLAs
20887 in function scopes as long as they aren't part of a
20888 parameter declaration. */
20889 cp_parser_error (parser,
20890 "array bound is not an integer constant");
20891 bounds = error_mark_node;
20893 else if (processing_template_decl
20894 && !type_dependent_expression_p (bounds))
20896 /* Remember this wasn't a constant-expression. */
20897 bounds = build_nop (TREE_TYPE (bounds), bounds);
20898 TREE_SIDE_EFFECTS (bounds) = 1;
20901 else
20902 bounds = NULL_TREE;
20903 /* Look for the closing `]'. */
20904 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20906 declarator = cp_error_declarator;
20907 break;
20910 attrs = cp_parser_std_attribute_spec_seq (parser);
20911 declarator = make_array_declarator (declarator, bounds);
20912 declarator->std_attributes = attrs;
20914 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20917 tree qualifying_scope;
20918 tree unqualified_name;
20919 tree attrs;
20920 special_function_kind sfk;
20921 bool abstract_ok;
20922 bool pack_expansion_p = false;
20923 cp_token *declarator_id_start_token;
20925 /* Parse a declarator-id */
20926 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20927 if (abstract_ok)
20929 cp_parser_parse_tentatively (parser);
20931 /* If we see an ellipsis, we should be looking at a
20932 parameter pack. */
20933 if (token->type == CPP_ELLIPSIS)
20935 /* Consume the `...' */
20936 cp_lexer_consume_token (parser->lexer);
20938 pack_expansion_p = true;
20942 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20943 unqualified_name
20944 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20945 qualifying_scope = parser->scope;
20946 if (abstract_ok)
20948 bool okay = false;
20950 if (!unqualified_name && pack_expansion_p)
20952 /* Check whether an error occurred. */
20953 okay = !cp_parser_error_occurred (parser);
20955 /* We already consumed the ellipsis to mark a
20956 parameter pack, but we have no way to report it,
20957 so abort the tentative parse. We will be exiting
20958 immediately anyway. */
20959 cp_parser_abort_tentative_parse (parser);
20961 else
20962 okay = cp_parser_parse_definitely (parser);
20964 if (!okay)
20965 unqualified_name = error_mark_node;
20966 else if (unqualified_name
20967 && (qualifying_scope
20968 || (!identifier_p (unqualified_name))))
20970 cp_parser_error (parser, "expected unqualified-id");
20971 unqualified_name = error_mark_node;
20975 if (!unqualified_name)
20976 return NULL;
20977 if (unqualified_name == error_mark_node)
20979 declarator = cp_error_declarator;
20980 pack_expansion_p = false;
20981 declarator->parameter_pack_p = false;
20982 break;
20985 attrs = cp_parser_std_attribute_spec_seq (parser);
20987 if (qualifying_scope && at_namespace_scope_p ()
20988 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20990 /* In the declaration of a member of a template class
20991 outside of the class itself, the SCOPE will sometimes
20992 be a TYPENAME_TYPE. For example, given:
20994 template <typename T>
20995 int S<T>::R::i = 3;
20997 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20998 this context, we must resolve S<T>::R to an ordinary
20999 type, rather than a typename type.
21001 The reason we normally avoid resolving TYPENAME_TYPEs
21002 is that a specialization of `S' might render
21003 `S<T>::R' not a type. However, if `S' is
21004 specialized, then this `i' will not be used, so there
21005 is no harm in resolving the types here. */
21006 tree type;
21008 /* Resolve the TYPENAME_TYPE. */
21009 type = resolve_typename_type (qualifying_scope,
21010 /*only_current_p=*/false);
21011 /* If that failed, the declarator is invalid. */
21012 if (TREE_CODE (type) == TYPENAME_TYPE)
21014 if (typedef_variant_p (type))
21015 error_at (declarator_id_start_token->location,
21016 "cannot define member of dependent typedef "
21017 "%qT", type);
21018 else
21019 error_at (declarator_id_start_token->location,
21020 "%<%T::%E%> is not a type",
21021 TYPE_CONTEXT (qualifying_scope),
21022 TYPE_IDENTIFIER (qualifying_scope));
21024 qualifying_scope = type;
21027 sfk = sfk_none;
21029 if (unqualified_name)
21031 tree class_type;
21033 if (qualifying_scope
21034 && CLASS_TYPE_P (qualifying_scope))
21035 class_type = qualifying_scope;
21036 else
21037 class_type = current_class_type;
21039 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21041 tree name_type = TREE_TYPE (unqualified_name);
21043 if (!class_type || !same_type_p (name_type, class_type))
21045 /* We do not attempt to print the declarator
21046 here because we do not have enough
21047 information about its original syntactic
21048 form. */
21049 cp_parser_error (parser, "invalid declarator");
21050 declarator = cp_error_declarator;
21051 break;
21053 else if (qualifying_scope
21054 && CLASSTYPE_USE_TEMPLATE (name_type))
21056 error_at (declarator_id_start_token->location,
21057 "invalid use of constructor as a template");
21058 inform (declarator_id_start_token->location,
21059 "use %<%T::%D%> instead of %<%T::%D%> to "
21060 "name the constructor in a qualified name",
21061 class_type,
21062 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21063 class_type, name_type);
21064 declarator = cp_error_declarator;
21065 break;
21067 unqualified_name = constructor_name (class_type);
21070 if (class_type)
21072 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21073 sfk = sfk_destructor;
21074 else if (identifier_p (unqualified_name)
21075 && IDENTIFIER_CONV_OP_P (unqualified_name))
21076 sfk = sfk_conversion;
21077 else if (/* There's no way to declare a constructor
21078 for an unnamed type, even if the type
21079 got a name for linkage purposes. */
21080 !TYPE_WAS_UNNAMED (class_type)
21081 /* Handle correctly (c++/19200):
21083 struct S {
21084 struct T{};
21085 friend void S(T);
21088 and also:
21090 namespace N {
21091 void S();
21094 struct S {
21095 friend void N::S();
21096 }; */
21097 && (!friend_p || class_type == qualifying_scope)
21098 && constructor_name_p (unqualified_name,
21099 class_type))
21100 sfk = sfk_constructor;
21101 else if (is_overloaded_fn (unqualified_name)
21102 && DECL_CONSTRUCTOR_P (get_first_fn
21103 (unqualified_name)))
21104 sfk = sfk_constructor;
21106 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21107 *ctor_dtor_or_conv_p = -1;
21110 declarator = make_id_declarator (qualifying_scope,
21111 unqualified_name,
21112 sfk, token->location);
21113 declarator->std_attributes = attrs;
21114 declarator->parameter_pack_p = pack_expansion_p;
21116 if (pack_expansion_p)
21117 maybe_warn_variadic_templates ();
21119 /* We're looking for this case in [temp.res]:
21120 A qualified-id is assumed to name a type if [...]
21121 - it is a decl-specifier of the decl-specifier-seq of a
21122 parameter-declaration in a declarator of a function or
21123 function template declaration, ... */
21124 if (cxx_dialect >= cxx2a
21125 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21126 && declarator->kind == cdk_id
21127 && !at_class_scope_p ()
21128 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21130 /* ...whose declarator-id is qualified. If it isn't, never
21131 assume the parameters to refer to types. */
21132 if (qualifying_scope == NULL_TREE)
21133 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21134 else
21136 /* Now we have something like
21137 template <typename T> int C::x(S::p);
21138 which can be a function template declaration or a
21139 variable template definition. If name lookup for
21140 the declarator-id C::x finds one or more function
21141 templates, assume S::p to name a type. Otherwise,
21142 don't. */
21143 tree decl
21144 = cp_parser_lookup_name_simple (parser, unqualified_name,
21145 token->location);
21146 if (!is_overloaded_fn (decl))
21147 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21152 handle_declarator:;
21153 scope = get_scope_of_declarator (declarator);
21154 if (scope)
21156 /* Any names that appear after the declarator-id for a
21157 member are looked up in the containing scope. */
21158 if (at_function_scope_p ())
21160 /* But declarations with qualified-ids can't appear in a
21161 function. */
21162 cp_parser_error (parser, "qualified-id in declaration");
21163 declarator = cp_error_declarator;
21164 break;
21166 pushed_scope = push_scope (scope);
21168 parser->in_declarator_p = true;
21169 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21170 || (declarator && declarator->kind == cdk_id))
21171 /* Default args are only allowed on function
21172 declarations. */
21173 parser->default_arg_ok_p = saved_default_arg_ok_p;
21174 else
21175 parser->default_arg_ok_p = false;
21177 first = false;
21179 /* We're done. */
21180 else
21181 break;
21184 /* For an abstract declarator, we might wind up with nothing at this
21185 point. That's an error; the declarator is not optional. */
21186 if (!declarator)
21187 cp_parser_error (parser, "expected declarator");
21188 else if (open_paren)
21190 /* Record overly parenthesized declarator so we can give a
21191 diagnostic about confusing decl/expr disambiguation. */
21192 if (declarator->kind == cdk_array)
21194 /* If the open and close parens are on different lines, this
21195 is probably a formatting thing, so ignore. */
21196 expanded_location open = expand_location (open_paren->location);
21197 expanded_location close = expand_location (close_paren->location);
21198 if (open.line != close.line || open.file != close.file)
21199 open_paren = NULL;
21201 if (open_paren)
21202 declarator->parenthesized = open_paren->location;
21205 /* If we entered a scope, we must exit it now. */
21206 if (pushed_scope)
21207 pop_scope (pushed_scope);
21209 parser->default_arg_ok_p = saved_default_arg_ok_p;
21210 parser->in_declarator_p = saved_in_declarator_p;
21212 return declarator;
21215 /* Parse a ptr-operator.
21217 ptr-operator:
21218 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21219 * cv-qualifier-seq [opt]
21221 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21222 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21224 GNU Extension:
21226 ptr-operator:
21227 & cv-qualifier-seq [opt]
21229 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21230 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21231 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21232 filled in with the TYPE containing the member. *CV_QUALS is
21233 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21234 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21235 Note that the tree codes returned by this function have nothing
21236 to do with the types of trees that will be eventually be created
21237 to represent the pointer or reference type being parsed. They are
21238 just constants with suggestive names. */
21239 static enum tree_code
21240 cp_parser_ptr_operator (cp_parser* parser,
21241 tree* type,
21242 cp_cv_quals *cv_quals,
21243 tree *attributes)
21245 enum tree_code code = ERROR_MARK;
21246 cp_token *token;
21247 tree attrs = NULL_TREE;
21249 /* Assume that it's not a pointer-to-member. */
21250 *type = NULL_TREE;
21251 /* And that there are no cv-qualifiers. */
21252 *cv_quals = TYPE_UNQUALIFIED;
21254 /* Peek at the next token. */
21255 token = cp_lexer_peek_token (parser->lexer);
21257 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21258 if (token->type == CPP_MULT)
21259 code = INDIRECT_REF;
21260 else if (token->type == CPP_AND)
21261 code = ADDR_EXPR;
21262 else if ((cxx_dialect != cxx98) &&
21263 token->type == CPP_AND_AND) /* C++0x only */
21264 code = NON_LVALUE_EXPR;
21266 if (code != ERROR_MARK)
21268 /* Consume the `*', `&' or `&&'. */
21269 cp_lexer_consume_token (parser->lexer);
21271 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21272 `&', if we are allowing GNU extensions. (The only qualifier
21273 that can legally appear after `&' is `restrict', but that is
21274 enforced during semantic analysis. */
21275 if (code == INDIRECT_REF
21276 || cp_parser_allow_gnu_extensions_p (parser))
21277 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21279 attrs = cp_parser_std_attribute_spec_seq (parser);
21280 if (attributes != NULL)
21281 *attributes = attrs;
21283 else
21285 /* Try the pointer-to-member case. */
21286 cp_parser_parse_tentatively (parser);
21287 /* Look for the optional `::' operator. */
21288 cp_parser_global_scope_opt (parser,
21289 /*current_scope_valid_p=*/false);
21290 /* Look for the nested-name specifier. */
21291 token = cp_lexer_peek_token (parser->lexer);
21292 cp_parser_nested_name_specifier (parser,
21293 /*typename_keyword_p=*/false,
21294 /*check_dependency_p=*/true,
21295 /*type_p=*/false,
21296 /*is_declaration=*/false);
21297 /* If we found it, and the next token is a `*', then we are
21298 indeed looking at a pointer-to-member operator. */
21299 if (!cp_parser_error_occurred (parser)
21300 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21302 /* Indicate that the `*' operator was used. */
21303 code = INDIRECT_REF;
21305 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21306 error_at (token->location, "%qD is a namespace", parser->scope);
21307 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21308 error_at (token->location, "cannot form pointer to member of "
21309 "non-class %q#T", parser->scope);
21310 else
21312 /* The type of which the member is a member is given by the
21313 current SCOPE. */
21314 *type = parser->scope;
21315 /* The next name will not be qualified. */
21316 parser->scope = NULL_TREE;
21317 parser->qualifying_scope = NULL_TREE;
21318 parser->object_scope = NULL_TREE;
21319 /* Look for optional c++11 attributes. */
21320 attrs = cp_parser_std_attribute_spec_seq (parser);
21321 if (attributes != NULL)
21322 *attributes = attrs;
21323 /* Look for the optional cv-qualifier-seq. */
21324 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21327 /* If that didn't work we don't have a ptr-operator. */
21328 if (!cp_parser_parse_definitely (parser))
21329 cp_parser_error (parser, "expected ptr-operator");
21332 return code;
21335 /* Parse an (optional) cv-qualifier-seq.
21337 cv-qualifier-seq:
21338 cv-qualifier cv-qualifier-seq [opt]
21340 cv-qualifier:
21341 const
21342 volatile
21344 GNU Extension:
21346 cv-qualifier:
21347 __restrict__
21349 Returns a bitmask representing the cv-qualifiers. */
21351 static cp_cv_quals
21352 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21354 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21356 while (true)
21358 cp_token *token;
21359 cp_cv_quals cv_qualifier;
21361 /* Peek at the next token. */
21362 token = cp_lexer_peek_token (parser->lexer);
21363 /* See if it's a cv-qualifier. */
21364 switch (token->keyword)
21366 case RID_CONST:
21367 cv_qualifier = TYPE_QUAL_CONST;
21368 break;
21370 case RID_VOLATILE:
21371 cv_qualifier = TYPE_QUAL_VOLATILE;
21372 break;
21374 case RID_RESTRICT:
21375 cv_qualifier = TYPE_QUAL_RESTRICT;
21376 break;
21378 default:
21379 cv_qualifier = TYPE_UNQUALIFIED;
21380 break;
21383 if (!cv_qualifier)
21384 break;
21386 if (cv_quals & cv_qualifier)
21388 gcc_rich_location richloc (token->location);
21389 richloc.add_fixit_remove ();
21390 error_at (&richloc, "duplicate cv-qualifier");
21391 cp_lexer_purge_token (parser->lexer);
21393 else
21395 cp_lexer_consume_token (parser->lexer);
21396 cv_quals |= cv_qualifier;
21400 return cv_quals;
21403 /* Parse an (optional) ref-qualifier
21405 ref-qualifier:
21409 Returns cp_ref_qualifier representing ref-qualifier. */
21411 static cp_ref_qualifier
21412 cp_parser_ref_qualifier_opt (cp_parser* parser)
21414 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21416 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21417 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21418 return ref_qual;
21420 while (true)
21422 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21423 cp_token *token = cp_lexer_peek_token (parser->lexer);
21425 switch (token->type)
21427 case CPP_AND:
21428 curr_ref_qual = REF_QUAL_LVALUE;
21429 break;
21431 case CPP_AND_AND:
21432 curr_ref_qual = REF_QUAL_RVALUE;
21433 break;
21435 default:
21436 curr_ref_qual = REF_QUAL_NONE;
21437 break;
21440 if (!curr_ref_qual)
21441 break;
21442 else if (ref_qual)
21444 error_at (token->location, "multiple ref-qualifiers");
21445 cp_lexer_purge_token (parser->lexer);
21447 else
21449 ref_qual = curr_ref_qual;
21450 cp_lexer_consume_token (parser->lexer);
21454 return ref_qual;
21457 /* Parse an optional tx-qualifier.
21459 tx-qualifier:
21460 transaction_safe
21461 transaction_safe_dynamic */
21463 static tree
21464 cp_parser_tx_qualifier_opt (cp_parser *parser)
21466 cp_token *token = cp_lexer_peek_token (parser->lexer);
21467 if (token->type == CPP_NAME)
21469 tree name = token->u.value;
21470 const char *p = IDENTIFIER_POINTER (name);
21471 const int len = strlen ("transaction_safe");
21472 if (!strncmp (p, "transaction_safe", len))
21474 p += len;
21475 if (*p == '\0'
21476 || !strcmp (p, "_dynamic"))
21478 cp_lexer_consume_token (parser->lexer);
21479 if (!flag_tm)
21481 error ("%qE requires %<-fgnu-tm%>", name);
21482 return NULL_TREE;
21484 else
21485 return name;
21489 return NULL_TREE;
21492 /* Parse an (optional) virt-specifier-seq.
21494 virt-specifier-seq:
21495 virt-specifier virt-specifier-seq [opt]
21497 virt-specifier:
21498 override
21499 final
21501 Returns a bitmask representing the virt-specifiers. */
21503 static cp_virt_specifiers
21504 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21506 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21508 while (true)
21510 cp_token *token;
21511 cp_virt_specifiers virt_specifier;
21513 /* Peek at the next token. */
21514 token = cp_lexer_peek_token (parser->lexer);
21515 /* See if it's a virt-specifier-qualifier. */
21516 if (token->type != CPP_NAME)
21517 break;
21518 if (id_equal (token->u.value, "override"))
21520 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21521 virt_specifier = VIRT_SPEC_OVERRIDE;
21523 else if (id_equal (token->u.value, "final"))
21525 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21526 virt_specifier = VIRT_SPEC_FINAL;
21528 else if (id_equal (token->u.value, "__final"))
21530 virt_specifier = VIRT_SPEC_FINAL;
21532 else
21533 break;
21535 if (virt_specifiers & virt_specifier)
21537 gcc_rich_location richloc (token->location);
21538 richloc.add_fixit_remove ();
21539 error_at (&richloc, "duplicate virt-specifier");
21540 cp_lexer_purge_token (parser->lexer);
21542 else
21544 cp_lexer_consume_token (parser->lexer);
21545 virt_specifiers |= virt_specifier;
21548 return virt_specifiers;
21551 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21552 is in scope even though it isn't real. */
21554 void
21555 inject_this_parameter (tree ctype, cp_cv_quals quals)
21557 tree this_parm;
21559 if (current_class_ptr)
21561 /* We don't clear this between NSDMIs. Is it already what we want? */
21562 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21563 if (DECL_P (current_class_ptr)
21564 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21565 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21566 && cp_type_quals (type) == quals)
21567 return;
21570 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21571 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21572 current_class_ptr = NULL_TREE;
21573 current_class_ref
21574 = cp_build_fold_indirect_ref (this_parm);
21575 current_class_ptr = this_parm;
21578 /* Return true iff our current scope is a non-static data member
21579 initializer. */
21581 bool
21582 parsing_nsdmi (void)
21584 /* We recognize NSDMI context by the context-less 'this' pointer set up
21585 by the function above. */
21586 if (current_class_ptr
21587 && TREE_CODE (current_class_ptr) == PARM_DECL
21588 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21589 return true;
21590 return false;
21593 /* Parse a late-specified return type, if any. This is not a separate
21594 non-terminal, but part of a function declarator, which looks like
21596 -> trailing-type-specifier-seq abstract-declarator(opt)
21598 Returns the type indicated by the type-id.
21600 In addition to this, parse any queued up #pragma omp declare simd
21601 clauses, and #pragma acc routine clauses.
21603 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21604 function. */
21606 static tree
21607 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21608 tree& requires_clause, cp_cv_quals quals)
21610 cp_token *token;
21611 tree type = NULL_TREE;
21612 bool declare_simd_p = (parser->omp_declare_simd
21613 && declarator
21614 && declarator->kind == cdk_id);
21616 bool oacc_routine_p = (parser->oacc_routine
21617 && declarator
21618 && declarator->kind == cdk_id);
21620 /* Peek at the next token. */
21621 token = cp_lexer_peek_token (parser->lexer);
21622 /* A late-specified return type is indicated by an initial '->'. */
21623 if (token->type != CPP_DEREF
21624 && token->keyword != RID_REQUIRES
21625 && !(token->type == CPP_NAME
21626 && token->u.value == ridpointers[RID_REQUIRES])
21627 && !(declare_simd_p || oacc_routine_p))
21628 return NULL_TREE;
21630 tree save_ccp = current_class_ptr;
21631 tree save_ccr = current_class_ref;
21632 if (quals >= 0)
21634 /* DR 1207: 'this' is in scope in the trailing return type. */
21635 inject_this_parameter (current_class_type, quals);
21638 if (token->type == CPP_DEREF)
21640 /* Consume the ->. */
21641 cp_lexer_consume_token (parser->lexer);
21643 type = cp_parser_trailing_type_id (parser);
21646 /* Function declarations may be followed by a trailing
21647 requires-clause. */
21648 requires_clause = cp_parser_requires_clause_opt (parser);
21650 if (declare_simd_p)
21651 declarator->attributes
21652 = cp_parser_late_parsing_omp_declare_simd (parser,
21653 declarator->attributes);
21654 if (oacc_routine_p)
21655 declarator->attributes
21656 = cp_parser_late_parsing_oacc_routine (parser,
21657 declarator->attributes);
21659 if (quals >= 0)
21661 current_class_ptr = save_ccp;
21662 current_class_ref = save_ccr;
21665 return type;
21668 /* Parse a declarator-id.
21670 declarator-id:
21671 id-expression
21672 :: [opt] nested-name-specifier [opt] type-name
21674 In the `id-expression' case, the value returned is as for
21675 cp_parser_id_expression if the id-expression was an unqualified-id.
21676 If the id-expression was a qualified-id, then a SCOPE_REF is
21677 returned. The first operand is the scope (either a NAMESPACE_DECL
21678 or TREE_TYPE), but the second is still just a representation of an
21679 unqualified-id. */
21681 static tree
21682 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21684 tree id;
21685 /* The expression must be an id-expression. Assume that qualified
21686 names are the names of types so that:
21688 template <class T>
21689 int S<T>::R::i = 3;
21691 will work; we must treat `S<T>::R' as the name of a type.
21692 Similarly, assume that qualified names are templates, where
21693 required, so that:
21695 template <class T>
21696 int S<T>::R<T>::i = 3;
21698 will work, too. */
21699 id = cp_parser_id_expression (parser,
21700 /*template_keyword_p=*/false,
21701 /*check_dependency_p=*/false,
21702 /*template_p=*/NULL,
21703 /*declarator_p=*/true,
21704 optional_p);
21705 if (id && BASELINK_P (id))
21706 id = BASELINK_FUNCTIONS (id);
21707 return id;
21710 /* Parse a type-id.
21712 type-id:
21713 type-specifier-seq abstract-declarator [opt]
21715 The parser flags FLAGS is used to control type-specifier parsing.
21717 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21719 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21720 i.e. we've just seen "->".
21722 Returns the TYPE specified. */
21724 static tree
21725 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21726 bool is_template_arg, bool is_trailing_return,
21727 location_t *type_location)
21729 cp_decl_specifier_seq type_specifier_seq;
21730 cp_declarator *abstract_declarator;
21732 /* Parse the type-specifier-seq. */
21733 cp_parser_type_specifier_seq (parser, flags,
21734 /*is_declaration=*/false,
21735 is_trailing_return,
21736 &type_specifier_seq);
21737 if (type_location)
21738 *type_location = type_specifier_seq.locations[ds_type_spec];
21740 if (is_template_arg && type_specifier_seq.type
21741 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21742 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21743 /* A bare template name as a template argument is a template template
21744 argument, not a placeholder, so fail parsing it as a type argument. */
21746 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21747 cp_parser_simulate_error (parser);
21748 return error_mark_node;
21750 if (type_specifier_seq.type == error_mark_node)
21751 return error_mark_node;
21753 /* There might or might not be an abstract declarator. */
21754 cp_parser_parse_tentatively (parser);
21755 /* Look for the declarator. */
21756 abstract_declarator
21757 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21758 CP_PARSER_FLAGS_NONE, NULL,
21759 /*parenthesized_p=*/NULL,
21760 /*member_p=*/false,
21761 /*friend_p=*/false,
21762 /*static_p=*/false);
21763 /* Check to see if there really was a declarator. */
21764 if (!cp_parser_parse_definitely (parser))
21765 abstract_declarator = NULL;
21767 if (type_specifier_seq.type
21768 /* The concepts TS allows 'auto' as a type-id. */
21769 && (!flag_concepts || parser->in_type_id_in_expr_p)
21770 /* None of the valid uses of 'auto' in C++14 involve the type-id
21771 nonterminal, but it is valid in a trailing-return-type. */
21772 && !(cxx_dialect >= cxx14 && is_trailing_return))
21773 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21775 /* A type-id with type 'auto' is only ok if the abstract declarator
21776 is a function declarator with a late-specified return type.
21778 A type-id with 'auto' is also valid in a trailing-return-type
21779 in a compound-requirement. */
21780 if (abstract_declarator
21781 && abstract_declarator->kind == cdk_function
21782 && abstract_declarator->u.function.late_return_type)
21783 /* OK */;
21784 else if (parser->in_result_type_constraint_p)
21785 /* OK */;
21786 else
21788 location_t loc = type_specifier_seq.locations[ds_type_spec];
21789 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21791 error_at (loc, "missing template arguments after %qT",
21792 auto_node);
21793 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21794 tmpl);
21796 else
21797 error_at (loc, "invalid use of %qT", auto_node);
21798 return error_mark_node;
21802 return groktypename (&type_specifier_seq, abstract_declarator,
21803 is_template_arg);
21806 /* Wrapper for cp_parser_type_id_1. */
21808 static tree
21809 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21810 location_t *type_location)
21812 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21815 /* Wrapper for cp_parser_type_id_1. */
21817 static tree
21818 cp_parser_template_type_arg (cp_parser *parser)
21820 tree r;
21821 const char *saved_message = parser->type_definition_forbidden_message;
21822 parser->type_definition_forbidden_message
21823 = G_("types may not be defined in template arguments");
21824 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21825 parser->type_definition_forbidden_message = saved_message;
21826 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21828 error ("invalid use of %<auto%> in template argument");
21829 r = error_mark_node;
21831 return r;
21834 /* Wrapper for cp_parser_type_id_1. */
21836 static tree
21837 cp_parser_trailing_type_id (cp_parser *parser)
21839 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21840 false, true, NULL);
21843 /* Parse a type-specifier-seq.
21845 type-specifier-seq:
21846 type-specifier type-specifier-seq [opt]
21848 GNU extension:
21850 type-specifier-seq:
21851 attributes type-specifier-seq [opt]
21853 The parser flags FLAGS is used to control type-specifier parsing.
21855 If IS_DECLARATION is true, we are at the start of a "condition" or
21856 exception-declaration, so we might be followed by a declarator-id.
21858 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21859 i.e. we've just seen "->".
21861 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21863 static void
21864 cp_parser_type_specifier_seq (cp_parser* parser,
21865 cp_parser_flags flags,
21866 bool is_declaration,
21867 bool is_trailing_return,
21868 cp_decl_specifier_seq *type_specifier_seq)
21870 bool seen_type_specifier = false;
21871 cp_token *start_token = NULL;
21873 /* Clear the TYPE_SPECIFIER_SEQ. */
21874 clear_decl_specs (type_specifier_seq);
21876 flags |= CP_PARSER_FLAGS_OPTIONAL;
21877 /* In the context of a trailing return type, enum E { } is an
21878 elaborated-type-specifier followed by a function-body, not an
21879 enum-specifier. */
21880 if (is_trailing_return)
21881 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21883 /* Parse the type-specifiers and attributes. */
21884 while (true)
21886 tree type_specifier;
21887 bool is_cv_qualifier;
21889 /* Check for attributes first. */
21890 if (cp_next_tokens_can_be_attribute_p (parser))
21892 type_specifier_seq->attributes
21893 = attr_chainon (type_specifier_seq->attributes,
21894 cp_parser_attributes_opt (parser));
21895 continue;
21898 /* record the token of the beginning of the type specifier seq,
21899 for error reporting purposes*/
21900 if (!start_token)
21901 start_token = cp_lexer_peek_token (parser->lexer);
21903 /* Look for the type-specifier. */
21904 type_specifier = cp_parser_type_specifier (parser,
21905 flags,
21906 type_specifier_seq,
21907 /*is_declaration=*/false,
21908 NULL,
21909 &is_cv_qualifier);
21910 if (!type_specifier)
21912 /* If the first type-specifier could not be found, this is not a
21913 type-specifier-seq at all. */
21914 if (!seen_type_specifier)
21916 /* Set in_declarator_p to avoid skipping to the semicolon. */
21917 int in_decl = parser->in_declarator_p;
21918 parser->in_declarator_p = true;
21920 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21921 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21922 cp_parser_error (parser, "expected type-specifier");
21924 parser->in_declarator_p = in_decl;
21926 type_specifier_seq->type = error_mark_node;
21927 return;
21929 /* If subsequent type-specifiers could not be found, the
21930 type-specifier-seq is complete. */
21931 break;
21934 seen_type_specifier = true;
21935 /* The standard says that a condition can be:
21937 type-specifier-seq declarator = assignment-expression
21939 However, given:
21941 struct S {};
21942 if (int S = ...)
21944 we should treat the "S" as a declarator, not as a
21945 type-specifier. The standard doesn't say that explicitly for
21946 type-specifier-seq, but it does say that for
21947 decl-specifier-seq in an ordinary declaration. Perhaps it
21948 would be clearer just to allow a decl-specifier-seq here, and
21949 then add a semantic restriction that if any decl-specifiers
21950 that are not type-specifiers appear, the program is invalid. */
21951 if (is_declaration && !is_cv_qualifier)
21952 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21956 /* Return whether the function currently being declared has an associated
21957 template parameter list. */
21959 static bool
21960 function_being_declared_is_template_p (cp_parser* parser)
21962 if (!current_template_parms || processing_template_parmlist)
21963 return false;
21965 if (parser->implicit_template_scope)
21966 return true;
21968 if (at_class_scope_p ()
21969 && TYPE_BEING_DEFINED (current_class_type))
21970 return parser->num_template_parameter_lists != 0;
21972 return ((int) parser->num_template_parameter_lists > template_class_depth
21973 (current_class_type));
21976 /* Parse a parameter-declaration-clause.
21978 parameter-declaration-clause:
21979 parameter-declaration-list [opt] ... [opt]
21980 parameter-declaration-list , ...
21982 The parser flags FLAGS is used to control type-specifier parsing.
21984 Returns a representation for the parameter declarations. A return
21985 value of NULL indicates a parameter-declaration-clause consisting
21986 only of an ellipsis. */
21988 static tree
21989 cp_parser_parameter_declaration_clause (cp_parser* parser,
21990 cp_parser_flags flags)
21992 tree parameters;
21993 cp_token *token;
21994 bool ellipsis_p;
21996 temp_override<bool> cleanup
21997 (parser->auto_is_implicit_function_template_parm_p);
21999 if (!processing_specialization
22000 && !processing_template_parmlist
22001 && !processing_explicit_instantiation
22002 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22003 actual function or a random abstract declarator. */
22004 && parser->default_arg_ok_p)
22005 if (!current_function_decl
22006 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22007 parser->auto_is_implicit_function_template_parm_p = true;
22009 /* Peek at the next token. */
22010 token = cp_lexer_peek_token (parser->lexer);
22011 /* Check for trivial parameter-declaration-clauses. */
22012 if (token->type == CPP_ELLIPSIS)
22014 /* Consume the `...' token. */
22015 cp_lexer_consume_token (parser->lexer);
22016 return NULL_TREE;
22018 else if (token->type == CPP_CLOSE_PAREN)
22019 /* There are no parameters. */
22020 return void_list_node;
22021 /* Check for `(void)', too, which is a special case. */
22022 else if (token->keyword == RID_VOID
22023 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22024 == CPP_CLOSE_PAREN))
22026 /* Consume the `void' token. */
22027 cp_lexer_consume_token (parser->lexer);
22028 /* There are no parameters. */
22029 return void_list_node;
22032 /* Parse the parameter-declaration-list. */
22033 parameters = cp_parser_parameter_declaration_list (parser, flags);
22034 /* If a parse error occurred while parsing the
22035 parameter-declaration-list, then the entire
22036 parameter-declaration-clause is erroneous. */
22037 if (parameters == error_mark_node)
22038 return NULL_TREE;
22040 /* Peek at the next token. */
22041 token = cp_lexer_peek_token (parser->lexer);
22042 /* If it's a `,', the clause should terminate with an ellipsis. */
22043 if (token->type == CPP_COMMA)
22045 /* Consume the `,'. */
22046 cp_lexer_consume_token (parser->lexer);
22047 /* Expect an ellipsis. */
22048 ellipsis_p
22049 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22051 /* It might also be `...' if the optional trailing `,' was
22052 omitted. */
22053 else if (token->type == CPP_ELLIPSIS)
22055 /* Consume the `...' token. */
22056 cp_lexer_consume_token (parser->lexer);
22057 /* And remember that we saw it. */
22058 ellipsis_p = true;
22060 else
22061 ellipsis_p = false;
22063 /* Finish the parameter list. */
22064 if (!ellipsis_p)
22065 parameters = chainon (parameters, void_list_node);
22067 return parameters;
22070 /* Parse a parameter-declaration-list.
22072 parameter-declaration-list:
22073 parameter-declaration
22074 parameter-declaration-list , parameter-declaration
22076 The parser flags FLAGS is used to control type-specifier parsing.
22078 Returns a representation of the parameter-declaration-list, as for
22079 cp_parser_parameter_declaration_clause. However, the
22080 `void_list_node' is never appended to the list. */
22082 static tree
22083 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22085 tree parameters = NULL_TREE;
22086 tree *tail = &parameters;
22087 bool saved_in_unbraced_linkage_specification_p;
22088 int index = 0;
22090 /* The special considerations that apply to a function within an
22091 unbraced linkage specifications do not apply to the parameters
22092 to the function. */
22093 saved_in_unbraced_linkage_specification_p
22094 = parser->in_unbraced_linkage_specification_p;
22095 parser->in_unbraced_linkage_specification_p = false;
22097 /* Look for more parameters. */
22098 while (true)
22100 cp_parameter_declarator *parameter;
22101 tree decl = error_mark_node;
22102 bool parenthesized_p = false;
22104 /* Parse the parameter. */
22105 parameter
22106 = cp_parser_parameter_declaration (parser, flags,
22107 /*template_parm_p=*/false,
22108 &parenthesized_p);
22110 /* We don't know yet if the enclosing context is deprecated, so wait
22111 and warn in grokparms if appropriate. */
22112 deprecated_state = DEPRECATED_SUPPRESS;
22114 if (parameter)
22116 decl = grokdeclarator (parameter->declarator,
22117 &parameter->decl_specifiers,
22118 PARM,
22119 parameter->default_argument != NULL_TREE,
22120 &parameter->decl_specifiers.attributes);
22121 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22122 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22125 deprecated_state = DEPRECATED_NORMAL;
22127 /* If a parse error occurred parsing the parameter declaration,
22128 then the entire parameter-declaration-list is erroneous. */
22129 if (decl == error_mark_node)
22131 parameters = error_mark_node;
22132 break;
22135 if (parameter->decl_specifiers.attributes)
22136 cplus_decl_attributes (&decl,
22137 parameter->decl_specifiers.attributes,
22139 if (DECL_NAME (decl))
22140 decl = pushdecl (decl);
22142 if (decl != error_mark_node)
22144 retrofit_lang_decl (decl);
22145 DECL_PARM_INDEX (decl) = ++index;
22146 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22149 /* Add the new parameter to the list. */
22150 *tail = build_tree_list (parameter->default_argument, decl);
22151 tail = &TREE_CHAIN (*tail);
22153 /* Peek at the next token. */
22154 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22155 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22156 /* These are for Objective-C++ */
22157 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22158 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22159 /* The parameter-declaration-list is complete. */
22160 break;
22161 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22163 cp_token *token;
22165 /* Peek at the next token. */
22166 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22167 /* If it's an ellipsis, then the list is complete. */
22168 if (token->type == CPP_ELLIPSIS)
22169 break;
22170 /* Otherwise, there must be more parameters. Consume the
22171 `,'. */
22172 cp_lexer_consume_token (parser->lexer);
22173 /* When parsing something like:
22175 int i(float f, double d)
22177 we can tell after seeing the declaration for "f" that we
22178 are not looking at an initialization of a variable "i",
22179 but rather at the declaration of a function "i".
22181 Due to the fact that the parsing of template arguments
22182 (as specified to a template-id) requires backtracking we
22183 cannot use this technique when inside a template argument
22184 list. */
22185 if (!parser->in_template_argument_list_p
22186 && !parser->in_type_id_in_expr_p
22187 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22188 /* However, a parameter-declaration of the form
22189 "float(f)" (which is a valid declaration of a
22190 parameter "f") can also be interpreted as an
22191 expression (the conversion of "f" to "float"). */
22192 && !parenthesized_p)
22193 cp_parser_commit_to_tentative_parse (parser);
22195 else
22197 cp_parser_error (parser, "expected %<,%> or %<...%>");
22198 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22199 cp_parser_skip_to_closing_parenthesis (parser,
22200 /*recovering=*/true,
22201 /*or_comma=*/false,
22202 /*consume_paren=*/false);
22203 break;
22207 parser->in_unbraced_linkage_specification_p
22208 = saved_in_unbraced_linkage_specification_p;
22210 /* Reset implicit_template_scope if we are about to leave the function
22211 parameter list that introduced it. Note that for out-of-line member
22212 definitions, there will be one or more class scopes before we get to
22213 the template parameter scope. */
22215 if (cp_binding_level *its = parser->implicit_template_scope)
22216 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22218 while (maybe_its->kind == sk_class)
22219 maybe_its = maybe_its->level_chain;
22220 if (maybe_its == its)
22222 parser->implicit_template_parms = 0;
22223 parser->implicit_template_scope = 0;
22227 return parameters;
22230 /* Parse a parameter declaration.
22232 parameter-declaration:
22233 decl-specifier-seq ... [opt] declarator
22234 decl-specifier-seq declarator = assignment-expression
22235 decl-specifier-seq ... [opt] abstract-declarator [opt]
22236 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22238 The parser flags FLAGS is used to control type-specifier parsing.
22240 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22241 declares a template parameter. (In that case, a non-nested `>'
22242 token encountered during the parsing of the assignment-expression
22243 is not interpreted as a greater-than operator.)
22245 Returns a representation of the parameter, or NULL if an error
22246 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22247 true iff the declarator is of the form "(p)". */
22249 static cp_parameter_declarator *
22250 cp_parser_parameter_declaration (cp_parser *parser,
22251 cp_parser_flags flags,
22252 bool template_parm_p,
22253 bool *parenthesized_p)
22255 int declares_class_or_enum;
22256 cp_decl_specifier_seq decl_specifiers;
22257 cp_declarator *declarator;
22258 tree default_argument;
22259 cp_token *token = NULL, *declarator_token_start = NULL;
22260 const char *saved_message;
22261 bool template_parameter_pack_p = false;
22263 /* In a template parameter, `>' is not an operator.
22265 [temp.param]
22267 When parsing a default template-argument for a non-type
22268 template-parameter, the first non-nested `>' is taken as the end
22269 of the template parameter-list rather than a greater-than
22270 operator. */
22272 /* Type definitions may not appear in parameter types. */
22273 saved_message = parser->type_definition_forbidden_message;
22274 parser->type_definition_forbidden_message
22275 = G_("types may not be defined in parameter types");
22277 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22278 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22279 (current_template_parms)) : 0);
22281 /* Parse the declaration-specifiers. */
22282 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22283 cp_parser_decl_specifier_seq (parser,
22284 flags,
22285 &decl_specifiers,
22286 &declares_class_or_enum);
22288 /* Complain about missing 'typename' or other invalid type names. */
22289 if (!decl_specifiers.any_type_specifiers_p
22290 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22291 decl_specifiers.type = error_mark_node;
22293 /* If an error occurred, there's no reason to attempt to parse the
22294 rest of the declaration. */
22295 if (cp_parser_error_occurred (parser))
22297 parser->type_definition_forbidden_message = saved_message;
22298 return NULL;
22301 /* Peek at the next token. */
22302 token = cp_lexer_peek_token (parser->lexer);
22304 /* If the next token is a `)', `,', `=', `>', or `...', then there
22305 is no declarator. However, when variadic templates are enabled,
22306 there may be a declarator following `...'. */
22307 if (token->type == CPP_CLOSE_PAREN
22308 || token->type == CPP_COMMA
22309 || token->type == CPP_EQ
22310 || token->type == CPP_GREATER)
22312 declarator = NULL;
22313 if (parenthesized_p)
22314 *parenthesized_p = false;
22316 /* Otherwise, there should be a declarator. */
22317 else
22319 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22320 parser->default_arg_ok_p = false;
22322 /* After seeing a decl-specifier-seq, if the next token is not a
22323 "(", there is no possibility that the code is a valid
22324 expression. Therefore, if parsing tentatively, we commit at
22325 this point. */
22326 if (!parser->in_template_argument_list_p
22327 /* In an expression context, having seen:
22329 (int((char ...
22331 we cannot be sure whether we are looking at a
22332 function-type (taking a "char" as a parameter) or a cast
22333 of some object of type "char" to "int". */
22334 && !parser->in_type_id_in_expr_p
22335 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22336 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22337 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22338 cp_parser_commit_to_tentative_parse (parser);
22339 /* Parse the declarator. */
22340 declarator_token_start = token;
22341 declarator = cp_parser_declarator (parser,
22342 CP_PARSER_DECLARATOR_EITHER,
22343 CP_PARSER_FLAGS_NONE,
22344 /*ctor_dtor_or_conv_p=*/NULL,
22345 parenthesized_p,
22346 /*member_p=*/false,
22347 /*friend_p=*/false,
22348 /*static_p=*/false);
22349 parser->default_arg_ok_p = saved_default_arg_ok_p;
22350 /* After the declarator, allow more attributes. */
22351 decl_specifiers.attributes
22352 = attr_chainon (decl_specifiers.attributes,
22353 cp_parser_attributes_opt (parser));
22355 /* If the declarator is a template parameter pack, remember that and
22356 clear the flag in the declarator itself so we don't get errors
22357 from grokdeclarator. */
22358 if (template_parm_p && declarator && declarator->parameter_pack_p)
22360 declarator->parameter_pack_p = false;
22361 template_parameter_pack_p = true;
22365 /* If the next token is an ellipsis, and we have not seen a declarator
22366 name, and if either the type of the declarator contains parameter
22367 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22368 for, eg, abbreviated integral type names), then we actually have a
22369 parameter pack expansion expression. Otherwise, leave the ellipsis
22370 for a C-style variadic function. */
22371 token = cp_lexer_peek_token (parser->lexer);
22373 /* If a function parameter pack was specified and an implicit template
22374 parameter was introduced during cp_parser_parameter_declaration,
22375 change any implicit parameters introduced into packs. */
22376 if (parser->implicit_template_parms
22377 && ((token->type == CPP_ELLIPSIS
22378 && declarator_can_be_parameter_pack (declarator))
22379 || (declarator && declarator->parameter_pack_p)))
22381 int latest_template_parm_idx = TREE_VEC_LENGTH
22382 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22384 if (latest_template_parm_idx != template_parm_idx)
22385 decl_specifiers.type = convert_generic_types_to_packs
22386 (decl_specifiers.type,
22387 template_parm_idx, latest_template_parm_idx);
22390 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22392 tree type = decl_specifiers.type;
22394 if (type && DECL_P (type))
22395 type = TREE_TYPE (type);
22397 if (((type
22398 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22399 && (template_parm_p || uses_parameter_packs (type)))
22400 || (!type && template_parm_p))
22401 && declarator_can_be_parameter_pack (declarator))
22403 /* Consume the `...'. */
22404 cp_lexer_consume_token (parser->lexer);
22405 maybe_warn_variadic_templates ();
22407 /* Build a pack expansion type */
22408 if (template_parm_p)
22409 template_parameter_pack_p = true;
22410 else if (declarator)
22411 declarator->parameter_pack_p = true;
22412 else
22413 decl_specifiers.type = make_pack_expansion (type);
22417 /* The restriction on defining new types applies only to the type
22418 of the parameter, not to the default argument. */
22419 parser->type_definition_forbidden_message = saved_message;
22421 /* If the next token is `=', then process a default argument. */
22422 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22424 tree type = decl_specifiers.type;
22425 token = cp_lexer_peek_token (parser->lexer);
22426 /* If we are defining a class, then the tokens that make up the
22427 default argument must be saved and processed later. */
22428 if (!template_parm_p && at_class_scope_p ()
22429 && TYPE_BEING_DEFINED (current_class_type)
22430 && !LAMBDA_TYPE_P (current_class_type))
22431 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22433 // A constrained-type-specifier may declare a type template-parameter.
22434 else if (declares_constrained_type_template_parameter (type))
22435 default_argument
22436 = cp_parser_default_type_template_argument (parser);
22438 // A constrained-type-specifier may declare a template-template-parameter.
22439 else if (declares_constrained_template_template_parameter (type))
22440 default_argument
22441 = cp_parser_default_template_template_argument (parser);
22443 /* Outside of a class definition, we can just parse the
22444 assignment-expression. */
22445 else
22446 default_argument
22447 = cp_parser_default_argument (parser, template_parm_p);
22449 if (!parser->default_arg_ok_p)
22451 permerror (token->location,
22452 "default arguments are only "
22453 "permitted for function parameters");
22455 else if ((declarator && declarator->parameter_pack_p)
22456 || template_parameter_pack_p
22457 || (decl_specifiers.type
22458 && PACK_EXPANSION_P (decl_specifiers.type)))
22460 /* Find the name of the parameter pack. */
22461 cp_declarator *id_declarator = declarator;
22462 while (id_declarator && id_declarator->kind != cdk_id)
22463 id_declarator = id_declarator->declarator;
22465 if (id_declarator && id_declarator->kind == cdk_id)
22466 error_at (declarator_token_start->location,
22467 template_parm_p
22468 ? G_("template parameter pack %qD "
22469 "cannot have a default argument")
22470 : G_("parameter pack %qD cannot have "
22471 "a default argument"),
22472 id_declarator->u.id.unqualified_name);
22473 else
22474 error_at (declarator_token_start->location,
22475 template_parm_p
22476 ? G_("template parameter pack cannot have "
22477 "a default argument")
22478 : G_("parameter pack cannot have a "
22479 "default argument"));
22481 default_argument = NULL_TREE;
22484 else
22485 default_argument = NULL_TREE;
22487 if (default_argument)
22488 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22490 /* Generate a location for the parameter, ranging from the start of the
22491 initial token to the end of the final token (using input_location for
22492 the latter, set up by cp_lexer_set_source_position_from_token when
22493 consuming tokens).
22495 If we have a identifier, then use it for the caret location, e.g.
22497 extern int callee (int one, int (*two)(int, int), float three);
22498 ~~~~~~^~~~~~~~~~~~~~
22500 otherwise, reuse the start location for the caret location e.g.:
22502 extern int callee (int one, int (*)(int, int), float three);
22503 ^~~~~~~~~~~~~~~~~
22506 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22507 ? declarator->id_loc
22508 : decl_spec_token_start->location);
22509 location_t param_loc = make_location (caret_loc,
22510 decl_spec_token_start->location,
22511 input_location);
22513 return make_parameter_declarator (&decl_specifiers,
22514 declarator,
22515 default_argument,
22516 param_loc,
22517 template_parameter_pack_p);
22520 /* Parse a default argument and return it.
22522 TEMPLATE_PARM_P is true if this is a default argument for a
22523 non-type template parameter. */
22524 static tree
22525 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22527 tree default_argument = NULL_TREE;
22528 bool saved_greater_than_is_operator_p;
22529 unsigned char saved_local_variables_forbidden_p;
22530 bool non_constant_p, is_direct_init;
22532 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22533 set correctly. */
22534 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22535 parser->greater_than_is_operator_p = !template_parm_p;
22536 /* Local variable names (and the `this' keyword) may not
22537 appear in a default argument. */
22538 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22539 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22540 /* Parse the assignment-expression. */
22541 if (template_parm_p)
22542 push_deferring_access_checks (dk_no_deferred);
22543 tree saved_class_ptr = NULL_TREE;
22544 tree saved_class_ref = NULL_TREE;
22545 /* The "this" pointer is not valid in a default argument. */
22546 if (cfun)
22548 saved_class_ptr = current_class_ptr;
22549 cp_function_chain->x_current_class_ptr = NULL_TREE;
22550 saved_class_ref = current_class_ref;
22551 cp_function_chain->x_current_class_ref = NULL_TREE;
22553 default_argument
22554 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22555 /* Restore the "this" pointer. */
22556 if (cfun)
22558 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22559 cp_function_chain->x_current_class_ref = saved_class_ref;
22561 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22562 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22563 if (template_parm_p)
22564 pop_deferring_access_checks ();
22565 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22566 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22568 return default_argument;
22571 /* Parse a function-body.
22573 function-body:
22574 compound_statement */
22576 static void
22577 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22579 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22580 ? BCS_TRY_BLOCK : BCS_NORMAL),
22581 true);
22584 /* Parse a ctor-initializer-opt followed by a function-body. Return
22585 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22586 is true we are parsing a function-try-block. */
22588 static void
22589 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22590 bool in_function_try_block)
22592 tree body, list;
22593 const bool check_body_p =
22594 DECL_CONSTRUCTOR_P (current_function_decl)
22595 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22596 tree last = NULL;
22598 /* Begin the function body. */
22599 body = begin_function_body ();
22600 /* Parse the optional ctor-initializer. */
22601 cp_parser_ctor_initializer_opt (parser);
22603 /* If we're parsing a constexpr constructor definition, we need
22604 to check that the constructor body is indeed empty. However,
22605 before we get to cp_parser_function_body lot of junk has been
22606 generated, so we can't just check that we have an empty block.
22607 Rather we take a snapshot of the outermost block, and check whether
22608 cp_parser_function_body changed its state. */
22609 if (check_body_p)
22611 list = cur_stmt_list;
22612 if (STATEMENT_LIST_TAIL (list))
22613 last = STATEMENT_LIST_TAIL (list)->stmt;
22615 /* Parse the function-body. */
22616 cp_parser_function_body (parser, in_function_try_block);
22617 if (check_body_p)
22618 check_constexpr_ctor_body (last, list, /*complain=*/true);
22619 /* Finish the function body. */
22620 finish_function_body (body);
22623 /* Parse an initializer.
22625 initializer:
22626 = initializer-clause
22627 ( expression-list )
22629 Returns an expression representing the initializer. If no
22630 initializer is present, NULL_TREE is returned.
22632 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22633 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22634 set to TRUE if there is no initializer present. If there is an
22635 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22636 is set to true; otherwise it is set to false. */
22638 static tree
22639 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22640 bool* non_constant_p, bool subexpression_p)
22642 cp_token *token;
22643 tree init;
22645 /* Peek at the next token. */
22646 token = cp_lexer_peek_token (parser->lexer);
22648 /* Let our caller know whether or not this initializer was
22649 parenthesized. */
22650 *is_direct_init = (token->type != CPP_EQ);
22651 /* Assume that the initializer is constant. */
22652 *non_constant_p = false;
22654 if (token->type == CPP_EQ)
22656 /* Consume the `='. */
22657 cp_lexer_consume_token (parser->lexer);
22658 /* Parse the initializer-clause. */
22659 init = cp_parser_initializer_clause (parser, non_constant_p);
22661 else if (token->type == CPP_OPEN_PAREN)
22663 vec<tree, va_gc> *vec;
22664 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22665 /*cast_p=*/false,
22666 /*allow_expansion_p=*/true,
22667 non_constant_p);
22668 if (vec == NULL)
22669 return error_mark_node;
22670 init = build_tree_list_vec (vec);
22671 release_tree_vector (vec);
22673 else if (token->type == CPP_OPEN_BRACE)
22675 cp_lexer_set_source_position (parser->lexer);
22676 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22677 init = cp_parser_braced_list (parser, non_constant_p);
22678 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22680 else
22682 /* Anything else is an error. */
22683 cp_parser_error (parser, "expected initializer");
22684 init = error_mark_node;
22687 if (!subexpression_p && check_for_bare_parameter_packs (init))
22688 init = error_mark_node;
22690 return init;
22693 /* Parse an initializer-clause.
22695 initializer-clause:
22696 assignment-expression
22697 braced-init-list
22699 Returns an expression representing the initializer.
22701 If the `assignment-expression' production is used the value
22702 returned is simply a representation for the expression.
22704 Otherwise, calls cp_parser_braced_list. */
22706 static cp_expr
22707 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22709 cp_expr initializer;
22711 /* Assume the expression is constant. */
22712 *non_constant_p = false;
22714 /* If it is not a `{', then we are looking at an
22715 assignment-expression. */
22716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22718 initializer
22719 = cp_parser_constant_expression (parser,
22720 /*allow_non_constant_p=*/true,
22721 non_constant_p);
22723 else
22724 initializer = cp_parser_braced_list (parser, non_constant_p);
22726 return initializer;
22729 /* Parse a brace-enclosed initializer list.
22731 braced-init-list:
22732 { initializer-list , [opt] }
22733 { designated-initializer-list , [opt] }
22736 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22737 the elements of the initializer-list (or NULL, if the last
22738 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22739 NULL_TREE. There is no way to detect whether or not the optional
22740 trailing `,' was provided. NON_CONSTANT_P is as for
22741 cp_parser_initializer. */
22743 static cp_expr
22744 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22746 tree initializer;
22747 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22749 /* Consume the `{' token. */
22750 matching_braces braces;
22751 braces.require_open (parser);
22752 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22753 initializer = make_node (CONSTRUCTOR);
22754 /* If it's not a `}', then there is a non-trivial initializer. */
22755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22757 /* Parse the initializer list. */
22758 CONSTRUCTOR_ELTS (initializer)
22759 = cp_parser_initializer_list (parser, non_constant_p);
22760 /* A trailing `,' token is allowed. */
22761 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22762 cp_lexer_consume_token (parser->lexer);
22764 else
22765 *non_constant_p = false;
22766 /* Now, there should be a trailing `}'. */
22767 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22768 braces.require_close (parser);
22769 TREE_TYPE (initializer) = init_list_type_node;
22771 cp_expr result (initializer);
22772 /* Build a location of the form:
22773 { ... }
22774 ^~~~~~~
22775 with caret==start at the open brace, finish at the close brace. */
22776 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22777 result.set_location (combined_loc);
22778 return result;
22781 /* Consume tokens up to, and including, the next non-nested closing `]'.
22782 Returns true iff we found a closing `]'. */
22784 static bool
22785 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22787 unsigned square_depth = 0;
22789 while (true)
22791 cp_token * token = cp_lexer_peek_token (parser->lexer);
22793 switch (token->type)
22795 case CPP_PRAGMA_EOL:
22796 if (!parser->lexer->in_pragma)
22797 break;
22798 /* FALLTHRU */
22799 case CPP_EOF:
22800 /* If we've run out of tokens, then there is no closing `]'. */
22801 return false;
22803 case CPP_OPEN_SQUARE:
22804 ++square_depth;
22805 break;
22807 case CPP_CLOSE_SQUARE:
22808 if (!square_depth--)
22810 cp_lexer_consume_token (parser->lexer);
22811 return true;
22813 break;
22815 default:
22816 break;
22819 /* Consume the token. */
22820 cp_lexer_consume_token (parser->lexer);
22824 /* Return true if we are looking at an array-designator, false otherwise. */
22826 static bool
22827 cp_parser_array_designator_p (cp_parser *parser)
22829 /* Consume the `['. */
22830 cp_lexer_consume_token (parser->lexer);
22832 cp_lexer_save_tokens (parser->lexer);
22834 /* Skip tokens until the next token is a closing square bracket.
22835 If we find the closing `]', and the next token is a `=', then
22836 we are looking at an array designator. */
22837 bool array_designator_p
22838 = (cp_parser_skip_to_closing_square_bracket (parser)
22839 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22841 /* Roll back the tokens we skipped. */
22842 cp_lexer_rollback_tokens (parser->lexer);
22844 return array_designator_p;
22847 /* Parse an initializer-list.
22849 initializer-list:
22850 initializer-clause ... [opt]
22851 initializer-list , initializer-clause ... [opt]
22853 C++2A Extension:
22855 designated-initializer-list:
22856 designated-initializer-clause
22857 designated-initializer-list , designated-initializer-clause
22859 designated-initializer-clause:
22860 designator brace-or-equal-initializer
22862 designator:
22863 . identifier
22865 GNU Extension:
22867 initializer-list:
22868 designation initializer-clause ...[opt]
22869 initializer-list , designation initializer-clause ...[opt]
22871 designation:
22872 . identifier =
22873 identifier :
22874 [ constant-expression ] =
22876 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22877 for the initializer. If the INDEX of the elt is non-NULL, it is the
22878 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22879 as for cp_parser_initializer. */
22881 static vec<constructor_elt, va_gc> *
22882 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22884 vec<constructor_elt, va_gc> *v = NULL;
22885 bool first_p = true;
22886 tree first_designator = NULL_TREE;
22888 /* Assume all of the expressions are constant. */
22889 *non_constant_p = false;
22891 /* Parse the rest of the list. */
22892 while (true)
22894 cp_token *token;
22895 tree designator;
22896 tree initializer;
22897 bool clause_non_constant_p;
22898 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22900 /* Handle the C++2A syntax, '. id ='. */
22901 if ((cxx_dialect >= cxx2a
22902 || cp_parser_allow_gnu_extensions_p (parser))
22903 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22904 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22905 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22906 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22907 == CPP_OPEN_BRACE)))
22909 if (cxx_dialect < cxx2a)
22910 pedwarn (loc, OPT_Wpedantic,
22911 "C++ designated initializers only available with "
22912 "-std=c++2a or -std=gnu++2a");
22913 /* Consume the `.'. */
22914 cp_lexer_consume_token (parser->lexer);
22915 /* Consume the identifier. */
22916 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22917 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22918 /* Consume the `='. */
22919 cp_lexer_consume_token (parser->lexer);
22921 /* Also, if the next token is an identifier and the following one is a
22922 colon, we are looking at the GNU designated-initializer
22923 syntax. */
22924 else if (cp_parser_allow_gnu_extensions_p (parser)
22925 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22926 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22927 == CPP_COLON))
22929 /* Warn the user that they are using an extension. */
22930 pedwarn (loc, OPT_Wpedantic,
22931 "ISO C++ does not allow GNU designated initializers");
22932 /* Consume the identifier. */
22933 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22934 /* Consume the `:'. */
22935 cp_lexer_consume_token (parser->lexer);
22937 /* Also handle C99 array designators, '[ const ] ='. */
22938 else if (cp_parser_allow_gnu_extensions_p (parser)
22939 && !c_dialect_objc ()
22940 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22942 /* In C++11, [ could start a lambda-introducer. */
22943 bool non_const = false;
22945 cp_parser_parse_tentatively (parser);
22947 if (!cp_parser_array_designator_p (parser))
22949 cp_parser_simulate_error (parser);
22950 designator = NULL_TREE;
22952 else
22954 designator = cp_parser_constant_expression (parser, true,
22955 &non_const);
22956 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22957 cp_parser_require (parser, CPP_EQ, RT_EQ);
22960 if (!cp_parser_parse_definitely (parser))
22961 designator = NULL_TREE;
22962 else if (non_const
22963 && (!require_potential_rvalue_constant_expression
22964 (designator)))
22965 designator = NULL_TREE;
22966 if (designator)
22967 /* Warn the user that they are using an extension. */
22968 pedwarn (loc, OPT_Wpedantic,
22969 "ISO C++ does not allow C99 designated initializers");
22971 else
22972 designator = NULL_TREE;
22974 if (first_p)
22976 first_designator = designator;
22977 first_p = false;
22979 else if (cxx_dialect >= cxx2a
22980 && first_designator != error_mark_node
22981 && (!first_designator != !designator))
22983 error_at (loc, "either all initializer clauses should be designated "
22984 "or none of them should be");
22985 first_designator = error_mark_node;
22987 else if (cxx_dialect < cxx2a && !first_designator)
22988 first_designator = designator;
22990 /* Parse the initializer. */
22991 initializer = cp_parser_initializer_clause (parser,
22992 &clause_non_constant_p);
22993 /* If any clause is non-constant, so is the entire initializer. */
22994 if (clause_non_constant_p)
22995 *non_constant_p = true;
22997 /* If we have an ellipsis, this is an initializer pack
22998 expansion. */
22999 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23001 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23003 /* Consume the `...'. */
23004 cp_lexer_consume_token (parser->lexer);
23006 if (designator && cxx_dialect >= cxx2a)
23007 error_at (loc,
23008 "%<...%> not allowed in designated initializer list");
23010 /* Turn the initializer into an initializer expansion. */
23011 initializer = make_pack_expansion (initializer);
23014 /* Add it to the vector. */
23015 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23017 /* If the next token is not a comma, we have reached the end of
23018 the list. */
23019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23020 break;
23022 /* Peek at the next token. */
23023 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23024 /* If the next token is a `}', then we're still done. An
23025 initializer-clause can have a trailing `,' after the
23026 initializer-list and before the closing `}'. */
23027 if (token->type == CPP_CLOSE_BRACE)
23028 break;
23030 /* Consume the `,' token. */
23031 cp_lexer_consume_token (parser->lexer);
23034 /* The same identifier shall not appear in multiple designators
23035 of a designated-initializer-list. */
23036 if (first_designator)
23038 unsigned int i;
23039 tree designator, val;
23040 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23041 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23043 if (IDENTIFIER_MARKED (designator))
23045 error_at (cp_expr_loc_or_loc (val, input_location),
23046 "%<.%s%> designator used multiple times in "
23047 "the same initializer list",
23048 IDENTIFIER_POINTER (designator));
23049 (*v)[i].index = error_mark_node;
23051 else
23052 IDENTIFIER_MARKED (designator) = 1;
23054 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23055 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23056 IDENTIFIER_MARKED (designator) = 0;
23059 return v;
23062 /* Classes [gram.class] */
23064 /* Parse a class-name.
23066 class-name:
23067 identifier
23068 template-id
23070 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23071 to indicate that names looked up in dependent types should be
23072 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23073 keyword has been used to indicate that the name that appears next
23074 is a template. TAG_TYPE indicates the explicit tag given before
23075 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23076 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23077 is the class being defined in a class-head. If ENUM_OK is TRUE,
23078 enum-names are also accepted.
23080 Returns the TYPE_DECL representing the class. */
23082 static tree
23083 cp_parser_class_name (cp_parser *parser,
23084 bool typename_keyword_p,
23085 bool template_keyword_p,
23086 enum tag_types tag_type,
23087 bool check_dependency_p,
23088 bool class_head_p,
23089 bool is_declaration,
23090 bool enum_ok)
23092 tree decl;
23093 tree scope;
23094 bool typename_p;
23095 cp_token *token;
23096 tree identifier = NULL_TREE;
23098 /* All class-names start with an identifier. */
23099 token = cp_lexer_peek_token (parser->lexer);
23100 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23102 cp_parser_error (parser, "expected class-name");
23103 return error_mark_node;
23106 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23107 to a template-id, so we save it here. */
23108 scope = parser->scope;
23109 if (scope == error_mark_node)
23110 return error_mark_node;
23112 /* Any name names a type if we're following the `typename' keyword
23113 in a qualified name where the enclosing scope is type-dependent. */
23114 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23115 && dependent_type_p (scope));
23116 /* Handle the common case (an identifier, but not a template-id)
23117 efficiently. */
23118 if (token->type == CPP_NAME
23119 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23121 cp_token *identifier_token;
23122 bool ambiguous_p;
23124 /* Look for the identifier. */
23125 identifier_token = cp_lexer_peek_token (parser->lexer);
23126 ambiguous_p = identifier_token->error_reported;
23127 identifier = cp_parser_identifier (parser);
23128 /* If the next token isn't an identifier, we are certainly not
23129 looking at a class-name. */
23130 if (identifier == error_mark_node)
23131 decl = error_mark_node;
23132 /* If we know this is a type-name, there's no need to look it
23133 up. */
23134 else if (typename_p)
23135 decl = identifier;
23136 else
23138 tree ambiguous_decls;
23139 /* If we already know that this lookup is ambiguous, then
23140 we've already issued an error message; there's no reason
23141 to check again. */
23142 if (ambiguous_p)
23144 cp_parser_simulate_error (parser);
23145 return error_mark_node;
23147 /* If the next token is a `::', then the name must be a type
23148 name.
23150 [basic.lookup.qual]
23152 During the lookup for a name preceding the :: scope
23153 resolution operator, object, function, and enumerator
23154 names are ignored. */
23155 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23156 tag_type = scope_type;
23157 /* Look up the name. */
23158 decl = cp_parser_lookup_name (parser, identifier,
23159 tag_type,
23160 /*is_template=*/false,
23161 /*is_namespace=*/false,
23162 check_dependency_p,
23163 &ambiguous_decls,
23164 identifier_token->location);
23165 if (ambiguous_decls)
23167 if (cp_parser_parsing_tentatively (parser))
23168 cp_parser_simulate_error (parser);
23169 return error_mark_node;
23173 else
23175 /* Try a template-id. */
23176 decl = cp_parser_template_id (parser, template_keyword_p,
23177 check_dependency_p,
23178 tag_type,
23179 is_declaration);
23180 if (decl == error_mark_node)
23181 return error_mark_node;
23184 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23186 /* If this is a typename, create a TYPENAME_TYPE. */
23187 if (typename_p
23188 && decl != error_mark_node
23189 && !is_overloaded_fn (decl))
23191 decl = make_typename_type (scope, decl, typename_type,
23192 /*complain=*/tf_error);
23193 if (decl != error_mark_node)
23194 decl = TYPE_NAME (decl);
23197 decl = strip_using_decl (decl);
23199 /* Check to see that it is really the name of a class. */
23200 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23201 && identifier_p (TREE_OPERAND (decl, 0))
23202 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23203 /* Situations like this:
23205 template <typename T> struct A {
23206 typename T::template X<int>::I i;
23209 are problematic. Is `T::template X<int>' a class-name? The
23210 standard does not seem to be definitive, but there is no other
23211 valid interpretation of the following `::'. Therefore, those
23212 names are considered class-names. */
23214 decl = make_typename_type (scope, decl, tag_type, tf_error);
23215 if (decl != error_mark_node)
23216 decl = TYPE_NAME (decl);
23218 else if (TREE_CODE (decl) != TYPE_DECL
23219 || TREE_TYPE (decl) == error_mark_node
23220 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23221 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23222 /* In Objective-C 2.0, a classname followed by '.' starts a
23223 dot-syntax expression, and it's not a type-name. */
23224 || (c_dialect_objc ()
23225 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23226 && objc_is_class_name (decl)))
23227 decl = error_mark_node;
23229 if (decl == error_mark_node)
23230 cp_parser_error (parser, "expected class-name");
23231 else if (identifier && !parser->scope)
23232 maybe_note_name_used_in_class (identifier, decl);
23234 return decl;
23237 /* Parse a class-specifier.
23239 class-specifier:
23240 class-head { member-specification [opt] }
23242 Returns the TREE_TYPE representing the class. */
23244 static tree
23245 cp_parser_class_specifier_1 (cp_parser* parser)
23247 tree type;
23248 tree attributes = NULL_TREE;
23249 bool nested_name_specifier_p;
23250 unsigned saved_num_template_parameter_lists;
23251 bool saved_in_function_body;
23252 unsigned char in_statement;
23253 bool in_switch_statement_p;
23254 bool saved_in_unbraced_linkage_specification_p;
23255 tree old_scope = NULL_TREE;
23256 tree scope = NULL_TREE;
23257 cp_token *closing_brace;
23259 push_deferring_access_checks (dk_no_deferred);
23261 /* Parse the class-head. */
23262 type = cp_parser_class_head (parser,
23263 &nested_name_specifier_p);
23264 /* If the class-head was a semantic disaster, skip the entire body
23265 of the class. */
23266 if (!type)
23268 cp_parser_skip_to_end_of_block_or_statement (parser);
23269 pop_deferring_access_checks ();
23270 return error_mark_node;
23273 /* Look for the `{'. */
23274 matching_braces braces;
23275 if (!braces.require_open (parser))
23277 pop_deferring_access_checks ();
23278 return error_mark_node;
23281 cp_ensure_no_omp_declare_simd (parser);
23282 cp_ensure_no_oacc_routine (parser);
23284 /* Issue an error message if type-definitions are forbidden here. */
23285 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23286 /* Remember that we are defining one more class. */
23287 ++parser->num_classes_being_defined;
23288 /* Inside the class, surrounding template-parameter-lists do not
23289 apply. */
23290 saved_num_template_parameter_lists
23291 = parser->num_template_parameter_lists;
23292 parser->num_template_parameter_lists = 0;
23293 /* We are not in a function body. */
23294 saved_in_function_body = parser->in_function_body;
23295 parser->in_function_body = false;
23296 /* Or in a loop. */
23297 in_statement = parser->in_statement;
23298 parser->in_statement = 0;
23299 /* Or in a switch. */
23300 in_switch_statement_p = parser->in_switch_statement_p;
23301 parser->in_switch_statement_p = false;
23302 /* We are not immediately inside an extern "lang" block. */
23303 saved_in_unbraced_linkage_specification_p
23304 = parser->in_unbraced_linkage_specification_p;
23305 parser->in_unbraced_linkage_specification_p = false;
23307 // Associate constraints with the type.
23308 if (flag_concepts)
23309 type = associate_classtype_constraints (type);
23311 /* Start the class. */
23312 if (nested_name_specifier_p)
23314 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23315 old_scope = push_inner_scope (scope);
23317 type = begin_class_definition (type);
23319 if (type == error_mark_node)
23320 /* If the type is erroneous, skip the entire body of the class. */
23321 cp_parser_skip_to_closing_brace (parser);
23322 else
23323 /* Parse the member-specification. */
23324 cp_parser_member_specification_opt (parser);
23326 /* Look for the trailing `}'. */
23327 closing_brace = braces.require_close (parser);
23328 /* Look for trailing attributes to apply to this class. */
23329 if (cp_parser_allow_gnu_extensions_p (parser))
23330 attributes = cp_parser_gnu_attributes_opt (parser);
23331 if (type != error_mark_node)
23332 type = finish_struct (type, attributes);
23333 if (nested_name_specifier_p)
23334 pop_inner_scope (old_scope, scope);
23336 /* We've finished a type definition. Check for the common syntax
23337 error of forgetting a semicolon after the definition. We need to
23338 be careful, as we can't just check for not-a-semicolon and be done
23339 with it; the user might have typed:
23341 class X { } c = ...;
23342 class X { } *p = ...;
23344 and so forth. Instead, enumerate all the possible tokens that
23345 might follow this production; if we don't see one of them, then
23346 complain and silently insert the semicolon. */
23348 cp_token *token = cp_lexer_peek_token (parser->lexer);
23349 bool want_semicolon = true;
23351 if (cp_next_tokens_can_be_std_attribute_p (parser))
23352 /* Don't try to parse c++11 attributes here. As per the
23353 grammar, that should be a task for
23354 cp_parser_decl_specifier_seq. */
23355 want_semicolon = false;
23357 switch (token->type)
23359 case CPP_NAME:
23360 case CPP_SEMICOLON:
23361 case CPP_MULT:
23362 case CPP_AND:
23363 case CPP_OPEN_PAREN:
23364 case CPP_CLOSE_PAREN:
23365 case CPP_COMMA:
23366 want_semicolon = false;
23367 break;
23369 /* While it's legal for type qualifiers and storage class
23370 specifiers to follow type definitions in the grammar, only
23371 compiler testsuites contain code like that. Assume that if
23372 we see such code, then what we're really seeing is a case
23373 like:
23375 class X { }
23376 const <type> var = ...;
23380 class Y { }
23381 static <type> func (...) ...
23383 i.e. the qualifier or specifier applies to the next
23384 declaration. To do so, however, we need to look ahead one
23385 more token to see if *that* token is a type specifier.
23387 This code could be improved to handle:
23389 class Z { }
23390 static const <type> var = ...; */
23391 case CPP_KEYWORD:
23392 if (keyword_is_decl_specifier (token->keyword))
23394 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23396 /* Handling user-defined types here would be nice, but very
23397 tricky. */
23398 want_semicolon
23399 = (lookahead->type == CPP_KEYWORD
23400 && keyword_begins_type_specifier (lookahead->keyword));
23402 break;
23403 default:
23404 break;
23407 /* If we don't have a type, then something is very wrong and we
23408 shouldn't try to do anything clever. Likewise for not seeing the
23409 closing brace. */
23410 if (closing_brace && TYPE_P (type) && want_semicolon)
23412 /* Locate the closing brace. */
23413 cp_token_position prev
23414 = cp_lexer_previous_token_position (parser->lexer);
23415 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23416 location_t loc = prev_token->location;
23418 /* We want to suggest insertion of a ';' immediately *after* the
23419 closing brace, so, if we can, offset the location by 1 column. */
23420 location_t next_loc = loc;
23421 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23422 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23424 rich_location richloc (line_table, next_loc);
23426 /* If we successfully offset the location, suggest the fix-it. */
23427 if (next_loc != loc)
23428 richloc.add_fixit_insert_before (next_loc, ";");
23430 if (CLASSTYPE_DECLARED_CLASS (type))
23431 error_at (&richloc,
23432 "expected %<;%> after class definition");
23433 else if (TREE_CODE (type) == RECORD_TYPE)
23434 error_at (&richloc,
23435 "expected %<;%> after struct definition");
23436 else if (TREE_CODE (type) == UNION_TYPE)
23437 error_at (&richloc,
23438 "expected %<;%> after union definition");
23439 else
23440 gcc_unreachable ();
23442 /* Unget one token and smash it to look as though we encountered
23443 a semicolon in the input stream. */
23444 cp_lexer_set_token_position (parser->lexer, prev);
23445 token = cp_lexer_peek_token (parser->lexer);
23446 token->type = CPP_SEMICOLON;
23447 token->keyword = RID_MAX;
23451 /* If this class is not itself within the scope of another class,
23452 then we need to parse the bodies of all of the queued function
23453 definitions. Note that the queued functions defined in a class
23454 are not always processed immediately following the
23455 class-specifier for that class. Consider:
23457 struct A {
23458 struct B { void f() { sizeof (A); } };
23461 If `f' were processed before the processing of `A' were
23462 completed, there would be no way to compute the size of `A'.
23463 Note that the nesting we are interested in here is lexical --
23464 not the semantic nesting given by TYPE_CONTEXT. In particular,
23465 for:
23467 struct A { struct B; };
23468 struct A::B { void f() { } };
23470 there is no need to delay the parsing of `A::B::f'. */
23471 if (--parser->num_classes_being_defined == 0)
23473 tree decl;
23474 tree class_type = NULL_TREE;
23475 tree pushed_scope = NULL_TREE;
23476 unsigned ix;
23477 cp_default_arg_entry *e;
23478 tree save_ccp, save_ccr;
23480 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23482 /* Skip default arguments, NSDMIs, etc, in order to improve
23483 error recovery (c++/71169, c++/71832). */
23484 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23485 vec_safe_truncate (unparsed_nsdmis, 0);
23486 vec_safe_truncate (unparsed_classes, 0);
23487 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23490 /* In a first pass, parse default arguments to the functions.
23491 Then, in a second pass, parse the bodies of the functions.
23492 This two-phased approach handles cases like:
23494 struct S {
23495 void f() { g(); }
23496 void g(int i = 3);
23500 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23502 decl = e->decl;
23503 /* If there are default arguments that have not yet been processed,
23504 take care of them now. */
23505 if (class_type != e->class_type)
23507 if (pushed_scope)
23508 pop_scope (pushed_scope);
23509 class_type = e->class_type;
23510 pushed_scope = push_scope (class_type);
23512 /* Make sure that any template parameters are in scope. */
23513 maybe_begin_member_template_processing (decl);
23514 /* Parse the default argument expressions. */
23515 cp_parser_late_parsing_default_args (parser, decl);
23516 /* Remove any template parameters from the symbol table. */
23517 maybe_end_member_template_processing ();
23519 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23520 /* Now parse any NSDMIs. */
23521 save_ccp = current_class_ptr;
23522 save_ccr = current_class_ref;
23523 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23525 if (class_type != DECL_CONTEXT (decl))
23527 if (pushed_scope)
23528 pop_scope (pushed_scope);
23529 class_type = DECL_CONTEXT (decl);
23530 pushed_scope = push_scope (class_type);
23532 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23533 cp_parser_late_parsing_nsdmi (parser, decl);
23535 vec_safe_truncate (unparsed_nsdmis, 0);
23536 current_class_ptr = save_ccp;
23537 current_class_ref = save_ccr;
23538 if (pushed_scope)
23539 pop_scope (pushed_scope);
23541 /* Now do some post-NSDMI bookkeeping. */
23542 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23543 after_nsdmi_defaulted_late_checks (class_type);
23544 vec_safe_truncate (unparsed_classes, 0);
23545 after_nsdmi_defaulted_late_checks (type);
23547 /* Now parse the body of the functions. */
23548 if (flag_openmp)
23550 /* OpenMP UDRs need to be parsed before all other functions. */
23551 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23552 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23553 cp_parser_late_parsing_for_member (parser, decl);
23554 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23555 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23556 cp_parser_late_parsing_for_member (parser, decl);
23558 else
23559 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23560 cp_parser_late_parsing_for_member (parser, decl);
23561 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23563 else
23564 vec_safe_push (unparsed_classes, type);
23566 /* Put back any saved access checks. */
23567 pop_deferring_access_checks ();
23569 /* Restore saved state. */
23570 parser->in_switch_statement_p = in_switch_statement_p;
23571 parser->in_statement = in_statement;
23572 parser->in_function_body = saved_in_function_body;
23573 parser->num_template_parameter_lists
23574 = saved_num_template_parameter_lists;
23575 parser->in_unbraced_linkage_specification_p
23576 = saved_in_unbraced_linkage_specification_p;
23578 return type;
23581 static tree
23582 cp_parser_class_specifier (cp_parser* parser)
23584 tree ret;
23585 timevar_push (TV_PARSE_STRUCT);
23586 ret = cp_parser_class_specifier_1 (parser);
23587 timevar_pop (TV_PARSE_STRUCT);
23588 return ret;
23591 /* Parse a class-head.
23593 class-head:
23594 class-key identifier [opt] base-clause [opt]
23595 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23596 class-key nested-name-specifier [opt] template-id
23597 base-clause [opt]
23599 class-virt-specifier:
23600 final
23602 GNU Extensions:
23603 class-key attributes identifier [opt] base-clause [opt]
23604 class-key attributes nested-name-specifier identifier base-clause [opt]
23605 class-key attributes nested-name-specifier [opt] template-id
23606 base-clause [opt]
23608 Upon return BASES is initialized to the list of base classes (or
23609 NULL, if there are none) in the same form returned by
23610 cp_parser_base_clause.
23612 Returns the TYPE of the indicated class. Sets
23613 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23614 involving a nested-name-specifier was used, and FALSE otherwise.
23616 Returns error_mark_node if this is not a class-head.
23618 Returns NULL_TREE if the class-head is syntactically valid, but
23619 semantically invalid in a way that means we should skip the entire
23620 body of the class. */
23622 static tree
23623 cp_parser_class_head (cp_parser* parser,
23624 bool* nested_name_specifier_p)
23626 tree nested_name_specifier;
23627 enum tag_types class_key;
23628 tree id = NULL_TREE;
23629 tree type = NULL_TREE;
23630 tree attributes;
23631 tree bases;
23632 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23633 bool template_id_p = false;
23634 bool qualified_p = false;
23635 bool invalid_nested_name_p = false;
23636 bool invalid_explicit_specialization_p = false;
23637 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23638 tree pushed_scope = NULL_TREE;
23639 unsigned num_templates;
23640 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23641 /* Assume no nested-name-specifier will be present. */
23642 *nested_name_specifier_p = false;
23643 /* Assume no template parameter lists will be used in defining the
23644 type. */
23645 num_templates = 0;
23646 parser->colon_corrects_to_scope_p = false;
23648 /* Look for the class-key. */
23649 class_key = cp_parser_class_key (parser);
23650 if (class_key == none_type)
23651 return error_mark_node;
23653 location_t class_head_start_location = input_location;
23655 /* Parse the attributes. */
23656 attributes = cp_parser_attributes_opt (parser);
23658 /* If the next token is `::', that is invalid -- but sometimes
23659 people do try to write:
23661 struct ::S {};
23663 Handle this gracefully by accepting the extra qualifier, and then
23664 issuing an error about it later if this really is a
23665 class-head. If it turns out just to be an elaborated type
23666 specifier, remain silent. */
23667 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23668 qualified_p = true;
23670 push_deferring_access_checks (dk_no_check);
23672 /* Determine the name of the class. Begin by looking for an
23673 optional nested-name-specifier. */
23674 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23675 nested_name_specifier
23676 = cp_parser_nested_name_specifier_opt (parser,
23677 /*typename_keyword_p=*/false,
23678 /*check_dependency_p=*/false,
23679 /*type_p=*/true,
23680 /*is_declaration=*/false);
23681 /* If there was a nested-name-specifier, then there *must* be an
23682 identifier. */
23684 cp_token *bad_template_keyword = NULL;
23686 if (nested_name_specifier)
23688 type_start_token = cp_lexer_peek_token (parser->lexer);
23689 /* Although the grammar says `identifier', it really means
23690 `class-name' or `template-name'. You are only allowed to
23691 define a class that has already been declared with this
23692 syntax.
23694 The proposed resolution for Core Issue 180 says that wherever
23695 you see `class T::X' you should treat `X' as a type-name.
23697 It is OK to define an inaccessible class; for example:
23699 class A { class B; };
23700 class A::B {};
23702 We do not know if we will see a class-name, or a
23703 template-name. We look for a class-name first, in case the
23704 class-name is a template-id; if we looked for the
23705 template-name first we would stop after the template-name. */
23706 cp_parser_parse_tentatively (parser);
23707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23708 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23709 type = cp_parser_class_name (parser,
23710 /*typename_keyword_p=*/false,
23711 /*template_keyword_p=*/false,
23712 class_type,
23713 /*check_dependency_p=*/false,
23714 /*class_head_p=*/true,
23715 /*is_declaration=*/false);
23716 /* If that didn't work, ignore the nested-name-specifier. */
23717 if (!cp_parser_parse_definitely (parser))
23719 invalid_nested_name_p = true;
23720 type_start_token = cp_lexer_peek_token (parser->lexer);
23721 id = cp_parser_identifier (parser);
23722 if (id == error_mark_node)
23723 id = NULL_TREE;
23725 /* If we could not find a corresponding TYPE, treat this
23726 declaration like an unqualified declaration. */
23727 if (type == error_mark_node)
23728 nested_name_specifier = NULL_TREE;
23729 /* Otherwise, count the number of templates used in TYPE and its
23730 containing scopes. */
23731 else
23732 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23734 /* Otherwise, the identifier is optional. */
23735 else
23737 /* We don't know whether what comes next is a template-id,
23738 an identifier, or nothing at all. */
23739 cp_parser_parse_tentatively (parser);
23740 /* Check for a template-id. */
23741 type_start_token = cp_lexer_peek_token (parser->lexer);
23742 id = cp_parser_template_id (parser,
23743 /*template_keyword_p=*/false,
23744 /*check_dependency_p=*/true,
23745 class_key,
23746 /*is_declaration=*/true);
23747 /* If that didn't work, it could still be an identifier. */
23748 if (!cp_parser_parse_definitely (parser))
23750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23752 type_start_token = cp_lexer_peek_token (parser->lexer);
23753 id = cp_parser_identifier (parser);
23755 else
23756 id = NULL_TREE;
23758 else
23760 template_id_p = true;
23761 ++num_templates;
23765 pop_deferring_access_checks ();
23767 if (id)
23769 cp_parser_check_for_invalid_template_id (parser, id,
23770 class_key,
23771 type_start_token->location);
23773 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23775 /* If it's not a `:' or a `{' then we can't really be looking at a
23776 class-head, since a class-head only appears as part of a
23777 class-specifier. We have to detect this situation before calling
23778 xref_tag, since that has irreversible side-effects. */
23779 if (!cp_parser_next_token_starts_class_definition_p (parser))
23781 cp_parser_error (parser, "expected %<{%> or %<:%>");
23782 type = error_mark_node;
23783 goto out;
23786 /* At this point, we're going ahead with the class-specifier, even
23787 if some other problem occurs. */
23788 cp_parser_commit_to_tentative_parse (parser);
23789 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23791 cp_parser_error (parser,
23792 "cannot specify %<override%> for a class");
23793 type = error_mark_node;
23794 goto out;
23796 /* Issue the error about the overly-qualified name now. */
23797 if (qualified_p)
23799 cp_parser_error (parser,
23800 "global qualification of class name is invalid");
23801 type = error_mark_node;
23802 goto out;
23804 else if (invalid_nested_name_p)
23806 cp_parser_error (parser,
23807 "qualified name does not name a class");
23808 type = error_mark_node;
23809 goto out;
23811 else if (nested_name_specifier)
23813 tree scope;
23815 if (bad_template_keyword)
23816 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23817 keyword template shall not appear at the top level. */
23818 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23819 "keyword %<template%> not allowed in class-head-name");
23821 /* Reject typedef-names in class heads. */
23822 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23824 error_at (type_start_token->location,
23825 "invalid class name in declaration of %qD",
23826 type);
23827 type = NULL_TREE;
23828 goto done;
23831 /* Figure out in what scope the declaration is being placed. */
23832 scope = current_scope ();
23833 /* If that scope does not contain the scope in which the
23834 class was originally declared, the program is invalid. */
23835 if (scope && !is_ancestor (scope, nested_name_specifier))
23837 if (at_namespace_scope_p ())
23838 error_at (type_start_token->location,
23839 "declaration of %qD in namespace %qD which does not "
23840 "enclose %qD",
23841 type, scope, nested_name_specifier);
23842 else
23843 error_at (type_start_token->location,
23844 "declaration of %qD in %qD which does not enclose %qD",
23845 type, scope, nested_name_specifier);
23846 type = NULL_TREE;
23847 goto done;
23849 /* [dcl.meaning]
23851 A declarator-id shall not be qualified except for the
23852 definition of a ... nested class outside of its class
23853 ... [or] the definition or explicit instantiation of a
23854 class member of a namespace outside of its namespace. */
23855 if (scope == nested_name_specifier)
23857 permerror (nested_name_specifier_token_start->location,
23858 "extra qualification not allowed");
23859 nested_name_specifier = NULL_TREE;
23860 num_templates = 0;
23863 /* An explicit-specialization must be preceded by "template <>". If
23864 it is not, try to recover gracefully. */
23865 if (at_namespace_scope_p ()
23866 && parser->num_template_parameter_lists == 0
23867 && !processing_template_parmlist
23868 && template_id_p)
23870 /* Build a location of this form:
23871 struct typename <ARGS>
23872 ^~~~~~~~~~~~~~~~~~~~~~
23873 with caret==start at the start token, and
23874 finishing at the end of the type. */
23875 location_t reported_loc
23876 = make_location (class_head_start_location,
23877 class_head_start_location,
23878 get_finish (type_start_token->location));
23879 rich_location richloc (line_table, reported_loc);
23880 richloc.add_fixit_insert_before (class_head_start_location,
23881 "template <> ");
23882 error_at (&richloc,
23883 "an explicit specialization must be preceded by"
23884 " %<template <>%>");
23885 invalid_explicit_specialization_p = true;
23886 /* Take the same action that would have been taken by
23887 cp_parser_explicit_specialization. */
23888 ++parser->num_template_parameter_lists;
23889 begin_specialization ();
23891 /* There must be no "return" statements between this point and the
23892 end of this function; set "type "to the correct return value and
23893 use "goto done;" to return. */
23894 /* Make sure that the right number of template parameters were
23895 present. */
23896 if (!cp_parser_check_template_parameters (parser, num_templates,
23897 template_id_p,
23898 type_start_token->location,
23899 /*declarator=*/NULL))
23901 /* If something went wrong, there is no point in even trying to
23902 process the class-definition. */
23903 type = NULL_TREE;
23904 goto done;
23907 /* Look up the type. */
23908 if (template_id_p)
23910 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23911 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23912 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23914 error_at (type_start_token->location,
23915 "function template %qD redeclared as a class template", id);
23916 type = error_mark_node;
23918 else
23920 type = TREE_TYPE (id);
23921 type = maybe_process_partial_specialization (type);
23923 /* Check the scope while we still know whether or not we had a
23924 nested-name-specifier. */
23925 if (type != error_mark_node)
23926 check_unqualified_spec_or_inst (type, type_start_token->location);
23928 if (nested_name_specifier)
23929 pushed_scope = push_scope (nested_name_specifier);
23931 else if (nested_name_specifier)
23933 tree class_type;
23935 /* Given:
23937 template <typename T> struct S { struct T };
23938 template <typename T> struct S<T>::T { };
23940 we will get a TYPENAME_TYPE when processing the definition of
23941 `S::T'. We need to resolve it to the actual type before we
23942 try to define it. */
23943 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23945 class_type = resolve_typename_type (TREE_TYPE (type),
23946 /*only_current_p=*/false);
23947 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23948 type = TYPE_NAME (class_type);
23949 else
23951 cp_parser_error (parser, "could not resolve typename type");
23952 type = error_mark_node;
23956 if (maybe_process_partial_specialization (TREE_TYPE (type))
23957 == error_mark_node)
23959 type = NULL_TREE;
23960 goto done;
23963 class_type = current_class_type;
23964 /* Enter the scope indicated by the nested-name-specifier. */
23965 pushed_scope = push_scope (nested_name_specifier);
23966 /* Get the canonical version of this type. */
23967 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23968 /* Call push_template_decl if it seems like we should be defining a
23969 template either from the template headers or the type we're
23970 defining, so that we diagnose both extra and missing headers. */
23971 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23972 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23973 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23975 type = push_template_decl (type);
23976 if (type == error_mark_node)
23978 type = NULL_TREE;
23979 goto done;
23983 type = TREE_TYPE (type);
23984 *nested_name_specifier_p = true;
23986 else /* The name is not a nested name. */
23988 /* If the class was unnamed, create a dummy name. */
23989 if (!id)
23990 id = make_anon_name ();
23991 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23992 ? ts_within_enclosing_non_class
23993 : ts_current);
23994 type = xref_tag (class_key, id, tag_scope,
23995 parser->num_template_parameter_lists);
23998 /* Indicate whether this class was declared as a `class' or as a
23999 `struct'. */
24000 if (TREE_CODE (type) == RECORD_TYPE)
24001 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24002 cp_parser_check_class_key (class_key, type);
24004 /* If this type was already complete, and we see another definition,
24005 that's an error. */
24006 if (type != error_mark_node && COMPLETE_TYPE_P (type))
24008 error_at (type_start_token->location, "redefinition of %q#T",
24009 type);
24010 inform (location_of (type), "previous definition of %q#T",
24011 type);
24012 type = NULL_TREE;
24013 goto done;
24015 else if (type == error_mark_node)
24016 type = NULL_TREE;
24018 if (type)
24020 /* Apply attributes now, before any use of the class as a template
24021 argument in its base list. */
24022 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24023 fixup_attribute_variants (type);
24026 /* We will have entered the scope containing the class; the names of
24027 base classes should be looked up in that context. For example:
24029 struct A { struct B {}; struct C; };
24030 struct A::C : B {};
24032 is valid. */
24034 /* Get the list of base-classes, if there is one. */
24035 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24037 /* PR59482: enter the class scope so that base-specifiers are looked
24038 up correctly. */
24039 if (type)
24040 pushclass (type);
24041 bases = cp_parser_base_clause (parser);
24042 /* PR59482: get out of the previously pushed class scope so that the
24043 subsequent pops pop the right thing. */
24044 if (type)
24045 popclass ();
24047 else
24048 bases = NULL_TREE;
24050 /* If we're really defining a class, process the base classes.
24051 If they're invalid, fail. */
24052 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24053 xref_basetypes (type, bases);
24055 done:
24056 /* Leave the scope given by the nested-name-specifier. We will
24057 enter the class scope itself while processing the members. */
24058 if (pushed_scope)
24059 pop_scope (pushed_scope);
24061 if (invalid_explicit_specialization_p)
24063 end_specialization ();
24064 --parser->num_template_parameter_lists;
24067 if (type)
24068 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24069 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24070 CLASSTYPE_FINAL (type) = 1;
24071 out:
24072 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24073 return type;
24076 /* Parse a class-key.
24078 class-key:
24079 class
24080 struct
24081 union
24083 Returns the kind of class-key specified, or none_type to indicate
24084 error. */
24086 static enum tag_types
24087 cp_parser_class_key (cp_parser* parser)
24089 cp_token *token;
24090 enum tag_types tag_type;
24092 /* Look for the class-key. */
24093 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24094 if (!token)
24095 return none_type;
24097 /* Check to see if the TOKEN is a class-key. */
24098 tag_type = cp_parser_token_is_class_key (token);
24099 if (!tag_type)
24100 cp_parser_error (parser, "expected class-key");
24101 return tag_type;
24104 /* Parse a type-parameter-key.
24106 type-parameter-key:
24107 class
24108 typename
24111 static void
24112 cp_parser_type_parameter_key (cp_parser* parser)
24114 /* Look for the type-parameter-key. */
24115 enum tag_types tag_type = none_type;
24116 cp_token *token = cp_lexer_peek_token (parser->lexer);
24117 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24119 cp_lexer_consume_token (parser->lexer);
24120 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24121 /* typename is not allowed in a template template parameter
24122 by the standard until C++17. */
24123 pedwarn (token->location, OPT_Wpedantic,
24124 "ISO C++ forbids typename key in template template parameter;"
24125 " use -std=c++17 or -std=gnu++17");
24127 else
24128 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24130 return;
24133 /* Parse an (optional) member-specification.
24135 member-specification:
24136 member-declaration member-specification [opt]
24137 access-specifier : member-specification [opt] */
24139 static void
24140 cp_parser_member_specification_opt (cp_parser* parser)
24142 while (true)
24144 cp_token *token;
24145 enum rid keyword;
24147 /* Peek at the next token. */
24148 token = cp_lexer_peek_token (parser->lexer);
24149 /* If it's a `}', or EOF then we've seen all the members. */
24150 if (token->type == CPP_CLOSE_BRACE
24151 || token->type == CPP_EOF
24152 || token->type == CPP_PRAGMA_EOL)
24153 break;
24155 /* See if this token is a keyword. */
24156 keyword = token->keyword;
24157 switch (keyword)
24159 case RID_PUBLIC:
24160 case RID_PROTECTED:
24161 case RID_PRIVATE:
24162 /* Consume the access-specifier. */
24163 cp_lexer_consume_token (parser->lexer);
24164 /* Remember which access-specifier is active. */
24165 current_access_specifier = token->u.value;
24166 /* Look for the `:'. */
24167 cp_parser_require (parser, CPP_COLON, RT_COLON);
24168 break;
24170 default:
24171 /* Accept #pragmas at class scope. */
24172 if (token->type == CPP_PRAGMA)
24174 cp_parser_pragma (parser, pragma_member, NULL);
24175 break;
24178 /* Otherwise, the next construction must be a
24179 member-declaration. */
24180 cp_parser_member_declaration (parser);
24185 /* Parse a member-declaration.
24187 member-declaration:
24188 decl-specifier-seq [opt] member-declarator-list [opt] ;
24189 function-definition ; [opt]
24190 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24191 using-declaration
24192 template-declaration
24193 alias-declaration
24195 member-declarator-list:
24196 member-declarator
24197 member-declarator-list , member-declarator
24199 member-declarator:
24200 declarator pure-specifier [opt]
24201 declarator constant-initializer [opt]
24202 identifier [opt] : constant-expression
24204 GNU Extensions:
24206 member-declaration:
24207 __extension__ member-declaration
24209 member-declarator:
24210 declarator attributes [opt] pure-specifier [opt]
24211 declarator attributes [opt] constant-initializer [opt]
24212 identifier [opt] attributes [opt] : constant-expression
24214 C++0x Extensions:
24216 member-declaration:
24217 static_assert-declaration */
24219 static void
24220 cp_parser_member_declaration (cp_parser* parser)
24222 cp_decl_specifier_seq decl_specifiers;
24223 tree prefix_attributes;
24224 tree decl;
24225 int declares_class_or_enum;
24226 bool friend_p;
24227 cp_token *token = NULL;
24228 cp_token *decl_spec_token_start = NULL;
24229 cp_token *initializer_token_start = NULL;
24230 int saved_pedantic;
24231 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24233 /* Check for the `__extension__' keyword. */
24234 if (cp_parser_extension_opt (parser, &saved_pedantic))
24236 /* Recurse. */
24237 cp_parser_member_declaration (parser);
24238 /* Restore the old value of the PEDANTIC flag. */
24239 pedantic = saved_pedantic;
24241 return;
24244 /* Check for a template-declaration. */
24245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24247 /* An explicit specialization here is an error condition, and we
24248 expect the specialization handler to detect and report this. */
24249 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24250 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24251 cp_parser_explicit_specialization (parser);
24252 else
24253 cp_parser_template_declaration (parser, /*member_p=*/true);
24255 return;
24257 /* Check for a template introduction. */
24258 else if (cp_parser_template_declaration_after_export (parser, true))
24259 return;
24261 /* Check for a using-declaration. */
24262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24264 if (cxx_dialect < cxx11)
24266 /* Parse the using-declaration. */
24267 cp_parser_using_declaration (parser,
24268 /*access_declaration_p=*/false);
24269 return;
24271 else
24273 tree decl;
24274 bool alias_decl_expected;
24275 cp_parser_parse_tentatively (parser);
24276 decl = cp_parser_alias_declaration (parser);
24277 /* Note that if we actually see the '=' token after the
24278 identifier, cp_parser_alias_declaration commits the
24279 tentative parse. In that case, we really expect an
24280 alias-declaration. Otherwise, we expect a using
24281 declaration. */
24282 alias_decl_expected =
24283 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24284 cp_parser_parse_definitely (parser);
24286 if (alias_decl_expected)
24287 finish_member_declaration (decl);
24288 else
24289 cp_parser_using_declaration (parser,
24290 /*access_declaration_p=*/false);
24291 return;
24295 /* Check for @defs. */
24296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24298 tree ivar, member;
24299 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24300 ivar = ivar_chains;
24301 while (ivar)
24303 member = ivar;
24304 ivar = TREE_CHAIN (member);
24305 TREE_CHAIN (member) = NULL_TREE;
24306 finish_member_declaration (member);
24308 return;
24311 /* If the next token is `static_assert' we have a static assertion. */
24312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24314 cp_parser_static_assert (parser, /*member_p=*/true);
24315 return;
24318 parser->colon_corrects_to_scope_p = false;
24320 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24321 goto out;
24323 /* Parse the decl-specifier-seq. */
24324 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24325 cp_parser_decl_specifier_seq (parser,
24326 (CP_PARSER_FLAGS_OPTIONAL
24327 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24328 &decl_specifiers,
24329 &declares_class_or_enum);
24330 /* Check for an invalid type-name. */
24331 if (!decl_specifiers.any_type_specifiers_p
24332 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24333 goto out;
24334 /* If there is no declarator, then the decl-specifier-seq should
24335 specify a type. */
24336 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24338 /* If there was no decl-specifier-seq, and the next token is a
24339 `;', then we have something like:
24341 struct S { ; };
24343 [class.mem]
24345 Each member-declaration shall declare at least one member
24346 name of the class. */
24347 if (!decl_specifiers.any_specifiers_p)
24349 cp_token *token = cp_lexer_peek_token (parser->lexer);
24350 if (!in_system_header_at (token->location))
24352 gcc_rich_location richloc (token->location);
24353 richloc.add_fixit_remove ();
24354 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24357 else
24359 tree type;
24361 /* See if this declaration is a friend. */
24362 friend_p = cp_parser_friend_p (&decl_specifiers);
24363 /* If there were decl-specifiers, check to see if there was
24364 a class-declaration. */
24365 type = check_tag_decl (&decl_specifiers,
24366 /*explicit_type_instantiation_p=*/false);
24367 /* Nested classes have already been added to the class, but
24368 a `friend' needs to be explicitly registered. */
24369 if (friend_p)
24371 /* If the `friend' keyword was present, the friend must
24372 be introduced with a class-key. */
24373 if (!declares_class_or_enum && cxx_dialect < cxx11)
24374 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24375 "in C++03 a class-key must be used "
24376 "when declaring a friend");
24377 /* In this case:
24379 template <typename T> struct A {
24380 friend struct A<T>::B;
24383 A<T>::B will be represented by a TYPENAME_TYPE, and
24384 therefore not recognized by check_tag_decl. */
24385 if (!type)
24387 type = decl_specifiers.type;
24388 if (type && TREE_CODE (type) == TYPE_DECL)
24389 type = TREE_TYPE (type);
24391 if (!type || !TYPE_P (type))
24392 error_at (decl_spec_token_start->location,
24393 "friend declaration does not name a class or "
24394 "function");
24395 else
24396 make_friend_class (current_class_type, type,
24397 /*complain=*/true);
24399 /* If there is no TYPE, an error message will already have
24400 been issued. */
24401 else if (!type || type == error_mark_node)
24403 /* An anonymous aggregate has to be handled specially; such
24404 a declaration really declares a data member (with a
24405 particular type), as opposed to a nested class. */
24406 else if (ANON_AGGR_TYPE_P (type))
24408 /* C++11 9.5/6. */
24409 if (decl_specifiers.storage_class != sc_none)
24410 error_at (decl_spec_token_start->location,
24411 "a storage class on an anonymous aggregate "
24412 "in class scope is not allowed");
24414 /* Remove constructors and such from TYPE, now that we
24415 know it is an anonymous aggregate. */
24416 fixup_anonymous_aggr (type);
24417 /* And make the corresponding data member. */
24418 decl = build_decl (decl_spec_token_start->location,
24419 FIELD_DECL, NULL_TREE, type);
24420 /* Add it to the class. */
24421 finish_member_declaration (decl);
24423 else
24424 cp_parser_check_access_in_redeclaration
24425 (TYPE_NAME (type),
24426 decl_spec_token_start->location);
24429 else
24431 bool assume_semicolon = false;
24433 /* Clear attributes from the decl_specifiers but keep them
24434 around as prefix attributes that apply them to the entity
24435 being declared. */
24436 prefix_attributes = decl_specifiers.attributes;
24437 decl_specifiers.attributes = NULL_TREE;
24439 /* See if these declarations will be friends. */
24440 friend_p = cp_parser_friend_p (&decl_specifiers);
24442 /* Keep going until we hit the `;' at the end of the
24443 declaration. */
24444 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24446 tree attributes = NULL_TREE;
24447 tree first_attribute;
24448 tree initializer;
24449 bool named_bitfld = false;
24451 /* Peek at the next token. */
24452 token = cp_lexer_peek_token (parser->lexer);
24454 /* The following code wants to know early if it is a bit-field
24455 or some other declaration. Attributes can appear before
24456 the `:' token. Skip over them without consuming any tokens
24457 to peek if they are followed by `:'. */
24458 if (cp_next_tokens_can_be_attribute_p (parser)
24459 || (token->type == CPP_NAME
24460 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24461 && (named_bitfld = true)))
24463 size_t n
24464 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24465 token = cp_lexer_peek_nth_token (parser->lexer, n);
24468 /* Check for a bitfield declaration. */
24469 if (token->type == CPP_COLON
24470 || (token->type == CPP_NAME
24471 && token == cp_lexer_peek_token (parser->lexer)
24472 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24473 && (named_bitfld = true)))
24475 tree identifier;
24476 tree width;
24477 tree late_attributes = NULL_TREE;
24478 location_t id_location
24479 = cp_lexer_peek_token (parser->lexer)->location;
24481 if (named_bitfld)
24482 identifier = cp_parser_identifier (parser);
24483 else
24484 identifier = NULL_TREE;
24486 /* Look for attributes that apply to the bitfield. */
24487 attributes = cp_parser_attributes_opt (parser);
24489 /* Consume the `:' token. */
24490 cp_lexer_consume_token (parser->lexer);
24492 /* Get the width of the bitfield. */
24493 width = cp_parser_constant_expression (parser, false, NULL,
24494 cxx_dialect >= cxx11);
24496 /* In C++2A and as extension for C++11 and above we allow
24497 default member initializers for bit-fields. */
24498 initializer = NULL_TREE;
24499 if (cxx_dialect >= cxx11
24500 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24501 || cp_lexer_next_token_is (parser->lexer,
24502 CPP_OPEN_BRACE)))
24504 location_t loc
24505 = cp_lexer_peek_token (parser->lexer)->location;
24506 if (cxx_dialect < cxx2a
24507 && !in_system_header_at (loc)
24508 && identifier != NULL_TREE)
24509 pedwarn (loc, 0,
24510 "default member initializers for bit-fields "
24511 "only available with -std=c++2a or "
24512 "-std=gnu++2a");
24514 initializer = cp_parser_save_nsdmi (parser);
24515 if (identifier == NULL_TREE)
24517 error_at (loc, "default member initializer for "
24518 "unnamed bit-field");
24519 initializer = NULL_TREE;
24522 else
24524 /* Look for attributes that apply to the bitfield after
24525 the `:' token and width. This is where GCC used to
24526 parse attributes in the past, pedwarn if there is
24527 a std attribute. */
24528 if (cp_next_tokens_can_be_std_attribute_p (parser))
24529 pedwarn (input_location, OPT_Wpedantic,
24530 "ISO C++ allows bit-field attributes only "
24531 "before the %<:%> token");
24533 late_attributes = cp_parser_attributes_opt (parser);
24536 attributes = attr_chainon (attributes, late_attributes);
24538 /* Remember which attributes are prefix attributes and
24539 which are not. */
24540 first_attribute = attributes;
24541 /* Combine the attributes. */
24542 attributes = attr_chainon (prefix_attributes, attributes);
24544 /* Create the bitfield declaration. */
24545 decl = grokbitfield (identifier
24546 ? make_id_declarator (NULL_TREE,
24547 identifier,
24548 sfk_none,
24549 id_location)
24550 : NULL,
24551 &decl_specifiers,
24552 width, initializer,
24553 attributes);
24555 else
24557 cp_declarator *declarator;
24558 tree asm_specification;
24559 int ctor_dtor_or_conv_p;
24560 bool static_p = (decl_specifiers.storage_class == sc_static);
24562 /* Parse the declarator. */
24563 declarator
24564 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24565 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24566 &ctor_dtor_or_conv_p,
24567 /*parenthesized_p=*/NULL,
24568 /*member_p=*/true,
24569 friend_p, static_p);
24571 /* If something went wrong parsing the declarator, make sure
24572 that we at least consume some tokens. */
24573 if (declarator == cp_error_declarator)
24575 /* Skip to the end of the statement. */
24576 cp_parser_skip_to_end_of_statement (parser);
24577 /* If the next token is not a semicolon, that is
24578 probably because we just skipped over the body of
24579 a function. So, we consume a semicolon if
24580 present, but do not issue an error message if it
24581 is not present. */
24582 if (cp_lexer_next_token_is (parser->lexer,
24583 CPP_SEMICOLON))
24584 cp_lexer_consume_token (parser->lexer);
24585 goto out;
24588 if (declares_class_or_enum & 2)
24589 cp_parser_check_for_definition_in_return_type
24590 (declarator, decl_specifiers.type,
24591 decl_specifiers.locations[ds_type_spec]);
24593 /* Look for an asm-specification. */
24594 asm_specification = cp_parser_asm_specification_opt (parser);
24595 /* Look for attributes that apply to the declaration. */
24596 attributes = cp_parser_attributes_opt (parser);
24597 /* Remember which attributes are prefix attributes and
24598 which are not. */
24599 first_attribute = attributes;
24600 /* Combine the attributes. */
24601 attributes = attr_chainon (prefix_attributes, attributes);
24603 /* If it's an `=', then we have a constant-initializer or a
24604 pure-specifier. It is not correct to parse the
24605 initializer before registering the member declaration
24606 since the member declaration should be in scope while
24607 its initializer is processed. However, the rest of the
24608 front end does not yet provide an interface that allows
24609 us to handle this correctly. */
24610 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24612 /* In [class.mem]:
24614 A pure-specifier shall be used only in the declaration of
24615 a virtual function.
24617 A member-declarator can contain a constant-initializer
24618 only if it declares a static member of integral or
24619 enumeration type.
24621 Therefore, if the DECLARATOR is for a function, we look
24622 for a pure-specifier; otherwise, we look for a
24623 constant-initializer. When we call `grokfield', it will
24624 perform more stringent semantics checks. */
24625 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24626 if (function_declarator_p (declarator)
24627 || (decl_specifiers.type
24628 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24629 && declarator->kind == cdk_id
24630 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24631 == FUNCTION_TYPE)))
24632 initializer = cp_parser_pure_specifier (parser);
24633 else if (decl_specifiers.storage_class != sc_static)
24634 initializer = cp_parser_save_nsdmi (parser);
24635 else if (cxx_dialect >= cxx11)
24637 bool nonconst;
24638 /* Don't require a constant rvalue in C++11, since we
24639 might want a reference constant. We'll enforce
24640 constancy later. */
24641 cp_lexer_consume_token (parser->lexer);
24642 /* Parse the initializer. */
24643 initializer = cp_parser_initializer_clause (parser,
24644 &nonconst);
24646 else
24647 /* Parse the initializer. */
24648 initializer = cp_parser_constant_initializer (parser);
24650 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24651 && !function_declarator_p (declarator))
24653 bool x;
24654 if (decl_specifiers.storage_class != sc_static)
24655 initializer = cp_parser_save_nsdmi (parser);
24656 else
24657 initializer = cp_parser_initializer (parser, &x, &x);
24659 /* Otherwise, there is no initializer. */
24660 else
24661 initializer = NULL_TREE;
24663 /* See if we are probably looking at a function
24664 definition. We are certainly not looking at a
24665 member-declarator. Calling `grokfield' has
24666 side-effects, so we must not do it unless we are sure
24667 that we are looking at a member-declarator. */
24668 if (cp_parser_token_starts_function_definition_p
24669 (cp_lexer_peek_token (parser->lexer)))
24671 /* The grammar does not allow a pure-specifier to be
24672 used when a member function is defined. (It is
24673 possible that this fact is an oversight in the
24674 standard, since a pure function may be defined
24675 outside of the class-specifier. */
24676 if (initializer && initializer_token_start)
24677 error_at (initializer_token_start->location,
24678 "pure-specifier on function-definition");
24679 decl = cp_parser_save_member_function_body (parser,
24680 &decl_specifiers,
24681 declarator,
24682 attributes);
24683 if (parser->fully_implicit_function_template_p)
24684 decl = finish_fully_implicit_template (parser, decl);
24685 /* If the member was not a friend, declare it here. */
24686 if (!friend_p)
24687 finish_member_declaration (decl);
24688 /* Peek at the next token. */
24689 token = cp_lexer_peek_token (parser->lexer);
24690 /* If the next token is a semicolon, consume it. */
24691 if (token->type == CPP_SEMICOLON)
24693 location_t semicolon_loc
24694 = cp_lexer_consume_token (parser->lexer)->location;
24695 gcc_rich_location richloc (semicolon_loc);
24696 richloc.add_fixit_remove ();
24697 warning_at (&richloc, OPT_Wextra_semi,
24698 "extra %<;%> after in-class "
24699 "function definition");
24701 goto out;
24703 else
24704 if (declarator->kind == cdk_function)
24705 declarator->id_loc = token->location;
24706 /* Create the declaration. */
24707 decl = grokfield (declarator, &decl_specifiers,
24708 initializer, /*init_const_expr_p=*/true,
24709 asm_specification, attributes);
24710 if (parser->fully_implicit_function_template_p)
24712 if (friend_p)
24713 finish_fully_implicit_template (parser, 0);
24714 else
24715 decl = finish_fully_implicit_template (parser, decl);
24719 cp_finalize_omp_declare_simd (parser, decl);
24720 cp_finalize_oacc_routine (parser, decl, false);
24722 /* Reset PREFIX_ATTRIBUTES. */
24723 if (attributes != error_mark_node)
24725 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24726 attributes = TREE_CHAIN (attributes);
24727 if (attributes)
24728 TREE_CHAIN (attributes) = NULL_TREE;
24731 /* If there is any qualification still in effect, clear it
24732 now; we will be starting fresh with the next declarator. */
24733 parser->scope = NULL_TREE;
24734 parser->qualifying_scope = NULL_TREE;
24735 parser->object_scope = NULL_TREE;
24736 /* If it's a `,', then there are more declarators. */
24737 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24739 cp_lexer_consume_token (parser->lexer);
24740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24742 cp_token *token = cp_lexer_previous_token (parser->lexer);
24743 gcc_rich_location richloc (token->location);
24744 richloc.add_fixit_remove ();
24745 error_at (&richloc, "stray %<,%> at end of "
24746 "member declaration");
24749 /* If the next token isn't a `;', then we have a parse error. */
24750 else if (cp_lexer_next_token_is_not (parser->lexer,
24751 CPP_SEMICOLON))
24753 /* The next token might be a ways away from where the
24754 actual semicolon is missing. Find the previous token
24755 and use that for our error position. */
24756 cp_token *token = cp_lexer_previous_token (parser->lexer);
24757 gcc_rich_location richloc (token->location);
24758 richloc.add_fixit_insert_after (";");
24759 error_at (&richloc, "expected %<;%> at end of "
24760 "member declaration");
24762 /* Assume that the user meant to provide a semicolon. If
24763 we were to cp_parser_skip_to_end_of_statement, we might
24764 skip to a semicolon inside a member function definition
24765 and issue nonsensical error messages. */
24766 assume_semicolon = true;
24769 if (decl)
24771 /* Add DECL to the list of members. */
24772 if (!friend_p
24773 /* Explicitly include, eg, NSDMIs, for better error
24774 recovery (c++/58650). */
24775 || !DECL_DECLARES_FUNCTION_P (decl))
24776 finish_member_declaration (decl);
24778 if (TREE_CODE (decl) == FUNCTION_DECL)
24779 cp_parser_save_default_args (parser, decl);
24780 else if (TREE_CODE (decl) == FIELD_DECL
24781 && DECL_INITIAL (decl))
24782 /* Add DECL to the queue of NSDMI to be parsed later. */
24783 vec_safe_push (unparsed_nsdmis, decl);
24786 if (assume_semicolon)
24787 goto out;
24791 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24792 out:
24793 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24796 /* Parse a pure-specifier.
24798 pure-specifier:
24801 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24802 Otherwise, ERROR_MARK_NODE is returned. */
24804 static tree
24805 cp_parser_pure_specifier (cp_parser* parser)
24807 cp_token *token;
24809 /* Look for the `=' token. */
24810 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24811 return error_mark_node;
24812 /* Look for the `0' token. */
24813 token = cp_lexer_peek_token (parser->lexer);
24815 if (token->type == CPP_EOF
24816 || token->type == CPP_PRAGMA_EOL)
24817 return error_mark_node;
24819 cp_lexer_consume_token (parser->lexer);
24821 /* Accept = default or = delete in c++0x mode. */
24822 if (token->keyword == RID_DEFAULT
24823 || token->keyword == RID_DELETE)
24825 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24826 return token->u.value;
24829 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24830 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24832 cp_parser_error (parser,
24833 "invalid pure specifier (only %<= 0%> is allowed)");
24834 cp_parser_skip_to_end_of_statement (parser);
24835 return error_mark_node;
24837 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24839 error_at (token->location, "templates may not be %<virtual%>");
24840 return error_mark_node;
24843 return integer_zero_node;
24846 /* Parse a constant-initializer.
24848 constant-initializer:
24849 = constant-expression
24851 Returns a representation of the constant-expression. */
24853 static tree
24854 cp_parser_constant_initializer (cp_parser* parser)
24856 /* Look for the `=' token. */
24857 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24858 return error_mark_node;
24860 /* It is invalid to write:
24862 struct S { static const int i = { 7 }; };
24865 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24867 cp_parser_error (parser,
24868 "a brace-enclosed initializer is not allowed here");
24869 /* Consume the opening brace. */
24870 matching_braces braces;
24871 braces.consume_open (parser);
24872 /* Skip the initializer. */
24873 cp_parser_skip_to_closing_brace (parser);
24874 /* Look for the trailing `}'. */
24875 braces.require_close (parser);
24877 return error_mark_node;
24880 return cp_parser_constant_expression (parser);
24883 /* Derived classes [gram.class.derived] */
24885 /* Parse a base-clause.
24887 base-clause:
24888 : base-specifier-list
24890 base-specifier-list:
24891 base-specifier ... [opt]
24892 base-specifier-list , base-specifier ... [opt]
24894 Returns a TREE_LIST representing the base-classes, in the order in
24895 which they were declared. The representation of each node is as
24896 described by cp_parser_base_specifier.
24898 In the case that no bases are specified, this function will return
24899 NULL_TREE, not ERROR_MARK_NODE. */
24901 static tree
24902 cp_parser_base_clause (cp_parser* parser)
24904 tree bases = NULL_TREE;
24906 /* Look for the `:' that begins the list. */
24907 cp_parser_require (parser, CPP_COLON, RT_COLON);
24909 /* Scan the base-specifier-list. */
24910 while (true)
24912 cp_token *token;
24913 tree base;
24914 bool pack_expansion_p = false;
24916 /* Look for the base-specifier. */
24917 base = cp_parser_base_specifier (parser);
24918 /* Look for the (optional) ellipsis. */
24919 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24921 /* Consume the `...'. */
24922 cp_lexer_consume_token (parser->lexer);
24924 pack_expansion_p = true;
24927 /* Add BASE to the front of the list. */
24928 if (base && base != error_mark_node)
24930 if (pack_expansion_p)
24931 /* Make this a pack expansion type. */
24932 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24934 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24936 TREE_CHAIN (base) = bases;
24937 bases = base;
24940 /* Peek at the next token. */
24941 token = cp_lexer_peek_token (parser->lexer);
24942 /* If it's not a comma, then the list is complete. */
24943 if (token->type != CPP_COMMA)
24944 break;
24945 /* Consume the `,'. */
24946 cp_lexer_consume_token (parser->lexer);
24949 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24950 base class had a qualified name. However, the next name that
24951 appears is certainly not qualified. */
24952 parser->scope = NULL_TREE;
24953 parser->qualifying_scope = NULL_TREE;
24954 parser->object_scope = NULL_TREE;
24956 return nreverse (bases);
24959 /* Parse a base-specifier.
24961 base-specifier:
24962 :: [opt] nested-name-specifier [opt] class-name
24963 virtual access-specifier [opt] :: [opt] nested-name-specifier
24964 [opt] class-name
24965 access-specifier virtual [opt] :: [opt] nested-name-specifier
24966 [opt] class-name
24968 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24969 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24970 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24971 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24973 static tree
24974 cp_parser_base_specifier (cp_parser* parser)
24976 cp_token *token;
24977 bool done = false;
24978 bool virtual_p = false;
24979 bool duplicate_virtual_error_issued_p = false;
24980 bool duplicate_access_error_issued_p = false;
24981 bool class_scope_p, template_p;
24982 tree access = access_default_node;
24983 tree type;
24985 /* Process the optional `virtual' and `access-specifier'. */
24986 while (!done)
24988 /* Peek at the next token. */
24989 token = cp_lexer_peek_token (parser->lexer);
24990 /* Process `virtual'. */
24991 switch (token->keyword)
24993 case RID_VIRTUAL:
24994 /* If `virtual' appears more than once, issue an error. */
24995 if (virtual_p && !duplicate_virtual_error_issued_p)
24997 cp_parser_error (parser,
24998 "%<virtual%> specified more than once in base-specifier");
24999 duplicate_virtual_error_issued_p = true;
25002 virtual_p = true;
25004 /* Consume the `virtual' token. */
25005 cp_lexer_consume_token (parser->lexer);
25007 break;
25009 case RID_PUBLIC:
25010 case RID_PROTECTED:
25011 case RID_PRIVATE:
25012 /* If more than one access specifier appears, issue an
25013 error. */
25014 if (access != access_default_node
25015 && !duplicate_access_error_issued_p)
25017 cp_parser_error (parser,
25018 "more than one access specifier in base-specifier");
25019 duplicate_access_error_issued_p = true;
25022 access = ridpointers[(int) token->keyword];
25024 /* Consume the access-specifier. */
25025 cp_lexer_consume_token (parser->lexer);
25027 break;
25029 default:
25030 done = true;
25031 break;
25034 /* It is not uncommon to see programs mechanically, erroneously, use
25035 the 'typename' keyword to denote (dependent) qualified types
25036 as base classes. */
25037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25039 token = cp_lexer_peek_token (parser->lexer);
25040 if (!processing_template_decl)
25041 error_at (token->location,
25042 "keyword %<typename%> not allowed outside of templates");
25043 else
25044 error_at (token->location,
25045 "keyword %<typename%> not allowed in this context "
25046 "(the base class is implicitly a type)");
25047 cp_lexer_consume_token (parser->lexer);
25050 /* Look for the optional `::' operator. */
25051 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25052 /* Look for the nested-name-specifier. The simplest way to
25053 implement:
25055 [temp.res]
25057 The keyword `typename' is not permitted in a base-specifier or
25058 mem-initializer; in these contexts a qualified name that
25059 depends on a template-parameter is implicitly assumed to be a
25060 type name.
25062 is to pretend that we have seen the `typename' keyword at this
25063 point. */
25064 cp_parser_nested_name_specifier_opt (parser,
25065 /*typename_keyword_p=*/true,
25066 /*check_dependency_p=*/true,
25067 /*type_p=*/true,
25068 /*is_declaration=*/true);
25069 /* If the base class is given by a qualified name, assume that names
25070 we see are type names or templates, as appropriate. */
25071 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25072 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25074 if (!parser->scope
25075 && cp_lexer_next_token_is_decltype (parser->lexer))
25076 /* DR 950 allows decltype as a base-specifier. */
25077 type = cp_parser_decltype (parser);
25078 else
25080 /* Otherwise, look for the class-name. */
25081 type = cp_parser_class_name (parser,
25082 class_scope_p,
25083 template_p,
25084 typename_type,
25085 /*check_dependency_p=*/true,
25086 /*class_head_p=*/false,
25087 /*is_declaration=*/true);
25088 type = TREE_TYPE (type);
25091 if (type == error_mark_node)
25092 return error_mark_node;
25094 return finish_base_specifier (type, access, virtual_p);
25097 /* Exception handling [gram.exception] */
25099 /* Parse an (optional) noexcept-specification.
25101 noexcept-specification:
25102 noexcept ( constant-expression ) [opt]
25104 If no noexcept-specification is present, returns NULL_TREE.
25105 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25106 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25107 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25108 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25109 in which case a boolean condition is returned instead. */
25111 static tree
25112 cp_parser_noexcept_specification_opt (cp_parser* parser,
25113 bool require_constexpr,
25114 bool* consumed_expr,
25115 bool return_cond)
25117 cp_token *token;
25118 const char *saved_message;
25120 /* Peek at the next token. */
25121 token = cp_lexer_peek_token (parser->lexer);
25123 /* Is it a noexcept-specification? */
25124 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25126 tree expr;
25127 cp_lexer_consume_token (parser->lexer);
25129 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25131 matching_parens parens;
25132 parens.consume_open (parser);
25134 tree save_ccp = current_class_ptr;
25135 tree save_ccr = current_class_ref;
25137 if (current_class_type)
25138 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25140 if (require_constexpr)
25142 /* Types may not be defined in an exception-specification. */
25143 saved_message = parser->type_definition_forbidden_message;
25144 parser->type_definition_forbidden_message
25145 = G_("types may not be defined in an exception-specification");
25147 bool non_constant_p;
25148 expr
25149 = cp_parser_constant_expression (parser,
25150 /*allow_non_constant=*/true,
25151 &non_constant_p);
25152 if (non_constant_p
25153 && !require_potential_rvalue_constant_expression (expr))
25155 expr = NULL_TREE;
25156 return_cond = true;
25159 /* Restore the saved message. */
25160 parser->type_definition_forbidden_message = saved_message;
25162 else
25164 expr = cp_parser_expression (parser);
25165 *consumed_expr = true;
25168 parens.require_close (parser);
25170 current_class_ptr = save_ccp;
25171 current_class_ref = save_ccr;
25173 else
25175 expr = boolean_true_node;
25176 if (!require_constexpr)
25177 *consumed_expr = false;
25180 /* We cannot build a noexcept-spec right away because this will check
25181 that expr is a constexpr. */
25182 if (!return_cond)
25183 return build_noexcept_spec (expr, tf_warning_or_error);
25184 else
25185 return expr;
25187 else
25188 return NULL_TREE;
25191 /* Parse an (optional) exception-specification.
25193 exception-specification:
25194 throw ( type-id-list [opt] )
25196 Returns a TREE_LIST representing the exception-specification. The
25197 TREE_VALUE of each node is a type. */
25199 static tree
25200 cp_parser_exception_specification_opt (cp_parser* parser)
25202 cp_token *token;
25203 tree type_id_list;
25204 const char *saved_message;
25206 /* Peek at the next token. */
25207 token = cp_lexer_peek_token (parser->lexer);
25209 /* Is it a noexcept-specification? */
25210 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25211 false);
25212 if (type_id_list != NULL_TREE)
25213 return type_id_list;
25215 /* If it's not `throw', then there's no exception-specification. */
25216 if (!cp_parser_is_keyword (token, RID_THROW))
25217 return NULL_TREE;
25219 location_t loc = token->location;
25221 /* Consume the `throw'. */
25222 cp_lexer_consume_token (parser->lexer);
25224 /* Look for the `('. */
25225 matching_parens parens;
25226 parens.require_open (parser);
25228 /* Peek at the next token. */
25229 token = cp_lexer_peek_token (parser->lexer);
25230 /* If it's not a `)', then there is a type-id-list. */
25231 if (token->type != CPP_CLOSE_PAREN)
25233 /* Types may not be defined in an exception-specification. */
25234 saved_message = parser->type_definition_forbidden_message;
25235 parser->type_definition_forbidden_message
25236 = G_("types may not be defined in an exception-specification");
25237 /* Parse the type-id-list. */
25238 type_id_list = cp_parser_type_id_list (parser);
25239 /* Restore the saved message. */
25240 parser->type_definition_forbidden_message = saved_message;
25242 if (cxx_dialect >= cxx17)
25244 error_at (loc, "ISO C++17 does not allow dynamic exception "
25245 "specifications");
25246 type_id_list = NULL_TREE;
25248 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25249 warning_at (loc, OPT_Wdeprecated,
25250 "dynamic exception specifications are deprecated in "
25251 "C++11");
25253 /* In C++17, throw() is equivalent to noexcept (true). throw()
25254 is deprecated in C++11 and above as well, but is still widely used,
25255 so don't warn about it yet. */
25256 else if (cxx_dialect >= cxx17)
25257 type_id_list = noexcept_true_spec;
25258 else
25259 type_id_list = empty_except_spec;
25261 /* Look for the `)'. */
25262 parens.require_close (parser);
25264 return type_id_list;
25267 /* Parse an (optional) type-id-list.
25269 type-id-list:
25270 type-id ... [opt]
25271 type-id-list , type-id ... [opt]
25273 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25274 in the order that the types were presented. */
25276 static tree
25277 cp_parser_type_id_list (cp_parser* parser)
25279 tree types = NULL_TREE;
25281 while (true)
25283 cp_token *token;
25284 tree type;
25286 token = cp_lexer_peek_token (parser->lexer);
25288 /* Get the next type-id. */
25289 type = cp_parser_type_id (parser);
25290 /* Check for invalid 'auto'. */
25291 if (flag_concepts && type_uses_auto (type))
25293 error_at (token->location,
25294 "invalid use of %<auto%> in exception-specification");
25295 type = error_mark_node;
25297 /* Parse the optional ellipsis. */
25298 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25300 /* Consume the `...'. */
25301 cp_lexer_consume_token (parser->lexer);
25303 /* Turn the type into a pack expansion expression. */
25304 type = make_pack_expansion (type);
25306 /* Add it to the list. */
25307 types = add_exception_specifier (types, type, /*complain=*/1);
25308 /* Peek at the next token. */
25309 token = cp_lexer_peek_token (parser->lexer);
25310 /* If it is not a `,', we are done. */
25311 if (token->type != CPP_COMMA)
25312 break;
25313 /* Consume the `,'. */
25314 cp_lexer_consume_token (parser->lexer);
25317 return nreverse (types);
25320 /* Parse a try-block.
25322 try-block:
25323 try compound-statement handler-seq */
25325 static tree
25326 cp_parser_try_block (cp_parser* parser)
25328 tree try_block;
25330 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25331 if (parser->in_function_body
25332 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
25333 error ("%<try%> in %<constexpr%> function");
25335 try_block = begin_try_block ();
25336 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25337 finish_try_block (try_block);
25338 cp_parser_handler_seq (parser);
25339 finish_handler_sequence (try_block);
25341 return try_block;
25344 /* Parse a function-try-block.
25346 function-try-block:
25347 try ctor-initializer [opt] function-body handler-seq */
25349 static void
25350 cp_parser_function_try_block (cp_parser* parser)
25352 tree compound_stmt;
25353 tree try_block;
25355 /* Look for the `try' keyword. */
25356 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25357 return;
25358 /* Let the rest of the front end know where we are. */
25359 try_block = begin_function_try_block (&compound_stmt);
25360 /* Parse the function-body. */
25361 cp_parser_ctor_initializer_opt_and_function_body
25362 (parser, /*in_function_try_block=*/true);
25363 /* We're done with the `try' part. */
25364 finish_function_try_block (try_block);
25365 /* Parse the handlers. */
25366 cp_parser_handler_seq (parser);
25367 /* We're done with the handlers. */
25368 finish_function_handler_sequence (try_block, compound_stmt);
25371 /* Parse a handler-seq.
25373 handler-seq:
25374 handler handler-seq [opt] */
25376 static void
25377 cp_parser_handler_seq (cp_parser* parser)
25379 while (true)
25381 cp_token *token;
25383 /* Parse the handler. */
25384 cp_parser_handler (parser);
25385 /* Peek at the next token. */
25386 token = cp_lexer_peek_token (parser->lexer);
25387 /* If it's not `catch' then there are no more handlers. */
25388 if (!cp_parser_is_keyword (token, RID_CATCH))
25389 break;
25393 /* Parse a handler.
25395 handler:
25396 catch ( exception-declaration ) compound-statement */
25398 static void
25399 cp_parser_handler (cp_parser* parser)
25401 tree handler;
25402 tree declaration;
25404 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25405 handler = begin_handler ();
25406 matching_parens parens;
25407 parens.require_open (parser);
25408 declaration = cp_parser_exception_declaration (parser);
25409 finish_handler_parms (declaration, handler);
25410 parens.require_close (parser);
25411 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25412 finish_handler (handler);
25415 /* Parse an exception-declaration.
25417 exception-declaration:
25418 type-specifier-seq declarator
25419 type-specifier-seq abstract-declarator
25420 type-specifier-seq
25423 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25424 ellipsis variant is used. */
25426 static tree
25427 cp_parser_exception_declaration (cp_parser* parser)
25429 cp_decl_specifier_seq type_specifiers;
25430 cp_declarator *declarator;
25431 const char *saved_message;
25433 /* If it's an ellipsis, it's easy to handle. */
25434 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25436 /* Consume the `...' token. */
25437 cp_lexer_consume_token (parser->lexer);
25438 return NULL_TREE;
25441 /* Types may not be defined in exception-declarations. */
25442 saved_message = parser->type_definition_forbidden_message;
25443 parser->type_definition_forbidden_message
25444 = G_("types may not be defined in exception-declarations");
25446 /* Parse the type-specifier-seq. */
25447 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25448 /*is_declaration=*/true,
25449 /*is_trailing_return=*/false,
25450 &type_specifiers);
25451 /* If it's a `)', then there is no declarator. */
25452 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25453 declarator = NULL;
25454 else
25455 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25456 CP_PARSER_FLAGS_NONE,
25457 /*ctor_dtor_or_conv_p=*/NULL,
25458 /*parenthesized_p=*/NULL,
25459 /*member_p=*/false,
25460 /*friend_p=*/false,
25461 /*static_p=*/false);
25463 /* Restore the saved message. */
25464 parser->type_definition_forbidden_message = saved_message;
25466 if (!type_specifiers.any_specifiers_p)
25467 return error_mark_node;
25469 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25472 /* Parse a throw-expression.
25474 throw-expression:
25475 throw assignment-expression [opt]
25477 Returns a THROW_EXPR representing the throw-expression. */
25479 static tree
25480 cp_parser_throw_expression (cp_parser* parser)
25482 tree expression;
25483 cp_token* token;
25485 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25486 token = cp_lexer_peek_token (parser->lexer);
25487 /* Figure out whether or not there is an assignment-expression
25488 following the "throw" keyword. */
25489 if (token->type == CPP_COMMA
25490 || token->type == CPP_SEMICOLON
25491 || token->type == CPP_CLOSE_PAREN
25492 || token->type == CPP_CLOSE_SQUARE
25493 || token->type == CPP_CLOSE_BRACE
25494 || token->type == CPP_COLON)
25495 expression = NULL_TREE;
25496 else
25497 expression = cp_parser_assignment_expression (parser);
25499 return build_throw (expression);
25502 /* GNU Extensions */
25504 /* Parse an (optional) asm-specification.
25506 asm-specification:
25507 asm ( string-literal )
25509 If the asm-specification is present, returns a STRING_CST
25510 corresponding to the string-literal. Otherwise, returns
25511 NULL_TREE. */
25513 static tree
25514 cp_parser_asm_specification_opt (cp_parser* parser)
25516 cp_token *token;
25517 tree asm_specification;
25519 /* Peek at the next token. */
25520 token = cp_lexer_peek_token (parser->lexer);
25521 /* If the next token isn't the `asm' keyword, then there's no
25522 asm-specification. */
25523 if (!cp_parser_is_keyword (token, RID_ASM))
25524 return NULL_TREE;
25526 /* Consume the `asm' token. */
25527 cp_lexer_consume_token (parser->lexer);
25528 /* Look for the `('. */
25529 matching_parens parens;
25530 parens.require_open (parser);
25532 /* Look for the string-literal. */
25533 asm_specification = cp_parser_string_literal (parser, false, false);
25535 /* Look for the `)'. */
25536 parens.require_close (parser);
25538 return asm_specification;
25541 /* Parse an asm-operand-list.
25543 asm-operand-list:
25544 asm-operand
25545 asm-operand-list , asm-operand
25547 asm-operand:
25548 string-literal ( expression )
25549 [ string-literal ] string-literal ( expression )
25551 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25552 each node is the expression. The TREE_PURPOSE is itself a
25553 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25554 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25555 is a STRING_CST for the string literal before the parenthesis. Returns
25556 ERROR_MARK_NODE if any of the operands are invalid. */
25558 static tree
25559 cp_parser_asm_operand_list (cp_parser* parser)
25561 tree asm_operands = NULL_TREE;
25562 bool invalid_operands = false;
25564 while (true)
25566 tree string_literal;
25567 tree expression;
25568 tree name;
25570 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25572 /* Consume the `[' token. */
25573 cp_lexer_consume_token (parser->lexer);
25574 /* Read the operand name. */
25575 name = cp_parser_identifier (parser);
25576 if (name != error_mark_node)
25577 name = build_string (IDENTIFIER_LENGTH (name),
25578 IDENTIFIER_POINTER (name));
25579 /* Look for the closing `]'. */
25580 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25582 else
25583 name = NULL_TREE;
25584 /* Look for the string-literal. */
25585 string_literal = cp_parser_string_literal (parser, false, false);
25587 /* Look for the `('. */
25588 matching_parens parens;
25589 parens.require_open (parser);
25590 /* Parse the expression. */
25591 expression = cp_parser_expression (parser);
25592 /* Look for the `)'. */
25593 parens.require_close (parser);
25595 if (name == error_mark_node
25596 || string_literal == error_mark_node
25597 || expression == error_mark_node)
25598 invalid_operands = true;
25600 /* Add this operand to the list. */
25601 asm_operands = tree_cons (build_tree_list (name, string_literal),
25602 expression,
25603 asm_operands);
25604 /* If the next token is not a `,', there are no more
25605 operands. */
25606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25607 break;
25608 /* Consume the `,'. */
25609 cp_lexer_consume_token (parser->lexer);
25612 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25615 /* Parse an asm-clobber-list.
25617 asm-clobber-list:
25618 string-literal
25619 asm-clobber-list , string-literal
25621 Returns a TREE_LIST, indicating the clobbers in the order that they
25622 appeared. The TREE_VALUE of each node is a STRING_CST. */
25624 static tree
25625 cp_parser_asm_clobber_list (cp_parser* parser)
25627 tree clobbers = NULL_TREE;
25629 while (true)
25631 tree string_literal;
25633 /* Look for the string literal. */
25634 string_literal = cp_parser_string_literal (parser, false, false);
25635 /* Add it to the list. */
25636 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25637 /* If the next token is not a `,', then the list is
25638 complete. */
25639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25640 break;
25641 /* Consume the `,' token. */
25642 cp_lexer_consume_token (parser->lexer);
25645 return clobbers;
25648 /* Parse an asm-label-list.
25650 asm-label-list:
25651 identifier
25652 asm-label-list , identifier
25654 Returns a TREE_LIST, indicating the labels in the order that they
25655 appeared. The TREE_VALUE of each node is a label. */
25657 static tree
25658 cp_parser_asm_label_list (cp_parser* parser)
25660 tree labels = NULL_TREE;
25662 while (true)
25664 tree identifier, label, name;
25666 /* Look for the identifier. */
25667 identifier = cp_parser_identifier (parser);
25668 if (!error_operand_p (identifier))
25670 label = lookup_label (identifier);
25671 if (TREE_CODE (label) == LABEL_DECL)
25673 TREE_USED (label) = 1;
25674 check_goto (label);
25675 name = build_string (IDENTIFIER_LENGTH (identifier),
25676 IDENTIFIER_POINTER (identifier));
25677 labels = tree_cons (name, label, labels);
25680 /* If the next token is not a `,', then the list is
25681 complete. */
25682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25683 break;
25684 /* Consume the `,' token. */
25685 cp_lexer_consume_token (parser->lexer);
25688 return nreverse (labels);
25691 /* Return TRUE iff the next tokens in the stream are possibly the
25692 beginning of a GNU extension attribute. */
25694 static bool
25695 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25697 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25700 /* Return TRUE iff the next tokens in the stream are possibly the
25701 beginning of a standard C++-11 attribute specifier. */
25703 static bool
25704 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25706 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25709 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25710 beginning of a standard C++-11 attribute specifier. */
25712 static bool
25713 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25715 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25717 return (cxx_dialect >= cxx11
25718 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25719 || (token->type == CPP_OPEN_SQUARE
25720 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25721 && token->type == CPP_OPEN_SQUARE)));
25724 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25725 beginning of a GNU extension attribute. */
25727 static bool
25728 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25730 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25732 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25735 /* Return true iff the next tokens can be the beginning of either a
25736 GNU attribute list, or a standard C++11 attribute sequence. */
25738 static bool
25739 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25741 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25742 || cp_next_tokens_can_be_std_attribute_p (parser));
25745 /* Return true iff the next Nth tokens can be the beginning of either
25746 a GNU attribute list, or a standard C++11 attribute sequence. */
25748 static bool
25749 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25751 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25752 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25755 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25756 of GNU attributes, or return NULL. */
25758 static tree
25759 cp_parser_attributes_opt (cp_parser *parser)
25761 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25762 return cp_parser_gnu_attributes_opt (parser);
25763 return cp_parser_std_attribute_spec_seq (parser);
25766 /* Parse an (optional) series of attributes.
25768 attributes:
25769 attributes attribute
25771 attribute:
25772 __attribute__ (( attribute-list [opt] ))
25774 The return value is as for cp_parser_gnu_attribute_list. */
25776 static tree
25777 cp_parser_gnu_attributes_opt (cp_parser* parser)
25779 tree attributes = NULL_TREE;
25781 temp_override<bool> cleanup
25782 (parser->auto_is_implicit_function_template_parm_p, false);
25784 while (true)
25786 cp_token *token;
25787 tree attribute_list;
25788 bool ok = true;
25790 /* Peek at the next token. */
25791 token = cp_lexer_peek_token (parser->lexer);
25792 /* If it's not `__attribute__', then we're done. */
25793 if (token->keyword != RID_ATTRIBUTE)
25794 break;
25796 /* Consume the `__attribute__' keyword. */
25797 cp_lexer_consume_token (parser->lexer);
25798 /* Look for the two `(' tokens. */
25799 matching_parens outer_parens;
25800 if (!outer_parens.require_open (parser))
25801 ok = false;
25802 matching_parens inner_parens;
25803 if (!inner_parens.require_open (parser))
25804 ok = false;
25806 /* Peek at the next token. */
25807 token = cp_lexer_peek_token (parser->lexer);
25808 if (token->type != CPP_CLOSE_PAREN)
25809 /* Parse the attribute-list. */
25810 attribute_list = cp_parser_gnu_attribute_list (parser);
25811 else
25812 /* If the next token is a `)', then there is no attribute
25813 list. */
25814 attribute_list = NULL;
25816 /* Look for the two `)' tokens. */
25817 if (!inner_parens.require_close (parser))
25818 ok = false;
25819 if (!outer_parens.require_close (parser))
25820 ok = false;
25821 if (!ok)
25822 cp_parser_skip_to_end_of_statement (parser);
25824 /* Add these new attributes to the list. */
25825 attributes = attr_chainon (attributes, attribute_list);
25828 return attributes;
25831 /* Parse a GNU attribute-list.
25833 attribute-list:
25834 attribute
25835 attribute-list , attribute
25837 attribute:
25838 identifier
25839 identifier ( identifier )
25840 identifier ( identifier , expression-list )
25841 identifier ( expression-list )
25843 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25844 to an attribute. The TREE_PURPOSE of each node is the identifier
25845 indicating which attribute is in use. The TREE_VALUE represents
25846 the arguments, if any. */
25848 static tree
25849 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25851 tree attribute_list = NULL_TREE;
25852 bool save_translate_strings_p = parser->translate_strings_p;
25854 /* Don't create wrapper nodes within attributes: the
25855 handlers don't know how to handle them. */
25856 auto_suppress_location_wrappers sentinel;
25858 parser->translate_strings_p = false;
25859 while (true)
25861 cp_token *token;
25862 tree identifier;
25863 tree attribute;
25865 /* Look for the identifier. We also allow keywords here; for
25866 example `__attribute__ ((const))' is legal. */
25867 token = cp_lexer_peek_token (parser->lexer);
25868 if (token->type == CPP_NAME
25869 || token->type == CPP_KEYWORD)
25871 tree arguments = NULL_TREE;
25873 /* Consume the token, but save it since we need it for the
25874 SIMD enabled function parsing. */
25875 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25877 /* Save away the identifier that indicates which attribute
25878 this is. */
25879 identifier = (token->type == CPP_KEYWORD)
25880 /* For keywords, use the canonical spelling, not the
25881 parsed identifier. */
25882 ? ridpointers[(int) token->keyword]
25883 : id_token->u.value;
25885 identifier = canonicalize_attr_name (identifier);
25886 attribute = build_tree_list (identifier, NULL_TREE);
25888 /* Peek at the next token. */
25889 token = cp_lexer_peek_token (parser->lexer);
25890 /* If it's an `(', then parse the attribute arguments. */
25891 if (token->type == CPP_OPEN_PAREN)
25893 vec<tree, va_gc> *vec;
25894 int attr_flag = (attribute_takes_identifier_p (identifier)
25895 ? id_attr : normal_attr);
25896 vec = cp_parser_parenthesized_expression_list
25897 (parser, attr_flag, /*cast_p=*/false,
25898 /*allow_expansion_p=*/false,
25899 /*non_constant_p=*/NULL);
25900 if (vec == NULL)
25901 arguments = error_mark_node;
25902 else
25904 arguments = build_tree_list_vec (vec);
25905 release_tree_vector (vec);
25907 /* Save the arguments away. */
25908 TREE_VALUE (attribute) = arguments;
25911 if (arguments != error_mark_node)
25913 /* Add this attribute to the list. */
25914 TREE_CHAIN (attribute) = attribute_list;
25915 attribute_list = attribute;
25918 token = cp_lexer_peek_token (parser->lexer);
25920 /* Unless EXACTLY_ONE is set look for more attributes.
25921 If the next token isn't a `,', we're done. */
25922 if (exactly_one || token->type != CPP_COMMA)
25923 break;
25925 /* Consume the comma and keep going. */
25926 cp_lexer_consume_token (parser->lexer);
25928 parser->translate_strings_p = save_translate_strings_p;
25930 /* We built up the list in reverse order. */
25931 return nreverse (attribute_list);
25934 /* Parse a standard C++11 attribute.
25936 The returned representation is a TREE_LIST which TREE_PURPOSE is
25937 the scoped name of the attribute, and the TREE_VALUE is its
25938 arguments list.
25940 Note that the scoped name of the attribute is itself a TREE_LIST
25941 which TREE_PURPOSE is the namespace of the attribute, and
25942 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25943 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25944 and which TREE_PURPOSE is directly the attribute name.
25946 Clients of the attribute code should use get_attribute_namespace
25947 and get_attribute_name to get the actual namespace and name of
25948 attributes, regardless of their being GNU or C++11 attributes.
25950 attribute:
25951 attribute-token attribute-argument-clause [opt]
25953 attribute-token:
25954 identifier
25955 attribute-scoped-token
25957 attribute-scoped-token:
25958 attribute-namespace :: identifier
25960 attribute-namespace:
25961 identifier
25963 attribute-argument-clause:
25964 ( balanced-token-seq )
25966 balanced-token-seq:
25967 balanced-token [opt]
25968 balanced-token-seq balanced-token
25970 balanced-token:
25971 ( balanced-token-seq )
25972 [ balanced-token-seq ]
25973 { balanced-token-seq }. */
25975 static tree
25976 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25978 tree attribute, attr_id = NULL_TREE, arguments;
25979 cp_token *token;
25981 temp_override<bool> cleanup
25982 (parser->auto_is_implicit_function_template_parm_p, false);
25984 /* First, parse name of the attribute, a.k.a attribute-token. */
25986 token = cp_lexer_peek_token (parser->lexer);
25987 if (token->type == CPP_NAME)
25988 attr_id = token->u.value;
25989 else if (token->type == CPP_KEYWORD)
25990 attr_id = ridpointers[(int) token->keyword];
25991 else if (token->flags & NAMED_OP)
25992 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25994 if (attr_id == NULL_TREE)
25995 return NULL_TREE;
25997 cp_lexer_consume_token (parser->lexer);
25999 token = cp_lexer_peek_token (parser->lexer);
26000 if (token->type == CPP_SCOPE)
26002 /* We are seeing a scoped attribute token. */
26004 cp_lexer_consume_token (parser->lexer);
26005 if (attr_ns)
26006 error_at (token->location, "attribute using prefix used together "
26007 "with scoped attribute token");
26008 attr_ns = attr_id;
26010 token = cp_lexer_consume_token (parser->lexer);
26011 if (token->type == CPP_NAME)
26012 attr_id = token->u.value;
26013 else if (token->type == CPP_KEYWORD)
26014 attr_id = ridpointers[(int) token->keyword];
26015 else if (token->flags & NAMED_OP)
26016 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26017 else
26019 error_at (token->location,
26020 "expected an identifier for the attribute name");
26021 return error_mark_node;
26024 attr_ns = canonicalize_attr_name (attr_ns);
26025 attr_id = canonicalize_attr_name (attr_id);
26026 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26027 NULL_TREE);
26028 token = cp_lexer_peek_token (parser->lexer);
26030 else if (attr_ns)
26032 attr_ns = canonicalize_attr_name (attr_ns);
26033 attr_id = canonicalize_attr_name (attr_id);
26034 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26035 NULL_TREE);
26037 else
26039 attr_id = canonicalize_attr_name (attr_id);
26040 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26041 NULL_TREE);
26042 /* C++11 noreturn attribute is equivalent to GNU's. */
26043 if (is_attribute_p ("noreturn", attr_id))
26044 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26045 /* C++14 deprecated attribute is equivalent to GNU's. */
26046 else if (is_attribute_p ("deprecated", attr_id))
26047 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26048 /* C++17 fallthrough attribute is equivalent to GNU's. */
26049 else if (is_attribute_p ("fallthrough", attr_id))
26050 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26051 /* Transactional Memory TS optimize_for_synchronized attribute is
26052 equivalent to GNU transaction_callable. */
26053 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26054 TREE_PURPOSE (attribute)
26055 = get_identifier ("transaction_callable");
26056 /* Transactional Memory attributes are GNU attributes. */
26057 else if (tm_attr_to_mask (attr_id))
26058 TREE_PURPOSE (attribute) = attr_id;
26061 /* Now parse the optional argument clause of the attribute. */
26063 if (token->type != CPP_OPEN_PAREN)
26064 return attribute;
26067 vec<tree, va_gc> *vec;
26068 int attr_flag = normal_attr;
26070 if (attr_ns == gnu_identifier
26071 && attribute_takes_identifier_p (attr_id))
26072 /* A GNU attribute that takes an identifier in parameter. */
26073 attr_flag = id_attr;
26075 vec = cp_parser_parenthesized_expression_list
26076 (parser, attr_flag, /*cast_p=*/false,
26077 /*allow_expansion_p=*/true,
26078 /*non_constant_p=*/NULL);
26079 if (vec == NULL)
26080 arguments = error_mark_node;
26081 else
26083 arguments = build_tree_list_vec (vec);
26084 release_tree_vector (vec);
26087 if (arguments == error_mark_node)
26088 attribute = error_mark_node;
26089 else
26090 TREE_VALUE (attribute) = arguments;
26093 return attribute;
26096 /* Check that the attribute ATTRIBUTE appears at most once in the
26097 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26098 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26099 isn't implemented yet in GCC. */
26101 static void
26102 cp_parser_check_std_attribute (tree attributes, tree attribute)
26104 if (attributes)
26106 tree name = get_attribute_name (attribute);
26107 if (is_attribute_p ("noreturn", name)
26108 && lookup_attribute ("noreturn", attributes))
26109 error ("attribute %<noreturn%> can appear at most once "
26110 "in an attribute-list");
26111 else if (is_attribute_p ("deprecated", name)
26112 && lookup_attribute ("deprecated", attributes))
26113 error ("attribute %<deprecated%> can appear at most once "
26114 "in an attribute-list");
26118 /* Parse a list of standard C++-11 attributes.
26120 attribute-list:
26121 attribute [opt]
26122 attribute-list , attribute[opt]
26123 attribute ...
26124 attribute-list , attribute ...
26127 static tree
26128 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26130 tree attributes = NULL_TREE, attribute = NULL_TREE;
26131 cp_token *token = NULL;
26133 while (true)
26135 attribute = cp_parser_std_attribute (parser, attr_ns);
26136 if (attribute == error_mark_node)
26137 break;
26138 if (attribute != NULL_TREE)
26140 cp_parser_check_std_attribute (attributes, attribute);
26141 TREE_CHAIN (attribute) = attributes;
26142 attributes = attribute;
26144 token = cp_lexer_peek_token (parser->lexer);
26145 if (token->type == CPP_ELLIPSIS)
26147 cp_lexer_consume_token (parser->lexer);
26148 if (attribute == NULL_TREE)
26149 error_at (token->location,
26150 "expected attribute before %<...%>");
26151 else
26153 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26154 if (pack == error_mark_node)
26155 return error_mark_node;
26156 TREE_VALUE (attribute) = pack;
26158 token = cp_lexer_peek_token (parser->lexer);
26160 if (token->type != CPP_COMMA)
26161 break;
26162 cp_lexer_consume_token (parser->lexer);
26164 attributes = nreverse (attributes);
26165 return attributes;
26168 /* Parse a standard C++-11 attribute specifier.
26170 attribute-specifier:
26171 [ [ attribute-using-prefix [opt] attribute-list ] ]
26172 alignment-specifier
26174 attribute-using-prefix:
26175 using attribute-namespace :
26177 alignment-specifier:
26178 alignas ( type-id ... [opt] )
26179 alignas ( alignment-expression ... [opt] ). */
26181 static tree
26182 cp_parser_std_attribute_spec (cp_parser *parser)
26184 tree attributes = NULL_TREE;
26185 cp_token *token = cp_lexer_peek_token (parser->lexer);
26187 if (token->type == CPP_OPEN_SQUARE
26188 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26190 tree attr_ns = NULL_TREE;
26192 cp_lexer_consume_token (parser->lexer);
26193 cp_lexer_consume_token (parser->lexer);
26195 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26197 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26198 if (token->type == CPP_NAME)
26199 attr_ns = token->u.value;
26200 else if (token->type == CPP_KEYWORD)
26201 attr_ns = ridpointers[(int) token->keyword];
26202 else if (token->flags & NAMED_OP)
26203 attr_ns = get_identifier (cpp_type2name (token->type,
26204 token->flags));
26205 if (attr_ns
26206 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26208 if (cxx_dialect < cxx17
26209 && !in_system_header_at (input_location))
26210 pedwarn (input_location, 0,
26211 "attribute using prefix only available "
26212 "with -std=c++17 or -std=gnu++17");
26214 cp_lexer_consume_token (parser->lexer);
26215 cp_lexer_consume_token (parser->lexer);
26216 cp_lexer_consume_token (parser->lexer);
26218 else
26219 attr_ns = NULL_TREE;
26222 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26224 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26225 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26226 cp_parser_skip_to_end_of_statement (parser);
26227 else
26228 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26229 when we are sure that we have actually parsed them. */
26230 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26232 else
26234 tree alignas_expr;
26236 /* Look for an alignment-specifier. */
26238 token = cp_lexer_peek_token (parser->lexer);
26240 if (token->type != CPP_KEYWORD
26241 || token->keyword != RID_ALIGNAS)
26242 return NULL_TREE;
26244 cp_lexer_consume_token (parser->lexer);
26245 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26247 matching_parens parens;
26248 if (!parens.require_open (parser))
26249 return error_mark_node;
26251 cp_parser_parse_tentatively (parser);
26252 alignas_expr = cp_parser_type_id (parser);
26254 if (!cp_parser_parse_definitely (parser))
26256 alignas_expr = cp_parser_assignment_expression (parser);
26257 if (alignas_expr == error_mark_node)
26258 cp_parser_skip_to_end_of_statement (parser);
26259 if (alignas_expr == NULL_TREE
26260 || alignas_expr == error_mark_node)
26261 return alignas_expr;
26264 alignas_expr = cxx_alignas_expr (alignas_expr);
26265 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26267 /* Handle alignas (pack...). */
26268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26270 cp_lexer_consume_token (parser->lexer);
26271 alignas_expr = make_pack_expansion (alignas_expr);
26274 /* Something went wrong, so don't build the attribute. */
26275 if (alignas_expr == error_mark_node)
26276 return error_mark_node;
26278 if (!parens.require_close (parser))
26279 return error_mark_node;
26281 /* Build the C++-11 representation of an 'aligned'
26282 attribute. */
26283 attributes
26284 = build_tree_list (build_tree_list (gnu_identifier,
26285 aligned_identifier), alignas_expr);
26288 return attributes;
26291 /* Parse a standard C++-11 attribute-specifier-seq.
26293 attribute-specifier-seq:
26294 attribute-specifier-seq [opt] attribute-specifier
26297 static tree
26298 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26300 tree attr_specs = NULL_TREE;
26301 tree attr_last = NULL_TREE;
26303 /* Don't create wrapper nodes within attributes: the
26304 handlers don't know how to handle them. */
26305 auto_suppress_location_wrappers sentinel;
26307 while (true)
26309 tree attr_spec = cp_parser_std_attribute_spec (parser);
26310 if (attr_spec == NULL_TREE)
26311 break;
26312 if (attr_spec == error_mark_node)
26313 return error_mark_node;
26315 if (attr_last)
26316 TREE_CHAIN (attr_last) = attr_spec;
26317 else
26318 attr_specs = attr_last = attr_spec;
26319 attr_last = tree_last (attr_last);
26322 return attr_specs;
26325 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26326 return index of the first token after balanced-token, or N on failure. */
26328 static size_t
26329 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26331 size_t orig_n = n;
26332 int nparens = 0, nbraces = 0, nsquares = 0;
26334 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26336 case CPP_PRAGMA_EOL:
26337 if (!parser->lexer->in_pragma)
26338 break;
26339 /* FALLTHRU */
26340 case CPP_EOF:
26341 /* Ran out of tokens. */
26342 return orig_n;
26343 case CPP_OPEN_PAREN:
26344 ++nparens;
26345 break;
26346 case CPP_OPEN_BRACE:
26347 ++nbraces;
26348 break;
26349 case CPP_OPEN_SQUARE:
26350 ++nsquares;
26351 break;
26352 case CPP_CLOSE_PAREN:
26353 --nparens;
26354 break;
26355 case CPP_CLOSE_BRACE:
26356 --nbraces;
26357 break;
26358 case CPP_CLOSE_SQUARE:
26359 --nsquares;
26360 break;
26361 default:
26362 break;
26364 while (nparens || nbraces || nsquares);
26365 return n;
26368 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26369 return index of the first token after the GNU attribute tokens, or N on
26370 failure. */
26372 static size_t
26373 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26375 while (true)
26377 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26378 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26379 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26380 break;
26382 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26383 if (n2 == n + 2)
26384 break;
26385 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26386 break;
26387 n = n2 + 1;
26389 return n;
26392 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26393 next token), return index of the first token after the standard C++11
26394 attribute tokens, or N on failure. */
26396 static size_t
26397 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26399 while (true)
26401 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26402 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26404 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26405 if (n2 == n + 1)
26406 break;
26407 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26408 break;
26409 n = n2 + 1;
26411 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26412 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26414 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26415 if (n2 == n + 1)
26416 break;
26417 n = n2;
26419 else
26420 break;
26422 return n;
26425 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26426 as the next token), return index of the first token after the attribute
26427 tokens, or N on failure. */
26429 static size_t
26430 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26432 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26433 return cp_parser_skip_gnu_attributes_opt (parser, n);
26434 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26437 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26438 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26439 current value of the PEDANTIC flag, regardless of whether or not
26440 the `__extension__' keyword is present. The caller is responsible
26441 for restoring the value of the PEDANTIC flag. */
26443 static bool
26444 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26446 /* Save the old value of the PEDANTIC flag. */
26447 *saved_pedantic = pedantic;
26449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26451 /* Consume the `__extension__' token. */
26452 cp_lexer_consume_token (parser->lexer);
26453 /* We're not being pedantic while the `__extension__' keyword is
26454 in effect. */
26455 pedantic = 0;
26457 return true;
26460 return false;
26463 /* Parse a label declaration.
26465 label-declaration:
26466 __label__ label-declarator-seq ;
26468 label-declarator-seq:
26469 identifier , label-declarator-seq
26470 identifier */
26472 static void
26473 cp_parser_label_declaration (cp_parser* parser)
26475 /* Look for the `__label__' keyword. */
26476 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26478 while (true)
26480 tree identifier;
26482 /* Look for an identifier. */
26483 identifier = cp_parser_identifier (parser);
26484 /* If we failed, stop. */
26485 if (identifier == error_mark_node)
26486 break;
26487 /* Declare it as a label. */
26488 finish_label_decl (identifier);
26489 /* If the next token is a `;', stop. */
26490 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26491 break;
26492 /* Look for the `,' separating the label declarations. */
26493 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26496 /* Look for the final `;'. */
26497 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26500 // -------------------------------------------------------------------------- //
26501 // Requires Clause
26503 // Parse a requires clause.
26505 // requires-clause:
26506 // 'requires' logical-or-expression
26508 // The required logical-or-expression must be a constant expression. Note
26509 // that we don't check that the expression is constepxr here. We defer until
26510 // we analyze constraints and then, we only check atomic constraints.
26511 static tree
26512 cp_parser_requires_clause (cp_parser *parser)
26514 // Parse the requires clause so that it is not automatically folded.
26515 ++processing_template_decl;
26516 tree expr = cp_parser_binary_expression (parser, false, false,
26517 PREC_NOT_OPERATOR, NULL);
26518 if (check_for_bare_parameter_packs (expr))
26519 expr = error_mark_node;
26520 --processing_template_decl;
26521 return expr;
26524 // Optionally parse a requires clause:
26525 static tree
26526 cp_parser_requires_clause_opt (cp_parser *parser)
26528 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26529 if (tok->keyword != RID_REQUIRES)
26531 if (!flag_concepts && tok->type == CPP_NAME
26532 && tok->u.value == ridpointers[RID_REQUIRES])
26534 error_at (cp_lexer_peek_token (parser->lexer)->location,
26535 "%<requires%> only available with -fconcepts");
26536 /* Parse and discard the requires-clause. */
26537 cp_lexer_consume_token (parser->lexer);
26538 cp_parser_requires_clause (parser);
26540 return NULL_TREE;
26542 cp_lexer_consume_token (parser->lexer);
26543 return cp_parser_requires_clause (parser);
26547 /*---------------------------------------------------------------------------
26548 Requires expressions
26549 ---------------------------------------------------------------------------*/
26551 /* Parse a requires expression
26553 requirement-expression:
26554 'requires' requirement-parameter-list [opt] requirement-body */
26555 static tree
26556 cp_parser_requires_expression (cp_parser *parser)
26558 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26559 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26561 /* A requires-expression shall appear only within a concept
26562 definition or a requires-clause.
26564 TODO: Implement this diagnostic correctly. */
26565 if (!processing_template_decl)
26567 error_at (loc, "a requires expression cannot appear outside a template");
26568 cp_parser_skip_to_end_of_statement (parser);
26569 return error_mark_node;
26572 tree parms, reqs;
26574 /* Local parameters are delared as variables within the scope
26575 of the expression. They are not visible past the end of
26576 the expression. Expressions within the requires-expression
26577 are unevaluated. */
26578 struct scope_sentinel
26580 scope_sentinel ()
26582 ++cp_unevaluated_operand;
26583 begin_scope (sk_block, NULL_TREE);
26586 ~scope_sentinel ()
26588 pop_bindings_and_leave_scope ();
26589 --cp_unevaluated_operand;
26591 } s;
26593 /* Parse the optional parameter list. */
26594 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26596 parms = cp_parser_requirement_parameter_list (parser);
26597 if (parms == error_mark_node)
26598 return error_mark_node;
26600 else
26601 parms = NULL_TREE;
26603 /* Parse the requirement body. */
26604 reqs = cp_parser_requirement_body (parser);
26605 if (reqs == error_mark_node)
26606 return error_mark_node;
26609 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26610 the parm chain. */
26611 grokparms (parms, &parms);
26612 return finish_requires_expr (parms, reqs);
26615 /* Parse a parameterized requirement.
26617 requirement-parameter-list:
26618 '(' parameter-declaration-clause ')' */
26619 static tree
26620 cp_parser_requirement_parameter_list (cp_parser *parser)
26622 matching_parens parens;
26623 if (!parens.require_open (parser))
26624 return error_mark_node;
26626 tree parms
26627 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26629 if (!parens.require_close (parser))
26630 return error_mark_node;
26632 return parms;
26635 /* Parse the body of a requirement.
26637 requirement-body:
26638 '{' requirement-list '}' */
26639 static tree
26640 cp_parser_requirement_body (cp_parser *parser)
26642 matching_braces braces;
26643 if (!braces.require_open (parser))
26644 return error_mark_node;
26646 tree reqs = cp_parser_requirement_list (parser);
26648 if (!braces.require_close (parser))
26649 return error_mark_node;
26651 return reqs;
26654 /* Parse a list of requirements.
26656 requirement-list:
26657 requirement
26658 requirement-list ';' requirement[opt] */
26659 static tree
26660 cp_parser_requirement_list (cp_parser *parser)
26662 tree result = NULL_TREE;
26663 while (true)
26665 tree req = cp_parser_requirement (parser);
26666 if (req == error_mark_node)
26667 return error_mark_node;
26669 result = tree_cons (NULL_TREE, req, result);
26671 /* If we see a semi-colon, consume it. */
26672 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26673 cp_lexer_consume_token (parser->lexer);
26675 /* Stop processing at the end of the list. */
26676 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26677 break;
26680 /* Reverse the order of requirements so they are analyzed in
26681 declaration order. */
26682 return nreverse (result);
26685 /* Parse a syntactic requirement or type requirement.
26687 requirement:
26688 simple-requirement
26689 compound-requirement
26690 type-requirement
26691 nested-requirement */
26692 static tree
26693 cp_parser_requirement (cp_parser *parser)
26695 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26696 return cp_parser_compound_requirement (parser);
26697 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26698 return cp_parser_type_requirement (parser);
26699 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26700 return cp_parser_nested_requirement (parser);
26701 else
26702 return cp_parser_simple_requirement (parser);
26705 /* Parse a simple requirement.
26707 simple-requirement:
26708 expression ';' */
26709 static tree
26710 cp_parser_simple_requirement (cp_parser *parser)
26712 tree expr = cp_parser_expression (parser, NULL, false, false);
26713 if (!expr || expr == error_mark_node)
26714 return error_mark_node;
26716 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26717 return error_mark_node;
26719 return finish_simple_requirement (expr);
26722 /* Parse a type requirement
26724 type-requirement
26725 nested-name-specifier [opt] required-type-name ';'
26727 required-type-name:
26728 type-name
26729 'template' [opt] simple-template-id */
26730 static tree
26731 cp_parser_type_requirement (cp_parser *parser)
26733 cp_lexer_consume_token (parser->lexer);
26735 // Save the scope before parsing name specifiers.
26736 tree saved_scope = parser->scope;
26737 tree saved_object_scope = parser->object_scope;
26738 tree saved_qualifying_scope = parser->qualifying_scope;
26739 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26740 cp_parser_nested_name_specifier_opt (parser,
26741 /*typename_keyword_p=*/true,
26742 /*check_dependency_p=*/false,
26743 /*type_p=*/true,
26744 /*is_declaration=*/false);
26746 tree type;
26747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26749 cp_lexer_consume_token (parser->lexer);
26750 type = cp_parser_template_id (parser,
26751 /*template_keyword_p=*/true,
26752 /*check_dependency=*/false,
26753 /*tag_type=*/none_type,
26754 /*is_declaration=*/false);
26755 type = make_typename_type (parser->scope, type, typename_type,
26756 /*complain=*/tf_error);
26758 else
26759 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26761 if (TREE_CODE (type) == TYPE_DECL)
26762 type = TREE_TYPE (type);
26764 parser->scope = saved_scope;
26765 parser->object_scope = saved_object_scope;
26766 parser->qualifying_scope = saved_qualifying_scope;
26768 if (type == error_mark_node)
26769 cp_parser_skip_to_end_of_statement (parser);
26771 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26772 return error_mark_node;
26773 if (type == error_mark_node)
26774 return error_mark_node;
26776 return finish_type_requirement (type);
26779 /* Parse a compound requirement
26781 compound-requirement:
26782 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26783 static tree
26784 cp_parser_compound_requirement (cp_parser *parser)
26786 /* Parse an expression enclosed in '{ }'s. */
26787 matching_braces braces;
26788 if (!braces.require_open (parser))
26789 return error_mark_node;
26791 tree expr = cp_parser_expression (parser, NULL, false, false);
26792 if (!expr || expr == error_mark_node)
26793 return error_mark_node;
26795 if (!braces.require_close (parser))
26796 return error_mark_node;
26798 /* Parse the optional noexcept. */
26799 bool noexcept_p = false;
26800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26802 cp_lexer_consume_token (parser->lexer);
26803 noexcept_p = true;
26806 /* Parse the optional trailing return type. */
26807 tree type = NULL_TREE;
26808 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26810 cp_lexer_consume_token (parser->lexer);
26811 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26812 parser->in_result_type_constraint_p = true;
26813 type = cp_parser_trailing_type_id (parser);
26814 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26815 if (type == error_mark_node)
26816 return error_mark_node;
26819 return finish_compound_requirement (expr, type, noexcept_p);
26822 /* Parse a nested requirement. This is the same as a requires clause.
26824 nested-requirement:
26825 requires-clause */
26826 static tree
26827 cp_parser_nested_requirement (cp_parser *parser)
26829 cp_lexer_consume_token (parser->lexer);
26830 tree req = cp_parser_requires_clause (parser);
26831 if (req == error_mark_node)
26832 return error_mark_node;
26833 return finish_nested_requirement (req);
26836 /* Support Functions */
26838 /* Return the appropriate prefer_type argument for lookup_name_real based on
26839 tag_type and template_mem_access. */
26841 static inline int
26842 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26844 /* DR 141: When looking in the current enclosing context for a template-name
26845 after -> or ., only consider class templates. */
26846 if (template_mem_access)
26847 return 2;
26848 switch (tag_type)
26850 case none_type: return 0; // No preference.
26851 case scope_type: return 1; // Type or namespace.
26852 default: return 2; // Type only.
26856 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26857 NAME should have one of the representations used for an
26858 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26859 is returned. If PARSER->SCOPE is a dependent type, then a
26860 SCOPE_REF is returned.
26862 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26863 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26864 was formed. Abstractly, such entities should not be passed to this
26865 function, because they do not need to be looked up, but it is
26866 simpler to check for this special case here, rather than at the
26867 call-sites.
26869 In cases not explicitly covered above, this function returns a
26870 DECL, OVERLOAD, or baselink representing the result of the lookup.
26871 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26872 is returned.
26874 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26875 (e.g., "struct") that was used. In that case bindings that do not
26876 refer to types are ignored.
26878 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26879 ignored.
26881 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26882 are ignored.
26884 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26885 types.
26887 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26888 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26889 NULL_TREE otherwise. */
26891 static cp_expr
26892 cp_parser_lookup_name (cp_parser *parser, tree name,
26893 enum tag_types tag_type,
26894 bool is_template,
26895 bool is_namespace,
26896 bool check_dependency,
26897 tree *ambiguous_decls,
26898 location_t name_location)
26900 tree decl;
26901 tree object_type = parser->context->object_type;
26903 /* Assume that the lookup will be unambiguous. */
26904 if (ambiguous_decls)
26905 *ambiguous_decls = NULL_TREE;
26907 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26908 no longer valid. Note that if we are parsing tentatively, and
26909 the parse fails, OBJECT_TYPE will be automatically restored. */
26910 parser->context->object_type = NULL_TREE;
26912 if (name == error_mark_node)
26913 return error_mark_node;
26915 /* A template-id has already been resolved; there is no lookup to
26916 do. */
26917 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26918 return name;
26919 if (BASELINK_P (name))
26921 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26922 == TEMPLATE_ID_EXPR);
26923 return name;
26926 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26927 it should already have been checked to make sure that the name
26928 used matches the type being destroyed. */
26929 if (TREE_CODE (name) == BIT_NOT_EXPR)
26931 tree type;
26933 /* Figure out to which type this destructor applies. */
26934 if (parser->scope)
26935 type = parser->scope;
26936 else if (object_type)
26937 type = object_type;
26938 else
26939 type = current_class_type;
26940 /* If that's not a class type, there is no destructor. */
26941 if (!type || !CLASS_TYPE_P (type))
26942 return error_mark_node;
26944 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26945 lazily_declare_fn (sfk_destructor, type);
26947 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26948 return dtor;
26950 return error_mark_node;
26953 /* By this point, the NAME should be an ordinary identifier. If
26954 the id-expression was a qualified name, the qualifying scope is
26955 stored in PARSER->SCOPE at this point. */
26956 gcc_assert (identifier_p (name));
26958 /* Perform the lookup. */
26959 if (parser->scope)
26961 bool dependent_p;
26963 if (parser->scope == error_mark_node)
26964 return error_mark_node;
26966 /* If the SCOPE is dependent, the lookup must be deferred until
26967 the template is instantiated -- unless we are explicitly
26968 looking up names in uninstantiated templates. Even then, we
26969 cannot look up the name if the scope is not a class type; it
26970 might, for example, be a template type parameter. */
26971 dependent_p = (TYPE_P (parser->scope)
26972 && dependent_scope_p (parser->scope));
26973 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26974 && dependent_p)
26975 /* Defer lookup. */
26976 decl = error_mark_node;
26977 else
26979 tree pushed_scope = NULL_TREE;
26981 /* If PARSER->SCOPE is a dependent type, then it must be a
26982 class type, and we must not be checking dependencies;
26983 otherwise, we would have processed this lookup above. So
26984 that PARSER->SCOPE is not considered a dependent base by
26985 lookup_member, we must enter the scope here. */
26986 if (dependent_p)
26987 pushed_scope = push_scope (parser->scope);
26989 /* If the PARSER->SCOPE is a template specialization, it
26990 may be instantiated during name lookup. In that case,
26991 errors may be issued. Even if we rollback the current
26992 tentative parse, those errors are valid. */
26993 decl = lookup_qualified_name (parser->scope, name,
26994 prefer_type_arg (tag_type),
26995 /*complain=*/true);
26997 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26998 lookup result and the nested-name-specifier nominates a class C:
26999 * if the name specified after the nested-name-specifier, when
27000 looked up in C, is the injected-class-name of C (Clause 9), or
27001 * if the name specified after the nested-name-specifier is the
27002 same as the identifier or the simple-template-id's template-
27003 name in the last component of the nested-name-specifier,
27004 the name is instead considered to name the constructor of
27005 class C. [ Note: for example, the constructor is not an
27006 acceptable lookup result in an elaborated-type-specifier so
27007 the constructor would not be used in place of the
27008 injected-class-name. --end note ] Such a constructor name
27009 shall be used only in the declarator-id of a declaration that
27010 names a constructor or in a using-declaration. */
27011 if (tag_type == none_type
27012 && DECL_SELF_REFERENCE_P (decl)
27013 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27014 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27015 prefer_type_arg (tag_type),
27016 /*complain=*/true);
27018 /* If we have a single function from a using decl, pull it out. */
27019 if (TREE_CODE (decl) == OVERLOAD
27020 && !really_overloaded_fn (decl))
27021 decl = OVL_FUNCTION (decl);
27023 if (pushed_scope)
27024 pop_scope (pushed_scope);
27027 /* If the scope is a dependent type and either we deferred lookup or
27028 we did lookup but didn't find the name, rememeber the name. */
27029 if (decl == error_mark_node && TYPE_P (parser->scope)
27030 && dependent_type_p (parser->scope))
27032 if (tag_type)
27034 tree type;
27036 /* The resolution to Core Issue 180 says that `struct
27037 A::B' should be considered a type-name, even if `A'
27038 is dependent. */
27039 type = make_typename_type (parser->scope, name, tag_type,
27040 /*complain=*/tf_error);
27041 if (type != error_mark_node)
27042 decl = TYPE_NAME (type);
27044 else if (is_template
27045 && (cp_parser_next_token_ends_template_argument_p (parser)
27046 || cp_lexer_next_token_is (parser->lexer,
27047 CPP_CLOSE_PAREN)))
27048 decl = make_unbound_class_template (parser->scope,
27049 name, NULL_TREE,
27050 /*complain=*/tf_error);
27051 else
27052 decl = build_qualified_name (/*type=*/NULL_TREE,
27053 parser->scope, name,
27054 is_template);
27056 parser->qualifying_scope = parser->scope;
27057 parser->object_scope = NULL_TREE;
27059 else if (object_type)
27061 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27062 OBJECT_TYPE is not a class. */
27063 if (CLASS_TYPE_P (object_type))
27064 /* If the OBJECT_TYPE is a template specialization, it may
27065 be instantiated during name lookup. In that case, errors
27066 may be issued. Even if we rollback the current tentative
27067 parse, those errors are valid. */
27068 decl = lookup_member (object_type,
27069 name,
27070 /*protect=*/0,
27071 prefer_type_arg (tag_type),
27072 tf_warning_or_error);
27073 else
27074 decl = NULL_TREE;
27076 if (!decl)
27077 /* Look it up in the enclosing context. DR 141: When looking for a
27078 template-name after -> or ., only consider class templates. */
27079 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27080 /*nonclass=*/0,
27081 /*block_p=*/true, is_namespace, 0);
27082 if (object_type == unknown_type_node)
27083 /* The object is type-dependent, so we can't look anything up; we used
27084 this to get the DR 141 behavior. */
27085 object_type = NULL_TREE;
27086 parser->object_scope = object_type;
27087 parser->qualifying_scope = NULL_TREE;
27089 else
27091 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27092 /*nonclass=*/0,
27093 /*block_p=*/true, is_namespace, 0);
27094 parser->qualifying_scope = NULL_TREE;
27095 parser->object_scope = NULL_TREE;
27098 /* If the lookup failed, let our caller know. */
27099 if (!decl || decl == error_mark_node)
27100 return error_mark_node;
27102 /* Pull out the template from an injected-class-name (or multiple). */
27103 if (is_template)
27104 decl = maybe_get_template_decl_from_type_decl (decl);
27106 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27107 if (TREE_CODE (decl) == TREE_LIST)
27109 if (ambiguous_decls)
27110 *ambiguous_decls = decl;
27111 /* The error message we have to print is too complicated for
27112 cp_parser_error, so we incorporate its actions directly. */
27113 if (!cp_parser_simulate_error (parser))
27115 error_at (name_location, "reference to %qD is ambiguous",
27116 name);
27117 print_candidates (decl);
27119 return error_mark_node;
27122 gcc_assert (DECL_P (decl)
27123 || TREE_CODE (decl) == OVERLOAD
27124 || TREE_CODE (decl) == SCOPE_REF
27125 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27126 || BASELINK_P (decl));
27128 /* If we have resolved the name of a member declaration, check to
27129 see if the declaration is accessible. When the name resolves to
27130 set of overloaded functions, accessibility is checked when
27131 overload resolution is done.
27133 During an explicit instantiation, access is not checked at all,
27134 as per [temp.explicit]. */
27135 if (DECL_P (decl))
27136 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27138 maybe_record_typedef_use (decl);
27140 return cp_expr (decl, name_location);
27143 /* Like cp_parser_lookup_name, but for use in the typical case where
27144 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27145 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27147 static tree
27148 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27150 return cp_parser_lookup_name (parser, name,
27151 none_type,
27152 /*is_template=*/false,
27153 /*is_namespace=*/false,
27154 /*check_dependency=*/true,
27155 /*ambiguous_decls=*/NULL,
27156 location);
27159 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27160 the current context, return the TYPE_DECL. If TAG_NAME_P is
27161 true, the DECL indicates the class being defined in a class-head,
27162 or declared in an elaborated-type-specifier.
27164 Otherwise, return DECL. */
27166 static tree
27167 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27169 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27170 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27172 struct A {
27173 template <typename T> struct B;
27176 template <typename T> struct A::B {};
27178 Similarly, in an elaborated-type-specifier:
27180 namespace N { struct X{}; }
27182 struct A {
27183 template <typename T> friend struct N::X;
27186 However, if the DECL refers to a class type, and we are in
27187 the scope of the class, then the name lookup automatically
27188 finds the TYPE_DECL created by build_self_reference rather
27189 than a TEMPLATE_DECL. For example, in:
27191 template <class T> struct S {
27192 S s;
27195 there is no need to handle such case. */
27197 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27198 return DECL_TEMPLATE_RESULT (decl);
27200 return decl;
27203 /* If too many, or too few, template-parameter lists apply to the
27204 declarator, issue an error message. Returns TRUE if all went well,
27205 and FALSE otherwise. */
27207 static bool
27208 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27209 cp_declarator *declarator,
27210 location_t declarator_location)
27212 switch (declarator->kind)
27214 case cdk_id:
27216 unsigned num_templates = 0;
27217 tree scope = declarator->u.id.qualifying_scope;
27218 bool template_id_p = false;
27220 if (scope)
27221 num_templates = num_template_headers_for_class (scope);
27222 else if (TREE_CODE (declarator->u.id.unqualified_name)
27223 == TEMPLATE_ID_EXPR)
27225 /* If the DECLARATOR has the form `X<y>' then it uses one
27226 additional level of template parameters. */
27227 ++num_templates;
27228 template_id_p = true;
27231 return cp_parser_check_template_parameters
27232 (parser, num_templates, template_id_p, declarator_location,
27233 declarator);
27236 case cdk_function:
27237 case cdk_array:
27238 case cdk_pointer:
27239 case cdk_reference:
27240 case cdk_ptrmem:
27241 return (cp_parser_check_declarator_template_parameters
27242 (parser, declarator->declarator, declarator_location));
27244 case cdk_decomp:
27245 case cdk_error:
27246 return true;
27248 default:
27249 gcc_unreachable ();
27251 return false;
27254 /* NUM_TEMPLATES were used in the current declaration. If that is
27255 invalid, return FALSE and issue an error messages. Otherwise,
27256 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27257 declarator and we can print more accurate diagnostics. */
27259 static bool
27260 cp_parser_check_template_parameters (cp_parser* parser,
27261 unsigned num_templates,
27262 bool template_id_p,
27263 location_t location,
27264 cp_declarator *declarator)
27266 /* If there are the same number of template classes and parameter
27267 lists, that's OK. */
27268 if (parser->num_template_parameter_lists == num_templates)
27269 return true;
27270 /* If there are more, but only one more, and the name ends in an identifier,
27271 then we are declaring a primary template. That's OK too. */
27272 if (!template_id_p
27273 && parser->num_template_parameter_lists == num_templates + 1)
27274 return true;
27275 /* If there are more template classes than parameter lists, we have
27276 something like:
27278 template <class T> void S<T>::R<T>::f (); */
27279 if (parser->num_template_parameter_lists < num_templates)
27281 if (declarator && !current_function_decl)
27282 error_at (location, "specializing member %<%T::%E%> "
27283 "requires %<template<>%> syntax",
27284 declarator->u.id.qualifying_scope,
27285 declarator->u.id.unqualified_name);
27286 else if (declarator)
27287 error_at (location, "invalid declaration of %<%T::%E%>",
27288 declarator->u.id.qualifying_scope,
27289 declarator->u.id.unqualified_name);
27290 else
27291 error_at (location, "too few template-parameter-lists");
27292 return false;
27294 /* Otherwise, there are too many template parameter lists. We have
27295 something like:
27297 template <class T> template <class U> void S::f(); */
27298 error_at (location, "too many template-parameter-lists");
27299 return false;
27302 /* Parse an optional `::' token indicating that the following name is
27303 from the global namespace. If so, PARSER->SCOPE is set to the
27304 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27305 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27306 Returns the new value of PARSER->SCOPE, if the `::' token is
27307 present, and NULL_TREE otherwise. */
27309 static tree
27310 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27312 cp_token *token;
27314 /* Peek at the next token. */
27315 token = cp_lexer_peek_token (parser->lexer);
27316 /* If we're looking at a `::' token then we're starting from the
27317 global namespace, not our current location. */
27318 if (token->type == CPP_SCOPE)
27320 /* Consume the `::' token. */
27321 cp_lexer_consume_token (parser->lexer);
27322 /* Set the SCOPE so that we know where to start the lookup. */
27323 parser->scope = global_namespace;
27324 parser->qualifying_scope = global_namespace;
27325 parser->object_scope = NULL_TREE;
27327 return parser->scope;
27329 else if (!current_scope_valid_p)
27331 parser->scope = NULL_TREE;
27332 parser->qualifying_scope = NULL_TREE;
27333 parser->object_scope = NULL_TREE;
27336 return NULL_TREE;
27339 /* Returns TRUE if the upcoming token sequence is the start of a
27340 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27341 declarator is preceded by the `friend' specifier. */
27343 static bool
27344 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
27346 bool constructor_p;
27347 bool outside_class_specifier_p;
27348 tree nested_name_specifier;
27349 cp_token *next_token;
27351 /* The common case is that this is not a constructor declarator, so
27352 try to avoid doing lots of work if at all possible. It's not
27353 valid declare a constructor at function scope. */
27354 if (parser->in_function_body)
27355 return false;
27356 /* And only certain tokens can begin a constructor declarator. */
27357 next_token = cp_lexer_peek_token (parser->lexer);
27358 if (next_token->type != CPP_NAME
27359 && next_token->type != CPP_SCOPE
27360 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27361 && next_token->type != CPP_TEMPLATE_ID)
27362 return false;
27364 /* Parse tentatively; we are going to roll back all of the tokens
27365 consumed here. */
27366 cp_parser_parse_tentatively (parser);
27367 /* Assume that we are looking at a constructor declarator. */
27368 constructor_p = true;
27370 /* Look for the optional `::' operator. */
27371 cp_parser_global_scope_opt (parser,
27372 /*current_scope_valid_p=*/false);
27373 /* Look for the nested-name-specifier. */
27374 nested_name_specifier
27375 = (cp_parser_nested_name_specifier_opt (parser,
27376 /*typename_keyword_p=*/false,
27377 /*check_dependency_p=*/false,
27378 /*type_p=*/false,
27379 /*is_declaration=*/false));
27381 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27382 if (nested_name_specifier
27383 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27385 tree s = resolve_typename_type (nested_name_specifier,
27386 /*only_current_p=*/false);
27387 if (TREE_CODE (s) != TYPENAME_TYPE)
27388 nested_name_specifier = s;
27391 outside_class_specifier_p = (!at_class_scope_p ()
27392 || !TYPE_BEING_DEFINED (current_class_type)
27393 || friend_p);
27395 /* Outside of a class-specifier, there must be a
27396 nested-name-specifier. Except in C++17 mode, where we
27397 might be declaring a guiding declaration. */
27398 if (!nested_name_specifier && outside_class_specifier_p
27399 && cxx_dialect < cxx17)
27400 constructor_p = false;
27401 else if (nested_name_specifier == error_mark_node)
27402 constructor_p = false;
27404 /* If we have a class scope, this is easy; DR 147 says that S::S always
27405 names the constructor, and no other qualified name could. */
27406 if (constructor_p && nested_name_specifier
27407 && CLASS_TYPE_P (nested_name_specifier))
27409 tree id = cp_parser_unqualified_id (parser,
27410 /*template_keyword_p=*/false,
27411 /*check_dependency_p=*/false,
27412 /*declarator_p=*/true,
27413 /*optional_p=*/false);
27414 if (is_overloaded_fn (id))
27415 id = DECL_NAME (get_first_fn (id));
27416 if (!constructor_name_p (id, nested_name_specifier))
27417 constructor_p = false;
27419 /* If we still think that this might be a constructor-declarator,
27420 look for a class-name. */
27421 else if (constructor_p)
27423 /* If we have:
27425 template <typename T> struct S {
27426 S();
27429 we must recognize that the nested `S' names a class. */
27430 if (cxx_dialect >= cxx17)
27431 cp_parser_parse_tentatively (parser);
27433 tree type_decl;
27434 type_decl = cp_parser_class_name (parser,
27435 /*typename_keyword_p=*/false,
27436 /*template_keyword_p=*/false,
27437 none_type,
27438 /*check_dependency_p=*/false,
27439 /*class_head_p=*/false,
27440 /*is_declaration=*/false);
27442 if (cxx_dialect >= cxx17
27443 && !cp_parser_parse_definitely (parser))
27445 type_decl = NULL_TREE;
27446 tree tmpl = cp_parser_template_name (parser,
27447 /*template_keyword*/false,
27448 /*check_dependency_p*/false,
27449 /*is_declaration*/false,
27450 none_type,
27451 /*is_identifier*/NULL);
27452 if (DECL_CLASS_TEMPLATE_P (tmpl)
27453 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27454 /* It's a deduction guide, return true. */;
27455 else
27456 cp_parser_simulate_error (parser);
27459 /* If there was no class-name, then this is not a constructor.
27460 Otherwise, if we are in a class-specifier and we aren't
27461 handling a friend declaration, check that its type matches
27462 current_class_type (c++/38313). Note: error_mark_node
27463 is left alone for error recovery purposes. */
27464 constructor_p = (!cp_parser_error_occurred (parser)
27465 && (outside_class_specifier_p
27466 || type_decl == NULL_TREE
27467 || type_decl == error_mark_node
27468 || same_type_p (current_class_type,
27469 TREE_TYPE (type_decl))));
27471 /* If we're still considering a constructor, we have to see a `(',
27472 to begin the parameter-declaration-clause, followed by either a
27473 `)', an `...', or a decl-specifier. We need to check for a
27474 type-specifier to avoid being fooled into thinking that:
27476 S (f) (int);
27478 is a constructor. (It is actually a function named `f' that
27479 takes one parameter (of type `int') and returns a value of type
27480 `S'. */
27481 if (constructor_p
27482 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27483 constructor_p = false;
27485 if (constructor_p
27486 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27487 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27488 /* A parameter declaration begins with a decl-specifier,
27489 which is either the "attribute" keyword, a storage class
27490 specifier, or (usually) a type-specifier. */
27491 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27493 tree type;
27494 tree pushed_scope = NULL_TREE;
27495 unsigned saved_num_template_parameter_lists;
27497 /* Names appearing in the type-specifier should be looked up
27498 in the scope of the class. */
27499 if (current_class_type)
27500 type = NULL_TREE;
27501 else if (type_decl)
27503 type = TREE_TYPE (type_decl);
27504 if (TREE_CODE (type) == TYPENAME_TYPE)
27506 type = resolve_typename_type (type,
27507 /*only_current_p=*/false);
27508 if (TREE_CODE (type) == TYPENAME_TYPE)
27510 cp_parser_abort_tentative_parse (parser);
27511 return false;
27514 pushed_scope = push_scope (type);
27517 /* Inside the constructor parameter list, surrounding
27518 template-parameter-lists do not apply. */
27519 saved_num_template_parameter_lists
27520 = parser->num_template_parameter_lists;
27521 parser->num_template_parameter_lists = 0;
27523 /* Look for the type-specifier. */
27524 cp_parser_type_specifier (parser,
27525 CP_PARSER_FLAGS_NONE,
27526 /*decl_specs=*/NULL,
27527 /*is_declarator=*/true,
27528 /*declares_class_or_enum=*/NULL,
27529 /*is_cv_qualifier=*/NULL);
27531 parser->num_template_parameter_lists
27532 = saved_num_template_parameter_lists;
27534 /* Leave the scope of the class. */
27535 if (pushed_scope)
27536 pop_scope (pushed_scope);
27538 constructor_p = !cp_parser_error_occurred (parser);
27542 /* We did not really want to consume any tokens. */
27543 cp_parser_abort_tentative_parse (parser);
27545 return constructor_p;
27548 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27549 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27550 they must be performed once we are in the scope of the function.
27552 Returns the function defined. */
27554 static tree
27555 cp_parser_function_definition_from_specifiers_and_declarator
27556 (cp_parser* parser,
27557 cp_decl_specifier_seq *decl_specifiers,
27558 tree attributes,
27559 const cp_declarator *declarator)
27561 tree fn;
27562 bool success_p;
27564 /* Begin the function-definition. */
27565 success_p = start_function (decl_specifiers, declarator, attributes);
27567 /* The things we're about to see are not directly qualified by any
27568 template headers we've seen thus far. */
27569 reset_specialization ();
27571 /* If there were names looked up in the decl-specifier-seq that we
27572 did not check, check them now. We must wait until we are in the
27573 scope of the function to perform the checks, since the function
27574 might be a friend. */
27575 perform_deferred_access_checks (tf_warning_or_error);
27577 if (success_p)
27579 cp_finalize_omp_declare_simd (parser, current_function_decl);
27580 parser->omp_declare_simd = NULL;
27581 cp_finalize_oacc_routine (parser, current_function_decl, true);
27582 parser->oacc_routine = NULL;
27585 if (!success_p)
27587 /* Skip the entire function. */
27588 cp_parser_skip_to_end_of_block_or_statement (parser);
27589 fn = error_mark_node;
27591 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27593 /* Seen already, skip it. An error message has already been output. */
27594 cp_parser_skip_to_end_of_block_or_statement (parser);
27595 fn = current_function_decl;
27596 current_function_decl = NULL_TREE;
27597 /* If this is a function from a class, pop the nested class. */
27598 if (current_class_name)
27599 pop_nested_class ();
27601 else
27603 timevar_id_t tv;
27604 if (DECL_DECLARED_INLINE_P (current_function_decl))
27605 tv = TV_PARSE_INLINE;
27606 else
27607 tv = TV_PARSE_FUNC;
27608 timevar_push (tv);
27609 fn = cp_parser_function_definition_after_declarator (parser,
27610 /*inline_p=*/false);
27611 timevar_pop (tv);
27614 return fn;
27617 /* Parse the part of a function-definition that follows the
27618 declarator. INLINE_P is TRUE iff this function is an inline
27619 function defined within a class-specifier.
27621 Returns the function defined. */
27623 static tree
27624 cp_parser_function_definition_after_declarator (cp_parser* parser,
27625 bool inline_p)
27627 tree fn;
27628 bool saved_in_unbraced_linkage_specification_p;
27629 bool saved_in_function_body;
27630 unsigned saved_num_template_parameter_lists;
27631 cp_token *token;
27632 bool fully_implicit_function_template_p
27633 = parser->fully_implicit_function_template_p;
27634 parser->fully_implicit_function_template_p = false;
27635 tree implicit_template_parms
27636 = parser->implicit_template_parms;
27637 parser->implicit_template_parms = 0;
27638 cp_binding_level* implicit_template_scope
27639 = parser->implicit_template_scope;
27640 parser->implicit_template_scope = 0;
27642 saved_in_function_body = parser->in_function_body;
27643 parser->in_function_body = true;
27644 /* If the next token is `return', then the code may be trying to
27645 make use of the "named return value" extension that G++ used to
27646 support. */
27647 token = cp_lexer_peek_token (parser->lexer);
27648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27650 /* Consume the `return' keyword. */
27651 cp_lexer_consume_token (parser->lexer);
27652 /* Look for the identifier that indicates what value is to be
27653 returned. */
27654 cp_parser_identifier (parser);
27655 /* Issue an error message. */
27656 error_at (token->location,
27657 "named return values are no longer supported");
27658 /* Skip tokens until we reach the start of the function body. */
27659 while (true)
27661 cp_token *token = cp_lexer_peek_token (parser->lexer);
27662 if (token->type == CPP_OPEN_BRACE
27663 || token->type == CPP_EOF
27664 || token->type == CPP_PRAGMA_EOL)
27665 break;
27666 cp_lexer_consume_token (parser->lexer);
27669 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27670 anything declared inside `f'. */
27671 saved_in_unbraced_linkage_specification_p
27672 = parser->in_unbraced_linkage_specification_p;
27673 parser->in_unbraced_linkage_specification_p = false;
27674 /* Inside the function, surrounding template-parameter-lists do not
27675 apply. */
27676 saved_num_template_parameter_lists
27677 = parser->num_template_parameter_lists;
27678 parser->num_template_parameter_lists = 0;
27680 /* If the next token is `try', `__transaction_atomic', or
27681 `__transaction_relaxed`, then we are looking at either function-try-block
27682 or function-transaction-block. Note that all of these include the
27683 function-body. */
27684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27685 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27686 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27687 RID_TRANSACTION_RELAXED))
27688 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27689 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27690 cp_parser_function_try_block (parser);
27691 else
27692 cp_parser_ctor_initializer_opt_and_function_body
27693 (parser, /*in_function_try_block=*/false);
27695 /* Finish the function. */
27696 fn = finish_function (inline_p);
27697 /* Generate code for it, if necessary. */
27698 expand_or_defer_fn (fn);
27699 /* Restore the saved values. */
27700 parser->in_unbraced_linkage_specification_p
27701 = saved_in_unbraced_linkage_specification_p;
27702 parser->num_template_parameter_lists
27703 = saved_num_template_parameter_lists;
27704 parser->in_function_body = saved_in_function_body;
27706 parser->fully_implicit_function_template_p
27707 = fully_implicit_function_template_p;
27708 parser->implicit_template_parms
27709 = implicit_template_parms;
27710 parser->implicit_template_scope
27711 = implicit_template_scope;
27713 if (parser->fully_implicit_function_template_p)
27714 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27716 return fn;
27719 /* Parse a template-declaration body (following argument list). */
27721 static void
27722 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27723 tree parameter_list,
27724 bool member_p)
27726 tree decl = NULL_TREE;
27727 bool friend_p = false;
27729 /* We just processed one more parameter list. */
27730 ++parser->num_template_parameter_lists;
27732 /* Get the deferred access checks from the parameter list. These
27733 will be checked once we know what is being declared, as for a
27734 member template the checks must be performed in the scope of the
27735 class containing the member. */
27736 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27738 /* Tentatively parse for a new template parameter list, which can either be
27739 the template keyword or a template introduction. */
27740 if (cp_parser_template_declaration_after_export (parser, member_p))
27741 /* OK */;
27742 else if (cxx_dialect >= cxx11
27743 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27744 decl = cp_parser_alias_declaration (parser);
27745 else
27747 /* There are no access checks when parsing a template, as we do not
27748 know if a specialization will be a friend. */
27749 push_deferring_access_checks (dk_no_check);
27750 cp_token *token = cp_lexer_peek_token (parser->lexer);
27751 decl = cp_parser_single_declaration (parser,
27752 checks,
27753 member_p,
27754 /*explicit_specialization_p=*/false,
27755 &friend_p);
27756 pop_deferring_access_checks ();
27758 /* If this is a member template declaration, let the front
27759 end know. */
27760 if (member_p && !friend_p && decl)
27762 if (TREE_CODE (decl) == TYPE_DECL)
27763 cp_parser_check_access_in_redeclaration (decl, token->location);
27765 decl = finish_member_template_decl (decl);
27767 else if (friend_p && decl
27768 && DECL_DECLARES_TYPE_P (decl))
27769 make_friend_class (current_class_type, TREE_TYPE (decl),
27770 /*complain=*/true);
27772 /* We are done with the current parameter list. */
27773 --parser->num_template_parameter_lists;
27775 pop_deferring_access_checks ();
27777 /* Finish up. */
27778 finish_template_decl (parameter_list);
27780 /* Check the template arguments for a literal operator template. */
27781 if (decl
27782 && DECL_DECLARES_FUNCTION_P (decl)
27783 && UDLIT_OPER_P (DECL_NAME (decl)))
27785 bool ok = true;
27786 if (parameter_list == NULL_TREE)
27787 ok = false;
27788 else
27790 int num_parms = TREE_VEC_LENGTH (parameter_list);
27791 if (num_parms == 1)
27793 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27794 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27795 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27796 /* OK, C++20 string literal operator template. We don't need
27797 to warn in lower dialects here because we will have already
27798 warned about the template parameter. */;
27799 else if (TREE_TYPE (parm) != char_type_node
27800 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27801 ok = false;
27803 else if (num_parms == 2 && cxx_dialect >= cxx14)
27805 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27806 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27807 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27808 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27809 if (parm == error_mark_node
27810 || TREE_TYPE (parm) != TREE_TYPE (type)
27811 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27812 ok = false;
27813 else
27814 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27815 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27816 "ISO C++ did not adopt string literal operator templa"
27817 "tes taking an argument pack of characters");
27819 else
27820 ok = false;
27822 if (!ok)
27824 if (cxx_dialect > cxx17)
27825 error ("literal operator template %qD has invalid parameter list;"
27826 " Expected non-type template parameter pack <char...> "
27827 " or single non-type parameter of class type",
27828 decl);
27829 else
27830 error ("literal operator template %qD has invalid parameter list."
27831 " Expected non-type template parameter pack <char...>",
27832 decl);
27836 /* Register member declarations. */
27837 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27838 finish_member_declaration (decl);
27839 /* If DECL is a function template, we must return to parse it later.
27840 (Even though there is no definition, there might be default
27841 arguments that need handling.) */
27842 if (member_p && decl
27843 && DECL_DECLARES_FUNCTION_P (decl))
27844 vec_safe_push (unparsed_funs_with_definitions, decl);
27847 /* Parse a template introduction header for a template-declaration. Returns
27848 false if tentative parse fails. */
27850 static bool
27851 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27853 cp_parser_parse_tentatively (parser);
27855 tree saved_scope = parser->scope;
27856 tree saved_object_scope = parser->object_scope;
27857 tree saved_qualifying_scope = parser->qualifying_scope;
27859 /* Look for the optional `::' operator. */
27860 cp_parser_global_scope_opt (parser,
27861 /*current_scope_valid_p=*/false);
27862 /* Look for the nested-name-specifier. */
27863 cp_parser_nested_name_specifier_opt (parser,
27864 /*typename_keyword_p=*/false,
27865 /*check_dependency_p=*/true,
27866 /*type_p=*/false,
27867 /*is_declaration=*/false);
27869 cp_token *token = cp_lexer_peek_token (parser->lexer);
27870 tree concept_name = cp_parser_identifier (parser);
27872 /* Look up the concept for which we will be matching
27873 template parameters. */
27874 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27875 token->location);
27876 parser->scope = saved_scope;
27877 parser->object_scope = saved_object_scope;
27878 parser->qualifying_scope = saved_qualifying_scope;
27880 if (concept_name == error_mark_node)
27881 cp_parser_simulate_error (parser);
27883 /* Look for opening brace for introduction. */
27884 matching_braces braces;
27885 braces.require_open (parser);
27887 if (!cp_parser_parse_definitely (parser))
27888 return false;
27890 push_deferring_access_checks (dk_deferred);
27892 /* Build vector of placeholder parameters and grab
27893 matching identifiers. */
27894 tree introduction_list = cp_parser_introduction_list (parser);
27896 /* Look for closing brace for introduction. */
27897 if (!braces.require_close (parser))
27898 return true;
27900 /* The introduction-list shall not be empty. */
27901 int nargs = TREE_VEC_LENGTH (introduction_list);
27902 if (nargs == 0)
27904 /* In cp_parser_introduction_list we have already issued an error. */
27905 return true;
27908 if (tmpl_decl == error_mark_node)
27910 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27911 token->location);
27912 return true;
27915 /* Build and associate the constraint. */
27916 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27917 if (parms && parms != error_mark_node)
27919 cp_parser_template_declaration_after_parameters (parser, parms,
27920 member_p);
27921 return true;
27924 error_at (token->location, "no matching concept for template-introduction");
27925 return true;
27928 /* Parse a normal template-declaration following the template keyword. */
27930 static void
27931 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27933 tree parameter_list;
27934 bool need_lang_pop;
27935 location_t location = input_location;
27937 /* Look for the `<' token. */
27938 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27939 return;
27940 if (at_class_scope_p () && current_function_decl)
27942 /* 14.5.2.2 [temp.mem]
27944 A local class shall not have member templates. */
27945 error_at (location,
27946 "invalid declaration of member template in local class");
27947 cp_parser_skip_to_end_of_block_or_statement (parser);
27948 return;
27950 /* [temp]
27952 A template ... shall not have C linkage. */
27953 if (current_lang_name == lang_name_c)
27955 error_at (location, "template with C linkage");
27956 maybe_show_extern_c_location ();
27957 /* Give it C++ linkage to avoid confusing other parts of the
27958 front end. */
27959 push_lang_context (lang_name_cplusplus);
27960 need_lang_pop = true;
27962 else
27963 need_lang_pop = false;
27965 /* We cannot perform access checks on the template parameter
27966 declarations until we know what is being declared, just as we
27967 cannot check the decl-specifier list. */
27968 push_deferring_access_checks (dk_deferred);
27970 /* If the next token is `>', then we have an invalid
27971 specialization. Rather than complain about an invalid template
27972 parameter, issue an error message here. */
27973 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27975 cp_parser_error (parser, "invalid explicit specialization");
27976 begin_specialization ();
27977 parameter_list = NULL_TREE;
27979 else
27981 /* Parse the template parameters. */
27982 parameter_list = cp_parser_template_parameter_list (parser);
27985 /* Look for the `>'. */
27986 cp_parser_skip_to_end_of_template_parameter_list (parser);
27988 /* Manage template requirements */
27989 if (flag_concepts)
27991 tree reqs = get_shorthand_constraints (current_template_parms);
27992 if (tree r = cp_parser_requires_clause_opt (parser))
27993 reqs = conjoin_constraints (reqs, normalize_expression (r));
27994 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27997 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27998 member_p);
28000 /* For the erroneous case of a template with C linkage, we pushed an
28001 implicit C++ linkage scope; exit that scope now. */
28002 if (need_lang_pop)
28003 pop_lang_context ();
28006 /* Parse a template-declaration, assuming that the `export' (and
28007 `extern') keywords, if present, has already been scanned. MEMBER_P
28008 is as for cp_parser_template_declaration. */
28010 static bool
28011 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28015 cp_lexer_consume_token (parser->lexer);
28016 cp_parser_explicit_template_declaration (parser, member_p);
28017 return true;
28019 else if (flag_concepts)
28020 return cp_parser_template_introduction (parser, member_p);
28022 return false;
28025 /* Perform the deferred access checks from a template-parameter-list.
28026 CHECKS is a TREE_LIST of access checks, as returned by
28027 get_deferred_access_checks. */
28029 static void
28030 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28032 ++processing_template_parmlist;
28033 perform_access_checks (checks, tf_warning_or_error);
28034 --processing_template_parmlist;
28037 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28038 `function-definition' sequence that follows a template header.
28039 If MEMBER_P is true, this declaration appears in a class scope.
28041 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28042 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28044 static tree
28045 cp_parser_single_declaration (cp_parser* parser,
28046 vec<deferred_access_check, va_gc> *checks,
28047 bool member_p,
28048 bool explicit_specialization_p,
28049 bool* friend_p)
28051 int declares_class_or_enum;
28052 tree decl = NULL_TREE;
28053 cp_decl_specifier_seq decl_specifiers;
28054 bool function_definition_p = false;
28055 cp_token *decl_spec_token_start;
28057 /* This function is only used when processing a template
28058 declaration. */
28059 gcc_assert (innermost_scope_kind () == sk_template_parms
28060 || innermost_scope_kind () == sk_template_spec);
28062 /* Defer access checks until we know what is being declared. */
28063 push_deferring_access_checks (dk_deferred);
28065 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28066 alternative. */
28067 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28068 cp_parser_decl_specifier_seq (parser,
28069 (CP_PARSER_FLAGS_OPTIONAL
28070 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28071 &decl_specifiers,
28072 &declares_class_or_enum);
28073 if (friend_p)
28074 *friend_p = cp_parser_friend_p (&decl_specifiers);
28076 /* There are no template typedefs. */
28077 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28079 error_at (decl_spec_token_start->location,
28080 "template declaration of %<typedef%>");
28081 decl = error_mark_node;
28084 /* Gather up the access checks that occurred the
28085 decl-specifier-seq. */
28086 stop_deferring_access_checks ();
28088 /* Check for the declaration of a template class. */
28089 if (declares_class_or_enum)
28091 if (cp_parser_declares_only_class_p (parser)
28092 || (declares_class_or_enum & 2))
28094 // If this is a declaration, but not a definition, associate
28095 // any constraints with the type declaration. Constraints
28096 // are associated with definitions in cp_parser_class_specifier.
28097 if (declares_class_or_enum == 1)
28098 associate_classtype_constraints (decl_specifiers.type);
28100 decl = shadow_tag (&decl_specifiers);
28102 /* In this case:
28104 struct C {
28105 friend template <typename T> struct A<T>::B;
28108 A<T>::B will be represented by a TYPENAME_TYPE, and
28109 therefore not recognized by shadow_tag. */
28110 if (friend_p && *friend_p
28111 && !decl
28112 && decl_specifiers.type
28113 && TYPE_P (decl_specifiers.type))
28114 decl = decl_specifiers.type;
28116 if (decl && decl != error_mark_node)
28117 decl = TYPE_NAME (decl);
28118 else
28119 decl = error_mark_node;
28121 /* Perform access checks for template parameters. */
28122 cp_parser_perform_template_parameter_access_checks (checks);
28124 /* Give a helpful diagnostic for
28125 template <class T> struct A { } a;
28126 if we aren't already recovering from an error. */
28127 if (!cp_parser_declares_only_class_p (parser)
28128 && !seen_error ())
28130 error_at (cp_lexer_peek_token (parser->lexer)->location,
28131 "a class template declaration must not declare "
28132 "anything else");
28133 cp_parser_skip_to_end_of_block_or_statement (parser);
28134 goto out;
28139 /* Complain about missing 'typename' or other invalid type names. */
28140 if (!decl_specifiers.any_type_specifiers_p
28141 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28143 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28144 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28145 the rest of this declaration. */
28146 decl = error_mark_node;
28147 goto out;
28150 /* If it's not a template class, try for a template function. If
28151 the next token is a `;', then this declaration does not declare
28152 anything. But, if there were errors in the decl-specifiers, then
28153 the error might well have come from an attempted class-specifier.
28154 In that case, there's no need to warn about a missing declarator. */
28155 if (!decl
28156 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28157 || decl_specifiers.type != error_mark_node))
28159 decl = cp_parser_init_declarator (parser,
28160 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28161 &decl_specifiers,
28162 checks,
28163 /*function_definition_allowed_p=*/true,
28164 member_p,
28165 declares_class_or_enum,
28166 &function_definition_p,
28167 NULL, NULL, NULL);
28169 /* 7.1.1-1 [dcl.stc]
28171 A storage-class-specifier shall not be specified in an explicit
28172 specialization... */
28173 if (decl
28174 && explicit_specialization_p
28175 && decl_specifiers.storage_class != sc_none)
28177 error_at (decl_spec_token_start->location,
28178 "explicit template specialization cannot have a storage class");
28179 decl = error_mark_node;
28182 if (decl && VAR_P (decl))
28183 check_template_variable (decl);
28186 /* Look for a trailing `;' after the declaration. */
28187 if (!function_definition_p
28188 && (decl == error_mark_node
28189 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28190 cp_parser_skip_to_end_of_block_or_statement (parser);
28192 out:
28193 pop_deferring_access_checks ();
28195 /* Clear any current qualification; whatever comes next is the start
28196 of something new. */
28197 parser->scope = NULL_TREE;
28198 parser->qualifying_scope = NULL_TREE;
28199 parser->object_scope = NULL_TREE;
28201 return decl;
28204 /* Parse a cast-expression that is not the operand of a unary "&". */
28206 static cp_expr
28207 cp_parser_simple_cast_expression (cp_parser *parser)
28209 return cp_parser_cast_expression (parser, /*address_p=*/false,
28210 /*cast_p=*/false, /*decltype*/false, NULL);
28213 /* Parse a functional cast to TYPE. Returns an expression
28214 representing the cast. */
28216 static cp_expr
28217 cp_parser_functional_cast (cp_parser* parser, tree type)
28219 vec<tree, va_gc> *vec;
28220 tree expression_list;
28221 cp_expr cast;
28222 bool nonconst_p;
28224 location_t start_loc = input_location;
28226 if (!type)
28227 type = error_mark_node;
28229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28231 cp_lexer_set_source_position (parser->lexer);
28232 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28233 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28234 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28235 if (TREE_CODE (type) == TYPE_DECL)
28236 type = TREE_TYPE (type);
28238 cast = finish_compound_literal (type, expression_list,
28239 tf_warning_or_error, fcl_functional);
28240 /* Create a location of the form:
28241 type_name{i, f}
28242 ^~~~~~~~~~~~~~~
28243 with caret == start at the start of the type name,
28244 finishing at the closing brace. */
28245 location_t finish_loc
28246 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28247 location_t combined_loc = make_location (start_loc, start_loc,
28248 finish_loc);
28249 cast.set_location (combined_loc);
28250 return cast;
28254 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28255 /*cast_p=*/true,
28256 /*allow_expansion_p=*/true,
28257 /*non_constant_p=*/NULL);
28258 if (vec == NULL)
28259 expression_list = error_mark_node;
28260 else
28262 expression_list = build_tree_list_vec (vec);
28263 release_tree_vector (vec);
28266 cast = build_functional_cast (type, expression_list,
28267 tf_warning_or_error);
28268 /* [expr.const]/1: In an integral constant expression "only type
28269 conversions to integral or enumeration type can be used". */
28270 if (TREE_CODE (type) == TYPE_DECL)
28271 type = TREE_TYPE (type);
28272 if (cast != error_mark_node
28273 && !cast_valid_in_integral_constant_expression_p (type)
28274 && cp_parser_non_integral_constant_expression (parser,
28275 NIC_CONSTRUCTOR))
28276 return error_mark_node;
28278 /* Create a location of the form:
28279 float(i)
28280 ^~~~~~~~
28281 with caret == start at the start of the type name,
28282 finishing at the closing paren. */
28283 location_t finish_loc
28284 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28285 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28286 cast.set_location (combined_loc);
28287 return cast;
28290 /* Save the tokens that make up the body of a member function defined
28291 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28292 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28293 specifiers applied to the declaration. Returns the FUNCTION_DECL
28294 for the member function. */
28296 static tree
28297 cp_parser_save_member_function_body (cp_parser* parser,
28298 cp_decl_specifier_seq *decl_specifiers,
28299 cp_declarator *declarator,
28300 tree attributes)
28302 cp_token *first;
28303 cp_token *last;
28304 tree fn;
28305 bool function_try_block = false;
28307 /* Create the FUNCTION_DECL. */
28308 fn = grokmethod (decl_specifiers, declarator, attributes);
28309 cp_finalize_omp_declare_simd (parser, fn);
28310 cp_finalize_oacc_routine (parser, fn, true);
28311 /* If something went badly wrong, bail out now. */
28312 if (fn == error_mark_node)
28314 /* If there's a function-body, skip it. */
28315 if (cp_parser_token_starts_function_definition_p
28316 (cp_lexer_peek_token (parser->lexer)))
28317 cp_parser_skip_to_end_of_block_or_statement (parser);
28318 return error_mark_node;
28321 /* Remember it, if there default args to post process. */
28322 cp_parser_save_default_args (parser, fn);
28324 /* Save away the tokens that make up the body of the
28325 function. */
28326 first = parser->lexer->next_token;
28328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28329 cp_lexer_consume_token (parser->lexer);
28330 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28331 RID_TRANSACTION_ATOMIC))
28333 cp_lexer_consume_token (parser->lexer);
28334 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28336 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28337 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28338 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28339 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28340 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28342 cp_lexer_consume_token (parser->lexer);
28343 cp_lexer_consume_token (parser->lexer);
28344 cp_lexer_consume_token (parser->lexer);
28345 cp_lexer_consume_token (parser->lexer);
28346 cp_lexer_consume_token (parser->lexer);
28348 else
28349 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28350 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28352 cp_lexer_consume_token (parser->lexer);
28353 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28354 break;
28358 /* Handle function try blocks. */
28359 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28361 cp_lexer_consume_token (parser->lexer);
28362 function_try_block = true;
28364 /* We can have braced-init-list mem-initializers before the fn body. */
28365 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28367 cp_lexer_consume_token (parser->lexer);
28368 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28370 /* cache_group will stop after an un-nested { } pair, too. */
28371 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28372 break;
28374 /* variadic mem-inits have ... after the ')'. */
28375 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28376 cp_lexer_consume_token (parser->lexer);
28379 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28380 /* Handle function try blocks. */
28381 if (function_try_block)
28382 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28383 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28384 last = parser->lexer->next_token;
28386 /* Save away the inline definition; we will process it when the
28387 class is complete. */
28388 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28389 DECL_PENDING_INLINE_P (fn) = 1;
28391 /* We need to know that this was defined in the class, so that
28392 friend templates are handled correctly. */
28393 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28395 /* Add FN to the queue of functions to be parsed later. */
28396 vec_safe_push (unparsed_funs_with_definitions, fn);
28398 return fn;
28401 /* Save the tokens that make up the in-class initializer for a non-static
28402 data member. Returns a DEFAULT_ARG. */
28404 static tree
28405 cp_parser_save_nsdmi (cp_parser* parser)
28407 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28410 /* Parse a template-argument-list, as well as the trailing ">" (but
28411 not the opening "<"). See cp_parser_template_argument_list for the
28412 return value. */
28414 static tree
28415 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28417 tree arguments;
28418 tree saved_scope;
28419 tree saved_qualifying_scope;
28420 tree saved_object_scope;
28421 bool saved_greater_than_is_operator_p;
28423 /* [temp.names]
28425 When parsing a template-id, the first non-nested `>' is taken as
28426 the end of the template-argument-list rather than a greater-than
28427 operator. */
28428 saved_greater_than_is_operator_p
28429 = parser->greater_than_is_operator_p;
28430 parser->greater_than_is_operator_p = false;
28431 /* Parsing the argument list may modify SCOPE, so we save it
28432 here. */
28433 saved_scope = parser->scope;
28434 saved_qualifying_scope = parser->qualifying_scope;
28435 saved_object_scope = parser->object_scope;
28436 /* We need to evaluate the template arguments, even though this
28437 template-id may be nested within a "sizeof". */
28438 cp_evaluated ev;
28439 /* Parse the template-argument-list itself. */
28440 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28441 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28442 arguments = NULL_TREE;
28443 else
28444 arguments = cp_parser_template_argument_list (parser);
28445 /* Look for the `>' that ends the template-argument-list. If we find
28446 a '>>' instead, it's probably just a typo. */
28447 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28449 if (cxx_dialect != cxx98)
28451 /* In C++0x, a `>>' in a template argument list or cast
28452 expression is considered to be two separate `>'
28453 tokens. So, change the current token to a `>', but don't
28454 consume it: it will be consumed later when the outer
28455 template argument list (or cast expression) is parsed.
28456 Note that this replacement of `>' for `>>' is necessary
28457 even if we are parsing tentatively: in the tentative
28458 case, after calling
28459 cp_parser_enclosed_template_argument_list we will always
28460 throw away all of the template arguments and the first
28461 closing `>', either because the template argument list
28462 was erroneous or because we are replacing those tokens
28463 with a CPP_TEMPLATE_ID token. The second `>' (which will
28464 not have been thrown away) is needed either to close an
28465 outer template argument list or to complete a new-style
28466 cast. */
28467 cp_token *token = cp_lexer_peek_token (parser->lexer);
28468 token->type = CPP_GREATER;
28470 else if (!saved_greater_than_is_operator_p)
28472 /* If we're in a nested template argument list, the '>>' has
28473 to be a typo for '> >'. We emit the error message, but we
28474 continue parsing and we push a '>' as next token, so that
28475 the argument list will be parsed correctly. Note that the
28476 global source location is still on the token before the
28477 '>>', so we need to say explicitly where we want it. */
28478 cp_token *token = cp_lexer_peek_token (parser->lexer);
28479 gcc_rich_location richloc (token->location);
28480 richloc.add_fixit_replace ("> >");
28481 error_at (&richloc, "%<>>%> should be %<> >%> "
28482 "within a nested template argument list");
28484 token->type = CPP_GREATER;
28486 else
28488 /* If this is not a nested template argument list, the '>>'
28489 is a typo for '>'. Emit an error message and continue.
28490 Same deal about the token location, but here we can get it
28491 right by consuming the '>>' before issuing the diagnostic. */
28492 cp_token *token = cp_lexer_consume_token (parser->lexer);
28493 error_at (token->location,
28494 "spurious %<>>%>, use %<>%> to terminate "
28495 "a template argument list");
28498 else
28499 cp_parser_skip_to_end_of_template_parameter_list (parser);
28500 /* The `>' token might be a greater-than operator again now. */
28501 parser->greater_than_is_operator_p
28502 = saved_greater_than_is_operator_p;
28503 /* Restore the SAVED_SCOPE. */
28504 parser->scope = saved_scope;
28505 parser->qualifying_scope = saved_qualifying_scope;
28506 parser->object_scope = saved_object_scope;
28508 return arguments;
28511 /* MEMBER_FUNCTION is a member function, or a friend. If default
28512 arguments, or the body of the function have not yet been parsed,
28513 parse them now. */
28515 static void
28516 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28518 timevar_push (TV_PARSE_INMETH);
28519 /* If this member is a template, get the underlying
28520 FUNCTION_DECL. */
28521 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28522 member_function = DECL_TEMPLATE_RESULT (member_function);
28524 /* There should not be any class definitions in progress at this
28525 point; the bodies of members are only parsed outside of all class
28526 definitions. */
28527 gcc_assert (parser->num_classes_being_defined == 0);
28528 /* While we're parsing the member functions we might encounter more
28529 classes. We want to handle them right away, but we don't want
28530 them getting mixed up with functions that are currently in the
28531 queue. */
28532 push_unparsed_function_queues (parser);
28534 /* Make sure that any template parameters are in scope. */
28535 maybe_begin_member_template_processing (member_function);
28537 /* If the body of the function has not yet been parsed, parse it
28538 now. */
28539 if (DECL_PENDING_INLINE_P (member_function))
28541 tree function_scope;
28542 cp_token_cache *tokens;
28544 /* The function is no longer pending; we are processing it. */
28545 tokens = DECL_PENDING_INLINE_INFO (member_function);
28546 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28547 DECL_PENDING_INLINE_P (member_function) = 0;
28549 /* If this is a local class, enter the scope of the containing
28550 function. */
28551 function_scope = current_function_decl;
28552 if (function_scope)
28553 push_function_context ();
28555 /* Push the body of the function onto the lexer stack. */
28556 cp_parser_push_lexer_for_tokens (parser, tokens);
28558 /* Let the front end know that we going to be defining this
28559 function. */
28560 start_preparsed_function (member_function, NULL_TREE,
28561 SF_PRE_PARSED | SF_INCLASS_INLINE);
28563 /* Don't do access checking if it is a templated function. */
28564 if (processing_template_decl)
28565 push_deferring_access_checks (dk_no_check);
28567 /* #pragma omp declare reduction needs special parsing. */
28568 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28570 parser->lexer->in_pragma = true;
28571 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28572 finish_function (/*inline_p=*/true);
28573 cp_check_omp_declare_reduction (member_function);
28575 else
28576 /* Now, parse the body of the function. */
28577 cp_parser_function_definition_after_declarator (parser,
28578 /*inline_p=*/true);
28580 if (processing_template_decl)
28581 pop_deferring_access_checks ();
28583 /* Leave the scope of the containing function. */
28584 if (function_scope)
28585 pop_function_context ();
28586 cp_parser_pop_lexer (parser);
28589 /* Remove any template parameters from the symbol table. */
28590 maybe_end_member_template_processing ();
28592 /* Restore the queue. */
28593 pop_unparsed_function_queues (parser);
28594 timevar_pop (TV_PARSE_INMETH);
28597 /* If DECL contains any default args, remember it on the unparsed
28598 functions queue. */
28600 static void
28601 cp_parser_save_default_args (cp_parser* parser, tree decl)
28603 tree probe;
28605 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28606 probe;
28607 probe = TREE_CHAIN (probe))
28608 if (TREE_PURPOSE (probe))
28610 cp_default_arg_entry entry = {current_class_type, decl};
28611 vec_safe_push (unparsed_funs_with_default_args, entry);
28612 break;
28616 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28617 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28618 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28619 from the parameter-type-list. */
28621 static tree
28622 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28623 tree default_arg, tree parmtype)
28625 cp_token_cache *tokens;
28626 tree parsed_arg;
28627 bool dummy;
28629 if (default_arg == error_mark_node)
28630 return error_mark_node;
28632 /* Push the saved tokens for the default argument onto the parser's
28633 lexer stack. */
28634 tokens = DEFARG_TOKENS (default_arg);
28635 cp_parser_push_lexer_for_tokens (parser, tokens);
28637 start_lambda_scope (decl);
28639 /* Parse the default argument. */
28640 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28641 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28644 finish_lambda_scope ();
28646 if (parsed_arg == error_mark_node)
28647 cp_parser_skip_to_end_of_statement (parser);
28649 if (!processing_template_decl)
28651 /* In a non-template class, check conversions now. In a template,
28652 we'll wait and instantiate these as needed. */
28653 if (TREE_CODE (decl) == PARM_DECL)
28654 parsed_arg = check_default_argument (parmtype, parsed_arg,
28655 tf_warning_or_error);
28656 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28657 parsed_arg = error_mark_node;
28658 else
28659 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28662 /* If the token stream has not been completely used up, then
28663 there was extra junk after the end of the default
28664 argument. */
28665 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28667 if (TREE_CODE (decl) == PARM_DECL)
28668 cp_parser_error (parser, "expected %<,%>");
28669 else
28670 cp_parser_error (parser, "expected %<;%>");
28673 /* Revert to the main lexer. */
28674 cp_parser_pop_lexer (parser);
28676 return parsed_arg;
28679 /* FIELD is a non-static data member with an initializer which we saved for
28680 later; parse it now. */
28682 static void
28683 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28685 tree def;
28687 maybe_begin_member_template_processing (field);
28689 push_unparsed_function_queues (parser);
28690 def = cp_parser_late_parse_one_default_arg (parser, field,
28691 DECL_INITIAL (field),
28692 NULL_TREE);
28693 pop_unparsed_function_queues (parser);
28695 maybe_end_member_template_processing ();
28697 DECL_INITIAL (field) = def;
28700 /* FN is a FUNCTION_DECL which may contains a parameter with an
28701 unparsed DEFAULT_ARG. Parse the default args now. This function
28702 assumes that the current scope is the scope in which the default
28703 argument should be processed. */
28705 static void
28706 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28708 unsigned char saved_local_variables_forbidden_p;
28709 tree parm, parmdecl;
28711 /* While we're parsing the default args, we might (due to the
28712 statement expression extension) encounter more classes. We want
28713 to handle them right away, but we don't want them getting mixed
28714 up with default args that are currently in the queue. */
28715 push_unparsed_function_queues (parser);
28717 /* Local variable names (and the `this' keyword) may not appear
28718 in a default argument. */
28719 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28720 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28722 push_defarg_context (fn);
28724 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28725 parmdecl = DECL_ARGUMENTS (fn);
28726 parm && parm != void_list_node;
28727 parm = TREE_CHAIN (parm),
28728 parmdecl = DECL_CHAIN (parmdecl))
28730 tree default_arg = TREE_PURPOSE (parm);
28731 tree parsed_arg;
28732 vec<tree, va_gc> *insts;
28733 tree copy;
28734 unsigned ix;
28736 if (!default_arg)
28737 continue;
28739 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28740 /* This can happen for a friend declaration for a function
28741 already declared with default arguments. */
28742 continue;
28744 parsed_arg
28745 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28746 default_arg,
28747 TREE_VALUE (parm));
28748 TREE_PURPOSE (parm) = parsed_arg;
28750 /* Update any instantiations we've already created. */
28751 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28752 vec_safe_iterate (insts, ix, &copy); ix++)
28753 TREE_PURPOSE (copy) = parsed_arg;
28756 pop_defarg_context ();
28758 /* Make sure no default arg is missing. */
28759 check_default_args (fn);
28761 /* Restore the state of local_variables_forbidden_p. */
28762 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28764 /* Restore the queue. */
28765 pop_unparsed_function_queues (parser);
28768 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28770 sizeof ... ( identifier )
28772 where the 'sizeof' token has already been consumed. */
28774 static tree
28775 cp_parser_sizeof_pack (cp_parser *parser)
28777 /* Consume the `...'. */
28778 cp_lexer_consume_token (parser->lexer);
28779 maybe_warn_variadic_templates ();
28781 matching_parens parens;
28782 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28783 if (paren)
28784 parens.consume_open (parser);
28785 else
28786 permerror (cp_lexer_peek_token (parser->lexer)->location,
28787 "%<sizeof...%> argument must be surrounded by parentheses");
28789 cp_token *token = cp_lexer_peek_token (parser->lexer);
28790 tree name = cp_parser_identifier (parser);
28791 if (name == error_mark_node)
28792 return error_mark_node;
28793 /* The name is not qualified. */
28794 parser->scope = NULL_TREE;
28795 parser->qualifying_scope = NULL_TREE;
28796 parser->object_scope = NULL_TREE;
28797 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28798 if (expr == error_mark_node)
28799 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28800 token->location);
28801 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28802 expr = TREE_TYPE (expr);
28803 else if (TREE_CODE (expr) == CONST_DECL)
28804 expr = DECL_INITIAL (expr);
28805 expr = make_pack_expansion (expr);
28806 PACK_EXPANSION_SIZEOF_P (expr) = true;
28808 if (paren)
28809 parens.require_close (parser);
28811 return expr;
28814 /* Parse the operand of `sizeof' (or a similar operator). Returns
28815 either a TYPE or an expression, depending on the form of the
28816 input. The KEYWORD indicates which kind of expression we have
28817 encountered. */
28819 static tree
28820 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28822 tree expr = NULL_TREE;
28823 const char *saved_message;
28824 char *tmp;
28825 bool saved_integral_constant_expression_p;
28826 bool saved_non_integral_constant_expression_p;
28828 /* If it's a `...', then we are computing the length of a parameter
28829 pack. */
28830 if (keyword == RID_SIZEOF
28831 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28832 return cp_parser_sizeof_pack (parser);
28834 /* Types cannot be defined in a `sizeof' expression. Save away the
28835 old message. */
28836 saved_message = parser->type_definition_forbidden_message;
28837 /* And create the new one. */
28838 tmp = concat ("types may not be defined in %<",
28839 IDENTIFIER_POINTER (ridpointers[keyword]),
28840 "%> expressions", NULL);
28841 parser->type_definition_forbidden_message = tmp;
28843 /* The restrictions on constant-expressions do not apply inside
28844 sizeof expressions. */
28845 saved_integral_constant_expression_p
28846 = parser->integral_constant_expression_p;
28847 saved_non_integral_constant_expression_p
28848 = parser->non_integral_constant_expression_p;
28849 parser->integral_constant_expression_p = false;
28851 /* Do not actually evaluate the expression. */
28852 ++cp_unevaluated_operand;
28853 ++c_inhibit_evaluation_warnings;
28854 /* If it's a `(', then we might be looking at the type-id
28855 construction. */
28856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28858 tree type = NULL_TREE;
28860 /* We can't be sure yet whether we're looking at a type-id or an
28861 expression. */
28862 cp_parser_parse_tentatively (parser);
28864 matching_parens parens;
28865 parens.consume_open (parser);
28867 /* Note: as a GNU Extension, compound literals are considered
28868 postfix-expressions as they are in C99, so they are valid
28869 arguments to sizeof. See comment in cp_parser_cast_expression
28870 for details. */
28871 if (cp_parser_compound_literal_p (parser))
28872 cp_parser_simulate_error (parser);
28873 else
28875 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28876 parser->in_type_id_in_expr_p = true;
28877 /* Look for the type-id. */
28878 type = cp_parser_type_id (parser);
28879 /* Look for the closing `)'. */
28880 parens.require_close (parser);
28881 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28884 /* If all went well, then we're done. */
28885 if (cp_parser_parse_definitely (parser))
28886 expr = type;
28889 /* If the type-id production did not work out, then we must be
28890 looking at the unary-expression production. */
28891 if (!expr)
28892 expr = cp_parser_unary_expression (parser);
28894 /* Go back to evaluating expressions. */
28895 --cp_unevaluated_operand;
28896 --c_inhibit_evaluation_warnings;
28898 /* Free the message we created. */
28899 free (tmp);
28900 /* And restore the old one. */
28901 parser->type_definition_forbidden_message = saved_message;
28902 parser->integral_constant_expression_p
28903 = saved_integral_constant_expression_p;
28904 parser->non_integral_constant_expression_p
28905 = saved_non_integral_constant_expression_p;
28907 return expr;
28910 /* If the current declaration has no declarator, return true. */
28912 static bool
28913 cp_parser_declares_only_class_p (cp_parser *parser)
28915 /* If the next token is a `;' or a `,' then there is no
28916 declarator. */
28917 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28918 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28921 /* Update the DECL_SPECS to reflect the storage class indicated by
28922 KEYWORD. */
28924 static void
28925 cp_parser_set_storage_class (cp_parser *parser,
28926 cp_decl_specifier_seq *decl_specs,
28927 enum rid keyword,
28928 cp_token *token)
28930 cp_storage_class storage_class;
28932 if (parser->in_unbraced_linkage_specification_p)
28934 error_at (token->location, "invalid use of %qD in linkage specification",
28935 ridpointers[keyword]);
28936 return;
28938 else if (decl_specs->storage_class != sc_none)
28940 decl_specs->conflicting_specifiers_p = true;
28941 return;
28944 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28945 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28946 && decl_specs->gnu_thread_keyword_p)
28948 pedwarn (decl_specs->locations[ds_thread], 0,
28949 "%<__thread%> before %qD", ridpointers[keyword]);
28952 switch (keyword)
28954 case RID_AUTO:
28955 storage_class = sc_auto;
28956 break;
28957 case RID_REGISTER:
28958 storage_class = sc_register;
28959 break;
28960 case RID_STATIC:
28961 storage_class = sc_static;
28962 break;
28963 case RID_EXTERN:
28964 storage_class = sc_extern;
28965 break;
28966 case RID_MUTABLE:
28967 storage_class = sc_mutable;
28968 break;
28969 default:
28970 gcc_unreachable ();
28972 decl_specs->storage_class = storage_class;
28973 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28975 /* A storage class specifier cannot be applied alongside a typedef
28976 specifier. If there is a typedef specifier present then set
28977 conflicting_specifiers_p which will trigger an error later
28978 on in grokdeclarator. */
28979 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28980 decl_specs->conflicting_specifiers_p = true;
28983 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28984 is true, the type is a class or enum definition. */
28986 static void
28987 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28988 tree type_spec,
28989 cp_token *token,
28990 bool type_definition_p)
28992 decl_specs->any_specifiers_p = true;
28994 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
28995 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
28996 this is what happened. In system headers, we ignore these
28997 declarations so that G++ can work with system headers that are not
28998 C++-safe. */
28999 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29000 && !type_definition_p
29001 && (type_spec == boolean_type_node
29002 || type_spec == char8_type_node
29003 || type_spec == char16_type_node
29004 || type_spec == char32_type_node
29005 || type_spec == wchar_type_node)
29006 && (decl_specs->type
29007 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29008 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29009 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29010 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29012 decl_specs->redefined_builtin_type = type_spec;
29013 set_and_check_decl_spec_loc (decl_specs,
29014 ds_redefined_builtin_type_spec,
29015 token);
29016 if (!decl_specs->type)
29018 decl_specs->type = type_spec;
29019 decl_specs->type_definition_p = false;
29020 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29023 else if (decl_specs->type)
29024 decl_specs->multiple_types_p = true;
29025 else
29027 decl_specs->type = type_spec;
29028 decl_specs->type_definition_p = type_definition_p;
29029 decl_specs->redefined_builtin_type = NULL_TREE;
29030 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29034 /* True iff TOKEN is the GNU keyword __thread. */
29036 static bool
29037 token_is__thread (cp_token *token)
29039 gcc_assert (token->keyword == RID_THREAD);
29040 return id_equal (token->u.value, "__thread");
29043 /* Set the location for a declarator specifier and check if it is
29044 duplicated.
29046 DECL_SPECS is the sequence of declarator specifiers onto which to
29047 set the location.
29049 DS is the single declarator specifier to set which location is to
29050 be set onto the existing sequence of declarators.
29052 LOCATION is the location for the declarator specifier to
29053 consider. */
29055 static void
29056 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29057 cp_decl_spec ds, cp_token *token)
29059 gcc_assert (ds < ds_last);
29061 if (decl_specs == NULL)
29062 return;
29064 location_t location = token->location;
29066 if (decl_specs->locations[ds] == 0)
29068 decl_specs->locations[ds] = location;
29069 if (ds == ds_thread)
29070 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29072 else
29074 if (ds == ds_long)
29076 if (decl_specs->locations[ds_long_long] != 0)
29077 error_at (location,
29078 "%<long long long%> is too long for GCC");
29079 else
29081 decl_specs->locations[ds_long_long] = location;
29082 pedwarn_cxx98 (location,
29083 OPT_Wlong_long,
29084 "ISO C++ 1998 does not support %<long long%>");
29087 else if (ds == ds_thread)
29089 bool gnu = token_is__thread (token);
29090 gcc_rich_location richloc (location);
29091 if (gnu != decl_specs->gnu_thread_keyword_p)
29093 richloc.add_range (decl_specs->locations[ds_thread]);
29094 error_at (&richloc,
29095 "both %<__thread%> and %<thread_local%> specified");
29097 else
29099 richloc.add_fixit_remove ();
29100 error_at (&richloc, "duplicate %qD", token->u.value);
29103 else
29105 static const char *const decl_spec_names[] = {
29106 "signed",
29107 "unsigned",
29108 "short",
29109 "long",
29110 "const",
29111 "volatile",
29112 "restrict",
29113 "inline",
29114 "virtual",
29115 "explicit",
29116 "friend",
29117 "typedef",
29118 "using",
29119 "constexpr",
29120 "__complex"
29122 gcc_rich_location richloc (location);
29123 richloc.add_fixit_remove ();
29124 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29129 /* Return true iff the declarator specifier DS is present in the
29130 sequence of declarator specifiers DECL_SPECS. */
29132 bool
29133 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29134 cp_decl_spec ds)
29136 gcc_assert (ds < ds_last);
29138 if (decl_specs == NULL)
29139 return false;
29141 return decl_specs->locations[ds] != 0;
29144 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29145 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29147 static bool
29148 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29150 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29153 /* Issue an error message indicating that TOKEN_DESC was expected.
29154 If KEYWORD is true, it indicated this function is called by
29155 cp_parser_require_keword and the required token can only be
29156 a indicated keyword.
29158 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29159 within any error as the location of an "opening" token matching
29160 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29161 RT_CLOSE_PAREN). */
29163 static void
29164 cp_parser_required_error (cp_parser *parser,
29165 required_token token_desc,
29166 bool keyword,
29167 location_t matching_location)
29169 if (cp_parser_simulate_error (parser))
29170 return;
29172 const char *gmsgid = NULL;
29173 switch (token_desc)
29175 case RT_NEW:
29176 gmsgid = G_("expected %<new%>");
29177 break;
29178 case RT_DELETE:
29179 gmsgid = G_("expected %<delete%>");
29180 break;
29181 case RT_RETURN:
29182 gmsgid = G_("expected %<return%>");
29183 break;
29184 case RT_WHILE:
29185 gmsgid = G_("expected %<while%>");
29186 break;
29187 case RT_EXTERN:
29188 gmsgid = G_("expected %<extern%>");
29189 break;
29190 case RT_STATIC_ASSERT:
29191 gmsgid = G_("expected %<static_assert%>");
29192 break;
29193 case RT_DECLTYPE:
29194 gmsgid = G_("expected %<decltype%>");
29195 break;
29196 case RT_OPERATOR:
29197 gmsgid = G_("expected %<operator%>");
29198 break;
29199 case RT_CLASS:
29200 gmsgid = G_("expected %<class%>");
29201 break;
29202 case RT_TEMPLATE:
29203 gmsgid = G_("expected %<template%>");
29204 break;
29205 case RT_NAMESPACE:
29206 gmsgid = G_("expected %<namespace%>");
29207 break;
29208 case RT_USING:
29209 gmsgid = G_("expected %<using%>");
29210 break;
29211 case RT_ASM:
29212 gmsgid = G_("expected %<asm%>");
29213 break;
29214 case RT_TRY:
29215 gmsgid = G_("expected %<try%>");
29216 break;
29217 case RT_CATCH:
29218 gmsgid = G_("expected %<catch%>");
29219 break;
29220 case RT_THROW:
29221 gmsgid = G_("expected %<throw%>");
29222 break;
29223 case RT_LABEL:
29224 gmsgid = G_("expected %<__label__%>");
29225 break;
29226 case RT_AT_TRY:
29227 gmsgid = G_("expected %<@try%>");
29228 break;
29229 case RT_AT_SYNCHRONIZED:
29230 gmsgid = G_("expected %<@synchronized%>");
29231 break;
29232 case RT_AT_THROW:
29233 gmsgid = G_("expected %<@throw%>");
29234 break;
29235 case RT_TRANSACTION_ATOMIC:
29236 gmsgid = G_("expected %<__transaction_atomic%>");
29237 break;
29238 case RT_TRANSACTION_RELAXED:
29239 gmsgid = G_("expected %<__transaction_relaxed%>");
29240 break;
29241 default:
29242 break;
29245 if (!gmsgid && !keyword)
29247 switch (token_desc)
29249 case RT_SEMICOLON:
29250 gmsgid = G_("expected %<;%>");
29251 break;
29252 case RT_OPEN_PAREN:
29253 gmsgid = G_("expected %<(%>");
29254 break;
29255 case RT_CLOSE_BRACE:
29256 gmsgid = G_("expected %<}%>");
29257 break;
29258 case RT_OPEN_BRACE:
29259 gmsgid = G_("expected %<{%>");
29260 break;
29261 case RT_CLOSE_SQUARE:
29262 gmsgid = G_("expected %<]%>");
29263 break;
29264 case RT_OPEN_SQUARE:
29265 gmsgid = G_("expected %<[%>");
29266 break;
29267 case RT_COMMA:
29268 gmsgid = G_("expected %<,%>");
29269 break;
29270 case RT_SCOPE:
29271 gmsgid = G_("expected %<::%>");
29272 break;
29273 case RT_LESS:
29274 gmsgid = G_("expected %<<%>");
29275 break;
29276 case RT_GREATER:
29277 gmsgid = G_("expected %<>%>");
29278 break;
29279 case RT_EQ:
29280 gmsgid = G_("expected %<=%>");
29281 break;
29282 case RT_ELLIPSIS:
29283 gmsgid = G_("expected %<...%>");
29284 break;
29285 case RT_MULT:
29286 gmsgid = G_("expected %<*%>");
29287 break;
29288 case RT_COMPL:
29289 gmsgid = G_("expected %<~%>");
29290 break;
29291 case RT_COLON:
29292 gmsgid = G_("expected %<:%>");
29293 break;
29294 case RT_COLON_SCOPE:
29295 gmsgid = G_("expected %<:%> or %<::%>");
29296 break;
29297 case RT_CLOSE_PAREN:
29298 gmsgid = G_("expected %<)%>");
29299 break;
29300 case RT_COMMA_CLOSE_PAREN:
29301 gmsgid = G_("expected %<,%> or %<)%>");
29302 break;
29303 case RT_PRAGMA_EOL:
29304 gmsgid = G_("expected end of line");
29305 break;
29306 case RT_NAME:
29307 gmsgid = G_("expected identifier");
29308 break;
29309 case RT_SELECT:
29310 gmsgid = G_("expected selection-statement");
29311 break;
29312 case RT_ITERATION:
29313 gmsgid = G_("expected iteration-statement");
29314 break;
29315 case RT_JUMP:
29316 gmsgid = G_("expected jump-statement");
29317 break;
29318 case RT_CLASS_KEY:
29319 gmsgid = G_("expected class-key");
29320 break;
29321 case RT_CLASS_TYPENAME_TEMPLATE:
29322 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29323 break;
29324 default:
29325 gcc_unreachable ();
29329 if (gmsgid)
29330 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29334 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29335 issue an error message indicating that TOKEN_DESC was expected.
29337 Returns the token consumed, if the token had the appropriate type.
29338 Otherwise, returns NULL.
29340 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29341 within any error as the location of an "opening" token matching
29342 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29343 RT_CLOSE_PAREN). */
29345 static cp_token *
29346 cp_parser_require (cp_parser* parser,
29347 enum cpp_ttype type,
29348 required_token token_desc,
29349 location_t matching_location)
29351 if (cp_lexer_next_token_is (parser->lexer, type))
29352 return cp_lexer_consume_token (parser->lexer);
29353 else
29355 /* Output the MESSAGE -- unless we're parsing tentatively. */
29356 if (!cp_parser_simulate_error (parser))
29357 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29358 matching_location);
29359 return NULL;
29363 /* An error message is produced if the next token is not '>'.
29364 All further tokens are skipped until the desired token is
29365 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29367 static void
29368 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29370 /* Current level of '< ... >'. */
29371 unsigned level = 0;
29372 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29373 unsigned nesting_depth = 0;
29375 /* Are we ready, yet? If not, issue error message. */
29376 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29377 return;
29379 /* Skip tokens until the desired token is found. */
29380 while (true)
29382 /* Peek at the next token. */
29383 switch (cp_lexer_peek_token (parser->lexer)->type)
29385 case CPP_LESS:
29386 if (!nesting_depth)
29387 ++level;
29388 break;
29390 case CPP_RSHIFT:
29391 if (cxx_dialect == cxx98)
29392 /* C++0x views the `>>' operator as two `>' tokens, but
29393 C++98 does not. */
29394 break;
29395 else if (!nesting_depth && level-- == 0)
29397 /* We've hit a `>>' where the first `>' closes the
29398 template argument list, and the second `>' is
29399 spurious. Just consume the `>>' and stop; we've
29400 already produced at least one error. */
29401 cp_lexer_consume_token (parser->lexer);
29402 return;
29404 /* Fall through for C++0x, so we handle the second `>' in
29405 the `>>'. */
29406 gcc_fallthrough ();
29408 case CPP_GREATER:
29409 if (!nesting_depth && level-- == 0)
29411 /* We've reached the token we want, consume it and stop. */
29412 cp_lexer_consume_token (parser->lexer);
29413 return;
29415 break;
29417 case CPP_OPEN_PAREN:
29418 case CPP_OPEN_SQUARE:
29419 ++nesting_depth;
29420 break;
29422 case CPP_CLOSE_PAREN:
29423 case CPP_CLOSE_SQUARE:
29424 if (nesting_depth-- == 0)
29425 return;
29426 break;
29428 case CPP_EOF:
29429 case CPP_PRAGMA_EOL:
29430 case CPP_SEMICOLON:
29431 case CPP_OPEN_BRACE:
29432 case CPP_CLOSE_BRACE:
29433 /* The '>' was probably forgotten, don't look further. */
29434 return;
29436 default:
29437 break;
29440 /* Consume this token. */
29441 cp_lexer_consume_token (parser->lexer);
29445 /* If the next token is the indicated keyword, consume it. Otherwise,
29446 issue an error message indicating that TOKEN_DESC was expected.
29448 Returns the token consumed, if the token had the appropriate type.
29449 Otherwise, returns NULL. */
29451 static cp_token *
29452 cp_parser_require_keyword (cp_parser* parser,
29453 enum rid keyword,
29454 required_token token_desc)
29456 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29458 if (token && token->keyword != keyword)
29460 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29461 UNKNOWN_LOCATION);
29462 return NULL;
29465 return token;
29468 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29469 function-definition. */
29471 static bool
29472 cp_parser_token_starts_function_definition_p (cp_token* token)
29474 return (/* An ordinary function-body begins with an `{'. */
29475 token->type == CPP_OPEN_BRACE
29476 /* A ctor-initializer begins with a `:'. */
29477 || token->type == CPP_COLON
29478 /* A function-try-block begins with `try'. */
29479 || token->keyword == RID_TRY
29480 /* A function-transaction-block begins with `__transaction_atomic'
29481 or `__transaction_relaxed'. */
29482 || token->keyword == RID_TRANSACTION_ATOMIC
29483 || token->keyword == RID_TRANSACTION_RELAXED
29484 /* The named return value extension begins with `return'. */
29485 || token->keyword == RID_RETURN);
29488 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29489 definition. */
29491 static bool
29492 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29494 cp_token *token;
29496 token = cp_lexer_peek_token (parser->lexer);
29497 return (token->type == CPP_OPEN_BRACE
29498 || (token->type == CPP_COLON
29499 && !parser->colon_doesnt_start_class_def_p));
29502 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29503 C++0x) ending a template-argument. */
29505 static bool
29506 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29508 cp_token *token;
29510 token = cp_lexer_peek_token (parser->lexer);
29511 return (token->type == CPP_COMMA
29512 || token->type == CPP_GREATER
29513 || token->type == CPP_ELLIPSIS
29514 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29517 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29518 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29520 static bool
29521 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29522 size_t n)
29524 cp_token *token;
29526 token = cp_lexer_peek_nth_token (parser->lexer, n);
29527 if (token->type == CPP_LESS)
29528 return true;
29529 /* Check for the sequence `<::' in the original code. It would be lexed as
29530 `[:', where `[' is a digraph, and there is no whitespace before
29531 `:'. */
29532 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29534 cp_token *token2;
29535 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29536 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29537 return true;
29539 return false;
29542 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29543 or none_type otherwise. */
29545 static enum tag_types
29546 cp_parser_token_is_class_key (cp_token* token)
29548 switch (token->keyword)
29550 case RID_CLASS:
29551 return class_type;
29552 case RID_STRUCT:
29553 return record_type;
29554 case RID_UNION:
29555 return union_type;
29557 default:
29558 return none_type;
29562 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29563 or none_type otherwise or if the token is null. */
29565 static enum tag_types
29566 cp_parser_token_is_type_parameter_key (cp_token* token)
29568 if (!token)
29569 return none_type;
29571 switch (token->keyword)
29573 case RID_CLASS:
29574 return class_type;
29575 case RID_TYPENAME:
29576 return typename_type;
29578 default:
29579 return none_type;
29583 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29585 static void
29586 cp_parser_check_class_key (enum tag_types class_key, tree type)
29588 if (type == error_mark_node)
29589 return;
29590 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29592 if (permerror (input_location, "%qs tag used in naming %q#T",
29593 class_key == union_type ? "union"
29594 : class_key == record_type ? "struct" : "class",
29595 type))
29596 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29597 "%q#T was previously declared here", type);
29601 /* Issue an error message if DECL is redeclared with different
29602 access than its original declaration [class.access.spec/3].
29603 This applies to nested classes, nested class templates and
29604 enumerations [class.mem/1]. */
29606 static void
29607 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29609 if (!decl
29610 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29611 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29612 return;
29614 if ((TREE_PRIVATE (decl)
29615 != (current_access_specifier == access_private_node))
29616 || (TREE_PROTECTED (decl)
29617 != (current_access_specifier == access_protected_node)))
29618 error_at (location, "%qD redeclared with different access", decl);
29621 /* Look for the `template' keyword, as a syntactic disambiguator.
29622 Return TRUE iff it is present, in which case it will be
29623 consumed. */
29625 static bool
29626 cp_parser_optional_template_keyword (cp_parser *parser)
29628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29630 /* In C++98 the `template' keyword can only be used within templates;
29631 outside templates the parser can always figure out what is a
29632 template and what is not. In C++11, per the resolution of DR 468,
29633 `template' is allowed in cases where it is not strictly necessary. */
29634 if (!processing_template_decl
29635 && pedantic && cxx_dialect == cxx98)
29637 cp_token *token = cp_lexer_peek_token (parser->lexer);
29638 pedwarn (token->location, OPT_Wpedantic,
29639 "in C++98 %<template%> (as a disambiguator) is only "
29640 "allowed within templates");
29641 /* If this part of the token stream is rescanned, the same
29642 error message would be generated. So, we purge the token
29643 from the stream. */
29644 cp_lexer_purge_token (parser->lexer);
29645 return false;
29647 else
29649 /* Consume the `template' keyword. */
29650 cp_lexer_consume_token (parser->lexer);
29651 return true;
29654 return false;
29657 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29658 set PARSER->SCOPE, and perform other related actions. */
29660 static void
29661 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29663 struct tree_check *check_value;
29665 /* Get the stored value. */
29666 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29667 /* Set the scope from the stored value. */
29668 parser->scope = saved_checks_value (check_value);
29669 parser->qualifying_scope = check_value->qualifying_scope;
29670 parser->object_scope = NULL_TREE;
29673 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29674 encounter the end of a block before what we were looking for. */
29676 static bool
29677 cp_parser_cache_group (cp_parser *parser,
29678 enum cpp_ttype end,
29679 unsigned depth)
29681 while (true)
29683 cp_token *token = cp_lexer_peek_token (parser->lexer);
29685 /* Abort a parenthesized expression if we encounter a semicolon. */
29686 if ((end == CPP_CLOSE_PAREN || depth == 0)
29687 && token->type == CPP_SEMICOLON)
29688 return true;
29689 /* If we've reached the end of the file, stop. */
29690 if (token->type == CPP_EOF
29691 || (end != CPP_PRAGMA_EOL
29692 && token->type == CPP_PRAGMA_EOL))
29693 return true;
29694 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29695 /* We've hit the end of an enclosing block, so there's been some
29696 kind of syntax error. */
29697 return true;
29699 /* Consume the token. */
29700 cp_lexer_consume_token (parser->lexer);
29701 /* See if it starts a new group. */
29702 if (token->type == CPP_OPEN_BRACE)
29704 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29705 /* In theory this should probably check end == '}', but
29706 cp_parser_save_member_function_body needs it to exit
29707 after either '}' or ')' when called with ')'. */
29708 if (depth == 0)
29709 return false;
29711 else if (token->type == CPP_OPEN_PAREN)
29713 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29714 if (depth == 0 && end == CPP_CLOSE_PAREN)
29715 return false;
29717 else if (token->type == CPP_PRAGMA)
29718 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29719 else if (token->type == end)
29720 return false;
29724 /* Like above, for caching a default argument or NSDMI. Both of these are
29725 terminated by a non-nested comma, but it can be unclear whether or not a
29726 comma is nested in a template argument list unless we do more parsing.
29727 In order to handle this ambiguity, when we encounter a ',' after a '<'
29728 we try to parse what follows as a parameter-declaration-list (in the
29729 case of a default argument) or a member-declarator (in the case of an
29730 NSDMI). If that succeeds, then we stop caching. */
29732 static tree
29733 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29735 unsigned depth = 0;
29736 int maybe_template_id = 0;
29737 cp_token *first_token;
29738 cp_token *token;
29739 tree default_argument;
29741 /* Add tokens until we have processed the entire default
29742 argument. We add the range [first_token, token). */
29743 first_token = cp_lexer_peek_token (parser->lexer);
29744 if (first_token->type == CPP_OPEN_BRACE)
29746 /* For list-initialization, this is straightforward. */
29747 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29748 token = cp_lexer_peek_token (parser->lexer);
29750 else while (true)
29752 bool done = false;
29754 /* Peek at the next token. */
29755 token = cp_lexer_peek_token (parser->lexer);
29756 /* What we do depends on what token we have. */
29757 switch (token->type)
29759 /* In valid code, a default argument must be
29760 immediately followed by a `,' `)', or `...'. */
29761 case CPP_COMMA:
29762 if (depth == 0 && maybe_template_id)
29764 /* If we've seen a '<', we might be in a
29765 template-argument-list. Until Core issue 325 is
29766 resolved, we don't know how this situation ought
29767 to be handled, so try to DTRT. We check whether
29768 what comes after the comma is a valid parameter
29769 declaration list. If it is, then the comma ends
29770 the default argument; otherwise the default
29771 argument continues. */
29772 bool error = false;
29773 cp_token *peek;
29775 /* Set ITALP so cp_parser_parameter_declaration_list
29776 doesn't decide to commit to this parse. */
29777 bool saved_italp = parser->in_template_argument_list_p;
29778 parser->in_template_argument_list_p = true;
29780 cp_parser_parse_tentatively (parser);
29782 if (nsdmi)
29784 /* Parse declarators until we reach a non-comma or
29785 somthing that cannot be an initializer.
29786 Just checking whether we're looking at a single
29787 declarator is insufficient. Consider:
29788 int var = tuple<T,U>::x;
29789 The template parameter 'U' looks exactly like a
29790 declarator. */
29793 int ctor_dtor_or_conv_p;
29794 cp_lexer_consume_token (parser->lexer);
29795 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29796 CP_PARSER_FLAGS_NONE,
29797 &ctor_dtor_or_conv_p,
29798 /*parenthesized_p=*/NULL,
29799 /*member_p=*/true,
29800 /*friend_p=*/false,
29801 /*static_p=*/false);
29802 peek = cp_lexer_peek_token (parser->lexer);
29803 if (cp_parser_error_occurred (parser))
29804 break;
29806 while (peek->type == CPP_COMMA);
29807 /* If we met an '=' or ';' then the original comma
29808 was the end of the NSDMI. Otherwise assume
29809 we're still in the NSDMI. */
29810 error = (peek->type != CPP_EQ
29811 && peek->type != CPP_SEMICOLON);
29813 else
29815 cp_lexer_consume_token (parser->lexer);
29816 begin_scope (sk_function_parms, NULL_TREE);
29817 tree t = cp_parser_parameter_declaration_list
29818 (parser, CP_PARSER_FLAGS_NONE);
29819 if (t == error_mark_node)
29820 error = true;
29821 pop_bindings_and_leave_scope ();
29823 if (!cp_parser_error_occurred (parser) && !error)
29824 done = true;
29825 cp_parser_abort_tentative_parse (parser);
29827 parser->in_template_argument_list_p = saved_italp;
29828 break;
29830 /* FALLTHRU */
29831 case CPP_CLOSE_PAREN:
29832 case CPP_ELLIPSIS:
29833 /* If we run into a non-nested `;', `}', or `]',
29834 then the code is invalid -- but the default
29835 argument is certainly over. */
29836 case CPP_SEMICOLON:
29837 case CPP_CLOSE_BRACE:
29838 case CPP_CLOSE_SQUARE:
29839 if (depth == 0
29840 /* Handle correctly int n = sizeof ... ( p ); */
29841 && token->type != CPP_ELLIPSIS)
29842 done = true;
29843 /* Update DEPTH, if necessary. */
29844 else if (token->type == CPP_CLOSE_PAREN
29845 || token->type == CPP_CLOSE_BRACE
29846 || token->type == CPP_CLOSE_SQUARE)
29847 --depth;
29848 break;
29850 case CPP_OPEN_PAREN:
29851 case CPP_OPEN_SQUARE:
29852 case CPP_OPEN_BRACE:
29853 ++depth;
29854 break;
29856 case CPP_LESS:
29857 if (depth == 0)
29858 /* This might be the comparison operator, or it might
29859 start a template argument list. */
29860 ++maybe_template_id;
29861 break;
29863 case CPP_RSHIFT:
29864 if (cxx_dialect == cxx98)
29865 break;
29866 /* Fall through for C++0x, which treats the `>>'
29867 operator like two `>' tokens in certain
29868 cases. */
29869 gcc_fallthrough ();
29871 case CPP_GREATER:
29872 if (depth == 0)
29874 /* This might be an operator, or it might close a
29875 template argument list. But if a previous '<'
29876 started a template argument list, this will have
29877 closed it, so we can't be in one anymore. */
29878 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29879 if (maybe_template_id < 0)
29880 maybe_template_id = 0;
29882 break;
29884 /* If we run out of tokens, issue an error message. */
29885 case CPP_EOF:
29886 case CPP_PRAGMA_EOL:
29887 error_at (token->location, "file ends in default argument");
29888 return error_mark_node;
29890 case CPP_NAME:
29891 case CPP_SCOPE:
29892 /* In these cases, we should look for template-ids.
29893 For example, if the default argument is
29894 `X<int, double>()', we need to do name lookup to
29895 figure out whether or not `X' is a template; if
29896 so, the `,' does not end the default argument.
29898 That is not yet done. */
29899 break;
29901 default:
29902 break;
29905 /* If we've reached the end, stop. */
29906 if (done)
29907 break;
29909 /* Add the token to the token block. */
29910 token = cp_lexer_consume_token (parser->lexer);
29913 /* Create a DEFAULT_ARG to represent the unparsed default
29914 argument. */
29915 default_argument = make_node (DEFAULT_ARG);
29916 DEFARG_TOKENS (default_argument)
29917 = cp_token_cache_new (first_token, token);
29918 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29920 return default_argument;
29923 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29925 location_t
29926 defarg_location (tree default_argument)
29928 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29929 location_t start = tokens->first->location;
29930 location_t end = tokens->last->location;
29931 return make_location (start, start, end);
29934 /* Begin parsing tentatively. We always save tokens while parsing
29935 tentatively so that if the tentative parsing fails we can restore the
29936 tokens. */
29938 static void
29939 cp_parser_parse_tentatively (cp_parser* parser)
29941 /* Enter a new parsing context. */
29942 parser->context = cp_parser_context_new (parser->context);
29943 /* Begin saving tokens. */
29944 cp_lexer_save_tokens (parser->lexer);
29945 /* In order to avoid repetitive access control error messages,
29946 access checks are queued up until we are no longer parsing
29947 tentatively. */
29948 push_deferring_access_checks (dk_deferred);
29951 /* Commit to the currently active tentative parse. */
29953 static void
29954 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29956 cp_parser_context *context;
29957 cp_lexer *lexer;
29959 /* Mark all of the levels as committed. */
29960 lexer = parser->lexer;
29961 for (context = parser->context; context->next; context = context->next)
29963 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29964 break;
29965 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29966 while (!cp_lexer_saving_tokens (lexer))
29967 lexer = lexer->next;
29968 cp_lexer_commit_tokens (lexer);
29972 /* Commit to the topmost currently active tentative parse.
29974 Note that this function shouldn't be called when there are
29975 irreversible side-effects while in a tentative state. For
29976 example, we shouldn't create a permanent entry in the symbol
29977 table, or issue an error message that might not apply if the
29978 tentative parse is aborted. */
29980 static void
29981 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29983 cp_parser_context *context = parser->context;
29984 cp_lexer *lexer = parser->lexer;
29986 if (context)
29988 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29989 return;
29990 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29992 while (!cp_lexer_saving_tokens (lexer))
29993 lexer = lexer->next;
29994 cp_lexer_commit_tokens (lexer);
29998 /* Abort the currently active tentative parse. All consumed tokens
29999 will be rolled back, and no diagnostics will be issued. */
30001 static void
30002 cp_parser_abort_tentative_parse (cp_parser* parser)
30004 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30005 || errorcount > 0);
30006 cp_parser_simulate_error (parser);
30007 /* Now, pretend that we want to see if the construct was
30008 successfully parsed. */
30009 cp_parser_parse_definitely (parser);
30012 /* Stop parsing tentatively. If a parse error has occurred, restore the
30013 token stream. Otherwise, commit to the tokens we have consumed.
30014 Returns true if no error occurred; false otherwise. */
30016 static bool
30017 cp_parser_parse_definitely (cp_parser* parser)
30019 bool error_occurred;
30020 cp_parser_context *context;
30022 /* Remember whether or not an error occurred, since we are about to
30023 destroy that information. */
30024 error_occurred = cp_parser_error_occurred (parser);
30025 /* Remove the topmost context from the stack. */
30026 context = parser->context;
30027 parser->context = context->next;
30028 /* If no parse errors occurred, commit to the tentative parse. */
30029 if (!error_occurred)
30031 /* Commit to the tokens read tentatively, unless that was
30032 already done. */
30033 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30034 cp_lexer_commit_tokens (parser->lexer);
30036 pop_to_parent_deferring_access_checks ();
30038 /* Otherwise, if errors occurred, roll back our state so that things
30039 are just as they were before we began the tentative parse. */
30040 else
30042 cp_lexer_rollback_tokens (parser->lexer);
30043 pop_deferring_access_checks ();
30045 /* Add the context to the front of the free list. */
30046 context->next = cp_parser_context_free_list;
30047 cp_parser_context_free_list = context;
30049 return !error_occurred;
30052 /* Returns true if we are parsing tentatively and are not committed to
30053 this tentative parse. */
30055 static bool
30056 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30058 return (cp_parser_parsing_tentatively (parser)
30059 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30062 /* Returns nonzero iff an error has occurred during the most recent
30063 tentative parse. */
30065 static bool
30066 cp_parser_error_occurred (cp_parser* parser)
30068 return (cp_parser_parsing_tentatively (parser)
30069 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30072 /* Returns nonzero if GNU extensions are allowed. */
30074 static bool
30075 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30077 return parser->allow_gnu_extensions_p;
30080 /* Objective-C++ Productions */
30083 /* Parse an Objective-C expression, which feeds into a primary-expression
30084 above.
30086 objc-expression:
30087 objc-message-expression
30088 objc-string-literal
30089 objc-encode-expression
30090 objc-protocol-expression
30091 objc-selector-expression
30093 Returns a tree representation of the expression. */
30095 static cp_expr
30096 cp_parser_objc_expression (cp_parser* parser)
30098 /* Try to figure out what kind of declaration is present. */
30099 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30101 switch (kwd->type)
30103 case CPP_OPEN_SQUARE:
30104 return cp_parser_objc_message_expression (parser);
30106 case CPP_OBJC_STRING:
30107 kwd = cp_lexer_consume_token (parser->lexer);
30108 return objc_build_string_object (kwd->u.value);
30110 case CPP_KEYWORD:
30111 switch (kwd->keyword)
30113 case RID_AT_ENCODE:
30114 return cp_parser_objc_encode_expression (parser);
30116 case RID_AT_PROTOCOL:
30117 return cp_parser_objc_protocol_expression (parser);
30119 case RID_AT_SELECTOR:
30120 return cp_parser_objc_selector_expression (parser);
30122 default:
30123 break;
30125 /* FALLTHRU */
30126 default:
30127 error_at (kwd->location,
30128 "misplaced %<@%D%> Objective-C++ construct",
30129 kwd->u.value);
30130 cp_parser_skip_to_end_of_block_or_statement (parser);
30133 return error_mark_node;
30136 /* Parse an Objective-C message expression.
30138 objc-message-expression:
30139 [ objc-message-receiver objc-message-args ]
30141 Returns a representation of an Objective-C message. */
30143 static tree
30144 cp_parser_objc_message_expression (cp_parser* parser)
30146 tree receiver, messageargs;
30148 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30149 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30150 receiver = cp_parser_objc_message_receiver (parser);
30151 messageargs = cp_parser_objc_message_args (parser);
30152 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30153 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30155 tree result = objc_build_message_expr (receiver, messageargs);
30157 /* Construct a location e.g.
30158 [self func1:5]
30159 ^~~~~~~~~~~~~~
30160 ranging from the '[' to the ']', with the caret at the start. */
30161 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30162 protected_set_expr_location (result, combined_loc);
30164 return result;
30167 /* Parse an objc-message-receiver.
30169 objc-message-receiver:
30170 expression
30171 simple-type-specifier
30173 Returns a representation of the type or expression. */
30175 static tree
30176 cp_parser_objc_message_receiver (cp_parser* parser)
30178 tree rcv;
30180 /* An Objective-C message receiver may be either (1) a type
30181 or (2) an expression. */
30182 cp_parser_parse_tentatively (parser);
30183 rcv = cp_parser_expression (parser);
30185 /* If that worked out, fine. */
30186 if (cp_parser_parse_definitely (parser))
30187 return rcv;
30189 cp_parser_parse_tentatively (parser);
30190 rcv = cp_parser_simple_type_specifier (parser,
30191 /*decl_specs=*/NULL,
30192 CP_PARSER_FLAGS_NONE);
30194 if (cp_parser_parse_definitely (parser))
30195 return objc_get_class_reference (rcv);
30197 cp_parser_error (parser, "objective-c++ message receiver expected");
30198 return error_mark_node;
30201 /* Parse the arguments and selectors comprising an Objective-C message.
30203 objc-message-args:
30204 objc-selector
30205 objc-selector-args
30206 objc-selector-args , objc-comma-args
30208 objc-selector-args:
30209 objc-selector [opt] : assignment-expression
30210 objc-selector-args objc-selector [opt] : assignment-expression
30212 objc-comma-args:
30213 assignment-expression
30214 objc-comma-args , assignment-expression
30216 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30217 selector arguments and TREE_VALUE containing a list of comma
30218 arguments. */
30220 static tree
30221 cp_parser_objc_message_args (cp_parser* parser)
30223 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30224 bool maybe_unary_selector_p = true;
30225 cp_token *token = cp_lexer_peek_token (parser->lexer);
30227 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30229 tree selector = NULL_TREE, arg;
30231 if (token->type != CPP_COLON)
30232 selector = cp_parser_objc_selector (parser);
30234 /* Detect if we have a unary selector. */
30235 if (maybe_unary_selector_p
30236 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30237 return build_tree_list (selector, NULL_TREE);
30239 maybe_unary_selector_p = false;
30240 cp_parser_require (parser, CPP_COLON, RT_COLON);
30241 arg = cp_parser_assignment_expression (parser);
30243 sel_args
30244 = chainon (sel_args,
30245 build_tree_list (selector, arg));
30247 token = cp_lexer_peek_token (parser->lexer);
30250 /* Handle non-selector arguments, if any. */
30251 while (token->type == CPP_COMMA)
30253 tree arg;
30255 cp_lexer_consume_token (parser->lexer);
30256 arg = cp_parser_assignment_expression (parser);
30258 addl_args
30259 = chainon (addl_args,
30260 build_tree_list (NULL_TREE, arg));
30262 token = cp_lexer_peek_token (parser->lexer);
30265 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30267 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30268 return build_tree_list (error_mark_node, error_mark_node);
30271 return build_tree_list (sel_args, addl_args);
30274 /* Parse an Objective-C encode expression.
30276 objc-encode-expression:
30277 @encode objc-typename
30279 Returns an encoded representation of the type argument. */
30281 static cp_expr
30282 cp_parser_objc_encode_expression (cp_parser* parser)
30284 tree type;
30285 cp_token *token;
30286 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30288 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30289 matching_parens parens;
30290 parens.require_open (parser);
30291 token = cp_lexer_peek_token (parser->lexer);
30292 type = complete_type (cp_parser_type_id (parser));
30293 parens.require_close (parser);
30295 if (!type)
30297 error_at (token->location,
30298 "%<@encode%> must specify a type as an argument");
30299 return error_mark_node;
30302 /* This happens if we find @encode(T) (where T is a template
30303 typename or something dependent on a template typename) when
30304 parsing a template. In that case, we can't compile it
30305 immediately, but we rather create an AT_ENCODE_EXPR which will
30306 need to be instantiated when the template is used.
30308 if (dependent_type_p (type))
30310 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30311 TREE_READONLY (value) = 1;
30312 return value;
30316 /* Build a location of the form:
30317 @encode(int)
30318 ^~~~~~~~~~~~
30319 with caret==start at the @ token, finishing at the close paren. */
30320 location_t combined_loc
30321 = make_location (start_loc, start_loc,
30322 cp_lexer_previous_token (parser->lexer)->location);
30324 return cp_expr (objc_build_encode_expr (type), combined_loc);
30327 /* Parse an Objective-C @defs expression. */
30329 static tree
30330 cp_parser_objc_defs_expression (cp_parser *parser)
30332 tree name;
30334 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30335 matching_parens parens;
30336 parens.require_open (parser);
30337 name = cp_parser_identifier (parser);
30338 parens.require_close (parser);
30340 return objc_get_class_ivars (name);
30343 /* Parse an Objective-C protocol expression.
30345 objc-protocol-expression:
30346 @protocol ( identifier )
30348 Returns a representation of the protocol expression. */
30350 static tree
30351 cp_parser_objc_protocol_expression (cp_parser* parser)
30353 tree proto;
30354 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30356 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30357 matching_parens parens;
30358 parens.require_open (parser);
30359 proto = cp_parser_identifier (parser);
30360 parens.require_close (parser);
30362 /* Build a location of the form:
30363 @protocol(prot)
30364 ^~~~~~~~~~~~~~~
30365 with caret==start at the @ token, finishing at the close paren. */
30366 location_t combined_loc
30367 = make_location (start_loc, start_loc,
30368 cp_lexer_previous_token (parser->lexer)->location);
30369 tree result = objc_build_protocol_expr (proto);
30370 protected_set_expr_location (result, combined_loc);
30371 return result;
30374 /* Parse an Objective-C selector expression.
30376 objc-selector-expression:
30377 @selector ( objc-method-signature )
30379 objc-method-signature:
30380 objc-selector
30381 objc-selector-seq
30383 objc-selector-seq:
30384 objc-selector :
30385 objc-selector-seq objc-selector :
30387 Returns a representation of the method selector. */
30389 static tree
30390 cp_parser_objc_selector_expression (cp_parser* parser)
30392 tree sel_seq = NULL_TREE;
30393 bool maybe_unary_selector_p = true;
30394 cp_token *token;
30395 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30397 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30398 matching_parens parens;
30399 parens.require_open (parser);
30400 token = cp_lexer_peek_token (parser->lexer);
30402 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30403 || token->type == CPP_SCOPE)
30405 tree selector = NULL_TREE;
30407 if (token->type != CPP_COLON
30408 || token->type == CPP_SCOPE)
30409 selector = cp_parser_objc_selector (parser);
30411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30412 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30414 /* Detect if we have a unary selector. */
30415 if (maybe_unary_selector_p)
30417 sel_seq = selector;
30418 goto finish_selector;
30420 else
30422 cp_parser_error (parser, "expected %<:%>");
30425 maybe_unary_selector_p = false;
30426 token = cp_lexer_consume_token (parser->lexer);
30428 if (token->type == CPP_SCOPE)
30430 sel_seq
30431 = chainon (sel_seq,
30432 build_tree_list (selector, NULL_TREE));
30433 sel_seq
30434 = chainon (sel_seq,
30435 build_tree_list (NULL_TREE, NULL_TREE));
30437 else
30438 sel_seq
30439 = chainon (sel_seq,
30440 build_tree_list (selector, NULL_TREE));
30442 token = cp_lexer_peek_token (parser->lexer);
30445 finish_selector:
30446 parens.require_close (parser);
30449 /* Build a location of the form:
30450 @selector(func)
30451 ^~~~~~~~~~~~~~~
30452 with caret==start at the @ token, finishing at the close paren. */
30453 location_t combined_loc
30454 = make_location (loc, loc,
30455 cp_lexer_previous_token (parser->lexer)->location);
30456 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30457 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30458 protected_set_expr_location (result, combined_loc);
30459 return result;
30462 /* Parse a list of identifiers.
30464 objc-identifier-list:
30465 identifier
30466 objc-identifier-list , identifier
30468 Returns a TREE_LIST of identifier nodes. */
30470 static tree
30471 cp_parser_objc_identifier_list (cp_parser* parser)
30473 tree identifier;
30474 tree list;
30475 cp_token *sep;
30477 identifier = cp_parser_identifier (parser);
30478 if (identifier == error_mark_node)
30479 return error_mark_node;
30481 list = build_tree_list (NULL_TREE, identifier);
30482 sep = cp_lexer_peek_token (parser->lexer);
30484 while (sep->type == CPP_COMMA)
30486 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30487 identifier = cp_parser_identifier (parser);
30488 if (identifier == error_mark_node)
30489 return list;
30491 list = chainon (list, build_tree_list (NULL_TREE,
30492 identifier));
30493 sep = cp_lexer_peek_token (parser->lexer);
30496 return list;
30499 /* Parse an Objective-C alias declaration.
30501 objc-alias-declaration:
30502 @compatibility_alias identifier identifier ;
30504 This function registers the alias mapping with the Objective-C front end.
30505 It returns nothing. */
30507 static void
30508 cp_parser_objc_alias_declaration (cp_parser* parser)
30510 tree alias, orig;
30512 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30513 alias = cp_parser_identifier (parser);
30514 orig = cp_parser_identifier (parser);
30515 objc_declare_alias (alias, orig);
30516 cp_parser_consume_semicolon_at_end_of_statement (parser);
30519 /* Parse an Objective-C class forward-declaration.
30521 objc-class-declaration:
30522 @class objc-identifier-list ;
30524 The function registers the forward declarations with the Objective-C
30525 front end. It returns nothing. */
30527 static void
30528 cp_parser_objc_class_declaration (cp_parser* parser)
30530 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30531 while (true)
30533 tree id;
30535 id = cp_parser_identifier (parser);
30536 if (id == error_mark_node)
30537 break;
30539 objc_declare_class (id);
30541 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30542 cp_lexer_consume_token (parser->lexer);
30543 else
30544 break;
30546 cp_parser_consume_semicolon_at_end_of_statement (parser);
30549 /* Parse a list of Objective-C protocol references.
30551 objc-protocol-refs-opt:
30552 objc-protocol-refs [opt]
30554 objc-protocol-refs:
30555 < objc-identifier-list >
30557 Returns a TREE_LIST of identifiers, if any. */
30559 static tree
30560 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30562 tree protorefs = NULL_TREE;
30564 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30566 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30567 protorefs = cp_parser_objc_identifier_list (parser);
30568 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30571 return protorefs;
30574 /* Parse a Objective-C visibility specification. */
30576 static void
30577 cp_parser_objc_visibility_spec (cp_parser* parser)
30579 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30581 switch (vis->keyword)
30583 case RID_AT_PRIVATE:
30584 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30585 break;
30586 case RID_AT_PROTECTED:
30587 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30588 break;
30589 case RID_AT_PUBLIC:
30590 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30591 break;
30592 case RID_AT_PACKAGE:
30593 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30594 break;
30595 default:
30596 return;
30599 /* Eat '@private'/'@protected'/'@public'. */
30600 cp_lexer_consume_token (parser->lexer);
30603 /* Parse an Objective-C method type. Return 'true' if it is a class
30604 (+) method, and 'false' if it is an instance (-) method. */
30606 static inline bool
30607 cp_parser_objc_method_type (cp_parser* parser)
30609 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30610 return true;
30611 else
30612 return false;
30615 /* Parse an Objective-C protocol qualifier. */
30617 static tree
30618 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30620 tree quals = NULL_TREE, node;
30621 cp_token *token = cp_lexer_peek_token (parser->lexer);
30623 node = token->u.value;
30625 while (node && identifier_p (node)
30626 && (node == ridpointers [(int) RID_IN]
30627 || node == ridpointers [(int) RID_OUT]
30628 || node == ridpointers [(int) RID_INOUT]
30629 || node == ridpointers [(int) RID_BYCOPY]
30630 || node == ridpointers [(int) RID_BYREF]
30631 || node == ridpointers [(int) RID_ONEWAY]))
30633 quals = tree_cons (NULL_TREE, node, quals);
30634 cp_lexer_consume_token (parser->lexer);
30635 token = cp_lexer_peek_token (parser->lexer);
30636 node = token->u.value;
30639 return quals;
30642 /* Parse an Objective-C typename. */
30644 static tree
30645 cp_parser_objc_typename (cp_parser* parser)
30647 tree type_name = NULL_TREE;
30649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30651 tree proto_quals, cp_type = NULL_TREE;
30653 matching_parens parens;
30654 parens.consume_open (parser); /* Eat '('. */
30655 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30657 /* An ObjC type name may consist of just protocol qualifiers, in which
30658 case the type shall default to 'id'. */
30659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30661 cp_type = cp_parser_type_id (parser);
30663 /* If the type could not be parsed, an error has already
30664 been produced. For error recovery, behave as if it had
30665 not been specified, which will use the default type
30666 'id'. */
30667 if (cp_type == error_mark_node)
30669 cp_type = NULL_TREE;
30670 /* We need to skip to the closing parenthesis as
30671 cp_parser_type_id() does not seem to do it for
30672 us. */
30673 cp_parser_skip_to_closing_parenthesis (parser,
30674 /*recovering=*/true,
30675 /*or_comma=*/false,
30676 /*consume_paren=*/false);
30680 parens.require_close (parser);
30681 type_name = build_tree_list (proto_quals, cp_type);
30684 return type_name;
30687 /* Check to see if TYPE refers to an Objective-C selector name. */
30689 static bool
30690 cp_parser_objc_selector_p (enum cpp_ttype type)
30692 return (type == CPP_NAME || type == CPP_KEYWORD
30693 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30694 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30695 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30696 || type == CPP_XOR || type == CPP_XOR_EQ);
30699 /* Parse an Objective-C selector. */
30701 static tree
30702 cp_parser_objc_selector (cp_parser* parser)
30704 cp_token *token = cp_lexer_consume_token (parser->lexer);
30706 if (!cp_parser_objc_selector_p (token->type))
30708 error_at (token->location, "invalid Objective-C++ selector name");
30709 return error_mark_node;
30712 /* C++ operator names are allowed to appear in ObjC selectors. */
30713 switch (token->type)
30715 case CPP_AND_AND: return get_identifier ("and");
30716 case CPP_AND_EQ: return get_identifier ("and_eq");
30717 case CPP_AND: return get_identifier ("bitand");
30718 case CPP_OR: return get_identifier ("bitor");
30719 case CPP_COMPL: return get_identifier ("compl");
30720 case CPP_NOT: return get_identifier ("not");
30721 case CPP_NOT_EQ: return get_identifier ("not_eq");
30722 case CPP_OR_OR: return get_identifier ("or");
30723 case CPP_OR_EQ: return get_identifier ("or_eq");
30724 case CPP_XOR: return get_identifier ("xor");
30725 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30726 default: return token->u.value;
30730 /* Parse an Objective-C params list. */
30732 static tree
30733 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30735 tree params = NULL_TREE;
30736 bool maybe_unary_selector_p = true;
30737 cp_token *token = cp_lexer_peek_token (parser->lexer);
30739 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30741 tree selector = NULL_TREE, type_name, identifier;
30742 tree parm_attr = NULL_TREE;
30744 if (token->keyword == RID_ATTRIBUTE)
30745 break;
30747 if (token->type != CPP_COLON)
30748 selector = cp_parser_objc_selector (parser);
30750 /* Detect if we have a unary selector. */
30751 if (maybe_unary_selector_p
30752 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30754 params = selector; /* Might be followed by attributes. */
30755 break;
30758 maybe_unary_selector_p = false;
30759 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30761 /* Something went quite wrong. There should be a colon
30762 here, but there is not. Stop parsing parameters. */
30763 break;
30765 type_name = cp_parser_objc_typename (parser);
30766 /* New ObjC allows attributes on parameters too. */
30767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30768 parm_attr = cp_parser_attributes_opt (parser);
30769 identifier = cp_parser_identifier (parser);
30771 params
30772 = chainon (params,
30773 objc_build_keyword_decl (selector,
30774 type_name,
30775 identifier,
30776 parm_attr));
30778 token = cp_lexer_peek_token (parser->lexer);
30781 if (params == NULL_TREE)
30783 cp_parser_error (parser, "objective-c++ method declaration is expected");
30784 return error_mark_node;
30787 /* We allow tail attributes for the method. */
30788 if (token->keyword == RID_ATTRIBUTE)
30790 *attributes = cp_parser_attributes_opt (parser);
30791 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30792 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30793 return params;
30794 cp_parser_error (parser,
30795 "method attributes must be specified at the end");
30796 return error_mark_node;
30799 if (params == NULL_TREE)
30801 cp_parser_error (parser, "objective-c++ method declaration is expected");
30802 return error_mark_node;
30804 return params;
30807 /* Parse the non-keyword Objective-C params. */
30809 static tree
30810 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30811 tree* attributes)
30813 tree params = make_node (TREE_LIST);
30814 cp_token *token = cp_lexer_peek_token (parser->lexer);
30815 *ellipsisp = false; /* Initially, assume no ellipsis. */
30817 while (token->type == CPP_COMMA)
30819 cp_parameter_declarator *parmdecl;
30820 tree parm;
30822 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30823 token = cp_lexer_peek_token (parser->lexer);
30825 if (token->type == CPP_ELLIPSIS)
30827 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30828 *ellipsisp = true;
30829 token = cp_lexer_peek_token (parser->lexer);
30830 break;
30833 /* TODO: parse attributes for tail parameters. */
30834 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30835 false, NULL);
30836 parm = grokdeclarator (parmdecl->declarator,
30837 &parmdecl->decl_specifiers,
30838 PARM, /*initialized=*/0,
30839 /*attrlist=*/NULL);
30841 chainon (params, build_tree_list (NULL_TREE, parm));
30842 token = cp_lexer_peek_token (parser->lexer);
30845 /* We allow tail attributes for the method. */
30846 if (token->keyword == RID_ATTRIBUTE)
30848 if (*attributes == NULL_TREE)
30850 *attributes = cp_parser_attributes_opt (parser);
30851 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30852 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30853 return params;
30855 else
30856 /* We have an error, but parse the attributes, so that we can
30857 carry on. */
30858 *attributes = cp_parser_attributes_opt (parser);
30860 cp_parser_error (parser,
30861 "method attributes must be specified at the end");
30862 return error_mark_node;
30865 return params;
30868 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30870 static void
30871 cp_parser_objc_interstitial_code (cp_parser* parser)
30873 cp_token *token = cp_lexer_peek_token (parser->lexer);
30875 /* If the next token is `extern' and the following token is a string
30876 literal, then we have a linkage specification. */
30877 if (token->keyword == RID_EXTERN
30878 && cp_parser_is_pure_string_literal
30879 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30880 cp_parser_linkage_specification (parser);
30881 /* Handle #pragma, if any. */
30882 else if (token->type == CPP_PRAGMA)
30883 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30884 /* Allow stray semicolons. */
30885 else if (token->type == CPP_SEMICOLON)
30886 cp_lexer_consume_token (parser->lexer);
30887 /* Mark methods as optional or required, when building protocols. */
30888 else if (token->keyword == RID_AT_OPTIONAL)
30890 cp_lexer_consume_token (parser->lexer);
30891 objc_set_method_opt (true);
30893 else if (token->keyword == RID_AT_REQUIRED)
30895 cp_lexer_consume_token (parser->lexer);
30896 objc_set_method_opt (false);
30898 else if (token->keyword == RID_NAMESPACE)
30899 cp_parser_namespace_definition (parser);
30900 /* Other stray characters must generate errors. */
30901 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30903 cp_lexer_consume_token (parser->lexer);
30904 error ("stray %qs between Objective-C++ methods",
30905 token->type == CPP_OPEN_BRACE ? "{" : "}");
30907 /* Finally, try to parse a block-declaration, or a function-definition. */
30908 else
30909 cp_parser_block_declaration (parser, /*statement_p=*/false);
30912 /* Parse a method signature. */
30914 static tree
30915 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30917 tree rettype, kwdparms, optparms;
30918 bool ellipsis = false;
30919 bool is_class_method;
30921 is_class_method = cp_parser_objc_method_type (parser);
30922 rettype = cp_parser_objc_typename (parser);
30923 *attributes = NULL_TREE;
30924 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30925 if (kwdparms == error_mark_node)
30926 return error_mark_node;
30927 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30928 if (optparms == error_mark_node)
30929 return error_mark_node;
30931 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30934 static bool
30935 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30937 tree tattr;
30938 cp_lexer_save_tokens (parser->lexer);
30939 tattr = cp_parser_attributes_opt (parser);
30940 gcc_assert (tattr) ;
30942 /* If the attributes are followed by a method introducer, this is not allowed.
30943 Dump the attributes and flag the situation. */
30944 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30945 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30946 return true;
30948 /* Otherwise, the attributes introduce some interstitial code, possibly so
30949 rewind to allow that check. */
30950 cp_lexer_rollback_tokens (parser->lexer);
30951 return false;
30954 /* Parse an Objective-C method prototype list. */
30956 static void
30957 cp_parser_objc_method_prototype_list (cp_parser* parser)
30959 cp_token *token = cp_lexer_peek_token (parser->lexer);
30961 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30963 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30965 tree attributes, sig;
30966 bool is_class_method;
30967 if (token->type == CPP_PLUS)
30968 is_class_method = true;
30969 else
30970 is_class_method = false;
30971 sig = cp_parser_objc_method_signature (parser, &attributes);
30972 if (sig == error_mark_node)
30974 cp_parser_skip_to_end_of_block_or_statement (parser);
30975 token = cp_lexer_peek_token (parser->lexer);
30976 continue;
30978 objc_add_method_declaration (is_class_method, sig, attributes);
30979 cp_parser_consume_semicolon_at_end_of_statement (parser);
30981 else if (token->keyword == RID_AT_PROPERTY)
30982 cp_parser_objc_at_property_declaration (parser);
30983 else if (token->keyword == RID_ATTRIBUTE
30984 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30985 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30986 OPT_Wattributes,
30987 "prefix attributes are ignored for methods");
30988 else
30989 /* Allow for interspersed non-ObjC++ code. */
30990 cp_parser_objc_interstitial_code (parser);
30992 token = cp_lexer_peek_token (parser->lexer);
30995 if (token->type != CPP_EOF)
30996 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30997 else
30998 cp_parser_error (parser, "expected %<@end%>");
31000 objc_finish_interface ();
31003 /* Parse an Objective-C method definition list. */
31005 static void
31006 cp_parser_objc_method_definition_list (cp_parser* parser)
31008 cp_token *token = cp_lexer_peek_token (parser->lexer);
31010 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31012 tree meth;
31014 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31016 cp_token *ptk;
31017 tree sig, attribute;
31018 bool is_class_method;
31019 if (token->type == CPP_PLUS)
31020 is_class_method = true;
31021 else
31022 is_class_method = false;
31023 push_deferring_access_checks (dk_deferred);
31024 sig = cp_parser_objc_method_signature (parser, &attribute);
31025 if (sig == error_mark_node)
31027 cp_parser_skip_to_end_of_block_or_statement (parser);
31028 token = cp_lexer_peek_token (parser->lexer);
31029 continue;
31031 objc_start_method_definition (is_class_method, sig, attribute,
31032 NULL_TREE);
31034 /* For historical reasons, we accept an optional semicolon. */
31035 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31036 cp_lexer_consume_token (parser->lexer);
31038 ptk = cp_lexer_peek_token (parser->lexer);
31039 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31040 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31042 perform_deferred_access_checks (tf_warning_or_error);
31043 stop_deferring_access_checks ();
31044 meth = cp_parser_function_definition_after_declarator (parser,
31045 false);
31046 pop_deferring_access_checks ();
31047 objc_finish_method_definition (meth);
31050 /* The following case will be removed once @synthesize is
31051 completely implemented. */
31052 else if (token->keyword == RID_AT_PROPERTY)
31053 cp_parser_objc_at_property_declaration (parser);
31054 else if (token->keyword == RID_AT_SYNTHESIZE)
31055 cp_parser_objc_at_synthesize_declaration (parser);
31056 else if (token->keyword == RID_AT_DYNAMIC)
31057 cp_parser_objc_at_dynamic_declaration (parser);
31058 else if (token->keyword == RID_ATTRIBUTE
31059 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31060 warning_at (token->location, OPT_Wattributes,
31061 "prefix attributes are ignored for methods");
31062 else
31063 /* Allow for interspersed non-ObjC++ code. */
31064 cp_parser_objc_interstitial_code (parser);
31066 token = cp_lexer_peek_token (parser->lexer);
31069 if (token->type != CPP_EOF)
31070 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31071 else
31072 cp_parser_error (parser, "expected %<@end%>");
31074 objc_finish_implementation ();
31077 /* Parse Objective-C ivars. */
31079 static void
31080 cp_parser_objc_class_ivars (cp_parser* parser)
31082 cp_token *token = cp_lexer_peek_token (parser->lexer);
31084 if (token->type != CPP_OPEN_BRACE)
31085 return; /* No ivars specified. */
31087 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31088 token = cp_lexer_peek_token (parser->lexer);
31090 while (token->type != CPP_CLOSE_BRACE
31091 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31093 cp_decl_specifier_seq declspecs;
31094 int decl_class_or_enum_p;
31095 tree prefix_attributes;
31097 cp_parser_objc_visibility_spec (parser);
31099 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31100 break;
31102 cp_parser_decl_specifier_seq (parser,
31103 CP_PARSER_FLAGS_OPTIONAL,
31104 &declspecs,
31105 &decl_class_or_enum_p);
31107 /* auto, register, static, extern, mutable. */
31108 if (declspecs.storage_class != sc_none)
31110 cp_parser_error (parser, "invalid type for instance variable");
31111 declspecs.storage_class = sc_none;
31114 /* thread_local. */
31115 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31117 cp_parser_error (parser, "invalid type for instance variable");
31118 declspecs.locations[ds_thread] = 0;
31121 /* typedef. */
31122 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31124 cp_parser_error (parser, "invalid type for instance variable");
31125 declspecs.locations[ds_typedef] = 0;
31128 prefix_attributes = declspecs.attributes;
31129 declspecs.attributes = NULL_TREE;
31131 /* Keep going until we hit the `;' at the end of the
31132 declaration. */
31133 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31135 tree width = NULL_TREE, attributes, first_attribute, decl;
31136 cp_declarator *declarator = NULL;
31137 int ctor_dtor_or_conv_p;
31139 /* Check for a (possibly unnamed) bitfield declaration. */
31140 token = cp_lexer_peek_token (parser->lexer);
31141 if (token->type == CPP_COLON)
31142 goto eat_colon;
31144 if (token->type == CPP_NAME
31145 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31146 == CPP_COLON))
31148 /* Get the name of the bitfield. */
31149 declarator = make_id_declarator (NULL_TREE,
31150 cp_parser_identifier (parser),
31151 sfk_none, token->location);
31153 eat_colon:
31154 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31155 /* Get the width of the bitfield. */
31156 width
31157 = cp_parser_constant_expression (parser);
31159 else
31161 /* Parse the declarator. */
31162 declarator
31163 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31164 CP_PARSER_FLAGS_NONE,
31165 &ctor_dtor_or_conv_p,
31166 /*parenthesized_p=*/NULL,
31167 /*member_p=*/false,
31168 /*friend_p=*/false,
31169 /*static_p=*/false);
31172 /* Look for attributes that apply to the ivar. */
31173 attributes = cp_parser_attributes_opt (parser);
31174 /* Remember which attributes are prefix attributes and
31175 which are not. */
31176 first_attribute = attributes;
31177 /* Combine the attributes. */
31178 attributes = attr_chainon (prefix_attributes, attributes);
31180 if (width)
31181 /* Create the bitfield declaration. */
31182 decl = grokbitfield (declarator, &declspecs,
31183 width, NULL_TREE, attributes);
31184 else
31185 decl = grokfield (declarator, &declspecs,
31186 NULL_TREE, /*init_const_expr_p=*/false,
31187 NULL_TREE, attributes);
31189 /* Add the instance variable. */
31190 if (decl != error_mark_node && decl != NULL_TREE)
31191 objc_add_instance_variable (decl);
31193 /* Reset PREFIX_ATTRIBUTES. */
31194 if (attributes != error_mark_node)
31196 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31197 attributes = TREE_CHAIN (attributes);
31198 if (attributes)
31199 TREE_CHAIN (attributes) = NULL_TREE;
31202 token = cp_lexer_peek_token (parser->lexer);
31204 if (token->type == CPP_COMMA)
31206 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31207 continue;
31209 break;
31212 cp_parser_consume_semicolon_at_end_of_statement (parser);
31213 token = cp_lexer_peek_token (parser->lexer);
31216 if (token->keyword == RID_AT_END)
31217 cp_parser_error (parser, "expected %<}%>");
31219 /* Do not consume the RID_AT_END, so it will be read again as terminating
31220 the @interface of @implementation. */
31221 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31222 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31224 /* For historical reasons, we accept an optional semicolon. */
31225 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31226 cp_lexer_consume_token (parser->lexer);
31229 /* Parse an Objective-C protocol declaration. */
31231 static void
31232 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31234 tree proto, protorefs;
31235 cp_token *tok;
31237 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31240 tok = cp_lexer_peek_token (parser->lexer);
31241 error_at (tok->location, "identifier expected after %<@protocol%>");
31242 cp_parser_consume_semicolon_at_end_of_statement (parser);
31243 return;
31246 /* See if we have a forward declaration or a definition. */
31247 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31249 /* Try a forward declaration first. */
31250 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31252 while (true)
31254 tree id;
31256 id = cp_parser_identifier (parser);
31257 if (id == error_mark_node)
31258 break;
31260 objc_declare_protocol (id, attributes);
31262 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31263 cp_lexer_consume_token (parser->lexer);
31264 else
31265 break;
31267 cp_parser_consume_semicolon_at_end_of_statement (parser);
31270 /* Ok, we got a full-fledged definition (or at least should). */
31271 else
31273 proto = cp_parser_identifier (parser);
31274 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31275 objc_start_protocol (proto, protorefs, attributes);
31276 cp_parser_objc_method_prototype_list (parser);
31280 /* Parse an Objective-C superclass or category. */
31282 static void
31283 cp_parser_objc_superclass_or_category (cp_parser *parser,
31284 bool iface_p,
31285 tree *super,
31286 tree *categ, bool *is_class_extension)
31288 cp_token *next = cp_lexer_peek_token (parser->lexer);
31290 *super = *categ = NULL_TREE;
31291 *is_class_extension = false;
31292 if (next->type == CPP_COLON)
31294 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31295 *super = cp_parser_identifier (parser);
31297 else if (next->type == CPP_OPEN_PAREN)
31299 matching_parens parens;
31300 parens.consume_open (parser); /* Eat '('. */
31302 /* If there is no category name, and this is an @interface, we
31303 have a class extension. */
31304 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31306 *categ = NULL_TREE;
31307 *is_class_extension = true;
31309 else
31310 *categ = cp_parser_identifier (parser);
31312 parens.require_close (parser);
31316 /* Parse an Objective-C class interface. */
31318 static void
31319 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31321 tree name, super, categ, protos;
31322 bool is_class_extension;
31324 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31325 name = cp_parser_identifier (parser);
31326 if (name == error_mark_node)
31328 /* It's hard to recover because even if valid @interface stuff
31329 is to follow, we can't compile it (or validate it) if we
31330 don't even know which class it refers to. Let's assume this
31331 was a stray '@interface' token in the stream and skip it.
31333 return;
31335 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31336 &is_class_extension);
31337 protos = cp_parser_objc_protocol_refs_opt (parser);
31339 /* We have either a class or a category on our hands. */
31340 if (categ || is_class_extension)
31341 objc_start_category_interface (name, categ, protos, attributes);
31342 else
31344 objc_start_class_interface (name, super, protos, attributes);
31345 /* Handle instance variable declarations, if any. */
31346 cp_parser_objc_class_ivars (parser);
31347 objc_continue_interface ();
31350 cp_parser_objc_method_prototype_list (parser);
31353 /* Parse an Objective-C class implementation. */
31355 static void
31356 cp_parser_objc_class_implementation (cp_parser* parser)
31358 tree name, super, categ;
31359 bool is_class_extension;
31361 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31362 name = cp_parser_identifier (parser);
31363 if (name == error_mark_node)
31365 /* It's hard to recover because even if valid @implementation
31366 stuff is to follow, we can't compile it (or validate it) if
31367 we don't even know which class it refers to. Let's assume
31368 this was a stray '@implementation' token in the stream and
31369 skip it.
31371 return;
31373 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31374 &is_class_extension);
31376 /* We have either a class or a category on our hands. */
31377 if (categ)
31378 objc_start_category_implementation (name, categ);
31379 else
31381 objc_start_class_implementation (name, super);
31382 /* Handle instance variable declarations, if any. */
31383 cp_parser_objc_class_ivars (parser);
31384 objc_continue_implementation ();
31387 cp_parser_objc_method_definition_list (parser);
31390 /* Consume the @end token and finish off the implementation. */
31392 static void
31393 cp_parser_objc_end_implementation (cp_parser* parser)
31395 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31396 objc_finish_implementation ();
31399 /* Parse an Objective-C declaration. */
31401 static void
31402 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31404 /* Try to figure out what kind of declaration is present. */
31405 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31407 if (attributes)
31408 switch (kwd->keyword)
31410 case RID_AT_ALIAS:
31411 case RID_AT_CLASS:
31412 case RID_AT_END:
31413 error_at (kwd->location, "attributes may not be specified before"
31414 " the %<@%D%> Objective-C++ keyword",
31415 kwd->u.value);
31416 attributes = NULL;
31417 break;
31418 case RID_AT_IMPLEMENTATION:
31419 warning_at (kwd->location, OPT_Wattributes,
31420 "prefix attributes are ignored before %<@%D%>",
31421 kwd->u.value);
31422 attributes = NULL;
31423 default:
31424 break;
31427 switch (kwd->keyword)
31429 case RID_AT_ALIAS:
31430 cp_parser_objc_alias_declaration (parser);
31431 break;
31432 case RID_AT_CLASS:
31433 cp_parser_objc_class_declaration (parser);
31434 break;
31435 case RID_AT_PROTOCOL:
31436 cp_parser_objc_protocol_declaration (parser, attributes);
31437 break;
31438 case RID_AT_INTERFACE:
31439 cp_parser_objc_class_interface (parser, attributes);
31440 break;
31441 case RID_AT_IMPLEMENTATION:
31442 cp_parser_objc_class_implementation (parser);
31443 break;
31444 case RID_AT_END:
31445 cp_parser_objc_end_implementation (parser);
31446 break;
31447 default:
31448 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31449 kwd->u.value);
31450 cp_parser_skip_to_end_of_block_or_statement (parser);
31454 /* Parse an Objective-C try-catch-finally statement.
31456 objc-try-catch-finally-stmt:
31457 @try compound-statement objc-catch-clause-seq [opt]
31458 objc-finally-clause [opt]
31460 objc-catch-clause-seq:
31461 objc-catch-clause objc-catch-clause-seq [opt]
31463 objc-catch-clause:
31464 @catch ( objc-exception-declaration ) compound-statement
31466 objc-finally-clause:
31467 @finally compound-statement
31469 objc-exception-declaration:
31470 parameter-declaration
31471 '...'
31473 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31475 Returns NULL_TREE.
31477 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31478 for C. Keep them in sync. */
31480 static tree
31481 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31483 location_t location;
31484 tree stmt;
31486 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31487 location = cp_lexer_peek_token (parser->lexer)->location;
31488 objc_maybe_warn_exceptions (location);
31489 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31490 node, lest it get absorbed into the surrounding block. */
31491 stmt = push_stmt_list ();
31492 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31493 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31495 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31497 cp_parameter_declarator *parm;
31498 tree parameter_declaration = error_mark_node;
31499 bool seen_open_paren = false;
31500 matching_parens parens;
31502 cp_lexer_consume_token (parser->lexer);
31503 if (parens.require_open (parser))
31504 seen_open_paren = true;
31505 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31507 /* We have "@catch (...)" (where the '...' are literally
31508 what is in the code). Skip the '...'.
31509 parameter_declaration is set to NULL_TREE, and
31510 objc_being_catch_clauses() knows that that means
31511 '...'. */
31512 cp_lexer_consume_token (parser->lexer);
31513 parameter_declaration = NULL_TREE;
31515 else
31517 /* We have "@catch (NSException *exception)" or something
31518 like that. Parse the parameter declaration. */
31519 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31520 false, NULL);
31521 if (parm == NULL)
31522 parameter_declaration = error_mark_node;
31523 else
31524 parameter_declaration = grokdeclarator (parm->declarator,
31525 &parm->decl_specifiers,
31526 PARM, /*initialized=*/0,
31527 /*attrlist=*/NULL);
31529 if (seen_open_paren)
31530 parens.require_close (parser);
31531 else
31533 /* If there was no open parenthesis, we are recovering from
31534 an error, and we are trying to figure out what mistake
31535 the user has made. */
31537 /* If there is an immediate closing parenthesis, the user
31538 probably forgot the opening one (ie, they typed "@catch
31539 NSException *e)". Parse the closing parenthesis and keep
31540 going. */
31541 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31542 cp_lexer_consume_token (parser->lexer);
31544 /* If these is no immediate closing parenthesis, the user
31545 probably doesn't know that parenthesis are required at
31546 all (ie, they typed "@catch NSException *e"). So, just
31547 forget about the closing parenthesis and keep going. */
31549 objc_begin_catch_clause (parameter_declaration);
31550 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31551 objc_finish_catch_clause ();
31553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31555 cp_lexer_consume_token (parser->lexer);
31556 location = cp_lexer_peek_token (parser->lexer)->location;
31557 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31558 node, lest it get absorbed into the surrounding block. */
31559 stmt = push_stmt_list ();
31560 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31561 objc_build_finally_clause (location, pop_stmt_list (stmt));
31564 return objc_finish_try_stmt ();
31567 /* Parse an Objective-C synchronized statement.
31569 objc-synchronized-stmt:
31570 @synchronized ( expression ) compound-statement
31572 Returns NULL_TREE. */
31574 static tree
31575 cp_parser_objc_synchronized_statement (cp_parser *parser)
31577 location_t location;
31578 tree lock, stmt;
31580 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31582 location = cp_lexer_peek_token (parser->lexer)->location;
31583 objc_maybe_warn_exceptions (location);
31584 matching_parens parens;
31585 parens.require_open (parser);
31586 lock = cp_parser_expression (parser);
31587 parens.require_close (parser);
31589 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31590 node, lest it get absorbed into the surrounding block. */
31591 stmt = push_stmt_list ();
31592 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31594 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31597 /* Parse an Objective-C throw statement.
31599 objc-throw-stmt:
31600 @throw assignment-expression [opt] ;
31602 Returns a constructed '@throw' statement. */
31604 static tree
31605 cp_parser_objc_throw_statement (cp_parser *parser)
31607 tree expr = NULL_TREE;
31608 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31610 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31613 expr = cp_parser_expression (parser);
31615 cp_parser_consume_semicolon_at_end_of_statement (parser);
31617 return objc_build_throw_stmt (loc, expr);
31620 /* Parse an Objective-C statement. */
31622 static tree
31623 cp_parser_objc_statement (cp_parser * parser)
31625 /* Try to figure out what kind of declaration is present. */
31626 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31628 switch (kwd->keyword)
31630 case RID_AT_TRY:
31631 return cp_parser_objc_try_catch_finally_statement (parser);
31632 case RID_AT_SYNCHRONIZED:
31633 return cp_parser_objc_synchronized_statement (parser);
31634 case RID_AT_THROW:
31635 return cp_parser_objc_throw_statement (parser);
31636 default:
31637 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31638 kwd->u.value);
31639 cp_parser_skip_to_end_of_block_or_statement (parser);
31642 return error_mark_node;
31645 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31646 look ahead to see if an objc keyword follows the attributes. This
31647 is to detect the use of prefix attributes on ObjC @interface and
31648 @protocol. */
31650 static bool
31651 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31653 cp_lexer_save_tokens (parser->lexer);
31654 *attrib = cp_parser_attributes_opt (parser);
31655 gcc_assert (*attrib);
31656 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31658 cp_lexer_commit_tokens (parser->lexer);
31659 return true;
31661 cp_lexer_rollback_tokens (parser->lexer);
31662 return false;
31665 /* This routine is a minimal replacement for
31666 c_parser_struct_declaration () used when parsing the list of
31667 types/names or ObjC++ properties. For example, when parsing the
31668 code
31670 @property (readonly) int a, b, c;
31672 this function is responsible for parsing "int a, int b, int c" and
31673 returning the declarations as CHAIN of DECLs.
31675 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31676 similar parsing. */
31677 static tree
31678 cp_parser_objc_struct_declaration (cp_parser *parser)
31680 tree decls = NULL_TREE;
31681 cp_decl_specifier_seq declspecs;
31682 int decl_class_or_enum_p;
31683 tree prefix_attributes;
31685 cp_parser_decl_specifier_seq (parser,
31686 CP_PARSER_FLAGS_NONE,
31687 &declspecs,
31688 &decl_class_or_enum_p);
31690 if (declspecs.type == error_mark_node)
31691 return error_mark_node;
31693 /* auto, register, static, extern, mutable. */
31694 if (declspecs.storage_class != sc_none)
31696 cp_parser_error (parser, "invalid type for property");
31697 declspecs.storage_class = sc_none;
31700 /* thread_local. */
31701 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31703 cp_parser_error (parser, "invalid type for property");
31704 declspecs.locations[ds_thread] = 0;
31707 /* typedef. */
31708 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31710 cp_parser_error (parser, "invalid type for property");
31711 declspecs.locations[ds_typedef] = 0;
31714 prefix_attributes = declspecs.attributes;
31715 declspecs.attributes = NULL_TREE;
31717 /* Keep going until we hit the `;' at the end of the declaration. */
31718 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31720 tree attributes, first_attribute, decl;
31721 cp_declarator *declarator;
31722 cp_token *token;
31724 /* Parse the declarator. */
31725 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31726 CP_PARSER_FLAGS_NONE,
31727 NULL, NULL, false, false, false);
31729 /* Look for attributes that apply to the ivar. */
31730 attributes = cp_parser_attributes_opt (parser);
31731 /* Remember which attributes are prefix attributes and
31732 which are not. */
31733 first_attribute = attributes;
31734 /* Combine the attributes. */
31735 attributes = attr_chainon (prefix_attributes, attributes);
31737 decl = grokfield (declarator, &declspecs,
31738 NULL_TREE, /*init_const_expr_p=*/false,
31739 NULL_TREE, attributes);
31741 if (decl == error_mark_node || decl == NULL_TREE)
31742 return error_mark_node;
31744 /* Reset PREFIX_ATTRIBUTES. */
31745 if (attributes != error_mark_node)
31747 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31748 attributes = TREE_CHAIN (attributes);
31749 if (attributes)
31750 TREE_CHAIN (attributes) = NULL_TREE;
31753 DECL_CHAIN (decl) = decls;
31754 decls = decl;
31756 token = cp_lexer_peek_token (parser->lexer);
31757 if (token->type == CPP_COMMA)
31759 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31760 continue;
31762 else
31763 break;
31765 return decls;
31768 /* Parse an Objective-C @property declaration. The syntax is:
31770 objc-property-declaration:
31771 '@property' objc-property-attributes[opt] struct-declaration ;
31773 objc-property-attributes:
31774 '(' objc-property-attribute-list ')'
31776 objc-property-attribute-list:
31777 objc-property-attribute
31778 objc-property-attribute-list, objc-property-attribute
31780 objc-property-attribute
31781 'getter' = identifier
31782 'setter' = identifier
31783 'readonly'
31784 'readwrite'
31785 'assign'
31786 'retain'
31787 'copy'
31788 'nonatomic'
31790 For example:
31791 @property NSString *name;
31792 @property (readonly) id object;
31793 @property (retain, nonatomic, getter=getTheName) id name;
31794 @property int a, b, c;
31796 PS: This function is identical to
31797 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31798 static void
31799 cp_parser_objc_at_property_declaration (cp_parser *parser)
31801 /* The following variables hold the attributes of the properties as
31802 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31803 seen. When we see an attribute, we set them to 'true' (if they
31804 are boolean properties) or to the identifier (if they have an
31805 argument, ie, for getter and setter). Note that here we only
31806 parse the list of attributes, check the syntax and accumulate the
31807 attributes that we find. objc_add_property_declaration() will
31808 then process the information. */
31809 bool property_assign = false;
31810 bool property_copy = false;
31811 tree property_getter_ident = NULL_TREE;
31812 bool property_nonatomic = false;
31813 bool property_readonly = false;
31814 bool property_readwrite = false;
31815 bool property_retain = false;
31816 tree property_setter_ident = NULL_TREE;
31818 /* 'properties' is the list of properties that we read. Usually a
31819 single one, but maybe more (eg, in "@property int a, b, c;" there
31820 are three). */
31821 tree properties;
31822 location_t loc;
31824 loc = cp_lexer_peek_token (parser->lexer)->location;
31826 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31828 /* Parse the optional attribute list... */
31829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31831 /* Eat the '('. */
31832 matching_parens parens;
31833 parens.consume_open (parser);
31835 while (true)
31837 bool syntax_error = false;
31838 cp_token *token = cp_lexer_peek_token (parser->lexer);
31839 enum rid keyword;
31841 if (token->type != CPP_NAME)
31843 cp_parser_error (parser, "expected identifier");
31844 break;
31846 keyword = C_RID_CODE (token->u.value);
31847 cp_lexer_consume_token (parser->lexer);
31848 switch (keyword)
31850 case RID_ASSIGN: property_assign = true; break;
31851 case RID_COPY: property_copy = true; break;
31852 case RID_NONATOMIC: property_nonatomic = true; break;
31853 case RID_READONLY: property_readonly = true; break;
31854 case RID_READWRITE: property_readwrite = true; break;
31855 case RID_RETAIN: property_retain = true; break;
31857 case RID_GETTER:
31858 case RID_SETTER:
31859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31861 if (keyword == RID_GETTER)
31862 cp_parser_error (parser,
31863 "missing %<=%> (after %<getter%> attribute)");
31864 else
31865 cp_parser_error (parser,
31866 "missing %<=%> (after %<setter%> attribute)");
31867 syntax_error = true;
31868 break;
31870 cp_lexer_consume_token (parser->lexer); /* eat the = */
31871 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31873 cp_parser_error (parser, "expected identifier");
31874 syntax_error = true;
31875 break;
31877 if (keyword == RID_SETTER)
31879 if (property_setter_ident != NULL_TREE)
31881 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31882 cp_lexer_consume_token (parser->lexer);
31884 else
31885 property_setter_ident = cp_parser_objc_selector (parser);
31886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31887 cp_parser_error (parser, "setter name must terminate with %<:%>");
31888 else
31889 cp_lexer_consume_token (parser->lexer);
31891 else
31893 if (property_getter_ident != NULL_TREE)
31895 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31896 cp_lexer_consume_token (parser->lexer);
31898 else
31899 property_getter_ident = cp_parser_objc_selector (parser);
31901 break;
31902 default:
31903 cp_parser_error (parser, "unknown property attribute");
31904 syntax_error = true;
31905 break;
31908 if (syntax_error)
31909 break;
31911 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31912 cp_lexer_consume_token (parser->lexer);
31913 else
31914 break;
31917 /* FIXME: "@property (setter, assign);" will generate a spurious
31918 "error: expected ‘)’ before ‘,’ token". This is because
31919 cp_parser_require, unlike the C counterpart, will produce an
31920 error even if we are in error recovery. */
31921 if (!parens.require_close (parser))
31923 cp_parser_skip_to_closing_parenthesis (parser,
31924 /*recovering=*/true,
31925 /*or_comma=*/false,
31926 /*consume_paren=*/true);
31930 /* ... and the property declaration(s). */
31931 properties = cp_parser_objc_struct_declaration (parser);
31933 if (properties == error_mark_node)
31935 cp_parser_skip_to_end_of_statement (parser);
31936 /* If the next token is now a `;', consume it. */
31937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31938 cp_lexer_consume_token (parser->lexer);
31939 return;
31942 if (properties == NULL_TREE)
31943 cp_parser_error (parser, "expected identifier");
31944 else
31946 /* Comma-separated properties are chained together in
31947 reverse order; add them one by one. */
31948 properties = nreverse (properties);
31950 for (; properties; properties = TREE_CHAIN (properties))
31951 objc_add_property_declaration (loc, copy_node (properties),
31952 property_readonly, property_readwrite,
31953 property_assign, property_retain,
31954 property_copy, property_nonatomic,
31955 property_getter_ident, property_setter_ident);
31958 cp_parser_consume_semicolon_at_end_of_statement (parser);
31961 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31963 objc-synthesize-declaration:
31964 @synthesize objc-synthesize-identifier-list ;
31966 objc-synthesize-identifier-list:
31967 objc-synthesize-identifier
31968 objc-synthesize-identifier-list, objc-synthesize-identifier
31970 objc-synthesize-identifier
31971 identifier
31972 identifier = identifier
31974 For example:
31975 @synthesize MyProperty;
31976 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31978 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31979 for C. Keep them in sync.
31981 static void
31982 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31984 tree list = NULL_TREE;
31985 location_t loc;
31986 loc = cp_lexer_peek_token (parser->lexer)->location;
31988 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31989 while (true)
31991 tree property, ivar;
31992 property = cp_parser_identifier (parser);
31993 if (property == error_mark_node)
31995 cp_parser_consume_semicolon_at_end_of_statement (parser);
31996 return;
31998 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32000 cp_lexer_consume_token (parser->lexer);
32001 ivar = cp_parser_identifier (parser);
32002 if (ivar == error_mark_node)
32004 cp_parser_consume_semicolon_at_end_of_statement (parser);
32005 return;
32008 else
32009 ivar = NULL_TREE;
32010 list = chainon (list, build_tree_list (ivar, property));
32011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32012 cp_lexer_consume_token (parser->lexer);
32013 else
32014 break;
32016 cp_parser_consume_semicolon_at_end_of_statement (parser);
32017 objc_add_synthesize_declaration (loc, list);
32020 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32022 objc-dynamic-declaration:
32023 @dynamic identifier-list ;
32025 For example:
32026 @dynamic MyProperty;
32027 @dynamic MyProperty, AnotherProperty;
32029 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32030 for C. Keep them in sync.
32032 static void
32033 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32035 tree list = NULL_TREE;
32036 location_t loc;
32037 loc = cp_lexer_peek_token (parser->lexer)->location;
32039 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32040 while (true)
32042 tree property;
32043 property = cp_parser_identifier (parser);
32044 if (property == error_mark_node)
32046 cp_parser_consume_semicolon_at_end_of_statement (parser);
32047 return;
32049 list = chainon (list, build_tree_list (NULL, property));
32050 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32051 cp_lexer_consume_token (parser->lexer);
32052 else
32053 break;
32055 cp_parser_consume_semicolon_at_end_of_statement (parser);
32056 objc_add_dynamic_declaration (loc, list);
32060 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32062 /* Returns name of the next clause.
32063 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32064 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32065 returned and the token is consumed. */
32067 static pragma_omp_clause
32068 cp_parser_omp_clause_name (cp_parser *parser)
32070 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32073 result = PRAGMA_OACC_CLAUSE_AUTO;
32074 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32075 result = PRAGMA_OMP_CLAUSE_IF;
32076 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32077 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32078 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32079 result = PRAGMA_OACC_CLAUSE_DELETE;
32080 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32081 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32082 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32083 result = PRAGMA_OMP_CLAUSE_FOR;
32084 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32086 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32087 const char *p = IDENTIFIER_POINTER (id);
32089 switch (p[0])
32091 case 'a':
32092 if (!strcmp ("aligned", p))
32093 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32094 else if (!strcmp ("async", p))
32095 result = PRAGMA_OACC_CLAUSE_ASYNC;
32096 break;
32097 case 'c':
32098 if (!strcmp ("collapse", p))
32099 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32100 else if (!strcmp ("copy", p))
32101 result = PRAGMA_OACC_CLAUSE_COPY;
32102 else if (!strcmp ("copyin", p))
32103 result = PRAGMA_OMP_CLAUSE_COPYIN;
32104 else if (!strcmp ("copyout", p))
32105 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32106 else if (!strcmp ("copyprivate", p))
32107 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32108 else if (!strcmp ("create", p))
32109 result = PRAGMA_OACC_CLAUSE_CREATE;
32110 break;
32111 case 'd':
32112 if (!strcmp ("defaultmap", p))
32113 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32114 else if (!strcmp ("depend", p))
32115 result = PRAGMA_OMP_CLAUSE_DEPEND;
32116 else if (!strcmp ("device", p))
32117 result = PRAGMA_OMP_CLAUSE_DEVICE;
32118 else if (!strcmp ("deviceptr", p))
32119 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32120 else if (!strcmp ("device_resident", p))
32121 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32122 else if (!strcmp ("dist_schedule", p))
32123 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32124 break;
32125 case 'f':
32126 if (!strcmp ("final", p))
32127 result = PRAGMA_OMP_CLAUSE_FINAL;
32128 else if (!strcmp ("finalize", p))
32129 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32130 else if (!strcmp ("firstprivate", p))
32131 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32132 else if (!strcmp ("from", p))
32133 result = PRAGMA_OMP_CLAUSE_FROM;
32134 break;
32135 case 'g':
32136 if (!strcmp ("gang", p))
32137 result = PRAGMA_OACC_CLAUSE_GANG;
32138 else if (!strcmp ("grainsize", p))
32139 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32140 break;
32141 case 'h':
32142 if (!strcmp ("hint", p))
32143 result = PRAGMA_OMP_CLAUSE_HINT;
32144 else if (!strcmp ("host", p))
32145 result = PRAGMA_OACC_CLAUSE_HOST;
32146 break;
32147 case 'i':
32148 if (!strcmp ("if_present", p))
32149 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32150 else if (!strcmp ("in_reduction", p))
32151 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32152 else if (!strcmp ("inbranch", p))
32153 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32154 else if (!strcmp ("independent", p))
32155 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32156 else if (!strcmp ("is_device_ptr", p))
32157 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32158 break;
32159 case 'l':
32160 if (!strcmp ("lastprivate", p))
32161 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32162 else if (!strcmp ("linear", p))
32163 result = PRAGMA_OMP_CLAUSE_LINEAR;
32164 else if (!strcmp ("link", p))
32165 result = PRAGMA_OMP_CLAUSE_LINK;
32166 break;
32167 case 'm':
32168 if (!strcmp ("map", p))
32169 result = PRAGMA_OMP_CLAUSE_MAP;
32170 else if (!strcmp ("mergeable", p))
32171 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32172 break;
32173 case 'n':
32174 if (!strcmp ("nogroup", p))
32175 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32176 else if (!strcmp ("nontemporal", p))
32177 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32178 else if (!strcmp ("notinbranch", p))
32179 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32180 else if (!strcmp ("nowait", p))
32181 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32182 else if (!strcmp ("num_gangs", p))
32183 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32184 else if (!strcmp ("num_tasks", p))
32185 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32186 else if (!strcmp ("num_teams", p))
32187 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32188 else if (!strcmp ("num_threads", p))
32189 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32190 else if (!strcmp ("num_workers", p))
32191 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32192 break;
32193 case 'o':
32194 if (!strcmp ("ordered", p))
32195 result = PRAGMA_OMP_CLAUSE_ORDERED;
32196 break;
32197 case 'p':
32198 if (!strcmp ("parallel", p))
32199 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32200 else if (!strcmp ("present", p))
32201 result = PRAGMA_OACC_CLAUSE_PRESENT;
32202 else if (!strcmp ("present_or_copy", p)
32203 || !strcmp ("pcopy", p))
32204 result = PRAGMA_OACC_CLAUSE_COPY;
32205 else if (!strcmp ("present_or_copyin", p)
32206 || !strcmp ("pcopyin", p))
32207 result = PRAGMA_OACC_CLAUSE_COPYIN;
32208 else if (!strcmp ("present_or_copyout", p)
32209 || !strcmp ("pcopyout", p))
32210 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32211 else if (!strcmp ("present_or_create", p)
32212 || !strcmp ("pcreate", p))
32213 result = PRAGMA_OACC_CLAUSE_CREATE;
32214 else if (!strcmp ("priority", p))
32215 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32216 else if (!strcmp ("proc_bind", p))
32217 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32218 break;
32219 case 'r':
32220 if (!strcmp ("reduction", p))
32221 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32222 break;
32223 case 's':
32224 if (!strcmp ("safelen", p))
32225 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32226 else if (!strcmp ("schedule", p))
32227 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32228 else if (!strcmp ("sections", p))
32229 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32230 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32231 result = PRAGMA_OACC_CLAUSE_HOST;
32232 else if (!strcmp ("seq", p))
32233 result = PRAGMA_OACC_CLAUSE_SEQ;
32234 else if (!strcmp ("shared", p))
32235 result = PRAGMA_OMP_CLAUSE_SHARED;
32236 else if (!strcmp ("simd", p))
32237 result = PRAGMA_OMP_CLAUSE_SIMD;
32238 else if (!strcmp ("simdlen", p))
32239 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32240 break;
32241 case 't':
32242 if (!strcmp ("task_reduction", p))
32243 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32244 else if (!strcmp ("taskgroup", p))
32245 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32246 else if (!strcmp ("thread_limit", p))
32247 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32248 else if (!strcmp ("threads", p))
32249 result = PRAGMA_OMP_CLAUSE_THREADS;
32250 else if (!strcmp ("tile", p))
32251 result = PRAGMA_OACC_CLAUSE_TILE;
32252 else if (!strcmp ("to", p))
32253 result = PRAGMA_OMP_CLAUSE_TO;
32254 break;
32255 case 'u':
32256 if (!strcmp ("uniform", p))
32257 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32258 else if (!strcmp ("untied", p))
32259 result = PRAGMA_OMP_CLAUSE_UNTIED;
32260 else if (!strcmp ("use_device", p))
32261 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32262 else if (!strcmp ("use_device_ptr", p))
32263 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32264 break;
32265 case 'v':
32266 if (!strcmp ("vector", p))
32267 result = PRAGMA_OACC_CLAUSE_VECTOR;
32268 else if (!strcmp ("vector_length", p))
32269 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32270 break;
32271 case 'w':
32272 if (!strcmp ("wait", p))
32273 result = PRAGMA_OACC_CLAUSE_WAIT;
32274 else if (!strcmp ("worker", p))
32275 result = PRAGMA_OACC_CLAUSE_WORKER;
32276 break;
32280 if (result != PRAGMA_OMP_CLAUSE_NONE)
32281 cp_lexer_consume_token (parser->lexer);
32283 return result;
32286 /* Validate that a clause of the given type does not already exist. */
32288 static void
32289 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32290 const char *name, location_t location)
32292 tree c;
32294 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32295 if (OMP_CLAUSE_CODE (c) == code)
32297 error_at (location, "too many %qs clauses", name);
32298 break;
32302 /* OpenMP 2.5:
32303 variable-list:
32304 identifier
32305 variable-list , identifier
32307 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32308 colon). An opening parenthesis will have been consumed by the caller.
32310 If KIND is nonzero, create the appropriate node and install the decl
32311 in OMP_CLAUSE_DECL and add the node to the head of the list.
32313 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32314 return the list created.
32316 COLON can be NULL if only closing parenthesis should end the list,
32317 or pointer to bool which will receive false if the list is terminated
32318 by closing parenthesis or true if the list is terminated by colon. */
32320 static tree
32321 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32322 tree list, bool *colon)
32324 cp_token *token;
32325 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32326 if (colon)
32328 parser->colon_corrects_to_scope_p = false;
32329 *colon = false;
32331 while (1)
32333 tree name, decl;
32335 if (kind == OMP_CLAUSE_DEPEND)
32336 cp_parser_parse_tentatively (parser);
32337 token = cp_lexer_peek_token (parser->lexer);
32338 if (kind != 0
32339 && current_class_ptr
32340 && cp_parser_is_keyword (token, RID_THIS))
32342 decl = finish_this_expr ();
32343 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32344 || CONVERT_EXPR_P (decl))
32345 decl = TREE_OPERAND (decl, 0);
32346 cp_lexer_consume_token (parser->lexer);
32348 else
32350 name = cp_parser_id_expression (parser, /*template_p=*/false,
32351 /*check_dependency_p=*/true,
32352 /*template_p=*/NULL,
32353 /*declarator_p=*/false,
32354 /*optional_p=*/false);
32355 if (name == error_mark_node)
32357 if (kind == OMP_CLAUSE_DEPEND
32358 && cp_parser_simulate_error (parser))
32359 goto depend_lvalue;
32360 goto skip_comma;
32363 if (identifier_p (name))
32364 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32365 else
32366 decl = name;
32367 if (decl == error_mark_node)
32369 if (kind == OMP_CLAUSE_DEPEND
32370 && cp_parser_simulate_error (parser))
32371 goto depend_lvalue;
32372 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32373 token->location);
32376 if (decl == error_mark_node)
32378 else if (kind != 0)
32380 switch (kind)
32382 case OMP_CLAUSE__CACHE_:
32383 /* The OpenACC cache directive explicitly only allows "array
32384 elements or subarrays". */
32385 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32387 error_at (token->location, "expected %<[%>");
32388 decl = error_mark_node;
32389 break;
32391 /* FALLTHROUGH. */
32392 case OMP_CLAUSE_MAP:
32393 case OMP_CLAUSE_FROM:
32394 case OMP_CLAUSE_TO:
32395 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32397 location_t loc
32398 = cp_lexer_peek_token (parser->lexer)->location;
32399 cp_id_kind idk = CP_ID_KIND_NONE;
32400 cp_lexer_consume_token (parser->lexer);
32401 decl = convert_from_reference (decl);
32402 decl
32403 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32404 decl, false,
32405 &idk, loc);
32407 /* FALLTHROUGH. */
32408 case OMP_CLAUSE_DEPEND:
32409 case OMP_CLAUSE_REDUCTION:
32410 case OMP_CLAUSE_IN_REDUCTION:
32411 case OMP_CLAUSE_TASK_REDUCTION:
32412 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32414 tree low_bound = NULL_TREE, length = NULL_TREE;
32416 parser->colon_corrects_to_scope_p = false;
32417 cp_lexer_consume_token (parser->lexer);
32418 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32419 low_bound = cp_parser_expression (parser);
32420 if (!colon)
32421 parser->colon_corrects_to_scope_p
32422 = saved_colon_corrects_to_scope_p;
32423 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32424 length = integer_one_node;
32425 else
32427 /* Look for `:'. */
32428 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32430 if (kind == OMP_CLAUSE_DEPEND
32431 && cp_parser_simulate_error (parser))
32432 goto depend_lvalue;
32433 goto skip_comma;
32435 if (kind == OMP_CLAUSE_DEPEND)
32436 cp_parser_commit_to_tentative_parse (parser);
32437 if (!cp_lexer_next_token_is (parser->lexer,
32438 CPP_CLOSE_SQUARE))
32439 length = cp_parser_expression (parser);
32441 /* Look for the closing `]'. */
32442 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32443 RT_CLOSE_SQUARE))
32445 if (kind == OMP_CLAUSE_DEPEND
32446 && cp_parser_simulate_error (parser))
32447 goto depend_lvalue;
32448 goto skip_comma;
32451 decl = tree_cons (low_bound, length, decl);
32453 break;
32454 default:
32455 break;
32458 if (kind == OMP_CLAUSE_DEPEND)
32460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32461 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32462 && cp_parser_simulate_error (parser))
32464 depend_lvalue:
32465 cp_parser_abort_tentative_parse (parser);
32466 decl = cp_parser_assignment_expression (parser, NULL,
32467 false, false);
32469 else
32470 cp_parser_parse_definitely (parser);
32473 tree u = build_omp_clause (token->location, kind);
32474 OMP_CLAUSE_DECL (u) = decl;
32475 OMP_CLAUSE_CHAIN (u) = list;
32476 list = u;
32478 else
32479 list = tree_cons (decl, NULL_TREE, list);
32481 get_comma:
32482 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32483 break;
32484 cp_lexer_consume_token (parser->lexer);
32487 if (colon)
32488 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32490 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32492 *colon = true;
32493 cp_parser_require (parser, CPP_COLON, RT_COLON);
32494 return list;
32497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32499 int ending;
32501 /* Try to resync to an unnested comma. Copied from
32502 cp_parser_parenthesized_expression_list. */
32503 skip_comma:
32504 if (colon)
32505 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32506 ending = cp_parser_skip_to_closing_parenthesis (parser,
32507 /*recovering=*/true,
32508 /*or_comma=*/true,
32509 /*consume_paren=*/true);
32510 if (ending < 0)
32511 goto get_comma;
32514 return list;
32517 /* Similarly, but expect leading and trailing parenthesis. This is a very
32518 common case for omp clauses. */
32520 static tree
32521 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32523 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32524 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32525 return list;
32528 /* OpenACC 2.0:
32529 copy ( variable-list )
32530 copyin ( variable-list )
32531 copyout ( variable-list )
32532 create ( variable-list )
32533 delete ( variable-list )
32534 present ( variable-list ) */
32536 static tree
32537 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32538 tree list)
32540 enum gomp_map_kind kind;
32541 switch (c_kind)
32543 case PRAGMA_OACC_CLAUSE_COPY:
32544 kind = GOMP_MAP_TOFROM;
32545 break;
32546 case PRAGMA_OACC_CLAUSE_COPYIN:
32547 kind = GOMP_MAP_TO;
32548 break;
32549 case PRAGMA_OACC_CLAUSE_COPYOUT:
32550 kind = GOMP_MAP_FROM;
32551 break;
32552 case PRAGMA_OACC_CLAUSE_CREATE:
32553 kind = GOMP_MAP_ALLOC;
32554 break;
32555 case PRAGMA_OACC_CLAUSE_DELETE:
32556 kind = GOMP_MAP_RELEASE;
32557 break;
32558 case PRAGMA_OACC_CLAUSE_DEVICE:
32559 kind = GOMP_MAP_FORCE_TO;
32560 break;
32561 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32562 kind = GOMP_MAP_DEVICE_RESIDENT;
32563 break;
32564 case PRAGMA_OACC_CLAUSE_HOST:
32565 kind = GOMP_MAP_FORCE_FROM;
32566 break;
32567 case PRAGMA_OACC_CLAUSE_LINK:
32568 kind = GOMP_MAP_LINK;
32569 break;
32570 case PRAGMA_OACC_CLAUSE_PRESENT:
32571 kind = GOMP_MAP_FORCE_PRESENT;
32572 break;
32573 default:
32574 gcc_unreachable ();
32576 tree nl, c;
32577 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32579 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32580 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32582 return nl;
32585 /* OpenACC 2.0:
32586 deviceptr ( variable-list ) */
32588 static tree
32589 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32591 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32592 tree vars, t;
32594 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32595 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32596 variable-list must only allow for pointer variables. */
32597 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32598 for (t = vars; t; t = TREE_CHAIN (t))
32600 tree v = TREE_PURPOSE (t);
32601 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32602 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32603 OMP_CLAUSE_DECL (u) = v;
32604 OMP_CLAUSE_CHAIN (u) = list;
32605 list = u;
32608 return list;
32611 /* OpenACC 2.5:
32612 auto
32613 finalize
32614 independent
32615 nohost
32616 seq */
32618 static tree
32619 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32620 tree list)
32622 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32624 tree c = build_omp_clause (loc, code);
32625 OMP_CLAUSE_CHAIN (c) = list;
32627 return c;
32630 /* OpenACC:
32631 num_gangs ( expression )
32632 num_workers ( expression )
32633 vector_length ( expression ) */
32635 static tree
32636 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32637 const char *str, tree list)
32639 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32641 matching_parens parens;
32642 if (!parens.require_open (parser))
32643 return list;
32645 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32647 if (t == error_mark_node
32648 || !parens.require_close (parser))
32650 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32651 /*or_comma=*/false,
32652 /*consume_paren=*/true);
32653 return list;
32656 check_no_duplicate_clause (list, code, str, loc);
32658 tree c = build_omp_clause (loc, code);
32659 OMP_CLAUSE_OPERAND (c, 0) = t;
32660 OMP_CLAUSE_CHAIN (c) = list;
32661 return c;
32664 /* OpenACC:
32666 gang [( gang-arg-list )]
32667 worker [( [num:] int-expr )]
32668 vector [( [length:] int-expr )]
32670 where gang-arg is one of:
32672 [num:] int-expr
32673 static: size-expr
32675 and size-expr may be:
32678 int-expr
32681 static tree
32682 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
32683 omp_clause_code kind,
32684 const char *str, tree list)
32686 const char *id = "num";
32687 cp_lexer *lexer = parser->lexer;
32688 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32690 if (kind == OMP_CLAUSE_VECTOR)
32691 id = "length";
32693 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32695 matching_parens parens;
32696 parens.consume_open (parser);
32700 cp_token *next = cp_lexer_peek_token (lexer);
32701 int idx = 0;
32703 /* Gang static argument. */
32704 if (kind == OMP_CLAUSE_GANG
32705 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32707 cp_lexer_consume_token (lexer);
32709 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32710 goto cleanup_error;
32712 idx = 1;
32713 if (ops[idx] != NULL)
32715 cp_parser_error (parser, "too many %<static%> arguments");
32716 goto cleanup_error;
32719 /* Check for the '*' argument. */
32720 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32721 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32722 || cp_lexer_nth_token_is (parser->lexer, 2,
32723 CPP_CLOSE_PAREN)))
32725 cp_lexer_consume_token (lexer);
32726 ops[idx] = integer_minus_one_node;
32728 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32730 cp_lexer_consume_token (lexer);
32731 continue;
32733 else break;
32736 /* Worker num: argument and vector length: arguments. */
32737 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32738 && id_equal (next->u.value, id)
32739 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32741 cp_lexer_consume_token (lexer); /* id */
32742 cp_lexer_consume_token (lexer); /* ':' */
32745 /* Now collect the actual argument. */
32746 if (ops[idx] != NULL_TREE)
32748 cp_parser_error (parser, "unexpected argument");
32749 goto cleanup_error;
32752 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32753 false);
32754 if (expr == error_mark_node)
32755 goto cleanup_error;
32757 mark_exp_read (expr);
32758 ops[idx] = expr;
32760 if (kind == OMP_CLAUSE_GANG
32761 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32763 cp_lexer_consume_token (lexer);
32764 continue;
32766 break;
32768 while (1);
32770 if (!parens.require_close (parser))
32771 goto cleanup_error;
32774 check_no_duplicate_clause (list, kind, str, loc);
32776 c = build_omp_clause (loc, kind);
32778 if (ops[1])
32779 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32781 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32782 OMP_CLAUSE_CHAIN (c) = list;
32784 return c;
32786 cleanup_error:
32787 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32788 return list;
32791 /* OpenACC 2.0:
32792 tile ( size-expr-list ) */
32794 static tree
32795 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32797 tree c, expr = error_mark_node;
32798 tree tile = NULL_TREE;
32800 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32801 so, but the spec authors never considered such a case and have
32802 differing opinions on what it might mean, including 'not
32803 allowed'.) */
32804 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32805 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32806 clause_loc);
32808 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32809 return list;
32813 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32814 return list;
32816 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32817 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32818 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32820 cp_lexer_consume_token (parser->lexer);
32821 expr = integer_zero_node;
32823 else
32824 expr = cp_parser_constant_expression (parser);
32826 tile = tree_cons (NULL_TREE, expr, tile);
32828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32830 /* Consume the trailing ')'. */
32831 cp_lexer_consume_token (parser->lexer);
32833 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32834 tile = nreverse (tile);
32835 OMP_CLAUSE_TILE_LIST (c) = tile;
32836 OMP_CLAUSE_CHAIN (c) = list;
32837 return c;
32840 /* OpenACC 2.0
32841 Parse wait clause or directive parameters. */
32843 static tree
32844 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32846 vec<tree, va_gc> *args;
32847 tree t, args_tree;
32849 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32850 /*cast_p=*/false,
32851 /*allow_expansion_p=*/true,
32852 /*non_constant_p=*/NULL);
32854 if (args == NULL || args->length () == 0)
32856 if (args != NULL)
32858 cp_parser_error (parser, "expected integer expression list");
32859 release_tree_vector (args);
32861 return list;
32864 args_tree = build_tree_list_vec (args);
32866 release_tree_vector (args);
32868 for (t = args_tree; t; t = TREE_CHAIN (t))
32870 tree targ = TREE_VALUE (t);
32872 if (targ != error_mark_node)
32874 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32875 error ("%<wait%> expression must be integral");
32876 else
32878 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32880 targ = mark_rvalue_use (targ);
32881 OMP_CLAUSE_DECL (c) = targ;
32882 OMP_CLAUSE_CHAIN (c) = list;
32883 list = c;
32888 return list;
32891 /* OpenACC:
32892 wait [( int-expr-list )] */
32894 static tree
32895 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32897 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32899 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32900 list = cp_parser_oacc_wait_list (parser, location, list);
32901 else
32903 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
32905 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32906 OMP_CLAUSE_CHAIN (c) = list;
32907 list = c;
32910 return list;
32913 /* OpenMP 3.0:
32914 collapse ( constant-expression ) */
32916 static tree
32917 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32919 tree c, num;
32920 location_t loc;
32921 HOST_WIDE_INT n;
32923 loc = cp_lexer_peek_token (parser->lexer)->location;
32924 matching_parens parens;
32925 if (!parens.require_open (parser))
32926 return list;
32928 num = cp_parser_constant_expression (parser);
32930 if (!parens.require_close (parser))
32931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32932 /*or_comma=*/false,
32933 /*consume_paren=*/true);
32935 if (num == error_mark_node)
32936 return list;
32937 num = fold_non_dependent_expr (num);
32938 if (!tree_fits_shwi_p (num)
32939 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32940 || (n = tree_to_shwi (num)) <= 0
32941 || (int) n != n)
32943 error_at (loc, "collapse argument needs positive constant integer expression");
32944 return list;
32947 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32948 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32949 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32950 OMP_CLAUSE_CHAIN (c) = list;
32951 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32953 return c;
32956 /* OpenMP 2.5:
32957 default ( none | shared )
32959 OpenACC:
32960 default ( none | present ) */
32962 static tree
32963 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32964 location_t location, bool is_oacc)
32966 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32967 tree c;
32969 matching_parens parens;
32970 if (!parens.require_open (parser))
32971 return list;
32972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32974 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32975 const char *p = IDENTIFIER_POINTER (id);
32977 switch (p[0])
32979 case 'n':
32980 if (strcmp ("none", p) != 0)
32981 goto invalid_kind;
32982 kind = OMP_CLAUSE_DEFAULT_NONE;
32983 break;
32985 case 'p':
32986 if (strcmp ("present", p) != 0 || !is_oacc)
32987 goto invalid_kind;
32988 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32989 break;
32991 case 's':
32992 if (strcmp ("shared", p) != 0 || is_oacc)
32993 goto invalid_kind;
32994 kind = OMP_CLAUSE_DEFAULT_SHARED;
32995 break;
32997 default:
32998 goto invalid_kind;
33001 cp_lexer_consume_token (parser->lexer);
33003 else
33005 invalid_kind:
33006 if (is_oacc)
33007 cp_parser_error (parser, "expected %<none%> or %<present%>");
33008 else
33009 cp_parser_error (parser, "expected %<none%> or %<shared%>");
33012 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
33013 || !parens.require_close (parser))
33014 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33015 /*or_comma=*/false,
33016 /*consume_paren=*/true);
33018 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33019 return list;
33021 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33022 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33023 OMP_CLAUSE_CHAIN (c) = list;
33024 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33026 return c;
33029 /* OpenMP 3.1:
33030 final ( expression ) */
33032 static tree
33033 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33035 tree t, c;
33037 matching_parens parens;
33038 if (!parens.require_open (parser))
33039 return list;
33041 t = cp_parser_assignment_expression (parser);
33043 if (t == error_mark_node
33044 || !parens.require_close (parser))
33045 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33046 /*or_comma=*/false,
33047 /*consume_paren=*/true);
33049 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33051 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33052 OMP_CLAUSE_FINAL_EXPR (c) = t;
33053 OMP_CLAUSE_CHAIN (c) = list;
33055 return c;
33058 /* OpenMP 2.5:
33059 if ( expression )
33061 OpenMP 4.5:
33062 if ( directive-name-modifier : expression )
33064 directive-name-modifier:
33065 parallel | task | taskloop | target data | target | target update
33066 | target enter data | target exit data
33068 OpenMP 5.0:
33069 directive-name-modifier:
33070 ... | simd | cancel */
33072 static tree
33073 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33074 bool is_omp)
33076 tree t, c;
33077 enum tree_code if_modifier = ERROR_MARK;
33079 matching_parens parens;
33080 if (!parens.require_open (parser))
33081 return list;
33083 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33085 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33086 const char *p = IDENTIFIER_POINTER (id);
33087 int n = 2;
33089 if (strcmp ("cancel", p) == 0)
33090 if_modifier = VOID_CST;
33091 else if (strcmp ("parallel", p) == 0)
33092 if_modifier = OMP_PARALLEL;
33093 else if (strcmp ("simd", p) == 0)
33094 if_modifier = OMP_SIMD;
33095 else if (strcmp ("task", p) == 0)
33096 if_modifier = OMP_TASK;
33097 else if (strcmp ("taskloop", p) == 0)
33098 if_modifier = OMP_TASKLOOP;
33099 else if (strcmp ("target", p) == 0)
33101 if_modifier = OMP_TARGET;
33102 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33104 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33105 p = IDENTIFIER_POINTER (id);
33106 if (strcmp ("data", p) == 0)
33107 if_modifier = OMP_TARGET_DATA;
33108 else if (strcmp ("update", p) == 0)
33109 if_modifier = OMP_TARGET_UPDATE;
33110 else if (strcmp ("enter", p) == 0)
33111 if_modifier = OMP_TARGET_ENTER_DATA;
33112 else if (strcmp ("exit", p) == 0)
33113 if_modifier = OMP_TARGET_EXIT_DATA;
33114 if (if_modifier != OMP_TARGET)
33115 n = 3;
33116 else
33118 location_t loc
33119 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33120 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33121 "or %<exit%>");
33122 if_modifier = ERROR_MARK;
33124 if (if_modifier == OMP_TARGET_ENTER_DATA
33125 || if_modifier == OMP_TARGET_EXIT_DATA)
33127 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33129 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33130 p = IDENTIFIER_POINTER (id);
33131 if (strcmp ("data", p) == 0)
33132 n = 4;
33134 if (n != 4)
33136 location_t loc
33137 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33138 error_at (loc, "expected %<data%>");
33139 if_modifier = ERROR_MARK;
33144 if (if_modifier != ERROR_MARK)
33146 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33148 while (n-- > 0)
33149 cp_lexer_consume_token (parser->lexer);
33151 else
33153 if (n > 2)
33155 location_t loc
33156 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33157 error_at (loc, "expected %<:%>");
33159 if_modifier = ERROR_MARK;
33164 t = cp_parser_assignment_expression (parser);
33166 if (t == error_mark_node
33167 || !parens.require_close (parser))
33168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33169 /*or_comma=*/false,
33170 /*consume_paren=*/true);
33172 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33173 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33175 if (if_modifier != ERROR_MARK
33176 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33178 const char *p = NULL;
33179 switch (if_modifier)
33181 case VOID_CST: p = "cancel"; break;
33182 case OMP_PARALLEL: p = "parallel"; break;
33183 case OMP_SIMD: p = "simd"; break;
33184 case OMP_TASK: p = "task"; break;
33185 case OMP_TASKLOOP: p = "taskloop"; break;
33186 case OMP_TARGET_DATA: p = "target data"; break;
33187 case OMP_TARGET: p = "target"; break;
33188 case OMP_TARGET_UPDATE: p = "target update"; break;
33189 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33190 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33191 default: gcc_unreachable ();
33193 error_at (location, "too many %<if%> clauses with %qs modifier",
33195 return list;
33197 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33199 if (!is_omp)
33200 error_at (location, "too many %<if%> clauses");
33201 else
33202 error_at (location, "too many %<if%> clauses without modifier");
33203 return list;
33205 else if (if_modifier == ERROR_MARK
33206 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33208 error_at (location, "if any %<if%> clause has modifier, then all "
33209 "%<if%> clauses have to use modifier");
33210 return list;
33214 c = build_omp_clause (location, OMP_CLAUSE_IF);
33215 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33216 OMP_CLAUSE_IF_EXPR (c) = t;
33217 OMP_CLAUSE_CHAIN (c) = list;
33219 return c;
33222 /* OpenMP 3.1:
33223 mergeable */
33225 static tree
33226 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33227 tree list, location_t location)
33229 tree c;
33231 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33232 location);
33234 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33235 OMP_CLAUSE_CHAIN (c) = list;
33236 return c;
33239 /* OpenMP 2.5:
33240 nowait */
33242 static tree
33243 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33244 tree list, location_t location)
33246 tree c;
33248 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33250 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33251 OMP_CLAUSE_CHAIN (c) = list;
33252 return c;
33255 /* OpenMP 2.5:
33256 num_threads ( expression ) */
33258 static tree
33259 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33260 location_t location)
33262 tree t, c;
33264 matching_parens parens;
33265 if (!parens.require_open (parser))
33266 return list;
33268 t = cp_parser_assignment_expression (parser);
33270 if (t == error_mark_node
33271 || !parens.require_close (parser))
33272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33273 /*or_comma=*/false,
33274 /*consume_paren=*/true);
33276 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33277 "num_threads", location);
33279 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33280 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33281 OMP_CLAUSE_CHAIN (c) = list;
33283 return c;
33286 /* OpenMP 4.5:
33287 num_tasks ( expression ) */
33289 static tree
33290 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33291 location_t location)
33293 tree t, c;
33295 matching_parens parens;
33296 if (!parens.require_open (parser))
33297 return list;
33299 t = cp_parser_assignment_expression (parser);
33301 if (t == error_mark_node
33302 || !parens.require_close (parser))
33303 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33304 /*or_comma=*/false,
33305 /*consume_paren=*/true);
33307 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33308 "num_tasks", location);
33310 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33311 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33312 OMP_CLAUSE_CHAIN (c) = list;
33314 return c;
33317 /* OpenMP 4.5:
33318 grainsize ( expression ) */
33320 static tree
33321 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33322 location_t location)
33324 tree t, c;
33326 matching_parens parens;
33327 if (!parens.require_open (parser))
33328 return list;
33330 t = cp_parser_assignment_expression (parser);
33332 if (t == error_mark_node
33333 || !parens.require_close (parser))
33334 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33335 /*or_comma=*/false,
33336 /*consume_paren=*/true);
33338 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33339 "grainsize", location);
33341 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33342 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33343 OMP_CLAUSE_CHAIN (c) = list;
33345 return c;
33348 /* OpenMP 4.5:
33349 priority ( expression ) */
33351 static tree
33352 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33353 location_t location)
33355 tree t, c;
33357 matching_parens parens;
33358 if (!parens.require_open (parser))
33359 return list;
33361 t = cp_parser_assignment_expression (parser);
33363 if (t == error_mark_node
33364 || !parens.require_close (parser))
33365 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33366 /*or_comma=*/false,
33367 /*consume_paren=*/true);
33369 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33370 "priority", location);
33372 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33373 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33374 OMP_CLAUSE_CHAIN (c) = list;
33376 return c;
33379 /* OpenMP 4.5:
33380 hint ( expression ) */
33382 static tree
33383 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33385 tree t, c;
33387 matching_parens parens;
33388 if (!parens.require_open (parser))
33389 return list;
33391 t = cp_parser_assignment_expression (parser);
33393 if (t == error_mark_node
33394 || !parens.require_close (parser))
33395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33396 /*or_comma=*/false,
33397 /*consume_paren=*/true);
33399 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33401 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33402 OMP_CLAUSE_HINT_EXPR (c) = t;
33403 OMP_CLAUSE_CHAIN (c) = list;
33405 return c;
33408 /* OpenMP 4.5:
33409 defaultmap ( tofrom : scalar )
33411 OpenMP 5.0:
33412 defaultmap ( implicit-behavior [ : variable-category ] ) */
33414 static tree
33415 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33416 location_t location)
33418 tree c, id;
33419 const char *p;
33420 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33421 enum omp_clause_defaultmap_kind category
33422 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33424 matching_parens parens;
33425 if (!parens.require_open (parser))
33426 return list;
33428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33429 p = "default";
33430 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33432 invalid_behavior:
33433 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33434 "%<tofrom%>, %<firstprivate%>, %<none%> "
33435 "or %<default%>");
33436 goto out_err;
33438 else
33440 id = cp_lexer_peek_token (parser->lexer)->u.value;
33441 p = IDENTIFIER_POINTER (id);
33444 switch (p[0])
33446 case 'a':
33447 if (strcmp ("alloc", p) == 0)
33448 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33449 else
33450 goto invalid_behavior;
33451 break;
33453 case 'd':
33454 if (strcmp ("default", p) == 0)
33455 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33456 else
33457 goto invalid_behavior;
33458 break;
33460 case 'f':
33461 if (strcmp ("firstprivate", p) == 0)
33462 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33463 else if (strcmp ("from", p) == 0)
33464 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33465 else
33466 goto invalid_behavior;
33467 break;
33469 case 'n':
33470 if (strcmp ("none", p) == 0)
33471 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33472 else
33473 goto invalid_behavior;
33474 break;
33476 case 't':
33477 if (strcmp ("tofrom", p) == 0)
33478 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33479 else if (strcmp ("to", p) == 0)
33480 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33481 else
33482 goto invalid_behavior;
33483 break;
33485 default:
33486 goto invalid_behavior;
33488 cp_lexer_consume_token (parser->lexer);
33490 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33492 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33493 goto out_err;
33495 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33497 invalid_category:
33498 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33499 "%<pointer%>");
33500 goto out_err;
33502 id = cp_lexer_peek_token (parser->lexer)->u.value;
33503 p = IDENTIFIER_POINTER (id);
33505 switch (p[0])
33507 case 'a':
33508 if (strcmp ("aggregate", p) == 0)
33509 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33510 else
33511 goto invalid_category;
33512 break;
33514 case 'p':
33515 if (strcmp ("pointer", p) == 0)
33516 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33517 else
33518 goto invalid_category;
33519 break;
33521 case 's':
33522 if (strcmp ("scalar", p) == 0)
33523 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33524 else
33525 goto invalid_category;
33526 break;
33528 default:
33529 goto invalid_category;
33532 cp_lexer_consume_token (parser->lexer);
33534 if (!parens.require_close (parser))
33535 goto out_err;
33537 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33538 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33539 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33540 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33541 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33542 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33544 enum omp_clause_defaultmap_kind cat = category;
33545 location_t loc = OMP_CLAUSE_LOCATION (c);
33546 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33547 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33548 p = NULL;
33549 switch (cat)
33551 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33552 p = NULL;
33553 break;
33554 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33555 p = "aggregate";
33556 break;
33557 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33558 p = "pointer";
33559 break;
33560 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33561 p = "scalar";
33562 break;
33563 default:
33564 gcc_unreachable ();
33566 if (p)
33567 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33569 else
33570 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33571 "category");
33572 break;
33575 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33576 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33577 OMP_CLAUSE_CHAIN (c) = list;
33578 return c;
33580 out_err:
33581 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33582 /*or_comma=*/false,
33583 /*consume_paren=*/true);
33584 return list;
33587 /* OpenMP 2.5:
33588 ordered
33590 OpenMP 4.5:
33591 ordered ( constant-expression ) */
33593 static tree
33594 cp_parser_omp_clause_ordered (cp_parser *parser,
33595 tree list, location_t location)
33597 tree c, num = NULL_TREE;
33598 HOST_WIDE_INT n;
33600 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33601 "ordered", location);
33603 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33605 matching_parens parens;
33606 parens.consume_open (parser);
33608 num = cp_parser_constant_expression (parser);
33610 if (!parens.require_close (parser))
33611 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33612 /*or_comma=*/false,
33613 /*consume_paren=*/true);
33615 if (num == error_mark_node)
33616 return list;
33617 num = fold_non_dependent_expr (num);
33618 if (!tree_fits_shwi_p (num)
33619 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33620 || (n = tree_to_shwi (num)) <= 0
33621 || (int) n != n)
33623 error_at (location,
33624 "ordered argument needs positive constant integer "
33625 "expression");
33626 return list;
33630 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33631 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33632 OMP_CLAUSE_CHAIN (c) = list;
33633 return c;
33636 /* OpenMP 2.5:
33637 reduction ( reduction-operator : variable-list )
33639 reduction-operator:
33640 One of: + * - & ^ | && ||
33642 OpenMP 3.1:
33644 reduction-operator:
33645 One of: + * - & ^ | && || min max
33647 OpenMP 4.0:
33649 reduction-operator:
33650 One of: + * - & ^ | && ||
33651 id-expression
33653 OpenMP 5.0:
33654 reduction ( reduction-modifier, reduction-operator : variable-list )
33655 in_reduction ( reduction-operator : variable-list )
33656 task_reduction ( reduction-operator : variable-list ) */
33658 static tree
33659 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33660 bool is_omp, tree list)
33662 enum tree_code code = ERROR_MARK;
33663 tree nlist, c, id = NULL_TREE;
33664 bool task = false;
33665 bool inscan = false;
33667 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33668 return list;
33670 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33673 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33675 cp_lexer_consume_token (parser->lexer);
33676 cp_lexer_consume_token (parser->lexer);
33678 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33679 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33681 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33682 const char *p = IDENTIFIER_POINTER (id);
33683 if (strcmp (p, "task") == 0)
33684 task = true;
33685 else if (strcmp (p, "inscan") == 0)
33687 inscan = true;
33688 sorry ("%<inscan%> modifier on %<reduction%> clause "
33689 "not supported yet");
33691 if (task || inscan)
33693 cp_lexer_consume_token (parser->lexer);
33694 cp_lexer_consume_token (parser->lexer);
33699 switch (cp_lexer_peek_token (parser->lexer)->type)
33701 case CPP_PLUS: code = PLUS_EXPR; break;
33702 case CPP_MULT: code = MULT_EXPR; break;
33703 case CPP_MINUS: code = MINUS_EXPR; break;
33704 case CPP_AND: code = BIT_AND_EXPR; break;
33705 case CPP_XOR: code = BIT_XOR_EXPR; break;
33706 case CPP_OR: code = BIT_IOR_EXPR; break;
33707 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33708 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33709 default: break;
33712 if (code != ERROR_MARK)
33713 cp_lexer_consume_token (parser->lexer);
33714 else
33716 bool saved_colon_corrects_to_scope_p;
33717 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33718 parser->colon_corrects_to_scope_p = false;
33719 id = cp_parser_id_expression (parser, /*template_p=*/false,
33720 /*check_dependency_p=*/true,
33721 /*template_p=*/NULL,
33722 /*declarator_p=*/false,
33723 /*optional_p=*/false);
33724 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33725 if (identifier_p (id))
33727 const char *p = IDENTIFIER_POINTER (id);
33729 if (strcmp (p, "min") == 0)
33730 code = MIN_EXPR;
33731 else if (strcmp (p, "max") == 0)
33732 code = MAX_EXPR;
33733 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33734 code = PLUS_EXPR;
33735 else if (id == ovl_op_identifier (false, MULT_EXPR))
33736 code = MULT_EXPR;
33737 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33738 code = MINUS_EXPR;
33739 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33740 code = BIT_AND_EXPR;
33741 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33742 code = BIT_IOR_EXPR;
33743 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33744 code = BIT_XOR_EXPR;
33745 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33746 code = TRUTH_ANDIF_EXPR;
33747 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33748 code = TRUTH_ORIF_EXPR;
33749 id = omp_reduction_id (code, id, NULL_TREE);
33750 tree scope = parser->scope;
33751 if (scope)
33752 id = build_qualified_name (NULL_TREE, scope, id, false);
33753 parser->scope = NULL_TREE;
33754 parser->qualifying_scope = NULL_TREE;
33755 parser->object_scope = NULL_TREE;
33757 else
33759 error ("invalid reduction-identifier");
33760 resync_fail:
33761 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33762 /*or_comma=*/false,
33763 /*consume_paren=*/true);
33764 return list;
33768 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33769 goto resync_fail;
33771 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33772 NULL);
33773 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33775 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33776 if (task)
33777 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33778 else if (inscan)
33779 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33780 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33783 return nlist;
33786 /* OpenMP 2.5:
33787 schedule ( schedule-kind )
33788 schedule ( schedule-kind , expression )
33790 schedule-kind:
33791 static | dynamic | guided | runtime | auto
33793 OpenMP 4.5:
33794 schedule ( schedule-modifier : schedule-kind )
33795 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33797 schedule-modifier:
33798 simd
33799 monotonic
33800 nonmonotonic */
33802 static tree
33803 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33805 tree c, t;
33806 int modifiers = 0, nmodifiers = 0;
33808 matching_parens parens;
33809 if (!parens.require_open (parser))
33810 return list;
33812 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33814 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33816 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33817 const char *p = IDENTIFIER_POINTER (id);
33818 if (strcmp ("simd", p) == 0)
33819 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33820 else if (strcmp ("monotonic", p) == 0)
33821 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33822 else if (strcmp ("nonmonotonic", p) == 0)
33823 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33824 else
33825 break;
33826 cp_lexer_consume_token (parser->lexer);
33827 if (nmodifiers++ == 0
33828 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33829 cp_lexer_consume_token (parser->lexer);
33830 else
33832 cp_parser_require (parser, CPP_COLON, RT_COLON);
33833 break;
33837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33839 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33840 const char *p = IDENTIFIER_POINTER (id);
33842 switch (p[0])
33844 case 'd':
33845 if (strcmp ("dynamic", p) != 0)
33846 goto invalid_kind;
33847 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33848 break;
33850 case 'g':
33851 if (strcmp ("guided", p) != 0)
33852 goto invalid_kind;
33853 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33854 break;
33856 case 'r':
33857 if (strcmp ("runtime", p) != 0)
33858 goto invalid_kind;
33859 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33860 break;
33862 default:
33863 goto invalid_kind;
33866 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33867 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33868 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33869 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33870 else
33871 goto invalid_kind;
33872 cp_lexer_consume_token (parser->lexer);
33874 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33875 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33876 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33877 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33879 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33880 "specified");
33881 modifiers = 0;
33884 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33886 cp_token *token;
33887 cp_lexer_consume_token (parser->lexer);
33889 token = cp_lexer_peek_token (parser->lexer);
33890 t = cp_parser_assignment_expression (parser);
33892 if (t == error_mark_node)
33893 goto resync_fail;
33894 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33895 error_at (token->location, "schedule %<runtime%> does not take "
33896 "a %<chunk_size%> parameter");
33897 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33898 error_at (token->location, "schedule %<auto%> does not take "
33899 "a %<chunk_size%> parameter");
33900 else
33901 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33903 if (!parens.require_close (parser))
33904 goto resync_fail;
33906 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33907 goto resync_fail;
33909 OMP_CLAUSE_SCHEDULE_KIND (c)
33910 = (enum omp_clause_schedule_kind)
33911 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33913 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33914 OMP_CLAUSE_CHAIN (c) = list;
33915 return c;
33917 invalid_kind:
33918 cp_parser_error (parser, "invalid schedule kind");
33919 resync_fail:
33920 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33921 /*or_comma=*/false,
33922 /*consume_paren=*/true);
33923 return list;
33926 /* OpenMP 3.0:
33927 untied */
33929 static tree
33930 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33931 tree list, location_t location)
33933 tree c;
33935 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33937 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33938 OMP_CLAUSE_CHAIN (c) = list;
33939 return c;
33942 /* OpenMP 4.0:
33943 inbranch
33944 notinbranch */
33946 static tree
33947 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33948 tree list, location_t location)
33950 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33951 tree c = build_omp_clause (location, code);
33952 OMP_CLAUSE_CHAIN (c) = list;
33953 return c;
33956 /* OpenMP 4.0:
33957 parallel
33959 sections
33960 taskgroup */
33962 static tree
33963 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33964 enum omp_clause_code code,
33965 tree list, location_t location)
33967 tree c = build_omp_clause (location, code);
33968 OMP_CLAUSE_CHAIN (c) = list;
33969 return c;
33972 /* OpenMP 4.5:
33973 nogroup */
33975 static tree
33976 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33977 tree list, location_t location)
33979 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33980 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33981 OMP_CLAUSE_CHAIN (c) = list;
33982 return c;
33985 /* OpenMP 4.5:
33986 simd
33987 threads */
33989 static tree
33990 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33991 enum omp_clause_code code,
33992 tree list, location_t location)
33994 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33995 tree c = build_omp_clause (location, code);
33996 OMP_CLAUSE_CHAIN (c) = list;
33997 return c;
34000 /* OpenMP 4.0:
34001 num_teams ( expression ) */
34003 static tree
34004 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
34005 location_t location)
34007 tree t, c;
34009 matching_parens parens;
34010 if (!parens.require_open (parser))
34011 return list;
34013 t = cp_parser_assignment_expression (parser);
34015 if (t == error_mark_node
34016 || !parens.require_close (parser))
34017 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34018 /*or_comma=*/false,
34019 /*consume_paren=*/true);
34021 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34022 "num_teams", location);
34024 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34025 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34026 OMP_CLAUSE_CHAIN (c) = list;
34028 return c;
34031 /* OpenMP 4.0:
34032 thread_limit ( expression ) */
34034 static tree
34035 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34036 location_t location)
34038 tree t, c;
34040 matching_parens parens;
34041 if (!parens.require_open (parser))
34042 return list;
34044 t = cp_parser_assignment_expression (parser);
34046 if (t == error_mark_node
34047 || !parens.require_close (parser))
34048 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34049 /*or_comma=*/false,
34050 /*consume_paren=*/true);
34052 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34053 "thread_limit", location);
34055 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34056 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34057 OMP_CLAUSE_CHAIN (c) = list;
34059 return c;
34062 /* OpenMP 4.0:
34063 aligned ( variable-list )
34064 aligned ( variable-list : constant-expression ) */
34066 static tree
34067 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34069 tree nlist, c, alignment = NULL_TREE;
34070 bool colon;
34072 matching_parens parens;
34073 if (!parens.require_open (parser))
34074 return list;
34076 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34077 &colon);
34079 if (colon)
34081 alignment = cp_parser_constant_expression (parser);
34083 if (!parens.require_close (parser))
34084 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34085 /*or_comma=*/false,
34086 /*consume_paren=*/true);
34088 if (alignment == error_mark_node)
34089 alignment = NULL_TREE;
34092 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34093 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34095 return nlist;
34098 /* OpenMP 2.5:
34099 lastprivate ( variable-list )
34101 OpenMP 5.0:
34102 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34104 static tree
34105 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34107 bool conditional = false;
34109 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34110 return list;
34112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34113 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34116 const char *p = IDENTIFIER_POINTER (id);
34118 if (strcmp ("conditional", p) == 0)
34120 conditional = true;
34121 cp_lexer_consume_token (parser->lexer);
34122 cp_lexer_consume_token (parser->lexer);
34126 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34127 list, NULL);
34129 if (conditional)
34130 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34131 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34132 return nlist;
34135 /* OpenMP 4.0:
34136 linear ( variable-list )
34137 linear ( variable-list : expression )
34139 OpenMP 4.5:
34140 linear ( modifier ( variable-list ) )
34141 linear ( modifier ( variable-list ) : expression ) */
34143 static tree
34144 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34145 bool declare_simd)
34147 tree nlist, c, step = integer_one_node;
34148 bool colon;
34149 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34151 matching_parens parens;
34152 if (!parens.require_open (parser))
34153 return list;
34155 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34157 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34158 const char *p = IDENTIFIER_POINTER (id);
34160 if (strcmp ("ref", p) == 0)
34161 kind = OMP_CLAUSE_LINEAR_REF;
34162 else if (strcmp ("val", p) == 0)
34163 kind = OMP_CLAUSE_LINEAR_VAL;
34164 else if (strcmp ("uval", p) == 0)
34165 kind = OMP_CLAUSE_LINEAR_UVAL;
34166 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34167 cp_lexer_consume_token (parser->lexer);
34168 else
34169 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34172 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34173 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34174 &colon);
34175 else
34177 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34178 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34179 if (colon)
34180 cp_parser_require (parser, CPP_COLON, RT_COLON);
34181 else if (!parens.require_close (parser))
34182 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34183 /*or_comma=*/false,
34184 /*consume_paren=*/true);
34187 if (colon)
34189 step = NULL_TREE;
34190 if (declare_simd
34191 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34192 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34194 cp_token *token = cp_lexer_peek_token (parser->lexer);
34195 cp_parser_parse_tentatively (parser);
34196 step = cp_parser_id_expression (parser, /*template_p=*/false,
34197 /*check_dependency_p=*/true,
34198 /*template_p=*/NULL,
34199 /*declarator_p=*/false,
34200 /*optional_p=*/false);
34201 if (step != error_mark_node)
34202 step = cp_parser_lookup_name_simple (parser, step, token->location);
34203 if (step == error_mark_node)
34205 step = NULL_TREE;
34206 cp_parser_abort_tentative_parse (parser);
34208 else if (!cp_parser_parse_definitely (parser))
34209 step = NULL_TREE;
34211 if (!step)
34212 step = cp_parser_assignment_expression (parser);
34214 if (!parens.require_close (parser))
34215 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34216 /*or_comma=*/false,
34217 /*consume_paren=*/true);
34219 if (step == error_mark_node)
34220 return list;
34223 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34225 OMP_CLAUSE_LINEAR_STEP (c) = step;
34226 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34229 return nlist;
34232 /* OpenMP 4.0:
34233 safelen ( constant-expression ) */
34235 static tree
34236 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34237 location_t location)
34239 tree t, c;
34241 matching_parens parens;
34242 if (!parens.require_open (parser))
34243 return list;
34245 t = cp_parser_constant_expression (parser);
34247 if (t == error_mark_node
34248 || !parens.require_close (parser))
34249 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34250 /*or_comma=*/false,
34251 /*consume_paren=*/true);
34253 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34255 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34256 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34257 OMP_CLAUSE_CHAIN (c) = list;
34259 return c;
34262 /* OpenMP 4.0:
34263 simdlen ( constant-expression ) */
34265 static tree
34266 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34267 location_t location)
34269 tree t, c;
34271 matching_parens parens;
34272 if (!parens.require_open (parser))
34273 return list;
34275 t = cp_parser_constant_expression (parser);
34277 if (t == error_mark_node
34278 || !parens.require_close (parser))
34279 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34280 /*or_comma=*/false,
34281 /*consume_paren=*/true);
34283 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34285 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34286 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34287 OMP_CLAUSE_CHAIN (c) = list;
34289 return c;
34292 /* OpenMP 4.5:
34293 vec:
34294 identifier [+/- integer]
34295 vec , identifier [+/- integer]
34298 static tree
34299 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34300 tree list)
34302 tree vec = NULL;
34304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34306 cp_parser_error (parser, "expected identifier");
34307 return list;
34310 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34312 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34313 tree t, identifier = cp_parser_identifier (parser);
34314 tree addend = NULL;
34316 if (identifier == error_mark_node)
34317 t = error_mark_node;
34318 else
34320 t = cp_parser_lookup_name_simple
34321 (parser, identifier,
34322 cp_lexer_peek_token (parser->lexer)->location);
34323 if (t == error_mark_node)
34324 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34325 id_loc);
34328 bool neg = false;
34329 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34330 neg = true;
34331 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34333 addend = integer_zero_node;
34334 goto add_to_vector;
34336 cp_lexer_consume_token (parser->lexer);
34338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34340 cp_parser_error (parser, "expected integer");
34341 return list;
34344 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34345 if (TREE_CODE (addend) != INTEGER_CST)
34347 cp_parser_error (parser, "expected integer");
34348 return list;
34350 cp_lexer_consume_token (parser->lexer);
34352 add_to_vector:
34353 if (t != error_mark_node)
34355 vec = tree_cons (addend, t, vec);
34356 if (neg)
34357 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34361 break;
34363 cp_lexer_consume_token (parser->lexer);
34366 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34368 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34369 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34370 OMP_CLAUSE_DECL (u) = nreverse (vec);
34371 OMP_CLAUSE_CHAIN (u) = list;
34372 return u;
34374 return list;
34377 /* OpenMP 5.0:
34378 iterators ( iterators-definition )
34380 iterators-definition:
34381 iterator-specifier
34382 iterator-specifier , iterators-definition
34384 iterator-specifier:
34385 identifier = range-specification
34386 iterator-type identifier = range-specification
34388 range-specification:
34389 begin : end
34390 begin : end : step */
34392 static tree
34393 cp_parser_omp_iterators (cp_parser *parser)
34395 tree ret = NULL_TREE, *last = &ret;
34396 cp_lexer_consume_token (parser->lexer);
34398 matching_parens parens;
34399 if (!parens.require_open (parser))
34400 return error_mark_node;
34402 bool saved_colon_corrects_to_scope_p
34403 = parser->colon_corrects_to_scope_p;
34404 bool saved_colon_doesnt_start_class_def_p
34405 = parser->colon_doesnt_start_class_def_p;
34409 tree iter_type;
34410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34411 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34412 iter_type = integer_type_node;
34413 else
34415 const char *saved_message
34416 = parser->type_definition_forbidden_message;
34417 parser->type_definition_forbidden_message
34418 = G_("types may not be defined in iterator type");
34420 iter_type = cp_parser_type_id (parser);
34422 parser->type_definition_forbidden_message = saved_message;
34425 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34428 cp_parser_error (parser, "expected identifier");
34429 break;
34432 tree id = cp_parser_identifier (parser);
34433 if (id == error_mark_node)
34434 break;
34436 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34437 break;
34439 parser->colon_corrects_to_scope_p = false;
34440 parser->colon_doesnt_start_class_def_p = true;
34441 tree begin = cp_parser_assignment_expression (parser);
34443 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34444 break;
34446 tree end = cp_parser_assignment_expression (parser);
34448 tree step = integer_one_node;
34449 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34451 cp_lexer_consume_token (parser->lexer);
34452 step = cp_parser_assignment_expression (parser);
34455 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34456 DECL_ARTIFICIAL (iter_var) = 1;
34457 DECL_CONTEXT (iter_var) = current_function_decl;
34458 pushdecl (iter_var);
34460 *last = make_tree_vec (6);
34461 TREE_VEC_ELT (*last, 0) = iter_var;
34462 TREE_VEC_ELT (*last, 1) = begin;
34463 TREE_VEC_ELT (*last, 2) = end;
34464 TREE_VEC_ELT (*last, 3) = step;
34465 last = &TREE_CHAIN (*last);
34467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34469 cp_lexer_consume_token (parser->lexer);
34470 continue;
34472 break;
34474 while (1);
34476 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34477 parser->colon_doesnt_start_class_def_p
34478 = saved_colon_doesnt_start_class_def_p;
34480 if (!parens.require_close (parser))
34481 cp_parser_skip_to_closing_parenthesis (parser,
34482 /*recovering=*/true,
34483 /*or_comma=*/false,
34484 /*consume_paren=*/true);
34486 return ret ? ret : error_mark_node;
34489 /* OpenMP 4.0:
34490 depend ( depend-kind : variable-list )
34492 depend-kind:
34493 in | out | inout
34495 OpenMP 4.5:
34496 depend ( source )
34498 depend ( sink : vec )
34500 OpenMP 5.0:
34501 depend ( depend-modifier , depend-kind: variable-list )
34503 depend-kind:
34504 in | out | inout | mutexinoutset | depobj
34506 depend-modifier:
34507 iterator ( iterators-definition ) */
34509 static tree
34510 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34512 tree nlist, c, iterators = NULL_TREE;
34513 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34515 matching_parens parens;
34516 if (!parens.require_open (parser))
34517 return list;
34521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34522 goto invalid_kind;
34524 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34525 const char *p = IDENTIFIER_POINTER (id);
34527 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34529 begin_scope (sk_omp, NULL);
34530 iterators = cp_parser_omp_iterators (parser);
34531 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34532 continue;
34534 if (strcmp ("in", p) == 0)
34535 kind = OMP_CLAUSE_DEPEND_IN;
34536 else if (strcmp ("inout", p) == 0)
34537 kind = OMP_CLAUSE_DEPEND_INOUT;
34538 else if (strcmp ("mutexinoutset", p) == 0)
34539 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34540 else if (strcmp ("out", p) == 0)
34541 kind = OMP_CLAUSE_DEPEND_OUT;
34542 else if (strcmp ("depobj", p) == 0)
34543 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34544 else if (strcmp ("sink", p) == 0)
34545 kind = OMP_CLAUSE_DEPEND_SINK;
34546 else if (strcmp ("source", p) == 0)
34547 kind = OMP_CLAUSE_DEPEND_SOURCE;
34548 else
34549 goto invalid_kind;
34550 break;
34552 while (1);
34554 cp_lexer_consume_token (parser->lexer);
34556 if (iterators
34557 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34559 poplevel (0, 1, 0);
34560 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34561 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34562 iterators = NULL_TREE;
34565 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34567 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34568 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34569 OMP_CLAUSE_DECL (c) = NULL_TREE;
34570 OMP_CLAUSE_CHAIN (c) = list;
34571 if (!parens.require_close (parser))
34572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34573 /*or_comma=*/false,
34574 /*consume_paren=*/true);
34575 return c;
34578 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34579 goto resync_fail;
34581 if (kind == OMP_CLAUSE_DEPEND_SINK)
34582 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34583 else
34585 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34586 list, NULL);
34588 if (iterators)
34590 tree block = poplevel (1, 1, 0);
34591 if (iterators == error_mark_node)
34592 iterators = NULL_TREE;
34593 else
34594 TREE_VEC_ELT (iterators, 5) = block;
34597 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34599 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34600 if (iterators)
34601 OMP_CLAUSE_DECL (c)
34602 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34605 return nlist;
34607 invalid_kind:
34608 cp_parser_error (parser, "invalid depend kind");
34609 resync_fail:
34610 if (iterators)
34611 poplevel (0, 1, 0);
34612 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34613 /*or_comma=*/false,
34614 /*consume_paren=*/true);
34615 return list;
34618 /* OpenMP 4.0:
34619 map ( map-kind : variable-list )
34620 map ( variable-list )
34622 map-kind:
34623 alloc | to | from | tofrom
34625 OpenMP 4.5:
34626 map-kind:
34627 alloc | to | from | tofrom | release | delete
34629 map ( always [,] map-kind: variable-list ) */
34631 static tree
34632 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34634 tree nlist, c;
34635 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34636 bool always = false;
34638 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34639 return list;
34641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34644 const char *p = IDENTIFIER_POINTER (id);
34646 if (strcmp ("always", p) == 0)
34648 int nth = 2;
34649 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34650 nth++;
34651 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34652 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34653 == RID_DELETE))
34654 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34655 == CPP_COLON))
34657 always = true;
34658 cp_lexer_consume_token (parser->lexer);
34659 if (nth == 3)
34660 cp_lexer_consume_token (parser->lexer);
34665 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34666 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34668 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34669 const char *p = IDENTIFIER_POINTER (id);
34671 if (strcmp ("alloc", p) == 0)
34672 kind = GOMP_MAP_ALLOC;
34673 else if (strcmp ("to", p) == 0)
34674 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34675 else if (strcmp ("from", p) == 0)
34676 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34677 else if (strcmp ("tofrom", p) == 0)
34678 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34679 else if (strcmp ("release", p) == 0)
34680 kind = GOMP_MAP_RELEASE;
34681 else
34683 cp_parser_error (parser, "invalid map kind");
34684 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34685 /*or_comma=*/false,
34686 /*consume_paren=*/true);
34687 return list;
34689 cp_lexer_consume_token (parser->lexer);
34690 cp_lexer_consume_token (parser->lexer);
34692 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34693 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34695 kind = GOMP_MAP_DELETE;
34696 cp_lexer_consume_token (parser->lexer);
34697 cp_lexer_consume_token (parser->lexer);
34700 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34701 NULL);
34703 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34704 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34706 return nlist;
34709 /* OpenMP 4.0:
34710 device ( expression ) */
34712 static tree
34713 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34714 location_t location)
34716 tree t, c;
34718 matching_parens parens;
34719 if (!parens.require_open (parser))
34720 return list;
34722 t = cp_parser_assignment_expression (parser);
34724 if (t == error_mark_node
34725 || !parens.require_close (parser))
34726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34727 /*or_comma=*/false,
34728 /*consume_paren=*/true);
34730 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34731 "device", location);
34733 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34734 OMP_CLAUSE_DEVICE_ID (c) = t;
34735 OMP_CLAUSE_CHAIN (c) = list;
34737 return c;
34740 /* OpenMP 4.0:
34741 dist_schedule ( static )
34742 dist_schedule ( static , expression ) */
34744 static tree
34745 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34746 location_t location)
34748 tree c, t;
34750 matching_parens parens;
34751 if (!parens.require_open (parser))
34752 return list;
34754 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34756 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34757 goto invalid_kind;
34758 cp_lexer_consume_token (parser->lexer);
34760 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34762 cp_lexer_consume_token (parser->lexer);
34764 t = cp_parser_assignment_expression (parser);
34766 if (t == error_mark_node)
34767 goto resync_fail;
34768 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34770 if (!parens.require_close (parser))
34771 goto resync_fail;
34773 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34774 goto resync_fail;
34776 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34777 location);
34778 OMP_CLAUSE_CHAIN (c) = list;
34779 return c;
34781 invalid_kind:
34782 cp_parser_error (parser, "invalid dist_schedule kind");
34783 resync_fail:
34784 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34785 /*or_comma=*/false,
34786 /*consume_paren=*/true);
34787 return list;
34790 /* OpenMP 4.0:
34791 proc_bind ( proc-bind-kind )
34793 proc-bind-kind:
34794 master | close | spread */
34796 static tree
34797 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34798 location_t location)
34800 tree c;
34801 enum omp_clause_proc_bind_kind kind;
34803 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34804 return list;
34806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34808 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34809 const char *p = IDENTIFIER_POINTER (id);
34811 if (strcmp ("master", p) == 0)
34812 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34813 else if (strcmp ("close", p) == 0)
34814 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34815 else if (strcmp ("spread", p) == 0)
34816 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34817 else
34818 goto invalid_kind;
34820 else
34821 goto invalid_kind;
34823 cp_lexer_consume_token (parser->lexer);
34824 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34825 goto resync_fail;
34827 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34828 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34829 location);
34830 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34831 OMP_CLAUSE_CHAIN (c) = list;
34832 return c;
34834 invalid_kind:
34835 cp_parser_error (parser, "invalid depend kind");
34836 resync_fail:
34837 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34838 /*or_comma=*/false,
34839 /*consume_paren=*/true);
34840 return list;
34843 /* OpenACC:
34844 async [( int-expr )] */
34846 static tree
34847 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34849 tree c, t;
34850 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34852 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34854 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34856 matching_parens parens;
34857 parens.consume_open (parser);
34859 t = cp_parser_expression (parser);
34860 if (t == error_mark_node
34861 || !parens.require_close (parser))
34862 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34863 /*or_comma=*/false,
34864 /*consume_paren=*/true);
34867 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34869 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34870 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34871 OMP_CLAUSE_CHAIN (c) = list;
34872 list = c;
34874 return list;
34877 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34878 is a bitmask in MASK. Return the list of clauses found. */
34880 static tree
34881 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34882 const char *where, cp_token *pragma_tok,
34883 bool finish_p = true)
34885 tree clauses = NULL;
34886 bool first = true;
34888 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34890 location_t here;
34891 pragma_omp_clause c_kind;
34892 omp_clause_code code;
34893 const char *c_name;
34894 tree prev = clauses;
34896 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34897 cp_lexer_consume_token (parser->lexer);
34899 here = cp_lexer_peek_token (parser->lexer)->location;
34900 c_kind = cp_parser_omp_clause_name (parser);
34902 switch (c_kind)
34904 case PRAGMA_OACC_CLAUSE_ASYNC:
34905 clauses = cp_parser_oacc_clause_async (parser, clauses);
34906 c_name = "async";
34907 break;
34908 case PRAGMA_OACC_CLAUSE_AUTO:
34909 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
34910 clauses);
34911 c_name = "auto";
34912 break;
34913 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34914 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34915 c_name = "collapse";
34916 break;
34917 case PRAGMA_OACC_CLAUSE_COPY:
34918 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34919 c_name = "copy";
34920 break;
34921 case PRAGMA_OACC_CLAUSE_COPYIN:
34922 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34923 c_name = "copyin";
34924 break;
34925 case PRAGMA_OACC_CLAUSE_COPYOUT:
34926 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34927 c_name = "copyout";
34928 break;
34929 case PRAGMA_OACC_CLAUSE_CREATE:
34930 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34931 c_name = "create";
34932 break;
34933 case PRAGMA_OACC_CLAUSE_DELETE:
34934 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34935 c_name = "delete";
34936 break;
34937 case PRAGMA_OMP_CLAUSE_DEFAULT:
34938 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34939 c_name = "default";
34940 break;
34941 case PRAGMA_OACC_CLAUSE_DEVICE:
34942 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34943 c_name = "device";
34944 break;
34945 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34946 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34947 c_name = "deviceptr";
34948 break;
34949 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34950 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34951 c_name = "device_resident";
34952 break;
34953 case PRAGMA_OACC_CLAUSE_FINALIZE:
34954 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
34955 clauses);
34956 c_name = "finalize";
34957 break;
34958 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34959 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34960 clauses);
34961 c_name = "firstprivate";
34962 break;
34963 case PRAGMA_OACC_CLAUSE_GANG:
34964 c_name = "gang";
34965 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
34966 c_name, clauses);
34967 break;
34968 case PRAGMA_OACC_CLAUSE_HOST:
34969 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34970 c_name = "host";
34971 break;
34972 case PRAGMA_OACC_CLAUSE_IF:
34973 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34974 c_name = "if";
34975 break;
34976 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34977 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
34978 clauses);
34979 c_name = "if_present";
34980 break;
34981 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34982 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
34983 clauses);
34984 c_name = "independent";
34985 break;
34986 case PRAGMA_OACC_CLAUSE_LINK:
34987 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34988 c_name = "link";
34989 break;
34990 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34991 code = OMP_CLAUSE_NUM_GANGS;
34992 c_name = "num_gangs";
34993 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34994 clauses);
34995 break;
34996 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34997 c_name = "num_workers";
34998 code = OMP_CLAUSE_NUM_WORKERS;
34999 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35000 clauses);
35001 break;
35002 case PRAGMA_OACC_CLAUSE_PRESENT:
35003 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35004 c_name = "present";
35005 break;
35006 case PRAGMA_OACC_CLAUSE_PRIVATE:
35007 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35008 clauses);
35009 c_name = "private";
35010 break;
35011 case PRAGMA_OACC_CLAUSE_REDUCTION:
35012 clauses
35013 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35014 false, clauses);
35015 c_name = "reduction";
35016 break;
35017 case PRAGMA_OACC_CLAUSE_SEQ:
35018 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35019 clauses);
35020 c_name = "seq";
35021 break;
35022 case PRAGMA_OACC_CLAUSE_TILE:
35023 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35024 c_name = "tile";
35025 break;
35026 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35027 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35028 clauses);
35029 c_name = "use_device";
35030 break;
35031 case PRAGMA_OACC_CLAUSE_VECTOR:
35032 c_name = "vector";
35033 clauses = cp_parser_oacc_shape_clause (parser, here,
35034 OMP_CLAUSE_VECTOR,
35035 c_name, clauses);
35036 break;
35037 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35038 c_name = "vector_length";
35039 code = OMP_CLAUSE_VECTOR_LENGTH;
35040 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35041 clauses);
35042 break;
35043 case PRAGMA_OACC_CLAUSE_WAIT:
35044 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35045 c_name = "wait";
35046 break;
35047 case PRAGMA_OACC_CLAUSE_WORKER:
35048 c_name = "worker";
35049 clauses = cp_parser_oacc_shape_clause (parser, here,
35050 OMP_CLAUSE_WORKER,
35051 c_name, clauses);
35052 break;
35053 default:
35054 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35055 goto saw_error;
35058 first = false;
35060 if (((mask >> c_kind) & 1) == 0)
35062 /* Remove the invalid clause(s) from the list to avoid
35063 confusing the rest of the compiler. */
35064 clauses = prev;
35065 error_at (here, "%qs is not valid for %qs", c_name, where);
35069 saw_error:
35070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35072 if (finish_p)
35073 return finish_omp_clauses (clauses, C_ORT_ACC);
35075 return clauses;
35078 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35079 is a bitmask in MASK. Return the list of clauses found; the result
35080 of clause default goes in *pdefault. */
35082 static tree
35083 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35084 const char *where, cp_token *pragma_tok,
35085 bool finish_p = true)
35087 tree clauses = NULL;
35088 bool first = true;
35089 cp_token *token = NULL;
35091 /* Don't create location wrapper nodes within OpenMP clauses. */
35092 auto_suppress_location_wrappers sentinel;
35094 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35096 pragma_omp_clause c_kind;
35097 const char *c_name;
35098 tree prev = clauses;
35100 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35101 cp_lexer_consume_token (parser->lexer);
35103 token = cp_lexer_peek_token (parser->lexer);
35104 c_kind = cp_parser_omp_clause_name (parser);
35106 switch (c_kind)
35108 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35109 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35110 token->location);
35111 c_name = "collapse";
35112 break;
35113 case PRAGMA_OMP_CLAUSE_COPYIN:
35114 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35115 c_name = "copyin";
35116 break;
35117 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35118 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35119 clauses);
35120 c_name = "copyprivate";
35121 break;
35122 case PRAGMA_OMP_CLAUSE_DEFAULT:
35123 clauses = cp_parser_omp_clause_default (parser, clauses,
35124 token->location, false);
35125 c_name = "default";
35126 break;
35127 case PRAGMA_OMP_CLAUSE_FINAL:
35128 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35129 c_name = "final";
35130 break;
35131 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35132 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35133 clauses);
35134 c_name = "firstprivate";
35135 break;
35136 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35137 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35138 token->location);
35139 c_name = "grainsize";
35140 break;
35141 case PRAGMA_OMP_CLAUSE_HINT:
35142 clauses = cp_parser_omp_clause_hint (parser, clauses,
35143 token->location);
35144 c_name = "hint";
35145 break;
35146 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35147 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35148 token->location);
35149 c_name = "defaultmap";
35150 break;
35151 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35152 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35153 clauses);
35154 c_name = "use_device_ptr";
35155 break;
35156 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35157 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35158 clauses);
35159 c_name = "is_device_ptr";
35160 break;
35161 case PRAGMA_OMP_CLAUSE_IF:
35162 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35163 true);
35164 c_name = "if";
35165 break;
35166 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35167 clauses
35168 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35169 true, clauses);
35170 c_name = "in_reduction";
35171 break;
35172 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35173 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35174 c_name = "lastprivate";
35175 break;
35176 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35177 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35178 token->location);
35179 c_name = "mergeable";
35180 break;
35181 case PRAGMA_OMP_CLAUSE_NOWAIT:
35182 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35183 c_name = "nowait";
35184 break;
35185 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35186 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35187 token->location);
35188 c_name = "num_tasks";
35189 break;
35190 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35191 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35192 token->location);
35193 c_name = "num_threads";
35194 break;
35195 case PRAGMA_OMP_CLAUSE_ORDERED:
35196 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35197 token->location);
35198 c_name = "ordered";
35199 break;
35200 case PRAGMA_OMP_CLAUSE_PRIORITY:
35201 clauses = cp_parser_omp_clause_priority (parser, clauses,
35202 token->location);
35203 c_name = "priority";
35204 break;
35205 case PRAGMA_OMP_CLAUSE_PRIVATE:
35206 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35207 clauses);
35208 c_name = "private";
35209 break;
35210 case PRAGMA_OMP_CLAUSE_REDUCTION:
35211 clauses
35212 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35213 true, clauses);
35214 c_name = "reduction";
35215 break;
35216 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35217 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35218 token->location);
35219 c_name = "schedule";
35220 break;
35221 case PRAGMA_OMP_CLAUSE_SHARED:
35222 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35223 clauses);
35224 c_name = "shared";
35225 break;
35226 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35227 clauses
35228 = cp_parser_omp_clause_reduction (parser,
35229 OMP_CLAUSE_TASK_REDUCTION,
35230 true, clauses);
35231 c_name = "task_reduction";
35232 break;
35233 case PRAGMA_OMP_CLAUSE_UNTIED:
35234 clauses = cp_parser_omp_clause_untied (parser, clauses,
35235 token->location);
35236 c_name = "untied";
35237 break;
35238 case PRAGMA_OMP_CLAUSE_INBRANCH:
35239 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35240 clauses, token->location);
35241 c_name = "inbranch";
35242 break;
35243 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35244 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35245 clauses);
35246 c_name = "nontemporal";
35247 break;
35248 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35249 clauses = cp_parser_omp_clause_branch (parser,
35250 OMP_CLAUSE_NOTINBRANCH,
35251 clauses, token->location);
35252 c_name = "notinbranch";
35253 break;
35254 case PRAGMA_OMP_CLAUSE_PARALLEL:
35255 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35256 clauses, token->location);
35257 c_name = "parallel";
35258 if (!first)
35260 clause_not_first:
35261 error_at (token->location, "%qs must be the first clause of %qs",
35262 c_name, where);
35263 clauses = prev;
35265 break;
35266 case PRAGMA_OMP_CLAUSE_FOR:
35267 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35268 clauses, token->location);
35269 c_name = "for";
35270 if (!first)
35271 goto clause_not_first;
35272 break;
35273 case PRAGMA_OMP_CLAUSE_SECTIONS:
35274 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35275 clauses, token->location);
35276 c_name = "sections";
35277 if (!first)
35278 goto clause_not_first;
35279 break;
35280 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35281 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35282 clauses, token->location);
35283 c_name = "taskgroup";
35284 if (!first)
35285 goto clause_not_first;
35286 break;
35287 case PRAGMA_OMP_CLAUSE_LINK:
35288 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35289 c_name = "to";
35290 break;
35291 case PRAGMA_OMP_CLAUSE_TO:
35292 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35293 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35294 clauses);
35295 else
35296 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35297 c_name = "to";
35298 break;
35299 case PRAGMA_OMP_CLAUSE_FROM:
35300 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35301 c_name = "from";
35302 break;
35303 case PRAGMA_OMP_CLAUSE_UNIFORM:
35304 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35305 clauses);
35306 c_name = "uniform";
35307 break;
35308 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35309 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35310 token->location);
35311 c_name = "num_teams";
35312 break;
35313 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35314 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35315 token->location);
35316 c_name = "thread_limit";
35317 break;
35318 case PRAGMA_OMP_CLAUSE_ALIGNED:
35319 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35320 c_name = "aligned";
35321 break;
35322 case PRAGMA_OMP_CLAUSE_LINEAR:
35324 bool declare_simd = false;
35325 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35326 declare_simd = true;
35327 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35329 c_name = "linear";
35330 break;
35331 case PRAGMA_OMP_CLAUSE_DEPEND:
35332 clauses = cp_parser_omp_clause_depend (parser, clauses,
35333 token->location);
35334 c_name = "depend";
35335 break;
35336 case PRAGMA_OMP_CLAUSE_MAP:
35337 clauses = cp_parser_omp_clause_map (parser, clauses);
35338 c_name = "map";
35339 break;
35340 case PRAGMA_OMP_CLAUSE_DEVICE:
35341 clauses = cp_parser_omp_clause_device (parser, clauses,
35342 token->location);
35343 c_name = "device";
35344 break;
35345 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35346 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35347 token->location);
35348 c_name = "dist_schedule";
35349 break;
35350 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35351 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35352 token->location);
35353 c_name = "proc_bind";
35354 break;
35355 case PRAGMA_OMP_CLAUSE_SAFELEN:
35356 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35357 token->location);
35358 c_name = "safelen";
35359 break;
35360 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35361 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35362 token->location);
35363 c_name = "simdlen";
35364 break;
35365 case PRAGMA_OMP_CLAUSE_NOGROUP:
35366 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35367 token->location);
35368 c_name = "nogroup";
35369 break;
35370 case PRAGMA_OMP_CLAUSE_THREADS:
35371 clauses
35372 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35373 clauses, token->location);
35374 c_name = "threads";
35375 break;
35376 case PRAGMA_OMP_CLAUSE_SIMD:
35377 clauses
35378 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35379 clauses, token->location);
35380 c_name = "simd";
35381 break;
35382 default:
35383 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35384 goto saw_error;
35387 first = false;
35389 if (((mask >> c_kind) & 1) == 0)
35391 /* Remove the invalid clause(s) from the list to avoid
35392 confusing the rest of the compiler. */
35393 clauses = prev;
35394 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35397 saw_error:
35398 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35399 if (finish_p)
35401 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35402 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35403 else
35404 return finish_omp_clauses (clauses, C_ORT_OMP);
35406 return clauses;
35409 /* OpenMP 2.5:
35410 structured-block:
35411 statement
35413 In practice, we're also interested in adding the statement to an
35414 outer node. So it is convenient if we work around the fact that
35415 cp_parser_statement calls add_stmt. */
35417 static unsigned
35418 cp_parser_begin_omp_structured_block (cp_parser *parser)
35420 unsigned save = parser->in_statement;
35422 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35423 This preserves the "not within loop or switch" style error messages
35424 for nonsense cases like
35425 void foo() {
35426 #pragma omp single
35427 break;
35430 if (parser->in_statement)
35431 parser->in_statement = IN_OMP_BLOCK;
35433 return save;
35436 static void
35437 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35439 parser->in_statement = save;
35442 static tree
35443 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35445 tree stmt = begin_omp_structured_block ();
35446 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35448 cp_parser_statement (parser, NULL_TREE, false, if_p);
35450 cp_parser_end_omp_structured_block (parser, save);
35451 return finish_omp_structured_block (stmt);
35454 /* OpenMP 2.5:
35455 # pragma omp atomic new-line
35456 expression-stmt
35458 expression-stmt:
35459 x binop= expr | x++ | ++x | x-- | --x
35460 binop:
35461 +, *, -, /, &, ^, |, <<, >>
35463 where x is an lvalue expression with scalar type.
35465 OpenMP 3.1:
35466 # pragma omp atomic new-line
35467 update-stmt
35469 # pragma omp atomic read new-line
35470 read-stmt
35472 # pragma omp atomic write new-line
35473 write-stmt
35475 # pragma omp atomic update new-line
35476 update-stmt
35478 # pragma omp atomic capture new-line
35479 capture-stmt
35481 # pragma omp atomic capture new-line
35482 capture-block
35484 read-stmt:
35485 v = x
35486 write-stmt:
35487 x = expr
35488 update-stmt:
35489 expression-stmt | x = x binop expr
35490 capture-stmt:
35491 v = expression-stmt
35492 capture-block:
35493 { v = x; update-stmt; } | { update-stmt; v = x; }
35495 OpenMP 4.0:
35496 update-stmt:
35497 expression-stmt | x = x binop expr | x = expr binop x
35498 capture-stmt:
35499 v = update-stmt
35500 capture-block:
35501 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35503 where x and v are lvalue expressions with scalar type. */
35505 static void
35506 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35508 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35509 tree rhs1 = NULL_TREE, orig_lhs;
35510 location_t loc = pragma_tok->location;
35511 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35512 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35513 bool structured_block = false;
35514 bool first = true;
35515 tree clauses = NULL_TREE;
35517 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35519 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35520 cp_lexer_consume_token (parser->lexer);
35522 first = false;
35524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35526 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35527 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35528 const char *p = IDENTIFIER_POINTER (id);
35529 enum tree_code new_code = ERROR_MARK;
35530 enum omp_memory_order new_memory_order
35531 = OMP_MEMORY_ORDER_UNSPECIFIED;
35533 if (!strcmp (p, "read"))
35534 new_code = OMP_ATOMIC_READ;
35535 else if (!strcmp (p, "write"))
35536 new_code = NOP_EXPR;
35537 else if (!strcmp (p, "update"))
35538 new_code = OMP_ATOMIC;
35539 else if (!strcmp (p, "capture"))
35540 new_code = OMP_ATOMIC_CAPTURE_NEW;
35541 else if (!strcmp (p, "seq_cst"))
35542 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35543 else if (!strcmp (p, "acq_rel"))
35544 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35545 else if (!strcmp (p, "release"))
35546 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35547 else if (!strcmp (p, "acquire"))
35548 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35549 else if (!strcmp (p, "relaxed"))
35550 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35551 else if (!strcmp (p, "hint"))
35553 cp_lexer_consume_token (parser->lexer);
35554 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35555 continue;
35557 else
35559 p = NULL;
35560 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35561 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35562 "%<release%>, %<relaxed%> or %<hint%> clause");
35564 if (p)
35566 if (new_code != ERROR_MARK)
35568 if (code != ERROR_MARK)
35569 error_at (cloc, "too many atomic clauses");
35570 else
35571 code = new_code;
35573 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35575 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35576 error_at (cloc, "too many memory order clauses");
35577 else
35578 memory_order = new_memory_order;
35580 cp_lexer_consume_token (parser->lexer);
35581 continue;
35584 break;
35586 cp_parser_require_pragma_eol (parser, pragma_tok);
35588 if (code == ERROR_MARK)
35589 code = OMP_ATOMIC;
35590 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35592 omp_requires_mask
35593 = (enum omp_requires) (omp_requires_mask
35594 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35595 switch ((enum omp_memory_order)
35596 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35598 case OMP_MEMORY_ORDER_UNSPECIFIED:
35599 case OMP_MEMORY_ORDER_RELAXED:
35600 memory_order = OMP_MEMORY_ORDER_RELAXED;
35601 break;
35602 case OMP_MEMORY_ORDER_SEQ_CST:
35603 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35604 break;
35605 case OMP_MEMORY_ORDER_ACQ_REL:
35606 switch (code)
35608 case OMP_ATOMIC_READ:
35609 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35610 break;
35611 case NOP_EXPR: /* atomic write */
35612 case OMP_ATOMIC:
35613 memory_order = OMP_MEMORY_ORDER_RELEASE;
35614 break;
35615 default:
35616 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35617 break;
35619 break;
35620 default:
35621 gcc_unreachable ();
35624 else
35625 switch (code)
35627 case OMP_ATOMIC_READ:
35628 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35629 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35631 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35632 "%<acq_rel%> or %<release%> clauses");
35633 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35635 break;
35636 case NOP_EXPR: /* atomic write */
35637 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35638 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35640 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35641 "%<acq_rel%> or %<acquire%> clauses");
35642 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35644 break;
35645 case OMP_ATOMIC:
35646 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35647 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35649 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35650 "%<acq_rel%> or %<acquire%> clauses");
35651 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35653 break;
35654 default:
35655 break;
35658 switch (code)
35660 case OMP_ATOMIC_READ:
35661 case NOP_EXPR: /* atomic write */
35662 v = cp_parser_unary_expression (parser);
35663 if (v == error_mark_node)
35664 goto saw_error;
35665 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35666 goto saw_error;
35667 if (code == NOP_EXPR)
35668 lhs = cp_parser_expression (parser);
35669 else
35670 lhs = cp_parser_unary_expression (parser);
35671 if (lhs == error_mark_node)
35672 goto saw_error;
35673 if (code == NOP_EXPR)
35675 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35676 opcode. */
35677 code = OMP_ATOMIC;
35678 rhs = lhs;
35679 lhs = v;
35680 v = NULL_TREE;
35682 goto done;
35683 case OMP_ATOMIC_CAPTURE_NEW:
35684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35686 cp_lexer_consume_token (parser->lexer);
35687 structured_block = true;
35689 else
35691 v = cp_parser_unary_expression (parser);
35692 if (v == error_mark_node)
35693 goto saw_error;
35694 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35695 goto saw_error;
35697 default:
35698 break;
35701 restart:
35702 lhs = cp_parser_unary_expression (parser);
35703 orig_lhs = lhs;
35704 switch (TREE_CODE (lhs))
35706 case ERROR_MARK:
35707 goto saw_error;
35709 case POSTINCREMENT_EXPR:
35710 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35711 code = OMP_ATOMIC_CAPTURE_OLD;
35712 /* FALLTHROUGH */
35713 case PREINCREMENT_EXPR:
35714 lhs = TREE_OPERAND (lhs, 0);
35715 opcode = PLUS_EXPR;
35716 rhs = integer_one_node;
35717 break;
35719 case POSTDECREMENT_EXPR:
35720 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35721 code = OMP_ATOMIC_CAPTURE_OLD;
35722 /* FALLTHROUGH */
35723 case PREDECREMENT_EXPR:
35724 lhs = TREE_OPERAND (lhs, 0);
35725 opcode = MINUS_EXPR;
35726 rhs = integer_one_node;
35727 break;
35729 case COMPOUND_EXPR:
35730 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35731 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35732 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35733 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35734 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35735 (TREE_OPERAND (lhs, 1), 0), 0)))
35736 == BOOLEAN_TYPE)
35737 /* Undo effects of boolean_increment for post {in,de}crement. */
35738 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35739 /* FALLTHRU */
35740 case MODIFY_EXPR:
35741 if (TREE_CODE (lhs) == MODIFY_EXPR
35742 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35744 /* Undo effects of boolean_increment. */
35745 if (integer_onep (TREE_OPERAND (lhs, 1)))
35747 /* This is pre or post increment. */
35748 rhs = TREE_OPERAND (lhs, 1);
35749 lhs = TREE_OPERAND (lhs, 0);
35750 opcode = NOP_EXPR;
35751 if (code == OMP_ATOMIC_CAPTURE_NEW
35752 && !structured_block
35753 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35754 code = OMP_ATOMIC_CAPTURE_OLD;
35755 break;
35758 /* FALLTHRU */
35759 default:
35760 switch (cp_lexer_peek_token (parser->lexer)->type)
35762 case CPP_MULT_EQ:
35763 opcode = MULT_EXPR;
35764 break;
35765 case CPP_DIV_EQ:
35766 opcode = TRUNC_DIV_EXPR;
35767 break;
35768 case CPP_PLUS_EQ:
35769 opcode = PLUS_EXPR;
35770 break;
35771 case CPP_MINUS_EQ:
35772 opcode = MINUS_EXPR;
35773 break;
35774 case CPP_LSHIFT_EQ:
35775 opcode = LSHIFT_EXPR;
35776 break;
35777 case CPP_RSHIFT_EQ:
35778 opcode = RSHIFT_EXPR;
35779 break;
35780 case CPP_AND_EQ:
35781 opcode = BIT_AND_EXPR;
35782 break;
35783 case CPP_OR_EQ:
35784 opcode = BIT_IOR_EXPR;
35785 break;
35786 case CPP_XOR_EQ:
35787 opcode = BIT_XOR_EXPR;
35788 break;
35789 case CPP_EQ:
35790 enum cp_parser_prec oprec;
35791 cp_token *token;
35792 cp_lexer_consume_token (parser->lexer);
35793 cp_parser_parse_tentatively (parser);
35794 rhs1 = cp_parser_simple_cast_expression (parser);
35795 if (rhs1 == error_mark_node)
35797 cp_parser_abort_tentative_parse (parser);
35798 cp_parser_simple_cast_expression (parser);
35799 goto saw_error;
35801 token = cp_lexer_peek_token (parser->lexer);
35802 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35804 cp_parser_abort_tentative_parse (parser);
35805 cp_parser_parse_tentatively (parser);
35806 rhs = cp_parser_binary_expression (parser, false, true,
35807 PREC_NOT_OPERATOR, NULL);
35808 if (rhs == error_mark_node)
35810 cp_parser_abort_tentative_parse (parser);
35811 cp_parser_binary_expression (parser, false, true,
35812 PREC_NOT_OPERATOR, NULL);
35813 goto saw_error;
35815 switch (TREE_CODE (rhs))
35817 case MULT_EXPR:
35818 case TRUNC_DIV_EXPR:
35819 case RDIV_EXPR:
35820 case PLUS_EXPR:
35821 case MINUS_EXPR:
35822 case LSHIFT_EXPR:
35823 case RSHIFT_EXPR:
35824 case BIT_AND_EXPR:
35825 case BIT_IOR_EXPR:
35826 case BIT_XOR_EXPR:
35827 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35829 if (cp_parser_parse_definitely (parser))
35831 opcode = TREE_CODE (rhs);
35832 rhs1 = TREE_OPERAND (rhs, 0);
35833 rhs = TREE_OPERAND (rhs, 1);
35834 goto stmt_done;
35836 else
35837 goto saw_error;
35839 break;
35840 default:
35841 break;
35843 cp_parser_abort_tentative_parse (parser);
35844 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35846 rhs = cp_parser_expression (parser);
35847 if (rhs == error_mark_node)
35848 goto saw_error;
35849 opcode = NOP_EXPR;
35850 rhs1 = NULL_TREE;
35851 goto stmt_done;
35853 cp_parser_error (parser,
35854 "invalid form of %<#pragma omp atomic%>");
35855 goto saw_error;
35857 if (!cp_parser_parse_definitely (parser))
35858 goto saw_error;
35859 switch (token->type)
35861 case CPP_SEMICOLON:
35862 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35864 code = OMP_ATOMIC_CAPTURE_OLD;
35865 v = lhs;
35866 lhs = NULL_TREE;
35867 lhs1 = rhs1;
35868 rhs1 = NULL_TREE;
35869 cp_lexer_consume_token (parser->lexer);
35870 goto restart;
35872 else if (structured_block)
35874 opcode = NOP_EXPR;
35875 rhs = rhs1;
35876 rhs1 = NULL_TREE;
35877 goto stmt_done;
35879 cp_parser_error (parser,
35880 "invalid form of %<#pragma omp atomic%>");
35881 goto saw_error;
35882 case CPP_MULT:
35883 opcode = MULT_EXPR;
35884 break;
35885 case CPP_DIV:
35886 opcode = TRUNC_DIV_EXPR;
35887 break;
35888 case CPP_PLUS:
35889 opcode = PLUS_EXPR;
35890 break;
35891 case CPP_MINUS:
35892 opcode = MINUS_EXPR;
35893 break;
35894 case CPP_LSHIFT:
35895 opcode = LSHIFT_EXPR;
35896 break;
35897 case CPP_RSHIFT:
35898 opcode = RSHIFT_EXPR;
35899 break;
35900 case CPP_AND:
35901 opcode = BIT_AND_EXPR;
35902 break;
35903 case CPP_OR:
35904 opcode = BIT_IOR_EXPR;
35905 break;
35906 case CPP_XOR:
35907 opcode = BIT_XOR_EXPR;
35908 break;
35909 default:
35910 cp_parser_error (parser,
35911 "invalid operator for %<#pragma omp atomic%>");
35912 goto saw_error;
35914 oprec = TOKEN_PRECEDENCE (token);
35915 gcc_assert (oprec != PREC_NOT_OPERATOR);
35916 if (commutative_tree_code (opcode))
35917 oprec = (enum cp_parser_prec) (oprec - 1);
35918 cp_lexer_consume_token (parser->lexer);
35919 rhs = cp_parser_binary_expression (parser, false, false,
35920 oprec, NULL);
35921 if (rhs == error_mark_node)
35922 goto saw_error;
35923 goto stmt_done;
35924 /* FALLTHROUGH */
35925 default:
35926 cp_parser_error (parser,
35927 "invalid operator for %<#pragma omp atomic%>");
35928 goto saw_error;
35930 cp_lexer_consume_token (parser->lexer);
35932 rhs = cp_parser_expression (parser);
35933 if (rhs == error_mark_node)
35934 goto saw_error;
35935 break;
35937 stmt_done:
35938 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35940 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35941 goto saw_error;
35942 v = cp_parser_unary_expression (parser);
35943 if (v == error_mark_node)
35944 goto saw_error;
35945 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35946 goto saw_error;
35947 lhs1 = cp_parser_unary_expression (parser);
35948 if (lhs1 == error_mark_node)
35949 goto saw_error;
35951 if (structured_block)
35953 cp_parser_consume_semicolon_at_end_of_statement (parser);
35954 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35956 done:
35957 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35958 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35959 rhs1, clauses, memory_order);
35960 if (!structured_block)
35961 cp_parser_consume_semicolon_at_end_of_statement (parser);
35962 return;
35964 saw_error:
35965 cp_parser_skip_to_end_of_block_or_statement (parser);
35966 if (structured_block)
35968 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35969 cp_lexer_consume_token (parser->lexer);
35970 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35972 cp_parser_skip_to_end_of_block_or_statement (parser);
35973 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35974 cp_lexer_consume_token (parser->lexer);
35980 /* OpenMP 2.5:
35981 # pragma omp barrier new-line */
35983 static void
35984 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35986 cp_parser_require_pragma_eol (parser, pragma_tok);
35987 finish_omp_barrier ();
35990 /* OpenMP 2.5:
35991 # pragma omp critical [(name)] new-line
35992 structured-block
35994 OpenMP 4.5:
35995 # pragma omp critical [(name) [hint(expression)]] new-line
35996 structured-block */
35998 #define OMP_CRITICAL_CLAUSE_MASK \
35999 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36001 static tree
36002 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36004 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36008 matching_parens parens;
36009 parens.consume_open (parser);
36011 name = cp_parser_identifier (parser);
36013 if (name == error_mark_node
36014 || !parens.require_close (parser))
36015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36016 /*or_comma=*/false,
36017 /*consume_paren=*/true);
36018 if (name == error_mark_node)
36019 name = NULL;
36021 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36022 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36023 cp_lexer_consume_token (parser->lexer);
36025 clauses = cp_parser_omp_all_clauses (parser,
36026 OMP_CRITICAL_CLAUSE_MASK,
36027 "#pragma omp critical", pragma_tok);
36029 else
36030 cp_parser_require_pragma_eol (parser, pragma_tok);
36032 stmt = cp_parser_omp_structured_block (parser, if_p);
36033 return c_finish_omp_critical (input_location, stmt, name, clauses);
36036 /* OpenMP 5.0:
36037 # pragma omp depobj ( depobj ) depobj-clause new-line
36039 depobj-clause:
36040 depend (dependence-type : locator)
36041 destroy
36042 update (dependence-type)
36044 dependence-type:
36047 inout
36048 mutexinout */
36050 static void
36051 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36053 location_t loc = pragma_tok->location;
36054 matching_parens parens;
36055 if (!parens.require_open (parser))
36057 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36058 return;
36061 tree depobj = cp_parser_assignment_expression (parser);
36063 if (!parens.require_close (parser))
36064 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36065 /*or_comma=*/false,
36066 /*consume_paren=*/true);
36068 tree clause = NULL_TREE;
36069 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36070 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36071 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36073 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36074 const char *p = IDENTIFIER_POINTER (id);
36076 cp_lexer_consume_token (parser->lexer);
36077 if (!strcmp ("depend", p))
36079 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36080 if (clause)
36081 clause = finish_omp_clauses (clause, C_ORT_OMP);
36082 if (!clause)
36083 clause = error_mark_node;
36085 else if (!strcmp ("destroy", p))
36086 kind = OMP_CLAUSE_DEPEND_LAST;
36087 else if (!strcmp ("update", p))
36089 matching_parens c_parens;
36090 if (c_parens.require_open (parser))
36092 location_t c2_loc
36093 = cp_lexer_peek_token (parser->lexer)->location;
36094 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36096 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36097 const char *p2 = IDENTIFIER_POINTER (id2);
36099 cp_lexer_consume_token (parser->lexer);
36100 if (!strcmp ("in", p2))
36101 kind = OMP_CLAUSE_DEPEND_IN;
36102 else if (!strcmp ("out", p2))
36103 kind = OMP_CLAUSE_DEPEND_OUT;
36104 else if (!strcmp ("inout", p2))
36105 kind = OMP_CLAUSE_DEPEND_INOUT;
36106 else if (!strcmp ("mutexinoutset", p2))
36107 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36109 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36111 clause = error_mark_node;
36112 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36113 "%<mutexinoutset%>");
36115 if (!c_parens.require_close (parser))
36116 cp_parser_skip_to_closing_parenthesis (parser,
36117 /*recovering=*/true,
36118 /*or_comma=*/false,
36119 /*consume_paren=*/true);
36121 else
36122 clause = error_mark_node;
36125 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36127 clause = error_mark_node;
36128 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36130 cp_parser_require_pragma_eol (parser, pragma_tok);
36132 finish_omp_depobj (loc, depobj, kind, clause);
36136 /* OpenMP 2.5:
36137 # pragma omp flush flush-vars[opt] new-line
36139 flush-vars:
36140 ( variable-list )
36142 OpenMP 5.0:
36143 # pragma omp flush memory-order-clause new-line */
36145 static void
36146 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36148 enum memmodel mo = MEMMODEL_LAST;
36149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36151 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36152 const char *p = IDENTIFIER_POINTER (id);
36153 if (!strcmp (p, "acq_rel"))
36154 mo = MEMMODEL_ACQ_REL;
36155 else if (!strcmp (p, "release"))
36156 mo = MEMMODEL_RELEASE;
36157 else if (!strcmp (p, "acquire"))
36158 mo = MEMMODEL_ACQUIRE;
36159 else
36160 error_at (cp_lexer_peek_token (parser->lexer)->location,
36161 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36162 cp_lexer_consume_token (parser->lexer);
36164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36166 if (mo != MEMMODEL_LAST)
36167 error_at (cp_lexer_peek_token (parser->lexer)->location,
36168 "%<flush%> list specified together with memory order "
36169 "clause");
36170 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36172 cp_parser_require_pragma_eol (parser, pragma_tok);
36174 finish_omp_flush (mo);
36177 /* Helper function, to parse omp for increment expression. */
36179 static tree
36180 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36182 tree cond = cp_parser_binary_expression (parser, false, true,
36183 PREC_NOT_OPERATOR, NULL);
36184 if (cond == error_mark_node
36185 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36187 cp_parser_skip_to_end_of_statement (parser);
36188 return error_mark_node;
36191 switch (TREE_CODE (cond))
36193 case GT_EXPR:
36194 case GE_EXPR:
36195 case LT_EXPR:
36196 case LE_EXPR:
36197 break;
36198 case NE_EXPR:
36199 if (code != OACC_LOOP)
36200 break;
36201 gcc_fallthrough ();
36202 default:
36203 return error_mark_node;
36206 /* If decl is an iterator, preserve LHS and RHS of the relational
36207 expr until finish_omp_for. */
36208 if (decl
36209 && (type_dependent_expression_p (decl)
36210 || CLASS_TYPE_P (TREE_TYPE (decl))))
36211 return cond;
36213 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36214 TREE_CODE (cond),
36215 TREE_OPERAND (cond, 0), ERROR_MARK,
36216 TREE_OPERAND (cond, 1), ERROR_MARK,
36217 /*overload=*/NULL, tf_warning_or_error);
36220 /* Helper function, to parse omp for increment expression. */
36222 static tree
36223 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36225 cp_token *token = cp_lexer_peek_token (parser->lexer);
36226 enum tree_code op;
36227 tree lhs, rhs;
36228 cp_id_kind idk;
36229 bool decl_first;
36231 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36233 op = (token->type == CPP_PLUS_PLUS
36234 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36235 cp_lexer_consume_token (parser->lexer);
36236 lhs = cp_parser_simple_cast_expression (parser);
36237 if (lhs != decl
36238 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36239 return error_mark_node;
36240 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36243 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36244 if (lhs != decl
36245 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36246 return error_mark_node;
36248 token = cp_lexer_peek_token (parser->lexer);
36249 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36251 op = (token->type == CPP_PLUS_PLUS
36252 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36253 cp_lexer_consume_token (parser->lexer);
36254 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36257 op = cp_parser_assignment_operator_opt (parser);
36258 if (op == ERROR_MARK)
36259 return error_mark_node;
36261 if (op != NOP_EXPR)
36263 rhs = cp_parser_assignment_expression (parser);
36264 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36265 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36268 lhs = cp_parser_binary_expression (parser, false, false,
36269 PREC_ADDITIVE_EXPRESSION, NULL);
36270 token = cp_lexer_peek_token (parser->lexer);
36271 decl_first = (lhs == decl
36272 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36273 if (decl_first)
36274 lhs = NULL_TREE;
36275 if (token->type != CPP_PLUS
36276 && token->type != CPP_MINUS)
36277 return error_mark_node;
36281 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36282 cp_lexer_consume_token (parser->lexer);
36283 rhs = cp_parser_binary_expression (parser, false, false,
36284 PREC_ADDITIVE_EXPRESSION, NULL);
36285 token = cp_lexer_peek_token (parser->lexer);
36286 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36288 if (lhs == NULL_TREE)
36290 if (op == PLUS_EXPR)
36291 lhs = rhs;
36292 else
36293 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36294 tf_warning_or_error);
36296 else
36297 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36298 ERROR_MARK, NULL, tf_warning_or_error);
36301 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36303 if (!decl_first)
36305 if ((rhs != decl
36306 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36307 || op == MINUS_EXPR)
36308 return error_mark_node;
36309 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36311 else
36312 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36314 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36317 /* Parse the initialization statement of an OpenMP for loop.
36319 Return true if the resulting construct should have an
36320 OMP_CLAUSE_PRIVATE added to it. */
36322 static tree
36323 cp_parser_omp_for_loop_init (cp_parser *parser,
36324 tree &this_pre_body,
36325 vec<tree, va_gc> *&for_block,
36326 tree &init,
36327 tree &orig_init,
36328 tree &decl,
36329 tree &real_decl)
36331 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36332 return NULL_TREE;
36334 tree add_private_clause = NULL_TREE;
36336 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36338 init-expr:
36339 var = lb
36340 integer-type var = lb
36341 random-access-iterator-type var = lb
36342 pointer-type var = lb
36344 cp_decl_specifier_seq type_specifiers;
36346 /* First, try to parse as an initialized declaration. See
36347 cp_parser_condition, from whence the bulk of this is copied. */
36349 cp_parser_parse_tentatively (parser);
36350 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36351 /*is_declaration=*/true,
36352 /*is_trailing_return=*/false,
36353 &type_specifiers);
36354 if (cp_parser_parse_definitely (parser))
36356 /* If parsing a type specifier seq succeeded, then this
36357 MUST be a initialized declaration. */
36358 tree asm_specification, attributes;
36359 cp_declarator *declarator;
36361 declarator = cp_parser_declarator (parser,
36362 CP_PARSER_DECLARATOR_NAMED,
36363 CP_PARSER_FLAGS_NONE,
36364 /*ctor_dtor_or_conv_p=*/NULL,
36365 /*parenthesized_p=*/NULL,
36366 /*member_p=*/false,
36367 /*friend_p=*/false,
36368 /*static_p=*/false);
36369 attributes = cp_parser_attributes_opt (parser);
36370 asm_specification = cp_parser_asm_specification_opt (parser);
36372 if (declarator == cp_error_declarator)
36373 cp_parser_skip_to_end_of_statement (parser);
36375 else
36377 tree pushed_scope, auto_node;
36379 decl = start_decl (declarator, &type_specifiers,
36380 SD_INITIALIZED, attributes,
36381 /*prefix_attributes=*/NULL_TREE,
36382 &pushed_scope);
36384 auto_node = type_uses_auto (TREE_TYPE (decl));
36385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36387 if (cp_lexer_next_token_is (parser->lexer,
36388 CPP_OPEN_PAREN))
36389 error ("parenthesized initialization is not allowed in "
36390 "OpenMP %<for%> loop");
36391 else
36392 /* Trigger an error. */
36393 cp_parser_require (parser, CPP_EQ, RT_EQ);
36395 init = error_mark_node;
36396 cp_parser_skip_to_end_of_statement (parser);
36398 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36399 || type_dependent_expression_p (decl)
36400 || auto_node)
36402 bool is_direct_init, is_non_constant_init;
36404 init = cp_parser_initializer (parser,
36405 &is_direct_init,
36406 &is_non_constant_init);
36408 if (auto_node)
36410 TREE_TYPE (decl)
36411 = do_auto_deduction (TREE_TYPE (decl), init,
36412 auto_node);
36414 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36415 && !type_dependent_expression_p (decl))
36416 goto non_class;
36419 cp_finish_decl (decl, init, !is_non_constant_init,
36420 asm_specification,
36421 LOOKUP_ONLYCONVERTING);
36422 orig_init = init;
36423 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36425 vec_safe_push (for_block, this_pre_body);
36426 init = NULL_TREE;
36428 else
36430 init = pop_stmt_list (this_pre_body);
36431 if (init && TREE_CODE (init) == STATEMENT_LIST)
36433 tree_stmt_iterator i = tsi_start (init);
36434 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36435 while (!tsi_end_p (i))
36437 tree t = tsi_stmt (i);
36438 if (TREE_CODE (t) == DECL_EXPR
36439 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36441 tsi_delink (&i);
36442 vec_safe_push (for_block, t);
36443 continue;
36445 break;
36447 if (tsi_one_before_end_p (i))
36449 tree t = tsi_stmt (i);
36450 tsi_delink (&i);
36451 free_stmt_list (init);
36452 init = t;
36456 this_pre_body = NULL_TREE;
36458 else
36460 /* Consume '='. */
36461 cp_lexer_consume_token (parser->lexer);
36462 init = cp_parser_assignment_expression (parser);
36464 non_class:
36465 if (TYPE_REF_P (TREE_TYPE (decl)))
36466 init = error_mark_node;
36467 else
36468 cp_finish_decl (decl, NULL_TREE,
36469 /*init_const_expr_p=*/false,
36470 asm_specification,
36471 LOOKUP_ONLYCONVERTING);
36474 if (pushed_scope)
36475 pop_scope (pushed_scope);
36478 else
36480 cp_id_kind idk;
36481 /* If parsing a type specifier sequence failed, then
36482 this MUST be a simple expression. */
36483 cp_parser_parse_tentatively (parser);
36484 decl = cp_parser_primary_expression (parser, false, false,
36485 false, &idk);
36486 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36487 if (!cp_parser_error_occurred (parser)
36488 && decl
36489 && (TREE_CODE (decl) == COMPONENT_REF
36490 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36492 cp_parser_abort_tentative_parse (parser);
36493 cp_parser_parse_tentatively (parser);
36494 cp_token *token = cp_lexer_peek_token (parser->lexer);
36495 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36496 /*check_dependency_p=*/true,
36497 /*template_p=*/NULL,
36498 /*declarator_p=*/false,
36499 /*optional_p=*/false);
36500 if (name != error_mark_node
36501 && last_tok == cp_lexer_peek_token (parser->lexer))
36503 decl = cp_parser_lookup_name_simple (parser, name,
36504 token->location);
36505 if (TREE_CODE (decl) == FIELD_DECL)
36506 add_private_clause = omp_privatize_field (decl, false);
36508 cp_parser_abort_tentative_parse (parser);
36509 cp_parser_parse_tentatively (parser);
36510 decl = cp_parser_primary_expression (parser, false, false,
36511 false, &idk);
36513 if (!cp_parser_error_occurred (parser)
36514 && decl
36515 && DECL_P (decl)
36516 && CLASS_TYPE_P (TREE_TYPE (decl)))
36518 tree rhs;
36520 cp_parser_parse_definitely (parser);
36521 cp_parser_require (parser, CPP_EQ, RT_EQ);
36522 rhs = cp_parser_assignment_expression (parser);
36523 orig_init = rhs;
36524 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36525 decl, NOP_EXPR,
36526 rhs,
36527 tf_warning_or_error));
36528 if (!add_private_clause)
36529 add_private_clause = decl;
36531 else
36533 decl = NULL;
36534 cp_parser_abort_tentative_parse (parser);
36535 init = cp_parser_expression (parser);
36536 if (init)
36538 if (TREE_CODE (init) == MODIFY_EXPR
36539 || TREE_CODE (init) == MODOP_EXPR)
36540 real_decl = TREE_OPERAND (init, 0);
36544 return add_private_clause;
36547 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36549 void
36550 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36551 tree &decl, tree &orig_decl, tree &init,
36552 tree &orig_init, tree &cond, tree &incr)
36554 tree begin, end, range_temp_decl = NULL_TREE;
36555 tree iter_type, begin_expr, end_expr;
36557 if (processing_template_decl)
36559 if (check_for_bare_parameter_packs (init))
36560 init = error_mark_node;
36561 if (!type_dependent_expression_p (init)
36562 /* do_auto_deduction doesn't mess with template init-lists. */
36563 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36565 tree d = decl;
36566 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36568 tree v = DECL_VALUE_EXPR (decl);
36569 if (TREE_CODE (v) == ARRAY_REF
36570 && VAR_P (TREE_OPERAND (v, 0))
36571 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36572 d = TREE_OPERAND (v, 0);
36574 do_range_for_auto_deduction (d, init);
36576 cond = global_namespace;
36577 incr = NULL_TREE;
36578 orig_init = init;
36579 if (this_pre_body)
36580 this_pre_body = pop_stmt_list (this_pre_body);
36581 return;
36584 init = mark_lvalue_use (init);
36586 if (decl == error_mark_node || init == error_mark_node)
36587 /* If an error happened previously do nothing or else a lot of
36588 unhelpful errors would be issued. */
36589 begin_expr = end_expr = iter_type = error_mark_node;
36590 else
36592 tree range_temp;
36594 if (VAR_P (init)
36595 && array_of_runtime_bound_p (TREE_TYPE (init)))
36596 /* Can't bind a reference to an array of runtime bound. */
36597 range_temp = init;
36598 else
36600 range_temp = build_range_temp (init);
36601 DECL_NAME (range_temp) = NULL_TREE;
36602 pushdecl (range_temp);
36603 cp_finish_decl (range_temp, init,
36604 /*is_constant_init*/false, NULL_TREE,
36605 LOOKUP_ONLYCONVERTING);
36606 range_temp_decl = range_temp;
36607 range_temp = convert_from_reference (range_temp);
36609 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36610 &begin_expr, &end_expr);
36613 tree end_iter_type = iter_type;
36614 if (cxx_dialect >= cxx17)
36615 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36616 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36617 TREE_USED (end) = 1;
36618 DECL_ARTIFICIAL (end) = 1;
36619 pushdecl (end);
36620 cp_finish_decl (end, end_expr,
36621 /*is_constant_init*/false, NULL_TREE,
36622 LOOKUP_ONLYCONVERTING);
36624 /* The new for initialization statement. */
36625 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36626 TREE_USED (begin) = 1;
36627 DECL_ARTIFICIAL (begin) = 1;
36628 pushdecl (begin);
36629 orig_init = init;
36630 if (CLASS_TYPE_P (iter_type))
36631 init = NULL_TREE;
36632 else
36634 init = begin_expr;
36635 begin_expr = NULL_TREE;
36637 cp_finish_decl (begin, begin_expr,
36638 /*is_constant_init*/false, NULL_TREE,
36639 LOOKUP_ONLYCONVERTING);
36641 /* The new for condition. */
36642 if (CLASS_TYPE_P (iter_type))
36643 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36644 else
36645 cond = build_x_binary_op (input_location, NE_EXPR,
36646 begin, ERROR_MARK,
36647 end, ERROR_MARK,
36648 NULL, tf_warning_or_error);
36650 /* The new increment expression. */
36651 if (CLASS_TYPE_P (iter_type))
36652 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36653 else
36654 incr = finish_unary_op_expr (input_location,
36655 PREINCREMENT_EXPR, begin,
36656 tf_warning_or_error);
36658 orig_decl = decl;
36659 decl = begin;
36660 if (for_block)
36662 vec_safe_push (for_block, this_pre_body);
36663 this_pre_body = NULL_TREE;
36666 tree decomp_first_name = NULL_TREE;
36667 unsigned decomp_cnt = 0;
36668 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36670 tree v = DECL_VALUE_EXPR (orig_decl);
36671 if (TREE_CODE (v) == ARRAY_REF
36672 && VAR_P (TREE_OPERAND (v, 0))
36673 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36675 tree d = orig_decl;
36676 orig_decl = TREE_OPERAND (v, 0);
36677 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36678 decomp_first_name = d;
36682 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36683 if (auto_node)
36685 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36686 tf_none);
36687 if (!error_operand_p (t))
36688 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36689 t, auto_node);
36692 tree v = make_tree_vec (decomp_cnt + 3);
36693 TREE_VEC_ELT (v, 0) = range_temp_decl;
36694 TREE_VEC_ELT (v, 1) = end;
36695 TREE_VEC_ELT (v, 2) = orig_decl;
36696 for (unsigned i = 0; i < decomp_cnt; i++)
36698 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36699 decomp_first_name = DECL_CHAIN (decomp_first_name);
36701 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36704 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36705 inside of the collapsed body. */
36707 void
36708 cp_finish_omp_range_for (tree orig, tree begin)
36710 gcc_assert (TREE_CODE (orig) == TREE_LIST
36711 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36712 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36713 tree decomp_first_name = NULL_TREE;
36714 unsigned int decomp_cnt = 0;
36716 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36718 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36719 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36720 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36723 /* The declaration is initialized with *__begin inside the loop body. */
36724 cp_finish_decl (decl,
36725 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36726 tf_warning_or_error),
36727 /*is_constant_init*/false, NULL_TREE,
36728 LOOKUP_ONLYCONVERTING);
36729 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36730 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36733 /* Parse the restricted form of the for statement allowed by OpenMP. */
36735 static tree
36736 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36737 tree *cclauses, bool *if_p)
36739 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36740 tree orig_decl;
36741 tree real_decl, initv, condv, incrv, declv, orig_declv;
36742 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36743 location_t loc_first;
36744 bool collapse_err = false;
36745 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36746 vec<tree, va_gc> *for_block = make_tree_vector ();
36747 auto_vec<tree, 4> orig_inits;
36748 bool tiling = false;
36750 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36751 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36752 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36753 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36755 tiling = true;
36756 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36758 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36759 && OMP_CLAUSE_ORDERED_EXPR (cl))
36761 ordered_cl = cl;
36762 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36765 if (ordered && ordered < collapse)
36767 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36768 "%<ordered%> clause parameter is less than %<collapse%>");
36769 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36770 = build_int_cst (NULL_TREE, collapse);
36771 ordered = collapse;
36773 if (ordered)
36775 for (tree *pc = &clauses; *pc; )
36776 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36778 error_at (OMP_CLAUSE_LOCATION (*pc),
36779 "%<linear%> clause may not be specified together "
36780 "with %<ordered%> clause with a parameter");
36781 *pc = OMP_CLAUSE_CHAIN (*pc);
36783 else
36784 pc = &OMP_CLAUSE_CHAIN (*pc);
36787 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36788 count = ordered ? ordered : collapse;
36790 declv = make_tree_vec (count);
36791 initv = make_tree_vec (count);
36792 condv = make_tree_vec (count);
36793 incrv = make_tree_vec (count);
36794 orig_declv = NULL_TREE;
36796 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36798 for (i = 0; i < count; i++)
36800 int bracecount = 0;
36801 tree add_private_clause = NULL_TREE;
36802 location_t loc;
36804 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36806 if (!collapse_err)
36807 cp_parser_error (parser, "for statement expected");
36808 return NULL;
36810 loc = cp_lexer_consume_token (parser->lexer)->location;
36812 /* Don't create location wrapper nodes within an OpenMP "for"
36813 statement. */
36814 auto_suppress_location_wrappers sentinel;
36816 matching_parens parens;
36817 if (!parens.require_open (parser))
36818 return NULL;
36820 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36821 this_pre_body = push_stmt_list ();
36823 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36825 /* Save tokens so that we can put them back. */
36826 cp_lexer_save_tokens (parser->lexer);
36828 /* Look for ':' that is not nested in () or {}. */
36829 bool is_range_for
36830 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36831 /*recovering=*/false,
36832 CPP_COLON,
36833 /*consume_paren=*/
36834 false) == -1);
36836 /* Roll back the tokens we skipped. */
36837 cp_lexer_rollback_tokens (parser->lexer);
36839 if (is_range_for)
36841 bool saved_colon_corrects_to_scope_p
36842 = parser->colon_corrects_to_scope_p;
36844 /* A colon is used in range-based for. */
36845 parser->colon_corrects_to_scope_p = false;
36847 /* Parse the declaration. */
36848 cp_parser_simple_declaration (parser,
36849 /*function_definition_allowed_p=*/
36850 false, &decl);
36851 parser->colon_corrects_to_scope_p
36852 = saved_colon_corrects_to_scope_p;
36854 cp_parser_require (parser, CPP_COLON, RT_COLON);
36856 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36857 false, 0, true);
36859 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36860 orig_decl, init, orig_init,
36861 cond, incr);
36862 if (this_pre_body)
36864 if (pre_body)
36866 tree t = pre_body;
36867 pre_body = push_stmt_list ();
36868 add_stmt (t);
36869 add_stmt (this_pre_body);
36870 pre_body = pop_stmt_list (pre_body);
36872 else
36873 pre_body = this_pre_body;
36876 if (ordered_cl)
36877 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36878 "%<ordered%> clause with parameter on "
36879 "range-based %<for%> loop");
36881 goto parse_close_paren;
36885 add_private_clause
36886 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36887 init, orig_init, decl, real_decl);
36889 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36890 if (this_pre_body)
36892 this_pre_body = pop_stmt_list (this_pre_body);
36893 if (pre_body)
36895 tree t = pre_body;
36896 pre_body = push_stmt_list ();
36897 add_stmt (t);
36898 add_stmt (this_pre_body);
36899 pre_body = pop_stmt_list (pre_body);
36901 else
36902 pre_body = this_pre_body;
36905 if (decl)
36906 real_decl = decl;
36907 if (cclauses != NULL
36908 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36909 && real_decl != NULL_TREE)
36911 tree *c;
36912 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36913 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36914 && OMP_CLAUSE_DECL (*c) == real_decl)
36916 error_at (loc, "iteration variable %qD"
36917 " should not be firstprivate", real_decl);
36918 *c = OMP_CLAUSE_CHAIN (*c);
36920 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36921 && OMP_CLAUSE_DECL (*c) == real_decl)
36923 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36924 tree l = *c;
36925 *c = OMP_CLAUSE_CHAIN (*c);
36926 if (code == OMP_SIMD)
36928 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36929 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36931 else
36933 OMP_CLAUSE_CHAIN (l) = clauses;
36934 clauses = l;
36936 add_private_clause = NULL_TREE;
36938 else
36940 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36941 && OMP_CLAUSE_DECL (*c) == real_decl)
36942 add_private_clause = NULL_TREE;
36943 c = &OMP_CLAUSE_CHAIN (*c);
36947 if (add_private_clause)
36949 tree c;
36950 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36952 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36953 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36954 && OMP_CLAUSE_DECL (c) == decl)
36955 break;
36956 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36957 && OMP_CLAUSE_DECL (c) == decl)
36958 error_at (loc, "iteration variable %qD "
36959 "should not be firstprivate",
36960 decl);
36961 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36962 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36963 && OMP_CLAUSE_DECL (c) == decl)
36964 error_at (loc, "iteration variable %qD should not be reduction",
36965 decl);
36967 if (c == NULL)
36969 if (code != OMP_SIMD)
36970 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36971 else if (collapse == 1)
36972 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36973 else
36974 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36975 OMP_CLAUSE_DECL (c) = add_private_clause;
36976 c = finish_omp_clauses (c, C_ORT_OMP);
36977 if (c)
36979 OMP_CLAUSE_CHAIN (c) = clauses;
36980 clauses = c;
36981 /* For linear, signal that we need to fill up
36982 the so far unknown linear step. */
36983 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36984 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36989 cond = NULL;
36990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36991 cond = cp_parser_omp_for_cond (parser, decl, code);
36992 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36994 incr = NULL;
36995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36997 /* If decl is an iterator, preserve the operator on decl
36998 until finish_omp_for. */
36999 if (real_decl
37000 && ((processing_template_decl
37001 && (TREE_TYPE (real_decl) == NULL_TREE
37002 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37003 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37004 incr = cp_parser_omp_for_incr (parser, real_decl);
37005 else
37006 incr = cp_parser_expression (parser);
37007 if (!EXPR_HAS_LOCATION (incr))
37008 protected_set_expr_location (incr, input_location);
37011 parse_close_paren:
37012 if (!parens.require_close (parser))
37013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37014 /*or_comma=*/false,
37015 /*consume_paren=*/true);
37017 TREE_VEC_ELT (declv, i) = decl;
37018 TREE_VEC_ELT (initv, i) = init;
37019 TREE_VEC_ELT (condv, i) = cond;
37020 TREE_VEC_ELT (incrv, i) = incr;
37021 if (orig_init)
37023 orig_inits.safe_grow_cleared (i + 1);
37024 orig_inits[i] = orig_init;
37026 if (orig_decl)
37028 if (!orig_declv)
37029 orig_declv = copy_node (declv);
37030 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37032 else if (orig_declv)
37033 TREE_VEC_ELT (orig_declv, i) = decl;
37035 if (i == count - 1)
37036 break;
37038 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37039 in between the collapsed for loops to be still considered perfectly
37040 nested. Hopefully the final version clarifies this.
37041 For now handle (multiple) {'s and empty statements. */
37042 cp_parser_parse_tentatively (parser);
37043 for (;;)
37045 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37046 break;
37047 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37049 cp_lexer_consume_token (parser->lexer);
37050 bracecount++;
37052 else if (bracecount
37053 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37054 cp_lexer_consume_token (parser->lexer);
37055 else
37057 loc = cp_lexer_peek_token (parser->lexer)->location;
37058 error_at (loc, "not enough for loops to collapse");
37059 collapse_err = true;
37060 cp_parser_abort_tentative_parse (parser);
37061 declv = NULL_TREE;
37062 break;
37066 if (declv)
37068 cp_parser_parse_definitely (parser);
37069 nbraces += bracecount;
37073 if (nbraces)
37074 if_p = NULL;
37076 /* Note that we saved the original contents of this flag when we entered
37077 the structured block, and so we don't need to re-save it here. */
37078 parser->in_statement = IN_OMP_FOR;
37080 /* Note that the grammar doesn't call for a structured block here,
37081 though the loop as a whole is a structured block. */
37082 if (orig_declv)
37084 body = begin_omp_structured_block ();
37085 for (i = 0; i < count; i++)
37086 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37087 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37088 TREE_VEC_ELT (declv, i));
37090 else
37091 body = push_stmt_list ();
37092 cp_parser_statement (parser, NULL_TREE, false, if_p);
37093 if (orig_declv)
37094 body = finish_omp_structured_block (body);
37095 else
37096 body = pop_stmt_list (body);
37098 if (declv == NULL_TREE)
37099 ret = NULL_TREE;
37100 else
37101 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37102 incrv, body, pre_body, &orig_inits, clauses);
37104 while (nbraces)
37106 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37108 cp_lexer_consume_token (parser->lexer);
37109 nbraces--;
37111 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37112 cp_lexer_consume_token (parser->lexer);
37113 else
37115 if (!collapse_err)
37117 error_at (cp_lexer_peek_token (parser->lexer)->location,
37118 "collapsed loops not perfectly nested");
37120 collapse_err = true;
37121 cp_parser_statement_seq_opt (parser, NULL);
37122 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37123 break;
37127 while (!for_block->is_empty ())
37129 tree t = for_block->pop ();
37130 if (TREE_CODE (t) == STATEMENT_LIST)
37131 add_stmt (pop_stmt_list (t));
37132 else
37133 add_stmt (t);
37135 release_tree_vector (for_block);
37137 return ret;
37140 /* Helper function for OpenMP parsing, split clauses and call
37141 finish_omp_clauses on each of the set of clauses afterwards. */
37143 static void
37144 cp_omp_split_clauses (location_t loc, enum tree_code code,
37145 omp_clause_mask mask, tree clauses, tree *cclauses)
37147 int i;
37148 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37149 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37150 if (cclauses[i])
37151 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37154 /* OpenMP 4.0:
37155 #pragma omp simd simd-clause[optseq] new-line
37156 for-loop */
37158 #define OMP_SIMD_CLAUSE_MASK \
37159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37170 static tree
37171 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37172 char *p_name, omp_clause_mask mask, tree *cclauses,
37173 bool *if_p)
37175 tree clauses, sb, ret;
37176 unsigned int save;
37177 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37179 strcat (p_name, " simd");
37180 mask |= OMP_SIMD_CLAUSE_MASK;
37182 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37183 cclauses == NULL);
37184 if (cclauses)
37186 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37187 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37188 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37189 OMP_CLAUSE_ORDERED);
37190 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37192 error_at (OMP_CLAUSE_LOCATION (c),
37193 "%<ordered%> clause with parameter may not be specified "
37194 "on %qs construct", p_name);
37195 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37199 keep_next_level (true);
37200 sb = begin_omp_structured_block ();
37201 save = cp_parser_begin_omp_structured_block (parser);
37203 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37205 cp_parser_end_omp_structured_block (parser, save);
37206 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37208 return ret;
37211 /* OpenMP 2.5:
37212 #pragma omp for for-clause[optseq] new-line
37213 for-loop
37215 OpenMP 4.0:
37216 #pragma omp for simd for-simd-clause[optseq] new-line
37217 for-loop */
37219 #define OMP_FOR_CLAUSE_MASK \
37220 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37230 static tree
37231 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37232 char *p_name, omp_clause_mask mask, tree *cclauses,
37233 bool *if_p)
37235 tree clauses, sb, ret;
37236 unsigned int save;
37237 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37239 strcat (p_name, " for");
37240 mask |= OMP_FOR_CLAUSE_MASK;
37241 /* parallel for{, simd} disallows nowait clause, but for
37242 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37243 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37244 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37245 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37246 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37247 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37252 const char *p = IDENTIFIER_POINTER (id);
37254 if (strcmp (p, "simd") == 0)
37256 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37257 if (cclauses == NULL)
37258 cclauses = cclauses_buf;
37260 cp_lexer_consume_token (parser->lexer);
37261 if (!flag_openmp) /* flag_openmp_simd */
37262 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37263 cclauses, if_p);
37264 sb = begin_omp_structured_block ();
37265 save = cp_parser_begin_omp_structured_block (parser);
37266 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37267 cclauses, if_p);
37268 cp_parser_end_omp_structured_block (parser, save);
37269 tree body = finish_omp_structured_block (sb);
37270 if (ret == NULL)
37271 return ret;
37272 ret = make_node (OMP_FOR);
37273 TREE_TYPE (ret) = void_type_node;
37274 OMP_FOR_BODY (ret) = body;
37275 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37276 SET_EXPR_LOCATION (ret, loc);
37277 add_stmt (ret);
37278 return ret;
37281 if (!flag_openmp) /* flag_openmp_simd */
37283 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37284 return NULL_TREE;
37287 /* Composite distribute parallel for disallows linear clause. */
37288 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37289 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37291 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37292 cclauses == NULL);
37293 if (cclauses)
37295 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37296 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37299 keep_next_level (true);
37300 sb = begin_omp_structured_block ();
37301 save = cp_parser_begin_omp_structured_block (parser);
37303 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37305 cp_parser_end_omp_structured_block (parser, save);
37306 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37308 return ret;
37311 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37312 omp_clause_mask, tree *, bool *);
37314 /* OpenMP 2.5:
37315 # pragma omp master new-line
37316 structured-block */
37318 static tree
37319 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37320 char *p_name, omp_clause_mask mask, tree *cclauses,
37321 bool *if_p)
37323 tree clauses, sb, ret;
37324 unsigned int save;
37325 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37327 strcat (p_name, " master");
37329 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37331 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37332 const char *p = IDENTIFIER_POINTER (id);
37334 if (strcmp (p, "taskloop") == 0)
37336 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37337 if (cclauses == NULL)
37338 cclauses = cclauses_buf;
37340 cp_lexer_consume_token (parser->lexer);
37341 if (!flag_openmp) /* flag_openmp_simd */
37342 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37343 cclauses, if_p);
37344 sb = begin_omp_structured_block ();
37345 save = cp_parser_begin_omp_structured_block (parser);
37346 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37347 cclauses, if_p);
37348 cp_parser_end_omp_structured_block (parser, save);
37349 tree body = finish_omp_structured_block (sb);
37350 if (ret == NULL)
37351 return ret;
37352 return c_finish_omp_master (loc, body);
37355 if (!flag_openmp) /* flag_openmp_simd */
37357 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37358 return NULL_TREE;
37361 if (cclauses)
37363 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37364 false);
37365 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37367 else
37368 cp_parser_require_pragma_eol (parser, pragma_tok);
37370 return c_finish_omp_master (loc,
37371 cp_parser_omp_structured_block (parser, if_p));
37374 /* OpenMP 2.5:
37375 # pragma omp ordered new-line
37376 structured-block
37378 OpenMP 4.5:
37379 # pragma omp ordered ordered-clauses new-line
37380 structured-block */
37382 #define OMP_ORDERED_CLAUSE_MASK \
37383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37386 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37387 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37389 static bool
37390 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37391 enum pragma_context context, bool *if_p)
37393 location_t loc = pragma_tok->location;
37395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37397 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37398 const char *p = IDENTIFIER_POINTER (id);
37400 if (strcmp (p, "depend") == 0)
37402 if (!flag_openmp) /* flag_openmp_simd */
37404 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37405 return false;
37407 if (context == pragma_stmt)
37409 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37410 "%<depend%> clause may only be used in compound "
37411 "statements");
37412 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37413 return false;
37415 tree clauses
37416 = cp_parser_omp_all_clauses (parser,
37417 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37418 "#pragma omp ordered", pragma_tok);
37419 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37420 return false;
37424 tree clauses
37425 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37426 "#pragma omp ordered", pragma_tok);
37428 if (!flag_openmp /* flag_openmp_simd */
37429 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37430 return false;
37432 c_finish_omp_ordered (loc, clauses,
37433 cp_parser_omp_structured_block (parser, if_p));
37434 return true;
37437 /* OpenMP 2.5:
37439 section-scope:
37440 { section-sequence }
37442 section-sequence:
37443 section-directive[opt] structured-block
37444 section-sequence section-directive structured-block */
37446 static tree
37447 cp_parser_omp_sections_scope (cp_parser *parser)
37449 tree stmt, substmt;
37450 bool error_suppress = false;
37451 cp_token *tok;
37453 matching_braces braces;
37454 if (!braces.require_open (parser))
37455 return NULL_TREE;
37457 stmt = push_stmt_list ();
37459 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37460 != PRAGMA_OMP_SECTION)
37462 substmt = cp_parser_omp_structured_block (parser, NULL);
37463 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37464 add_stmt (substmt);
37467 while (1)
37469 tok = cp_lexer_peek_token (parser->lexer);
37470 if (tok->type == CPP_CLOSE_BRACE)
37471 break;
37472 if (tok->type == CPP_EOF)
37473 break;
37475 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37477 cp_lexer_consume_token (parser->lexer);
37478 cp_parser_require_pragma_eol (parser, tok);
37479 error_suppress = false;
37481 else if (!error_suppress)
37483 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37484 error_suppress = true;
37487 substmt = cp_parser_omp_structured_block (parser, NULL);
37488 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37489 add_stmt (substmt);
37491 braces.require_close (parser);
37493 substmt = pop_stmt_list (stmt);
37495 stmt = make_node (OMP_SECTIONS);
37496 TREE_TYPE (stmt) = void_type_node;
37497 OMP_SECTIONS_BODY (stmt) = substmt;
37499 add_stmt (stmt);
37500 return stmt;
37503 /* OpenMP 2.5:
37504 # pragma omp sections sections-clause[optseq] newline
37505 sections-scope */
37507 #define OMP_SECTIONS_CLAUSE_MASK \
37508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37514 static tree
37515 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37516 char *p_name, omp_clause_mask mask, tree *cclauses)
37518 tree clauses, ret;
37519 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37521 strcat (p_name, " sections");
37522 mask |= OMP_SECTIONS_CLAUSE_MASK;
37523 if (cclauses)
37524 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37526 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37527 cclauses == NULL);
37528 if (cclauses)
37530 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37531 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37534 ret = cp_parser_omp_sections_scope (parser);
37535 if (ret)
37536 OMP_SECTIONS_CLAUSES (ret) = clauses;
37538 return ret;
37541 /* OpenMP 2.5:
37542 # pragma omp parallel parallel-clause[optseq] new-line
37543 structured-block
37544 # pragma omp parallel for parallel-for-clause[optseq] new-line
37545 structured-block
37546 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37547 structured-block
37549 OpenMP 4.0:
37550 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37551 structured-block */
37553 #define OMP_PARALLEL_CLAUSE_MASK \
37554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37564 static tree
37565 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37566 char *p_name, omp_clause_mask mask, tree *cclauses,
37567 bool *if_p)
37569 tree stmt, clauses, block;
37570 unsigned int save;
37571 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37573 strcat (p_name, " parallel");
37574 mask |= OMP_PARALLEL_CLAUSE_MASK;
37575 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37576 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37577 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37578 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37582 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37583 if (cclauses == NULL)
37584 cclauses = cclauses_buf;
37586 cp_lexer_consume_token (parser->lexer);
37587 if (!flag_openmp) /* flag_openmp_simd */
37588 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37589 if_p);
37590 block = begin_omp_parallel ();
37591 save = cp_parser_begin_omp_structured_block (parser);
37592 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37593 if_p);
37594 cp_parser_end_omp_structured_block (parser, save);
37595 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37596 block);
37597 if (ret == NULL_TREE)
37598 return ret;
37599 OMP_PARALLEL_COMBINED (stmt) = 1;
37600 return stmt;
37602 /* When combined with distribute, parallel has to be followed by for.
37603 #pragma omp target parallel is allowed though. */
37604 else if (cclauses
37605 && (mask & (OMP_CLAUSE_MASK_1
37606 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37608 error_at (loc, "expected %<for%> after %qs", p_name);
37609 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37610 return NULL_TREE;
37612 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37614 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37615 const char *p = IDENTIFIER_POINTER (id);
37616 if (strcmp (p, "master") == 0)
37618 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37619 cclauses = cclauses_buf;
37621 cp_lexer_consume_token (parser->lexer);
37622 block = begin_omp_parallel ();
37623 save = cp_parser_begin_omp_structured_block (parser);
37624 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37625 cclauses, if_p);
37626 cp_parser_end_omp_structured_block (parser, save);
37627 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37628 block);
37629 OMP_PARALLEL_COMBINED (stmt) = 1;
37630 if (ret == NULL_TREE)
37631 return ret;
37632 return stmt;
37634 else if (!flag_openmp) /* flag_openmp_simd */
37636 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37637 return NULL_TREE;
37639 else if (strcmp (p, "sections") == 0)
37641 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37642 cclauses = cclauses_buf;
37644 cp_lexer_consume_token (parser->lexer);
37645 block = begin_omp_parallel ();
37646 save = cp_parser_begin_omp_structured_block (parser);
37647 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37648 cp_parser_end_omp_structured_block (parser, save);
37649 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37650 block);
37651 OMP_PARALLEL_COMBINED (stmt) = 1;
37652 return stmt;
37655 else if (!flag_openmp) /* flag_openmp_simd */
37657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37658 return NULL_TREE;
37661 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37662 cclauses == NULL);
37663 if (cclauses)
37665 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37666 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37669 block = begin_omp_parallel ();
37670 save = cp_parser_begin_omp_structured_block (parser);
37671 cp_parser_statement (parser, NULL_TREE, false, if_p);
37672 cp_parser_end_omp_structured_block (parser, save);
37673 stmt = finish_omp_parallel (clauses, block);
37674 return stmt;
37677 /* OpenMP 2.5:
37678 # pragma omp single single-clause[optseq] new-line
37679 structured-block */
37681 #define OMP_SINGLE_CLAUSE_MASK \
37682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37687 static tree
37688 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37690 tree stmt = make_node (OMP_SINGLE);
37691 TREE_TYPE (stmt) = void_type_node;
37692 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37694 OMP_SINGLE_CLAUSES (stmt)
37695 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37696 "#pragma omp single", pragma_tok);
37697 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37699 return add_stmt (stmt);
37702 /* OpenMP 3.0:
37703 # pragma omp task task-clause[optseq] new-line
37704 structured-block */
37706 #define OMP_TASK_CLAUSE_MASK \
37707 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37719 static tree
37720 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37722 tree clauses, block;
37723 unsigned int save;
37725 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37726 "#pragma omp task", pragma_tok);
37727 block = begin_omp_task ();
37728 save = cp_parser_begin_omp_structured_block (parser);
37729 cp_parser_statement (parser, NULL_TREE, false, if_p);
37730 cp_parser_end_omp_structured_block (parser, save);
37731 return finish_omp_task (clauses, block);
37734 /* OpenMP 3.0:
37735 # pragma omp taskwait new-line
37737 OpenMP 5.0:
37738 # pragma omp taskwait taskwait-clause[opt] new-line */
37740 #define OMP_TASKWAIT_CLAUSE_MASK \
37741 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37743 static void
37744 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37746 tree clauses
37747 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37748 "#pragma omp taskwait", pragma_tok);
37750 if (clauses)
37752 tree stmt = make_node (OMP_TASK);
37753 TREE_TYPE (stmt) = void_node;
37754 OMP_TASK_CLAUSES (stmt) = clauses;
37755 OMP_TASK_BODY (stmt) = NULL_TREE;
37756 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37757 add_stmt (stmt);
37759 else
37760 finish_omp_taskwait ();
37763 /* OpenMP 3.1:
37764 # pragma omp taskyield new-line */
37766 static void
37767 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37769 cp_parser_require_pragma_eol (parser, pragma_tok);
37770 finish_omp_taskyield ();
37773 /* OpenMP 4.0:
37774 # pragma omp taskgroup new-line
37775 structured-block
37777 OpenMP 5.0:
37778 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37780 #define OMP_TASKGROUP_CLAUSE_MASK \
37781 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37783 static tree
37784 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37786 tree clauses
37787 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37788 "#pragma omp taskgroup", pragma_tok);
37789 return c_finish_omp_taskgroup (input_location,
37790 cp_parser_omp_structured_block (parser,
37791 if_p),
37792 clauses);
37796 /* OpenMP 2.5:
37797 # pragma omp threadprivate (variable-list) */
37799 static void
37800 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37802 tree vars;
37804 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37805 cp_parser_require_pragma_eol (parser, pragma_tok);
37807 finish_omp_threadprivate (vars);
37810 /* OpenMP 4.0:
37811 # pragma omp cancel cancel-clause[optseq] new-line */
37813 #define OMP_CANCEL_CLAUSE_MASK \
37814 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37820 static void
37821 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37823 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37824 "#pragma omp cancel", pragma_tok);
37825 finish_omp_cancel (clauses);
37828 /* OpenMP 4.0:
37829 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37831 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37837 static void
37838 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37839 enum pragma_context context)
37841 tree clauses;
37842 bool point_seen = false;
37844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37846 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37847 const char *p = IDENTIFIER_POINTER (id);
37849 if (strcmp (p, "point") == 0)
37851 cp_lexer_consume_token (parser->lexer);
37852 point_seen = true;
37855 if (!point_seen)
37857 cp_parser_error (parser, "expected %<point%>");
37858 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37859 return;
37862 if (context != pragma_compound)
37864 if (context == pragma_stmt)
37865 error_at (pragma_tok->location,
37866 "%<#pragma %s%> may only be used in compound statements",
37867 "omp cancellation point");
37868 else
37869 cp_parser_error (parser, "expected declaration specifiers");
37870 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37871 return;
37874 clauses = cp_parser_omp_all_clauses (parser,
37875 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37876 "#pragma omp cancellation point",
37877 pragma_tok);
37878 finish_omp_cancellation_point (clauses);
37881 /* OpenMP 4.0:
37882 #pragma omp distribute distribute-clause[optseq] new-line
37883 for-loop */
37885 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37892 static tree
37893 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37894 char *p_name, omp_clause_mask mask, tree *cclauses,
37895 bool *if_p)
37897 tree clauses, sb, ret;
37898 unsigned int save;
37899 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37901 strcat (p_name, " distribute");
37902 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37906 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37907 const char *p = IDENTIFIER_POINTER (id);
37908 bool simd = false;
37909 bool parallel = false;
37911 if (strcmp (p, "simd") == 0)
37912 simd = true;
37913 else
37914 parallel = strcmp (p, "parallel") == 0;
37915 if (parallel || simd)
37917 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37918 if (cclauses == NULL)
37919 cclauses = cclauses_buf;
37920 cp_lexer_consume_token (parser->lexer);
37921 if (!flag_openmp) /* flag_openmp_simd */
37923 if (simd)
37924 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37925 cclauses, if_p);
37926 else
37927 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37928 cclauses, if_p);
37930 sb = begin_omp_structured_block ();
37931 save = cp_parser_begin_omp_structured_block (parser);
37932 if (simd)
37933 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37934 cclauses, if_p);
37935 else
37936 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37937 cclauses, if_p);
37938 cp_parser_end_omp_structured_block (parser, save);
37939 tree body = finish_omp_structured_block (sb);
37940 if (ret == NULL)
37941 return ret;
37942 ret = make_node (OMP_DISTRIBUTE);
37943 TREE_TYPE (ret) = void_type_node;
37944 OMP_FOR_BODY (ret) = body;
37945 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37946 SET_EXPR_LOCATION (ret, loc);
37947 add_stmt (ret);
37948 return ret;
37951 if (!flag_openmp) /* flag_openmp_simd */
37953 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37954 return NULL_TREE;
37957 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37958 cclauses == NULL);
37959 if (cclauses)
37961 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37962 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37965 keep_next_level (true);
37966 sb = begin_omp_structured_block ();
37967 save = cp_parser_begin_omp_structured_block (parser);
37969 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37971 cp_parser_end_omp_structured_block (parser, save);
37972 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37974 return ret;
37977 /* OpenMP 4.0:
37978 # pragma omp teams teams-clause[optseq] new-line
37979 structured-block */
37981 #define OMP_TEAMS_CLAUSE_MASK \
37982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37990 static tree
37991 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37992 char *p_name, omp_clause_mask mask, tree *cclauses,
37993 bool *if_p)
37995 tree clauses, sb, ret;
37996 unsigned int save;
37997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37999 strcat (p_name, " teams");
38000 mask |= OMP_TEAMS_CLAUSE_MASK;
38002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38005 const char *p = IDENTIFIER_POINTER (id);
38006 if (strcmp (p, "distribute") == 0)
38008 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38009 if (cclauses == NULL)
38010 cclauses = cclauses_buf;
38012 cp_lexer_consume_token (parser->lexer);
38013 if (!flag_openmp) /* flag_openmp_simd */
38014 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38015 cclauses, if_p);
38016 keep_next_level (true);
38017 sb = begin_omp_structured_block ();
38018 save = cp_parser_begin_omp_structured_block (parser);
38019 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38020 cclauses, if_p);
38021 cp_parser_end_omp_structured_block (parser, save);
38022 tree body = finish_omp_structured_block (sb);
38023 if (ret == NULL)
38024 return ret;
38025 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38026 ret = make_node (OMP_TEAMS);
38027 TREE_TYPE (ret) = void_type_node;
38028 OMP_TEAMS_CLAUSES (ret) = clauses;
38029 OMP_TEAMS_BODY (ret) = body;
38030 OMP_TEAMS_COMBINED (ret) = 1;
38031 SET_EXPR_LOCATION (ret, loc);
38032 return add_stmt (ret);
38035 if (!flag_openmp) /* flag_openmp_simd */
38037 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38038 return NULL_TREE;
38041 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38042 cclauses == NULL);
38043 if (cclauses)
38045 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38046 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38049 tree stmt = make_node (OMP_TEAMS);
38050 TREE_TYPE (stmt) = void_type_node;
38051 OMP_TEAMS_CLAUSES (stmt) = clauses;
38052 keep_next_level (true);
38053 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38054 SET_EXPR_LOCATION (stmt, loc);
38056 return add_stmt (stmt);
38059 /* OpenMP 4.0:
38060 # pragma omp target data target-data-clause[optseq] new-line
38061 structured-block */
38063 #define OMP_TARGET_DATA_CLAUSE_MASK \
38064 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38069 static tree
38070 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38072 tree clauses
38073 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38074 "#pragma omp target data", pragma_tok);
38075 int map_seen = 0;
38076 for (tree *pc = &clauses; *pc;)
38078 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38079 switch (OMP_CLAUSE_MAP_KIND (*pc))
38081 case GOMP_MAP_TO:
38082 case GOMP_MAP_ALWAYS_TO:
38083 case GOMP_MAP_FROM:
38084 case GOMP_MAP_ALWAYS_FROM:
38085 case GOMP_MAP_TOFROM:
38086 case GOMP_MAP_ALWAYS_TOFROM:
38087 case GOMP_MAP_ALLOC:
38088 map_seen = 3;
38089 break;
38090 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38091 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38092 case GOMP_MAP_ALWAYS_POINTER:
38093 break;
38094 default:
38095 map_seen |= 1;
38096 error_at (OMP_CLAUSE_LOCATION (*pc),
38097 "%<#pragma omp target data%> with map-type other "
38098 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38099 "on %<map%> clause");
38100 *pc = OMP_CLAUSE_CHAIN (*pc);
38101 continue;
38103 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38104 map_seen = 3;
38105 pc = &OMP_CLAUSE_CHAIN (*pc);
38108 if (map_seen != 3)
38110 if (map_seen == 0)
38111 error_at (pragma_tok->location,
38112 "%<#pragma omp target data%> must contain at least "
38113 "one %<map%> or %<use_device_ptr%> clause");
38114 return NULL_TREE;
38117 tree stmt = make_node (OMP_TARGET_DATA);
38118 TREE_TYPE (stmt) = void_type_node;
38119 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38121 keep_next_level (true);
38122 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38124 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38125 return add_stmt (stmt);
38128 /* OpenMP 4.5:
38129 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38130 structured-block */
38132 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38139 static tree
38140 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38141 enum pragma_context context)
38143 bool data_seen = false;
38144 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38146 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38147 const char *p = IDENTIFIER_POINTER (id);
38149 if (strcmp (p, "data") == 0)
38151 cp_lexer_consume_token (parser->lexer);
38152 data_seen = true;
38155 if (!data_seen)
38157 cp_parser_error (parser, "expected %<data%>");
38158 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38159 return NULL_TREE;
38162 if (context == pragma_stmt)
38164 error_at (pragma_tok->location,
38165 "%<#pragma %s%> may only be used in compound statements",
38166 "omp target enter data");
38167 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38168 return NULL_TREE;
38171 tree clauses
38172 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38173 "#pragma omp target enter data", pragma_tok);
38174 int map_seen = 0;
38175 for (tree *pc = &clauses; *pc;)
38177 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38178 switch (OMP_CLAUSE_MAP_KIND (*pc))
38180 case GOMP_MAP_TO:
38181 case GOMP_MAP_ALWAYS_TO:
38182 case GOMP_MAP_ALLOC:
38183 map_seen = 3;
38184 break;
38185 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38186 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38187 case GOMP_MAP_ALWAYS_POINTER:
38188 break;
38189 default:
38190 map_seen |= 1;
38191 error_at (OMP_CLAUSE_LOCATION (*pc),
38192 "%<#pragma omp target enter data%> with map-type other "
38193 "than %<to%> or %<alloc%> on %<map%> clause");
38194 *pc = OMP_CLAUSE_CHAIN (*pc);
38195 continue;
38197 pc = &OMP_CLAUSE_CHAIN (*pc);
38200 if (map_seen != 3)
38202 if (map_seen == 0)
38203 error_at (pragma_tok->location,
38204 "%<#pragma omp target enter data%> must contain at least "
38205 "one %<map%> clause");
38206 return NULL_TREE;
38209 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38210 TREE_TYPE (stmt) = void_type_node;
38211 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38212 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38213 return add_stmt (stmt);
38216 /* OpenMP 4.5:
38217 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38218 structured-block */
38220 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38227 static tree
38228 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38229 enum pragma_context context)
38231 bool data_seen = false;
38232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38234 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38235 const char *p = IDENTIFIER_POINTER (id);
38237 if (strcmp (p, "data") == 0)
38239 cp_lexer_consume_token (parser->lexer);
38240 data_seen = true;
38243 if (!data_seen)
38245 cp_parser_error (parser, "expected %<data%>");
38246 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38247 return NULL_TREE;
38250 if (context == pragma_stmt)
38252 error_at (pragma_tok->location,
38253 "%<#pragma %s%> may only be used in compound statements",
38254 "omp target exit data");
38255 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38256 return NULL_TREE;
38259 tree clauses
38260 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38261 "#pragma omp target exit data", pragma_tok);
38262 int map_seen = 0;
38263 for (tree *pc = &clauses; *pc;)
38265 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38266 switch (OMP_CLAUSE_MAP_KIND (*pc))
38268 case GOMP_MAP_FROM:
38269 case GOMP_MAP_ALWAYS_FROM:
38270 case GOMP_MAP_RELEASE:
38271 case GOMP_MAP_DELETE:
38272 map_seen = 3;
38273 break;
38274 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38275 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38276 case GOMP_MAP_ALWAYS_POINTER:
38277 break;
38278 default:
38279 map_seen |= 1;
38280 error_at (OMP_CLAUSE_LOCATION (*pc),
38281 "%<#pragma omp target exit data%> with map-type other "
38282 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38283 " clause");
38284 *pc = OMP_CLAUSE_CHAIN (*pc);
38285 continue;
38287 pc = &OMP_CLAUSE_CHAIN (*pc);
38290 if (map_seen != 3)
38292 if (map_seen == 0)
38293 error_at (pragma_tok->location,
38294 "%<#pragma omp target exit data%> must contain at least "
38295 "one %<map%> clause");
38296 return NULL_TREE;
38299 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38300 TREE_TYPE (stmt) = void_type_node;
38301 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38302 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38303 return add_stmt (stmt);
38306 /* OpenMP 4.0:
38307 # pragma omp target update target-update-clause[optseq] new-line */
38309 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38310 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38317 static bool
38318 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38319 enum pragma_context context)
38321 if (context == pragma_stmt)
38323 error_at (pragma_tok->location,
38324 "%<#pragma %s%> may only be used in compound statements",
38325 "omp target update");
38326 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38327 return false;
38330 tree clauses
38331 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38332 "#pragma omp target update", pragma_tok);
38333 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38334 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38336 error_at (pragma_tok->location,
38337 "%<#pragma omp target update%> must contain at least one "
38338 "%<from%> or %<to%> clauses");
38339 return false;
38342 tree stmt = make_node (OMP_TARGET_UPDATE);
38343 TREE_TYPE (stmt) = void_type_node;
38344 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38345 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38346 add_stmt (stmt);
38347 return false;
38350 /* OpenMP 4.0:
38351 # pragma omp target target-clause[optseq] new-line
38352 structured-block */
38354 #define OMP_TARGET_CLAUSE_MASK \
38355 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38365 static bool
38366 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38367 enum pragma_context context, bool *if_p)
38369 tree *pc = NULL, stmt;
38371 if (flag_openmp)
38372 omp_requires_mask
38373 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38375 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38377 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38378 const char *p = IDENTIFIER_POINTER (id);
38379 enum tree_code ccode = ERROR_MARK;
38381 if (strcmp (p, "teams") == 0)
38382 ccode = OMP_TEAMS;
38383 else if (strcmp (p, "parallel") == 0)
38384 ccode = OMP_PARALLEL;
38385 else if (strcmp (p, "simd") == 0)
38386 ccode = OMP_SIMD;
38387 if (ccode != ERROR_MARK)
38389 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38390 char p_name[sizeof ("#pragma omp target teams distribute "
38391 "parallel for simd")];
38393 cp_lexer_consume_token (parser->lexer);
38394 strcpy (p_name, "#pragma omp target");
38395 if (!flag_openmp) /* flag_openmp_simd */
38397 tree stmt;
38398 switch (ccode)
38400 case OMP_TEAMS:
38401 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38402 OMP_TARGET_CLAUSE_MASK,
38403 cclauses, if_p);
38404 break;
38405 case OMP_PARALLEL:
38406 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38407 OMP_TARGET_CLAUSE_MASK,
38408 cclauses, if_p);
38409 break;
38410 case OMP_SIMD:
38411 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38412 OMP_TARGET_CLAUSE_MASK,
38413 cclauses, if_p);
38414 break;
38415 default:
38416 gcc_unreachable ();
38418 return stmt != NULL_TREE;
38420 keep_next_level (true);
38421 tree sb = begin_omp_structured_block (), ret;
38422 unsigned save = cp_parser_begin_omp_structured_block (parser);
38423 switch (ccode)
38425 case OMP_TEAMS:
38426 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38427 OMP_TARGET_CLAUSE_MASK, cclauses,
38428 if_p);
38429 break;
38430 case OMP_PARALLEL:
38431 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38432 OMP_TARGET_CLAUSE_MASK, cclauses,
38433 if_p);
38434 break;
38435 case OMP_SIMD:
38436 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38437 OMP_TARGET_CLAUSE_MASK, cclauses,
38438 if_p);
38439 break;
38440 default:
38441 gcc_unreachable ();
38443 cp_parser_end_omp_structured_block (parser, save);
38444 tree body = finish_omp_structured_block (sb);
38445 if (ret == NULL_TREE)
38446 return false;
38447 if (ccode == OMP_TEAMS && !processing_template_decl)
38449 /* For combined target teams, ensure the num_teams and
38450 thread_limit clause expressions are evaluated on the host,
38451 before entering the target construct. */
38452 tree c;
38453 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38454 c; c = OMP_CLAUSE_CHAIN (c))
38455 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38456 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38457 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38459 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38460 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38461 if (expr == error_mark_node)
38462 continue;
38463 tree tmp = TARGET_EXPR_SLOT (expr);
38464 add_stmt (expr);
38465 OMP_CLAUSE_OPERAND (c, 0) = expr;
38466 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38467 OMP_CLAUSE_FIRSTPRIVATE);
38468 OMP_CLAUSE_DECL (tc) = tmp;
38469 OMP_CLAUSE_CHAIN (tc)
38470 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38471 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38474 tree stmt = make_node (OMP_TARGET);
38475 TREE_TYPE (stmt) = void_type_node;
38476 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38477 OMP_TARGET_BODY (stmt) = body;
38478 OMP_TARGET_COMBINED (stmt) = 1;
38479 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38480 add_stmt (stmt);
38481 pc = &OMP_TARGET_CLAUSES (stmt);
38482 goto check_clauses;
38484 else if (!flag_openmp) /* flag_openmp_simd */
38486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38487 return false;
38489 else if (strcmp (p, "data") == 0)
38491 cp_lexer_consume_token (parser->lexer);
38492 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38493 return true;
38495 else if (strcmp (p, "enter") == 0)
38497 cp_lexer_consume_token (parser->lexer);
38498 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38499 return false;
38501 else if (strcmp (p, "exit") == 0)
38503 cp_lexer_consume_token (parser->lexer);
38504 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38505 return false;
38507 else if (strcmp (p, "update") == 0)
38509 cp_lexer_consume_token (parser->lexer);
38510 return cp_parser_omp_target_update (parser, pragma_tok, context);
38513 if (!flag_openmp) /* flag_openmp_simd */
38515 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38516 return false;
38519 stmt = make_node (OMP_TARGET);
38520 TREE_TYPE (stmt) = void_type_node;
38522 OMP_TARGET_CLAUSES (stmt)
38523 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38524 "#pragma omp target", pragma_tok);
38525 pc = &OMP_TARGET_CLAUSES (stmt);
38526 keep_next_level (true);
38527 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38529 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38530 add_stmt (stmt);
38532 check_clauses:
38533 while (*pc)
38535 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38536 switch (OMP_CLAUSE_MAP_KIND (*pc))
38538 case GOMP_MAP_TO:
38539 case GOMP_MAP_ALWAYS_TO:
38540 case GOMP_MAP_FROM:
38541 case GOMP_MAP_ALWAYS_FROM:
38542 case GOMP_MAP_TOFROM:
38543 case GOMP_MAP_ALWAYS_TOFROM:
38544 case GOMP_MAP_ALLOC:
38545 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38546 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38547 case GOMP_MAP_ALWAYS_POINTER:
38548 break;
38549 default:
38550 error_at (OMP_CLAUSE_LOCATION (*pc),
38551 "%<#pragma omp target%> with map-type other "
38552 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38553 "on %<map%> clause");
38554 *pc = OMP_CLAUSE_CHAIN (*pc);
38555 continue;
38557 pc = &OMP_CLAUSE_CHAIN (*pc);
38559 return true;
38562 /* OpenACC 2.0:
38563 # pragma acc cache (variable-list) new-line
38566 static tree
38567 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38569 tree stmt, clauses;
38571 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38572 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38574 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38576 stmt = make_node (OACC_CACHE);
38577 TREE_TYPE (stmt) = void_type_node;
38578 OACC_CACHE_CLAUSES (stmt) = clauses;
38579 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38580 add_stmt (stmt);
38582 return stmt;
38585 /* OpenACC 2.0:
38586 # pragma acc data oacc-data-clause[optseq] new-line
38587 structured-block */
38589 #define OACC_DATA_CLAUSE_MASK \
38590 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38598 static tree
38599 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38601 tree stmt, clauses, block;
38602 unsigned int save;
38604 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38605 "#pragma acc data", pragma_tok);
38607 block = begin_omp_parallel ();
38608 save = cp_parser_begin_omp_structured_block (parser);
38609 cp_parser_statement (parser, NULL_TREE, false, if_p);
38610 cp_parser_end_omp_structured_block (parser, save);
38611 stmt = finish_oacc_data (clauses, block);
38612 return stmt;
38615 /* OpenACC 2.0:
38616 # pragma acc host_data <clauses> new-line
38617 structured-block */
38619 #define OACC_HOST_DATA_CLAUSE_MASK \
38620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38622 static tree
38623 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38625 tree stmt, clauses, block;
38626 unsigned int save;
38628 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38629 "#pragma acc host_data", pragma_tok);
38631 block = begin_omp_parallel ();
38632 save = cp_parser_begin_omp_structured_block (parser);
38633 cp_parser_statement (parser, NULL_TREE, false, if_p);
38634 cp_parser_end_omp_structured_block (parser, save);
38635 stmt = finish_oacc_host_data (clauses, block);
38636 return stmt;
38639 /* OpenACC 2.0:
38640 # pragma acc declare oacc-data-clause[optseq] new-line
38643 #define OACC_DECLARE_CLAUSE_MASK \
38644 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38653 static tree
38654 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38656 tree clauses, stmt;
38657 bool error = false;
38659 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38660 "#pragma acc declare", pragma_tok, true);
38663 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38665 error_at (pragma_tok->location,
38666 "no valid clauses specified in %<#pragma acc declare%>");
38667 return NULL_TREE;
38670 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38672 location_t loc = OMP_CLAUSE_LOCATION (t);
38673 tree decl = OMP_CLAUSE_DECL (t);
38674 if (!DECL_P (decl))
38676 error_at (loc, "array section in %<#pragma acc declare%>");
38677 error = true;
38678 continue;
38680 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38681 switch (OMP_CLAUSE_MAP_KIND (t))
38683 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38684 case GOMP_MAP_ALLOC:
38685 case GOMP_MAP_TO:
38686 case GOMP_MAP_FORCE_DEVICEPTR:
38687 case GOMP_MAP_DEVICE_RESIDENT:
38688 break;
38690 case GOMP_MAP_LINK:
38691 if (!global_bindings_p ()
38692 && (TREE_STATIC (decl)
38693 || !DECL_EXTERNAL (decl)))
38695 error_at (loc,
38696 "%qD must be a global variable in "
38697 "%<#pragma acc declare link%>",
38698 decl);
38699 error = true;
38700 continue;
38702 break;
38704 default:
38705 if (global_bindings_p ())
38707 error_at (loc, "invalid OpenACC clause at file scope");
38708 error = true;
38709 continue;
38711 if (DECL_EXTERNAL (decl))
38713 error_at (loc,
38714 "invalid use of %<extern%> variable %qD "
38715 "in %<#pragma acc declare%>", decl);
38716 error = true;
38717 continue;
38719 else if (TREE_PUBLIC (decl))
38721 error_at (loc,
38722 "invalid use of %<global%> variable %qD "
38723 "in %<#pragma acc declare%>", decl);
38724 error = true;
38725 continue;
38727 break;
38730 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38731 || lookup_attribute ("omp declare target link",
38732 DECL_ATTRIBUTES (decl)))
38734 error_at (loc, "variable %qD used more than once with "
38735 "%<#pragma acc declare%>", decl);
38736 error = true;
38737 continue;
38740 if (!error)
38742 tree id;
38744 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38745 id = get_identifier ("omp declare target link");
38746 else
38747 id = get_identifier ("omp declare target");
38749 DECL_ATTRIBUTES (decl)
38750 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38751 if (global_bindings_p ())
38753 symtab_node *node = symtab_node::get (decl);
38754 if (node != NULL)
38756 node->offloadable = 1;
38757 if (ENABLE_OFFLOADING)
38759 g->have_offload = true;
38760 if (is_a <varpool_node *> (node))
38761 vec_safe_push (offload_vars, decl);
38768 if (error || global_bindings_p ())
38769 return NULL_TREE;
38771 stmt = make_node (OACC_DECLARE);
38772 TREE_TYPE (stmt) = void_type_node;
38773 OACC_DECLARE_CLAUSES (stmt) = clauses;
38774 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38776 add_stmt (stmt);
38778 return NULL_TREE;
38781 /* OpenACC 2.0:
38782 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38786 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38788 LOC is the location of the #pragma token.
38791 #define OACC_ENTER_DATA_CLAUSE_MASK \
38792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38798 #define OACC_EXIT_DATA_CLAUSE_MASK \
38799 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38806 static tree
38807 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38808 bool enter)
38810 location_t loc = pragma_tok->location;
38811 tree stmt, clauses;
38812 const char *p = "";
38814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38815 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38817 if (strcmp (p, "data") != 0)
38819 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38820 enter ? "enter" : "exit");
38821 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38822 return NULL_TREE;
38825 cp_lexer_consume_token (parser->lexer);
38827 if (enter)
38828 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38829 "#pragma acc enter data", pragma_tok);
38830 else
38831 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38832 "#pragma acc exit data", pragma_tok);
38834 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38836 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38837 enter ? "enter" : "exit");
38838 return NULL_TREE;
38841 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38842 TREE_TYPE (stmt) = void_type_node;
38843 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38844 SET_EXPR_LOCATION (stmt, loc);
38845 add_stmt (stmt);
38846 return stmt;
38849 /* OpenACC 2.0:
38850 # pragma acc loop oacc-loop-clause[optseq] new-line
38851 structured-block */
38853 #define OACC_LOOP_CLAUSE_MASK \
38854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38865 static tree
38866 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38867 omp_clause_mask mask, tree *cclauses, bool *if_p)
38869 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38871 strcat (p_name, " loop");
38872 mask |= OACC_LOOP_CLAUSE_MASK;
38874 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38875 cclauses == NULL);
38876 if (cclauses)
38878 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38879 if (*cclauses)
38880 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38881 if (clauses)
38882 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38885 tree block = begin_omp_structured_block ();
38886 int save = cp_parser_begin_omp_structured_block (parser);
38887 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38888 cp_parser_end_omp_structured_block (parser, save);
38889 add_stmt (finish_omp_structured_block (block));
38891 return stmt;
38894 /* OpenACC 2.0:
38895 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38896 structured-block
38900 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38901 structured-block
38904 #define OACC_KERNELS_CLAUSE_MASK \
38905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38919 #define OACC_PARALLEL_CLAUSE_MASK \
38920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38937 static tree
38938 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38939 char *p_name, bool *if_p)
38941 omp_clause_mask mask;
38942 enum tree_code code;
38943 switch (cp_parser_pragma_kind (pragma_tok))
38945 case PRAGMA_OACC_KERNELS:
38946 strcat (p_name, " kernels");
38947 mask = OACC_KERNELS_CLAUSE_MASK;
38948 code = OACC_KERNELS;
38949 break;
38950 case PRAGMA_OACC_PARALLEL:
38951 strcat (p_name, " parallel");
38952 mask = OACC_PARALLEL_CLAUSE_MASK;
38953 code = OACC_PARALLEL;
38954 break;
38955 default:
38956 gcc_unreachable ();
38959 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38961 const char *p
38962 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38963 if (strcmp (p, "loop") == 0)
38965 cp_lexer_consume_token (parser->lexer);
38966 tree block = begin_omp_parallel ();
38967 tree clauses;
38968 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
38969 &clauses, if_p);
38970 protected_set_expr_location (stmt, pragma_tok->location);
38971 return finish_omp_construct (code, block, clauses);
38975 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38977 tree block = begin_omp_parallel ();
38978 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38979 cp_parser_statement (parser, NULL_TREE, false, if_p);
38980 cp_parser_end_omp_structured_block (parser, save);
38981 return finish_omp_construct (code, block, clauses);
38984 /* OpenACC 2.0:
38985 # pragma acc update oacc-update-clause[optseq] new-line
38988 #define OACC_UPDATE_CLAUSE_MASK \
38989 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38996 static tree
38997 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38999 tree stmt, clauses;
39001 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39002 "#pragma acc update", pragma_tok);
39004 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39006 error_at (pragma_tok->location,
39007 "%<#pragma acc update%> must contain at least one "
39008 "%<device%> or %<host%> or %<self%> clause");
39009 return NULL_TREE;
39012 stmt = make_node (OACC_UPDATE);
39013 TREE_TYPE (stmt) = void_type_node;
39014 OACC_UPDATE_CLAUSES (stmt) = clauses;
39015 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39016 add_stmt (stmt);
39017 return stmt;
39020 /* OpenACC 2.0:
39021 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39023 LOC is the location of the #pragma token.
39026 #define OACC_WAIT_CLAUSE_MASK \
39027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39029 static tree
39030 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39032 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39033 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39035 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39036 list = cp_parser_oacc_wait_list (parser, loc, list);
39038 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39039 "#pragma acc wait", pragma_tok);
39041 stmt = c_finish_oacc_wait (loc, list, clauses);
39042 stmt = finish_expr_stmt (stmt);
39044 return stmt;
39047 /* OpenMP 4.0:
39048 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39050 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39058 static void
39059 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39060 enum pragma_context context)
39062 bool first_p = parser->omp_declare_simd == NULL;
39063 cp_omp_declare_simd_data data;
39064 if (first_p)
39066 data.error_seen = false;
39067 data.fndecl_seen = false;
39068 data.tokens = vNULL;
39069 data.clauses = NULL_TREE;
39070 /* It is safe to take the address of a local variable; it will only be
39071 used while this scope is live. */
39072 parser->omp_declare_simd = &data;
39075 /* Store away all pragma tokens. */
39076 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39077 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39078 cp_lexer_consume_token (parser->lexer);
39079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39080 parser->omp_declare_simd->error_seen = true;
39081 cp_parser_require_pragma_eol (parser, pragma_tok);
39082 struct cp_token_cache *cp
39083 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39084 parser->omp_declare_simd->tokens.safe_push (cp);
39086 if (first_p)
39088 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39089 cp_parser_pragma (parser, context, NULL);
39090 switch (context)
39092 case pragma_external:
39093 cp_parser_declaration (parser);
39094 break;
39095 case pragma_member:
39096 cp_parser_member_declaration (parser);
39097 break;
39098 case pragma_objc_icode:
39099 cp_parser_block_declaration (parser, /*statement_p=*/false);
39100 break;
39101 default:
39102 cp_parser_declaration_statement (parser);
39103 break;
39105 if (parser->omp_declare_simd
39106 && !parser->omp_declare_simd->error_seen
39107 && !parser->omp_declare_simd->fndecl_seen)
39108 error_at (pragma_tok->location,
39109 "%<#pragma omp declare simd%> not immediately followed by "
39110 "function declaration or definition");
39111 data.tokens.release ();
39112 parser->omp_declare_simd = NULL;
39116 /* Finalize #pragma omp declare simd clauses after direct declarator has
39117 been parsed, and put that into "omp declare simd" attribute. */
39119 static tree
39120 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39122 struct cp_token_cache *ce;
39123 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39124 int i;
39126 if (!data->error_seen && data->fndecl_seen)
39128 error ("%<#pragma omp declare simd%> not immediately followed by "
39129 "a single function declaration or definition");
39130 data->error_seen = true;
39132 if (data->error_seen)
39133 return attrs;
39135 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39137 tree c, cl;
39139 cp_parser_push_lexer_for_tokens (parser, ce);
39140 parser->lexer->in_pragma = true;
39141 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39142 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39143 cp_lexer_consume_token (parser->lexer);
39144 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39145 "#pragma omp declare simd", pragma_tok);
39146 cp_parser_pop_lexer (parser);
39147 if (cl)
39148 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39149 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39150 TREE_CHAIN (c) = attrs;
39151 if (processing_template_decl)
39152 ATTR_IS_DEPENDENT (c) = 1;
39153 attrs = c;
39156 data->fndecl_seen = true;
39157 return attrs;
39161 /* OpenMP 4.0:
39162 # pragma omp declare target new-line
39163 declarations and definitions
39164 # pragma omp end declare target new-line
39166 OpenMP 4.5:
39167 # pragma omp declare target ( extended-list ) new-line
39169 # pragma omp declare target declare-target-clauses[seq] new-line */
39171 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39175 static void
39176 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39178 tree clauses = NULL_TREE;
39179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39180 clauses
39181 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39182 "#pragma omp declare target", pragma_tok);
39183 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39185 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39186 clauses);
39187 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39188 cp_parser_require_pragma_eol (parser, pragma_tok);
39190 else
39192 cp_parser_require_pragma_eol (parser, pragma_tok);
39193 scope_chain->omp_declare_target_attribute++;
39194 return;
39196 if (scope_chain->omp_declare_target_attribute)
39197 error_at (pragma_tok->location,
39198 "%<#pragma omp declare target%> with clauses in between "
39199 "%<#pragma omp declare target%> without clauses and "
39200 "%<#pragma omp end declare target%>");
39201 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39203 tree t = OMP_CLAUSE_DECL (c), id;
39204 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39205 tree at2 = lookup_attribute ("omp declare target link",
39206 DECL_ATTRIBUTES (t));
39207 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39209 id = get_identifier ("omp declare target link");
39210 std::swap (at1, at2);
39212 else
39213 id = get_identifier ("omp declare target");
39214 if (at2)
39216 error_at (OMP_CLAUSE_LOCATION (c),
39217 "%qD specified both in declare target %<link%> and %<to%>"
39218 " clauses", t);
39219 continue;
39221 if (!at1)
39223 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39224 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39225 continue;
39227 symtab_node *node = symtab_node::get (t);
39228 if (node != NULL)
39230 node->offloadable = 1;
39231 if (ENABLE_OFFLOADING)
39233 g->have_offload = true;
39234 if (is_a <varpool_node *> (node))
39235 vec_safe_push (offload_vars, t);
39242 static void
39243 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39245 const char *p = "";
39246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39248 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39249 p = IDENTIFIER_POINTER (id);
39251 if (strcmp (p, "declare") == 0)
39253 cp_lexer_consume_token (parser->lexer);
39254 p = "";
39255 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39257 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39258 p = IDENTIFIER_POINTER (id);
39260 if (strcmp (p, "target") == 0)
39261 cp_lexer_consume_token (parser->lexer);
39262 else
39264 cp_parser_error (parser, "expected %<target%>");
39265 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39266 return;
39269 else
39271 cp_parser_error (parser, "expected %<declare%>");
39272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39273 return;
39275 cp_parser_require_pragma_eol (parser, pragma_tok);
39276 if (!scope_chain->omp_declare_target_attribute)
39277 error_at (pragma_tok->location,
39278 "%<#pragma omp end declare target%> without corresponding "
39279 "%<#pragma omp declare target%>");
39280 else
39281 scope_chain->omp_declare_target_attribute--;
39284 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39285 expression and optional initializer clause of
39286 #pragma omp declare reduction. We store the expression(s) as
39287 either 3, 6 or 7 special statements inside of the artificial function's
39288 body. The first two statements are DECL_EXPRs for the artificial
39289 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39290 expression that uses those variables.
39291 If there was any INITIALIZER clause, this is followed by further statements,
39292 the fourth and fifth statements are DECL_EXPRs for the artificial
39293 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39294 constructor variant (first token after open paren is not omp_priv),
39295 then the sixth statement is a statement with the function call expression
39296 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39297 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39298 to initialize the OMP_PRIV artificial variable and there is seventh
39299 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39301 static bool
39302 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39304 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39305 gcc_assert (TYPE_REF_P (type));
39306 type = TREE_TYPE (type);
39307 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39308 DECL_ARTIFICIAL (omp_out) = 1;
39309 pushdecl (omp_out);
39310 add_decl_expr (omp_out);
39311 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39312 DECL_ARTIFICIAL (omp_in) = 1;
39313 pushdecl (omp_in);
39314 add_decl_expr (omp_in);
39315 tree combiner;
39316 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39318 keep_next_level (true);
39319 tree block = begin_omp_structured_block ();
39320 combiner = cp_parser_expression (parser);
39321 finish_expr_stmt (combiner);
39322 block = finish_omp_structured_block (block);
39323 add_stmt (block);
39325 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39326 return false;
39328 const char *p = "";
39329 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39331 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39332 p = IDENTIFIER_POINTER (id);
39335 if (strcmp (p, "initializer") == 0)
39337 cp_lexer_consume_token (parser->lexer);
39338 matching_parens parens;
39339 if (!parens.require_open (parser))
39340 return false;
39342 p = "";
39343 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39345 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39346 p = IDENTIFIER_POINTER (id);
39349 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39350 DECL_ARTIFICIAL (omp_priv) = 1;
39351 pushdecl (omp_priv);
39352 add_decl_expr (omp_priv);
39353 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39354 DECL_ARTIFICIAL (omp_orig) = 1;
39355 pushdecl (omp_orig);
39356 add_decl_expr (omp_orig);
39358 keep_next_level (true);
39359 block = begin_omp_structured_block ();
39361 bool ctor = false;
39362 if (strcmp (p, "omp_priv") == 0)
39364 bool is_direct_init, is_non_constant_init;
39365 ctor = true;
39366 cp_lexer_consume_token (parser->lexer);
39367 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39368 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39369 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39370 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39371 == CPP_CLOSE_PAREN
39372 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39373 == CPP_CLOSE_PAREN))
39375 finish_omp_structured_block (block);
39376 error ("invalid initializer clause");
39377 return false;
39379 initializer = cp_parser_initializer (parser, &is_direct_init,
39380 &is_non_constant_init);
39381 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39382 NULL_TREE, LOOKUP_ONLYCONVERTING);
39384 else
39386 cp_parser_parse_tentatively (parser);
39387 /* Don't create location wrapper nodes here. */
39388 auto_suppress_location_wrappers sentinel;
39389 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39390 /*check_dependency_p=*/true,
39391 /*template_p=*/NULL,
39392 /*declarator_p=*/false,
39393 /*optional_p=*/false);
39394 vec<tree, va_gc> *args;
39395 if (fn_name == error_mark_node
39396 || cp_parser_error_occurred (parser)
39397 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39398 || ((args = cp_parser_parenthesized_expression_list
39399 (parser, non_attr, /*cast_p=*/false,
39400 /*allow_expansion_p=*/true,
39401 /*non_constant_p=*/NULL)),
39402 cp_parser_error_occurred (parser)))
39404 finish_omp_structured_block (block);
39405 cp_parser_abort_tentative_parse (parser);
39406 cp_parser_error (parser, "expected id-expression (arguments)");
39407 return false;
39409 unsigned int i;
39410 tree arg;
39411 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39412 if (arg == omp_priv
39413 || (TREE_CODE (arg) == ADDR_EXPR
39414 && TREE_OPERAND (arg, 0) == omp_priv))
39415 break;
39416 cp_parser_abort_tentative_parse (parser);
39417 if (arg == NULL_TREE)
39418 error ("one of the initializer call arguments should be %<omp_priv%>"
39419 " or %<&omp_priv%>");
39420 initializer = cp_parser_postfix_expression (parser, false, false, false,
39421 false, NULL);
39422 finish_expr_stmt (initializer);
39425 block = finish_omp_structured_block (block);
39426 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39427 add_stmt (block);
39429 if (ctor)
39430 add_decl_expr (omp_orig);
39432 if (!parens.require_close (parser))
39433 return false;
39436 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39437 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39438 UNKNOWN_LOCATION);
39440 return true;
39443 /* OpenMP 4.0
39444 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39445 initializer-clause[opt] new-line
39447 initializer-clause:
39448 initializer (omp_priv initializer)
39449 initializer (function-name (argument-list)) */
39451 static void
39452 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39453 enum pragma_context)
39455 auto_vec<tree> types;
39456 enum tree_code reduc_code = ERROR_MARK;
39457 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39458 unsigned int i;
39459 cp_token *first_token;
39460 cp_token_cache *cp;
39461 int errs;
39462 void *p;
39464 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39465 p = obstack_alloc (&declarator_obstack, 0);
39467 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39468 goto fail;
39470 switch (cp_lexer_peek_token (parser->lexer)->type)
39472 case CPP_PLUS:
39473 reduc_code = PLUS_EXPR;
39474 break;
39475 case CPP_MULT:
39476 reduc_code = MULT_EXPR;
39477 break;
39478 case CPP_MINUS:
39479 reduc_code = MINUS_EXPR;
39480 break;
39481 case CPP_AND:
39482 reduc_code = BIT_AND_EXPR;
39483 break;
39484 case CPP_XOR:
39485 reduc_code = BIT_XOR_EXPR;
39486 break;
39487 case CPP_OR:
39488 reduc_code = BIT_IOR_EXPR;
39489 break;
39490 case CPP_AND_AND:
39491 reduc_code = TRUTH_ANDIF_EXPR;
39492 break;
39493 case CPP_OR_OR:
39494 reduc_code = TRUTH_ORIF_EXPR;
39495 break;
39496 case CPP_NAME:
39497 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39498 break;
39499 default:
39500 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39501 "%<|%>, %<&&%>, %<||%> or identifier");
39502 goto fail;
39505 if (reduc_code != ERROR_MARK)
39506 cp_lexer_consume_token (parser->lexer);
39508 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39509 if (reduc_id == error_mark_node)
39510 goto fail;
39512 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39513 goto fail;
39515 /* Types may not be defined in declare reduction type list. */
39516 const char *saved_message;
39517 saved_message = parser->type_definition_forbidden_message;
39518 parser->type_definition_forbidden_message
39519 = G_("types may not be defined in declare reduction type list");
39520 bool saved_colon_corrects_to_scope_p;
39521 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39522 parser->colon_corrects_to_scope_p = false;
39523 bool saved_colon_doesnt_start_class_def_p;
39524 saved_colon_doesnt_start_class_def_p
39525 = parser->colon_doesnt_start_class_def_p;
39526 parser->colon_doesnt_start_class_def_p = true;
39528 while (true)
39530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39531 type = cp_parser_type_id (parser);
39532 if (type == error_mark_node)
39534 else if (ARITHMETIC_TYPE_P (type)
39535 && (orig_reduc_id == NULL_TREE
39536 || (TREE_CODE (type) != COMPLEX_TYPE
39537 && (id_equal (orig_reduc_id, "min")
39538 || id_equal (orig_reduc_id, "max")))))
39539 error_at (loc, "predeclared arithmetic type %qT in "
39540 "%<#pragma omp declare reduction%>", type);
39541 else if (TREE_CODE (type) == FUNCTION_TYPE
39542 || TREE_CODE (type) == METHOD_TYPE
39543 || TREE_CODE (type) == ARRAY_TYPE)
39544 error_at (loc, "function or array type %qT in "
39545 "%<#pragma omp declare reduction%>", type);
39546 else if (TYPE_REF_P (type))
39547 error_at (loc, "reference type %qT in "
39548 "%<#pragma omp declare reduction%>", type);
39549 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39550 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39551 "%<#pragma omp declare reduction%>", type);
39552 else
39553 types.safe_push (type);
39555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39556 cp_lexer_consume_token (parser->lexer);
39557 else
39558 break;
39561 /* Restore the saved message. */
39562 parser->type_definition_forbidden_message = saved_message;
39563 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39564 parser->colon_doesnt_start_class_def_p
39565 = saved_colon_doesnt_start_class_def_p;
39567 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39568 || types.is_empty ())
39570 fail:
39571 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39572 goto done;
39575 first_token = cp_lexer_peek_token (parser->lexer);
39576 cp = NULL;
39577 errs = errorcount;
39578 FOR_EACH_VEC_ELT (types, i, type)
39580 tree fntype
39581 = build_function_type_list (void_type_node,
39582 cp_build_reference_type (type, false),
39583 NULL_TREE);
39584 tree this_reduc_id = reduc_id;
39585 if (!dependent_type_p (type))
39586 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39587 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39588 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39589 DECL_ARTIFICIAL (fndecl) = 1;
39590 DECL_EXTERNAL (fndecl) = 1;
39591 DECL_DECLARED_INLINE_P (fndecl) = 1;
39592 DECL_IGNORED_P (fndecl) = 1;
39593 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39594 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39595 DECL_ATTRIBUTES (fndecl)
39596 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39597 DECL_ATTRIBUTES (fndecl));
39598 if (processing_template_decl)
39599 fndecl = push_template_decl (fndecl);
39600 bool block_scope = false;
39601 tree block = NULL_TREE;
39602 if (current_function_decl)
39604 block_scope = true;
39605 DECL_CONTEXT (fndecl) = global_namespace;
39606 if (!processing_template_decl)
39607 pushdecl (fndecl);
39609 else if (current_class_type)
39611 if (cp == NULL)
39613 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39614 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39615 cp_lexer_consume_token (parser->lexer);
39616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39617 goto fail;
39618 cp = cp_token_cache_new (first_token,
39619 cp_lexer_peek_nth_token (parser->lexer,
39620 2));
39622 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39623 finish_member_declaration (fndecl);
39624 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39625 DECL_PENDING_INLINE_P (fndecl) = 1;
39626 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39627 continue;
39629 else
39631 DECL_CONTEXT (fndecl) = current_namespace;
39632 pushdecl (fndecl);
39634 if (!block_scope)
39635 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39636 else
39637 block = begin_omp_structured_block ();
39638 if (cp)
39640 cp_parser_push_lexer_for_tokens (parser, cp);
39641 parser->lexer->in_pragma = true;
39643 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39645 if (!block_scope)
39646 finish_function (/*inline_p=*/false);
39647 else
39648 DECL_CONTEXT (fndecl) = current_function_decl;
39649 if (cp)
39650 cp_parser_pop_lexer (parser);
39651 goto fail;
39653 if (cp)
39654 cp_parser_pop_lexer (parser);
39655 if (!block_scope)
39656 finish_function (/*inline_p=*/false);
39657 else
39659 DECL_CONTEXT (fndecl) = current_function_decl;
39660 block = finish_omp_structured_block (block);
39661 if (TREE_CODE (block) == BIND_EXPR)
39662 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39663 else if (TREE_CODE (block) == STATEMENT_LIST)
39664 DECL_SAVED_TREE (fndecl) = block;
39665 if (processing_template_decl)
39666 add_decl_expr (fndecl);
39668 cp_check_omp_declare_reduction (fndecl);
39669 if (cp == NULL && types.length () > 1)
39670 cp = cp_token_cache_new (first_token,
39671 cp_lexer_peek_nth_token (parser->lexer, 2));
39672 if (errs != errorcount)
39673 break;
39676 cp_parser_require_pragma_eol (parser, pragma_tok);
39678 done:
39679 /* Free any declarators allocated. */
39680 obstack_free (&declarator_obstack, p);
39683 /* OpenMP 4.0
39684 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39685 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39686 initializer-clause[opt] new-line
39687 #pragma omp declare target new-line */
39689 static bool
39690 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39691 enum pragma_context context)
39693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39695 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39696 const char *p = IDENTIFIER_POINTER (id);
39698 if (strcmp (p, "simd") == 0)
39700 cp_lexer_consume_token (parser->lexer);
39701 cp_parser_omp_declare_simd (parser, pragma_tok,
39702 context);
39703 return true;
39705 cp_ensure_no_omp_declare_simd (parser);
39706 if (strcmp (p, "reduction") == 0)
39708 cp_lexer_consume_token (parser->lexer);
39709 cp_parser_omp_declare_reduction (parser, pragma_tok,
39710 context);
39711 return false;
39713 if (!flag_openmp) /* flag_openmp_simd */
39715 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39716 return false;
39718 if (strcmp (p, "target") == 0)
39720 cp_lexer_consume_token (parser->lexer);
39721 cp_parser_omp_declare_target (parser, pragma_tok);
39722 return false;
39725 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39726 "or %<target%>");
39727 cp_parser_require_pragma_eol (parser, pragma_tok);
39728 return false;
39731 /* OpenMP 5.0
39732 #pragma omp requires clauses[optseq] new-line */
39734 static bool
39735 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39737 bool first = true;
39738 enum omp_requires new_req = (enum omp_requires) 0;
39740 location_t loc = pragma_tok->location;
39741 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39743 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39744 cp_lexer_consume_token (parser->lexer);
39746 first = false;
39748 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39751 const char *p = IDENTIFIER_POINTER (id);
39752 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39753 enum omp_requires this_req = (enum omp_requires) 0;
39755 if (!strcmp (p, "unified_address"))
39756 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39757 else if (!strcmp (p, "unified_shared_memory"))
39758 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39759 else if (!strcmp (p, "dynamic_allocators"))
39760 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39761 else if (!strcmp (p, "reverse_offload"))
39762 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39763 else if (!strcmp (p, "atomic_default_mem_order"))
39765 cp_lexer_consume_token (parser->lexer);
39767 matching_parens parens;
39768 if (parens.require_open (parser))
39770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39772 id = cp_lexer_peek_token (parser->lexer)->u.value;
39773 p = IDENTIFIER_POINTER (id);
39775 if (!strcmp (p, "seq_cst"))
39776 this_req
39777 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39778 else if (!strcmp (p, "relaxed"))
39779 this_req
39780 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39781 else if (!strcmp (p, "acq_rel"))
39782 this_req
39783 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39785 if (this_req == 0)
39787 error_at (cp_lexer_peek_token (parser->lexer)->location,
39788 "expected %<seq_cst%>, %<relaxed%> or "
39789 "%<acq_rel%>");
39790 if (cp_lexer_nth_token_is (parser->lexer, 2,
39791 CPP_CLOSE_PAREN))
39792 cp_lexer_consume_token (parser->lexer);
39794 else
39795 cp_lexer_consume_token (parser->lexer);
39797 if (!parens.require_close (parser))
39798 cp_parser_skip_to_closing_parenthesis (parser,
39799 /*recovering=*/true,
39800 /*or_comma=*/false,
39801 /*consume_paren=*/
39802 true);
39804 if (this_req == 0)
39806 cp_parser_require_pragma_eol (parser, pragma_tok);
39807 return false;
39810 p = NULL;
39812 else
39814 error_at (cloc, "expected %<unified_address%>, "
39815 "%<unified_shared_memory%>, "
39816 "%<dynamic_allocators%>, "
39817 "%<reverse_offload%> "
39818 "or %<atomic_default_mem_order%> clause");
39819 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39820 return false;
39822 if (p)
39823 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39824 "supported yet", p);
39825 if (p)
39826 cp_lexer_consume_token (parser->lexer);
39827 if (this_req)
39829 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39831 if ((this_req & new_req) != 0)
39832 error_at (cloc, "too many %qs clauses", p);
39833 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39834 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39835 error_at (cloc, "%qs clause used lexically after first "
39836 "target construct or offloading API", p);
39838 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39840 error_at (cloc, "too many %qs clauses",
39841 "atomic_default_mem_order");
39842 this_req = (enum omp_requires) 0;
39844 else if ((omp_requires_mask
39845 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39847 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39848 " clause in a single compilation unit");
39849 this_req
39850 = (enum omp_requires)
39851 (omp_requires_mask
39852 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39854 else if ((omp_requires_mask
39855 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39856 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39857 "lexically after first %<atomic%> construct "
39858 "without memory order clause");
39859 new_req = (enum omp_requires) (new_req | this_req);
39860 omp_requires_mask
39861 = (enum omp_requires) (omp_requires_mask | this_req);
39862 continue;
39865 break;
39867 cp_parser_require_pragma_eol (parser, pragma_tok);
39869 if (new_req == 0)
39870 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39871 return false;
39875 /* OpenMP 4.5:
39876 #pragma omp taskloop taskloop-clause[optseq] new-line
39877 for-loop
39879 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39880 for-loop */
39882 #define OMP_TASKLOOP_CLAUSE_MASK \
39883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39900 static tree
39901 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39902 char *p_name, omp_clause_mask mask, tree *cclauses,
39903 bool *if_p)
39905 tree clauses, sb, ret;
39906 unsigned int save;
39907 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39909 strcat (p_name, " taskloop");
39910 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39911 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39912 clause. */
39913 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39914 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39918 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39919 const char *p = IDENTIFIER_POINTER (id);
39921 if (strcmp (p, "simd") == 0)
39923 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39924 if (cclauses == NULL)
39925 cclauses = cclauses_buf;
39927 cp_lexer_consume_token (parser->lexer);
39928 if (!flag_openmp) /* flag_openmp_simd */
39929 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39930 cclauses, if_p);
39931 sb = begin_omp_structured_block ();
39932 save = cp_parser_begin_omp_structured_block (parser);
39933 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39934 cclauses, if_p);
39935 cp_parser_end_omp_structured_block (parser, save);
39936 tree body = finish_omp_structured_block (sb);
39937 if (ret == NULL)
39938 return ret;
39939 ret = make_node (OMP_TASKLOOP);
39940 TREE_TYPE (ret) = void_type_node;
39941 OMP_FOR_BODY (ret) = body;
39942 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39943 SET_EXPR_LOCATION (ret, loc);
39944 add_stmt (ret);
39945 return ret;
39948 if (!flag_openmp) /* flag_openmp_simd */
39950 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39951 return NULL_TREE;
39954 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39955 cclauses == NULL);
39956 if (cclauses)
39958 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39959 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39962 keep_next_level (true);
39963 sb = begin_omp_structured_block ();
39964 save = cp_parser_begin_omp_structured_block (parser);
39966 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39967 if_p);
39969 cp_parser_end_omp_structured_block (parser, save);
39970 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39972 return ret;
39976 /* OpenACC 2.0:
39977 # pragma acc routine oacc-routine-clause[optseq] new-line
39978 function-definition
39980 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39983 #define OACC_ROUTINE_CLAUSE_MASK \
39984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39990 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39991 component, which must resolve to a declared namespace-scope
39992 function. The clauses are either processed directly (for a named
39993 function), or defered until the immediatley following declaration
39994 is parsed. */
39996 static void
39997 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39998 enum pragma_context context)
40000 gcc_checking_assert (context == pragma_external);
40001 /* The checking for "another pragma following this one" in the "no optional
40002 '( name )'" case makes sure that we dont re-enter. */
40003 gcc_checking_assert (parser->oacc_routine == NULL);
40005 cp_oacc_routine_data data;
40006 data.error_seen = false;
40007 data.fndecl_seen = false;
40008 data.tokens = vNULL;
40009 data.clauses = NULL_TREE;
40010 data.loc = pragma_tok->location;
40011 /* It is safe to take the address of a local variable; it will only be
40012 used while this scope is live. */
40013 parser->oacc_routine = &data;
40015 /* Look for optional '( name )'. */
40016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40018 matching_parens parens;
40019 parens.consume_open (parser); /* '(' */
40021 /* We parse the name as an id-expression. If it resolves to
40022 anything other than a non-overloaded function at namespace
40023 scope, it's an error. */
40024 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40025 tree name = cp_parser_id_expression (parser,
40026 /*template_keyword_p=*/false,
40027 /*check_dependency_p=*/false,
40028 /*template_p=*/NULL,
40029 /*declarator_p=*/false,
40030 /*optional_p=*/false);
40031 tree decl = (identifier_p (name)
40032 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40033 : name);
40034 if (name != error_mark_node && decl == error_mark_node)
40035 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40037 if (decl == error_mark_node
40038 || !parens.require_close (parser))
40040 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40041 parser->oacc_routine = NULL;
40042 return;
40045 data.clauses
40046 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40047 "#pragma acc routine",
40048 cp_lexer_peek_token (parser->lexer));
40050 if (decl && is_overloaded_fn (decl)
40051 && (TREE_CODE (decl) != FUNCTION_DECL
40052 || DECL_FUNCTION_TEMPLATE_P (decl)))
40054 error_at (name_loc,
40055 "%<#pragma acc routine%> names a set of overloads");
40056 parser->oacc_routine = NULL;
40057 return;
40060 /* Perhaps we should use the same rule as declarations in different
40061 namespaces? */
40062 if (!DECL_NAMESPACE_SCOPE_P (decl))
40064 error_at (name_loc,
40065 "%qD does not refer to a namespace scope function", decl);
40066 parser->oacc_routine = NULL;
40067 return;
40070 if (TREE_CODE (decl) != FUNCTION_DECL)
40072 error_at (name_loc, "%qD does not refer to a function", decl);
40073 parser->oacc_routine = NULL;
40074 return;
40077 cp_finalize_oacc_routine (parser, decl, false);
40078 parser->oacc_routine = NULL;
40080 else /* No optional '( name )'. */
40082 /* Store away all pragma tokens. */
40083 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40084 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40085 cp_lexer_consume_token (parser->lexer);
40086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40087 parser->oacc_routine->error_seen = true;
40088 cp_parser_require_pragma_eol (parser, pragma_tok);
40089 struct cp_token_cache *cp
40090 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40091 parser->oacc_routine->tokens.safe_push (cp);
40093 /* Emit a helpful diagnostic if there's another pragma following this
40094 one. */
40095 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40097 cp_ensure_no_oacc_routine (parser);
40098 data.tokens.release ();
40099 /* ..., and then just keep going. */
40100 return;
40103 /* We only have to consider the pragma_external case here. */
40104 cp_parser_declaration (parser);
40105 if (parser->oacc_routine
40106 && !parser->oacc_routine->fndecl_seen)
40107 cp_ensure_no_oacc_routine (parser);
40108 else
40109 parser->oacc_routine = NULL;
40110 data.tokens.release ();
40114 /* Finalize #pragma acc routine clauses after direct declarator has
40115 been parsed. */
40117 static tree
40118 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40120 struct cp_token_cache *ce;
40121 cp_oacc_routine_data *data = parser->oacc_routine;
40123 if (!data->error_seen && data->fndecl_seen)
40125 error_at (data->loc,
40126 "%<#pragma acc routine%> not immediately followed by "
40127 "a single function declaration or definition");
40128 data->error_seen = true;
40130 if (data->error_seen)
40131 return attrs;
40133 gcc_checking_assert (data->tokens.length () == 1);
40134 ce = data->tokens[0];
40136 cp_parser_push_lexer_for_tokens (parser, ce);
40137 parser->lexer->in_pragma = true;
40138 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40140 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40141 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40142 parser->oacc_routine->clauses
40143 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40144 "#pragma acc routine", pragma_tok);
40145 cp_parser_pop_lexer (parser);
40146 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40147 fndecl_seen. */
40149 return attrs;
40152 /* Apply any saved OpenACC routine clauses to a just-parsed
40153 declaration. */
40155 static void
40156 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40158 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40160 /* Keep going if we're in error reporting mode. */
40161 if (parser->oacc_routine->error_seen
40162 || fndecl == error_mark_node)
40163 return;
40165 if (parser->oacc_routine->fndecl_seen)
40167 error_at (parser->oacc_routine->loc,
40168 "%<#pragma acc routine%> not immediately followed by"
40169 " a single function declaration or definition");
40170 parser->oacc_routine = NULL;
40171 return;
40173 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40175 cp_ensure_no_oacc_routine (parser);
40176 return;
40179 if (oacc_get_fn_attrib (fndecl))
40181 error_at (parser->oacc_routine->loc,
40182 "%<#pragma acc routine%> already applied to %qD", fndecl);
40183 parser->oacc_routine = NULL;
40184 return;
40187 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40189 error_at (parser->oacc_routine->loc,
40190 TREE_USED (fndecl)
40191 ? G_("%<#pragma acc routine%> must be applied before use")
40192 : G_("%<#pragma acc routine%> must be applied before "
40193 "definition"));
40194 parser->oacc_routine = NULL;
40195 return;
40198 /* Process the routine's dimension clauses. */
40199 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40200 oacc_replace_fn_attrib (fndecl, dims);
40202 /* Add an "omp declare target" attribute. */
40203 DECL_ATTRIBUTES (fndecl)
40204 = tree_cons (get_identifier ("omp declare target"),
40205 NULL_TREE, DECL_ATTRIBUTES (fndecl));
40207 /* Don't unset parser->oacc_routine here: we may still need it to
40208 diagnose wrong usage. But, remember that we've used this "#pragma acc
40209 routine". */
40210 parser->oacc_routine->fndecl_seen = true;
40214 /* Main entry point to OpenMP statement pragmas. */
40216 static void
40217 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40219 tree stmt;
40220 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40221 omp_clause_mask mask (0);
40223 switch (cp_parser_pragma_kind (pragma_tok))
40225 case PRAGMA_OACC_ATOMIC:
40226 cp_parser_omp_atomic (parser, pragma_tok);
40227 return;
40228 case PRAGMA_OACC_CACHE:
40229 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40230 break;
40231 case PRAGMA_OACC_DATA:
40232 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40233 break;
40234 case PRAGMA_OACC_ENTER_DATA:
40235 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40236 break;
40237 case PRAGMA_OACC_EXIT_DATA:
40238 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40239 break;
40240 case PRAGMA_OACC_HOST_DATA:
40241 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40242 break;
40243 case PRAGMA_OACC_KERNELS:
40244 case PRAGMA_OACC_PARALLEL:
40245 strcpy (p_name, "#pragma acc");
40246 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40247 if_p);
40248 break;
40249 case PRAGMA_OACC_LOOP:
40250 strcpy (p_name, "#pragma acc");
40251 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40252 if_p);
40253 break;
40254 case PRAGMA_OACC_UPDATE:
40255 stmt = cp_parser_oacc_update (parser, pragma_tok);
40256 break;
40257 case PRAGMA_OACC_WAIT:
40258 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40259 break;
40260 case PRAGMA_OMP_ATOMIC:
40261 cp_parser_omp_atomic (parser, pragma_tok);
40262 return;
40263 case PRAGMA_OMP_CRITICAL:
40264 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40265 break;
40266 case PRAGMA_OMP_DISTRIBUTE:
40267 strcpy (p_name, "#pragma omp");
40268 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40269 if_p);
40270 break;
40271 case PRAGMA_OMP_FOR:
40272 strcpy (p_name, "#pragma omp");
40273 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40274 if_p);
40275 break;
40276 case PRAGMA_OMP_MASTER:
40277 strcpy (p_name, "#pragma omp");
40278 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40279 if_p);
40280 break;
40281 case PRAGMA_OMP_PARALLEL:
40282 strcpy (p_name, "#pragma omp");
40283 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40284 if_p);
40285 break;
40286 case PRAGMA_OMP_SECTIONS:
40287 strcpy (p_name, "#pragma omp");
40288 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40289 break;
40290 case PRAGMA_OMP_SIMD:
40291 strcpy (p_name, "#pragma omp");
40292 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40293 if_p);
40294 break;
40295 case PRAGMA_OMP_SINGLE:
40296 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40297 break;
40298 case PRAGMA_OMP_TASK:
40299 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40300 break;
40301 case PRAGMA_OMP_TASKGROUP:
40302 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40303 break;
40304 case PRAGMA_OMP_TASKLOOP:
40305 strcpy (p_name, "#pragma omp");
40306 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40307 if_p);
40308 break;
40309 case PRAGMA_OMP_TEAMS:
40310 strcpy (p_name, "#pragma omp");
40311 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40312 if_p);
40313 break;
40314 default:
40315 gcc_unreachable ();
40318 protected_set_expr_location (stmt, pragma_tok->location);
40321 /* Transactional Memory parsing routines. */
40323 /* Parse a transaction attribute.
40325 txn-attribute:
40326 attribute
40327 [ [ identifier ] ]
40329 We use this instead of cp_parser_attributes_opt for transactions to avoid
40330 the pedwarn in C++98 mode. */
40332 static tree
40333 cp_parser_txn_attribute_opt (cp_parser *parser)
40335 cp_token *token;
40336 tree attr_name, attr = NULL;
40338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40339 return cp_parser_attributes_opt (parser);
40341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40342 return NULL_TREE;
40343 cp_lexer_consume_token (parser->lexer);
40344 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40345 goto error1;
40347 token = cp_lexer_peek_token (parser->lexer);
40348 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40350 token = cp_lexer_consume_token (parser->lexer);
40352 attr_name = (token->type == CPP_KEYWORD
40353 /* For keywords, use the canonical spelling,
40354 not the parsed identifier. */
40355 ? ridpointers[(int) token->keyword]
40356 : token->u.value);
40357 attr = build_tree_list (attr_name, NULL_TREE);
40359 else
40360 cp_parser_error (parser, "expected identifier");
40362 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40363 error1:
40364 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40365 return attr;
40368 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40370 transaction-statement:
40371 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40372 compound-statement
40373 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40376 static tree
40377 cp_parser_transaction (cp_parser *parser, cp_token *token)
40379 unsigned char old_in = parser->in_transaction;
40380 unsigned char this_in = 1, new_in;
40381 enum rid keyword = token->keyword;
40382 tree stmt, attrs, noex;
40384 cp_lexer_consume_token (parser->lexer);
40386 if (keyword == RID_TRANSACTION_RELAXED
40387 || keyword == RID_SYNCHRONIZED)
40388 this_in |= TM_STMT_ATTR_RELAXED;
40389 else
40391 attrs = cp_parser_txn_attribute_opt (parser);
40392 if (attrs)
40393 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40396 /* Parse a noexcept specification. */
40397 if (keyword == RID_ATOMIC_NOEXCEPT)
40398 noex = boolean_true_node;
40399 else if (keyword == RID_ATOMIC_CANCEL)
40401 /* cancel-and-throw is unimplemented. */
40402 sorry ("atomic_cancel");
40403 noex = NULL_TREE;
40405 else
40406 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40408 /* Keep track if we're in the lexical scope of an outer transaction. */
40409 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40411 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40413 parser->in_transaction = new_in;
40414 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40415 parser->in_transaction = old_in;
40417 finish_transaction_stmt (stmt, NULL, this_in, noex);
40419 return stmt;
40422 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40424 transaction-expression:
40425 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40426 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40429 static tree
40430 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40432 unsigned char old_in = parser->in_transaction;
40433 unsigned char this_in = 1;
40434 cp_token *token;
40435 tree expr, noex;
40436 bool noex_expr;
40437 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40439 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40440 || keyword == RID_TRANSACTION_RELAXED);
40442 if (!flag_tm)
40443 error_at (loc,
40444 keyword == RID_TRANSACTION_RELAXED
40445 ? G_("%<__transaction_relaxed%> without transactional memory "
40446 "support enabled")
40447 : G_("%<__transaction_atomic%> without transactional memory "
40448 "support enabled"));
40450 token = cp_parser_require_keyword (parser, keyword,
40451 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40452 : RT_TRANSACTION_RELAXED));
40453 gcc_assert (token != NULL);
40455 if (keyword == RID_TRANSACTION_RELAXED)
40456 this_in |= TM_STMT_ATTR_RELAXED;
40458 /* Set this early. This might mean that we allow transaction_cancel in
40459 an expression that we find out later actually has to be a constexpr.
40460 However, we expect that cxx_constant_value will be able to deal with
40461 this; also, if the noexcept has no constexpr, then what we parse next
40462 really is a transaction's body. */
40463 parser->in_transaction = this_in;
40465 /* Parse a noexcept specification. */
40466 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40467 true);
40469 if (!noex || !noex_expr
40470 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40472 matching_parens parens;
40473 parens.require_open (parser);
40475 expr = cp_parser_expression (parser);
40476 expr = finish_parenthesized_expr (expr);
40478 parens.require_close (parser);
40480 else
40482 /* The only expression that is available got parsed for the noexcept
40483 already. noexcept is true then. */
40484 expr = noex;
40485 noex = boolean_true_node;
40488 expr = build_transaction_expr (token->location, expr, this_in, noex);
40489 parser->in_transaction = old_in;
40491 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40492 return error_mark_node;
40494 return (flag_tm ? expr : error_mark_node);
40497 /* Parse a function-transaction-block.
40499 function-transaction-block:
40500 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40501 function-body
40502 __transaction_atomic txn-attribute[opt] function-try-block
40503 __transaction_relaxed ctor-initializer[opt] function-body
40504 __transaction_relaxed function-try-block
40507 static void
40508 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40510 unsigned char old_in = parser->in_transaction;
40511 unsigned char new_in = 1;
40512 tree compound_stmt, stmt, attrs;
40513 cp_token *token;
40515 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40516 || keyword == RID_TRANSACTION_RELAXED);
40517 token = cp_parser_require_keyword (parser, keyword,
40518 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40519 : RT_TRANSACTION_RELAXED));
40520 gcc_assert (token != NULL);
40522 if (keyword == RID_TRANSACTION_RELAXED)
40523 new_in |= TM_STMT_ATTR_RELAXED;
40524 else
40526 attrs = cp_parser_txn_attribute_opt (parser);
40527 if (attrs)
40528 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40531 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40533 parser->in_transaction = new_in;
40535 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40536 cp_parser_function_try_block (parser);
40537 else
40538 cp_parser_ctor_initializer_opt_and_function_body
40539 (parser, /*in_function_try_block=*/false);
40541 parser->in_transaction = old_in;
40543 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40546 /* Parse a __transaction_cancel statement.
40548 cancel-statement:
40549 __transaction_cancel txn-attribute[opt] ;
40550 __transaction_cancel txn-attribute[opt] throw-expression ;
40552 ??? Cancel and throw is not yet implemented. */
40554 static tree
40555 cp_parser_transaction_cancel (cp_parser *parser)
40557 cp_token *token;
40558 bool is_outer = false;
40559 tree stmt, attrs;
40561 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40562 RT_TRANSACTION_CANCEL);
40563 gcc_assert (token != NULL);
40565 attrs = cp_parser_txn_attribute_opt (parser);
40566 if (attrs)
40567 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40569 /* ??? Parse cancel-and-throw here. */
40571 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40573 if (!flag_tm)
40575 error_at (token->location, "%<__transaction_cancel%> without "
40576 "transactional memory support enabled");
40577 return error_mark_node;
40579 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40581 error_at (token->location, "%<__transaction_cancel%> within a "
40582 "%<__transaction_relaxed%>");
40583 return error_mark_node;
40585 else if (is_outer)
40587 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40588 && !is_tm_may_cancel_outer (current_function_decl))
40590 error_at (token->location, "outer %<__transaction_cancel%> not "
40591 "within outer %<__transaction_atomic%>");
40592 error_at (token->location,
40593 " or a %<transaction_may_cancel_outer%> function");
40594 return error_mark_node;
40597 else if (parser->in_transaction == 0)
40599 error_at (token->location, "%<__transaction_cancel%> not within "
40600 "%<__transaction_atomic%>");
40601 return error_mark_node;
40604 stmt = build_tm_abort_call (token->location, is_outer);
40605 add_stmt (stmt);
40607 return stmt;
40610 /* The parser. */
40612 static GTY (()) cp_parser *the_parser;
40615 /* Special handling for the first token or line in the file. The first
40616 thing in the file might be #pragma GCC pch_preprocess, which loads a
40617 PCH file, which is a GC collection point. So we need to handle this
40618 first pragma without benefit of an existing lexer structure.
40620 Always returns one token to the caller in *FIRST_TOKEN. This is
40621 either the true first token of the file, or the first token after
40622 the initial pragma. */
40624 static void
40625 cp_parser_initial_pragma (cp_token *first_token)
40627 tree name = NULL;
40629 cp_lexer_get_preprocessor_token (NULL, first_token);
40630 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40631 return;
40633 cp_lexer_get_preprocessor_token (NULL, first_token);
40634 if (first_token->type == CPP_STRING)
40636 name = first_token->u.value;
40638 cp_lexer_get_preprocessor_token (NULL, first_token);
40639 if (first_token->type != CPP_PRAGMA_EOL)
40640 error_at (first_token->location,
40641 "junk at end of %<#pragma GCC pch_preprocess%>");
40643 else
40644 error_at (first_token->location, "expected string literal");
40646 /* Skip to the end of the pragma. */
40647 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40648 cp_lexer_get_preprocessor_token (NULL, first_token);
40650 /* Now actually load the PCH file. */
40651 if (name)
40652 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40654 /* Read one more token to return to our caller. We have to do this
40655 after reading the PCH file in, since its pointers have to be
40656 live. */
40657 cp_lexer_get_preprocessor_token (NULL, first_token);
40660 /* Parse a pragma GCC ivdep. */
40662 static bool
40663 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40665 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40666 return true;
40669 /* Parse a pragma GCC unroll. */
40671 static unsigned short
40672 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40674 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40675 tree expr = cp_parser_constant_expression (parser);
40676 unsigned short unroll;
40677 expr = maybe_constant_value (expr);
40678 HOST_WIDE_INT lunroll = 0;
40679 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40680 || TREE_CODE (expr) != INTEGER_CST
40681 || (lunroll = tree_to_shwi (expr)) < 0
40682 || lunroll >= USHRT_MAX)
40684 error_at (location, "%<#pragma GCC unroll%> requires an"
40685 " assignment-expression that evaluates to a non-negative"
40686 " integral constant less than %u", USHRT_MAX);
40687 unroll = 0;
40689 else
40691 unroll = (unsigned short)lunroll;
40692 if (unroll == 0)
40693 unroll = 1;
40695 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40696 return unroll;
40699 /* Normal parsing of a pragma token. Here we can (and must) use the
40700 regular lexer. */
40702 static bool
40703 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40705 cp_token *pragma_tok;
40706 unsigned int id;
40707 tree stmt;
40708 bool ret;
40710 pragma_tok = cp_lexer_consume_token (parser->lexer);
40711 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40712 parser->lexer->in_pragma = true;
40714 id = cp_parser_pragma_kind (pragma_tok);
40715 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40716 cp_ensure_no_omp_declare_simd (parser);
40717 switch (id)
40719 case PRAGMA_GCC_PCH_PREPROCESS:
40720 error_at (pragma_tok->location,
40721 "%<#pragma GCC pch_preprocess%> must be first");
40722 break;
40724 case PRAGMA_OMP_BARRIER:
40725 switch (context)
40727 case pragma_compound:
40728 cp_parser_omp_barrier (parser, pragma_tok);
40729 return false;
40730 case pragma_stmt:
40731 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40732 "used in compound statements", "omp barrier");
40733 break;
40734 default:
40735 goto bad_stmt;
40737 break;
40739 case PRAGMA_OMP_DEPOBJ:
40740 switch (context)
40742 case pragma_compound:
40743 cp_parser_omp_depobj (parser, pragma_tok);
40744 return false;
40745 case pragma_stmt:
40746 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40747 "used in compound statements", "omp depobj");
40748 break;
40749 default:
40750 goto bad_stmt;
40752 break;
40754 case PRAGMA_OMP_FLUSH:
40755 switch (context)
40757 case pragma_compound:
40758 cp_parser_omp_flush (parser, pragma_tok);
40759 return false;
40760 case pragma_stmt:
40761 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40762 "used in compound statements", "omp flush");
40763 break;
40764 default:
40765 goto bad_stmt;
40767 break;
40769 case PRAGMA_OMP_TASKWAIT:
40770 switch (context)
40772 case pragma_compound:
40773 cp_parser_omp_taskwait (parser, pragma_tok);
40774 return false;
40775 case pragma_stmt:
40776 error_at (pragma_tok->location,
40777 "%<#pragma %s%> may only be used in compound statements",
40778 "omp taskwait");
40779 break;
40780 default:
40781 goto bad_stmt;
40783 break;
40785 case PRAGMA_OMP_TASKYIELD:
40786 switch (context)
40788 case pragma_compound:
40789 cp_parser_omp_taskyield (parser, pragma_tok);
40790 return false;
40791 case pragma_stmt:
40792 error_at (pragma_tok->location,
40793 "%<#pragma %s%> may only be used in compound statements",
40794 "omp taskyield");
40795 break;
40796 default:
40797 goto bad_stmt;
40799 break;
40801 case PRAGMA_OMP_CANCEL:
40802 switch (context)
40804 case pragma_compound:
40805 cp_parser_omp_cancel (parser, pragma_tok);
40806 return false;
40807 case pragma_stmt:
40808 error_at (pragma_tok->location,
40809 "%<#pragma %s%> may only be used in compound statements",
40810 "omp cancel");
40811 break;
40812 default:
40813 goto bad_stmt;
40815 break;
40817 case PRAGMA_OMP_CANCELLATION_POINT:
40818 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40819 return false;
40821 case PRAGMA_OMP_THREADPRIVATE:
40822 cp_parser_omp_threadprivate (parser, pragma_tok);
40823 return false;
40825 case PRAGMA_OMP_DECLARE:
40826 return cp_parser_omp_declare (parser, pragma_tok, context);
40828 case PRAGMA_OACC_DECLARE:
40829 cp_parser_oacc_declare (parser, pragma_tok);
40830 return false;
40832 case PRAGMA_OACC_ENTER_DATA:
40833 if (context == pragma_stmt)
40835 error_at (pragma_tok->location,
40836 "%<#pragma %s%> may only be used in compound statements",
40837 "acc enter data");
40838 break;
40840 else if (context != pragma_compound)
40841 goto bad_stmt;
40842 cp_parser_omp_construct (parser, pragma_tok, if_p);
40843 return true;
40845 case PRAGMA_OACC_EXIT_DATA:
40846 if (context == pragma_stmt)
40848 error_at (pragma_tok->location,
40849 "%<#pragma %s%> may only be used in compound statements",
40850 "acc exit data");
40851 break;
40853 else if (context != pragma_compound)
40854 goto bad_stmt;
40855 cp_parser_omp_construct (parser, pragma_tok, if_p);
40856 return true;
40858 case PRAGMA_OACC_ROUTINE:
40859 if (context != pragma_external)
40861 error_at (pragma_tok->location,
40862 "%<#pragma acc routine%> must be at file scope");
40863 break;
40865 cp_parser_oacc_routine (parser, pragma_tok, context);
40866 return false;
40868 case PRAGMA_OACC_UPDATE:
40869 if (context == pragma_stmt)
40871 error_at (pragma_tok->location,
40872 "%<#pragma %s%> may only be used in compound statements",
40873 "acc update");
40874 break;
40876 else if (context != pragma_compound)
40877 goto bad_stmt;
40878 cp_parser_omp_construct (parser, pragma_tok, if_p);
40879 return true;
40881 case PRAGMA_OACC_WAIT:
40882 if (context == pragma_stmt)
40884 error_at (pragma_tok->location,
40885 "%<#pragma %s%> may only be used in compound statements",
40886 "acc wait");
40887 break;
40889 else if (context != pragma_compound)
40890 goto bad_stmt;
40891 cp_parser_omp_construct (parser, pragma_tok, if_p);
40892 return true;
40894 case PRAGMA_OACC_ATOMIC:
40895 case PRAGMA_OACC_CACHE:
40896 case PRAGMA_OACC_DATA:
40897 case PRAGMA_OACC_HOST_DATA:
40898 case PRAGMA_OACC_KERNELS:
40899 case PRAGMA_OACC_PARALLEL:
40900 case PRAGMA_OACC_LOOP:
40901 case PRAGMA_OMP_ATOMIC:
40902 case PRAGMA_OMP_CRITICAL:
40903 case PRAGMA_OMP_DISTRIBUTE:
40904 case PRAGMA_OMP_FOR:
40905 case PRAGMA_OMP_MASTER:
40906 case PRAGMA_OMP_PARALLEL:
40907 case PRAGMA_OMP_SECTIONS:
40908 case PRAGMA_OMP_SIMD:
40909 case PRAGMA_OMP_SINGLE:
40910 case PRAGMA_OMP_TASK:
40911 case PRAGMA_OMP_TASKGROUP:
40912 case PRAGMA_OMP_TASKLOOP:
40913 case PRAGMA_OMP_TEAMS:
40914 if (context != pragma_stmt && context != pragma_compound)
40915 goto bad_stmt;
40916 stmt = push_omp_privatization_clauses (false);
40917 cp_parser_omp_construct (parser, pragma_tok, if_p);
40918 pop_omp_privatization_clauses (stmt);
40919 return true;
40921 case PRAGMA_OMP_REQUIRES:
40922 return cp_parser_omp_requires (parser, pragma_tok);
40924 case PRAGMA_OMP_ORDERED:
40925 if (context != pragma_stmt && context != pragma_compound)
40926 goto bad_stmt;
40927 stmt = push_omp_privatization_clauses (false);
40928 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40929 pop_omp_privatization_clauses (stmt);
40930 return ret;
40932 case PRAGMA_OMP_TARGET:
40933 if (context != pragma_stmt && context != pragma_compound)
40934 goto bad_stmt;
40935 stmt = push_omp_privatization_clauses (false);
40936 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40937 pop_omp_privatization_clauses (stmt);
40938 return ret;
40940 case PRAGMA_OMP_END_DECLARE_TARGET:
40941 cp_parser_omp_end_declare_target (parser, pragma_tok);
40942 return false;
40944 case PRAGMA_OMP_SECTION:
40945 error_at (pragma_tok->location,
40946 "%<#pragma omp section%> may only be used in "
40947 "%<#pragma omp sections%> construct");
40948 break;
40950 case PRAGMA_IVDEP:
40952 if (context == pragma_external)
40954 error_at (pragma_tok->location,
40955 "%<#pragma GCC ivdep%> must be inside a function");
40956 break;
40958 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40959 unsigned short unroll;
40960 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40961 if (tok->type == CPP_PRAGMA
40962 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40964 tok = cp_lexer_consume_token (parser->lexer);
40965 unroll = cp_parser_pragma_unroll (parser, tok);
40966 tok = cp_lexer_peek_token (the_parser->lexer);
40968 else
40969 unroll = 0;
40970 if (tok->type != CPP_KEYWORD
40971 || (tok->keyword != RID_FOR
40972 && tok->keyword != RID_WHILE
40973 && tok->keyword != RID_DO))
40975 cp_parser_error (parser, "for, while or do statement expected");
40976 return false;
40978 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40979 return true;
40982 case PRAGMA_UNROLL:
40984 if (context == pragma_external)
40986 error_at (pragma_tok->location,
40987 "%<#pragma GCC unroll%> must be inside a function");
40988 break;
40990 const unsigned short unroll
40991 = cp_parser_pragma_unroll (parser, pragma_tok);
40992 bool ivdep;
40993 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40994 if (tok->type == CPP_PRAGMA
40995 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40997 tok = cp_lexer_consume_token (parser->lexer);
40998 ivdep = cp_parser_pragma_ivdep (parser, tok);
40999 tok = cp_lexer_peek_token (the_parser->lexer);
41001 else
41002 ivdep = false;
41003 if (tok->type != CPP_KEYWORD
41004 || (tok->keyword != RID_FOR
41005 && tok->keyword != RID_WHILE
41006 && tok->keyword != RID_DO))
41008 cp_parser_error (parser, "for, while or do statement expected");
41009 return false;
41011 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41012 return true;
41015 default:
41016 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41017 c_invoke_pragma_handler (id);
41018 break;
41020 bad_stmt:
41021 cp_parser_error (parser, "expected declaration specifiers");
41022 break;
41025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41026 return false;
41029 /* The interface the pragma parsers have to the lexer. */
41031 enum cpp_ttype
41032 pragma_lex (tree *value, location_t *loc)
41034 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41035 enum cpp_ttype ret = tok->type;
41037 *value = tok->u.value;
41038 if (loc)
41039 *loc = tok->location;
41041 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41042 ret = CPP_EOF;
41043 else if (ret == CPP_STRING)
41044 *value = cp_parser_string_literal (the_parser, false, false);
41045 else
41047 if (ret == CPP_KEYWORD)
41048 ret = CPP_NAME;
41049 cp_lexer_consume_token (the_parser->lexer);
41052 return ret;
41056 /* External interface. */
41058 /* Parse one entire translation unit. */
41060 void
41061 c_parse_file (void)
41063 static bool already_called = false;
41065 if (already_called)
41066 fatal_error (input_location,
41067 "inter-module optimizations not implemented for C++");
41068 already_called = true;
41070 the_parser = cp_parser_new ();
41071 push_deferring_access_checks (flag_access_control
41072 ? dk_no_deferred : dk_no_check);
41073 cp_parser_translation_unit (the_parser);
41074 the_parser = NULL;
41076 finish_translation_unit ();
41079 /* Create an identifier for a generic parameter type (a synthesized
41080 template parameter implied by `auto' or a concept identifier). */
41082 static GTY(()) int generic_parm_count;
41083 static tree
41084 make_generic_type_name ()
41086 char buf[32];
41087 sprintf (buf, "auto:%d", ++generic_parm_count);
41088 return get_identifier (buf);
41091 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41092 (creating a new template parameter list if necessary). Returns the newly
41093 created template type parm. */
41095 static tree
41096 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41098 gcc_assert (current_binding_level->kind == sk_function_parms);
41100 /* Before committing to modifying any scope, if we're in an
41101 implicit template scope, and we're trying to synthesize a
41102 constrained parameter, try to find a previous parameter with
41103 the same name. This is the same-type rule for abbreviated
41104 function templates.
41106 NOTE: We can generate implicit parameters when tentatively
41107 parsing a nested name specifier, only to reject that parse
41108 later. However, matching the same template-id as part of a
41109 direct-declarator should generate an identical template
41110 parameter, so this rule will merge them. */
41111 if (parser->implicit_template_scope && constr)
41113 tree t = parser->implicit_template_parms;
41114 while (t)
41116 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41118 tree d = TREE_VALUE (t);
41119 if (TREE_CODE (d) == PARM_DECL)
41120 /* Return the TEMPLATE_PARM_INDEX. */
41121 d = DECL_INITIAL (d);
41122 return d;
41124 t = TREE_CHAIN (t);
41128 /* We are either continuing a function template that already contains implicit
41129 template parameters, creating a new fully-implicit function template, or
41130 extending an existing explicit function template with implicit template
41131 parameters. */
41133 cp_binding_level *const entry_scope = current_binding_level;
41135 bool become_template = false;
41136 cp_binding_level *parent_scope = 0;
41138 if (parser->implicit_template_scope)
41140 gcc_assert (parser->implicit_template_parms);
41142 current_binding_level = parser->implicit_template_scope;
41144 else
41146 /* Roll back to the existing template parameter scope (in the case of
41147 extending an explicit function template) or introduce a new template
41148 parameter scope ahead of the function parameter scope (or class scope
41149 in the case of out-of-line member definitions). The function scope is
41150 added back after template parameter synthesis below. */
41152 cp_binding_level *scope = entry_scope;
41154 while (scope->kind == sk_function_parms)
41156 parent_scope = scope;
41157 scope = scope->level_chain;
41159 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41161 /* If not defining a class, then any class scope is a scope level in
41162 an out-of-line member definition. In this case simply wind back
41163 beyond the first such scope to inject the template parameter list.
41164 Otherwise wind back to the class being defined. The latter can
41165 occur in class member friend declarations such as:
41167 class A {
41168 void foo (auto);
41170 class B {
41171 friend void A::foo (auto);
41174 The template parameter list synthesized for the friend declaration
41175 must be injected in the scope of 'B'. This can also occur in
41176 erroneous cases such as:
41178 struct A {
41179 struct B {
41180 void foo (auto);
41182 void B::foo (auto) {}
41185 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41186 but, nevertheless, the template parameter list synthesized for the
41187 declarator should be injected into the scope of 'A' as if the
41188 ill-formed template was specified explicitly. */
41190 while (scope->kind == sk_class && !scope->defining_class_p)
41192 parent_scope = scope;
41193 scope = scope->level_chain;
41197 current_binding_level = scope;
41199 if (scope->kind != sk_template_parms
41200 || !function_being_declared_is_template_p (parser))
41202 /* Introduce a new template parameter list for implicit template
41203 parameters. */
41205 become_template = true;
41207 parser->implicit_template_scope
41208 = begin_scope (sk_template_parms, NULL);
41210 ++processing_template_decl;
41212 parser->fully_implicit_function_template_p = true;
41213 ++parser->num_template_parameter_lists;
41215 else
41217 /* Synthesize implicit template parameters at the end of the explicit
41218 template parameter list. */
41220 gcc_assert (current_template_parms);
41222 parser->implicit_template_scope = scope;
41224 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41225 parser->implicit_template_parms
41226 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41230 /* Synthesize a new template parameter and track the current template
41231 parameter chain with implicit_template_parms. */
41233 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41234 tree synth_id = make_generic_type_name ();
41235 tree synth_tmpl_parm;
41236 bool non_type = false;
41238 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41239 synth_tmpl_parm
41240 = finish_template_type_parm (class_type_node, synth_id);
41241 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41242 synth_tmpl_parm
41243 = finish_constrained_template_template_parm (proto, synth_id);
41244 else
41246 synth_tmpl_parm = copy_decl (proto);
41247 DECL_NAME (synth_tmpl_parm) = synth_id;
41248 non_type = true;
41251 // Attach the constraint to the parm before processing.
41252 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41253 TREE_TYPE (node) = constr;
41254 tree new_parm
41255 = process_template_parm (parser->implicit_template_parms,
41256 input_location,
41257 node,
41258 /*non_type=*/non_type,
41259 /*param_pack=*/false);
41261 // Chain the new parameter to the list of implicit parameters.
41262 if (parser->implicit_template_parms)
41263 parser->implicit_template_parms
41264 = TREE_CHAIN (parser->implicit_template_parms);
41265 else
41266 parser->implicit_template_parms = new_parm;
41268 tree new_decl = get_local_decls ();
41269 if (non_type)
41270 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41271 new_decl = DECL_INITIAL (new_decl);
41273 /* If creating a fully implicit function template, start the new implicit
41274 template parameter list with this synthesized type, otherwise grow the
41275 current template parameter list. */
41277 if (become_template)
41279 parent_scope->level_chain = current_binding_level;
41281 tree new_parms = make_tree_vec (1);
41282 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41283 current_template_parms = tree_cons (size_int (processing_template_decl),
41284 new_parms, current_template_parms);
41286 else
41288 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41289 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41290 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41291 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41294 // If the new parameter was constrained, we need to add that to the
41295 // constraints in the template parameter list.
41296 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41298 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41299 reqs = conjoin_constraints (reqs, req);
41300 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41303 current_binding_level = entry_scope;
41305 return new_decl;
41308 /* Finish the declaration of a fully implicit function template. Such a
41309 template has no explicit template parameter list so has not been through the
41310 normal template head and tail processing. synthesize_implicit_template_parm
41311 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41312 provided if the declaration is a class member such that its template
41313 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41314 form is returned. Otherwise NULL_TREE is returned. */
41316 static tree
41317 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41319 gcc_assert (parser->fully_implicit_function_template_p);
41321 if (member_decl_opt && member_decl_opt != error_mark_node
41322 && DECL_VIRTUAL_P (member_decl_opt))
41324 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41325 "implicit templates may not be %<virtual%>");
41326 DECL_VIRTUAL_P (member_decl_opt) = false;
41329 if (member_decl_opt)
41330 member_decl_opt = finish_member_template_decl (member_decl_opt);
41331 end_template_decl ();
41333 parser->fully_implicit_function_template_p = false;
41334 parser->implicit_template_parms = 0;
41335 parser->implicit_template_scope = 0;
41336 --parser->num_template_parameter_lists;
41338 return member_decl_opt;
41341 /* Like finish_fully_implicit_template, but to be used in error
41342 recovery, rearranging scopes so that we restore the state we had
41343 before synthesize_implicit_template_parm inserted the implement
41344 template parms scope. */
41346 static void
41347 abort_fully_implicit_template (cp_parser *parser)
41349 cp_binding_level *return_to_scope = current_binding_level;
41351 if (parser->implicit_template_scope
41352 && return_to_scope != parser->implicit_template_scope)
41354 cp_binding_level *child = return_to_scope;
41355 for (cp_binding_level *scope = child->level_chain;
41356 scope != parser->implicit_template_scope;
41357 scope = child->level_chain)
41358 child = scope;
41359 child->level_chain = parser->implicit_template_scope->level_chain;
41360 parser->implicit_template_scope->level_chain = return_to_scope;
41361 current_binding_level = parser->implicit_template_scope;
41363 else
41364 return_to_scope = return_to_scope->level_chain;
41366 finish_fully_implicit_template (parser, NULL);
41368 gcc_assert (current_binding_level == return_to_scope);
41371 /* Helper function for diagnostics that have complained about things
41372 being used with 'extern "C"' linkage.
41374 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41376 void
41377 maybe_show_extern_c_location (void)
41379 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41380 inform (the_parser->innermost_linkage_specification_location,
41381 "%<extern \"C\"%> linkage started here");
41384 #include "gt-cp-parser.h"