re PR c++/86499 (lambda-expressions with capture-default are allowed at namespace...
[official-gcc.git] / gcc / cp / parser.c
blob49d476b383fb37f38d9191ac9d50373b37331a7f
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 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 "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2650 /* Concept-related syntactic transformations */
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 return token->keyword == keyword;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 return true;
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2774 switch (token_desc)
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2792 switch (token_desc)
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2848 if (token->type == CPP_PRAGMA)
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 error_at (loc, "version control conflict marker in file");
2865 expanded_location token_exploc = expand_location (token->location);
2866 /* Consume tokens until the end of the source line. */
2867 while (1)
2869 cp_lexer_consume_token (parser->lexer);
2870 cp_token *next = cp_lexer_peek_token (parser->lexer);
2871 if (next == NULL)
2872 break;
2873 expanded_location next_exploc = expand_location (next->location);
2874 if (next_exploc.file != token_exploc.file)
2875 break;
2876 if (next_exploc.line != token_exploc.line)
2877 break;
2879 return;
2883 gcc_rich_location richloc (input_location);
2885 bool added_matching_location = false;
2887 if (missing_token_desc != RT_NONE)
2889 /* Potentially supply a fix-it hint, suggesting to add the
2890 missing token immediately after the *previous* token.
2891 This may move the primary location within richloc. */
2892 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2893 location_t prev_token_loc
2894 = cp_lexer_previous_token (parser->lexer)->location;
2895 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2897 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2898 Attempt to consolidate diagnostics by printing it as a
2899 secondary range within the main diagnostic. */
2900 if (matching_location != UNKNOWN_LOCATION)
2901 added_matching_location
2902 = richloc.add_location_if_nearby (matching_location);
2905 /* Actually emit the error. */
2906 c_parse_error (gmsgid,
2907 /* Because c_parser_error does not understand
2908 CPP_KEYWORD, keywords are treated like
2909 identifiers. */
2910 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2911 token->u.value, token->flags, &richloc);
2913 if (missing_token_desc != RT_NONE)
2915 /* If we weren't able to consolidate matching_location, then
2916 print it as a secondary diagnostic. */
2917 if (matching_location != UNKNOWN_LOCATION
2918 && !added_matching_location)
2919 inform (matching_location, "to match this %qs",
2920 get_matching_symbol (missing_token_desc));
2924 /* If not parsing tentatively, issue a diagnostic of the form
2925 FILE:LINE: MESSAGE before TOKEN
2926 where TOKEN is the next token in the input stream. MESSAGE
2927 (specified by the caller) is usually of the form "expected
2928 OTHER-TOKEN". */
2930 static void
2931 cp_parser_error (cp_parser* parser, const char* gmsgid)
2933 if (!cp_parser_simulate_error (parser))
2934 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2937 /* Issue an error about name-lookup failing. NAME is the
2938 IDENTIFIER_NODE DECL is the result of
2939 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2940 the thing that we hoped to find. */
2942 static void
2943 cp_parser_name_lookup_error (cp_parser* parser,
2944 tree name,
2945 tree decl,
2946 name_lookup_error desired,
2947 location_t location)
2949 /* If name lookup completely failed, tell the user that NAME was not
2950 declared. */
2951 if (decl == error_mark_node)
2953 if (parser->scope && parser->scope != global_namespace)
2954 error_at (location, "%<%E::%E%> has not been declared",
2955 parser->scope, name);
2956 else if (parser->scope == global_namespace)
2957 error_at (location, "%<::%E%> has not been declared", name);
2958 else if (parser->object_scope
2959 && !CLASS_TYPE_P (parser->object_scope))
2960 error_at (location, "request for member %qE in non-class type %qT",
2961 name, parser->object_scope);
2962 else if (parser->object_scope)
2963 error_at (location, "%<%T::%E%> has not been declared",
2964 parser->object_scope, name);
2965 else
2966 error_at (location, "%qE has not been declared", name);
2968 else if (parser->scope && parser->scope != global_namespace)
2970 switch (desired)
2972 case NLE_TYPE:
2973 error_at (location, "%<%E::%E%> is not a type",
2974 parser->scope, name);
2975 break;
2976 case NLE_CXX98:
2977 error_at (location, "%<%E::%E%> is not a class or namespace",
2978 parser->scope, name);
2979 break;
2980 case NLE_NOT_CXX98:
2981 error_at (location,
2982 "%<%E::%E%> is not a class, namespace, or enumeration",
2983 parser->scope, name);
2984 break;
2985 default:
2986 gcc_unreachable ();
2990 else if (parser->scope == global_namespace)
2992 switch (desired)
2994 case NLE_TYPE:
2995 error_at (location, "%<::%E%> is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%<::%E%> is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%<::%E%> is not a class, namespace, or enumeration",
3003 name);
3004 break;
3005 default:
3006 gcc_unreachable ();
3009 else
3011 switch (desired)
3013 case NLE_TYPE:
3014 error_at (location, "%qE is not a type", name);
3015 break;
3016 case NLE_CXX98:
3017 error_at (location, "%qE is not a class or namespace", name);
3018 break;
3019 case NLE_NOT_CXX98:
3020 error_at (location,
3021 "%qE is not a class, namespace, or enumeration", name);
3022 break;
3023 default:
3024 gcc_unreachable ();
3029 /* If we are parsing tentatively, remember that an error has occurred
3030 during this tentative parse. Returns true if the error was
3031 simulated; false if a message should be issued by the caller. */
3033 static bool
3034 cp_parser_simulate_error (cp_parser* parser)
3036 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3038 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3039 return true;
3041 return false;
3044 /* This function is called when a type is defined. If type
3045 definitions are forbidden at this point, an error message is
3046 issued. */
3048 static bool
3049 cp_parser_check_type_definition (cp_parser* parser)
3051 /* If types are forbidden here, issue a message. */
3052 if (parser->type_definition_forbidden_message)
3054 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3055 in the message need to be interpreted. */
3056 error (parser->type_definition_forbidden_message);
3057 return false;
3059 return true;
3062 /* This function is called when the DECLARATOR is processed. The TYPE
3063 was a type defined in the decl-specifiers. If it is invalid to
3064 define a type in the decl-specifiers for DECLARATOR, an error is
3065 issued. TYPE_LOCATION is the location of TYPE and is used
3066 for error reporting. */
3068 static void
3069 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3070 tree type, location_t type_location)
3072 /* [dcl.fct] forbids type definitions in return types.
3073 Unfortunately, it's not easy to know whether or not we are
3074 processing a return type until after the fact. */
3075 while (declarator
3076 && (declarator->kind == cdk_pointer
3077 || declarator->kind == cdk_reference
3078 || declarator->kind == cdk_ptrmem))
3079 declarator = declarator->declarator;
3080 if (declarator
3081 && declarator->kind == cdk_function)
3083 error_at (type_location,
3084 "new types may not be defined in a return type");
3085 inform (type_location,
3086 "(perhaps a semicolon is missing after the definition of %qT)",
3087 type);
3091 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3092 "<" in any valid C++ program. If the next token is indeed "<",
3093 issue a message warning the user about what appears to be an
3094 invalid attempt to form a template-id. LOCATION is the location
3095 of the type-specifier (TYPE) */
3097 static void
3098 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3099 tree type,
3100 enum tag_types tag_type,
3101 location_t location)
3103 cp_token_position start = 0;
3105 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3107 if (TREE_CODE (type) == TYPE_DECL)
3108 type = TREE_TYPE (type);
3109 if (TYPE_P (type) && !template_placeholder_p (type))
3110 error_at (location, "%qT is not a template", type);
3111 else if (identifier_p (type))
3113 if (tag_type != none_type)
3114 error_at (location, "%qE is not a class template", type);
3115 else
3116 error_at (location, "%qE is not a template", type);
3118 else
3119 error_at (location, "invalid template-id");
3120 /* Remember the location of the invalid "<". */
3121 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3122 start = cp_lexer_token_position (parser->lexer, true);
3123 /* Consume the "<". */
3124 cp_lexer_consume_token (parser->lexer);
3125 /* Parse the template arguments. */
3126 cp_parser_enclosed_template_argument_list (parser);
3127 /* Permanently remove the invalid template arguments so that
3128 this error message is not issued again. */
3129 if (start)
3130 cp_lexer_purge_tokens_after (parser->lexer, start);
3134 /* If parsing an integral constant-expression, issue an error message
3135 about the fact that THING appeared and return true. Otherwise,
3136 return false. In either case, set
3137 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3139 static bool
3140 cp_parser_non_integral_constant_expression (cp_parser *parser,
3141 non_integral_constant thing)
3143 parser->non_integral_constant_expression_p = true;
3144 if (parser->integral_constant_expression_p)
3146 if (!parser->allow_non_integral_constant_expression_p)
3148 const char *msg = NULL;
3149 switch (thing)
3151 case NIC_FLOAT:
3152 pedwarn (input_location, OPT_Wpedantic,
3153 "ISO C++ forbids using a floating-point literal "
3154 "in a constant-expression");
3155 return true;
3156 case NIC_CAST:
3157 error ("a cast to a type other than an integral or "
3158 "enumeration type cannot appear in a "
3159 "constant-expression");
3160 return true;
3161 case NIC_TYPEID:
3162 error ("%<typeid%> operator "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_NCC:
3166 error ("non-constant compound literals "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_FUNC_CALL:
3170 error ("a function call "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_INC:
3174 error ("an increment "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_DEC:
3178 error ("an decrement "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_ARRAY_REF:
3182 error ("an array reference "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_ADDR_LABEL:
3186 error ("the address of a label "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_OVERLOADED:
3190 error ("calls to overloaded operators "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_ASSIGNMENT:
3194 error ("an assignment cannot appear in a constant-expression");
3195 return true;
3196 case NIC_COMMA:
3197 error ("a comma operator "
3198 "cannot appear in a constant-expression");
3199 return true;
3200 case NIC_CONSTRUCTOR:
3201 error ("a call to a constructor "
3202 "cannot appear in a constant-expression");
3203 return true;
3204 case NIC_TRANSACTION:
3205 error ("a transaction expression "
3206 "cannot appear in a constant-expression");
3207 return true;
3208 case NIC_THIS:
3209 msg = "this";
3210 break;
3211 case NIC_FUNC_NAME:
3212 msg = "__FUNCTION__";
3213 break;
3214 case NIC_PRETTY_FUNC:
3215 msg = "__PRETTY_FUNCTION__";
3216 break;
3217 case NIC_C99_FUNC:
3218 msg = "__func__";
3219 break;
3220 case NIC_VA_ARG:
3221 msg = "va_arg";
3222 break;
3223 case NIC_ARROW:
3224 msg = "->";
3225 break;
3226 case NIC_POINT:
3227 msg = ".";
3228 break;
3229 case NIC_STAR:
3230 msg = "*";
3231 break;
3232 case NIC_ADDR:
3233 msg = "&";
3234 break;
3235 case NIC_PREINCREMENT:
3236 msg = "++";
3237 break;
3238 case NIC_PREDECREMENT:
3239 msg = "--";
3240 break;
3241 case NIC_NEW:
3242 msg = "new";
3243 break;
3244 case NIC_DEL:
3245 msg = "delete";
3246 break;
3247 default:
3248 gcc_unreachable ();
3250 if (msg)
3251 error ("%qs cannot appear in a constant-expression", msg);
3252 return true;
3255 return false;
3258 /* Emit a diagnostic for an invalid type name. This function commits
3259 to the current active tentative parse, if any. (Otherwise, the
3260 problematic construct might be encountered again later, resulting
3261 in duplicate error messages.) LOCATION is the location of ID. */
3263 static void
3264 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3265 location_t location)
3267 tree decl, ambiguous_decls;
3268 cp_parser_commit_to_tentative_parse (parser);
3269 /* Try to lookup the identifier. */
3270 decl = cp_parser_lookup_name (parser, id, none_type,
3271 /*is_template=*/false,
3272 /*is_namespace=*/false,
3273 /*check_dependency=*/true,
3274 &ambiguous_decls, location);
3275 if (ambiguous_decls)
3276 /* If the lookup was ambiguous, an error will already have
3277 been issued. */
3278 return;
3279 /* If the lookup found a template-name, it means that the user forgot
3280 to specify an argument list. Emit a useful error message. */
3281 if (DECL_TYPE_TEMPLATE_P (decl))
3283 auto_diagnostic_group d;
3284 error_at (location,
3285 "invalid use of template-name %qE without an argument list",
3286 decl);
3287 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3288 inform (location, "class template argument deduction is only available "
3289 "with -std=c++17 or -std=gnu++17");
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3292 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3293 error_at (location, "invalid use of destructor %qD as a type", id);
3294 else if (TREE_CODE (decl) == TYPE_DECL)
3295 /* Something like 'unsigned A a;' */
3296 error_at (location, "invalid combination of multiple type-specifiers");
3297 else if (!parser->scope)
3299 /* Issue an error message. */
3300 auto_diagnostic_group d;
3301 name_hint hint;
3302 if (TREE_CODE (id) == IDENTIFIER_NODE)
3303 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3304 if (hint)
3306 gcc_rich_location richloc (location);
3307 richloc.add_fixit_replace (hint.suggestion ());
3308 error_at (&richloc,
3309 "%qE does not name a type; did you mean %qs?",
3310 id, hint.suggestion ());
3312 else
3313 error_at (location, "%qE does not name a type", id);
3314 /* If we're in a template class, it's possible that the user was
3315 referring to a type from a base class. For example:
3317 template <typename T> struct A { typedef T X; };
3318 template <typename T> struct B : public A<T> { X x; };
3320 The user should have said "typename A<T>::X". */
3321 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3322 inform (location, "C++11 %<constexpr%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3325 inform (location, "C++11 %<noexcept%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (cxx_dialect < cxx11
3328 && TREE_CODE (id) == IDENTIFIER_NODE
3329 && id_equal (id, "thread_local"))
3330 inform (location, "C++11 %<thread_local%> only available with "
3331 "-std=c++11 or -std=gnu++11");
3332 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3333 inform (location, "%<concept%> only available with -fconcepts");
3334 else if (processing_template_decl && current_class_type
3335 && TYPE_BINFO (current_class_type))
3337 tree b;
3339 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3341 b = TREE_CHAIN (b))
3343 tree base_type = BINFO_TYPE (b);
3344 if (CLASS_TYPE_P (base_type)
3345 && dependent_type_p (base_type))
3347 tree field;
3348 /* Go from a particular instantiation of the
3349 template (which will have an empty TYPE_FIELDs),
3350 to the main version. */
3351 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3352 for (field = TYPE_FIELDS (base_type);
3353 field;
3354 field = DECL_CHAIN (field))
3355 if (TREE_CODE (field) == TYPE_DECL
3356 && DECL_NAME (field) == id)
3358 inform (location,
3359 "(perhaps %<typename %T::%E%> was intended)",
3360 BINFO_TYPE (b), id);
3361 break;
3363 if (field)
3364 break;
3369 /* Here we diagnose qualified-ids where the scope is actually correct,
3370 but the identifier does not resolve to a valid type name. */
3371 else if (parser->scope != error_mark_node)
3373 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3375 auto_diagnostic_group d;
3376 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3377 error_at (location_of (id),
3378 "%qE in namespace %qE does not name a template type",
3379 id, parser->scope);
3380 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3381 error_at (location_of (id),
3382 "%qE in namespace %qE does not name a template type",
3383 TREE_OPERAND (id, 0), parser->scope);
3384 else
3385 error_at (location_of (id),
3386 "%qE in namespace %qE does not name a type",
3387 id, parser->scope);
3388 if (DECL_P (decl))
3389 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3390 else if (decl == error_mark_node)
3391 suggest_alternative_in_explicit_scope (location, id,
3392 parser->scope);
3394 else if (CLASS_TYPE_P (parser->scope)
3395 && constructor_name_p (id, parser->scope))
3397 /* A<T>::A<T>() */
3398 auto_diagnostic_group d;
3399 error_at (location, "%<%T::%E%> names the constructor, not"
3400 " the type", parser->scope, id);
3401 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3402 error_at (location, "and %qT has no template constructors",
3403 parser->scope);
3405 else if (TYPE_P (parser->scope)
3406 && dependent_scope_p (parser->scope))
3408 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3409 error_at (location,
3410 "need %<typename%> before %<%T::%D::%E%> because "
3411 "%<%T::%D%> is a dependent scope",
3412 TYPE_CONTEXT (parser->scope),
3413 TYPENAME_TYPE_FULLNAME (parser->scope),
3415 TYPE_CONTEXT (parser->scope),
3416 TYPENAME_TYPE_FULLNAME (parser->scope));
3417 else
3418 error_at (location, "need %<typename%> before %<%T::%E%> because "
3419 "%qT is a dependent scope",
3420 parser->scope, id, parser->scope);
3422 else if (TYPE_P (parser->scope))
3424 auto_diagnostic_group d;
3425 if (!COMPLETE_TYPE_P (parser->scope))
3426 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3427 parser->scope);
3428 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3429 error_at (location_of (id),
3430 "%qE in %q#T does not name a template type",
3431 id, parser->scope);
3432 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3433 error_at (location_of (id),
3434 "%qE in %q#T does not name a template type",
3435 TREE_OPERAND (id, 0), parser->scope);
3436 else
3437 error_at (location_of (id),
3438 "%qE in %q#T does not name a type",
3439 id, parser->scope);
3440 if (DECL_P (decl))
3441 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3443 else
3444 gcc_unreachable ();
3448 /* Check for a common situation where a type-name should be present,
3449 but is not, and issue a sensible error message. Returns true if an
3450 invalid type-name was detected.
3452 The situation handled by this function are variable declarations of the
3453 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3454 Usually, `ID' should name a type, but if we got here it means that it
3455 does not. We try to emit the best possible error message depending on
3456 how exactly the id-expression looks like. */
3458 static bool
3459 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3461 tree id;
3462 cp_token *token = cp_lexer_peek_token (parser->lexer);
3464 /* Avoid duplicate error about ambiguous lookup. */
3465 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3467 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3468 if (next->type == CPP_NAME && next->error_reported)
3469 goto out;
3472 cp_parser_parse_tentatively (parser);
3473 id = cp_parser_id_expression (parser,
3474 /*template_keyword_p=*/false,
3475 /*check_dependency_p=*/true,
3476 /*template_p=*/NULL,
3477 /*declarator_p=*/false,
3478 /*optional_p=*/false);
3479 /* If the next token is a (, this is a function with no explicit return
3480 type, i.e. constructor, destructor or conversion op. */
3481 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3482 || TREE_CODE (id) == TYPE_DECL)
3484 cp_parser_abort_tentative_parse (parser);
3485 return false;
3487 if (!cp_parser_parse_definitely (parser))
3488 return false;
3490 /* Emit a diagnostic for the invalid type. */
3491 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3492 out:
3493 /* If we aren't in the middle of a declarator (i.e. in a
3494 parameter-declaration-clause), skip to the end of the declaration;
3495 there's no point in trying to process it. */
3496 if (!parser->in_declarator_p)
3497 cp_parser_skip_to_end_of_block_or_statement (parser);
3498 return true;
3501 /* Consume tokens up to, and including, the next non-nested closing `)'.
3502 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3503 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3504 found an unnested token of that type. */
3506 static int
3507 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3508 bool recovering,
3509 cpp_ttype or_ttype,
3510 bool consume_paren)
3512 unsigned paren_depth = 0;
3513 unsigned brace_depth = 0;
3514 unsigned square_depth = 0;
3515 unsigned condop_depth = 0;
3517 if (recovering && or_ttype == CPP_EOF
3518 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3519 return 0;
3521 while (true)
3523 cp_token * token = cp_lexer_peek_token (parser->lexer);
3525 /* Have we found what we're looking for before the closing paren? */
3526 if (token->type == or_ttype && or_ttype != CPP_EOF
3527 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3528 return -1;
3530 switch (token->type)
3532 case CPP_EOF:
3533 case CPP_PRAGMA_EOL:
3534 /* If we've run out of tokens, then there is no closing `)'. */
3535 return 0;
3537 /* This is good for lambda expression capture-lists. */
3538 case CPP_OPEN_SQUARE:
3539 ++square_depth;
3540 break;
3541 case CPP_CLOSE_SQUARE:
3542 if (!square_depth--)
3543 return 0;
3544 break;
3546 case CPP_SEMICOLON:
3547 /* This matches the processing in skip_to_end_of_statement. */
3548 if (!brace_depth)
3549 return 0;
3550 break;
3552 case CPP_OPEN_BRACE:
3553 ++brace_depth;
3554 break;
3555 case CPP_CLOSE_BRACE:
3556 if (!brace_depth--)
3557 return 0;
3558 break;
3560 case CPP_OPEN_PAREN:
3561 if (!brace_depth)
3562 ++paren_depth;
3563 break;
3565 case CPP_CLOSE_PAREN:
3566 if (!brace_depth && !paren_depth--)
3568 if (consume_paren)
3569 cp_lexer_consume_token (parser->lexer);
3570 return 1;
3572 break;
3574 case CPP_QUERY:
3575 if (!brace_depth && !paren_depth && !square_depth)
3576 ++condop_depth;
3577 break;
3579 case CPP_COLON:
3580 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3581 condop_depth--;
3582 break;
3584 default:
3585 break;
3588 /* Consume the token. */
3589 cp_lexer_consume_token (parser->lexer);
3593 /* Consume tokens up to, and including, the next non-nested closing `)'.
3594 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3595 are doing error recovery. Returns -1 if OR_COMMA is true and we
3596 found an unnested token of that type. */
3598 static int
3599 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3600 bool recovering,
3601 bool or_comma,
3602 bool consume_paren)
3604 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3605 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3606 ttype, consume_paren);
3609 /* Consume tokens until we reach the end of the current statement.
3610 Normally, that will be just before consuming a `;'. However, if a
3611 non-nested `}' comes first, then we stop before consuming that. */
3613 static void
3614 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3616 unsigned nesting_depth = 0;
3618 /* Unwind generic function template scope if necessary. */
3619 if (parser->fully_implicit_function_template_p)
3620 abort_fully_implicit_template (parser);
3622 while (true)
3624 cp_token *token = cp_lexer_peek_token (parser->lexer);
3626 switch (token->type)
3628 case CPP_EOF:
3629 case CPP_PRAGMA_EOL:
3630 /* If we've run out of tokens, stop. */
3631 return;
3633 case CPP_SEMICOLON:
3634 /* If the next token is a `;', we have reached the end of the
3635 statement. */
3636 if (!nesting_depth)
3637 return;
3638 break;
3640 case CPP_CLOSE_BRACE:
3641 /* If this is a non-nested '}', stop before consuming it.
3642 That way, when confronted with something like:
3644 { 3 + }
3646 we stop before consuming the closing '}', even though we
3647 have not yet reached a `;'. */
3648 if (nesting_depth == 0)
3649 return;
3651 /* If it is the closing '}' for a block that we have
3652 scanned, stop -- but only after consuming the token.
3653 That way given:
3655 void f g () { ... }
3656 typedef int I;
3658 we will stop after the body of the erroneously declared
3659 function, but before consuming the following `typedef'
3660 declaration. */
3661 if (--nesting_depth == 0)
3663 cp_lexer_consume_token (parser->lexer);
3664 return;
3666 break;
3668 case CPP_OPEN_BRACE:
3669 ++nesting_depth;
3670 break;
3672 default:
3673 break;
3676 /* Consume the token. */
3677 cp_lexer_consume_token (parser->lexer);
3681 /* This function is called at the end of a statement or declaration.
3682 If the next token is a semicolon, it is consumed; otherwise, error
3683 recovery is attempted. */
3685 static void
3686 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3688 /* Look for the trailing `;'. */
3689 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3691 /* If there is additional (erroneous) input, skip to the end of
3692 the statement. */
3693 cp_parser_skip_to_end_of_statement (parser);
3694 /* If the next token is now a `;', consume it. */
3695 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3696 cp_lexer_consume_token (parser->lexer);
3700 /* Skip tokens until we have consumed an entire block, or until we
3701 have consumed a non-nested `;'. */
3703 static void
3704 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3706 int nesting_depth = 0;
3708 /* Unwind generic function template scope if necessary. */
3709 if (parser->fully_implicit_function_template_p)
3710 abort_fully_implicit_template (parser);
3712 while (nesting_depth >= 0)
3714 cp_token *token = cp_lexer_peek_token (parser->lexer);
3716 switch (token->type)
3718 case CPP_EOF:
3719 case CPP_PRAGMA_EOL:
3720 /* If we've run out of tokens, stop. */
3721 return;
3723 case CPP_SEMICOLON:
3724 /* Stop if this is an unnested ';'. */
3725 if (!nesting_depth)
3726 nesting_depth = -1;
3727 break;
3729 case CPP_CLOSE_BRACE:
3730 /* Stop if this is an unnested '}', or closes the outermost
3731 nesting level. */
3732 nesting_depth--;
3733 if (nesting_depth < 0)
3734 return;
3735 if (!nesting_depth)
3736 nesting_depth = -1;
3737 break;
3739 case CPP_OPEN_BRACE:
3740 /* Nest. */
3741 nesting_depth++;
3742 break;
3744 default:
3745 break;
3748 /* Consume the token. */
3749 cp_lexer_consume_token (parser->lexer);
3753 /* Skip tokens until a non-nested closing curly brace is the next
3754 token, or there are no more tokens. Return true in the first case,
3755 false otherwise. */
3757 static bool
3758 cp_parser_skip_to_closing_brace (cp_parser *parser)
3760 unsigned nesting_depth = 0;
3762 while (true)
3764 cp_token *token = cp_lexer_peek_token (parser->lexer);
3766 switch (token->type)
3768 case CPP_EOF:
3769 case CPP_PRAGMA_EOL:
3770 /* If we've run out of tokens, stop. */
3771 return false;
3773 case CPP_CLOSE_BRACE:
3774 /* If the next token is a non-nested `}', then we have reached
3775 the end of the current block. */
3776 if (nesting_depth-- == 0)
3777 return true;
3778 break;
3780 case CPP_OPEN_BRACE:
3781 /* If it the next token is a `{', then we are entering a new
3782 block. Consume the entire block. */
3783 ++nesting_depth;
3784 break;
3786 default:
3787 break;
3790 /* Consume the token. */
3791 cp_lexer_consume_token (parser->lexer);
3795 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3796 parameter is the PRAGMA token, allowing us to purge the entire pragma
3797 sequence. */
3799 static void
3800 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3802 cp_token *token;
3804 parser->lexer->in_pragma = false;
3807 token = cp_lexer_consume_token (parser->lexer);
3808 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3810 /* Ensure that the pragma is not parsed again. */
3811 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3814 /* Require pragma end of line, resyncing with it as necessary. The
3815 arguments are as for cp_parser_skip_to_pragma_eol. */
3817 static void
3818 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3820 parser->lexer->in_pragma = false;
3821 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3822 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3825 /* This is a simple wrapper around make_typename_type. When the id is
3826 an unresolved identifier node, we can provide a superior diagnostic
3827 using cp_parser_diagnose_invalid_type_name. */
3829 static tree
3830 cp_parser_make_typename_type (cp_parser *parser, tree id,
3831 location_t id_location)
3833 tree result;
3834 if (identifier_p (id))
3836 result = make_typename_type (parser->scope, id, typename_type,
3837 /*complain=*/tf_none);
3838 if (result == error_mark_node)
3839 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3840 return result;
3842 return make_typename_type (parser->scope, id, typename_type, tf_error);
3845 /* This is a wrapper around the
3846 make_{pointer,ptrmem,reference}_declarator functions that decides
3847 which one to call based on the CODE and CLASS_TYPE arguments. The
3848 CODE argument should be one of the values returned by
3849 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3850 appertain to the pointer or reference. */
3852 static cp_declarator *
3853 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3854 cp_cv_quals cv_qualifiers,
3855 cp_declarator *target,
3856 tree attributes)
3858 if (code == ERROR_MARK || target == cp_error_declarator)
3859 return cp_error_declarator;
3861 if (code == INDIRECT_REF)
3862 if (class_type == NULL_TREE)
3863 return make_pointer_declarator (cv_qualifiers, target, attributes);
3864 else
3865 return make_ptrmem_declarator (cv_qualifiers, class_type,
3866 target, attributes);
3867 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3868 return make_reference_declarator (cv_qualifiers, target,
3869 false, attributes);
3870 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3871 return make_reference_declarator (cv_qualifiers, target,
3872 true, attributes);
3873 gcc_unreachable ();
3876 /* Create a new C++ parser. */
3878 static cp_parser *
3879 cp_parser_new (void)
3881 cp_parser *parser;
3882 cp_lexer *lexer;
3883 unsigned i;
3885 /* cp_lexer_new_main is called before doing GC allocation because
3886 cp_lexer_new_main might load a PCH file. */
3887 lexer = cp_lexer_new_main ();
3889 /* Initialize the binops_by_token so that we can get the tree
3890 directly from the token. */
3891 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3892 binops_by_token[binops[i].token_type] = binops[i];
3894 parser = ggc_cleared_alloc<cp_parser> ();
3895 parser->lexer = lexer;
3896 parser->context = cp_parser_context_new (NULL);
3898 /* For now, we always accept GNU extensions. */
3899 parser->allow_gnu_extensions_p = 1;
3901 /* The `>' token is a greater-than operator, not the end of a
3902 template-id. */
3903 parser->greater_than_is_operator_p = true;
3905 parser->default_arg_ok_p = true;
3907 /* We are not parsing a constant-expression. */
3908 parser->integral_constant_expression_p = false;
3909 parser->allow_non_integral_constant_expression_p = false;
3910 parser->non_integral_constant_expression_p = false;
3912 /* Local variable names are not forbidden. */
3913 parser->local_variables_forbidden_p = false;
3915 /* We are not processing an `extern "C"' declaration. */
3916 parser->in_unbraced_linkage_specification_p = false;
3918 /* We are not processing a declarator. */
3919 parser->in_declarator_p = false;
3921 /* We are not processing a template-argument-list. */
3922 parser->in_template_argument_list_p = false;
3924 /* We are not in an iteration statement. */
3925 parser->in_statement = 0;
3927 /* We are not in a switch statement. */
3928 parser->in_switch_statement_p = false;
3930 /* We are not parsing a type-id inside an expression. */
3931 parser->in_type_id_in_expr_p = false;
3933 /* Declarations aren't implicitly extern "C". */
3934 parser->implicit_extern_c = false;
3936 /* String literals should be translated to the execution character set. */
3937 parser->translate_strings_p = true;
3939 /* We are not parsing a function body. */
3940 parser->in_function_body = false;
3942 /* We can correct until told otherwise. */
3943 parser->colon_corrects_to_scope_p = true;
3945 /* The unparsed function queue is empty. */
3946 push_unparsed_function_queues (parser);
3948 /* There are no classes being defined. */
3949 parser->num_classes_being_defined = 0;
3951 /* No template parameters apply. */
3952 parser->num_template_parameter_lists = 0;
3954 /* Special parsing data structures. */
3955 parser->omp_declare_simd = NULL;
3956 parser->oacc_routine = NULL;
3958 /* Not declaring an implicit function template. */
3959 parser->auto_is_implicit_function_template_parm_p = false;
3960 parser->fully_implicit_function_template_p = false;
3961 parser->implicit_template_parms = 0;
3962 parser->implicit_template_scope = 0;
3964 /* Allow constrained-type-specifiers. */
3965 parser->prevent_constrained_type_specifiers = 0;
3967 /* We haven't yet seen an 'extern "C"'. */
3968 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3970 return parser;
3973 /* Create a cp_lexer structure which will emit the tokens in CACHE
3974 and push it onto the parser's lexer stack. This is used for delayed
3975 parsing of in-class method bodies and default arguments, and should
3976 not be confused with tentative parsing. */
3977 static void
3978 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3980 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3981 lexer->next = parser->lexer;
3982 parser->lexer = lexer;
3984 /* Move the current source position to that of the first token in the
3985 new lexer. */
3986 cp_lexer_set_source_position_from_token (lexer->next_token);
3989 /* Pop the top lexer off the parser stack. This is never used for the
3990 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3991 static void
3992 cp_parser_pop_lexer (cp_parser *parser)
3994 cp_lexer *lexer = parser->lexer;
3995 parser->lexer = lexer->next;
3996 cp_lexer_destroy (lexer);
3998 /* Put the current source position back where it was before this
3999 lexer was pushed. */
4000 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4003 /* Lexical conventions [gram.lex] */
4005 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4006 identifier. */
4008 static cp_expr
4009 cp_parser_identifier (cp_parser* parser)
4011 cp_token *token;
4013 /* Look for the identifier. */
4014 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4015 /* Return the value. */
4016 if (token)
4017 return cp_expr (token->u.value, token->location);
4018 else
4019 return error_mark_node;
4022 /* Parse a sequence of adjacent string constants. Returns a
4023 TREE_STRING representing the combined, nul-terminated string
4024 constant. If TRANSLATE is true, translate the string to the
4025 execution character set. If WIDE_OK is true, a wide string is
4026 invalid here.
4028 C++98 [lex.string] says that if a narrow string literal token is
4029 adjacent to a wide string literal token, the behavior is undefined.
4030 However, C99 6.4.5p4 says that this results in a wide string literal.
4031 We follow C99 here, for consistency with the C front end.
4033 This code is largely lifted from lex_string() in c-lex.c.
4035 FUTURE: ObjC++ will need to handle @-strings here. */
4036 static cp_expr
4037 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4038 bool lookup_udlit = true)
4040 tree value;
4041 size_t count;
4042 struct obstack str_ob;
4043 cpp_string str, istr, *strs;
4044 cp_token *tok;
4045 enum cpp_ttype type, curr_type;
4046 int have_suffix_p = 0;
4047 tree string_tree;
4048 tree suffix_id = NULL_TREE;
4049 bool curr_tok_is_userdef_p = false;
4051 tok = cp_lexer_peek_token (parser->lexer);
4052 if (!cp_parser_is_string_literal (tok))
4054 cp_parser_error (parser, "expected string-literal");
4055 return error_mark_node;
4058 location_t loc = tok->location;
4060 if (cpp_userdef_string_p (tok->type))
4062 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4063 curr_type = cpp_userdef_string_remove_type (tok->type);
4064 curr_tok_is_userdef_p = true;
4066 else
4068 string_tree = tok->u.value;
4069 curr_type = tok->type;
4071 type = curr_type;
4073 /* Try to avoid the overhead of creating and destroying an obstack
4074 for the common case of just one string. */
4075 if (!cp_parser_is_string_literal
4076 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4078 cp_lexer_consume_token (parser->lexer);
4080 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4081 str.len = TREE_STRING_LENGTH (string_tree);
4082 count = 1;
4084 if (curr_tok_is_userdef_p)
4086 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4087 have_suffix_p = 1;
4088 curr_type = cpp_userdef_string_remove_type (tok->type);
4090 else
4091 curr_type = tok->type;
4093 strs = &str;
4095 else
4097 location_t last_tok_loc = tok->location;
4098 gcc_obstack_init (&str_ob);
4099 count = 0;
4103 cp_lexer_consume_token (parser->lexer);
4104 count++;
4105 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4106 str.len = TREE_STRING_LENGTH (string_tree);
4108 if (curr_tok_is_userdef_p)
4110 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4111 if (have_suffix_p == 0)
4113 suffix_id = curr_suffix_id;
4114 have_suffix_p = 1;
4116 else if (have_suffix_p == 1
4117 && curr_suffix_id != suffix_id)
4119 error ("inconsistent user-defined literal suffixes"
4120 " %qD and %qD in string literal",
4121 suffix_id, curr_suffix_id);
4122 have_suffix_p = -1;
4124 curr_type = cpp_userdef_string_remove_type (tok->type);
4126 else
4127 curr_type = tok->type;
4129 if (type != curr_type)
4131 if (type == CPP_STRING)
4132 type = curr_type;
4133 else if (curr_type != CPP_STRING)
4135 rich_location rich_loc (line_table, tok->location);
4136 rich_loc.add_range (last_tok_loc, false);
4137 error_at (&rich_loc,
4138 "unsupported non-standard concatenation "
4139 "of string literals");
4143 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4145 last_tok_loc = tok->location;
4147 tok = cp_lexer_peek_token (parser->lexer);
4148 if (cpp_userdef_string_p (tok->type))
4150 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4151 curr_type = cpp_userdef_string_remove_type (tok->type);
4152 curr_tok_is_userdef_p = true;
4154 else
4156 string_tree = tok->u.value;
4157 curr_type = tok->type;
4158 curr_tok_is_userdef_p = false;
4161 while (cp_parser_is_string_literal (tok));
4163 /* A string literal built by concatenation has its caret=start at
4164 the start of the initial string, and its finish at the finish of
4165 the final string literal. */
4166 loc = make_location (loc, loc, get_finish (last_tok_loc));
4168 strs = (cpp_string *) obstack_finish (&str_ob);
4171 if (type != CPP_STRING && !wide_ok)
4173 cp_parser_error (parser, "a wide string is invalid in this context");
4174 type = CPP_STRING;
4177 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4178 (parse_in, strs, count, &istr, type))
4180 value = build_string (istr.len, (const char *)istr.text);
4181 free (CONST_CAST (unsigned char *, istr.text));
4183 switch (type)
4185 default:
4186 case CPP_STRING:
4187 case CPP_UTF8STRING:
4188 TREE_TYPE (value) = char_array_type_node;
4189 break;
4190 case CPP_STRING16:
4191 TREE_TYPE (value) = char16_array_type_node;
4192 break;
4193 case CPP_STRING32:
4194 TREE_TYPE (value) = char32_array_type_node;
4195 break;
4196 case CPP_WSTRING:
4197 TREE_TYPE (value) = wchar_array_type_node;
4198 break;
4201 value = fix_string_type (value);
4203 if (have_suffix_p)
4205 tree literal = build_userdef_literal (suffix_id, value,
4206 OT_NONE, NULL_TREE);
4207 if (lookup_udlit)
4208 value = cp_parser_userdef_string_literal (literal);
4209 else
4210 value = literal;
4213 else
4214 /* cpp_interpret_string has issued an error. */
4215 value = error_mark_node;
4217 if (count > 1)
4218 obstack_free (&str_ob, 0);
4220 return cp_expr (value, loc);
4223 /* Look up a literal operator with the name and the exact arguments. */
4225 static tree
4226 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4228 tree decl;
4229 decl = lookup_name (name);
4230 if (!decl || !is_overloaded_fn (decl))
4231 return error_mark_node;
4233 for (lkp_iterator iter (decl); iter; ++iter)
4235 unsigned int ix;
4236 bool found = true;
4237 tree fn = *iter;
4238 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4239 if (parmtypes != NULL_TREE)
4241 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4242 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4244 tree tparm = TREE_VALUE (parmtypes);
4245 tree targ = TREE_TYPE ((*args)[ix]);
4246 bool ptr = TYPE_PTR_P (tparm);
4247 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4248 if ((ptr || arr || !same_type_p (tparm, targ))
4249 && (!ptr || !arr
4250 || !same_type_p (TREE_TYPE (tparm),
4251 TREE_TYPE (targ))))
4252 found = false;
4254 if (found
4255 && ix == vec_safe_length (args)
4256 /* May be this should be sufficient_parms_p instead,
4257 depending on how exactly should user-defined literals
4258 work in presence of default arguments on the literal
4259 operator parameters. */
4260 && parmtypes == void_list_node)
4261 return decl;
4265 return error_mark_node;
4268 /* Parse a user-defined char constant. Returns a call to a user-defined
4269 literal operator taking the character as an argument. */
4271 static cp_expr
4272 cp_parser_userdef_char_literal (cp_parser *parser)
4274 cp_token *token = cp_lexer_consume_token (parser->lexer);
4275 tree literal = token->u.value;
4276 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4277 tree value = USERDEF_LITERAL_VALUE (literal);
4278 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4279 tree decl, result;
4281 /* Build up a call to the user-defined operator */
4282 /* Lookup the name we got back from the id-expression. */
4283 vec<tree, va_gc> *args = make_tree_vector ();
4284 vec_safe_push (args, value);
4285 decl = lookup_literal_operator (name, args);
4286 if (!decl || decl == error_mark_node)
4288 error ("unable to find character literal operator %qD with %qT argument",
4289 name, TREE_TYPE (value));
4290 release_tree_vector (args);
4291 return error_mark_node;
4293 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4294 release_tree_vector (args);
4295 return result;
4298 /* A subroutine of cp_parser_userdef_numeric_literal to
4299 create a char... template parameter pack from a string node. */
4301 static tree
4302 make_char_string_pack (tree value)
4304 tree charvec;
4305 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4306 const char *str = TREE_STRING_POINTER (value);
4307 int i, len = TREE_STRING_LENGTH (value) - 1;
4308 tree argvec = make_tree_vec (1);
4310 /* Fill in CHARVEC with all of the parameters. */
4311 charvec = make_tree_vec (len);
4312 for (i = 0; i < len; ++i)
4314 unsigned char s[3] = { '\'', str[i], '\'' };
4315 cpp_string in = { 3, s };
4316 cpp_string out = { 0, 0 };
4317 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4318 return NULL_TREE;
4319 gcc_assert (out.len == 2);
4320 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4321 out.text[0]);
4324 /* Build the argument packs. */
4325 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4327 TREE_VEC_ELT (argvec, 0) = argpack;
4329 return argvec;
4332 /* A subroutine of cp_parser_userdef_numeric_literal to
4333 create a char... template parameter pack from a string node. */
4335 static tree
4336 make_string_pack (tree value)
4338 tree charvec;
4339 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4340 const unsigned char *str
4341 = (const unsigned char *) TREE_STRING_POINTER (value);
4342 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4343 int len = TREE_STRING_LENGTH (value) / sz - 1;
4344 tree argvec = make_tree_vec (2);
4346 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4347 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4349 /* First template parm is character type. */
4350 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4352 /* Fill in CHARVEC with all of the parameters. */
4353 charvec = make_tree_vec (len);
4354 for (int i = 0; i < len; ++i)
4355 TREE_VEC_ELT (charvec, i)
4356 = double_int_to_tree (str_char_type_node,
4357 double_int::from_buffer (str + i * sz, sz));
4359 /* Build the argument packs. */
4360 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4362 TREE_VEC_ELT (argvec, 1) = argpack;
4364 return argvec;
4367 /* Parse a user-defined numeric constant. returns a call to a user-defined
4368 literal operator. */
4370 static cp_expr
4371 cp_parser_userdef_numeric_literal (cp_parser *parser)
4373 cp_token *token = cp_lexer_consume_token (parser->lexer);
4374 tree literal = token->u.value;
4375 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4376 tree value = USERDEF_LITERAL_VALUE (literal);
4377 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4378 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4379 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4380 tree decl, result;
4381 vec<tree, va_gc> *args;
4383 /* Look for a literal operator taking the exact type of numeric argument
4384 as the literal value. */
4385 args = make_tree_vector ();
4386 vec_safe_push (args, value);
4387 decl = lookup_literal_operator (name, args);
4388 if (decl && decl != error_mark_node)
4390 result = finish_call_expr (decl, &args, false, true,
4391 tf_warning_or_error);
4393 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4395 warning_at (token->location, OPT_Woverflow,
4396 "integer literal exceeds range of %qT type",
4397 long_long_unsigned_type_node);
4399 else
4401 if (overflow > 0)
4402 warning_at (token->location, OPT_Woverflow,
4403 "floating literal exceeds range of %qT type",
4404 long_double_type_node);
4405 else if (overflow < 0)
4406 warning_at (token->location, OPT_Woverflow,
4407 "floating literal truncated to zero");
4410 release_tree_vector (args);
4411 return result;
4413 release_tree_vector (args);
4415 /* If the numeric argument didn't work, look for a raw literal
4416 operator taking a const char* argument consisting of the number
4417 in string format. */
4418 args = make_tree_vector ();
4419 vec_safe_push (args, num_string);
4420 decl = lookup_literal_operator (name, args);
4421 if (decl && decl != error_mark_node)
4423 result = finish_call_expr (decl, &args, false, true,
4424 tf_warning_or_error);
4425 release_tree_vector (args);
4426 return result;
4428 release_tree_vector (args);
4430 /* If the raw literal didn't work, look for a non-type template
4431 function with parameter pack char.... Call the function with
4432 template parameter characters representing the number. */
4433 args = make_tree_vector ();
4434 decl = lookup_literal_operator (name, args);
4435 if (decl && decl != error_mark_node)
4437 tree tmpl_args = make_char_string_pack (num_string);
4438 if (tmpl_args == NULL_TREE)
4440 error ("failed to translate literal to execution character set %qT",
4441 num_string);
4442 return error_mark_node;
4444 decl = lookup_template_function (decl, tmpl_args);
4445 result = finish_call_expr (decl, &args, false, true,
4446 tf_warning_or_error);
4447 release_tree_vector (args);
4448 return result;
4451 release_tree_vector (args);
4453 /* In C++14 the standard library defines complex number suffixes that
4454 conflict with GNU extensions. Prefer them if <complex> is #included. */
4455 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4456 bool i14 = (cxx_dialect > cxx11
4457 && (id_equal (suffix_id, "i")
4458 || id_equal (suffix_id, "if")
4459 || id_equal (suffix_id, "il")));
4460 diagnostic_t kind = DK_ERROR;
4461 int opt = 0;
4463 if (i14 && ext)
4465 tree cxlit = lookup_qualified_name (std_node,
4466 get_identifier ("complex_literals"),
4467 0, false, false);
4468 if (cxlit == error_mark_node)
4470 /* No <complex>, so pedwarn and use GNU semantics. */
4471 kind = DK_PEDWARN;
4472 opt = OPT_Wpedantic;
4476 bool complained
4477 = emit_diagnostic (kind, input_location, opt,
4478 "unable to find numeric literal operator %qD", name);
4480 if (!complained)
4481 /* Don't inform either. */;
4482 else if (i14)
4484 inform (token->location, "add %<using namespace std::complex_literals%> "
4485 "(from <complex>) to enable the C++14 user-defined literal "
4486 "suffixes");
4487 if (ext)
4488 inform (token->location, "or use %<j%> instead of %<i%> for the "
4489 "GNU built-in suffix");
4491 else if (!ext)
4492 inform (token->location, "use -fext-numeric-literals "
4493 "to enable more built-in suffixes");
4495 if (kind == DK_ERROR)
4496 value = error_mark_node;
4497 else
4499 /* Use the built-in semantics. */
4500 tree type;
4501 if (id_equal (suffix_id, "i"))
4503 if (TREE_CODE (value) == INTEGER_CST)
4504 type = integer_type_node;
4505 else
4506 type = double_type_node;
4508 else if (id_equal (suffix_id, "if"))
4509 type = float_type_node;
4510 else /* if (id_equal (suffix_id, "il")) */
4511 type = long_double_type_node;
4513 value = build_complex (build_complex_type (type),
4514 fold_convert (type, integer_zero_node),
4515 fold_convert (type, value));
4518 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4519 /* Avoid repeated diagnostics. */
4520 token->u.value = value;
4521 return value;
4524 /* Parse a user-defined string constant. Returns a call to a user-defined
4525 literal operator taking a character pointer and the length of the string
4526 as arguments. */
4528 static tree
4529 cp_parser_userdef_string_literal (tree literal)
4531 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4532 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4533 tree value = USERDEF_LITERAL_VALUE (literal);
4534 int len = TREE_STRING_LENGTH (value)
4535 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4536 tree decl, result;
4537 vec<tree, va_gc> *args;
4539 /* Build up a call to the user-defined operator. */
4540 /* Lookup the name we got back from the id-expression. */
4541 args = make_tree_vector ();
4542 vec_safe_push (args, value);
4543 vec_safe_push (args, build_int_cst (size_type_node, len));
4544 decl = lookup_literal_operator (name, args);
4546 if (decl && decl != error_mark_node)
4548 result = finish_call_expr (decl, &args, false, true,
4549 tf_warning_or_error);
4550 release_tree_vector (args);
4551 return result;
4553 release_tree_vector (args);
4555 /* Look for a template function with typename parameter CharT
4556 and parameter pack CharT... Call the function with
4557 template parameter characters representing the string. */
4558 args = make_tree_vector ();
4559 decl = lookup_literal_operator (name, args);
4560 if (decl && decl != error_mark_node)
4562 tree tmpl_args = make_string_pack (value);
4563 decl = lookup_template_function (decl, tmpl_args);
4564 result = finish_call_expr (decl, &args, false, true,
4565 tf_warning_or_error);
4566 release_tree_vector (args);
4567 return result;
4569 release_tree_vector (args);
4571 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4572 name, TREE_TYPE (value), size_type_node);
4573 return error_mark_node;
4577 /* Basic concepts [gram.basic] */
4579 /* Parse a translation-unit.
4581 translation-unit:
4582 declaration-seq [opt]
4584 Returns TRUE if all went well. */
4586 static bool
4587 cp_parser_translation_unit (cp_parser* parser)
4589 /* The address of the first non-permanent object on the declarator
4590 obstack. */
4591 static void *declarator_obstack_base;
4593 bool success;
4595 /* Create the declarator obstack, if necessary. */
4596 if (!cp_error_declarator)
4598 gcc_obstack_init (&declarator_obstack);
4599 /* Create the error declarator. */
4600 cp_error_declarator = make_declarator (cdk_error);
4601 /* Create the empty parameter list. */
4602 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4603 UNKNOWN_LOCATION);
4604 /* Remember where the base of the declarator obstack lies. */
4605 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4608 cp_parser_declaration_seq_opt (parser);
4610 /* If there are no tokens left then all went well. */
4611 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4613 /* Get rid of the token array; we don't need it any more. */
4614 cp_lexer_destroy (parser->lexer);
4615 parser->lexer = NULL;
4617 /* This file might have been a context that's implicitly extern
4618 "C". If so, pop the lang context. (Only relevant for PCH.) */
4619 if (parser->implicit_extern_c)
4621 pop_lang_context ();
4622 parser->implicit_extern_c = false;
4625 /* Finish up. */
4626 finish_translation_unit ();
4628 success = true;
4630 else
4632 cp_parser_error (parser, "expected declaration");
4633 success = false;
4636 /* Make sure the declarator obstack was fully cleaned up. */
4637 gcc_assert (obstack_next_free (&declarator_obstack)
4638 == declarator_obstack_base);
4640 /* All went well. */
4641 return success;
4644 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4645 decltype context. */
4647 static inline tsubst_flags_t
4648 complain_flags (bool decltype_p)
4650 tsubst_flags_t complain = tf_warning_or_error;
4651 if (decltype_p)
4652 complain |= tf_decltype;
4653 return complain;
4656 /* We're about to parse a collection of statements. If we're currently
4657 parsing tentatively, set up a firewall so that any nested
4658 cp_parser_commit_to_tentative_parse won't affect the current context. */
4660 static cp_token_position
4661 cp_parser_start_tentative_firewall (cp_parser *parser)
4663 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4664 return 0;
4666 cp_parser_parse_tentatively (parser);
4667 cp_parser_commit_to_topmost_tentative_parse (parser);
4668 return cp_lexer_token_position (parser->lexer, false);
4671 /* We've finished parsing the collection of statements. Wrap up the
4672 firewall and replace the relevant tokens with the parsed form. */
4674 static void
4675 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4676 tree expr)
4678 if (!start)
4679 return;
4681 /* Finish the firewall level. */
4682 cp_parser_parse_definitely (parser);
4683 /* And remember the result of the parse for when we try again. */
4684 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4685 token->type = CPP_PREPARSED_EXPR;
4686 token->u.value = expr;
4687 token->keyword = RID_MAX;
4688 cp_lexer_purge_tokens_after (parser->lexer, start);
4691 /* Like the above functions, but let the user modify the tokens. Used by
4692 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4693 later parses, so it makes sense to localize the effects of
4694 cp_parser_commit_to_tentative_parse. */
4696 struct tentative_firewall
4698 cp_parser *parser;
4699 bool set;
4701 tentative_firewall (cp_parser *p): parser(p)
4703 /* If we're currently parsing tentatively, start a committed level as a
4704 firewall and then an inner tentative parse. */
4705 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4707 cp_parser_parse_tentatively (parser);
4708 cp_parser_commit_to_topmost_tentative_parse (parser);
4709 cp_parser_parse_tentatively (parser);
4713 ~tentative_firewall()
4715 if (set)
4717 /* Finish the inner tentative parse and the firewall, propagating any
4718 uncommitted error state to the outer tentative parse. */
4719 bool err = cp_parser_error_occurred (parser);
4720 cp_parser_parse_definitely (parser);
4721 cp_parser_parse_definitely (parser);
4722 if (err)
4723 cp_parser_simulate_error (parser);
4728 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4729 This class is for tracking such a matching pair of symbols.
4730 In particular, it tracks the location of the first token,
4731 so that if the second token is missing, we can highlight the
4732 location of the first token when notifying the user about the
4733 problem. */
4735 template <typename traits_t>
4736 class token_pair
4738 public:
4739 /* token_pair's ctor. */
4740 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4742 /* If the next token is the opening symbol for this pair, consume it and
4743 return true.
4744 Otherwise, issue an error and return false.
4745 In either case, record the location of the opening token. */
4747 bool require_open (cp_parser *parser)
4749 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4750 return cp_parser_require (parser, traits_t::open_token_type,
4751 traits_t::required_token_open);
4754 /* Consume the next token from PARSER, recording its location as
4755 that of the opening token within the pair. */
4757 cp_token * consume_open (cp_parser *parser)
4759 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4760 gcc_assert (tok->type == traits_t::open_token_type);
4761 m_open_loc = tok->location;
4762 return tok;
4765 /* If the next token is the closing symbol for this pair, consume it
4766 and return it.
4767 Otherwise, issue an error, highlighting the location of the
4768 corresponding opening token, and return NULL. */
4770 cp_token *require_close (cp_parser *parser) const
4772 return cp_parser_require (parser, traits_t::close_token_type,
4773 traits_t::required_token_close,
4774 m_open_loc);
4777 private:
4778 location_t m_open_loc;
4781 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4783 struct matching_paren_traits
4785 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4786 static const enum required_token required_token_open = RT_OPEN_PAREN;
4787 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4788 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4791 /* "matching_parens" is a token_pair<T> class for tracking matching
4792 pairs of parentheses. */
4794 typedef token_pair<matching_paren_traits> matching_parens;
4796 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4798 struct matching_brace_traits
4800 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4801 static const enum required_token required_token_open = RT_OPEN_BRACE;
4802 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4803 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4806 /* "matching_braces" is a token_pair<T> class for tracking matching
4807 pairs of braces. */
4809 typedef token_pair<matching_brace_traits> matching_braces;
4812 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4813 enclosing parentheses. */
4815 static cp_expr
4816 cp_parser_statement_expr (cp_parser *parser)
4818 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4820 /* Consume the '('. */
4821 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4822 matching_parens parens;
4823 parens.consume_open (parser);
4824 /* Start the statement-expression. */
4825 tree expr = begin_stmt_expr ();
4826 /* Parse the compound-statement. */
4827 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4828 /* Finish up. */
4829 expr = finish_stmt_expr (expr, false);
4830 /* Consume the ')'. */
4831 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4832 if (!parens.require_close (parser))
4833 cp_parser_skip_to_end_of_statement (parser);
4835 cp_parser_end_tentative_firewall (parser, start, expr);
4836 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4837 return cp_expr (expr, combined_loc);
4840 /* Expressions [gram.expr] */
4842 /* Parse a fold-operator.
4844 fold-operator:
4845 - * / % ^ & | = < > << >>
4846 = -= *= /= %= ^= &= |= <<= >>=
4847 == != <= >= && || , .* ->*
4849 This returns the tree code corresponding to the matched operator
4850 as an int. When the current token matches a compound assignment
4851 opertor, the resulting tree code is the negative value of the
4852 non-assignment operator. */
4854 static int
4855 cp_parser_fold_operator (cp_token *token)
4857 switch (token->type)
4859 case CPP_PLUS: return PLUS_EXPR;
4860 case CPP_MINUS: return MINUS_EXPR;
4861 case CPP_MULT: return MULT_EXPR;
4862 case CPP_DIV: return TRUNC_DIV_EXPR;
4863 case CPP_MOD: return TRUNC_MOD_EXPR;
4864 case CPP_XOR: return BIT_XOR_EXPR;
4865 case CPP_AND: return BIT_AND_EXPR;
4866 case CPP_OR: return BIT_IOR_EXPR;
4867 case CPP_LSHIFT: return LSHIFT_EXPR;
4868 case CPP_RSHIFT: return RSHIFT_EXPR;
4870 case CPP_EQ: return -NOP_EXPR;
4871 case CPP_PLUS_EQ: return -PLUS_EXPR;
4872 case CPP_MINUS_EQ: return -MINUS_EXPR;
4873 case CPP_MULT_EQ: return -MULT_EXPR;
4874 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4875 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4876 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4877 case CPP_AND_EQ: return -BIT_AND_EXPR;
4878 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4879 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4880 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4882 case CPP_EQ_EQ: return EQ_EXPR;
4883 case CPP_NOT_EQ: return NE_EXPR;
4884 case CPP_LESS: return LT_EXPR;
4885 case CPP_GREATER: return GT_EXPR;
4886 case CPP_LESS_EQ: return LE_EXPR;
4887 case CPP_GREATER_EQ: return GE_EXPR;
4889 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4890 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4892 case CPP_COMMA: return COMPOUND_EXPR;
4894 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4895 case CPP_DEREF_STAR: return MEMBER_REF;
4897 default: return ERROR_MARK;
4901 /* Returns true if CODE indicates a binary expression, which is not allowed in
4902 the LHS of a fold-expression. More codes will need to be added to use this
4903 function in other contexts. */
4905 static bool
4906 is_binary_op (tree_code code)
4908 switch (code)
4910 case PLUS_EXPR:
4911 case POINTER_PLUS_EXPR:
4912 case MINUS_EXPR:
4913 case MULT_EXPR:
4914 case TRUNC_DIV_EXPR:
4915 case TRUNC_MOD_EXPR:
4916 case BIT_XOR_EXPR:
4917 case BIT_AND_EXPR:
4918 case BIT_IOR_EXPR:
4919 case LSHIFT_EXPR:
4920 case RSHIFT_EXPR:
4922 case MODOP_EXPR:
4924 case EQ_EXPR:
4925 case NE_EXPR:
4926 case LE_EXPR:
4927 case GE_EXPR:
4928 case LT_EXPR:
4929 case GT_EXPR:
4931 case TRUTH_ANDIF_EXPR:
4932 case TRUTH_ORIF_EXPR:
4934 case COMPOUND_EXPR:
4936 case DOTSTAR_EXPR:
4937 case MEMBER_REF:
4938 return true;
4940 default:
4941 return false;
4945 /* If the next token is a suitable fold operator, consume it and return as
4946 the function above. */
4948 static int
4949 cp_parser_fold_operator (cp_parser *parser)
4951 cp_token* token = cp_lexer_peek_token (parser->lexer);
4952 int code = cp_parser_fold_operator (token);
4953 if (code != ERROR_MARK)
4954 cp_lexer_consume_token (parser->lexer);
4955 return code;
4958 /* Parse a fold-expression.
4960 fold-expression:
4961 ( ... folding-operator cast-expression)
4962 ( cast-expression folding-operator ... )
4963 ( cast-expression folding operator ... folding-operator cast-expression)
4965 Note that the '(' and ')' are matched in primary expression. */
4967 static cp_expr
4968 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4970 cp_id_kind pidk;
4972 // Left fold.
4973 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4975 cp_lexer_consume_token (parser->lexer);
4976 int op = cp_parser_fold_operator (parser);
4977 if (op == ERROR_MARK)
4979 cp_parser_error (parser, "expected binary operator");
4980 return error_mark_node;
4983 tree expr = cp_parser_cast_expression (parser, false, false,
4984 false, &pidk);
4985 if (expr == error_mark_node)
4986 return error_mark_node;
4987 return finish_left_unary_fold_expr (expr, op);
4990 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4991 int op = cp_parser_fold_operator (parser);
4992 if (op == ERROR_MARK)
4994 cp_parser_error (parser, "expected binary operator");
4995 return error_mark_node;
4998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5000 cp_parser_error (parser, "expected ...");
5001 return error_mark_node;
5003 cp_lexer_consume_token (parser->lexer);
5005 /* The operands of a fold-expression are cast-expressions, so binary or
5006 conditional expressions are not allowed. We check this here to avoid
5007 tentative parsing. */
5008 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5009 /* OK, the expression was parenthesized. */;
5010 else if (is_binary_op (TREE_CODE (expr1)))
5011 error_at (location_of (expr1),
5012 "binary expression in operand of fold-expression");
5013 else if (TREE_CODE (expr1) == COND_EXPR
5014 || (REFERENCE_REF_P (expr1)
5015 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5016 error_at (location_of (expr1),
5017 "conditional expression in operand of fold-expression");
5019 // Right fold.
5020 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5021 return finish_right_unary_fold_expr (expr1, op);
5023 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5025 cp_parser_error (parser, "mismatched operator in fold-expression");
5026 return error_mark_node;
5028 cp_lexer_consume_token (parser->lexer);
5030 // Binary left or right fold.
5031 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5032 if (expr2 == error_mark_node)
5033 return error_mark_node;
5034 return finish_binary_fold_expr (expr1, expr2, op);
5037 /* Parse a primary-expression.
5039 primary-expression:
5040 literal
5041 this
5042 ( expression )
5043 id-expression
5044 lambda-expression (C++11)
5046 GNU Extensions:
5048 primary-expression:
5049 ( compound-statement )
5050 __builtin_va_arg ( assignment-expression , type-id )
5051 __builtin_offsetof ( type-id , offsetof-expression )
5053 C++ Extensions:
5054 __has_nothrow_assign ( type-id )
5055 __has_nothrow_constructor ( type-id )
5056 __has_nothrow_copy ( type-id )
5057 __has_trivial_assign ( type-id )
5058 __has_trivial_constructor ( type-id )
5059 __has_trivial_copy ( type-id )
5060 __has_trivial_destructor ( type-id )
5061 __has_virtual_destructor ( type-id )
5062 __is_abstract ( type-id )
5063 __is_base_of ( type-id , type-id )
5064 __is_class ( type-id )
5065 __is_empty ( type-id )
5066 __is_enum ( type-id )
5067 __is_final ( type-id )
5068 __is_literal_type ( type-id )
5069 __is_pod ( type-id )
5070 __is_polymorphic ( type-id )
5071 __is_std_layout ( type-id )
5072 __is_trivial ( type-id )
5073 __is_union ( type-id )
5075 Objective-C++ Extension:
5077 primary-expression:
5078 objc-expression
5080 literal:
5081 __null
5083 ADDRESS_P is true iff this expression was immediately preceded by
5084 "&" and therefore might denote a pointer-to-member. CAST_P is true
5085 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5086 true iff this expression is a template argument.
5088 Returns a representation of the expression. Upon return, *IDK
5089 indicates what kind of id-expression (if any) was present. */
5091 static cp_expr
5092 cp_parser_primary_expression (cp_parser *parser,
5093 bool address_p,
5094 bool cast_p,
5095 bool template_arg_p,
5096 bool decltype_p,
5097 cp_id_kind *idk)
5099 cp_token *token = NULL;
5101 /* Assume the primary expression is not an id-expression. */
5102 *idk = CP_ID_KIND_NONE;
5104 /* Peek at the next token. */
5105 token = cp_lexer_peek_token (parser->lexer);
5106 switch ((int) token->type)
5108 /* literal:
5109 integer-literal
5110 character-literal
5111 floating-literal
5112 string-literal
5113 boolean-literal
5114 pointer-literal
5115 user-defined-literal */
5116 case CPP_CHAR:
5117 case CPP_CHAR16:
5118 case CPP_CHAR32:
5119 case CPP_WCHAR:
5120 case CPP_UTF8CHAR:
5121 case CPP_NUMBER:
5122 case CPP_PREPARSED_EXPR:
5123 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5124 return cp_parser_userdef_numeric_literal (parser);
5125 token = cp_lexer_consume_token (parser->lexer);
5126 if (TREE_CODE (token->u.value) == FIXED_CST)
5128 error_at (token->location,
5129 "fixed-point types not supported in C++");
5130 return error_mark_node;
5132 /* Floating-point literals are only allowed in an integral
5133 constant expression if they are cast to an integral or
5134 enumeration type. */
5135 if (TREE_CODE (token->u.value) == REAL_CST
5136 && parser->integral_constant_expression_p
5137 && pedantic)
5139 /* CAST_P will be set even in invalid code like "int(2.7 +
5140 ...)". Therefore, we have to check that the next token
5141 is sure to end the cast. */
5142 if (cast_p)
5144 cp_token *next_token;
5146 next_token = cp_lexer_peek_token (parser->lexer);
5147 if (/* The comma at the end of an
5148 enumerator-definition. */
5149 next_token->type != CPP_COMMA
5150 /* The curly brace at the end of an enum-specifier. */
5151 && next_token->type != CPP_CLOSE_BRACE
5152 /* The end of a statement. */
5153 && next_token->type != CPP_SEMICOLON
5154 /* The end of the cast-expression. */
5155 && next_token->type != CPP_CLOSE_PAREN
5156 /* The end of an array bound. */
5157 && next_token->type != CPP_CLOSE_SQUARE
5158 /* The closing ">" in a template-argument-list. */
5159 && (next_token->type != CPP_GREATER
5160 || parser->greater_than_is_operator_p)
5161 /* C++0x only: A ">>" treated like two ">" tokens,
5162 in a template-argument-list. */
5163 && (next_token->type != CPP_RSHIFT
5164 || (cxx_dialect == cxx98)
5165 || parser->greater_than_is_operator_p))
5166 cast_p = false;
5169 /* If we are within a cast, then the constraint that the
5170 cast is to an integral or enumeration type will be
5171 checked at that point. If we are not within a cast, then
5172 this code is invalid. */
5173 if (!cast_p)
5174 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5176 return cp_expr (token->u.value, token->location);
5178 case CPP_CHAR_USERDEF:
5179 case CPP_CHAR16_USERDEF:
5180 case CPP_CHAR32_USERDEF:
5181 case CPP_WCHAR_USERDEF:
5182 case CPP_UTF8CHAR_USERDEF:
5183 return cp_parser_userdef_char_literal (parser);
5185 case CPP_STRING:
5186 case CPP_STRING16:
5187 case CPP_STRING32:
5188 case CPP_WSTRING:
5189 case CPP_UTF8STRING:
5190 case CPP_STRING_USERDEF:
5191 case CPP_STRING16_USERDEF:
5192 case CPP_STRING32_USERDEF:
5193 case CPP_WSTRING_USERDEF:
5194 case CPP_UTF8STRING_USERDEF:
5195 /* ??? Should wide strings be allowed when parser->translate_strings_p
5196 is false (i.e. in attributes)? If not, we can kill the third
5197 argument to cp_parser_string_literal. */
5198 return cp_parser_string_literal (parser,
5199 parser->translate_strings_p,
5200 true);
5202 case CPP_OPEN_PAREN:
5203 /* If we see `( { ' then we are looking at the beginning of
5204 a GNU statement-expression. */
5205 if (cp_parser_allow_gnu_extensions_p (parser)
5206 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5208 /* Statement-expressions are not allowed by the standard. */
5209 pedwarn (token->location, OPT_Wpedantic,
5210 "ISO C++ forbids braced-groups within expressions");
5212 /* And they're not allowed outside of a function-body; you
5213 cannot, for example, write:
5215 int i = ({ int j = 3; j + 1; });
5217 at class or namespace scope. */
5218 if (!parser->in_function_body
5219 || parser->in_template_argument_list_p)
5221 error_at (token->location,
5222 "statement-expressions are not allowed outside "
5223 "functions nor in template-argument lists");
5224 cp_parser_skip_to_end_of_block_or_statement (parser);
5225 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5226 cp_lexer_consume_token (parser->lexer);
5227 return error_mark_node;
5229 else
5230 return cp_parser_statement_expr (parser);
5232 /* Otherwise it's a normal parenthesized expression. */
5234 cp_expr expr;
5235 bool saved_greater_than_is_operator_p;
5237 location_t open_paren_loc = token->location;
5239 /* Consume the `('. */
5240 matching_parens parens;
5241 parens.consume_open (parser);
5242 /* Within a parenthesized expression, a `>' token is always
5243 the greater-than operator. */
5244 saved_greater_than_is_operator_p
5245 = parser->greater_than_is_operator_p;
5246 parser->greater_than_is_operator_p = true;
5248 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5249 /* Left fold expression. */
5250 expr = NULL_TREE;
5251 else
5252 /* Parse the parenthesized expression. */
5253 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5255 token = cp_lexer_peek_token (parser->lexer);
5256 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5258 expr = cp_parser_fold_expression (parser, expr);
5259 if (expr != error_mark_node
5260 && cxx_dialect < cxx17
5261 && !in_system_header_at (input_location))
5262 pedwarn (input_location, 0, "fold-expressions only available "
5263 "with -std=c++17 or -std=gnu++17");
5265 else
5266 /* Let the front end know that this expression was
5267 enclosed in parentheses. This matters in case, for
5268 example, the expression is of the form `A::B', since
5269 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5270 not. */
5271 expr = finish_parenthesized_expr (expr);
5273 /* DR 705: Wrapping an unqualified name in parentheses
5274 suppresses arg-dependent lookup. We want to pass back
5275 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5276 (c++/37862), but none of the others. */
5277 if (*idk != CP_ID_KIND_QUALIFIED)
5278 *idk = CP_ID_KIND_NONE;
5280 /* The `>' token might be the end of a template-id or
5281 template-parameter-list now. */
5282 parser->greater_than_is_operator_p
5283 = saved_greater_than_is_operator_p;
5285 /* Consume the `)'. */
5286 token = cp_lexer_peek_token (parser->lexer);
5287 location_t close_paren_loc = token->location;
5288 expr.set_range (open_paren_loc, close_paren_loc);
5289 if (!parens.require_close (parser)
5290 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5291 cp_parser_skip_to_end_of_statement (parser);
5293 return expr;
5296 case CPP_OPEN_SQUARE:
5298 if (c_dialect_objc ())
5300 /* We might have an Objective-C++ message. */
5301 cp_parser_parse_tentatively (parser);
5302 tree msg = cp_parser_objc_message_expression (parser);
5303 /* If that works out, we're done ... */
5304 if (cp_parser_parse_definitely (parser))
5305 return msg;
5306 /* ... else, fall though to see if it's a lambda. */
5308 cp_expr lam = cp_parser_lambda_expression (parser);
5309 /* Don't warn about a failed tentative parse. */
5310 if (cp_parser_error_occurred (parser))
5311 return error_mark_node;
5312 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5313 return lam;
5316 case CPP_OBJC_STRING:
5317 if (c_dialect_objc ())
5318 /* We have an Objective-C++ string literal. */
5319 return cp_parser_objc_expression (parser);
5320 cp_parser_error (parser, "expected primary-expression");
5321 return error_mark_node;
5323 case CPP_KEYWORD:
5324 switch (token->keyword)
5326 /* These two are the boolean literals. */
5327 case RID_TRUE:
5328 cp_lexer_consume_token (parser->lexer);
5329 return cp_expr (boolean_true_node, token->location);
5330 case RID_FALSE:
5331 cp_lexer_consume_token (parser->lexer);
5332 return cp_expr (boolean_false_node, token->location);
5334 /* The `__null' literal. */
5335 case RID_NULL:
5336 cp_lexer_consume_token (parser->lexer);
5337 return cp_expr (null_node, token->location);
5339 /* The `nullptr' literal. */
5340 case RID_NULLPTR:
5341 cp_lexer_consume_token (parser->lexer);
5342 return cp_expr (nullptr_node, token->location);
5344 /* Recognize the `this' keyword. */
5345 case RID_THIS:
5346 cp_lexer_consume_token (parser->lexer);
5347 if (parser->local_variables_forbidden_p)
5349 error_at (token->location,
5350 "%<this%> may not be used in this context");
5351 return error_mark_node;
5353 /* Pointers cannot appear in constant-expressions. */
5354 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5355 return error_mark_node;
5356 return cp_expr (finish_this_expr (), token->location);
5358 /* The `operator' keyword can be the beginning of an
5359 id-expression. */
5360 case RID_OPERATOR:
5361 goto id_expression;
5363 case RID_FUNCTION_NAME:
5364 case RID_PRETTY_FUNCTION_NAME:
5365 case RID_C99_FUNCTION_NAME:
5367 non_integral_constant name;
5369 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5370 __func__ are the names of variables -- but they are
5371 treated specially. Therefore, they are handled here,
5372 rather than relying on the generic id-expression logic
5373 below. Grammatically, these names are id-expressions.
5375 Consume the token. */
5376 token = cp_lexer_consume_token (parser->lexer);
5378 switch (token->keyword)
5380 case RID_FUNCTION_NAME:
5381 name = NIC_FUNC_NAME;
5382 break;
5383 case RID_PRETTY_FUNCTION_NAME:
5384 name = NIC_PRETTY_FUNC;
5385 break;
5386 case RID_C99_FUNCTION_NAME:
5387 name = NIC_C99_FUNC;
5388 break;
5389 default:
5390 gcc_unreachable ();
5393 if (cp_parser_non_integral_constant_expression (parser, name))
5394 return error_mark_node;
5396 /* Look up the name. */
5397 return finish_fname (token->u.value);
5400 case RID_VA_ARG:
5402 tree expression;
5403 tree type;
5404 source_location type_location;
5405 location_t start_loc
5406 = cp_lexer_peek_token (parser->lexer)->location;
5407 /* The `__builtin_va_arg' construct is used to handle
5408 `va_arg'. Consume the `__builtin_va_arg' token. */
5409 cp_lexer_consume_token (parser->lexer);
5410 /* Look for the opening `('. */
5411 matching_parens parens;
5412 parens.require_open (parser);
5413 /* Now, parse the assignment-expression. */
5414 expression = cp_parser_assignment_expression (parser);
5415 /* Look for the `,'. */
5416 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5417 type_location = cp_lexer_peek_token (parser->lexer)->location;
5418 /* Parse the type-id. */
5420 type_id_in_expr_sentinel s (parser);
5421 type = cp_parser_type_id (parser);
5423 /* Look for the closing `)'. */
5424 location_t finish_loc
5425 = cp_lexer_peek_token (parser->lexer)->location;
5426 parens.require_close (parser);
5427 /* Using `va_arg' in a constant-expression is not
5428 allowed. */
5429 if (cp_parser_non_integral_constant_expression (parser,
5430 NIC_VA_ARG))
5431 return error_mark_node;
5432 /* Construct a location of the form:
5433 __builtin_va_arg (v, int)
5434 ~~~~~~~~~~~~~~~~~~~~~^~~~
5435 with the caret at the type, ranging from the start of the
5436 "__builtin_va_arg" token to the close paren. */
5437 location_t combined_loc
5438 = make_location (type_location, start_loc, finish_loc);
5439 return build_x_va_arg (combined_loc, expression, type);
5442 case RID_OFFSETOF:
5443 return cp_parser_builtin_offsetof (parser);
5445 case RID_HAS_NOTHROW_ASSIGN:
5446 case RID_HAS_NOTHROW_CONSTRUCTOR:
5447 case RID_HAS_NOTHROW_COPY:
5448 case RID_HAS_TRIVIAL_ASSIGN:
5449 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5450 case RID_HAS_TRIVIAL_COPY:
5451 case RID_HAS_TRIVIAL_DESTRUCTOR:
5452 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5453 case RID_HAS_VIRTUAL_DESTRUCTOR:
5454 case RID_IS_ABSTRACT:
5455 case RID_IS_AGGREGATE:
5456 case RID_IS_BASE_OF:
5457 case RID_IS_CLASS:
5458 case RID_IS_EMPTY:
5459 case RID_IS_ENUM:
5460 case RID_IS_FINAL:
5461 case RID_IS_LITERAL_TYPE:
5462 case RID_IS_POD:
5463 case RID_IS_POLYMORPHIC:
5464 case RID_IS_SAME_AS:
5465 case RID_IS_STD_LAYOUT:
5466 case RID_IS_TRIVIAL:
5467 case RID_IS_TRIVIALLY_ASSIGNABLE:
5468 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5469 case RID_IS_TRIVIALLY_COPYABLE:
5470 case RID_IS_UNION:
5471 case RID_IS_ASSIGNABLE:
5472 case RID_IS_CONSTRUCTIBLE:
5473 return cp_parser_trait_expr (parser, token->keyword);
5475 // C++ concepts
5476 case RID_REQUIRES:
5477 return cp_parser_requires_expression (parser);
5479 /* Objective-C++ expressions. */
5480 case RID_AT_ENCODE:
5481 case RID_AT_PROTOCOL:
5482 case RID_AT_SELECTOR:
5483 return cp_parser_objc_expression (parser);
5485 case RID_TEMPLATE:
5486 if (parser->in_function_body
5487 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5488 == CPP_LESS))
5490 error_at (token->location,
5491 "a template declaration cannot appear at block scope");
5492 cp_parser_skip_to_end_of_block_or_statement (parser);
5493 return error_mark_node;
5495 /* FALLTHRU */
5496 default:
5497 cp_parser_error (parser, "expected primary-expression");
5498 return error_mark_node;
5501 /* An id-expression can start with either an identifier, a
5502 `::' as the beginning of a qualified-id, or the "operator"
5503 keyword. */
5504 case CPP_NAME:
5505 case CPP_SCOPE:
5506 case CPP_TEMPLATE_ID:
5507 case CPP_NESTED_NAME_SPECIFIER:
5509 id_expression:
5510 cp_expr id_expression;
5511 cp_expr decl;
5512 const char *error_msg;
5513 bool template_p;
5514 bool done;
5515 cp_token *id_expr_token;
5517 /* Parse the id-expression. */
5518 id_expression
5519 = cp_parser_id_expression (parser,
5520 /*template_keyword_p=*/false,
5521 /*check_dependency_p=*/true,
5522 &template_p,
5523 /*declarator_p=*/false,
5524 /*optional_p=*/false);
5525 if (id_expression == error_mark_node)
5526 return error_mark_node;
5527 id_expr_token = token;
5528 token = cp_lexer_peek_token (parser->lexer);
5529 done = (token->type != CPP_OPEN_SQUARE
5530 && token->type != CPP_OPEN_PAREN
5531 && token->type != CPP_DOT
5532 && token->type != CPP_DEREF
5533 && token->type != CPP_PLUS_PLUS
5534 && token->type != CPP_MINUS_MINUS);
5535 /* If we have a template-id, then no further lookup is
5536 required. If the template-id was for a template-class, we
5537 will sometimes have a TYPE_DECL at this point. */
5538 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5539 || TREE_CODE (id_expression) == TYPE_DECL)
5540 decl = id_expression;
5541 /* Look up the name. */
5542 else
5544 tree ambiguous_decls;
5546 /* If we already know that this lookup is ambiguous, then
5547 we've already issued an error message; there's no reason
5548 to check again. */
5549 if (id_expr_token->type == CPP_NAME
5550 && id_expr_token->error_reported)
5552 cp_parser_simulate_error (parser);
5553 return error_mark_node;
5556 decl = cp_parser_lookup_name (parser, id_expression,
5557 none_type,
5558 template_p,
5559 /*is_namespace=*/false,
5560 /*check_dependency=*/true,
5561 &ambiguous_decls,
5562 id_expr_token->location);
5563 /* If the lookup was ambiguous, an error will already have
5564 been issued. */
5565 if (ambiguous_decls)
5566 return error_mark_node;
5568 /* In Objective-C++, we may have an Objective-C 2.0
5569 dot-syntax for classes here. */
5570 if (c_dialect_objc ()
5571 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5572 && TREE_CODE (decl) == TYPE_DECL
5573 && objc_is_class_name (decl))
5575 tree component;
5576 cp_lexer_consume_token (parser->lexer);
5577 component = cp_parser_identifier (parser);
5578 if (component == error_mark_node)
5579 return error_mark_node;
5581 tree result = objc_build_class_component_ref (id_expression,
5582 component);
5583 /* Build a location of the form:
5584 expr.component
5585 ~~~~~^~~~~~~~~
5586 with caret at the start of the component name (at
5587 input_location), ranging from the start of the id_expression
5588 to the end of the component name. */
5589 location_t combined_loc
5590 = make_location (input_location, id_expression.get_start (),
5591 get_finish (input_location));
5592 protected_set_expr_location (result, combined_loc);
5593 return result;
5596 /* In Objective-C++, an instance variable (ivar) may be preferred
5597 to whatever cp_parser_lookup_name() found.
5598 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5599 rest of c-family, we have to do a little extra work to preserve
5600 any location information in cp_expr "decl". Given that
5601 objc_lookup_ivar is implemented in "c-family" and "objc", we
5602 have a trip through the pure "tree" type, rather than cp_expr.
5603 Naively copying it back to "decl" would implicitly give the
5604 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5605 store an EXPR_LOCATION. Hence we only update "decl" (and
5606 hence its location_t) if we get back a different tree node. */
5607 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5608 id_expression);
5609 if (decl_tree != decl.get_value ())
5610 decl = cp_expr (decl_tree);
5612 /* If name lookup gives us a SCOPE_REF, then the
5613 qualifying scope was dependent. */
5614 if (TREE_CODE (decl) == SCOPE_REF)
5616 /* At this point, we do not know if DECL is a valid
5617 integral constant expression. We assume that it is
5618 in fact such an expression, so that code like:
5620 template <int N> struct A {
5621 int a[B<N>::i];
5624 is accepted. At template-instantiation time, we
5625 will check that B<N>::i is actually a constant. */
5626 return decl;
5628 /* Check to see if DECL is a local variable in a context
5629 where that is forbidden. */
5630 if (parser->local_variables_forbidden_p
5631 && local_variable_p (decl))
5633 error_at (id_expr_token->location,
5634 "local variable %qD may not appear in this context",
5635 decl.get_value ());
5636 return error_mark_node;
5640 if (processing_template_decl)
5641 if (tree fns = maybe_get_fns (decl))
5642 /* It's too difficult to mark ths in all the places where
5643 we know for sure we need to keep the lookup, so do it
5644 now. The cost is extra GC to recycle the lookups
5645 resolved at parse time. */
5646 lookup_keep (fns);
5648 decl = (finish_id_expression
5649 (id_expression, decl, parser->scope,
5650 idk,
5651 parser->integral_constant_expression_p,
5652 parser->allow_non_integral_constant_expression_p,
5653 &parser->non_integral_constant_expression_p,
5654 template_p, done, address_p,
5655 template_arg_p,
5656 &error_msg,
5657 id_expression.get_location ()));
5658 if (error_msg)
5659 cp_parser_error (parser, error_msg);
5660 decl.set_location (id_expr_token->location);
5661 return decl;
5664 /* Anything else is an error. */
5665 default:
5666 cp_parser_error (parser, "expected primary-expression");
5667 return error_mark_node;
5671 static inline cp_expr
5672 cp_parser_primary_expression (cp_parser *parser,
5673 bool address_p,
5674 bool cast_p,
5675 bool template_arg_p,
5676 cp_id_kind *idk)
5678 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5679 /*decltype*/false, idk);
5682 /* Parse an id-expression.
5684 id-expression:
5685 unqualified-id
5686 qualified-id
5688 qualified-id:
5689 :: [opt] nested-name-specifier template [opt] unqualified-id
5690 :: identifier
5691 :: operator-function-id
5692 :: template-id
5694 Return a representation of the unqualified portion of the
5695 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5696 a `::' or nested-name-specifier.
5698 Often, if the id-expression was a qualified-id, the caller will
5699 want to make a SCOPE_REF to represent the qualified-id. This
5700 function does not do this in order to avoid wastefully creating
5701 SCOPE_REFs when they are not required.
5703 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5704 `template' keyword.
5706 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5707 uninstantiated templates.
5709 If *TEMPLATE_P is non-NULL, it is set to true iff the
5710 `template' keyword is used to explicitly indicate that the entity
5711 named is a template.
5713 If DECLARATOR_P is true, the id-expression is appearing as part of
5714 a declarator, rather than as part of an expression. */
5716 static cp_expr
5717 cp_parser_id_expression (cp_parser *parser,
5718 bool template_keyword_p,
5719 bool check_dependency_p,
5720 bool *template_p,
5721 bool declarator_p,
5722 bool optional_p)
5724 bool global_scope_p;
5725 bool nested_name_specifier_p;
5727 /* Assume the `template' keyword was not used. */
5728 if (template_p)
5729 *template_p = template_keyword_p;
5731 /* Look for the optional `::' operator. */
5732 global_scope_p
5733 = (!template_keyword_p
5734 && (cp_parser_global_scope_opt (parser,
5735 /*current_scope_valid_p=*/false)
5736 != NULL_TREE));
5738 /* Look for the optional nested-name-specifier. */
5739 nested_name_specifier_p
5740 = (cp_parser_nested_name_specifier_opt (parser,
5741 /*typename_keyword_p=*/false,
5742 check_dependency_p,
5743 /*type_p=*/false,
5744 declarator_p,
5745 template_keyword_p)
5746 != NULL_TREE);
5748 /* If there is a nested-name-specifier, then we are looking at
5749 the first qualified-id production. */
5750 if (nested_name_specifier_p)
5752 tree saved_scope;
5753 tree saved_object_scope;
5754 tree saved_qualifying_scope;
5755 cp_expr unqualified_id;
5756 bool is_template;
5758 /* See if the next token is the `template' keyword. */
5759 if (!template_p)
5760 template_p = &is_template;
5761 *template_p = cp_parser_optional_template_keyword (parser);
5762 /* Name lookup we do during the processing of the
5763 unqualified-id might obliterate SCOPE. */
5764 saved_scope = parser->scope;
5765 saved_object_scope = parser->object_scope;
5766 saved_qualifying_scope = parser->qualifying_scope;
5767 /* Process the final unqualified-id. */
5768 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5769 check_dependency_p,
5770 declarator_p,
5771 /*optional_p=*/false);
5772 /* Restore the SAVED_SCOPE for our caller. */
5773 parser->scope = saved_scope;
5774 parser->object_scope = saved_object_scope;
5775 parser->qualifying_scope = saved_qualifying_scope;
5777 return unqualified_id;
5779 /* Otherwise, if we are in global scope, then we are looking at one
5780 of the other qualified-id productions. */
5781 else if (global_scope_p)
5783 cp_token *token;
5784 tree id;
5786 /* Peek at the next token. */
5787 token = cp_lexer_peek_token (parser->lexer);
5789 /* If it's an identifier, and the next token is not a "<", then
5790 we can avoid the template-id case. This is an optimization
5791 for this common case. */
5792 if (token->type == CPP_NAME
5793 && !cp_parser_nth_token_starts_template_argument_list_p
5794 (parser, 2))
5795 return cp_parser_identifier (parser);
5797 cp_parser_parse_tentatively (parser);
5798 /* Try a template-id. */
5799 id = cp_parser_template_id (parser,
5800 /*template_keyword_p=*/false,
5801 /*check_dependency_p=*/true,
5802 none_type,
5803 declarator_p);
5804 /* If that worked, we're done. */
5805 if (cp_parser_parse_definitely (parser))
5806 return id;
5808 /* Peek at the next token. (Changes in the token buffer may
5809 have invalidated the pointer obtained above.) */
5810 token = cp_lexer_peek_token (parser->lexer);
5812 switch (token->type)
5814 case CPP_NAME:
5815 return cp_parser_identifier (parser);
5817 case CPP_KEYWORD:
5818 if (token->keyword == RID_OPERATOR)
5819 return cp_parser_operator_function_id (parser);
5820 /* Fall through. */
5822 default:
5823 cp_parser_error (parser, "expected id-expression");
5824 return error_mark_node;
5827 else
5828 return cp_parser_unqualified_id (parser, template_keyword_p,
5829 /*check_dependency_p=*/true,
5830 declarator_p,
5831 optional_p);
5834 /* Parse an unqualified-id.
5836 unqualified-id:
5837 identifier
5838 operator-function-id
5839 conversion-function-id
5840 ~ class-name
5841 template-id
5843 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5844 keyword, in a construct like `A::template ...'.
5846 Returns a representation of unqualified-id. For the `identifier'
5847 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5848 production a BIT_NOT_EXPR is returned; the operand of the
5849 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5850 other productions, see the documentation accompanying the
5851 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5852 names are looked up in uninstantiated templates. If DECLARATOR_P
5853 is true, the unqualified-id is appearing as part of a declarator,
5854 rather than as part of an expression. */
5856 static cp_expr
5857 cp_parser_unqualified_id (cp_parser* parser,
5858 bool template_keyword_p,
5859 bool check_dependency_p,
5860 bool declarator_p,
5861 bool optional_p)
5863 cp_token *token;
5865 /* Peek at the next token. */
5866 token = cp_lexer_peek_token (parser->lexer);
5868 switch ((int) token->type)
5870 case CPP_NAME:
5872 tree id;
5874 /* We don't know yet whether or not this will be a
5875 template-id. */
5876 cp_parser_parse_tentatively (parser);
5877 /* Try a template-id. */
5878 id = cp_parser_template_id (parser, template_keyword_p,
5879 check_dependency_p,
5880 none_type,
5881 declarator_p);
5882 /* If it worked, we're done. */
5883 if (cp_parser_parse_definitely (parser))
5884 return id;
5885 /* Otherwise, it's an ordinary identifier. */
5886 return cp_parser_identifier (parser);
5889 case CPP_TEMPLATE_ID:
5890 return cp_parser_template_id (parser, template_keyword_p,
5891 check_dependency_p,
5892 none_type,
5893 declarator_p);
5895 case CPP_COMPL:
5897 tree type_decl;
5898 tree qualifying_scope;
5899 tree object_scope;
5900 tree scope;
5901 bool done;
5903 /* Consume the `~' token. */
5904 cp_lexer_consume_token (parser->lexer);
5905 /* Parse the class-name. The standard, as written, seems to
5906 say that:
5908 template <typename T> struct S { ~S (); };
5909 template <typename T> S<T>::~S() {}
5911 is invalid, since `~' must be followed by a class-name, but
5912 `S<T>' is dependent, and so not known to be a class.
5913 That's not right; we need to look in uninstantiated
5914 templates. A further complication arises from:
5916 template <typename T> void f(T t) {
5917 t.T::~T();
5920 Here, it is not possible to look up `T' in the scope of `T'
5921 itself. We must look in both the current scope, and the
5922 scope of the containing complete expression.
5924 Yet another issue is:
5926 struct S {
5927 int S;
5928 ~S();
5931 S::~S() {}
5933 The standard does not seem to say that the `S' in `~S'
5934 should refer to the type `S' and not the data member
5935 `S::S'. */
5937 /* DR 244 says that we look up the name after the "~" in the
5938 same scope as we looked up the qualifying name. That idea
5939 isn't fully worked out; it's more complicated than that. */
5940 scope = parser->scope;
5941 object_scope = parser->object_scope;
5942 qualifying_scope = parser->qualifying_scope;
5944 /* Check for invalid scopes. */
5945 if (scope == error_mark_node)
5947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5948 cp_lexer_consume_token (parser->lexer);
5949 return error_mark_node;
5951 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5953 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5954 error_at (token->location,
5955 "scope %qT before %<~%> is not a class-name",
5956 scope);
5957 cp_parser_simulate_error (parser);
5958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5959 cp_lexer_consume_token (parser->lexer);
5960 return error_mark_node;
5962 gcc_assert (!scope || TYPE_P (scope));
5964 /* If the name is of the form "X::~X" it's OK even if X is a
5965 typedef. */
5966 token = cp_lexer_peek_token (parser->lexer);
5967 if (scope
5968 && token->type == CPP_NAME
5969 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5970 != CPP_LESS)
5971 && (token->u.value == TYPE_IDENTIFIER (scope)
5972 || (CLASS_TYPE_P (scope)
5973 && constructor_name_p (token->u.value, scope))))
5975 cp_lexer_consume_token (parser->lexer);
5976 return build_nt (BIT_NOT_EXPR, scope);
5979 /* ~auto means the destructor of whatever the object is. */
5980 if (cp_parser_is_keyword (token, RID_AUTO))
5982 if (cxx_dialect < cxx14)
5983 pedwarn (input_location, 0,
5984 "%<~auto%> only available with "
5985 "-std=c++14 or -std=gnu++14");
5986 cp_lexer_consume_token (parser->lexer);
5987 return build_nt (BIT_NOT_EXPR, make_auto ());
5990 /* If there was an explicit qualification (S::~T), first look
5991 in the scope given by the qualification (i.e., S).
5993 Note: in the calls to cp_parser_class_name below we pass
5994 typename_type so that lookup finds the injected-class-name
5995 rather than the constructor. */
5996 done = false;
5997 type_decl = NULL_TREE;
5998 if (scope)
6000 cp_parser_parse_tentatively (parser);
6001 type_decl = cp_parser_class_name (parser,
6002 /*typename_keyword_p=*/false,
6003 /*template_keyword_p=*/false,
6004 typename_type,
6005 /*check_dependency=*/false,
6006 /*class_head_p=*/false,
6007 declarator_p);
6008 if (cp_parser_parse_definitely (parser))
6009 done = true;
6011 /* In "N::S::~S", look in "N" as well. */
6012 if (!done && scope && qualifying_scope)
6014 cp_parser_parse_tentatively (parser);
6015 parser->scope = qualifying_scope;
6016 parser->object_scope = NULL_TREE;
6017 parser->qualifying_scope = NULL_TREE;
6018 type_decl
6019 = cp_parser_class_name (parser,
6020 /*typename_keyword_p=*/false,
6021 /*template_keyword_p=*/false,
6022 typename_type,
6023 /*check_dependency=*/false,
6024 /*class_head_p=*/false,
6025 declarator_p);
6026 if (cp_parser_parse_definitely (parser))
6027 done = true;
6029 /* In "p->S::~T", look in the scope given by "*p" as well. */
6030 else if (!done && object_scope)
6032 cp_parser_parse_tentatively (parser);
6033 parser->scope = object_scope;
6034 parser->object_scope = NULL_TREE;
6035 parser->qualifying_scope = NULL_TREE;
6036 type_decl
6037 = cp_parser_class_name (parser,
6038 /*typename_keyword_p=*/false,
6039 /*template_keyword_p=*/false,
6040 typename_type,
6041 /*check_dependency=*/false,
6042 /*class_head_p=*/false,
6043 declarator_p);
6044 if (cp_parser_parse_definitely (parser))
6045 done = true;
6047 /* Look in the surrounding context. */
6048 if (!done)
6050 parser->scope = NULL_TREE;
6051 parser->object_scope = NULL_TREE;
6052 parser->qualifying_scope = NULL_TREE;
6053 if (processing_template_decl)
6054 cp_parser_parse_tentatively (parser);
6055 type_decl
6056 = cp_parser_class_name (parser,
6057 /*typename_keyword_p=*/false,
6058 /*template_keyword_p=*/false,
6059 typename_type,
6060 /*check_dependency=*/false,
6061 /*class_head_p=*/false,
6062 declarator_p);
6063 if (processing_template_decl
6064 && ! cp_parser_parse_definitely (parser))
6066 /* We couldn't find a type with this name. If we're parsing
6067 tentatively, fail and try something else. */
6068 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6070 cp_parser_simulate_error (parser);
6071 return error_mark_node;
6073 /* Otherwise, accept it and check for a match at instantiation
6074 time. */
6075 type_decl = cp_parser_identifier (parser);
6076 if (type_decl != error_mark_node)
6077 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6078 return type_decl;
6081 /* If an error occurred, assume that the name of the
6082 destructor is the same as the name of the qualifying
6083 class. That allows us to keep parsing after running
6084 into ill-formed destructor names. */
6085 if (type_decl == error_mark_node && scope)
6086 return build_nt (BIT_NOT_EXPR, scope);
6087 else if (type_decl == error_mark_node)
6088 return error_mark_node;
6090 /* Check that destructor name and scope match. */
6091 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6093 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6094 error_at (token->location,
6095 "declaration of %<~%T%> as member of %qT",
6096 type_decl, scope);
6097 cp_parser_simulate_error (parser);
6098 return error_mark_node;
6101 /* [class.dtor]
6103 A typedef-name that names a class shall not be used as the
6104 identifier in the declarator for a destructor declaration. */
6105 if (declarator_p
6106 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6107 && !DECL_SELF_REFERENCE_P (type_decl)
6108 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6109 error_at (token->location,
6110 "typedef-name %qD used as destructor declarator",
6111 type_decl);
6113 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6116 case CPP_KEYWORD:
6117 if (token->keyword == RID_OPERATOR)
6119 cp_expr id;
6121 /* This could be a template-id, so we try that first. */
6122 cp_parser_parse_tentatively (parser);
6123 /* Try a template-id. */
6124 id = cp_parser_template_id (parser, template_keyword_p,
6125 /*check_dependency_p=*/true,
6126 none_type,
6127 declarator_p);
6128 /* If that worked, we're done. */
6129 if (cp_parser_parse_definitely (parser))
6130 return id;
6131 /* We still don't know whether we're looking at an
6132 operator-function-id or a conversion-function-id. */
6133 cp_parser_parse_tentatively (parser);
6134 /* Try an operator-function-id. */
6135 id = cp_parser_operator_function_id (parser);
6136 /* If that didn't work, try a conversion-function-id. */
6137 if (!cp_parser_parse_definitely (parser))
6138 id = cp_parser_conversion_function_id (parser);
6140 return id;
6142 /* Fall through. */
6144 default:
6145 if (optional_p)
6146 return NULL_TREE;
6147 cp_parser_error (parser, "expected unqualified-id");
6148 return error_mark_node;
6152 /* Parse an (optional) nested-name-specifier.
6154 nested-name-specifier: [C++98]
6155 class-or-namespace-name :: nested-name-specifier [opt]
6156 class-or-namespace-name :: template nested-name-specifier [opt]
6158 nested-name-specifier: [C++0x]
6159 type-name ::
6160 namespace-name ::
6161 nested-name-specifier identifier ::
6162 nested-name-specifier template [opt] simple-template-id ::
6164 PARSER->SCOPE should be set appropriately before this function is
6165 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6166 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6167 in name lookups.
6169 Sets PARSER->SCOPE to the class (TYPE) or namespace
6170 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6171 it unchanged if there is no nested-name-specifier. Returns the new
6172 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6174 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6175 part of a declaration and/or decl-specifier. */
6177 static tree
6178 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6179 bool typename_keyword_p,
6180 bool check_dependency_p,
6181 bool type_p,
6182 bool is_declaration,
6183 bool template_keyword_p /* = false */)
6185 bool success = false;
6186 cp_token_position start = 0;
6187 cp_token *token;
6189 /* Remember where the nested-name-specifier starts. */
6190 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6192 start = cp_lexer_token_position (parser->lexer, false);
6193 push_deferring_access_checks (dk_deferred);
6196 while (true)
6198 tree new_scope;
6199 tree old_scope;
6200 tree saved_qualifying_scope;
6202 /* Spot cases that cannot be the beginning of a
6203 nested-name-specifier. */
6204 token = cp_lexer_peek_token (parser->lexer);
6206 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6207 the already parsed nested-name-specifier. */
6208 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6210 /* Grab the nested-name-specifier and continue the loop. */
6211 cp_parser_pre_parsed_nested_name_specifier (parser);
6212 /* If we originally encountered this nested-name-specifier
6213 with IS_DECLARATION set to false, we will not have
6214 resolved TYPENAME_TYPEs, so we must do so here. */
6215 if (is_declaration
6216 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6218 new_scope = resolve_typename_type (parser->scope,
6219 /*only_current_p=*/false);
6220 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6221 parser->scope = new_scope;
6223 success = true;
6224 continue;
6227 /* Spot cases that cannot be the beginning of a
6228 nested-name-specifier. On the second and subsequent times
6229 through the loop, we look for the `template' keyword. */
6230 if (success && token->keyword == RID_TEMPLATE)
6232 /* A template-id can start a nested-name-specifier. */
6233 else if (token->type == CPP_TEMPLATE_ID)
6235 /* DR 743: decltype can be used in a nested-name-specifier. */
6236 else if (token_is_decltype (token))
6238 else
6240 /* If the next token is not an identifier, then it is
6241 definitely not a type-name or namespace-name. */
6242 if (token->type != CPP_NAME)
6243 break;
6244 /* If the following token is neither a `<' (to begin a
6245 template-id), nor a `::', then we are not looking at a
6246 nested-name-specifier. */
6247 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6249 if (token->type == CPP_COLON
6250 && parser->colon_corrects_to_scope_p
6251 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6253 gcc_rich_location richloc (token->location);
6254 richloc.add_fixit_replace ("::");
6255 error_at (&richloc,
6256 "found %<:%> in nested-name-specifier, "
6257 "expected %<::%>");
6258 token->type = CPP_SCOPE;
6261 if (token->type != CPP_SCOPE
6262 && !cp_parser_nth_token_starts_template_argument_list_p
6263 (parser, 2))
6264 break;
6267 /* The nested-name-specifier is optional, so we parse
6268 tentatively. */
6269 cp_parser_parse_tentatively (parser);
6271 /* Look for the optional `template' keyword, if this isn't the
6272 first time through the loop. */
6273 if (success)
6274 template_keyword_p = cp_parser_optional_template_keyword (parser);
6276 /* Save the old scope since the name lookup we are about to do
6277 might destroy it. */
6278 old_scope = parser->scope;
6279 saved_qualifying_scope = parser->qualifying_scope;
6280 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6281 look up names in "X<T>::I" in order to determine that "Y" is
6282 a template. So, if we have a typename at this point, we make
6283 an effort to look through it. */
6284 if (is_declaration
6285 && !typename_keyword_p
6286 && parser->scope
6287 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6288 parser->scope = resolve_typename_type (parser->scope,
6289 /*only_current_p=*/false);
6290 /* Parse the qualifying entity. */
6291 new_scope
6292 = cp_parser_qualifying_entity (parser,
6293 typename_keyword_p,
6294 template_keyword_p,
6295 check_dependency_p,
6296 type_p,
6297 is_declaration);
6298 /* Look for the `::' token. */
6299 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6301 /* If we found what we wanted, we keep going; otherwise, we're
6302 done. */
6303 if (!cp_parser_parse_definitely (parser))
6305 bool error_p = false;
6307 /* Restore the OLD_SCOPE since it was valid before the
6308 failed attempt at finding the last
6309 class-or-namespace-name. */
6310 parser->scope = old_scope;
6311 parser->qualifying_scope = saved_qualifying_scope;
6313 /* If the next token is a decltype, and the one after that is a
6314 `::', then the decltype has failed to resolve to a class or
6315 enumeration type. Give this error even when parsing
6316 tentatively since it can't possibly be valid--and we're going
6317 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6318 won't get another chance.*/
6319 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6320 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6321 == CPP_SCOPE))
6323 token = cp_lexer_consume_token (parser->lexer);
6324 error_at (token->location, "decltype evaluates to %qT, "
6325 "which is not a class or enumeration type",
6326 token->u.tree_check_value->value);
6327 parser->scope = error_mark_node;
6328 error_p = true;
6329 /* As below. */
6330 success = true;
6331 cp_lexer_consume_token (parser->lexer);
6334 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6335 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6337 /* If we have a non-type template-id followed by ::, it can't
6338 possibly be valid. */
6339 token = cp_lexer_peek_token (parser->lexer);
6340 tree tid = token->u.tree_check_value->value;
6341 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6342 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6344 tree tmpl = NULL_TREE;
6345 if (is_overloaded_fn (tid))
6347 tree fns = get_fns (tid);
6348 if (OVL_SINGLE_P (fns))
6349 tmpl = OVL_FIRST (fns);
6350 error_at (token->location, "function template-id %qD "
6351 "in nested-name-specifier", tid);
6353 else
6355 /* Variable template. */
6356 tmpl = TREE_OPERAND (tid, 0);
6357 gcc_assert (variable_template_p (tmpl));
6358 error_at (token->location, "variable template-id %qD "
6359 "in nested-name-specifier", tid);
6361 if (tmpl)
6362 inform (DECL_SOURCE_LOCATION (tmpl),
6363 "%qD declared here", tmpl);
6365 parser->scope = error_mark_node;
6366 error_p = true;
6367 /* As below. */
6368 success = true;
6369 cp_lexer_consume_token (parser->lexer);
6370 cp_lexer_consume_token (parser->lexer);
6374 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6375 break;
6376 /* If the next token is an identifier, and the one after
6377 that is a `::', then any valid interpretation would have
6378 found a class-or-namespace-name. */
6379 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6380 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6381 == CPP_SCOPE)
6382 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6383 != CPP_COMPL))
6385 token = cp_lexer_consume_token (parser->lexer);
6386 if (!error_p)
6388 if (!token->error_reported)
6390 tree decl;
6391 tree ambiguous_decls;
6393 decl = cp_parser_lookup_name (parser, token->u.value,
6394 none_type,
6395 /*is_template=*/false,
6396 /*is_namespace=*/false,
6397 /*check_dependency=*/true,
6398 &ambiguous_decls,
6399 token->location);
6400 if (TREE_CODE (decl) == TEMPLATE_DECL)
6401 error_at (token->location,
6402 "%qD used without template arguments",
6403 decl);
6404 else if (ambiguous_decls)
6406 // cp_parser_lookup_name has the same diagnostic,
6407 // thus make sure to emit it at most once.
6408 if (cp_parser_uncommitted_to_tentative_parse_p
6409 (parser))
6411 error_at (token->location,
6412 "reference to %qD is ambiguous",
6413 token->u.value);
6414 print_candidates (ambiguous_decls);
6416 decl = error_mark_node;
6418 else
6420 if (cxx_dialect != cxx98)
6421 cp_parser_name_lookup_error
6422 (parser, token->u.value, decl, NLE_NOT_CXX98,
6423 token->location);
6424 else
6425 cp_parser_name_lookup_error
6426 (parser, token->u.value, decl, NLE_CXX98,
6427 token->location);
6430 parser->scope = error_mark_node;
6431 error_p = true;
6432 /* Treat this as a successful nested-name-specifier
6433 due to:
6435 [basic.lookup.qual]
6437 If the name found is not a class-name (clause
6438 _class_) or namespace-name (_namespace.def_), the
6439 program is ill-formed. */
6440 success = true;
6442 cp_lexer_consume_token (parser->lexer);
6444 break;
6446 /* We've found one valid nested-name-specifier. */
6447 success = true;
6448 /* Name lookup always gives us a DECL. */
6449 if (TREE_CODE (new_scope) == TYPE_DECL)
6450 new_scope = TREE_TYPE (new_scope);
6451 /* Uses of "template" must be followed by actual templates. */
6452 if (template_keyword_p
6453 && !(CLASS_TYPE_P (new_scope)
6454 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6455 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6456 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6457 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6458 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6459 == TEMPLATE_ID_EXPR)))
6460 permerror (input_location, TYPE_P (new_scope)
6461 ? G_("%qT is not a template")
6462 : G_("%qD is not a template"),
6463 new_scope);
6464 /* If it is a class scope, try to complete it; we are about to
6465 be looking up names inside the class. */
6466 if (TYPE_P (new_scope)
6467 /* Since checking types for dependency can be expensive,
6468 avoid doing it if the type is already complete. */
6469 && !COMPLETE_TYPE_P (new_scope)
6470 /* Do not try to complete dependent types. */
6471 && !dependent_type_p (new_scope))
6473 new_scope = complete_type (new_scope);
6474 /* If it is a typedef to current class, use the current
6475 class instead, as the typedef won't have any names inside
6476 it yet. */
6477 if (!COMPLETE_TYPE_P (new_scope)
6478 && currently_open_class (new_scope))
6479 new_scope = TYPE_MAIN_VARIANT (new_scope);
6481 /* Make sure we look in the right scope the next time through
6482 the loop. */
6483 parser->scope = new_scope;
6486 /* If parsing tentatively, replace the sequence of tokens that makes
6487 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6488 token. That way, should we re-parse the token stream, we will
6489 not have to repeat the effort required to do the parse, nor will
6490 we issue duplicate error messages. */
6491 if (success && start)
6493 cp_token *token;
6495 token = cp_lexer_token_at (parser->lexer, start);
6496 /* Reset the contents of the START token. */
6497 token->type = CPP_NESTED_NAME_SPECIFIER;
6498 /* Retrieve any deferred checks. Do not pop this access checks yet
6499 so the memory will not be reclaimed during token replacing below. */
6500 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6501 token->u.tree_check_value->value = parser->scope;
6502 token->u.tree_check_value->checks = get_deferred_access_checks ();
6503 token->u.tree_check_value->qualifying_scope =
6504 parser->qualifying_scope;
6505 token->keyword = RID_MAX;
6507 /* Purge all subsequent tokens. */
6508 cp_lexer_purge_tokens_after (parser->lexer, start);
6511 if (start)
6512 pop_to_parent_deferring_access_checks ();
6514 return success ? parser->scope : NULL_TREE;
6517 /* Parse a nested-name-specifier. See
6518 cp_parser_nested_name_specifier_opt for details. This function
6519 behaves identically, except that it will an issue an error if no
6520 nested-name-specifier is present. */
6522 static tree
6523 cp_parser_nested_name_specifier (cp_parser *parser,
6524 bool typename_keyword_p,
6525 bool check_dependency_p,
6526 bool type_p,
6527 bool is_declaration)
6529 tree scope;
6531 /* Look for the nested-name-specifier. */
6532 scope = cp_parser_nested_name_specifier_opt (parser,
6533 typename_keyword_p,
6534 check_dependency_p,
6535 type_p,
6536 is_declaration);
6537 /* If it was not present, issue an error message. */
6538 if (!scope)
6540 cp_parser_error (parser, "expected nested-name-specifier");
6541 parser->scope = NULL_TREE;
6544 return scope;
6547 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6548 this is either a class-name or a namespace-name (which corresponds
6549 to the class-or-namespace-name production in the grammar). For
6550 C++0x, it can also be a type-name that refers to an enumeration
6551 type or a simple-template-id.
6553 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6554 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6555 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6556 TYPE_P is TRUE iff the next name should be taken as a class-name,
6557 even the same name is declared to be another entity in the same
6558 scope.
6560 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6561 specified by the class-or-namespace-name. If neither is found the
6562 ERROR_MARK_NODE is returned. */
6564 static tree
6565 cp_parser_qualifying_entity (cp_parser *parser,
6566 bool typename_keyword_p,
6567 bool template_keyword_p,
6568 bool check_dependency_p,
6569 bool type_p,
6570 bool is_declaration)
6572 tree saved_scope;
6573 tree saved_qualifying_scope;
6574 tree saved_object_scope;
6575 tree scope;
6576 bool only_class_p;
6577 bool successful_parse_p;
6579 /* DR 743: decltype can appear in a nested-name-specifier. */
6580 if (cp_lexer_next_token_is_decltype (parser->lexer))
6582 scope = cp_parser_decltype (parser);
6583 if (TREE_CODE (scope) != ENUMERAL_TYPE
6584 && !MAYBE_CLASS_TYPE_P (scope))
6586 cp_parser_simulate_error (parser);
6587 return error_mark_node;
6589 if (TYPE_NAME (scope))
6590 scope = TYPE_NAME (scope);
6591 return scope;
6594 /* Before we try to parse the class-name, we must save away the
6595 current PARSER->SCOPE since cp_parser_class_name will destroy
6596 it. */
6597 saved_scope = parser->scope;
6598 saved_qualifying_scope = parser->qualifying_scope;
6599 saved_object_scope = parser->object_scope;
6600 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6601 there is no need to look for a namespace-name. */
6602 only_class_p = template_keyword_p
6603 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6604 if (!only_class_p)
6605 cp_parser_parse_tentatively (parser);
6606 scope = cp_parser_class_name (parser,
6607 typename_keyword_p,
6608 template_keyword_p,
6609 type_p ? class_type : none_type,
6610 check_dependency_p,
6611 /*class_head_p=*/false,
6612 is_declaration,
6613 /*enum_ok=*/cxx_dialect > cxx98);
6614 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6615 /* If that didn't work, try for a namespace-name. */
6616 if (!only_class_p && !successful_parse_p)
6618 /* Restore the saved scope. */
6619 parser->scope = saved_scope;
6620 parser->qualifying_scope = saved_qualifying_scope;
6621 parser->object_scope = saved_object_scope;
6622 /* If we are not looking at an identifier followed by the scope
6623 resolution operator, then this is not part of a
6624 nested-name-specifier. (Note that this function is only used
6625 to parse the components of a nested-name-specifier.) */
6626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6627 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6628 return error_mark_node;
6629 scope = cp_parser_namespace_name (parser);
6632 return scope;
6635 /* Return true if we are looking at a compound-literal, false otherwise. */
6637 static bool
6638 cp_parser_compound_literal_p (cp_parser *parser)
6640 cp_lexer_save_tokens (parser->lexer);
6642 /* Skip tokens until the next token is a closing parenthesis.
6643 If we find the closing `)', and the next token is a `{', then
6644 we are looking at a compound-literal. */
6645 bool compound_literal_p
6646 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6647 /*consume_paren=*/true)
6648 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6650 /* Roll back the tokens we skipped. */
6651 cp_lexer_rollback_tokens (parser->lexer);
6653 return compound_literal_p;
6656 /* Return true if EXPR is the integer constant zero or a complex constant
6657 of zero, without any folding, but ignoring location wrappers. */
6659 bool
6660 literal_integer_zerop (const_tree expr)
6662 return (location_wrapper_p (expr)
6663 && integer_zerop (TREE_OPERAND (expr, 0)));
6666 /* Parse a postfix-expression.
6668 postfix-expression:
6669 primary-expression
6670 postfix-expression [ expression ]
6671 postfix-expression ( expression-list [opt] )
6672 simple-type-specifier ( expression-list [opt] )
6673 typename :: [opt] nested-name-specifier identifier
6674 ( expression-list [opt] )
6675 typename :: [opt] nested-name-specifier template [opt] template-id
6676 ( expression-list [opt] )
6677 postfix-expression . template [opt] id-expression
6678 postfix-expression -> template [opt] id-expression
6679 postfix-expression . pseudo-destructor-name
6680 postfix-expression -> pseudo-destructor-name
6681 postfix-expression ++
6682 postfix-expression --
6683 dynamic_cast < type-id > ( expression )
6684 static_cast < type-id > ( expression )
6685 reinterpret_cast < type-id > ( expression )
6686 const_cast < type-id > ( expression )
6687 typeid ( expression )
6688 typeid ( type-id )
6690 GNU Extension:
6692 postfix-expression:
6693 ( type-id ) { initializer-list , [opt] }
6695 This extension is a GNU version of the C99 compound-literal
6696 construct. (The C99 grammar uses `type-name' instead of `type-id',
6697 but they are essentially the same concept.)
6699 If ADDRESS_P is true, the postfix expression is the operand of the
6700 `&' operator. CAST_P is true if this expression is the target of a
6701 cast.
6703 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6704 class member access expressions [expr.ref].
6706 Returns a representation of the expression. */
6708 static cp_expr
6709 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6710 bool member_access_only_p, bool decltype_p,
6711 cp_id_kind * pidk_return)
6713 cp_token *token;
6714 location_t loc;
6715 enum rid keyword;
6716 cp_id_kind idk = CP_ID_KIND_NONE;
6717 cp_expr postfix_expression = NULL_TREE;
6718 bool is_member_access = false;
6720 /* Peek at the next token. */
6721 token = cp_lexer_peek_token (parser->lexer);
6722 loc = token->location;
6723 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6725 /* Some of the productions are determined by keywords. */
6726 keyword = token->keyword;
6727 switch (keyword)
6729 case RID_DYNCAST:
6730 case RID_STATCAST:
6731 case RID_REINTCAST:
6732 case RID_CONSTCAST:
6734 tree type;
6735 cp_expr expression;
6736 const char *saved_message;
6737 bool saved_in_type_id_in_expr_p;
6739 /* All of these can be handled in the same way from the point
6740 of view of parsing. Begin by consuming the token
6741 identifying the cast. */
6742 cp_lexer_consume_token (parser->lexer);
6744 /* New types cannot be defined in the cast. */
6745 saved_message = parser->type_definition_forbidden_message;
6746 parser->type_definition_forbidden_message
6747 = G_("types may not be defined in casts");
6749 /* Look for the opening `<'. */
6750 cp_parser_require (parser, CPP_LESS, RT_LESS);
6751 /* Parse the type to which we are casting. */
6752 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6753 parser->in_type_id_in_expr_p = true;
6754 type = cp_parser_type_id (parser);
6755 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6756 /* Look for the closing `>'. */
6757 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6758 /* Restore the old message. */
6759 parser->type_definition_forbidden_message = saved_message;
6761 bool saved_greater_than_is_operator_p
6762 = parser->greater_than_is_operator_p;
6763 parser->greater_than_is_operator_p = true;
6765 /* And the expression which is being cast. */
6766 matching_parens parens;
6767 parens.require_open (parser);
6768 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6769 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6770 RT_CLOSE_PAREN);
6771 location_t end_loc = close_paren ?
6772 close_paren->location : UNKNOWN_LOCATION;
6774 parser->greater_than_is_operator_p
6775 = saved_greater_than_is_operator_p;
6777 /* Only type conversions to integral or enumeration types
6778 can be used in constant-expressions. */
6779 if (!cast_valid_in_integral_constant_expression_p (type)
6780 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6782 postfix_expression = error_mark_node;
6783 break;
6786 switch (keyword)
6788 case RID_DYNCAST:
6789 postfix_expression
6790 = build_dynamic_cast (type, expression, tf_warning_or_error);
6791 break;
6792 case RID_STATCAST:
6793 postfix_expression
6794 = build_static_cast (type, expression, tf_warning_or_error);
6795 break;
6796 case RID_REINTCAST:
6797 postfix_expression
6798 = build_reinterpret_cast (type, expression,
6799 tf_warning_or_error);
6800 break;
6801 case RID_CONSTCAST:
6802 postfix_expression
6803 = build_const_cast (type, expression, tf_warning_or_error);
6804 break;
6805 default:
6806 gcc_unreachable ();
6809 /* Construct a location e.g. :
6810 reinterpret_cast <int *> (expr)
6811 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6812 ranging from the start of the "*_cast" token to the final closing
6813 paren, with the caret at the start. */
6814 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6815 postfix_expression.set_location (cp_cast_loc);
6817 break;
6819 case RID_TYPEID:
6821 tree type;
6822 const char *saved_message;
6823 bool saved_in_type_id_in_expr_p;
6825 /* Consume the `typeid' token. */
6826 cp_lexer_consume_token (parser->lexer);
6827 /* Look for the `(' token. */
6828 matching_parens parens;
6829 parens.require_open (parser);
6830 /* Types cannot be defined in a `typeid' expression. */
6831 saved_message = parser->type_definition_forbidden_message;
6832 parser->type_definition_forbidden_message
6833 = G_("types may not be defined in a %<typeid%> expression");
6834 /* We can't be sure yet whether we're looking at a type-id or an
6835 expression. */
6836 cp_parser_parse_tentatively (parser);
6837 /* Try a type-id first. */
6838 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6839 parser->in_type_id_in_expr_p = true;
6840 type = cp_parser_type_id (parser);
6841 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6842 /* Look for the `)' token. Otherwise, we can't be sure that
6843 we're not looking at an expression: consider `typeid (int
6844 (3))', for example. */
6845 cp_token *close_paren = parens.require_close (parser);
6846 /* If all went well, simply lookup the type-id. */
6847 if (cp_parser_parse_definitely (parser))
6848 postfix_expression = get_typeid (type, tf_warning_or_error);
6849 /* Otherwise, fall back to the expression variant. */
6850 else
6852 tree expression;
6854 /* Look for an expression. */
6855 expression = cp_parser_expression (parser, & idk);
6856 /* Compute its typeid. */
6857 postfix_expression = build_typeid (expression, tf_warning_or_error);
6858 /* Look for the `)' token. */
6859 close_paren = parens.require_close (parser);
6861 /* Restore the saved message. */
6862 parser->type_definition_forbidden_message = saved_message;
6863 /* `typeid' may not appear in an integral constant expression. */
6864 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6865 postfix_expression = error_mark_node;
6867 /* Construct a location e.g. :
6868 typeid (expr)
6869 ^~~~~~~~~~~~~
6870 ranging from the start of the "typeid" token to the final closing
6871 paren, with the caret at the start. */
6872 if (close_paren)
6874 location_t typeid_loc
6875 = make_location (start_loc, start_loc, close_paren->location);
6876 postfix_expression.set_location (typeid_loc);
6877 postfix_expression.maybe_add_location_wrapper ();
6880 break;
6882 case RID_TYPENAME:
6884 tree type;
6885 /* The syntax permitted here is the same permitted for an
6886 elaborated-type-specifier. */
6887 ++parser->prevent_constrained_type_specifiers;
6888 type = cp_parser_elaborated_type_specifier (parser,
6889 /*is_friend=*/false,
6890 /*is_declaration=*/false);
6891 --parser->prevent_constrained_type_specifiers;
6892 postfix_expression = cp_parser_functional_cast (parser, type);
6894 break;
6896 case RID_ADDRESSOF:
6897 case RID_BUILTIN_SHUFFLE:
6898 case RID_BUILTIN_LAUNDER:
6900 vec<tree, va_gc> *vec;
6901 unsigned int i;
6902 tree p;
6904 cp_lexer_consume_token (parser->lexer);
6905 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6906 /*cast_p=*/false, /*allow_expansion_p=*/true,
6907 /*non_constant_p=*/NULL);
6908 if (vec == NULL)
6910 postfix_expression = error_mark_node;
6911 break;
6914 FOR_EACH_VEC_ELT (*vec, i, p)
6915 mark_exp_read (p);
6917 switch (keyword)
6919 case RID_ADDRESSOF:
6920 if (vec->length () == 1)
6921 postfix_expression
6922 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6923 else
6925 error_at (loc, "wrong number of arguments to "
6926 "%<__builtin_addressof%>");
6927 postfix_expression = error_mark_node;
6929 break;
6931 case RID_BUILTIN_LAUNDER:
6932 if (vec->length () == 1)
6933 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6934 tf_warning_or_error);
6935 else
6937 error_at (loc, "wrong number of arguments to "
6938 "%<__builtin_launder%>");
6939 postfix_expression = error_mark_node;
6941 break;
6943 case RID_BUILTIN_SHUFFLE:
6944 if (vec->length () == 2)
6945 postfix_expression
6946 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6947 (*vec)[1], tf_warning_or_error);
6948 else if (vec->length () == 3)
6949 postfix_expression
6950 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6951 (*vec)[2], tf_warning_or_error);
6952 else
6954 error_at (loc, "wrong number of arguments to "
6955 "%<__builtin_shuffle%>");
6956 postfix_expression = error_mark_node;
6958 break;
6960 default:
6961 gcc_unreachable ();
6963 break;
6966 default:
6968 tree type;
6970 /* If the next thing is a simple-type-specifier, we may be
6971 looking at a functional cast. We could also be looking at
6972 an id-expression. So, we try the functional cast, and if
6973 that doesn't work we fall back to the primary-expression. */
6974 cp_parser_parse_tentatively (parser);
6975 /* Look for the simple-type-specifier. */
6976 ++parser->prevent_constrained_type_specifiers;
6977 type = cp_parser_simple_type_specifier (parser,
6978 /*decl_specs=*/NULL,
6979 CP_PARSER_FLAGS_NONE);
6980 --parser->prevent_constrained_type_specifiers;
6981 /* Parse the cast itself. */
6982 if (!cp_parser_error_occurred (parser))
6983 postfix_expression
6984 = cp_parser_functional_cast (parser, type);
6985 /* If that worked, we're done. */
6986 if (cp_parser_parse_definitely (parser))
6987 break;
6989 /* If the functional-cast didn't work out, try a
6990 compound-literal. */
6991 if (cp_parser_allow_gnu_extensions_p (parser)
6992 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6994 cp_expr initializer = NULL_TREE;
6996 cp_parser_parse_tentatively (parser);
6998 matching_parens parens;
6999 parens.consume_open (parser);
7001 /* Avoid calling cp_parser_type_id pointlessly, see comment
7002 in cp_parser_cast_expression about c++/29234. */
7003 if (!cp_parser_compound_literal_p (parser))
7004 cp_parser_simulate_error (parser);
7005 else
7007 /* Parse the type. */
7008 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7009 parser->in_type_id_in_expr_p = true;
7010 type = cp_parser_type_id (parser);
7011 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7012 parens.require_close (parser);
7015 /* If things aren't going well, there's no need to
7016 keep going. */
7017 if (!cp_parser_error_occurred (parser))
7019 bool non_constant_p;
7020 /* Parse the brace-enclosed initializer list. */
7021 initializer = cp_parser_braced_list (parser,
7022 &non_constant_p);
7024 /* If that worked, we're definitely looking at a
7025 compound-literal expression. */
7026 if (cp_parser_parse_definitely (parser))
7028 /* Warn the user that a compound literal is not
7029 allowed in standard C++. */
7030 pedwarn (input_location, OPT_Wpedantic,
7031 "ISO C++ forbids compound-literals");
7032 /* For simplicity, we disallow compound literals in
7033 constant-expressions. We could
7034 allow compound literals of integer type, whose
7035 initializer was a constant, in constant
7036 expressions. Permitting that usage, as a further
7037 extension, would not change the meaning of any
7038 currently accepted programs. (Of course, as
7039 compound literals are not part of ISO C++, the
7040 standard has nothing to say.) */
7041 if (cp_parser_non_integral_constant_expression (parser,
7042 NIC_NCC))
7044 postfix_expression = error_mark_node;
7045 break;
7047 /* Form the representation of the compound-literal. */
7048 postfix_expression
7049 = finish_compound_literal (type, initializer,
7050 tf_warning_or_error, fcl_c99);
7051 postfix_expression.set_location (initializer.get_location ());
7052 break;
7056 /* It must be a primary-expression. */
7057 postfix_expression
7058 = cp_parser_primary_expression (parser, address_p, cast_p,
7059 /*template_arg_p=*/false,
7060 decltype_p,
7061 &idk);
7063 break;
7066 /* Note that we don't need to worry about calling build_cplus_new on a
7067 class-valued CALL_EXPR in decltype when it isn't the end of the
7068 postfix-expression; unary_complex_lvalue will take care of that for
7069 all these cases. */
7071 /* Keep looping until the postfix-expression is complete. */
7072 while (true)
7074 if (idk == CP_ID_KIND_UNQUALIFIED
7075 && identifier_p (postfix_expression)
7076 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7077 /* It is not a Koenig lookup function call. */
7078 postfix_expression
7079 = unqualified_name_lookup_error (postfix_expression);
7081 /* Peek at the next token. */
7082 token = cp_lexer_peek_token (parser->lexer);
7084 switch (token->type)
7086 case CPP_OPEN_SQUARE:
7087 if (cp_next_tokens_can_be_std_attribute_p (parser))
7089 cp_parser_error (parser,
7090 "two consecutive %<[%> shall "
7091 "only introduce an attribute");
7092 return error_mark_node;
7094 postfix_expression
7095 = cp_parser_postfix_open_square_expression (parser,
7096 postfix_expression,
7097 false,
7098 decltype_p);
7099 postfix_expression.set_range (start_loc,
7100 postfix_expression.get_location ());
7102 idk = CP_ID_KIND_NONE;
7103 is_member_access = false;
7104 break;
7106 case CPP_OPEN_PAREN:
7107 /* postfix-expression ( expression-list [opt] ) */
7109 bool koenig_p;
7110 bool is_builtin_constant_p;
7111 bool saved_integral_constant_expression_p = false;
7112 bool saved_non_integral_constant_expression_p = false;
7113 tsubst_flags_t complain = complain_flags (decltype_p);
7114 vec<tree, va_gc> *args;
7115 location_t close_paren_loc = UNKNOWN_LOCATION;
7117 is_member_access = false;
7119 is_builtin_constant_p
7120 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7121 if (is_builtin_constant_p)
7123 /* The whole point of __builtin_constant_p is to allow
7124 non-constant expressions to appear as arguments. */
7125 saved_integral_constant_expression_p
7126 = parser->integral_constant_expression_p;
7127 saved_non_integral_constant_expression_p
7128 = parser->non_integral_constant_expression_p;
7129 parser->integral_constant_expression_p = false;
7131 args = (cp_parser_parenthesized_expression_list
7132 (parser, non_attr,
7133 /*cast_p=*/false, /*allow_expansion_p=*/true,
7134 /*non_constant_p=*/NULL,
7135 /*close_paren_loc=*/&close_paren_loc,
7136 /*wrap_locations_p=*/true));
7137 if (is_builtin_constant_p)
7139 parser->integral_constant_expression_p
7140 = saved_integral_constant_expression_p;
7141 parser->non_integral_constant_expression_p
7142 = saved_non_integral_constant_expression_p;
7145 if (args == NULL)
7147 postfix_expression = error_mark_node;
7148 break;
7151 /* Function calls are not permitted in
7152 constant-expressions. */
7153 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7154 && cp_parser_non_integral_constant_expression (parser,
7155 NIC_FUNC_CALL))
7157 postfix_expression = error_mark_node;
7158 release_tree_vector (args);
7159 break;
7162 koenig_p = false;
7163 if (idk == CP_ID_KIND_UNQUALIFIED
7164 || idk == CP_ID_KIND_TEMPLATE_ID)
7166 if (identifier_p (postfix_expression))
7168 if (!args->is_empty ())
7170 koenig_p = true;
7171 if (!any_type_dependent_arguments_p (args))
7172 postfix_expression
7173 = perform_koenig_lookup (postfix_expression, args,
7174 complain);
7176 else
7177 postfix_expression
7178 = unqualified_fn_lookup_error (postfix_expression);
7180 /* We do not perform argument-dependent lookup if
7181 normal lookup finds a non-function, in accordance
7182 with the expected resolution of DR 218. */
7183 else if (!args->is_empty ()
7184 && is_overloaded_fn (postfix_expression))
7186 tree fn = get_first_fn (postfix_expression);
7187 fn = STRIP_TEMPLATE (fn);
7189 /* Do not do argument dependent lookup if regular
7190 lookup finds a member function or a block-scope
7191 function declaration. [basic.lookup.argdep]/3 */
7192 if (!DECL_FUNCTION_MEMBER_P (fn)
7193 && !DECL_LOCAL_FUNCTION_P (fn))
7195 koenig_p = true;
7196 if (!any_type_dependent_arguments_p (args))
7197 postfix_expression
7198 = perform_koenig_lookup (postfix_expression, args,
7199 complain);
7204 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7206 tree instance = TREE_OPERAND (postfix_expression, 0);
7207 tree fn = TREE_OPERAND (postfix_expression, 1);
7209 if (processing_template_decl
7210 && (type_dependent_object_expression_p (instance)
7211 || (!BASELINK_P (fn)
7212 && TREE_CODE (fn) != FIELD_DECL)
7213 || type_dependent_expression_p (fn)
7214 || any_type_dependent_arguments_p (args)))
7216 maybe_generic_this_capture (instance, fn);
7217 postfix_expression
7218 = build_min_nt_call_vec (postfix_expression, args);
7219 release_tree_vector (args);
7220 break;
7223 if (BASELINK_P (fn))
7225 postfix_expression
7226 = (build_new_method_call
7227 (instance, fn, &args, NULL_TREE,
7228 (idk == CP_ID_KIND_QUALIFIED
7229 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7230 : LOOKUP_NORMAL),
7231 /*fn_p=*/NULL,
7232 complain));
7234 else
7235 postfix_expression
7236 = finish_call_expr (postfix_expression, &args,
7237 /*disallow_virtual=*/false,
7238 /*koenig_p=*/false,
7239 complain);
7241 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7242 || TREE_CODE (postfix_expression) == MEMBER_REF
7243 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7244 postfix_expression = (build_offset_ref_call_from_tree
7245 (postfix_expression, &args,
7246 complain));
7247 else if (idk == CP_ID_KIND_QUALIFIED)
7248 /* A call to a static class member, or a namespace-scope
7249 function. */
7250 postfix_expression
7251 = finish_call_expr (postfix_expression, &args,
7252 /*disallow_virtual=*/true,
7253 koenig_p,
7254 complain);
7255 else
7256 /* All other function calls. */
7257 postfix_expression
7258 = finish_call_expr (postfix_expression, &args,
7259 /*disallow_virtual=*/false,
7260 koenig_p,
7261 complain);
7263 if (close_paren_loc != UNKNOWN_LOCATION)
7265 location_t combined_loc = make_location (token->location,
7266 start_loc,
7267 close_paren_loc);
7268 postfix_expression.set_location (combined_loc);
7271 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7272 idk = CP_ID_KIND_NONE;
7274 release_tree_vector (args);
7276 break;
7278 case CPP_DOT:
7279 case CPP_DEREF:
7280 /* postfix-expression . template [opt] id-expression
7281 postfix-expression . pseudo-destructor-name
7282 postfix-expression -> template [opt] id-expression
7283 postfix-expression -> pseudo-destructor-name */
7285 /* Consume the `.' or `->' operator. */
7286 cp_lexer_consume_token (parser->lexer);
7288 postfix_expression
7289 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7290 postfix_expression,
7291 false, &idk, loc);
7293 is_member_access = true;
7294 break;
7296 case CPP_PLUS_PLUS:
7297 /* postfix-expression ++ */
7298 /* Consume the `++' token. */
7299 cp_lexer_consume_token (parser->lexer);
7300 /* Generate a representation for the complete expression. */
7301 postfix_expression
7302 = finish_increment_expr (postfix_expression,
7303 POSTINCREMENT_EXPR);
7304 /* Increments may not appear in constant-expressions. */
7305 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7306 postfix_expression = error_mark_node;
7307 idk = CP_ID_KIND_NONE;
7308 is_member_access = false;
7309 break;
7311 case CPP_MINUS_MINUS:
7312 /* postfix-expression -- */
7313 /* Consume the `--' token. */
7314 cp_lexer_consume_token (parser->lexer);
7315 /* Generate a representation for the complete expression. */
7316 postfix_expression
7317 = finish_increment_expr (postfix_expression,
7318 POSTDECREMENT_EXPR);
7319 /* Decrements may not appear in constant-expressions. */
7320 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7321 postfix_expression = error_mark_node;
7322 idk = CP_ID_KIND_NONE;
7323 is_member_access = false;
7324 break;
7326 default:
7327 if (pidk_return != NULL)
7328 * pidk_return = idk;
7329 if (member_access_only_p)
7330 return is_member_access
7331 ? postfix_expression
7332 : cp_expr (error_mark_node);
7333 else
7334 return postfix_expression;
7338 /* We should never get here. */
7339 gcc_unreachable ();
7340 return error_mark_node;
7343 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7344 by cp_parser_builtin_offsetof. We're looking for
7346 postfix-expression [ expression ]
7347 postfix-expression [ braced-init-list ] (C++11)
7349 FOR_OFFSETOF is set if we're being called in that context, which
7350 changes how we deal with integer constant expressions. */
7352 static tree
7353 cp_parser_postfix_open_square_expression (cp_parser *parser,
7354 tree postfix_expression,
7355 bool for_offsetof,
7356 bool decltype_p)
7358 tree index = NULL_TREE;
7359 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7360 bool saved_greater_than_is_operator_p;
7362 /* Consume the `[' token. */
7363 cp_lexer_consume_token (parser->lexer);
7365 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7366 parser->greater_than_is_operator_p = true;
7368 /* Parse the index expression. */
7369 /* ??? For offsetof, there is a question of what to allow here. If
7370 offsetof is not being used in an integral constant expression context,
7371 then we *could* get the right answer by computing the value at runtime.
7372 If we are in an integral constant expression context, then we might
7373 could accept any constant expression; hard to say without analysis.
7374 Rather than open the barn door too wide right away, allow only integer
7375 constant expressions here. */
7376 if (for_offsetof)
7377 index = cp_parser_constant_expression (parser);
7378 else
7380 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7382 bool expr_nonconst_p;
7383 cp_lexer_set_source_position (parser->lexer);
7384 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7385 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7387 else
7388 index = cp_parser_expression (parser);
7391 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7393 /* Look for the closing `]'. */
7394 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7396 /* Build the ARRAY_REF. */
7397 postfix_expression = grok_array_decl (loc, postfix_expression,
7398 index, decltype_p);
7400 /* When not doing offsetof, array references are not permitted in
7401 constant-expressions. */
7402 if (!for_offsetof
7403 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7404 postfix_expression = error_mark_node;
7406 return postfix_expression;
7409 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7410 dereference of incomplete type, returns true if error_mark_node should
7411 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7412 and *DEPENDENT_P. */
7414 bool
7415 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7416 bool *dependent_p)
7418 /* In a template, be permissive by treating an object expression
7419 of incomplete type as dependent (after a pedwarn). */
7420 diagnostic_t kind = (processing_template_decl
7421 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7423 switch (TREE_CODE (*postfix_expression))
7425 case CAST_EXPR:
7426 case REINTERPRET_CAST_EXPR:
7427 case CONST_CAST_EXPR:
7428 case STATIC_CAST_EXPR:
7429 case DYNAMIC_CAST_EXPR:
7430 case IMPLICIT_CONV_EXPR:
7431 case VIEW_CONVERT_EXPR:
7432 case NON_LVALUE_EXPR:
7433 kind = DK_ERROR;
7434 break;
7435 case OVERLOAD:
7436 /* Don't emit any diagnostic for OVERLOADs. */
7437 kind = DK_IGNORED;
7438 break;
7439 default:
7440 /* Avoid clobbering e.g. DECLs. */
7441 if (!EXPR_P (*postfix_expression))
7442 kind = DK_ERROR;
7443 break;
7446 if (kind == DK_IGNORED)
7447 return false;
7449 location_t exploc = location_of (*postfix_expression);
7450 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7451 if (!MAYBE_CLASS_TYPE_P (*scope))
7452 return true;
7453 if (kind == DK_ERROR)
7454 *scope = *postfix_expression = error_mark_node;
7455 else if (processing_template_decl)
7457 *dependent_p = true;
7458 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7460 return false;
7463 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7464 by cp_parser_builtin_offsetof. We're looking for
7466 postfix-expression . template [opt] id-expression
7467 postfix-expression . pseudo-destructor-name
7468 postfix-expression -> template [opt] id-expression
7469 postfix-expression -> pseudo-destructor-name
7471 FOR_OFFSETOF is set if we're being called in that context. That sorta
7472 limits what of the above we'll actually accept, but nevermind.
7473 TOKEN_TYPE is the "." or "->" token, which will already have been
7474 removed from the stream. */
7476 static tree
7477 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7478 enum cpp_ttype token_type,
7479 cp_expr postfix_expression,
7480 bool for_offsetof, cp_id_kind *idk,
7481 location_t location)
7483 tree name;
7484 bool dependent_p;
7485 bool pseudo_destructor_p;
7486 tree scope = NULL_TREE;
7487 location_t start_loc = postfix_expression.get_start ();
7489 /* If this is a `->' operator, dereference the pointer. */
7490 if (token_type == CPP_DEREF)
7491 postfix_expression = build_x_arrow (location, postfix_expression,
7492 tf_warning_or_error);
7493 /* Check to see whether or not the expression is type-dependent and
7494 not the current instantiation. */
7495 dependent_p = type_dependent_object_expression_p (postfix_expression);
7496 /* The identifier following the `->' or `.' is not qualified. */
7497 parser->scope = NULL_TREE;
7498 parser->qualifying_scope = NULL_TREE;
7499 parser->object_scope = NULL_TREE;
7500 *idk = CP_ID_KIND_NONE;
7502 /* Enter the scope corresponding to the type of the object
7503 given by the POSTFIX_EXPRESSION. */
7504 if (!dependent_p)
7506 scope = TREE_TYPE (postfix_expression);
7507 /* According to the standard, no expression should ever have
7508 reference type. Unfortunately, we do not currently match
7509 the standard in this respect in that our internal representation
7510 of an expression may have reference type even when the standard
7511 says it does not. Therefore, we have to manually obtain the
7512 underlying type here. */
7513 scope = non_reference (scope);
7514 /* The type of the POSTFIX_EXPRESSION must be complete. */
7515 /* Unlike the object expression in other contexts, *this is not
7516 required to be of complete type for purposes of class member
7517 access (5.2.5) outside the member function body. */
7518 if (postfix_expression != current_class_ref
7519 && scope != error_mark_node
7520 && !currently_open_class (scope))
7522 scope = complete_type (scope);
7523 if (!COMPLETE_TYPE_P (scope)
7524 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7525 &dependent_p))
7526 return error_mark_node;
7529 if (!dependent_p)
7531 /* Let the name lookup machinery know that we are processing a
7532 class member access expression. */
7533 parser->context->object_type = scope;
7534 /* If something went wrong, we want to be able to discern that case,
7535 as opposed to the case where there was no SCOPE due to the type
7536 of expression being dependent. */
7537 if (!scope)
7538 scope = error_mark_node;
7539 /* If the SCOPE was erroneous, make the various semantic analysis
7540 functions exit quickly -- and without issuing additional error
7541 messages. */
7542 if (scope == error_mark_node)
7543 postfix_expression = error_mark_node;
7547 if (dependent_p)
7548 /* Tell cp_parser_lookup_name that there was an object, even though it's
7549 type-dependent. */
7550 parser->context->object_type = unknown_type_node;
7552 /* Assume this expression is not a pseudo-destructor access. */
7553 pseudo_destructor_p = false;
7555 /* If the SCOPE is a scalar type, then, if this is a valid program,
7556 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7557 is type dependent, it can be pseudo-destructor-name or something else.
7558 Try to parse it as pseudo-destructor-name first. */
7559 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7561 tree s;
7562 tree type;
7564 cp_parser_parse_tentatively (parser);
7565 /* Parse the pseudo-destructor-name. */
7566 s = NULL_TREE;
7567 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7568 &s, &type);
7569 if (dependent_p
7570 && (cp_parser_error_occurred (parser)
7571 || !SCALAR_TYPE_P (type)))
7572 cp_parser_abort_tentative_parse (parser);
7573 else if (cp_parser_parse_definitely (parser))
7575 pseudo_destructor_p = true;
7576 postfix_expression
7577 = finish_pseudo_destructor_expr (postfix_expression,
7578 s, type, location);
7582 if (!pseudo_destructor_p)
7584 /* If the SCOPE is not a scalar type, we are looking at an
7585 ordinary class member access expression, rather than a
7586 pseudo-destructor-name. */
7587 bool template_p;
7588 cp_token *token = cp_lexer_peek_token (parser->lexer);
7589 /* Parse the id-expression. */
7590 name = (cp_parser_id_expression
7591 (parser,
7592 cp_parser_optional_template_keyword (parser),
7593 /*check_dependency_p=*/true,
7594 &template_p,
7595 /*declarator_p=*/false,
7596 /*optional_p=*/false));
7597 /* In general, build a SCOPE_REF if the member name is qualified.
7598 However, if the name was not dependent and has already been
7599 resolved; there is no need to build the SCOPE_REF. For example;
7601 struct X { void f(); };
7602 template <typename T> void f(T* t) { t->X::f(); }
7604 Even though "t" is dependent, "X::f" is not and has been resolved
7605 to a BASELINK; there is no need to include scope information. */
7607 /* But we do need to remember that there was an explicit scope for
7608 virtual function calls. */
7609 if (parser->scope)
7610 *idk = CP_ID_KIND_QUALIFIED;
7612 /* If the name is a template-id that names a type, we will get a
7613 TYPE_DECL here. That is invalid code. */
7614 if (TREE_CODE (name) == TYPE_DECL)
7616 error_at (token->location, "invalid use of %qD", name);
7617 postfix_expression = error_mark_node;
7619 else
7621 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7623 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7625 error_at (token->location, "%<%D::%D%> is not a class member",
7626 parser->scope, name);
7627 postfix_expression = error_mark_node;
7629 else
7630 name = build_qualified_name (/*type=*/NULL_TREE,
7631 parser->scope,
7632 name,
7633 template_p);
7634 parser->scope = NULL_TREE;
7635 parser->qualifying_scope = NULL_TREE;
7636 parser->object_scope = NULL_TREE;
7638 if (parser->scope && name && BASELINK_P (name))
7639 adjust_result_of_qualified_name_lookup
7640 (name, parser->scope, scope);
7641 postfix_expression
7642 = finish_class_member_access_expr (postfix_expression, name,
7643 template_p,
7644 tf_warning_or_error);
7645 /* Build a location e.g.:
7646 ptr->access_expr
7647 ~~~^~~~~~~~~~~~~
7648 where the caret is at the deref token, ranging from
7649 the start of postfix_expression to the end of the access expr. */
7650 location_t end_loc
7651 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7652 location_t combined_loc
7653 = make_location (input_location, start_loc, end_loc);
7654 protected_set_expr_location (postfix_expression, combined_loc);
7658 /* We no longer need to look up names in the scope of the object on
7659 the left-hand side of the `.' or `->' operator. */
7660 parser->context->object_type = NULL_TREE;
7662 /* Outside of offsetof, these operators may not appear in
7663 constant-expressions. */
7664 if (!for_offsetof
7665 && (cp_parser_non_integral_constant_expression
7666 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7667 postfix_expression = error_mark_node;
7669 return postfix_expression;
7672 /* Parse a parenthesized expression-list.
7674 expression-list:
7675 assignment-expression
7676 expression-list, assignment-expression
7678 attribute-list:
7679 expression-list
7680 identifier
7681 identifier, expression-list
7683 CAST_P is true if this expression is the target of a cast.
7685 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7686 argument pack.
7688 WRAP_LOCATIONS_P is true if expressions within this list for which
7689 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7690 their source locations.
7692 Returns a vector of trees. Each element is a representation of an
7693 assignment-expression. NULL is returned if the ( and or ) are
7694 missing. An empty, but allocated, vector is returned on no
7695 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7696 if we are parsing an attribute list for an attribute that wants a
7697 plain identifier argument, normal_attr for an attribute that wants
7698 an expression, or non_attr if we aren't parsing an attribute list. If
7699 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7700 not all of the expressions in the list were constant.
7701 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7702 will be written to with the location of the closing parenthesis. If
7703 an error occurs, it may or may not be written to. */
7705 static vec<tree, va_gc> *
7706 cp_parser_parenthesized_expression_list (cp_parser* parser,
7707 int is_attribute_list,
7708 bool cast_p,
7709 bool allow_expansion_p,
7710 bool *non_constant_p,
7711 location_t *close_paren_loc,
7712 bool wrap_locations_p)
7714 vec<tree, va_gc> *expression_list;
7715 bool fold_expr_p = is_attribute_list != non_attr;
7716 tree identifier = NULL_TREE;
7717 bool saved_greater_than_is_operator_p;
7719 /* Assume all the expressions will be constant. */
7720 if (non_constant_p)
7721 *non_constant_p = false;
7723 matching_parens parens;
7724 if (!parens.require_open (parser))
7725 return NULL;
7727 expression_list = make_tree_vector ();
7729 /* Within a parenthesized expression, a `>' token is always
7730 the greater-than operator. */
7731 saved_greater_than_is_operator_p
7732 = parser->greater_than_is_operator_p;
7733 parser->greater_than_is_operator_p = true;
7735 cp_expr expr (NULL_TREE);
7737 /* Consume expressions until there are no more. */
7738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7739 while (true)
7741 /* At the beginning of attribute lists, check to see if the
7742 next token is an identifier. */
7743 if (is_attribute_list == id_attr
7744 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7746 cp_token *token;
7748 /* Consume the identifier. */
7749 token = cp_lexer_consume_token (parser->lexer);
7750 /* Save the identifier. */
7751 identifier = token->u.value;
7753 else
7755 bool expr_non_constant_p;
7757 /* Parse the next assignment-expression. */
7758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7760 /* A braced-init-list. */
7761 cp_lexer_set_source_position (parser->lexer);
7762 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7763 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7764 if (non_constant_p && expr_non_constant_p)
7765 *non_constant_p = true;
7767 else if (non_constant_p)
7769 expr = (cp_parser_constant_expression
7770 (parser, /*allow_non_constant_p=*/true,
7771 &expr_non_constant_p));
7772 if (expr_non_constant_p)
7773 *non_constant_p = true;
7775 else
7776 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7777 cast_p);
7779 if (fold_expr_p)
7780 expr = instantiate_non_dependent_expr (expr);
7782 /* If we have an ellipsis, then this is an expression
7783 expansion. */
7784 if (allow_expansion_p
7785 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7787 /* Consume the `...'. */
7788 cp_lexer_consume_token (parser->lexer);
7790 /* Build the argument pack. */
7791 expr = make_pack_expansion (expr);
7794 if (wrap_locations_p)
7795 expr.maybe_add_location_wrapper ();
7797 /* Add it to the list. We add error_mark_node
7798 expressions to the list, so that we can still tell if
7799 the correct form for a parenthesized expression-list
7800 is found. That gives better errors. */
7801 vec_safe_push (expression_list, expr.get_value ());
7803 if (expr == error_mark_node)
7804 goto skip_comma;
7807 /* After the first item, attribute lists look the same as
7808 expression lists. */
7809 is_attribute_list = non_attr;
7811 get_comma:;
7812 /* If the next token isn't a `,', then we are done. */
7813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7814 break;
7816 /* Otherwise, consume the `,' and keep going. */
7817 cp_lexer_consume_token (parser->lexer);
7820 if (close_paren_loc)
7821 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7823 if (!parens.require_close (parser))
7825 int ending;
7827 skip_comma:;
7828 /* We try and resync to an unnested comma, as that will give the
7829 user better diagnostics. */
7830 ending = cp_parser_skip_to_closing_parenthesis (parser,
7831 /*recovering=*/true,
7832 /*or_comma=*/true,
7833 /*consume_paren=*/true);
7834 if (ending < 0)
7835 goto get_comma;
7836 if (!ending)
7838 parser->greater_than_is_operator_p
7839 = saved_greater_than_is_operator_p;
7840 return NULL;
7844 parser->greater_than_is_operator_p
7845 = saved_greater_than_is_operator_p;
7847 if (identifier)
7848 vec_safe_insert (expression_list, 0, identifier);
7850 return expression_list;
7853 /* Parse a pseudo-destructor-name.
7855 pseudo-destructor-name:
7856 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7857 :: [opt] nested-name-specifier template template-id :: ~ type-name
7858 :: [opt] nested-name-specifier [opt] ~ type-name
7860 If either of the first two productions is used, sets *SCOPE to the
7861 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7862 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7863 or ERROR_MARK_NODE if the parse fails. */
7865 static void
7866 cp_parser_pseudo_destructor_name (cp_parser* parser,
7867 tree object,
7868 tree* scope,
7869 tree* type)
7871 bool nested_name_specifier_p;
7873 /* Handle ~auto. */
7874 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7875 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7876 && !type_dependent_expression_p (object))
7878 if (cxx_dialect < cxx14)
7879 pedwarn (input_location, 0,
7880 "%<~auto%> only available with "
7881 "-std=c++14 or -std=gnu++14");
7882 cp_lexer_consume_token (parser->lexer);
7883 cp_lexer_consume_token (parser->lexer);
7884 *scope = NULL_TREE;
7885 *type = TREE_TYPE (object);
7886 return;
7889 /* Assume that things will not work out. */
7890 *type = error_mark_node;
7892 /* Look for the optional `::' operator. */
7893 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7894 /* Look for the optional nested-name-specifier. */
7895 nested_name_specifier_p
7896 = (cp_parser_nested_name_specifier_opt (parser,
7897 /*typename_keyword_p=*/false,
7898 /*check_dependency_p=*/true,
7899 /*type_p=*/false,
7900 /*is_declaration=*/false)
7901 != NULL_TREE);
7902 /* Now, if we saw a nested-name-specifier, we might be doing the
7903 second production. */
7904 if (nested_name_specifier_p
7905 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7907 /* Consume the `template' keyword. */
7908 cp_lexer_consume_token (parser->lexer);
7909 /* Parse the template-id. */
7910 cp_parser_template_id (parser,
7911 /*template_keyword_p=*/true,
7912 /*check_dependency_p=*/false,
7913 class_type,
7914 /*is_declaration=*/true);
7915 /* Look for the `::' token. */
7916 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7918 /* If the next token is not a `~', then there might be some
7919 additional qualification. */
7920 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7922 /* At this point, we're looking for "type-name :: ~". The type-name
7923 must not be a class-name, since this is a pseudo-destructor. So,
7924 it must be either an enum-name, or a typedef-name -- both of which
7925 are just identifiers. So, we peek ahead to check that the "::"
7926 and "~" tokens are present; if they are not, then we can avoid
7927 calling type_name. */
7928 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7929 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7930 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7932 cp_parser_error (parser, "non-scalar type");
7933 return;
7936 /* Look for the type-name. */
7937 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7938 if (*scope == error_mark_node)
7939 return;
7941 /* Look for the `::' token. */
7942 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7944 else
7945 *scope = NULL_TREE;
7947 /* Look for the `~'. */
7948 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7950 /* Once we see the ~, this has to be a pseudo-destructor. */
7951 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7952 cp_parser_commit_to_topmost_tentative_parse (parser);
7954 /* Look for the type-name again. We are not responsible for
7955 checking that it matches the first type-name. */
7956 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7959 /* Parse a unary-expression.
7961 unary-expression:
7962 postfix-expression
7963 ++ cast-expression
7964 -- cast-expression
7965 unary-operator cast-expression
7966 sizeof unary-expression
7967 sizeof ( type-id )
7968 alignof ( type-id ) [C++0x]
7969 new-expression
7970 delete-expression
7972 GNU Extensions:
7974 unary-expression:
7975 __extension__ cast-expression
7976 __alignof__ unary-expression
7977 __alignof__ ( type-id )
7978 alignof unary-expression [C++0x]
7979 __real__ cast-expression
7980 __imag__ cast-expression
7981 && identifier
7982 sizeof ( type-id ) { initializer-list , [opt] }
7983 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7984 __alignof__ ( type-id ) { initializer-list , [opt] }
7986 ADDRESS_P is true iff the unary-expression is appearing as the
7987 operand of the `&' operator. CAST_P is true if this expression is
7988 the target of a cast.
7990 Returns a representation of the expression. */
7992 static cp_expr
7993 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7994 bool address_p, bool cast_p, bool decltype_p)
7996 cp_token *token;
7997 enum tree_code unary_operator;
7999 /* Peek at the next token. */
8000 token = cp_lexer_peek_token (parser->lexer);
8001 /* Some keywords give away the kind of expression. */
8002 if (token->type == CPP_KEYWORD)
8004 enum rid keyword = token->keyword;
8006 switch (keyword)
8008 case RID_ALIGNOF:
8009 case RID_SIZEOF:
8011 tree operand, ret;
8012 enum tree_code op;
8013 location_t start_loc = token->location;
8015 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8016 bool std_alignof = id_equal (token->u.value, "alignof");
8018 /* Consume the token. */
8019 cp_lexer_consume_token (parser->lexer);
8020 /* Parse the operand. */
8021 operand = cp_parser_sizeof_operand (parser, keyword);
8023 if (TYPE_P (operand))
8024 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8025 true);
8026 else
8028 /* ISO C++ defines alignof only with types, not with
8029 expressions. So pedwarn if alignof is used with a non-
8030 type expression. However, __alignof__ is ok. */
8031 if (std_alignof)
8032 pedwarn (token->location, OPT_Wpedantic,
8033 "ISO C++ does not allow %<alignof%> "
8034 "with a non-type");
8036 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8038 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8039 SIZEOF_EXPR with the original operand. */
8040 if (op == SIZEOF_EXPR && ret != error_mark_node)
8042 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8044 if (!processing_template_decl && TYPE_P (operand))
8046 ret = build_min (SIZEOF_EXPR, size_type_node,
8047 build1 (NOP_EXPR, operand,
8048 error_mark_node));
8049 SIZEOF_EXPR_TYPE_P (ret) = 1;
8051 else
8052 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8053 TREE_SIDE_EFFECTS (ret) = 0;
8054 TREE_READONLY (ret) = 1;
8058 /* Construct a location e.g. :
8059 alignof (expr)
8060 ^~~~~~~~~~~~~~
8061 with start == caret at the start of the "alignof"/"sizeof"
8062 token, with the endpoint at the final closing paren. */
8063 location_t finish_loc
8064 = cp_lexer_previous_token (parser->lexer)->location;
8065 location_t compound_loc
8066 = make_location (start_loc, start_loc, finish_loc);
8068 cp_expr ret_expr (ret);
8069 ret_expr.set_location (compound_loc);
8070 ret_expr = ret_expr.maybe_add_location_wrapper ();
8071 return ret_expr;
8074 case RID_NEW:
8075 return cp_parser_new_expression (parser);
8077 case RID_DELETE:
8078 return cp_parser_delete_expression (parser);
8080 case RID_EXTENSION:
8082 /* The saved value of the PEDANTIC flag. */
8083 int saved_pedantic;
8084 tree expr;
8086 /* Save away the PEDANTIC flag. */
8087 cp_parser_extension_opt (parser, &saved_pedantic);
8088 /* Parse the cast-expression. */
8089 expr = cp_parser_simple_cast_expression (parser);
8090 /* Restore the PEDANTIC flag. */
8091 pedantic = saved_pedantic;
8093 return expr;
8096 case RID_REALPART:
8097 case RID_IMAGPART:
8099 tree expression;
8101 /* Consume the `__real__' or `__imag__' token. */
8102 cp_lexer_consume_token (parser->lexer);
8103 /* Parse the cast-expression. */
8104 expression = cp_parser_simple_cast_expression (parser);
8105 /* Create the complete representation. */
8106 return build_x_unary_op (token->location,
8107 (keyword == RID_REALPART
8108 ? REALPART_EXPR : IMAGPART_EXPR),
8109 expression,
8110 tf_warning_or_error);
8112 break;
8114 case RID_TRANSACTION_ATOMIC:
8115 case RID_TRANSACTION_RELAXED:
8116 return cp_parser_transaction_expression (parser, keyword);
8118 case RID_NOEXCEPT:
8120 tree expr;
8121 const char *saved_message;
8122 bool saved_integral_constant_expression_p;
8123 bool saved_non_integral_constant_expression_p;
8124 bool saved_greater_than_is_operator_p;
8126 location_t start_loc = token->location;
8128 cp_lexer_consume_token (parser->lexer);
8129 matching_parens parens;
8130 parens.require_open (parser);
8132 saved_message = parser->type_definition_forbidden_message;
8133 parser->type_definition_forbidden_message
8134 = G_("types may not be defined in %<noexcept%> expressions");
8136 saved_integral_constant_expression_p
8137 = parser->integral_constant_expression_p;
8138 saved_non_integral_constant_expression_p
8139 = parser->non_integral_constant_expression_p;
8140 parser->integral_constant_expression_p = false;
8142 saved_greater_than_is_operator_p
8143 = parser->greater_than_is_operator_p;
8144 parser->greater_than_is_operator_p = true;
8146 ++cp_unevaluated_operand;
8147 ++c_inhibit_evaluation_warnings;
8148 ++cp_noexcept_operand;
8149 expr = cp_parser_expression (parser);
8150 --cp_noexcept_operand;
8151 --c_inhibit_evaluation_warnings;
8152 --cp_unevaluated_operand;
8154 parser->greater_than_is_operator_p
8155 = saved_greater_than_is_operator_p;
8157 parser->integral_constant_expression_p
8158 = saved_integral_constant_expression_p;
8159 parser->non_integral_constant_expression_p
8160 = saved_non_integral_constant_expression_p;
8162 parser->type_definition_forbidden_message = saved_message;
8164 location_t finish_loc
8165 = cp_lexer_peek_token (parser->lexer)->location;
8166 parens.require_close (parser);
8168 /* Construct a location of the form:
8169 noexcept (expr)
8170 ^~~~~~~~~~~~~~~
8171 with start == caret, finishing at the close-paren. */
8172 location_t noexcept_loc
8173 = make_location (start_loc, start_loc, finish_loc);
8175 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8176 noexcept_loc);
8179 default:
8180 break;
8184 /* Look for the `:: new' and `:: delete', which also signal the
8185 beginning of a new-expression, or delete-expression,
8186 respectively. If the next token is `::', then it might be one of
8187 these. */
8188 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8190 enum rid keyword;
8192 /* See if the token after the `::' is one of the keywords in
8193 which we're interested. */
8194 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8195 /* If it's `new', we have a new-expression. */
8196 if (keyword == RID_NEW)
8197 return cp_parser_new_expression (parser);
8198 /* Similarly, for `delete'. */
8199 else if (keyword == RID_DELETE)
8200 return cp_parser_delete_expression (parser);
8203 /* Look for a unary operator. */
8204 unary_operator = cp_parser_unary_operator (token);
8205 /* The `++' and `--' operators can be handled similarly, even though
8206 they are not technically unary-operators in the grammar. */
8207 if (unary_operator == ERROR_MARK)
8209 if (token->type == CPP_PLUS_PLUS)
8210 unary_operator = PREINCREMENT_EXPR;
8211 else if (token->type == CPP_MINUS_MINUS)
8212 unary_operator = PREDECREMENT_EXPR;
8213 /* Handle the GNU address-of-label extension. */
8214 else if (cp_parser_allow_gnu_extensions_p (parser)
8215 && token->type == CPP_AND_AND)
8217 tree identifier;
8218 tree expression;
8219 location_t start_loc = token->location;
8221 /* Consume the '&&' token. */
8222 cp_lexer_consume_token (parser->lexer);
8223 /* Look for the identifier. */
8224 location_t finish_loc
8225 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8226 identifier = cp_parser_identifier (parser);
8227 /* Construct a location of the form:
8228 &&label
8229 ^~~~~~~
8230 with caret==start at the "&&", finish at the end of the label. */
8231 location_t combined_loc
8232 = make_location (start_loc, start_loc, finish_loc);
8233 /* Create an expression representing the address. */
8234 expression = finish_label_address_expr (identifier, combined_loc);
8235 if (cp_parser_non_integral_constant_expression (parser,
8236 NIC_ADDR_LABEL))
8237 expression = error_mark_node;
8238 return expression;
8241 if (unary_operator != ERROR_MARK)
8243 cp_expr cast_expression;
8244 cp_expr expression = error_mark_node;
8245 non_integral_constant non_constant_p = NIC_NONE;
8246 location_t loc = token->location;
8247 tsubst_flags_t complain = complain_flags (decltype_p);
8249 /* Consume the operator token. */
8250 token = cp_lexer_consume_token (parser->lexer);
8251 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8253 /* Parse the cast-expression. */
8254 cast_expression
8255 = cp_parser_cast_expression (parser,
8256 unary_operator == ADDR_EXPR,
8257 /*cast_p=*/false,
8258 /*decltype*/false,
8259 pidk);
8261 /* Make a location:
8262 OP_TOKEN CAST_EXPRESSION
8263 ^~~~~~~~~~~~~~~~~~~~~~~~~
8264 with start==caret at the operator token, and
8265 extending to the end of the cast_expression. */
8266 loc = make_location (loc, loc, cast_expression.get_finish ());
8268 /* Now, build an appropriate representation. */
8269 switch (unary_operator)
8271 case INDIRECT_REF:
8272 non_constant_p = NIC_STAR;
8273 expression = build_x_indirect_ref (loc, cast_expression,
8274 RO_UNARY_STAR,
8275 complain);
8276 /* TODO: build_x_indirect_ref does not always honor the
8277 location, so ensure it is set. */
8278 expression.set_location (loc);
8279 break;
8281 case ADDR_EXPR:
8282 non_constant_p = NIC_ADDR;
8283 /* Fall through. */
8284 case BIT_NOT_EXPR:
8285 expression = build_x_unary_op (loc, unary_operator,
8286 cast_expression,
8287 complain);
8288 /* TODO: build_x_unary_op does not always honor the location,
8289 so ensure it is set. */
8290 expression.set_location (loc);
8291 break;
8293 case PREINCREMENT_EXPR:
8294 case PREDECREMENT_EXPR:
8295 non_constant_p = unary_operator == PREINCREMENT_EXPR
8296 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8297 /* Fall through. */
8298 case NEGATE_EXPR:
8299 /* Immediately fold negation of a constant, unless the constant is 0
8300 (since -0 == 0) or it would overflow. */
8301 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8302 && CONSTANT_CLASS_P (cast_expression)
8303 && !integer_zerop (cast_expression)
8304 && !TREE_OVERFLOW (cast_expression))
8306 tree folded = fold_build1 (unary_operator,
8307 TREE_TYPE (cast_expression),
8308 cast_expression);
8309 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8311 expression = cp_expr (folded, loc);
8312 break;
8315 /* Fall through. */
8316 case UNARY_PLUS_EXPR:
8317 case TRUTH_NOT_EXPR:
8318 expression = finish_unary_op_expr (loc, unary_operator,
8319 cast_expression, complain);
8320 break;
8322 default:
8323 gcc_unreachable ();
8326 if (non_constant_p != NIC_NONE
8327 && cp_parser_non_integral_constant_expression (parser,
8328 non_constant_p))
8329 expression = error_mark_node;
8331 return expression;
8334 return cp_parser_postfix_expression (parser, address_p, cast_p,
8335 /*member_access_only_p=*/false,
8336 decltype_p,
8337 pidk);
8340 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8341 unary-operator, the corresponding tree code is returned. */
8343 static enum tree_code
8344 cp_parser_unary_operator (cp_token* token)
8346 switch (token->type)
8348 case CPP_MULT:
8349 return INDIRECT_REF;
8351 case CPP_AND:
8352 return ADDR_EXPR;
8354 case CPP_PLUS:
8355 return UNARY_PLUS_EXPR;
8357 case CPP_MINUS:
8358 return NEGATE_EXPR;
8360 case CPP_NOT:
8361 return TRUTH_NOT_EXPR;
8363 case CPP_COMPL:
8364 return BIT_NOT_EXPR;
8366 default:
8367 return ERROR_MARK;
8371 /* Parse a new-expression.
8373 new-expression:
8374 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8375 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8377 Returns a representation of the expression. */
8379 static tree
8380 cp_parser_new_expression (cp_parser* parser)
8382 bool global_scope_p;
8383 vec<tree, va_gc> *placement;
8384 tree type;
8385 vec<tree, va_gc> *initializer;
8386 tree nelts = NULL_TREE;
8387 tree ret;
8389 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8391 /* Look for the optional `::' operator. */
8392 global_scope_p
8393 = (cp_parser_global_scope_opt (parser,
8394 /*current_scope_valid_p=*/false)
8395 != NULL_TREE);
8396 /* Look for the `new' operator. */
8397 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8398 /* There's no easy way to tell a new-placement from the
8399 `( type-id )' construct. */
8400 cp_parser_parse_tentatively (parser);
8401 /* Look for a new-placement. */
8402 placement = cp_parser_new_placement (parser);
8403 /* If that didn't work out, there's no new-placement. */
8404 if (!cp_parser_parse_definitely (parser))
8406 if (placement != NULL)
8407 release_tree_vector (placement);
8408 placement = NULL;
8411 /* If the next token is a `(', then we have a parenthesized
8412 type-id. */
8413 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8415 cp_token *token;
8416 const char *saved_message = parser->type_definition_forbidden_message;
8418 /* Consume the `('. */
8419 matching_parens parens;
8420 parens.consume_open (parser);
8422 /* Parse the type-id. */
8423 parser->type_definition_forbidden_message
8424 = G_("types may not be defined in a new-expression");
8426 type_id_in_expr_sentinel s (parser);
8427 type = cp_parser_type_id (parser);
8429 parser->type_definition_forbidden_message = saved_message;
8431 /* Look for the closing `)'. */
8432 parens.require_close (parser);
8433 token = cp_lexer_peek_token (parser->lexer);
8434 /* There should not be a direct-new-declarator in this production,
8435 but GCC used to allowed this, so we check and emit a sensible error
8436 message for this case. */
8437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8439 error_at (token->location,
8440 "array bound forbidden after parenthesized type-id");
8441 inform (token->location,
8442 "try removing the parentheses around the type-id");
8443 cp_parser_direct_new_declarator (parser);
8446 /* Otherwise, there must be a new-type-id. */
8447 else
8448 type = cp_parser_new_type_id (parser, &nelts);
8450 /* If the next token is a `(' or '{', then we have a new-initializer. */
8451 cp_token *token = cp_lexer_peek_token (parser->lexer);
8452 if (token->type == CPP_OPEN_PAREN
8453 || token->type == CPP_OPEN_BRACE)
8454 initializer = cp_parser_new_initializer (parser);
8455 else
8456 initializer = NULL;
8458 /* A new-expression may not appear in an integral constant
8459 expression. */
8460 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8461 ret = error_mark_node;
8462 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8463 of a new-type-id or type-id of a new-expression, the new-expression shall
8464 contain a new-initializer of the form ( assignment-expression )".
8465 Additionally, consistently with the spirit of DR 1467, we want to accept
8466 'new auto { 2 }' too. */
8467 else if ((ret = type_uses_auto (type))
8468 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8469 && (vec_safe_length (initializer) != 1
8470 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8471 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8473 error_at (token->location,
8474 "initialization of new-expression for type %<auto%> "
8475 "requires exactly one element");
8476 ret = error_mark_node;
8478 else
8480 /* Construct a location e.g.:
8481 ptr = new int[100]
8482 ^~~~~~~~~~~~
8483 with caret == start at the start of the "new" token, and the end
8484 at the end of the final token we consumed. */
8485 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8486 location_t end_loc = get_finish (end_tok->location);
8487 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8489 /* Create a representation of the new-expression. */
8490 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8491 tf_warning_or_error);
8492 protected_set_expr_location (ret, combined_loc);
8495 if (placement != NULL)
8496 release_tree_vector (placement);
8497 if (initializer != NULL)
8498 release_tree_vector (initializer);
8500 return ret;
8503 /* Parse a new-placement.
8505 new-placement:
8506 ( expression-list )
8508 Returns the same representation as for an expression-list. */
8510 static vec<tree, va_gc> *
8511 cp_parser_new_placement (cp_parser* parser)
8513 vec<tree, va_gc> *expression_list;
8515 /* Parse the expression-list. */
8516 expression_list = (cp_parser_parenthesized_expression_list
8517 (parser, non_attr, /*cast_p=*/false,
8518 /*allow_expansion_p=*/true,
8519 /*non_constant_p=*/NULL));
8521 if (expression_list && expression_list->is_empty ())
8522 error ("expected expression-list or type-id");
8524 return expression_list;
8527 /* Parse a new-type-id.
8529 new-type-id:
8530 type-specifier-seq new-declarator [opt]
8532 Returns the TYPE allocated. If the new-type-id indicates an array
8533 type, *NELTS is set to the number of elements in the last array
8534 bound; the TYPE will not include the last array bound. */
8536 static tree
8537 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8539 cp_decl_specifier_seq type_specifier_seq;
8540 cp_declarator *new_declarator;
8541 cp_declarator *declarator;
8542 cp_declarator *outer_declarator;
8543 const char *saved_message;
8545 /* The type-specifier sequence must not contain type definitions.
8546 (It cannot contain declarations of new types either, but if they
8547 are not definitions we will catch that because they are not
8548 complete.) */
8549 saved_message = parser->type_definition_forbidden_message;
8550 parser->type_definition_forbidden_message
8551 = G_("types may not be defined in a new-type-id");
8552 /* Parse the type-specifier-seq. */
8553 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8554 /*is_trailing_return=*/false,
8555 &type_specifier_seq);
8556 /* Restore the old message. */
8557 parser->type_definition_forbidden_message = saved_message;
8559 if (type_specifier_seq.type == error_mark_node)
8560 return error_mark_node;
8562 /* Parse the new-declarator. */
8563 new_declarator = cp_parser_new_declarator_opt (parser);
8565 /* Determine the number of elements in the last array dimension, if
8566 any. */
8567 *nelts = NULL_TREE;
8568 /* Skip down to the last array dimension. */
8569 declarator = new_declarator;
8570 outer_declarator = NULL;
8571 while (declarator && (declarator->kind == cdk_pointer
8572 || declarator->kind == cdk_ptrmem))
8574 outer_declarator = declarator;
8575 declarator = declarator->declarator;
8577 while (declarator
8578 && declarator->kind == cdk_array
8579 && declarator->declarator
8580 && declarator->declarator->kind == cdk_array)
8582 outer_declarator = declarator;
8583 declarator = declarator->declarator;
8586 if (declarator && declarator->kind == cdk_array)
8588 *nelts = declarator->u.array.bounds;
8589 if (*nelts == error_mark_node)
8590 *nelts = integer_one_node;
8592 if (outer_declarator)
8593 outer_declarator->declarator = declarator->declarator;
8594 else
8595 new_declarator = NULL;
8598 return groktypename (&type_specifier_seq, new_declarator, false);
8601 /* Parse an (optional) new-declarator.
8603 new-declarator:
8604 ptr-operator new-declarator [opt]
8605 direct-new-declarator
8607 Returns the declarator. */
8609 static cp_declarator *
8610 cp_parser_new_declarator_opt (cp_parser* parser)
8612 enum tree_code code;
8613 tree type, std_attributes = NULL_TREE;
8614 cp_cv_quals cv_quals;
8616 /* We don't know if there's a ptr-operator next, or not. */
8617 cp_parser_parse_tentatively (parser);
8618 /* Look for a ptr-operator. */
8619 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8620 /* If that worked, look for more new-declarators. */
8621 if (cp_parser_parse_definitely (parser))
8623 cp_declarator *declarator;
8625 /* Parse another optional declarator. */
8626 declarator = cp_parser_new_declarator_opt (parser);
8628 declarator = cp_parser_make_indirect_declarator
8629 (code, type, cv_quals, declarator, std_attributes);
8631 return declarator;
8634 /* If the next token is a `[', there is a direct-new-declarator. */
8635 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8636 return cp_parser_direct_new_declarator (parser);
8638 return NULL;
8641 /* Parse a direct-new-declarator.
8643 direct-new-declarator:
8644 [ expression ]
8645 direct-new-declarator [constant-expression]
8649 static cp_declarator *
8650 cp_parser_direct_new_declarator (cp_parser* parser)
8652 cp_declarator *declarator = NULL;
8654 while (true)
8656 tree expression;
8657 cp_token *token;
8659 /* Look for the opening `['. */
8660 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8662 token = cp_lexer_peek_token (parser->lexer);
8663 expression = cp_parser_expression (parser);
8664 /* The standard requires that the expression have integral
8665 type. DR 74 adds enumeration types. We believe that the
8666 real intent is that these expressions be handled like the
8667 expression in a `switch' condition, which also allows
8668 classes with a single conversion to integral or
8669 enumeration type. */
8670 if (!processing_template_decl)
8672 expression
8673 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8674 expression,
8675 /*complain=*/true);
8676 if (!expression)
8678 error_at (token->location,
8679 "expression in new-declarator must have integral "
8680 "or enumeration type");
8681 expression = error_mark_node;
8685 /* Look for the closing `]'. */
8686 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8688 /* Add this bound to the declarator. */
8689 declarator = make_array_declarator (declarator, expression);
8691 /* If the next token is not a `[', then there are no more
8692 bounds. */
8693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8694 break;
8697 return declarator;
8700 /* Parse a new-initializer.
8702 new-initializer:
8703 ( expression-list [opt] )
8704 braced-init-list
8706 Returns a representation of the expression-list. */
8708 static vec<tree, va_gc> *
8709 cp_parser_new_initializer (cp_parser* parser)
8711 vec<tree, va_gc> *expression_list;
8713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8715 tree t;
8716 bool expr_non_constant_p;
8717 cp_lexer_set_source_position (parser->lexer);
8718 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8719 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8720 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8721 expression_list = make_tree_vector_single (t);
8723 else
8724 expression_list = (cp_parser_parenthesized_expression_list
8725 (parser, non_attr, /*cast_p=*/false,
8726 /*allow_expansion_p=*/true,
8727 /*non_constant_p=*/NULL));
8729 return expression_list;
8732 /* Parse a delete-expression.
8734 delete-expression:
8735 :: [opt] delete cast-expression
8736 :: [opt] delete [ ] cast-expression
8738 Returns a representation of the expression. */
8740 static tree
8741 cp_parser_delete_expression (cp_parser* parser)
8743 bool global_scope_p;
8744 bool array_p;
8745 tree expression;
8747 /* Look for the optional `::' operator. */
8748 global_scope_p
8749 = (cp_parser_global_scope_opt (parser,
8750 /*current_scope_valid_p=*/false)
8751 != NULL_TREE);
8752 /* Look for the `delete' keyword. */
8753 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8754 /* See if the array syntax is in use. */
8755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8757 /* Consume the `[' token. */
8758 cp_lexer_consume_token (parser->lexer);
8759 /* Look for the `]' token. */
8760 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8761 /* Remember that this is the `[]' construct. */
8762 array_p = true;
8764 else
8765 array_p = false;
8767 /* Parse the cast-expression. */
8768 expression = cp_parser_simple_cast_expression (parser);
8770 /* A delete-expression may not appear in an integral constant
8771 expression. */
8772 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8773 return error_mark_node;
8775 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8776 tf_warning_or_error);
8779 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8780 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8781 0 otherwise. */
8783 static int
8784 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8786 cp_token *token = cp_lexer_peek_token (parser->lexer);
8787 switch (token->type)
8789 case CPP_COMMA:
8790 case CPP_SEMICOLON:
8791 case CPP_QUERY:
8792 case CPP_COLON:
8793 case CPP_CLOSE_SQUARE:
8794 case CPP_CLOSE_PAREN:
8795 case CPP_CLOSE_BRACE:
8796 case CPP_OPEN_BRACE:
8797 case CPP_DOT:
8798 case CPP_DOT_STAR:
8799 case CPP_DEREF:
8800 case CPP_DEREF_STAR:
8801 case CPP_DIV:
8802 case CPP_MOD:
8803 case CPP_LSHIFT:
8804 case CPP_RSHIFT:
8805 case CPP_LESS:
8806 case CPP_GREATER:
8807 case CPP_LESS_EQ:
8808 case CPP_GREATER_EQ:
8809 case CPP_EQ_EQ:
8810 case CPP_NOT_EQ:
8811 case CPP_EQ:
8812 case CPP_MULT_EQ:
8813 case CPP_DIV_EQ:
8814 case CPP_MOD_EQ:
8815 case CPP_PLUS_EQ:
8816 case CPP_MINUS_EQ:
8817 case CPP_RSHIFT_EQ:
8818 case CPP_LSHIFT_EQ:
8819 case CPP_AND_EQ:
8820 case CPP_XOR_EQ:
8821 case CPP_OR_EQ:
8822 case CPP_XOR:
8823 case CPP_OR:
8824 case CPP_OR_OR:
8825 case CPP_EOF:
8826 case CPP_ELLIPSIS:
8827 return 0;
8829 case CPP_OPEN_PAREN:
8830 /* In ((type ()) () the last () isn't a valid cast-expression,
8831 so the whole must be parsed as postfix-expression. */
8832 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8833 != CPP_CLOSE_PAREN;
8835 case CPP_OPEN_SQUARE:
8836 /* '[' may start a primary-expression in obj-c++ and in C++11,
8837 as a lambda-expression, eg, '(void)[]{}'. */
8838 if (cxx_dialect >= cxx11)
8839 return -1;
8840 return c_dialect_objc ();
8842 case CPP_PLUS_PLUS:
8843 case CPP_MINUS_MINUS:
8844 /* '++' and '--' may or may not start a cast-expression:
8846 struct T { void operator++(int); };
8847 void f() { (T())++; }
8851 int a;
8852 (int)++a; */
8853 return -1;
8855 default:
8856 return 1;
8860 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8861 in the order: const_cast, static_cast, reinterpret_cast.
8863 Don't suggest dynamic_cast.
8865 Return the first legal cast kind found, or NULL otherwise. */
8867 static const char *
8868 get_cast_suggestion (tree dst_type, tree orig_expr)
8870 tree trial;
8872 /* Reuse the parser logic by attempting to build the various kinds of
8873 cast, with "complain" disabled.
8874 Identify the first such cast that is valid. */
8876 /* Don't attempt to run such logic within template processing. */
8877 if (processing_template_decl)
8878 return NULL;
8880 /* First try const_cast. */
8881 trial = build_const_cast (dst_type, orig_expr, tf_none);
8882 if (trial != error_mark_node)
8883 return "const_cast";
8885 /* If that fails, try static_cast. */
8886 trial = build_static_cast (dst_type, orig_expr, tf_none);
8887 if (trial != error_mark_node)
8888 return "static_cast";
8890 /* Finally, try reinterpret_cast. */
8891 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8892 if (trial != error_mark_node)
8893 return "reinterpret_cast";
8895 /* No such cast possible. */
8896 return NULL;
8899 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8900 suggesting how to convert a C-style cast of the form:
8902 (DST_TYPE)ORIG_EXPR
8904 to a C++-style cast.
8906 The primary range of RICHLOC is asssumed to be that of the original
8907 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8908 of the parens in the C-style cast. */
8910 static void
8911 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8912 location_t close_paren_loc, tree orig_expr,
8913 tree dst_type)
8915 /* This function is non-trivial, so bail out now if the warning isn't
8916 going to be emitted. */
8917 if (!warn_old_style_cast)
8918 return;
8920 /* Try to find a legal C++ cast, trying them in order:
8921 const_cast, static_cast, reinterpret_cast. */
8922 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8923 if (!cast_suggestion)
8924 return;
8926 /* Replace the open paren with "CAST_SUGGESTION<". */
8927 pretty_printer pp;
8928 pp_printf (&pp, "%s<", cast_suggestion);
8929 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8931 /* Replace the close paren with "> (". */
8932 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8934 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8935 rich_loc->add_fixit_insert_after (")");
8939 /* Parse a cast-expression.
8941 cast-expression:
8942 unary-expression
8943 ( type-id ) cast-expression
8945 ADDRESS_P is true iff the unary-expression is appearing as the
8946 operand of the `&' operator. CAST_P is true if this expression is
8947 the target of a cast.
8949 Returns a representation of the expression. */
8951 static cp_expr
8952 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8953 bool decltype_p, cp_id_kind * pidk)
8955 /* If it's a `(', then we might be looking at a cast. */
8956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8958 tree type = NULL_TREE;
8959 cp_expr expr (NULL_TREE);
8960 int cast_expression = 0;
8961 const char *saved_message;
8963 /* There's no way to know yet whether or not this is a cast.
8964 For example, `(int (3))' is a unary-expression, while `(int)
8965 3' is a cast. So, we resort to parsing tentatively. */
8966 cp_parser_parse_tentatively (parser);
8967 /* Types may not be defined in a cast. */
8968 saved_message = parser->type_definition_forbidden_message;
8969 parser->type_definition_forbidden_message
8970 = G_("types may not be defined in casts");
8971 /* Consume the `('. */
8972 matching_parens parens;
8973 cp_token *open_paren = parens.consume_open (parser);
8974 location_t open_paren_loc = open_paren->location;
8975 location_t close_paren_loc = UNKNOWN_LOCATION;
8977 /* A very tricky bit is that `(struct S) { 3 }' is a
8978 compound-literal (which we permit in C++ as an extension).
8979 But, that construct is not a cast-expression -- it is a
8980 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8981 is legal; if the compound-literal were a cast-expression,
8982 you'd need an extra set of parentheses.) But, if we parse
8983 the type-id, and it happens to be a class-specifier, then we
8984 will commit to the parse at that point, because we cannot
8985 undo the action that is done when creating a new class. So,
8986 then we cannot back up and do a postfix-expression.
8988 Another tricky case is the following (c++/29234):
8990 struct S { void operator () (); };
8992 void foo ()
8994 ( S()() );
8997 As a type-id we parse the parenthesized S()() as a function
8998 returning a function, groktypename complains and we cannot
8999 back up in this case either.
9001 Therefore, we scan ahead to the closing `)', and check to see
9002 if the tokens after the `)' can start a cast-expression. Otherwise
9003 we are dealing with an unary-expression, a postfix-expression
9004 or something else.
9006 Yet another tricky case, in C++11, is the following (c++/54891):
9008 (void)[]{};
9010 The issue is that usually, besides the case of lambda-expressions,
9011 the parenthesized type-id cannot be followed by '[', and, eg, we
9012 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9013 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9014 we don't commit, we try a cast-expression, then an unary-expression.
9016 Save tokens so that we can put them back. */
9017 cp_lexer_save_tokens (parser->lexer);
9019 /* We may be looking at a cast-expression. */
9020 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9021 /*consume_paren=*/true))
9022 cast_expression
9023 = cp_parser_tokens_start_cast_expression (parser);
9025 /* Roll back the tokens we skipped. */
9026 cp_lexer_rollback_tokens (parser->lexer);
9027 /* If we aren't looking at a cast-expression, simulate an error so
9028 that the call to cp_parser_error_occurred below returns true. */
9029 if (!cast_expression)
9030 cp_parser_simulate_error (parser);
9031 else
9033 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9034 parser->in_type_id_in_expr_p = true;
9035 /* Look for the type-id. */
9036 type = cp_parser_type_id (parser);
9037 /* Look for the closing `)'. */
9038 cp_token *close_paren = parens.require_close (parser);
9039 if (close_paren)
9040 close_paren_loc = close_paren->location;
9041 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9044 /* Restore the saved message. */
9045 parser->type_definition_forbidden_message = saved_message;
9047 /* At this point this can only be either a cast or a
9048 parenthesized ctor such as `(T ())' that looks like a cast to
9049 function returning T. */
9050 if (!cp_parser_error_occurred (parser))
9052 /* Only commit if the cast-expression doesn't start with
9053 '++', '--', or '[' in C++11. */
9054 if (cast_expression > 0)
9055 cp_parser_commit_to_topmost_tentative_parse (parser);
9057 expr = cp_parser_cast_expression (parser,
9058 /*address_p=*/false,
9059 /*cast_p=*/true,
9060 /*decltype_p=*/false,
9061 pidk);
9063 if (cp_parser_parse_definitely (parser))
9065 /* Warn about old-style casts, if so requested. */
9066 if (warn_old_style_cast
9067 && !in_system_header_at (input_location)
9068 && !VOID_TYPE_P (type)
9069 && current_lang_name != lang_name_c)
9071 gcc_rich_location rich_loc (input_location);
9072 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9073 expr, type);
9074 warning_at (&rich_loc, OPT_Wold_style_cast,
9075 "use of old-style cast to %q#T", type);
9078 /* Only type conversions to integral or enumeration types
9079 can be used in constant-expressions. */
9080 if (!cast_valid_in_integral_constant_expression_p (type)
9081 && cp_parser_non_integral_constant_expression (parser,
9082 NIC_CAST))
9083 return error_mark_node;
9085 /* Perform the cast. */
9086 /* Make a location:
9087 (TYPE) EXPR
9088 ^~~~~~~~~~~
9089 with start==caret at the open paren, extending to the
9090 end of "expr". */
9091 location_t cast_loc = make_location (open_paren_loc,
9092 open_paren_loc,
9093 expr.get_finish ());
9094 expr = build_c_cast (cast_loc, type, expr);
9095 return expr;
9098 else
9099 cp_parser_abort_tentative_parse (parser);
9102 /* If we get here, then it's not a cast, so it must be a
9103 unary-expression. */
9104 return cp_parser_unary_expression (parser, pidk, address_p,
9105 cast_p, decltype_p);
9108 /* Parse a binary expression of the general form:
9110 pm-expression:
9111 cast-expression
9112 pm-expression .* cast-expression
9113 pm-expression ->* cast-expression
9115 multiplicative-expression:
9116 pm-expression
9117 multiplicative-expression * pm-expression
9118 multiplicative-expression / pm-expression
9119 multiplicative-expression % pm-expression
9121 additive-expression:
9122 multiplicative-expression
9123 additive-expression + multiplicative-expression
9124 additive-expression - multiplicative-expression
9126 shift-expression:
9127 additive-expression
9128 shift-expression << additive-expression
9129 shift-expression >> additive-expression
9131 relational-expression:
9132 shift-expression
9133 relational-expression < shift-expression
9134 relational-expression > shift-expression
9135 relational-expression <= shift-expression
9136 relational-expression >= shift-expression
9138 GNU Extension:
9140 relational-expression:
9141 relational-expression <? shift-expression
9142 relational-expression >? shift-expression
9144 equality-expression:
9145 relational-expression
9146 equality-expression == relational-expression
9147 equality-expression != relational-expression
9149 and-expression:
9150 equality-expression
9151 and-expression & equality-expression
9153 exclusive-or-expression:
9154 and-expression
9155 exclusive-or-expression ^ and-expression
9157 inclusive-or-expression:
9158 exclusive-or-expression
9159 inclusive-or-expression | exclusive-or-expression
9161 logical-and-expression:
9162 inclusive-or-expression
9163 logical-and-expression && inclusive-or-expression
9165 logical-or-expression:
9166 logical-and-expression
9167 logical-or-expression || logical-and-expression
9169 All these are implemented with a single function like:
9171 binary-expression:
9172 simple-cast-expression
9173 binary-expression <token> binary-expression
9175 CAST_P is true if this expression is the target of a cast.
9177 The binops_by_token map is used to get the tree codes for each <token> type.
9178 binary-expressions are associated according to a precedence table. */
9180 #define TOKEN_PRECEDENCE(token) \
9181 (((token->type == CPP_GREATER \
9182 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9183 && !parser->greater_than_is_operator_p) \
9184 ? PREC_NOT_OPERATOR \
9185 : binops_by_token[token->type].prec)
9187 static cp_expr
9188 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9189 bool no_toplevel_fold_p,
9190 bool decltype_p,
9191 enum cp_parser_prec prec,
9192 cp_id_kind * pidk)
9194 cp_parser_expression_stack stack;
9195 cp_parser_expression_stack_entry *sp = &stack[0];
9196 cp_parser_expression_stack_entry current;
9197 cp_expr rhs;
9198 cp_token *token;
9199 enum tree_code rhs_type;
9200 enum cp_parser_prec new_prec, lookahead_prec;
9201 tree overload;
9203 /* Parse the first expression. */
9204 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9205 ? TRUTH_NOT_EXPR : ERROR_MARK);
9206 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9207 cast_p, decltype_p, pidk);
9208 current.prec = prec;
9210 if (cp_parser_error_occurred (parser))
9211 return error_mark_node;
9213 for (;;)
9215 /* Get an operator token. */
9216 token = cp_lexer_peek_token (parser->lexer);
9218 if (warn_cxx11_compat
9219 && token->type == CPP_RSHIFT
9220 && !parser->greater_than_is_operator_p)
9222 if (warning_at (token->location, OPT_Wc__11_compat,
9223 "%<>>%> operator is treated"
9224 " as two right angle brackets in C++11"))
9225 inform (token->location,
9226 "suggest parentheses around %<>>%> expression");
9229 new_prec = TOKEN_PRECEDENCE (token);
9230 if (new_prec != PREC_NOT_OPERATOR
9231 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9232 /* This is a fold-expression; handle it later. */
9233 new_prec = PREC_NOT_OPERATOR;
9235 /* Popping an entry off the stack means we completed a subexpression:
9236 - either we found a token which is not an operator (`>' where it is not
9237 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9238 will happen repeatedly;
9239 - or, we found an operator which has lower priority. This is the case
9240 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9241 parsing `3 * 4'. */
9242 if (new_prec <= current.prec)
9244 if (sp == stack)
9245 break;
9246 else
9247 goto pop;
9250 get_rhs:
9251 current.tree_type = binops_by_token[token->type].tree_type;
9252 current.loc = token->location;
9254 /* We used the operator token. */
9255 cp_lexer_consume_token (parser->lexer);
9257 /* For "false && x" or "true || x", x will never be executed;
9258 disable warnings while evaluating it. */
9259 if (current.tree_type == TRUTH_ANDIF_EXPR)
9260 c_inhibit_evaluation_warnings +=
9261 cp_fully_fold (current.lhs) == truthvalue_false_node;
9262 else if (current.tree_type == TRUTH_ORIF_EXPR)
9263 c_inhibit_evaluation_warnings +=
9264 cp_fully_fold (current.lhs) == truthvalue_true_node;
9266 /* Extract another operand. It may be the RHS of this expression
9267 or the LHS of a new, higher priority expression. */
9268 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9269 ? TRUTH_NOT_EXPR : ERROR_MARK);
9270 rhs = cp_parser_simple_cast_expression (parser);
9272 /* Get another operator token. Look up its precedence to avoid
9273 building a useless (immediately popped) stack entry for common
9274 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9275 token = cp_lexer_peek_token (parser->lexer);
9276 lookahead_prec = TOKEN_PRECEDENCE (token);
9277 if (lookahead_prec != PREC_NOT_OPERATOR
9278 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9279 lookahead_prec = PREC_NOT_OPERATOR;
9280 if (lookahead_prec > new_prec)
9282 /* ... and prepare to parse the RHS of the new, higher priority
9283 expression. Since precedence levels on the stack are
9284 monotonically increasing, we do not have to care about
9285 stack overflows. */
9286 *sp = current;
9287 ++sp;
9288 current.lhs = rhs;
9289 current.lhs_type = rhs_type;
9290 current.prec = new_prec;
9291 new_prec = lookahead_prec;
9292 goto get_rhs;
9294 pop:
9295 lookahead_prec = new_prec;
9296 /* If the stack is not empty, we have parsed into LHS the right side
9297 (`4' in the example above) of an expression we had suspended.
9298 We can use the information on the stack to recover the LHS (`3')
9299 from the stack together with the tree code (`MULT_EXPR'), and
9300 the precedence of the higher level subexpression
9301 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9302 which will be used to actually build the additive expression. */
9303 rhs = current.lhs;
9304 rhs_type = current.lhs_type;
9305 --sp;
9306 current = *sp;
9309 /* Undo the disabling of warnings done above. */
9310 if (current.tree_type == TRUTH_ANDIF_EXPR)
9311 c_inhibit_evaluation_warnings -=
9312 cp_fully_fold (current.lhs) == truthvalue_false_node;
9313 else if (current.tree_type == TRUTH_ORIF_EXPR)
9314 c_inhibit_evaluation_warnings -=
9315 cp_fully_fold (current.lhs) == truthvalue_true_node;
9317 if (warn_logical_not_paren
9318 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9319 && current.lhs_type == TRUTH_NOT_EXPR
9320 /* Avoid warning for !!x == y. */
9321 && (TREE_CODE (current.lhs) != NE_EXPR
9322 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9323 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9324 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9325 /* Avoid warning for !b == y where b is boolean. */
9326 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9327 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9328 != BOOLEAN_TYPE))))
9329 /* Avoid warning for !!b == y where b is boolean. */
9330 && (!DECL_P (current.lhs)
9331 || TREE_TYPE (current.lhs) == NULL_TREE
9332 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9333 warn_logical_not_parentheses (current.loc, current.tree_type,
9334 current.lhs, maybe_constant_value (rhs));
9336 overload = NULL;
9338 location_t combined_loc = make_location (current.loc,
9339 current.lhs.get_start (),
9340 rhs.get_finish ());
9342 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9343 ERROR_MARK for everything that is not a binary expression.
9344 This makes warn_about_parentheses miss some warnings that
9345 involve unary operators. For unary expressions we should
9346 pass the correct tree_code unless the unary expression was
9347 surrounded by parentheses.
9349 if (no_toplevel_fold_p
9350 && lookahead_prec <= current.prec
9351 && sp == stack)
9353 if (current.lhs == error_mark_node || rhs == error_mark_node)
9354 current.lhs = error_mark_node;
9355 else
9357 current.lhs
9358 = build_min (current.tree_type,
9359 TREE_CODE_CLASS (current.tree_type)
9360 == tcc_comparison
9361 ? boolean_type_node : TREE_TYPE (current.lhs),
9362 current.lhs.get_value (), rhs.get_value ());
9363 SET_EXPR_LOCATION (current.lhs, combined_loc);
9366 else
9368 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9369 current.lhs, current.lhs_type,
9370 rhs, rhs_type, &overload,
9371 complain_flags (decltype_p));
9372 /* TODO: build_x_binary_op doesn't always honor the location. */
9373 current.lhs.set_location (combined_loc);
9375 current.lhs_type = current.tree_type;
9377 /* If the binary operator required the use of an overloaded operator,
9378 then this expression cannot be an integral constant-expression.
9379 An overloaded operator can be used even if both operands are
9380 otherwise permissible in an integral constant-expression if at
9381 least one of the operands is of enumeration type. */
9383 if (overload
9384 && cp_parser_non_integral_constant_expression (parser,
9385 NIC_OVERLOADED))
9386 return error_mark_node;
9389 return current.lhs;
9392 static cp_expr
9393 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9394 bool no_toplevel_fold_p,
9395 enum cp_parser_prec prec,
9396 cp_id_kind * pidk)
9398 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9399 /*decltype*/false, prec, pidk);
9402 /* Parse the `? expression : assignment-expression' part of a
9403 conditional-expression. The LOGICAL_OR_EXPR is the
9404 logical-or-expression that started the conditional-expression.
9405 Returns a representation of the entire conditional-expression.
9407 This routine is used by cp_parser_assignment_expression.
9409 ? expression : assignment-expression
9411 GNU Extensions:
9413 ? : assignment-expression */
9415 static tree
9416 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9418 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9419 cp_expr assignment_expr;
9420 struct cp_token *token;
9421 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9423 /* Consume the `?' token. */
9424 cp_lexer_consume_token (parser->lexer);
9425 token = cp_lexer_peek_token (parser->lexer);
9426 if (cp_parser_allow_gnu_extensions_p (parser)
9427 && token->type == CPP_COLON)
9429 pedwarn (token->location, OPT_Wpedantic,
9430 "ISO C++ does not allow ?: with omitted middle operand");
9431 /* Implicit true clause. */
9432 expr = NULL_TREE;
9433 c_inhibit_evaluation_warnings +=
9434 folded_logical_or_expr == truthvalue_true_node;
9435 warn_for_omitted_condop (token->location, logical_or_expr);
9437 else
9439 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9440 parser->colon_corrects_to_scope_p = false;
9441 /* Parse the expression. */
9442 c_inhibit_evaluation_warnings +=
9443 folded_logical_or_expr == truthvalue_false_node;
9444 expr = cp_parser_expression (parser);
9445 c_inhibit_evaluation_warnings +=
9446 ((folded_logical_or_expr == truthvalue_true_node)
9447 - (folded_logical_or_expr == truthvalue_false_node));
9448 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9451 /* The next token should be a `:'. */
9452 cp_parser_require (parser, CPP_COLON, RT_COLON);
9453 /* Parse the assignment-expression. */
9454 assignment_expr = cp_parser_assignment_expression (parser);
9455 c_inhibit_evaluation_warnings -=
9456 folded_logical_or_expr == truthvalue_true_node;
9458 /* Make a location:
9459 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9460 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9461 with the caret at the "?", ranging from the start of
9462 the logical_or_expr to the end of the assignment_expr. */
9463 loc = make_location (loc,
9464 logical_or_expr.get_start (),
9465 assignment_expr.get_finish ());
9467 /* Build the conditional-expression. */
9468 return build_x_conditional_expr (loc, logical_or_expr,
9469 expr,
9470 assignment_expr,
9471 tf_warning_or_error);
9474 /* Parse an assignment-expression.
9476 assignment-expression:
9477 conditional-expression
9478 logical-or-expression assignment-operator assignment_expression
9479 throw-expression
9481 CAST_P is true if this expression is the target of a cast.
9482 DECLTYPE_P is true if this expression is the operand of decltype.
9484 Returns a representation for the expression. */
9486 static cp_expr
9487 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9488 bool cast_p, bool decltype_p)
9490 cp_expr expr;
9492 /* If the next token is the `throw' keyword, then we're looking at
9493 a throw-expression. */
9494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9495 expr = cp_parser_throw_expression (parser);
9496 /* Otherwise, it must be that we are looking at a
9497 logical-or-expression. */
9498 else
9500 /* Parse the binary expressions (logical-or-expression). */
9501 expr = cp_parser_binary_expression (parser, cast_p, false,
9502 decltype_p,
9503 PREC_NOT_OPERATOR, pidk);
9504 /* If the next token is a `?' then we're actually looking at a
9505 conditional-expression. */
9506 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9507 return cp_parser_question_colon_clause (parser, expr);
9508 else
9510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9512 /* If it's an assignment-operator, we're using the second
9513 production. */
9514 enum tree_code assignment_operator
9515 = cp_parser_assignment_operator_opt (parser);
9516 if (assignment_operator != ERROR_MARK)
9518 bool non_constant_p;
9520 /* Parse the right-hand side of the assignment. */
9521 cp_expr rhs = cp_parser_initializer_clause (parser,
9522 &non_constant_p);
9524 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9525 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9527 /* An assignment may not appear in a
9528 constant-expression. */
9529 if (cp_parser_non_integral_constant_expression (parser,
9530 NIC_ASSIGNMENT))
9531 return error_mark_node;
9532 /* Build the assignment expression. Its default
9533 location:
9534 LHS = RHS
9535 ~~~~^~~~~
9536 is the location of the '=' token as the
9537 caret, ranging from the start of the lhs to the
9538 end of the rhs. */
9539 loc = make_location (loc,
9540 expr.get_start (),
9541 rhs.get_finish ());
9542 expr = build_x_modify_expr (loc, expr,
9543 assignment_operator,
9544 rhs,
9545 complain_flags (decltype_p));
9546 /* TODO: build_x_modify_expr doesn't honor the location,
9547 so we must set it here. */
9548 expr.set_location (loc);
9553 return expr;
9556 /* Parse an (optional) assignment-operator.
9558 assignment-operator: one of
9559 = *= /= %= += -= >>= <<= &= ^= |=
9561 GNU Extension:
9563 assignment-operator: one of
9564 <?= >?=
9566 If the next token is an assignment operator, the corresponding tree
9567 code is returned, and the token is consumed. For example, for
9568 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9569 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9570 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9571 operator, ERROR_MARK is returned. */
9573 static enum tree_code
9574 cp_parser_assignment_operator_opt (cp_parser* parser)
9576 enum tree_code op;
9577 cp_token *token;
9579 /* Peek at the next token. */
9580 token = cp_lexer_peek_token (parser->lexer);
9582 switch (token->type)
9584 case CPP_EQ:
9585 op = NOP_EXPR;
9586 break;
9588 case CPP_MULT_EQ:
9589 op = MULT_EXPR;
9590 break;
9592 case CPP_DIV_EQ:
9593 op = TRUNC_DIV_EXPR;
9594 break;
9596 case CPP_MOD_EQ:
9597 op = TRUNC_MOD_EXPR;
9598 break;
9600 case CPP_PLUS_EQ:
9601 op = PLUS_EXPR;
9602 break;
9604 case CPP_MINUS_EQ:
9605 op = MINUS_EXPR;
9606 break;
9608 case CPP_RSHIFT_EQ:
9609 op = RSHIFT_EXPR;
9610 break;
9612 case CPP_LSHIFT_EQ:
9613 op = LSHIFT_EXPR;
9614 break;
9616 case CPP_AND_EQ:
9617 op = BIT_AND_EXPR;
9618 break;
9620 case CPP_XOR_EQ:
9621 op = BIT_XOR_EXPR;
9622 break;
9624 case CPP_OR_EQ:
9625 op = BIT_IOR_EXPR;
9626 break;
9628 default:
9629 /* Nothing else is an assignment operator. */
9630 op = ERROR_MARK;
9633 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9634 if (op != ERROR_MARK
9635 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9636 op = ERROR_MARK;
9638 /* If it was an assignment operator, consume it. */
9639 if (op != ERROR_MARK)
9640 cp_lexer_consume_token (parser->lexer);
9642 return op;
9645 /* Parse an expression.
9647 expression:
9648 assignment-expression
9649 expression , assignment-expression
9651 CAST_P is true if this expression is the target of a cast.
9652 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9653 except possibly parenthesized or on the RHS of a comma (N3276).
9655 Returns a representation of the expression. */
9657 static cp_expr
9658 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9659 bool cast_p, bool decltype_p)
9661 cp_expr expression = NULL_TREE;
9662 location_t loc = UNKNOWN_LOCATION;
9664 while (true)
9666 cp_expr assignment_expression;
9668 /* Parse the next assignment-expression. */
9669 assignment_expression
9670 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9672 /* We don't create a temporary for a call that is the immediate operand
9673 of decltype or on the RHS of a comma. But when we see a comma, we
9674 need to create a temporary for a call on the LHS. */
9675 if (decltype_p && !processing_template_decl
9676 && TREE_CODE (assignment_expression) == CALL_EXPR
9677 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9678 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9679 assignment_expression
9680 = build_cplus_new (TREE_TYPE (assignment_expression),
9681 assignment_expression, tf_warning_or_error);
9683 /* If this is the first assignment-expression, we can just
9684 save it away. */
9685 if (!expression)
9686 expression = assignment_expression;
9687 else
9689 /* Create a location with caret at the comma, ranging
9690 from the start of the LHS to the end of the RHS. */
9691 loc = make_location (loc,
9692 expression.get_start (),
9693 assignment_expression.get_finish ());
9694 expression = build_x_compound_expr (loc, expression,
9695 assignment_expression,
9696 complain_flags (decltype_p));
9697 expression.set_location (loc);
9699 /* If the next token is not a comma, or we're in a fold-expression, then
9700 we are done with the expression. */
9701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9702 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9703 break;
9704 /* Consume the `,'. */
9705 loc = cp_lexer_peek_token (parser->lexer)->location;
9706 cp_lexer_consume_token (parser->lexer);
9707 /* A comma operator cannot appear in a constant-expression. */
9708 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9709 expression = error_mark_node;
9712 return expression;
9715 /* Parse a constant-expression.
9717 constant-expression:
9718 conditional-expression
9720 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9721 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9722 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9723 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9724 only parse a conditional-expression, otherwise parse an
9725 assignment-expression. See below for rationale. */
9727 static cp_expr
9728 cp_parser_constant_expression (cp_parser* parser,
9729 bool allow_non_constant_p,
9730 bool *non_constant_p,
9731 bool strict_p)
9733 bool saved_integral_constant_expression_p;
9734 bool saved_allow_non_integral_constant_expression_p;
9735 bool saved_non_integral_constant_expression_p;
9736 cp_expr expression;
9738 /* It might seem that we could simply parse the
9739 conditional-expression, and then check to see if it were
9740 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9741 one that the compiler can figure out is constant, possibly after
9742 doing some simplifications or optimizations. The standard has a
9743 precise definition of constant-expression, and we must honor
9744 that, even though it is somewhat more restrictive.
9746 For example:
9748 int i[(2, 3)];
9750 is not a legal declaration, because `(2, 3)' is not a
9751 constant-expression. The `,' operator is forbidden in a
9752 constant-expression. However, GCC's constant-folding machinery
9753 will fold this operation to an INTEGER_CST for `3'. */
9755 /* Save the old settings. */
9756 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9757 saved_allow_non_integral_constant_expression_p
9758 = parser->allow_non_integral_constant_expression_p;
9759 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9760 /* We are now parsing a constant-expression. */
9761 parser->integral_constant_expression_p = true;
9762 parser->allow_non_integral_constant_expression_p
9763 = (allow_non_constant_p || cxx_dialect >= cxx11);
9764 parser->non_integral_constant_expression_p = false;
9765 /* Although the grammar says "conditional-expression", when not STRICT_P,
9766 we parse an "assignment-expression", which also permits
9767 "throw-expression" and the use of assignment operators. In the case
9768 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9769 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9770 actually essential that we look for an assignment-expression.
9771 For example, cp_parser_initializer_clauses uses this function to
9772 determine whether a particular assignment-expression is in fact
9773 constant. */
9774 if (strict_p)
9776 /* Parse the binary expressions (logical-or-expression). */
9777 expression = cp_parser_binary_expression (parser, false, false, false,
9778 PREC_NOT_OPERATOR, NULL);
9779 /* If the next token is a `?' then we're actually looking at
9780 a conditional-expression; otherwise we're done. */
9781 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9782 expression = cp_parser_question_colon_clause (parser, expression);
9784 else
9785 expression = cp_parser_assignment_expression (parser);
9786 /* Restore the old settings. */
9787 parser->integral_constant_expression_p
9788 = saved_integral_constant_expression_p;
9789 parser->allow_non_integral_constant_expression_p
9790 = saved_allow_non_integral_constant_expression_p;
9791 if (cxx_dialect >= cxx11)
9793 /* Require an rvalue constant expression here; that's what our
9794 callers expect. Reference constant expressions are handled
9795 separately in e.g. cp_parser_template_argument. */
9796 tree decay = expression;
9797 if (TREE_TYPE (expression)
9798 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9799 decay = build_address (expression);
9800 bool is_const = potential_rvalue_constant_expression (decay);
9801 parser->non_integral_constant_expression_p = !is_const;
9802 if (!is_const && !allow_non_constant_p)
9803 require_potential_rvalue_constant_expression (decay);
9805 if (allow_non_constant_p)
9806 *non_constant_p = parser->non_integral_constant_expression_p;
9807 parser->non_integral_constant_expression_p
9808 = saved_non_integral_constant_expression_p;
9810 return expression;
9813 /* Parse __builtin_offsetof.
9815 offsetof-expression:
9816 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9818 offsetof-member-designator:
9819 id-expression
9820 | offsetof-member-designator "." id-expression
9821 | offsetof-member-designator "[" expression "]"
9822 | offsetof-member-designator "->" id-expression */
9824 static cp_expr
9825 cp_parser_builtin_offsetof (cp_parser *parser)
9827 int save_ice_p, save_non_ice_p;
9828 tree type;
9829 cp_expr expr;
9830 cp_id_kind dummy;
9831 cp_token *token;
9832 location_t finish_loc;
9834 /* We're about to accept non-integral-constant things, but will
9835 definitely yield an integral constant expression. Save and
9836 restore these values around our local parsing. */
9837 save_ice_p = parser->integral_constant_expression_p;
9838 save_non_ice_p = parser->non_integral_constant_expression_p;
9840 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9842 /* Consume the "__builtin_offsetof" token. */
9843 cp_lexer_consume_token (parser->lexer);
9844 /* Consume the opening `('. */
9845 matching_parens parens;
9846 parens.require_open (parser);
9847 /* Parse the type-id. */
9848 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9850 const char *saved_message = parser->type_definition_forbidden_message;
9851 parser->type_definition_forbidden_message
9852 = G_("types may not be defined within __builtin_offsetof");
9853 type = cp_parser_type_id (parser);
9854 parser->type_definition_forbidden_message = saved_message;
9856 /* Look for the `,'. */
9857 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9858 token = cp_lexer_peek_token (parser->lexer);
9860 /* Build the (type *)null that begins the traditional offsetof macro. */
9861 tree object_ptr
9862 = build_static_cast (build_pointer_type (type), null_pointer_node,
9863 tf_warning_or_error);
9865 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9866 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9867 true, &dummy, token->location);
9868 while (true)
9870 token = cp_lexer_peek_token (parser->lexer);
9871 switch (token->type)
9873 case CPP_OPEN_SQUARE:
9874 /* offsetof-member-designator "[" expression "]" */
9875 expr = cp_parser_postfix_open_square_expression (parser, expr,
9876 true, false);
9877 break;
9879 case CPP_DEREF:
9880 /* offsetof-member-designator "->" identifier */
9881 expr = grok_array_decl (token->location, expr,
9882 integer_zero_node, false);
9883 /* FALLTHRU */
9885 case CPP_DOT:
9886 /* offsetof-member-designator "." identifier */
9887 cp_lexer_consume_token (parser->lexer);
9888 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9889 expr, true, &dummy,
9890 token->location);
9891 break;
9893 case CPP_CLOSE_PAREN:
9894 /* Consume the ")" token. */
9895 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9896 cp_lexer_consume_token (parser->lexer);
9897 goto success;
9899 default:
9900 /* Error. We know the following require will fail, but
9901 that gives the proper error message. */
9902 parens.require_close (parser);
9903 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9904 expr = error_mark_node;
9905 goto failure;
9909 success:
9910 /* Make a location of the form:
9911 __builtin_offsetof (struct s, f)
9912 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9913 with caret at the type-id, ranging from the start of the
9914 "_builtin_offsetof" token to the close paren. */
9915 loc = make_location (loc, start_loc, finish_loc);
9916 /* The result will be an INTEGER_CST, so we need to explicitly
9917 preserve the location. */
9918 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9920 failure:
9921 parser->integral_constant_expression_p = save_ice_p;
9922 parser->non_integral_constant_expression_p = save_non_ice_p;
9924 expr = expr.maybe_add_location_wrapper ();
9925 return expr;
9928 /* Parse a trait expression.
9930 Returns a representation of the expression, the underlying type
9931 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9933 static cp_expr
9934 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9936 cp_trait_kind kind;
9937 tree type1, type2 = NULL_TREE;
9938 bool binary = false;
9939 bool variadic = false;
9941 switch (keyword)
9943 case RID_HAS_NOTHROW_ASSIGN:
9944 kind = CPTK_HAS_NOTHROW_ASSIGN;
9945 break;
9946 case RID_HAS_NOTHROW_CONSTRUCTOR:
9947 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9948 break;
9949 case RID_HAS_NOTHROW_COPY:
9950 kind = CPTK_HAS_NOTHROW_COPY;
9951 break;
9952 case RID_HAS_TRIVIAL_ASSIGN:
9953 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9954 break;
9955 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9956 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9957 break;
9958 case RID_HAS_TRIVIAL_COPY:
9959 kind = CPTK_HAS_TRIVIAL_COPY;
9960 break;
9961 case RID_HAS_TRIVIAL_DESTRUCTOR:
9962 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9963 break;
9964 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9965 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9966 break;
9967 case RID_HAS_VIRTUAL_DESTRUCTOR:
9968 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9969 break;
9970 case RID_IS_ABSTRACT:
9971 kind = CPTK_IS_ABSTRACT;
9972 break;
9973 case RID_IS_AGGREGATE:
9974 kind = CPTK_IS_AGGREGATE;
9975 break;
9976 case RID_IS_BASE_OF:
9977 kind = CPTK_IS_BASE_OF;
9978 binary = true;
9979 break;
9980 case RID_IS_CLASS:
9981 kind = CPTK_IS_CLASS;
9982 break;
9983 case RID_IS_EMPTY:
9984 kind = CPTK_IS_EMPTY;
9985 break;
9986 case RID_IS_ENUM:
9987 kind = CPTK_IS_ENUM;
9988 break;
9989 case RID_IS_FINAL:
9990 kind = CPTK_IS_FINAL;
9991 break;
9992 case RID_IS_LITERAL_TYPE:
9993 kind = CPTK_IS_LITERAL_TYPE;
9994 break;
9995 case RID_IS_POD:
9996 kind = CPTK_IS_POD;
9997 break;
9998 case RID_IS_POLYMORPHIC:
9999 kind = CPTK_IS_POLYMORPHIC;
10000 break;
10001 case RID_IS_SAME_AS:
10002 kind = CPTK_IS_SAME_AS;
10003 binary = true;
10004 break;
10005 case RID_IS_STD_LAYOUT:
10006 kind = CPTK_IS_STD_LAYOUT;
10007 break;
10008 case RID_IS_TRIVIAL:
10009 kind = CPTK_IS_TRIVIAL;
10010 break;
10011 case RID_IS_TRIVIALLY_ASSIGNABLE:
10012 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10013 binary = true;
10014 break;
10015 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10016 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10017 variadic = true;
10018 break;
10019 case RID_IS_TRIVIALLY_COPYABLE:
10020 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10021 break;
10022 case RID_IS_UNION:
10023 kind = CPTK_IS_UNION;
10024 break;
10025 case RID_UNDERLYING_TYPE:
10026 kind = CPTK_UNDERLYING_TYPE;
10027 break;
10028 case RID_BASES:
10029 kind = CPTK_BASES;
10030 break;
10031 case RID_DIRECT_BASES:
10032 kind = CPTK_DIRECT_BASES;
10033 break;
10034 case RID_IS_ASSIGNABLE:
10035 kind = CPTK_IS_ASSIGNABLE;
10036 binary = true;
10037 break;
10038 case RID_IS_CONSTRUCTIBLE:
10039 kind = CPTK_IS_CONSTRUCTIBLE;
10040 variadic = true;
10041 break;
10042 default:
10043 gcc_unreachable ();
10046 /* Get location of initial token. */
10047 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10049 /* Consume the token. */
10050 cp_lexer_consume_token (parser->lexer);
10052 matching_parens parens;
10053 parens.require_open (parser);
10056 type_id_in_expr_sentinel s (parser);
10057 type1 = cp_parser_type_id (parser);
10060 if (type1 == error_mark_node)
10061 return error_mark_node;
10063 if (binary)
10065 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10068 type_id_in_expr_sentinel s (parser);
10069 type2 = cp_parser_type_id (parser);
10072 if (type2 == error_mark_node)
10073 return error_mark_node;
10075 else if (variadic)
10077 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10079 cp_lexer_consume_token (parser->lexer);
10080 tree elt = cp_parser_type_id (parser);
10081 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10083 cp_lexer_consume_token (parser->lexer);
10084 elt = make_pack_expansion (elt);
10086 if (elt == error_mark_node)
10087 return error_mark_node;
10088 type2 = tree_cons (NULL_TREE, elt, type2);
10092 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10093 parens.require_close (parser);
10095 /* Construct a location of the form:
10096 __is_trivially_copyable(_Tp)
10097 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10098 with start == caret, finishing at the close-paren. */
10099 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10101 /* Complete the trait expression, which may mean either processing
10102 the trait expr now or saving it for template instantiation. */
10103 switch (kind)
10105 case CPTK_UNDERLYING_TYPE:
10106 return cp_expr (finish_underlying_type (type1), trait_loc);
10107 case CPTK_BASES:
10108 return cp_expr (finish_bases (type1, false), trait_loc);
10109 case CPTK_DIRECT_BASES:
10110 return cp_expr (finish_bases (type1, true), trait_loc);
10111 default:
10112 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10116 /* Parse a lambda expression.
10118 lambda-expression:
10119 lambda-introducer lambda-declarator [opt] compound-statement
10121 Returns a representation of the expression. */
10123 static cp_expr
10124 cp_parser_lambda_expression (cp_parser* parser)
10126 tree lambda_expr = build_lambda_expr ();
10127 tree type;
10128 bool ok = true;
10129 cp_token *token = cp_lexer_peek_token (parser->lexer);
10130 cp_token_position start = 0;
10132 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10134 if (cp_unevaluated_operand)
10136 if (!token->error_reported)
10138 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10139 "lambda-expression in unevaluated context");
10140 token->error_reported = true;
10142 ok = false;
10144 else if (parser->in_template_argument_list_p)
10146 if (!token->error_reported)
10148 error_at (token->location, "lambda-expression in template-argument");
10149 token->error_reported = true;
10151 ok = false;
10154 /* We may be in the middle of deferred access check. Disable
10155 it now. */
10156 push_deferring_access_checks (dk_no_deferred);
10158 cp_parser_lambda_introducer (parser, lambda_expr);
10160 type = begin_lambda_type (lambda_expr);
10161 if (type == error_mark_node)
10162 return error_mark_node;
10164 record_lambda_scope (lambda_expr);
10166 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10167 determine_visibility (TYPE_NAME (type));
10169 /* Now that we've started the type, add the capture fields for any
10170 explicit captures. */
10171 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10174 /* Inside the class, surrounding template-parameter-lists do not apply. */
10175 unsigned int saved_num_template_parameter_lists
10176 = parser->num_template_parameter_lists;
10177 unsigned char in_statement = parser->in_statement;
10178 bool in_switch_statement_p = parser->in_switch_statement_p;
10179 bool fully_implicit_function_template_p
10180 = parser->fully_implicit_function_template_p;
10181 tree implicit_template_parms = parser->implicit_template_parms;
10182 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10183 bool auto_is_implicit_function_template_parm_p
10184 = parser->auto_is_implicit_function_template_parm_p;
10186 parser->num_template_parameter_lists = 0;
10187 parser->in_statement = 0;
10188 parser->in_switch_statement_p = false;
10189 parser->fully_implicit_function_template_p = false;
10190 parser->implicit_template_parms = 0;
10191 parser->implicit_template_scope = 0;
10192 parser->auto_is_implicit_function_template_parm_p = false;
10194 /* By virtue of defining a local class, a lambda expression has access to
10195 the private variables of enclosing classes. */
10197 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10199 if (ok && cp_parser_error_occurred (parser))
10200 ok = false;
10202 if (ok)
10204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10205 && cp_parser_start_tentative_firewall (parser))
10206 start = token;
10207 cp_parser_lambda_body (parser, lambda_expr);
10209 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10211 if (cp_parser_skip_to_closing_brace (parser))
10212 cp_lexer_consume_token (parser->lexer);
10215 /* The capture list was built up in reverse order; fix that now. */
10216 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10217 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10219 if (ok)
10220 maybe_add_lambda_conv_op (type);
10222 type = finish_struct (type, /*attributes=*/NULL_TREE);
10224 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10225 parser->in_statement = in_statement;
10226 parser->in_switch_statement_p = in_switch_statement_p;
10227 parser->fully_implicit_function_template_p
10228 = fully_implicit_function_template_p;
10229 parser->implicit_template_parms = implicit_template_parms;
10230 parser->implicit_template_scope = implicit_template_scope;
10231 parser->auto_is_implicit_function_template_parm_p
10232 = auto_is_implicit_function_template_parm_p;
10235 /* This field is only used during parsing of the lambda. */
10236 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10238 /* This lambda shouldn't have any proxies left at this point. */
10239 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10240 /* And now that we're done, push proxies for an enclosing lambda. */
10241 insert_pending_capture_proxies ();
10243 /* Update the lambda expression to a range. */
10244 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10245 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10246 token->location,
10247 end_tok->location);
10249 if (ok)
10250 lambda_expr = build_lambda_object (lambda_expr);
10251 else
10252 lambda_expr = error_mark_node;
10254 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10256 pop_deferring_access_checks ();
10258 return lambda_expr;
10261 /* Parse the beginning of a lambda expression.
10263 lambda-introducer:
10264 [ lambda-capture [opt] ]
10266 LAMBDA_EXPR is the current representation of the lambda expression. */
10268 static void
10269 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10271 /* Need commas after the first capture. */
10272 bool first = true;
10274 /* Eat the leading `['. */
10275 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10277 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10278 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10279 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10280 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10281 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10282 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10284 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10286 cp_lexer_consume_token (parser->lexer);
10287 first = false;
10289 if (!(at_function_scope_p () || parsing_nsdmi ()))
10290 error ("non-local lambda expression cannot have a capture-default");
10293 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10295 cp_token* capture_token;
10296 tree capture_id;
10297 tree capture_init_expr;
10298 cp_id_kind idk = CP_ID_KIND_NONE;
10299 bool explicit_init_p = false;
10301 enum capture_kind_type
10303 BY_COPY,
10304 BY_REFERENCE
10306 enum capture_kind_type capture_kind = BY_COPY;
10308 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10310 error ("expected end of capture-list");
10311 return;
10314 if (first)
10315 first = false;
10316 else
10317 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10319 /* Possibly capture `this'. */
10320 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10322 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10323 if (cxx_dialect < cxx2a
10324 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10325 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10326 "with by-copy capture default");
10327 cp_lexer_consume_token (parser->lexer);
10328 add_capture (lambda_expr,
10329 /*id=*/this_identifier,
10330 /*initializer=*/finish_this_expr (),
10331 /*by_reference_p=*/true,
10332 explicit_init_p);
10333 continue;
10336 /* Possibly capture `*this'. */
10337 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10338 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10340 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10341 if (cxx_dialect < cxx17)
10342 pedwarn (loc, 0, "%<*this%> capture only available with "
10343 "-std=c++17 or -std=gnu++17");
10344 cp_lexer_consume_token (parser->lexer);
10345 cp_lexer_consume_token (parser->lexer);
10346 add_capture (lambda_expr,
10347 /*id=*/this_identifier,
10348 /*initializer=*/finish_this_expr (),
10349 /*by_reference_p=*/false,
10350 explicit_init_p);
10351 continue;
10354 /* Remember whether we want to capture as a reference or not. */
10355 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10357 capture_kind = BY_REFERENCE;
10358 cp_lexer_consume_token (parser->lexer);
10361 /* Get the identifier. */
10362 capture_token = cp_lexer_peek_token (parser->lexer);
10363 capture_id = cp_parser_identifier (parser);
10365 if (capture_id == error_mark_node)
10366 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10367 delimiters, but I modified this to stop on unnested ']' as well. It
10368 was already changed to stop on unnested '}', so the
10369 "closing_parenthesis" name is no more misleading with my change. */
10371 cp_parser_skip_to_closing_parenthesis (parser,
10372 /*recovering=*/true,
10373 /*or_comma=*/true,
10374 /*consume_paren=*/true);
10375 break;
10378 /* Find the initializer for this capture. */
10379 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10380 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10381 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10383 bool direct, non_constant;
10384 /* An explicit initializer exists. */
10385 if (cxx_dialect < cxx14)
10386 pedwarn (input_location, 0,
10387 "lambda capture initializers "
10388 "only available with -std=c++14 or -std=gnu++14");
10389 capture_init_expr = cp_parser_initializer (parser, &direct,
10390 &non_constant, true);
10391 explicit_init_p = true;
10392 if (capture_init_expr == NULL_TREE)
10394 error ("empty initializer for lambda init-capture");
10395 capture_init_expr = error_mark_node;
10398 else
10400 const char* error_msg;
10402 /* Turn the identifier into an id-expression. */
10403 capture_init_expr
10404 = cp_parser_lookup_name_simple (parser, capture_id,
10405 capture_token->location);
10407 if (capture_init_expr == error_mark_node)
10409 unqualified_name_lookup_error (capture_id);
10410 continue;
10412 else if (!VAR_P (capture_init_expr)
10413 && TREE_CODE (capture_init_expr) != PARM_DECL)
10415 error_at (capture_token->location,
10416 "capture of non-variable %qE",
10417 capture_init_expr);
10418 if (DECL_P (capture_init_expr))
10419 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10420 "%q#D declared here", capture_init_expr);
10421 continue;
10423 if (VAR_P (capture_init_expr)
10424 && decl_storage_duration (capture_init_expr) != dk_auto)
10426 if (pedwarn (capture_token->location, 0, "capture of variable "
10427 "%qD with non-automatic storage duration",
10428 capture_init_expr))
10429 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10430 "%q#D declared here", capture_init_expr);
10431 continue;
10434 capture_init_expr
10435 = finish_id_expression
10436 (capture_id,
10437 capture_init_expr,
10438 parser->scope,
10439 &idk,
10440 /*integral_constant_expression_p=*/false,
10441 /*allow_non_integral_constant_expression_p=*/false,
10442 /*non_integral_constant_expression_p=*/NULL,
10443 /*template_p=*/false,
10444 /*done=*/true,
10445 /*address_p=*/false,
10446 /*template_arg_p=*/false,
10447 &error_msg,
10448 capture_token->location);
10450 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10452 cp_lexer_consume_token (parser->lexer);
10453 capture_init_expr = make_pack_expansion (capture_init_expr);
10457 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10458 && !explicit_init_p)
10460 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10461 && capture_kind == BY_COPY)
10462 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10463 "of %qD redundant with by-copy capture default",
10464 capture_id);
10465 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10466 && capture_kind == BY_REFERENCE)
10467 pedwarn (capture_token->location, 0, "explicit by-reference "
10468 "capture of %qD redundant with by-reference capture "
10469 "default", capture_id);
10472 add_capture (lambda_expr,
10473 capture_id,
10474 capture_init_expr,
10475 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10476 explicit_init_p);
10478 /* If there is any qualification still in effect, clear it
10479 now; we will be starting fresh with the next capture. */
10480 parser->scope = NULL_TREE;
10481 parser->qualifying_scope = NULL_TREE;
10482 parser->object_scope = NULL_TREE;
10485 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10488 /* Parse the (optional) middle of a lambda expression.
10490 lambda-declarator:
10491 < template-parameter-list [opt] >
10492 ( parameter-declaration-clause [opt] )
10493 attribute-specifier [opt]
10494 decl-specifier-seq [opt]
10495 exception-specification [opt]
10496 lambda-return-type-clause [opt]
10498 LAMBDA_EXPR is the current representation of the lambda expression. */
10500 static bool
10501 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10503 /* 5.1.1.4 of the standard says:
10504 If a lambda-expression does not include a lambda-declarator, it is as if
10505 the lambda-declarator were ().
10506 This means an empty parameter list, no attributes, and no exception
10507 specification. */
10508 tree param_list = void_list_node;
10509 tree attributes = NULL_TREE;
10510 tree exception_spec = NULL_TREE;
10511 tree template_param_list = NULL_TREE;
10512 tree tx_qual = NULL_TREE;
10513 tree return_type = NULL_TREE;
10514 cp_decl_specifier_seq lambda_specs;
10515 clear_decl_specs (&lambda_specs);
10517 /* The template-parameter-list is optional, but must begin with
10518 an opening angle if present. */
10519 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10521 if (cxx_dialect < cxx14)
10522 pedwarn (parser->lexer->next_token->location, 0,
10523 "lambda templates are only available with "
10524 "-std=c++14 or -std=gnu++14");
10525 else if (cxx_dialect < cxx2a)
10526 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10527 "lambda templates are only available with "
10528 "-std=c++2a or -std=gnu++2a");
10530 cp_lexer_consume_token (parser->lexer);
10532 template_param_list = cp_parser_template_parameter_list (parser);
10534 cp_parser_skip_to_end_of_template_parameter_list (parser);
10536 /* We just processed one more parameter list. */
10537 ++parser->num_template_parameter_lists;
10540 /* The parameter-declaration-clause is optional (unless
10541 template-parameter-list was given), but must begin with an
10542 opening parenthesis if present. */
10543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10545 matching_parens parens;
10546 parens.consume_open (parser);
10548 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10550 /* Parse parameters. */
10551 param_list = cp_parser_parameter_declaration_clause (parser);
10553 /* Default arguments shall not be specified in the
10554 parameter-declaration-clause of a lambda-declarator. */
10555 if (cxx_dialect < cxx14)
10556 for (tree t = param_list; t; t = TREE_CHAIN (t))
10557 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10558 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10559 "default argument specified for lambda parameter");
10561 parens.require_close (parser);
10563 attributes = cp_parser_attributes_opt (parser);
10565 /* In the decl-specifier-seq of the lambda-declarator, each
10566 decl-specifier shall either be mutable or constexpr. */
10567 int declares_class_or_enum;
10568 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10569 cp_parser_decl_specifier_seq (parser,
10570 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10571 &lambda_specs, &declares_class_or_enum);
10572 if (lambda_specs.storage_class == sc_mutable)
10574 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10575 if (lambda_specs.conflicting_specifiers_p)
10576 error_at (lambda_specs.locations[ds_storage_class],
10577 "duplicate %<mutable%>");
10580 tx_qual = cp_parser_tx_qualifier_opt (parser);
10582 /* Parse optional exception specification. */
10583 exception_spec = cp_parser_exception_specification_opt (parser);
10585 /* Parse optional trailing return type. */
10586 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10588 cp_lexer_consume_token (parser->lexer);
10589 return_type = cp_parser_trailing_type_id (parser);
10592 /* The function parameters must be in scope all the way until after the
10593 trailing-return-type in case of decltype. */
10594 pop_bindings_and_leave_scope ();
10596 else if (template_param_list != NULL_TREE) // generate diagnostic
10597 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10599 /* Create the function call operator.
10601 Messing with declarators like this is no uglier than building up the
10602 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10603 other code. */
10605 cp_decl_specifier_seq return_type_specs;
10606 cp_declarator* declarator;
10607 tree fco;
10608 int quals;
10609 void *p;
10611 clear_decl_specs (&return_type_specs);
10612 return_type_specs.type = make_auto ();
10614 if (lambda_specs.locations[ds_constexpr])
10616 if (cxx_dialect >= cxx17)
10617 return_type_specs.locations[ds_constexpr]
10618 = lambda_specs.locations[ds_constexpr];
10619 else
10620 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10621 "lambda only available with -std=c++17 or -std=gnu++17");
10624 p = obstack_alloc (&declarator_obstack, 0);
10626 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10628 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10629 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10630 declarator = make_call_declarator (declarator, param_list, quals,
10631 VIRT_SPEC_UNSPECIFIED,
10632 REF_QUAL_NONE,
10633 tx_qual,
10634 exception_spec,
10635 /*late_return_type=*/NULL_TREE,
10636 /*requires_clause*/NULL_TREE);
10637 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10638 if (return_type)
10639 declarator->u.function.late_return_type = return_type;
10641 fco = grokmethod (&return_type_specs,
10642 declarator,
10643 attributes);
10644 if (fco != error_mark_node)
10646 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10647 DECL_ARTIFICIAL (fco) = 1;
10648 /* Give the object parameter a different name. */
10649 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10651 if (template_param_list)
10653 fco = finish_member_template_decl (fco);
10654 finish_template_decl (template_param_list);
10655 --parser->num_template_parameter_lists;
10657 else if (parser->fully_implicit_function_template_p)
10658 fco = finish_fully_implicit_template (parser, fco);
10660 finish_member_declaration (fco);
10662 obstack_free (&declarator_obstack, p);
10664 return (fco != error_mark_node);
10668 /* Parse the body of a lambda expression, which is simply
10670 compound-statement
10672 but which requires special handling.
10673 LAMBDA_EXPR is the current representation of the lambda expression. */
10675 static void
10676 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10678 bool nested = (current_function_decl != NULL_TREE);
10679 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10680 bool in_function_body = parser->in_function_body;
10682 if (nested)
10683 push_function_context ();
10684 else
10685 /* Still increment function_depth so that we don't GC in the
10686 middle of an expression. */
10687 ++function_depth;
10689 vec<tree> omp_privatization_save;
10690 save_omp_privatization_clauses (omp_privatization_save);
10691 /* Clear this in case we're in the middle of a default argument. */
10692 parser->local_variables_forbidden_p = false;
10693 parser->in_function_body = true;
10696 local_specialization_stack s (lss_copy);
10697 tree fco = lambda_function (lambda_expr);
10698 tree body = start_lambda_function (fco, lambda_expr);
10699 matching_braces braces;
10701 if (braces.require_open (parser))
10703 tree compound_stmt = begin_compound_stmt (0);
10705 /* Originally C++11 required us to peek for 'return expr'; and
10706 process it specially here to deduce the return type. N3638
10707 removed the need for that. */
10709 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10710 cp_parser_label_declaration (parser);
10711 cp_parser_statement_seq_opt (parser, NULL_TREE);
10712 braces.require_close (parser);
10714 finish_compound_stmt (compound_stmt);
10717 finish_lambda_function (body);
10720 restore_omp_privatization_clauses (omp_privatization_save);
10721 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10722 parser->in_function_body = in_function_body;
10723 if (nested)
10724 pop_function_context();
10725 else
10726 --function_depth;
10729 /* Statements [gram.stmt.stmt] */
10731 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10733 static void
10734 add_debug_begin_stmt (location_t loc)
10736 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10737 return;
10738 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10739 /* A concept is never expanded normally. */
10740 return;
10742 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10743 SET_EXPR_LOCATION (stmt, loc);
10744 add_stmt (stmt);
10747 /* Parse a statement.
10749 statement:
10750 labeled-statement
10751 expression-statement
10752 compound-statement
10753 selection-statement
10754 iteration-statement
10755 jump-statement
10756 declaration-statement
10757 try-block
10759 C++11:
10761 statement:
10762 labeled-statement
10763 attribute-specifier-seq (opt) expression-statement
10764 attribute-specifier-seq (opt) compound-statement
10765 attribute-specifier-seq (opt) selection-statement
10766 attribute-specifier-seq (opt) iteration-statement
10767 attribute-specifier-seq (opt) jump-statement
10768 declaration-statement
10769 attribute-specifier-seq (opt) try-block
10771 init-statement:
10772 expression-statement
10773 simple-declaration
10775 TM Extension:
10777 statement:
10778 atomic-statement
10780 IN_COMPOUND is true when the statement is nested inside a
10781 cp_parser_compound_statement; this matters for certain pragmas.
10783 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10784 is a (possibly labeled) if statement which is not enclosed in braces
10785 and has an else clause. This is used to implement -Wparentheses.
10787 CHAIN is a vector of if-else-if conditions. */
10789 static void
10790 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10791 bool in_compound, bool *if_p, vec<tree> *chain,
10792 location_t *loc_after_labels)
10794 tree statement, std_attrs = NULL_TREE;
10795 cp_token *token;
10796 location_t statement_location, attrs_location;
10798 restart:
10799 if (if_p != NULL)
10800 *if_p = false;
10801 /* There is no statement yet. */
10802 statement = NULL_TREE;
10804 saved_token_sentinel saved_tokens (parser->lexer);
10805 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10806 if (c_dialect_objc ())
10807 /* In obj-c++, seeing '[[' might be the either the beginning of
10808 c++11 attributes, or a nested objc-message-expression. So
10809 let's parse the c++11 attributes tentatively. */
10810 cp_parser_parse_tentatively (parser);
10811 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10812 if (c_dialect_objc ())
10814 if (!cp_parser_parse_definitely (parser))
10815 std_attrs = NULL_TREE;
10818 /* Peek at the next token. */
10819 token = cp_lexer_peek_token (parser->lexer);
10820 /* Remember the location of the first token in the statement. */
10821 statement_location = token->location;
10822 add_debug_begin_stmt (statement_location);
10823 /* If this is a keyword, then that will often determine what kind of
10824 statement we have. */
10825 if (token->type == CPP_KEYWORD)
10827 enum rid keyword = token->keyword;
10829 switch (keyword)
10831 case RID_CASE:
10832 case RID_DEFAULT:
10833 /* Looks like a labeled-statement with a case label.
10834 Parse the label, and then use tail recursion to parse
10835 the statement. */
10836 cp_parser_label_for_labeled_statement (parser, std_attrs);
10837 in_compound = false;
10838 goto restart;
10840 case RID_IF:
10841 case RID_SWITCH:
10842 statement = cp_parser_selection_statement (parser, if_p, chain);
10843 break;
10845 case RID_WHILE:
10846 case RID_DO:
10847 case RID_FOR:
10848 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10849 break;
10851 case RID_BREAK:
10852 case RID_CONTINUE:
10853 case RID_RETURN:
10854 case RID_GOTO:
10855 statement = cp_parser_jump_statement (parser);
10856 break;
10858 /* Objective-C++ exception-handling constructs. */
10859 case RID_AT_TRY:
10860 case RID_AT_CATCH:
10861 case RID_AT_FINALLY:
10862 case RID_AT_SYNCHRONIZED:
10863 case RID_AT_THROW:
10864 statement = cp_parser_objc_statement (parser);
10865 break;
10867 case RID_TRY:
10868 statement = cp_parser_try_block (parser);
10869 break;
10871 case RID_NAMESPACE:
10872 /* This must be a namespace alias definition. */
10873 cp_parser_declaration_statement (parser);
10874 return;
10876 case RID_TRANSACTION_ATOMIC:
10877 case RID_TRANSACTION_RELAXED:
10878 case RID_SYNCHRONIZED:
10879 case RID_ATOMIC_NOEXCEPT:
10880 case RID_ATOMIC_CANCEL:
10881 statement = cp_parser_transaction (parser, token);
10882 break;
10883 case RID_TRANSACTION_CANCEL:
10884 statement = cp_parser_transaction_cancel (parser);
10885 break;
10887 default:
10888 /* It might be a keyword like `int' that can start a
10889 declaration-statement. */
10890 break;
10893 else if (token->type == CPP_NAME)
10895 /* If the next token is a `:', then we are looking at a
10896 labeled-statement. */
10897 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10898 if (token->type == CPP_COLON)
10900 /* Looks like a labeled-statement with an ordinary label.
10901 Parse the label, and then use tail recursion to parse
10902 the statement. */
10904 cp_parser_label_for_labeled_statement (parser, std_attrs);
10905 in_compound = false;
10906 goto restart;
10909 /* Anything that starts with a `{' must be a compound-statement. */
10910 else if (token->type == CPP_OPEN_BRACE)
10911 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10912 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10913 a statement all its own. */
10914 else if (token->type == CPP_PRAGMA)
10916 /* Only certain OpenMP pragmas are attached to statements, and thus
10917 are considered statements themselves. All others are not. In
10918 the context of a compound, accept the pragma as a "statement" and
10919 return so that we can check for a close brace. Otherwise we
10920 require a real statement and must go back and read one. */
10921 if (in_compound)
10922 cp_parser_pragma (parser, pragma_compound, if_p);
10923 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10924 goto restart;
10925 return;
10927 else if (token->type == CPP_EOF)
10929 cp_parser_error (parser, "expected statement");
10930 return;
10933 /* Everything else must be a declaration-statement or an
10934 expression-statement. Try for the declaration-statement
10935 first, unless we are looking at a `;', in which case we know that
10936 we have an expression-statement. */
10937 if (!statement)
10939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10941 if (std_attrs != NULL_TREE)
10943 /* Attributes should be parsed as part of the the
10944 declaration, so let's un-parse them. */
10945 saved_tokens.rollback();
10946 std_attrs = NULL_TREE;
10949 cp_parser_parse_tentatively (parser);
10950 /* Try to parse the declaration-statement. */
10951 cp_parser_declaration_statement (parser);
10952 /* If that worked, we're done. */
10953 if (cp_parser_parse_definitely (parser))
10954 return;
10956 /* All preceding labels have been parsed at this point. */
10957 if (loc_after_labels != NULL)
10958 *loc_after_labels = statement_location;
10960 /* Look for an expression-statement instead. */
10961 statement = cp_parser_expression_statement (parser, in_statement_expr);
10963 /* Handle [[fallthrough]];. */
10964 if (attribute_fallthrough_p (std_attrs))
10966 /* The next token after the fallthrough attribute is ';'. */
10967 if (statement == NULL_TREE)
10969 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10970 statement = build_call_expr_internal_loc (statement_location,
10971 IFN_FALLTHROUGH,
10972 void_type_node, 0);
10973 finish_expr_stmt (statement);
10975 else
10976 warning_at (statement_location, OPT_Wattributes,
10977 "%<fallthrough%> attribute not followed by %<;%>");
10978 std_attrs = NULL_TREE;
10982 /* Set the line number for the statement. */
10983 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10984 SET_EXPR_LOCATION (statement, statement_location);
10986 /* Allow "[[fallthrough]];", but warn otherwise. */
10987 if (std_attrs != NULL_TREE)
10988 warning_at (attrs_location,
10989 OPT_Wattributes,
10990 "attributes at the beginning of statement are ignored");
10993 /* Append ATTR to attribute list ATTRS. */
10995 static tree
10996 attr_chainon (tree attrs, tree attr)
10998 if (attrs == error_mark_node)
10999 return error_mark_node;
11000 if (attr == error_mark_node)
11001 return error_mark_node;
11002 return chainon (attrs, attr);
11005 /* Parse the label for a labeled-statement, i.e.
11007 identifier :
11008 case constant-expression :
11009 default :
11011 GNU Extension:
11012 case constant-expression ... constant-expression : statement
11014 When a label is parsed without errors, the label is added to the
11015 parse tree by the finish_* functions, so this function doesn't
11016 have to return the label. */
11018 static void
11019 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11021 cp_token *token;
11022 tree label = NULL_TREE;
11023 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11025 /* The next token should be an identifier. */
11026 token = cp_lexer_peek_token (parser->lexer);
11027 if (token->type != CPP_NAME
11028 && token->type != CPP_KEYWORD)
11030 cp_parser_error (parser, "expected labeled-statement");
11031 return;
11034 /* Remember whether this case or a user-defined label is allowed to fall
11035 through to. */
11036 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11038 parser->colon_corrects_to_scope_p = false;
11039 switch (token->keyword)
11041 case RID_CASE:
11043 tree expr, expr_hi;
11044 cp_token *ellipsis;
11046 /* Consume the `case' token. */
11047 cp_lexer_consume_token (parser->lexer);
11048 /* Parse the constant-expression. */
11049 expr = cp_parser_constant_expression (parser);
11050 if (check_for_bare_parameter_packs (expr))
11051 expr = error_mark_node;
11053 ellipsis = cp_lexer_peek_token (parser->lexer);
11054 if (ellipsis->type == CPP_ELLIPSIS)
11056 /* Consume the `...' token. */
11057 cp_lexer_consume_token (parser->lexer);
11058 expr_hi = cp_parser_constant_expression (parser);
11059 if (check_for_bare_parameter_packs (expr_hi))
11060 expr_hi = error_mark_node;
11062 /* We don't need to emit warnings here, as the common code
11063 will do this for us. */
11065 else
11066 expr_hi = NULL_TREE;
11068 if (parser->in_switch_statement_p)
11070 tree l = finish_case_label (token->location, expr, expr_hi);
11071 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11072 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11074 else
11075 error_at (token->location,
11076 "case label %qE not within a switch statement",
11077 expr);
11079 break;
11081 case RID_DEFAULT:
11082 /* Consume the `default' token. */
11083 cp_lexer_consume_token (parser->lexer);
11085 if (parser->in_switch_statement_p)
11087 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11088 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11089 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11091 else
11092 error_at (token->location, "case label not within a switch statement");
11093 break;
11095 default:
11096 /* Anything else must be an ordinary label. */
11097 label = finish_label_stmt (cp_parser_identifier (parser));
11098 if (label && TREE_CODE (label) == LABEL_DECL)
11099 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11100 break;
11103 /* Require the `:' token. */
11104 cp_parser_require (parser, CPP_COLON, RT_COLON);
11106 /* An ordinary label may optionally be followed by attributes.
11107 However, this is only permitted if the attributes are then
11108 followed by a semicolon. This is because, for backward
11109 compatibility, when parsing
11110 lab: __attribute__ ((unused)) int i;
11111 we want the attribute to attach to "i", not "lab". */
11112 if (label != NULL_TREE
11113 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11115 tree attrs;
11116 cp_parser_parse_tentatively (parser);
11117 attrs = cp_parser_gnu_attributes_opt (parser);
11118 if (attrs == NULL_TREE
11119 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11120 cp_parser_abort_tentative_parse (parser);
11121 else if (!cp_parser_parse_definitely (parser))
11123 else
11124 attributes = attr_chainon (attributes, attrs);
11127 if (attributes != NULL_TREE)
11128 cplus_decl_attributes (&label, attributes, 0);
11130 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11133 /* Parse an expression-statement.
11135 expression-statement:
11136 expression [opt] ;
11138 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11139 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11140 indicates whether this expression-statement is part of an
11141 expression statement. */
11143 static tree
11144 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11146 tree statement = NULL_TREE;
11147 cp_token *token = cp_lexer_peek_token (parser->lexer);
11148 location_t loc = token->location;
11150 /* There might be attribute fallthrough. */
11151 tree attr = cp_parser_gnu_attributes_opt (parser);
11153 /* If the next token is a ';', then there is no expression
11154 statement. */
11155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11157 statement = cp_parser_expression (parser);
11158 if (statement == error_mark_node
11159 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11161 cp_parser_skip_to_end_of_block_or_statement (parser);
11162 return error_mark_node;
11166 /* Handle [[fallthrough]];. */
11167 if (attribute_fallthrough_p (attr))
11169 /* The next token after the fallthrough attribute is ';'. */
11170 if (statement == NULL_TREE)
11171 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11172 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11173 void_type_node, 0);
11174 else
11175 warning_at (loc, OPT_Wattributes,
11176 "%<fallthrough%> attribute not followed by %<;%>");
11177 attr = NULL_TREE;
11180 /* Allow "[[fallthrough]];", but warn otherwise. */
11181 if (attr != NULL_TREE)
11182 warning_at (loc, OPT_Wattributes,
11183 "attributes at the beginning of statement are ignored");
11185 /* Give a helpful message for "A<T>::type t;" and the like. */
11186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11187 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11189 if (TREE_CODE (statement) == SCOPE_REF)
11190 error_at (token->location, "need %<typename%> before %qE because "
11191 "%qT is a dependent scope",
11192 statement, TREE_OPERAND (statement, 0));
11193 else if (is_overloaded_fn (statement)
11194 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11196 /* A::A a; */
11197 tree fn = get_first_fn (statement);
11198 error_at (token->location,
11199 "%<%T::%D%> names the constructor, not the type",
11200 DECL_CONTEXT (fn), DECL_NAME (fn));
11204 /* Consume the final `;'. */
11205 cp_parser_consume_semicolon_at_end_of_statement (parser);
11207 if (in_statement_expr
11208 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11209 /* This is the final expression statement of a statement
11210 expression. */
11211 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11212 else if (statement)
11213 statement = finish_expr_stmt (statement);
11215 return statement;
11218 /* Parse a compound-statement.
11220 compound-statement:
11221 { statement-seq [opt] }
11223 GNU extension:
11225 compound-statement:
11226 { label-declaration-seq [opt] statement-seq [opt] }
11228 label-declaration-seq:
11229 label-declaration
11230 label-declaration-seq label-declaration
11232 Returns a tree representing the statement. */
11234 static tree
11235 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11236 int bcs_flags, bool function_body)
11238 tree compound_stmt;
11239 matching_braces braces;
11241 /* Consume the `{'. */
11242 if (!braces.require_open (parser))
11243 return error_mark_node;
11244 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11245 && !function_body && cxx_dialect < cxx14)
11246 pedwarn (input_location, OPT_Wpedantic,
11247 "compound-statement in %<constexpr%> function");
11248 /* Begin the compound-statement. */
11249 compound_stmt = begin_compound_stmt (bcs_flags);
11250 /* If the next keyword is `__label__' we have a label declaration. */
11251 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11252 cp_parser_label_declaration (parser);
11253 /* Parse an (optional) statement-seq. */
11254 cp_parser_statement_seq_opt (parser, in_statement_expr);
11255 /* Finish the compound-statement. */
11256 finish_compound_stmt (compound_stmt);
11257 /* Consume the `}'. */
11258 braces.require_close (parser);
11260 return compound_stmt;
11263 /* Parse an (optional) statement-seq.
11265 statement-seq:
11266 statement
11267 statement-seq [opt] statement */
11269 static void
11270 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11272 /* Scan statements until there aren't any more. */
11273 while (true)
11275 cp_token *token = cp_lexer_peek_token (parser->lexer);
11277 /* If we are looking at a `}', then we have run out of
11278 statements; the same is true if we have reached the end
11279 of file, or have stumbled upon a stray '@end'. */
11280 if (token->type == CPP_CLOSE_BRACE
11281 || token->type == CPP_EOF
11282 || token->type == CPP_PRAGMA_EOL
11283 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11284 break;
11286 /* If we are in a compound statement and find 'else' then
11287 something went wrong. */
11288 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11290 if (parser->in_statement & IN_IF_STMT)
11291 break;
11292 else
11294 token = cp_lexer_consume_token (parser->lexer);
11295 error_at (token->location, "%<else%> without a previous %<if%>");
11299 /* Parse the statement. */
11300 cp_parser_statement (parser, in_statement_expr, true, NULL);
11304 /* Return true if this is the C++20 version of range-based-for with
11305 init-statement. */
11307 static bool
11308 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11310 bool r = false;
11312 /* Save tokens so that we can put them back. */
11313 cp_lexer_save_tokens (parser->lexer);
11315 /* There has to be an unnested ; followed by an unnested :. */
11316 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11317 /*recovering=*/false,
11318 CPP_SEMICOLON,
11319 /*consume_paren=*/false) != -1)
11320 goto out;
11322 /* We found the semicolon, eat it now. */
11323 cp_lexer_consume_token (parser->lexer);
11325 /* Now look for ':' that is not nested in () or {}. */
11326 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11327 /*recovering=*/false,
11328 CPP_COLON,
11329 /*consume_paren=*/false) == -1);
11331 out:
11332 /* Roll back the tokens we skipped. */
11333 cp_lexer_rollback_tokens (parser->lexer);
11335 return r;
11338 /* Return true if we're looking at (init; cond), false otherwise. */
11340 static bool
11341 cp_parser_init_statement_p (cp_parser *parser)
11343 /* Save tokens so that we can put them back. */
11344 cp_lexer_save_tokens (parser->lexer);
11346 /* Look for ';' that is not nested in () or {}. */
11347 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11348 /*recovering=*/false,
11349 CPP_SEMICOLON,
11350 /*consume_paren=*/false);
11352 /* Roll back the tokens we skipped. */
11353 cp_lexer_rollback_tokens (parser->lexer);
11355 return ret == -1;
11358 /* Parse a selection-statement.
11360 selection-statement:
11361 if ( init-statement [opt] condition ) statement
11362 if ( init-statement [opt] condition ) statement else statement
11363 switch ( init-statement [opt] condition ) statement
11365 Returns the new IF_STMT or SWITCH_STMT.
11367 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11368 is a (possibly labeled) if statement which is not enclosed in
11369 braces and has an else clause. This is used to implement
11370 -Wparentheses.
11372 CHAIN is a vector of if-else-if conditions. This is used to implement
11373 -Wduplicated-cond. */
11375 static tree
11376 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11377 vec<tree> *chain)
11379 cp_token *token;
11380 enum rid keyword;
11381 token_indent_info guard_tinfo;
11383 if (if_p != NULL)
11384 *if_p = false;
11386 /* Peek at the next token. */
11387 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11388 guard_tinfo = get_token_indent_info (token);
11390 /* See what kind of keyword it is. */
11391 keyword = token->keyword;
11392 switch (keyword)
11394 case RID_IF:
11395 case RID_SWITCH:
11397 tree statement;
11398 tree condition;
11400 bool cx = false;
11401 if (keyword == RID_IF
11402 && cp_lexer_next_token_is_keyword (parser->lexer,
11403 RID_CONSTEXPR))
11405 cx = true;
11406 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11407 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11408 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11409 "with -std=c++17 or -std=gnu++17");
11412 /* Look for the `('. */
11413 matching_parens parens;
11414 if (!parens.require_open (parser))
11416 cp_parser_skip_to_end_of_statement (parser);
11417 return error_mark_node;
11420 /* Begin the selection-statement. */
11421 if (keyword == RID_IF)
11423 statement = begin_if_stmt ();
11424 IF_STMT_CONSTEXPR_P (statement) = cx;
11426 else
11427 statement = begin_switch_stmt ();
11429 /* Parse the optional init-statement. */
11430 if (cp_parser_init_statement_p (parser))
11432 tree decl;
11433 if (cxx_dialect < cxx17)
11434 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11435 "init-statement in selection statements only available "
11436 "with -std=c++17 or -std=gnu++17");
11437 cp_parser_init_statement (parser, &decl);
11440 /* Parse the condition. */
11441 condition = cp_parser_condition (parser);
11442 /* Look for the `)'. */
11443 if (!parens.require_close (parser))
11444 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11445 /*consume_paren=*/true);
11447 if (keyword == RID_IF)
11449 bool nested_if;
11450 unsigned char in_statement;
11452 /* Add the condition. */
11453 condition = finish_if_stmt_cond (condition, statement);
11455 if (warn_duplicated_cond)
11456 warn_duplicated_cond_add_or_warn (token->location, condition,
11457 &chain);
11459 /* Parse the then-clause. */
11460 in_statement = parser->in_statement;
11461 parser->in_statement |= IN_IF_STMT;
11463 /* Outside a template, the non-selected branch of a constexpr
11464 if is a 'discarded statement', i.e. unevaluated. */
11465 bool was_discarded = in_discarded_stmt;
11466 bool discard_then = (cx && !processing_template_decl
11467 && integer_zerop (condition));
11468 if (discard_then)
11470 in_discarded_stmt = true;
11471 ++c_inhibit_evaluation_warnings;
11474 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11475 guard_tinfo);
11477 parser->in_statement = in_statement;
11479 finish_then_clause (statement);
11481 if (discard_then)
11483 THEN_CLAUSE (statement) = NULL_TREE;
11484 in_discarded_stmt = was_discarded;
11485 --c_inhibit_evaluation_warnings;
11488 /* If the next token is `else', parse the else-clause. */
11489 if (cp_lexer_next_token_is_keyword (parser->lexer,
11490 RID_ELSE))
11492 bool discard_else = (cx && !processing_template_decl
11493 && integer_nonzerop (condition));
11494 if (discard_else)
11496 in_discarded_stmt = true;
11497 ++c_inhibit_evaluation_warnings;
11500 guard_tinfo
11501 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11502 /* Consume the `else' keyword. */
11503 cp_lexer_consume_token (parser->lexer);
11504 if (warn_duplicated_cond)
11506 if (cp_lexer_next_token_is_keyword (parser->lexer,
11507 RID_IF)
11508 && chain == NULL)
11510 /* We've got "if (COND) else if (COND2)". Start
11511 the condition chain and add COND as the first
11512 element. */
11513 chain = new vec<tree> ();
11514 if (!CONSTANT_CLASS_P (condition)
11515 && !TREE_SIDE_EFFECTS (condition))
11517 /* Wrap it in a NOP_EXPR so that we can set the
11518 location of the condition. */
11519 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11520 condition);
11521 SET_EXPR_LOCATION (e, token->location);
11522 chain->safe_push (e);
11525 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11526 RID_IF))
11528 /* This is if-else without subsequent if. Zap the
11529 condition chain; we would have already warned at
11530 this point. */
11531 delete chain;
11532 chain = NULL;
11535 begin_else_clause (statement);
11536 /* Parse the else-clause. */
11537 cp_parser_implicitly_scoped_statement (parser, NULL,
11538 guard_tinfo, chain);
11540 finish_else_clause (statement);
11542 /* If we are currently parsing a then-clause, then
11543 IF_P will not be NULL. We set it to true to
11544 indicate that this if statement has an else clause.
11545 This may trigger the Wparentheses warning below
11546 when we get back up to the parent if statement. */
11547 if (if_p != NULL)
11548 *if_p = true;
11550 if (discard_else)
11552 ELSE_CLAUSE (statement) = NULL_TREE;
11553 in_discarded_stmt = was_discarded;
11554 --c_inhibit_evaluation_warnings;
11557 else
11559 /* This if statement does not have an else clause. If
11560 NESTED_IF is true, then the then-clause has an if
11561 statement which does have an else clause. We warn
11562 about the potential ambiguity. */
11563 if (nested_if)
11564 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11565 "suggest explicit braces to avoid ambiguous"
11566 " %<else%>");
11567 if (warn_duplicated_cond)
11569 /* We don't need the condition chain anymore. */
11570 delete chain;
11571 chain = NULL;
11575 /* Now we're all done with the if-statement. */
11576 finish_if_stmt (statement);
11578 else
11580 bool in_switch_statement_p;
11581 unsigned char in_statement;
11583 /* Add the condition. */
11584 finish_switch_cond (condition, statement);
11586 /* Parse the body of the switch-statement. */
11587 in_switch_statement_p = parser->in_switch_statement_p;
11588 in_statement = parser->in_statement;
11589 parser->in_switch_statement_p = true;
11590 parser->in_statement |= IN_SWITCH_STMT;
11591 cp_parser_implicitly_scoped_statement (parser, if_p,
11592 guard_tinfo);
11593 parser->in_switch_statement_p = in_switch_statement_p;
11594 parser->in_statement = in_statement;
11596 /* Now we're all done with the switch-statement. */
11597 finish_switch_stmt (statement);
11600 return statement;
11602 break;
11604 default:
11605 cp_parser_error (parser, "expected selection-statement");
11606 return error_mark_node;
11610 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11611 If we have seen at least one decl-specifier, and the next token
11612 is not a parenthesis, then we must be looking at a declaration.
11613 (After "int (" we might be looking at a functional cast.) */
11615 static void
11616 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11617 bool any_specifiers_p)
11619 if (any_specifiers_p
11620 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11621 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11622 && !cp_parser_error_occurred (parser))
11623 cp_parser_commit_to_tentative_parse (parser);
11626 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11627 The declarator shall not specify a function or an array. Returns
11628 TRUE if the declarator is valid, FALSE otherwise. */
11630 static bool
11631 cp_parser_check_condition_declarator (cp_parser* parser,
11632 cp_declarator *declarator,
11633 location_t loc)
11635 if (declarator == cp_error_declarator
11636 || function_declarator_p (declarator)
11637 || declarator->kind == cdk_array)
11639 if (declarator == cp_error_declarator)
11640 /* Already complained. */;
11641 else if (declarator->kind == cdk_array)
11642 error_at (loc, "condition declares an array");
11643 else
11644 error_at (loc, "condition declares a function");
11645 if (parser->fully_implicit_function_template_p)
11646 abort_fully_implicit_template (parser);
11647 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11648 /*or_comma=*/false,
11649 /*consume_paren=*/false);
11650 return false;
11652 else
11653 return true;
11656 /* Parse a condition.
11658 condition:
11659 expression
11660 type-specifier-seq declarator = initializer-clause
11661 type-specifier-seq declarator braced-init-list
11663 GNU Extension:
11665 condition:
11666 type-specifier-seq declarator asm-specification [opt]
11667 attributes [opt] = assignment-expression
11669 Returns the expression that should be tested. */
11671 static tree
11672 cp_parser_condition (cp_parser* parser)
11674 cp_decl_specifier_seq type_specifiers;
11675 const char *saved_message;
11676 int declares_class_or_enum;
11678 /* Try the declaration first. */
11679 cp_parser_parse_tentatively (parser);
11680 /* New types are not allowed in the type-specifier-seq for a
11681 condition. */
11682 saved_message = parser->type_definition_forbidden_message;
11683 parser->type_definition_forbidden_message
11684 = G_("types may not be defined in conditions");
11685 /* Parse the type-specifier-seq. */
11686 cp_parser_decl_specifier_seq (parser,
11687 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11688 &type_specifiers,
11689 &declares_class_or_enum);
11690 /* Restore the saved message. */
11691 parser->type_definition_forbidden_message = saved_message;
11693 cp_parser_maybe_commit_to_declaration (parser,
11694 type_specifiers.any_specifiers_p);
11696 /* If all is well, we might be looking at a declaration. */
11697 if (!cp_parser_error_occurred (parser))
11699 tree decl;
11700 tree asm_specification;
11701 tree attributes;
11702 cp_declarator *declarator;
11703 tree initializer = NULL_TREE;
11704 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11706 /* Parse the declarator. */
11707 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11708 /*ctor_dtor_or_conv_p=*/NULL,
11709 /*parenthesized_p=*/NULL,
11710 /*member_p=*/false,
11711 /*friend_p=*/false);
11712 /* Parse the attributes. */
11713 attributes = cp_parser_attributes_opt (parser);
11714 /* Parse the asm-specification. */
11715 asm_specification = cp_parser_asm_specification_opt (parser);
11716 /* If the next token is not an `=' or '{', then we might still be
11717 looking at an expression. For example:
11719 if (A(a).x)
11721 looks like a decl-specifier-seq and a declarator -- but then
11722 there is no `=', so this is an expression. */
11723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11724 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11725 cp_parser_simulate_error (parser);
11727 /* If we did see an `=' or '{', then we are looking at a declaration
11728 for sure. */
11729 if (cp_parser_parse_definitely (parser))
11731 tree pushed_scope;
11732 bool non_constant_p = false;
11733 int flags = LOOKUP_ONLYCONVERTING;
11735 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11736 return error_mark_node;
11738 /* Create the declaration. */
11739 decl = start_decl (declarator, &type_specifiers,
11740 /*initialized_p=*/true,
11741 attributes, /*prefix_attributes=*/NULL_TREE,
11742 &pushed_scope);
11744 /* Parse the initializer. */
11745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11747 initializer = cp_parser_braced_list (parser, &non_constant_p);
11748 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11749 flags = 0;
11751 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11753 /* Consume the `='. */
11754 cp_lexer_consume_token (parser->lexer);
11755 initializer = cp_parser_initializer_clause (parser,
11756 &non_constant_p);
11758 else
11760 cp_parser_error (parser, "expected initializer");
11761 initializer = error_mark_node;
11763 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11766 /* Process the initializer. */
11767 cp_finish_decl (decl,
11768 initializer, !non_constant_p,
11769 asm_specification,
11770 flags);
11772 if (pushed_scope)
11773 pop_scope (pushed_scope);
11775 return convert_from_reference (decl);
11778 /* If we didn't even get past the declarator successfully, we are
11779 definitely not looking at a declaration. */
11780 else
11781 cp_parser_abort_tentative_parse (parser);
11783 /* Otherwise, we are looking at an expression. */
11784 return cp_parser_expression (parser);
11787 /* Parses a for-statement or range-for-statement until the closing ')',
11788 not included. */
11790 static tree
11791 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11793 tree init, scope, decl;
11794 bool is_range_for;
11796 /* Begin the for-statement. */
11797 scope = begin_for_scope (&init);
11799 /* Parse the initialization. */
11800 is_range_for = cp_parser_init_statement (parser, &decl);
11802 if (is_range_for)
11803 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11804 else
11805 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11808 static tree
11809 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11810 unsigned short unroll)
11812 /* Normal for loop */
11813 tree condition = NULL_TREE;
11814 tree expression = NULL_TREE;
11815 tree stmt;
11817 stmt = begin_for_stmt (scope, init);
11818 /* The init-statement has already been parsed in
11819 cp_parser_init_statement, so no work is needed here. */
11820 finish_init_stmt (stmt);
11822 /* If there's a condition, process it. */
11823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11824 condition = cp_parser_condition (parser);
11825 else if (ivdep)
11827 cp_parser_error (parser, "missing loop condition in loop with "
11828 "%<GCC ivdep%> pragma");
11829 condition = error_mark_node;
11831 else if (unroll)
11833 cp_parser_error (parser, "missing loop condition in loop with "
11834 "%<GCC unroll%> pragma");
11835 condition = error_mark_node;
11837 finish_for_cond (condition, stmt, ivdep, unroll);
11838 /* Look for the `;'. */
11839 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11841 /* If there's an expression, process it. */
11842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11843 expression = cp_parser_expression (parser);
11844 finish_for_expr (expression, stmt);
11846 return stmt;
11849 /* Tries to parse a range-based for-statement:
11851 range-based-for:
11852 decl-specifier-seq declarator : expression
11854 The decl-specifier-seq declarator and the `:' are already parsed by
11855 cp_parser_init_statement. If processing_template_decl it returns a
11856 newly created RANGE_FOR_STMT; if not, it is converted to a
11857 regular FOR_STMT. */
11859 static tree
11860 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11861 bool ivdep, unsigned short unroll)
11863 tree stmt, range_expr;
11864 auto_vec <cxx_binding *, 16> bindings;
11865 auto_vec <tree, 16> names;
11866 tree decomp_first_name = NULL_TREE;
11867 unsigned int decomp_cnt = 0;
11869 /* Get the range declaration momentarily out of the way so that
11870 the range expression doesn't clash with it. */
11871 if (range_decl != error_mark_node)
11873 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11875 tree v = DECL_VALUE_EXPR (range_decl);
11876 /* For decomposition declaration get all of the corresponding
11877 declarations out of the way. */
11878 if (TREE_CODE (v) == ARRAY_REF
11879 && VAR_P (TREE_OPERAND (v, 0))
11880 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11882 tree d = range_decl;
11883 range_decl = TREE_OPERAND (v, 0);
11884 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11885 decomp_first_name = d;
11886 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11888 tree name = DECL_NAME (d);
11889 names.safe_push (name);
11890 bindings.safe_push (IDENTIFIER_BINDING (name));
11891 IDENTIFIER_BINDING (name)
11892 = IDENTIFIER_BINDING (name)->previous;
11896 if (names.is_empty ())
11898 tree name = DECL_NAME (range_decl);
11899 names.safe_push (name);
11900 bindings.safe_push (IDENTIFIER_BINDING (name));
11901 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11907 bool expr_non_constant_p;
11908 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11910 else
11911 range_expr = cp_parser_expression (parser);
11913 /* Put the range declaration(s) back into scope. */
11914 for (unsigned int i = 0; i < names.length (); i++)
11916 cxx_binding *binding = bindings[i];
11917 binding->previous = IDENTIFIER_BINDING (names[i]);
11918 IDENTIFIER_BINDING (names[i]) = binding;
11921 /* If in template, STMT is converted to a normal for-statement
11922 at instantiation. If not, it is done just ahead. */
11923 if (processing_template_decl)
11925 if (check_for_bare_parameter_packs (range_expr))
11926 range_expr = error_mark_node;
11927 stmt = begin_range_for_stmt (scope, init);
11928 if (ivdep)
11929 RANGE_FOR_IVDEP (stmt) = 1;
11930 if (unroll)
11931 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11932 finish_range_for_decl (stmt, range_decl, range_expr);
11933 if (!type_dependent_expression_p (range_expr)
11934 /* do_auto_deduction doesn't mess with template init-lists. */
11935 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11936 do_range_for_auto_deduction (range_decl, range_expr);
11938 else
11940 stmt = begin_for_stmt (scope, init);
11941 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11942 decomp_first_name, decomp_cnt, ivdep,
11943 unroll);
11945 return stmt;
11948 /* Subroutine of cp_convert_range_for: given the initializer expression,
11949 builds up the range temporary. */
11951 static tree
11952 build_range_temp (tree range_expr)
11954 tree range_type, range_temp;
11956 /* Find out the type deduced by the declaration
11957 `auto &&__range = range_expr'. */
11958 range_type = cp_build_reference_type (make_auto (), true);
11959 range_type = do_auto_deduction (range_type, range_expr,
11960 type_uses_auto (range_type));
11962 /* Create the __range variable. */
11963 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
11964 range_type);
11965 TREE_USED (range_temp) = 1;
11966 DECL_ARTIFICIAL (range_temp) = 1;
11968 return range_temp;
11971 /* Used by cp_parser_range_for in template context: we aren't going to
11972 do a full conversion yet, but we still need to resolve auto in the
11973 type of the for-range-declaration if present. This is basically
11974 a shortcut version of cp_convert_range_for. */
11976 static void
11977 do_range_for_auto_deduction (tree decl, tree range_expr)
11979 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11980 if (auto_node)
11982 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11983 range_temp = convert_from_reference (build_range_temp (range_expr));
11984 iter_type = (cp_parser_perform_range_for_lookup
11985 (range_temp, &begin_dummy, &end_dummy));
11986 if (iter_type)
11988 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11989 iter_type);
11990 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11991 RO_UNARY_STAR,
11992 tf_warning_or_error);
11993 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11994 iter_decl, auto_node);
11999 /* Converts a range-based for-statement into a normal
12000 for-statement, as per the definition.
12002 for (RANGE_DECL : RANGE_EXPR)
12003 BLOCK
12005 should be equivalent to:
12008 auto &&__range = RANGE_EXPR;
12009 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12010 __begin != __end;
12011 ++__begin)
12013 RANGE_DECL = *__begin;
12014 BLOCK
12018 If RANGE_EXPR is an array:
12019 BEGIN_EXPR = __range
12020 END_EXPR = __range + ARRAY_SIZE(__range)
12021 Else if RANGE_EXPR has a member 'begin' or 'end':
12022 BEGIN_EXPR = __range.begin()
12023 END_EXPR = __range.end()
12024 Else:
12025 BEGIN_EXPR = begin(__range)
12026 END_EXPR = end(__range);
12028 If __range has a member 'begin' but not 'end', or vice versa, we must
12029 still use the second alternative (it will surely fail, however).
12030 When calling begin()/end() in the third alternative we must use
12031 argument dependent lookup, but always considering 'std' as an associated
12032 namespace. */
12034 tree
12035 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12036 tree decomp_first_name, unsigned int decomp_cnt,
12037 bool ivdep, unsigned short unroll)
12039 tree begin, end;
12040 tree iter_type, begin_expr, end_expr;
12041 tree condition, expression;
12043 range_expr = mark_lvalue_use (range_expr);
12045 if (range_decl == error_mark_node || range_expr == error_mark_node)
12046 /* If an error happened previously do nothing or else a lot of
12047 unhelpful errors would be issued. */
12048 begin_expr = end_expr = iter_type = error_mark_node;
12049 else
12051 tree range_temp;
12053 if (VAR_P (range_expr)
12054 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12055 /* Can't bind a reference to an array of runtime bound. */
12056 range_temp = range_expr;
12057 else
12059 range_temp = build_range_temp (range_expr);
12060 pushdecl (range_temp);
12061 cp_finish_decl (range_temp, range_expr,
12062 /*is_constant_init*/false, NULL_TREE,
12063 LOOKUP_ONLYCONVERTING);
12064 range_temp = convert_from_reference (range_temp);
12066 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12067 &begin_expr, &end_expr);
12070 /* The new for initialization statement. */
12071 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12072 iter_type);
12073 TREE_USED (begin) = 1;
12074 DECL_ARTIFICIAL (begin) = 1;
12075 pushdecl (begin);
12076 cp_finish_decl (begin, begin_expr,
12077 /*is_constant_init*/false, NULL_TREE,
12078 LOOKUP_ONLYCONVERTING);
12080 if (cxx_dialect >= cxx17)
12081 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12082 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12083 TREE_USED (end) = 1;
12084 DECL_ARTIFICIAL (end) = 1;
12085 pushdecl (end);
12086 cp_finish_decl (end, end_expr,
12087 /*is_constant_init*/false, NULL_TREE,
12088 LOOKUP_ONLYCONVERTING);
12090 finish_init_stmt (statement);
12092 /* The new for condition. */
12093 condition = build_x_binary_op (input_location, NE_EXPR,
12094 begin, ERROR_MARK,
12095 end, ERROR_MARK,
12096 NULL, tf_warning_or_error);
12097 finish_for_cond (condition, statement, ivdep, unroll);
12099 /* The new increment expression. */
12100 expression = finish_unary_op_expr (input_location,
12101 PREINCREMENT_EXPR, begin,
12102 tf_warning_or_error);
12103 finish_for_expr (expression, statement);
12105 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12106 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12108 /* The declaration is initialized with *__begin inside the loop body. */
12109 cp_finish_decl (range_decl,
12110 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12111 tf_warning_or_error),
12112 /*is_constant_init*/false, NULL_TREE,
12113 LOOKUP_ONLYCONVERTING);
12114 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12115 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12117 return statement;
12120 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12121 We need to solve both at the same time because the method used
12122 depends on the existence of members begin or end.
12123 Returns the type deduced for the iterator expression. */
12125 static tree
12126 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12128 if (error_operand_p (range))
12130 *begin = *end = error_mark_node;
12131 return error_mark_node;
12134 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12136 error ("range-based %<for%> expression of type %qT "
12137 "has incomplete type", TREE_TYPE (range));
12138 *begin = *end = error_mark_node;
12139 return error_mark_node;
12141 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12143 /* If RANGE is an array, we will use pointer arithmetic. */
12144 *begin = decay_conversion (range, tf_warning_or_error);
12145 *end = build_binary_op (input_location, PLUS_EXPR,
12146 range,
12147 array_type_nelts_top (TREE_TYPE (range)),
12148 false);
12149 return TREE_TYPE (*begin);
12151 else
12153 /* If it is not an array, we must do a bit of magic. */
12154 tree id_begin, id_end;
12155 tree member_begin, member_end;
12157 *begin = *end = error_mark_node;
12159 id_begin = get_identifier ("begin");
12160 id_end = get_identifier ("end");
12161 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12162 /*protect=*/2, /*want_type=*/false,
12163 tf_warning_or_error);
12164 member_end = lookup_member (TREE_TYPE (range), id_end,
12165 /*protect=*/2, /*want_type=*/false,
12166 tf_warning_or_error);
12168 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12170 /* Use the member functions. */
12171 *begin = cp_parser_range_for_member_function (range, id_begin);
12172 *end = cp_parser_range_for_member_function (range, id_end);
12174 else
12176 /* Use global functions with ADL. */
12177 vec<tree, va_gc> *vec;
12178 vec = make_tree_vector ();
12180 vec_safe_push (vec, range);
12182 member_begin = perform_koenig_lookup (id_begin, vec,
12183 tf_warning_or_error);
12184 *begin = finish_call_expr (member_begin, &vec, false, true,
12185 tf_warning_or_error);
12186 member_end = perform_koenig_lookup (id_end, vec,
12187 tf_warning_or_error);
12188 *end = finish_call_expr (member_end, &vec, false, true,
12189 tf_warning_or_error);
12191 release_tree_vector (vec);
12194 /* Last common checks. */
12195 if (*begin == error_mark_node || *end == error_mark_node)
12197 /* If one of the expressions is an error do no more checks. */
12198 *begin = *end = error_mark_node;
12199 return error_mark_node;
12201 else if (type_dependent_expression_p (*begin)
12202 || type_dependent_expression_p (*end))
12203 /* Can happen, when, eg, in a template context, Koenig lookup
12204 can't resolve begin/end (c++/58503). */
12205 return NULL_TREE;
12206 else
12208 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12209 /* The unqualified type of the __begin and __end temporaries should
12210 be the same, as required by the multiple auto declaration. */
12211 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12213 if (cxx_dialect >= cxx17
12214 && (build_x_binary_op (input_location, NE_EXPR,
12215 *begin, ERROR_MARK,
12216 *end, ERROR_MARK,
12217 NULL, tf_none)
12218 != error_mark_node))
12219 /* P0184R0 allows __begin and __end to have different types,
12220 but make sure they are comparable so we can give a better
12221 diagnostic. */;
12222 else
12223 error ("inconsistent begin/end types in range-based %<for%> "
12224 "statement: %qT and %qT",
12225 TREE_TYPE (*begin), TREE_TYPE (*end));
12227 return iter_type;
12232 /* Helper function for cp_parser_perform_range_for_lookup.
12233 Builds a tree for RANGE.IDENTIFIER(). */
12235 static tree
12236 cp_parser_range_for_member_function (tree range, tree identifier)
12238 tree member, res;
12239 vec<tree, va_gc> *vec;
12241 member = finish_class_member_access_expr (range, identifier,
12242 false, tf_warning_or_error);
12243 if (member == error_mark_node)
12244 return error_mark_node;
12246 vec = make_tree_vector ();
12247 res = finish_call_expr (member, &vec,
12248 /*disallow_virtual=*/false,
12249 /*koenig_p=*/false,
12250 tf_warning_or_error);
12251 release_tree_vector (vec);
12252 return res;
12255 /* Parse an iteration-statement.
12257 iteration-statement:
12258 while ( condition ) statement
12259 do statement while ( expression ) ;
12260 for ( init-statement condition [opt] ; expression [opt] )
12261 statement
12263 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12265 static tree
12266 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12267 unsigned short unroll)
12269 cp_token *token;
12270 enum rid keyword;
12271 tree statement;
12272 unsigned char in_statement;
12273 token_indent_info guard_tinfo;
12275 /* Peek at the next token. */
12276 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12277 if (!token)
12278 return error_mark_node;
12280 guard_tinfo = get_token_indent_info (token);
12282 /* Remember whether or not we are already within an iteration
12283 statement. */
12284 in_statement = parser->in_statement;
12286 /* See what kind of keyword it is. */
12287 keyword = token->keyword;
12288 switch (keyword)
12290 case RID_WHILE:
12292 tree condition;
12294 /* Begin the while-statement. */
12295 statement = begin_while_stmt ();
12296 /* Look for the `('. */
12297 matching_parens parens;
12298 parens.require_open (parser);
12299 /* Parse the condition. */
12300 condition = cp_parser_condition (parser);
12301 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12302 /* Look for the `)'. */
12303 parens.require_close (parser);
12304 /* Parse the dependent statement. */
12305 parser->in_statement = IN_ITERATION_STMT;
12306 bool prev = note_iteration_stmt_body_start ();
12307 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12308 note_iteration_stmt_body_end (prev);
12309 parser->in_statement = in_statement;
12310 /* We're done with the while-statement. */
12311 finish_while_stmt (statement);
12313 break;
12315 case RID_DO:
12317 tree expression;
12319 /* Begin the do-statement. */
12320 statement = begin_do_stmt ();
12321 /* Parse the body of the do-statement. */
12322 parser->in_statement = IN_ITERATION_STMT;
12323 bool prev = note_iteration_stmt_body_start ();
12324 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12325 note_iteration_stmt_body_end (prev);
12326 parser->in_statement = in_statement;
12327 finish_do_body (statement);
12328 /* Look for the `while' keyword. */
12329 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12330 /* Look for the `('. */
12331 matching_parens parens;
12332 parens.require_open (parser);
12333 /* Parse the expression. */
12334 expression = cp_parser_expression (parser);
12335 /* We're done with the do-statement. */
12336 finish_do_stmt (expression, statement, ivdep, unroll);
12337 /* Look for the `)'. */
12338 parens.require_close (parser);
12339 /* Look for the `;'. */
12340 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12342 break;
12344 case RID_FOR:
12346 /* Look for the `('. */
12347 matching_parens parens;
12348 parens.require_open (parser);
12350 statement = cp_parser_for (parser, ivdep, unroll);
12352 /* Look for the `)'. */
12353 parens.require_close (parser);
12355 /* Parse the body of the for-statement. */
12356 parser->in_statement = IN_ITERATION_STMT;
12357 bool prev = note_iteration_stmt_body_start ();
12358 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12359 note_iteration_stmt_body_end (prev);
12360 parser->in_statement = in_statement;
12362 /* We're done with the for-statement. */
12363 finish_for_stmt (statement);
12365 break;
12367 default:
12368 cp_parser_error (parser, "expected iteration-statement");
12369 statement = error_mark_node;
12370 break;
12373 return statement;
12376 /* Parse a init-statement or the declarator of a range-based-for.
12377 Returns true if a range-based-for declaration is seen.
12379 init-statement:
12380 expression-statement
12381 simple-declaration */
12383 static bool
12384 cp_parser_init_statement (cp_parser *parser, tree *decl)
12386 /* If the next token is a `;', then we have an empty
12387 expression-statement. Grammatically, this is also a
12388 simple-declaration, but an invalid one, because it does not
12389 declare anything. Therefore, if we did not handle this case
12390 specially, we would issue an error message about an invalid
12391 declaration. */
12392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12394 bool is_range_for = false;
12395 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12397 /* Try to parse the init-statement. */
12398 if (cp_parser_range_based_for_with_init_p (parser))
12400 tree dummy;
12401 cp_parser_parse_tentatively (parser);
12402 /* Parse the declaration. */
12403 cp_parser_simple_declaration (parser,
12404 /*function_definition_allowed_p=*/false,
12405 &dummy);
12406 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12407 if (!cp_parser_parse_definitely (parser))
12408 /* That didn't work, try to parse it as an expression-statement. */
12409 cp_parser_expression_statement (parser, NULL_TREE);
12411 if (cxx_dialect < cxx2a)
12413 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12414 "range-based %<for%> loops with initializer only "
12415 "available with -std=c++2a or -std=gnu++2a");
12416 *decl = error_mark_node;
12420 /* A colon is used in range-based for. */
12421 parser->colon_corrects_to_scope_p = false;
12423 /* We're going to speculatively look for a declaration, falling back
12424 to an expression, if necessary. */
12425 cp_parser_parse_tentatively (parser);
12426 /* Parse the declaration. */
12427 cp_parser_simple_declaration (parser,
12428 /*function_definition_allowed_p=*/false,
12429 decl);
12430 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12431 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12433 /* It is a range-for, consume the ':'. */
12434 cp_lexer_consume_token (parser->lexer);
12435 is_range_for = true;
12436 if (cxx_dialect < cxx11)
12437 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12438 "range-based %<for%> loops only available with "
12439 "-std=c++11 or -std=gnu++11");
12441 else
12442 /* The ';' is not consumed yet because we told
12443 cp_parser_simple_declaration not to. */
12444 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12446 if (cp_parser_parse_definitely (parser))
12447 return is_range_for;
12448 /* If the tentative parse failed, then we shall need to look for an
12449 expression-statement. */
12451 /* If we are here, it is an expression-statement. */
12452 cp_parser_expression_statement (parser, NULL_TREE);
12453 return false;
12456 /* Parse a jump-statement.
12458 jump-statement:
12459 break ;
12460 continue ;
12461 return expression [opt] ;
12462 return braced-init-list ;
12463 goto identifier ;
12465 GNU extension:
12467 jump-statement:
12468 goto * expression ;
12470 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12472 static tree
12473 cp_parser_jump_statement (cp_parser* parser)
12475 tree statement = error_mark_node;
12476 cp_token *token;
12477 enum rid keyword;
12478 unsigned char in_statement;
12480 /* Peek at the next token. */
12481 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12482 if (!token)
12483 return error_mark_node;
12485 /* See what kind of keyword it is. */
12486 keyword = token->keyword;
12487 switch (keyword)
12489 case RID_BREAK:
12490 in_statement = parser->in_statement & ~IN_IF_STMT;
12491 switch (in_statement)
12493 case 0:
12494 error_at (token->location, "break statement not within loop or switch");
12495 break;
12496 default:
12497 gcc_assert ((in_statement & IN_SWITCH_STMT)
12498 || in_statement == IN_ITERATION_STMT);
12499 statement = finish_break_stmt ();
12500 if (in_statement == IN_ITERATION_STMT)
12501 break_maybe_infinite_loop ();
12502 break;
12503 case IN_OMP_BLOCK:
12504 error_at (token->location, "invalid exit from OpenMP structured block");
12505 break;
12506 case IN_OMP_FOR:
12507 error_at (token->location, "break statement used with OpenMP for loop");
12508 break;
12510 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12511 break;
12513 case RID_CONTINUE:
12514 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12516 case 0:
12517 error_at (token->location, "continue statement not within a loop");
12518 break;
12519 /* Fall through. */
12520 case IN_ITERATION_STMT:
12521 case IN_OMP_FOR:
12522 statement = finish_continue_stmt ();
12523 break;
12524 case IN_OMP_BLOCK:
12525 error_at (token->location, "invalid exit from OpenMP structured block");
12526 break;
12527 default:
12528 gcc_unreachable ();
12530 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12531 break;
12533 case RID_RETURN:
12535 tree expr;
12536 bool expr_non_constant_p;
12538 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12540 cp_lexer_set_source_position (parser->lexer);
12541 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12542 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12544 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12545 expr = cp_parser_expression (parser);
12546 else
12547 /* If the next token is a `;', then there is no
12548 expression. */
12549 expr = NULL_TREE;
12550 /* Build the return-statement. */
12551 if (current_function_auto_return_pattern && in_discarded_stmt)
12552 /* Don't deduce from a discarded return statement. */;
12553 else
12554 statement = finish_return_stmt (expr);
12555 /* Look for the final `;'. */
12556 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12558 break;
12560 case RID_GOTO:
12561 if (parser->in_function_body
12562 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12564 error ("%<goto%> in %<constexpr%> function");
12565 cp_function_chain->invalid_constexpr = true;
12568 /* Create the goto-statement. */
12569 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12571 /* Issue a warning about this use of a GNU extension. */
12572 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12573 /* Consume the '*' token. */
12574 cp_lexer_consume_token (parser->lexer);
12575 /* Parse the dependent expression. */
12576 finish_goto_stmt (cp_parser_expression (parser));
12578 else
12579 finish_goto_stmt (cp_parser_identifier (parser));
12580 /* Look for the final `;'. */
12581 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12582 break;
12584 default:
12585 cp_parser_error (parser, "expected jump-statement");
12586 break;
12589 return statement;
12592 /* Parse a declaration-statement.
12594 declaration-statement:
12595 block-declaration */
12597 static void
12598 cp_parser_declaration_statement (cp_parser* parser)
12600 void *p;
12602 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12603 p = obstack_alloc (&declarator_obstack, 0);
12605 /* Parse the block-declaration. */
12606 cp_parser_block_declaration (parser, /*statement_p=*/true);
12608 /* Free any declarators allocated. */
12609 obstack_free (&declarator_obstack, p);
12612 /* Some dependent statements (like `if (cond) statement'), are
12613 implicitly in their own scope. In other words, if the statement is
12614 a single statement (as opposed to a compound-statement), it is
12615 none-the-less treated as if it were enclosed in braces. Any
12616 declarations appearing in the dependent statement are out of scope
12617 after control passes that point. This function parses a statement,
12618 but ensures that is in its own scope, even if it is not a
12619 compound-statement.
12621 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12622 is a (possibly labeled) if statement which is not enclosed in
12623 braces and has an else clause. This is used to implement
12624 -Wparentheses.
12626 CHAIN is a vector of if-else-if conditions. This is used to implement
12627 -Wduplicated-cond.
12629 Returns the new statement. */
12631 static tree
12632 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12633 const token_indent_info &guard_tinfo,
12634 vec<tree> *chain)
12636 tree statement;
12637 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12638 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12639 token_indent_info body_tinfo
12640 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12642 if (if_p != NULL)
12643 *if_p = false;
12645 /* Mark if () ; with a special NOP_EXPR. */
12646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12648 cp_lexer_consume_token (parser->lexer);
12649 statement = add_stmt (build_empty_stmt (body_loc));
12651 if (guard_tinfo.keyword == RID_IF
12652 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12653 warning_at (body_loc, OPT_Wempty_body,
12654 "suggest braces around empty body in an %<if%> statement");
12655 else if (guard_tinfo.keyword == RID_ELSE)
12656 warning_at (body_loc, OPT_Wempty_body,
12657 "suggest braces around empty body in an %<else%> statement");
12659 /* if a compound is opened, we simply parse the statement directly. */
12660 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12661 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12662 /* If the token is not a `{', then we must take special action. */
12663 else
12665 /* Create a compound-statement. */
12666 statement = begin_compound_stmt (0);
12667 /* Parse the dependent-statement. */
12668 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12669 &body_loc_after_labels);
12670 /* Finish the dummy compound-statement. */
12671 finish_compound_stmt (statement);
12674 token_indent_info next_tinfo
12675 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12676 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12678 if (body_loc_after_labels != UNKNOWN_LOCATION
12679 && next_tinfo.type != CPP_SEMICOLON)
12680 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12681 guard_tinfo.location, guard_tinfo.keyword);
12683 /* Return the statement. */
12684 return statement;
12687 /* For some dependent statements (like `while (cond) statement'), we
12688 have already created a scope. Therefore, even if the dependent
12689 statement is a compound-statement, we do not want to create another
12690 scope. */
12692 static void
12693 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12694 const token_indent_info &guard_tinfo)
12696 /* If the token is a `{', then we must take special action. */
12697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12699 token_indent_info body_tinfo
12700 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12701 location_t loc_after_labels = UNKNOWN_LOCATION;
12703 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12704 &loc_after_labels);
12705 token_indent_info next_tinfo
12706 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12707 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12709 if (loc_after_labels != UNKNOWN_LOCATION
12710 && next_tinfo.type != CPP_SEMICOLON)
12711 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12712 guard_tinfo.location,
12713 guard_tinfo.keyword);
12715 else
12717 /* Avoid calling cp_parser_compound_statement, so that we
12718 don't create a new scope. Do everything else by hand. */
12719 matching_braces braces;
12720 braces.require_open (parser);
12721 /* If the next keyword is `__label__' we have a label declaration. */
12722 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12723 cp_parser_label_declaration (parser);
12724 /* Parse an (optional) statement-seq. */
12725 cp_parser_statement_seq_opt (parser, NULL_TREE);
12726 braces.require_close (parser);
12730 /* Declarations [gram.dcl.dcl] */
12732 /* Parse an optional declaration-sequence.
12734 declaration-seq:
12735 declaration
12736 declaration-seq declaration */
12738 static void
12739 cp_parser_declaration_seq_opt (cp_parser* parser)
12741 while (true)
12743 cp_token *token;
12745 token = cp_lexer_peek_token (parser->lexer);
12747 if (token->type == CPP_CLOSE_BRACE
12748 || token->type == CPP_EOF
12749 || token->type == CPP_PRAGMA_EOL)
12750 break;
12752 if (token->type == CPP_SEMICOLON)
12754 /* A declaration consisting of a single semicolon is
12755 invalid. Allow it unless we're being pedantic. */
12756 cp_lexer_consume_token (parser->lexer);
12757 if (!in_system_header_at (input_location))
12758 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12759 continue;
12762 /* If we're entering or exiting a region that's implicitly
12763 extern "C", modify the lang context appropriately. */
12764 if (!parser->implicit_extern_c && token->implicit_extern_c)
12766 push_lang_context (lang_name_c);
12767 parser->implicit_extern_c = true;
12769 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12771 pop_lang_context ();
12772 parser->implicit_extern_c = false;
12775 if (token->type == CPP_PRAGMA)
12777 /* A top-level declaration can consist solely of a #pragma.
12778 A nested declaration cannot, so this is done here and not
12779 in cp_parser_declaration. (A #pragma at block scope is
12780 handled in cp_parser_statement.) */
12781 cp_parser_pragma (parser, pragma_external, NULL);
12782 continue;
12785 /* Parse the declaration itself. */
12786 cp_parser_declaration (parser);
12790 /* Parse a declaration.
12792 declaration:
12793 block-declaration
12794 function-definition
12795 template-declaration
12796 explicit-instantiation
12797 explicit-specialization
12798 linkage-specification
12799 namespace-definition
12801 C++17:
12802 deduction-guide
12804 GNU extension:
12806 declaration:
12807 __extension__ declaration */
12809 static void
12810 cp_parser_declaration (cp_parser* parser)
12812 cp_token token1;
12813 cp_token token2;
12814 int saved_pedantic;
12815 void *p;
12816 tree attributes = NULL_TREE;
12818 /* Check for the `__extension__' keyword. */
12819 if (cp_parser_extension_opt (parser, &saved_pedantic))
12821 /* Parse the qualified declaration. */
12822 cp_parser_declaration (parser);
12823 /* Restore the PEDANTIC flag. */
12824 pedantic = saved_pedantic;
12826 return;
12829 /* Try to figure out what kind of declaration is present. */
12830 token1 = *cp_lexer_peek_token (parser->lexer);
12832 if (token1.type != CPP_EOF)
12833 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12834 else
12836 token2.type = CPP_EOF;
12837 token2.keyword = RID_MAX;
12840 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12841 p = obstack_alloc (&declarator_obstack, 0);
12843 /* If the next token is `extern' and the following token is a string
12844 literal, then we have a linkage specification. */
12845 if (token1.keyword == RID_EXTERN
12846 && cp_parser_is_pure_string_literal (&token2))
12847 cp_parser_linkage_specification (parser);
12848 /* If the next token is `template', then we have either a template
12849 declaration, an explicit instantiation, or an explicit
12850 specialization. */
12851 else if (token1.keyword == RID_TEMPLATE)
12853 /* `template <>' indicates a template specialization. */
12854 if (token2.type == CPP_LESS
12855 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12856 cp_parser_explicit_specialization (parser);
12857 /* `template <' indicates a template declaration. */
12858 else if (token2.type == CPP_LESS)
12859 cp_parser_template_declaration (parser, /*member_p=*/false);
12860 /* Anything else must be an explicit instantiation. */
12861 else
12862 cp_parser_explicit_instantiation (parser);
12864 /* If the next token is `export', then we have a template
12865 declaration. */
12866 else if (token1.keyword == RID_EXPORT)
12867 cp_parser_template_declaration (parser, /*member_p=*/false);
12868 /* If the next token is `extern', 'static' or 'inline' and the one
12869 after that is `template', we have a GNU extended explicit
12870 instantiation directive. */
12871 else if (cp_parser_allow_gnu_extensions_p (parser)
12872 && (token1.keyword == RID_EXTERN
12873 || token1.keyword == RID_STATIC
12874 || token1.keyword == RID_INLINE)
12875 && token2.keyword == RID_TEMPLATE)
12876 cp_parser_explicit_instantiation (parser);
12877 /* If the next token is `namespace', check for a named or unnamed
12878 namespace definition. */
12879 else if (token1.keyword == RID_NAMESPACE
12880 && (/* A named namespace definition. */
12881 (token2.type == CPP_NAME
12882 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12883 != CPP_EQ))
12884 || (token2.type == CPP_OPEN_SQUARE
12885 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12886 == CPP_OPEN_SQUARE)
12887 /* An unnamed namespace definition. */
12888 || token2.type == CPP_OPEN_BRACE
12889 || token2.keyword == RID_ATTRIBUTE))
12890 cp_parser_namespace_definition (parser);
12891 /* An inline (associated) namespace definition. */
12892 else if (token1.keyword == RID_INLINE
12893 && token2.keyword == RID_NAMESPACE)
12894 cp_parser_namespace_definition (parser);
12895 /* Objective-C++ declaration/definition. */
12896 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12897 cp_parser_objc_declaration (parser, NULL_TREE);
12898 else if (c_dialect_objc ()
12899 && token1.keyword == RID_ATTRIBUTE
12900 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12901 cp_parser_objc_declaration (parser, attributes);
12902 /* At this point we may have a template declared by a concept
12903 introduction. */
12904 else if (flag_concepts
12905 && cp_parser_template_declaration_after_export (parser,
12906 /*member_p=*/false))
12907 /* We did. */;
12908 else
12909 /* Try to parse a block-declaration, or a function-definition. */
12910 cp_parser_block_declaration (parser, /*statement_p=*/false);
12912 /* Free any declarators allocated. */
12913 obstack_free (&declarator_obstack, p);
12916 /* Parse a block-declaration.
12918 block-declaration:
12919 simple-declaration
12920 asm-definition
12921 namespace-alias-definition
12922 using-declaration
12923 using-directive
12925 GNU Extension:
12927 block-declaration:
12928 __extension__ block-declaration
12930 C++0x Extension:
12932 block-declaration:
12933 static_assert-declaration
12935 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12936 part of a declaration-statement. */
12938 static void
12939 cp_parser_block_declaration (cp_parser *parser,
12940 bool statement_p)
12942 cp_token *token1;
12943 int saved_pedantic;
12945 /* Check for the `__extension__' keyword. */
12946 if (cp_parser_extension_opt (parser, &saved_pedantic))
12948 /* Parse the qualified declaration. */
12949 cp_parser_block_declaration (parser, statement_p);
12950 /* Restore the PEDANTIC flag. */
12951 pedantic = saved_pedantic;
12953 return;
12956 /* Peek at the next token to figure out which kind of declaration is
12957 present. */
12958 token1 = cp_lexer_peek_token (parser->lexer);
12960 /* If the next keyword is `asm', we have an asm-definition. */
12961 if (token1->keyword == RID_ASM)
12963 if (statement_p)
12964 cp_parser_commit_to_tentative_parse (parser);
12965 cp_parser_asm_definition (parser);
12967 /* If the next keyword is `namespace', we have a
12968 namespace-alias-definition. */
12969 else if (token1->keyword == RID_NAMESPACE)
12970 cp_parser_namespace_alias_definition (parser);
12971 /* If the next keyword is `using', we have a
12972 using-declaration, a using-directive, or an alias-declaration. */
12973 else if (token1->keyword == RID_USING)
12975 cp_token *token2;
12977 if (statement_p)
12978 cp_parser_commit_to_tentative_parse (parser);
12979 /* If the token after `using' is `namespace', then we have a
12980 using-directive. */
12981 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12982 if (token2->keyword == RID_NAMESPACE)
12983 cp_parser_using_directive (parser);
12984 /* If the second token after 'using' is '=', then we have an
12985 alias-declaration. */
12986 else if (cxx_dialect >= cxx11
12987 && token2->type == CPP_NAME
12988 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12989 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12990 cp_parser_alias_declaration (parser);
12991 /* Otherwise, it's a using-declaration. */
12992 else
12993 cp_parser_using_declaration (parser,
12994 /*access_declaration_p=*/false);
12996 /* If the next keyword is `__label__' we have a misplaced label
12997 declaration. */
12998 else if (token1->keyword == RID_LABEL)
13000 cp_lexer_consume_token (parser->lexer);
13001 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13002 cp_parser_skip_to_end_of_statement (parser);
13003 /* If the next token is now a `;', consume it. */
13004 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13005 cp_lexer_consume_token (parser->lexer);
13007 /* If the next token is `static_assert' we have a static assertion. */
13008 else if (token1->keyword == RID_STATIC_ASSERT)
13009 cp_parser_static_assert (parser, /*member_p=*/false);
13010 /* Anything else must be a simple-declaration. */
13011 else
13012 cp_parser_simple_declaration (parser, !statement_p,
13013 /*maybe_range_for_decl*/NULL);
13016 /* Parse a simple-declaration.
13018 simple-declaration:
13019 decl-specifier-seq [opt] init-declarator-list [opt] ;
13020 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13021 brace-or-equal-initializer ;
13023 init-declarator-list:
13024 init-declarator
13025 init-declarator-list , init-declarator
13027 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13028 function-definition as a simple-declaration.
13030 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13031 parsed declaration if it is an uninitialized single declarator not followed
13032 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13033 if present, will not be consumed. */
13035 static void
13036 cp_parser_simple_declaration (cp_parser* parser,
13037 bool function_definition_allowed_p,
13038 tree *maybe_range_for_decl)
13040 cp_decl_specifier_seq decl_specifiers;
13041 int declares_class_or_enum;
13042 bool saw_declarator;
13043 location_t comma_loc = UNKNOWN_LOCATION;
13044 location_t init_loc = UNKNOWN_LOCATION;
13046 if (maybe_range_for_decl)
13047 *maybe_range_for_decl = NULL_TREE;
13049 /* Defer access checks until we know what is being declared; the
13050 checks for names appearing in the decl-specifier-seq should be
13051 done as if we were in the scope of the thing being declared. */
13052 push_deferring_access_checks (dk_deferred);
13054 /* Parse the decl-specifier-seq. We have to keep track of whether
13055 or not the decl-specifier-seq declares a named class or
13056 enumeration type, since that is the only case in which the
13057 init-declarator-list is allowed to be empty.
13059 [dcl.dcl]
13061 In a simple-declaration, the optional init-declarator-list can be
13062 omitted only when declaring a class or enumeration, that is when
13063 the decl-specifier-seq contains either a class-specifier, an
13064 elaborated-type-specifier, or an enum-specifier. */
13065 cp_parser_decl_specifier_seq (parser,
13066 CP_PARSER_FLAGS_OPTIONAL,
13067 &decl_specifiers,
13068 &declares_class_or_enum);
13069 /* We no longer need to defer access checks. */
13070 stop_deferring_access_checks ();
13072 /* In a block scope, a valid declaration must always have a
13073 decl-specifier-seq. By not trying to parse declarators, we can
13074 resolve the declaration/expression ambiguity more quickly. */
13075 if (!function_definition_allowed_p
13076 && !decl_specifiers.any_specifiers_p)
13078 cp_parser_error (parser, "expected declaration");
13079 goto done;
13082 /* If the next two tokens are both identifiers, the code is
13083 erroneous. The usual cause of this situation is code like:
13085 T t;
13087 where "T" should name a type -- but does not. */
13088 if (!decl_specifiers.any_type_specifiers_p
13089 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13091 /* If parsing tentatively, we should commit; we really are
13092 looking at a declaration. */
13093 cp_parser_commit_to_tentative_parse (parser);
13094 /* Give up. */
13095 goto done;
13098 cp_parser_maybe_commit_to_declaration (parser,
13099 decl_specifiers.any_specifiers_p);
13101 /* Look for C++17 decomposition declaration. */
13102 for (size_t n = 1; ; n++)
13103 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13104 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13105 continue;
13106 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13107 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13108 && decl_specifiers.any_specifiers_p)
13110 tree decl
13111 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13112 maybe_range_for_decl,
13113 &init_loc);
13115 /* The next token should be either a `,' or a `;'. */
13116 cp_token *token = cp_lexer_peek_token (parser->lexer);
13117 /* If it's a `;', we are done. */
13118 if (token->type == CPP_SEMICOLON)
13119 goto finish;
13120 else if (maybe_range_for_decl)
13122 if (*maybe_range_for_decl == NULL_TREE)
13123 *maybe_range_for_decl = error_mark_node;
13124 goto finish;
13126 /* Anything else is an error. */
13127 else
13129 /* If we have already issued an error message we don't need
13130 to issue another one. */
13131 if ((decl != error_mark_node
13132 && DECL_INITIAL (decl) != error_mark_node)
13133 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13134 cp_parser_error (parser, "expected %<,%> or %<;%>");
13135 /* Skip tokens until we reach the end of the statement. */
13136 cp_parser_skip_to_end_of_statement (parser);
13137 /* If the next token is now a `;', consume it. */
13138 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13139 cp_lexer_consume_token (parser->lexer);
13140 goto done;
13143 else
13144 break;
13146 tree last_type;
13147 bool auto_specifier_p;
13148 /* NULL_TREE if both variable and function declaration are allowed,
13149 error_mark_node if function declaration are not allowed and
13150 a FUNCTION_DECL that should be diagnosed if it is followed by
13151 variable declarations. */
13152 tree auto_function_declaration;
13154 last_type = NULL_TREE;
13155 auto_specifier_p
13156 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13157 auto_function_declaration = NULL_TREE;
13159 /* Keep going until we hit the `;' at the end of the simple
13160 declaration. */
13161 saw_declarator = false;
13162 while (cp_lexer_next_token_is_not (parser->lexer,
13163 CPP_SEMICOLON))
13165 cp_token *token;
13166 bool function_definition_p;
13167 tree decl;
13168 tree auto_result = NULL_TREE;
13170 if (saw_declarator)
13172 /* If we are processing next declarator, comma is expected */
13173 token = cp_lexer_peek_token (parser->lexer);
13174 gcc_assert (token->type == CPP_COMMA);
13175 cp_lexer_consume_token (parser->lexer);
13176 if (maybe_range_for_decl)
13178 *maybe_range_for_decl = error_mark_node;
13179 if (comma_loc == UNKNOWN_LOCATION)
13180 comma_loc = token->location;
13183 else
13184 saw_declarator = true;
13186 /* Parse the init-declarator. */
13187 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13188 /*checks=*/NULL,
13189 function_definition_allowed_p,
13190 /*member_p=*/false,
13191 declares_class_or_enum,
13192 &function_definition_p,
13193 maybe_range_for_decl,
13194 &init_loc,
13195 &auto_result);
13196 /* If an error occurred while parsing tentatively, exit quickly.
13197 (That usually happens when in the body of a function; each
13198 statement is treated as a declaration-statement until proven
13199 otherwise.) */
13200 if (cp_parser_error_occurred (parser))
13201 goto done;
13203 if (auto_specifier_p && cxx_dialect >= cxx14)
13205 /* If the init-declarator-list contains more than one
13206 init-declarator, they shall all form declarations of
13207 variables. */
13208 if (auto_function_declaration == NULL_TREE)
13209 auto_function_declaration
13210 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13211 else if (TREE_CODE (decl) == FUNCTION_DECL
13212 || auto_function_declaration != error_mark_node)
13214 error_at (decl_specifiers.locations[ds_type_spec],
13215 "non-variable %qD in declaration with more than one "
13216 "declarator with placeholder type",
13217 TREE_CODE (decl) == FUNCTION_DECL
13218 ? decl : auto_function_declaration);
13219 auto_function_declaration = error_mark_node;
13223 if (auto_result
13224 && (!processing_template_decl || !type_uses_auto (auto_result)))
13226 if (last_type
13227 && last_type != error_mark_node
13228 && !same_type_p (auto_result, last_type))
13230 /* If the list of declarators contains more than one declarator,
13231 the type of each declared variable is determined as described
13232 above. If the type deduced for the template parameter U is not
13233 the same in each deduction, the program is ill-formed. */
13234 error_at (decl_specifiers.locations[ds_type_spec],
13235 "inconsistent deduction for %qT: %qT and then %qT",
13236 decl_specifiers.type, last_type, auto_result);
13237 last_type = error_mark_node;
13239 else
13240 last_type = auto_result;
13243 /* Handle function definitions specially. */
13244 if (function_definition_p)
13246 /* If the next token is a `,', then we are probably
13247 processing something like:
13249 void f() {}, *p;
13251 which is erroneous. */
13252 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13254 cp_token *token = cp_lexer_peek_token (parser->lexer);
13255 error_at (token->location,
13256 "mixing"
13257 " declarations and function-definitions is forbidden");
13259 /* Otherwise, we're done with the list of declarators. */
13260 else
13262 pop_deferring_access_checks ();
13263 return;
13266 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13267 *maybe_range_for_decl = decl;
13268 /* The next token should be either a `,' or a `;'. */
13269 token = cp_lexer_peek_token (parser->lexer);
13270 /* If it's a `,', there are more declarators to come. */
13271 if (token->type == CPP_COMMA)
13272 /* will be consumed next time around */;
13273 /* If it's a `;', we are done. */
13274 else if (token->type == CPP_SEMICOLON)
13275 break;
13276 else if (maybe_range_for_decl)
13278 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13279 permerror (decl_specifiers.locations[ds_type_spec],
13280 "types may not be defined in a for-range-declaration");
13281 break;
13283 /* Anything else is an error. */
13284 else
13286 /* If we have already issued an error message we don't need
13287 to issue another one. */
13288 if ((decl != error_mark_node
13289 && DECL_INITIAL (decl) != error_mark_node)
13290 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13291 cp_parser_error (parser, "expected %<,%> or %<;%>");
13292 /* Skip tokens until we reach the end of the statement. */
13293 cp_parser_skip_to_end_of_statement (parser);
13294 /* If the next token is now a `;', consume it. */
13295 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13296 cp_lexer_consume_token (parser->lexer);
13297 goto done;
13299 /* After the first time around, a function-definition is not
13300 allowed -- even if it was OK at first. For example:
13302 int i, f() {}
13304 is not valid. */
13305 function_definition_allowed_p = false;
13308 /* Issue an error message if no declarators are present, and the
13309 decl-specifier-seq does not itself declare a class or
13310 enumeration: [dcl.dcl]/3. */
13311 if (!saw_declarator)
13313 if (cp_parser_declares_only_class_p (parser))
13315 if (!declares_class_or_enum
13316 && decl_specifiers.type
13317 && OVERLOAD_TYPE_P (decl_specifiers.type))
13318 /* Ensure an error is issued anyway when finish_decltype_type,
13319 called via cp_parser_decl_specifier_seq, returns a class or
13320 an enumeration (c++/51786). */
13321 decl_specifiers.type = NULL_TREE;
13322 shadow_tag (&decl_specifiers);
13324 /* Perform any deferred access checks. */
13325 perform_deferred_access_checks (tf_warning_or_error);
13328 /* Consume the `;'. */
13329 finish:
13330 if (!maybe_range_for_decl)
13331 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13332 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13334 if (init_loc != UNKNOWN_LOCATION)
13335 error_at (init_loc, "initializer in range-based %<for%> loop");
13336 if (comma_loc != UNKNOWN_LOCATION)
13337 error_at (comma_loc,
13338 "multiple declarations in range-based %<for%> loop");
13341 done:
13342 pop_deferring_access_checks ();
13345 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13346 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13347 initializer ; */
13349 static tree
13350 cp_parser_decomposition_declaration (cp_parser *parser,
13351 cp_decl_specifier_seq *decl_specifiers,
13352 tree *maybe_range_for_decl,
13353 location_t *init_loc)
13355 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13356 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13357 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13359 /* Parse the identifier-list. */
13360 auto_vec<cp_expr, 10> v;
13361 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13362 while (true)
13364 cp_expr e = cp_parser_identifier (parser);
13365 if (e.get_value () == error_mark_node)
13366 break;
13367 v.safe_push (e);
13368 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13369 break;
13370 cp_lexer_consume_token (parser->lexer);
13373 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13374 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13376 end_loc = UNKNOWN_LOCATION;
13377 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13378 false);
13379 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13380 cp_lexer_consume_token (parser->lexer);
13381 else
13383 cp_parser_skip_to_end_of_statement (parser);
13384 return error_mark_node;
13388 if (cxx_dialect < cxx17)
13389 pedwarn (loc, 0, "structured bindings only available with "
13390 "-std=c++17 or -std=gnu++17");
13392 tree pushed_scope;
13393 cp_declarator *declarator = make_declarator (cdk_decomp);
13394 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13395 declarator->id_loc = loc;
13396 if (ref_qual != REF_QUAL_NONE)
13397 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13398 ref_qual == REF_QUAL_RVALUE,
13399 NULL_TREE);
13400 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13401 NULL_TREE, decl_specifiers->attributes,
13402 &pushed_scope);
13403 tree orig_decl = decl;
13405 unsigned int i;
13406 cp_expr e;
13407 cp_decl_specifier_seq decl_specs;
13408 clear_decl_specs (&decl_specs);
13409 decl_specs.type = make_auto ();
13410 tree prev = decl;
13411 FOR_EACH_VEC_ELT (v, i, e)
13413 if (i == 0)
13414 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13415 else
13416 declarator->u.id.unqualified_name = e.get_value ();
13417 declarator->id_loc = e.get_location ();
13418 tree elt_pushed_scope;
13419 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13420 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13421 if (decl2 == error_mark_node)
13422 decl = error_mark_node;
13423 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13425 /* Ensure we've diagnosed redeclaration if we aren't creating
13426 a new VAR_DECL. */
13427 gcc_assert (errorcount);
13428 decl = error_mark_node;
13430 else
13431 prev = decl2;
13432 if (elt_pushed_scope)
13433 pop_scope (elt_pushed_scope);
13436 if (v.is_empty ())
13438 error_at (loc, "empty structured binding declaration");
13439 decl = error_mark_node;
13442 if (maybe_range_for_decl == NULL
13443 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13445 bool non_constant_p = false, is_direct_init = false;
13446 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13447 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13448 &non_constant_p);
13449 if (initializer == NULL_TREE
13450 || (TREE_CODE (initializer) == TREE_LIST
13451 && TREE_CHAIN (initializer))
13452 || (is_direct_init
13453 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13454 && CONSTRUCTOR_NELTS (initializer) != 1))
13456 error_at (loc, "invalid initializer for structured binding "
13457 "declaration");
13458 initializer = error_mark_node;
13461 if (decl != error_mark_node)
13463 cp_maybe_mangle_decomp (decl, prev, v.length ());
13464 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13465 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13466 cp_finish_decomp (decl, prev, v.length ());
13469 else if (decl != error_mark_node)
13471 *maybe_range_for_decl = prev;
13472 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13473 the underlying DECL. */
13474 cp_finish_decomp (decl, prev, v.length ());
13477 if (pushed_scope)
13478 pop_scope (pushed_scope);
13480 if (decl == error_mark_node && DECL_P (orig_decl))
13482 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13483 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13486 return decl;
13489 /* Parse a decl-specifier-seq.
13491 decl-specifier-seq:
13492 decl-specifier-seq [opt] decl-specifier
13493 decl-specifier attribute-specifier-seq [opt] (C++11)
13495 decl-specifier:
13496 storage-class-specifier
13497 type-specifier
13498 function-specifier
13499 friend
13500 typedef
13502 GNU Extension:
13504 decl-specifier:
13505 attributes
13507 Concepts Extension:
13509 decl-specifier:
13510 concept
13512 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13514 The parser flags FLAGS is used to control type-specifier parsing.
13516 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13517 flags:
13519 1: one of the decl-specifiers is an elaborated-type-specifier
13520 (i.e., a type declaration)
13521 2: one of the decl-specifiers is an enum-specifier or a
13522 class-specifier (i.e., a type definition)
13526 static void
13527 cp_parser_decl_specifier_seq (cp_parser* parser,
13528 cp_parser_flags flags,
13529 cp_decl_specifier_seq *decl_specs,
13530 int* declares_class_or_enum)
13532 bool constructor_possible_p = !parser->in_declarator_p;
13533 bool found_decl_spec = false;
13534 cp_token *start_token = NULL;
13535 cp_decl_spec ds;
13537 /* Clear DECL_SPECS. */
13538 clear_decl_specs (decl_specs);
13540 /* Assume no class or enumeration type is declared. */
13541 *declares_class_or_enum = 0;
13543 /* Keep reading specifiers until there are no more to read. */
13544 while (true)
13546 bool constructor_p;
13547 cp_token *token;
13548 ds = ds_last;
13550 /* Peek at the next token. */
13551 token = cp_lexer_peek_token (parser->lexer);
13553 /* Save the first token of the decl spec list for error
13554 reporting. */
13555 if (!start_token)
13556 start_token = token;
13557 /* Handle attributes. */
13558 if (cp_next_tokens_can_be_attribute_p (parser))
13560 /* Parse the attributes. */
13561 tree attrs = cp_parser_attributes_opt (parser);
13563 /* In a sequence of declaration specifiers, c++11 attributes
13564 appertain to the type that precede them. In that case
13565 [dcl.spec]/1 says:
13567 The attribute-specifier-seq affects the type only for
13568 the declaration it appears in, not other declarations
13569 involving the same type.
13571 But for now let's force the user to position the
13572 attribute either at the beginning of the declaration or
13573 after the declarator-id, which would clearly mean that it
13574 applies to the declarator. */
13575 if (cxx11_attribute_p (attrs))
13577 if (!found_decl_spec)
13578 /* The c++11 attribute is at the beginning of the
13579 declaration. It appertains to the entity being
13580 declared. */;
13581 else
13583 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13585 /* This is an attribute following a
13586 class-specifier. */
13587 if (decl_specs->type_definition_p)
13588 warn_misplaced_attr_for_class_type (token->location,
13589 decl_specs->type);
13590 attrs = NULL_TREE;
13592 else
13594 decl_specs->std_attributes
13595 = attr_chainon (decl_specs->std_attributes, attrs);
13596 if (decl_specs->locations[ds_std_attribute] == 0)
13597 decl_specs->locations[ds_std_attribute] = token->location;
13599 continue;
13603 decl_specs->attributes
13604 = attr_chainon (decl_specs->attributes, attrs);
13605 if (decl_specs->locations[ds_attribute] == 0)
13606 decl_specs->locations[ds_attribute] = token->location;
13607 continue;
13609 /* Assume we will find a decl-specifier keyword. */
13610 found_decl_spec = true;
13611 /* If the next token is an appropriate keyword, we can simply
13612 add it to the list. */
13613 switch (token->keyword)
13615 /* decl-specifier:
13616 friend
13617 constexpr */
13618 case RID_FRIEND:
13619 if (!at_class_scope_p ())
13621 gcc_rich_location richloc (token->location);
13622 richloc.add_fixit_remove ();
13623 error_at (&richloc, "%<friend%> used outside of class");
13624 cp_lexer_purge_token (parser->lexer);
13626 else
13628 ds = ds_friend;
13629 /* Consume the token. */
13630 cp_lexer_consume_token (parser->lexer);
13632 break;
13634 case RID_CONSTEXPR:
13635 ds = ds_constexpr;
13636 cp_lexer_consume_token (parser->lexer);
13637 break;
13639 case RID_CONCEPT:
13640 ds = ds_concept;
13641 cp_lexer_consume_token (parser->lexer);
13642 break;
13644 /* function-specifier:
13645 inline
13646 virtual
13647 explicit */
13648 case RID_INLINE:
13649 case RID_VIRTUAL:
13650 case RID_EXPLICIT:
13651 cp_parser_function_specifier_opt (parser, decl_specs);
13652 break;
13654 /* decl-specifier:
13655 typedef */
13656 case RID_TYPEDEF:
13657 ds = ds_typedef;
13658 /* Consume the token. */
13659 cp_lexer_consume_token (parser->lexer);
13660 /* A constructor declarator cannot appear in a typedef. */
13661 constructor_possible_p = false;
13662 /* The "typedef" keyword can only occur in a declaration; we
13663 may as well commit at this point. */
13664 cp_parser_commit_to_tentative_parse (parser);
13666 if (decl_specs->storage_class != sc_none)
13667 decl_specs->conflicting_specifiers_p = true;
13668 break;
13670 /* storage-class-specifier:
13671 auto
13672 register
13673 static
13674 extern
13675 mutable
13677 GNU Extension:
13678 thread */
13679 case RID_AUTO:
13680 if (cxx_dialect == cxx98)
13682 /* Consume the token. */
13683 cp_lexer_consume_token (parser->lexer);
13685 /* Complain about `auto' as a storage specifier, if
13686 we're complaining about C++0x compatibility. */
13687 gcc_rich_location richloc (token->location);
13688 richloc.add_fixit_remove ();
13689 warning_at (&richloc, OPT_Wc__11_compat,
13690 "%<auto%> changes meaning in C++11; "
13691 "please remove it");
13693 /* Set the storage class anyway. */
13694 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13695 token);
13697 else
13698 /* C++0x auto type-specifier. */
13699 found_decl_spec = false;
13700 break;
13702 case RID_REGISTER:
13703 case RID_STATIC:
13704 case RID_EXTERN:
13705 case RID_MUTABLE:
13706 /* Consume the token. */
13707 cp_lexer_consume_token (parser->lexer);
13708 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13709 token);
13710 break;
13711 case RID_THREAD:
13712 /* Consume the token. */
13713 ds = ds_thread;
13714 cp_lexer_consume_token (parser->lexer);
13715 break;
13717 default:
13718 /* We did not yet find a decl-specifier yet. */
13719 found_decl_spec = false;
13720 break;
13723 if (found_decl_spec
13724 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13725 && token->keyword != RID_CONSTEXPR)
13726 error ("decl-specifier invalid in condition");
13728 if (found_decl_spec
13729 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13730 && token->keyword != RID_MUTABLE
13731 && token->keyword != RID_CONSTEXPR)
13732 error_at (token->location, "%qD invalid in lambda",
13733 ridpointers[token->keyword]);
13735 if (ds != ds_last)
13736 set_and_check_decl_spec_loc (decl_specs, ds, token);
13738 /* Constructors are a special case. The `S' in `S()' is not a
13739 decl-specifier; it is the beginning of the declarator. */
13740 constructor_p
13741 = (!found_decl_spec
13742 && constructor_possible_p
13743 && (cp_parser_constructor_declarator_p
13744 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13746 /* If we don't have a DECL_SPEC yet, then we must be looking at
13747 a type-specifier. */
13748 if (!found_decl_spec && !constructor_p)
13750 int decl_spec_declares_class_or_enum;
13751 bool is_cv_qualifier;
13752 tree type_spec;
13754 type_spec
13755 = cp_parser_type_specifier (parser, flags,
13756 decl_specs,
13757 /*is_declaration=*/true,
13758 &decl_spec_declares_class_or_enum,
13759 &is_cv_qualifier);
13760 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13762 /* If this type-specifier referenced a user-defined type
13763 (a typedef, class-name, etc.), then we can't allow any
13764 more such type-specifiers henceforth.
13766 [dcl.spec]
13768 The longest sequence of decl-specifiers that could
13769 possibly be a type name is taken as the
13770 decl-specifier-seq of a declaration. The sequence shall
13771 be self-consistent as described below.
13773 [dcl.type]
13775 As a general rule, at most one type-specifier is allowed
13776 in the complete decl-specifier-seq of a declaration. The
13777 only exceptions are the following:
13779 -- const or volatile can be combined with any other
13780 type-specifier.
13782 -- signed or unsigned can be combined with char, long,
13783 short, or int.
13785 -- ..
13787 Example:
13789 typedef char* Pc;
13790 void g (const int Pc);
13792 Here, Pc is *not* part of the decl-specifier seq; it's
13793 the declarator. Therefore, once we see a type-specifier
13794 (other than a cv-qualifier), we forbid any additional
13795 user-defined types. We *do* still allow things like `int
13796 int' to be considered a decl-specifier-seq, and issue the
13797 error message later. */
13798 if (type_spec && !is_cv_qualifier)
13799 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13800 /* A constructor declarator cannot follow a type-specifier. */
13801 if (type_spec)
13803 constructor_possible_p = false;
13804 found_decl_spec = true;
13805 if (!is_cv_qualifier)
13806 decl_specs->any_type_specifiers_p = true;
13808 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13809 error_at (token->location, "type-specifier invalid in lambda");
13813 /* If we still do not have a DECL_SPEC, then there are no more
13814 decl-specifiers. */
13815 if (!found_decl_spec)
13816 break;
13818 decl_specs->any_specifiers_p = true;
13819 /* After we see one decl-specifier, further decl-specifiers are
13820 always optional. */
13821 flags |= CP_PARSER_FLAGS_OPTIONAL;
13824 /* Don't allow a friend specifier with a class definition. */
13825 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13826 && (*declares_class_or_enum & 2))
13827 error_at (decl_specs->locations[ds_friend],
13828 "class definition may not be declared a friend");
13831 /* Parse an (optional) storage-class-specifier.
13833 storage-class-specifier:
13834 auto
13835 register
13836 static
13837 extern
13838 mutable
13840 GNU Extension:
13842 storage-class-specifier:
13843 thread
13845 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13847 static tree
13848 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13850 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13852 case RID_AUTO:
13853 if (cxx_dialect != cxx98)
13854 return NULL_TREE;
13855 /* Fall through for C++98. */
13856 gcc_fallthrough ();
13858 case RID_REGISTER:
13859 case RID_STATIC:
13860 case RID_EXTERN:
13861 case RID_MUTABLE:
13862 case RID_THREAD:
13863 /* Consume the token. */
13864 return cp_lexer_consume_token (parser->lexer)->u.value;
13866 default:
13867 return NULL_TREE;
13871 /* Parse an (optional) function-specifier.
13873 function-specifier:
13874 inline
13875 virtual
13876 explicit
13878 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13879 Updates DECL_SPECS, if it is non-NULL. */
13881 static tree
13882 cp_parser_function_specifier_opt (cp_parser* parser,
13883 cp_decl_specifier_seq *decl_specs)
13885 cp_token *token = cp_lexer_peek_token (parser->lexer);
13886 switch (token->keyword)
13888 case RID_INLINE:
13889 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13890 break;
13892 case RID_VIRTUAL:
13893 /* 14.5.2.3 [temp.mem]
13895 A member function template shall not be virtual. */
13896 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13897 && current_class_type)
13898 error_at (token->location, "templates may not be %<virtual%>");
13899 else
13900 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13901 break;
13903 case RID_EXPLICIT:
13904 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13905 break;
13907 default:
13908 return NULL_TREE;
13911 /* Consume the token. */
13912 return cp_lexer_consume_token (parser->lexer)->u.value;
13915 /* Parse a linkage-specification.
13917 linkage-specification:
13918 extern string-literal { declaration-seq [opt] }
13919 extern string-literal declaration */
13921 static void
13922 cp_parser_linkage_specification (cp_parser* parser)
13924 tree linkage;
13926 /* Look for the `extern' keyword. */
13927 cp_token *extern_token
13928 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13930 /* Look for the string-literal. */
13931 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13932 linkage = cp_parser_string_literal (parser, false, false);
13934 /* Transform the literal into an identifier. If the literal is a
13935 wide-character string, or contains embedded NULs, then we can't
13936 handle it as the user wants. */
13937 if (strlen (TREE_STRING_POINTER (linkage))
13938 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13940 cp_parser_error (parser, "invalid linkage-specification");
13941 /* Assume C++ linkage. */
13942 linkage = lang_name_cplusplus;
13944 else
13945 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13947 /* We're now using the new linkage. */
13948 push_lang_context (linkage);
13950 /* Preserve the location of the the innermost linkage specification,
13951 tracking the locations of nested specifications via a local. */
13952 location_t saved_location
13953 = parser->innermost_linkage_specification_location;
13954 /* Construct a location ranging from the start of the "extern" to
13955 the end of the string-literal, with the caret at the start, e.g.:
13956 extern "C" {
13957 ^~~~~~~~~~
13959 parser->innermost_linkage_specification_location
13960 = make_location (extern_token->location,
13961 extern_token->location,
13962 get_finish (string_token->location));
13964 /* If the next token is a `{', then we're using the first
13965 production. */
13966 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13968 cp_ensure_no_omp_declare_simd (parser);
13969 cp_ensure_no_oacc_routine (parser);
13971 /* Consume the `{' token. */
13972 matching_braces braces;
13973 braces.consume_open (parser)->location;
13974 /* Parse the declarations. */
13975 cp_parser_declaration_seq_opt (parser);
13976 /* Look for the closing `}'. */
13977 braces.require_close (parser);
13979 /* Otherwise, there's just one declaration. */
13980 else
13982 bool saved_in_unbraced_linkage_specification_p;
13984 saved_in_unbraced_linkage_specification_p
13985 = parser->in_unbraced_linkage_specification_p;
13986 parser->in_unbraced_linkage_specification_p = true;
13987 cp_parser_declaration (parser);
13988 parser->in_unbraced_linkage_specification_p
13989 = saved_in_unbraced_linkage_specification_p;
13992 /* We're done with the linkage-specification. */
13993 pop_lang_context ();
13995 /* Restore location of parent linkage specification, if any. */
13996 parser->innermost_linkage_specification_location = saved_location;
13999 /* Parse a static_assert-declaration.
14001 static_assert-declaration:
14002 static_assert ( constant-expression , string-literal ) ;
14003 static_assert ( constant-expression ) ; (C++17)
14005 If MEMBER_P, this static_assert is a class member. */
14007 static void
14008 cp_parser_static_assert(cp_parser *parser, bool member_p)
14010 cp_expr condition;
14011 location_t token_loc;
14012 tree message;
14013 bool dummy;
14015 /* Peek at the `static_assert' token so we can keep track of exactly
14016 where the static assertion started. */
14017 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14019 /* Look for the `static_assert' keyword. */
14020 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14021 RT_STATIC_ASSERT))
14022 return;
14024 /* We know we are in a static assertion; commit to any tentative
14025 parse. */
14026 if (cp_parser_parsing_tentatively (parser))
14027 cp_parser_commit_to_tentative_parse (parser);
14029 /* Parse the `(' starting the static assertion condition. */
14030 matching_parens parens;
14031 parens.require_open (parser);
14033 /* Parse the constant-expression. Allow a non-constant expression
14034 here in order to give better diagnostics in finish_static_assert. */
14035 condition =
14036 cp_parser_constant_expression (parser,
14037 /*allow_non_constant_p=*/true,
14038 /*non_constant_p=*/&dummy);
14040 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14042 if (cxx_dialect < cxx17)
14043 pedwarn (input_location, OPT_Wpedantic,
14044 "static_assert without a message "
14045 "only available with -std=c++17 or -std=gnu++17");
14046 /* Eat the ')' */
14047 cp_lexer_consume_token (parser->lexer);
14048 message = build_string (1, "");
14049 TREE_TYPE (message) = char_array_type_node;
14050 fix_string_type (message);
14052 else
14054 /* Parse the separating `,'. */
14055 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14057 /* Parse the string-literal message. */
14058 message = cp_parser_string_literal (parser,
14059 /*translate=*/false,
14060 /*wide_ok=*/true);
14062 /* A `)' completes the static assertion. */
14063 if (!parens.require_close (parser))
14064 cp_parser_skip_to_closing_parenthesis (parser,
14065 /*recovering=*/true,
14066 /*or_comma=*/false,
14067 /*consume_paren=*/true);
14070 /* A semicolon terminates the declaration. */
14071 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14073 /* Get the location for the static assertion. Use that of the
14074 condition if available, otherwise, use that of the "static_assert"
14075 token. */
14076 location_t assert_loc = condition.get_location ();
14077 if (assert_loc == UNKNOWN_LOCATION)
14078 assert_loc = token_loc;
14080 /* Complete the static assertion, which may mean either processing
14081 the static assert now or saving it for template instantiation. */
14082 finish_static_assert (condition, message, assert_loc, member_p);
14085 /* Parse the expression in decltype ( expression ). */
14087 static tree
14088 cp_parser_decltype_expr (cp_parser *parser,
14089 bool &id_expression_or_member_access_p)
14091 cp_token *id_expr_start_token;
14092 tree expr;
14094 /* Since we're going to preserve any side-effects from this parse, set up a
14095 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14096 in the expression. */
14097 tentative_firewall firewall (parser);
14099 /* First, try parsing an id-expression. */
14100 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14101 cp_parser_parse_tentatively (parser);
14102 expr = cp_parser_id_expression (parser,
14103 /*template_keyword_p=*/false,
14104 /*check_dependency_p=*/true,
14105 /*template_p=*/NULL,
14106 /*declarator_p=*/false,
14107 /*optional_p=*/false);
14109 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14111 bool non_integral_constant_expression_p = false;
14112 tree id_expression = expr;
14113 cp_id_kind idk;
14114 const char *error_msg;
14116 if (identifier_p (expr))
14117 /* Lookup the name we got back from the id-expression. */
14118 expr = cp_parser_lookup_name_simple (parser, expr,
14119 id_expr_start_token->location);
14121 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14122 /* A template without args is not a complete id-expression. */
14123 expr = error_mark_node;
14125 if (expr
14126 && expr != error_mark_node
14127 && TREE_CODE (expr) != TYPE_DECL
14128 && (TREE_CODE (expr) != BIT_NOT_EXPR
14129 || !TYPE_P (TREE_OPERAND (expr, 0)))
14130 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14132 /* Complete lookup of the id-expression. */
14133 expr = (finish_id_expression
14134 (id_expression, expr, parser->scope, &idk,
14135 /*integral_constant_expression_p=*/false,
14136 /*allow_non_integral_constant_expression_p=*/true,
14137 &non_integral_constant_expression_p,
14138 /*template_p=*/false,
14139 /*done=*/true,
14140 /*address_p=*/false,
14141 /*template_arg_p=*/false,
14142 &error_msg,
14143 id_expr_start_token->location));
14145 if (expr == error_mark_node)
14146 /* We found an id-expression, but it was something that we
14147 should not have found. This is an error, not something
14148 we can recover from, so note that we found an
14149 id-expression and we'll recover as gracefully as
14150 possible. */
14151 id_expression_or_member_access_p = true;
14154 if (expr
14155 && expr != error_mark_node
14156 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14157 /* We have an id-expression. */
14158 id_expression_or_member_access_p = true;
14161 if (!id_expression_or_member_access_p)
14163 /* Abort the id-expression parse. */
14164 cp_parser_abort_tentative_parse (parser);
14166 /* Parsing tentatively, again. */
14167 cp_parser_parse_tentatively (parser);
14169 /* Parse a class member access. */
14170 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14171 /*cast_p=*/false, /*decltype*/true,
14172 /*member_access_only_p=*/true, NULL);
14174 if (expr
14175 && expr != error_mark_node
14176 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14177 /* We have an id-expression. */
14178 id_expression_or_member_access_p = true;
14181 if (id_expression_or_member_access_p)
14182 /* We have parsed the complete id-expression or member access. */
14183 cp_parser_parse_definitely (parser);
14184 else
14186 /* Abort our attempt to parse an id-expression or member access
14187 expression. */
14188 cp_parser_abort_tentative_parse (parser);
14190 /* Commit to the tentative_firewall so we get syntax errors. */
14191 cp_parser_commit_to_tentative_parse (parser);
14193 /* Parse a full expression. */
14194 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14195 /*decltype_p=*/true);
14198 return expr;
14201 /* Parse a `decltype' type. Returns the type.
14203 simple-type-specifier:
14204 decltype ( expression )
14205 C++14 proposal:
14206 decltype ( auto ) */
14208 static tree
14209 cp_parser_decltype (cp_parser *parser)
14211 bool id_expression_or_member_access_p = false;
14212 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14214 if (start_token->type == CPP_DECLTYPE)
14216 /* Already parsed. */
14217 cp_lexer_consume_token (parser->lexer);
14218 return saved_checks_value (start_token->u.tree_check_value);
14221 /* Look for the `decltype' token. */
14222 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14223 return error_mark_node;
14225 /* Parse the opening `('. */
14226 matching_parens parens;
14227 if (!parens.require_open (parser))
14228 return error_mark_node;
14230 push_deferring_access_checks (dk_deferred);
14232 tree expr = NULL_TREE;
14234 if (cxx_dialect >= cxx14
14235 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14236 /* decltype (auto) */
14237 cp_lexer_consume_token (parser->lexer);
14238 else
14240 /* decltype (expression) */
14242 /* Types cannot be defined in a `decltype' expression. Save away the
14243 old message and set the new one. */
14244 const char *saved_message = parser->type_definition_forbidden_message;
14245 parser->type_definition_forbidden_message
14246 = G_("types may not be defined in %<decltype%> expressions");
14248 /* The restrictions on constant-expressions do not apply inside
14249 decltype expressions. */
14250 bool saved_integral_constant_expression_p
14251 = parser->integral_constant_expression_p;
14252 bool saved_non_integral_constant_expression_p
14253 = parser->non_integral_constant_expression_p;
14254 parser->integral_constant_expression_p = false;
14256 /* Within a parenthesized expression, a `>' token is always
14257 the greater-than operator. */
14258 bool saved_greater_than_is_operator_p
14259 = parser->greater_than_is_operator_p;
14260 parser->greater_than_is_operator_p = true;
14262 /* Do not actually evaluate the expression. */
14263 ++cp_unevaluated_operand;
14265 /* Do not warn about problems with the expression. */
14266 ++c_inhibit_evaluation_warnings;
14268 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14270 /* Go back to evaluating expressions. */
14271 --cp_unevaluated_operand;
14272 --c_inhibit_evaluation_warnings;
14274 /* The `>' token might be the end of a template-id or
14275 template-parameter-list now. */
14276 parser->greater_than_is_operator_p
14277 = saved_greater_than_is_operator_p;
14279 /* Restore the old message and the integral constant expression
14280 flags. */
14281 parser->type_definition_forbidden_message = saved_message;
14282 parser->integral_constant_expression_p
14283 = saved_integral_constant_expression_p;
14284 parser->non_integral_constant_expression_p
14285 = saved_non_integral_constant_expression_p;
14288 /* Parse to the closing `)'. */
14289 if (!parens.require_close (parser))
14291 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14292 /*consume_paren=*/true);
14293 pop_deferring_access_checks ();
14294 return error_mark_node;
14297 if (!expr)
14299 /* Build auto. */
14300 expr = make_decltype_auto ();
14301 AUTO_IS_DECLTYPE (expr) = true;
14303 else
14304 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14305 tf_warning_or_error);
14307 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14308 it again. */
14309 start_token->type = CPP_DECLTYPE;
14310 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14311 start_token->u.tree_check_value->value = expr;
14312 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14313 start_token->keyword = RID_MAX;
14314 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14316 pop_to_parent_deferring_access_checks ();
14318 return expr;
14321 /* Special member functions [gram.special] */
14323 /* Parse a conversion-function-id.
14325 conversion-function-id:
14326 operator conversion-type-id
14328 Returns an IDENTIFIER_NODE representing the operator. */
14330 static tree
14331 cp_parser_conversion_function_id (cp_parser* parser)
14333 tree type;
14334 tree saved_scope;
14335 tree saved_qualifying_scope;
14336 tree saved_object_scope;
14337 tree pushed_scope = NULL_TREE;
14339 /* Look for the `operator' token. */
14340 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14341 return error_mark_node;
14342 /* When we parse the conversion-type-id, the current scope will be
14343 reset. However, we need that information in able to look up the
14344 conversion function later, so we save it here. */
14345 saved_scope = parser->scope;
14346 saved_qualifying_scope = parser->qualifying_scope;
14347 saved_object_scope = parser->object_scope;
14348 /* We must enter the scope of the class so that the names of
14349 entities declared within the class are available in the
14350 conversion-type-id. For example, consider:
14352 struct S {
14353 typedef int I;
14354 operator I();
14357 S::operator I() { ... }
14359 In order to see that `I' is a type-name in the definition, we
14360 must be in the scope of `S'. */
14361 if (saved_scope)
14362 pushed_scope = push_scope (saved_scope);
14363 /* Parse the conversion-type-id. */
14364 type = cp_parser_conversion_type_id (parser);
14365 /* Leave the scope of the class, if any. */
14366 if (pushed_scope)
14367 pop_scope (pushed_scope);
14368 /* Restore the saved scope. */
14369 parser->scope = saved_scope;
14370 parser->qualifying_scope = saved_qualifying_scope;
14371 parser->object_scope = saved_object_scope;
14372 /* If the TYPE is invalid, indicate failure. */
14373 if (type == error_mark_node)
14374 return error_mark_node;
14375 return make_conv_op_name (type);
14378 /* Parse a conversion-type-id:
14380 conversion-type-id:
14381 type-specifier-seq conversion-declarator [opt]
14383 Returns the TYPE specified. */
14385 static tree
14386 cp_parser_conversion_type_id (cp_parser* parser)
14388 tree attributes;
14389 cp_decl_specifier_seq type_specifiers;
14390 cp_declarator *declarator;
14391 tree type_specified;
14392 const char *saved_message;
14394 /* Parse the attributes. */
14395 attributes = cp_parser_attributes_opt (parser);
14397 saved_message = parser->type_definition_forbidden_message;
14398 parser->type_definition_forbidden_message
14399 = G_("types may not be defined in a conversion-type-id");
14401 /* Parse the type-specifiers. */
14402 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14403 /*is_trailing_return=*/false,
14404 &type_specifiers);
14406 parser->type_definition_forbidden_message = saved_message;
14408 /* If that didn't work, stop. */
14409 if (type_specifiers.type == error_mark_node)
14410 return error_mark_node;
14411 /* Parse the conversion-declarator. */
14412 declarator = cp_parser_conversion_declarator_opt (parser);
14414 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14415 /*initialized=*/0, &attributes);
14416 if (attributes)
14417 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14419 /* Don't give this error when parsing tentatively. This happens to
14420 work because we always parse this definitively once. */
14421 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14422 && type_uses_auto (type_specified))
14424 if (cxx_dialect < cxx14)
14426 error ("invalid use of %<auto%> in conversion operator");
14427 return error_mark_node;
14429 else if (template_parm_scope_p ())
14430 warning (0, "use of %<auto%> in member template "
14431 "conversion operator can never be deduced");
14434 return type_specified;
14437 /* Parse an (optional) conversion-declarator.
14439 conversion-declarator:
14440 ptr-operator conversion-declarator [opt]
14444 static cp_declarator *
14445 cp_parser_conversion_declarator_opt (cp_parser* parser)
14447 enum tree_code code;
14448 tree class_type, std_attributes = NULL_TREE;
14449 cp_cv_quals cv_quals;
14451 /* We don't know if there's a ptr-operator next, or not. */
14452 cp_parser_parse_tentatively (parser);
14453 /* Try the ptr-operator. */
14454 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14455 &std_attributes);
14456 /* If it worked, look for more conversion-declarators. */
14457 if (cp_parser_parse_definitely (parser))
14459 cp_declarator *declarator;
14461 /* Parse another optional declarator. */
14462 declarator = cp_parser_conversion_declarator_opt (parser);
14464 declarator = cp_parser_make_indirect_declarator
14465 (code, class_type, cv_quals, declarator, std_attributes);
14467 return declarator;
14470 return NULL;
14473 /* Parse an (optional) ctor-initializer.
14475 ctor-initializer:
14476 : mem-initializer-list */
14478 static void
14479 cp_parser_ctor_initializer_opt (cp_parser* parser)
14481 /* If the next token is not a `:', then there is no
14482 ctor-initializer. */
14483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14485 /* Do default initialization of any bases and members. */
14486 if (DECL_CONSTRUCTOR_P (current_function_decl))
14487 finish_mem_initializers (NULL_TREE);
14488 return;
14491 /* Consume the `:' token. */
14492 cp_lexer_consume_token (parser->lexer);
14493 /* And the mem-initializer-list. */
14494 cp_parser_mem_initializer_list (parser);
14497 /* Parse a mem-initializer-list.
14499 mem-initializer-list:
14500 mem-initializer ... [opt]
14501 mem-initializer ... [opt] , mem-initializer-list */
14503 static void
14504 cp_parser_mem_initializer_list (cp_parser* parser)
14506 tree mem_initializer_list = NULL_TREE;
14507 tree target_ctor = error_mark_node;
14508 cp_token *token = cp_lexer_peek_token (parser->lexer);
14510 /* Let the semantic analysis code know that we are starting the
14511 mem-initializer-list. */
14512 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14513 error_at (token->location,
14514 "only constructors take member initializers");
14516 /* Loop through the list. */
14517 while (true)
14519 tree mem_initializer;
14521 token = cp_lexer_peek_token (parser->lexer);
14522 /* Parse the mem-initializer. */
14523 mem_initializer = cp_parser_mem_initializer (parser);
14524 /* If the next token is a `...', we're expanding member initializers. */
14525 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14526 if (ellipsis
14527 || (mem_initializer != error_mark_node
14528 && check_for_bare_parameter_packs (TREE_PURPOSE
14529 (mem_initializer))))
14531 /* Consume the `...'. */
14532 if (ellipsis)
14533 cp_lexer_consume_token (parser->lexer);
14535 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14536 can be expanded but members cannot. */
14537 if (mem_initializer != error_mark_node
14538 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14540 error_at (token->location,
14541 "cannot expand initializer for member %qD",
14542 TREE_PURPOSE (mem_initializer));
14543 mem_initializer = error_mark_node;
14546 /* Construct the pack expansion type. */
14547 if (mem_initializer != error_mark_node)
14548 mem_initializer = make_pack_expansion (mem_initializer);
14550 if (target_ctor != error_mark_node
14551 && mem_initializer != error_mark_node)
14553 error ("mem-initializer for %qD follows constructor delegation",
14554 TREE_PURPOSE (mem_initializer));
14555 mem_initializer = error_mark_node;
14557 /* Look for a target constructor. */
14558 if (mem_initializer != error_mark_node
14559 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14560 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14562 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14563 if (mem_initializer_list)
14565 error ("constructor delegation follows mem-initializer for %qD",
14566 TREE_PURPOSE (mem_initializer_list));
14567 mem_initializer = error_mark_node;
14569 target_ctor = mem_initializer;
14571 /* Add it to the list, unless it was erroneous. */
14572 if (mem_initializer != error_mark_node)
14574 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14575 mem_initializer_list = mem_initializer;
14577 /* If the next token is not a `,', we're done. */
14578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14579 break;
14580 /* Consume the `,' token. */
14581 cp_lexer_consume_token (parser->lexer);
14584 /* Perform semantic analysis. */
14585 if (DECL_CONSTRUCTOR_P (current_function_decl))
14586 finish_mem_initializers (mem_initializer_list);
14589 /* Parse a mem-initializer.
14591 mem-initializer:
14592 mem-initializer-id ( expression-list [opt] )
14593 mem-initializer-id braced-init-list
14595 GNU extension:
14597 mem-initializer:
14598 ( expression-list [opt] )
14600 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14601 class) or FIELD_DECL (for a non-static data member) to initialize;
14602 the TREE_VALUE is the expression-list. An empty initialization
14603 list is represented by void_list_node. */
14605 static tree
14606 cp_parser_mem_initializer (cp_parser* parser)
14608 tree mem_initializer_id;
14609 tree expression_list;
14610 tree member;
14611 cp_token *token = cp_lexer_peek_token (parser->lexer);
14613 /* Find out what is being initialized. */
14614 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14616 permerror (token->location,
14617 "anachronistic old-style base class initializer");
14618 mem_initializer_id = NULL_TREE;
14620 else
14622 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14623 if (mem_initializer_id == error_mark_node)
14624 return mem_initializer_id;
14626 member = expand_member_init (mem_initializer_id);
14627 if (member && !DECL_P (member))
14628 in_base_initializer = 1;
14630 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14632 bool expr_non_constant_p;
14633 cp_lexer_set_source_position (parser->lexer);
14634 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14635 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14636 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14637 expression_list = build_tree_list (NULL_TREE, expression_list);
14639 else
14641 vec<tree, va_gc> *vec;
14642 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14643 /*cast_p=*/false,
14644 /*allow_expansion_p=*/true,
14645 /*non_constant_p=*/NULL);
14646 if (vec == NULL)
14647 return error_mark_node;
14648 expression_list = build_tree_list_vec (vec);
14649 release_tree_vector (vec);
14652 if (expression_list == error_mark_node)
14653 return error_mark_node;
14654 if (!expression_list)
14655 expression_list = void_type_node;
14657 in_base_initializer = 0;
14659 return member ? build_tree_list (member, expression_list) : error_mark_node;
14662 /* Parse a mem-initializer-id.
14664 mem-initializer-id:
14665 :: [opt] nested-name-specifier [opt] class-name
14666 decltype-specifier (C++11)
14667 identifier
14669 Returns a TYPE indicating the class to be initialized for the first
14670 production (and the second in C++11). Returns an IDENTIFIER_NODE
14671 indicating the data member to be initialized for the last production. */
14673 static tree
14674 cp_parser_mem_initializer_id (cp_parser* parser)
14676 bool global_scope_p;
14677 bool nested_name_specifier_p;
14678 bool template_p = false;
14679 tree id;
14681 cp_token *token = cp_lexer_peek_token (parser->lexer);
14683 /* `typename' is not allowed in this context ([temp.res]). */
14684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14686 error_at (token->location,
14687 "keyword %<typename%> not allowed in this context (a qualified "
14688 "member initializer is implicitly a type)");
14689 cp_lexer_consume_token (parser->lexer);
14691 /* Look for the optional `::' operator. */
14692 global_scope_p
14693 = (cp_parser_global_scope_opt (parser,
14694 /*current_scope_valid_p=*/false)
14695 != NULL_TREE);
14696 /* Look for the optional nested-name-specifier. The simplest way to
14697 implement:
14699 [temp.res]
14701 The keyword `typename' is not permitted in a base-specifier or
14702 mem-initializer; in these contexts a qualified name that
14703 depends on a template-parameter is implicitly assumed to be a
14704 type name.
14706 is to assume that we have seen the `typename' keyword at this
14707 point. */
14708 nested_name_specifier_p
14709 = (cp_parser_nested_name_specifier_opt (parser,
14710 /*typename_keyword_p=*/true,
14711 /*check_dependency_p=*/true,
14712 /*type_p=*/true,
14713 /*is_declaration=*/true)
14714 != NULL_TREE);
14715 if (nested_name_specifier_p)
14716 template_p = cp_parser_optional_template_keyword (parser);
14717 /* If there is a `::' operator or a nested-name-specifier, then we
14718 are definitely looking for a class-name. */
14719 if (global_scope_p || nested_name_specifier_p)
14720 return cp_parser_class_name (parser,
14721 /*typename_keyword_p=*/true,
14722 /*template_keyword_p=*/template_p,
14723 typename_type,
14724 /*check_dependency_p=*/true,
14725 /*class_head_p=*/false,
14726 /*is_declaration=*/true);
14727 /* Otherwise, we could also be looking for an ordinary identifier. */
14728 cp_parser_parse_tentatively (parser);
14729 if (cp_lexer_next_token_is_decltype (parser->lexer))
14730 /* Try a decltype-specifier. */
14731 id = cp_parser_decltype (parser);
14732 else
14733 /* Otherwise, try a class-name. */
14734 id = cp_parser_class_name (parser,
14735 /*typename_keyword_p=*/true,
14736 /*template_keyword_p=*/false,
14737 none_type,
14738 /*check_dependency_p=*/true,
14739 /*class_head_p=*/false,
14740 /*is_declaration=*/true);
14741 /* If we found one, we're done. */
14742 if (cp_parser_parse_definitely (parser))
14743 return id;
14744 /* Otherwise, look for an ordinary identifier. */
14745 return cp_parser_identifier (parser);
14748 /* Overloading [gram.over] */
14750 /* Parse an operator-function-id.
14752 operator-function-id:
14753 operator operator
14755 Returns an IDENTIFIER_NODE for the operator which is a
14756 human-readable spelling of the identifier, e.g., `operator +'. */
14758 static cp_expr
14759 cp_parser_operator_function_id (cp_parser* parser)
14761 /* Look for the `operator' keyword. */
14762 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14763 return error_mark_node;
14764 /* And then the name of the operator itself. */
14765 return cp_parser_operator (parser);
14768 /* Return an identifier node for a user-defined literal operator.
14769 The suffix identifier is chained to the operator name identifier. */
14771 tree
14772 cp_literal_operator_id (const char* name)
14774 tree identifier;
14775 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14776 + strlen (name) + 10);
14777 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14778 identifier = get_identifier (buffer);
14780 return identifier;
14783 /* Parse an operator.
14785 operator:
14786 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14787 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14788 || ++ -- , ->* -> () []
14790 GNU Extensions:
14792 operator:
14793 <? >? <?= >?=
14795 Returns an IDENTIFIER_NODE for the operator which is a
14796 human-readable spelling of the identifier, e.g., `operator +'. */
14798 static cp_expr
14799 cp_parser_operator (cp_parser* parser)
14801 tree id = NULL_TREE;
14802 cp_token *token;
14803 bool utf8 = false;
14805 /* Peek at the next token. */
14806 token = cp_lexer_peek_token (parser->lexer);
14808 location_t start_loc = token->location;
14810 /* Figure out which operator we have. */
14811 enum tree_code op = ERROR_MARK;
14812 bool assop = false;
14813 bool consumed = false;
14814 switch (token->type)
14816 case CPP_KEYWORD:
14818 /* The keyword should be either `new' or `delete'. */
14819 if (token->keyword == RID_NEW)
14820 op = NEW_EXPR;
14821 else if (token->keyword == RID_DELETE)
14822 op = DELETE_EXPR;
14823 else
14824 break;
14826 /* Consume the `new' or `delete' token. */
14827 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14829 /* Peek at the next token. */
14830 token = cp_lexer_peek_token (parser->lexer);
14831 /* If it's a `[' token then this is the array variant of the
14832 operator. */
14833 if (token->type == CPP_OPEN_SQUARE)
14835 /* Consume the `[' token. */
14836 cp_lexer_consume_token (parser->lexer);
14837 /* Look for the `]' token. */
14838 if (cp_token *close_token
14839 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14840 end_loc = close_token->location;
14841 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14843 start_loc = make_location (start_loc, start_loc, end_loc);
14844 consumed = true;
14845 break;
14848 case CPP_PLUS:
14849 op = PLUS_EXPR;
14850 break;
14852 case CPP_MINUS:
14853 op = MINUS_EXPR;
14854 break;
14856 case CPP_MULT:
14857 op = MULT_EXPR;
14858 break;
14860 case CPP_DIV:
14861 op = TRUNC_DIV_EXPR;
14862 break;
14864 case CPP_MOD:
14865 op = TRUNC_MOD_EXPR;
14866 break;
14868 case CPP_XOR:
14869 op = BIT_XOR_EXPR;
14870 break;
14872 case CPP_AND:
14873 op = BIT_AND_EXPR;
14874 break;
14876 case CPP_OR:
14877 op = BIT_IOR_EXPR;
14878 break;
14880 case CPP_COMPL:
14881 op = BIT_NOT_EXPR;
14882 break;
14884 case CPP_NOT:
14885 op = TRUTH_NOT_EXPR;
14886 break;
14888 case CPP_EQ:
14889 assop = true;
14890 op = NOP_EXPR;
14891 break;
14893 case CPP_LESS:
14894 op = LT_EXPR;
14895 break;
14897 case CPP_GREATER:
14898 op = GT_EXPR;
14899 break;
14901 case CPP_PLUS_EQ:
14902 assop = true;
14903 op = PLUS_EXPR;
14904 break;
14906 case CPP_MINUS_EQ:
14907 assop = true;
14908 op = MINUS_EXPR;
14909 break;
14911 case CPP_MULT_EQ:
14912 assop = true;
14913 op = MULT_EXPR;
14914 break;
14916 case CPP_DIV_EQ:
14917 assop = true;
14918 op = TRUNC_DIV_EXPR;
14919 break;
14921 case CPP_MOD_EQ:
14922 assop = true;
14923 op = TRUNC_MOD_EXPR;
14924 break;
14926 case CPP_XOR_EQ:
14927 assop = true;
14928 op = BIT_XOR_EXPR;
14929 break;
14931 case CPP_AND_EQ:
14932 assop = true;
14933 op = BIT_AND_EXPR;
14934 break;
14936 case CPP_OR_EQ:
14937 assop = true;
14938 op = BIT_IOR_EXPR;
14939 break;
14941 case CPP_LSHIFT:
14942 op = LSHIFT_EXPR;
14943 break;
14945 case CPP_RSHIFT:
14946 op = RSHIFT_EXPR;
14947 break;
14949 case CPP_LSHIFT_EQ:
14950 assop = true;
14951 op = LSHIFT_EXPR;
14952 break;
14954 case CPP_RSHIFT_EQ:
14955 assop = true;
14956 op = RSHIFT_EXPR;
14957 break;
14959 case CPP_EQ_EQ:
14960 op = EQ_EXPR;
14961 break;
14963 case CPP_NOT_EQ:
14964 op = NE_EXPR;
14965 break;
14967 case CPP_LESS_EQ:
14968 op = LE_EXPR;
14969 break;
14971 case CPP_GREATER_EQ:
14972 op = GE_EXPR;
14973 break;
14975 case CPP_AND_AND:
14976 op = TRUTH_ANDIF_EXPR;
14977 break;
14979 case CPP_OR_OR:
14980 op = TRUTH_ORIF_EXPR;
14981 break;
14983 case CPP_PLUS_PLUS:
14984 op = POSTINCREMENT_EXPR;
14985 break;
14987 case CPP_MINUS_MINUS:
14988 op = PREDECREMENT_EXPR;
14989 break;
14991 case CPP_COMMA:
14992 op = COMPOUND_EXPR;
14993 break;
14995 case CPP_DEREF_STAR:
14996 op = MEMBER_REF;
14997 break;
14999 case CPP_DEREF:
15000 op = COMPONENT_REF;
15001 break;
15003 case CPP_OPEN_PAREN:
15005 /* Consume the `('. */
15006 matching_parens parens;
15007 parens.consume_open (parser);
15008 /* Look for the matching `)'. */
15009 parens.require_close (parser);
15010 op = CALL_EXPR;
15011 consumed = true;
15012 break;
15015 case CPP_OPEN_SQUARE:
15016 /* Consume the `['. */
15017 cp_lexer_consume_token (parser->lexer);
15018 /* Look for the matching `]'. */
15019 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15020 op = ARRAY_REF;
15021 consumed = true;
15022 break;
15024 case CPP_UTF8STRING:
15025 case CPP_UTF8STRING_USERDEF:
15026 utf8 = true;
15027 /* FALLTHRU */
15028 case CPP_STRING:
15029 case CPP_WSTRING:
15030 case CPP_STRING16:
15031 case CPP_STRING32:
15032 case CPP_STRING_USERDEF:
15033 case CPP_WSTRING_USERDEF:
15034 case CPP_STRING16_USERDEF:
15035 case CPP_STRING32_USERDEF:
15037 tree str, string_tree;
15038 int sz, len;
15040 if (cxx_dialect == cxx98)
15041 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15043 /* Consume the string. */
15044 str = cp_parser_string_literal (parser, /*translate=*/true,
15045 /*wide_ok=*/true, /*lookup_udlit=*/false);
15046 if (str == error_mark_node)
15047 return error_mark_node;
15048 else if (TREE_CODE (str) == USERDEF_LITERAL)
15050 string_tree = USERDEF_LITERAL_VALUE (str);
15051 id = USERDEF_LITERAL_SUFFIX_ID (str);
15053 else
15055 string_tree = str;
15056 /* Look for the suffix identifier. */
15057 token = cp_lexer_peek_token (parser->lexer);
15058 if (token->type == CPP_NAME)
15059 id = cp_parser_identifier (parser);
15060 else if (token->type == CPP_KEYWORD)
15062 error ("unexpected keyword;"
15063 " remove space between quotes and suffix identifier");
15064 return error_mark_node;
15066 else
15068 error ("expected suffix identifier");
15069 return error_mark_node;
15072 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15073 (TREE_TYPE (TREE_TYPE (string_tree))));
15074 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15075 if (len != 0)
15077 error ("expected empty string after %<operator%> keyword");
15078 return error_mark_node;
15080 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15081 != char_type_node)
15083 error ("invalid encoding prefix in literal operator");
15084 return error_mark_node;
15086 if (id != error_mark_node)
15088 const char *name = IDENTIFIER_POINTER (id);
15089 id = cp_literal_operator_id (name);
15091 return id;
15094 default:
15095 /* Anything else is an error. */
15096 break;
15099 /* If we have selected an identifier, we need to consume the
15100 operator token. */
15101 if (op != ERROR_MARK)
15103 id = ovl_op_identifier (assop, op);
15104 if (!consumed)
15105 cp_lexer_consume_token (parser->lexer);
15107 /* Otherwise, no valid operator name was present. */
15108 else
15110 cp_parser_error (parser, "expected operator");
15111 id = error_mark_node;
15114 return cp_expr (id, start_loc);
15117 /* Parse a template-declaration.
15119 template-declaration:
15120 export [opt] template < template-parameter-list > declaration
15122 If MEMBER_P is TRUE, this template-declaration occurs within a
15123 class-specifier.
15125 The grammar rule given by the standard isn't correct. What
15126 is really meant is:
15128 template-declaration:
15129 export [opt] template-parameter-list-seq
15130 decl-specifier-seq [opt] init-declarator [opt] ;
15131 export [opt] template-parameter-list-seq
15132 function-definition
15134 template-parameter-list-seq:
15135 template-parameter-list-seq [opt]
15136 template < template-parameter-list >
15138 Concept Extensions:
15140 template-parameter-list-seq:
15141 template < template-parameter-list > requires-clause [opt]
15143 requires-clause:
15144 requires logical-or-expression */
15146 static void
15147 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15149 /* Check for `export'. */
15150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15152 /* Consume the `export' token. */
15153 cp_lexer_consume_token (parser->lexer);
15154 /* Warn that we do not support `export'. */
15155 warning (0, "keyword %<export%> not implemented, and will be ignored");
15158 cp_parser_template_declaration_after_export (parser, member_p);
15161 /* Parse a template-parameter-list.
15163 template-parameter-list:
15164 template-parameter
15165 template-parameter-list , template-parameter
15167 Returns a TREE_LIST. Each node represents a template parameter.
15168 The nodes are connected via their TREE_CHAINs. */
15170 static tree
15171 cp_parser_template_parameter_list (cp_parser* parser)
15173 tree parameter_list = NULL_TREE;
15175 begin_template_parm_list ();
15177 /* The loop below parses the template parms. We first need to know
15178 the total number of template parms to be able to compute proper
15179 canonical types of each dependent type. So after the loop, when
15180 we know the total number of template parms,
15181 end_template_parm_list computes the proper canonical types and
15182 fixes up the dependent types accordingly. */
15183 while (true)
15185 tree parameter;
15186 bool is_non_type;
15187 bool is_parameter_pack;
15188 location_t parm_loc;
15190 /* Parse the template-parameter. */
15191 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15192 parameter = cp_parser_template_parameter (parser,
15193 &is_non_type,
15194 &is_parameter_pack);
15195 /* Add it to the list. */
15196 if (parameter != error_mark_node)
15197 parameter_list = process_template_parm (parameter_list,
15198 parm_loc,
15199 parameter,
15200 is_non_type,
15201 is_parameter_pack);
15202 else
15204 tree err_parm = build_tree_list (parameter, parameter);
15205 parameter_list = chainon (parameter_list, err_parm);
15208 /* If the next token is not a `,', we're done. */
15209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15210 break;
15211 /* Otherwise, consume the `,' token. */
15212 cp_lexer_consume_token (parser->lexer);
15215 return end_template_parm_list (parameter_list);
15218 /* Parse a introduction-list.
15220 introduction-list:
15221 introduced-parameter
15222 introduction-list , introduced-parameter
15224 introduced-parameter:
15225 ...[opt] identifier
15227 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15228 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15229 WILDCARD_DECL will also have DECL_NAME set and token location in
15230 DECL_SOURCE_LOCATION. */
15232 static tree
15233 cp_parser_introduction_list (cp_parser *parser)
15235 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15237 while (true)
15239 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15240 if (is_pack)
15241 cp_lexer_consume_token (parser->lexer);
15243 /* Build placeholder. */
15244 tree parm = build_nt (WILDCARD_DECL);
15245 DECL_SOURCE_LOCATION (parm)
15246 = cp_lexer_peek_token (parser->lexer)->location;
15247 DECL_NAME (parm) = cp_parser_identifier (parser);
15248 WILDCARD_PACK_P (parm) = is_pack;
15249 vec_safe_push (introduction_vec, parm);
15251 /* If the next token is not a `,', we're done. */
15252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15253 break;
15254 /* Otherwise, consume the `,' token. */
15255 cp_lexer_consume_token (parser->lexer);
15258 /* Convert the vec into a TREE_VEC. */
15259 tree introduction_list = make_tree_vec (introduction_vec->length ());
15260 unsigned int n;
15261 tree parm;
15262 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15263 TREE_VEC_ELT (introduction_list, n) = parm;
15265 release_tree_vector (introduction_vec);
15266 return introduction_list;
15269 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15270 is an abstract declarator. */
15272 static inline cp_declarator*
15273 get_id_declarator (cp_declarator *declarator)
15275 cp_declarator *d = declarator;
15276 while (d && d->kind != cdk_id)
15277 d = d->declarator;
15278 return d;
15281 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15282 is an abstract declarator. */
15284 static inline tree
15285 get_unqualified_id (cp_declarator *declarator)
15287 declarator = get_id_declarator (declarator);
15288 if (declarator)
15289 return declarator->u.id.unqualified_name;
15290 else
15291 return NULL_TREE;
15294 /* Returns true if DECL represents a constrained-parameter. */
15296 static inline bool
15297 is_constrained_parameter (tree decl)
15299 return (decl
15300 && TREE_CODE (decl) == TYPE_DECL
15301 && CONSTRAINED_PARM_CONCEPT (decl)
15302 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15305 /* Returns true if PARM declares a constrained-parameter. */
15307 static inline bool
15308 is_constrained_parameter (cp_parameter_declarator *parm)
15310 return is_constrained_parameter (parm->decl_specifiers.type);
15313 /* Check that the type parameter is only a declarator-id, and that its
15314 type is not cv-qualified. */
15316 bool
15317 cp_parser_check_constrained_type_parm (cp_parser *parser,
15318 cp_parameter_declarator *parm)
15320 if (!parm->declarator)
15321 return true;
15323 if (parm->declarator->kind != cdk_id)
15325 cp_parser_error (parser, "invalid constrained type parameter");
15326 return false;
15329 /* Don't allow cv-qualified type parameters. */
15330 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15331 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15333 cp_parser_error (parser, "cv-qualified type parameter");
15334 return false;
15337 return true;
15340 /* Finish parsing/processing a template type parameter and checking
15341 various restrictions. */
15343 static inline tree
15344 cp_parser_constrained_type_template_parm (cp_parser *parser,
15345 tree id,
15346 cp_parameter_declarator* parmdecl)
15348 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15349 return finish_template_type_parm (class_type_node, id);
15350 else
15351 return error_mark_node;
15354 static tree
15355 finish_constrained_template_template_parm (tree proto, tree id)
15357 /* FIXME: This should probably be copied, and we may need to adjust
15358 the template parameter depths. */
15359 tree saved_parms = current_template_parms;
15360 begin_template_parm_list ();
15361 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15362 end_template_parm_list ();
15364 tree parm = finish_template_template_parm (class_type_node, id);
15365 current_template_parms = saved_parms;
15367 return parm;
15370 /* Finish parsing/processing a template template parameter by borrowing
15371 the template parameter list from the prototype parameter. */
15373 static tree
15374 cp_parser_constrained_template_template_parm (cp_parser *parser,
15375 tree proto,
15376 tree id,
15377 cp_parameter_declarator *parmdecl)
15379 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15380 return error_mark_node;
15381 return finish_constrained_template_template_parm (proto, id);
15384 /* Create a new non-type template parameter from the given PARM
15385 declarator. */
15387 static tree
15388 constrained_non_type_template_parm (bool *is_non_type,
15389 cp_parameter_declarator *parm)
15391 *is_non_type = true;
15392 cp_declarator *decl = parm->declarator;
15393 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15394 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15395 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15398 /* Build a constrained template parameter based on the PARMDECL
15399 declarator. The type of PARMDECL is the constrained type, which
15400 refers to the prototype template parameter that ultimately
15401 specifies the type of the declared parameter. */
15403 static tree
15404 finish_constrained_parameter (cp_parser *parser,
15405 cp_parameter_declarator *parmdecl,
15406 bool *is_non_type,
15407 bool *is_parameter_pack)
15409 tree decl = parmdecl->decl_specifiers.type;
15410 tree id = get_unqualified_id (parmdecl->declarator);
15411 tree def = parmdecl->default_argument;
15412 tree proto = DECL_INITIAL (decl);
15414 /* A template parameter constrained by a variadic concept shall also
15415 be declared as a template parameter pack. */
15416 bool is_variadic = template_parameter_pack_p (proto);
15417 if (is_variadic && !*is_parameter_pack)
15418 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15420 /* Build the parameter. Return an error if the declarator was invalid. */
15421 tree parm;
15422 if (TREE_CODE (proto) == TYPE_DECL)
15423 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15424 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15425 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15426 parmdecl);
15427 else
15428 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15429 if (parm == error_mark_node)
15430 return error_mark_node;
15432 /* Finish the parameter decl and create a node attaching the
15433 default argument and constraint. */
15434 parm = build_tree_list (def, parm);
15435 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15437 return parm;
15440 /* Returns true if the parsed type actually represents the declaration
15441 of a type template-parameter. */
15443 static inline bool
15444 declares_constrained_type_template_parameter (tree type)
15446 return (is_constrained_parameter (type)
15447 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15451 /* Returns true if the parsed type actually represents the declaration of
15452 a template template-parameter. */
15454 static bool
15455 declares_constrained_template_template_parameter (tree type)
15457 return (is_constrained_parameter (type)
15458 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15461 /* Parse a default argument for a type template-parameter.
15462 Note that diagnostics are handled in cp_parser_template_parameter. */
15464 static tree
15465 cp_parser_default_type_template_argument (cp_parser *parser)
15467 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15469 /* Consume the `=' token. */
15470 cp_lexer_consume_token (parser->lexer);
15472 cp_token *token = cp_lexer_peek_token (parser->lexer);
15474 /* Parse the default-argument. */
15475 push_deferring_access_checks (dk_no_deferred);
15476 tree default_argument = cp_parser_type_id (parser);
15477 pop_deferring_access_checks ();
15479 if (flag_concepts && type_uses_auto (default_argument))
15481 error_at (token->location,
15482 "invalid use of %<auto%> in default template argument");
15483 return error_mark_node;
15486 return default_argument;
15489 /* Parse a default argument for a template template-parameter. */
15491 static tree
15492 cp_parser_default_template_template_argument (cp_parser *parser)
15494 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15496 bool is_template;
15498 /* Consume the `='. */
15499 cp_lexer_consume_token (parser->lexer);
15500 /* Parse the id-expression. */
15501 push_deferring_access_checks (dk_no_deferred);
15502 /* save token before parsing the id-expression, for error
15503 reporting */
15504 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15505 tree default_argument
15506 = cp_parser_id_expression (parser,
15507 /*template_keyword_p=*/false,
15508 /*check_dependency_p=*/true,
15509 /*template_p=*/&is_template,
15510 /*declarator_p=*/false,
15511 /*optional_p=*/false);
15512 if (TREE_CODE (default_argument) == TYPE_DECL)
15513 /* If the id-expression was a template-id that refers to
15514 a template-class, we already have the declaration here,
15515 so no further lookup is needed. */
15517 else
15518 /* Look up the name. */
15519 default_argument
15520 = cp_parser_lookup_name (parser, default_argument,
15521 none_type,
15522 /*is_template=*/is_template,
15523 /*is_namespace=*/false,
15524 /*check_dependency=*/true,
15525 /*ambiguous_decls=*/NULL,
15526 token->location);
15527 /* See if the default argument is valid. */
15528 default_argument = check_template_template_default_arg (default_argument);
15529 pop_deferring_access_checks ();
15530 return default_argument;
15533 /* Parse a template-parameter.
15535 template-parameter:
15536 type-parameter
15537 parameter-declaration
15539 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15540 the parameter. The TREE_PURPOSE is the default value, if any.
15541 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15542 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15543 set to true iff this parameter is a parameter pack. */
15545 static tree
15546 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15547 bool *is_parameter_pack)
15549 cp_token *token;
15550 cp_parameter_declarator *parameter_declarator;
15551 tree parm;
15553 /* Assume it is a type parameter or a template parameter. */
15554 *is_non_type = false;
15555 /* Assume it not a parameter pack. */
15556 *is_parameter_pack = false;
15557 /* Peek at the next token. */
15558 token = cp_lexer_peek_token (parser->lexer);
15559 /* If it is `template', we have a type-parameter. */
15560 if (token->keyword == RID_TEMPLATE)
15561 return cp_parser_type_parameter (parser, is_parameter_pack);
15562 /* If it is `class' or `typename' we do not know yet whether it is a
15563 type parameter or a non-type parameter. Consider:
15565 template <typename T, typename T::X X> ...
15569 template <class C, class D*> ...
15571 Here, the first parameter is a type parameter, and the second is
15572 a non-type parameter. We can tell by looking at the token after
15573 the identifier -- if it is a `,', `=', or `>' then we have a type
15574 parameter. */
15575 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15577 /* Peek at the token after `class' or `typename'. */
15578 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15579 /* If it's an ellipsis, we have a template type parameter
15580 pack. */
15581 if (token->type == CPP_ELLIPSIS)
15582 return cp_parser_type_parameter (parser, is_parameter_pack);
15583 /* If it's an identifier, skip it. */
15584 if (token->type == CPP_NAME)
15585 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15586 /* Now, see if the token looks like the end of a template
15587 parameter. */
15588 if (token->type == CPP_COMMA
15589 || token->type == CPP_EQ
15590 || token->type == CPP_GREATER)
15591 return cp_parser_type_parameter (parser, is_parameter_pack);
15594 /* Otherwise, it is a non-type parameter or a constrained parameter.
15596 [temp.param]
15598 When parsing a default template-argument for a non-type
15599 template-parameter, the first non-nested `>' is taken as the end
15600 of the template parameter-list rather than a greater-than
15601 operator. */
15602 parameter_declarator
15603 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15604 /*parenthesized_p=*/NULL);
15606 if (!parameter_declarator)
15607 return error_mark_node;
15609 /* If the parameter declaration is marked as a parameter pack, set
15610 *IS_PARAMETER_PACK to notify the caller. */
15611 if (parameter_declarator->template_parameter_pack_p)
15612 *is_parameter_pack = true;
15614 if (parameter_declarator->default_argument)
15616 /* Can happen in some cases of erroneous input (c++/34892). */
15617 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15618 /* Consume the `...' for better error recovery. */
15619 cp_lexer_consume_token (parser->lexer);
15622 // The parameter may have been constrained.
15623 if (is_constrained_parameter (parameter_declarator))
15624 return finish_constrained_parameter (parser,
15625 parameter_declarator,
15626 is_non_type,
15627 is_parameter_pack);
15629 // Now we're sure that the parameter is a non-type parameter.
15630 *is_non_type = true;
15632 parm = grokdeclarator (parameter_declarator->declarator,
15633 &parameter_declarator->decl_specifiers,
15634 TPARM, /*initialized=*/0,
15635 /*attrlist=*/NULL);
15636 if (parm == error_mark_node)
15637 return error_mark_node;
15639 return build_tree_list (parameter_declarator->default_argument, parm);
15642 /* Parse a type-parameter.
15644 type-parameter:
15645 class identifier [opt]
15646 class identifier [opt] = type-id
15647 typename identifier [opt]
15648 typename identifier [opt] = type-id
15649 template < template-parameter-list > class identifier [opt]
15650 template < template-parameter-list > class identifier [opt]
15651 = id-expression
15653 GNU Extension (variadic templates):
15655 type-parameter:
15656 class ... identifier [opt]
15657 typename ... identifier [opt]
15659 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15660 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15661 the declaration of the parameter.
15663 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15665 static tree
15666 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15668 cp_token *token;
15669 tree parameter;
15671 /* Look for a keyword to tell us what kind of parameter this is. */
15672 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15673 if (!token)
15674 return error_mark_node;
15676 switch (token->keyword)
15678 case RID_CLASS:
15679 case RID_TYPENAME:
15681 tree identifier;
15682 tree default_argument;
15684 /* If the next token is an ellipsis, we have a template
15685 argument pack. */
15686 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15688 /* Consume the `...' token. */
15689 cp_lexer_consume_token (parser->lexer);
15690 maybe_warn_variadic_templates ();
15692 *is_parameter_pack = true;
15695 /* If the next token is an identifier, then it names the
15696 parameter. */
15697 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15698 identifier = cp_parser_identifier (parser);
15699 else
15700 identifier = NULL_TREE;
15702 /* Create the parameter. */
15703 parameter = finish_template_type_parm (class_type_node, identifier);
15705 /* If the next token is an `=', we have a default argument. */
15706 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15708 default_argument
15709 = cp_parser_default_type_template_argument (parser);
15711 /* Template parameter packs cannot have default
15712 arguments. */
15713 if (*is_parameter_pack)
15715 if (identifier)
15716 error_at (token->location,
15717 "template parameter pack %qD cannot have a "
15718 "default argument", identifier);
15719 else
15720 error_at (token->location,
15721 "template parameter packs cannot have "
15722 "default arguments");
15723 default_argument = NULL_TREE;
15725 else if (check_for_bare_parameter_packs (default_argument))
15726 default_argument = error_mark_node;
15728 else
15729 default_argument = NULL_TREE;
15731 /* Create the combined representation of the parameter and the
15732 default argument. */
15733 parameter = build_tree_list (default_argument, parameter);
15735 break;
15737 case RID_TEMPLATE:
15739 tree identifier;
15740 tree default_argument;
15742 /* Look for the `<'. */
15743 cp_parser_require (parser, CPP_LESS, RT_LESS);
15744 /* Parse the template-parameter-list. */
15745 cp_parser_template_parameter_list (parser);
15746 /* Look for the `>'. */
15747 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15749 // If template requirements are present, parse them.
15750 if (flag_concepts)
15752 tree reqs = get_shorthand_constraints (current_template_parms);
15753 if (tree r = cp_parser_requires_clause_opt (parser))
15754 reqs = conjoin_constraints (reqs, normalize_expression (r));
15755 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15758 /* Look for the `class' or 'typename' keywords. */
15759 cp_parser_type_parameter_key (parser);
15760 /* If the next token is an ellipsis, we have a template
15761 argument pack. */
15762 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15764 /* Consume the `...' token. */
15765 cp_lexer_consume_token (parser->lexer);
15766 maybe_warn_variadic_templates ();
15768 *is_parameter_pack = true;
15770 /* If the next token is an `=', then there is a
15771 default-argument. If the next token is a `>', we are at
15772 the end of the parameter-list. If the next token is a `,',
15773 then we are at the end of this parameter. */
15774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15775 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15776 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15778 identifier = cp_parser_identifier (parser);
15779 /* Treat invalid names as if the parameter were nameless. */
15780 if (identifier == error_mark_node)
15781 identifier = NULL_TREE;
15783 else
15784 identifier = NULL_TREE;
15786 /* Create the template parameter. */
15787 parameter = finish_template_template_parm (class_type_node,
15788 identifier);
15790 /* If the next token is an `=', then there is a
15791 default-argument. */
15792 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15794 default_argument
15795 = cp_parser_default_template_template_argument (parser);
15797 /* Template parameter packs cannot have default
15798 arguments. */
15799 if (*is_parameter_pack)
15801 if (identifier)
15802 error_at (token->location,
15803 "template parameter pack %qD cannot "
15804 "have a default argument",
15805 identifier);
15806 else
15807 error_at (token->location, "template parameter packs cannot "
15808 "have default arguments");
15809 default_argument = NULL_TREE;
15812 else
15813 default_argument = NULL_TREE;
15815 /* Create the combined representation of the parameter and the
15816 default argument. */
15817 parameter = build_tree_list (default_argument, parameter);
15819 break;
15821 default:
15822 gcc_unreachable ();
15823 break;
15826 return parameter;
15829 /* Parse a template-id.
15831 template-id:
15832 template-name < template-argument-list [opt] >
15834 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15835 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15836 returned. Otherwise, if the template-name names a function, or set
15837 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15838 names a class, returns a TYPE_DECL for the specialization.
15840 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15841 uninstantiated templates. */
15843 static tree
15844 cp_parser_template_id (cp_parser *parser,
15845 bool template_keyword_p,
15846 bool check_dependency_p,
15847 enum tag_types tag_type,
15848 bool is_declaration)
15850 tree templ;
15851 tree arguments;
15852 tree template_id;
15853 cp_token_position start_of_id = 0;
15854 cp_token *next_token = NULL, *next_token_2 = NULL;
15855 bool is_identifier;
15857 /* If the next token corresponds to a template-id, there is no need
15858 to reparse it. */
15859 cp_token *token = cp_lexer_peek_token (parser->lexer);
15860 if (token->type == CPP_TEMPLATE_ID)
15862 cp_lexer_consume_token (parser->lexer);
15863 return saved_checks_value (token->u.tree_check_value);
15866 /* Avoid performing name lookup if there is no possibility of
15867 finding a template-id. */
15868 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15869 || (token->type == CPP_NAME
15870 && !cp_parser_nth_token_starts_template_argument_list_p
15871 (parser, 2)))
15873 cp_parser_error (parser, "expected template-id");
15874 return error_mark_node;
15877 /* Remember where the template-id starts. */
15878 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15879 start_of_id = cp_lexer_token_position (parser->lexer, false);
15881 push_deferring_access_checks (dk_deferred);
15883 /* Parse the template-name. */
15884 is_identifier = false;
15885 templ = cp_parser_template_name (parser, template_keyword_p,
15886 check_dependency_p,
15887 is_declaration,
15888 tag_type,
15889 &is_identifier);
15890 if (templ == error_mark_node || is_identifier)
15892 pop_deferring_access_checks ();
15893 return templ;
15896 /* Since we're going to preserve any side-effects from this parse, set up a
15897 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15898 in the template arguments. */
15899 tentative_firewall firewall (parser);
15901 /* If we find the sequence `[:' after a template-name, it's probably
15902 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15903 parse correctly the argument list. */
15904 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15905 == CPP_OPEN_SQUARE)
15906 && next_token->flags & DIGRAPH
15907 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15908 == CPP_COLON)
15909 && !(next_token_2->flags & PREV_WHITE))
15911 cp_parser_parse_tentatively (parser);
15912 /* Change `:' into `::'. */
15913 next_token_2->type = CPP_SCOPE;
15914 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15915 CPP_LESS. */
15916 cp_lexer_consume_token (parser->lexer);
15918 /* Parse the arguments. */
15919 arguments = cp_parser_enclosed_template_argument_list (parser);
15920 if (!cp_parser_parse_definitely (parser))
15922 /* If we couldn't parse an argument list, then we revert our changes
15923 and return simply an error. Maybe this is not a template-id
15924 after all. */
15925 next_token_2->type = CPP_COLON;
15926 cp_parser_error (parser, "expected %<<%>");
15927 pop_deferring_access_checks ();
15928 return error_mark_node;
15930 /* Otherwise, emit an error about the invalid digraph, but continue
15931 parsing because we got our argument list. */
15932 if (permerror (next_token->location,
15933 "%<<::%> cannot begin a template-argument list"))
15935 static bool hint = false;
15936 inform (next_token->location,
15937 "%<<:%> is an alternate spelling for %<[%>."
15938 " Insert whitespace between %<<%> and %<::%>");
15939 if (!hint && !flag_permissive)
15941 inform (next_token->location, "(if you use %<-fpermissive%> "
15942 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15943 "accept your code)");
15944 hint = true;
15948 else
15950 /* Look for the `<' that starts the template-argument-list. */
15951 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15953 pop_deferring_access_checks ();
15954 return error_mark_node;
15956 /* Parse the arguments. */
15957 arguments = cp_parser_enclosed_template_argument_list (parser);
15960 /* Set the location to be of the form:
15961 template-name < template-argument-list [opt] >
15962 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15963 with caret == start at the start of the template-name,
15964 ranging until the closing '>'. */
15965 location_t finish_loc
15966 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15967 location_t combined_loc
15968 = make_location (token->location, token->location, finish_loc);
15970 /* Check for concepts autos where they don't belong. We could
15971 identify types in some cases of idnetifier TEMPL, looking ahead
15972 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15973 types. We reject them in functions, but if what we have is an
15974 identifier, even with none_type we can't conclude it's NOT a
15975 type, we have to wait for template substitution. */
15976 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15977 template_id = error_mark_node;
15978 /* Build a representation of the specialization. */
15979 else if (identifier_p (templ))
15980 template_id = build_min_nt_loc (combined_loc,
15981 TEMPLATE_ID_EXPR,
15982 templ, arguments);
15983 else if (DECL_TYPE_TEMPLATE_P (templ)
15984 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15986 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15987 template (rather than some instantiation thereof) only if
15988 is not nested within some other construct. For example, in
15989 "template <typename T> void f(T) { A<T>::", A<T> is just an
15990 instantiation of A. */
15991 bool entering_scope
15992 = (template_parm_scope_p ()
15993 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
15994 template_id
15995 = finish_template_type (templ, arguments, entering_scope);
15997 /* A template-like identifier may be a partial concept id. */
15998 else if (flag_concepts
15999 && (template_id = (cp_parser_maybe_partial_concept_id
16000 (parser, templ, arguments))))
16001 return template_id;
16002 else if (variable_template_p (templ))
16004 template_id = lookup_template_variable (templ, arguments);
16005 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16006 SET_EXPR_LOCATION (template_id, combined_loc);
16008 else
16010 /* If it's not a class-template or a template-template, it should be
16011 a function-template. */
16012 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16013 || TREE_CODE (templ) == OVERLOAD
16014 || BASELINK_P (templ)));
16016 template_id = lookup_template_function (templ, arguments);
16017 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16018 SET_EXPR_LOCATION (template_id, combined_loc);
16021 /* If parsing tentatively, replace the sequence of tokens that makes
16022 up the template-id with a CPP_TEMPLATE_ID token. That way,
16023 should we re-parse the token stream, we will not have to repeat
16024 the effort required to do the parse, nor will we issue duplicate
16025 error messages about problems during instantiation of the
16026 template. */
16027 if (start_of_id
16028 /* Don't do this if we had a parse error in a declarator; re-parsing
16029 might succeed if a name changes meaning (60361). */
16030 && !(cp_parser_error_occurred (parser)
16031 && cp_parser_parsing_tentatively (parser)
16032 && parser->in_declarator_p))
16034 /* Reset the contents of the START_OF_ID token. */
16035 token->type = CPP_TEMPLATE_ID;
16036 token->location = combined_loc;
16038 /* Retrieve any deferred checks. Do not pop this access checks yet
16039 so the memory will not be reclaimed during token replacing below. */
16040 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16041 token->u.tree_check_value->value = template_id;
16042 token->u.tree_check_value->checks = get_deferred_access_checks ();
16043 token->keyword = RID_MAX;
16045 /* Purge all subsequent tokens. */
16046 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16048 /* ??? Can we actually assume that, if template_id ==
16049 error_mark_node, we will have issued a diagnostic to the
16050 user, as opposed to simply marking the tentative parse as
16051 failed? */
16052 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16053 error_at (token->location, "parse error in template argument list");
16056 pop_to_parent_deferring_access_checks ();
16057 return template_id;
16060 /* Parse a template-name.
16062 template-name:
16063 identifier
16065 The standard should actually say:
16067 template-name:
16068 identifier
16069 operator-function-id
16071 A defect report has been filed about this issue.
16073 A conversion-function-id cannot be a template name because they cannot
16074 be part of a template-id. In fact, looking at this code:
16076 a.operator K<int>()
16078 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16079 It is impossible to call a templated conversion-function-id with an
16080 explicit argument list, since the only allowed template parameter is
16081 the type to which it is converting.
16083 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16084 `template' keyword, in a construction like:
16086 T::template f<3>()
16088 In that case `f' is taken to be a template-name, even though there
16089 is no way of knowing for sure.
16091 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16092 name refers to a set of overloaded functions, at least one of which
16093 is a template, or an IDENTIFIER_NODE with the name of the template,
16094 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16095 names are looked up inside uninstantiated templates. */
16097 static tree
16098 cp_parser_template_name (cp_parser* parser,
16099 bool template_keyword_p,
16100 bool check_dependency_p,
16101 bool is_declaration,
16102 enum tag_types tag_type,
16103 bool *is_identifier)
16105 tree identifier;
16106 tree decl;
16107 cp_token *token = cp_lexer_peek_token (parser->lexer);
16109 /* If the next token is `operator', then we have either an
16110 operator-function-id or a conversion-function-id. */
16111 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16113 /* We don't know whether we're looking at an
16114 operator-function-id or a conversion-function-id. */
16115 cp_parser_parse_tentatively (parser);
16116 /* Try an operator-function-id. */
16117 identifier = cp_parser_operator_function_id (parser);
16118 /* If that didn't work, try a conversion-function-id. */
16119 if (!cp_parser_parse_definitely (parser))
16121 cp_parser_error (parser, "expected template-name");
16122 return error_mark_node;
16125 /* Look for the identifier. */
16126 else
16127 identifier = cp_parser_identifier (parser);
16129 /* If we didn't find an identifier, we don't have a template-id. */
16130 if (identifier == error_mark_node)
16131 return error_mark_node;
16133 /* If the name immediately followed the `template' keyword, then it
16134 is a template-name. However, if the next token is not `<', then
16135 we do not treat it as a template-name, since it is not being used
16136 as part of a template-id. This enables us to handle constructs
16137 like:
16139 template <typename T> struct S { S(); };
16140 template <typename T> S<T>::S();
16142 correctly. We would treat `S' as a template -- if it were `S<T>'
16143 -- but we do not if there is no `<'. */
16145 if (processing_template_decl
16146 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16148 /* In a declaration, in a dependent context, we pretend that the
16149 "template" keyword was present in order to improve error
16150 recovery. For example, given:
16152 template <typename T> void f(T::X<int>);
16154 we want to treat "X<int>" as a template-id. */
16155 if (is_declaration
16156 && !template_keyword_p
16157 && parser->scope && TYPE_P (parser->scope)
16158 && check_dependency_p
16159 && dependent_scope_p (parser->scope)
16160 /* Do not do this for dtors (or ctors), since they never
16161 need the template keyword before their name. */
16162 && !constructor_name_p (identifier, parser->scope))
16164 cp_token_position start = 0;
16166 /* Explain what went wrong. */
16167 error_at (token->location, "non-template %qD used as template",
16168 identifier);
16169 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16170 parser->scope, identifier);
16171 /* If parsing tentatively, find the location of the "<" token. */
16172 if (cp_parser_simulate_error (parser))
16173 start = cp_lexer_token_position (parser->lexer, true);
16174 /* Parse the template arguments so that we can issue error
16175 messages about them. */
16176 cp_lexer_consume_token (parser->lexer);
16177 cp_parser_enclosed_template_argument_list (parser);
16178 /* Skip tokens until we find a good place from which to
16179 continue parsing. */
16180 cp_parser_skip_to_closing_parenthesis (parser,
16181 /*recovering=*/true,
16182 /*or_comma=*/true,
16183 /*consume_paren=*/false);
16184 /* If parsing tentatively, permanently remove the
16185 template argument list. That will prevent duplicate
16186 error messages from being issued about the missing
16187 "template" keyword. */
16188 if (start)
16189 cp_lexer_purge_tokens_after (parser->lexer, start);
16190 if (is_identifier)
16191 *is_identifier = true;
16192 parser->context->object_type = NULL_TREE;
16193 return identifier;
16196 /* If the "template" keyword is present, then there is generally
16197 no point in doing name-lookup, so we just return IDENTIFIER.
16198 But, if the qualifying scope is non-dependent then we can
16199 (and must) do name-lookup normally. */
16200 if (template_keyword_p)
16202 tree scope = (parser->scope ? parser->scope
16203 : parser->context->object_type);
16204 if (scope && TYPE_P (scope)
16205 && (!CLASS_TYPE_P (scope)
16206 || (check_dependency_p && dependent_type_p (scope))))
16208 /* We're optimizing away the call to cp_parser_lookup_name, but
16209 we still need to do this. */
16210 parser->context->object_type = NULL_TREE;
16211 return identifier;
16216 /* Look up the name. */
16217 decl = cp_parser_lookup_name (parser, identifier,
16218 tag_type,
16219 /*is_template=*/true,
16220 /*is_namespace=*/false,
16221 check_dependency_p,
16222 /*ambiguous_decls=*/NULL,
16223 token->location);
16225 decl = strip_using_decl (decl);
16227 /* If DECL is a template, then the name was a template-name. */
16228 if (TREE_CODE (decl) == TEMPLATE_DECL)
16230 if (TREE_DEPRECATED (decl)
16231 && deprecated_state != DEPRECATED_SUPPRESS)
16232 warn_deprecated_use (decl, NULL_TREE);
16234 else
16236 /* The standard does not explicitly indicate whether a name that
16237 names a set of overloaded declarations, some of which are
16238 templates, is a template-name. However, such a name should
16239 be a template-name; otherwise, there is no way to form a
16240 template-id for the overloaded templates. */
16241 bool found = false;
16243 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16244 !found && iter; ++iter)
16245 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16246 found = true;
16248 if (!found)
16250 /* The name does not name a template. */
16251 cp_parser_error (parser, "expected template-name");
16252 return error_mark_node;
16256 /* If DECL is dependent, and refers to a function, then just return
16257 its name; we will look it up again during template instantiation. */
16258 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16260 tree scope = ovl_scope (decl);
16261 if (TYPE_P (scope) && dependent_type_p (scope))
16262 return identifier;
16265 return decl;
16268 /* Parse a template-argument-list.
16270 template-argument-list:
16271 template-argument ... [opt]
16272 template-argument-list , template-argument ... [opt]
16274 Returns a TREE_VEC containing the arguments. */
16276 static tree
16277 cp_parser_template_argument_list (cp_parser* parser)
16279 tree fixed_args[10];
16280 unsigned n_args = 0;
16281 unsigned alloced = 10;
16282 tree *arg_ary = fixed_args;
16283 tree vec;
16284 bool saved_in_template_argument_list_p;
16285 bool saved_ice_p;
16286 bool saved_non_ice_p;
16288 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16289 parser->in_template_argument_list_p = true;
16290 /* Even if the template-id appears in an integral
16291 constant-expression, the contents of the argument list do
16292 not. */
16293 saved_ice_p = parser->integral_constant_expression_p;
16294 parser->integral_constant_expression_p = false;
16295 saved_non_ice_p = parser->non_integral_constant_expression_p;
16296 parser->non_integral_constant_expression_p = false;
16298 /* Parse the arguments. */
16301 tree argument;
16303 if (n_args)
16304 /* Consume the comma. */
16305 cp_lexer_consume_token (parser->lexer);
16307 /* Parse the template-argument. */
16308 argument = cp_parser_template_argument (parser);
16310 /* If the next token is an ellipsis, we're expanding a template
16311 argument pack. */
16312 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16314 if (argument == error_mark_node)
16316 cp_token *token = cp_lexer_peek_token (parser->lexer);
16317 error_at (token->location,
16318 "expected parameter pack before %<...%>");
16320 /* Consume the `...' token. */
16321 cp_lexer_consume_token (parser->lexer);
16323 /* Make the argument into a TYPE_PACK_EXPANSION or
16324 EXPR_PACK_EXPANSION. */
16325 argument = make_pack_expansion (argument);
16328 if (n_args == alloced)
16330 alloced *= 2;
16332 if (arg_ary == fixed_args)
16334 arg_ary = XNEWVEC (tree, alloced);
16335 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16337 else
16338 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16340 arg_ary[n_args++] = argument;
16342 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16344 vec = make_tree_vec (n_args);
16346 while (n_args--)
16347 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16349 if (arg_ary != fixed_args)
16350 free (arg_ary);
16351 parser->non_integral_constant_expression_p = saved_non_ice_p;
16352 parser->integral_constant_expression_p = saved_ice_p;
16353 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16354 if (CHECKING_P)
16355 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16356 return vec;
16359 /* Parse a template-argument.
16361 template-argument:
16362 assignment-expression
16363 type-id
16364 id-expression
16366 The representation is that of an assignment-expression, type-id, or
16367 id-expression -- except that the qualified id-expression is
16368 evaluated, so that the value returned is either a DECL or an
16369 OVERLOAD.
16371 Although the standard says "assignment-expression", it forbids
16372 throw-expressions or assignments in the template argument.
16373 Therefore, we use "conditional-expression" instead. */
16375 static tree
16376 cp_parser_template_argument (cp_parser* parser)
16378 tree argument;
16379 bool template_p;
16380 bool address_p;
16381 bool maybe_type_id = false;
16382 cp_token *token = NULL, *argument_start_token = NULL;
16383 location_t loc = 0;
16384 cp_id_kind idk;
16386 /* There's really no way to know what we're looking at, so we just
16387 try each alternative in order.
16389 [temp.arg]
16391 In a template-argument, an ambiguity between a type-id and an
16392 expression is resolved to a type-id, regardless of the form of
16393 the corresponding template-parameter.
16395 Therefore, we try a type-id first. */
16396 cp_parser_parse_tentatively (parser);
16397 argument = cp_parser_template_type_arg (parser);
16398 /* If there was no error parsing the type-id but the next token is a
16399 '>>', our behavior depends on which dialect of C++ we're
16400 parsing. In C++98, we probably found a typo for '> >'. But there
16401 are type-id which are also valid expressions. For instance:
16403 struct X { int operator >> (int); };
16404 template <int V> struct Foo {};
16405 Foo<X () >> 5> r;
16407 Here 'X()' is a valid type-id of a function type, but the user just
16408 wanted to write the expression "X() >> 5". Thus, we remember that we
16409 found a valid type-id, but we still try to parse the argument as an
16410 expression to see what happens.
16412 In C++0x, the '>>' will be considered two separate '>'
16413 tokens. */
16414 if (!cp_parser_error_occurred (parser)
16415 && cxx_dialect == cxx98
16416 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16418 maybe_type_id = true;
16419 cp_parser_abort_tentative_parse (parser);
16421 else
16423 /* If the next token isn't a `,' or a `>', then this argument wasn't
16424 really finished. This means that the argument is not a valid
16425 type-id. */
16426 if (!cp_parser_next_token_ends_template_argument_p (parser))
16427 cp_parser_error (parser, "expected template-argument");
16428 /* If that worked, we're done. */
16429 if (cp_parser_parse_definitely (parser))
16430 return argument;
16432 /* We're still not sure what the argument will be. */
16433 cp_parser_parse_tentatively (parser);
16434 /* Try a template. */
16435 argument_start_token = cp_lexer_peek_token (parser->lexer);
16436 argument = cp_parser_id_expression (parser,
16437 /*template_keyword_p=*/false,
16438 /*check_dependency_p=*/true,
16439 &template_p,
16440 /*declarator_p=*/false,
16441 /*optional_p=*/false);
16442 /* If the next token isn't a `,' or a `>', then this argument wasn't
16443 really finished. */
16444 if (!cp_parser_next_token_ends_template_argument_p (parser))
16445 cp_parser_error (parser, "expected template-argument");
16446 if (!cp_parser_error_occurred (parser))
16448 /* Figure out what is being referred to. If the id-expression
16449 was for a class template specialization, then we will have a
16450 TYPE_DECL at this point. There is no need to do name lookup
16451 at this point in that case. */
16452 if (TREE_CODE (argument) != TYPE_DECL)
16453 argument = cp_parser_lookup_name (parser, argument,
16454 none_type,
16455 /*is_template=*/template_p,
16456 /*is_namespace=*/false,
16457 /*check_dependency=*/true,
16458 /*ambiguous_decls=*/NULL,
16459 argument_start_token->location);
16460 /* Handle a constrained-type-specifier for a non-type template
16461 parameter. */
16462 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16463 argument = decl;
16464 else if (TREE_CODE (argument) != TEMPLATE_DECL
16465 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16466 cp_parser_error (parser, "expected template-name");
16468 if (cp_parser_parse_definitely (parser))
16470 if (TREE_DEPRECATED (argument))
16471 warn_deprecated_use (argument, NULL_TREE);
16472 return argument;
16474 /* It must be a non-type argument. In C++17 any constant-expression is
16475 allowed. */
16476 if (cxx_dialect > cxx14)
16477 goto general_expr;
16479 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16481 -- an integral constant-expression of integral or enumeration
16482 type; or
16484 -- the name of a non-type template-parameter; or
16486 -- the name of an object or function with external linkage...
16488 -- the address of an object or function with external linkage...
16490 -- a pointer to member... */
16491 /* Look for a non-type template parameter. */
16492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16494 cp_parser_parse_tentatively (parser);
16495 argument = cp_parser_primary_expression (parser,
16496 /*address_p=*/false,
16497 /*cast_p=*/false,
16498 /*template_arg_p=*/true,
16499 &idk);
16500 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16501 || !cp_parser_next_token_ends_template_argument_p (parser))
16502 cp_parser_simulate_error (parser);
16503 if (cp_parser_parse_definitely (parser))
16504 return argument;
16507 /* If the next token is "&", the argument must be the address of an
16508 object or function with external linkage. */
16509 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16510 if (address_p)
16512 loc = cp_lexer_peek_token (parser->lexer)->location;
16513 cp_lexer_consume_token (parser->lexer);
16515 /* See if we might have an id-expression. */
16516 token = cp_lexer_peek_token (parser->lexer);
16517 if (token->type == CPP_NAME
16518 || token->keyword == RID_OPERATOR
16519 || token->type == CPP_SCOPE
16520 || token->type == CPP_TEMPLATE_ID
16521 || token->type == CPP_NESTED_NAME_SPECIFIER)
16523 cp_parser_parse_tentatively (parser);
16524 argument = cp_parser_primary_expression (parser,
16525 address_p,
16526 /*cast_p=*/false,
16527 /*template_arg_p=*/true,
16528 &idk);
16529 if (cp_parser_error_occurred (parser)
16530 || !cp_parser_next_token_ends_template_argument_p (parser))
16531 cp_parser_abort_tentative_parse (parser);
16532 else
16534 tree probe;
16536 if (INDIRECT_REF_P (argument))
16538 /* Strip the dereference temporarily. */
16539 gcc_assert (REFERENCE_REF_P (argument));
16540 argument = TREE_OPERAND (argument, 0);
16543 /* If we're in a template, we represent a qualified-id referring
16544 to a static data member as a SCOPE_REF even if the scope isn't
16545 dependent so that we can check access control later. */
16546 probe = argument;
16547 if (TREE_CODE (probe) == SCOPE_REF)
16548 probe = TREE_OPERAND (probe, 1);
16549 if (VAR_P (probe))
16551 /* A variable without external linkage might still be a
16552 valid constant-expression, so no error is issued here
16553 if the external-linkage check fails. */
16554 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16555 cp_parser_simulate_error (parser);
16557 else if (is_overloaded_fn (argument))
16558 /* All overloaded functions are allowed; if the external
16559 linkage test does not pass, an error will be issued
16560 later. */
16562 else if (address_p
16563 && (TREE_CODE (argument) == OFFSET_REF
16564 || TREE_CODE (argument) == SCOPE_REF))
16565 /* A pointer-to-member. */
16567 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16569 else
16570 cp_parser_simulate_error (parser);
16572 if (cp_parser_parse_definitely (parser))
16574 if (address_p)
16575 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16576 tf_warning_or_error);
16577 else
16578 argument = convert_from_reference (argument);
16579 return argument;
16583 /* If the argument started with "&", there are no other valid
16584 alternatives at this point. */
16585 if (address_p)
16587 cp_parser_error (parser, "invalid non-type template argument");
16588 return error_mark_node;
16591 general_expr:
16592 /* If the argument wasn't successfully parsed as a type-id followed
16593 by '>>', the argument can only be a constant expression now.
16594 Otherwise, we try parsing the constant-expression tentatively,
16595 because the argument could really be a type-id. */
16596 if (maybe_type_id)
16597 cp_parser_parse_tentatively (parser);
16599 if (cxx_dialect <= cxx14)
16600 argument = cp_parser_constant_expression (parser);
16601 else
16603 /* With C++17 generalized non-type template arguments we need to handle
16604 lvalue constant expressions, too. */
16605 argument = cp_parser_assignment_expression (parser);
16606 require_potential_constant_expression (argument);
16609 if (!maybe_type_id)
16610 return argument;
16611 if (!cp_parser_next_token_ends_template_argument_p (parser))
16612 cp_parser_error (parser, "expected template-argument");
16613 if (cp_parser_parse_definitely (parser))
16614 return argument;
16615 /* We did our best to parse the argument as a non type-id, but that
16616 was the only alternative that matched (albeit with a '>' after
16617 it). We can assume it's just a typo from the user, and a
16618 diagnostic will then be issued. */
16619 return cp_parser_template_type_arg (parser);
16622 /* Parse an explicit-instantiation.
16624 explicit-instantiation:
16625 template declaration
16627 Although the standard says `declaration', what it really means is:
16629 explicit-instantiation:
16630 template decl-specifier-seq [opt] declarator [opt] ;
16632 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16633 supposed to be allowed. A defect report has been filed about this
16634 issue.
16636 GNU Extension:
16638 explicit-instantiation:
16639 storage-class-specifier template
16640 decl-specifier-seq [opt] declarator [opt] ;
16641 function-specifier template
16642 decl-specifier-seq [opt] declarator [opt] ; */
16644 static void
16645 cp_parser_explicit_instantiation (cp_parser* parser)
16647 int declares_class_or_enum;
16648 cp_decl_specifier_seq decl_specifiers;
16649 tree extension_specifier = NULL_TREE;
16651 timevar_push (TV_TEMPLATE_INST);
16653 /* Look for an (optional) storage-class-specifier or
16654 function-specifier. */
16655 if (cp_parser_allow_gnu_extensions_p (parser))
16657 extension_specifier
16658 = cp_parser_storage_class_specifier_opt (parser);
16659 if (!extension_specifier)
16660 extension_specifier
16661 = cp_parser_function_specifier_opt (parser,
16662 /*decl_specs=*/NULL);
16665 /* Look for the `template' keyword. */
16666 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16667 /* Let the front end know that we are processing an explicit
16668 instantiation. */
16669 begin_explicit_instantiation ();
16670 /* [temp.explicit] says that we are supposed to ignore access
16671 control while processing explicit instantiation directives. */
16672 push_deferring_access_checks (dk_no_check);
16673 /* Parse a decl-specifier-seq. */
16674 cp_parser_decl_specifier_seq (parser,
16675 CP_PARSER_FLAGS_OPTIONAL,
16676 &decl_specifiers,
16677 &declares_class_or_enum);
16678 /* If there was exactly one decl-specifier, and it declared a class,
16679 and there's no declarator, then we have an explicit type
16680 instantiation. */
16681 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16683 tree type;
16685 type = check_tag_decl (&decl_specifiers,
16686 /*explicit_type_instantiation_p=*/true);
16687 /* Turn access control back on for names used during
16688 template instantiation. */
16689 pop_deferring_access_checks ();
16690 if (type)
16691 do_type_instantiation (type, extension_specifier,
16692 /*complain=*/tf_error);
16694 else
16696 cp_declarator *declarator;
16697 tree decl;
16699 /* Parse the declarator. */
16700 declarator
16701 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16702 /*ctor_dtor_or_conv_p=*/NULL,
16703 /*parenthesized_p=*/NULL,
16704 /*member_p=*/false,
16705 /*friend_p=*/false);
16706 if (declares_class_or_enum & 2)
16707 cp_parser_check_for_definition_in_return_type (declarator,
16708 decl_specifiers.type,
16709 decl_specifiers.locations[ds_type_spec]);
16710 if (declarator != cp_error_declarator)
16712 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16713 permerror (decl_specifiers.locations[ds_inline],
16714 "explicit instantiation shall not use"
16715 " %<inline%> specifier");
16716 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16717 permerror (decl_specifiers.locations[ds_constexpr],
16718 "explicit instantiation shall not use"
16719 " %<constexpr%> specifier");
16721 decl = grokdeclarator (declarator, &decl_specifiers,
16722 NORMAL, 0, &decl_specifiers.attributes);
16723 /* Turn access control back on for names used during
16724 template instantiation. */
16725 pop_deferring_access_checks ();
16726 /* Do the explicit instantiation. */
16727 do_decl_instantiation (decl, extension_specifier);
16729 else
16731 pop_deferring_access_checks ();
16732 /* Skip the body of the explicit instantiation. */
16733 cp_parser_skip_to_end_of_statement (parser);
16736 /* We're done with the instantiation. */
16737 end_explicit_instantiation ();
16739 cp_parser_consume_semicolon_at_end_of_statement (parser);
16741 timevar_pop (TV_TEMPLATE_INST);
16744 /* Parse an explicit-specialization.
16746 explicit-specialization:
16747 template < > declaration
16749 Although the standard says `declaration', what it really means is:
16751 explicit-specialization:
16752 template <> decl-specifier [opt] init-declarator [opt] ;
16753 template <> function-definition
16754 template <> explicit-specialization
16755 template <> template-declaration */
16757 static void
16758 cp_parser_explicit_specialization (cp_parser* parser)
16760 bool need_lang_pop;
16761 cp_token *token = cp_lexer_peek_token (parser->lexer);
16763 /* Look for the `template' keyword. */
16764 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16765 /* Look for the `<'. */
16766 cp_parser_require (parser, CPP_LESS, RT_LESS);
16767 /* Look for the `>'. */
16768 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16769 /* We have processed another parameter list. */
16770 ++parser->num_template_parameter_lists;
16771 /* [temp]
16773 A template ... explicit specialization ... shall not have C
16774 linkage. */
16775 if (current_lang_name == lang_name_c)
16777 error_at (token->location, "template specialization with C linkage");
16778 maybe_show_extern_c_location ();
16779 /* Give it C++ linkage to avoid confusing other parts of the
16780 front end. */
16781 push_lang_context (lang_name_cplusplus);
16782 need_lang_pop = true;
16784 else
16785 need_lang_pop = false;
16786 /* Let the front end know that we are beginning a specialization. */
16787 if (!begin_specialization ())
16789 end_specialization ();
16790 return;
16793 /* If the next keyword is `template', we need to figure out whether
16794 or not we're looking a template-declaration. */
16795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16797 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16798 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16799 cp_parser_template_declaration_after_export (parser,
16800 /*member_p=*/false);
16801 else
16802 cp_parser_explicit_specialization (parser);
16804 else
16805 /* Parse the dependent declaration. */
16806 cp_parser_single_declaration (parser,
16807 /*checks=*/NULL,
16808 /*member_p=*/false,
16809 /*explicit_specialization_p=*/true,
16810 /*friend_p=*/NULL);
16811 /* We're done with the specialization. */
16812 end_specialization ();
16813 /* For the erroneous case of a template with C linkage, we pushed an
16814 implicit C++ linkage scope; exit that scope now. */
16815 if (need_lang_pop)
16816 pop_lang_context ();
16817 /* We're done with this parameter list. */
16818 --parser->num_template_parameter_lists;
16821 /* Parse a type-specifier.
16823 type-specifier:
16824 simple-type-specifier
16825 class-specifier
16826 enum-specifier
16827 elaborated-type-specifier
16828 cv-qualifier
16830 GNU Extension:
16832 type-specifier:
16833 __complex__
16835 Returns a representation of the type-specifier. For a
16836 class-specifier, enum-specifier, or elaborated-type-specifier, a
16837 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16839 The parser flags FLAGS is used to control type-specifier parsing.
16841 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16842 in a decl-specifier-seq.
16844 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16845 class-specifier, enum-specifier, or elaborated-type-specifier, then
16846 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16847 if a type is declared; 2 if it is defined. Otherwise, it is set to
16848 zero.
16850 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16851 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16852 is set to FALSE. */
16854 static tree
16855 cp_parser_type_specifier (cp_parser* parser,
16856 cp_parser_flags flags,
16857 cp_decl_specifier_seq *decl_specs,
16858 bool is_declaration,
16859 int* declares_class_or_enum,
16860 bool* is_cv_qualifier)
16862 tree type_spec = NULL_TREE;
16863 cp_token *token;
16864 enum rid keyword;
16865 cp_decl_spec ds = ds_last;
16867 /* Assume this type-specifier does not declare a new type. */
16868 if (declares_class_or_enum)
16869 *declares_class_or_enum = 0;
16870 /* And that it does not specify a cv-qualifier. */
16871 if (is_cv_qualifier)
16872 *is_cv_qualifier = false;
16873 /* Peek at the next token. */
16874 token = cp_lexer_peek_token (parser->lexer);
16876 /* If we're looking at a keyword, we can use that to guide the
16877 production we choose. */
16878 keyword = token->keyword;
16879 switch (keyword)
16881 case RID_ENUM:
16882 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16883 goto elaborated_type_specifier;
16885 /* Look for the enum-specifier. */
16886 type_spec = cp_parser_enum_specifier (parser);
16887 /* If that worked, we're done. */
16888 if (type_spec)
16890 if (declares_class_or_enum)
16891 *declares_class_or_enum = 2;
16892 if (decl_specs)
16893 cp_parser_set_decl_spec_type (decl_specs,
16894 type_spec,
16895 token,
16896 /*type_definition_p=*/true);
16897 return type_spec;
16899 else
16900 goto elaborated_type_specifier;
16902 /* Any of these indicate either a class-specifier, or an
16903 elaborated-type-specifier. */
16904 case RID_CLASS:
16905 case RID_STRUCT:
16906 case RID_UNION:
16907 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16908 goto elaborated_type_specifier;
16910 /* Parse tentatively so that we can back up if we don't find a
16911 class-specifier. */
16912 cp_parser_parse_tentatively (parser);
16913 /* Look for the class-specifier. */
16914 type_spec = cp_parser_class_specifier (parser);
16915 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16916 /* If that worked, we're done. */
16917 if (cp_parser_parse_definitely (parser))
16919 if (declares_class_or_enum)
16920 *declares_class_or_enum = 2;
16921 if (decl_specs)
16922 cp_parser_set_decl_spec_type (decl_specs,
16923 type_spec,
16924 token,
16925 /*type_definition_p=*/true);
16926 return type_spec;
16929 /* Fall through. */
16930 elaborated_type_specifier:
16931 /* We're declaring (not defining) a class or enum. */
16932 if (declares_class_or_enum)
16933 *declares_class_or_enum = 1;
16935 /* Fall through. */
16936 case RID_TYPENAME:
16937 /* Look for an elaborated-type-specifier. */
16938 type_spec
16939 = (cp_parser_elaborated_type_specifier
16940 (parser,
16941 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16942 is_declaration));
16943 if (decl_specs)
16944 cp_parser_set_decl_spec_type (decl_specs,
16945 type_spec,
16946 token,
16947 /*type_definition_p=*/false);
16948 return type_spec;
16950 case RID_CONST:
16951 ds = ds_const;
16952 if (is_cv_qualifier)
16953 *is_cv_qualifier = true;
16954 break;
16956 case RID_VOLATILE:
16957 ds = ds_volatile;
16958 if (is_cv_qualifier)
16959 *is_cv_qualifier = true;
16960 break;
16962 case RID_RESTRICT:
16963 ds = ds_restrict;
16964 if (is_cv_qualifier)
16965 *is_cv_qualifier = true;
16966 break;
16968 case RID_COMPLEX:
16969 /* The `__complex__' keyword is a GNU extension. */
16970 ds = ds_complex;
16971 break;
16973 default:
16974 break;
16977 /* Handle simple keywords. */
16978 if (ds != ds_last)
16980 if (decl_specs)
16982 set_and_check_decl_spec_loc (decl_specs, ds, token);
16983 decl_specs->any_specifiers_p = true;
16985 return cp_lexer_consume_token (parser->lexer)->u.value;
16988 /* If we do not already have a type-specifier, assume we are looking
16989 at a simple-type-specifier. */
16990 type_spec = cp_parser_simple_type_specifier (parser,
16991 decl_specs,
16992 flags);
16994 /* If we didn't find a type-specifier, and a type-specifier was not
16995 optional in this context, issue an error message. */
16996 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16998 cp_parser_error (parser, "expected type specifier");
16999 return error_mark_node;
17002 return type_spec;
17005 /* Parse a simple-type-specifier.
17007 simple-type-specifier:
17008 :: [opt] nested-name-specifier [opt] type-name
17009 :: [opt] nested-name-specifier template template-id
17010 char
17011 wchar_t
17012 bool
17013 short
17015 long
17016 signed
17017 unsigned
17018 float
17019 double
17020 void
17022 C++11 Extension:
17024 simple-type-specifier:
17025 auto
17026 decltype ( expression )
17027 char16_t
17028 char32_t
17029 __underlying_type ( type-id )
17031 C++17 extension:
17033 nested-name-specifier(opt) template-name
17035 GNU Extension:
17037 simple-type-specifier:
17038 __int128
17039 __typeof__ unary-expression
17040 __typeof__ ( type-id )
17041 __typeof__ ( type-id ) { initializer-list , [opt] }
17043 Concepts Extension:
17045 simple-type-specifier:
17046 constrained-type-specifier
17048 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17049 appropriately updated. */
17051 static tree
17052 cp_parser_simple_type_specifier (cp_parser* parser,
17053 cp_decl_specifier_seq *decl_specs,
17054 cp_parser_flags flags)
17056 tree type = NULL_TREE;
17057 cp_token *token;
17058 int idx;
17060 /* Peek at the next token. */
17061 token = cp_lexer_peek_token (parser->lexer);
17063 /* If we're looking at a keyword, things are easy. */
17064 switch (token->keyword)
17066 case RID_CHAR:
17067 if (decl_specs)
17068 decl_specs->explicit_char_p = true;
17069 type = char_type_node;
17070 break;
17071 case RID_CHAR16:
17072 type = char16_type_node;
17073 break;
17074 case RID_CHAR32:
17075 type = char32_type_node;
17076 break;
17077 case RID_WCHAR:
17078 type = wchar_type_node;
17079 break;
17080 case RID_BOOL:
17081 type = boolean_type_node;
17082 break;
17083 case RID_SHORT:
17084 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17085 type = short_integer_type_node;
17086 break;
17087 case RID_INT:
17088 if (decl_specs)
17089 decl_specs->explicit_int_p = true;
17090 type = integer_type_node;
17091 break;
17092 case RID_INT_N_0:
17093 case RID_INT_N_1:
17094 case RID_INT_N_2:
17095 case RID_INT_N_3:
17096 idx = token->keyword - RID_INT_N_0;
17097 if (! int_n_enabled_p [idx])
17098 break;
17099 if (decl_specs)
17101 decl_specs->explicit_intN_p = true;
17102 decl_specs->int_n_idx = idx;
17104 type = int_n_trees [idx].signed_type;
17105 break;
17106 case RID_LONG:
17107 if (decl_specs)
17108 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17109 type = long_integer_type_node;
17110 break;
17111 case RID_SIGNED:
17112 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17113 type = integer_type_node;
17114 break;
17115 case RID_UNSIGNED:
17116 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17117 type = unsigned_type_node;
17118 break;
17119 case RID_FLOAT:
17120 type = float_type_node;
17121 break;
17122 case RID_DOUBLE:
17123 type = double_type_node;
17124 break;
17125 case RID_VOID:
17126 type = void_type_node;
17127 break;
17129 case RID_AUTO:
17130 maybe_warn_cpp0x (CPP0X_AUTO);
17131 if (parser->auto_is_implicit_function_template_parm_p)
17133 /* The 'auto' might be the placeholder return type for a function decl
17134 with trailing return type. */
17135 bool have_trailing_return_fn_decl = false;
17137 cp_parser_parse_tentatively (parser);
17138 cp_lexer_consume_token (parser->lexer);
17139 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17140 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17141 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17142 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17146 cp_lexer_consume_token (parser->lexer);
17147 cp_parser_skip_to_closing_parenthesis (parser,
17148 /*recovering*/false,
17149 /*or_comma*/false,
17150 /*consume_paren*/true);
17151 continue;
17154 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17156 have_trailing_return_fn_decl = true;
17157 break;
17160 cp_lexer_consume_token (parser->lexer);
17162 cp_parser_abort_tentative_parse (parser);
17164 if (have_trailing_return_fn_decl)
17166 type = make_auto ();
17167 break;
17170 if (cxx_dialect >= cxx14)
17172 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17173 type = TREE_TYPE (type);
17175 else
17176 type = error_mark_node;
17178 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17180 if (cxx_dialect < cxx14)
17181 error_at (token->location,
17182 "use of %<auto%> in lambda parameter declaration "
17183 "only available with "
17184 "-std=c++14 or -std=gnu++14");
17186 else if (cxx_dialect < cxx14)
17187 error_at (token->location,
17188 "use of %<auto%> in parameter declaration "
17189 "only available with "
17190 "-std=c++14 or -std=gnu++14");
17191 else if (!flag_concepts)
17192 pedwarn (token->location, 0,
17193 "use of %<auto%> in parameter declaration "
17194 "only available with -fconcepts");
17196 else
17197 type = make_auto ();
17198 break;
17200 case RID_DECLTYPE:
17201 /* Since DR 743, decltype can either be a simple-type-specifier by
17202 itself or begin a nested-name-specifier. Parsing it will replace
17203 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17204 handling below decide what to do. */
17205 cp_parser_decltype (parser);
17206 cp_lexer_set_token_position (parser->lexer, token);
17207 break;
17209 case RID_TYPEOF:
17210 /* Consume the `typeof' token. */
17211 cp_lexer_consume_token (parser->lexer);
17212 /* Parse the operand to `typeof'. */
17213 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17214 /* If it is not already a TYPE, take its type. */
17215 if (!TYPE_P (type))
17216 type = finish_typeof (type);
17218 if (decl_specs)
17219 cp_parser_set_decl_spec_type (decl_specs, type,
17220 token,
17221 /*type_definition_p=*/false);
17223 return type;
17225 case RID_UNDERLYING_TYPE:
17226 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17227 if (decl_specs)
17228 cp_parser_set_decl_spec_type (decl_specs, type,
17229 token,
17230 /*type_definition_p=*/false);
17232 return type;
17234 case RID_BASES:
17235 case RID_DIRECT_BASES:
17236 type = cp_parser_trait_expr (parser, token->keyword);
17237 if (decl_specs)
17238 cp_parser_set_decl_spec_type (decl_specs, type,
17239 token,
17240 /*type_definition_p=*/false);
17241 return type;
17242 default:
17243 break;
17246 /* If token is an already-parsed decltype not followed by ::,
17247 it's a simple-type-specifier. */
17248 if (token->type == CPP_DECLTYPE
17249 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17251 type = saved_checks_value (token->u.tree_check_value);
17252 if (decl_specs)
17254 cp_parser_set_decl_spec_type (decl_specs, type,
17255 token,
17256 /*type_definition_p=*/false);
17257 /* Remember that we are handling a decltype in order to
17258 implement the resolution of DR 1510 when the argument
17259 isn't instantiation dependent. */
17260 decl_specs->decltype_p = true;
17262 cp_lexer_consume_token (parser->lexer);
17263 return type;
17266 /* If the type-specifier was for a built-in type, we're done. */
17267 if (type)
17269 /* Record the type. */
17270 if (decl_specs
17271 && (token->keyword != RID_SIGNED
17272 && token->keyword != RID_UNSIGNED
17273 && token->keyword != RID_SHORT
17274 && token->keyword != RID_LONG))
17275 cp_parser_set_decl_spec_type (decl_specs,
17276 type,
17277 token,
17278 /*type_definition_p=*/false);
17279 if (decl_specs)
17280 decl_specs->any_specifiers_p = true;
17282 /* Consume the token. */
17283 cp_lexer_consume_token (parser->lexer);
17285 if (type == error_mark_node)
17286 return error_mark_node;
17288 /* There is no valid C++ program where a non-template type is
17289 followed by a "<". That usually indicates that the user thought
17290 that the type was a template. */
17291 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17292 token->location);
17294 return TYPE_NAME (type);
17297 /* The type-specifier must be a user-defined type. */
17298 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17300 bool qualified_p;
17301 bool global_p;
17303 /* Don't gobble tokens or issue error messages if this is an
17304 optional type-specifier. */
17305 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17306 cp_parser_parse_tentatively (parser);
17308 token = cp_lexer_peek_token (parser->lexer);
17310 /* Look for the optional `::' operator. */
17311 global_p
17312 = (cp_parser_global_scope_opt (parser,
17313 /*current_scope_valid_p=*/false)
17314 != NULL_TREE);
17315 /* Look for the nested-name specifier. */
17316 qualified_p
17317 = (cp_parser_nested_name_specifier_opt (parser,
17318 /*typename_keyword_p=*/false,
17319 /*check_dependency_p=*/true,
17320 /*type_p=*/false,
17321 /*is_declaration=*/false)
17322 != NULL_TREE);
17323 /* If we have seen a nested-name-specifier, and the next token
17324 is `template', then we are using the template-id production. */
17325 if (parser->scope
17326 && cp_parser_optional_template_keyword (parser))
17328 /* Look for the template-id. */
17329 type = cp_parser_template_id (parser,
17330 /*template_keyword_p=*/true,
17331 /*check_dependency_p=*/true,
17332 none_type,
17333 /*is_declaration=*/false);
17334 /* If the template-id did not name a type, we are out of
17335 luck. */
17336 if (TREE_CODE (type) != TYPE_DECL)
17338 cp_parser_error (parser, "expected template-id for type");
17339 type = NULL_TREE;
17342 /* Otherwise, look for a type-name. */
17343 else
17344 type = cp_parser_type_name (parser);
17345 /* Keep track of all name-lookups performed in class scopes. */
17346 if (type
17347 && !global_p
17348 && !qualified_p
17349 && TREE_CODE (type) == TYPE_DECL
17350 && identifier_p (DECL_NAME (type)))
17351 maybe_note_name_used_in_class (DECL_NAME (type), type);
17352 /* If it didn't work out, we don't have a TYPE. */
17353 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17354 && !cp_parser_parse_definitely (parser))
17355 type = NULL_TREE;
17356 if (!type && cxx_dialect >= cxx17)
17358 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17359 cp_parser_parse_tentatively (parser);
17361 cp_parser_global_scope_opt (parser,
17362 /*current_scope_valid_p=*/false);
17363 cp_parser_nested_name_specifier_opt (parser,
17364 /*typename_keyword_p=*/false,
17365 /*check_dependency_p=*/true,
17366 /*type_p=*/false,
17367 /*is_declaration=*/false);
17368 tree name = cp_parser_identifier (parser);
17369 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17370 && parser->scope != error_mark_node)
17372 tree tmpl = cp_parser_lookup_name (parser, name,
17373 none_type,
17374 /*is_template=*/false,
17375 /*is_namespace=*/false,
17376 /*check_dependency=*/true,
17377 /*ambiguous_decls=*/NULL,
17378 token->location);
17379 if (tmpl && tmpl != error_mark_node
17380 && (DECL_CLASS_TEMPLATE_P (tmpl)
17381 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17382 type = make_template_placeholder (tmpl);
17383 else
17385 type = error_mark_node;
17386 if (!cp_parser_simulate_error (parser))
17387 cp_parser_name_lookup_error (parser, name, tmpl,
17388 NLE_TYPE, token->location);
17391 else
17392 type = error_mark_node;
17394 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17395 && !cp_parser_parse_definitely (parser))
17396 type = NULL_TREE;
17398 if (type && decl_specs)
17399 cp_parser_set_decl_spec_type (decl_specs, type,
17400 token,
17401 /*type_definition_p=*/false);
17404 /* If we didn't get a type-name, issue an error message. */
17405 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17407 cp_parser_error (parser, "expected type-name");
17408 return error_mark_node;
17411 if (type && type != error_mark_node)
17413 /* See if TYPE is an Objective-C type, and if so, parse and
17414 accept any protocol references following it. Do this before
17415 the cp_parser_check_for_invalid_template_id() call, because
17416 Objective-C types can be followed by '<...>' which would
17417 enclose protocol names rather than template arguments, and so
17418 everything is fine. */
17419 if (c_dialect_objc () && !parser->scope
17420 && (objc_is_id (type) || objc_is_class_name (type)))
17422 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17423 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17425 /* Clobber the "unqualified" type previously entered into
17426 DECL_SPECS with the new, improved protocol-qualified version. */
17427 if (decl_specs)
17428 decl_specs->type = qual_type;
17430 return qual_type;
17433 /* There is no valid C++ program where a non-template type is
17434 followed by a "<". That usually indicates that the user
17435 thought that the type was a template. */
17436 cp_parser_check_for_invalid_template_id (parser, type,
17437 none_type,
17438 token->location);
17441 return type;
17444 /* Parse a type-name.
17446 type-name:
17447 class-name
17448 enum-name
17449 typedef-name
17450 simple-template-id [in c++0x]
17452 enum-name:
17453 identifier
17455 typedef-name:
17456 identifier
17458 Concepts:
17460 type-name:
17461 concept-name
17462 partial-concept-id
17464 concept-name:
17465 identifier
17467 Returns a TYPE_DECL for the type. */
17469 static tree
17470 cp_parser_type_name (cp_parser* parser)
17472 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17475 /* See above. */
17476 static tree
17477 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17479 tree type_decl;
17481 /* We can't know yet whether it is a class-name or not. */
17482 cp_parser_parse_tentatively (parser);
17483 /* Try a class-name. */
17484 type_decl = cp_parser_class_name (parser,
17485 typename_keyword_p,
17486 /*template_keyword_p=*/false,
17487 none_type,
17488 /*check_dependency_p=*/true,
17489 /*class_head_p=*/false,
17490 /*is_declaration=*/false);
17491 /* If it's not a class-name, keep looking. */
17492 if (!cp_parser_parse_definitely (parser))
17494 if (cxx_dialect < cxx11)
17495 /* It must be a typedef-name or an enum-name. */
17496 return cp_parser_nonclass_name (parser);
17498 cp_parser_parse_tentatively (parser);
17499 /* It is either a simple-template-id representing an
17500 instantiation of an alias template... */
17501 type_decl = cp_parser_template_id (parser,
17502 /*template_keyword_p=*/false,
17503 /*check_dependency_p=*/true,
17504 none_type,
17505 /*is_declaration=*/false);
17506 /* Note that this must be an instantiation of an alias template
17507 because [temp.names]/6 says:
17509 A template-id that names an alias template specialization
17510 is a type-name.
17512 Whereas [temp.names]/7 says:
17514 A simple-template-id that names a class template
17515 specialization is a class-name.
17517 With concepts, this could also be a partial-concept-id that
17518 declares a non-type template parameter. */
17519 if (type_decl != NULL_TREE
17520 && TREE_CODE (type_decl) == TYPE_DECL
17521 && TYPE_DECL_ALIAS_P (type_decl))
17522 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17523 else if (is_constrained_parameter (type_decl))
17524 /* Don't do anything. */ ;
17525 else
17526 cp_parser_simulate_error (parser);
17528 if (!cp_parser_parse_definitely (parser))
17529 /* ... Or a typedef-name or an enum-name. */
17530 return cp_parser_nonclass_name (parser);
17533 return type_decl;
17536 /* Check if DECL and ARGS can form a constrained-type-specifier.
17537 If ARGS is non-null, we try to form a concept check of the
17538 form DECL<?, ARGS> where ? is a wildcard that matches any
17539 kind of template argument. If ARGS is NULL, then we try to
17540 form a concept check of the form DECL<?>. */
17542 static tree
17543 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17544 tree decl, tree args)
17546 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17548 /* If we a constrained-type-specifier cannot be deduced. */
17549 if (parser->prevent_constrained_type_specifiers)
17550 return NULL_TREE;
17552 /* A constrained type specifier can only be found in an
17553 overload set or as a reference to a template declaration.
17555 FIXME: This might be masking a bug. It's possible that
17556 that the deduction below is causing template specializations
17557 to be formed with the wildcard as an argument. */
17558 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17559 return NULL_TREE;
17561 /* Try to build a call expression that evaluates the
17562 concept. This can fail if the overload set refers
17563 only to non-templates. */
17564 tree placeholder = build_nt (WILDCARD_DECL);
17565 tree check = build_concept_check (decl, placeholder, args);
17566 if (check == error_mark_node)
17567 return NULL_TREE;
17569 /* Deduce the checked constraint and the prototype parameter.
17571 FIXME: In certain cases, failure to deduce should be a
17572 diagnosable error. */
17573 tree conc;
17574 tree proto;
17575 if (!deduce_constrained_parameter (check, conc, proto))
17576 return NULL_TREE;
17578 /* In template parameter scope, this results in a constrained
17579 parameter. Return a descriptor of that parm. */
17580 if (processing_template_parmlist)
17581 return build_constrained_parameter (conc, proto, args);
17583 /* In a parameter-declaration-clause, constrained-type
17584 specifiers result in invented template parameters. */
17585 if (parser->auto_is_implicit_function_template_parm_p)
17587 tree x = build_constrained_parameter (conc, proto, args);
17588 return synthesize_implicit_template_parm (parser, x);
17590 else
17592 /* Otherwise, we're in a context where the constrained
17593 type name is deduced and the constraint applies
17594 after deduction. */
17595 return make_constrained_auto (conc, args);
17598 return NULL_TREE;
17601 /* If DECL refers to a concept, return a TYPE_DECL representing
17602 the result of using the constrained type specifier in the
17603 current context. DECL refers to a concept if
17605 - it is an overload set containing a function concept taking a single
17606 type argument, or
17608 - it is a variable concept taking a single type argument. */
17610 static tree
17611 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17613 if (flag_concepts
17614 && (TREE_CODE (decl) == OVERLOAD
17615 || BASELINK_P (decl)
17616 || variable_concept_p (decl)))
17617 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17618 else
17619 return NULL_TREE;
17622 /* Check if DECL and ARGS form a partial-concept-id. If so,
17623 assign ID to the resulting constrained placeholder.
17625 Returns true if the partial-concept-id designates a placeholder
17626 and false otherwise. Note that *id is set to NULL_TREE in
17627 this case. */
17629 static tree
17630 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17632 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17635 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17636 or a concept-name.
17638 enum-name:
17639 identifier
17641 typedef-name:
17642 identifier
17644 concept-name:
17645 identifier
17647 Returns a TYPE_DECL for the type. */
17649 static tree
17650 cp_parser_nonclass_name (cp_parser* parser)
17652 tree type_decl;
17653 tree identifier;
17655 cp_token *token = cp_lexer_peek_token (parser->lexer);
17656 identifier = cp_parser_identifier (parser);
17657 if (identifier == error_mark_node)
17658 return error_mark_node;
17660 /* Look up the type-name. */
17661 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17663 type_decl = strip_using_decl (type_decl);
17665 /* If we found an overload set, then it may refer to a concept-name. */
17666 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17667 type_decl = decl;
17669 if (TREE_CODE (type_decl) != TYPE_DECL
17670 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17672 /* See if this is an Objective-C type. */
17673 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17674 tree type = objc_get_protocol_qualified_type (identifier, protos);
17675 if (type)
17676 type_decl = TYPE_NAME (type);
17679 /* Issue an error if we did not find a type-name. */
17680 if (TREE_CODE (type_decl) != TYPE_DECL
17681 /* In Objective-C, we have the complication that class names are
17682 normally type names and start declarations (eg, the
17683 "NSObject" in "NSObject *object;"), but can be used in an
17684 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17685 is an expression. So, a classname followed by a dot is not a
17686 valid type-name. */
17687 || (objc_is_class_name (TREE_TYPE (type_decl))
17688 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17690 if (!cp_parser_simulate_error (parser))
17691 cp_parser_name_lookup_error (parser, identifier, type_decl,
17692 NLE_TYPE, token->location);
17693 return error_mark_node;
17695 /* Remember that the name was used in the definition of the
17696 current class so that we can check later to see if the
17697 meaning would have been different after the class was
17698 entirely defined. */
17699 else if (type_decl != error_mark_node
17700 && !parser->scope)
17701 maybe_note_name_used_in_class (identifier, type_decl);
17703 return type_decl;
17706 /* Parse an elaborated-type-specifier. Note that the grammar given
17707 here incorporates the resolution to DR68.
17709 elaborated-type-specifier:
17710 class-key :: [opt] nested-name-specifier [opt] identifier
17711 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17712 enum-key :: [opt] nested-name-specifier [opt] identifier
17713 typename :: [opt] nested-name-specifier identifier
17714 typename :: [opt] nested-name-specifier template [opt]
17715 template-id
17717 GNU extension:
17719 elaborated-type-specifier:
17720 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17721 class-key attributes :: [opt] nested-name-specifier [opt]
17722 template [opt] template-id
17723 enum attributes :: [opt] nested-name-specifier [opt] identifier
17725 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17726 declared `friend'. If IS_DECLARATION is TRUE, then this
17727 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17728 something is being declared.
17730 Returns the TYPE specified. */
17732 static tree
17733 cp_parser_elaborated_type_specifier (cp_parser* parser,
17734 bool is_friend,
17735 bool is_declaration)
17737 enum tag_types tag_type;
17738 tree identifier;
17739 tree type = NULL_TREE;
17740 tree attributes = NULL_TREE;
17741 tree globalscope;
17742 cp_token *token = NULL;
17744 /* See if we're looking at the `enum' keyword. */
17745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17747 /* Consume the `enum' token. */
17748 cp_lexer_consume_token (parser->lexer);
17749 /* Remember that it's an enumeration type. */
17750 tag_type = enum_type;
17751 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17752 enums) is used here. */
17753 cp_token *token = cp_lexer_peek_token (parser->lexer);
17754 if (cp_parser_is_keyword (token, RID_CLASS)
17755 || cp_parser_is_keyword (token, RID_STRUCT))
17757 gcc_rich_location richloc (token->location);
17758 richloc.add_range (input_location, false);
17759 richloc.add_fixit_remove ();
17760 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17761 "a scoped enum must not use the %qD keyword",
17762 token->u.value);
17763 /* Consume the `struct' or `class' and parse it anyway. */
17764 cp_lexer_consume_token (parser->lexer);
17766 /* Parse the attributes. */
17767 attributes = cp_parser_attributes_opt (parser);
17769 /* Or, it might be `typename'. */
17770 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17771 RID_TYPENAME))
17773 /* Consume the `typename' token. */
17774 cp_lexer_consume_token (parser->lexer);
17775 /* Remember that it's a `typename' type. */
17776 tag_type = typename_type;
17778 /* Otherwise it must be a class-key. */
17779 else
17781 tag_type = cp_parser_class_key (parser);
17782 if (tag_type == none_type)
17783 return error_mark_node;
17784 /* Parse the attributes. */
17785 attributes = cp_parser_attributes_opt (parser);
17788 /* Look for the `::' operator. */
17789 globalscope = cp_parser_global_scope_opt (parser,
17790 /*current_scope_valid_p=*/false);
17791 /* Look for the nested-name-specifier. */
17792 tree nested_name_specifier;
17793 if (tag_type == typename_type && !globalscope)
17795 nested_name_specifier
17796 = cp_parser_nested_name_specifier (parser,
17797 /*typename_keyword_p=*/true,
17798 /*check_dependency_p=*/true,
17799 /*type_p=*/true,
17800 is_declaration);
17801 if (!nested_name_specifier)
17802 return error_mark_node;
17804 else
17805 /* Even though `typename' is not present, the proposed resolution
17806 to Core Issue 180 says that in `class A<T>::B', `B' should be
17807 considered a type-name, even if `A<T>' is dependent. */
17808 nested_name_specifier
17809 = cp_parser_nested_name_specifier_opt (parser,
17810 /*typename_keyword_p=*/true,
17811 /*check_dependency_p=*/true,
17812 /*type_p=*/true,
17813 is_declaration);
17814 /* For everything but enumeration types, consider a template-id.
17815 For an enumeration type, consider only a plain identifier. */
17816 if (tag_type != enum_type)
17818 bool template_p = false;
17819 tree decl;
17821 /* Allow the `template' keyword. */
17822 template_p = cp_parser_optional_template_keyword (parser);
17823 /* If we didn't see `template', we don't know if there's a
17824 template-id or not. */
17825 if (!template_p)
17826 cp_parser_parse_tentatively (parser);
17827 /* Parse the template-id. */
17828 token = cp_lexer_peek_token (parser->lexer);
17829 decl = cp_parser_template_id (parser, template_p,
17830 /*check_dependency_p=*/true,
17831 tag_type,
17832 is_declaration);
17833 /* If we didn't find a template-id, look for an ordinary
17834 identifier. */
17835 if (!template_p && !cp_parser_parse_definitely (parser))
17837 /* We can get here when cp_parser_template_id, called by
17838 cp_parser_class_name with tag_type == none_type, succeeds
17839 and caches a BASELINK. Then, when called again here,
17840 instead of failing and returning an error_mark_node
17841 returns it (see template/typename17.C in C++11).
17842 ??? Could we diagnose this earlier? */
17843 else if (tag_type == typename_type && BASELINK_P (decl))
17845 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17846 type = error_mark_node;
17848 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17849 in effect, then we must assume that, upon instantiation, the
17850 template will correspond to a class. */
17851 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17852 && tag_type == typename_type)
17853 type = make_typename_type (parser->scope, decl,
17854 typename_type,
17855 /*complain=*/tf_error);
17856 /* If the `typename' keyword is in effect and DECL is not a type
17857 decl, then type is non existent. */
17858 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17860 else if (TREE_CODE (decl) == TYPE_DECL)
17862 type = check_elaborated_type_specifier (tag_type, decl,
17863 /*allow_template_p=*/true);
17865 /* If the next token is a semicolon, this must be a specialization,
17866 instantiation, or friend declaration. Check the scope while we
17867 still know whether or not we had a nested-name-specifier. */
17868 if (type != error_mark_node
17869 && !nested_name_specifier && !is_friend
17870 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17871 check_unqualified_spec_or_inst (type, token->location);
17873 else if (decl == error_mark_node)
17874 type = error_mark_node;
17877 if (!type)
17879 token = cp_lexer_peek_token (parser->lexer);
17880 identifier = cp_parser_identifier (parser);
17882 if (identifier == error_mark_node)
17884 parser->scope = NULL_TREE;
17885 return error_mark_node;
17888 /* For a `typename', we needn't call xref_tag. */
17889 if (tag_type == typename_type
17890 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17891 return cp_parser_make_typename_type (parser, identifier,
17892 token->location);
17894 /* Template parameter lists apply only if we are not within a
17895 function parameter list. */
17896 bool template_parm_lists_apply
17897 = parser->num_template_parameter_lists;
17898 if (template_parm_lists_apply)
17899 for (cp_binding_level *s = current_binding_level;
17900 s && s->kind != sk_template_parms;
17901 s = s->level_chain)
17902 if (s->kind == sk_function_parms)
17903 template_parm_lists_apply = false;
17905 /* Look up a qualified name in the usual way. */
17906 if (parser->scope)
17908 tree decl;
17909 tree ambiguous_decls;
17911 decl = cp_parser_lookup_name (parser, identifier,
17912 tag_type,
17913 /*is_template=*/false,
17914 /*is_namespace=*/false,
17915 /*check_dependency=*/true,
17916 &ambiguous_decls,
17917 token->location);
17919 /* If the lookup was ambiguous, an error will already have been
17920 issued. */
17921 if (ambiguous_decls)
17922 return error_mark_node;
17924 /* If we are parsing friend declaration, DECL may be a
17925 TEMPLATE_DECL tree node here. However, we need to check
17926 whether this TEMPLATE_DECL results in valid code. Consider
17927 the following example:
17929 namespace N {
17930 template <class T> class C {};
17932 class X {
17933 template <class T> friend class N::C; // #1, valid code
17935 template <class T> class Y {
17936 friend class N::C; // #2, invalid code
17939 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17940 name lookup of `N::C'. We see that friend declaration must
17941 be template for the code to be valid. Note that
17942 processing_template_decl does not work here since it is
17943 always 1 for the above two cases. */
17945 decl = (cp_parser_maybe_treat_template_as_class
17946 (decl, /*tag_name_p=*/is_friend
17947 && template_parm_lists_apply));
17949 if (TREE_CODE (decl) != TYPE_DECL)
17951 cp_parser_diagnose_invalid_type_name (parser,
17952 identifier,
17953 token->location);
17954 return error_mark_node;
17957 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17959 bool allow_template = (template_parm_lists_apply
17960 || DECL_SELF_REFERENCE_P (decl));
17961 type = check_elaborated_type_specifier (tag_type, decl,
17962 allow_template);
17964 if (type == error_mark_node)
17965 return error_mark_node;
17968 /* Forward declarations of nested types, such as
17970 class C1::C2;
17971 class C1::C2::C3;
17973 are invalid unless all components preceding the final '::'
17974 are complete. If all enclosing types are complete, these
17975 declarations become merely pointless.
17977 Invalid forward declarations of nested types are errors
17978 caught elsewhere in parsing. Those that are pointless arrive
17979 here. */
17981 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17982 && !is_friend && !processing_explicit_instantiation)
17983 warning (0, "declaration %qD does not declare anything", decl);
17985 type = TREE_TYPE (decl);
17987 else
17989 /* An elaborated-type-specifier sometimes introduces a new type and
17990 sometimes names an existing type. Normally, the rule is that it
17991 introduces a new type only if there is not an existing type of
17992 the same name already in scope. For example, given:
17994 struct S {};
17995 void f() { struct S s; }
17997 the `struct S' in the body of `f' is the same `struct S' as in
17998 the global scope; the existing definition is used. However, if
17999 there were no global declaration, this would introduce a new
18000 local class named `S'.
18002 An exception to this rule applies to the following code:
18004 namespace N { struct S; }
18006 Here, the elaborated-type-specifier names a new type
18007 unconditionally; even if there is already an `S' in the
18008 containing scope this declaration names a new type.
18009 This exception only applies if the elaborated-type-specifier
18010 forms the complete declaration:
18012 [class.name]
18014 A declaration consisting solely of `class-key identifier ;' is
18015 either a redeclaration of the name in the current scope or a
18016 forward declaration of the identifier as a class name. It
18017 introduces the name into the current scope.
18019 We are in this situation precisely when the next token is a `;'.
18021 An exception to the exception is that a `friend' declaration does
18022 *not* name a new type; i.e., given:
18024 struct S { friend struct T; };
18026 `T' is not a new type in the scope of `S'.
18028 Also, `new struct S' or `sizeof (struct S)' never results in the
18029 definition of a new type; a new type can only be declared in a
18030 declaration context. */
18032 tag_scope ts;
18033 bool template_p;
18035 if (is_friend)
18036 /* Friends have special name lookup rules. */
18037 ts = ts_within_enclosing_non_class;
18038 else if (is_declaration
18039 && cp_lexer_next_token_is (parser->lexer,
18040 CPP_SEMICOLON))
18041 /* This is a `class-key identifier ;' */
18042 ts = ts_current;
18043 else
18044 ts = ts_global;
18046 template_p =
18047 (template_parm_lists_apply
18048 && (cp_parser_next_token_starts_class_definition_p (parser)
18049 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18050 /* An unqualified name was used to reference this type, so
18051 there were no qualifying templates. */
18052 if (template_parm_lists_apply
18053 && !cp_parser_check_template_parameters (parser,
18054 /*num_templates=*/0,
18055 /*template_id*/false,
18056 token->location,
18057 /*declarator=*/NULL))
18058 return error_mark_node;
18059 type = xref_tag (tag_type, identifier, ts, template_p);
18063 if (type == error_mark_node)
18064 return error_mark_node;
18066 /* Allow attributes on forward declarations of classes. */
18067 if (attributes)
18069 if (TREE_CODE (type) == TYPENAME_TYPE)
18070 warning (OPT_Wattributes,
18071 "attributes ignored on uninstantiated type");
18072 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18073 && ! processing_explicit_instantiation)
18074 warning (OPT_Wattributes,
18075 "attributes ignored on template instantiation");
18076 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18077 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18078 else
18079 warning (OPT_Wattributes,
18080 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18083 if (tag_type != enum_type)
18085 /* Indicate whether this class was declared as a `class' or as a
18086 `struct'. */
18087 if (CLASS_TYPE_P (type))
18088 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18089 cp_parser_check_class_key (tag_type, type);
18092 /* A "<" cannot follow an elaborated type specifier. If that
18093 happens, the user was probably trying to form a template-id. */
18094 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18095 token->location);
18097 return type;
18100 /* Parse an enum-specifier.
18102 enum-specifier:
18103 enum-head { enumerator-list [opt] }
18104 enum-head { enumerator-list , } [C++0x]
18106 enum-head:
18107 enum-key identifier [opt] enum-base [opt]
18108 enum-key nested-name-specifier identifier enum-base [opt]
18110 enum-key:
18111 enum
18112 enum class [C++0x]
18113 enum struct [C++0x]
18115 enum-base: [C++0x]
18116 : type-specifier-seq
18118 opaque-enum-specifier:
18119 enum-key identifier enum-base [opt] ;
18121 GNU Extensions:
18122 enum-key attributes[opt] identifier [opt] enum-base [opt]
18123 { enumerator-list [opt] }attributes[opt]
18124 enum-key attributes[opt] identifier [opt] enum-base [opt]
18125 { enumerator-list, }attributes[opt] [C++0x]
18127 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18128 if the token stream isn't an enum-specifier after all. */
18130 static tree
18131 cp_parser_enum_specifier (cp_parser* parser)
18133 tree identifier;
18134 tree type = NULL_TREE;
18135 tree prev_scope;
18136 tree nested_name_specifier = NULL_TREE;
18137 tree attributes;
18138 bool scoped_enum_p = false;
18139 bool has_underlying_type = false;
18140 bool nested_being_defined = false;
18141 bool new_value_list = false;
18142 bool is_new_type = false;
18143 bool is_unnamed = false;
18144 tree underlying_type = NULL_TREE;
18145 cp_token *type_start_token = NULL;
18146 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18148 parser->colon_corrects_to_scope_p = false;
18150 /* Parse tentatively so that we can back up if we don't find a
18151 enum-specifier. */
18152 cp_parser_parse_tentatively (parser);
18154 /* Caller guarantees that the current token is 'enum', an identifier
18155 possibly follows, and the token after that is an opening brace.
18156 If we don't have an identifier, fabricate an anonymous name for
18157 the enumeration being defined. */
18158 cp_lexer_consume_token (parser->lexer);
18160 /* Parse the "class" or "struct", which indicates a scoped
18161 enumeration type in C++0x. */
18162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18163 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18165 if (cxx_dialect < cxx11)
18166 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18168 /* Consume the `struct' or `class' token. */
18169 cp_lexer_consume_token (parser->lexer);
18171 scoped_enum_p = true;
18174 attributes = cp_parser_attributes_opt (parser);
18176 /* Clear the qualification. */
18177 parser->scope = NULL_TREE;
18178 parser->qualifying_scope = NULL_TREE;
18179 parser->object_scope = NULL_TREE;
18181 /* Figure out in what scope the declaration is being placed. */
18182 prev_scope = current_scope ();
18184 type_start_token = cp_lexer_peek_token (parser->lexer);
18186 push_deferring_access_checks (dk_no_check);
18187 nested_name_specifier
18188 = cp_parser_nested_name_specifier_opt (parser,
18189 /*typename_keyword_p=*/true,
18190 /*check_dependency_p=*/false,
18191 /*type_p=*/false,
18192 /*is_declaration=*/false);
18194 if (nested_name_specifier)
18196 tree name;
18198 identifier = cp_parser_identifier (parser);
18199 name = cp_parser_lookup_name (parser, identifier,
18200 enum_type,
18201 /*is_template=*/false,
18202 /*is_namespace=*/false,
18203 /*check_dependency=*/true,
18204 /*ambiguous_decls=*/NULL,
18205 input_location);
18206 if (name && name != error_mark_node)
18208 type = TREE_TYPE (name);
18209 if (TREE_CODE (type) == TYPENAME_TYPE)
18211 /* Are template enums allowed in ISO? */
18212 if (template_parm_scope_p ())
18213 pedwarn (type_start_token->location, OPT_Wpedantic,
18214 "%qD is an enumeration template", name);
18215 /* ignore a typename reference, for it will be solved by name
18216 in start_enum. */
18217 type = NULL_TREE;
18220 else if (nested_name_specifier == error_mark_node)
18221 /* We already issued an error. */;
18222 else
18224 error_at (type_start_token->location,
18225 "%qD does not name an enumeration in %qT",
18226 identifier, nested_name_specifier);
18227 nested_name_specifier = error_mark_node;
18230 else
18232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18233 identifier = cp_parser_identifier (parser);
18234 else
18236 identifier = make_anon_name ();
18237 is_unnamed = true;
18238 if (scoped_enum_p)
18239 error_at (type_start_token->location,
18240 "unnamed scoped enum is not allowed");
18243 pop_deferring_access_checks ();
18245 /* Check for the `:' that denotes a specified underlying type in C++0x.
18246 Note that a ':' could also indicate a bitfield width, however. */
18247 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18249 cp_decl_specifier_seq type_specifiers;
18251 /* Consume the `:'. */
18252 cp_lexer_consume_token (parser->lexer);
18254 /* Parse the type-specifier-seq. */
18255 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18256 /*is_trailing_return=*/false,
18257 &type_specifiers);
18259 /* At this point this is surely not elaborated type specifier. */
18260 if (!cp_parser_parse_definitely (parser))
18261 return NULL_TREE;
18263 if (cxx_dialect < cxx11)
18264 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18266 has_underlying_type = true;
18268 /* If that didn't work, stop. */
18269 if (type_specifiers.type != error_mark_node)
18271 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18272 /*initialized=*/0, NULL);
18273 if (underlying_type == error_mark_node
18274 || check_for_bare_parameter_packs (underlying_type))
18275 underlying_type = NULL_TREE;
18279 /* Look for the `{' but don't consume it yet. */
18280 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18282 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18284 cp_parser_error (parser, "expected %<{%>");
18285 if (has_underlying_type)
18287 type = NULL_TREE;
18288 goto out;
18291 /* An opaque-enum-specifier must have a ';' here. */
18292 if ((scoped_enum_p || underlying_type)
18293 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18295 cp_parser_error (parser, "expected %<;%> or %<{%>");
18296 if (has_underlying_type)
18298 type = NULL_TREE;
18299 goto out;
18304 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18305 return NULL_TREE;
18307 if (nested_name_specifier)
18309 if (CLASS_TYPE_P (nested_name_specifier))
18311 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18312 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18313 push_scope (nested_name_specifier);
18315 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18317 push_nested_namespace (nested_name_specifier);
18321 /* Issue an error message if type-definitions are forbidden here. */
18322 if (!cp_parser_check_type_definition (parser))
18323 type = error_mark_node;
18324 else
18325 /* Create the new type. We do this before consuming the opening
18326 brace so the enum will be recorded as being on the line of its
18327 tag (or the 'enum' keyword, if there is no tag). */
18328 type = start_enum (identifier, type, underlying_type,
18329 attributes, scoped_enum_p, &is_new_type);
18331 /* If the next token is not '{' it is an opaque-enum-specifier or an
18332 elaborated-type-specifier. */
18333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18335 timevar_push (TV_PARSE_ENUM);
18336 if (nested_name_specifier
18337 && nested_name_specifier != error_mark_node)
18339 /* The following catches invalid code such as:
18340 enum class S<int>::E { A, B, C }; */
18341 if (!processing_specialization
18342 && CLASS_TYPE_P (nested_name_specifier)
18343 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18344 error_at (type_start_token->location, "cannot add an enumerator "
18345 "list to a template instantiation");
18347 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18349 error_at (type_start_token->location,
18350 "%<%T::%E%> has not been declared",
18351 TYPE_CONTEXT (nested_name_specifier),
18352 nested_name_specifier);
18353 type = error_mark_node;
18355 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18356 && !CLASS_TYPE_P (nested_name_specifier))
18358 error_at (type_start_token->location, "nested name specifier "
18359 "%qT for enum declaration does not name a class "
18360 "or namespace", nested_name_specifier);
18361 type = error_mark_node;
18363 /* If that scope does not contain the scope in which the
18364 class was originally declared, the program is invalid. */
18365 else if (prev_scope && !is_ancestor (prev_scope,
18366 nested_name_specifier))
18368 if (at_namespace_scope_p ())
18369 error_at (type_start_token->location,
18370 "declaration of %qD in namespace %qD which does not "
18371 "enclose %qD",
18372 type, prev_scope, nested_name_specifier);
18373 else
18374 error_at (type_start_token->location,
18375 "declaration of %qD in %qD which does not "
18376 "enclose %qD",
18377 type, prev_scope, nested_name_specifier);
18378 type = error_mark_node;
18380 /* If that scope is the scope where the declaration is being placed
18381 the program is invalid. */
18382 else if (CLASS_TYPE_P (nested_name_specifier)
18383 && CLASS_TYPE_P (prev_scope)
18384 && same_type_p (nested_name_specifier, prev_scope))
18386 permerror (type_start_token->location,
18387 "extra qualification not allowed");
18388 nested_name_specifier = NULL_TREE;
18392 if (scoped_enum_p)
18393 begin_scope (sk_scoped_enum, type);
18395 /* Consume the opening brace. */
18396 matching_braces braces;
18397 braces.consume_open (parser);
18399 if (type == error_mark_node)
18400 ; /* Nothing to add */
18401 else if (OPAQUE_ENUM_P (type)
18402 || (cxx_dialect > cxx98 && processing_specialization))
18404 new_value_list = true;
18405 SET_OPAQUE_ENUM_P (type, false);
18406 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18408 else
18410 error_at (type_start_token->location,
18411 "multiple definition of %q#T", type);
18412 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18413 "previous definition here");
18414 type = error_mark_node;
18417 if (type == error_mark_node)
18418 cp_parser_skip_to_end_of_block_or_statement (parser);
18419 /* If the next token is not '}', then there are some enumerators. */
18420 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18422 if (is_unnamed && !scoped_enum_p)
18423 pedwarn (type_start_token->location, OPT_Wpedantic,
18424 "ISO C++ forbids empty unnamed enum");
18426 else
18427 cp_parser_enumerator_list (parser, type);
18429 /* Consume the final '}'. */
18430 braces.require_close (parser);
18432 if (scoped_enum_p)
18433 finish_scope ();
18434 timevar_pop (TV_PARSE_ENUM);
18436 else
18438 /* If a ';' follows, then it is an opaque-enum-specifier
18439 and additional restrictions apply. */
18440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18442 if (is_unnamed)
18443 error_at (type_start_token->location,
18444 "opaque-enum-specifier without name");
18445 else if (nested_name_specifier)
18446 error_at (type_start_token->location,
18447 "opaque-enum-specifier must use a simple identifier");
18451 /* Look for trailing attributes to apply to this enumeration, and
18452 apply them if appropriate. */
18453 if (cp_parser_allow_gnu_extensions_p (parser))
18455 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18456 cplus_decl_attributes (&type,
18457 trailing_attr,
18458 (int) ATTR_FLAG_TYPE_IN_PLACE);
18461 /* Finish up the enumeration. */
18462 if (type != error_mark_node)
18464 if (new_value_list)
18465 finish_enum_value_list (type);
18466 if (is_new_type)
18467 finish_enum (type);
18470 if (nested_name_specifier)
18472 if (CLASS_TYPE_P (nested_name_specifier))
18474 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18475 pop_scope (nested_name_specifier);
18477 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18479 pop_nested_namespace (nested_name_specifier);
18482 out:
18483 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18484 return type;
18487 /* Parse an enumerator-list. The enumerators all have the indicated
18488 TYPE.
18490 enumerator-list:
18491 enumerator-definition
18492 enumerator-list , enumerator-definition */
18494 static void
18495 cp_parser_enumerator_list (cp_parser* parser, tree type)
18497 while (true)
18499 /* Parse an enumerator-definition. */
18500 cp_parser_enumerator_definition (parser, type);
18502 /* If the next token is not a ',', we've reached the end of
18503 the list. */
18504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18505 break;
18506 /* Otherwise, consume the `,' and keep going. */
18507 cp_lexer_consume_token (parser->lexer);
18508 /* If the next token is a `}', there is a trailing comma. */
18509 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18511 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18512 pedwarn (input_location, OPT_Wpedantic,
18513 "comma at end of enumerator list");
18514 break;
18519 /* Parse an enumerator-definition. The enumerator has the indicated
18520 TYPE.
18522 enumerator-definition:
18523 enumerator
18524 enumerator = constant-expression
18526 enumerator:
18527 identifier
18529 GNU Extensions:
18531 enumerator-definition:
18532 enumerator attributes [opt]
18533 enumerator attributes [opt] = constant-expression */
18535 static void
18536 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18538 tree identifier;
18539 tree value;
18540 location_t loc;
18542 /* Save the input location because we are interested in the location
18543 of the identifier and not the location of the explicit value. */
18544 loc = cp_lexer_peek_token (parser->lexer)->location;
18546 /* Look for the identifier. */
18547 identifier = cp_parser_identifier (parser);
18548 if (identifier == error_mark_node)
18549 return;
18551 /* Parse any specified attributes. */
18552 tree attrs = cp_parser_attributes_opt (parser);
18554 /* If the next token is an '=', then there is an explicit value. */
18555 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18557 /* Consume the `=' token. */
18558 cp_lexer_consume_token (parser->lexer);
18559 /* Parse the value. */
18560 value = cp_parser_constant_expression (parser);
18562 else
18563 value = NULL_TREE;
18565 /* If we are processing a template, make sure the initializer of the
18566 enumerator doesn't contain any bare template parameter pack. */
18567 if (check_for_bare_parameter_packs (value))
18568 value = error_mark_node;
18570 /* Create the enumerator. */
18571 build_enumerator (identifier, value, type, attrs, loc);
18574 /* Parse a namespace-name.
18576 namespace-name:
18577 original-namespace-name
18578 namespace-alias
18580 Returns the NAMESPACE_DECL for the namespace. */
18582 static tree
18583 cp_parser_namespace_name (cp_parser* parser)
18585 tree identifier;
18586 tree namespace_decl;
18588 cp_token *token = cp_lexer_peek_token (parser->lexer);
18590 /* Get the name of the namespace. */
18591 identifier = cp_parser_identifier (parser);
18592 if (identifier == error_mark_node)
18593 return error_mark_node;
18595 /* Look up the identifier in the currently active scope. Look only
18596 for namespaces, due to:
18598 [basic.lookup.udir]
18600 When looking up a namespace-name in a using-directive or alias
18601 definition, only namespace names are considered.
18603 And:
18605 [basic.lookup.qual]
18607 During the lookup of a name preceding the :: scope resolution
18608 operator, object, function, and enumerator names are ignored.
18610 (Note that cp_parser_qualifying_entity only calls this
18611 function if the token after the name is the scope resolution
18612 operator.) */
18613 namespace_decl = cp_parser_lookup_name (parser, identifier,
18614 none_type,
18615 /*is_template=*/false,
18616 /*is_namespace=*/true,
18617 /*check_dependency=*/true,
18618 /*ambiguous_decls=*/NULL,
18619 token->location);
18620 /* If it's not a namespace, issue an error. */
18621 if (namespace_decl == error_mark_node
18622 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18624 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18626 auto_diagnostic_group d;
18627 error_at (token->location, "%qD is not a namespace-name", identifier);
18628 if (namespace_decl == error_mark_node
18629 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18630 suggest_alternative_in_explicit_scope (token->location, identifier,
18631 parser->scope);
18633 cp_parser_error (parser, "expected namespace-name");
18634 namespace_decl = error_mark_node;
18637 return namespace_decl;
18640 /* Parse a namespace-definition.
18642 namespace-definition:
18643 named-namespace-definition
18644 unnamed-namespace-definition
18646 named-namespace-definition:
18647 original-namespace-definition
18648 extension-namespace-definition
18650 original-namespace-definition:
18651 namespace identifier { namespace-body }
18653 extension-namespace-definition:
18654 namespace original-namespace-name { namespace-body }
18656 unnamed-namespace-definition:
18657 namespace { namespace-body } */
18659 static void
18660 cp_parser_namespace_definition (cp_parser* parser)
18662 tree identifier;
18663 int nested_definition_count = 0;
18665 cp_ensure_no_omp_declare_simd (parser);
18666 cp_ensure_no_oacc_routine (parser);
18668 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18670 if (is_inline)
18672 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18673 cp_lexer_consume_token (parser->lexer);
18676 /* Look for the `namespace' keyword. */
18677 cp_token* token
18678 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18680 /* Parse any specified attributes before the identifier. */
18681 tree attribs = cp_parser_attributes_opt (parser);
18683 for (;;)
18685 identifier = NULL_TREE;
18687 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18689 identifier = cp_parser_identifier (parser);
18691 if (cp_next_tokens_can_be_std_attribute_p (parser))
18692 pedwarn (input_location, OPT_Wpedantic,
18693 "standard attributes on namespaces must precede "
18694 "the namespace name");
18696 /* Parse any attributes specified after the identifier. */
18697 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18701 break;
18703 if (!nested_definition_count && cxx_dialect < cxx17)
18704 pedwarn (input_location, OPT_Wpedantic,
18705 "nested namespace definitions only available with "
18706 "-std=c++17 or -std=gnu++17");
18708 /* Nested namespace names can create new namespaces (unlike
18709 other qualified-ids). */
18710 if (int count = identifier ? push_namespace (identifier) : 0)
18711 nested_definition_count += count;
18712 else
18713 cp_parser_error (parser, "nested namespace name required");
18714 cp_lexer_consume_token (parser->lexer);
18717 if (nested_definition_count && !identifier)
18718 cp_parser_error (parser, "namespace name required");
18720 if (nested_definition_count && attribs)
18721 error_at (token->location,
18722 "a nested namespace definition cannot have attributes");
18723 if (nested_definition_count && is_inline)
18724 error_at (token->location,
18725 "a nested namespace definition cannot be inline");
18727 /* Start the namespace. */
18728 nested_definition_count += push_namespace (identifier, is_inline);
18730 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18732 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18734 /* Look for the `{' to validate starting the namespace. */
18735 matching_braces braces;
18736 if (braces.require_open (parser))
18738 /* Parse the body of the namespace. */
18739 cp_parser_namespace_body (parser);
18741 /* Look for the final `}'. */
18742 braces.require_close (parser);
18745 if (has_visibility)
18746 pop_visibility (1);
18748 /* Pop the nested namespace definitions. */
18749 while (nested_definition_count--)
18750 pop_namespace ();
18753 /* Parse a namespace-body.
18755 namespace-body:
18756 declaration-seq [opt] */
18758 static void
18759 cp_parser_namespace_body (cp_parser* parser)
18761 cp_parser_declaration_seq_opt (parser);
18764 /* Parse a namespace-alias-definition.
18766 namespace-alias-definition:
18767 namespace identifier = qualified-namespace-specifier ; */
18769 static void
18770 cp_parser_namespace_alias_definition (cp_parser* parser)
18772 tree identifier;
18773 tree namespace_specifier;
18775 cp_token *token = cp_lexer_peek_token (parser->lexer);
18777 /* Look for the `namespace' keyword. */
18778 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18779 /* Look for the identifier. */
18780 identifier = cp_parser_identifier (parser);
18781 if (identifier == error_mark_node)
18782 return;
18783 /* Look for the `=' token. */
18784 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18785 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18787 error_at (token->location, "%<namespace%> definition is not allowed here");
18788 /* Skip the definition. */
18789 cp_lexer_consume_token (parser->lexer);
18790 if (cp_parser_skip_to_closing_brace (parser))
18791 cp_lexer_consume_token (parser->lexer);
18792 return;
18794 cp_parser_require (parser, CPP_EQ, RT_EQ);
18795 /* Look for the qualified-namespace-specifier. */
18796 namespace_specifier
18797 = cp_parser_qualified_namespace_specifier (parser);
18798 /* Look for the `;' token. */
18799 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18801 /* Register the alias in the symbol table. */
18802 do_namespace_alias (identifier, namespace_specifier);
18805 /* Parse a qualified-namespace-specifier.
18807 qualified-namespace-specifier:
18808 :: [opt] nested-name-specifier [opt] namespace-name
18810 Returns a NAMESPACE_DECL corresponding to the specified
18811 namespace. */
18813 static tree
18814 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18816 /* Look for the optional `::'. */
18817 cp_parser_global_scope_opt (parser,
18818 /*current_scope_valid_p=*/false);
18820 /* Look for the optional nested-name-specifier. */
18821 cp_parser_nested_name_specifier_opt (parser,
18822 /*typename_keyword_p=*/false,
18823 /*check_dependency_p=*/true,
18824 /*type_p=*/false,
18825 /*is_declaration=*/true);
18827 return cp_parser_namespace_name (parser);
18830 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18831 access declaration.
18833 using-declaration:
18834 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18835 using :: unqualified-id ;
18837 access-declaration:
18838 qualified-id ;
18842 static bool
18843 cp_parser_using_declaration (cp_parser* parser,
18844 bool access_declaration_p)
18846 cp_token *token;
18847 bool typename_p = false;
18848 bool global_scope_p;
18849 tree decl;
18850 tree identifier;
18851 tree qscope;
18852 int oldcount = errorcount;
18853 cp_token *diag_token = NULL;
18855 if (access_declaration_p)
18857 diag_token = cp_lexer_peek_token (parser->lexer);
18858 cp_parser_parse_tentatively (parser);
18860 else
18862 /* Look for the `using' keyword. */
18863 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18865 again:
18866 /* Peek at the next token. */
18867 token = cp_lexer_peek_token (parser->lexer);
18868 /* See if it's `typename'. */
18869 if (token->keyword == RID_TYPENAME)
18871 /* Remember that we've seen it. */
18872 typename_p = true;
18873 /* Consume the `typename' token. */
18874 cp_lexer_consume_token (parser->lexer);
18878 /* Look for the optional global scope qualification. */
18879 global_scope_p
18880 = (cp_parser_global_scope_opt (parser,
18881 /*current_scope_valid_p=*/false)
18882 != NULL_TREE);
18884 /* If we saw `typename', or didn't see `::', then there must be a
18885 nested-name-specifier present. */
18886 if (typename_p || !global_scope_p)
18888 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18889 /*check_dependency_p=*/true,
18890 /*type_p=*/false,
18891 /*is_declaration=*/true);
18892 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18894 cp_parser_skip_to_end_of_block_or_statement (parser);
18895 return false;
18898 /* Otherwise, we could be in either of the two productions. In that
18899 case, treat the nested-name-specifier as optional. */
18900 else
18901 qscope = cp_parser_nested_name_specifier_opt (parser,
18902 /*typename_keyword_p=*/false,
18903 /*check_dependency_p=*/true,
18904 /*type_p=*/false,
18905 /*is_declaration=*/true);
18906 if (!qscope)
18907 qscope = global_namespace;
18908 else if (UNSCOPED_ENUM_P (qscope))
18909 qscope = CP_TYPE_CONTEXT (qscope);
18911 if (access_declaration_p && cp_parser_error_occurred (parser))
18912 /* Something has already gone wrong; there's no need to parse
18913 further. Since an error has occurred, the return value of
18914 cp_parser_parse_definitely will be false, as required. */
18915 return cp_parser_parse_definitely (parser);
18917 token = cp_lexer_peek_token (parser->lexer);
18918 /* Parse the unqualified-id. */
18919 identifier = cp_parser_unqualified_id (parser,
18920 /*template_keyword_p=*/false,
18921 /*check_dependency_p=*/true,
18922 /*declarator_p=*/true,
18923 /*optional_p=*/false);
18925 if (access_declaration_p)
18927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18928 cp_parser_simulate_error (parser);
18929 if (!cp_parser_parse_definitely (parser))
18930 return false;
18932 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18934 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18935 if (cxx_dialect < cxx17
18936 && !in_system_header_at (ell->location))
18937 pedwarn (ell->location, 0,
18938 "pack expansion in using-declaration only available "
18939 "with -std=c++17 or -std=gnu++17");
18940 qscope = make_pack_expansion (qscope);
18943 /* The function we call to handle a using-declaration is different
18944 depending on what scope we are in. */
18945 if (qscope == error_mark_node || identifier == error_mark_node)
18947 else if (!identifier_p (identifier)
18948 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18949 /* [namespace.udecl]
18951 A using declaration shall not name a template-id. */
18952 error_at (token->location,
18953 "a template-id may not appear in a using-declaration");
18954 else
18956 if (at_class_scope_p ())
18958 /* Create the USING_DECL. */
18959 decl = do_class_using_decl (qscope, identifier);
18961 if (decl && typename_p)
18962 USING_DECL_TYPENAME_P (decl) = 1;
18964 if (check_for_bare_parameter_packs (decl))
18966 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18967 return false;
18969 else
18970 /* Add it to the list of members in this class. */
18971 finish_member_declaration (decl);
18973 else
18975 decl = cp_parser_lookup_name_simple (parser,
18976 identifier,
18977 token->location);
18978 if (decl == error_mark_node)
18979 cp_parser_name_lookup_error (parser, identifier,
18980 decl, NLE_NULL,
18981 token->location);
18982 else if (check_for_bare_parameter_packs (decl))
18984 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18985 return false;
18987 else if (!at_namespace_scope_p ())
18988 finish_local_using_decl (decl, qscope, identifier);
18989 else
18990 finish_namespace_using_decl (decl, qscope, identifier);
18994 if (!access_declaration_p
18995 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18997 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18998 if (cxx_dialect < cxx17)
18999 pedwarn (comma->location, 0,
19000 "comma-separated list in using-declaration only available "
19001 "with -std=c++17 or -std=gnu++17");
19002 goto again;
19005 /* Look for the final `;'. */
19006 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19008 if (access_declaration_p && errorcount == oldcount)
19009 warning_at (diag_token->location, OPT_Wdeprecated,
19010 "access declarations are deprecated "
19011 "in favour of using-declarations; "
19012 "suggestion: add the %<using%> keyword");
19014 return true;
19017 /* Parse an alias-declaration.
19019 alias-declaration:
19020 using identifier attribute-specifier-seq [opt] = type-id */
19022 static tree
19023 cp_parser_alias_declaration (cp_parser* parser)
19025 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19026 location_t id_location;
19027 cp_declarator *declarator;
19028 cp_decl_specifier_seq decl_specs;
19029 bool member_p;
19030 const char *saved_message = NULL;
19032 /* Look for the `using' keyword. */
19033 cp_token *using_token
19034 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19035 if (using_token == NULL)
19036 return error_mark_node;
19038 id_location = cp_lexer_peek_token (parser->lexer)->location;
19039 id = cp_parser_identifier (parser);
19040 if (id == error_mark_node)
19041 return error_mark_node;
19043 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19044 attributes = cp_parser_attributes_opt (parser);
19045 if (attributes == error_mark_node)
19046 return error_mark_node;
19048 cp_parser_require (parser, CPP_EQ, RT_EQ);
19050 if (cp_parser_error_occurred (parser))
19051 return error_mark_node;
19053 cp_parser_commit_to_tentative_parse (parser);
19055 /* Now we are going to parse the type-id of the declaration. */
19058 [dcl.type]/3 says:
19060 "A type-specifier-seq shall not define a class or enumeration
19061 unless it appears in the type-id of an alias-declaration (7.1.3) that
19062 is not the declaration of a template-declaration."
19064 In other words, if we currently are in an alias template, the
19065 type-id should not define a type.
19067 So let's set parser->type_definition_forbidden_message in that
19068 case; cp_parser_check_type_definition (called by
19069 cp_parser_class_specifier) will then emit an error if a type is
19070 defined in the type-id. */
19071 if (parser->num_template_parameter_lists)
19073 saved_message = parser->type_definition_forbidden_message;
19074 parser->type_definition_forbidden_message =
19075 G_("types may not be defined in alias template declarations");
19078 type = cp_parser_type_id (parser);
19080 /* Restore the error message if need be. */
19081 if (parser->num_template_parameter_lists)
19082 parser->type_definition_forbidden_message = saved_message;
19084 if (type == error_mark_node
19085 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19087 cp_parser_skip_to_end_of_block_or_statement (parser);
19088 return error_mark_node;
19091 /* A typedef-name can also be introduced by an alias-declaration. The
19092 identifier following the using keyword becomes a typedef-name. It has
19093 the same semantics as if it were introduced by the typedef
19094 specifier. In particular, it does not define a new type and it shall
19095 not appear in the type-id. */
19097 clear_decl_specs (&decl_specs);
19098 decl_specs.type = type;
19099 if (attributes != NULL_TREE)
19101 decl_specs.attributes = attributes;
19102 set_and_check_decl_spec_loc (&decl_specs,
19103 ds_attribute,
19104 attrs_token);
19106 set_and_check_decl_spec_loc (&decl_specs,
19107 ds_typedef,
19108 using_token);
19109 set_and_check_decl_spec_loc (&decl_specs,
19110 ds_alias,
19111 using_token);
19113 if (parser->num_template_parameter_lists
19114 && !cp_parser_check_template_parameters (parser,
19115 /*num_templates=*/0,
19116 /*template_id*/false,
19117 id_location,
19118 /*declarator=*/NULL))
19119 return error_mark_node;
19121 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19122 declarator->id_loc = id_location;
19124 member_p = at_class_scope_p ();
19125 if (member_p)
19126 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19127 NULL_TREE, attributes);
19128 else
19129 decl = start_decl (declarator, &decl_specs, 0,
19130 attributes, NULL_TREE, &pushed_scope);
19131 if (decl == error_mark_node)
19132 return decl;
19134 // Attach constraints to the alias declaration.
19135 if (flag_concepts && current_template_parms)
19137 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19138 tree constr = build_constraints (reqs, NULL_TREE);
19139 set_constraints (decl, constr);
19142 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19144 if (pushed_scope)
19145 pop_scope (pushed_scope);
19147 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19148 added into the symbol table; otherwise, return the TYPE_DECL. */
19149 if (DECL_LANG_SPECIFIC (decl)
19150 && DECL_TEMPLATE_INFO (decl)
19151 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19153 decl = DECL_TI_TEMPLATE (decl);
19154 if (member_p)
19155 check_member_template (decl);
19158 return decl;
19161 /* Parse a using-directive.
19163 using-directive:
19164 using namespace :: [opt] nested-name-specifier [opt]
19165 namespace-name ; */
19167 static void
19168 cp_parser_using_directive (cp_parser* parser)
19170 tree namespace_decl;
19171 tree attribs;
19173 /* Look for the `using' keyword. */
19174 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19175 /* And the `namespace' keyword. */
19176 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19177 /* Look for the optional `::' operator. */
19178 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19179 /* And the optional nested-name-specifier. */
19180 cp_parser_nested_name_specifier_opt (parser,
19181 /*typename_keyword_p=*/false,
19182 /*check_dependency_p=*/true,
19183 /*type_p=*/false,
19184 /*is_declaration=*/true);
19185 /* Get the namespace being used. */
19186 namespace_decl = cp_parser_namespace_name (parser);
19187 /* And any specified attributes. */
19188 attribs = cp_parser_attributes_opt (parser);
19190 /* Update the symbol table. */
19191 if (namespace_bindings_p ())
19192 finish_namespace_using_directive (namespace_decl, attribs);
19193 else
19194 finish_local_using_directive (namespace_decl, attribs);
19196 /* Look for the final `;'. */
19197 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19200 /* Parse an asm-definition.
19202 asm-definition:
19203 asm ( string-literal ) ;
19205 GNU Extension:
19207 asm-definition:
19208 asm volatile [opt] ( string-literal ) ;
19209 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19210 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19211 : asm-operand-list [opt] ) ;
19212 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19213 : asm-operand-list [opt]
19214 : asm-clobber-list [opt] ) ;
19215 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19216 : asm-clobber-list [opt]
19217 : asm-goto-list ) ; */
19219 static void
19220 cp_parser_asm_definition (cp_parser* parser)
19222 tree string;
19223 tree outputs = NULL_TREE;
19224 tree inputs = NULL_TREE;
19225 tree clobbers = NULL_TREE;
19226 tree labels = NULL_TREE;
19227 tree asm_stmt;
19228 bool volatile_p = false;
19229 bool extended_p = false;
19230 bool invalid_inputs_p = false;
19231 bool invalid_outputs_p = false;
19232 bool goto_p = false;
19233 required_token missing = RT_NONE;
19235 /* Look for the `asm' keyword. */
19236 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19238 if (parser->in_function_body
19239 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19241 error ("%<asm%> in %<constexpr%> function");
19242 cp_function_chain->invalid_constexpr = true;
19245 /* See if the next token is `volatile'. */
19246 if (cp_parser_allow_gnu_extensions_p (parser)
19247 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19249 /* Remember that we saw the `volatile' keyword. */
19250 volatile_p = true;
19251 /* Consume the token. */
19252 cp_lexer_consume_token (parser->lexer);
19254 if (cp_parser_allow_gnu_extensions_p (parser)
19255 && parser->in_function_body
19256 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19258 /* Remember that we saw the `goto' keyword. */
19259 goto_p = true;
19260 /* Consume the token. */
19261 cp_lexer_consume_token (parser->lexer);
19263 /* Look for the opening `('. */
19264 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19265 return;
19266 /* Look for the string. */
19267 string = cp_parser_string_literal (parser, false, false);
19268 if (string == error_mark_node)
19270 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19271 /*consume_paren=*/true);
19272 return;
19275 /* If we're allowing GNU extensions, check for the extended assembly
19276 syntax. Unfortunately, the `:' tokens need not be separated by
19277 a space in C, and so, for compatibility, we tolerate that here
19278 too. Doing that means that we have to treat the `::' operator as
19279 two `:' tokens. */
19280 if (cp_parser_allow_gnu_extensions_p (parser)
19281 && parser->in_function_body
19282 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19283 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19285 bool inputs_p = false;
19286 bool clobbers_p = false;
19287 bool labels_p = false;
19289 /* The extended syntax was used. */
19290 extended_p = true;
19292 /* Look for outputs. */
19293 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19295 /* Consume the `:'. */
19296 cp_lexer_consume_token (parser->lexer);
19297 /* Parse the output-operands. */
19298 if (cp_lexer_next_token_is_not (parser->lexer,
19299 CPP_COLON)
19300 && cp_lexer_next_token_is_not (parser->lexer,
19301 CPP_SCOPE)
19302 && cp_lexer_next_token_is_not (parser->lexer,
19303 CPP_CLOSE_PAREN)
19304 && !goto_p)
19306 outputs = cp_parser_asm_operand_list (parser);
19307 if (outputs == error_mark_node)
19308 invalid_outputs_p = true;
19311 /* If the next token is `::', there are no outputs, and the
19312 next token is the beginning of the inputs. */
19313 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19314 /* The inputs are coming next. */
19315 inputs_p = true;
19317 /* Look for inputs. */
19318 if (inputs_p
19319 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19321 /* Consume the `:' or `::'. */
19322 cp_lexer_consume_token (parser->lexer);
19323 /* Parse the output-operands. */
19324 if (cp_lexer_next_token_is_not (parser->lexer,
19325 CPP_COLON)
19326 && cp_lexer_next_token_is_not (parser->lexer,
19327 CPP_SCOPE)
19328 && cp_lexer_next_token_is_not (parser->lexer,
19329 CPP_CLOSE_PAREN))
19331 inputs = cp_parser_asm_operand_list (parser);
19332 if (inputs == error_mark_node)
19333 invalid_inputs_p = true;
19336 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19337 /* The clobbers are coming next. */
19338 clobbers_p = true;
19340 /* Look for clobbers. */
19341 if (clobbers_p
19342 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19344 clobbers_p = true;
19345 /* Consume the `:' or `::'. */
19346 cp_lexer_consume_token (parser->lexer);
19347 /* Parse the clobbers. */
19348 if (cp_lexer_next_token_is_not (parser->lexer,
19349 CPP_COLON)
19350 && cp_lexer_next_token_is_not (parser->lexer,
19351 CPP_CLOSE_PAREN))
19352 clobbers = cp_parser_asm_clobber_list (parser);
19354 else if (goto_p
19355 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19356 /* The labels are coming next. */
19357 labels_p = true;
19359 /* Look for labels. */
19360 if (labels_p
19361 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19363 labels_p = true;
19364 /* Consume the `:' or `::'. */
19365 cp_lexer_consume_token (parser->lexer);
19366 /* Parse the labels. */
19367 labels = cp_parser_asm_label_list (parser);
19370 if (goto_p && !labels_p)
19371 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19373 else if (goto_p)
19374 missing = RT_COLON_SCOPE;
19376 /* Look for the closing `)'. */
19377 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19378 missing ? missing : RT_CLOSE_PAREN))
19379 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19380 /*consume_paren=*/true);
19381 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19383 if (!invalid_inputs_p && !invalid_outputs_p)
19385 /* Create the ASM_EXPR. */
19386 if (parser->in_function_body)
19388 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19389 inputs, clobbers, labels);
19390 /* If the extended syntax was not used, mark the ASM_EXPR. */
19391 if (!extended_p)
19393 tree temp = asm_stmt;
19394 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19395 temp = TREE_OPERAND (temp, 0);
19397 ASM_INPUT_P (temp) = 1;
19400 else
19401 symtab->finalize_toplevel_asm (string);
19405 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19406 type that comes from the decl-specifier-seq. */
19408 static tree
19409 strip_declarator_types (tree type, cp_declarator *declarator)
19411 for (cp_declarator *d = declarator; d;)
19412 switch (d->kind)
19414 case cdk_id:
19415 case cdk_decomp:
19416 case cdk_error:
19417 d = NULL;
19418 break;
19420 default:
19421 if (TYPE_PTRMEMFUNC_P (type))
19422 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19423 type = TREE_TYPE (type);
19424 d = d->declarator;
19425 break;
19428 return type;
19431 /* Declarators [gram.dcl.decl] */
19433 /* Parse an init-declarator.
19435 init-declarator:
19436 declarator initializer [opt]
19438 GNU Extension:
19440 init-declarator:
19441 declarator asm-specification [opt] attributes [opt] initializer [opt]
19443 function-definition:
19444 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19445 function-body
19446 decl-specifier-seq [opt] declarator function-try-block
19448 GNU Extension:
19450 function-definition:
19451 __extension__ function-definition
19453 TM Extension:
19455 function-definition:
19456 decl-specifier-seq [opt] declarator function-transaction-block
19458 The DECL_SPECIFIERS apply to this declarator. Returns a
19459 representation of the entity declared. If MEMBER_P is TRUE, then
19460 this declarator appears in a class scope. The new DECL created by
19461 this declarator is returned.
19463 The CHECKS are access checks that should be performed once we know
19464 what entity is being declared (and, therefore, what classes have
19465 befriended it).
19467 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19468 for a function-definition here as well. If the declarator is a
19469 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19470 be TRUE upon return. By that point, the function-definition will
19471 have been completely parsed.
19473 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19474 is FALSE.
19476 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19477 parsed declaration if it is an uninitialized single declarator not followed
19478 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19479 if present, will not be consumed. If returned, this declarator will be
19480 created with SD_INITIALIZED but will not call cp_finish_decl.
19482 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19483 and there is an initializer, the pointed location_t is set to the
19484 location of the '=' or `(', or '{' in C++11 token introducing the
19485 initializer. */
19487 static tree
19488 cp_parser_init_declarator (cp_parser* parser,
19489 cp_decl_specifier_seq *decl_specifiers,
19490 vec<deferred_access_check, va_gc> *checks,
19491 bool function_definition_allowed_p,
19492 bool member_p,
19493 int declares_class_or_enum,
19494 bool* function_definition_p,
19495 tree* maybe_range_for_decl,
19496 location_t* init_loc,
19497 tree* auto_result)
19499 cp_token *token = NULL, *asm_spec_start_token = NULL,
19500 *attributes_start_token = NULL;
19501 cp_declarator *declarator;
19502 tree prefix_attributes;
19503 tree attributes = NULL;
19504 tree asm_specification;
19505 tree initializer;
19506 tree decl = NULL_TREE;
19507 tree scope;
19508 int is_initialized;
19509 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19510 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19511 "(...)". */
19512 enum cpp_ttype initialization_kind;
19513 bool is_direct_init = false;
19514 bool is_non_constant_init;
19515 int ctor_dtor_or_conv_p;
19516 bool friend_p = cp_parser_friend_p (decl_specifiers);
19517 tree pushed_scope = NULL_TREE;
19518 bool range_for_decl_p = false;
19519 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19520 location_t tmp_init_loc = UNKNOWN_LOCATION;
19522 /* Gather the attributes that were provided with the
19523 decl-specifiers. */
19524 prefix_attributes = decl_specifiers->attributes;
19526 /* Assume that this is not the declarator for a function
19527 definition. */
19528 if (function_definition_p)
19529 *function_definition_p = false;
19531 /* Default arguments are only permitted for function parameters. */
19532 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19533 parser->default_arg_ok_p = false;
19535 /* Defer access checks while parsing the declarator; we cannot know
19536 what names are accessible until we know what is being
19537 declared. */
19538 resume_deferring_access_checks ();
19540 token = cp_lexer_peek_token (parser->lexer);
19542 /* Parse the declarator. */
19543 declarator
19544 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19545 &ctor_dtor_or_conv_p,
19546 /*parenthesized_p=*/NULL,
19547 member_p, friend_p);
19548 /* Gather up the deferred checks. */
19549 stop_deferring_access_checks ();
19551 parser->default_arg_ok_p = saved_default_arg_ok_p;
19553 /* If the DECLARATOR was erroneous, there's no need to go
19554 further. */
19555 if (declarator == cp_error_declarator)
19556 return error_mark_node;
19558 /* Check that the number of template-parameter-lists is OK. */
19559 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19560 token->location))
19561 return error_mark_node;
19563 if (declares_class_or_enum & 2)
19564 cp_parser_check_for_definition_in_return_type (declarator,
19565 decl_specifiers->type,
19566 decl_specifiers->locations[ds_type_spec]);
19568 /* Figure out what scope the entity declared by the DECLARATOR is
19569 located in. `grokdeclarator' sometimes changes the scope, so
19570 we compute it now. */
19571 scope = get_scope_of_declarator (declarator);
19573 /* Perform any lookups in the declared type which were thought to be
19574 dependent, but are not in the scope of the declarator. */
19575 decl_specifiers->type
19576 = maybe_update_decl_type (decl_specifiers->type, scope);
19578 /* If we're allowing GNU extensions, look for an
19579 asm-specification. */
19580 if (cp_parser_allow_gnu_extensions_p (parser))
19582 /* Look for an asm-specification. */
19583 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19584 asm_specification = cp_parser_asm_specification_opt (parser);
19586 else
19587 asm_specification = NULL_TREE;
19589 /* Look for attributes. */
19590 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19591 attributes = cp_parser_attributes_opt (parser);
19593 /* Peek at the next token. */
19594 token = cp_lexer_peek_token (parser->lexer);
19596 bool bogus_implicit_tmpl = false;
19598 if (function_declarator_p (declarator))
19600 /* Handle C++17 deduction guides. */
19601 if (!decl_specifiers->type
19602 && ctor_dtor_or_conv_p <= 0
19603 && cxx_dialect >= cxx17)
19605 cp_declarator *id = get_id_declarator (declarator);
19606 tree name = id->u.id.unqualified_name;
19607 parser->scope = id->u.id.qualifying_scope;
19608 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19609 if (tmpl
19610 && (DECL_CLASS_TEMPLATE_P (tmpl)
19611 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19613 id->u.id.unqualified_name = dguide_name (tmpl);
19614 id->u.id.sfk = sfk_deduction_guide;
19615 ctor_dtor_or_conv_p = 1;
19619 /* Check to see if the token indicates the start of a
19620 function-definition. */
19621 if (cp_parser_token_starts_function_definition_p (token))
19623 if (!function_definition_allowed_p)
19625 /* If a function-definition should not appear here, issue an
19626 error message. */
19627 cp_parser_error (parser,
19628 "a function-definition is not allowed here");
19629 return error_mark_node;
19632 location_t func_brace_location
19633 = cp_lexer_peek_token (parser->lexer)->location;
19635 /* Neither attributes nor an asm-specification are allowed
19636 on a function-definition. */
19637 if (asm_specification)
19638 error_at (asm_spec_start_token->location,
19639 "an asm-specification is not allowed "
19640 "on a function-definition");
19641 if (attributes)
19642 error_at (attributes_start_token->location,
19643 "attributes are not allowed "
19644 "on a function-definition");
19645 /* This is a function-definition. */
19646 *function_definition_p = true;
19648 /* Parse the function definition. */
19649 if (member_p)
19650 decl = cp_parser_save_member_function_body (parser,
19651 decl_specifiers,
19652 declarator,
19653 prefix_attributes);
19654 else
19655 decl =
19656 (cp_parser_function_definition_from_specifiers_and_declarator
19657 (parser, decl_specifiers, prefix_attributes, declarator));
19659 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19661 /* This is where the prologue starts... */
19662 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19663 = func_brace_location;
19666 return decl;
19669 else if (parser->fully_implicit_function_template_p)
19671 /* A non-template declaration involving a function parameter list
19672 containing an implicit template parameter will be made into a
19673 template. If the resulting declaration is not going to be an
19674 actual function then finish the template scope here to prevent it.
19675 An error message will be issued once we have a decl to talk about.
19677 FIXME probably we should do type deduction rather than create an
19678 implicit template, but the standard currently doesn't allow it. */
19679 bogus_implicit_tmpl = true;
19680 finish_fully_implicit_template (parser, NULL_TREE);
19683 /* [dcl.dcl]
19685 Only in function declarations for constructors, destructors, type
19686 conversions, and deduction guides can the decl-specifier-seq be omitted.
19688 We explicitly postpone this check past the point where we handle
19689 function-definitions because we tolerate function-definitions
19690 that are missing their return types in some modes. */
19691 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19693 cp_parser_error (parser,
19694 "expected constructor, destructor, or type conversion");
19695 return error_mark_node;
19698 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19699 if (token->type == CPP_EQ
19700 || token->type == CPP_OPEN_PAREN
19701 || token->type == CPP_OPEN_BRACE)
19703 is_initialized = SD_INITIALIZED;
19704 initialization_kind = token->type;
19705 if (maybe_range_for_decl)
19706 *maybe_range_for_decl = error_mark_node;
19707 tmp_init_loc = token->location;
19708 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19709 *init_loc = tmp_init_loc;
19711 if (token->type == CPP_EQ
19712 && function_declarator_p (declarator))
19714 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19715 if (t2->keyword == RID_DEFAULT)
19716 is_initialized = SD_DEFAULTED;
19717 else if (t2->keyword == RID_DELETE)
19718 is_initialized = SD_DELETED;
19721 else
19723 /* If the init-declarator isn't initialized and isn't followed by a
19724 `,' or `;', it's not a valid init-declarator. */
19725 if (token->type != CPP_COMMA
19726 && token->type != CPP_SEMICOLON)
19728 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19729 range_for_decl_p = true;
19730 else
19732 if (!maybe_range_for_decl)
19733 cp_parser_error (parser, "expected initializer");
19734 return error_mark_node;
19737 is_initialized = SD_UNINITIALIZED;
19738 initialization_kind = CPP_EOF;
19741 /* Because start_decl has side-effects, we should only call it if we
19742 know we're going ahead. By this point, we know that we cannot
19743 possibly be looking at any other construct. */
19744 cp_parser_commit_to_tentative_parse (parser);
19746 /* Enter the newly declared entry in the symbol table. If we're
19747 processing a declaration in a class-specifier, we wait until
19748 after processing the initializer. */
19749 if (!member_p)
19751 if (parser->in_unbraced_linkage_specification_p)
19752 decl_specifiers->storage_class = sc_extern;
19753 decl = start_decl (declarator, decl_specifiers,
19754 range_for_decl_p? SD_INITIALIZED : is_initialized,
19755 attributes, prefix_attributes, &pushed_scope);
19756 cp_finalize_omp_declare_simd (parser, decl);
19757 cp_finalize_oacc_routine (parser, decl, false);
19758 /* Adjust location of decl if declarator->id_loc is more appropriate:
19759 set, and decl wasn't merged with another decl, in which case its
19760 location would be different from input_location, and more accurate. */
19761 if (DECL_P (decl)
19762 && declarator->id_loc != UNKNOWN_LOCATION
19763 && DECL_SOURCE_LOCATION (decl) == input_location)
19764 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19766 else if (scope)
19767 /* Enter the SCOPE. That way unqualified names appearing in the
19768 initializer will be looked up in SCOPE. */
19769 pushed_scope = push_scope (scope);
19771 /* Perform deferred access control checks, now that we know in which
19772 SCOPE the declared entity resides. */
19773 if (!member_p && decl)
19775 tree saved_current_function_decl = NULL_TREE;
19777 /* If the entity being declared is a function, pretend that we
19778 are in its scope. If it is a `friend', it may have access to
19779 things that would not otherwise be accessible. */
19780 if (TREE_CODE (decl) == FUNCTION_DECL)
19782 saved_current_function_decl = current_function_decl;
19783 current_function_decl = decl;
19786 /* Perform access checks for template parameters. */
19787 cp_parser_perform_template_parameter_access_checks (checks);
19789 /* Perform the access control checks for the declarator and the
19790 decl-specifiers. */
19791 perform_deferred_access_checks (tf_warning_or_error);
19793 /* Restore the saved value. */
19794 if (TREE_CODE (decl) == FUNCTION_DECL)
19795 current_function_decl = saved_current_function_decl;
19798 /* Parse the initializer. */
19799 initializer = NULL_TREE;
19800 is_direct_init = false;
19801 is_non_constant_init = true;
19802 if (is_initialized)
19804 if (function_declarator_p (declarator))
19806 if (initialization_kind == CPP_EQ)
19807 initializer = cp_parser_pure_specifier (parser);
19808 else
19810 /* If the declaration was erroneous, we don't really
19811 know what the user intended, so just silently
19812 consume the initializer. */
19813 if (decl != error_mark_node)
19814 error_at (tmp_init_loc, "initializer provided for function");
19815 cp_parser_skip_to_closing_parenthesis (parser,
19816 /*recovering=*/true,
19817 /*or_comma=*/false,
19818 /*consume_paren=*/true);
19821 else
19823 /* We want to record the extra mangling scope for in-class
19824 initializers of class members and initializers of static data
19825 member templates. The former involves deferring
19826 parsing of the initializer until end of class as with default
19827 arguments. So right here we only handle the latter. */
19828 if (!member_p && processing_template_decl && decl != error_mark_node)
19829 start_lambda_scope (decl);
19830 initializer = cp_parser_initializer (parser,
19831 &is_direct_init,
19832 &is_non_constant_init);
19833 if (!member_p && processing_template_decl && decl != error_mark_node)
19834 finish_lambda_scope ();
19835 if (initializer == error_mark_node)
19836 cp_parser_skip_to_end_of_statement (parser);
19840 /* The old parser allows attributes to appear after a parenthesized
19841 initializer. Mark Mitchell proposed removing this functionality
19842 on the GCC mailing lists on 2002-08-13. This parser accepts the
19843 attributes -- but ignores them. Made a permerror in GCC 8. */
19844 if (cp_parser_allow_gnu_extensions_p (parser)
19845 && initialization_kind == CPP_OPEN_PAREN
19846 && cp_parser_attributes_opt (parser)
19847 && permerror (input_location,
19848 "attributes after parenthesized initializer ignored"))
19850 static bool hint;
19851 if (flag_permissive && !hint)
19853 hint = true;
19854 inform (input_location,
19855 "this flexibility is deprecated and will be removed");
19859 /* And now complain about a non-function implicit template. */
19860 if (bogus_implicit_tmpl && decl != error_mark_node)
19861 error_at (DECL_SOURCE_LOCATION (decl),
19862 "non-function %qD declared as implicit template", decl);
19864 /* For an in-class declaration, use `grokfield' to create the
19865 declaration. */
19866 if (member_p)
19868 if (pushed_scope)
19870 pop_scope (pushed_scope);
19871 pushed_scope = NULL_TREE;
19873 decl = grokfield (declarator, decl_specifiers,
19874 initializer, !is_non_constant_init,
19875 /*asmspec=*/NULL_TREE,
19876 attr_chainon (attributes, prefix_attributes));
19877 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19878 cp_parser_save_default_args (parser, decl);
19879 cp_finalize_omp_declare_simd (parser, decl);
19880 cp_finalize_oacc_routine (parser, decl, false);
19883 /* Finish processing the declaration. But, skip member
19884 declarations. */
19885 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19887 cp_finish_decl (decl,
19888 initializer, !is_non_constant_init,
19889 asm_specification,
19890 /* If the initializer is in parentheses, then this is
19891 a direct-initialization, which means that an
19892 `explicit' constructor is OK. Otherwise, an
19893 `explicit' constructor cannot be used. */
19894 ((is_direct_init || !is_initialized)
19895 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19897 else if ((cxx_dialect != cxx98) && friend_p
19898 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19899 /* Core issue #226 (C++0x only): A default template-argument
19900 shall not be specified in a friend class template
19901 declaration. */
19902 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19903 /*is_partial=*/false, /*is_friend_decl=*/1);
19905 if (!friend_p && pushed_scope)
19906 pop_scope (pushed_scope);
19908 if (function_declarator_p (declarator)
19909 && parser->fully_implicit_function_template_p)
19911 if (member_p)
19912 decl = finish_fully_implicit_template (parser, decl);
19913 else
19914 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19917 if (auto_result && is_initialized && decl_specifiers->type
19918 && type_uses_auto (decl_specifiers->type))
19919 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19921 return decl;
19924 /* Parse a declarator.
19926 declarator:
19927 direct-declarator
19928 ptr-operator declarator
19930 abstract-declarator:
19931 ptr-operator abstract-declarator [opt]
19932 direct-abstract-declarator
19934 GNU Extensions:
19936 declarator:
19937 attributes [opt] direct-declarator
19938 attributes [opt] ptr-operator declarator
19940 abstract-declarator:
19941 attributes [opt] ptr-operator abstract-declarator [opt]
19942 attributes [opt] direct-abstract-declarator
19944 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19945 detect constructors, destructors, deduction guides, or conversion operators.
19946 It is set to -1 if the declarator is a name, and +1 if it is a
19947 function. Otherwise it is set to zero. Usually you just want to
19948 test for >0, but internally the negative value is used.
19950 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19951 a decl-specifier-seq unless it declares a constructor, destructor,
19952 or conversion. It might seem that we could check this condition in
19953 semantic analysis, rather than parsing, but that makes it difficult
19954 to handle something like `f()'. We want to notice that there are
19955 no decl-specifiers, and therefore realize that this is an
19956 expression, not a declaration.)
19958 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19959 the declarator is a direct-declarator of the form "(...)".
19961 MEMBER_P is true iff this declarator is a member-declarator.
19963 FRIEND_P is true iff this declarator is a friend. */
19965 static cp_declarator *
19966 cp_parser_declarator (cp_parser* parser,
19967 cp_parser_declarator_kind dcl_kind,
19968 int* ctor_dtor_or_conv_p,
19969 bool* parenthesized_p,
19970 bool member_p, bool friend_p)
19972 cp_declarator *declarator;
19973 enum tree_code code;
19974 cp_cv_quals cv_quals;
19975 tree class_type;
19976 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19978 /* Assume this is not a constructor, destructor, or type-conversion
19979 operator. */
19980 if (ctor_dtor_or_conv_p)
19981 *ctor_dtor_or_conv_p = 0;
19983 if (cp_parser_allow_gnu_extensions_p (parser))
19984 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19986 /* Check for the ptr-operator production. */
19987 cp_parser_parse_tentatively (parser);
19988 /* Parse the ptr-operator. */
19989 code = cp_parser_ptr_operator (parser,
19990 &class_type,
19991 &cv_quals,
19992 &std_attributes);
19994 /* If that worked, then we have a ptr-operator. */
19995 if (cp_parser_parse_definitely (parser))
19997 /* If a ptr-operator was found, then this declarator was not
19998 parenthesized. */
19999 if (parenthesized_p)
20000 *parenthesized_p = true;
20001 /* The dependent declarator is optional if we are parsing an
20002 abstract-declarator. */
20003 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20004 cp_parser_parse_tentatively (parser);
20006 /* Parse the dependent declarator. */
20007 declarator = cp_parser_declarator (parser, dcl_kind,
20008 /*ctor_dtor_or_conv_p=*/NULL,
20009 /*parenthesized_p=*/NULL,
20010 /*member_p=*/false,
20011 friend_p);
20013 /* If we are parsing an abstract-declarator, we must handle the
20014 case where the dependent declarator is absent. */
20015 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20016 && !cp_parser_parse_definitely (parser))
20017 declarator = NULL;
20019 declarator = cp_parser_make_indirect_declarator
20020 (code, class_type, cv_quals, declarator, std_attributes);
20022 /* Everything else is a direct-declarator. */
20023 else
20025 if (parenthesized_p)
20026 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20027 CPP_OPEN_PAREN);
20028 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20029 ctor_dtor_or_conv_p,
20030 member_p, friend_p);
20033 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20034 declarator->attributes = gnu_attributes;
20035 return declarator;
20038 /* Parse a direct-declarator or direct-abstract-declarator.
20040 direct-declarator:
20041 declarator-id
20042 direct-declarator ( parameter-declaration-clause )
20043 cv-qualifier-seq [opt]
20044 ref-qualifier [opt]
20045 exception-specification [opt]
20046 direct-declarator [ constant-expression [opt] ]
20047 ( declarator )
20049 direct-abstract-declarator:
20050 direct-abstract-declarator [opt]
20051 ( parameter-declaration-clause )
20052 cv-qualifier-seq [opt]
20053 ref-qualifier [opt]
20054 exception-specification [opt]
20055 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20056 ( abstract-declarator )
20058 Returns a representation of the declarator. DCL_KIND is
20059 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20060 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20061 we are parsing a direct-declarator. It is
20062 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20063 of ambiguity we prefer an abstract declarator, as per
20064 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20065 as for cp_parser_declarator. */
20067 static cp_declarator *
20068 cp_parser_direct_declarator (cp_parser* parser,
20069 cp_parser_declarator_kind dcl_kind,
20070 int* ctor_dtor_or_conv_p,
20071 bool member_p, bool friend_p)
20073 cp_token *token;
20074 cp_declarator *declarator = NULL;
20075 tree scope = NULL_TREE;
20076 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20077 bool saved_in_declarator_p = parser->in_declarator_p;
20078 bool first = true;
20079 tree pushed_scope = NULL_TREE;
20080 cp_token *open_paren = NULL, *close_paren = NULL;
20082 while (true)
20084 /* Peek at the next token. */
20085 token = cp_lexer_peek_token (parser->lexer);
20086 if (token->type == CPP_OPEN_PAREN)
20088 /* This is either a parameter-declaration-clause, or a
20089 parenthesized declarator. When we know we are parsing a
20090 named declarator, it must be a parenthesized declarator
20091 if FIRST is true. For instance, `(int)' is a
20092 parameter-declaration-clause, with an omitted
20093 direct-abstract-declarator. But `((*))', is a
20094 parenthesized abstract declarator. Finally, when T is a
20095 template parameter `(T)' is a
20096 parameter-declaration-clause, and not a parenthesized
20097 named declarator.
20099 We first try and parse a parameter-declaration-clause,
20100 and then try a nested declarator (if FIRST is true).
20102 It is not an error for it not to be a
20103 parameter-declaration-clause, even when FIRST is
20104 false. Consider,
20106 int i (int);
20107 int i (3);
20109 The first is the declaration of a function while the
20110 second is the definition of a variable, including its
20111 initializer.
20113 Having seen only the parenthesis, we cannot know which of
20114 these two alternatives should be selected. Even more
20115 complex are examples like:
20117 int i (int (a));
20118 int i (int (3));
20120 The former is a function-declaration; the latter is a
20121 variable initialization.
20123 Thus again, we try a parameter-declaration-clause, and if
20124 that fails, we back out and return. */
20126 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20128 tree params;
20129 bool is_declarator = false;
20131 open_paren = NULL;
20133 /* In a member-declarator, the only valid interpretation
20134 of a parenthesis is the start of a
20135 parameter-declaration-clause. (It is invalid to
20136 initialize a static data member with a parenthesized
20137 initializer; only the "=" form of initialization is
20138 permitted.) */
20139 if (!member_p)
20140 cp_parser_parse_tentatively (parser);
20142 /* Consume the `('. */
20143 matching_parens parens;
20144 parens.consume_open (parser);
20145 if (first)
20147 /* If this is going to be an abstract declarator, we're
20148 in a declarator and we can't have default args. */
20149 parser->default_arg_ok_p = false;
20150 parser->in_declarator_p = true;
20153 begin_scope (sk_function_parms, NULL_TREE);
20155 /* Parse the parameter-declaration-clause. */
20156 params = cp_parser_parameter_declaration_clause (parser);
20158 /* Consume the `)'. */
20159 parens.require_close (parser);
20161 /* If all went well, parse the cv-qualifier-seq,
20162 ref-qualifier and the exception-specification. */
20163 if (member_p || cp_parser_parse_definitely (parser))
20165 cp_cv_quals cv_quals;
20166 cp_virt_specifiers virt_specifiers;
20167 cp_ref_qualifier ref_qual;
20168 tree exception_specification;
20169 tree late_return;
20170 tree attrs;
20171 bool memfn = (member_p || (pushed_scope
20172 && CLASS_TYPE_P (pushed_scope)));
20174 is_declarator = true;
20176 if (ctor_dtor_or_conv_p)
20177 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20178 first = false;
20180 /* Parse the cv-qualifier-seq. */
20181 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20182 /* Parse the ref-qualifier. */
20183 ref_qual = cp_parser_ref_qualifier_opt (parser);
20184 /* Parse the tx-qualifier. */
20185 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20186 /* And the exception-specification. */
20187 exception_specification
20188 = cp_parser_exception_specification_opt (parser);
20190 attrs = cp_parser_std_attribute_spec_seq (parser);
20192 /* In here, we handle cases where attribute is used after
20193 the function declaration. For example:
20194 void func (int x) __attribute__((vector(..))); */
20195 tree gnu_attrs = NULL_TREE;
20196 tree requires_clause = NULL_TREE;
20197 late_return = (cp_parser_late_return_type_opt
20198 (parser, declarator, requires_clause,
20199 memfn ? cv_quals : -1));
20201 /* Parse the virt-specifier-seq. */
20202 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20204 /* Create the function-declarator. */
20205 declarator = make_call_declarator (declarator,
20206 params,
20207 cv_quals,
20208 virt_specifiers,
20209 ref_qual,
20210 tx_qual,
20211 exception_specification,
20212 late_return,
20213 requires_clause);
20214 declarator->std_attributes = attrs;
20215 declarator->attributes = gnu_attrs;
20216 /* Any subsequent parameter lists are to do with
20217 return type, so are not those of the declared
20218 function. */
20219 parser->default_arg_ok_p = false;
20222 /* Remove the function parms from scope. */
20223 pop_bindings_and_leave_scope ();
20225 if (is_declarator)
20226 /* Repeat the main loop. */
20227 continue;
20230 /* If this is the first, we can try a parenthesized
20231 declarator. */
20232 if (first)
20234 bool saved_in_type_id_in_expr_p;
20236 parser->default_arg_ok_p = saved_default_arg_ok_p;
20237 parser->in_declarator_p = saved_in_declarator_p;
20239 open_paren = token;
20240 /* Consume the `('. */
20241 matching_parens parens;
20242 parens.consume_open (parser);
20243 /* Parse the nested declarator. */
20244 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20245 parser->in_type_id_in_expr_p = true;
20246 declarator
20247 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20248 /*parenthesized_p=*/NULL,
20249 member_p, friend_p);
20250 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20251 first = false;
20252 /* Expect a `)'. */
20253 close_paren = cp_lexer_peek_token (parser->lexer);
20254 if (!parens.require_close (parser))
20255 declarator = cp_error_declarator;
20256 if (declarator == cp_error_declarator)
20257 break;
20259 goto handle_declarator;
20261 /* Otherwise, we must be done. */
20262 else
20263 break;
20265 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20266 && token->type == CPP_OPEN_SQUARE
20267 && !cp_next_tokens_can_be_attribute_p (parser))
20269 /* Parse an array-declarator. */
20270 tree bounds, attrs;
20272 if (ctor_dtor_or_conv_p)
20273 *ctor_dtor_or_conv_p = 0;
20275 open_paren = NULL;
20276 first = false;
20277 parser->default_arg_ok_p = false;
20278 parser->in_declarator_p = true;
20279 /* Consume the `['. */
20280 cp_lexer_consume_token (parser->lexer);
20281 /* Peek at the next token. */
20282 token = cp_lexer_peek_token (parser->lexer);
20283 /* If the next token is `]', then there is no
20284 constant-expression. */
20285 if (token->type != CPP_CLOSE_SQUARE)
20287 bool non_constant_p;
20288 bounds
20289 = cp_parser_constant_expression (parser,
20290 /*allow_non_constant=*/true,
20291 &non_constant_p);
20292 if (!non_constant_p)
20293 /* OK */;
20294 else if (error_operand_p (bounds))
20295 /* Already gave an error. */;
20296 else if (!parser->in_function_body
20297 || current_binding_level->kind == sk_function_parms)
20299 /* Normally, the array bound must be an integral constant
20300 expression. However, as an extension, we allow VLAs
20301 in function scopes as long as they aren't part of a
20302 parameter declaration. */
20303 cp_parser_error (parser,
20304 "array bound is not an integer constant");
20305 bounds = error_mark_node;
20307 else if (processing_template_decl
20308 && !type_dependent_expression_p (bounds))
20310 /* Remember this wasn't a constant-expression. */
20311 bounds = build_nop (TREE_TYPE (bounds), bounds);
20312 TREE_SIDE_EFFECTS (bounds) = 1;
20315 else
20316 bounds = NULL_TREE;
20317 /* Look for the closing `]'. */
20318 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20320 declarator = cp_error_declarator;
20321 break;
20324 attrs = cp_parser_std_attribute_spec_seq (parser);
20325 declarator = make_array_declarator (declarator, bounds);
20326 declarator->std_attributes = attrs;
20328 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20331 tree qualifying_scope;
20332 tree unqualified_name;
20333 tree attrs;
20334 special_function_kind sfk;
20335 bool abstract_ok;
20336 bool pack_expansion_p = false;
20337 cp_token *declarator_id_start_token;
20339 /* Parse a declarator-id */
20340 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20341 if (abstract_ok)
20343 cp_parser_parse_tentatively (parser);
20345 /* If we see an ellipsis, we should be looking at a
20346 parameter pack. */
20347 if (token->type == CPP_ELLIPSIS)
20349 /* Consume the `...' */
20350 cp_lexer_consume_token (parser->lexer);
20352 pack_expansion_p = true;
20356 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20357 unqualified_name
20358 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20359 qualifying_scope = parser->scope;
20360 if (abstract_ok)
20362 bool okay = false;
20364 if (!unqualified_name && pack_expansion_p)
20366 /* Check whether an error occurred. */
20367 okay = !cp_parser_error_occurred (parser);
20369 /* We already consumed the ellipsis to mark a
20370 parameter pack, but we have no way to report it,
20371 so abort the tentative parse. We will be exiting
20372 immediately anyway. */
20373 cp_parser_abort_tentative_parse (parser);
20375 else
20376 okay = cp_parser_parse_definitely (parser);
20378 if (!okay)
20379 unqualified_name = error_mark_node;
20380 else if (unqualified_name
20381 && (qualifying_scope
20382 || (!identifier_p (unqualified_name))))
20384 cp_parser_error (parser, "expected unqualified-id");
20385 unqualified_name = error_mark_node;
20389 if (!unqualified_name)
20390 return NULL;
20391 if (unqualified_name == error_mark_node)
20393 declarator = cp_error_declarator;
20394 pack_expansion_p = false;
20395 declarator->parameter_pack_p = false;
20396 break;
20399 attrs = cp_parser_std_attribute_spec_seq (parser);
20401 if (qualifying_scope && at_namespace_scope_p ()
20402 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20404 /* In the declaration of a member of a template class
20405 outside of the class itself, the SCOPE will sometimes
20406 be a TYPENAME_TYPE. For example, given:
20408 template <typename T>
20409 int S<T>::R::i = 3;
20411 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20412 this context, we must resolve S<T>::R to an ordinary
20413 type, rather than a typename type.
20415 The reason we normally avoid resolving TYPENAME_TYPEs
20416 is that a specialization of `S' might render
20417 `S<T>::R' not a type. However, if `S' is
20418 specialized, then this `i' will not be used, so there
20419 is no harm in resolving the types here. */
20420 tree type;
20422 /* Resolve the TYPENAME_TYPE. */
20423 type = resolve_typename_type (qualifying_scope,
20424 /*only_current_p=*/false);
20425 /* If that failed, the declarator is invalid. */
20426 if (TREE_CODE (type) == TYPENAME_TYPE)
20428 if (typedef_variant_p (type))
20429 error_at (declarator_id_start_token->location,
20430 "cannot define member of dependent typedef "
20431 "%qT", type);
20432 else
20433 error_at (declarator_id_start_token->location,
20434 "%<%T::%E%> is not a type",
20435 TYPE_CONTEXT (qualifying_scope),
20436 TYPE_IDENTIFIER (qualifying_scope));
20438 qualifying_scope = type;
20441 sfk = sfk_none;
20443 if (unqualified_name)
20445 tree class_type;
20447 if (qualifying_scope
20448 && CLASS_TYPE_P (qualifying_scope))
20449 class_type = qualifying_scope;
20450 else
20451 class_type = current_class_type;
20453 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20455 tree name_type = TREE_TYPE (unqualified_name);
20457 if (!class_type || !same_type_p (name_type, class_type))
20459 /* We do not attempt to print the declarator
20460 here because we do not have enough
20461 information about its original syntactic
20462 form. */
20463 cp_parser_error (parser, "invalid declarator");
20464 declarator = cp_error_declarator;
20465 break;
20467 else if (qualifying_scope
20468 && CLASSTYPE_USE_TEMPLATE (name_type))
20470 error_at (declarator_id_start_token->location,
20471 "invalid use of constructor as a template");
20472 inform (declarator_id_start_token->location,
20473 "use %<%T::%D%> instead of %<%T::%D%> to "
20474 "name the constructor in a qualified name",
20475 class_type,
20476 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20477 class_type, name_type);
20478 declarator = cp_error_declarator;
20479 break;
20481 unqualified_name = constructor_name (class_type);
20484 if (class_type)
20486 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20487 sfk = sfk_destructor;
20488 else if (identifier_p (unqualified_name)
20489 && IDENTIFIER_CONV_OP_P (unqualified_name))
20490 sfk = sfk_conversion;
20491 else if (/* There's no way to declare a constructor
20492 for an unnamed type, even if the type
20493 got a name for linkage purposes. */
20494 !TYPE_WAS_UNNAMED (class_type)
20495 /* Handle correctly (c++/19200):
20497 struct S {
20498 struct T{};
20499 friend void S(T);
20502 and also:
20504 namespace N {
20505 void S();
20508 struct S {
20509 friend void N::S();
20510 }; */
20511 && (!friend_p || class_type == qualifying_scope)
20512 && constructor_name_p (unqualified_name,
20513 class_type))
20514 sfk = sfk_constructor;
20515 else if (is_overloaded_fn (unqualified_name)
20516 && DECL_CONSTRUCTOR_P (get_first_fn
20517 (unqualified_name)))
20518 sfk = sfk_constructor;
20520 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20521 *ctor_dtor_or_conv_p = -1;
20524 declarator = make_id_declarator (qualifying_scope,
20525 unqualified_name,
20526 sfk);
20527 declarator->std_attributes = attrs;
20528 declarator->id_loc = token->location;
20529 declarator->parameter_pack_p = pack_expansion_p;
20531 if (pack_expansion_p)
20532 maybe_warn_variadic_templates ();
20535 handle_declarator:;
20536 scope = get_scope_of_declarator (declarator);
20537 if (scope)
20539 /* Any names that appear after the declarator-id for a
20540 member are looked up in the containing scope. */
20541 if (at_function_scope_p ())
20543 /* But declarations with qualified-ids can't appear in a
20544 function. */
20545 cp_parser_error (parser, "qualified-id in declaration");
20546 declarator = cp_error_declarator;
20547 break;
20549 pushed_scope = push_scope (scope);
20551 parser->in_declarator_p = true;
20552 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20553 || (declarator && declarator->kind == cdk_id))
20554 /* Default args are only allowed on function
20555 declarations. */
20556 parser->default_arg_ok_p = saved_default_arg_ok_p;
20557 else
20558 parser->default_arg_ok_p = false;
20560 first = false;
20562 /* We're done. */
20563 else
20564 break;
20567 /* For an abstract declarator, we might wind up with nothing at this
20568 point. That's an error; the declarator is not optional. */
20569 if (!declarator)
20570 cp_parser_error (parser, "expected declarator");
20571 else if (open_paren)
20573 /* Record overly parenthesized declarator so we can give a
20574 diagnostic about confusing decl/expr disambiguation. */
20575 if (declarator->kind == cdk_array)
20577 /* If the open and close parens are on different lines, this
20578 is probably a formatting thing, so ignore. */
20579 expanded_location open = expand_location (open_paren->location);
20580 expanded_location close = expand_location (close_paren->location);
20581 if (open.line != close.line || open.file != close.file)
20582 open_paren = NULL;
20584 if (open_paren)
20585 declarator->parenthesized = open_paren->location;
20588 /* If we entered a scope, we must exit it now. */
20589 if (pushed_scope)
20590 pop_scope (pushed_scope);
20592 parser->default_arg_ok_p = saved_default_arg_ok_p;
20593 parser->in_declarator_p = saved_in_declarator_p;
20595 return declarator;
20598 /* Parse a ptr-operator.
20600 ptr-operator:
20601 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20602 * cv-qualifier-seq [opt]
20604 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20605 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20607 GNU Extension:
20609 ptr-operator:
20610 & cv-qualifier-seq [opt]
20612 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20613 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20614 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20615 filled in with the TYPE containing the member. *CV_QUALS is
20616 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20617 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20618 Note that the tree codes returned by this function have nothing
20619 to do with the types of trees that will be eventually be created
20620 to represent the pointer or reference type being parsed. They are
20621 just constants with suggestive names. */
20622 static enum tree_code
20623 cp_parser_ptr_operator (cp_parser* parser,
20624 tree* type,
20625 cp_cv_quals *cv_quals,
20626 tree *attributes)
20628 enum tree_code code = ERROR_MARK;
20629 cp_token *token;
20630 tree attrs = NULL_TREE;
20632 /* Assume that it's not a pointer-to-member. */
20633 *type = NULL_TREE;
20634 /* And that there are no cv-qualifiers. */
20635 *cv_quals = TYPE_UNQUALIFIED;
20637 /* Peek at the next token. */
20638 token = cp_lexer_peek_token (parser->lexer);
20640 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20641 if (token->type == CPP_MULT)
20642 code = INDIRECT_REF;
20643 else if (token->type == CPP_AND)
20644 code = ADDR_EXPR;
20645 else if ((cxx_dialect != cxx98) &&
20646 token->type == CPP_AND_AND) /* C++0x only */
20647 code = NON_LVALUE_EXPR;
20649 if (code != ERROR_MARK)
20651 /* Consume the `*', `&' or `&&'. */
20652 cp_lexer_consume_token (parser->lexer);
20654 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20655 `&', if we are allowing GNU extensions. (The only qualifier
20656 that can legally appear after `&' is `restrict', but that is
20657 enforced during semantic analysis. */
20658 if (code == INDIRECT_REF
20659 || cp_parser_allow_gnu_extensions_p (parser))
20660 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20662 attrs = cp_parser_std_attribute_spec_seq (parser);
20663 if (attributes != NULL)
20664 *attributes = attrs;
20666 else
20668 /* Try the pointer-to-member case. */
20669 cp_parser_parse_tentatively (parser);
20670 /* Look for the optional `::' operator. */
20671 cp_parser_global_scope_opt (parser,
20672 /*current_scope_valid_p=*/false);
20673 /* Look for the nested-name specifier. */
20674 token = cp_lexer_peek_token (parser->lexer);
20675 cp_parser_nested_name_specifier (parser,
20676 /*typename_keyword_p=*/false,
20677 /*check_dependency_p=*/true,
20678 /*type_p=*/false,
20679 /*is_declaration=*/false);
20680 /* If we found it, and the next token is a `*', then we are
20681 indeed looking at a pointer-to-member operator. */
20682 if (!cp_parser_error_occurred (parser)
20683 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20685 /* Indicate that the `*' operator was used. */
20686 code = INDIRECT_REF;
20688 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20689 error_at (token->location, "%qD is a namespace", parser->scope);
20690 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20691 error_at (token->location, "cannot form pointer to member of "
20692 "non-class %q#T", parser->scope);
20693 else
20695 /* The type of which the member is a member is given by the
20696 current SCOPE. */
20697 *type = parser->scope;
20698 /* The next name will not be qualified. */
20699 parser->scope = NULL_TREE;
20700 parser->qualifying_scope = NULL_TREE;
20701 parser->object_scope = NULL_TREE;
20702 /* Look for optional c++11 attributes. */
20703 attrs = cp_parser_std_attribute_spec_seq (parser);
20704 if (attributes != NULL)
20705 *attributes = attrs;
20706 /* Look for the optional cv-qualifier-seq. */
20707 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20710 /* If that didn't work we don't have a ptr-operator. */
20711 if (!cp_parser_parse_definitely (parser))
20712 cp_parser_error (parser, "expected ptr-operator");
20715 return code;
20718 /* Parse an (optional) cv-qualifier-seq.
20720 cv-qualifier-seq:
20721 cv-qualifier cv-qualifier-seq [opt]
20723 cv-qualifier:
20724 const
20725 volatile
20727 GNU Extension:
20729 cv-qualifier:
20730 __restrict__
20732 Returns a bitmask representing the cv-qualifiers. */
20734 static cp_cv_quals
20735 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20737 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20739 while (true)
20741 cp_token *token;
20742 cp_cv_quals cv_qualifier;
20744 /* Peek at the next token. */
20745 token = cp_lexer_peek_token (parser->lexer);
20746 /* See if it's a cv-qualifier. */
20747 switch (token->keyword)
20749 case RID_CONST:
20750 cv_qualifier = TYPE_QUAL_CONST;
20751 break;
20753 case RID_VOLATILE:
20754 cv_qualifier = TYPE_QUAL_VOLATILE;
20755 break;
20757 case RID_RESTRICT:
20758 cv_qualifier = TYPE_QUAL_RESTRICT;
20759 break;
20761 default:
20762 cv_qualifier = TYPE_UNQUALIFIED;
20763 break;
20766 if (!cv_qualifier)
20767 break;
20769 if (cv_quals & cv_qualifier)
20771 gcc_rich_location richloc (token->location);
20772 richloc.add_fixit_remove ();
20773 error_at (&richloc, "duplicate cv-qualifier");
20774 cp_lexer_purge_token (parser->lexer);
20776 else
20778 cp_lexer_consume_token (parser->lexer);
20779 cv_quals |= cv_qualifier;
20783 return cv_quals;
20786 /* Parse an (optional) ref-qualifier
20788 ref-qualifier:
20792 Returns cp_ref_qualifier representing ref-qualifier. */
20794 static cp_ref_qualifier
20795 cp_parser_ref_qualifier_opt (cp_parser* parser)
20797 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20799 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20800 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20801 return ref_qual;
20803 while (true)
20805 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20806 cp_token *token = cp_lexer_peek_token (parser->lexer);
20808 switch (token->type)
20810 case CPP_AND:
20811 curr_ref_qual = REF_QUAL_LVALUE;
20812 break;
20814 case CPP_AND_AND:
20815 curr_ref_qual = REF_QUAL_RVALUE;
20816 break;
20818 default:
20819 curr_ref_qual = REF_QUAL_NONE;
20820 break;
20823 if (!curr_ref_qual)
20824 break;
20825 else if (ref_qual)
20827 error_at (token->location, "multiple ref-qualifiers");
20828 cp_lexer_purge_token (parser->lexer);
20830 else
20832 ref_qual = curr_ref_qual;
20833 cp_lexer_consume_token (parser->lexer);
20837 return ref_qual;
20840 /* Parse an optional tx-qualifier.
20842 tx-qualifier:
20843 transaction_safe
20844 transaction_safe_dynamic */
20846 static tree
20847 cp_parser_tx_qualifier_opt (cp_parser *parser)
20849 cp_token *token = cp_lexer_peek_token (parser->lexer);
20850 if (token->type == CPP_NAME)
20852 tree name = token->u.value;
20853 const char *p = IDENTIFIER_POINTER (name);
20854 const int len = strlen ("transaction_safe");
20855 if (!strncmp (p, "transaction_safe", len))
20857 p += len;
20858 if (*p == '\0'
20859 || !strcmp (p, "_dynamic"))
20861 cp_lexer_consume_token (parser->lexer);
20862 if (!flag_tm)
20864 error ("%qE requires %<-fgnu-tm%>", name);
20865 return NULL_TREE;
20867 else
20868 return name;
20872 return NULL_TREE;
20875 /* Parse an (optional) virt-specifier-seq.
20877 virt-specifier-seq:
20878 virt-specifier virt-specifier-seq [opt]
20880 virt-specifier:
20881 override
20882 final
20884 Returns a bitmask representing the virt-specifiers. */
20886 static cp_virt_specifiers
20887 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20889 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20891 while (true)
20893 cp_token *token;
20894 cp_virt_specifiers virt_specifier;
20896 /* Peek at the next token. */
20897 token = cp_lexer_peek_token (parser->lexer);
20898 /* See if it's a virt-specifier-qualifier. */
20899 if (token->type != CPP_NAME)
20900 break;
20901 if (id_equal (token->u.value, "override"))
20903 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20904 virt_specifier = VIRT_SPEC_OVERRIDE;
20906 else if (id_equal (token->u.value, "final"))
20908 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20909 virt_specifier = VIRT_SPEC_FINAL;
20911 else if (id_equal (token->u.value, "__final"))
20913 virt_specifier = VIRT_SPEC_FINAL;
20915 else
20916 break;
20918 if (virt_specifiers & virt_specifier)
20920 gcc_rich_location richloc (token->location);
20921 richloc.add_fixit_remove ();
20922 error_at (&richloc, "duplicate virt-specifier");
20923 cp_lexer_purge_token (parser->lexer);
20925 else
20927 cp_lexer_consume_token (parser->lexer);
20928 virt_specifiers |= virt_specifier;
20931 return virt_specifiers;
20934 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20935 is in scope even though it isn't real. */
20937 void
20938 inject_this_parameter (tree ctype, cp_cv_quals quals)
20940 tree this_parm;
20942 if (current_class_ptr)
20944 /* We don't clear this between NSDMIs. Is it already what we want? */
20945 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20946 if (DECL_P (current_class_ptr)
20947 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20948 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20949 && cp_type_quals (type) == quals)
20950 return;
20953 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20954 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20955 current_class_ptr = NULL_TREE;
20956 current_class_ref
20957 = cp_build_fold_indirect_ref (this_parm);
20958 current_class_ptr = this_parm;
20961 /* Return true iff our current scope is a non-static data member
20962 initializer. */
20964 bool
20965 parsing_nsdmi (void)
20967 /* We recognize NSDMI context by the context-less 'this' pointer set up
20968 by the function above. */
20969 if (current_class_ptr
20970 && TREE_CODE (current_class_ptr) == PARM_DECL
20971 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20972 return true;
20973 return false;
20976 /* Parse a late-specified return type, if any. This is not a separate
20977 non-terminal, but part of a function declarator, which looks like
20979 -> trailing-type-specifier-seq abstract-declarator(opt)
20981 Returns the type indicated by the type-id.
20983 In addition to this, parse any queued up #pragma omp declare simd
20984 clauses, and #pragma acc routine clauses.
20986 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20987 function. */
20989 static tree
20990 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20991 tree& requires_clause, cp_cv_quals quals)
20993 cp_token *token;
20994 tree type = NULL_TREE;
20995 bool declare_simd_p = (parser->omp_declare_simd
20996 && declarator
20997 && declarator->kind == cdk_id);
20999 bool oacc_routine_p = (parser->oacc_routine
21000 && declarator
21001 && declarator->kind == cdk_id);
21003 /* Peek at the next token. */
21004 token = cp_lexer_peek_token (parser->lexer);
21005 /* A late-specified return type is indicated by an initial '->'. */
21006 if (token->type != CPP_DEREF
21007 && token->keyword != RID_REQUIRES
21008 && !(token->type == CPP_NAME
21009 && token->u.value == ridpointers[RID_REQUIRES])
21010 && !(declare_simd_p || oacc_routine_p))
21011 return NULL_TREE;
21013 tree save_ccp = current_class_ptr;
21014 tree save_ccr = current_class_ref;
21015 if (quals >= 0)
21017 /* DR 1207: 'this' is in scope in the trailing return type. */
21018 inject_this_parameter (current_class_type, quals);
21021 if (token->type == CPP_DEREF)
21023 /* Consume the ->. */
21024 cp_lexer_consume_token (parser->lexer);
21026 type = cp_parser_trailing_type_id (parser);
21029 /* Function declarations may be followed by a trailing
21030 requires-clause. */
21031 requires_clause = cp_parser_requires_clause_opt (parser);
21033 if (declare_simd_p)
21034 declarator->attributes
21035 = cp_parser_late_parsing_omp_declare_simd (parser,
21036 declarator->attributes);
21037 if (oacc_routine_p)
21038 declarator->attributes
21039 = cp_parser_late_parsing_oacc_routine (parser,
21040 declarator->attributes);
21042 if (quals >= 0)
21044 current_class_ptr = save_ccp;
21045 current_class_ref = save_ccr;
21048 return type;
21051 /* Parse a declarator-id.
21053 declarator-id:
21054 id-expression
21055 :: [opt] nested-name-specifier [opt] type-name
21057 In the `id-expression' case, the value returned is as for
21058 cp_parser_id_expression if the id-expression was an unqualified-id.
21059 If the id-expression was a qualified-id, then a SCOPE_REF is
21060 returned. The first operand is the scope (either a NAMESPACE_DECL
21061 or TREE_TYPE), but the second is still just a representation of an
21062 unqualified-id. */
21064 static tree
21065 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21067 tree id;
21068 /* The expression must be an id-expression. Assume that qualified
21069 names are the names of types so that:
21071 template <class T>
21072 int S<T>::R::i = 3;
21074 will work; we must treat `S<T>::R' as the name of a type.
21075 Similarly, assume that qualified names are templates, where
21076 required, so that:
21078 template <class T>
21079 int S<T>::R<T>::i = 3;
21081 will work, too. */
21082 id = cp_parser_id_expression (parser,
21083 /*template_keyword_p=*/false,
21084 /*check_dependency_p=*/false,
21085 /*template_p=*/NULL,
21086 /*declarator_p=*/true,
21087 optional_p);
21088 if (id && BASELINK_P (id))
21089 id = BASELINK_FUNCTIONS (id);
21090 return id;
21093 /* Parse a type-id.
21095 type-id:
21096 type-specifier-seq abstract-declarator [opt]
21098 Returns the TYPE specified. */
21100 static tree
21101 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21102 bool is_trailing_return)
21104 cp_decl_specifier_seq type_specifier_seq;
21105 cp_declarator *abstract_declarator;
21107 /* Parse the type-specifier-seq. */
21108 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21109 is_trailing_return,
21110 &type_specifier_seq);
21111 if (is_template_arg && type_specifier_seq.type
21112 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21113 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21114 /* A bare template name as a template argument is a template template
21115 argument, not a placeholder, so fail parsing it as a type argument. */
21117 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21118 cp_parser_simulate_error (parser);
21119 return error_mark_node;
21121 if (type_specifier_seq.type == error_mark_node)
21122 return error_mark_node;
21124 /* There might or might not be an abstract declarator. */
21125 cp_parser_parse_tentatively (parser);
21126 /* Look for the declarator. */
21127 abstract_declarator
21128 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21129 /*parenthesized_p=*/NULL,
21130 /*member_p=*/false,
21131 /*friend_p=*/false);
21132 /* Check to see if there really was a declarator. */
21133 if (!cp_parser_parse_definitely (parser))
21134 abstract_declarator = NULL;
21136 if (type_specifier_seq.type
21137 /* The concepts TS allows 'auto' as a type-id. */
21138 && (!flag_concepts || parser->in_type_id_in_expr_p)
21139 /* None of the valid uses of 'auto' in C++14 involve the type-id
21140 nonterminal, but it is valid in a trailing-return-type. */
21141 && !(cxx_dialect >= cxx14 && is_trailing_return))
21142 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21144 /* A type-id with type 'auto' is only ok if the abstract declarator
21145 is a function declarator with a late-specified return type.
21147 A type-id with 'auto' is also valid in a trailing-return-type
21148 in a compound-requirement. */
21149 if (abstract_declarator
21150 && abstract_declarator->kind == cdk_function
21151 && abstract_declarator->u.function.late_return_type)
21152 /* OK */;
21153 else if (parser->in_result_type_constraint_p)
21154 /* OK */;
21155 else
21157 location_t loc = type_specifier_seq.locations[ds_type_spec];
21158 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21160 error_at (loc, "missing template arguments after %qT",
21161 auto_node);
21162 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21163 tmpl);
21165 else
21166 error_at (loc, "invalid use of %qT", auto_node);
21167 return error_mark_node;
21171 return groktypename (&type_specifier_seq, abstract_declarator,
21172 is_template_arg);
21175 static tree
21176 cp_parser_type_id (cp_parser *parser)
21178 return cp_parser_type_id_1 (parser, false, false);
21181 static tree
21182 cp_parser_template_type_arg (cp_parser *parser)
21184 tree r;
21185 const char *saved_message = parser->type_definition_forbidden_message;
21186 parser->type_definition_forbidden_message
21187 = G_("types may not be defined in template arguments");
21188 r = cp_parser_type_id_1 (parser, true, false);
21189 parser->type_definition_forbidden_message = saved_message;
21190 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21192 error ("invalid use of %<auto%> in template argument");
21193 r = error_mark_node;
21195 return r;
21198 static tree
21199 cp_parser_trailing_type_id (cp_parser *parser)
21201 return cp_parser_type_id_1 (parser, false, true);
21204 /* Parse a type-specifier-seq.
21206 type-specifier-seq:
21207 type-specifier type-specifier-seq [opt]
21209 GNU extension:
21211 type-specifier-seq:
21212 attributes type-specifier-seq [opt]
21214 If IS_DECLARATION is true, we are at the start of a "condition" or
21215 exception-declaration, so we might be followed by a declarator-id.
21217 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21218 i.e. we've just seen "->".
21220 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21222 static void
21223 cp_parser_type_specifier_seq (cp_parser* parser,
21224 bool is_declaration,
21225 bool is_trailing_return,
21226 cp_decl_specifier_seq *type_specifier_seq)
21228 bool seen_type_specifier = false;
21229 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21230 cp_token *start_token = NULL;
21232 /* Clear the TYPE_SPECIFIER_SEQ. */
21233 clear_decl_specs (type_specifier_seq);
21235 /* In the context of a trailing return type, enum E { } is an
21236 elaborated-type-specifier followed by a function-body, not an
21237 enum-specifier. */
21238 if (is_trailing_return)
21239 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21241 /* Parse the type-specifiers and attributes. */
21242 while (true)
21244 tree type_specifier;
21245 bool is_cv_qualifier;
21247 /* Check for attributes first. */
21248 if (cp_next_tokens_can_be_attribute_p (parser))
21250 type_specifier_seq->attributes
21251 = attr_chainon (type_specifier_seq->attributes,
21252 cp_parser_attributes_opt (parser));
21253 continue;
21256 /* record the token of the beginning of the type specifier seq,
21257 for error reporting purposes*/
21258 if (!start_token)
21259 start_token = cp_lexer_peek_token (parser->lexer);
21261 /* Look for the type-specifier. */
21262 type_specifier = cp_parser_type_specifier (parser,
21263 flags,
21264 type_specifier_seq,
21265 /*is_declaration=*/false,
21266 NULL,
21267 &is_cv_qualifier);
21268 if (!type_specifier)
21270 /* If the first type-specifier could not be found, this is not a
21271 type-specifier-seq at all. */
21272 if (!seen_type_specifier)
21274 /* Set in_declarator_p to avoid skipping to the semicolon. */
21275 int in_decl = parser->in_declarator_p;
21276 parser->in_declarator_p = true;
21278 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21279 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21280 cp_parser_error (parser, "expected type-specifier");
21282 parser->in_declarator_p = in_decl;
21284 type_specifier_seq->type = error_mark_node;
21285 return;
21287 /* If subsequent type-specifiers could not be found, the
21288 type-specifier-seq is complete. */
21289 break;
21292 seen_type_specifier = true;
21293 /* The standard says that a condition can be:
21295 type-specifier-seq declarator = assignment-expression
21297 However, given:
21299 struct S {};
21300 if (int S = ...)
21302 we should treat the "S" as a declarator, not as a
21303 type-specifier. The standard doesn't say that explicitly for
21304 type-specifier-seq, but it does say that for
21305 decl-specifier-seq in an ordinary declaration. Perhaps it
21306 would be clearer just to allow a decl-specifier-seq here, and
21307 then add a semantic restriction that if any decl-specifiers
21308 that are not type-specifiers appear, the program is invalid. */
21309 if (is_declaration && !is_cv_qualifier)
21310 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21314 /* Return whether the function currently being declared has an associated
21315 template parameter list. */
21317 static bool
21318 function_being_declared_is_template_p (cp_parser* parser)
21320 if (!current_template_parms || processing_template_parmlist)
21321 return false;
21323 if (parser->implicit_template_scope)
21324 return true;
21326 if (at_class_scope_p ()
21327 && TYPE_BEING_DEFINED (current_class_type))
21328 return parser->num_template_parameter_lists != 0;
21330 return ((int) parser->num_template_parameter_lists > template_class_depth
21331 (current_class_type));
21334 /* Parse a parameter-declaration-clause.
21336 parameter-declaration-clause:
21337 parameter-declaration-list [opt] ... [opt]
21338 parameter-declaration-list , ...
21340 Returns a representation for the parameter declarations. A return
21341 value of NULL indicates a parameter-declaration-clause consisting
21342 only of an ellipsis. */
21344 static tree
21345 cp_parser_parameter_declaration_clause (cp_parser* parser)
21347 tree parameters;
21348 cp_token *token;
21349 bool ellipsis_p;
21351 temp_override<bool> cleanup
21352 (parser->auto_is_implicit_function_template_parm_p);
21354 if (!processing_specialization
21355 && !processing_template_parmlist
21356 && !processing_explicit_instantiation
21357 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21358 actual function or a random abstract declarator. */
21359 && parser->default_arg_ok_p)
21360 if (!current_function_decl
21361 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21362 parser->auto_is_implicit_function_template_parm_p = true;
21364 /* Peek at the next token. */
21365 token = cp_lexer_peek_token (parser->lexer);
21366 /* Check for trivial parameter-declaration-clauses. */
21367 if (token->type == CPP_ELLIPSIS)
21369 /* Consume the `...' token. */
21370 cp_lexer_consume_token (parser->lexer);
21371 return NULL_TREE;
21373 else if (token->type == CPP_CLOSE_PAREN)
21374 /* There are no parameters. */
21376 #ifdef SYSTEM_IMPLICIT_EXTERN_C
21377 if (in_system_header_at (input_location)
21378 && current_class_type == NULL
21379 && current_lang_name == lang_name_c)
21380 return NULL_TREE;
21381 else
21382 #endif
21383 return void_list_node;
21385 /* Check for `(void)', too, which is a special case. */
21386 else if (token->keyword == RID_VOID
21387 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21388 == CPP_CLOSE_PAREN))
21390 /* Consume the `void' token. */
21391 cp_lexer_consume_token (parser->lexer);
21392 /* There are no parameters. */
21393 return void_list_node;
21396 /* Parse the parameter-declaration-list. */
21397 parameters = cp_parser_parameter_declaration_list (parser);
21398 /* If a parse error occurred while parsing the
21399 parameter-declaration-list, then the entire
21400 parameter-declaration-clause is erroneous. */
21401 if (parameters == error_mark_node)
21402 return NULL_TREE;
21404 /* Peek at the next token. */
21405 token = cp_lexer_peek_token (parser->lexer);
21406 /* If it's a `,', the clause should terminate with an ellipsis. */
21407 if (token->type == CPP_COMMA)
21409 /* Consume the `,'. */
21410 cp_lexer_consume_token (parser->lexer);
21411 /* Expect an ellipsis. */
21412 ellipsis_p
21413 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21415 /* It might also be `...' if the optional trailing `,' was
21416 omitted. */
21417 else if (token->type == CPP_ELLIPSIS)
21419 /* Consume the `...' token. */
21420 cp_lexer_consume_token (parser->lexer);
21421 /* And remember that we saw it. */
21422 ellipsis_p = true;
21424 else
21425 ellipsis_p = false;
21427 /* Finish the parameter list. */
21428 if (!ellipsis_p)
21429 parameters = chainon (parameters, void_list_node);
21431 return parameters;
21434 /* Parse a parameter-declaration-list.
21436 parameter-declaration-list:
21437 parameter-declaration
21438 parameter-declaration-list , parameter-declaration
21440 Returns a representation of the parameter-declaration-list, as for
21441 cp_parser_parameter_declaration_clause. However, the
21442 `void_list_node' is never appended to the list. */
21444 static tree
21445 cp_parser_parameter_declaration_list (cp_parser* parser)
21447 tree parameters = NULL_TREE;
21448 tree *tail = &parameters;
21449 bool saved_in_unbraced_linkage_specification_p;
21450 int index = 0;
21452 /* The special considerations that apply to a function within an
21453 unbraced linkage specifications do not apply to the parameters
21454 to the function. */
21455 saved_in_unbraced_linkage_specification_p
21456 = parser->in_unbraced_linkage_specification_p;
21457 parser->in_unbraced_linkage_specification_p = false;
21459 /* Look for more parameters. */
21460 while (true)
21462 cp_parameter_declarator *parameter;
21463 tree decl = error_mark_node;
21464 bool parenthesized_p = false;
21466 /* Parse the parameter. */
21467 parameter
21468 = cp_parser_parameter_declaration (parser,
21469 /*template_parm_p=*/false,
21470 &parenthesized_p);
21472 /* We don't know yet if the enclosing context is deprecated, so wait
21473 and warn in grokparms if appropriate. */
21474 deprecated_state = DEPRECATED_SUPPRESS;
21476 if (parameter)
21478 decl = grokdeclarator (parameter->declarator,
21479 &parameter->decl_specifiers,
21480 PARM,
21481 parameter->default_argument != NULL_TREE,
21482 &parameter->decl_specifiers.attributes);
21483 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21484 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21487 deprecated_state = DEPRECATED_NORMAL;
21489 /* If a parse error occurred parsing the parameter declaration,
21490 then the entire parameter-declaration-list is erroneous. */
21491 if (decl == error_mark_node)
21493 parameters = error_mark_node;
21494 break;
21497 if (parameter->decl_specifiers.attributes)
21498 cplus_decl_attributes (&decl,
21499 parameter->decl_specifiers.attributes,
21501 if (DECL_NAME (decl))
21502 decl = pushdecl (decl);
21504 if (decl != error_mark_node)
21506 retrofit_lang_decl (decl);
21507 DECL_PARM_INDEX (decl) = ++index;
21508 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21511 /* Add the new parameter to the list. */
21512 *tail = build_tree_list (parameter->default_argument, decl);
21513 tail = &TREE_CHAIN (*tail);
21515 /* Peek at the next token. */
21516 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21517 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21518 /* These are for Objective-C++ */
21519 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21520 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21521 /* The parameter-declaration-list is complete. */
21522 break;
21523 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21525 cp_token *token;
21527 /* Peek at the next token. */
21528 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21529 /* If it's an ellipsis, then the list is complete. */
21530 if (token->type == CPP_ELLIPSIS)
21531 break;
21532 /* Otherwise, there must be more parameters. Consume the
21533 `,'. */
21534 cp_lexer_consume_token (parser->lexer);
21535 /* When parsing something like:
21537 int i(float f, double d)
21539 we can tell after seeing the declaration for "f" that we
21540 are not looking at an initialization of a variable "i",
21541 but rather at the declaration of a function "i".
21543 Due to the fact that the parsing of template arguments
21544 (as specified to a template-id) requires backtracking we
21545 cannot use this technique when inside a template argument
21546 list. */
21547 if (!parser->in_template_argument_list_p
21548 && !parser->in_type_id_in_expr_p
21549 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21550 /* However, a parameter-declaration of the form
21551 "float(f)" (which is a valid declaration of a
21552 parameter "f") can also be interpreted as an
21553 expression (the conversion of "f" to "float"). */
21554 && !parenthesized_p)
21555 cp_parser_commit_to_tentative_parse (parser);
21557 else
21559 cp_parser_error (parser, "expected %<,%> or %<...%>");
21560 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21561 cp_parser_skip_to_closing_parenthesis (parser,
21562 /*recovering=*/true,
21563 /*or_comma=*/false,
21564 /*consume_paren=*/false);
21565 break;
21569 parser->in_unbraced_linkage_specification_p
21570 = saved_in_unbraced_linkage_specification_p;
21572 /* Reset implicit_template_scope if we are about to leave the function
21573 parameter list that introduced it. Note that for out-of-line member
21574 definitions, there will be one or more class scopes before we get to
21575 the template parameter scope. */
21577 if (cp_binding_level *its = parser->implicit_template_scope)
21578 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21580 while (maybe_its->kind == sk_class)
21581 maybe_its = maybe_its->level_chain;
21582 if (maybe_its == its)
21584 parser->implicit_template_parms = 0;
21585 parser->implicit_template_scope = 0;
21589 return parameters;
21592 /* Parse a parameter declaration.
21594 parameter-declaration:
21595 decl-specifier-seq ... [opt] declarator
21596 decl-specifier-seq declarator = assignment-expression
21597 decl-specifier-seq ... [opt] abstract-declarator [opt]
21598 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21600 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21601 declares a template parameter. (In that case, a non-nested `>'
21602 token encountered during the parsing of the assignment-expression
21603 is not interpreted as a greater-than operator.)
21605 Returns a representation of the parameter, or NULL if an error
21606 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21607 true iff the declarator is of the form "(p)". */
21609 static cp_parameter_declarator *
21610 cp_parser_parameter_declaration (cp_parser *parser,
21611 bool template_parm_p,
21612 bool *parenthesized_p)
21614 int declares_class_or_enum;
21615 cp_decl_specifier_seq decl_specifiers;
21616 cp_declarator *declarator;
21617 tree default_argument;
21618 cp_token *token = NULL, *declarator_token_start = NULL;
21619 const char *saved_message;
21620 bool template_parameter_pack_p = false;
21622 /* In a template parameter, `>' is not an operator.
21624 [temp.param]
21626 When parsing a default template-argument for a non-type
21627 template-parameter, the first non-nested `>' is taken as the end
21628 of the template parameter-list rather than a greater-than
21629 operator. */
21631 /* Type definitions may not appear in parameter types. */
21632 saved_message = parser->type_definition_forbidden_message;
21633 parser->type_definition_forbidden_message
21634 = G_("types may not be defined in parameter types");
21636 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21637 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21638 (current_template_parms)) : 0);
21640 /* Parse the declaration-specifiers. */
21641 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21642 cp_parser_decl_specifier_seq (parser,
21643 CP_PARSER_FLAGS_NONE,
21644 &decl_specifiers,
21645 &declares_class_or_enum);
21647 /* Complain about missing 'typename' or other invalid type names. */
21648 if (!decl_specifiers.any_type_specifiers_p
21649 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21650 decl_specifiers.type = error_mark_node;
21652 /* If an error occurred, there's no reason to attempt to parse the
21653 rest of the declaration. */
21654 if (cp_parser_error_occurred (parser))
21656 parser->type_definition_forbidden_message = saved_message;
21657 return NULL;
21660 /* Peek at the next token. */
21661 token = cp_lexer_peek_token (parser->lexer);
21663 /* If the next token is a `)', `,', `=', `>', or `...', then there
21664 is no declarator. However, when variadic templates are enabled,
21665 there may be a declarator following `...'. */
21666 if (token->type == CPP_CLOSE_PAREN
21667 || token->type == CPP_COMMA
21668 || token->type == CPP_EQ
21669 || token->type == CPP_GREATER)
21671 declarator = NULL;
21672 if (parenthesized_p)
21673 *parenthesized_p = false;
21675 /* Otherwise, there should be a declarator. */
21676 else
21678 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21679 parser->default_arg_ok_p = false;
21681 /* After seeing a decl-specifier-seq, if the next token is not a
21682 "(", there is no possibility that the code is a valid
21683 expression. Therefore, if parsing tentatively, we commit at
21684 this point. */
21685 if (!parser->in_template_argument_list_p
21686 /* In an expression context, having seen:
21688 (int((char ...
21690 we cannot be sure whether we are looking at a
21691 function-type (taking a "char" as a parameter) or a cast
21692 of some object of type "char" to "int". */
21693 && !parser->in_type_id_in_expr_p
21694 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21695 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21696 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21697 cp_parser_commit_to_tentative_parse (parser);
21698 /* Parse the declarator. */
21699 declarator_token_start = token;
21700 declarator = cp_parser_declarator (parser,
21701 CP_PARSER_DECLARATOR_EITHER,
21702 /*ctor_dtor_or_conv_p=*/NULL,
21703 parenthesized_p,
21704 /*member_p=*/false,
21705 /*friend_p=*/false);
21706 parser->default_arg_ok_p = saved_default_arg_ok_p;
21707 /* After the declarator, allow more attributes. */
21708 decl_specifiers.attributes
21709 = attr_chainon (decl_specifiers.attributes,
21710 cp_parser_attributes_opt (parser));
21712 /* If the declarator is a template parameter pack, remember that and
21713 clear the flag in the declarator itself so we don't get errors
21714 from grokdeclarator. */
21715 if (template_parm_p && declarator && declarator->parameter_pack_p)
21717 declarator->parameter_pack_p = false;
21718 template_parameter_pack_p = true;
21722 /* If the next token is an ellipsis, and we have not seen a declarator
21723 name, and if either the type of the declarator contains parameter
21724 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21725 for, eg, abbreviated integral type names), then we actually have a
21726 parameter pack expansion expression. Otherwise, leave the ellipsis
21727 for a C-style variadic function. */
21728 token = cp_lexer_peek_token (parser->lexer);
21730 /* If a function parameter pack was specified and an implicit template
21731 parameter was introduced during cp_parser_parameter_declaration,
21732 change any implicit parameters introduced into packs. */
21733 if (parser->implicit_template_parms
21734 && ((token->type == CPP_ELLIPSIS
21735 && declarator_can_be_parameter_pack (declarator))
21736 || (declarator && declarator->parameter_pack_p)))
21738 int latest_template_parm_idx = TREE_VEC_LENGTH
21739 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21741 if (latest_template_parm_idx != template_parm_idx)
21742 decl_specifiers.type = convert_generic_types_to_packs
21743 (decl_specifiers.type,
21744 template_parm_idx, latest_template_parm_idx);
21747 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21749 tree type = decl_specifiers.type;
21751 if (type && DECL_P (type))
21752 type = TREE_TYPE (type);
21754 if (((type
21755 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21756 && (template_parm_p || uses_parameter_packs (type)))
21757 || (!type && template_parm_p))
21758 && declarator_can_be_parameter_pack (declarator))
21760 /* Consume the `...'. */
21761 cp_lexer_consume_token (parser->lexer);
21762 maybe_warn_variadic_templates ();
21764 /* Build a pack expansion type */
21765 if (template_parm_p)
21766 template_parameter_pack_p = true;
21767 else if (declarator)
21768 declarator->parameter_pack_p = true;
21769 else
21770 decl_specifiers.type = make_pack_expansion (type);
21774 /* The restriction on defining new types applies only to the type
21775 of the parameter, not to the default argument. */
21776 parser->type_definition_forbidden_message = saved_message;
21778 /* If the next token is `=', then process a default argument. */
21779 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21781 tree type = decl_specifiers.type;
21782 token = cp_lexer_peek_token (parser->lexer);
21783 /* If we are defining a class, then the tokens that make up the
21784 default argument must be saved and processed later. */
21785 if (!template_parm_p && at_class_scope_p ()
21786 && TYPE_BEING_DEFINED (current_class_type)
21787 && !LAMBDA_TYPE_P (current_class_type))
21788 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21790 // A constrained-type-specifier may declare a type template-parameter.
21791 else if (declares_constrained_type_template_parameter (type))
21792 default_argument
21793 = cp_parser_default_type_template_argument (parser);
21795 // A constrained-type-specifier may declare a template-template-parameter.
21796 else if (declares_constrained_template_template_parameter (type))
21797 default_argument
21798 = cp_parser_default_template_template_argument (parser);
21800 /* Outside of a class definition, we can just parse the
21801 assignment-expression. */
21802 else
21803 default_argument
21804 = cp_parser_default_argument (parser, template_parm_p);
21806 if (!parser->default_arg_ok_p)
21808 permerror (token->location,
21809 "default arguments are only "
21810 "permitted for function parameters");
21812 else if ((declarator && declarator->parameter_pack_p)
21813 || template_parameter_pack_p
21814 || (decl_specifiers.type
21815 && PACK_EXPANSION_P (decl_specifiers.type)))
21817 /* Find the name of the parameter pack. */
21818 cp_declarator *id_declarator = declarator;
21819 while (id_declarator && id_declarator->kind != cdk_id)
21820 id_declarator = id_declarator->declarator;
21822 if (id_declarator && id_declarator->kind == cdk_id)
21823 error_at (declarator_token_start->location,
21824 template_parm_p
21825 ? G_("template parameter pack %qD "
21826 "cannot have a default argument")
21827 : G_("parameter pack %qD cannot have "
21828 "a default argument"),
21829 id_declarator->u.id.unqualified_name);
21830 else
21831 error_at (declarator_token_start->location,
21832 template_parm_p
21833 ? G_("template parameter pack cannot have "
21834 "a default argument")
21835 : G_("parameter pack cannot have a "
21836 "default argument"));
21838 default_argument = NULL_TREE;
21841 else
21842 default_argument = NULL_TREE;
21844 /* Generate a location for the parameter, ranging from the start of the
21845 initial token to the end of the final token (using input_location for
21846 the latter, set up by cp_lexer_set_source_position_from_token when
21847 consuming tokens).
21849 If we have a identifier, then use it for the caret location, e.g.
21851 extern int callee (int one, int (*two)(int, int), float three);
21852 ~~~~~~^~~~~~~~~~~~~~
21854 otherwise, reuse the start location for the caret location e.g.:
21856 extern int callee (int one, int (*)(int, int), float three);
21857 ^~~~~~~~~~~~~~~~~
21860 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21861 ? declarator->id_loc
21862 : decl_spec_token_start->location);
21863 location_t param_loc = make_location (caret_loc,
21864 decl_spec_token_start->location,
21865 input_location);
21867 return make_parameter_declarator (&decl_specifiers,
21868 declarator,
21869 default_argument,
21870 param_loc,
21871 template_parameter_pack_p);
21874 /* Parse a default argument and return it.
21876 TEMPLATE_PARM_P is true if this is a default argument for a
21877 non-type template parameter. */
21878 static tree
21879 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21881 tree default_argument = NULL_TREE;
21882 bool saved_greater_than_is_operator_p;
21883 bool saved_local_variables_forbidden_p;
21884 bool non_constant_p, is_direct_init;
21886 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21887 set correctly. */
21888 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21889 parser->greater_than_is_operator_p = !template_parm_p;
21890 /* Local variable names (and the `this' keyword) may not
21891 appear in a default argument. */
21892 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21893 parser->local_variables_forbidden_p = true;
21894 /* Parse the assignment-expression. */
21895 if (template_parm_p)
21896 push_deferring_access_checks (dk_no_deferred);
21897 tree saved_class_ptr = NULL_TREE;
21898 tree saved_class_ref = NULL_TREE;
21899 /* The "this" pointer is not valid in a default argument. */
21900 if (cfun)
21902 saved_class_ptr = current_class_ptr;
21903 cp_function_chain->x_current_class_ptr = NULL_TREE;
21904 saved_class_ref = current_class_ref;
21905 cp_function_chain->x_current_class_ref = NULL_TREE;
21907 default_argument
21908 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21909 /* Restore the "this" pointer. */
21910 if (cfun)
21912 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21913 cp_function_chain->x_current_class_ref = saved_class_ref;
21915 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21916 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21917 if (template_parm_p)
21918 pop_deferring_access_checks ();
21919 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21920 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21922 return default_argument;
21925 /* Parse a function-body.
21927 function-body:
21928 compound_statement */
21930 static void
21931 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21933 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21934 ? BCS_TRY_BLOCK : BCS_NORMAL),
21935 true);
21938 /* Parse a ctor-initializer-opt followed by a function-body. Return
21939 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21940 is true we are parsing a function-try-block. */
21942 static void
21943 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21944 bool in_function_try_block)
21946 tree body, list;
21947 const bool check_body_p =
21948 DECL_CONSTRUCTOR_P (current_function_decl)
21949 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21950 tree last = NULL;
21952 /* Begin the function body. */
21953 body = begin_function_body ();
21954 /* Parse the optional ctor-initializer. */
21955 cp_parser_ctor_initializer_opt (parser);
21957 /* If we're parsing a constexpr constructor definition, we need
21958 to check that the constructor body is indeed empty. However,
21959 before we get to cp_parser_function_body lot of junk has been
21960 generated, so we can't just check that we have an empty block.
21961 Rather we take a snapshot of the outermost block, and check whether
21962 cp_parser_function_body changed its state. */
21963 if (check_body_p)
21965 list = cur_stmt_list;
21966 if (STATEMENT_LIST_TAIL (list))
21967 last = STATEMENT_LIST_TAIL (list)->stmt;
21969 /* Parse the function-body. */
21970 cp_parser_function_body (parser, in_function_try_block);
21971 if (check_body_p)
21972 check_constexpr_ctor_body (last, list, /*complain=*/true);
21973 /* Finish the function body. */
21974 finish_function_body (body);
21977 /* Parse an initializer.
21979 initializer:
21980 = initializer-clause
21981 ( expression-list )
21983 Returns an expression representing the initializer. If no
21984 initializer is present, NULL_TREE is returned.
21986 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21987 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21988 set to TRUE if there is no initializer present. If there is an
21989 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21990 is set to true; otherwise it is set to false. */
21992 static tree
21993 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21994 bool* non_constant_p, bool subexpression_p)
21996 cp_token *token;
21997 tree init;
21999 /* Peek at the next token. */
22000 token = cp_lexer_peek_token (parser->lexer);
22002 /* Let our caller know whether or not this initializer was
22003 parenthesized. */
22004 *is_direct_init = (token->type != CPP_EQ);
22005 /* Assume that the initializer is constant. */
22006 *non_constant_p = false;
22008 if (token->type == CPP_EQ)
22010 /* Consume the `='. */
22011 cp_lexer_consume_token (parser->lexer);
22012 /* Parse the initializer-clause. */
22013 init = cp_parser_initializer_clause (parser, non_constant_p);
22015 else if (token->type == CPP_OPEN_PAREN)
22017 vec<tree, va_gc> *vec;
22018 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22019 /*cast_p=*/false,
22020 /*allow_expansion_p=*/true,
22021 non_constant_p);
22022 if (vec == NULL)
22023 return error_mark_node;
22024 init = build_tree_list_vec (vec);
22025 release_tree_vector (vec);
22027 else if (token->type == CPP_OPEN_BRACE)
22029 cp_lexer_set_source_position (parser->lexer);
22030 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22031 init = cp_parser_braced_list (parser, non_constant_p);
22032 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22034 else
22036 /* Anything else is an error. */
22037 cp_parser_error (parser, "expected initializer");
22038 init = error_mark_node;
22041 if (!subexpression_p && check_for_bare_parameter_packs (init))
22042 init = error_mark_node;
22044 return init;
22047 /* Parse an initializer-clause.
22049 initializer-clause:
22050 assignment-expression
22051 braced-init-list
22053 Returns an expression representing the initializer.
22055 If the `assignment-expression' production is used the value
22056 returned is simply a representation for the expression.
22058 Otherwise, calls cp_parser_braced_list. */
22060 static cp_expr
22061 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22063 cp_expr initializer;
22065 /* Assume the expression is constant. */
22066 *non_constant_p = false;
22068 /* If it is not a `{', then we are looking at an
22069 assignment-expression. */
22070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22072 initializer
22073 = cp_parser_constant_expression (parser,
22074 /*allow_non_constant_p=*/true,
22075 non_constant_p);
22077 else
22078 initializer = cp_parser_braced_list (parser, non_constant_p);
22080 return initializer;
22083 /* Parse a brace-enclosed initializer list.
22085 braced-init-list:
22086 { initializer-list , [opt] }
22087 { designated-initializer-list , [opt] }
22090 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22091 the elements of the initializer-list (or NULL, if the last
22092 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22093 NULL_TREE. There is no way to detect whether or not the optional
22094 trailing `,' was provided. NON_CONSTANT_P is as for
22095 cp_parser_initializer. */
22097 static cp_expr
22098 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22100 tree initializer;
22101 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22103 /* Consume the `{' token. */
22104 matching_braces braces;
22105 braces.require_open (parser);
22106 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22107 initializer = make_node (CONSTRUCTOR);
22108 /* If it's not a `}', then there is a non-trivial initializer. */
22109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22111 /* Parse the initializer list. */
22112 CONSTRUCTOR_ELTS (initializer)
22113 = cp_parser_initializer_list (parser, non_constant_p);
22114 /* A trailing `,' token is allowed. */
22115 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22116 cp_lexer_consume_token (parser->lexer);
22118 else
22119 *non_constant_p = false;
22120 /* Now, there should be a trailing `}'. */
22121 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22122 braces.require_close (parser);
22123 TREE_TYPE (initializer) = init_list_type_node;
22125 cp_expr result (initializer);
22126 /* Build a location of the form:
22127 { ... }
22128 ^~~~~~~
22129 with caret==start at the open brace, finish at the close brace. */
22130 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22131 result.set_location (combined_loc);
22132 return result;
22135 /* Consume tokens up to, and including, the next non-nested closing `]'.
22136 Returns true iff we found a closing `]'. */
22138 static bool
22139 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22141 unsigned square_depth = 0;
22143 while (true)
22145 cp_token * token = cp_lexer_peek_token (parser->lexer);
22147 switch (token->type)
22149 case CPP_EOF:
22150 case CPP_PRAGMA_EOL:
22151 /* If we've run out of tokens, then there is no closing `]'. */
22152 return false;
22154 case CPP_OPEN_SQUARE:
22155 ++square_depth;
22156 break;
22158 case CPP_CLOSE_SQUARE:
22159 if (!square_depth--)
22161 cp_lexer_consume_token (parser->lexer);
22162 return true;
22164 break;
22166 default:
22167 break;
22170 /* Consume the token. */
22171 cp_lexer_consume_token (parser->lexer);
22175 /* Return true if we are looking at an array-designator, false otherwise. */
22177 static bool
22178 cp_parser_array_designator_p (cp_parser *parser)
22180 /* Consume the `['. */
22181 cp_lexer_consume_token (parser->lexer);
22183 cp_lexer_save_tokens (parser->lexer);
22185 /* Skip tokens until the next token is a closing square bracket.
22186 If we find the closing `]', and the next token is a `=', then
22187 we are looking at an array designator. */
22188 bool array_designator_p
22189 = (cp_parser_skip_to_closing_square_bracket (parser)
22190 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22192 /* Roll back the tokens we skipped. */
22193 cp_lexer_rollback_tokens (parser->lexer);
22195 return array_designator_p;
22198 /* Parse an initializer-list.
22200 initializer-list:
22201 initializer-clause ... [opt]
22202 initializer-list , initializer-clause ... [opt]
22204 C++2A Extension:
22206 designated-initializer-list:
22207 designated-initializer-clause
22208 designated-initializer-list , designated-initializer-clause
22210 designated-initializer-clause:
22211 designator brace-or-equal-initializer
22213 designator:
22214 . identifier
22216 GNU Extension:
22218 initializer-list:
22219 designation initializer-clause ...[opt]
22220 initializer-list , designation initializer-clause ...[opt]
22222 designation:
22223 . identifier =
22224 identifier :
22225 [ constant-expression ] =
22227 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22228 for the initializer. If the INDEX of the elt is non-NULL, it is the
22229 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22230 as for cp_parser_initializer. */
22232 static vec<constructor_elt, va_gc> *
22233 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22235 vec<constructor_elt, va_gc> *v = NULL;
22236 bool first_p = true;
22237 tree first_designator = NULL_TREE;
22239 /* Assume all of the expressions are constant. */
22240 *non_constant_p = false;
22242 /* Parse the rest of the list. */
22243 while (true)
22245 cp_token *token;
22246 tree designator;
22247 tree initializer;
22248 bool clause_non_constant_p;
22249 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22251 /* Handle the C++2A syntax, '. id ='. */
22252 if ((cxx_dialect >= cxx2a
22253 || cp_parser_allow_gnu_extensions_p (parser))
22254 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22255 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22256 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22257 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22258 == CPP_OPEN_BRACE)))
22260 if (cxx_dialect < cxx2a)
22261 pedwarn (loc, OPT_Wpedantic,
22262 "C++ designated initializers only available with "
22263 "-std=c++2a or -std=gnu++2a");
22264 /* Consume the `.'. */
22265 cp_lexer_consume_token (parser->lexer);
22266 /* Consume the identifier. */
22267 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22268 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22269 /* Consume the `='. */
22270 cp_lexer_consume_token (parser->lexer);
22272 /* Also, if the next token is an identifier and the following one is a
22273 colon, we are looking at the GNU designated-initializer
22274 syntax. */
22275 else if (cp_parser_allow_gnu_extensions_p (parser)
22276 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22277 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22278 == CPP_COLON))
22280 /* Warn the user that they are using an extension. */
22281 pedwarn (loc, OPT_Wpedantic,
22282 "ISO C++ does not allow GNU designated initializers");
22283 /* Consume the identifier. */
22284 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22285 /* Consume the `:'. */
22286 cp_lexer_consume_token (parser->lexer);
22288 /* Also handle C99 array designators, '[ const ] ='. */
22289 else if (cp_parser_allow_gnu_extensions_p (parser)
22290 && !c_dialect_objc ()
22291 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22293 /* In C++11, [ could start a lambda-introducer. */
22294 bool non_const = false;
22296 cp_parser_parse_tentatively (parser);
22298 if (!cp_parser_array_designator_p (parser))
22300 cp_parser_simulate_error (parser);
22301 designator = NULL_TREE;
22303 else
22305 designator = cp_parser_constant_expression (parser, true,
22306 &non_const);
22307 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22308 cp_parser_require (parser, CPP_EQ, RT_EQ);
22311 if (!cp_parser_parse_definitely (parser))
22312 designator = NULL_TREE;
22313 else if (non_const
22314 && (!require_potential_rvalue_constant_expression
22315 (designator)))
22316 designator = NULL_TREE;
22317 if (designator)
22318 /* Warn the user that they are using an extension. */
22319 pedwarn (loc, OPT_Wpedantic,
22320 "ISO C++ does not allow C99 designated initializers");
22322 else
22323 designator = NULL_TREE;
22325 if (first_p)
22327 first_designator = designator;
22328 first_p = false;
22330 else if (cxx_dialect >= cxx2a
22331 && first_designator != error_mark_node
22332 && (!first_designator != !designator))
22334 error_at (loc, "either all initializer clauses should be designated "
22335 "or none of them should be");
22336 first_designator = error_mark_node;
22338 else if (cxx_dialect < cxx2a && !first_designator)
22339 first_designator = designator;
22341 /* Parse the initializer. */
22342 initializer = cp_parser_initializer_clause (parser,
22343 &clause_non_constant_p);
22344 /* If any clause is non-constant, so is the entire initializer. */
22345 if (clause_non_constant_p)
22346 *non_constant_p = true;
22348 /* If we have an ellipsis, this is an initializer pack
22349 expansion. */
22350 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22352 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22354 /* Consume the `...'. */
22355 cp_lexer_consume_token (parser->lexer);
22357 if (designator && cxx_dialect >= cxx2a)
22358 error_at (loc,
22359 "%<...%> not allowed in designated initializer list");
22361 /* Turn the initializer into an initializer expansion. */
22362 initializer = make_pack_expansion (initializer);
22365 /* Add it to the vector. */
22366 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22368 /* If the next token is not a comma, we have reached the end of
22369 the list. */
22370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22371 break;
22373 /* Peek at the next token. */
22374 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22375 /* If the next token is a `}', then we're still done. An
22376 initializer-clause can have a trailing `,' after the
22377 initializer-list and before the closing `}'. */
22378 if (token->type == CPP_CLOSE_BRACE)
22379 break;
22381 /* Consume the `,' token. */
22382 cp_lexer_consume_token (parser->lexer);
22385 /* The same identifier shall not appear in multiple designators
22386 of a designated-initializer-list. */
22387 if (first_designator)
22389 unsigned int i;
22390 tree designator, val;
22391 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22392 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22394 if (IDENTIFIER_MARKED (designator))
22396 error_at (cp_expr_loc_or_loc (val, input_location),
22397 "%<.%s%> designator used multiple times in "
22398 "the same initializer list",
22399 IDENTIFIER_POINTER (designator));
22400 (*v)[i].index = NULL_TREE;
22402 else
22403 IDENTIFIER_MARKED (designator) = 1;
22405 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22406 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22407 IDENTIFIER_MARKED (designator) = 0;
22410 return v;
22413 /* Classes [gram.class] */
22415 /* Parse a class-name.
22417 class-name:
22418 identifier
22419 template-id
22421 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22422 to indicate that names looked up in dependent types should be
22423 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22424 keyword has been used to indicate that the name that appears next
22425 is a template. TAG_TYPE indicates the explicit tag given before
22426 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22427 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22428 is the class being defined in a class-head. If ENUM_OK is TRUE,
22429 enum-names are also accepted.
22431 Returns the TYPE_DECL representing the class. */
22433 static tree
22434 cp_parser_class_name (cp_parser *parser,
22435 bool typename_keyword_p,
22436 bool template_keyword_p,
22437 enum tag_types tag_type,
22438 bool check_dependency_p,
22439 bool class_head_p,
22440 bool is_declaration,
22441 bool enum_ok)
22443 tree decl;
22444 tree scope;
22445 bool typename_p;
22446 cp_token *token;
22447 tree identifier = NULL_TREE;
22449 /* All class-names start with an identifier. */
22450 token = cp_lexer_peek_token (parser->lexer);
22451 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22453 cp_parser_error (parser, "expected class-name");
22454 return error_mark_node;
22457 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22458 to a template-id, so we save it here. */
22459 scope = parser->scope;
22460 if (scope == error_mark_node)
22461 return error_mark_node;
22463 /* Any name names a type if we're following the `typename' keyword
22464 in a qualified name where the enclosing scope is type-dependent. */
22465 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22466 && dependent_type_p (scope));
22467 /* Handle the common case (an identifier, but not a template-id)
22468 efficiently. */
22469 if (token->type == CPP_NAME
22470 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22472 cp_token *identifier_token;
22473 bool ambiguous_p;
22475 /* Look for the identifier. */
22476 identifier_token = cp_lexer_peek_token (parser->lexer);
22477 ambiguous_p = identifier_token->error_reported;
22478 identifier = cp_parser_identifier (parser);
22479 /* If the next token isn't an identifier, we are certainly not
22480 looking at a class-name. */
22481 if (identifier == error_mark_node)
22482 decl = error_mark_node;
22483 /* If we know this is a type-name, there's no need to look it
22484 up. */
22485 else if (typename_p)
22486 decl = identifier;
22487 else
22489 tree ambiguous_decls;
22490 /* If we already know that this lookup is ambiguous, then
22491 we've already issued an error message; there's no reason
22492 to check again. */
22493 if (ambiguous_p)
22495 cp_parser_simulate_error (parser);
22496 return error_mark_node;
22498 /* If the next token is a `::', then the name must be a type
22499 name.
22501 [basic.lookup.qual]
22503 During the lookup for a name preceding the :: scope
22504 resolution operator, object, function, and enumerator
22505 names are ignored. */
22506 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22507 tag_type = scope_type;
22508 /* Look up the name. */
22509 decl = cp_parser_lookup_name (parser, identifier,
22510 tag_type,
22511 /*is_template=*/false,
22512 /*is_namespace=*/false,
22513 check_dependency_p,
22514 &ambiguous_decls,
22515 identifier_token->location);
22516 if (ambiguous_decls)
22518 if (cp_parser_parsing_tentatively (parser))
22519 cp_parser_simulate_error (parser);
22520 return error_mark_node;
22524 else
22526 /* Try a template-id. */
22527 decl = cp_parser_template_id (parser, template_keyword_p,
22528 check_dependency_p,
22529 tag_type,
22530 is_declaration);
22531 if (decl == error_mark_node)
22532 return error_mark_node;
22535 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22537 /* If this is a typename, create a TYPENAME_TYPE. */
22538 if (typename_p && decl != error_mark_node)
22540 decl = make_typename_type (scope, decl, typename_type,
22541 /*complain=*/tf_error);
22542 if (decl != error_mark_node)
22543 decl = TYPE_NAME (decl);
22546 decl = strip_using_decl (decl);
22548 /* Check to see that it is really the name of a class. */
22549 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22550 && identifier_p (TREE_OPERAND (decl, 0))
22551 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22552 /* Situations like this:
22554 template <typename T> struct A {
22555 typename T::template X<int>::I i;
22558 are problematic. Is `T::template X<int>' a class-name? The
22559 standard does not seem to be definitive, but there is no other
22560 valid interpretation of the following `::'. Therefore, those
22561 names are considered class-names. */
22563 decl = make_typename_type (scope, decl, tag_type, tf_error);
22564 if (decl != error_mark_node)
22565 decl = TYPE_NAME (decl);
22567 else if (TREE_CODE (decl) != TYPE_DECL
22568 || TREE_TYPE (decl) == error_mark_node
22569 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22570 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22571 /* In Objective-C 2.0, a classname followed by '.' starts a
22572 dot-syntax expression, and it's not a type-name. */
22573 || (c_dialect_objc ()
22574 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22575 && objc_is_class_name (decl)))
22576 decl = error_mark_node;
22578 if (decl == error_mark_node)
22579 cp_parser_error (parser, "expected class-name");
22580 else if (identifier && !parser->scope)
22581 maybe_note_name_used_in_class (identifier, decl);
22583 return decl;
22586 /* Parse a class-specifier.
22588 class-specifier:
22589 class-head { member-specification [opt] }
22591 Returns the TREE_TYPE representing the class. */
22593 static tree
22594 cp_parser_class_specifier_1 (cp_parser* parser)
22596 tree type;
22597 tree attributes = NULL_TREE;
22598 bool nested_name_specifier_p;
22599 unsigned saved_num_template_parameter_lists;
22600 bool saved_in_function_body;
22601 unsigned char in_statement;
22602 bool in_switch_statement_p;
22603 bool saved_in_unbraced_linkage_specification_p;
22604 tree old_scope = NULL_TREE;
22605 tree scope = NULL_TREE;
22606 cp_token *closing_brace;
22608 push_deferring_access_checks (dk_no_deferred);
22610 /* Parse the class-head. */
22611 type = cp_parser_class_head (parser,
22612 &nested_name_specifier_p);
22613 /* If the class-head was a semantic disaster, skip the entire body
22614 of the class. */
22615 if (!type)
22617 cp_parser_skip_to_end_of_block_or_statement (parser);
22618 pop_deferring_access_checks ();
22619 return error_mark_node;
22622 /* Look for the `{'. */
22623 matching_braces braces;
22624 if (!braces.require_open (parser))
22626 pop_deferring_access_checks ();
22627 return error_mark_node;
22630 cp_ensure_no_omp_declare_simd (parser);
22631 cp_ensure_no_oacc_routine (parser);
22633 /* Issue an error message if type-definitions are forbidden here. */
22634 cp_parser_check_type_definition (parser);
22635 /* Remember that we are defining one more class. */
22636 ++parser->num_classes_being_defined;
22637 /* Inside the class, surrounding template-parameter-lists do not
22638 apply. */
22639 saved_num_template_parameter_lists
22640 = parser->num_template_parameter_lists;
22641 parser->num_template_parameter_lists = 0;
22642 /* We are not in a function body. */
22643 saved_in_function_body = parser->in_function_body;
22644 parser->in_function_body = false;
22645 /* Or in a loop. */
22646 in_statement = parser->in_statement;
22647 parser->in_statement = 0;
22648 /* Or in a switch. */
22649 in_switch_statement_p = parser->in_switch_statement_p;
22650 parser->in_switch_statement_p = false;
22651 /* We are not immediately inside an extern "lang" block. */
22652 saved_in_unbraced_linkage_specification_p
22653 = parser->in_unbraced_linkage_specification_p;
22654 parser->in_unbraced_linkage_specification_p = false;
22656 // Associate constraints with the type.
22657 if (flag_concepts)
22658 type = associate_classtype_constraints (type);
22660 /* Start the class. */
22661 if (nested_name_specifier_p)
22663 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22664 old_scope = push_inner_scope (scope);
22666 type = begin_class_definition (type);
22668 if (type == error_mark_node)
22669 /* If the type is erroneous, skip the entire body of the class. */
22670 cp_parser_skip_to_closing_brace (parser);
22671 else
22672 /* Parse the member-specification. */
22673 cp_parser_member_specification_opt (parser);
22675 /* Look for the trailing `}'. */
22676 closing_brace = braces.require_close (parser);
22677 /* Look for trailing attributes to apply to this class. */
22678 if (cp_parser_allow_gnu_extensions_p (parser))
22679 attributes = cp_parser_gnu_attributes_opt (parser);
22680 if (type != error_mark_node)
22681 type = finish_struct (type, attributes);
22682 if (nested_name_specifier_p)
22683 pop_inner_scope (old_scope, scope);
22685 /* We've finished a type definition. Check for the common syntax
22686 error of forgetting a semicolon after the definition. We need to
22687 be careful, as we can't just check for not-a-semicolon and be done
22688 with it; the user might have typed:
22690 class X { } c = ...;
22691 class X { } *p = ...;
22693 and so forth. Instead, enumerate all the possible tokens that
22694 might follow this production; if we don't see one of them, then
22695 complain and silently insert the semicolon. */
22697 cp_token *token = cp_lexer_peek_token (parser->lexer);
22698 bool want_semicolon = true;
22700 if (cp_next_tokens_can_be_std_attribute_p (parser))
22701 /* Don't try to parse c++11 attributes here. As per the
22702 grammar, that should be a task for
22703 cp_parser_decl_specifier_seq. */
22704 want_semicolon = false;
22706 switch (token->type)
22708 case CPP_NAME:
22709 case CPP_SEMICOLON:
22710 case CPP_MULT:
22711 case CPP_AND:
22712 case CPP_OPEN_PAREN:
22713 case CPP_CLOSE_PAREN:
22714 case CPP_COMMA:
22715 want_semicolon = false;
22716 break;
22718 /* While it's legal for type qualifiers and storage class
22719 specifiers to follow type definitions in the grammar, only
22720 compiler testsuites contain code like that. Assume that if
22721 we see such code, then what we're really seeing is a case
22722 like:
22724 class X { }
22725 const <type> var = ...;
22729 class Y { }
22730 static <type> func (...) ...
22732 i.e. the qualifier or specifier applies to the next
22733 declaration. To do so, however, we need to look ahead one
22734 more token to see if *that* token is a type specifier.
22736 This code could be improved to handle:
22738 class Z { }
22739 static const <type> var = ...; */
22740 case CPP_KEYWORD:
22741 if (keyword_is_decl_specifier (token->keyword))
22743 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22745 /* Handling user-defined types here would be nice, but very
22746 tricky. */
22747 want_semicolon
22748 = (lookahead->type == CPP_KEYWORD
22749 && keyword_begins_type_specifier (lookahead->keyword));
22751 break;
22752 default:
22753 break;
22756 /* If we don't have a type, then something is very wrong and we
22757 shouldn't try to do anything clever. Likewise for not seeing the
22758 closing brace. */
22759 if (closing_brace && TYPE_P (type) && want_semicolon)
22761 /* Locate the closing brace. */
22762 cp_token_position prev
22763 = cp_lexer_previous_token_position (parser->lexer);
22764 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22765 location_t loc = prev_token->location;
22767 /* We want to suggest insertion of a ';' immediately *after* the
22768 closing brace, so, if we can, offset the location by 1 column. */
22769 location_t next_loc = loc;
22770 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22771 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22773 rich_location richloc (line_table, next_loc);
22775 /* If we successfully offset the location, suggest the fix-it. */
22776 if (next_loc != loc)
22777 richloc.add_fixit_insert_before (next_loc, ";");
22779 if (CLASSTYPE_DECLARED_CLASS (type))
22780 error_at (&richloc,
22781 "expected %<;%> after class definition");
22782 else if (TREE_CODE (type) == RECORD_TYPE)
22783 error_at (&richloc,
22784 "expected %<;%> after struct definition");
22785 else if (TREE_CODE (type) == UNION_TYPE)
22786 error_at (&richloc,
22787 "expected %<;%> after union definition");
22788 else
22789 gcc_unreachable ();
22791 /* Unget one token and smash it to look as though we encountered
22792 a semicolon in the input stream. */
22793 cp_lexer_set_token_position (parser->lexer, prev);
22794 token = cp_lexer_peek_token (parser->lexer);
22795 token->type = CPP_SEMICOLON;
22796 token->keyword = RID_MAX;
22800 /* If this class is not itself within the scope of another class,
22801 then we need to parse the bodies of all of the queued function
22802 definitions. Note that the queued functions defined in a class
22803 are not always processed immediately following the
22804 class-specifier for that class. Consider:
22806 struct A {
22807 struct B { void f() { sizeof (A); } };
22810 If `f' were processed before the processing of `A' were
22811 completed, there would be no way to compute the size of `A'.
22812 Note that the nesting we are interested in here is lexical --
22813 not the semantic nesting given by TYPE_CONTEXT. In particular,
22814 for:
22816 struct A { struct B; };
22817 struct A::B { void f() { } };
22819 there is no need to delay the parsing of `A::B::f'. */
22820 if (--parser->num_classes_being_defined == 0)
22822 tree decl;
22823 tree class_type = NULL_TREE;
22824 tree pushed_scope = NULL_TREE;
22825 unsigned ix;
22826 cp_default_arg_entry *e;
22827 tree save_ccp, save_ccr;
22829 if (any_erroneous_template_args_p (type))
22831 /* Skip default arguments, NSDMIs, etc, in order to improve
22832 error recovery (c++/71169, c++/71832). */
22833 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22834 vec_safe_truncate (unparsed_nsdmis, 0);
22835 vec_safe_truncate (unparsed_classes, 0);
22836 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22839 /* In a first pass, parse default arguments to the functions.
22840 Then, in a second pass, parse the bodies of the functions.
22841 This two-phased approach handles cases like:
22843 struct S {
22844 void f() { g(); }
22845 void g(int i = 3);
22849 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22851 decl = e->decl;
22852 /* If there are default arguments that have not yet been processed,
22853 take care of them now. */
22854 if (class_type != e->class_type)
22856 if (pushed_scope)
22857 pop_scope (pushed_scope);
22858 class_type = e->class_type;
22859 pushed_scope = push_scope (class_type);
22861 /* Make sure that any template parameters are in scope. */
22862 maybe_begin_member_template_processing (decl);
22863 /* Parse the default argument expressions. */
22864 cp_parser_late_parsing_default_args (parser, decl);
22865 /* Remove any template parameters from the symbol table. */
22866 maybe_end_member_template_processing ();
22868 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22869 /* Now parse any NSDMIs. */
22870 save_ccp = current_class_ptr;
22871 save_ccr = current_class_ref;
22872 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22874 if (class_type != DECL_CONTEXT (decl))
22876 if (pushed_scope)
22877 pop_scope (pushed_scope);
22878 class_type = DECL_CONTEXT (decl);
22879 pushed_scope = push_scope (class_type);
22881 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22882 cp_parser_late_parsing_nsdmi (parser, decl);
22884 vec_safe_truncate (unparsed_nsdmis, 0);
22885 current_class_ptr = save_ccp;
22886 current_class_ref = save_ccr;
22887 if (pushed_scope)
22888 pop_scope (pushed_scope);
22890 /* Now do some post-NSDMI bookkeeping. */
22891 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22892 after_nsdmi_defaulted_late_checks (class_type);
22893 vec_safe_truncate (unparsed_classes, 0);
22894 after_nsdmi_defaulted_late_checks (type);
22896 /* Now parse the body of the functions. */
22897 if (flag_openmp)
22899 /* OpenMP UDRs need to be parsed before all other functions. */
22900 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22901 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22902 cp_parser_late_parsing_for_member (parser, decl);
22903 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22904 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22905 cp_parser_late_parsing_for_member (parser, decl);
22907 else
22908 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22909 cp_parser_late_parsing_for_member (parser, decl);
22910 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22912 else
22913 vec_safe_push (unparsed_classes, type);
22915 /* Put back any saved access checks. */
22916 pop_deferring_access_checks ();
22918 /* Restore saved state. */
22919 parser->in_switch_statement_p = in_switch_statement_p;
22920 parser->in_statement = in_statement;
22921 parser->in_function_body = saved_in_function_body;
22922 parser->num_template_parameter_lists
22923 = saved_num_template_parameter_lists;
22924 parser->in_unbraced_linkage_specification_p
22925 = saved_in_unbraced_linkage_specification_p;
22927 return type;
22930 static tree
22931 cp_parser_class_specifier (cp_parser* parser)
22933 tree ret;
22934 timevar_push (TV_PARSE_STRUCT);
22935 ret = cp_parser_class_specifier_1 (parser);
22936 timevar_pop (TV_PARSE_STRUCT);
22937 return ret;
22940 /* Parse a class-head.
22942 class-head:
22943 class-key identifier [opt] base-clause [opt]
22944 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22945 class-key nested-name-specifier [opt] template-id
22946 base-clause [opt]
22948 class-virt-specifier:
22949 final
22951 GNU Extensions:
22952 class-key attributes identifier [opt] base-clause [opt]
22953 class-key attributes nested-name-specifier identifier base-clause [opt]
22954 class-key attributes nested-name-specifier [opt] template-id
22955 base-clause [opt]
22957 Upon return BASES is initialized to the list of base classes (or
22958 NULL, if there are none) in the same form returned by
22959 cp_parser_base_clause.
22961 Returns the TYPE of the indicated class. Sets
22962 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22963 involving a nested-name-specifier was used, and FALSE otherwise.
22965 Returns error_mark_node if this is not a class-head.
22967 Returns NULL_TREE if the class-head is syntactically valid, but
22968 semantically invalid in a way that means we should skip the entire
22969 body of the class. */
22971 static tree
22972 cp_parser_class_head (cp_parser* parser,
22973 bool* nested_name_specifier_p)
22975 tree nested_name_specifier;
22976 enum tag_types class_key;
22977 tree id = NULL_TREE;
22978 tree type = NULL_TREE;
22979 tree attributes;
22980 tree bases;
22981 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22982 bool template_id_p = false;
22983 bool qualified_p = false;
22984 bool invalid_nested_name_p = false;
22985 bool invalid_explicit_specialization_p = false;
22986 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22987 tree pushed_scope = NULL_TREE;
22988 unsigned num_templates;
22989 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22990 /* Assume no nested-name-specifier will be present. */
22991 *nested_name_specifier_p = false;
22992 /* Assume no template parameter lists will be used in defining the
22993 type. */
22994 num_templates = 0;
22995 parser->colon_corrects_to_scope_p = false;
22997 /* Look for the class-key. */
22998 class_key = cp_parser_class_key (parser);
22999 if (class_key == none_type)
23000 return error_mark_node;
23002 location_t class_head_start_location = input_location;
23004 /* Parse the attributes. */
23005 attributes = cp_parser_attributes_opt (parser);
23007 /* If the next token is `::', that is invalid -- but sometimes
23008 people do try to write:
23010 struct ::S {};
23012 Handle this gracefully by accepting the extra qualifier, and then
23013 issuing an error about it later if this really is a
23014 class-head. If it turns out just to be an elaborated type
23015 specifier, remain silent. */
23016 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23017 qualified_p = true;
23019 push_deferring_access_checks (dk_no_check);
23021 /* Determine the name of the class. Begin by looking for an
23022 optional nested-name-specifier. */
23023 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23024 nested_name_specifier
23025 = cp_parser_nested_name_specifier_opt (parser,
23026 /*typename_keyword_p=*/false,
23027 /*check_dependency_p=*/false,
23028 /*type_p=*/true,
23029 /*is_declaration=*/false);
23030 /* If there was a nested-name-specifier, then there *must* be an
23031 identifier. */
23033 cp_token *bad_template_keyword = NULL;
23035 if (nested_name_specifier)
23037 type_start_token = cp_lexer_peek_token (parser->lexer);
23038 /* Although the grammar says `identifier', it really means
23039 `class-name' or `template-name'. You are only allowed to
23040 define a class that has already been declared with this
23041 syntax.
23043 The proposed resolution for Core Issue 180 says that wherever
23044 you see `class T::X' you should treat `X' as a type-name.
23046 It is OK to define an inaccessible class; for example:
23048 class A { class B; };
23049 class A::B {};
23051 We do not know if we will see a class-name, or a
23052 template-name. We look for a class-name first, in case the
23053 class-name is a template-id; if we looked for the
23054 template-name first we would stop after the template-name. */
23055 cp_parser_parse_tentatively (parser);
23056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23057 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23058 type = cp_parser_class_name (parser,
23059 /*typename_keyword_p=*/false,
23060 /*template_keyword_p=*/false,
23061 class_type,
23062 /*check_dependency_p=*/false,
23063 /*class_head_p=*/true,
23064 /*is_declaration=*/false);
23065 /* If that didn't work, ignore the nested-name-specifier. */
23066 if (!cp_parser_parse_definitely (parser))
23068 invalid_nested_name_p = true;
23069 type_start_token = cp_lexer_peek_token (parser->lexer);
23070 id = cp_parser_identifier (parser);
23071 if (id == error_mark_node)
23072 id = NULL_TREE;
23074 /* If we could not find a corresponding TYPE, treat this
23075 declaration like an unqualified declaration. */
23076 if (type == error_mark_node)
23077 nested_name_specifier = NULL_TREE;
23078 /* Otherwise, count the number of templates used in TYPE and its
23079 containing scopes. */
23080 else
23081 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23083 /* Otherwise, the identifier is optional. */
23084 else
23086 /* We don't know whether what comes next is a template-id,
23087 an identifier, or nothing at all. */
23088 cp_parser_parse_tentatively (parser);
23089 /* Check for a template-id. */
23090 type_start_token = cp_lexer_peek_token (parser->lexer);
23091 id = cp_parser_template_id (parser,
23092 /*template_keyword_p=*/false,
23093 /*check_dependency_p=*/true,
23094 class_key,
23095 /*is_declaration=*/true);
23096 /* If that didn't work, it could still be an identifier. */
23097 if (!cp_parser_parse_definitely (parser))
23099 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23101 type_start_token = cp_lexer_peek_token (parser->lexer);
23102 id = cp_parser_identifier (parser);
23104 else
23105 id = NULL_TREE;
23107 else
23109 template_id_p = true;
23110 ++num_templates;
23114 pop_deferring_access_checks ();
23116 if (id)
23118 cp_parser_check_for_invalid_template_id (parser, id,
23119 class_key,
23120 type_start_token->location);
23122 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23124 /* If it's not a `:' or a `{' then we can't really be looking at a
23125 class-head, since a class-head only appears as part of a
23126 class-specifier. We have to detect this situation before calling
23127 xref_tag, since that has irreversible side-effects. */
23128 if (!cp_parser_next_token_starts_class_definition_p (parser))
23130 cp_parser_error (parser, "expected %<{%> or %<:%>");
23131 type = error_mark_node;
23132 goto out;
23135 /* At this point, we're going ahead with the class-specifier, even
23136 if some other problem occurs. */
23137 cp_parser_commit_to_tentative_parse (parser);
23138 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23140 cp_parser_error (parser,
23141 "cannot specify %<override%> for a class");
23142 type = error_mark_node;
23143 goto out;
23145 /* Issue the error about the overly-qualified name now. */
23146 if (qualified_p)
23148 cp_parser_error (parser,
23149 "global qualification of class name is invalid");
23150 type = error_mark_node;
23151 goto out;
23153 else if (invalid_nested_name_p)
23155 cp_parser_error (parser,
23156 "qualified name does not name a class");
23157 type = error_mark_node;
23158 goto out;
23160 else if (nested_name_specifier)
23162 tree scope;
23164 if (bad_template_keyword)
23165 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23166 keyword template shall not appear at the top level. */
23167 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23168 "keyword %<template%> not allowed in class-head-name");
23170 /* Reject typedef-names in class heads. */
23171 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23173 error_at (type_start_token->location,
23174 "invalid class name in declaration of %qD",
23175 type);
23176 type = NULL_TREE;
23177 goto done;
23180 /* Figure out in what scope the declaration is being placed. */
23181 scope = current_scope ();
23182 /* If that scope does not contain the scope in which the
23183 class was originally declared, the program is invalid. */
23184 if (scope && !is_ancestor (scope, nested_name_specifier))
23186 if (at_namespace_scope_p ())
23187 error_at (type_start_token->location,
23188 "declaration of %qD in namespace %qD which does not "
23189 "enclose %qD",
23190 type, scope, nested_name_specifier);
23191 else
23192 error_at (type_start_token->location,
23193 "declaration of %qD in %qD which does not enclose %qD",
23194 type, scope, nested_name_specifier);
23195 type = NULL_TREE;
23196 goto done;
23198 /* [dcl.meaning]
23200 A declarator-id shall not be qualified except for the
23201 definition of a ... nested class outside of its class
23202 ... [or] the definition or explicit instantiation of a
23203 class member of a namespace outside of its namespace. */
23204 if (scope == nested_name_specifier)
23206 permerror (nested_name_specifier_token_start->location,
23207 "extra qualification not allowed");
23208 nested_name_specifier = NULL_TREE;
23209 num_templates = 0;
23212 /* An explicit-specialization must be preceded by "template <>". If
23213 it is not, try to recover gracefully. */
23214 if (at_namespace_scope_p ()
23215 && parser->num_template_parameter_lists == 0
23216 && !processing_template_parmlist
23217 && template_id_p)
23219 /* Build a location of this form:
23220 struct typename <ARGS>
23221 ^~~~~~~~~~~~~~~~~~~~~~
23222 with caret==start at the start token, and
23223 finishing at the end of the type. */
23224 location_t reported_loc
23225 = make_location (class_head_start_location,
23226 class_head_start_location,
23227 get_finish (type_start_token->location));
23228 rich_location richloc (line_table, reported_loc);
23229 richloc.add_fixit_insert_before (class_head_start_location,
23230 "template <> ");
23231 error_at (&richloc,
23232 "an explicit specialization must be preceded by"
23233 " %<template <>%>");
23234 invalid_explicit_specialization_p = true;
23235 /* Take the same action that would have been taken by
23236 cp_parser_explicit_specialization. */
23237 ++parser->num_template_parameter_lists;
23238 begin_specialization ();
23240 /* There must be no "return" statements between this point and the
23241 end of this function; set "type "to the correct return value and
23242 use "goto done;" to return. */
23243 /* Make sure that the right number of template parameters were
23244 present. */
23245 if (!cp_parser_check_template_parameters (parser, num_templates,
23246 template_id_p,
23247 type_start_token->location,
23248 /*declarator=*/NULL))
23250 /* If something went wrong, there is no point in even trying to
23251 process the class-definition. */
23252 type = NULL_TREE;
23253 goto done;
23256 /* Look up the type. */
23257 if (template_id_p)
23259 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23260 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23261 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23263 error_at (type_start_token->location,
23264 "function template %qD redeclared as a class template", id);
23265 type = error_mark_node;
23267 else
23269 type = TREE_TYPE (id);
23270 type = maybe_process_partial_specialization (type);
23272 /* Check the scope while we still know whether or not we had a
23273 nested-name-specifier. */
23274 if (type != error_mark_node)
23275 check_unqualified_spec_or_inst (type, type_start_token->location);
23277 if (nested_name_specifier)
23278 pushed_scope = push_scope (nested_name_specifier);
23280 else if (nested_name_specifier)
23282 tree class_type;
23284 /* Given:
23286 template <typename T> struct S { struct T };
23287 template <typename T> struct S<T>::T { };
23289 we will get a TYPENAME_TYPE when processing the definition of
23290 `S::T'. We need to resolve it to the actual type before we
23291 try to define it. */
23292 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23294 class_type = resolve_typename_type (TREE_TYPE (type),
23295 /*only_current_p=*/false);
23296 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23297 type = TYPE_NAME (class_type);
23298 else
23300 cp_parser_error (parser, "could not resolve typename type");
23301 type = error_mark_node;
23305 if (maybe_process_partial_specialization (TREE_TYPE (type))
23306 == error_mark_node)
23308 type = NULL_TREE;
23309 goto done;
23312 class_type = current_class_type;
23313 /* Enter the scope indicated by the nested-name-specifier. */
23314 pushed_scope = push_scope (nested_name_specifier);
23315 /* Get the canonical version of this type. */
23316 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23317 /* Call push_template_decl if it seems like we should be defining a
23318 template either from the template headers or the type we're
23319 defining, so that we diagnose both extra and missing headers. */
23320 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23321 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23322 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23324 type = push_template_decl (type);
23325 if (type == error_mark_node)
23327 type = NULL_TREE;
23328 goto done;
23332 type = TREE_TYPE (type);
23333 *nested_name_specifier_p = true;
23335 else /* The name is not a nested name. */
23337 /* If the class was unnamed, create a dummy name. */
23338 if (!id)
23339 id = make_anon_name ();
23340 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23341 ? ts_within_enclosing_non_class
23342 : ts_current);
23343 type = xref_tag (class_key, id, tag_scope,
23344 parser->num_template_parameter_lists);
23347 /* Indicate whether this class was declared as a `class' or as a
23348 `struct'. */
23349 if (TREE_CODE (type) == RECORD_TYPE)
23350 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23351 cp_parser_check_class_key (class_key, type);
23353 /* If this type was already complete, and we see another definition,
23354 that's an error. */
23355 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23357 error_at (type_start_token->location, "redefinition of %q#T",
23358 type);
23359 inform (location_of (type), "previous definition of %q#T",
23360 type);
23361 type = NULL_TREE;
23362 goto done;
23364 else if (type == error_mark_node)
23365 type = NULL_TREE;
23367 if (type)
23369 /* Apply attributes now, before any use of the class as a template
23370 argument in its base list. */
23371 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23372 fixup_attribute_variants (type);
23375 /* We will have entered the scope containing the class; the names of
23376 base classes should be looked up in that context. For example:
23378 struct A { struct B {}; struct C; };
23379 struct A::C : B {};
23381 is valid. */
23383 /* Get the list of base-classes, if there is one. */
23384 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23386 /* PR59482: enter the class scope so that base-specifiers are looked
23387 up correctly. */
23388 if (type)
23389 pushclass (type);
23390 bases = cp_parser_base_clause (parser);
23391 /* PR59482: get out of the previously pushed class scope so that the
23392 subsequent pops pop the right thing. */
23393 if (type)
23394 popclass ();
23396 else
23397 bases = NULL_TREE;
23399 /* If we're really defining a class, process the base classes.
23400 If they're invalid, fail. */
23401 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23402 xref_basetypes (type, bases);
23404 done:
23405 /* Leave the scope given by the nested-name-specifier. We will
23406 enter the class scope itself while processing the members. */
23407 if (pushed_scope)
23408 pop_scope (pushed_scope);
23410 if (invalid_explicit_specialization_p)
23412 end_specialization ();
23413 --parser->num_template_parameter_lists;
23416 if (type)
23417 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23418 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23419 CLASSTYPE_FINAL (type) = 1;
23420 out:
23421 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23422 return type;
23425 /* Parse a class-key.
23427 class-key:
23428 class
23429 struct
23430 union
23432 Returns the kind of class-key specified, or none_type to indicate
23433 error. */
23435 static enum tag_types
23436 cp_parser_class_key (cp_parser* parser)
23438 cp_token *token;
23439 enum tag_types tag_type;
23441 /* Look for the class-key. */
23442 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23443 if (!token)
23444 return none_type;
23446 /* Check to see if the TOKEN is a class-key. */
23447 tag_type = cp_parser_token_is_class_key (token);
23448 if (!tag_type)
23449 cp_parser_error (parser, "expected class-key");
23450 return tag_type;
23453 /* Parse a type-parameter-key.
23455 type-parameter-key:
23456 class
23457 typename
23460 static void
23461 cp_parser_type_parameter_key (cp_parser* parser)
23463 /* Look for the type-parameter-key. */
23464 enum tag_types tag_type = none_type;
23465 cp_token *token = cp_lexer_peek_token (parser->lexer);
23466 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23468 cp_lexer_consume_token (parser->lexer);
23469 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23470 /* typename is not allowed in a template template parameter
23471 by the standard until C++17. */
23472 pedwarn (token->location, OPT_Wpedantic,
23473 "ISO C++ forbids typename key in template template parameter;"
23474 " use -std=c++17 or -std=gnu++17");
23476 else
23477 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23479 return;
23482 /* Parse an (optional) member-specification.
23484 member-specification:
23485 member-declaration member-specification [opt]
23486 access-specifier : member-specification [opt] */
23488 static void
23489 cp_parser_member_specification_opt (cp_parser* parser)
23491 while (true)
23493 cp_token *token;
23494 enum rid keyword;
23496 /* Peek at the next token. */
23497 token = cp_lexer_peek_token (parser->lexer);
23498 /* If it's a `}', or EOF then we've seen all the members. */
23499 if (token->type == CPP_CLOSE_BRACE
23500 || token->type == CPP_EOF
23501 || token->type == CPP_PRAGMA_EOL)
23502 break;
23504 /* See if this token is a keyword. */
23505 keyword = token->keyword;
23506 switch (keyword)
23508 case RID_PUBLIC:
23509 case RID_PROTECTED:
23510 case RID_PRIVATE:
23511 /* Consume the access-specifier. */
23512 cp_lexer_consume_token (parser->lexer);
23513 /* Remember which access-specifier is active. */
23514 current_access_specifier = token->u.value;
23515 /* Look for the `:'. */
23516 cp_parser_require (parser, CPP_COLON, RT_COLON);
23517 break;
23519 default:
23520 /* Accept #pragmas at class scope. */
23521 if (token->type == CPP_PRAGMA)
23523 cp_parser_pragma (parser, pragma_member, NULL);
23524 break;
23527 /* Otherwise, the next construction must be a
23528 member-declaration. */
23529 cp_parser_member_declaration (parser);
23534 /* Parse a member-declaration.
23536 member-declaration:
23537 decl-specifier-seq [opt] member-declarator-list [opt] ;
23538 function-definition ; [opt]
23539 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23540 using-declaration
23541 template-declaration
23542 alias-declaration
23544 member-declarator-list:
23545 member-declarator
23546 member-declarator-list , member-declarator
23548 member-declarator:
23549 declarator pure-specifier [opt]
23550 declarator constant-initializer [opt]
23551 identifier [opt] : constant-expression
23553 GNU Extensions:
23555 member-declaration:
23556 __extension__ member-declaration
23558 member-declarator:
23559 declarator attributes [opt] pure-specifier [opt]
23560 declarator attributes [opt] constant-initializer [opt]
23561 identifier [opt] attributes [opt] : constant-expression
23563 C++0x Extensions:
23565 member-declaration:
23566 static_assert-declaration */
23568 static void
23569 cp_parser_member_declaration (cp_parser* parser)
23571 cp_decl_specifier_seq decl_specifiers;
23572 tree prefix_attributes;
23573 tree decl;
23574 int declares_class_or_enum;
23575 bool friend_p;
23576 cp_token *token = NULL;
23577 cp_token *decl_spec_token_start = NULL;
23578 cp_token *initializer_token_start = NULL;
23579 int saved_pedantic;
23580 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23582 /* Check for the `__extension__' keyword. */
23583 if (cp_parser_extension_opt (parser, &saved_pedantic))
23585 /* Recurse. */
23586 cp_parser_member_declaration (parser);
23587 /* Restore the old value of the PEDANTIC flag. */
23588 pedantic = saved_pedantic;
23590 return;
23593 /* Check for a template-declaration. */
23594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23596 /* An explicit specialization here is an error condition, and we
23597 expect the specialization handler to detect and report this. */
23598 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23599 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23600 cp_parser_explicit_specialization (parser);
23601 else
23602 cp_parser_template_declaration (parser, /*member_p=*/true);
23604 return;
23606 /* Check for a template introduction. */
23607 else if (cp_parser_template_declaration_after_export (parser, true))
23608 return;
23610 /* Check for a using-declaration. */
23611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23613 if (cxx_dialect < cxx11)
23615 /* Parse the using-declaration. */
23616 cp_parser_using_declaration (parser,
23617 /*access_declaration_p=*/false);
23618 return;
23620 else
23622 tree decl;
23623 bool alias_decl_expected;
23624 cp_parser_parse_tentatively (parser);
23625 decl = cp_parser_alias_declaration (parser);
23626 /* Note that if we actually see the '=' token after the
23627 identifier, cp_parser_alias_declaration commits the
23628 tentative parse. In that case, we really expect an
23629 alias-declaration. Otherwise, we expect a using
23630 declaration. */
23631 alias_decl_expected =
23632 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23633 cp_parser_parse_definitely (parser);
23635 if (alias_decl_expected)
23636 finish_member_declaration (decl);
23637 else
23638 cp_parser_using_declaration (parser,
23639 /*access_declaration_p=*/false);
23640 return;
23644 /* Check for @defs. */
23645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23647 tree ivar, member;
23648 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23649 ivar = ivar_chains;
23650 while (ivar)
23652 member = ivar;
23653 ivar = TREE_CHAIN (member);
23654 TREE_CHAIN (member) = NULL_TREE;
23655 finish_member_declaration (member);
23657 return;
23660 /* If the next token is `static_assert' we have a static assertion. */
23661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23663 cp_parser_static_assert (parser, /*member_p=*/true);
23664 return;
23667 parser->colon_corrects_to_scope_p = false;
23669 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23670 goto out;
23672 /* Parse the decl-specifier-seq. */
23673 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23674 cp_parser_decl_specifier_seq (parser,
23675 CP_PARSER_FLAGS_OPTIONAL,
23676 &decl_specifiers,
23677 &declares_class_or_enum);
23678 /* Check for an invalid type-name. */
23679 if (!decl_specifiers.any_type_specifiers_p
23680 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23681 goto out;
23682 /* If there is no declarator, then the decl-specifier-seq should
23683 specify a type. */
23684 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23686 /* If there was no decl-specifier-seq, and the next token is a
23687 `;', then we have something like:
23689 struct S { ; };
23691 [class.mem]
23693 Each member-declaration shall declare at least one member
23694 name of the class. */
23695 if (!decl_specifiers.any_specifiers_p)
23697 cp_token *token = cp_lexer_peek_token (parser->lexer);
23698 if (!in_system_header_at (token->location))
23700 gcc_rich_location richloc (token->location);
23701 richloc.add_fixit_remove ();
23702 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23705 else
23707 tree type;
23709 /* See if this declaration is a friend. */
23710 friend_p = cp_parser_friend_p (&decl_specifiers);
23711 /* If there were decl-specifiers, check to see if there was
23712 a class-declaration. */
23713 type = check_tag_decl (&decl_specifiers,
23714 /*explicit_type_instantiation_p=*/false);
23715 /* Nested classes have already been added to the class, but
23716 a `friend' needs to be explicitly registered. */
23717 if (friend_p)
23719 /* If the `friend' keyword was present, the friend must
23720 be introduced with a class-key. */
23721 if (!declares_class_or_enum && cxx_dialect < cxx11)
23722 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23723 "in C++03 a class-key must be used "
23724 "when declaring a friend");
23725 /* In this case:
23727 template <typename T> struct A {
23728 friend struct A<T>::B;
23731 A<T>::B will be represented by a TYPENAME_TYPE, and
23732 therefore not recognized by check_tag_decl. */
23733 if (!type)
23735 type = decl_specifiers.type;
23736 if (type && TREE_CODE (type) == TYPE_DECL)
23737 type = TREE_TYPE (type);
23739 if (!type || !TYPE_P (type))
23740 error_at (decl_spec_token_start->location,
23741 "friend declaration does not name a class or "
23742 "function");
23743 else
23744 make_friend_class (current_class_type, type,
23745 /*complain=*/true);
23747 /* If there is no TYPE, an error message will already have
23748 been issued. */
23749 else if (!type || type == error_mark_node)
23751 /* An anonymous aggregate has to be handled specially; such
23752 a declaration really declares a data member (with a
23753 particular type), as opposed to a nested class. */
23754 else if (ANON_AGGR_TYPE_P (type))
23756 /* C++11 9.5/6. */
23757 if (decl_specifiers.storage_class != sc_none)
23758 error_at (decl_spec_token_start->location,
23759 "a storage class on an anonymous aggregate "
23760 "in class scope is not allowed");
23762 /* Remove constructors and such from TYPE, now that we
23763 know it is an anonymous aggregate. */
23764 fixup_anonymous_aggr (type);
23765 /* And make the corresponding data member. */
23766 decl = build_decl (decl_spec_token_start->location,
23767 FIELD_DECL, NULL_TREE, type);
23768 /* Add it to the class. */
23769 finish_member_declaration (decl);
23771 else
23772 cp_parser_check_access_in_redeclaration
23773 (TYPE_NAME (type),
23774 decl_spec_token_start->location);
23777 else
23779 bool assume_semicolon = false;
23781 /* Clear attributes from the decl_specifiers but keep them
23782 around as prefix attributes that apply them to the entity
23783 being declared. */
23784 prefix_attributes = decl_specifiers.attributes;
23785 decl_specifiers.attributes = NULL_TREE;
23787 /* See if these declarations will be friends. */
23788 friend_p = cp_parser_friend_p (&decl_specifiers);
23790 /* Keep going until we hit the `;' at the end of the
23791 declaration. */
23792 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23794 tree attributes = NULL_TREE;
23795 tree first_attribute;
23796 tree initializer;
23797 bool named_bitfld = false;
23799 /* Peek at the next token. */
23800 token = cp_lexer_peek_token (parser->lexer);
23802 /* The following code wants to know early if it is a bit-field
23803 or some other declaration. Attributes can appear before
23804 the `:' token. Skip over them without consuming any tokens
23805 to peek if they are followed by `:'. */
23806 if (cp_next_tokens_can_be_attribute_p (parser)
23807 || (token->type == CPP_NAME
23808 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23809 && (named_bitfld = true)))
23811 size_t n
23812 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23813 token = cp_lexer_peek_nth_token (parser->lexer, n);
23816 /* Check for a bitfield declaration. */
23817 if (token->type == CPP_COLON
23818 || (token->type == CPP_NAME
23819 && token == cp_lexer_peek_token (parser->lexer)
23820 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23821 && (named_bitfld = true)))
23823 tree identifier;
23824 tree width;
23825 tree late_attributes = NULL_TREE;
23827 if (named_bitfld)
23828 identifier = cp_parser_identifier (parser);
23829 else
23830 identifier = NULL_TREE;
23832 /* Look for attributes that apply to the bitfield. */
23833 attributes = cp_parser_attributes_opt (parser);
23835 /* Consume the `:' token. */
23836 cp_lexer_consume_token (parser->lexer);
23838 /* Get the width of the bitfield. */
23839 width = cp_parser_constant_expression (parser, false, NULL,
23840 cxx_dialect >= cxx11);
23842 /* In C++2A and as extension for C++11 and above we allow
23843 default member initializers for bit-fields. */
23844 initializer = NULL_TREE;
23845 if (cxx_dialect >= cxx11
23846 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23847 || cp_lexer_next_token_is (parser->lexer,
23848 CPP_OPEN_BRACE)))
23850 location_t loc
23851 = cp_lexer_peek_token (parser->lexer)->location;
23852 if (cxx_dialect < cxx2a
23853 && !in_system_header_at (loc)
23854 && identifier != NULL_TREE)
23855 pedwarn (loc, 0,
23856 "default member initializers for bit-fields "
23857 "only available with -std=c++2a or "
23858 "-std=gnu++2a");
23860 initializer = cp_parser_save_nsdmi (parser);
23861 if (identifier == NULL_TREE)
23863 error_at (loc, "default member initializer for "
23864 "unnamed bit-field");
23865 initializer = NULL_TREE;
23868 else
23870 /* Look for attributes that apply to the bitfield after
23871 the `:' token and width. This is where GCC used to
23872 parse attributes in the past, pedwarn if there is
23873 a std attribute. */
23874 if (cp_next_tokens_can_be_std_attribute_p (parser))
23875 pedwarn (input_location, OPT_Wpedantic,
23876 "ISO C++ allows bit-field attributes only "
23877 "before the %<:%> token");
23879 late_attributes = cp_parser_attributes_opt (parser);
23882 attributes = attr_chainon (attributes, late_attributes);
23884 /* Remember which attributes are prefix attributes and
23885 which are not. */
23886 first_attribute = attributes;
23887 /* Combine the attributes. */
23888 attributes = attr_chainon (prefix_attributes, attributes);
23890 /* Create the bitfield declaration. */
23891 decl = grokbitfield (identifier
23892 ? make_id_declarator (NULL_TREE,
23893 identifier,
23894 sfk_none)
23895 : NULL,
23896 &decl_specifiers,
23897 width, initializer,
23898 attributes);
23900 else
23902 cp_declarator *declarator;
23903 tree asm_specification;
23904 int ctor_dtor_or_conv_p;
23906 /* Parse the declarator. */
23907 declarator
23908 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23909 &ctor_dtor_or_conv_p,
23910 /*parenthesized_p=*/NULL,
23911 /*member_p=*/true,
23912 friend_p);
23914 /* If something went wrong parsing the declarator, make sure
23915 that we at least consume some tokens. */
23916 if (declarator == cp_error_declarator)
23918 /* Skip to the end of the statement. */
23919 cp_parser_skip_to_end_of_statement (parser);
23920 /* If the next token is not a semicolon, that is
23921 probably because we just skipped over the body of
23922 a function. So, we consume a semicolon if
23923 present, but do not issue an error message if it
23924 is not present. */
23925 if (cp_lexer_next_token_is (parser->lexer,
23926 CPP_SEMICOLON))
23927 cp_lexer_consume_token (parser->lexer);
23928 goto out;
23931 if (declares_class_or_enum & 2)
23932 cp_parser_check_for_definition_in_return_type
23933 (declarator, decl_specifiers.type,
23934 decl_specifiers.locations[ds_type_spec]);
23936 /* Look for an asm-specification. */
23937 asm_specification = cp_parser_asm_specification_opt (parser);
23938 /* Look for attributes that apply to the declaration. */
23939 attributes = cp_parser_attributes_opt (parser);
23940 /* Remember which attributes are prefix attributes and
23941 which are not. */
23942 first_attribute = attributes;
23943 /* Combine the attributes. */
23944 attributes = attr_chainon (prefix_attributes, attributes);
23946 /* If it's an `=', then we have a constant-initializer or a
23947 pure-specifier. It is not correct to parse the
23948 initializer before registering the member declaration
23949 since the member declaration should be in scope while
23950 its initializer is processed. However, the rest of the
23951 front end does not yet provide an interface that allows
23952 us to handle this correctly. */
23953 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23955 /* In [class.mem]:
23957 A pure-specifier shall be used only in the declaration of
23958 a virtual function.
23960 A member-declarator can contain a constant-initializer
23961 only if it declares a static member of integral or
23962 enumeration type.
23964 Therefore, if the DECLARATOR is for a function, we look
23965 for a pure-specifier; otherwise, we look for a
23966 constant-initializer. When we call `grokfield', it will
23967 perform more stringent semantics checks. */
23968 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23969 if (function_declarator_p (declarator)
23970 || (decl_specifiers.type
23971 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23972 && declarator->kind == cdk_id
23973 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23974 == FUNCTION_TYPE)))
23975 initializer = cp_parser_pure_specifier (parser);
23976 else if (decl_specifiers.storage_class != sc_static)
23977 initializer = cp_parser_save_nsdmi (parser);
23978 else if (cxx_dialect >= cxx11)
23980 bool nonconst;
23981 /* Don't require a constant rvalue in C++11, since we
23982 might want a reference constant. We'll enforce
23983 constancy later. */
23984 cp_lexer_consume_token (parser->lexer);
23985 /* Parse the initializer. */
23986 initializer = cp_parser_initializer_clause (parser,
23987 &nonconst);
23989 else
23990 /* Parse the initializer. */
23991 initializer = cp_parser_constant_initializer (parser);
23993 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23994 && !function_declarator_p (declarator))
23996 bool x;
23997 if (decl_specifiers.storage_class != sc_static)
23998 initializer = cp_parser_save_nsdmi (parser);
23999 else
24000 initializer = cp_parser_initializer (parser, &x, &x);
24002 /* Otherwise, there is no initializer. */
24003 else
24004 initializer = NULL_TREE;
24006 /* See if we are probably looking at a function
24007 definition. We are certainly not looking at a
24008 member-declarator. Calling `grokfield' has
24009 side-effects, so we must not do it unless we are sure
24010 that we are looking at a member-declarator. */
24011 if (cp_parser_token_starts_function_definition_p
24012 (cp_lexer_peek_token (parser->lexer)))
24014 /* The grammar does not allow a pure-specifier to be
24015 used when a member function is defined. (It is
24016 possible that this fact is an oversight in the
24017 standard, since a pure function may be defined
24018 outside of the class-specifier. */
24019 if (initializer && initializer_token_start)
24020 error_at (initializer_token_start->location,
24021 "pure-specifier on function-definition");
24022 decl = cp_parser_save_member_function_body (parser,
24023 &decl_specifiers,
24024 declarator,
24025 attributes);
24026 if (parser->fully_implicit_function_template_p)
24027 decl = finish_fully_implicit_template (parser, decl);
24028 /* If the member was not a friend, declare it here. */
24029 if (!friend_p)
24030 finish_member_declaration (decl);
24031 /* Peek at the next token. */
24032 token = cp_lexer_peek_token (parser->lexer);
24033 /* If the next token is a semicolon, consume it. */
24034 if (token->type == CPP_SEMICOLON)
24036 location_t semicolon_loc
24037 = cp_lexer_consume_token (parser->lexer)->location;
24038 gcc_rich_location richloc (semicolon_loc);
24039 richloc.add_fixit_remove ();
24040 warning_at (&richloc, OPT_Wextra_semi,
24041 "extra %<;%> after in-class "
24042 "function definition");
24044 goto out;
24046 else
24047 if (declarator->kind == cdk_function)
24048 declarator->id_loc = token->location;
24049 /* Create the declaration. */
24050 decl = grokfield (declarator, &decl_specifiers,
24051 initializer, /*init_const_expr_p=*/true,
24052 asm_specification, attributes);
24053 if (parser->fully_implicit_function_template_p)
24055 if (friend_p)
24056 finish_fully_implicit_template (parser, 0);
24057 else
24058 decl = finish_fully_implicit_template (parser, decl);
24062 cp_finalize_omp_declare_simd (parser, decl);
24063 cp_finalize_oacc_routine (parser, decl, false);
24065 /* Reset PREFIX_ATTRIBUTES. */
24066 if (attributes != error_mark_node)
24068 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24069 attributes = TREE_CHAIN (attributes);
24070 if (attributes)
24071 TREE_CHAIN (attributes) = NULL_TREE;
24074 /* If there is any qualification still in effect, clear it
24075 now; we will be starting fresh with the next declarator. */
24076 parser->scope = NULL_TREE;
24077 parser->qualifying_scope = NULL_TREE;
24078 parser->object_scope = NULL_TREE;
24079 /* If it's a `,', then there are more declarators. */
24080 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24082 cp_lexer_consume_token (parser->lexer);
24083 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24085 cp_token *token = cp_lexer_previous_token (parser->lexer);
24086 gcc_rich_location richloc (token->location);
24087 richloc.add_fixit_remove ();
24088 error_at (&richloc, "stray %<,%> at end of "
24089 "member declaration");
24092 /* If the next token isn't a `;', then we have a parse error. */
24093 else if (cp_lexer_next_token_is_not (parser->lexer,
24094 CPP_SEMICOLON))
24096 /* The next token might be a ways away from where the
24097 actual semicolon is missing. Find the previous token
24098 and use that for our error position. */
24099 cp_token *token = cp_lexer_previous_token (parser->lexer);
24100 gcc_rich_location richloc (token->location);
24101 richloc.add_fixit_insert_after (";");
24102 error_at (&richloc, "expected %<;%> at end of "
24103 "member declaration");
24105 /* Assume that the user meant to provide a semicolon. If
24106 we were to cp_parser_skip_to_end_of_statement, we might
24107 skip to a semicolon inside a member function definition
24108 and issue nonsensical error messages. */
24109 assume_semicolon = true;
24112 if (decl)
24114 /* Add DECL to the list of members. */
24115 if (!friend_p
24116 /* Explicitly include, eg, NSDMIs, for better error
24117 recovery (c++/58650). */
24118 || !DECL_DECLARES_FUNCTION_P (decl))
24119 finish_member_declaration (decl);
24121 if (TREE_CODE (decl) == FUNCTION_DECL)
24122 cp_parser_save_default_args (parser, decl);
24123 else if (TREE_CODE (decl) == FIELD_DECL
24124 && DECL_INITIAL (decl))
24125 /* Add DECL to the queue of NSDMI to be parsed later. */
24126 vec_safe_push (unparsed_nsdmis, decl);
24129 if (assume_semicolon)
24130 goto out;
24134 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24135 out:
24136 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24139 /* Parse a pure-specifier.
24141 pure-specifier:
24144 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24145 Otherwise, ERROR_MARK_NODE is returned. */
24147 static tree
24148 cp_parser_pure_specifier (cp_parser* parser)
24150 cp_token *token;
24152 /* Look for the `=' token. */
24153 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24154 return error_mark_node;
24155 /* Look for the `0' token. */
24156 token = cp_lexer_peek_token (parser->lexer);
24158 if (token->type == CPP_EOF
24159 || token->type == CPP_PRAGMA_EOL)
24160 return error_mark_node;
24162 cp_lexer_consume_token (parser->lexer);
24164 /* Accept = default or = delete in c++0x mode. */
24165 if (token->keyword == RID_DEFAULT
24166 || token->keyword == RID_DELETE)
24168 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24169 return token->u.value;
24172 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24173 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24175 cp_parser_error (parser,
24176 "invalid pure specifier (only %<= 0%> is allowed)");
24177 cp_parser_skip_to_end_of_statement (parser);
24178 return error_mark_node;
24180 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24182 error_at (token->location, "templates may not be %<virtual%>");
24183 return error_mark_node;
24186 return integer_zero_node;
24189 /* Parse a constant-initializer.
24191 constant-initializer:
24192 = constant-expression
24194 Returns a representation of the constant-expression. */
24196 static tree
24197 cp_parser_constant_initializer (cp_parser* parser)
24199 /* Look for the `=' token. */
24200 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24201 return error_mark_node;
24203 /* It is invalid to write:
24205 struct S { static const int i = { 7 }; };
24208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24210 cp_parser_error (parser,
24211 "a brace-enclosed initializer is not allowed here");
24212 /* Consume the opening brace. */
24213 matching_braces braces;
24214 braces.consume_open (parser);
24215 /* Skip the initializer. */
24216 cp_parser_skip_to_closing_brace (parser);
24217 /* Look for the trailing `}'. */
24218 braces.require_close (parser);
24220 return error_mark_node;
24223 return cp_parser_constant_expression (parser);
24226 /* Derived classes [gram.class.derived] */
24228 /* Parse a base-clause.
24230 base-clause:
24231 : base-specifier-list
24233 base-specifier-list:
24234 base-specifier ... [opt]
24235 base-specifier-list , base-specifier ... [opt]
24237 Returns a TREE_LIST representing the base-classes, in the order in
24238 which they were declared. The representation of each node is as
24239 described by cp_parser_base_specifier.
24241 In the case that no bases are specified, this function will return
24242 NULL_TREE, not ERROR_MARK_NODE. */
24244 static tree
24245 cp_parser_base_clause (cp_parser* parser)
24247 tree bases = NULL_TREE;
24249 /* Look for the `:' that begins the list. */
24250 cp_parser_require (parser, CPP_COLON, RT_COLON);
24252 /* Scan the base-specifier-list. */
24253 while (true)
24255 cp_token *token;
24256 tree base;
24257 bool pack_expansion_p = false;
24259 /* Look for the base-specifier. */
24260 base = cp_parser_base_specifier (parser);
24261 /* Look for the (optional) ellipsis. */
24262 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24264 /* Consume the `...'. */
24265 cp_lexer_consume_token (parser->lexer);
24267 pack_expansion_p = true;
24270 /* Add BASE to the front of the list. */
24271 if (base && base != error_mark_node)
24273 if (pack_expansion_p)
24274 /* Make this a pack expansion type. */
24275 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24277 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24279 TREE_CHAIN (base) = bases;
24280 bases = base;
24283 /* Peek at the next token. */
24284 token = cp_lexer_peek_token (parser->lexer);
24285 /* If it's not a comma, then the list is complete. */
24286 if (token->type != CPP_COMMA)
24287 break;
24288 /* Consume the `,'. */
24289 cp_lexer_consume_token (parser->lexer);
24292 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24293 base class had a qualified name. However, the next name that
24294 appears is certainly not qualified. */
24295 parser->scope = NULL_TREE;
24296 parser->qualifying_scope = NULL_TREE;
24297 parser->object_scope = NULL_TREE;
24299 return nreverse (bases);
24302 /* Parse a base-specifier.
24304 base-specifier:
24305 :: [opt] nested-name-specifier [opt] class-name
24306 virtual access-specifier [opt] :: [opt] nested-name-specifier
24307 [opt] class-name
24308 access-specifier virtual [opt] :: [opt] nested-name-specifier
24309 [opt] class-name
24311 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24312 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24313 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24314 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24316 static tree
24317 cp_parser_base_specifier (cp_parser* parser)
24319 cp_token *token;
24320 bool done = false;
24321 bool virtual_p = false;
24322 bool duplicate_virtual_error_issued_p = false;
24323 bool duplicate_access_error_issued_p = false;
24324 bool class_scope_p, template_p;
24325 tree access = access_default_node;
24326 tree type;
24328 /* Process the optional `virtual' and `access-specifier'. */
24329 while (!done)
24331 /* Peek at the next token. */
24332 token = cp_lexer_peek_token (parser->lexer);
24333 /* Process `virtual'. */
24334 switch (token->keyword)
24336 case RID_VIRTUAL:
24337 /* If `virtual' appears more than once, issue an error. */
24338 if (virtual_p && !duplicate_virtual_error_issued_p)
24340 cp_parser_error (parser,
24341 "%<virtual%> specified more than once in base-specifier");
24342 duplicate_virtual_error_issued_p = true;
24345 virtual_p = true;
24347 /* Consume the `virtual' token. */
24348 cp_lexer_consume_token (parser->lexer);
24350 break;
24352 case RID_PUBLIC:
24353 case RID_PROTECTED:
24354 case RID_PRIVATE:
24355 /* If more than one access specifier appears, issue an
24356 error. */
24357 if (access != access_default_node
24358 && !duplicate_access_error_issued_p)
24360 cp_parser_error (parser,
24361 "more than one access specifier in base-specifier");
24362 duplicate_access_error_issued_p = true;
24365 access = ridpointers[(int) token->keyword];
24367 /* Consume the access-specifier. */
24368 cp_lexer_consume_token (parser->lexer);
24370 break;
24372 default:
24373 done = true;
24374 break;
24377 /* It is not uncommon to see programs mechanically, erroneously, use
24378 the 'typename' keyword to denote (dependent) qualified types
24379 as base classes. */
24380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24382 token = cp_lexer_peek_token (parser->lexer);
24383 if (!processing_template_decl)
24384 error_at (token->location,
24385 "keyword %<typename%> not allowed outside of templates");
24386 else
24387 error_at (token->location,
24388 "keyword %<typename%> not allowed in this context "
24389 "(the base class is implicitly a type)");
24390 cp_lexer_consume_token (parser->lexer);
24393 /* Look for the optional `::' operator. */
24394 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24395 /* Look for the nested-name-specifier. The simplest way to
24396 implement:
24398 [temp.res]
24400 The keyword `typename' is not permitted in a base-specifier or
24401 mem-initializer; in these contexts a qualified name that
24402 depends on a template-parameter is implicitly assumed to be a
24403 type name.
24405 is to pretend that we have seen the `typename' keyword at this
24406 point. */
24407 cp_parser_nested_name_specifier_opt (parser,
24408 /*typename_keyword_p=*/true,
24409 /*check_dependency_p=*/true,
24410 /*type_p=*/true,
24411 /*is_declaration=*/true);
24412 /* If the base class is given by a qualified name, assume that names
24413 we see are type names or templates, as appropriate. */
24414 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24415 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24417 if (!parser->scope
24418 && cp_lexer_next_token_is_decltype (parser->lexer))
24419 /* DR 950 allows decltype as a base-specifier. */
24420 type = cp_parser_decltype (parser);
24421 else
24423 /* Otherwise, look for the class-name. */
24424 type = cp_parser_class_name (parser,
24425 class_scope_p,
24426 template_p,
24427 typename_type,
24428 /*check_dependency_p=*/true,
24429 /*class_head_p=*/false,
24430 /*is_declaration=*/true);
24431 type = TREE_TYPE (type);
24434 if (type == error_mark_node)
24435 return error_mark_node;
24437 return finish_base_specifier (type, access, virtual_p);
24440 /* Exception handling [gram.exception] */
24442 /* Parse an (optional) noexcept-specification.
24444 noexcept-specification:
24445 noexcept ( constant-expression ) [opt]
24447 If no noexcept-specification is present, returns NULL_TREE.
24448 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24449 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24450 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24451 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24452 in which case a boolean condition is returned instead. */
24454 static tree
24455 cp_parser_noexcept_specification_opt (cp_parser* parser,
24456 bool require_constexpr,
24457 bool* consumed_expr,
24458 bool return_cond)
24460 cp_token *token;
24461 const char *saved_message;
24463 /* Peek at the next token. */
24464 token = cp_lexer_peek_token (parser->lexer);
24466 /* Is it a noexcept-specification? */
24467 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24469 tree expr;
24470 cp_lexer_consume_token (parser->lexer);
24472 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24474 matching_parens parens;
24475 parens.consume_open (parser);
24477 if (require_constexpr)
24479 /* Types may not be defined in an exception-specification. */
24480 saved_message = parser->type_definition_forbidden_message;
24481 parser->type_definition_forbidden_message
24482 = G_("types may not be defined in an exception-specification");
24484 expr = cp_parser_constant_expression (parser);
24486 /* Restore the saved message. */
24487 parser->type_definition_forbidden_message = saved_message;
24489 else
24491 expr = cp_parser_expression (parser);
24492 *consumed_expr = true;
24495 parens.require_close (parser);
24497 else
24499 expr = boolean_true_node;
24500 if (!require_constexpr)
24501 *consumed_expr = false;
24504 /* We cannot build a noexcept-spec right away because this will check
24505 that expr is a constexpr. */
24506 if (!return_cond)
24507 return build_noexcept_spec (expr, tf_warning_or_error);
24508 else
24509 return expr;
24511 else
24512 return NULL_TREE;
24515 /* Parse an (optional) exception-specification.
24517 exception-specification:
24518 throw ( type-id-list [opt] )
24520 Returns a TREE_LIST representing the exception-specification. The
24521 TREE_VALUE of each node is a type. */
24523 static tree
24524 cp_parser_exception_specification_opt (cp_parser* parser)
24526 cp_token *token;
24527 tree type_id_list;
24528 const char *saved_message;
24530 /* Peek at the next token. */
24531 token = cp_lexer_peek_token (parser->lexer);
24533 /* Is it a noexcept-specification? */
24534 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24535 false);
24536 if (type_id_list != NULL_TREE)
24537 return type_id_list;
24539 /* If it's not `throw', then there's no exception-specification. */
24540 if (!cp_parser_is_keyword (token, RID_THROW))
24541 return NULL_TREE;
24543 location_t loc = token->location;
24545 /* Consume the `throw'. */
24546 cp_lexer_consume_token (parser->lexer);
24548 /* Look for the `('. */
24549 matching_parens parens;
24550 parens.require_open (parser);
24552 /* Peek at the next token. */
24553 token = cp_lexer_peek_token (parser->lexer);
24554 /* If it's not a `)', then there is a type-id-list. */
24555 if (token->type != CPP_CLOSE_PAREN)
24557 /* Types may not be defined in an exception-specification. */
24558 saved_message = parser->type_definition_forbidden_message;
24559 parser->type_definition_forbidden_message
24560 = G_("types may not be defined in an exception-specification");
24561 /* Parse the type-id-list. */
24562 type_id_list = cp_parser_type_id_list (parser);
24563 /* Restore the saved message. */
24564 parser->type_definition_forbidden_message = saved_message;
24566 if (cxx_dialect >= cxx17)
24568 error_at (loc, "ISO C++17 does not allow dynamic exception "
24569 "specifications");
24570 type_id_list = NULL_TREE;
24572 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24573 warning_at (loc, OPT_Wdeprecated,
24574 "dynamic exception specifications are deprecated in "
24575 "C++11");
24577 /* In C++17, throw() is equivalent to noexcept (true). throw()
24578 is deprecated in C++11 and above as well, but is still widely used,
24579 so don't warn about it yet. */
24580 else if (cxx_dialect >= cxx17)
24581 type_id_list = noexcept_true_spec;
24582 else
24583 type_id_list = empty_except_spec;
24585 /* Look for the `)'. */
24586 parens.require_close (parser);
24588 return type_id_list;
24591 /* Parse an (optional) type-id-list.
24593 type-id-list:
24594 type-id ... [opt]
24595 type-id-list , type-id ... [opt]
24597 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24598 in the order that the types were presented. */
24600 static tree
24601 cp_parser_type_id_list (cp_parser* parser)
24603 tree types = NULL_TREE;
24605 while (true)
24607 cp_token *token;
24608 tree type;
24610 token = cp_lexer_peek_token (parser->lexer);
24612 /* Get the next type-id. */
24613 type = cp_parser_type_id (parser);
24614 /* Check for invalid 'auto'. */
24615 if (flag_concepts && type_uses_auto (type))
24617 error_at (token->location,
24618 "invalid use of %<auto%> in exception-specification");
24619 type = error_mark_node;
24621 /* Parse the optional ellipsis. */
24622 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24624 /* Consume the `...'. */
24625 cp_lexer_consume_token (parser->lexer);
24627 /* Turn the type into a pack expansion expression. */
24628 type = make_pack_expansion (type);
24630 /* Add it to the list. */
24631 types = add_exception_specifier (types, type, /*complain=*/1);
24632 /* Peek at the next token. */
24633 token = cp_lexer_peek_token (parser->lexer);
24634 /* If it is not a `,', we are done. */
24635 if (token->type != CPP_COMMA)
24636 break;
24637 /* Consume the `,'. */
24638 cp_lexer_consume_token (parser->lexer);
24641 return nreverse (types);
24644 /* Parse a try-block.
24646 try-block:
24647 try compound-statement handler-seq */
24649 static tree
24650 cp_parser_try_block (cp_parser* parser)
24652 tree try_block;
24654 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24655 if (parser->in_function_body
24656 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24657 error ("%<try%> in %<constexpr%> function");
24659 try_block = begin_try_block ();
24660 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24661 finish_try_block (try_block);
24662 cp_parser_handler_seq (parser);
24663 finish_handler_sequence (try_block);
24665 return try_block;
24668 /* Parse a function-try-block.
24670 function-try-block:
24671 try ctor-initializer [opt] function-body handler-seq */
24673 static void
24674 cp_parser_function_try_block (cp_parser* parser)
24676 tree compound_stmt;
24677 tree try_block;
24679 /* Look for the `try' keyword. */
24680 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24681 return;
24682 /* Let the rest of the front end know where we are. */
24683 try_block = begin_function_try_block (&compound_stmt);
24684 /* Parse the function-body. */
24685 cp_parser_ctor_initializer_opt_and_function_body
24686 (parser, /*in_function_try_block=*/true);
24687 /* We're done with the `try' part. */
24688 finish_function_try_block (try_block);
24689 /* Parse the handlers. */
24690 cp_parser_handler_seq (parser);
24691 /* We're done with the handlers. */
24692 finish_function_handler_sequence (try_block, compound_stmt);
24695 /* Parse a handler-seq.
24697 handler-seq:
24698 handler handler-seq [opt] */
24700 static void
24701 cp_parser_handler_seq (cp_parser* parser)
24703 while (true)
24705 cp_token *token;
24707 /* Parse the handler. */
24708 cp_parser_handler (parser);
24709 /* Peek at the next token. */
24710 token = cp_lexer_peek_token (parser->lexer);
24711 /* If it's not `catch' then there are no more handlers. */
24712 if (!cp_parser_is_keyword (token, RID_CATCH))
24713 break;
24717 /* Parse a handler.
24719 handler:
24720 catch ( exception-declaration ) compound-statement */
24722 static void
24723 cp_parser_handler (cp_parser* parser)
24725 tree handler;
24726 tree declaration;
24728 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24729 handler = begin_handler ();
24730 matching_parens parens;
24731 parens.require_open (parser);
24732 declaration = cp_parser_exception_declaration (parser);
24733 finish_handler_parms (declaration, handler);
24734 parens.require_close (parser);
24735 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24736 finish_handler (handler);
24739 /* Parse an exception-declaration.
24741 exception-declaration:
24742 type-specifier-seq declarator
24743 type-specifier-seq abstract-declarator
24744 type-specifier-seq
24747 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24748 ellipsis variant is used. */
24750 static tree
24751 cp_parser_exception_declaration (cp_parser* parser)
24753 cp_decl_specifier_seq type_specifiers;
24754 cp_declarator *declarator;
24755 const char *saved_message;
24757 /* If it's an ellipsis, it's easy to handle. */
24758 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24760 /* Consume the `...' token. */
24761 cp_lexer_consume_token (parser->lexer);
24762 return NULL_TREE;
24765 /* Types may not be defined in exception-declarations. */
24766 saved_message = parser->type_definition_forbidden_message;
24767 parser->type_definition_forbidden_message
24768 = G_("types may not be defined in exception-declarations");
24770 /* Parse the type-specifier-seq. */
24771 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24772 /*is_trailing_return=*/false,
24773 &type_specifiers);
24774 /* If it's a `)', then there is no declarator. */
24775 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24776 declarator = NULL;
24777 else
24778 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24779 /*ctor_dtor_or_conv_p=*/NULL,
24780 /*parenthesized_p=*/NULL,
24781 /*member_p=*/false,
24782 /*friend_p=*/false);
24784 /* Restore the saved message. */
24785 parser->type_definition_forbidden_message = saved_message;
24787 if (!type_specifiers.any_specifiers_p)
24788 return error_mark_node;
24790 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24793 /* Parse a throw-expression.
24795 throw-expression:
24796 throw assignment-expression [opt]
24798 Returns a THROW_EXPR representing the throw-expression. */
24800 static tree
24801 cp_parser_throw_expression (cp_parser* parser)
24803 tree expression;
24804 cp_token* token;
24806 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24807 token = cp_lexer_peek_token (parser->lexer);
24808 /* Figure out whether or not there is an assignment-expression
24809 following the "throw" keyword. */
24810 if (token->type == CPP_COMMA
24811 || token->type == CPP_SEMICOLON
24812 || token->type == CPP_CLOSE_PAREN
24813 || token->type == CPP_CLOSE_SQUARE
24814 || token->type == CPP_CLOSE_BRACE
24815 || token->type == CPP_COLON)
24816 expression = NULL_TREE;
24817 else
24818 expression = cp_parser_assignment_expression (parser);
24820 return build_throw (expression);
24823 /* GNU Extensions */
24825 /* Parse an (optional) asm-specification.
24827 asm-specification:
24828 asm ( string-literal )
24830 If the asm-specification is present, returns a STRING_CST
24831 corresponding to the string-literal. Otherwise, returns
24832 NULL_TREE. */
24834 static tree
24835 cp_parser_asm_specification_opt (cp_parser* parser)
24837 cp_token *token;
24838 tree asm_specification;
24840 /* Peek at the next token. */
24841 token = cp_lexer_peek_token (parser->lexer);
24842 /* If the next token isn't the `asm' keyword, then there's no
24843 asm-specification. */
24844 if (!cp_parser_is_keyword (token, RID_ASM))
24845 return NULL_TREE;
24847 /* Consume the `asm' token. */
24848 cp_lexer_consume_token (parser->lexer);
24849 /* Look for the `('. */
24850 matching_parens parens;
24851 parens.require_open (parser);
24853 /* Look for the string-literal. */
24854 asm_specification = cp_parser_string_literal (parser, false, false);
24856 /* Look for the `)'. */
24857 parens.require_close (parser);
24859 return asm_specification;
24862 /* Parse an asm-operand-list.
24864 asm-operand-list:
24865 asm-operand
24866 asm-operand-list , asm-operand
24868 asm-operand:
24869 string-literal ( expression )
24870 [ string-literal ] string-literal ( expression )
24872 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24873 each node is the expression. The TREE_PURPOSE is itself a
24874 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24875 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24876 is a STRING_CST for the string literal before the parenthesis. Returns
24877 ERROR_MARK_NODE if any of the operands are invalid. */
24879 static tree
24880 cp_parser_asm_operand_list (cp_parser* parser)
24882 tree asm_operands = NULL_TREE;
24883 bool invalid_operands = false;
24885 while (true)
24887 tree string_literal;
24888 tree expression;
24889 tree name;
24891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24893 /* Consume the `[' token. */
24894 cp_lexer_consume_token (parser->lexer);
24895 /* Read the operand name. */
24896 name = cp_parser_identifier (parser);
24897 if (name != error_mark_node)
24898 name = build_string (IDENTIFIER_LENGTH (name),
24899 IDENTIFIER_POINTER (name));
24900 /* Look for the closing `]'. */
24901 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24903 else
24904 name = NULL_TREE;
24905 /* Look for the string-literal. */
24906 string_literal = cp_parser_string_literal (parser, false, false);
24908 /* Look for the `('. */
24909 matching_parens parens;
24910 parens.require_open (parser);
24911 /* Parse the expression. */
24912 expression = cp_parser_expression (parser);
24913 /* Look for the `)'. */
24914 parens.require_close (parser);
24916 if (name == error_mark_node
24917 || string_literal == error_mark_node
24918 || expression == error_mark_node)
24919 invalid_operands = true;
24921 /* Add this operand to the list. */
24922 asm_operands = tree_cons (build_tree_list (name, string_literal),
24923 expression,
24924 asm_operands);
24925 /* If the next token is not a `,', there are no more
24926 operands. */
24927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24928 break;
24929 /* Consume the `,'. */
24930 cp_lexer_consume_token (parser->lexer);
24933 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24936 /* Parse an asm-clobber-list.
24938 asm-clobber-list:
24939 string-literal
24940 asm-clobber-list , string-literal
24942 Returns a TREE_LIST, indicating the clobbers in the order that they
24943 appeared. The TREE_VALUE of each node is a STRING_CST. */
24945 static tree
24946 cp_parser_asm_clobber_list (cp_parser* parser)
24948 tree clobbers = NULL_TREE;
24950 while (true)
24952 tree string_literal;
24954 /* Look for the string literal. */
24955 string_literal = cp_parser_string_literal (parser, false, false);
24956 /* Add it to the list. */
24957 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24958 /* If the next token is not a `,', then the list is
24959 complete. */
24960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24961 break;
24962 /* Consume the `,' token. */
24963 cp_lexer_consume_token (parser->lexer);
24966 return clobbers;
24969 /* Parse an asm-label-list.
24971 asm-label-list:
24972 identifier
24973 asm-label-list , identifier
24975 Returns a TREE_LIST, indicating the labels in the order that they
24976 appeared. The TREE_VALUE of each node is a label. */
24978 static tree
24979 cp_parser_asm_label_list (cp_parser* parser)
24981 tree labels = NULL_TREE;
24983 while (true)
24985 tree identifier, label, name;
24987 /* Look for the identifier. */
24988 identifier = cp_parser_identifier (parser);
24989 if (!error_operand_p (identifier))
24991 label = lookup_label (identifier);
24992 if (TREE_CODE (label) == LABEL_DECL)
24994 TREE_USED (label) = 1;
24995 check_goto (label);
24996 name = build_string (IDENTIFIER_LENGTH (identifier),
24997 IDENTIFIER_POINTER (identifier));
24998 labels = tree_cons (name, label, labels);
25001 /* If the next token is not a `,', then the list is
25002 complete. */
25003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25004 break;
25005 /* Consume the `,' token. */
25006 cp_lexer_consume_token (parser->lexer);
25009 return nreverse (labels);
25012 /* Return TRUE iff the next tokens in the stream are possibly the
25013 beginning of a GNU extension attribute. */
25015 static bool
25016 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25018 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25021 /* Return TRUE iff the next tokens in the stream are possibly the
25022 beginning of a standard C++-11 attribute specifier. */
25024 static bool
25025 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25027 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25030 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25031 beginning of a standard C++-11 attribute specifier. */
25033 static bool
25034 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25036 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25038 return (cxx_dialect >= cxx11
25039 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25040 || (token->type == CPP_OPEN_SQUARE
25041 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25042 && token->type == CPP_OPEN_SQUARE)));
25045 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25046 beginning of a GNU extension attribute. */
25048 static bool
25049 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25051 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25053 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25056 /* Return true iff the next tokens can be the beginning of either a
25057 GNU attribute list, or a standard C++11 attribute sequence. */
25059 static bool
25060 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25062 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25063 || cp_next_tokens_can_be_std_attribute_p (parser));
25066 /* Return true iff the next Nth tokens can be the beginning of either
25067 a GNU attribute list, or a standard C++11 attribute sequence. */
25069 static bool
25070 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25072 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25073 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25076 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25077 of GNU attributes, or return NULL. */
25079 static tree
25080 cp_parser_attributes_opt (cp_parser *parser)
25082 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25083 return cp_parser_gnu_attributes_opt (parser);
25084 return cp_parser_std_attribute_spec_seq (parser);
25087 /* Parse an (optional) series of attributes.
25089 attributes:
25090 attributes attribute
25092 attribute:
25093 __attribute__ (( attribute-list [opt] ))
25095 The return value is as for cp_parser_gnu_attribute_list. */
25097 static tree
25098 cp_parser_gnu_attributes_opt (cp_parser* parser)
25100 tree attributes = NULL_TREE;
25102 temp_override<bool> cleanup
25103 (parser->auto_is_implicit_function_template_parm_p, false);
25105 while (true)
25107 cp_token *token;
25108 tree attribute_list;
25109 bool ok = true;
25111 /* Peek at the next token. */
25112 token = cp_lexer_peek_token (parser->lexer);
25113 /* If it's not `__attribute__', then we're done. */
25114 if (token->keyword != RID_ATTRIBUTE)
25115 break;
25117 /* Consume the `__attribute__' keyword. */
25118 cp_lexer_consume_token (parser->lexer);
25119 /* Look for the two `(' tokens. */
25120 matching_parens outer_parens;
25121 outer_parens.require_open (parser);
25122 matching_parens inner_parens;
25123 inner_parens.require_open (parser);
25125 /* Peek at the next token. */
25126 token = cp_lexer_peek_token (parser->lexer);
25127 if (token->type != CPP_CLOSE_PAREN)
25128 /* Parse the attribute-list. */
25129 attribute_list = cp_parser_gnu_attribute_list (parser);
25130 else
25131 /* If the next token is a `)', then there is no attribute
25132 list. */
25133 attribute_list = NULL;
25135 /* Look for the two `)' tokens. */
25136 if (!inner_parens.require_close (parser))
25137 ok = false;
25138 if (!outer_parens.require_close (parser))
25139 ok = false;
25140 if (!ok)
25141 cp_parser_skip_to_end_of_statement (parser);
25143 /* Add these new attributes to the list. */
25144 attributes = attr_chainon (attributes, attribute_list);
25147 return attributes;
25150 /* Parse a GNU attribute-list.
25152 attribute-list:
25153 attribute
25154 attribute-list , attribute
25156 attribute:
25157 identifier
25158 identifier ( identifier )
25159 identifier ( identifier , expression-list )
25160 identifier ( expression-list )
25162 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25163 to an attribute. The TREE_PURPOSE of each node is the identifier
25164 indicating which attribute is in use. The TREE_VALUE represents
25165 the arguments, if any. */
25167 static tree
25168 cp_parser_gnu_attribute_list (cp_parser* parser)
25170 tree attribute_list = NULL_TREE;
25171 bool save_translate_strings_p = parser->translate_strings_p;
25173 parser->translate_strings_p = false;
25174 while (true)
25176 cp_token *token;
25177 tree identifier;
25178 tree attribute;
25180 /* Look for the identifier. We also allow keywords here; for
25181 example `__attribute__ ((const))' is legal. */
25182 token = cp_lexer_peek_token (parser->lexer);
25183 if (token->type == CPP_NAME
25184 || token->type == CPP_KEYWORD)
25186 tree arguments = NULL_TREE;
25188 /* Consume the token, but save it since we need it for the
25189 SIMD enabled function parsing. */
25190 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25192 /* Save away the identifier that indicates which attribute
25193 this is. */
25194 identifier = (token->type == CPP_KEYWORD)
25195 /* For keywords, use the canonical spelling, not the
25196 parsed identifier. */
25197 ? ridpointers[(int) token->keyword]
25198 : id_token->u.value;
25200 identifier = canonicalize_attr_name (identifier);
25201 attribute = build_tree_list (identifier, NULL_TREE);
25203 /* Peek at the next token. */
25204 token = cp_lexer_peek_token (parser->lexer);
25205 /* If it's an `(', then parse the attribute arguments. */
25206 if (token->type == CPP_OPEN_PAREN)
25208 vec<tree, va_gc> *vec;
25209 int attr_flag = (attribute_takes_identifier_p (identifier)
25210 ? id_attr : normal_attr);
25211 vec = cp_parser_parenthesized_expression_list
25212 (parser, attr_flag, /*cast_p=*/false,
25213 /*allow_expansion_p=*/false,
25214 /*non_constant_p=*/NULL);
25215 if (vec == NULL)
25216 arguments = error_mark_node;
25217 else
25219 arguments = build_tree_list_vec (vec);
25220 release_tree_vector (vec);
25222 /* Save the arguments away. */
25223 TREE_VALUE (attribute) = arguments;
25226 if (arguments != error_mark_node)
25228 /* Add this attribute to the list. */
25229 TREE_CHAIN (attribute) = attribute_list;
25230 attribute_list = attribute;
25233 token = cp_lexer_peek_token (parser->lexer);
25235 /* Now, look for more attributes. If the next token isn't a
25236 `,', we're done. */
25237 if (token->type != CPP_COMMA)
25238 break;
25240 /* Consume the comma and keep going. */
25241 cp_lexer_consume_token (parser->lexer);
25243 parser->translate_strings_p = save_translate_strings_p;
25245 /* We built up the list in reverse order. */
25246 return nreverse (attribute_list);
25249 /* Parse a standard C++11 attribute.
25251 The returned representation is a TREE_LIST which TREE_PURPOSE is
25252 the scoped name of the attribute, and the TREE_VALUE is its
25253 arguments list.
25255 Note that the scoped name of the attribute is itself a TREE_LIST
25256 which TREE_PURPOSE is the namespace of the attribute, and
25257 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25258 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25259 and which TREE_PURPOSE is directly the attribute name.
25261 Clients of the attribute code should use get_attribute_namespace
25262 and get_attribute_name to get the actual namespace and name of
25263 attributes, regardless of their being GNU or C++11 attributes.
25265 attribute:
25266 attribute-token attribute-argument-clause [opt]
25268 attribute-token:
25269 identifier
25270 attribute-scoped-token
25272 attribute-scoped-token:
25273 attribute-namespace :: identifier
25275 attribute-namespace:
25276 identifier
25278 attribute-argument-clause:
25279 ( balanced-token-seq )
25281 balanced-token-seq:
25282 balanced-token [opt]
25283 balanced-token-seq balanced-token
25285 balanced-token:
25286 ( balanced-token-seq )
25287 [ balanced-token-seq ]
25288 { balanced-token-seq }. */
25290 static tree
25291 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25293 tree attribute, attr_id = NULL_TREE, arguments;
25294 cp_token *token;
25296 temp_override<bool> cleanup
25297 (parser->auto_is_implicit_function_template_parm_p, false);
25299 /* First, parse name of the attribute, a.k.a attribute-token. */
25301 token = cp_lexer_peek_token (parser->lexer);
25302 if (token->type == CPP_NAME)
25303 attr_id = token->u.value;
25304 else if (token->type == CPP_KEYWORD)
25305 attr_id = ridpointers[(int) token->keyword];
25306 else if (token->flags & NAMED_OP)
25307 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25309 if (attr_id == NULL_TREE)
25310 return NULL_TREE;
25312 cp_lexer_consume_token (parser->lexer);
25314 token = cp_lexer_peek_token (parser->lexer);
25315 if (token->type == CPP_SCOPE)
25317 /* We are seeing a scoped attribute token. */
25319 cp_lexer_consume_token (parser->lexer);
25320 if (attr_ns)
25321 error_at (token->location, "attribute using prefix used together "
25322 "with scoped attribute token");
25323 attr_ns = attr_id;
25325 token = cp_lexer_consume_token (parser->lexer);
25326 if (token->type == CPP_NAME)
25327 attr_id = token->u.value;
25328 else if (token->type == CPP_KEYWORD)
25329 attr_id = ridpointers[(int) token->keyword];
25330 else if (token->flags & NAMED_OP)
25331 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25332 else
25334 error_at (token->location,
25335 "expected an identifier for the attribute name");
25336 return error_mark_node;
25339 attr_id = canonicalize_attr_name (attr_id);
25340 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25341 NULL_TREE);
25342 token = cp_lexer_peek_token (parser->lexer);
25344 else if (attr_ns)
25345 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25346 NULL_TREE);
25347 else
25349 attr_id = canonicalize_attr_name (attr_id);
25350 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25351 NULL_TREE);
25352 /* C++11 noreturn attribute is equivalent to GNU's. */
25353 if (is_attribute_p ("noreturn", attr_id))
25354 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25355 /* C++14 deprecated attribute is equivalent to GNU's. */
25356 else if (is_attribute_p ("deprecated", attr_id))
25357 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25358 /* C++17 fallthrough attribute is equivalent to GNU's. */
25359 else if (is_attribute_p ("fallthrough", attr_id))
25360 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25361 /* Transactional Memory TS optimize_for_synchronized attribute is
25362 equivalent to GNU transaction_callable. */
25363 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25364 TREE_PURPOSE (attribute)
25365 = get_identifier ("transaction_callable");
25366 /* Transactional Memory attributes are GNU attributes. */
25367 else if (tm_attr_to_mask (attr_id))
25368 TREE_PURPOSE (attribute) = attr_id;
25371 /* Now parse the optional argument clause of the attribute. */
25373 if (token->type != CPP_OPEN_PAREN)
25374 return attribute;
25377 vec<tree, va_gc> *vec;
25378 int attr_flag = normal_attr;
25380 if (attr_ns == gnu_identifier
25381 && attribute_takes_identifier_p (attr_id))
25382 /* A GNU attribute that takes an identifier in parameter. */
25383 attr_flag = id_attr;
25385 vec = cp_parser_parenthesized_expression_list
25386 (parser, attr_flag, /*cast_p=*/false,
25387 /*allow_expansion_p=*/true,
25388 /*non_constant_p=*/NULL);
25389 if (vec == NULL)
25390 arguments = error_mark_node;
25391 else
25393 arguments = build_tree_list_vec (vec);
25394 release_tree_vector (vec);
25397 if (arguments == error_mark_node)
25398 attribute = error_mark_node;
25399 else
25400 TREE_VALUE (attribute) = arguments;
25403 return attribute;
25406 /* Check that the attribute ATTRIBUTE appears at most once in the
25407 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25408 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25409 isn't implemented yet in GCC. */
25411 static void
25412 cp_parser_check_std_attribute (tree attributes, tree attribute)
25414 if (attributes)
25416 tree name = get_attribute_name (attribute);
25417 if (is_attribute_p ("noreturn", name)
25418 && lookup_attribute ("noreturn", attributes))
25419 error ("attribute %<noreturn%> can appear at most once "
25420 "in an attribute-list");
25421 else if (is_attribute_p ("deprecated", name)
25422 && lookup_attribute ("deprecated", attributes))
25423 error ("attribute %<deprecated%> can appear at most once "
25424 "in an attribute-list");
25428 /* Parse a list of standard C++-11 attributes.
25430 attribute-list:
25431 attribute [opt]
25432 attribute-list , attribute[opt]
25433 attribute ...
25434 attribute-list , attribute ...
25437 static tree
25438 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25440 tree attributes = NULL_TREE, attribute = NULL_TREE;
25441 cp_token *token = NULL;
25443 while (true)
25445 attribute = cp_parser_std_attribute (parser, attr_ns);
25446 if (attribute == error_mark_node)
25447 break;
25448 if (attribute != NULL_TREE)
25450 cp_parser_check_std_attribute (attributes, attribute);
25451 TREE_CHAIN (attribute) = attributes;
25452 attributes = attribute;
25454 token = cp_lexer_peek_token (parser->lexer);
25455 if (token->type == CPP_ELLIPSIS)
25457 cp_lexer_consume_token (parser->lexer);
25458 if (attribute == NULL_TREE)
25459 error_at (token->location,
25460 "expected attribute before %<...%>");
25461 else
25463 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25464 if (pack == error_mark_node)
25465 return error_mark_node;
25466 TREE_VALUE (attribute) = pack;
25468 token = cp_lexer_peek_token (parser->lexer);
25470 if (token->type != CPP_COMMA)
25471 break;
25472 cp_lexer_consume_token (parser->lexer);
25474 attributes = nreverse (attributes);
25475 return attributes;
25478 /* Parse a standard C++-11 attribute specifier.
25480 attribute-specifier:
25481 [ [ attribute-using-prefix [opt] attribute-list ] ]
25482 alignment-specifier
25484 attribute-using-prefix:
25485 using attribute-namespace :
25487 alignment-specifier:
25488 alignas ( type-id ... [opt] )
25489 alignas ( alignment-expression ... [opt] ). */
25491 static tree
25492 cp_parser_std_attribute_spec (cp_parser *parser)
25494 tree attributes = NULL_TREE;
25495 cp_token *token = cp_lexer_peek_token (parser->lexer);
25497 if (token->type == CPP_OPEN_SQUARE
25498 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25500 tree attr_ns = NULL_TREE;
25502 cp_lexer_consume_token (parser->lexer);
25503 cp_lexer_consume_token (parser->lexer);
25505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25507 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25508 if (token->type == CPP_NAME)
25509 attr_ns = token->u.value;
25510 else if (token->type == CPP_KEYWORD)
25511 attr_ns = ridpointers[(int) token->keyword];
25512 else if (token->flags & NAMED_OP)
25513 attr_ns = get_identifier (cpp_type2name (token->type,
25514 token->flags));
25515 if (attr_ns
25516 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25518 if (cxx_dialect < cxx17
25519 && !in_system_header_at (input_location))
25520 pedwarn (input_location, 0,
25521 "attribute using prefix only available "
25522 "with -std=c++17 or -std=gnu++17");
25524 cp_lexer_consume_token (parser->lexer);
25525 cp_lexer_consume_token (parser->lexer);
25526 cp_lexer_consume_token (parser->lexer);
25528 else
25529 attr_ns = NULL_TREE;
25532 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25534 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25535 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25536 cp_parser_skip_to_end_of_statement (parser);
25537 else
25538 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25539 when we are sure that we have actually parsed them. */
25540 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25542 else
25544 tree alignas_expr;
25546 /* Look for an alignment-specifier. */
25548 token = cp_lexer_peek_token (parser->lexer);
25550 if (token->type != CPP_KEYWORD
25551 || token->keyword != RID_ALIGNAS)
25552 return NULL_TREE;
25554 cp_lexer_consume_token (parser->lexer);
25555 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25557 matching_parens parens;
25558 if (!parens.require_open (parser))
25559 return error_mark_node;
25561 cp_parser_parse_tentatively (parser);
25562 alignas_expr = cp_parser_type_id (parser);
25564 if (!cp_parser_parse_definitely (parser))
25566 alignas_expr = cp_parser_assignment_expression (parser);
25567 if (alignas_expr == error_mark_node)
25568 cp_parser_skip_to_end_of_statement (parser);
25569 if (alignas_expr == NULL_TREE
25570 || alignas_expr == error_mark_node)
25571 return alignas_expr;
25574 alignas_expr = cxx_alignas_expr (alignas_expr);
25575 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25577 /* Handle alignas (pack...). */
25578 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25580 cp_lexer_consume_token (parser->lexer);
25581 alignas_expr = make_pack_expansion (alignas_expr);
25584 /* Something went wrong, so don't build the attribute. */
25585 if (alignas_expr == error_mark_node)
25586 return error_mark_node;
25588 if (!parens.require_close (parser))
25589 return error_mark_node;
25591 /* Build the C++-11 representation of an 'aligned'
25592 attribute. */
25593 attributes
25594 = build_tree_list (build_tree_list (gnu_identifier,
25595 aligned_identifier), alignas_expr);
25598 return attributes;
25601 /* Parse a standard C++-11 attribute-specifier-seq.
25603 attribute-specifier-seq:
25604 attribute-specifier-seq [opt] attribute-specifier
25607 static tree
25608 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25610 tree attr_specs = NULL_TREE;
25611 tree attr_last = NULL_TREE;
25613 while (true)
25615 tree attr_spec = cp_parser_std_attribute_spec (parser);
25616 if (attr_spec == NULL_TREE)
25617 break;
25618 if (attr_spec == error_mark_node)
25619 return error_mark_node;
25621 if (attr_last)
25622 TREE_CHAIN (attr_last) = attr_spec;
25623 else
25624 attr_specs = attr_last = attr_spec;
25625 attr_last = tree_last (attr_last);
25628 return attr_specs;
25631 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25632 return index of the first token after balanced-token, or N on failure. */
25634 static size_t
25635 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25637 size_t orig_n = n;
25638 int nparens = 0, nbraces = 0, nsquares = 0;
25640 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25642 case CPP_EOF:
25643 case CPP_PRAGMA_EOL:
25644 /* Ran out of tokens. */
25645 return orig_n;
25646 case CPP_OPEN_PAREN:
25647 ++nparens;
25648 break;
25649 case CPP_OPEN_BRACE:
25650 ++nbraces;
25651 break;
25652 case CPP_OPEN_SQUARE:
25653 ++nsquares;
25654 break;
25655 case CPP_CLOSE_PAREN:
25656 --nparens;
25657 break;
25658 case CPP_CLOSE_BRACE:
25659 --nbraces;
25660 break;
25661 case CPP_CLOSE_SQUARE:
25662 --nsquares;
25663 break;
25664 default:
25665 break;
25667 while (nparens || nbraces || nsquares);
25668 return n;
25671 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25672 return index of the first token after the GNU attribute tokens, or N on
25673 failure. */
25675 static size_t
25676 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25678 while (true)
25680 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25681 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25682 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25683 break;
25685 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25686 if (n2 == n + 2)
25687 break;
25688 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25689 break;
25690 n = n2 + 1;
25692 return n;
25695 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25696 next token), return index of the first token after the standard C++11
25697 attribute tokens, or N on failure. */
25699 static size_t
25700 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25702 while (true)
25704 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25705 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25707 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25708 if (n2 == n + 1)
25709 break;
25710 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25711 break;
25712 n = n2 + 1;
25714 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25715 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25717 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25718 if (n2 == n + 1)
25719 break;
25720 n = n2;
25722 else
25723 break;
25725 return n;
25728 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25729 as the next token), return index of the first token after the attribute
25730 tokens, or N on failure. */
25732 static size_t
25733 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25735 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25736 return cp_parser_skip_gnu_attributes_opt (parser, n);
25737 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25740 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25741 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25742 current value of the PEDANTIC flag, regardless of whether or not
25743 the `__extension__' keyword is present. The caller is responsible
25744 for restoring the value of the PEDANTIC flag. */
25746 static bool
25747 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25749 /* Save the old value of the PEDANTIC flag. */
25750 *saved_pedantic = pedantic;
25752 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25754 /* Consume the `__extension__' token. */
25755 cp_lexer_consume_token (parser->lexer);
25756 /* We're not being pedantic while the `__extension__' keyword is
25757 in effect. */
25758 pedantic = 0;
25760 return true;
25763 return false;
25766 /* Parse a label declaration.
25768 label-declaration:
25769 __label__ label-declarator-seq ;
25771 label-declarator-seq:
25772 identifier , label-declarator-seq
25773 identifier */
25775 static void
25776 cp_parser_label_declaration (cp_parser* parser)
25778 /* Look for the `__label__' keyword. */
25779 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25781 while (true)
25783 tree identifier;
25785 /* Look for an identifier. */
25786 identifier = cp_parser_identifier (parser);
25787 /* If we failed, stop. */
25788 if (identifier == error_mark_node)
25789 break;
25790 /* Declare it as a label. */
25791 finish_label_decl (identifier);
25792 /* If the next token is a `;', stop. */
25793 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25794 break;
25795 /* Look for the `,' separating the label declarations. */
25796 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25799 /* Look for the final `;'. */
25800 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25803 // -------------------------------------------------------------------------- //
25804 // Requires Clause
25806 // Parse a requires clause.
25808 // requires-clause:
25809 // 'requires' logical-or-expression
25811 // The required logical-or-expression must be a constant expression. Note
25812 // that we don't check that the expression is constepxr here. We defer until
25813 // we analyze constraints and then, we only check atomic constraints.
25814 static tree
25815 cp_parser_requires_clause (cp_parser *parser)
25817 // Parse the requires clause so that it is not automatically folded.
25818 ++processing_template_decl;
25819 tree expr = cp_parser_binary_expression (parser, false, false,
25820 PREC_NOT_OPERATOR, NULL);
25821 if (check_for_bare_parameter_packs (expr))
25822 expr = error_mark_node;
25823 --processing_template_decl;
25824 return expr;
25827 // Optionally parse a requires clause:
25828 static tree
25829 cp_parser_requires_clause_opt (cp_parser *parser)
25831 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25832 if (tok->keyword != RID_REQUIRES)
25834 if (!flag_concepts && tok->type == CPP_NAME
25835 && tok->u.value == ridpointers[RID_REQUIRES])
25837 error_at (cp_lexer_peek_token (parser->lexer)->location,
25838 "%<requires%> only available with -fconcepts");
25839 /* Parse and discard the requires-clause. */
25840 cp_lexer_consume_token (parser->lexer);
25841 cp_parser_requires_clause (parser);
25843 return NULL_TREE;
25845 cp_lexer_consume_token (parser->lexer);
25846 return cp_parser_requires_clause (parser);
25850 /*---------------------------------------------------------------------------
25851 Requires expressions
25852 ---------------------------------------------------------------------------*/
25854 /* Parse a requires expression
25856 requirement-expression:
25857 'requires' requirement-parameter-list [opt] requirement-body */
25858 static tree
25859 cp_parser_requires_expression (cp_parser *parser)
25861 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25862 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25864 /* A requires-expression shall appear only within a concept
25865 definition or a requires-clause.
25867 TODO: Implement this diagnostic correctly. */
25868 if (!processing_template_decl)
25870 error_at (loc, "a requires expression cannot appear outside a template");
25871 cp_parser_skip_to_end_of_statement (parser);
25872 return error_mark_node;
25875 tree parms, reqs;
25877 /* Local parameters are delared as variables within the scope
25878 of the expression. They are not visible past the end of
25879 the expression. Expressions within the requires-expression
25880 are unevaluated. */
25881 struct scope_sentinel
25883 scope_sentinel ()
25885 ++cp_unevaluated_operand;
25886 begin_scope (sk_block, NULL_TREE);
25889 ~scope_sentinel ()
25891 pop_bindings_and_leave_scope ();
25892 --cp_unevaluated_operand;
25894 } s;
25896 /* Parse the optional parameter list. */
25897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25899 parms = cp_parser_requirement_parameter_list (parser);
25900 if (parms == error_mark_node)
25901 return error_mark_node;
25903 else
25904 parms = NULL_TREE;
25906 /* Parse the requirement body. */
25907 reqs = cp_parser_requirement_body (parser);
25908 if (reqs == error_mark_node)
25909 return error_mark_node;
25912 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25913 the parm chain. */
25914 grokparms (parms, &parms);
25915 return finish_requires_expr (parms, reqs);
25918 /* Parse a parameterized requirement.
25920 requirement-parameter-list:
25921 '(' parameter-declaration-clause ')' */
25922 static tree
25923 cp_parser_requirement_parameter_list (cp_parser *parser)
25925 matching_parens parens;
25926 if (!parens.require_open (parser))
25927 return error_mark_node;
25929 tree parms = cp_parser_parameter_declaration_clause (parser);
25931 if (!parens.require_close (parser))
25932 return error_mark_node;
25934 return parms;
25937 /* Parse the body of a requirement.
25939 requirement-body:
25940 '{' requirement-list '}' */
25941 static tree
25942 cp_parser_requirement_body (cp_parser *parser)
25944 matching_braces braces;
25945 if (!braces.require_open (parser))
25946 return error_mark_node;
25948 tree reqs = cp_parser_requirement_list (parser);
25950 if (!braces.require_close (parser))
25951 return error_mark_node;
25953 return reqs;
25956 /* Parse a list of requirements.
25958 requirement-list:
25959 requirement
25960 requirement-list ';' requirement[opt] */
25961 static tree
25962 cp_parser_requirement_list (cp_parser *parser)
25964 tree result = NULL_TREE;
25965 while (true)
25967 tree req = cp_parser_requirement (parser);
25968 if (req == error_mark_node)
25969 return error_mark_node;
25971 result = tree_cons (NULL_TREE, req, result);
25973 /* If we see a semi-colon, consume it. */
25974 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25975 cp_lexer_consume_token (parser->lexer);
25977 /* Stop processing at the end of the list. */
25978 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25979 break;
25982 /* Reverse the order of requirements so they are analyzed in
25983 declaration order. */
25984 return nreverse (result);
25987 /* Parse a syntactic requirement or type requirement.
25989 requirement:
25990 simple-requirement
25991 compound-requirement
25992 type-requirement
25993 nested-requirement */
25994 static tree
25995 cp_parser_requirement (cp_parser *parser)
25997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25998 return cp_parser_compound_requirement (parser);
25999 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26000 return cp_parser_type_requirement (parser);
26001 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26002 return cp_parser_nested_requirement (parser);
26003 else
26004 return cp_parser_simple_requirement (parser);
26007 /* Parse a simple requirement.
26009 simple-requirement:
26010 expression ';' */
26011 static tree
26012 cp_parser_simple_requirement (cp_parser *parser)
26014 tree expr = cp_parser_expression (parser, NULL, false, false);
26015 if (!expr || expr == error_mark_node)
26016 return error_mark_node;
26018 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26019 return error_mark_node;
26021 return finish_simple_requirement (expr);
26024 /* Parse a type requirement
26026 type-requirement
26027 nested-name-specifier [opt] required-type-name ';'
26029 required-type-name:
26030 type-name
26031 'template' [opt] simple-template-id */
26032 static tree
26033 cp_parser_type_requirement (cp_parser *parser)
26035 cp_lexer_consume_token (parser->lexer);
26037 // Save the scope before parsing name specifiers.
26038 tree saved_scope = parser->scope;
26039 tree saved_object_scope = parser->object_scope;
26040 tree saved_qualifying_scope = parser->qualifying_scope;
26041 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26042 cp_parser_nested_name_specifier_opt (parser,
26043 /*typename_keyword_p=*/true,
26044 /*check_dependency_p=*/false,
26045 /*type_p=*/true,
26046 /*is_declaration=*/false);
26048 tree type;
26049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26051 cp_lexer_consume_token (parser->lexer);
26052 type = cp_parser_template_id (parser,
26053 /*template_keyword_p=*/true,
26054 /*check_dependency=*/false,
26055 /*tag_type=*/none_type,
26056 /*is_declaration=*/false);
26057 type = make_typename_type (parser->scope, type, typename_type,
26058 /*complain=*/tf_error);
26060 else
26061 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26063 if (TREE_CODE (type) == TYPE_DECL)
26064 type = TREE_TYPE (type);
26066 parser->scope = saved_scope;
26067 parser->object_scope = saved_object_scope;
26068 parser->qualifying_scope = saved_qualifying_scope;
26070 if (type == error_mark_node)
26071 cp_parser_skip_to_end_of_statement (parser);
26073 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26074 return error_mark_node;
26075 if (type == error_mark_node)
26076 return error_mark_node;
26078 return finish_type_requirement (type);
26081 /* Parse a compound requirement
26083 compound-requirement:
26084 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26085 static tree
26086 cp_parser_compound_requirement (cp_parser *parser)
26088 /* Parse an expression enclosed in '{ }'s. */
26089 matching_braces braces;
26090 if (!braces.require_open (parser))
26091 return error_mark_node;
26093 tree expr = cp_parser_expression (parser, NULL, false, false);
26094 if (!expr || expr == error_mark_node)
26095 return error_mark_node;
26097 if (!braces.require_close (parser))
26098 return error_mark_node;
26100 /* Parse the optional noexcept. */
26101 bool noexcept_p = false;
26102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26104 cp_lexer_consume_token (parser->lexer);
26105 noexcept_p = true;
26108 /* Parse the optional trailing return type. */
26109 tree type = NULL_TREE;
26110 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26112 cp_lexer_consume_token (parser->lexer);
26113 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26114 parser->in_result_type_constraint_p = true;
26115 type = cp_parser_trailing_type_id (parser);
26116 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26117 if (type == error_mark_node)
26118 return error_mark_node;
26121 return finish_compound_requirement (expr, type, noexcept_p);
26124 /* Parse a nested requirement. This is the same as a requires clause.
26126 nested-requirement:
26127 requires-clause */
26128 static tree
26129 cp_parser_nested_requirement (cp_parser *parser)
26131 cp_lexer_consume_token (parser->lexer);
26132 tree req = cp_parser_requires_clause (parser);
26133 if (req == error_mark_node)
26134 return error_mark_node;
26135 return finish_nested_requirement (req);
26138 /* Support Functions */
26140 /* Return the appropriate prefer_type argument for lookup_name_real based on
26141 tag_type and template_mem_access. */
26143 static inline int
26144 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26146 /* DR 141: When looking in the current enclosing context for a template-name
26147 after -> or ., only consider class templates. */
26148 if (template_mem_access)
26149 return 2;
26150 switch (tag_type)
26152 case none_type: return 0; // No preference.
26153 case scope_type: return 1; // Type or namespace.
26154 default: return 2; // Type only.
26158 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26159 NAME should have one of the representations used for an
26160 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26161 is returned. If PARSER->SCOPE is a dependent type, then a
26162 SCOPE_REF is returned.
26164 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26165 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26166 was formed. Abstractly, such entities should not be passed to this
26167 function, because they do not need to be looked up, but it is
26168 simpler to check for this special case here, rather than at the
26169 call-sites.
26171 In cases not explicitly covered above, this function returns a
26172 DECL, OVERLOAD, or baselink representing the result of the lookup.
26173 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26174 is returned.
26176 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26177 (e.g., "struct") that was used. In that case bindings that do not
26178 refer to types are ignored.
26180 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26181 ignored.
26183 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26184 are ignored.
26186 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26187 types.
26189 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26190 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26191 NULL_TREE otherwise. */
26193 static cp_expr
26194 cp_parser_lookup_name (cp_parser *parser, tree name,
26195 enum tag_types tag_type,
26196 bool is_template,
26197 bool is_namespace,
26198 bool check_dependency,
26199 tree *ambiguous_decls,
26200 location_t name_location)
26202 tree decl;
26203 tree object_type = parser->context->object_type;
26205 /* Assume that the lookup will be unambiguous. */
26206 if (ambiguous_decls)
26207 *ambiguous_decls = NULL_TREE;
26209 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26210 no longer valid. Note that if we are parsing tentatively, and
26211 the parse fails, OBJECT_TYPE will be automatically restored. */
26212 parser->context->object_type = NULL_TREE;
26214 if (name == error_mark_node)
26215 return error_mark_node;
26217 /* A template-id has already been resolved; there is no lookup to
26218 do. */
26219 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26220 return name;
26221 if (BASELINK_P (name))
26223 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26224 == TEMPLATE_ID_EXPR);
26225 return name;
26228 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26229 it should already have been checked to make sure that the name
26230 used matches the type being destroyed. */
26231 if (TREE_CODE (name) == BIT_NOT_EXPR)
26233 tree type;
26235 /* Figure out to which type this destructor applies. */
26236 if (parser->scope)
26237 type = parser->scope;
26238 else if (object_type)
26239 type = object_type;
26240 else
26241 type = current_class_type;
26242 /* If that's not a class type, there is no destructor. */
26243 if (!type || !CLASS_TYPE_P (type))
26244 return error_mark_node;
26246 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26247 lazily_declare_fn (sfk_destructor, type);
26249 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26250 return dtor;
26252 return error_mark_node;
26255 /* By this point, the NAME should be an ordinary identifier. If
26256 the id-expression was a qualified name, the qualifying scope is
26257 stored in PARSER->SCOPE at this point. */
26258 gcc_assert (identifier_p (name));
26260 /* Perform the lookup. */
26261 if (parser->scope)
26263 bool dependent_p;
26265 if (parser->scope == error_mark_node)
26266 return error_mark_node;
26268 /* If the SCOPE is dependent, the lookup must be deferred until
26269 the template is instantiated -- unless we are explicitly
26270 looking up names in uninstantiated templates. Even then, we
26271 cannot look up the name if the scope is not a class type; it
26272 might, for example, be a template type parameter. */
26273 dependent_p = (TYPE_P (parser->scope)
26274 && dependent_scope_p (parser->scope));
26275 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26276 && dependent_p)
26277 /* Defer lookup. */
26278 decl = error_mark_node;
26279 else
26281 tree pushed_scope = NULL_TREE;
26283 /* If PARSER->SCOPE is a dependent type, then it must be a
26284 class type, and we must not be checking dependencies;
26285 otherwise, we would have processed this lookup above. So
26286 that PARSER->SCOPE is not considered a dependent base by
26287 lookup_member, we must enter the scope here. */
26288 if (dependent_p)
26289 pushed_scope = push_scope (parser->scope);
26291 /* If the PARSER->SCOPE is a template specialization, it
26292 may be instantiated during name lookup. In that case,
26293 errors may be issued. Even if we rollback the current
26294 tentative parse, those errors are valid. */
26295 decl = lookup_qualified_name (parser->scope, name,
26296 prefer_type_arg (tag_type),
26297 /*complain=*/true);
26299 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26300 lookup result and the nested-name-specifier nominates a class C:
26301 * if the name specified after the nested-name-specifier, when
26302 looked up in C, is the injected-class-name of C (Clause 9), or
26303 * if the name specified after the nested-name-specifier is the
26304 same as the identifier or the simple-template-id's template-
26305 name in the last component of the nested-name-specifier,
26306 the name is instead considered to name the constructor of
26307 class C. [ Note: for example, the constructor is not an
26308 acceptable lookup result in an elaborated-type-specifier so
26309 the constructor would not be used in place of the
26310 injected-class-name. --end note ] Such a constructor name
26311 shall be used only in the declarator-id of a declaration that
26312 names a constructor or in a using-declaration. */
26313 if (tag_type == none_type
26314 && DECL_SELF_REFERENCE_P (decl)
26315 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26316 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26317 prefer_type_arg (tag_type),
26318 /*complain=*/true);
26320 /* If we have a single function from a using decl, pull it out. */
26321 if (TREE_CODE (decl) == OVERLOAD
26322 && !really_overloaded_fn (decl))
26323 decl = OVL_FUNCTION (decl);
26325 if (pushed_scope)
26326 pop_scope (pushed_scope);
26329 /* If the scope is a dependent type and either we deferred lookup or
26330 we did lookup but didn't find the name, rememeber the name. */
26331 if (decl == error_mark_node && TYPE_P (parser->scope)
26332 && dependent_type_p (parser->scope))
26334 if (tag_type)
26336 tree type;
26338 /* The resolution to Core Issue 180 says that `struct
26339 A::B' should be considered a type-name, even if `A'
26340 is dependent. */
26341 type = make_typename_type (parser->scope, name, tag_type,
26342 /*complain=*/tf_error);
26343 if (type != error_mark_node)
26344 decl = TYPE_NAME (type);
26346 else if (is_template
26347 && (cp_parser_next_token_ends_template_argument_p (parser)
26348 || cp_lexer_next_token_is (parser->lexer,
26349 CPP_CLOSE_PAREN)))
26350 decl = make_unbound_class_template (parser->scope,
26351 name, NULL_TREE,
26352 /*complain=*/tf_error);
26353 else
26354 decl = build_qualified_name (/*type=*/NULL_TREE,
26355 parser->scope, name,
26356 is_template);
26358 parser->qualifying_scope = parser->scope;
26359 parser->object_scope = NULL_TREE;
26361 else if (object_type)
26363 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26364 OBJECT_TYPE is not a class. */
26365 if (CLASS_TYPE_P (object_type))
26366 /* If the OBJECT_TYPE is a template specialization, it may
26367 be instantiated during name lookup. In that case, errors
26368 may be issued. Even if we rollback the current tentative
26369 parse, those errors are valid. */
26370 decl = lookup_member (object_type,
26371 name,
26372 /*protect=*/0,
26373 prefer_type_arg (tag_type),
26374 tf_warning_or_error);
26375 else
26376 decl = NULL_TREE;
26378 if (!decl)
26379 /* Look it up in the enclosing context. DR 141: When looking for a
26380 template-name after -> or ., only consider class templates. */
26381 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26382 /*nonclass=*/0,
26383 /*block_p=*/true, is_namespace, 0);
26384 if (object_type == unknown_type_node)
26385 /* The object is type-dependent, so we can't look anything up; we used
26386 this to get the DR 141 behavior. */
26387 object_type = NULL_TREE;
26388 parser->object_scope = object_type;
26389 parser->qualifying_scope = NULL_TREE;
26391 else
26393 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26394 /*nonclass=*/0,
26395 /*block_p=*/true, is_namespace, 0);
26396 parser->qualifying_scope = NULL_TREE;
26397 parser->object_scope = NULL_TREE;
26400 /* If the lookup failed, let our caller know. */
26401 if (!decl || decl == error_mark_node)
26402 return error_mark_node;
26404 /* Pull out the template from an injected-class-name (or multiple). */
26405 if (is_template)
26406 decl = maybe_get_template_decl_from_type_decl (decl);
26408 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26409 if (TREE_CODE (decl) == TREE_LIST)
26411 if (ambiguous_decls)
26412 *ambiguous_decls = decl;
26413 /* The error message we have to print is too complicated for
26414 cp_parser_error, so we incorporate its actions directly. */
26415 if (!cp_parser_simulate_error (parser))
26417 error_at (name_location, "reference to %qD is ambiguous",
26418 name);
26419 print_candidates (decl);
26421 return error_mark_node;
26424 gcc_assert (DECL_P (decl)
26425 || TREE_CODE (decl) == OVERLOAD
26426 || TREE_CODE (decl) == SCOPE_REF
26427 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26428 || BASELINK_P (decl));
26430 /* If we have resolved the name of a member declaration, check to
26431 see if the declaration is accessible. When the name resolves to
26432 set of overloaded functions, accessibility is checked when
26433 overload resolution is done.
26435 During an explicit instantiation, access is not checked at all,
26436 as per [temp.explicit]. */
26437 if (DECL_P (decl))
26438 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26440 maybe_record_typedef_use (decl);
26442 return cp_expr (decl, name_location);
26445 /* Like cp_parser_lookup_name, but for use in the typical case where
26446 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26447 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26449 static tree
26450 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26452 return cp_parser_lookup_name (parser, name,
26453 none_type,
26454 /*is_template=*/false,
26455 /*is_namespace=*/false,
26456 /*check_dependency=*/true,
26457 /*ambiguous_decls=*/NULL,
26458 location);
26461 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26462 the current context, return the TYPE_DECL. If TAG_NAME_P is
26463 true, the DECL indicates the class being defined in a class-head,
26464 or declared in an elaborated-type-specifier.
26466 Otherwise, return DECL. */
26468 static tree
26469 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26471 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26472 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26474 struct A {
26475 template <typename T> struct B;
26478 template <typename T> struct A::B {};
26480 Similarly, in an elaborated-type-specifier:
26482 namespace N { struct X{}; }
26484 struct A {
26485 template <typename T> friend struct N::X;
26488 However, if the DECL refers to a class type, and we are in
26489 the scope of the class, then the name lookup automatically
26490 finds the TYPE_DECL created by build_self_reference rather
26491 than a TEMPLATE_DECL. For example, in:
26493 template <class T> struct S {
26494 S s;
26497 there is no need to handle such case. */
26499 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26500 return DECL_TEMPLATE_RESULT (decl);
26502 return decl;
26505 /* If too many, or too few, template-parameter lists apply to the
26506 declarator, issue an error message. Returns TRUE if all went well,
26507 and FALSE otherwise. */
26509 static bool
26510 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26511 cp_declarator *declarator,
26512 location_t declarator_location)
26514 switch (declarator->kind)
26516 case cdk_id:
26518 unsigned num_templates = 0;
26519 tree scope = declarator->u.id.qualifying_scope;
26520 bool template_id_p = false;
26522 if (scope)
26523 num_templates = num_template_headers_for_class (scope);
26524 else if (TREE_CODE (declarator->u.id.unqualified_name)
26525 == TEMPLATE_ID_EXPR)
26527 /* If the DECLARATOR has the form `X<y>' then it uses one
26528 additional level of template parameters. */
26529 ++num_templates;
26530 template_id_p = true;
26533 return cp_parser_check_template_parameters
26534 (parser, num_templates, template_id_p, declarator_location,
26535 declarator);
26538 case cdk_function:
26539 case cdk_array:
26540 case cdk_pointer:
26541 case cdk_reference:
26542 case cdk_ptrmem:
26543 return (cp_parser_check_declarator_template_parameters
26544 (parser, declarator->declarator, declarator_location));
26546 case cdk_decomp:
26547 case cdk_error:
26548 return true;
26550 default:
26551 gcc_unreachable ();
26553 return false;
26556 /* NUM_TEMPLATES were used in the current declaration. If that is
26557 invalid, return FALSE and issue an error messages. Otherwise,
26558 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26559 declarator and we can print more accurate diagnostics. */
26561 static bool
26562 cp_parser_check_template_parameters (cp_parser* parser,
26563 unsigned num_templates,
26564 bool template_id_p,
26565 location_t location,
26566 cp_declarator *declarator)
26568 /* If there are the same number of template classes and parameter
26569 lists, that's OK. */
26570 if (parser->num_template_parameter_lists == num_templates)
26571 return true;
26572 /* If there are more, but only one more, and the name ends in an identifier,
26573 then we are declaring a primary template. That's OK too. */
26574 if (!template_id_p
26575 && parser->num_template_parameter_lists == num_templates + 1)
26576 return true;
26577 /* If there are more template classes than parameter lists, we have
26578 something like:
26580 template <class T> void S<T>::R<T>::f (); */
26581 if (parser->num_template_parameter_lists < num_templates)
26583 if (declarator && !current_function_decl)
26584 error_at (location, "specializing member %<%T::%E%> "
26585 "requires %<template<>%> syntax",
26586 declarator->u.id.qualifying_scope,
26587 declarator->u.id.unqualified_name);
26588 else if (declarator)
26589 error_at (location, "invalid declaration of %<%T::%E%>",
26590 declarator->u.id.qualifying_scope,
26591 declarator->u.id.unqualified_name);
26592 else
26593 error_at (location, "too few template-parameter-lists");
26594 return false;
26596 /* Otherwise, there are too many template parameter lists. We have
26597 something like:
26599 template <class T> template <class U> void S::f(); */
26600 error_at (location, "too many template-parameter-lists");
26601 return false;
26604 /* Parse an optional `::' token indicating that the following name is
26605 from the global namespace. If so, PARSER->SCOPE is set to the
26606 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26607 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26608 Returns the new value of PARSER->SCOPE, if the `::' token is
26609 present, and NULL_TREE otherwise. */
26611 static tree
26612 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26614 cp_token *token;
26616 /* Peek at the next token. */
26617 token = cp_lexer_peek_token (parser->lexer);
26618 /* If we're looking at a `::' token then we're starting from the
26619 global namespace, not our current location. */
26620 if (token->type == CPP_SCOPE)
26622 /* Consume the `::' token. */
26623 cp_lexer_consume_token (parser->lexer);
26624 /* Set the SCOPE so that we know where to start the lookup. */
26625 parser->scope = global_namespace;
26626 parser->qualifying_scope = global_namespace;
26627 parser->object_scope = NULL_TREE;
26629 return parser->scope;
26631 else if (!current_scope_valid_p)
26633 parser->scope = NULL_TREE;
26634 parser->qualifying_scope = NULL_TREE;
26635 parser->object_scope = NULL_TREE;
26638 return NULL_TREE;
26641 /* Returns TRUE if the upcoming token sequence is the start of a
26642 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26643 declarator is preceded by the `friend' specifier. */
26645 static bool
26646 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26648 bool constructor_p;
26649 bool outside_class_specifier_p;
26650 tree nested_name_specifier;
26651 cp_token *next_token;
26653 /* The common case is that this is not a constructor declarator, so
26654 try to avoid doing lots of work if at all possible. It's not
26655 valid declare a constructor at function scope. */
26656 if (parser->in_function_body)
26657 return false;
26658 /* And only certain tokens can begin a constructor declarator. */
26659 next_token = cp_lexer_peek_token (parser->lexer);
26660 if (next_token->type != CPP_NAME
26661 && next_token->type != CPP_SCOPE
26662 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26663 && next_token->type != CPP_TEMPLATE_ID)
26664 return false;
26666 /* Parse tentatively; we are going to roll back all of the tokens
26667 consumed here. */
26668 cp_parser_parse_tentatively (parser);
26669 /* Assume that we are looking at a constructor declarator. */
26670 constructor_p = true;
26672 /* Look for the optional `::' operator. */
26673 cp_parser_global_scope_opt (parser,
26674 /*current_scope_valid_p=*/false);
26675 /* Look for the nested-name-specifier. */
26676 nested_name_specifier
26677 = (cp_parser_nested_name_specifier_opt (parser,
26678 /*typename_keyword_p=*/false,
26679 /*check_dependency_p=*/false,
26680 /*type_p=*/false,
26681 /*is_declaration=*/false));
26683 outside_class_specifier_p = (!at_class_scope_p ()
26684 || !TYPE_BEING_DEFINED (current_class_type)
26685 || friend_p);
26687 /* Outside of a class-specifier, there must be a
26688 nested-name-specifier. Except in C++17 mode, where we
26689 might be declaring a guiding declaration. */
26690 if (!nested_name_specifier && outside_class_specifier_p
26691 && cxx_dialect < cxx17)
26692 constructor_p = false;
26693 else if (nested_name_specifier == error_mark_node)
26694 constructor_p = false;
26696 /* If we have a class scope, this is easy; DR 147 says that S::S always
26697 names the constructor, and no other qualified name could. */
26698 if (constructor_p && nested_name_specifier
26699 && CLASS_TYPE_P (nested_name_specifier))
26701 tree id = cp_parser_unqualified_id (parser,
26702 /*template_keyword_p=*/false,
26703 /*check_dependency_p=*/false,
26704 /*declarator_p=*/true,
26705 /*optional_p=*/false);
26706 if (is_overloaded_fn (id))
26707 id = DECL_NAME (get_first_fn (id));
26708 if (!constructor_name_p (id, nested_name_specifier))
26709 constructor_p = false;
26711 /* If we still think that this might be a constructor-declarator,
26712 look for a class-name. */
26713 else if (constructor_p)
26715 /* If we have:
26717 template <typename T> struct S {
26718 S();
26721 we must recognize that the nested `S' names a class. */
26722 if (cxx_dialect >= cxx17)
26723 cp_parser_parse_tentatively (parser);
26725 tree type_decl;
26726 type_decl = cp_parser_class_name (parser,
26727 /*typename_keyword_p=*/false,
26728 /*template_keyword_p=*/false,
26729 none_type,
26730 /*check_dependency_p=*/false,
26731 /*class_head_p=*/false,
26732 /*is_declaration=*/false);
26734 if (cxx_dialect >= cxx17
26735 && !cp_parser_parse_definitely (parser))
26737 type_decl = NULL_TREE;
26738 tree tmpl = cp_parser_template_name (parser,
26739 /*template_keyword*/false,
26740 /*check_dependency_p*/false,
26741 /*is_declaration*/false,
26742 none_type,
26743 /*is_identifier*/NULL);
26744 if (DECL_CLASS_TEMPLATE_P (tmpl)
26745 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26746 /* It's a deduction guide, return true. */;
26747 else
26748 cp_parser_simulate_error (parser);
26751 /* If there was no class-name, then this is not a constructor.
26752 Otherwise, if we are in a class-specifier and we aren't
26753 handling a friend declaration, check that its type matches
26754 current_class_type (c++/38313). Note: error_mark_node
26755 is left alone for error recovery purposes. */
26756 constructor_p = (!cp_parser_error_occurred (parser)
26757 && (outside_class_specifier_p
26758 || type_decl == NULL_TREE
26759 || type_decl == error_mark_node
26760 || same_type_p (current_class_type,
26761 TREE_TYPE (type_decl))));
26763 /* If we're still considering a constructor, we have to see a `(',
26764 to begin the parameter-declaration-clause, followed by either a
26765 `)', an `...', or a decl-specifier. We need to check for a
26766 type-specifier to avoid being fooled into thinking that:
26768 S (f) (int);
26770 is a constructor. (It is actually a function named `f' that
26771 takes one parameter (of type `int') and returns a value of type
26772 `S'. */
26773 if (constructor_p
26774 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26775 constructor_p = false;
26777 if (constructor_p
26778 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26779 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26780 /* A parameter declaration begins with a decl-specifier,
26781 which is either the "attribute" keyword, a storage class
26782 specifier, or (usually) a type-specifier. */
26783 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26785 tree type;
26786 tree pushed_scope = NULL_TREE;
26787 unsigned saved_num_template_parameter_lists;
26789 /* Names appearing in the type-specifier should be looked up
26790 in the scope of the class. */
26791 if (current_class_type)
26792 type = NULL_TREE;
26793 else if (type_decl)
26795 type = TREE_TYPE (type_decl);
26796 if (TREE_CODE (type) == TYPENAME_TYPE)
26798 type = resolve_typename_type (type,
26799 /*only_current_p=*/false);
26800 if (TREE_CODE (type) == TYPENAME_TYPE)
26802 cp_parser_abort_tentative_parse (parser);
26803 return false;
26806 pushed_scope = push_scope (type);
26809 /* Inside the constructor parameter list, surrounding
26810 template-parameter-lists do not apply. */
26811 saved_num_template_parameter_lists
26812 = parser->num_template_parameter_lists;
26813 parser->num_template_parameter_lists = 0;
26815 /* Look for the type-specifier. */
26816 cp_parser_type_specifier (parser,
26817 CP_PARSER_FLAGS_NONE,
26818 /*decl_specs=*/NULL,
26819 /*is_declarator=*/true,
26820 /*declares_class_or_enum=*/NULL,
26821 /*is_cv_qualifier=*/NULL);
26823 parser->num_template_parameter_lists
26824 = saved_num_template_parameter_lists;
26826 /* Leave the scope of the class. */
26827 if (pushed_scope)
26828 pop_scope (pushed_scope);
26830 constructor_p = !cp_parser_error_occurred (parser);
26834 /* We did not really want to consume any tokens. */
26835 cp_parser_abort_tentative_parse (parser);
26837 return constructor_p;
26840 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26841 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26842 they must be performed once we are in the scope of the function.
26844 Returns the function defined. */
26846 static tree
26847 cp_parser_function_definition_from_specifiers_and_declarator
26848 (cp_parser* parser,
26849 cp_decl_specifier_seq *decl_specifiers,
26850 tree attributes,
26851 const cp_declarator *declarator)
26853 tree fn;
26854 bool success_p;
26856 /* Begin the function-definition. */
26857 success_p = start_function (decl_specifiers, declarator, attributes);
26859 /* The things we're about to see are not directly qualified by any
26860 template headers we've seen thus far. */
26861 reset_specialization ();
26863 /* If there were names looked up in the decl-specifier-seq that we
26864 did not check, check them now. We must wait until we are in the
26865 scope of the function to perform the checks, since the function
26866 might be a friend. */
26867 perform_deferred_access_checks (tf_warning_or_error);
26869 if (success_p)
26871 cp_finalize_omp_declare_simd (parser, current_function_decl);
26872 parser->omp_declare_simd = NULL;
26873 cp_finalize_oacc_routine (parser, current_function_decl, true);
26874 parser->oacc_routine = NULL;
26877 if (!success_p)
26879 /* Skip the entire function. */
26880 cp_parser_skip_to_end_of_block_or_statement (parser);
26881 fn = error_mark_node;
26883 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26885 /* Seen already, skip it. An error message has already been output. */
26886 cp_parser_skip_to_end_of_block_or_statement (parser);
26887 fn = current_function_decl;
26888 current_function_decl = NULL_TREE;
26889 /* If this is a function from a class, pop the nested class. */
26890 if (current_class_name)
26891 pop_nested_class ();
26893 else
26895 timevar_id_t tv;
26896 if (DECL_DECLARED_INLINE_P (current_function_decl))
26897 tv = TV_PARSE_INLINE;
26898 else
26899 tv = TV_PARSE_FUNC;
26900 timevar_push (tv);
26901 fn = cp_parser_function_definition_after_declarator (parser,
26902 /*inline_p=*/false);
26903 timevar_pop (tv);
26906 return fn;
26909 /* Parse the part of a function-definition that follows the
26910 declarator. INLINE_P is TRUE iff this function is an inline
26911 function defined within a class-specifier.
26913 Returns the function defined. */
26915 static tree
26916 cp_parser_function_definition_after_declarator (cp_parser* parser,
26917 bool inline_p)
26919 tree fn;
26920 bool saved_in_unbraced_linkage_specification_p;
26921 bool saved_in_function_body;
26922 unsigned saved_num_template_parameter_lists;
26923 cp_token *token;
26924 bool fully_implicit_function_template_p
26925 = parser->fully_implicit_function_template_p;
26926 parser->fully_implicit_function_template_p = false;
26927 tree implicit_template_parms
26928 = parser->implicit_template_parms;
26929 parser->implicit_template_parms = 0;
26930 cp_binding_level* implicit_template_scope
26931 = parser->implicit_template_scope;
26932 parser->implicit_template_scope = 0;
26934 saved_in_function_body = parser->in_function_body;
26935 parser->in_function_body = true;
26936 /* If the next token is `return', then the code may be trying to
26937 make use of the "named return value" extension that G++ used to
26938 support. */
26939 token = cp_lexer_peek_token (parser->lexer);
26940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26942 /* Consume the `return' keyword. */
26943 cp_lexer_consume_token (parser->lexer);
26944 /* Look for the identifier that indicates what value is to be
26945 returned. */
26946 cp_parser_identifier (parser);
26947 /* Issue an error message. */
26948 error_at (token->location,
26949 "named return values are no longer supported");
26950 /* Skip tokens until we reach the start of the function body. */
26951 while (true)
26953 cp_token *token = cp_lexer_peek_token (parser->lexer);
26954 if (token->type == CPP_OPEN_BRACE
26955 || token->type == CPP_EOF
26956 || token->type == CPP_PRAGMA_EOL)
26957 break;
26958 cp_lexer_consume_token (parser->lexer);
26961 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26962 anything declared inside `f'. */
26963 saved_in_unbraced_linkage_specification_p
26964 = parser->in_unbraced_linkage_specification_p;
26965 parser->in_unbraced_linkage_specification_p = false;
26966 /* Inside the function, surrounding template-parameter-lists do not
26967 apply. */
26968 saved_num_template_parameter_lists
26969 = parser->num_template_parameter_lists;
26970 parser->num_template_parameter_lists = 0;
26972 /* If the next token is `try', `__transaction_atomic', or
26973 `__transaction_relaxed`, then we are looking at either function-try-block
26974 or function-transaction-block. Note that all of these include the
26975 function-body. */
26976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26977 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26978 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26979 RID_TRANSACTION_RELAXED))
26980 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26981 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26982 cp_parser_function_try_block (parser);
26983 else
26984 cp_parser_ctor_initializer_opt_and_function_body
26985 (parser, /*in_function_try_block=*/false);
26987 /* Finish the function. */
26988 fn = finish_function (inline_p);
26989 /* Generate code for it, if necessary. */
26990 expand_or_defer_fn (fn);
26991 /* Restore the saved values. */
26992 parser->in_unbraced_linkage_specification_p
26993 = saved_in_unbraced_linkage_specification_p;
26994 parser->num_template_parameter_lists
26995 = saved_num_template_parameter_lists;
26996 parser->in_function_body = saved_in_function_body;
26998 parser->fully_implicit_function_template_p
26999 = fully_implicit_function_template_p;
27000 parser->implicit_template_parms
27001 = implicit_template_parms;
27002 parser->implicit_template_scope
27003 = implicit_template_scope;
27005 if (parser->fully_implicit_function_template_p)
27006 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27008 return fn;
27011 /* Parse a template-declaration body (following argument list). */
27013 static void
27014 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27015 tree parameter_list,
27016 bool member_p)
27018 tree decl = NULL_TREE;
27019 bool friend_p = false;
27021 /* We just processed one more parameter list. */
27022 ++parser->num_template_parameter_lists;
27024 /* Get the deferred access checks from the parameter list. These
27025 will be checked once we know what is being declared, as for a
27026 member template the checks must be performed in the scope of the
27027 class containing the member. */
27028 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27030 /* Tentatively parse for a new template parameter list, which can either be
27031 the template keyword or a template introduction. */
27032 if (cp_parser_template_declaration_after_export (parser, member_p))
27033 /* OK */;
27034 else if (cxx_dialect >= cxx11
27035 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27036 decl = cp_parser_alias_declaration (parser);
27037 else
27039 /* There are no access checks when parsing a template, as we do not
27040 know if a specialization will be a friend. */
27041 push_deferring_access_checks (dk_no_check);
27042 cp_token *token = cp_lexer_peek_token (parser->lexer);
27043 decl = cp_parser_single_declaration (parser,
27044 checks,
27045 member_p,
27046 /*explicit_specialization_p=*/false,
27047 &friend_p);
27048 pop_deferring_access_checks ();
27050 /* If this is a member template declaration, let the front
27051 end know. */
27052 if (member_p && !friend_p && decl)
27054 if (TREE_CODE (decl) == TYPE_DECL)
27055 cp_parser_check_access_in_redeclaration (decl, token->location);
27057 decl = finish_member_template_decl (decl);
27059 else if (friend_p && decl
27060 && DECL_DECLARES_TYPE_P (decl))
27061 make_friend_class (current_class_type, TREE_TYPE (decl),
27062 /*complain=*/true);
27064 /* We are done with the current parameter list. */
27065 --parser->num_template_parameter_lists;
27067 pop_deferring_access_checks ();
27069 /* Finish up. */
27070 finish_template_decl (parameter_list);
27072 /* Check the template arguments for a literal operator template. */
27073 if (decl
27074 && DECL_DECLARES_FUNCTION_P (decl)
27075 && UDLIT_OPER_P (DECL_NAME (decl)))
27077 bool ok = true;
27078 if (parameter_list == NULL_TREE)
27079 ok = false;
27080 else
27082 int num_parms = TREE_VEC_LENGTH (parameter_list);
27083 if (num_parms == 1)
27085 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27086 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27087 if (TREE_TYPE (parm) != char_type_node
27088 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27089 ok = false;
27091 else if (num_parms == 2 && cxx_dialect >= cxx14)
27093 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27094 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27095 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27096 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27097 if (parm == error_mark_node
27098 || TREE_TYPE (parm) != TREE_TYPE (type)
27099 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27100 ok = false;
27102 else
27103 ok = false;
27105 if (!ok)
27107 if (cxx_dialect >= cxx14)
27108 error ("literal operator template %qD has invalid parameter list."
27109 " Expected non-type template argument pack <char...>"
27110 " or <typename CharT, CharT...>",
27111 decl);
27112 else
27113 error ("literal operator template %qD has invalid parameter list."
27114 " Expected non-type template argument pack <char...>",
27115 decl);
27119 /* Register member declarations. */
27120 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27121 finish_member_declaration (decl);
27122 /* If DECL is a function template, we must return to parse it later.
27123 (Even though there is no definition, there might be default
27124 arguments that need handling.) */
27125 if (member_p && decl
27126 && DECL_DECLARES_FUNCTION_P (decl))
27127 vec_safe_push (unparsed_funs_with_definitions, decl);
27130 /* Parse a template introduction header for a template-declaration. Returns
27131 false if tentative parse fails. */
27133 static bool
27134 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27136 cp_parser_parse_tentatively (parser);
27138 tree saved_scope = parser->scope;
27139 tree saved_object_scope = parser->object_scope;
27140 tree saved_qualifying_scope = parser->qualifying_scope;
27142 /* Look for the optional `::' operator. */
27143 cp_parser_global_scope_opt (parser,
27144 /*current_scope_valid_p=*/false);
27145 /* Look for the nested-name-specifier. */
27146 cp_parser_nested_name_specifier_opt (parser,
27147 /*typename_keyword_p=*/false,
27148 /*check_dependency_p=*/true,
27149 /*type_p=*/false,
27150 /*is_declaration=*/false);
27152 cp_token *token = cp_lexer_peek_token (parser->lexer);
27153 tree concept_name = cp_parser_identifier (parser);
27155 /* Look up the concept for which we will be matching
27156 template parameters. */
27157 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27158 token->location);
27159 parser->scope = saved_scope;
27160 parser->object_scope = saved_object_scope;
27161 parser->qualifying_scope = saved_qualifying_scope;
27163 if (concept_name == error_mark_node)
27164 cp_parser_simulate_error (parser);
27166 /* Look for opening brace for introduction. */
27167 matching_braces braces;
27168 braces.require_open (parser);
27170 if (!cp_parser_parse_definitely (parser))
27171 return false;
27173 push_deferring_access_checks (dk_deferred);
27175 /* Build vector of placeholder parameters and grab
27176 matching identifiers. */
27177 tree introduction_list = cp_parser_introduction_list (parser);
27179 /* The introduction-list shall not be empty. */
27180 int nargs = TREE_VEC_LENGTH (introduction_list);
27181 if (nargs == 0)
27183 error ("empty introduction-list");
27184 return true;
27187 /* Look for closing brace for introduction. */
27188 if (!braces.require_close (parser))
27189 return true;
27191 if (tmpl_decl == error_mark_node)
27193 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27194 token->location);
27195 return true;
27198 /* Build and associate the constraint. */
27199 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27200 if (parms && parms != error_mark_node)
27202 cp_parser_template_declaration_after_parameters (parser, parms,
27203 member_p);
27204 return true;
27207 error_at (token->location, "no matching concept for template-introduction");
27208 return true;
27211 /* Parse a normal template-declaration following the template keyword. */
27213 static void
27214 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27216 tree parameter_list;
27217 bool need_lang_pop;
27218 location_t location = input_location;
27220 /* Look for the `<' token. */
27221 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27222 return;
27223 if (at_class_scope_p () && current_function_decl)
27225 /* 14.5.2.2 [temp.mem]
27227 A local class shall not have member templates. */
27228 error_at (location,
27229 "invalid declaration of member template in local class");
27230 cp_parser_skip_to_end_of_block_or_statement (parser);
27231 return;
27233 /* [temp]
27235 A template ... shall not have C linkage. */
27236 if (current_lang_name == lang_name_c)
27238 error_at (location, "template with C linkage");
27239 maybe_show_extern_c_location ();
27240 /* Give it C++ linkage to avoid confusing other parts of the
27241 front end. */
27242 push_lang_context (lang_name_cplusplus);
27243 need_lang_pop = true;
27245 else
27246 need_lang_pop = false;
27248 /* We cannot perform access checks on the template parameter
27249 declarations until we know what is being declared, just as we
27250 cannot check the decl-specifier list. */
27251 push_deferring_access_checks (dk_deferred);
27253 /* If the next token is `>', then we have an invalid
27254 specialization. Rather than complain about an invalid template
27255 parameter, issue an error message here. */
27256 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27258 cp_parser_error (parser, "invalid explicit specialization");
27259 begin_specialization ();
27260 parameter_list = NULL_TREE;
27262 else
27264 /* Parse the template parameters. */
27265 parameter_list = cp_parser_template_parameter_list (parser);
27268 /* Look for the `>'. */
27269 cp_parser_skip_to_end_of_template_parameter_list (parser);
27271 /* Manage template requirements */
27272 if (flag_concepts)
27274 tree reqs = get_shorthand_constraints (current_template_parms);
27275 if (tree r = cp_parser_requires_clause_opt (parser))
27276 reqs = conjoin_constraints (reqs, normalize_expression (r));
27277 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27280 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27281 member_p);
27283 /* For the erroneous case of a template with C linkage, we pushed an
27284 implicit C++ linkage scope; exit that scope now. */
27285 if (need_lang_pop)
27286 pop_lang_context ();
27289 /* Parse a template-declaration, assuming that the `export' (and
27290 `extern') keywords, if present, has already been scanned. MEMBER_P
27291 is as for cp_parser_template_declaration. */
27293 static bool
27294 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27298 cp_lexer_consume_token (parser->lexer);
27299 cp_parser_explicit_template_declaration (parser, member_p);
27300 return true;
27302 else if (flag_concepts)
27303 return cp_parser_template_introduction (parser, member_p);
27305 return false;
27308 /* Perform the deferred access checks from a template-parameter-list.
27309 CHECKS is a TREE_LIST of access checks, as returned by
27310 get_deferred_access_checks. */
27312 static void
27313 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27315 ++processing_template_parmlist;
27316 perform_access_checks (checks, tf_warning_or_error);
27317 --processing_template_parmlist;
27320 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27321 `function-definition' sequence that follows a template header.
27322 If MEMBER_P is true, this declaration appears in a class scope.
27324 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27325 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27327 static tree
27328 cp_parser_single_declaration (cp_parser* parser,
27329 vec<deferred_access_check, va_gc> *checks,
27330 bool member_p,
27331 bool explicit_specialization_p,
27332 bool* friend_p)
27334 int declares_class_or_enum;
27335 tree decl = NULL_TREE;
27336 cp_decl_specifier_seq decl_specifiers;
27337 bool function_definition_p = false;
27338 cp_token *decl_spec_token_start;
27340 /* This function is only used when processing a template
27341 declaration. */
27342 gcc_assert (innermost_scope_kind () == sk_template_parms
27343 || innermost_scope_kind () == sk_template_spec);
27345 /* Defer access checks until we know what is being declared. */
27346 push_deferring_access_checks (dk_deferred);
27348 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27349 alternative. */
27350 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27351 cp_parser_decl_specifier_seq (parser,
27352 CP_PARSER_FLAGS_OPTIONAL,
27353 &decl_specifiers,
27354 &declares_class_or_enum);
27355 if (friend_p)
27356 *friend_p = cp_parser_friend_p (&decl_specifiers);
27358 /* There are no template typedefs. */
27359 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27361 error_at (decl_spec_token_start->location,
27362 "template declaration of %<typedef%>");
27363 decl = error_mark_node;
27366 /* Gather up the access checks that occurred the
27367 decl-specifier-seq. */
27368 stop_deferring_access_checks ();
27370 /* Check for the declaration of a template class. */
27371 if (declares_class_or_enum)
27373 if (cp_parser_declares_only_class_p (parser)
27374 || (declares_class_or_enum & 2))
27376 // If this is a declaration, but not a definition, associate
27377 // any constraints with the type declaration. Constraints
27378 // are associated with definitions in cp_parser_class_specifier.
27379 if (declares_class_or_enum == 1)
27380 associate_classtype_constraints (decl_specifiers.type);
27382 decl = shadow_tag (&decl_specifiers);
27384 /* In this case:
27386 struct C {
27387 friend template <typename T> struct A<T>::B;
27390 A<T>::B will be represented by a TYPENAME_TYPE, and
27391 therefore not recognized by shadow_tag. */
27392 if (friend_p && *friend_p
27393 && !decl
27394 && decl_specifiers.type
27395 && TYPE_P (decl_specifiers.type))
27396 decl = decl_specifiers.type;
27398 if (decl && decl != error_mark_node)
27399 decl = TYPE_NAME (decl);
27400 else
27401 decl = error_mark_node;
27403 /* Perform access checks for template parameters. */
27404 cp_parser_perform_template_parameter_access_checks (checks);
27406 /* Give a helpful diagnostic for
27407 template <class T> struct A { } a;
27408 if we aren't already recovering from an error. */
27409 if (!cp_parser_declares_only_class_p (parser)
27410 && !seen_error ())
27412 error_at (cp_lexer_peek_token (parser->lexer)->location,
27413 "a class template declaration must not declare "
27414 "anything else");
27415 cp_parser_skip_to_end_of_block_or_statement (parser);
27416 goto out;
27421 /* Complain about missing 'typename' or other invalid type names. */
27422 if (!decl_specifiers.any_type_specifiers_p
27423 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27425 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27426 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27427 the rest of this declaration. */
27428 decl = error_mark_node;
27429 goto out;
27432 /* If it's not a template class, try for a template function. If
27433 the next token is a `;', then this declaration does not declare
27434 anything. But, if there were errors in the decl-specifiers, then
27435 the error might well have come from an attempted class-specifier.
27436 In that case, there's no need to warn about a missing declarator. */
27437 if (!decl
27438 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27439 || decl_specifiers.type != error_mark_node))
27441 decl = cp_parser_init_declarator (parser,
27442 &decl_specifiers,
27443 checks,
27444 /*function_definition_allowed_p=*/true,
27445 member_p,
27446 declares_class_or_enum,
27447 &function_definition_p,
27448 NULL, NULL, NULL);
27450 /* 7.1.1-1 [dcl.stc]
27452 A storage-class-specifier shall not be specified in an explicit
27453 specialization... */
27454 if (decl
27455 && explicit_specialization_p
27456 && decl_specifiers.storage_class != sc_none)
27458 error_at (decl_spec_token_start->location,
27459 "explicit template specialization cannot have a storage class");
27460 decl = error_mark_node;
27463 if (decl && VAR_P (decl))
27464 check_template_variable (decl);
27467 /* Look for a trailing `;' after the declaration. */
27468 if (!function_definition_p
27469 && (decl == error_mark_node
27470 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27471 cp_parser_skip_to_end_of_block_or_statement (parser);
27473 out:
27474 pop_deferring_access_checks ();
27476 /* Clear any current qualification; whatever comes next is the start
27477 of something new. */
27478 parser->scope = NULL_TREE;
27479 parser->qualifying_scope = NULL_TREE;
27480 parser->object_scope = NULL_TREE;
27482 return decl;
27485 /* Parse a cast-expression that is not the operand of a unary "&". */
27487 static cp_expr
27488 cp_parser_simple_cast_expression (cp_parser *parser)
27490 return cp_parser_cast_expression (parser, /*address_p=*/false,
27491 /*cast_p=*/false, /*decltype*/false, NULL);
27494 /* Parse a functional cast to TYPE. Returns an expression
27495 representing the cast. */
27497 static cp_expr
27498 cp_parser_functional_cast (cp_parser* parser, tree type)
27500 vec<tree, va_gc> *vec;
27501 tree expression_list;
27502 cp_expr cast;
27503 bool nonconst_p;
27505 location_t start_loc = input_location;
27507 if (!type)
27508 type = error_mark_node;
27510 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27512 cp_lexer_set_source_position (parser->lexer);
27513 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27514 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27515 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27516 if (TREE_CODE (type) == TYPE_DECL)
27517 type = TREE_TYPE (type);
27519 cast = finish_compound_literal (type, expression_list,
27520 tf_warning_or_error, fcl_functional);
27521 /* Create a location of the form:
27522 type_name{i, f}
27523 ^~~~~~~~~~~~~~~
27524 with caret == start at the start of the type name,
27525 finishing at the closing brace. */
27526 location_t finish_loc
27527 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27528 location_t combined_loc = make_location (start_loc, start_loc,
27529 finish_loc);
27530 cast.set_location (combined_loc);
27531 return cast;
27535 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27536 /*cast_p=*/true,
27537 /*allow_expansion_p=*/true,
27538 /*non_constant_p=*/NULL);
27539 if (vec == NULL)
27540 expression_list = error_mark_node;
27541 else
27543 expression_list = build_tree_list_vec (vec);
27544 release_tree_vector (vec);
27547 cast = build_functional_cast (type, expression_list,
27548 tf_warning_or_error);
27549 /* [expr.const]/1: In an integral constant expression "only type
27550 conversions to integral or enumeration type can be used". */
27551 if (TREE_CODE (type) == TYPE_DECL)
27552 type = TREE_TYPE (type);
27553 if (cast != error_mark_node
27554 && !cast_valid_in_integral_constant_expression_p (type)
27555 && cp_parser_non_integral_constant_expression (parser,
27556 NIC_CONSTRUCTOR))
27557 return error_mark_node;
27559 /* Create a location of the form:
27560 float(i)
27561 ^~~~~~~~
27562 with caret == start at the start of the type name,
27563 finishing at the closing paren. */
27564 location_t finish_loc
27565 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27566 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27567 cast.set_location (combined_loc);
27568 return cast;
27571 /* Save the tokens that make up the body of a member function defined
27572 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27573 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27574 specifiers applied to the declaration. Returns the FUNCTION_DECL
27575 for the member function. */
27577 static tree
27578 cp_parser_save_member_function_body (cp_parser* parser,
27579 cp_decl_specifier_seq *decl_specifiers,
27580 cp_declarator *declarator,
27581 tree attributes)
27583 cp_token *first;
27584 cp_token *last;
27585 tree fn;
27586 bool function_try_block = false;
27588 /* Create the FUNCTION_DECL. */
27589 fn = grokmethod (decl_specifiers, declarator, attributes);
27590 cp_finalize_omp_declare_simd (parser, fn);
27591 cp_finalize_oacc_routine (parser, fn, true);
27592 /* If something went badly wrong, bail out now. */
27593 if (fn == error_mark_node)
27595 /* If there's a function-body, skip it. */
27596 if (cp_parser_token_starts_function_definition_p
27597 (cp_lexer_peek_token (parser->lexer)))
27598 cp_parser_skip_to_end_of_block_or_statement (parser);
27599 return error_mark_node;
27602 /* Remember it, if there default args to post process. */
27603 cp_parser_save_default_args (parser, fn);
27605 /* Save away the tokens that make up the body of the
27606 function. */
27607 first = parser->lexer->next_token;
27609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27610 cp_lexer_consume_token (parser->lexer);
27611 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27612 RID_TRANSACTION_ATOMIC))
27614 cp_lexer_consume_token (parser->lexer);
27615 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27616 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27617 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27618 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27619 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27620 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27621 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27623 cp_lexer_consume_token (parser->lexer);
27624 cp_lexer_consume_token (parser->lexer);
27625 cp_lexer_consume_token (parser->lexer);
27626 cp_lexer_consume_token (parser->lexer);
27627 cp_lexer_consume_token (parser->lexer);
27629 else
27630 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27631 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27633 cp_lexer_consume_token (parser->lexer);
27634 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27635 break;
27639 /* Handle function try blocks. */
27640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27642 cp_lexer_consume_token (parser->lexer);
27643 function_try_block = true;
27645 /* We can have braced-init-list mem-initializers before the fn body. */
27646 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27648 cp_lexer_consume_token (parser->lexer);
27649 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27651 /* cache_group will stop after an un-nested { } pair, too. */
27652 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27653 break;
27655 /* variadic mem-inits have ... after the ')'. */
27656 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27657 cp_lexer_consume_token (parser->lexer);
27660 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27661 /* Handle function try blocks. */
27662 if (function_try_block)
27663 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27664 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27665 last = parser->lexer->next_token;
27667 /* Save away the inline definition; we will process it when the
27668 class is complete. */
27669 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27670 DECL_PENDING_INLINE_P (fn) = 1;
27672 /* We need to know that this was defined in the class, so that
27673 friend templates are handled correctly. */
27674 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27676 /* Add FN to the queue of functions to be parsed later. */
27677 vec_safe_push (unparsed_funs_with_definitions, fn);
27679 return fn;
27682 /* Save the tokens that make up the in-class initializer for a non-static
27683 data member. Returns a DEFAULT_ARG. */
27685 static tree
27686 cp_parser_save_nsdmi (cp_parser* parser)
27688 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27691 /* Parse a template-argument-list, as well as the trailing ">" (but
27692 not the opening "<"). See cp_parser_template_argument_list for the
27693 return value. */
27695 static tree
27696 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27698 tree arguments;
27699 tree saved_scope;
27700 tree saved_qualifying_scope;
27701 tree saved_object_scope;
27702 bool saved_greater_than_is_operator_p;
27703 int saved_unevaluated_operand;
27704 int saved_inhibit_evaluation_warnings;
27706 /* [temp.names]
27708 When parsing a template-id, the first non-nested `>' is taken as
27709 the end of the template-argument-list rather than a greater-than
27710 operator. */
27711 saved_greater_than_is_operator_p
27712 = parser->greater_than_is_operator_p;
27713 parser->greater_than_is_operator_p = false;
27714 /* Parsing the argument list may modify SCOPE, so we save it
27715 here. */
27716 saved_scope = parser->scope;
27717 saved_qualifying_scope = parser->qualifying_scope;
27718 saved_object_scope = parser->object_scope;
27719 /* We need to evaluate the template arguments, even though this
27720 template-id may be nested within a "sizeof". */
27721 saved_unevaluated_operand = cp_unevaluated_operand;
27722 cp_unevaluated_operand = 0;
27723 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27724 c_inhibit_evaluation_warnings = 0;
27725 /* Parse the template-argument-list itself. */
27726 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27727 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27728 arguments = NULL_TREE;
27729 else
27730 arguments = cp_parser_template_argument_list (parser);
27731 /* Look for the `>' that ends the template-argument-list. If we find
27732 a '>>' instead, it's probably just a typo. */
27733 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27735 if (cxx_dialect != cxx98)
27737 /* In C++0x, a `>>' in a template argument list or cast
27738 expression is considered to be two separate `>'
27739 tokens. So, change the current token to a `>', but don't
27740 consume it: it will be consumed later when the outer
27741 template argument list (or cast expression) is parsed.
27742 Note that this replacement of `>' for `>>' is necessary
27743 even if we are parsing tentatively: in the tentative
27744 case, after calling
27745 cp_parser_enclosed_template_argument_list we will always
27746 throw away all of the template arguments and the first
27747 closing `>', either because the template argument list
27748 was erroneous or because we are replacing those tokens
27749 with a CPP_TEMPLATE_ID token. The second `>' (which will
27750 not have been thrown away) is needed either to close an
27751 outer template argument list or to complete a new-style
27752 cast. */
27753 cp_token *token = cp_lexer_peek_token (parser->lexer);
27754 token->type = CPP_GREATER;
27756 else if (!saved_greater_than_is_operator_p)
27758 /* If we're in a nested template argument list, the '>>' has
27759 to be a typo for '> >'. We emit the error message, but we
27760 continue parsing and we push a '>' as next token, so that
27761 the argument list will be parsed correctly. Note that the
27762 global source location is still on the token before the
27763 '>>', so we need to say explicitly where we want it. */
27764 cp_token *token = cp_lexer_peek_token (parser->lexer);
27765 gcc_rich_location richloc (token->location);
27766 richloc.add_fixit_replace ("> >");
27767 error_at (&richloc, "%<>>%> should be %<> >%> "
27768 "within a nested template argument list");
27770 token->type = CPP_GREATER;
27772 else
27774 /* If this is not a nested template argument list, the '>>'
27775 is a typo for '>'. Emit an error message and continue.
27776 Same deal about the token location, but here we can get it
27777 right by consuming the '>>' before issuing the diagnostic. */
27778 cp_token *token = cp_lexer_consume_token (parser->lexer);
27779 error_at (token->location,
27780 "spurious %<>>%>, use %<>%> to terminate "
27781 "a template argument list");
27784 else
27785 cp_parser_skip_to_end_of_template_parameter_list (parser);
27786 /* The `>' token might be a greater-than operator again now. */
27787 parser->greater_than_is_operator_p
27788 = saved_greater_than_is_operator_p;
27789 /* Restore the SAVED_SCOPE. */
27790 parser->scope = saved_scope;
27791 parser->qualifying_scope = saved_qualifying_scope;
27792 parser->object_scope = saved_object_scope;
27793 cp_unevaluated_operand = saved_unevaluated_operand;
27794 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27796 return arguments;
27799 /* MEMBER_FUNCTION is a member function, or a friend. If default
27800 arguments, or the body of the function have not yet been parsed,
27801 parse them now. */
27803 static void
27804 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27806 timevar_push (TV_PARSE_INMETH);
27807 /* If this member is a template, get the underlying
27808 FUNCTION_DECL. */
27809 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27810 member_function = DECL_TEMPLATE_RESULT (member_function);
27812 /* There should not be any class definitions in progress at this
27813 point; the bodies of members are only parsed outside of all class
27814 definitions. */
27815 gcc_assert (parser->num_classes_being_defined == 0);
27816 /* While we're parsing the member functions we might encounter more
27817 classes. We want to handle them right away, but we don't want
27818 them getting mixed up with functions that are currently in the
27819 queue. */
27820 push_unparsed_function_queues (parser);
27822 /* Make sure that any template parameters are in scope. */
27823 maybe_begin_member_template_processing (member_function);
27825 /* If the body of the function has not yet been parsed, parse it
27826 now. */
27827 if (DECL_PENDING_INLINE_P (member_function))
27829 tree function_scope;
27830 cp_token_cache *tokens;
27832 /* The function is no longer pending; we are processing it. */
27833 tokens = DECL_PENDING_INLINE_INFO (member_function);
27834 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27835 DECL_PENDING_INLINE_P (member_function) = 0;
27837 /* If this is a local class, enter the scope of the containing
27838 function. */
27839 function_scope = current_function_decl;
27840 if (function_scope)
27841 push_function_context ();
27843 /* Push the body of the function onto the lexer stack. */
27844 cp_parser_push_lexer_for_tokens (parser, tokens);
27846 /* Let the front end know that we going to be defining this
27847 function. */
27848 start_preparsed_function (member_function, NULL_TREE,
27849 SF_PRE_PARSED | SF_INCLASS_INLINE);
27851 /* Don't do access checking if it is a templated function. */
27852 if (processing_template_decl)
27853 push_deferring_access_checks (dk_no_check);
27855 /* #pragma omp declare reduction needs special parsing. */
27856 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27858 parser->lexer->in_pragma = true;
27859 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27860 finish_function (/*inline_p=*/true);
27861 cp_check_omp_declare_reduction (member_function);
27863 else
27864 /* Now, parse the body of the function. */
27865 cp_parser_function_definition_after_declarator (parser,
27866 /*inline_p=*/true);
27868 if (processing_template_decl)
27869 pop_deferring_access_checks ();
27871 /* Leave the scope of the containing function. */
27872 if (function_scope)
27873 pop_function_context ();
27874 cp_parser_pop_lexer (parser);
27877 /* Remove any template parameters from the symbol table. */
27878 maybe_end_member_template_processing ();
27880 /* Restore the queue. */
27881 pop_unparsed_function_queues (parser);
27882 timevar_pop (TV_PARSE_INMETH);
27885 /* If DECL contains any default args, remember it on the unparsed
27886 functions queue. */
27888 static void
27889 cp_parser_save_default_args (cp_parser* parser, tree decl)
27891 tree probe;
27893 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27894 probe;
27895 probe = TREE_CHAIN (probe))
27896 if (TREE_PURPOSE (probe))
27898 cp_default_arg_entry entry = {current_class_type, decl};
27899 vec_safe_push (unparsed_funs_with_default_args, entry);
27900 break;
27904 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27905 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27906 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27907 from the parameter-type-list. */
27909 static tree
27910 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27911 tree default_arg, tree parmtype)
27913 cp_token_cache *tokens;
27914 tree parsed_arg;
27915 bool dummy;
27917 if (default_arg == error_mark_node)
27918 return error_mark_node;
27920 /* Push the saved tokens for the default argument onto the parser's
27921 lexer stack. */
27922 tokens = DEFARG_TOKENS (default_arg);
27923 cp_parser_push_lexer_for_tokens (parser, tokens);
27925 start_lambda_scope (decl);
27927 /* Parse the default argument. */
27928 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27929 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27930 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27932 finish_lambda_scope ();
27934 if (parsed_arg == error_mark_node)
27935 cp_parser_skip_to_end_of_statement (parser);
27937 if (!processing_template_decl)
27939 /* In a non-template class, check conversions now. In a template,
27940 we'll wait and instantiate these as needed. */
27941 if (TREE_CODE (decl) == PARM_DECL)
27942 parsed_arg = check_default_argument (parmtype, parsed_arg,
27943 tf_warning_or_error);
27944 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27945 parsed_arg = error_mark_node;
27946 else
27947 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27950 /* If the token stream has not been completely used up, then
27951 there was extra junk after the end of the default
27952 argument. */
27953 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27955 if (TREE_CODE (decl) == PARM_DECL)
27956 cp_parser_error (parser, "expected %<,%>");
27957 else
27958 cp_parser_error (parser, "expected %<;%>");
27961 /* Revert to the main lexer. */
27962 cp_parser_pop_lexer (parser);
27964 return parsed_arg;
27967 /* FIELD is a non-static data member with an initializer which we saved for
27968 later; parse it now. */
27970 static void
27971 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27973 tree def;
27975 maybe_begin_member_template_processing (field);
27977 push_unparsed_function_queues (parser);
27978 def = cp_parser_late_parse_one_default_arg (parser, field,
27979 DECL_INITIAL (field),
27980 NULL_TREE);
27981 pop_unparsed_function_queues (parser);
27983 maybe_end_member_template_processing ();
27985 DECL_INITIAL (field) = def;
27988 /* FN is a FUNCTION_DECL which may contains a parameter with an
27989 unparsed DEFAULT_ARG. Parse the default args now. This function
27990 assumes that the current scope is the scope in which the default
27991 argument should be processed. */
27993 static void
27994 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27996 bool saved_local_variables_forbidden_p;
27997 tree parm, parmdecl;
27999 /* While we're parsing the default args, we might (due to the
28000 statement expression extension) encounter more classes. We want
28001 to handle them right away, but we don't want them getting mixed
28002 up with default args that are currently in the queue. */
28003 push_unparsed_function_queues (parser);
28005 /* Local variable names (and the `this' keyword) may not appear
28006 in a default argument. */
28007 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28008 parser->local_variables_forbidden_p = true;
28010 push_defarg_context (fn);
28012 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28013 parmdecl = DECL_ARGUMENTS (fn);
28014 parm && parm != void_list_node;
28015 parm = TREE_CHAIN (parm),
28016 parmdecl = DECL_CHAIN (parmdecl))
28018 tree default_arg = TREE_PURPOSE (parm);
28019 tree parsed_arg;
28020 vec<tree, va_gc> *insts;
28021 tree copy;
28022 unsigned ix;
28024 if (!default_arg)
28025 continue;
28027 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28028 /* This can happen for a friend declaration for a function
28029 already declared with default arguments. */
28030 continue;
28032 parsed_arg
28033 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28034 default_arg,
28035 TREE_VALUE (parm));
28036 TREE_PURPOSE (parm) = parsed_arg;
28038 /* Update any instantiations we've already created. */
28039 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28040 vec_safe_iterate (insts, ix, &copy); ix++)
28041 TREE_PURPOSE (copy) = parsed_arg;
28044 pop_defarg_context ();
28046 /* Make sure no default arg is missing. */
28047 check_default_args (fn);
28049 /* Restore the state of local_variables_forbidden_p. */
28050 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28052 /* Restore the queue. */
28053 pop_unparsed_function_queues (parser);
28056 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28058 sizeof ... ( identifier )
28060 where the 'sizeof' token has already been consumed. */
28062 static tree
28063 cp_parser_sizeof_pack (cp_parser *parser)
28065 /* Consume the `...'. */
28066 cp_lexer_consume_token (parser->lexer);
28067 maybe_warn_variadic_templates ();
28069 matching_parens parens;
28070 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28071 if (paren)
28072 parens.consume_open (parser);
28073 else
28074 permerror (cp_lexer_peek_token (parser->lexer)->location,
28075 "%<sizeof...%> argument must be surrounded by parentheses");
28077 cp_token *token = cp_lexer_peek_token (parser->lexer);
28078 tree name = cp_parser_identifier (parser);
28079 if (name == error_mark_node)
28080 return error_mark_node;
28081 /* The name is not qualified. */
28082 parser->scope = NULL_TREE;
28083 parser->qualifying_scope = NULL_TREE;
28084 parser->object_scope = NULL_TREE;
28085 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28086 if (expr == error_mark_node)
28087 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28088 token->location);
28089 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28090 expr = TREE_TYPE (expr);
28091 else if (TREE_CODE (expr) == CONST_DECL)
28092 expr = DECL_INITIAL (expr);
28093 expr = make_pack_expansion (expr);
28094 PACK_EXPANSION_SIZEOF_P (expr) = true;
28096 if (paren)
28097 parens.require_close (parser);
28099 return expr;
28102 /* Parse the operand of `sizeof' (or a similar operator). Returns
28103 either a TYPE or an expression, depending on the form of the
28104 input. The KEYWORD indicates which kind of expression we have
28105 encountered. */
28107 static tree
28108 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28110 tree expr = NULL_TREE;
28111 const char *saved_message;
28112 char *tmp;
28113 bool saved_integral_constant_expression_p;
28114 bool saved_non_integral_constant_expression_p;
28116 /* If it's a `...', then we are computing the length of a parameter
28117 pack. */
28118 if (keyword == RID_SIZEOF
28119 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28120 return cp_parser_sizeof_pack (parser);
28122 /* Types cannot be defined in a `sizeof' expression. Save away the
28123 old message. */
28124 saved_message = parser->type_definition_forbidden_message;
28125 /* And create the new one. */
28126 tmp = concat ("types may not be defined in %<",
28127 IDENTIFIER_POINTER (ridpointers[keyword]),
28128 "%> expressions", NULL);
28129 parser->type_definition_forbidden_message = tmp;
28131 /* The restrictions on constant-expressions do not apply inside
28132 sizeof expressions. */
28133 saved_integral_constant_expression_p
28134 = parser->integral_constant_expression_p;
28135 saved_non_integral_constant_expression_p
28136 = parser->non_integral_constant_expression_p;
28137 parser->integral_constant_expression_p = false;
28139 /* Do not actually evaluate the expression. */
28140 ++cp_unevaluated_operand;
28141 ++c_inhibit_evaluation_warnings;
28142 /* If it's a `(', then we might be looking at the type-id
28143 construction. */
28144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28146 tree type = NULL_TREE;
28148 /* We can't be sure yet whether we're looking at a type-id or an
28149 expression. */
28150 cp_parser_parse_tentatively (parser);
28152 matching_parens parens;
28153 parens.consume_open (parser);
28155 /* Note: as a GNU Extension, compound literals are considered
28156 postfix-expressions as they are in C99, so they are valid
28157 arguments to sizeof. See comment in cp_parser_cast_expression
28158 for details. */
28159 if (cp_parser_compound_literal_p (parser))
28160 cp_parser_simulate_error (parser);
28161 else
28163 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28164 parser->in_type_id_in_expr_p = true;
28165 /* Look for the type-id. */
28166 type = cp_parser_type_id (parser);
28167 /* Look for the closing `)'. */
28168 parens.require_close (parser);
28169 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28172 /* If all went well, then we're done. */
28173 if (cp_parser_parse_definitely (parser))
28175 cp_decl_specifier_seq decl_specs;
28177 /* Build a trivial decl-specifier-seq. */
28178 clear_decl_specs (&decl_specs);
28179 decl_specs.type = type;
28181 /* Call grokdeclarator to figure out what type this is. */
28182 expr = grokdeclarator (NULL,
28183 &decl_specs,
28184 TYPENAME,
28185 /*initialized=*/0,
28186 /*attrlist=*/NULL);
28190 /* If the type-id production did not work out, then we must be
28191 looking at the unary-expression production. */
28192 if (!expr)
28193 expr = cp_parser_unary_expression (parser);
28195 /* Go back to evaluating expressions. */
28196 --cp_unevaluated_operand;
28197 --c_inhibit_evaluation_warnings;
28199 /* Free the message we created. */
28200 free (tmp);
28201 /* And restore the old one. */
28202 parser->type_definition_forbidden_message = saved_message;
28203 parser->integral_constant_expression_p
28204 = saved_integral_constant_expression_p;
28205 parser->non_integral_constant_expression_p
28206 = saved_non_integral_constant_expression_p;
28208 return expr;
28211 /* If the current declaration has no declarator, return true. */
28213 static bool
28214 cp_parser_declares_only_class_p (cp_parser *parser)
28216 /* If the next token is a `;' or a `,' then there is no
28217 declarator. */
28218 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28219 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28222 /* Update the DECL_SPECS to reflect the storage class indicated by
28223 KEYWORD. */
28225 static void
28226 cp_parser_set_storage_class (cp_parser *parser,
28227 cp_decl_specifier_seq *decl_specs,
28228 enum rid keyword,
28229 cp_token *token)
28231 cp_storage_class storage_class;
28233 if (parser->in_unbraced_linkage_specification_p)
28235 error_at (token->location, "invalid use of %qD in linkage specification",
28236 ridpointers[keyword]);
28237 return;
28239 else if (decl_specs->storage_class != sc_none)
28241 decl_specs->conflicting_specifiers_p = true;
28242 return;
28245 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28246 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28247 && decl_specs->gnu_thread_keyword_p)
28249 pedwarn (decl_specs->locations[ds_thread], 0,
28250 "%<__thread%> before %qD", ridpointers[keyword]);
28253 switch (keyword)
28255 case RID_AUTO:
28256 storage_class = sc_auto;
28257 break;
28258 case RID_REGISTER:
28259 storage_class = sc_register;
28260 break;
28261 case RID_STATIC:
28262 storage_class = sc_static;
28263 break;
28264 case RID_EXTERN:
28265 storage_class = sc_extern;
28266 break;
28267 case RID_MUTABLE:
28268 storage_class = sc_mutable;
28269 break;
28270 default:
28271 gcc_unreachable ();
28273 decl_specs->storage_class = storage_class;
28274 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28276 /* A storage class specifier cannot be applied alongside a typedef
28277 specifier. If there is a typedef specifier present then set
28278 conflicting_specifiers_p which will trigger an error later
28279 on in grokdeclarator. */
28280 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28281 decl_specs->conflicting_specifiers_p = true;
28284 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28285 is true, the type is a class or enum definition. */
28287 static void
28288 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28289 tree type_spec,
28290 cp_token *token,
28291 bool type_definition_p)
28293 decl_specs->any_specifiers_p = true;
28295 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28296 (with, for example, in "typedef int wchar_t;") we remember that
28297 this is what happened. In system headers, we ignore these
28298 declarations so that G++ can work with system headers that are not
28299 C++-safe. */
28300 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28301 && !type_definition_p
28302 && (type_spec == boolean_type_node
28303 || type_spec == char16_type_node
28304 || type_spec == char32_type_node
28305 || type_spec == wchar_type_node)
28306 && (decl_specs->type
28307 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28308 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28309 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28310 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28312 decl_specs->redefined_builtin_type = type_spec;
28313 set_and_check_decl_spec_loc (decl_specs,
28314 ds_redefined_builtin_type_spec,
28315 token);
28316 if (!decl_specs->type)
28318 decl_specs->type = type_spec;
28319 decl_specs->type_definition_p = false;
28320 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28323 else if (decl_specs->type)
28324 decl_specs->multiple_types_p = true;
28325 else
28327 decl_specs->type = type_spec;
28328 decl_specs->type_definition_p = type_definition_p;
28329 decl_specs->redefined_builtin_type = NULL_TREE;
28330 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28334 /* True iff TOKEN is the GNU keyword __thread. */
28336 static bool
28337 token_is__thread (cp_token *token)
28339 gcc_assert (token->keyword == RID_THREAD);
28340 return id_equal (token->u.value, "__thread");
28343 /* Set the location for a declarator specifier and check if it is
28344 duplicated.
28346 DECL_SPECS is the sequence of declarator specifiers onto which to
28347 set the location.
28349 DS is the single declarator specifier to set which location is to
28350 be set onto the existing sequence of declarators.
28352 LOCATION is the location for the declarator specifier to
28353 consider. */
28355 static void
28356 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28357 cp_decl_spec ds, cp_token *token)
28359 gcc_assert (ds < ds_last);
28361 if (decl_specs == NULL)
28362 return;
28364 source_location location = token->location;
28366 if (decl_specs->locations[ds] == 0)
28368 decl_specs->locations[ds] = location;
28369 if (ds == ds_thread)
28370 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28372 else
28374 if (ds == ds_long)
28376 if (decl_specs->locations[ds_long_long] != 0)
28377 error_at (location,
28378 "%<long long long%> is too long for GCC");
28379 else
28381 decl_specs->locations[ds_long_long] = location;
28382 pedwarn_cxx98 (location,
28383 OPT_Wlong_long,
28384 "ISO C++ 1998 does not support %<long long%>");
28387 else if (ds == ds_thread)
28389 bool gnu = token_is__thread (token);
28390 gcc_rich_location richloc (location);
28391 if (gnu != decl_specs->gnu_thread_keyword_p)
28393 richloc.add_range (decl_specs->locations[ds_thread], false);
28394 error_at (&richloc,
28395 "both %<__thread%> and %<thread_local%> specified");
28397 else
28399 richloc.add_fixit_remove ();
28400 error_at (&richloc, "duplicate %qD", token->u.value);
28403 else
28405 static const char *const decl_spec_names[] = {
28406 "signed",
28407 "unsigned",
28408 "short",
28409 "long",
28410 "const",
28411 "volatile",
28412 "restrict",
28413 "inline",
28414 "virtual",
28415 "explicit",
28416 "friend",
28417 "typedef",
28418 "using",
28419 "constexpr",
28420 "__complex"
28422 gcc_rich_location richloc (location);
28423 richloc.add_fixit_remove ();
28424 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28429 /* Return true iff the declarator specifier DS is present in the
28430 sequence of declarator specifiers DECL_SPECS. */
28432 bool
28433 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28434 cp_decl_spec ds)
28436 gcc_assert (ds < ds_last);
28438 if (decl_specs == NULL)
28439 return false;
28441 return decl_specs->locations[ds] != 0;
28444 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28445 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28447 static bool
28448 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28450 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28453 /* Issue an error message indicating that TOKEN_DESC was expected.
28454 If KEYWORD is true, it indicated this function is called by
28455 cp_parser_require_keword and the required token can only be
28456 a indicated keyword.
28458 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28459 within any error as the location of an "opening" token matching
28460 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28461 RT_CLOSE_PAREN). */
28463 static void
28464 cp_parser_required_error (cp_parser *parser,
28465 required_token token_desc,
28466 bool keyword,
28467 location_t matching_location)
28469 if (cp_parser_simulate_error (parser))
28470 return;
28472 const char *gmsgid = NULL;
28473 switch (token_desc)
28475 case RT_NEW:
28476 gmsgid = G_("expected %<new%>");
28477 break;
28478 case RT_DELETE:
28479 gmsgid = G_("expected %<delete%>");
28480 break;
28481 case RT_RETURN:
28482 gmsgid = G_("expected %<return%>");
28483 break;
28484 case RT_WHILE:
28485 gmsgid = G_("expected %<while%>");
28486 break;
28487 case RT_EXTERN:
28488 gmsgid = G_("expected %<extern%>");
28489 break;
28490 case RT_STATIC_ASSERT:
28491 gmsgid = G_("expected %<static_assert%>");
28492 break;
28493 case RT_DECLTYPE:
28494 gmsgid = G_("expected %<decltype%>");
28495 break;
28496 case RT_OPERATOR:
28497 gmsgid = G_("expected %<operator%>");
28498 break;
28499 case RT_CLASS:
28500 gmsgid = G_("expected %<class%>");
28501 break;
28502 case RT_TEMPLATE:
28503 gmsgid = G_("expected %<template%>");
28504 break;
28505 case RT_NAMESPACE:
28506 gmsgid = G_("expected %<namespace%>");
28507 break;
28508 case RT_USING:
28509 gmsgid = G_("expected %<using%>");
28510 break;
28511 case RT_ASM:
28512 gmsgid = G_("expected %<asm%>");
28513 break;
28514 case RT_TRY:
28515 gmsgid = G_("expected %<try%>");
28516 break;
28517 case RT_CATCH:
28518 gmsgid = G_("expected %<catch%>");
28519 break;
28520 case RT_THROW:
28521 gmsgid = G_("expected %<throw%>");
28522 break;
28523 case RT_LABEL:
28524 gmsgid = G_("expected %<__label__%>");
28525 break;
28526 case RT_AT_TRY:
28527 gmsgid = G_("expected %<@try%>");
28528 break;
28529 case RT_AT_SYNCHRONIZED:
28530 gmsgid = G_("expected %<@synchronized%>");
28531 break;
28532 case RT_AT_THROW:
28533 gmsgid = G_("expected %<@throw%>");
28534 break;
28535 case RT_TRANSACTION_ATOMIC:
28536 gmsgid = G_("expected %<__transaction_atomic%>");
28537 break;
28538 case RT_TRANSACTION_RELAXED:
28539 gmsgid = G_("expected %<__transaction_relaxed%>");
28540 break;
28541 default:
28542 break;
28545 if (!gmsgid && !keyword)
28547 switch (token_desc)
28549 case RT_SEMICOLON:
28550 gmsgid = G_("expected %<;%>");
28551 break;
28552 case RT_OPEN_PAREN:
28553 gmsgid = G_("expected %<(%>");
28554 break;
28555 case RT_CLOSE_BRACE:
28556 gmsgid = G_("expected %<}%>");
28557 break;
28558 case RT_OPEN_BRACE:
28559 gmsgid = G_("expected %<{%>");
28560 break;
28561 case RT_CLOSE_SQUARE:
28562 gmsgid = G_("expected %<]%>");
28563 break;
28564 case RT_OPEN_SQUARE:
28565 gmsgid = G_("expected %<[%>");
28566 break;
28567 case RT_COMMA:
28568 gmsgid = G_("expected %<,%>");
28569 break;
28570 case RT_SCOPE:
28571 gmsgid = G_("expected %<::%>");
28572 break;
28573 case RT_LESS:
28574 gmsgid = G_("expected %<<%>");
28575 break;
28576 case RT_GREATER:
28577 gmsgid = G_("expected %<>%>");
28578 break;
28579 case RT_EQ:
28580 gmsgid = G_("expected %<=%>");
28581 break;
28582 case RT_ELLIPSIS:
28583 gmsgid = G_("expected %<...%>");
28584 break;
28585 case RT_MULT:
28586 gmsgid = G_("expected %<*%>");
28587 break;
28588 case RT_COMPL:
28589 gmsgid = G_("expected %<~%>");
28590 break;
28591 case RT_COLON:
28592 gmsgid = G_("expected %<:%>");
28593 break;
28594 case RT_COLON_SCOPE:
28595 gmsgid = G_("expected %<:%> or %<::%>");
28596 break;
28597 case RT_CLOSE_PAREN:
28598 gmsgid = G_("expected %<)%>");
28599 break;
28600 case RT_COMMA_CLOSE_PAREN:
28601 gmsgid = G_("expected %<,%> or %<)%>");
28602 break;
28603 case RT_PRAGMA_EOL:
28604 gmsgid = G_("expected end of line");
28605 break;
28606 case RT_NAME:
28607 gmsgid = G_("expected identifier");
28608 break;
28609 case RT_SELECT:
28610 gmsgid = G_("expected selection-statement");
28611 break;
28612 case RT_ITERATION:
28613 gmsgid = G_("expected iteration-statement");
28614 break;
28615 case RT_JUMP:
28616 gmsgid = G_("expected jump-statement");
28617 break;
28618 case RT_CLASS_KEY:
28619 gmsgid = G_("expected class-key");
28620 break;
28621 case RT_CLASS_TYPENAME_TEMPLATE:
28622 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28623 break;
28624 default:
28625 gcc_unreachable ();
28629 if (gmsgid)
28630 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28634 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28635 issue an error message indicating that TOKEN_DESC was expected.
28637 Returns the token consumed, if the token had the appropriate type.
28638 Otherwise, returns NULL.
28640 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28641 within any error as the location of an "opening" token matching
28642 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28643 RT_CLOSE_PAREN). */
28645 static cp_token *
28646 cp_parser_require (cp_parser* parser,
28647 enum cpp_ttype type,
28648 required_token token_desc,
28649 location_t matching_location)
28651 if (cp_lexer_next_token_is (parser->lexer, type))
28652 return cp_lexer_consume_token (parser->lexer);
28653 else
28655 /* Output the MESSAGE -- unless we're parsing tentatively. */
28656 if (!cp_parser_simulate_error (parser))
28657 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28658 matching_location);
28659 return NULL;
28663 /* An error message is produced if the next token is not '>'.
28664 All further tokens are skipped until the desired token is
28665 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28667 static void
28668 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28670 /* Current level of '< ... >'. */
28671 unsigned level = 0;
28672 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28673 unsigned nesting_depth = 0;
28675 /* Are we ready, yet? If not, issue error message. */
28676 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28677 return;
28679 /* Skip tokens until the desired token is found. */
28680 while (true)
28682 /* Peek at the next token. */
28683 switch (cp_lexer_peek_token (parser->lexer)->type)
28685 case CPP_LESS:
28686 if (!nesting_depth)
28687 ++level;
28688 break;
28690 case CPP_RSHIFT:
28691 if (cxx_dialect == cxx98)
28692 /* C++0x views the `>>' operator as two `>' tokens, but
28693 C++98 does not. */
28694 break;
28695 else if (!nesting_depth && level-- == 0)
28697 /* We've hit a `>>' where the first `>' closes the
28698 template argument list, and the second `>' is
28699 spurious. Just consume the `>>' and stop; we've
28700 already produced at least one error. */
28701 cp_lexer_consume_token (parser->lexer);
28702 return;
28704 /* Fall through for C++0x, so we handle the second `>' in
28705 the `>>'. */
28706 gcc_fallthrough ();
28708 case CPP_GREATER:
28709 if (!nesting_depth && level-- == 0)
28711 /* We've reached the token we want, consume it and stop. */
28712 cp_lexer_consume_token (parser->lexer);
28713 return;
28715 break;
28717 case CPP_OPEN_PAREN:
28718 case CPP_OPEN_SQUARE:
28719 ++nesting_depth;
28720 break;
28722 case CPP_CLOSE_PAREN:
28723 case CPP_CLOSE_SQUARE:
28724 if (nesting_depth-- == 0)
28725 return;
28726 break;
28728 case CPP_EOF:
28729 case CPP_PRAGMA_EOL:
28730 case CPP_SEMICOLON:
28731 case CPP_OPEN_BRACE:
28732 case CPP_CLOSE_BRACE:
28733 /* The '>' was probably forgotten, don't look further. */
28734 return;
28736 default:
28737 break;
28740 /* Consume this token. */
28741 cp_lexer_consume_token (parser->lexer);
28745 /* If the next token is the indicated keyword, consume it. Otherwise,
28746 issue an error message indicating that TOKEN_DESC was expected.
28748 Returns the token consumed, if the token had the appropriate type.
28749 Otherwise, returns NULL. */
28751 static cp_token *
28752 cp_parser_require_keyword (cp_parser* parser,
28753 enum rid keyword,
28754 required_token token_desc)
28756 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28758 if (token && token->keyword != keyword)
28760 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28761 UNKNOWN_LOCATION);
28762 return NULL;
28765 return token;
28768 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28769 function-definition. */
28771 static bool
28772 cp_parser_token_starts_function_definition_p (cp_token* token)
28774 return (/* An ordinary function-body begins with an `{'. */
28775 token->type == CPP_OPEN_BRACE
28776 /* A ctor-initializer begins with a `:'. */
28777 || token->type == CPP_COLON
28778 /* A function-try-block begins with `try'. */
28779 || token->keyword == RID_TRY
28780 /* A function-transaction-block begins with `__transaction_atomic'
28781 or `__transaction_relaxed'. */
28782 || token->keyword == RID_TRANSACTION_ATOMIC
28783 || token->keyword == RID_TRANSACTION_RELAXED
28784 /* The named return value extension begins with `return'. */
28785 || token->keyword == RID_RETURN);
28788 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28789 definition. */
28791 static bool
28792 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28794 cp_token *token;
28796 token = cp_lexer_peek_token (parser->lexer);
28797 return (token->type == CPP_OPEN_BRACE
28798 || (token->type == CPP_COLON
28799 && !parser->colon_doesnt_start_class_def_p));
28802 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28803 C++0x) ending a template-argument. */
28805 static bool
28806 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28808 cp_token *token;
28810 token = cp_lexer_peek_token (parser->lexer);
28811 return (token->type == CPP_COMMA
28812 || token->type == CPP_GREATER
28813 || token->type == CPP_ELLIPSIS
28814 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28817 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28818 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28820 static bool
28821 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28822 size_t n)
28824 cp_token *token;
28826 token = cp_lexer_peek_nth_token (parser->lexer, n);
28827 if (token->type == CPP_LESS)
28828 return true;
28829 /* Check for the sequence `<::' in the original code. It would be lexed as
28830 `[:', where `[' is a digraph, and there is no whitespace before
28831 `:'. */
28832 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28834 cp_token *token2;
28835 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28836 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28837 return true;
28839 return false;
28842 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28843 or none_type otherwise. */
28845 static enum tag_types
28846 cp_parser_token_is_class_key (cp_token* token)
28848 switch (token->keyword)
28850 case RID_CLASS:
28851 return class_type;
28852 case RID_STRUCT:
28853 return record_type;
28854 case RID_UNION:
28855 return union_type;
28857 default:
28858 return none_type;
28862 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28863 or none_type otherwise or if the token is null. */
28865 static enum tag_types
28866 cp_parser_token_is_type_parameter_key (cp_token* token)
28868 if (!token)
28869 return none_type;
28871 switch (token->keyword)
28873 case RID_CLASS:
28874 return class_type;
28875 case RID_TYPENAME:
28876 return typename_type;
28878 default:
28879 return none_type;
28883 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28885 static void
28886 cp_parser_check_class_key (enum tag_types class_key, tree type)
28888 if (type == error_mark_node)
28889 return;
28890 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28892 if (permerror (input_location, "%qs tag used in naming %q#T",
28893 class_key == union_type ? "union"
28894 : class_key == record_type ? "struct" : "class",
28895 type))
28896 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28897 "%q#T was previously declared here", type);
28901 /* Issue an error message if DECL is redeclared with different
28902 access than its original declaration [class.access.spec/3].
28903 This applies to nested classes, nested class templates and
28904 enumerations [class.mem/1]. */
28906 static void
28907 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28909 if (!decl
28910 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28911 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28912 return;
28914 if ((TREE_PRIVATE (decl)
28915 != (current_access_specifier == access_private_node))
28916 || (TREE_PROTECTED (decl)
28917 != (current_access_specifier == access_protected_node)))
28918 error_at (location, "%qD redeclared with different access", decl);
28921 /* Look for the `template' keyword, as a syntactic disambiguator.
28922 Return TRUE iff it is present, in which case it will be
28923 consumed. */
28925 static bool
28926 cp_parser_optional_template_keyword (cp_parser *parser)
28928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28930 /* In C++98 the `template' keyword can only be used within templates;
28931 outside templates the parser can always figure out what is a
28932 template and what is not. In C++11, per the resolution of DR 468,
28933 `template' is allowed in cases where it is not strictly necessary. */
28934 if (!processing_template_decl
28935 && pedantic && cxx_dialect == cxx98)
28937 cp_token *token = cp_lexer_peek_token (parser->lexer);
28938 pedwarn (token->location, OPT_Wpedantic,
28939 "in C++98 %<template%> (as a disambiguator) is only "
28940 "allowed within templates");
28941 /* If this part of the token stream is rescanned, the same
28942 error message would be generated. So, we purge the token
28943 from the stream. */
28944 cp_lexer_purge_token (parser->lexer);
28945 return false;
28947 else
28949 /* Consume the `template' keyword. */
28950 cp_lexer_consume_token (parser->lexer);
28951 return true;
28954 return false;
28957 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28958 set PARSER->SCOPE, and perform other related actions. */
28960 static void
28961 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28963 struct tree_check *check_value;
28965 /* Get the stored value. */
28966 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28967 /* Set the scope from the stored value. */
28968 parser->scope = saved_checks_value (check_value);
28969 parser->qualifying_scope = check_value->qualifying_scope;
28970 parser->object_scope = NULL_TREE;
28973 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28974 encounter the end of a block before what we were looking for. */
28976 static bool
28977 cp_parser_cache_group (cp_parser *parser,
28978 enum cpp_ttype end,
28979 unsigned depth)
28981 while (true)
28983 cp_token *token = cp_lexer_peek_token (parser->lexer);
28985 /* Abort a parenthesized expression if we encounter a semicolon. */
28986 if ((end == CPP_CLOSE_PAREN || depth == 0)
28987 && token->type == CPP_SEMICOLON)
28988 return true;
28989 /* If we've reached the end of the file, stop. */
28990 if (token->type == CPP_EOF
28991 || (end != CPP_PRAGMA_EOL
28992 && token->type == CPP_PRAGMA_EOL))
28993 return true;
28994 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28995 /* We've hit the end of an enclosing block, so there's been some
28996 kind of syntax error. */
28997 return true;
28999 /* Consume the token. */
29000 cp_lexer_consume_token (parser->lexer);
29001 /* See if it starts a new group. */
29002 if (token->type == CPP_OPEN_BRACE)
29004 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29005 /* In theory this should probably check end == '}', but
29006 cp_parser_save_member_function_body needs it to exit
29007 after either '}' or ')' when called with ')'. */
29008 if (depth == 0)
29009 return false;
29011 else if (token->type == CPP_OPEN_PAREN)
29013 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29014 if (depth == 0 && end == CPP_CLOSE_PAREN)
29015 return false;
29017 else if (token->type == CPP_PRAGMA)
29018 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29019 else if (token->type == end)
29020 return false;
29024 /* Like above, for caching a default argument or NSDMI. Both of these are
29025 terminated by a non-nested comma, but it can be unclear whether or not a
29026 comma is nested in a template argument list unless we do more parsing.
29027 In order to handle this ambiguity, when we encounter a ',' after a '<'
29028 we try to parse what follows as a parameter-declaration-list (in the
29029 case of a default argument) or a member-declarator (in the case of an
29030 NSDMI). If that succeeds, then we stop caching. */
29032 static tree
29033 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29035 unsigned depth = 0;
29036 int maybe_template_id = 0;
29037 cp_token *first_token;
29038 cp_token *token;
29039 tree default_argument;
29041 /* Add tokens until we have processed the entire default
29042 argument. We add the range [first_token, token). */
29043 first_token = cp_lexer_peek_token (parser->lexer);
29044 if (first_token->type == CPP_OPEN_BRACE)
29046 /* For list-initialization, this is straightforward. */
29047 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29048 token = cp_lexer_peek_token (parser->lexer);
29050 else while (true)
29052 bool done = false;
29054 /* Peek at the next token. */
29055 token = cp_lexer_peek_token (parser->lexer);
29056 /* What we do depends on what token we have. */
29057 switch (token->type)
29059 /* In valid code, a default argument must be
29060 immediately followed by a `,' `)', or `...'. */
29061 case CPP_COMMA:
29062 if (depth == 0 && maybe_template_id)
29064 /* If we've seen a '<', we might be in a
29065 template-argument-list. Until Core issue 325 is
29066 resolved, we don't know how this situation ought
29067 to be handled, so try to DTRT. We check whether
29068 what comes after the comma is a valid parameter
29069 declaration list. If it is, then the comma ends
29070 the default argument; otherwise the default
29071 argument continues. */
29072 bool error = false;
29073 cp_token *peek;
29075 /* Set ITALP so cp_parser_parameter_declaration_list
29076 doesn't decide to commit to this parse. */
29077 bool saved_italp = parser->in_template_argument_list_p;
29078 parser->in_template_argument_list_p = true;
29080 cp_parser_parse_tentatively (parser);
29082 if (nsdmi)
29084 /* Parse declarators until we reach a non-comma or
29085 somthing that cannot be an initializer.
29086 Just checking whether we're looking at a single
29087 declarator is insufficient. Consider:
29088 int var = tuple<T,U>::x;
29089 The template parameter 'U' looks exactly like a
29090 declarator. */
29093 int ctor_dtor_or_conv_p;
29094 cp_lexer_consume_token (parser->lexer);
29095 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29096 &ctor_dtor_or_conv_p,
29097 /*parenthesized_p=*/NULL,
29098 /*member_p=*/true,
29099 /*friend_p=*/false);
29100 peek = cp_lexer_peek_token (parser->lexer);
29101 if (cp_parser_error_occurred (parser))
29102 break;
29104 while (peek->type == CPP_COMMA);
29105 /* If we met an '=' or ';' then the original comma
29106 was the end of the NSDMI. Otherwise assume
29107 we're still in the NSDMI. */
29108 error = (peek->type != CPP_EQ
29109 && peek->type != CPP_SEMICOLON);
29111 else
29113 cp_lexer_consume_token (parser->lexer);
29114 begin_scope (sk_function_parms, NULL_TREE);
29115 if (cp_parser_parameter_declaration_list (parser)
29116 == error_mark_node)
29117 error = true;
29118 pop_bindings_and_leave_scope ();
29120 if (!cp_parser_error_occurred (parser) && !error)
29121 done = true;
29122 cp_parser_abort_tentative_parse (parser);
29124 parser->in_template_argument_list_p = saved_italp;
29125 break;
29127 /* FALLTHRU */
29128 case CPP_CLOSE_PAREN:
29129 case CPP_ELLIPSIS:
29130 /* If we run into a non-nested `;', `}', or `]',
29131 then the code is invalid -- but the default
29132 argument is certainly over. */
29133 case CPP_SEMICOLON:
29134 case CPP_CLOSE_BRACE:
29135 case CPP_CLOSE_SQUARE:
29136 if (depth == 0
29137 /* Handle correctly int n = sizeof ... ( p ); */
29138 && token->type != CPP_ELLIPSIS)
29139 done = true;
29140 /* Update DEPTH, if necessary. */
29141 else if (token->type == CPP_CLOSE_PAREN
29142 || token->type == CPP_CLOSE_BRACE
29143 || token->type == CPP_CLOSE_SQUARE)
29144 --depth;
29145 break;
29147 case CPP_OPEN_PAREN:
29148 case CPP_OPEN_SQUARE:
29149 case CPP_OPEN_BRACE:
29150 ++depth;
29151 break;
29153 case CPP_LESS:
29154 if (depth == 0)
29155 /* This might be the comparison operator, or it might
29156 start a template argument list. */
29157 ++maybe_template_id;
29158 break;
29160 case CPP_RSHIFT:
29161 if (cxx_dialect == cxx98)
29162 break;
29163 /* Fall through for C++0x, which treats the `>>'
29164 operator like two `>' tokens in certain
29165 cases. */
29166 gcc_fallthrough ();
29168 case CPP_GREATER:
29169 if (depth == 0)
29171 /* This might be an operator, or it might close a
29172 template argument list. But if a previous '<'
29173 started a template argument list, this will have
29174 closed it, so we can't be in one anymore. */
29175 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29176 if (maybe_template_id < 0)
29177 maybe_template_id = 0;
29179 break;
29181 /* If we run out of tokens, issue an error message. */
29182 case CPP_EOF:
29183 case CPP_PRAGMA_EOL:
29184 error_at (token->location, "file ends in default argument");
29185 return error_mark_node;
29187 case CPP_NAME:
29188 case CPP_SCOPE:
29189 /* In these cases, we should look for template-ids.
29190 For example, if the default argument is
29191 `X<int, double>()', we need to do name lookup to
29192 figure out whether or not `X' is a template; if
29193 so, the `,' does not end the default argument.
29195 That is not yet done. */
29196 break;
29198 default:
29199 break;
29202 /* If we've reached the end, stop. */
29203 if (done)
29204 break;
29206 /* Add the token to the token block. */
29207 token = cp_lexer_consume_token (parser->lexer);
29210 /* Create a DEFAULT_ARG to represent the unparsed default
29211 argument. */
29212 default_argument = make_node (DEFAULT_ARG);
29213 DEFARG_TOKENS (default_argument)
29214 = cp_token_cache_new (first_token, token);
29215 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29217 return default_argument;
29220 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29222 location_t
29223 defarg_location (tree default_argument)
29225 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29226 location_t start = tokens->first->location;
29227 location_t end = tokens->last->location;
29228 return make_location (start, start, end);
29231 /* Begin parsing tentatively. We always save tokens while parsing
29232 tentatively so that if the tentative parsing fails we can restore the
29233 tokens. */
29235 static void
29236 cp_parser_parse_tentatively (cp_parser* parser)
29238 /* Enter a new parsing context. */
29239 parser->context = cp_parser_context_new (parser->context);
29240 /* Begin saving tokens. */
29241 cp_lexer_save_tokens (parser->lexer);
29242 /* In order to avoid repetitive access control error messages,
29243 access checks are queued up until we are no longer parsing
29244 tentatively. */
29245 push_deferring_access_checks (dk_deferred);
29248 /* Commit to the currently active tentative parse. */
29250 static void
29251 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29253 cp_parser_context *context;
29254 cp_lexer *lexer;
29256 /* Mark all of the levels as committed. */
29257 lexer = parser->lexer;
29258 for (context = parser->context; context->next; context = context->next)
29260 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29261 break;
29262 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29263 while (!cp_lexer_saving_tokens (lexer))
29264 lexer = lexer->next;
29265 cp_lexer_commit_tokens (lexer);
29269 /* Commit to the topmost currently active tentative parse.
29271 Note that this function shouldn't be called when there are
29272 irreversible side-effects while in a tentative state. For
29273 example, we shouldn't create a permanent entry in the symbol
29274 table, or issue an error message that might not apply if the
29275 tentative parse is aborted. */
29277 static void
29278 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29280 cp_parser_context *context = parser->context;
29281 cp_lexer *lexer = parser->lexer;
29283 if (context)
29285 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29286 return;
29287 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29289 while (!cp_lexer_saving_tokens (lexer))
29290 lexer = lexer->next;
29291 cp_lexer_commit_tokens (lexer);
29295 /* Abort the currently active tentative parse. All consumed tokens
29296 will be rolled back, and no diagnostics will be issued. */
29298 static void
29299 cp_parser_abort_tentative_parse (cp_parser* parser)
29301 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29302 || errorcount > 0);
29303 cp_parser_simulate_error (parser);
29304 /* Now, pretend that we want to see if the construct was
29305 successfully parsed. */
29306 cp_parser_parse_definitely (parser);
29309 /* Stop parsing tentatively. If a parse error has occurred, restore the
29310 token stream. Otherwise, commit to the tokens we have consumed.
29311 Returns true if no error occurred; false otherwise. */
29313 static bool
29314 cp_parser_parse_definitely (cp_parser* parser)
29316 bool error_occurred;
29317 cp_parser_context *context;
29319 /* Remember whether or not an error occurred, since we are about to
29320 destroy that information. */
29321 error_occurred = cp_parser_error_occurred (parser);
29322 /* Remove the topmost context from the stack. */
29323 context = parser->context;
29324 parser->context = context->next;
29325 /* If no parse errors occurred, commit to the tentative parse. */
29326 if (!error_occurred)
29328 /* Commit to the tokens read tentatively, unless that was
29329 already done. */
29330 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29331 cp_lexer_commit_tokens (parser->lexer);
29333 pop_to_parent_deferring_access_checks ();
29335 /* Otherwise, if errors occurred, roll back our state so that things
29336 are just as they were before we began the tentative parse. */
29337 else
29339 cp_lexer_rollback_tokens (parser->lexer);
29340 pop_deferring_access_checks ();
29342 /* Add the context to the front of the free list. */
29343 context->next = cp_parser_context_free_list;
29344 cp_parser_context_free_list = context;
29346 return !error_occurred;
29349 /* Returns true if we are parsing tentatively and are not committed to
29350 this tentative parse. */
29352 static bool
29353 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29355 return (cp_parser_parsing_tentatively (parser)
29356 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29359 /* Returns nonzero iff an error has occurred during the most recent
29360 tentative parse. */
29362 static bool
29363 cp_parser_error_occurred (cp_parser* parser)
29365 return (cp_parser_parsing_tentatively (parser)
29366 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29369 /* Returns nonzero if GNU extensions are allowed. */
29371 static bool
29372 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29374 return parser->allow_gnu_extensions_p;
29377 /* Objective-C++ Productions */
29380 /* Parse an Objective-C expression, which feeds into a primary-expression
29381 above.
29383 objc-expression:
29384 objc-message-expression
29385 objc-string-literal
29386 objc-encode-expression
29387 objc-protocol-expression
29388 objc-selector-expression
29390 Returns a tree representation of the expression. */
29392 static cp_expr
29393 cp_parser_objc_expression (cp_parser* parser)
29395 /* Try to figure out what kind of declaration is present. */
29396 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29398 switch (kwd->type)
29400 case CPP_OPEN_SQUARE:
29401 return cp_parser_objc_message_expression (parser);
29403 case CPP_OBJC_STRING:
29404 kwd = cp_lexer_consume_token (parser->lexer);
29405 return objc_build_string_object (kwd->u.value);
29407 case CPP_KEYWORD:
29408 switch (kwd->keyword)
29410 case RID_AT_ENCODE:
29411 return cp_parser_objc_encode_expression (parser);
29413 case RID_AT_PROTOCOL:
29414 return cp_parser_objc_protocol_expression (parser);
29416 case RID_AT_SELECTOR:
29417 return cp_parser_objc_selector_expression (parser);
29419 default:
29420 break;
29422 /* FALLTHRU */
29423 default:
29424 error_at (kwd->location,
29425 "misplaced %<@%D%> Objective-C++ construct",
29426 kwd->u.value);
29427 cp_parser_skip_to_end_of_block_or_statement (parser);
29430 return error_mark_node;
29433 /* Parse an Objective-C message expression.
29435 objc-message-expression:
29436 [ objc-message-receiver objc-message-args ]
29438 Returns a representation of an Objective-C message. */
29440 static tree
29441 cp_parser_objc_message_expression (cp_parser* parser)
29443 tree receiver, messageargs;
29445 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29446 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29447 receiver = cp_parser_objc_message_receiver (parser);
29448 messageargs = cp_parser_objc_message_args (parser);
29449 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29450 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29452 tree result = objc_build_message_expr (receiver, messageargs);
29454 /* Construct a location e.g.
29455 [self func1:5]
29456 ^~~~~~~~~~~~~~
29457 ranging from the '[' to the ']', with the caret at the start. */
29458 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29459 protected_set_expr_location (result, combined_loc);
29461 return result;
29464 /* Parse an objc-message-receiver.
29466 objc-message-receiver:
29467 expression
29468 simple-type-specifier
29470 Returns a representation of the type or expression. */
29472 static tree
29473 cp_parser_objc_message_receiver (cp_parser* parser)
29475 tree rcv;
29477 /* An Objective-C message receiver may be either (1) a type
29478 or (2) an expression. */
29479 cp_parser_parse_tentatively (parser);
29480 rcv = cp_parser_expression (parser);
29482 /* If that worked out, fine. */
29483 if (cp_parser_parse_definitely (parser))
29484 return rcv;
29486 cp_parser_parse_tentatively (parser);
29487 rcv = cp_parser_simple_type_specifier (parser,
29488 /*decl_specs=*/NULL,
29489 CP_PARSER_FLAGS_NONE);
29491 if (cp_parser_parse_definitely (parser))
29492 return objc_get_class_reference (rcv);
29494 cp_parser_error (parser, "objective-c++ message receiver expected");
29495 return error_mark_node;
29498 /* Parse the arguments and selectors comprising an Objective-C message.
29500 objc-message-args:
29501 objc-selector
29502 objc-selector-args
29503 objc-selector-args , objc-comma-args
29505 objc-selector-args:
29506 objc-selector [opt] : assignment-expression
29507 objc-selector-args objc-selector [opt] : assignment-expression
29509 objc-comma-args:
29510 assignment-expression
29511 objc-comma-args , assignment-expression
29513 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29514 selector arguments and TREE_VALUE containing a list of comma
29515 arguments. */
29517 static tree
29518 cp_parser_objc_message_args (cp_parser* parser)
29520 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29521 bool maybe_unary_selector_p = true;
29522 cp_token *token = cp_lexer_peek_token (parser->lexer);
29524 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29526 tree selector = NULL_TREE, arg;
29528 if (token->type != CPP_COLON)
29529 selector = cp_parser_objc_selector (parser);
29531 /* Detect if we have a unary selector. */
29532 if (maybe_unary_selector_p
29533 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29534 return build_tree_list (selector, NULL_TREE);
29536 maybe_unary_selector_p = false;
29537 cp_parser_require (parser, CPP_COLON, RT_COLON);
29538 arg = cp_parser_assignment_expression (parser);
29540 sel_args
29541 = chainon (sel_args,
29542 build_tree_list (selector, arg));
29544 token = cp_lexer_peek_token (parser->lexer);
29547 /* Handle non-selector arguments, if any. */
29548 while (token->type == CPP_COMMA)
29550 tree arg;
29552 cp_lexer_consume_token (parser->lexer);
29553 arg = cp_parser_assignment_expression (parser);
29555 addl_args
29556 = chainon (addl_args,
29557 build_tree_list (NULL_TREE, arg));
29559 token = cp_lexer_peek_token (parser->lexer);
29562 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29564 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29565 return build_tree_list (error_mark_node, error_mark_node);
29568 return build_tree_list (sel_args, addl_args);
29571 /* Parse an Objective-C encode expression.
29573 objc-encode-expression:
29574 @encode objc-typename
29576 Returns an encoded representation of the type argument. */
29578 static cp_expr
29579 cp_parser_objc_encode_expression (cp_parser* parser)
29581 tree type;
29582 cp_token *token;
29583 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29585 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29586 matching_parens parens;
29587 parens.require_open (parser);
29588 token = cp_lexer_peek_token (parser->lexer);
29589 type = complete_type (cp_parser_type_id (parser));
29590 parens.require_close (parser);
29592 if (!type)
29594 error_at (token->location,
29595 "%<@encode%> must specify a type as an argument");
29596 return error_mark_node;
29599 /* This happens if we find @encode(T) (where T is a template
29600 typename or something dependent on a template typename) when
29601 parsing a template. In that case, we can't compile it
29602 immediately, but we rather create an AT_ENCODE_EXPR which will
29603 need to be instantiated when the template is used.
29605 if (dependent_type_p (type))
29607 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29608 TREE_READONLY (value) = 1;
29609 return value;
29613 /* Build a location of the form:
29614 @encode(int)
29615 ^~~~~~~~~~~~
29616 with caret==start at the @ token, finishing at the close paren. */
29617 location_t combined_loc
29618 = make_location (start_loc, start_loc,
29619 cp_lexer_previous_token (parser->lexer)->location);
29621 return cp_expr (objc_build_encode_expr (type), combined_loc);
29624 /* Parse an Objective-C @defs expression. */
29626 static tree
29627 cp_parser_objc_defs_expression (cp_parser *parser)
29629 tree name;
29631 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29632 matching_parens parens;
29633 parens.require_open (parser);
29634 name = cp_parser_identifier (parser);
29635 parens.require_close (parser);
29637 return objc_get_class_ivars (name);
29640 /* Parse an Objective-C protocol expression.
29642 objc-protocol-expression:
29643 @protocol ( identifier )
29645 Returns a representation of the protocol expression. */
29647 static tree
29648 cp_parser_objc_protocol_expression (cp_parser* parser)
29650 tree proto;
29651 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29653 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29654 matching_parens parens;
29655 parens.require_open (parser);
29656 proto = cp_parser_identifier (parser);
29657 parens.require_close (parser);
29659 /* Build a location of the form:
29660 @protocol(prot)
29661 ^~~~~~~~~~~~~~~
29662 with caret==start at the @ token, finishing at the close paren. */
29663 location_t combined_loc
29664 = make_location (start_loc, start_loc,
29665 cp_lexer_previous_token (parser->lexer)->location);
29666 tree result = objc_build_protocol_expr (proto);
29667 protected_set_expr_location (result, combined_loc);
29668 return result;
29671 /* Parse an Objective-C selector expression.
29673 objc-selector-expression:
29674 @selector ( objc-method-signature )
29676 objc-method-signature:
29677 objc-selector
29678 objc-selector-seq
29680 objc-selector-seq:
29681 objc-selector :
29682 objc-selector-seq objc-selector :
29684 Returns a representation of the method selector. */
29686 static tree
29687 cp_parser_objc_selector_expression (cp_parser* parser)
29689 tree sel_seq = NULL_TREE;
29690 bool maybe_unary_selector_p = true;
29691 cp_token *token;
29692 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29694 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29695 matching_parens parens;
29696 parens.require_open (parser);
29697 token = cp_lexer_peek_token (parser->lexer);
29699 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29700 || token->type == CPP_SCOPE)
29702 tree selector = NULL_TREE;
29704 if (token->type != CPP_COLON
29705 || token->type == CPP_SCOPE)
29706 selector = cp_parser_objc_selector (parser);
29708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29709 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29711 /* Detect if we have a unary selector. */
29712 if (maybe_unary_selector_p)
29714 sel_seq = selector;
29715 goto finish_selector;
29717 else
29719 cp_parser_error (parser, "expected %<:%>");
29722 maybe_unary_selector_p = false;
29723 token = cp_lexer_consume_token (parser->lexer);
29725 if (token->type == CPP_SCOPE)
29727 sel_seq
29728 = chainon (sel_seq,
29729 build_tree_list (selector, NULL_TREE));
29730 sel_seq
29731 = chainon (sel_seq,
29732 build_tree_list (NULL_TREE, NULL_TREE));
29734 else
29735 sel_seq
29736 = chainon (sel_seq,
29737 build_tree_list (selector, NULL_TREE));
29739 token = cp_lexer_peek_token (parser->lexer);
29742 finish_selector:
29743 parens.require_close (parser);
29746 /* Build a location of the form:
29747 @selector(func)
29748 ^~~~~~~~~~~~~~~
29749 with caret==start at the @ token, finishing at the close paren. */
29750 location_t combined_loc
29751 = make_location (loc, loc,
29752 cp_lexer_previous_token (parser->lexer)->location);
29753 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29754 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29755 protected_set_expr_location (result, combined_loc);
29756 return result;
29759 /* Parse a list of identifiers.
29761 objc-identifier-list:
29762 identifier
29763 objc-identifier-list , identifier
29765 Returns a TREE_LIST of identifier nodes. */
29767 static tree
29768 cp_parser_objc_identifier_list (cp_parser* parser)
29770 tree identifier;
29771 tree list;
29772 cp_token *sep;
29774 identifier = cp_parser_identifier (parser);
29775 if (identifier == error_mark_node)
29776 return error_mark_node;
29778 list = build_tree_list (NULL_TREE, identifier);
29779 sep = cp_lexer_peek_token (parser->lexer);
29781 while (sep->type == CPP_COMMA)
29783 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29784 identifier = cp_parser_identifier (parser);
29785 if (identifier == error_mark_node)
29786 return list;
29788 list = chainon (list, build_tree_list (NULL_TREE,
29789 identifier));
29790 sep = cp_lexer_peek_token (parser->lexer);
29793 return list;
29796 /* Parse an Objective-C alias declaration.
29798 objc-alias-declaration:
29799 @compatibility_alias identifier identifier ;
29801 This function registers the alias mapping with the Objective-C front end.
29802 It returns nothing. */
29804 static void
29805 cp_parser_objc_alias_declaration (cp_parser* parser)
29807 tree alias, orig;
29809 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29810 alias = cp_parser_identifier (parser);
29811 orig = cp_parser_identifier (parser);
29812 objc_declare_alias (alias, orig);
29813 cp_parser_consume_semicolon_at_end_of_statement (parser);
29816 /* Parse an Objective-C class forward-declaration.
29818 objc-class-declaration:
29819 @class objc-identifier-list ;
29821 The function registers the forward declarations with the Objective-C
29822 front end. It returns nothing. */
29824 static void
29825 cp_parser_objc_class_declaration (cp_parser* parser)
29827 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29828 while (true)
29830 tree id;
29832 id = cp_parser_identifier (parser);
29833 if (id == error_mark_node)
29834 break;
29836 objc_declare_class (id);
29838 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29839 cp_lexer_consume_token (parser->lexer);
29840 else
29841 break;
29843 cp_parser_consume_semicolon_at_end_of_statement (parser);
29846 /* Parse a list of Objective-C protocol references.
29848 objc-protocol-refs-opt:
29849 objc-protocol-refs [opt]
29851 objc-protocol-refs:
29852 < objc-identifier-list >
29854 Returns a TREE_LIST of identifiers, if any. */
29856 static tree
29857 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29859 tree protorefs = NULL_TREE;
29861 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29863 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29864 protorefs = cp_parser_objc_identifier_list (parser);
29865 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29868 return protorefs;
29871 /* Parse a Objective-C visibility specification. */
29873 static void
29874 cp_parser_objc_visibility_spec (cp_parser* parser)
29876 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29878 switch (vis->keyword)
29880 case RID_AT_PRIVATE:
29881 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29882 break;
29883 case RID_AT_PROTECTED:
29884 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29885 break;
29886 case RID_AT_PUBLIC:
29887 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29888 break;
29889 case RID_AT_PACKAGE:
29890 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29891 break;
29892 default:
29893 return;
29896 /* Eat '@private'/'@protected'/'@public'. */
29897 cp_lexer_consume_token (parser->lexer);
29900 /* Parse an Objective-C method type. Return 'true' if it is a class
29901 (+) method, and 'false' if it is an instance (-) method. */
29903 static inline bool
29904 cp_parser_objc_method_type (cp_parser* parser)
29906 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29907 return true;
29908 else
29909 return false;
29912 /* Parse an Objective-C protocol qualifier. */
29914 static tree
29915 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29917 tree quals = NULL_TREE, node;
29918 cp_token *token = cp_lexer_peek_token (parser->lexer);
29920 node = token->u.value;
29922 while (node && identifier_p (node)
29923 && (node == ridpointers [(int) RID_IN]
29924 || node == ridpointers [(int) RID_OUT]
29925 || node == ridpointers [(int) RID_INOUT]
29926 || node == ridpointers [(int) RID_BYCOPY]
29927 || node == ridpointers [(int) RID_BYREF]
29928 || node == ridpointers [(int) RID_ONEWAY]))
29930 quals = tree_cons (NULL_TREE, node, quals);
29931 cp_lexer_consume_token (parser->lexer);
29932 token = cp_lexer_peek_token (parser->lexer);
29933 node = token->u.value;
29936 return quals;
29939 /* Parse an Objective-C typename. */
29941 static tree
29942 cp_parser_objc_typename (cp_parser* parser)
29944 tree type_name = NULL_TREE;
29946 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29948 tree proto_quals, cp_type = NULL_TREE;
29950 matching_parens parens;
29951 parens.consume_open (parser); /* Eat '('. */
29952 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29954 /* An ObjC type name may consist of just protocol qualifiers, in which
29955 case the type shall default to 'id'. */
29956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29958 cp_type = cp_parser_type_id (parser);
29960 /* If the type could not be parsed, an error has already
29961 been produced. For error recovery, behave as if it had
29962 not been specified, which will use the default type
29963 'id'. */
29964 if (cp_type == error_mark_node)
29966 cp_type = NULL_TREE;
29967 /* We need to skip to the closing parenthesis as
29968 cp_parser_type_id() does not seem to do it for
29969 us. */
29970 cp_parser_skip_to_closing_parenthesis (parser,
29971 /*recovering=*/true,
29972 /*or_comma=*/false,
29973 /*consume_paren=*/false);
29977 parens.require_close (parser);
29978 type_name = build_tree_list (proto_quals, cp_type);
29981 return type_name;
29984 /* Check to see if TYPE refers to an Objective-C selector name. */
29986 static bool
29987 cp_parser_objc_selector_p (enum cpp_ttype type)
29989 return (type == CPP_NAME || type == CPP_KEYWORD
29990 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29991 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29992 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29993 || type == CPP_XOR || type == CPP_XOR_EQ);
29996 /* Parse an Objective-C selector. */
29998 static tree
29999 cp_parser_objc_selector (cp_parser* parser)
30001 cp_token *token = cp_lexer_consume_token (parser->lexer);
30003 if (!cp_parser_objc_selector_p (token->type))
30005 error_at (token->location, "invalid Objective-C++ selector name");
30006 return error_mark_node;
30009 /* C++ operator names are allowed to appear in ObjC selectors. */
30010 switch (token->type)
30012 case CPP_AND_AND: return get_identifier ("and");
30013 case CPP_AND_EQ: return get_identifier ("and_eq");
30014 case CPP_AND: return get_identifier ("bitand");
30015 case CPP_OR: return get_identifier ("bitor");
30016 case CPP_COMPL: return get_identifier ("compl");
30017 case CPP_NOT: return get_identifier ("not");
30018 case CPP_NOT_EQ: return get_identifier ("not_eq");
30019 case CPP_OR_OR: return get_identifier ("or");
30020 case CPP_OR_EQ: return get_identifier ("or_eq");
30021 case CPP_XOR: return get_identifier ("xor");
30022 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30023 default: return token->u.value;
30027 /* Parse an Objective-C params list. */
30029 static tree
30030 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30032 tree params = NULL_TREE;
30033 bool maybe_unary_selector_p = true;
30034 cp_token *token = cp_lexer_peek_token (parser->lexer);
30036 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30038 tree selector = NULL_TREE, type_name, identifier;
30039 tree parm_attr = NULL_TREE;
30041 if (token->keyword == RID_ATTRIBUTE)
30042 break;
30044 if (token->type != CPP_COLON)
30045 selector = cp_parser_objc_selector (parser);
30047 /* Detect if we have a unary selector. */
30048 if (maybe_unary_selector_p
30049 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30051 params = selector; /* Might be followed by attributes. */
30052 break;
30055 maybe_unary_selector_p = false;
30056 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30058 /* Something went quite wrong. There should be a colon
30059 here, but there is not. Stop parsing parameters. */
30060 break;
30062 type_name = cp_parser_objc_typename (parser);
30063 /* New ObjC allows attributes on parameters too. */
30064 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30065 parm_attr = cp_parser_attributes_opt (parser);
30066 identifier = cp_parser_identifier (parser);
30068 params
30069 = chainon (params,
30070 objc_build_keyword_decl (selector,
30071 type_name,
30072 identifier,
30073 parm_attr));
30075 token = cp_lexer_peek_token (parser->lexer);
30078 if (params == NULL_TREE)
30080 cp_parser_error (parser, "objective-c++ method declaration is expected");
30081 return error_mark_node;
30084 /* We allow tail attributes for the method. */
30085 if (token->keyword == RID_ATTRIBUTE)
30087 *attributes = cp_parser_attributes_opt (parser);
30088 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30089 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30090 return params;
30091 cp_parser_error (parser,
30092 "method attributes must be specified at the end");
30093 return error_mark_node;
30096 if (params == NULL_TREE)
30098 cp_parser_error (parser, "objective-c++ method declaration is expected");
30099 return error_mark_node;
30101 return params;
30104 /* Parse the non-keyword Objective-C params. */
30106 static tree
30107 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30108 tree* attributes)
30110 tree params = make_node (TREE_LIST);
30111 cp_token *token = cp_lexer_peek_token (parser->lexer);
30112 *ellipsisp = false; /* Initially, assume no ellipsis. */
30114 while (token->type == CPP_COMMA)
30116 cp_parameter_declarator *parmdecl;
30117 tree parm;
30119 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30120 token = cp_lexer_peek_token (parser->lexer);
30122 if (token->type == CPP_ELLIPSIS)
30124 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30125 *ellipsisp = true;
30126 token = cp_lexer_peek_token (parser->lexer);
30127 break;
30130 /* TODO: parse attributes for tail parameters. */
30131 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30132 parm = grokdeclarator (parmdecl->declarator,
30133 &parmdecl->decl_specifiers,
30134 PARM, /*initialized=*/0,
30135 /*attrlist=*/NULL);
30137 chainon (params, build_tree_list (NULL_TREE, parm));
30138 token = cp_lexer_peek_token (parser->lexer);
30141 /* We allow tail attributes for the method. */
30142 if (token->keyword == RID_ATTRIBUTE)
30144 if (*attributes == NULL_TREE)
30146 *attributes = cp_parser_attributes_opt (parser);
30147 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30148 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30149 return params;
30151 else
30152 /* We have an error, but parse the attributes, so that we can
30153 carry on. */
30154 *attributes = cp_parser_attributes_opt (parser);
30156 cp_parser_error (parser,
30157 "method attributes must be specified at the end");
30158 return error_mark_node;
30161 return params;
30164 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30166 static void
30167 cp_parser_objc_interstitial_code (cp_parser* parser)
30169 cp_token *token = cp_lexer_peek_token (parser->lexer);
30171 /* If the next token is `extern' and the following token is a string
30172 literal, then we have a linkage specification. */
30173 if (token->keyword == RID_EXTERN
30174 && cp_parser_is_pure_string_literal
30175 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30176 cp_parser_linkage_specification (parser);
30177 /* Handle #pragma, if any. */
30178 else if (token->type == CPP_PRAGMA)
30179 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30180 /* Allow stray semicolons. */
30181 else if (token->type == CPP_SEMICOLON)
30182 cp_lexer_consume_token (parser->lexer);
30183 /* Mark methods as optional or required, when building protocols. */
30184 else if (token->keyword == RID_AT_OPTIONAL)
30186 cp_lexer_consume_token (parser->lexer);
30187 objc_set_method_opt (true);
30189 else if (token->keyword == RID_AT_REQUIRED)
30191 cp_lexer_consume_token (parser->lexer);
30192 objc_set_method_opt (false);
30194 else if (token->keyword == RID_NAMESPACE)
30195 cp_parser_namespace_definition (parser);
30196 /* Other stray characters must generate errors. */
30197 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30199 cp_lexer_consume_token (parser->lexer);
30200 error ("stray %qs between Objective-C++ methods",
30201 token->type == CPP_OPEN_BRACE ? "{" : "}");
30203 /* Finally, try to parse a block-declaration, or a function-definition. */
30204 else
30205 cp_parser_block_declaration (parser, /*statement_p=*/false);
30208 /* Parse a method signature. */
30210 static tree
30211 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30213 tree rettype, kwdparms, optparms;
30214 bool ellipsis = false;
30215 bool is_class_method;
30217 is_class_method = cp_parser_objc_method_type (parser);
30218 rettype = cp_parser_objc_typename (parser);
30219 *attributes = NULL_TREE;
30220 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30221 if (kwdparms == error_mark_node)
30222 return error_mark_node;
30223 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30224 if (optparms == error_mark_node)
30225 return error_mark_node;
30227 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30230 static bool
30231 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30233 tree tattr;
30234 cp_lexer_save_tokens (parser->lexer);
30235 tattr = cp_parser_attributes_opt (parser);
30236 gcc_assert (tattr) ;
30238 /* If the attributes are followed by a method introducer, this is not allowed.
30239 Dump the attributes and flag the situation. */
30240 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30241 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30242 return true;
30244 /* Otherwise, the attributes introduce some interstitial code, possibly so
30245 rewind to allow that check. */
30246 cp_lexer_rollback_tokens (parser->lexer);
30247 return false;
30250 /* Parse an Objective-C method prototype list. */
30252 static void
30253 cp_parser_objc_method_prototype_list (cp_parser* parser)
30255 cp_token *token = cp_lexer_peek_token (parser->lexer);
30257 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30259 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30261 tree attributes, sig;
30262 bool is_class_method;
30263 if (token->type == CPP_PLUS)
30264 is_class_method = true;
30265 else
30266 is_class_method = false;
30267 sig = cp_parser_objc_method_signature (parser, &attributes);
30268 if (sig == error_mark_node)
30270 cp_parser_skip_to_end_of_block_or_statement (parser);
30271 token = cp_lexer_peek_token (parser->lexer);
30272 continue;
30274 objc_add_method_declaration (is_class_method, sig, attributes);
30275 cp_parser_consume_semicolon_at_end_of_statement (parser);
30277 else if (token->keyword == RID_AT_PROPERTY)
30278 cp_parser_objc_at_property_declaration (parser);
30279 else if (token->keyword == RID_ATTRIBUTE
30280 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30281 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30282 OPT_Wattributes,
30283 "prefix attributes are ignored for methods");
30284 else
30285 /* Allow for interspersed non-ObjC++ code. */
30286 cp_parser_objc_interstitial_code (parser);
30288 token = cp_lexer_peek_token (parser->lexer);
30291 if (token->type != CPP_EOF)
30292 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30293 else
30294 cp_parser_error (parser, "expected %<@end%>");
30296 objc_finish_interface ();
30299 /* Parse an Objective-C method definition list. */
30301 static void
30302 cp_parser_objc_method_definition_list (cp_parser* parser)
30304 cp_token *token = cp_lexer_peek_token (parser->lexer);
30306 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30308 tree meth;
30310 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30312 cp_token *ptk;
30313 tree sig, attribute;
30314 bool is_class_method;
30315 if (token->type == CPP_PLUS)
30316 is_class_method = true;
30317 else
30318 is_class_method = false;
30319 push_deferring_access_checks (dk_deferred);
30320 sig = cp_parser_objc_method_signature (parser, &attribute);
30321 if (sig == error_mark_node)
30323 cp_parser_skip_to_end_of_block_or_statement (parser);
30324 token = cp_lexer_peek_token (parser->lexer);
30325 continue;
30327 objc_start_method_definition (is_class_method, sig, attribute,
30328 NULL_TREE);
30330 /* For historical reasons, we accept an optional semicolon. */
30331 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30332 cp_lexer_consume_token (parser->lexer);
30334 ptk = cp_lexer_peek_token (parser->lexer);
30335 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30336 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30338 perform_deferred_access_checks (tf_warning_or_error);
30339 stop_deferring_access_checks ();
30340 meth = cp_parser_function_definition_after_declarator (parser,
30341 false);
30342 pop_deferring_access_checks ();
30343 objc_finish_method_definition (meth);
30346 /* The following case will be removed once @synthesize is
30347 completely implemented. */
30348 else if (token->keyword == RID_AT_PROPERTY)
30349 cp_parser_objc_at_property_declaration (parser);
30350 else if (token->keyword == RID_AT_SYNTHESIZE)
30351 cp_parser_objc_at_synthesize_declaration (parser);
30352 else if (token->keyword == RID_AT_DYNAMIC)
30353 cp_parser_objc_at_dynamic_declaration (parser);
30354 else if (token->keyword == RID_ATTRIBUTE
30355 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30356 warning_at (token->location, OPT_Wattributes,
30357 "prefix attributes are ignored for methods");
30358 else
30359 /* Allow for interspersed non-ObjC++ code. */
30360 cp_parser_objc_interstitial_code (parser);
30362 token = cp_lexer_peek_token (parser->lexer);
30365 if (token->type != CPP_EOF)
30366 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30367 else
30368 cp_parser_error (parser, "expected %<@end%>");
30370 objc_finish_implementation ();
30373 /* Parse Objective-C ivars. */
30375 static void
30376 cp_parser_objc_class_ivars (cp_parser* parser)
30378 cp_token *token = cp_lexer_peek_token (parser->lexer);
30380 if (token->type != CPP_OPEN_BRACE)
30381 return; /* No ivars specified. */
30383 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30384 token = cp_lexer_peek_token (parser->lexer);
30386 while (token->type != CPP_CLOSE_BRACE
30387 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30389 cp_decl_specifier_seq declspecs;
30390 int decl_class_or_enum_p;
30391 tree prefix_attributes;
30393 cp_parser_objc_visibility_spec (parser);
30395 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30396 break;
30398 cp_parser_decl_specifier_seq (parser,
30399 CP_PARSER_FLAGS_OPTIONAL,
30400 &declspecs,
30401 &decl_class_or_enum_p);
30403 /* auto, register, static, extern, mutable. */
30404 if (declspecs.storage_class != sc_none)
30406 cp_parser_error (parser, "invalid type for instance variable");
30407 declspecs.storage_class = sc_none;
30410 /* thread_local. */
30411 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30413 cp_parser_error (parser, "invalid type for instance variable");
30414 declspecs.locations[ds_thread] = 0;
30417 /* typedef. */
30418 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30420 cp_parser_error (parser, "invalid type for instance variable");
30421 declspecs.locations[ds_typedef] = 0;
30424 prefix_attributes = declspecs.attributes;
30425 declspecs.attributes = NULL_TREE;
30427 /* Keep going until we hit the `;' at the end of the
30428 declaration. */
30429 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30431 tree width = NULL_TREE, attributes, first_attribute, decl;
30432 cp_declarator *declarator = NULL;
30433 int ctor_dtor_or_conv_p;
30435 /* Check for a (possibly unnamed) bitfield declaration. */
30436 token = cp_lexer_peek_token (parser->lexer);
30437 if (token->type == CPP_COLON)
30438 goto eat_colon;
30440 if (token->type == CPP_NAME
30441 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30442 == CPP_COLON))
30444 /* Get the name of the bitfield. */
30445 declarator = make_id_declarator (NULL_TREE,
30446 cp_parser_identifier (parser),
30447 sfk_none);
30449 eat_colon:
30450 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30451 /* Get the width of the bitfield. */
30452 width
30453 = cp_parser_constant_expression (parser);
30455 else
30457 /* Parse the declarator. */
30458 declarator
30459 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30460 &ctor_dtor_or_conv_p,
30461 /*parenthesized_p=*/NULL,
30462 /*member_p=*/false,
30463 /*friend_p=*/false);
30466 /* Look for attributes that apply to the ivar. */
30467 attributes = cp_parser_attributes_opt (parser);
30468 /* Remember which attributes are prefix attributes and
30469 which are not. */
30470 first_attribute = attributes;
30471 /* Combine the attributes. */
30472 attributes = attr_chainon (prefix_attributes, attributes);
30474 if (width)
30475 /* Create the bitfield declaration. */
30476 decl = grokbitfield (declarator, &declspecs,
30477 width, NULL_TREE, attributes);
30478 else
30479 decl = grokfield (declarator, &declspecs,
30480 NULL_TREE, /*init_const_expr_p=*/false,
30481 NULL_TREE, attributes);
30483 /* Add the instance variable. */
30484 if (decl != error_mark_node && decl != NULL_TREE)
30485 objc_add_instance_variable (decl);
30487 /* Reset PREFIX_ATTRIBUTES. */
30488 if (attributes != error_mark_node)
30490 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30491 attributes = TREE_CHAIN (attributes);
30492 if (attributes)
30493 TREE_CHAIN (attributes) = NULL_TREE;
30496 token = cp_lexer_peek_token (parser->lexer);
30498 if (token->type == CPP_COMMA)
30500 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30501 continue;
30503 break;
30506 cp_parser_consume_semicolon_at_end_of_statement (parser);
30507 token = cp_lexer_peek_token (parser->lexer);
30510 if (token->keyword == RID_AT_END)
30511 cp_parser_error (parser, "expected %<}%>");
30513 /* Do not consume the RID_AT_END, so it will be read again as terminating
30514 the @interface of @implementation. */
30515 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30516 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30518 /* For historical reasons, we accept an optional semicolon. */
30519 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30520 cp_lexer_consume_token (parser->lexer);
30523 /* Parse an Objective-C protocol declaration. */
30525 static void
30526 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30528 tree proto, protorefs;
30529 cp_token *tok;
30531 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30534 tok = cp_lexer_peek_token (parser->lexer);
30535 error_at (tok->location, "identifier expected after %<@protocol%>");
30536 cp_parser_consume_semicolon_at_end_of_statement (parser);
30537 return;
30540 /* See if we have a forward declaration or a definition. */
30541 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30543 /* Try a forward declaration first. */
30544 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30546 while (true)
30548 tree id;
30550 id = cp_parser_identifier (parser);
30551 if (id == error_mark_node)
30552 break;
30554 objc_declare_protocol (id, attributes);
30556 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30557 cp_lexer_consume_token (parser->lexer);
30558 else
30559 break;
30561 cp_parser_consume_semicolon_at_end_of_statement (parser);
30564 /* Ok, we got a full-fledged definition (or at least should). */
30565 else
30567 proto = cp_parser_identifier (parser);
30568 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30569 objc_start_protocol (proto, protorefs, attributes);
30570 cp_parser_objc_method_prototype_list (parser);
30574 /* Parse an Objective-C superclass or category. */
30576 static void
30577 cp_parser_objc_superclass_or_category (cp_parser *parser,
30578 bool iface_p,
30579 tree *super,
30580 tree *categ, bool *is_class_extension)
30582 cp_token *next = cp_lexer_peek_token (parser->lexer);
30584 *super = *categ = NULL_TREE;
30585 *is_class_extension = false;
30586 if (next->type == CPP_COLON)
30588 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30589 *super = cp_parser_identifier (parser);
30591 else if (next->type == CPP_OPEN_PAREN)
30593 matching_parens parens;
30594 parens.consume_open (parser); /* Eat '('. */
30596 /* If there is no category name, and this is an @interface, we
30597 have a class extension. */
30598 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30600 *categ = NULL_TREE;
30601 *is_class_extension = true;
30603 else
30604 *categ = cp_parser_identifier (parser);
30606 parens.require_close (parser);
30610 /* Parse an Objective-C class interface. */
30612 static void
30613 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30615 tree name, super, categ, protos;
30616 bool is_class_extension;
30618 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30619 name = cp_parser_identifier (parser);
30620 if (name == error_mark_node)
30622 /* It's hard to recover because even if valid @interface stuff
30623 is to follow, we can't compile it (or validate it) if we
30624 don't even know which class it refers to. Let's assume this
30625 was a stray '@interface' token in the stream and skip it.
30627 return;
30629 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30630 &is_class_extension);
30631 protos = cp_parser_objc_protocol_refs_opt (parser);
30633 /* We have either a class or a category on our hands. */
30634 if (categ || is_class_extension)
30635 objc_start_category_interface (name, categ, protos, attributes);
30636 else
30638 objc_start_class_interface (name, super, protos, attributes);
30639 /* Handle instance variable declarations, if any. */
30640 cp_parser_objc_class_ivars (parser);
30641 objc_continue_interface ();
30644 cp_parser_objc_method_prototype_list (parser);
30647 /* Parse an Objective-C class implementation. */
30649 static void
30650 cp_parser_objc_class_implementation (cp_parser* parser)
30652 tree name, super, categ;
30653 bool is_class_extension;
30655 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30656 name = cp_parser_identifier (parser);
30657 if (name == error_mark_node)
30659 /* It's hard to recover because even if valid @implementation
30660 stuff is to follow, we can't compile it (or validate it) if
30661 we don't even know which class it refers to. Let's assume
30662 this was a stray '@implementation' token in the stream and
30663 skip it.
30665 return;
30667 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30668 &is_class_extension);
30670 /* We have either a class or a category on our hands. */
30671 if (categ)
30672 objc_start_category_implementation (name, categ);
30673 else
30675 objc_start_class_implementation (name, super);
30676 /* Handle instance variable declarations, if any. */
30677 cp_parser_objc_class_ivars (parser);
30678 objc_continue_implementation ();
30681 cp_parser_objc_method_definition_list (parser);
30684 /* Consume the @end token and finish off the implementation. */
30686 static void
30687 cp_parser_objc_end_implementation (cp_parser* parser)
30689 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30690 objc_finish_implementation ();
30693 /* Parse an Objective-C declaration. */
30695 static void
30696 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30698 /* Try to figure out what kind of declaration is present. */
30699 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30701 if (attributes)
30702 switch (kwd->keyword)
30704 case RID_AT_ALIAS:
30705 case RID_AT_CLASS:
30706 case RID_AT_END:
30707 error_at (kwd->location, "attributes may not be specified before"
30708 " the %<@%D%> Objective-C++ keyword",
30709 kwd->u.value);
30710 attributes = NULL;
30711 break;
30712 case RID_AT_IMPLEMENTATION:
30713 warning_at (kwd->location, OPT_Wattributes,
30714 "prefix attributes are ignored before %<@%D%>",
30715 kwd->u.value);
30716 attributes = NULL;
30717 default:
30718 break;
30721 switch (kwd->keyword)
30723 case RID_AT_ALIAS:
30724 cp_parser_objc_alias_declaration (parser);
30725 break;
30726 case RID_AT_CLASS:
30727 cp_parser_objc_class_declaration (parser);
30728 break;
30729 case RID_AT_PROTOCOL:
30730 cp_parser_objc_protocol_declaration (parser, attributes);
30731 break;
30732 case RID_AT_INTERFACE:
30733 cp_parser_objc_class_interface (parser, attributes);
30734 break;
30735 case RID_AT_IMPLEMENTATION:
30736 cp_parser_objc_class_implementation (parser);
30737 break;
30738 case RID_AT_END:
30739 cp_parser_objc_end_implementation (parser);
30740 break;
30741 default:
30742 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30743 kwd->u.value);
30744 cp_parser_skip_to_end_of_block_or_statement (parser);
30748 /* Parse an Objective-C try-catch-finally statement.
30750 objc-try-catch-finally-stmt:
30751 @try compound-statement objc-catch-clause-seq [opt]
30752 objc-finally-clause [opt]
30754 objc-catch-clause-seq:
30755 objc-catch-clause objc-catch-clause-seq [opt]
30757 objc-catch-clause:
30758 @catch ( objc-exception-declaration ) compound-statement
30760 objc-finally-clause:
30761 @finally compound-statement
30763 objc-exception-declaration:
30764 parameter-declaration
30765 '...'
30767 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30769 Returns NULL_TREE.
30771 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30772 for C. Keep them in sync. */
30774 static tree
30775 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30777 location_t location;
30778 tree stmt;
30780 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30781 location = cp_lexer_peek_token (parser->lexer)->location;
30782 objc_maybe_warn_exceptions (location);
30783 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30784 node, lest it get absorbed into the surrounding block. */
30785 stmt = push_stmt_list ();
30786 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30787 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30789 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30791 cp_parameter_declarator *parm;
30792 tree parameter_declaration = error_mark_node;
30793 bool seen_open_paren = false;
30794 matching_parens parens;
30796 cp_lexer_consume_token (parser->lexer);
30797 if (parens.require_open (parser))
30798 seen_open_paren = true;
30799 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30801 /* We have "@catch (...)" (where the '...' are literally
30802 what is in the code). Skip the '...'.
30803 parameter_declaration is set to NULL_TREE, and
30804 objc_being_catch_clauses() knows that that means
30805 '...'. */
30806 cp_lexer_consume_token (parser->lexer);
30807 parameter_declaration = NULL_TREE;
30809 else
30811 /* We have "@catch (NSException *exception)" or something
30812 like that. Parse the parameter declaration. */
30813 parm = cp_parser_parameter_declaration (parser, false, NULL);
30814 if (parm == NULL)
30815 parameter_declaration = error_mark_node;
30816 else
30817 parameter_declaration = grokdeclarator (parm->declarator,
30818 &parm->decl_specifiers,
30819 PARM, /*initialized=*/0,
30820 /*attrlist=*/NULL);
30822 if (seen_open_paren)
30823 parens.require_close (parser);
30824 else
30826 /* If there was no open parenthesis, we are recovering from
30827 an error, and we are trying to figure out what mistake
30828 the user has made. */
30830 /* If there is an immediate closing parenthesis, the user
30831 probably forgot the opening one (ie, they typed "@catch
30832 NSException *e)". Parse the closing parenthesis and keep
30833 going. */
30834 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30835 cp_lexer_consume_token (parser->lexer);
30837 /* If these is no immediate closing parenthesis, the user
30838 probably doesn't know that parenthesis are required at
30839 all (ie, they typed "@catch NSException *e"). So, just
30840 forget about the closing parenthesis and keep going. */
30842 objc_begin_catch_clause (parameter_declaration);
30843 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30844 objc_finish_catch_clause ();
30846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30848 cp_lexer_consume_token (parser->lexer);
30849 location = cp_lexer_peek_token (parser->lexer)->location;
30850 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30851 node, lest it get absorbed into the surrounding block. */
30852 stmt = push_stmt_list ();
30853 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30854 objc_build_finally_clause (location, pop_stmt_list (stmt));
30857 return objc_finish_try_stmt ();
30860 /* Parse an Objective-C synchronized statement.
30862 objc-synchronized-stmt:
30863 @synchronized ( expression ) compound-statement
30865 Returns NULL_TREE. */
30867 static tree
30868 cp_parser_objc_synchronized_statement (cp_parser *parser)
30870 location_t location;
30871 tree lock, stmt;
30873 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30875 location = cp_lexer_peek_token (parser->lexer)->location;
30876 objc_maybe_warn_exceptions (location);
30877 matching_parens parens;
30878 parens.require_open (parser);
30879 lock = cp_parser_expression (parser);
30880 parens.require_close (parser);
30882 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30883 node, lest it get absorbed into the surrounding block. */
30884 stmt = push_stmt_list ();
30885 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30887 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30890 /* Parse an Objective-C throw statement.
30892 objc-throw-stmt:
30893 @throw assignment-expression [opt] ;
30895 Returns a constructed '@throw' statement. */
30897 static tree
30898 cp_parser_objc_throw_statement (cp_parser *parser)
30900 tree expr = NULL_TREE;
30901 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30903 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30906 expr = cp_parser_expression (parser);
30908 cp_parser_consume_semicolon_at_end_of_statement (parser);
30910 return objc_build_throw_stmt (loc, expr);
30913 /* Parse an Objective-C statement. */
30915 static tree
30916 cp_parser_objc_statement (cp_parser * parser)
30918 /* Try to figure out what kind of declaration is present. */
30919 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30921 switch (kwd->keyword)
30923 case RID_AT_TRY:
30924 return cp_parser_objc_try_catch_finally_statement (parser);
30925 case RID_AT_SYNCHRONIZED:
30926 return cp_parser_objc_synchronized_statement (parser);
30927 case RID_AT_THROW:
30928 return cp_parser_objc_throw_statement (parser);
30929 default:
30930 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30931 kwd->u.value);
30932 cp_parser_skip_to_end_of_block_or_statement (parser);
30935 return error_mark_node;
30938 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30939 look ahead to see if an objc keyword follows the attributes. This
30940 is to detect the use of prefix attributes on ObjC @interface and
30941 @protocol. */
30943 static bool
30944 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30946 cp_lexer_save_tokens (parser->lexer);
30947 *attrib = cp_parser_attributes_opt (parser);
30948 gcc_assert (*attrib);
30949 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30951 cp_lexer_commit_tokens (parser->lexer);
30952 return true;
30954 cp_lexer_rollback_tokens (parser->lexer);
30955 return false;
30958 /* This routine is a minimal replacement for
30959 c_parser_struct_declaration () used when parsing the list of
30960 types/names or ObjC++ properties. For example, when parsing the
30961 code
30963 @property (readonly) int a, b, c;
30965 this function is responsible for parsing "int a, int b, int c" and
30966 returning the declarations as CHAIN of DECLs.
30968 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30969 similar parsing. */
30970 static tree
30971 cp_parser_objc_struct_declaration (cp_parser *parser)
30973 tree decls = NULL_TREE;
30974 cp_decl_specifier_seq declspecs;
30975 int decl_class_or_enum_p;
30976 tree prefix_attributes;
30978 cp_parser_decl_specifier_seq (parser,
30979 CP_PARSER_FLAGS_NONE,
30980 &declspecs,
30981 &decl_class_or_enum_p);
30983 if (declspecs.type == error_mark_node)
30984 return error_mark_node;
30986 /* auto, register, static, extern, mutable. */
30987 if (declspecs.storage_class != sc_none)
30989 cp_parser_error (parser, "invalid type for property");
30990 declspecs.storage_class = sc_none;
30993 /* thread_local. */
30994 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30996 cp_parser_error (parser, "invalid type for property");
30997 declspecs.locations[ds_thread] = 0;
31000 /* typedef. */
31001 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31003 cp_parser_error (parser, "invalid type for property");
31004 declspecs.locations[ds_typedef] = 0;
31007 prefix_attributes = declspecs.attributes;
31008 declspecs.attributes = NULL_TREE;
31010 /* Keep going until we hit the `;' at the end of the declaration. */
31011 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31013 tree attributes, first_attribute, decl;
31014 cp_declarator *declarator;
31015 cp_token *token;
31017 /* Parse the declarator. */
31018 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31019 NULL, NULL, false, false);
31021 /* Look for attributes that apply to the ivar. */
31022 attributes = cp_parser_attributes_opt (parser);
31023 /* Remember which attributes are prefix attributes and
31024 which are not. */
31025 first_attribute = attributes;
31026 /* Combine the attributes. */
31027 attributes = attr_chainon (prefix_attributes, attributes);
31029 decl = grokfield (declarator, &declspecs,
31030 NULL_TREE, /*init_const_expr_p=*/false,
31031 NULL_TREE, attributes);
31033 if (decl == error_mark_node || decl == NULL_TREE)
31034 return error_mark_node;
31036 /* Reset PREFIX_ATTRIBUTES. */
31037 if (attributes != error_mark_node)
31039 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31040 attributes = TREE_CHAIN (attributes);
31041 if (attributes)
31042 TREE_CHAIN (attributes) = NULL_TREE;
31045 DECL_CHAIN (decl) = decls;
31046 decls = decl;
31048 token = cp_lexer_peek_token (parser->lexer);
31049 if (token->type == CPP_COMMA)
31051 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31052 continue;
31054 else
31055 break;
31057 return decls;
31060 /* Parse an Objective-C @property declaration. The syntax is:
31062 objc-property-declaration:
31063 '@property' objc-property-attributes[opt] struct-declaration ;
31065 objc-property-attributes:
31066 '(' objc-property-attribute-list ')'
31068 objc-property-attribute-list:
31069 objc-property-attribute
31070 objc-property-attribute-list, objc-property-attribute
31072 objc-property-attribute
31073 'getter' = identifier
31074 'setter' = identifier
31075 'readonly'
31076 'readwrite'
31077 'assign'
31078 'retain'
31079 'copy'
31080 'nonatomic'
31082 For example:
31083 @property NSString *name;
31084 @property (readonly) id object;
31085 @property (retain, nonatomic, getter=getTheName) id name;
31086 @property int a, b, c;
31088 PS: This function is identical to
31089 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31090 static void
31091 cp_parser_objc_at_property_declaration (cp_parser *parser)
31093 /* The following variables hold the attributes of the properties as
31094 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31095 seen. When we see an attribute, we set them to 'true' (if they
31096 are boolean properties) or to the identifier (if they have an
31097 argument, ie, for getter and setter). Note that here we only
31098 parse the list of attributes, check the syntax and accumulate the
31099 attributes that we find. objc_add_property_declaration() will
31100 then process the information. */
31101 bool property_assign = false;
31102 bool property_copy = false;
31103 tree property_getter_ident = NULL_TREE;
31104 bool property_nonatomic = false;
31105 bool property_readonly = false;
31106 bool property_readwrite = false;
31107 bool property_retain = false;
31108 tree property_setter_ident = NULL_TREE;
31110 /* 'properties' is the list of properties that we read. Usually a
31111 single one, but maybe more (eg, in "@property int a, b, c;" there
31112 are three). */
31113 tree properties;
31114 location_t loc;
31116 loc = cp_lexer_peek_token (parser->lexer)->location;
31118 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31120 /* Parse the optional attribute list... */
31121 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31123 /* Eat the '('. */
31124 matching_parens parens;
31125 parens.consume_open (parser);
31127 while (true)
31129 bool syntax_error = false;
31130 cp_token *token = cp_lexer_peek_token (parser->lexer);
31131 enum rid keyword;
31133 if (token->type != CPP_NAME)
31135 cp_parser_error (parser, "expected identifier");
31136 break;
31138 keyword = C_RID_CODE (token->u.value);
31139 cp_lexer_consume_token (parser->lexer);
31140 switch (keyword)
31142 case RID_ASSIGN: property_assign = true; break;
31143 case RID_COPY: property_copy = true; break;
31144 case RID_NONATOMIC: property_nonatomic = true; break;
31145 case RID_READONLY: property_readonly = true; break;
31146 case RID_READWRITE: property_readwrite = true; break;
31147 case RID_RETAIN: property_retain = true; break;
31149 case RID_GETTER:
31150 case RID_SETTER:
31151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31153 if (keyword == RID_GETTER)
31154 cp_parser_error (parser,
31155 "missing %<=%> (after %<getter%> attribute)");
31156 else
31157 cp_parser_error (parser,
31158 "missing %<=%> (after %<setter%> attribute)");
31159 syntax_error = true;
31160 break;
31162 cp_lexer_consume_token (parser->lexer); /* eat the = */
31163 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31165 cp_parser_error (parser, "expected identifier");
31166 syntax_error = true;
31167 break;
31169 if (keyword == RID_SETTER)
31171 if (property_setter_ident != NULL_TREE)
31173 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31174 cp_lexer_consume_token (parser->lexer);
31176 else
31177 property_setter_ident = cp_parser_objc_selector (parser);
31178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31179 cp_parser_error (parser, "setter name must terminate with %<:%>");
31180 else
31181 cp_lexer_consume_token (parser->lexer);
31183 else
31185 if (property_getter_ident != NULL_TREE)
31187 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31188 cp_lexer_consume_token (parser->lexer);
31190 else
31191 property_getter_ident = cp_parser_objc_selector (parser);
31193 break;
31194 default:
31195 cp_parser_error (parser, "unknown property attribute");
31196 syntax_error = true;
31197 break;
31200 if (syntax_error)
31201 break;
31203 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31204 cp_lexer_consume_token (parser->lexer);
31205 else
31206 break;
31209 /* FIXME: "@property (setter, assign);" will generate a spurious
31210 "error: expected ‘)’ before ‘,’ token". This is because
31211 cp_parser_require, unlike the C counterpart, will produce an
31212 error even if we are in error recovery. */
31213 if (!parens.require_close (parser))
31215 cp_parser_skip_to_closing_parenthesis (parser,
31216 /*recovering=*/true,
31217 /*or_comma=*/false,
31218 /*consume_paren=*/true);
31222 /* ... and the property declaration(s). */
31223 properties = cp_parser_objc_struct_declaration (parser);
31225 if (properties == error_mark_node)
31227 cp_parser_skip_to_end_of_statement (parser);
31228 /* If the next token is now a `;', consume it. */
31229 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31230 cp_lexer_consume_token (parser->lexer);
31231 return;
31234 if (properties == NULL_TREE)
31235 cp_parser_error (parser, "expected identifier");
31236 else
31238 /* Comma-separated properties are chained together in
31239 reverse order; add them one by one. */
31240 properties = nreverse (properties);
31242 for (; properties; properties = TREE_CHAIN (properties))
31243 objc_add_property_declaration (loc, copy_node (properties),
31244 property_readonly, property_readwrite,
31245 property_assign, property_retain,
31246 property_copy, property_nonatomic,
31247 property_getter_ident, property_setter_ident);
31250 cp_parser_consume_semicolon_at_end_of_statement (parser);
31253 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31255 objc-synthesize-declaration:
31256 @synthesize objc-synthesize-identifier-list ;
31258 objc-synthesize-identifier-list:
31259 objc-synthesize-identifier
31260 objc-synthesize-identifier-list, objc-synthesize-identifier
31262 objc-synthesize-identifier
31263 identifier
31264 identifier = identifier
31266 For example:
31267 @synthesize MyProperty;
31268 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31270 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31271 for C. Keep them in sync.
31273 static void
31274 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31276 tree list = NULL_TREE;
31277 location_t loc;
31278 loc = cp_lexer_peek_token (parser->lexer)->location;
31280 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31281 while (true)
31283 tree property, ivar;
31284 property = cp_parser_identifier (parser);
31285 if (property == error_mark_node)
31287 cp_parser_consume_semicolon_at_end_of_statement (parser);
31288 return;
31290 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31292 cp_lexer_consume_token (parser->lexer);
31293 ivar = cp_parser_identifier (parser);
31294 if (ivar == error_mark_node)
31296 cp_parser_consume_semicolon_at_end_of_statement (parser);
31297 return;
31300 else
31301 ivar = NULL_TREE;
31302 list = chainon (list, build_tree_list (ivar, property));
31303 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31304 cp_lexer_consume_token (parser->lexer);
31305 else
31306 break;
31308 cp_parser_consume_semicolon_at_end_of_statement (parser);
31309 objc_add_synthesize_declaration (loc, list);
31312 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31314 objc-dynamic-declaration:
31315 @dynamic identifier-list ;
31317 For example:
31318 @dynamic MyProperty;
31319 @dynamic MyProperty, AnotherProperty;
31321 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31322 for C. Keep them in sync.
31324 static void
31325 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31327 tree list = NULL_TREE;
31328 location_t loc;
31329 loc = cp_lexer_peek_token (parser->lexer)->location;
31331 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31332 while (true)
31334 tree property;
31335 property = cp_parser_identifier (parser);
31336 if (property == error_mark_node)
31338 cp_parser_consume_semicolon_at_end_of_statement (parser);
31339 return;
31341 list = chainon (list, build_tree_list (NULL, property));
31342 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31343 cp_lexer_consume_token (parser->lexer);
31344 else
31345 break;
31347 cp_parser_consume_semicolon_at_end_of_statement (parser);
31348 objc_add_dynamic_declaration (loc, list);
31352 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31354 /* Returns name of the next clause.
31355 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31356 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31357 returned and the token is consumed. */
31359 static pragma_omp_clause
31360 cp_parser_omp_clause_name (cp_parser *parser)
31362 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31365 result = PRAGMA_OACC_CLAUSE_AUTO;
31366 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31367 result = PRAGMA_OMP_CLAUSE_IF;
31368 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31369 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31370 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31371 result = PRAGMA_OACC_CLAUSE_DELETE;
31372 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31373 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31374 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31375 result = PRAGMA_OMP_CLAUSE_FOR;
31376 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31378 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31379 const char *p = IDENTIFIER_POINTER (id);
31381 switch (p[0])
31383 case 'a':
31384 if (!strcmp ("aligned", p))
31385 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31386 else if (!strcmp ("async", p))
31387 result = PRAGMA_OACC_CLAUSE_ASYNC;
31388 break;
31389 case 'c':
31390 if (!strcmp ("collapse", p))
31391 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31392 else if (!strcmp ("copy", p))
31393 result = PRAGMA_OACC_CLAUSE_COPY;
31394 else if (!strcmp ("copyin", p))
31395 result = PRAGMA_OMP_CLAUSE_COPYIN;
31396 else if (!strcmp ("copyout", p))
31397 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31398 else if (!strcmp ("copyprivate", p))
31399 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31400 else if (!strcmp ("create", p))
31401 result = PRAGMA_OACC_CLAUSE_CREATE;
31402 break;
31403 case 'd':
31404 if (!strcmp ("defaultmap", p))
31405 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31406 else if (!strcmp ("depend", p))
31407 result = PRAGMA_OMP_CLAUSE_DEPEND;
31408 else if (!strcmp ("device", p))
31409 result = PRAGMA_OMP_CLAUSE_DEVICE;
31410 else if (!strcmp ("deviceptr", p))
31411 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31412 else if (!strcmp ("device_resident", p))
31413 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31414 else if (!strcmp ("dist_schedule", p))
31415 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31416 break;
31417 case 'f':
31418 if (!strcmp ("final", p))
31419 result = PRAGMA_OMP_CLAUSE_FINAL;
31420 else if (!strcmp ("finalize", p))
31421 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31422 else if (!strcmp ("firstprivate", p))
31423 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31424 else if (!strcmp ("from", p))
31425 result = PRAGMA_OMP_CLAUSE_FROM;
31426 break;
31427 case 'g':
31428 if (!strcmp ("gang", p))
31429 result = PRAGMA_OACC_CLAUSE_GANG;
31430 else if (!strcmp ("grainsize", p))
31431 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31432 break;
31433 case 'h':
31434 if (!strcmp ("hint", p))
31435 result = PRAGMA_OMP_CLAUSE_HINT;
31436 else if (!strcmp ("host", p))
31437 result = PRAGMA_OACC_CLAUSE_HOST;
31438 break;
31439 case 'i':
31440 if (!strcmp ("if_present", p))
31441 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31442 else if (!strcmp ("inbranch", p))
31443 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31444 else if (!strcmp ("independent", p))
31445 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31446 else if (!strcmp ("is_device_ptr", p))
31447 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31448 break;
31449 case 'l':
31450 if (!strcmp ("lastprivate", p))
31451 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31452 else if (!strcmp ("linear", p))
31453 result = PRAGMA_OMP_CLAUSE_LINEAR;
31454 else if (!strcmp ("link", p))
31455 result = PRAGMA_OMP_CLAUSE_LINK;
31456 break;
31457 case 'm':
31458 if (!strcmp ("map", p))
31459 result = PRAGMA_OMP_CLAUSE_MAP;
31460 else if (!strcmp ("mergeable", p))
31461 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31462 break;
31463 case 'n':
31464 if (!strcmp ("nogroup", p))
31465 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31466 else if (!strcmp ("notinbranch", p))
31467 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31468 else if (!strcmp ("nowait", p))
31469 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31470 else if (!strcmp ("num_gangs", p))
31471 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31472 else if (!strcmp ("num_tasks", p))
31473 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31474 else if (!strcmp ("num_teams", p))
31475 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31476 else if (!strcmp ("num_threads", p))
31477 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31478 else if (!strcmp ("num_workers", p))
31479 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31480 break;
31481 case 'o':
31482 if (!strcmp ("ordered", p))
31483 result = PRAGMA_OMP_CLAUSE_ORDERED;
31484 break;
31485 case 'p':
31486 if (!strcmp ("parallel", p))
31487 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31488 else if (!strcmp ("present", p))
31489 result = PRAGMA_OACC_CLAUSE_PRESENT;
31490 else if (!strcmp ("present_or_copy", p)
31491 || !strcmp ("pcopy", p))
31492 result = PRAGMA_OACC_CLAUSE_COPY;
31493 else if (!strcmp ("present_or_copyin", p)
31494 || !strcmp ("pcopyin", p))
31495 result = PRAGMA_OACC_CLAUSE_COPYIN;
31496 else if (!strcmp ("present_or_copyout", p)
31497 || !strcmp ("pcopyout", p))
31498 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31499 else if (!strcmp ("present_or_create", p)
31500 || !strcmp ("pcreate", p))
31501 result = PRAGMA_OACC_CLAUSE_CREATE;
31502 else if (!strcmp ("priority", p))
31503 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31504 else if (!strcmp ("proc_bind", p))
31505 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31506 break;
31507 case 'r':
31508 if (!strcmp ("reduction", p))
31509 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31510 break;
31511 case 's':
31512 if (!strcmp ("safelen", p))
31513 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31514 else if (!strcmp ("schedule", p))
31515 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31516 else if (!strcmp ("sections", p))
31517 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31518 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31519 result = PRAGMA_OACC_CLAUSE_HOST;
31520 else if (!strcmp ("seq", p))
31521 result = PRAGMA_OACC_CLAUSE_SEQ;
31522 else if (!strcmp ("shared", p))
31523 result = PRAGMA_OMP_CLAUSE_SHARED;
31524 else if (!strcmp ("simd", p))
31525 result = PRAGMA_OMP_CLAUSE_SIMD;
31526 else if (!strcmp ("simdlen", p))
31527 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31528 break;
31529 case 't':
31530 if (!strcmp ("taskgroup", p))
31531 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31532 else if (!strcmp ("thread_limit", p))
31533 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31534 else if (!strcmp ("threads", p))
31535 result = PRAGMA_OMP_CLAUSE_THREADS;
31536 else if (!strcmp ("tile", p))
31537 result = PRAGMA_OACC_CLAUSE_TILE;
31538 else if (!strcmp ("to", p))
31539 result = PRAGMA_OMP_CLAUSE_TO;
31540 break;
31541 case 'u':
31542 if (!strcmp ("uniform", p))
31543 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31544 else if (!strcmp ("untied", p))
31545 result = PRAGMA_OMP_CLAUSE_UNTIED;
31546 else if (!strcmp ("use_device", p))
31547 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31548 else if (!strcmp ("use_device_ptr", p))
31549 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31550 break;
31551 case 'v':
31552 if (!strcmp ("vector", p))
31553 result = PRAGMA_OACC_CLAUSE_VECTOR;
31554 else if (!strcmp ("vector_length", p))
31555 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31556 break;
31557 case 'w':
31558 if (!strcmp ("wait", p))
31559 result = PRAGMA_OACC_CLAUSE_WAIT;
31560 else if (!strcmp ("worker", p))
31561 result = PRAGMA_OACC_CLAUSE_WORKER;
31562 break;
31566 if (result != PRAGMA_OMP_CLAUSE_NONE)
31567 cp_lexer_consume_token (parser->lexer);
31569 return result;
31572 /* Validate that a clause of the given type does not already exist. */
31574 static void
31575 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31576 const char *name, location_t location)
31578 tree c;
31580 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31581 if (OMP_CLAUSE_CODE (c) == code)
31583 error_at (location, "too many %qs clauses", name);
31584 break;
31588 /* OpenMP 2.5:
31589 variable-list:
31590 identifier
31591 variable-list , identifier
31593 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31594 colon). An opening parenthesis will have been consumed by the caller.
31596 If KIND is nonzero, create the appropriate node and install the decl
31597 in OMP_CLAUSE_DECL and add the node to the head of the list.
31599 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31600 return the list created.
31602 COLON can be NULL if only closing parenthesis should end the list,
31603 or pointer to bool which will receive false if the list is terminated
31604 by closing parenthesis or true if the list is terminated by colon. */
31606 static tree
31607 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31608 tree list, bool *colon)
31610 cp_token *token;
31611 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31612 if (colon)
31614 parser->colon_corrects_to_scope_p = false;
31615 *colon = false;
31617 while (1)
31619 tree name, decl;
31621 token = cp_lexer_peek_token (parser->lexer);
31622 if (kind != 0
31623 && current_class_ptr
31624 && cp_parser_is_keyword (token, RID_THIS))
31626 decl = finish_this_expr ();
31627 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31628 || CONVERT_EXPR_P (decl))
31629 decl = TREE_OPERAND (decl, 0);
31630 cp_lexer_consume_token (parser->lexer);
31632 else
31634 name = cp_parser_id_expression (parser, /*template_p=*/false,
31635 /*check_dependency_p=*/true,
31636 /*template_p=*/NULL,
31637 /*declarator_p=*/false,
31638 /*optional_p=*/false);
31639 if (name == error_mark_node)
31640 goto skip_comma;
31642 if (identifier_p (name))
31643 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31644 else
31645 decl = name;
31646 if (decl == error_mark_node)
31647 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31648 token->location);
31650 if (decl == error_mark_node)
31652 else if (kind != 0)
31654 switch (kind)
31656 case OMP_CLAUSE__CACHE_:
31657 /* The OpenACC cache directive explicitly only allows "array
31658 elements or subarrays". */
31659 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31661 error_at (token->location, "expected %<[%>");
31662 decl = error_mark_node;
31663 break;
31665 /* FALLTHROUGH. */
31666 case OMP_CLAUSE_MAP:
31667 case OMP_CLAUSE_FROM:
31668 case OMP_CLAUSE_TO:
31669 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31671 location_t loc
31672 = cp_lexer_peek_token (parser->lexer)->location;
31673 cp_id_kind idk = CP_ID_KIND_NONE;
31674 cp_lexer_consume_token (parser->lexer);
31675 decl = convert_from_reference (decl);
31676 decl
31677 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31678 decl, false,
31679 &idk, loc);
31681 /* FALLTHROUGH. */
31682 case OMP_CLAUSE_DEPEND:
31683 case OMP_CLAUSE_REDUCTION:
31684 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31686 tree low_bound = NULL_TREE, length = NULL_TREE;
31688 parser->colon_corrects_to_scope_p = false;
31689 cp_lexer_consume_token (parser->lexer);
31690 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31691 low_bound = cp_parser_expression (parser);
31692 if (!colon)
31693 parser->colon_corrects_to_scope_p
31694 = saved_colon_corrects_to_scope_p;
31695 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31696 length = integer_one_node;
31697 else
31699 /* Look for `:'. */
31700 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31701 goto skip_comma;
31702 if (!cp_lexer_next_token_is (parser->lexer,
31703 CPP_CLOSE_SQUARE))
31704 length = cp_parser_expression (parser);
31706 /* Look for the closing `]'. */
31707 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31708 RT_CLOSE_SQUARE))
31709 goto skip_comma;
31711 decl = tree_cons (low_bound, length, decl);
31713 break;
31714 default:
31715 break;
31718 tree u = build_omp_clause (token->location, kind);
31719 OMP_CLAUSE_DECL (u) = decl;
31720 OMP_CLAUSE_CHAIN (u) = list;
31721 list = u;
31723 else
31724 list = tree_cons (decl, NULL_TREE, list);
31726 get_comma:
31727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31728 break;
31729 cp_lexer_consume_token (parser->lexer);
31732 if (colon)
31733 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31735 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31737 *colon = true;
31738 cp_parser_require (parser, CPP_COLON, RT_COLON);
31739 return list;
31742 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31744 int ending;
31746 /* Try to resync to an unnested comma. Copied from
31747 cp_parser_parenthesized_expression_list. */
31748 skip_comma:
31749 if (colon)
31750 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31751 ending = cp_parser_skip_to_closing_parenthesis (parser,
31752 /*recovering=*/true,
31753 /*or_comma=*/true,
31754 /*consume_paren=*/true);
31755 if (ending < 0)
31756 goto get_comma;
31759 return list;
31762 /* Similarly, but expect leading and trailing parenthesis. This is a very
31763 common case for omp clauses. */
31765 static tree
31766 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31768 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31769 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31770 return list;
31773 /* OpenACC 2.0:
31774 copy ( variable-list )
31775 copyin ( variable-list )
31776 copyout ( variable-list )
31777 create ( variable-list )
31778 delete ( variable-list )
31779 present ( variable-list ) */
31781 static tree
31782 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31783 tree list)
31785 enum gomp_map_kind kind;
31786 switch (c_kind)
31788 case PRAGMA_OACC_CLAUSE_COPY:
31789 kind = GOMP_MAP_TOFROM;
31790 break;
31791 case PRAGMA_OACC_CLAUSE_COPYIN:
31792 kind = GOMP_MAP_TO;
31793 break;
31794 case PRAGMA_OACC_CLAUSE_COPYOUT:
31795 kind = GOMP_MAP_FROM;
31796 break;
31797 case PRAGMA_OACC_CLAUSE_CREATE:
31798 kind = GOMP_MAP_ALLOC;
31799 break;
31800 case PRAGMA_OACC_CLAUSE_DELETE:
31801 kind = GOMP_MAP_RELEASE;
31802 break;
31803 case PRAGMA_OACC_CLAUSE_DEVICE:
31804 kind = GOMP_MAP_FORCE_TO;
31805 break;
31806 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31807 kind = GOMP_MAP_DEVICE_RESIDENT;
31808 break;
31809 case PRAGMA_OACC_CLAUSE_HOST:
31810 kind = GOMP_MAP_FORCE_FROM;
31811 break;
31812 case PRAGMA_OACC_CLAUSE_LINK:
31813 kind = GOMP_MAP_LINK;
31814 break;
31815 case PRAGMA_OACC_CLAUSE_PRESENT:
31816 kind = GOMP_MAP_FORCE_PRESENT;
31817 break;
31818 default:
31819 gcc_unreachable ();
31821 tree nl, c;
31822 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31824 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31825 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31827 return nl;
31830 /* OpenACC 2.0:
31831 deviceptr ( variable-list ) */
31833 static tree
31834 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31836 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31837 tree vars, t;
31839 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31840 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31841 variable-list must only allow for pointer variables. */
31842 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31843 for (t = vars; t; t = TREE_CHAIN (t))
31845 tree v = TREE_PURPOSE (t);
31846 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31847 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31848 OMP_CLAUSE_DECL (u) = v;
31849 OMP_CLAUSE_CHAIN (u) = list;
31850 list = u;
31853 return list;
31856 /* OpenACC 2.5:
31857 auto
31858 finalize
31859 independent
31860 nohost
31861 seq */
31863 static tree
31864 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31865 enum omp_clause_code code,
31866 tree list, location_t location)
31868 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31869 tree c = build_omp_clause (location, code);
31870 OMP_CLAUSE_CHAIN (c) = list;
31871 return c;
31874 /* OpenACC:
31875 num_gangs ( expression )
31876 num_workers ( expression )
31877 vector_length ( expression ) */
31879 static tree
31880 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31881 const char *str, tree list)
31883 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31885 matching_parens parens;
31886 if (!parens.require_open (parser))
31887 return list;
31889 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31891 if (t == error_mark_node
31892 || !parens.require_close (parser))
31894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31895 /*or_comma=*/false,
31896 /*consume_paren=*/true);
31897 return list;
31900 check_no_duplicate_clause (list, code, str, loc);
31902 tree c = build_omp_clause (loc, code);
31903 OMP_CLAUSE_OPERAND (c, 0) = t;
31904 OMP_CLAUSE_CHAIN (c) = list;
31905 return c;
31908 /* OpenACC:
31910 gang [( gang-arg-list )]
31911 worker [( [num:] int-expr )]
31912 vector [( [length:] int-expr )]
31914 where gang-arg is one of:
31916 [num:] int-expr
31917 static: size-expr
31919 and size-expr may be:
31922 int-expr
31925 static tree
31926 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31927 const char *str, tree list)
31929 const char *id = "num";
31930 cp_lexer *lexer = parser->lexer;
31931 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31932 location_t loc = cp_lexer_peek_token (lexer)->location;
31934 if (kind == OMP_CLAUSE_VECTOR)
31935 id = "length";
31937 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31939 matching_parens parens;
31940 parens.consume_open (parser);
31944 cp_token *next = cp_lexer_peek_token (lexer);
31945 int idx = 0;
31947 /* Gang static argument. */
31948 if (kind == OMP_CLAUSE_GANG
31949 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31951 cp_lexer_consume_token (lexer);
31953 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31954 goto cleanup_error;
31956 idx = 1;
31957 if (ops[idx] != NULL)
31959 cp_parser_error (parser, "too many %<static%> arguments");
31960 goto cleanup_error;
31963 /* Check for the '*' argument. */
31964 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31965 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31966 || cp_lexer_nth_token_is (parser->lexer, 2,
31967 CPP_CLOSE_PAREN)))
31969 cp_lexer_consume_token (lexer);
31970 ops[idx] = integer_minus_one_node;
31972 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31974 cp_lexer_consume_token (lexer);
31975 continue;
31977 else break;
31980 /* Worker num: argument and vector length: arguments. */
31981 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31982 && id_equal (next->u.value, id)
31983 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31985 cp_lexer_consume_token (lexer); /* id */
31986 cp_lexer_consume_token (lexer); /* ':' */
31989 /* Now collect the actual argument. */
31990 if (ops[idx] != NULL_TREE)
31992 cp_parser_error (parser, "unexpected argument");
31993 goto cleanup_error;
31996 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31997 false);
31998 if (expr == error_mark_node)
31999 goto cleanup_error;
32001 mark_exp_read (expr);
32002 ops[idx] = expr;
32004 if (kind == OMP_CLAUSE_GANG
32005 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32007 cp_lexer_consume_token (lexer);
32008 continue;
32010 break;
32012 while (1);
32014 if (!parens.require_close (parser))
32015 goto cleanup_error;
32018 check_no_duplicate_clause (list, kind, str, loc);
32020 c = build_omp_clause (loc, kind);
32022 if (ops[1])
32023 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32025 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32026 OMP_CLAUSE_CHAIN (c) = list;
32028 return c;
32030 cleanup_error:
32031 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32032 return list;
32035 /* OpenACC 2.0:
32036 tile ( size-expr-list ) */
32038 static tree
32039 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32041 tree c, expr = error_mark_node;
32042 tree tile = NULL_TREE;
32044 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32045 so, but the spec authors never considered such a case and have
32046 differing opinions on what it might mean, including 'not
32047 allowed'.) */
32048 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32049 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32050 clause_loc);
32052 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32053 return list;
32057 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32058 return list;
32060 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32061 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32062 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32064 cp_lexer_consume_token (parser->lexer);
32065 expr = integer_zero_node;
32067 else
32068 expr = cp_parser_constant_expression (parser);
32070 tile = tree_cons (NULL_TREE, expr, tile);
32072 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32074 /* Consume the trailing ')'. */
32075 cp_lexer_consume_token (parser->lexer);
32077 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32078 tile = nreverse (tile);
32079 OMP_CLAUSE_TILE_LIST (c) = tile;
32080 OMP_CLAUSE_CHAIN (c) = list;
32081 return c;
32084 /* OpenACC 2.0
32085 Parse wait clause or directive parameters. */
32087 static tree
32088 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32090 vec<tree, va_gc> *args;
32091 tree t, args_tree;
32093 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32094 /*cast_p=*/false,
32095 /*allow_expansion_p=*/true,
32096 /*non_constant_p=*/NULL);
32098 if (args == NULL || args->length () == 0)
32100 cp_parser_error (parser, "expected integer expression before ')'");
32101 if (args != NULL)
32102 release_tree_vector (args);
32103 return list;
32106 args_tree = build_tree_list_vec (args);
32108 release_tree_vector (args);
32110 for (t = args_tree; t; t = TREE_CHAIN (t))
32112 tree targ = TREE_VALUE (t);
32114 if (targ != error_mark_node)
32116 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32117 error ("%<wait%> expression must be integral");
32118 else
32120 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32122 targ = mark_rvalue_use (targ);
32123 OMP_CLAUSE_DECL (c) = targ;
32124 OMP_CLAUSE_CHAIN (c) = list;
32125 list = c;
32130 return list;
32133 /* OpenACC:
32134 wait ( int-expr-list ) */
32136 static tree
32137 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32139 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32141 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32142 return list;
32144 list = cp_parser_oacc_wait_list (parser, location, list);
32146 return list;
32149 /* OpenMP 3.0:
32150 collapse ( constant-expression ) */
32152 static tree
32153 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32155 tree c, num;
32156 location_t loc;
32157 HOST_WIDE_INT n;
32159 loc = cp_lexer_peek_token (parser->lexer)->location;
32160 matching_parens parens;
32161 if (!parens.require_open (parser))
32162 return list;
32164 num = cp_parser_constant_expression (parser);
32166 if (!parens.require_close (parser))
32167 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32168 /*or_comma=*/false,
32169 /*consume_paren=*/true);
32171 if (num == error_mark_node)
32172 return list;
32173 num = fold_non_dependent_expr (num);
32174 if (!tree_fits_shwi_p (num)
32175 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32176 || (n = tree_to_shwi (num)) <= 0
32177 || (int) n != n)
32179 error_at (loc, "collapse argument needs positive constant integer expression");
32180 return list;
32183 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32184 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32185 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32186 OMP_CLAUSE_CHAIN (c) = list;
32187 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32189 return c;
32192 /* OpenMP 2.5:
32193 default ( none | shared )
32195 OpenACC:
32196 default ( none | present ) */
32198 static tree
32199 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32200 location_t location, bool is_oacc)
32202 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32203 tree c;
32205 matching_parens parens;
32206 if (!parens.require_open (parser))
32207 return list;
32208 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32210 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32211 const char *p = IDENTIFIER_POINTER (id);
32213 switch (p[0])
32215 case 'n':
32216 if (strcmp ("none", p) != 0)
32217 goto invalid_kind;
32218 kind = OMP_CLAUSE_DEFAULT_NONE;
32219 break;
32221 case 'p':
32222 if (strcmp ("present", p) != 0 || !is_oacc)
32223 goto invalid_kind;
32224 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32225 break;
32227 case 's':
32228 if (strcmp ("shared", p) != 0 || is_oacc)
32229 goto invalid_kind;
32230 kind = OMP_CLAUSE_DEFAULT_SHARED;
32231 break;
32233 default:
32234 goto invalid_kind;
32237 cp_lexer_consume_token (parser->lexer);
32239 else
32241 invalid_kind:
32242 if (is_oacc)
32243 cp_parser_error (parser, "expected %<none%> or %<present%>");
32244 else
32245 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32248 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32249 || !parens.require_close (parser))
32250 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32251 /*or_comma=*/false,
32252 /*consume_paren=*/true);
32254 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32255 return list;
32257 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32258 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32259 OMP_CLAUSE_CHAIN (c) = list;
32260 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32262 return c;
32265 /* OpenMP 3.1:
32266 final ( expression ) */
32268 static tree
32269 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32271 tree t, c;
32273 matching_parens parens;
32274 if (!parens.require_open (parser))
32275 return list;
32277 t = cp_parser_condition (parser);
32279 if (t == error_mark_node
32280 || !parens.require_close (parser))
32281 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32282 /*or_comma=*/false,
32283 /*consume_paren=*/true);
32285 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32287 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32288 OMP_CLAUSE_FINAL_EXPR (c) = t;
32289 OMP_CLAUSE_CHAIN (c) = list;
32291 return c;
32294 /* OpenMP 2.5:
32295 if ( expression )
32297 OpenMP 4.5:
32298 if ( directive-name-modifier : expression )
32300 directive-name-modifier:
32301 parallel | task | taskloop | target data | target | target update
32302 | target enter data | target exit data */
32304 static tree
32305 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32306 bool is_omp)
32308 tree t, c;
32309 enum tree_code if_modifier = ERROR_MARK;
32311 matching_parens parens;
32312 if (!parens.require_open (parser))
32313 return list;
32315 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32318 const char *p = IDENTIFIER_POINTER (id);
32319 int n = 2;
32321 if (strcmp ("parallel", p) == 0)
32322 if_modifier = OMP_PARALLEL;
32323 else if (strcmp ("task", p) == 0)
32324 if_modifier = OMP_TASK;
32325 else if (strcmp ("taskloop", p) == 0)
32326 if_modifier = OMP_TASKLOOP;
32327 else if (strcmp ("target", p) == 0)
32329 if_modifier = OMP_TARGET;
32330 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32332 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32333 p = IDENTIFIER_POINTER (id);
32334 if (strcmp ("data", p) == 0)
32335 if_modifier = OMP_TARGET_DATA;
32336 else if (strcmp ("update", p) == 0)
32337 if_modifier = OMP_TARGET_UPDATE;
32338 else if (strcmp ("enter", p) == 0)
32339 if_modifier = OMP_TARGET_ENTER_DATA;
32340 else if (strcmp ("exit", p) == 0)
32341 if_modifier = OMP_TARGET_EXIT_DATA;
32342 if (if_modifier != OMP_TARGET)
32343 n = 3;
32344 else
32346 location_t loc
32347 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32348 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32349 "or %<exit%>");
32350 if_modifier = ERROR_MARK;
32352 if (if_modifier == OMP_TARGET_ENTER_DATA
32353 || if_modifier == OMP_TARGET_EXIT_DATA)
32355 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32357 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32358 p = IDENTIFIER_POINTER (id);
32359 if (strcmp ("data", p) == 0)
32360 n = 4;
32362 if (n != 4)
32364 location_t loc
32365 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32366 error_at (loc, "expected %<data%>");
32367 if_modifier = ERROR_MARK;
32372 if (if_modifier != ERROR_MARK)
32374 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32376 while (n-- > 0)
32377 cp_lexer_consume_token (parser->lexer);
32379 else
32381 if (n > 2)
32383 location_t loc
32384 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32385 error_at (loc, "expected %<:%>");
32387 if_modifier = ERROR_MARK;
32392 t = cp_parser_condition (parser);
32394 if (t == error_mark_node
32395 || !parens.require_close (parser))
32396 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32397 /*or_comma=*/false,
32398 /*consume_paren=*/true);
32400 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32401 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32403 if (if_modifier != ERROR_MARK
32404 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32406 const char *p = NULL;
32407 switch (if_modifier)
32409 case OMP_PARALLEL: p = "parallel"; break;
32410 case OMP_TASK: p = "task"; break;
32411 case OMP_TASKLOOP: p = "taskloop"; break;
32412 case OMP_TARGET_DATA: p = "target data"; break;
32413 case OMP_TARGET: p = "target"; break;
32414 case OMP_TARGET_UPDATE: p = "target update"; break;
32415 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32416 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32417 default: gcc_unreachable ();
32419 error_at (location, "too many %<if%> clauses with %qs modifier",
32421 return list;
32423 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32425 if (!is_omp)
32426 error_at (location, "too many %<if%> clauses");
32427 else
32428 error_at (location, "too many %<if%> clauses without modifier");
32429 return list;
32431 else if (if_modifier == ERROR_MARK
32432 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32434 error_at (location, "if any %<if%> clause has modifier, then all "
32435 "%<if%> clauses have to use modifier");
32436 return list;
32440 c = build_omp_clause (location, OMP_CLAUSE_IF);
32441 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32442 OMP_CLAUSE_IF_EXPR (c) = t;
32443 OMP_CLAUSE_CHAIN (c) = list;
32445 return c;
32448 /* OpenMP 3.1:
32449 mergeable */
32451 static tree
32452 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32453 tree list, location_t location)
32455 tree c;
32457 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32458 location);
32460 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32461 OMP_CLAUSE_CHAIN (c) = list;
32462 return c;
32465 /* OpenMP 2.5:
32466 nowait */
32468 static tree
32469 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32470 tree list, location_t location)
32472 tree c;
32474 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32476 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32477 OMP_CLAUSE_CHAIN (c) = list;
32478 return c;
32481 /* OpenMP 2.5:
32482 num_threads ( expression ) */
32484 static tree
32485 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32486 location_t location)
32488 tree t, c;
32490 matching_parens parens;
32491 if (!parens.require_open (parser))
32492 return list;
32494 t = cp_parser_expression (parser);
32496 if (t == error_mark_node
32497 || !parens.require_close (parser))
32498 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32499 /*or_comma=*/false,
32500 /*consume_paren=*/true);
32502 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32503 "num_threads", location);
32505 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32506 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32507 OMP_CLAUSE_CHAIN (c) = list;
32509 return c;
32512 /* OpenMP 4.5:
32513 num_tasks ( expression ) */
32515 static tree
32516 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32517 location_t location)
32519 tree t, c;
32521 matching_parens parens;
32522 if (!parens.require_open (parser))
32523 return list;
32525 t = cp_parser_expression (parser);
32527 if (t == error_mark_node
32528 || !parens.require_close (parser))
32529 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32530 /*or_comma=*/false,
32531 /*consume_paren=*/true);
32533 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32534 "num_tasks", location);
32536 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32537 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32538 OMP_CLAUSE_CHAIN (c) = list;
32540 return c;
32543 /* OpenMP 4.5:
32544 grainsize ( expression ) */
32546 static tree
32547 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32548 location_t location)
32550 tree t, c;
32552 matching_parens parens;
32553 if (!parens.require_open (parser))
32554 return list;
32556 t = cp_parser_expression (parser);
32558 if (t == error_mark_node
32559 || !parens.require_close (parser))
32560 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32561 /*or_comma=*/false,
32562 /*consume_paren=*/true);
32564 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32565 "grainsize", location);
32567 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32568 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32569 OMP_CLAUSE_CHAIN (c) = list;
32571 return c;
32574 /* OpenMP 4.5:
32575 priority ( expression ) */
32577 static tree
32578 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32579 location_t location)
32581 tree t, c;
32583 matching_parens parens;
32584 if (!parens.require_open (parser))
32585 return list;
32587 t = cp_parser_expression (parser);
32589 if (t == error_mark_node
32590 || !parens.require_close (parser))
32591 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32592 /*or_comma=*/false,
32593 /*consume_paren=*/true);
32595 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32596 "priority", location);
32598 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32599 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32600 OMP_CLAUSE_CHAIN (c) = list;
32602 return c;
32605 /* OpenMP 4.5:
32606 hint ( expression ) */
32608 static tree
32609 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32610 location_t location)
32612 tree t, c;
32614 matching_parens parens;
32615 if (!parens.require_open (parser))
32616 return list;
32618 t = cp_parser_expression (parser);
32620 if (t == error_mark_node
32621 || !parens.require_close (parser))
32622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32623 /*or_comma=*/false,
32624 /*consume_paren=*/true);
32626 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32628 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32629 OMP_CLAUSE_HINT_EXPR (c) = t;
32630 OMP_CLAUSE_CHAIN (c) = list;
32632 return c;
32635 /* OpenMP 4.5:
32636 defaultmap ( tofrom : scalar ) */
32638 static tree
32639 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32640 location_t location)
32642 tree c, id;
32643 const char *p;
32645 matching_parens parens;
32646 if (!parens.require_open (parser))
32647 return list;
32649 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32651 cp_parser_error (parser, "expected %<tofrom%>");
32652 goto out_err;
32654 id = cp_lexer_peek_token (parser->lexer)->u.value;
32655 p = IDENTIFIER_POINTER (id);
32656 if (strcmp (p, "tofrom") != 0)
32658 cp_parser_error (parser, "expected %<tofrom%>");
32659 goto out_err;
32661 cp_lexer_consume_token (parser->lexer);
32662 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32663 goto out_err;
32665 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32667 cp_parser_error (parser, "expected %<scalar%>");
32668 goto out_err;
32670 id = cp_lexer_peek_token (parser->lexer)->u.value;
32671 p = IDENTIFIER_POINTER (id);
32672 if (strcmp (p, "scalar") != 0)
32674 cp_parser_error (parser, "expected %<scalar%>");
32675 goto out_err;
32677 cp_lexer_consume_token (parser->lexer);
32678 if (!parens.require_close (parser))
32679 goto out_err;
32681 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32682 location);
32684 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32685 OMP_CLAUSE_CHAIN (c) = list;
32686 return c;
32688 out_err:
32689 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32690 /*or_comma=*/false,
32691 /*consume_paren=*/true);
32692 return list;
32695 /* OpenMP 2.5:
32696 ordered
32698 OpenMP 4.5:
32699 ordered ( constant-expression ) */
32701 static tree
32702 cp_parser_omp_clause_ordered (cp_parser *parser,
32703 tree list, location_t location)
32705 tree c, num = NULL_TREE;
32706 HOST_WIDE_INT n;
32708 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32709 "ordered", location);
32711 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32713 matching_parens parens;
32714 parens.consume_open (parser);
32716 num = cp_parser_constant_expression (parser);
32718 if (!parens.require_close (parser))
32719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32720 /*or_comma=*/false,
32721 /*consume_paren=*/true);
32723 if (num == error_mark_node)
32724 return list;
32725 num = fold_non_dependent_expr (num);
32726 if (!tree_fits_shwi_p (num)
32727 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32728 || (n = tree_to_shwi (num)) <= 0
32729 || (int) n != n)
32731 error_at (location,
32732 "ordered argument needs positive constant integer "
32733 "expression");
32734 return list;
32738 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32739 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32740 OMP_CLAUSE_CHAIN (c) = list;
32741 return c;
32744 /* OpenMP 2.5:
32745 reduction ( reduction-operator : variable-list )
32747 reduction-operator:
32748 One of: + * - & ^ | && ||
32750 OpenMP 3.1:
32752 reduction-operator:
32753 One of: + * - & ^ | && || min max
32755 OpenMP 4.0:
32757 reduction-operator:
32758 One of: + * - & ^ | && ||
32759 id-expression */
32761 static tree
32762 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32764 enum tree_code code = ERROR_MARK;
32765 tree nlist, c, id = NULL_TREE;
32767 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32768 return list;
32770 switch (cp_lexer_peek_token (parser->lexer)->type)
32772 case CPP_PLUS: code = PLUS_EXPR; break;
32773 case CPP_MULT: code = MULT_EXPR; break;
32774 case CPP_MINUS: code = MINUS_EXPR; break;
32775 case CPP_AND: code = BIT_AND_EXPR; break;
32776 case CPP_XOR: code = BIT_XOR_EXPR; break;
32777 case CPP_OR: code = BIT_IOR_EXPR; break;
32778 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32779 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32780 default: break;
32783 if (code != ERROR_MARK)
32784 cp_lexer_consume_token (parser->lexer);
32785 else
32787 bool saved_colon_corrects_to_scope_p;
32788 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32789 parser->colon_corrects_to_scope_p = false;
32790 id = cp_parser_id_expression (parser, /*template_p=*/false,
32791 /*check_dependency_p=*/true,
32792 /*template_p=*/NULL,
32793 /*declarator_p=*/false,
32794 /*optional_p=*/false);
32795 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32796 if (identifier_p (id))
32798 const char *p = IDENTIFIER_POINTER (id);
32800 if (strcmp (p, "min") == 0)
32801 code = MIN_EXPR;
32802 else if (strcmp (p, "max") == 0)
32803 code = MAX_EXPR;
32804 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32805 code = PLUS_EXPR;
32806 else if (id == ovl_op_identifier (false, MULT_EXPR))
32807 code = MULT_EXPR;
32808 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32809 code = MINUS_EXPR;
32810 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32811 code = BIT_AND_EXPR;
32812 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32813 code = BIT_IOR_EXPR;
32814 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32815 code = BIT_XOR_EXPR;
32816 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32817 code = TRUTH_ANDIF_EXPR;
32818 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32819 code = TRUTH_ORIF_EXPR;
32820 id = omp_reduction_id (code, id, NULL_TREE);
32821 tree scope = parser->scope;
32822 if (scope)
32823 id = build_qualified_name (NULL_TREE, scope, id, false);
32824 parser->scope = NULL_TREE;
32825 parser->qualifying_scope = NULL_TREE;
32826 parser->object_scope = NULL_TREE;
32828 else
32830 error ("invalid reduction-identifier");
32831 resync_fail:
32832 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32833 /*or_comma=*/false,
32834 /*consume_paren=*/true);
32835 return list;
32839 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32840 goto resync_fail;
32842 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32843 NULL);
32844 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32846 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32847 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32850 return nlist;
32853 /* OpenMP 2.5:
32854 schedule ( schedule-kind )
32855 schedule ( schedule-kind , expression )
32857 schedule-kind:
32858 static | dynamic | guided | runtime | auto
32860 OpenMP 4.5:
32861 schedule ( schedule-modifier : schedule-kind )
32862 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32864 schedule-modifier:
32865 simd
32866 monotonic
32867 nonmonotonic */
32869 static tree
32870 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32872 tree c, t;
32873 int modifiers = 0, nmodifiers = 0;
32875 matching_parens parens;
32876 if (!parens.require_open (parser))
32877 return list;
32879 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32881 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32883 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32884 const char *p = IDENTIFIER_POINTER (id);
32885 if (strcmp ("simd", p) == 0)
32886 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32887 else if (strcmp ("monotonic", p) == 0)
32888 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32889 else if (strcmp ("nonmonotonic", p) == 0)
32890 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32891 else
32892 break;
32893 cp_lexer_consume_token (parser->lexer);
32894 if (nmodifiers++ == 0
32895 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32896 cp_lexer_consume_token (parser->lexer);
32897 else
32899 cp_parser_require (parser, CPP_COLON, RT_COLON);
32900 break;
32904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32906 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32907 const char *p = IDENTIFIER_POINTER (id);
32909 switch (p[0])
32911 case 'd':
32912 if (strcmp ("dynamic", p) != 0)
32913 goto invalid_kind;
32914 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32915 break;
32917 case 'g':
32918 if (strcmp ("guided", p) != 0)
32919 goto invalid_kind;
32920 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32921 break;
32923 case 'r':
32924 if (strcmp ("runtime", p) != 0)
32925 goto invalid_kind;
32926 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32927 break;
32929 default:
32930 goto invalid_kind;
32933 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32934 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32935 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32936 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32937 else
32938 goto invalid_kind;
32939 cp_lexer_consume_token (parser->lexer);
32941 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32942 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32943 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32944 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32946 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32947 "specified");
32948 modifiers = 0;
32951 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32953 cp_token *token;
32954 cp_lexer_consume_token (parser->lexer);
32956 token = cp_lexer_peek_token (parser->lexer);
32957 t = cp_parser_assignment_expression (parser);
32959 if (t == error_mark_node)
32960 goto resync_fail;
32961 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32962 error_at (token->location, "schedule %<runtime%> does not take "
32963 "a %<chunk_size%> parameter");
32964 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32965 error_at (token->location, "schedule %<auto%> does not take "
32966 "a %<chunk_size%> parameter");
32967 else
32968 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32970 if (!parens.require_close (parser))
32971 goto resync_fail;
32973 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32974 goto resync_fail;
32976 OMP_CLAUSE_SCHEDULE_KIND (c)
32977 = (enum omp_clause_schedule_kind)
32978 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32980 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32981 OMP_CLAUSE_CHAIN (c) = list;
32982 return c;
32984 invalid_kind:
32985 cp_parser_error (parser, "invalid schedule kind");
32986 resync_fail:
32987 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32988 /*or_comma=*/false,
32989 /*consume_paren=*/true);
32990 return list;
32993 /* OpenMP 3.0:
32994 untied */
32996 static tree
32997 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32998 tree list, location_t location)
33000 tree c;
33002 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33004 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33005 OMP_CLAUSE_CHAIN (c) = list;
33006 return c;
33009 /* OpenMP 4.0:
33010 inbranch
33011 notinbranch */
33013 static tree
33014 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33015 tree list, location_t location)
33017 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33018 tree c = build_omp_clause (location, code);
33019 OMP_CLAUSE_CHAIN (c) = list;
33020 return c;
33023 /* OpenMP 4.0:
33024 parallel
33026 sections
33027 taskgroup */
33029 static tree
33030 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33031 enum omp_clause_code code,
33032 tree list, location_t location)
33034 tree c = build_omp_clause (location, code);
33035 OMP_CLAUSE_CHAIN (c) = list;
33036 return c;
33039 /* OpenMP 4.5:
33040 nogroup */
33042 static tree
33043 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33044 tree list, location_t location)
33046 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33047 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33048 OMP_CLAUSE_CHAIN (c) = list;
33049 return c;
33052 /* OpenMP 4.5:
33053 simd
33054 threads */
33056 static tree
33057 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33058 enum omp_clause_code code,
33059 tree list, location_t location)
33061 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33062 tree c = build_omp_clause (location, code);
33063 OMP_CLAUSE_CHAIN (c) = list;
33064 return c;
33067 /* OpenMP 4.0:
33068 num_teams ( expression ) */
33070 static tree
33071 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33072 location_t location)
33074 tree t, c;
33076 matching_parens parens;
33077 if (!parens.require_open (parser))
33078 return list;
33080 t = cp_parser_expression (parser);
33082 if (t == error_mark_node
33083 || !parens.require_close (parser))
33084 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33085 /*or_comma=*/false,
33086 /*consume_paren=*/true);
33088 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33089 "num_teams", location);
33091 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33092 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33093 OMP_CLAUSE_CHAIN (c) = list;
33095 return c;
33098 /* OpenMP 4.0:
33099 thread_limit ( expression ) */
33101 static tree
33102 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33103 location_t location)
33105 tree t, c;
33107 matching_parens parens;
33108 if (!parens.require_open (parser))
33109 return list;
33111 t = cp_parser_expression (parser);
33113 if (t == error_mark_node
33114 || !parens.require_close (parser))
33115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33116 /*or_comma=*/false,
33117 /*consume_paren=*/true);
33119 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33120 "thread_limit", location);
33122 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33123 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33124 OMP_CLAUSE_CHAIN (c) = list;
33126 return c;
33129 /* OpenMP 4.0:
33130 aligned ( variable-list )
33131 aligned ( variable-list : constant-expression ) */
33133 static tree
33134 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33136 tree nlist, c, alignment = NULL_TREE;
33137 bool colon;
33139 matching_parens parens;
33140 if (!parens.require_open (parser))
33141 return list;
33143 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33144 &colon);
33146 if (colon)
33148 alignment = cp_parser_constant_expression (parser);
33150 if (!parens.require_close (parser))
33151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33152 /*or_comma=*/false,
33153 /*consume_paren=*/true);
33155 if (alignment == error_mark_node)
33156 alignment = NULL_TREE;
33159 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33160 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33162 return nlist;
33165 /* OpenMP 4.0:
33166 linear ( variable-list )
33167 linear ( variable-list : expression )
33169 OpenMP 4.5:
33170 linear ( modifier ( variable-list ) )
33171 linear ( modifier ( variable-list ) : expression ) */
33173 static tree
33174 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33175 bool declare_simd)
33177 tree nlist, c, step = integer_one_node;
33178 bool colon;
33179 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33181 matching_parens parens;
33182 if (!parens.require_open (parser))
33183 return list;
33185 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33187 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33188 const char *p = IDENTIFIER_POINTER (id);
33190 if (strcmp ("ref", p) == 0)
33191 kind = OMP_CLAUSE_LINEAR_REF;
33192 else if (strcmp ("val", p) == 0)
33193 kind = OMP_CLAUSE_LINEAR_VAL;
33194 else if (strcmp ("uval", p) == 0)
33195 kind = OMP_CLAUSE_LINEAR_UVAL;
33196 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33197 cp_lexer_consume_token (parser->lexer);
33198 else
33199 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33202 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33203 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33204 &colon);
33205 else
33207 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33208 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33209 if (colon)
33210 cp_parser_require (parser, CPP_COLON, RT_COLON);
33211 else if (!parens.require_close (parser))
33212 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33213 /*or_comma=*/false,
33214 /*consume_paren=*/true);
33217 if (colon)
33219 step = NULL_TREE;
33220 if (declare_simd
33221 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33222 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33224 cp_token *token = cp_lexer_peek_token (parser->lexer);
33225 cp_parser_parse_tentatively (parser);
33226 step = cp_parser_id_expression (parser, /*template_p=*/false,
33227 /*check_dependency_p=*/true,
33228 /*template_p=*/NULL,
33229 /*declarator_p=*/false,
33230 /*optional_p=*/false);
33231 if (step != error_mark_node)
33232 step = cp_parser_lookup_name_simple (parser, step, token->location);
33233 if (step == error_mark_node)
33235 step = NULL_TREE;
33236 cp_parser_abort_tentative_parse (parser);
33238 else if (!cp_parser_parse_definitely (parser))
33239 step = NULL_TREE;
33241 if (!step)
33242 step = cp_parser_expression (parser);
33244 if (!parens.require_close (parser))
33245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33246 /*or_comma=*/false,
33247 /*consume_paren=*/true);
33249 if (step == error_mark_node)
33250 return list;
33253 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33255 OMP_CLAUSE_LINEAR_STEP (c) = step;
33256 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33259 return nlist;
33262 /* OpenMP 4.0:
33263 safelen ( constant-expression ) */
33265 static tree
33266 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33267 location_t location)
33269 tree t, c;
33271 matching_parens parens;
33272 if (!parens.require_open (parser))
33273 return list;
33275 t = cp_parser_constant_expression (parser);
33277 if (t == error_mark_node
33278 || !parens.require_close (parser))
33279 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33280 /*or_comma=*/false,
33281 /*consume_paren=*/true);
33283 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33285 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33286 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33287 OMP_CLAUSE_CHAIN (c) = list;
33289 return c;
33292 /* OpenMP 4.0:
33293 simdlen ( constant-expression ) */
33295 static tree
33296 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33297 location_t location)
33299 tree t, c;
33301 matching_parens parens;
33302 if (!parens.require_open (parser))
33303 return list;
33305 t = cp_parser_constant_expression (parser);
33307 if (t == error_mark_node
33308 || !parens.require_close (parser))
33309 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33310 /*or_comma=*/false,
33311 /*consume_paren=*/true);
33313 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33315 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33316 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33317 OMP_CLAUSE_CHAIN (c) = list;
33319 return c;
33322 /* OpenMP 4.5:
33323 vec:
33324 identifier [+/- integer]
33325 vec , identifier [+/- integer]
33328 static tree
33329 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33330 tree list)
33332 tree vec = NULL;
33334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33336 cp_parser_error (parser, "expected identifier");
33337 return list;
33340 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33342 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33343 tree t, identifier = cp_parser_identifier (parser);
33344 tree addend = NULL;
33346 if (identifier == error_mark_node)
33347 t = error_mark_node;
33348 else
33350 t = cp_parser_lookup_name_simple
33351 (parser, identifier,
33352 cp_lexer_peek_token (parser->lexer)->location);
33353 if (t == error_mark_node)
33354 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33355 id_loc);
33358 bool neg = false;
33359 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33360 neg = true;
33361 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33363 addend = integer_zero_node;
33364 goto add_to_vector;
33366 cp_lexer_consume_token (parser->lexer);
33368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33370 cp_parser_error (parser, "expected integer");
33371 return list;
33374 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33375 if (TREE_CODE (addend) != INTEGER_CST)
33377 cp_parser_error (parser, "expected integer");
33378 return list;
33380 cp_lexer_consume_token (parser->lexer);
33382 add_to_vector:
33383 if (t != error_mark_node)
33385 vec = tree_cons (addend, t, vec);
33386 if (neg)
33387 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33390 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33391 break;
33393 cp_lexer_consume_token (parser->lexer);
33396 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33398 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33399 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33400 OMP_CLAUSE_DECL (u) = nreverse (vec);
33401 OMP_CLAUSE_CHAIN (u) = list;
33402 return u;
33404 return list;
33407 /* OpenMP 4.0:
33408 depend ( depend-kind : variable-list )
33410 depend-kind:
33411 in | out | inout
33413 OpenMP 4.5:
33414 depend ( source )
33416 depend ( sink : vec ) */
33418 static tree
33419 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33421 tree nlist, c;
33422 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33424 matching_parens parens;
33425 if (!parens.require_open (parser))
33426 return list;
33428 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33430 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33431 const char *p = IDENTIFIER_POINTER (id);
33433 if (strcmp ("in", p) == 0)
33434 kind = OMP_CLAUSE_DEPEND_IN;
33435 else if (strcmp ("inout", p) == 0)
33436 kind = OMP_CLAUSE_DEPEND_INOUT;
33437 else if (strcmp ("out", p) == 0)
33438 kind = OMP_CLAUSE_DEPEND_OUT;
33439 else if (strcmp ("source", p) == 0)
33440 kind = OMP_CLAUSE_DEPEND_SOURCE;
33441 else if (strcmp ("sink", p) == 0)
33442 kind = OMP_CLAUSE_DEPEND_SINK;
33443 else
33444 goto invalid_kind;
33446 else
33447 goto invalid_kind;
33449 cp_lexer_consume_token (parser->lexer);
33451 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33453 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33454 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33455 OMP_CLAUSE_DECL (c) = NULL_TREE;
33456 OMP_CLAUSE_CHAIN (c) = list;
33457 if (!parens.require_close (parser))
33458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33459 /*or_comma=*/false,
33460 /*consume_paren=*/true);
33461 return c;
33464 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33465 goto resync_fail;
33467 if (kind == OMP_CLAUSE_DEPEND_SINK)
33468 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33469 else
33471 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33472 list, NULL);
33474 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33475 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33477 return nlist;
33479 invalid_kind:
33480 cp_parser_error (parser, "invalid depend kind");
33481 resync_fail:
33482 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33483 /*or_comma=*/false,
33484 /*consume_paren=*/true);
33485 return list;
33488 /* OpenMP 4.0:
33489 map ( map-kind : variable-list )
33490 map ( variable-list )
33492 map-kind:
33493 alloc | to | from | tofrom
33495 OpenMP 4.5:
33496 map-kind:
33497 alloc | to | from | tofrom | release | delete
33499 map ( always [,] map-kind: variable-list ) */
33501 static tree
33502 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33504 tree nlist, c;
33505 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33506 bool always = false;
33508 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33509 return list;
33511 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33513 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33514 const char *p = IDENTIFIER_POINTER (id);
33516 if (strcmp ("always", p) == 0)
33518 int nth = 2;
33519 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33520 nth++;
33521 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33522 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33523 == RID_DELETE))
33524 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33525 == CPP_COLON))
33527 always = true;
33528 cp_lexer_consume_token (parser->lexer);
33529 if (nth == 3)
33530 cp_lexer_consume_token (parser->lexer);
33535 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33536 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33538 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33539 const char *p = IDENTIFIER_POINTER (id);
33541 if (strcmp ("alloc", p) == 0)
33542 kind = GOMP_MAP_ALLOC;
33543 else if (strcmp ("to", p) == 0)
33544 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33545 else if (strcmp ("from", p) == 0)
33546 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33547 else if (strcmp ("tofrom", p) == 0)
33548 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33549 else if (strcmp ("release", p) == 0)
33550 kind = GOMP_MAP_RELEASE;
33551 else
33553 cp_parser_error (parser, "invalid map kind");
33554 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33555 /*or_comma=*/false,
33556 /*consume_paren=*/true);
33557 return list;
33559 cp_lexer_consume_token (parser->lexer);
33560 cp_lexer_consume_token (parser->lexer);
33562 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33563 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33565 kind = GOMP_MAP_DELETE;
33566 cp_lexer_consume_token (parser->lexer);
33567 cp_lexer_consume_token (parser->lexer);
33570 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33571 NULL);
33573 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33574 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33576 return nlist;
33579 /* OpenMP 4.0:
33580 device ( expression ) */
33582 static tree
33583 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33584 location_t location)
33586 tree t, c;
33588 matching_parens parens;
33589 if (!parens.require_open (parser))
33590 return list;
33592 t = cp_parser_expression (parser);
33594 if (t == error_mark_node
33595 || !parens.require_close (parser))
33596 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33597 /*or_comma=*/false,
33598 /*consume_paren=*/true);
33600 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33601 "device", location);
33603 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33604 OMP_CLAUSE_DEVICE_ID (c) = t;
33605 OMP_CLAUSE_CHAIN (c) = list;
33607 return c;
33610 /* OpenMP 4.0:
33611 dist_schedule ( static )
33612 dist_schedule ( static , expression ) */
33614 static tree
33615 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33616 location_t location)
33618 tree c, t;
33620 matching_parens parens;
33621 if (!parens.require_open (parser))
33622 return list;
33624 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33626 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33627 goto invalid_kind;
33628 cp_lexer_consume_token (parser->lexer);
33630 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33632 cp_lexer_consume_token (parser->lexer);
33634 t = cp_parser_assignment_expression (parser);
33636 if (t == error_mark_node)
33637 goto resync_fail;
33638 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33640 if (!parens.require_close (parser))
33641 goto resync_fail;
33643 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33644 goto resync_fail;
33646 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33647 location);
33648 OMP_CLAUSE_CHAIN (c) = list;
33649 return c;
33651 invalid_kind:
33652 cp_parser_error (parser, "invalid dist_schedule kind");
33653 resync_fail:
33654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33655 /*or_comma=*/false,
33656 /*consume_paren=*/true);
33657 return list;
33660 /* OpenMP 4.0:
33661 proc_bind ( proc-bind-kind )
33663 proc-bind-kind:
33664 master | close | spread */
33666 static tree
33667 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33668 location_t location)
33670 tree c;
33671 enum omp_clause_proc_bind_kind kind;
33673 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33674 return list;
33676 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33678 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33679 const char *p = IDENTIFIER_POINTER (id);
33681 if (strcmp ("master", p) == 0)
33682 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33683 else if (strcmp ("close", p) == 0)
33684 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33685 else if (strcmp ("spread", p) == 0)
33686 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33687 else
33688 goto invalid_kind;
33690 else
33691 goto invalid_kind;
33693 cp_lexer_consume_token (parser->lexer);
33694 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33695 goto resync_fail;
33697 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33698 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33699 location);
33700 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33701 OMP_CLAUSE_CHAIN (c) = list;
33702 return c;
33704 invalid_kind:
33705 cp_parser_error (parser, "invalid depend kind");
33706 resync_fail:
33707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33708 /*or_comma=*/false,
33709 /*consume_paren=*/true);
33710 return list;
33713 /* OpenACC:
33714 async [( int-expr )] */
33716 static tree
33717 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33719 tree c, t;
33720 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33722 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33724 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33726 matching_parens parens;
33727 parens.consume_open (parser);
33729 t = cp_parser_expression (parser);
33730 if (t == error_mark_node
33731 || !parens.require_close (parser))
33732 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33733 /*or_comma=*/false,
33734 /*consume_paren=*/true);
33737 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33739 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33740 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33741 OMP_CLAUSE_CHAIN (c) = list;
33742 list = c;
33744 return list;
33747 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33748 is a bitmask in MASK. Return the list of clauses found. */
33750 static tree
33751 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33752 const char *where, cp_token *pragma_tok,
33753 bool finish_p = true)
33755 tree clauses = NULL;
33756 bool first = true;
33758 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33760 location_t here;
33761 pragma_omp_clause c_kind;
33762 omp_clause_code code;
33763 const char *c_name;
33764 tree prev = clauses;
33766 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33767 cp_lexer_consume_token (parser->lexer);
33769 here = cp_lexer_peek_token (parser->lexer)->location;
33770 c_kind = cp_parser_omp_clause_name (parser);
33772 switch (c_kind)
33774 case PRAGMA_OACC_CLAUSE_ASYNC:
33775 clauses = cp_parser_oacc_clause_async (parser, clauses);
33776 c_name = "async";
33777 break;
33778 case PRAGMA_OACC_CLAUSE_AUTO:
33779 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33780 clauses, here);
33781 c_name = "auto";
33782 break;
33783 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33784 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33785 c_name = "collapse";
33786 break;
33787 case PRAGMA_OACC_CLAUSE_COPY:
33788 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33789 c_name = "copy";
33790 break;
33791 case PRAGMA_OACC_CLAUSE_COPYIN:
33792 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33793 c_name = "copyin";
33794 break;
33795 case PRAGMA_OACC_CLAUSE_COPYOUT:
33796 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33797 c_name = "copyout";
33798 break;
33799 case PRAGMA_OACC_CLAUSE_CREATE:
33800 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33801 c_name = "create";
33802 break;
33803 case PRAGMA_OACC_CLAUSE_DELETE:
33804 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33805 c_name = "delete";
33806 break;
33807 case PRAGMA_OMP_CLAUSE_DEFAULT:
33808 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33809 c_name = "default";
33810 break;
33811 case PRAGMA_OACC_CLAUSE_DEVICE:
33812 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33813 c_name = "device";
33814 break;
33815 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33816 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33817 c_name = "deviceptr";
33818 break;
33819 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33820 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33821 c_name = "device_resident";
33822 break;
33823 case PRAGMA_OACC_CLAUSE_FINALIZE:
33824 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33825 clauses, here);
33826 c_name = "finalize";
33827 break;
33828 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33829 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33830 clauses);
33831 c_name = "firstprivate";
33832 break;
33833 case PRAGMA_OACC_CLAUSE_GANG:
33834 c_name = "gang";
33835 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33836 c_name, clauses);
33837 break;
33838 case PRAGMA_OACC_CLAUSE_HOST:
33839 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33840 c_name = "host";
33841 break;
33842 case PRAGMA_OACC_CLAUSE_IF:
33843 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33844 c_name = "if";
33845 break;
33846 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33847 clauses = cp_parser_oacc_simple_clause (parser,
33848 OMP_CLAUSE_IF_PRESENT,
33849 clauses, here);
33850 c_name = "if_present";
33851 break;
33852 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33853 clauses = cp_parser_oacc_simple_clause (parser,
33854 OMP_CLAUSE_INDEPENDENT,
33855 clauses, here);
33856 c_name = "independent";
33857 break;
33858 case PRAGMA_OACC_CLAUSE_LINK:
33859 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33860 c_name = "link";
33861 break;
33862 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33863 code = OMP_CLAUSE_NUM_GANGS;
33864 c_name = "num_gangs";
33865 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33866 clauses);
33867 break;
33868 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33869 c_name = "num_workers";
33870 code = OMP_CLAUSE_NUM_WORKERS;
33871 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33872 clauses);
33873 break;
33874 case PRAGMA_OACC_CLAUSE_PRESENT:
33875 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33876 c_name = "present";
33877 break;
33878 case PRAGMA_OACC_CLAUSE_PRIVATE:
33879 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33880 clauses);
33881 c_name = "private";
33882 break;
33883 case PRAGMA_OACC_CLAUSE_REDUCTION:
33884 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33885 c_name = "reduction";
33886 break;
33887 case PRAGMA_OACC_CLAUSE_SEQ:
33888 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33889 clauses, here);
33890 c_name = "seq";
33891 break;
33892 case PRAGMA_OACC_CLAUSE_TILE:
33893 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33894 c_name = "tile";
33895 break;
33896 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33897 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33898 clauses);
33899 c_name = "use_device";
33900 break;
33901 case PRAGMA_OACC_CLAUSE_VECTOR:
33902 c_name = "vector";
33903 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33904 c_name, clauses);
33905 break;
33906 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33907 c_name = "vector_length";
33908 code = OMP_CLAUSE_VECTOR_LENGTH;
33909 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33910 clauses);
33911 break;
33912 case PRAGMA_OACC_CLAUSE_WAIT:
33913 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33914 c_name = "wait";
33915 break;
33916 case PRAGMA_OACC_CLAUSE_WORKER:
33917 c_name = "worker";
33918 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33919 c_name, clauses);
33920 break;
33921 default:
33922 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33923 goto saw_error;
33926 first = false;
33928 if (((mask >> c_kind) & 1) == 0)
33930 /* Remove the invalid clause(s) from the list to avoid
33931 confusing the rest of the compiler. */
33932 clauses = prev;
33933 error_at (here, "%qs is not valid for %qs", c_name, where);
33937 saw_error:
33938 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33940 if (finish_p)
33941 return finish_omp_clauses (clauses, C_ORT_ACC);
33943 return clauses;
33946 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33947 is a bitmask in MASK. Return the list of clauses found; the result
33948 of clause default goes in *pdefault. */
33950 static tree
33951 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33952 const char *where, cp_token *pragma_tok,
33953 bool finish_p = true)
33955 tree clauses = NULL;
33956 bool first = true;
33957 cp_token *token = NULL;
33959 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33961 pragma_omp_clause c_kind;
33962 const char *c_name;
33963 tree prev = clauses;
33965 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33966 cp_lexer_consume_token (parser->lexer);
33968 token = cp_lexer_peek_token (parser->lexer);
33969 c_kind = cp_parser_omp_clause_name (parser);
33971 switch (c_kind)
33973 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33974 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33975 token->location);
33976 c_name = "collapse";
33977 break;
33978 case PRAGMA_OMP_CLAUSE_COPYIN:
33979 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33980 c_name = "copyin";
33981 break;
33982 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33983 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33984 clauses);
33985 c_name = "copyprivate";
33986 break;
33987 case PRAGMA_OMP_CLAUSE_DEFAULT:
33988 clauses = cp_parser_omp_clause_default (parser, clauses,
33989 token->location, false);
33990 c_name = "default";
33991 break;
33992 case PRAGMA_OMP_CLAUSE_FINAL:
33993 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33994 c_name = "final";
33995 break;
33996 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33997 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33998 clauses);
33999 c_name = "firstprivate";
34000 break;
34001 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34002 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34003 token->location);
34004 c_name = "grainsize";
34005 break;
34006 case PRAGMA_OMP_CLAUSE_HINT:
34007 clauses = cp_parser_omp_clause_hint (parser, clauses,
34008 token->location);
34009 c_name = "hint";
34010 break;
34011 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34012 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34013 token->location);
34014 c_name = "defaultmap";
34015 break;
34016 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34017 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34018 clauses);
34019 c_name = "use_device_ptr";
34020 break;
34021 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34022 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34023 clauses);
34024 c_name = "is_device_ptr";
34025 break;
34026 case PRAGMA_OMP_CLAUSE_IF:
34027 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34028 true);
34029 c_name = "if";
34030 break;
34031 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34032 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34033 clauses);
34034 c_name = "lastprivate";
34035 break;
34036 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34037 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34038 token->location);
34039 c_name = "mergeable";
34040 break;
34041 case PRAGMA_OMP_CLAUSE_NOWAIT:
34042 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34043 c_name = "nowait";
34044 break;
34045 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34046 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34047 token->location);
34048 c_name = "num_tasks";
34049 break;
34050 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34051 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34052 token->location);
34053 c_name = "num_threads";
34054 break;
34055 case PRAGMA_OMP_CLAUSE_ORDERED:
34056 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34057 token->location);
34058 c_name = "ordered";
34059 break;
34060 case PRAGMA_OMP_CLAUSE_PRIORITY:
34061 clauses = cp_parser_omp_clause_priority (parser, clauses,
34062 token->location);
34063 c_name = "priority";
34064 break;
34065 case PRAGMA_OMP_CLAUSE_PRIVATE:
34066 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34067 clauses);
34068 c_name = "private";
34069 break;
34070 case PRAGMA_OMP_CLAUSE_REDUCTION:
34071 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34072 c_name = "reduction";
34073 break;
34074 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34075 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34076 token->location);
34077 c_name = "schedule";
34078 break;
34079 case PRAGMA_OMP_CLAUSE_SHARED:
34080 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34081 clauses);
34082 c_name = "shared";
34083 break;
34084 case PRAGMA_OMP_CLAUSE_UNTIED:
34085 clauses = cp_parser_omp_clause_untied (parser, clauses,
34086 token->location);
34087 c_name = "untied";
34088 break;
34089 case PRAGMA_OMP_CLAUSE_INBRANCH:
34090 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34091 clauses, token->location);
34092 c_name = "inbranch";
34093 break;
34094 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34095 clauses = cp_parser_omp_clause_branch (parser,
34096 OMP_CLAUSE_NOTINBRANCH,
34097 clauses, token->location);
34098 c_name = "notinbranch";
34099 break;
34100 case PRAGMA_OMP_CLAUSE_PARALLEL:
34101 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34102 clauses, token->location);
34103 c_name = "parallel";
34104 if (!first)
34106 clause_not_first:
34107 error_at (token->location, "%qs must be the first clause of %qs",
34108 c_name, where);
34109 clauses = prev;
34111 break;
34112 case PRAGMA_OMP_CLAUSE_FOR:
34113 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34114 clauses, token->location);
34115 c_name = "for";
34116 if (!first)
34117 goto clause_not_first;
34118 break;
34119 case PRAGMA_OMP_CLAUSE_SECTIONS:
34120 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34121 clauses, token->location);
34122 c_name = "sections";
34123 if (!first)
34124 goto clause_not_first;
34125 break;
34126 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34127 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34128 clauses, token->location);
34129 c_name = "taskgroup";
34130 if (!first)
34131 goto clause_not_first;
34132 break;
34133 case PRAGMA_OMP_CLAUSE_LINK:
34134 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34135 c_name = "to";
34136 break;
34137 case PRAGMA_OMP_CLAUSE_TO:
34138 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34139 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34140 clauses);
34141 else
34142 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34143 c_name = "to";
34144 break;
34145 case PRAGMA_OMP_CLAUSE_FROM:
34146 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34147 c_name = "from";
34148 break;
34149 case PRAGMA_OMP_CLAUSE_UNIFORM:
34150 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34151 clauses);
34152 c_name = "uniform";
34153 break;
34154 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34155 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34156 token->location);
34157 c_name = "num_teams";
34158 break;
34159 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34160 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34161 token->location);
34162 c_name = "thread_limit";
34163 break;
34164 case PRAGMA_OMP_CLAUSE_ALIGNED:
34165 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34166 c_name = "aligned";
34167 break;
34168 case PRAGMA_OMP_CLAUSE_LINEAR:
34170 bool declare_simd = false;
34171 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34172 declare_simd = true;
34173 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34175 c_name = "linear";
34176 break;
34177 case PRAGMA_OMP_CLAUSE_DEPEND:
34178 clauses = cp_parser_omp_clause_depend (parser, clauses,
34179 token->location);
34180 c_name = "depend";
34181 break;
34182 case PRAGMA_OMP_CLAUSE_MAP:
34183 clauses = cp_parser_omp_clause_map (parser, clauses);
34184 c_name = "map";
34185 break;
34186 case PRAGMA_OMP_CLAUSE_DEVICE:
34187 clauses = cp_parser_omp_clause_device (parser, clauses,
34188 token->location);
34189 c_name = "device";
34190 break;
34191 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34192 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34193 token->location);
34194 c_name = "dist_schedule";
34195 break;
34196 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34197 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34198 token->location);
34199 c_name = "proc_bind";
34200 break;
34201 case PRAGMA_OMP_CLAUSE_SAFELEN:
34202 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34203 token->location);
34204 c_name = "safelen";
34205 break;
34206 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34207 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34208 token->location);
34209 c_name = "simdlen";
34210 break;
34211 case PRAGMA_OMP_CLAUSE_NOGROUP:
34212 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34213 token->location);
34214 c_name = "nogroup";
34215 break;
34216 case PRAGMA_OMP_CLAUSE_THREADS:
34217 clauses
34218 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34219 clauses, token->location);
34220 c_name = "threads";
34221 break;
34222 case PRAGMA_OMP_CLAUSE_SIMD:
34223 clauses
34224 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34225 clauses, token->location);
34226 c_name = "simd";
34227 break;
34228 default:
34229 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34230 goto saw_error;
34233 first = false;
34235 if (((mask >> c_kind) & 1) == 0)
34237 /* Remove the invalid clause(s) from the list to avoid
34238 confusing the rest of the compiler. */
34239 clauses = prev;
34240 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34243 saw_error:
34244 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34245 if (finish_p)
34247 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34248 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34249 else
34250 return finish_omp_clauses (clauses, C_ORT_OMP);
34252 return clauses;
34255 /* OpenMP 2.5:
34256 structured-block:
34257 statement
34259 In practice, we're also interested in adding the statement to an
34260 outer node. So it is convenient if we work around the fact that
34261 cp_parser_statement calls add_stmt. */
34263 static unsigned
34264 cp_parser_begin_omp_structured_block (cp_parser *parser)
34266 unsigned save = parser->in_statement;
34268 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34269 This preserves the "not within loop or switch" style error messages
34270 for nonsense cases like
34271 void foo() {
34272 #pragma omp single
34273 break;
34276 if (parser->in_statement)
34277 parser->in_statement = IN_OMP_BLOCK;
34279 return save;
34282 static void
34283 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34285 parser->in_statement = save;
34288 static tree
34289 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34291 tree stmt = begin_omp_structured_block ();
34292 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34294 cp_parser_statement (parser, NULL_TREE, false, if_p);
34296 cp_parser_end_omp_structured_block (parser, save);
34297 return finish_omp_structured_block (stmt);
34300 /* OpenMP 2.5:
34301 # pragma omp atomic new-line
34302 expression-stmt
34304 expression-stmt:
34305 x binop= expr | x++ | ++x | x-- | --x
34306 binop:
34307 +, *, -, /, &, ^, |, <<, >>
34309 where x is an lvalue expression with scalar type.
34311 OpenMP 3.1:
34312 # pragma omp atomic new-line
34313 update-stmt
34315 # pragma omp atomic read new-line
34316 read-stmt
34318 # pragma omp atomic write new-line
34319 write-stmt
34321 # pragma omp atomic update new-line
34322 update-stmt
34324 # pragma omp atomic capture new-line
34325 capture-stmt
34327 # pragma omp atomic capture new-line
34328 capture-block
34330 read-stmt:
34331 v = x
34332 write-stmt:
34333 x = expr
34334 update-stmt:
34335 expression-stmt | x = x binop expr
34336 capture-stmt:
34337 v = expression-stmt
34338 capture-block:
34339 { v = x; update-stmt; } | { update-stmt; v = x; }
34341 OpenMP 4.0:
34342 update-stmt:
34343 expression-stmt | x = x binop expr | x = expr binop x
34344 capture-stmt:
34345 v = update-stmt
34346 capture-block:
34347 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34349 where x and v are lvalue expressions with scalar type. */
34351 static void
34352 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34354 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34355 tree rhs1 = NULL_TREE, orig_lhs;
34356 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34357 bool structured_block = false;
34358 bool seq_cst = false;
34360 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34362 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34363 const char *p = IDENTIFIER_POINTER (id);
34365 if (!strcmp (p, "seq_cst"))
34367 seq_cst = true;
34368 cp_lexer_consume_token (parser->lexer);
34369 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34370 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34371 cp_lexer_consume_token (parser->lexer);
34374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34377 const char *p = IDENTIFIER_POINTER (id);
34379 if (!strcmp (p, "read"))
34380 code = OMP_ATOMIC_READ;
34381 else if (!strcmp (p, "write"))
34382 code = NOP_EXPR;
34383 else if (!strcmp (p, "update"))
34384 code = OMP_ATOMIC;
34385 else if (!strcmp (p, "capture"))
34386 code = OMP_ATOMIC_CAPTURE_NEW;
34387 else
34388 p = NULL;
34389 if (p)
34390 cp_lexer_consume_token (parser->lexer);
34392 if (!seq_cst)
34394 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34395 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34396 cp_lexer_consume_token (parser->lexer);
34398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34400 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34401 const char *p = IDENTIFIER_POINTER (id);
34403 if (!strcmp (p, "seq_cst"))
34405 seq_cst = true;
34406 cp_lexer_consume_token (parser->lexer);
34410 cp_parser_require_pragma_eol (parser, pragma_tok);
34412 switch (code)
34414 case OMP_ATOMIC_READ:
34415 case NOP_EXPR: /* atomic write */
34416 v = cp_parser_unary_expression (parser);
34417 if (v == error_mark_node)
34418 goto saw_error;
34419 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34420 goto saw_error;
34421 if (code == NOP_EXPR)
34422 lhs = cp_parser_expression (parser);
34423 else
34424 lhs = cp_parser_unary_expression (parser);
34425 if (lhs == error_mark_node)
34426 goto saw_error;
34427 if (code == NOP_EXPR)
34429 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34430 opcode. */
34431 code = OMP_ATOMIC;
34432 rhs = lhs;
34433 lhs = v;
34434 v = NULL_TREE;
34436 goto done;
34437 case OMP_ATOMIC_CAPTURE_NEW:
34438 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34440 cp_lexer_consume_token (parser->lexer);
34441 structured_block = true;
34443 else
34445 v = cp_parser_unary_expression (parser);
34446 if (v == error_mark_node)
34447 goto saw_error;
34448 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34449 goto saw_error;
34451 default:
34452 break;
34455 restart:
34456 lhs = cp_parser_unary_expression (parser);
34457 orig_lhs = lhs;
34458 switch (TREE_CODE (lhs))
34460 case ERROR_MARK:
34461 goto saw_error;
34463 case POSTINCREMENT_EXPR:
34464 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34465 code = OMP_ATOMIC_CAPTURE_OLD;
34466 /* FALLTHROUGH */
34467 case PREINCREMENT_EXPR:
34468 lhs = TREE_OPERAND (lhs, 0);
34469 opcode = PLUS_EXPR;
34470 rhs = integer_one_node;
34471 break;
34473 case POSTDECREMENT_EXPR:
34474 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34475 code = OMP_ATOMIC_CAPTURE_OLD;
34476 /* FALLTHROUGH */
34477 case PREDECREMENT_EXPR:
34478 lhs = TREE_OPERAND (lhs, 0);
34479 opcode = MINUS_EXPR;
34480 rhs = integer_one_node;
34481 break;
34483 case COMPOUND_EXPR:
34484 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34485 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34486 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34487 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34488 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34489 (TREE_OPERAND (lhs, 1), 0), 0)))
34490 == BOOLEAN_TYPE)
34491 /* Undo effects of boolean_increment for post {in,de}crement. */
34492 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34493 /* FALLTHRU */
34494 case MODIFY_EXPR:
34495 if (TREE_CODE (lhs) == MODIFY_EXPR
34496 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34498 /* Undo effects of boolean_increment. */
34499 if (integer_onep (TREE_OPERAND (lhs, 1)))
34501 /* This is pre or post increment. */
34502 rhs = TREE_OPERAND (lhs, 1);
34503 lhs = TREE_OPERAND (lhs, 0);
34504 opcode = NOP_EXPR;
34505 if (code == OMP_ATOMIC_CAPTURE_NEW
34506 && !structured_block
34507 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34508 code = OMP_ATOMIC_CAPTURE_OLD;
34509 break;
34512 /* FALLTHRU */
34513 default:
34514 switch (cp_lexer_peek_token (parser->lexer)->type)
34516 case CPP_MULT_EQ:
34517 opcode = MULT_EXPR;
34518 break;
34519 case CPP_DIV_EQ:
34520 opcode = TRUNC_DIV_EXPR;
34521 break;
34522 case CPP_PLUS_EQ:
34523 opcode = PLUS_EXPR;
34524 break;
34525 case CPP_MINUS_EQ:
34526 opcode = MINUS_EXPR;
34527 break;
34528 case CPP_LSHIFT_EQ:
34529 opcode = LSHIFT_EXPR;
34530 break;
34531 case CPP_RSHIFT_EQ:
34532 opcode = RSHIFT_EXPR;
34533 break;
34534 case CPP_AND_EQ:
34535 opcode = BIT_AND_EXPR;
34536 break;
34537 case CPP_OR_EQ:
34538 opcode = BIT_IOR_EXPR;
34539 break;
34540 case CPP_XOR_EQ:
34541 opcode = BIT_XOR_EXPR;
34542 break;
34543 case CPP_EQ:
34544 enum cp_parser_prec oprec;
34545 cp_token *token;
34546 cp_lexer_consume_token (parser->lexer);
34547 cp_parser_parse_tentatively (parser);
34548 rhs1 = cp_parser_simple_cast_expression (parser);
34549 if (rhs1 == error_mark_node)
34551 cp_parser_abort_tentative_parse (parser);
34552 cp_parser_simple_cast_expression (parser);
34553 goto saw_error;
34555 token = cp_lexer_peek_token (parser->lexer);
34556 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34558 cp_parser_abort_tentative_parse (parser);
34559 cp_parser_parse_tentatively (parser);
34560 rhs = cp_parser_binary_expression (parser, false, true,
34561 PREC_NOT_OPERATOR, NULL);
34562 if (rhs == error_mark_node)
34564 cp_parser_abort_tentative_parse (parser);
34565 cp_parser_binary_expression (parser, false, true,
34566 PREC_NOT_OPERATOR, NULL);
34567 goto saw_error;
34569 switch (TREE_CODE (rhs))
34571 case MULT_EXPR:
34572 case TRUNC_DIV_EXPR:
34573 case RDIV_EXPR:
34574 case PLUS_EXPR:
34575 case MINUS_EXPR:
34576 case LSHIFT_EXPR:
34577 case RSHIFT_EXPR:
34578 case BIT_AND_EXPR:
34579 case BIT_IOR_EXPR:
34580 case BIT_XOR_EXPR:
34581 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34583 if (cp_parser_parse_definitely (parser))
34585 opcode = TREE_CODE (rhs);
34586 rhs1 = TREE_OPERAND (rhs, 0);
34587 rhs = TREE_OPERAND (rhs, 1);
34588 goto stmt_done;
34590 else
34591 goto saw_error;
34593 break;
34594 default:
34595 break;
34597 cp_parser_abort_tentative_parse (parser);
34598 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34600 rhs = cp_parser_expression (parser);
34601 if (rhs == error_mark_node)
34602 goto saw_error;
34603 opcode = NOP_EXPR;
34604 rhs1 = NULL_TREE;
34605 goto stmt_done;
34607 cp_parser_error (parser,
34608 "invalid form of %<#pragma omp atomic%>");
34609 goto saw_error;
34611 if (!cp_parser_parse_definitely (parser))
34612 goto saw_error;
34613 switch (token->type)
34615 case CPP_SEMICOLON:
34616 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34618 code = OMP_ATOMIC_CAPTURE_OLD;
34619 v = lhs;
34620 lhs = NULL_TREE;
34621 lhs1 = rhs1;
34622 rhs1 = NULL_TREE;
34623 cp_lexer_consume_token (parser->lexer);
34624 goto restart;
34626 else if (structured_block)
34628 opcode = NOP_EXPR;
34629 rhs = rhs1;
34630 rhs1 = NULL_TREE;
34631 goto stmt_done;
34633 cp_parser_error (parser,
34634 "invalid form of %<#pragma omp atomic%>");
34635 goto saw_error;
34636 case CPP_MULT:
34637 opcode = MULT_EXPR;
34638 break;
34639 case CPP_DIV:
34640 opcode = TRUNC_DIV_EXPR;
34641 break;
34642 case CPP_PLUS:
34643 opcode = PLUS_EXPR;
34644 break;
34645 case CPP_MINUS:
34646 opcode = MINUS_EXPR;
34647 break;
34648 case CPP_LSHIFT:
34649 opcode = LSHIFT_EXPR;
34650 break;
34651 case CPP_RSHIFT:
34652 opcode = RSHIFT_EXPR;
34653 break;
34654 case CPP_AND:
34655 opcode = BIT_AND_EXPR;
34656 break;
34657 case CPP_OR:
34658 opcode = BIT_IOR_EXPR;
34659 break;
34660 case CPP_XOR:
34661 opcode = BIT_XOR_EXPR;
34662 break;
34663 default:
34664 cp_parser_error (parser,
34665 "invalid operator for %<#pragma omp atomic%>");
34666 goto saw_error;
34668 oprec = TOKEN_PRECEDENCE (token);
34669 gcc_assert (oprec != PREC_NOT_OPERATOR);
34670 if (commutative_tree_code (opcode))
34671 oprec = (enum cp_parser_prec) (oprec - 1);
34672 cp_lexer_consume_token (parser->lexer);
34673 rhs = cp_parser_binary_expression (parser, false, false,
34674 oprec, NULL);
34675 if (rhs == error_mark_node)
34676 goto saw_error;
34677 goto stmt_done;
34678 /* FALLTHROUGH */
34679 default:
34680 cp_parser_error (parser,
34681 "invalid operator for %<#pragma omp atomic%>");
34682 goto saw_error;
34684 cp_lexer_consume_token (parser->lexer);
34686 rhs = cp_parser_expression (parser);
34687 if (rhs == error_mark_node)
34688 goto saw_error;
34689 break;
34691 stmt_done:
34692 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34694 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34695 goto saw_error;
34696 v = cp_parser_unary_expression (parser);
34697 if (v == error_mark_node)
34698 goto saw_error;
34699 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34700 goto saw_error;
34701 lhs1 = cp_parser_unary_expression (parser);
34702 if (lhs1 == error_mark_node)
34703 goto saw_error;
34705 if (structured_block)
34707 cp_parser_consume_semicolon_at_end_of_statement (parser);
34708 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34710 done:
34711 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34712 if (!structured_block)
34713 cp_parser_consume_semicolon_at_end_of_statement (parser);
34714 return;
34716 saw_error:
34717 cp_parser_skip_to_end_of_block_or_statement (parser);
34718 if (structured_block)
34720 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34721 cp_lexer_consume_token (parser->lexer);
34722 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34724 cp_parser_skip_to_end_of_block_or_statement (parser);
34725 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34726 cp_lexer_consume_token (parser->lexer);
34732 /* OpenMP 2.5:
34733 # pragma omp barrier new-line */
34735 static void
34736 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34738 cp_parser_require_pragma_eol (parser, pragma_tok);
34739 finish_omp_barrier ();
34742 /* OpenMP 2.5:
34743 # pragma omp critical [(name)] new-line
34744 structured-block
34746 OpenMP 4.5:
34747 # pragma omp critical [(name) [hint(expression)]] new-line
34748 structured-block */
34750 #define OMP_CRITICAL_CLAUSE_MASK \
34751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34753 static tree
34754 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34756 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34760 matching_parens parens;
34761 parens.consume_open (parser);
34763 name = cp_parser_identifier (parser);
34765 if (name == error_mark_node
34766 || !parens.require_close (parser))
34767 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34768 /*or_comma=*/false,
34769 /*consume_paren=*/true);
34770 if (name == error_mark_node)
34771 name = NULL;
34773 clauses = cp_parser_omp_all_clauses (parser,
34774 OMP_CRITICAL_CLAUSE_MASK,
34775 "#pragma omp critical", pragma_tok);
34777 else
34778 cp_parser_require_pragma_eol (parser, pragma_tok);
34780 stmt = cp_parser_omp_structured_block (parser, if_p);
34781 return c_finish_omp_critical (input_location, stmt, name, clauses);
34784 /* OpenMP 2.5:
34785 # pragma omp flush flush-vars[opt] new-line
34787 flush-vars:
34788 ( variable-list ) */
34790 static void
34791 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34793 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34794 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34795 cp_parser_require_pragma_eol (parser, pragma_tok);
34797 finish_omp_flush ();
34800 /* Helper function, to parse omp for increment expression. */
34802 static tree
34803 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34805 tree cond = cp_parser_binary_expression (parser, false, true,
34806 PREC_NOT_OPERATOR, NULL);
34807 if (cond == error_mark_node
34808 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34810 cp_parser_skip_to_end_of_statement (parser);
34811 return error_mark_node;
34814 switch (TREE_CODE (cond))
34816 case GT_EXPR:
34817 case GE_EXPR:
34818 case LT_EXPR:
34819 case LE_EXPR:
34820 break;
34821 case NE_EXPR:
34822 /* Fall through: OpenMP disallows NE_EXPR. */
34823 gcc_fallthrough ();
34824 default:
34825 return error_mark_node;
34828 /* If decl is an iterator, preserve LHS and RHS of the relational
34829 expr until finish_omp_for. */
34830 if (decl
34831 && (type_dependent_expression_p (decl)
34832 || CLASS_TYPE_P (TREE_TYPE (decl))))
34833 return cond;
34835 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34836 TREE_CODE (cond),
34837 TREE_OPERAND (cond, 0), ERROR_MARK,
34838 TREE_OPERAND (cond, 1), ERROR_MARK,
34839 /*overload=*/NULL, tf_warning_or_error);
34842 /* Helper function, to parse omp for increment expression. */
34844 static tree
34845 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34847 cp_token *token = cp_lexer_peek_token (parser->lexer);
34848 enum tree_code op;
34849 tree lhs, rhs;
34850 cp_id_kind idk;
34851 bool decl_first;
34853 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34855 op = (token->type == CPP_PLUS_PLUS
34856 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34857 cp_lexer_consume_token (parser->lexer);
34858 lhs = cp_parser_simple_cast_expression (parser);
34859 if (lhs != decl
34860 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34861 return error_mark_node;
34862 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34865 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34866 if (lhs != decl
34867 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34868 return error_mark_node;
34870 token = cp_lexer_peek_token (parser->lexer);
34871 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34873 op = (token->type == CPP_PLUS_PLUS
34874 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34875 cp_lexer_consume_token (parser->lexer);
34876 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34879 op = cp_parser_assignment_operator_opt (parser);
34880 if (op == ERROR_MARK)
34881 return error_mark_node;
34883 if (op != NOP_EXPR)
34885 rhs = cp_parser_assignment_expression (parser);
34886 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34887 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34890 lhs = cp_parser_binary_expression (parser, false, false,
34891 PREC_ADDITIVE_EXPRESSION, NULL);
34892 token = cp_lexer_peek_token (parser->lexer);
34893 decl_first = (lhs == decl
34894 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34895 if (decl_first)
34896 lhs = NULL_TREE;
34897 if (token->type != CPP_PLUS
34898 && token->type != CPP_MINUS)
34899 return error_mark_node;
34903 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34904 cp_lexer_consume_token (parser->lexer);
34905 rhs = cp_parser_binary_expression (parser, false, false,
34906 PREC_ADDITIVE_EXPRESSION, NULL);
34907 token = cp_lexer_peek_token (parser->lexer);
34908 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34910 if (lhs == NULL_TREE)
34912 if (op == PLUS_EXPR)
34913 lhs = rhs;
34914 else
34915 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34916 tf_warning_or_error);
34918 else
34919 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34920 ERROR_MARK, NULL, tf_warning_or_error);
34923 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34925 if (!decl_first)
34927 if ((rhs != decl
34928 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34929 || op == MINUS_EXPR)
34930 return error_mark_node;
34931 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34933 else
34934 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34936 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34939 /* Parse the initialization statement of an OpenMP for loop.
34941 Return true if the resulting construct should have an
34942 OMP_CLAUSE_PRIVATE added to it. */
34944 static tree
34945 cp_parser_omp_for_loop_init (cp_parser *parser,
34946 tree &this_pre_body,
34947 vec<tree, va_gc> *&for_block,
34948 tree &init,
34949 tree &orig_init,
34950 tree &decl,
34951 tree &real_decl)
34953 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34954 return NULL_TREE;
34956 tree add_private_clause = NULL_TREE;
34958 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34960 init-expr:
34961 var = lb
34962 integer-type var = lb
34963 random-access-iterator-type var = lb
34964 pointer-type var = lb
34966 cp_decl_specifier_seq type_specifiers;
34968 /* First, try to parse as an initialized declaration. See
34969 cp_parser_condition, from whence the bulk of this is copied. */
34971 cp_parser_parse_tentatively (parser);
34972 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34973 /*is_trailing_return=*/false,
34974 &type_specifiers);
34975 if (cp_parser_parse_definitely (parser))
34977 /* If parsing a type specifier seq succeeded, then this
34978 MUST be a initialized declaration. */
34979 tree asm_specification, attributes;
34980 cp_declarator *declarator;
34982 declarator = cp_parser_declarator (parser,
34983 CP_PARSER_DECLARATOR_NAMED,
34984 /*ctor_dtor_or_conv_p=*/NULL,
34985 /*parenthesized_p=*/NULL,
34986 /*member_p=*/false,
34987 /*friend_p=*/false);
34988 attributes = cp_parser_attributes_opt (parser);
34989 asm_specification = cp_parser_asm_specification_opt (parser);
34991 if (declarator == cp_error_declarator)
34992 cp_parser_skip_to_end_of_statement (parser);
34994 else
34996 tree pushed_scope, auto_node;
34998 decl = start_decl (declarator, &type_specifiers,
34999 SD_INITIALIZED, attributes,
35000 /*prefix_attributes=*/NULL_TREE,
35001 &pushed_scope);
35003 auto_node = type_uses_auto (TREE_TYPE (decl));
35004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35006 if (cp_lexer_next_token_is (parser->lexer,
35007 CPP_OPEN_PAREN))
35008 error ("parenthesized initialization is not allowed in "
35009 "OpenMP %<for%> loop");
35010 else
35011 /* Trigger an error. */
35012 cp_parser_require (parser, CPP_EQ, RT_EQ);
35014 init = error_mark_node;
35015 cp_parser_skip_to_end_of_statement (parser);
35017 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35018 || type_dependent_expression_p (decl)
35019 || auto_node)
35021 bool is_direct_init, is_non_constant_init;
35023 init = cp_parser_initializer (parser,
35024 &is_direct_init,
35025 &is_non_constant_init);
35027 if (auto_node)
35029 TREE_TYPE (decl)
35030 = do_auto_deduction (TREE_TYPE (decl), init,
35031 auto_node);
35033 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35034 && !type_dependent_expression_p (decl))
35035 goto non_class;
35038 cp_finish_decl (decl, init, !is_non_constant_init,
35039 asm_specification,
35040 LOOKUP_ONLYCONVERTING);
35041 orig_init = init;
35042 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35044 vec_safe_push (for_block, this_pre_body);
35045 init = NULL_TREE;
35047 else
35049 init = pop_stmt_list (this_pre_body);
35050 if (init && TREE_CODE (init) == STATEMENT_LIST)
35052 tree_stmt_iterator i = tsi_start (init);
35053 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35054 while (!tsi_end_p (i))
35056 tree t = tsi_stmt (i);
35057 if (TREE_CODE (t) == DECL_EXPR
35058 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35060 tsi_delink (&i);
35061 vec_safe_push (for_block, t);
35062 continue;
35064 break;
35066 if (tsi_one_before_end_p (i))
35068 tree t = tsi_stmt (i);
35069 tsi_delink (&i);
35070 free_stmt_list (init);
35071 init = t;
35075 this_pre_body = NULL_TREE;
35077 else
35079 /* Consume '='. */
35080 cp_lexer_consume_token (parser->lexer);
35081 init = cp_parser_assignment_expression (parser);
35083 non_class:
35084 if (TYPE_REF_P (TREE_TYPE (decl)))
35085 init = error_mark_node;
35086 else
35087 cp_finish_decl (decl, NULL_TREE,
35088 /*init_const_expr_p=*/false,
35089 asm_specification,
35090 LOOKUP_ONLYCONVERTING);
35093 if (pushed_scope)
35094 pop_scope (pushed_scope);
35097 else
35099 cp_id_kind idk;
35100 /* If parsing a type specifier sequence failed, then
35101 this MUST be a simple expression. */
35102 cp_parser_parse_tentatively (parser);
35103 decl = cp_parser_primary_expression (parser, false, false,
35104 false, &idk);
35105 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35106 if (!cp_parser_error_occurred (parser)
35107 && decl
35108 && (TREE_CODE (decl) == COMPONENT_REF
35109 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35111 cp_parser_abort_tentative_parse (parser);
35112 cp_parser_parse_tentatively (parser);
35113 cp_token *token = cp_lexer_peek_token (parser->lexer);
35114 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35115 /*check_dependency_p=*/true,
35116 /*template_p=*/NULL,
35117 /*declarator_p=*/false,
35118 /*optional_p=*/false);
35119 if (name != error_mark_node
35120 && last_tok == cp_lexer_peek_token (parser->lexer))
35122 decl = cp_parser_lookup_name_simple (parser, name,
35123 token->location);
35124 if (TREE_CODE (decl) == FIELD_DECL)
35125 add_private_clause = omp_privatize_field (decl, false);
35127 cp_parser_abort_tentative_parse (parser);
35128 cp_parser_parse_tentatively (parser);
35129 decl = cp_parser_primary_expression (parser, false, false,
35130 false, &idk);
35132 if (!cp_parser_error_occurred (parser)
35133 && decl
35134 && DECL_P (decl)
35135 && CLASS_TYPE_P (TREE_TYPE (decl)))
35137 tree rhs;
35139 cp_parser_parse_definitely (parser);
35140 cp_parser_require (parser, CPP_EQ, RT_EQ);
35141 rhs = cp_parser_assignment_expression (parser);
35142 orig_init = rhs;
35143 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35144 decl, NOP_EXPR,
35145 rhs,
35146 tf_warning_or_error));
35147 if (!add_private_clause)
35148 add_private_clause = decl;
35150 else
35152 decl = NULL;
35153 cp_parser_abort_tentative_parse (parser);
35154 init = cp_parser_expression (parser);
35155 if (init)
35157 if (TREE_CODE (init) == MODIFY_EXPR
35158 || TREE_CODE (init) == MODOP_EXPR)
35159 real_decl = TREE_OPERAND (init, 0);
35163 return add_private_clause;
35166 /* Parse the restricted form of the for statement allowed by OpenMP. */
35168 static tree
35169 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35170 tree *cclauses, bool *if_p)
35172 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35173 tree real_decl, initv, condv, incrv, declv;
35174 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35175 location_t loc_first;
35176 bool collapse_err = false;
35177 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35178 vec<tree, va_gc> *for_block = make_tree_vector ();
35179 auto_vec<tree, 4> orig_inits;
35180 bool tiling = false;
35182 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35183 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35184 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35185 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35187 tiling = true;
35188 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35190 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35191 && OMP_CLAUSE_ORDERED_EXPR (cl))
35193 ordered_cl = cl;
35194 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35197 if (ordered && ordered < collapse)
35199 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35200 "%<ordered%> clause parameter is less than %<collapse%>");
35201 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35202 = build_int_cst (NULL_TREE, collapse);
35203 ordered = collapse;
35205 if (ordered)
35207 for (tree *pc = &clauses; *pc; )
35208 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35210 error_at (OMP_CLAUSE_LOCATION (*pc),
35211 "%<linear%> clause may not be specified together "
35212 "with %<ordered%> clause with a parameter");
35213 *pc = OMP_CLAUSE_CHAIN (*pc);
35215 else
35216 pc = &OMP_CLAUSE_CHAIN (*pc);
35219 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35220 count = ordered ? ordered : collapse;
35222 declv = make_tree_vec (count);
35223 initv = make_tree_vec (count);
35224 condv = make_tree_vec (count);
35225 incrv = make_tree_vec (count);
35227 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35229 for (i = 0; i < count; i++)
35231 int bracecount = 0;
35232 tree add_private_clause = NULL_TREE;
35233 location_t loc;
35235 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35237 if (!collapse_err)
35238 cp_parser_error (parser, "for statement expected");
35239 return NULL;
35241 loc = cp_lexer_consume_token (parser->lexer)->location;
35243 matching_parens parens;
35244 if (!parens.require_open (parser))
35245 return NULL;
35247 init = orig_init = decl = real_decl = NULL;
35248 this_pre_body = push_stmt_list ();
35250 add_private_clause
35251 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35252 init, orig_init, decl, real_decl);
35254 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35255 if (this_pre_body)
35257 this_pre_body = pop_stmt_list (this_pre_body);
35258 if (pre_body)
35260 tree t = pre_body;
35261 pre_body = push_stmt_list ();
35262 add_stmt (t);
35263 add_stmt (this_pre_body);
35264 pre_body = pop_stmt_list (pre_body);
35266 else
35267 pre_body = this_pre_body;
35270 if (decl)
35271 real_decl = decl;
35272 if (cclauses != NULL
35273 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35274 && real_decl != NULL_TREE)
35276 tree *c;
35277 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35278 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35279 && OMP_CLAUSE_DECL (*c) == real_decl)
35281 error_at (loc, "iteration variable %qD"
35282 " should not be firstprivate", real_decl);
35283 *c = OMP_CLAUSE_CHAIN (*c);
35285 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35286 && OMP_CLAUSE_DECL (*c) == real_decl)
35288 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35289 tree l = *c;
35290 *c = OMP_CLAUSE_CHAIN (*c);
35291 if (code == OMP_SIMD)
35293 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35294 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35296 else
35298 OMP_CLAUSE_CHAIN (l) = clauses;
35299 clauses = l;
35301 add_private_clause = NULL_TREE;
35303 else
35305 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35306 && OMP_CLAUSE_DECL (*c) == real_decl)
35307 add_private_clause = NULL_TREE;
35308 c = &OMP_CLAUSE_CHAIN (*c);
35312 if (add_private_clause)
35314 tree c;
35315 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35317 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35318 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35319 && OMP_CLAUSE_DECL (c) == decl)
35320 break;
35321 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35322 && OMP_CLAUSE_DECL (c) == decl)
35323 error_at (loc, "iteration variable %qD "
35324 "should not be firstprivate",
35325 decl);
35326 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35327 && OMP_CLAUSE_DECL (c) == decl)
35328 error_at (loc, "iteration variable %qD should not be reduction",
35329 decl);
35331 if (c == NULL)
35333 if (code != OMP_SIMD)
35334 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35335 else if (collapse == 1)
35336 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35337 else
35338 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35339 OMP_CLAUSE_DECL (c) = add_private_clause;
35340 c = finish_omp_clauses (c, C_ORT_OMP);
35341 if (c)
35343 OMP_CLAUSE_CHAIN (c) = clauses;
35344 clauses = c;
35345 /* For linear, signal that we need to fill up
35346 the so far unknown linear step. */
35347 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35348 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35353 cond = NULL;
35354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35355 cond = cp_parser_omp_for_cond (parser, decl);
35356 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35358 incr = NULL;
35359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35361 /* If decl is an iterator, preserve the operator on decl
35362 until finish_omp_for. */
35363 if (real_decl
35364 && ((processing_template_decl
35365 && (TREE_TYPE (real_decl) == NULL_TREE
35366 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35367 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35368 incr = cp_parser_omp_for_incr (parser, real_decl);
35369 else
35370 incr = cp_parser_expression (parser);
35371 if (!EXPR_HAS_LOCATION (incr))
35372 protected_set_expr_location (incr, input_location);
35375 if (!parens.require_close (parser))
35376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35377 /*or_comma=*/false,
35378 /*consume_paren=*/true);
35380 TREE_VEC_ELT (declv, i) = decl;
35381 TREE_VEC_ELT (initv, i) = init;
35382 TREE_VEC_ELT (condv, i) = cond;
35383 TREE_VEC_ELT (incrv, i) = incr;
35384 if (orig_init)
35386 orig_inits.safe_grow_cleared (i + 1);
35387 orig_inits[i] = orig_init;
35390 if (i == count - 1)
35391 break;
35393 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35394 in between the collapsed for loops to be still considered perfectly
35395 nested. Hopefully the final version clarifies this.
35396 For now handle (multiple) {'s and empty statements. */
35397 cp_parser_parse_tentatively (parser);
35398 for (;;)
35400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35401 break;
35402 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35404 cp_lexer_consume_token (parser->lexer);
35405 bracecount++;
35407 else if (bracecount
35408 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35409 cp_lexer_consume_token (parser->lexer);
35410 else
35412 loc = cp_lexer_peek_token (parser->lexer)->location;
35413 error_at (loc, "not enough for loops to collapse");
35414 collapse_err = true;
35415 cp_parser_abort_tentative_parse (parser);
35416 declv = NULL_TREE;
35417 break;
35421 if (declv)
35423 cp_parser_parse_definitely (parser);
35424 nbraces += bracecount;
35428 if (nbraces)
35429 if_p = NULL;
35431 /* Note that we saved the original contents of this flag when we entered
35432 the structured block, and so we don't need to re-save it here. */
35433 parser->in_statement = IN_OMP_FOR;
35435 /* Note that the grammar doesn't call for a structured block here,
35436 though the loop as a whole is a structured block. */
35437 body = push_stmt_list ();
35438 cp_parser_statement (parser, NULL_TREE, false, if_p);
35439 body = pop_stmt_list (body);
35441 if (declv == NULL_TREE)
35442 ret = NULL_TREE;
35443 else
35444 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35445 body, pre_body, &orig_inits, clauses);
35447 while (nbraces)
35449 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35451 cp_lexer_consume_token (parser->lexer);
35452 nbraces--;
35454 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35455 cp_lexer_consume_token (parser->lexer);
35456 else
35458 if (!collapse_err)
35460 error_at (cp_lexer_peek_token (parser->lexer)->location,
35461 "collapsed loops not perfectly nested");
35463 collapse_err = true;
35464 cp_parser_statement_seq_opt (parser, NULL);
35465 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35466 break;
35470 while (!for_block->is_empty ())
35472 tree t = for_block->pop ();
35473 if (TREE_CODE (t) == STATEMENT_LIST)
35474 add_stmt (pop_stmt_list (t));
35475 else
35476 add_stmt (t);
35478 release_tree_vector (for_block);
35480 return ret;
35483 /* Helper function for OpenMP parsing, split clauses and call
35484 finish_omp_clauses on each of the set of clauses afterwards. */
35486 static void
35487 cp_omp_split_clauses (location_t loc, enum tree_code code,
35488 omp_clause_mask mask, tree clauses, tree *cclauses)
35490 int i;
35491 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35492 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35493 if (cclauses[i])
35494 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35497 /* OpenMP 4.0:
35498 #pragma omp simd simd-clause[optseq] new-line
35499 for-loop */
35501 #define OMP_SIMD_CLAUSE_MASK \
35502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35511 static tree
35512 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35513 char *p_name, omp_clause_mask mask, tree *cclauses,
35514 bool *if_p)
35516 tree clauses, sb, ret;
35517 unsigned int save;
35518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35520 strcat (p_name, " simd");
35521 mask |= OMP_SIMD_CLAUSE_MASK;
35523 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35524 cclauses == NULL);
35525 if (cclauses)
35527 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35528 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35529 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35530 OMP_CLAUSE_ORDERED);
35531 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35533 error_at (OMP_CLAUSE_LOCATION (c),
35534 "%<ordered%> clause with parameter may not be specified "
35535 "on %qs construct", p_name);
35536 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35540 sb = begin_omp_structured_block ();
35541 save = cp_parser_begin_omp_structured_block (parser);
35543 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35545 cp_parser_end_omp_structured_block (parser, save);
35546 add_stmt (finish_omp_structured_block (sb));
35548 return ret;
35551 /* OpenMP 2.5:
35552 #pragma omp for for-clause[optseq] new-line
35553 for-loop
35555 OpenMP 4.0:
35556 #pragma omp for simd for-simd-clause[optseq] new-line
35557 for-loop */
35559 #define OMP_FOR_CLAUSE_MASK \
35560 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35570 static tree
35571 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35572 char *p_name, omp_clause_mask mask, tree *cclauses,
35573 bool *if_p)
35575 tree clauses, sb, ret;
35576 unsigned int save;
35577 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35579 strcat (p_name, " for");
35580 mask |= OMP_FOR_CLAUSE_MASK;
35581 /* parallel for{, simd} disallows nowait clause, but for
35582 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35583 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35584 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35585 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35586 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35587 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35589 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35591 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35592 const char *p = IDENTIFIER_POINTER (id);
35594 if (strcmp (p, "simd") == 0)
35596 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35597 if (cclauses == NULL)
35598 cclauses = cclauses_buf;
35600 cp_lexer_consume_token (parser->lexer);
35601 if (!flag_openmp) /* flag_openmp_simd */
35602 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35603 cclauses, if_p);
35604 sb = begin_omp_structured_block ();
35605 save = cp_parser_begin_omp_structured_block (parser);
35606 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35607 cclauses, if_p);
35608 cp_parser_end_omp_structured_block (parser, save);
35609 tree body = finish_omp_structured_block (sb);
35610 if (ret == NULL)
35611 return ret;
35612 ret = make_node (OMP_FOR);
35613 TREE_TYPE (ret) = void_type_node;
35614 OMP_FOR_BODY (ret) = body;
35615 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35616 SET_EXPR_LOCATION (ret, loc);
35617 add_stmt (ret);
35618 return ret;
35621 if (!flag_openmp) /* flag_openmp_simd */
35623 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35624 return NULL_TREE;
35627 /* Composite distribute parallel for disallows linear clause. */
35628 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35629 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35631 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35632 cclauses == NULL);
35633 if (cclauses)
35635 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35636 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35639 sb = begin_omp_structured_block ();
35640 save = cp_parser_begin_omp_structured_block (parser);
35642 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35644 cp_parser_end_omp_structured_block (parser, save);
35645 add_stmt (finish_omp_structured_block (sb));
35647 return ret;
35650 /* OpenMP 2.5:
35651 # pragma omp master new-line
35652 structured-block */
35654 static tree
35655 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35657 cp_parser_require_pragma_eol (parser, pragma_tok);
35658 return c_finish_omp_master (input_location,
35659 cp_parser_omp_structured_block (parser, if_p));
35662 /* OpenMP 2.5:
35663 # pragma omp ordered new-line
35664 structured-block
35666 OpenMP 4.5:
35667 # pragma omp ordered ordered-clauses new-line
35668 structured-block */
35670 #define OMP_ORDERED_CLAUSE_MASK \
35671 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35674 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35675 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35677 static bool
35678 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35679 enum pragma_context context, bool *if_p)
35681 location_t loc = pragma_tok->location;
35683 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35685 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35686 const char *p = IDENTIFIER_POINTER (id);
35688 if (strcmp (p, "depend") == 0)
35690 if (!flag_openmp) /* flag_openmp_simd */
35692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35693 return false;
35695 if (context == pragma_stmt)
35697 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35698 "%<depend%> clause may only be used in compound "
35699 "statements");
35700 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35701 return false;
35703 tree clauses
35704 = cp_parser_omp_all_clauses (parser,
35705 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35706 "#pragma omp ordered", pragma_tok);
35707 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35708 return false;
35712 tree clauses
35713 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35714 "#pragma omp ordered", pragma_tok);
35716 if (!flag_openmp /* flag_openmp_simd */
35717 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35718 return false;
35720 c_finish_omp_ordered (loc, clauses,
35721 cp_parser_omp_structured_block (parser, if_p));
35722 return true;
35725 /* OpenMP 2.5:
35727 section-scope:
35728 { section-sequence }
35730 section-sequence:
35731 section-directive[opt] structured-block
35732 section-sequence section-directive structured-block */
35734 static tree
35735 cp_parser_omp_sections_scope (cp_parser *parser)
35737 tree stmt, substmt;
35738 bool error_suppress = false;
35739 cp_token *tok;
35741 matching_braces braces;
35742 if (!braces.require_open (parser))
35743 return NULL_TREE;
35745 stmt = push_stmt_list ();
35747 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35748 != PRAGMA_OMP_SECTION)
35750 substmt = cp_parser_omp_structured_block (parser, NULL);
35751 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35752 add_stmt (substmt);
35755 while (1)
35757 tok = cp_lexer_peek_token (parser->lexer);
35758 if (tok->type == CPP_CLOSE_BRACE)
35759 break;
35760 if (tok->type == CPP_EOF)
35761 break;
35763 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35765 cp_lexer_consume_token (parser->lexer);
35766 cp_parser_require_pragma_eol (parser, tok);
35767 error_suppress = false;
35769 else if (!error_suppress)
35771 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35772 error_suppress = true;
35775 substmt = cp_parser_omp_structured_block (parser, NULL);
35776 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35777 add_stmt (substmt);
35779 braces.require_close (parser);
35781 substmt = pop_stmt_list (stmt);
35783 stmt = make_node (OMP_SECTIONS);
35784 TREE_TYPE (stmt) = void_type_node;
35785 OMP_SECTIONS_BODY (stmt) = substmt;
35787 add_stmt (stmt);
35788 return stmt;
35791 /* OpenMP 2.5:
35792 # pragma omp sections sections-clause[optseq] newline
35793 sections-scope */
35795 #define OMP_SECTIONS_CLAUSE_MASK \
35796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35802 static tree
35803 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35804 char *p_name, omp_clause_mask mask, tree *cclauses)
35806 tree clauses, ret;
35807 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35809 strcat (p_name, " sections");
35810 mask |= OMP_SECTIONS_CLAUSE_MASK;
35811 if (cclauses)
35812 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35814 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35815 cclauses == NULL);
35816 if (cclauses)
35818 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35819 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35822 ret = cp_parser_omp_sections_scope (parser);
35823 if (ret)
35824 OMP_SECTIONS_CLAUSES (ret) = clauses;
35826 return ret;
35829 /* OpenMP 2.5:
35830 # pragma omp parallel parallel-clause[optseq] new-line
35831 structured-block
35832 # pragma omp parallel for parallel-for-clause[optseq] new-line
35833 structured-block
35834 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35835 structured-block
35837 OpenMP 4.0:
35838 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35839 structured-block */
35841 #define OMP_PARALLEL_CLAUSE_MASK \
35842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35852 static tree
35853 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35854 char *p_name, omp_clause_mask mask, tree *cclauses,
35855 bool *if_p)
35857 tree stmt, clauses, block;
35858 unsigned int save;
35859 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35861 strcat (p_name, " parallel");
35862 mask |= OMP_PARALLEL_CLAUSE_MASK;
35863 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35864 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35865 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35866 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35868 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35870 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35871 if (cclauses == NULL)
35872 cclauses = cclauses_buf;
35874 cp_lexer_consume_token (parser->lexer);
35875 if (!flag_openmp) /* flag_openmp_simd */
35876 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35877 if_p);
35878 block = begin_omp_parallel ();
35879 save = cp_parser_begin_omp_structured_block (parser);
35880 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35881 if_p);
35882 cp_parser_end_omp_structured_block (parser, save);
35883 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35884 block);
35885 if (ret == NULL_TREE)
35886 return ret;
35887 OMP_PARALLEL_COMBINED (stmt) = 1;
35888 return stmt;
35890 /* When combined with distribute, parallel has to be followed by for.
35891 #pragma omp target parallel is allowed though. */
35892 else if (cclauses
35893 && (mask & (OMP_CLAUSE_MASK_1
35894 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35896 error_at (loc, "expected %<for%> after %qs", p_name);
35897 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35898 return NULL_TREE;
35900 else if (!flag_openmp) /* flag_openmp_simd */
35902 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35903 return NULL_TREE;
35905 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35907 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35908 const char *p = IDENTIFIER_POINTER (id);
35909 if (strcmp (p, "sections") == 0)
35911 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35912 cclauses = cclauses_buf;
35914 cp_lexer_consume_token (parser->lexer);
35915 block = begin_omp_parallel ();
35916 save = cp_parser_begin_omp_structured_block (parser);
35917 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35918 cp_parser_end_omp_structured_block (parser, save);
35919 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35920 block);
35921 OMP_PARALLEL_COMBINED (stmt) = 1;
35922 return stmt;
35926 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35927 cclauses == NULL);
35928 if (cclauses)
35930 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35931 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35934 block = begin_omp_parallel ();
35935 save = cp_parser_begin_omp_structured_block (parser);
35936 cp_parser_statement (parser, NULL_TREE, false, if_p);
35937 cp_parser_end_omp_structured_block (parser, save);
35938 stmt = finish_omp_parallel (clauses, block);
35939 return stmt;
35942 /* OpenMP 2.5:
35943 # pragma omp single single-clause[optseq] new-line
35944 structured-block */
35946 #define OMP_SINGLE_CLAUSE_MASK \
35947 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35952 static tree
35953 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35955 tree stmt = make_node (OMP_SINGLE);
35956 TREE_TYPE (stmt) = void_type_node;
35958 OMP_SINGLE_CLAUSES (stmt)
35959 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35960 "#pragma omp single", pragma_tok);
35961 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35963 return add_stmt (stmt);
35966 /* OpenMP 3.0:
35967 # pragma omp task task-clause[optseq] new-line
35968 structured-block */
35970 #define OMP_TASK_CLAUSE_MASK \
35971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35982 static tree
35983 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35985 tree clauses, block;
35986 unsigned int save;
35988 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35989 "#pragma omp task", pragma_tok);
35990 block = begin_omp_task ();
35991 save = cp_parser_begin_omp_structured_block (parser);
35992 cp_parser_statement (parser, NULL_TREE, false, if_p);
35993 cp_parser_end_omp_structured_block (parser, save);
35994 return finish_omp_task (clauses, block);
35997 /* OpenMP 3.0:
35998 # pragma omp taskwait new-line */
36000 static void
36001 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
36003 cp_parser_require_pragma_eol (parser, pragma_tok);
36004 finish_omp_taskwait ();
36007 /* OpenMP 3.1:
36008 # pragma omp taskyield new-line */
36010 static void
36011 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36013 cp_parser_require_pragma_eol (parser, pragma_tok);
36014 finish_omp_taskyield ();
36017 /* OpenMP 4.0:
36018 # pragma omp taskgroup new-line
36019 structured-block */
36021 static tree
36022 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36024 cp_parser_require_pragma_eol (parser, pragma_tok);
36025 return c_finish_omp_taskgroup (input_location,
36026 cp_parser_omp_structured_block (parser,
36027 if_p));
36031 /* OpenMP 2.5:
36032 # pragma omp threadprivate (variable-list) */
36034 static void
36035 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36037 tree vars;
36039 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36040 cp_parser_require_pragma_eol (parser, pragma_tok);
36042 finish_omp_threadprivate (vars);
36045 /* OpenMP 4.0:
36046 # pragma omp cancel cancel-clause[optseq] new-line */
36048 #define OMP_CANCEL_CLAUSE_MASK \
36049 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36055 static void
36056 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36058 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36059 "#pragma omp cancel", pragma_tok);
36060 finish_omp_cancel (clauses);
36063 /* OpenMP 4.0:
36064 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36066 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36072 static void
36073 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36074 enum pragma_context context)
36076 tree clauses;
36077 bool point_seen = false;
36079 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36081 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36082 const char *p = IDENTIFIER_POINTER (id);
36084 if (strcmp (p, "point") == 0)
36086 cp_lexer_consume_token (parser->lexer);
36087 point_seen = true;
36090 if (!point_seen)
36092 cp_parser_error (parser, "expected %<point%>");
36093 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36094 return;
36097 if (context != pragma_compound)
36099 if (context == pragma_stmt)
36100 error_at (pragma_tok->location,
36101 "%<#pragma %s%> may only be used in compound statements",
36102 "omp cancellation point");
36103 else
36104 cp_parser_error (parser, "expected declaration specifiers");
36105 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36106 return;
36109 clauses = cp_parser_omp_all_clauses (parser,
36110 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36111 "#pragma omp cancellation point",
36112 pragma_tok);
36113 finish_omp_cancellation_point (clauses);
36116 /* OpenMP 4.0:
36117 #pragma omp distribute distribute-clause[optseq] new-line
36118 for-loop */
36120 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36127 static tree
36128 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36129 char *p_name, omp_clause_mask mask, tree *cclauses,
36130 bool *if_p)
36132 tree clauses, sb, ret;
36133 unsigned int save;
36134 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36136 strcat (p_name, " distribute");
36137 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36141 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36142 const char *p = IDENTIFIER_POINTER (id);
36143 bool simd = false;
36144 bool parallel = false;
36146 if (strcmp (p, "simd") == 0)
36147 simd = true;
36148 else
36149 parallel = strcmp (p, "parallel") == 0;
36150 if (parallel || simd)
36152 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36153 if (cclauses == NULL)
36154 cclauses = cclauses_buf;
36155 cp_lexer_consume_token (parser->lexer);
36156 if (!flag_openmp) /* flag_openmp_simd */
36158 if (simd)
36159 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36160 cclauses, if_p);
36161 else
36162 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36163 cclauses, if_p);
36165 sb = begin_omp_structured_block ();
36166 save = cp_parser_begin_omp_structured_block (parser);
36167 if (simd)
36168 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36169 cclauses, if_p);
36170 else
36171 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36172 cclauses, if_p);
36173 cp_parser_end_omp_structured_block (parser, save);
36174 tree body = finish_omp_structured_block (sb);
36175 if (ret == NULL)
36176 return ret;
36177 ret = make_node (OMP_DISTRIBUTE);
36178 TREE_TYPE (ret) = void_type_node;
36179 OMP_FOR_BODY (ret) = body;
36180 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36181 SET_EXPR_LOCATION (ret, loc);
36182 add_stmt (ret);
36183 return ret;
36186 if (!flag_openmp) /* flag_openmp_simd */
36188 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36189 return NULL_TREE;
36192 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36193 cclauses == NULL);
36194 if (cclauses)
36196 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36197 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36200 sb = begin_omp_structured_block ();
36201 save = cp_parser_begin_omp_structured_block (parser);
36203 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36205 cp_parser_end_omp_structured_block (parser, save);
36206 add_stmt (finish_omp_structured_block (sb));
36208 return ret;
36211 /* OpenMP 4.0:
36212 # pragma omp teams teams-clause[optseq] new-line
36213 structured-block */
36215 #define OMP_TEAMS_CLAUSE_MASK \
36216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36224 static tree
36225 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36226 char *p_name, omp_clause_mask mask, tree *cclauses,
36227 bool *if_p)
36229 tree clauses, sb, ret;
36230 unsigned int save;
36231 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36233 strcat (p_name, " teams");
36234 mask |= OMP_TEAMS_CLAUSE_MASK;
36236 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36238 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36239 const char *p = IDENTIFIER_POINTER (id);
36240 if (strcmp (p, "distribute") == 0)
36242 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36243 if (cclauses == NULL)
36244 cclauses = cclauses_buf;
36246 cp_lexer_consume_token (parser->lexer);
36247 if (!flag_openmp) /* flag_openmp_simd */
36248 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36249 cclauses, if_p);
36250 sb = begin_omp_structured_block ();
36251 save = cp_parser_begin_omp_structured_block (parser);
36252 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36253 cclauses, if_p);
36254 cp_parser_end_omp_structured_block (parser, save);
36255 tree body = finish_omp_structured_block (sb);
36256 if (ret == NULL)
36257 return ret;
36258 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36259 ret = make_node (OMP_TEAMS);
36260 TREE_TYPE (ret) = void_type_node;
36261 OMP_TEAMS_CLAUSES (ret) = clauses;
36262 OMP_TEAMS_BODY (ret) = body;
36263 OMP_TEAMS_COMBINED (ret) = 1;
36264 SET_EXPR_LOCATION (ret, loc);
36265 return add_stmt (ret);
36268 if (!flag_openmp) /* flag_openmp_simd */
36270 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36271 return NULL_TREE;
36274 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36275 cclauses == NULL);
36276 if (cclauses)
36278 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36279 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36282 tree stmt = make_node (OMP_TEAMS);
36283 TREE_TYPE (stmt) = void_type_node;
36284 OMP_TEAMS_CLAUSES (stmt) = clauses;
36285 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36286 SET_EXPR_LOCATION (stmt, loc);
36288 return add_stmt (stmt);
36291 /* OpenMP 4.0:
36292 # pragma omp target data target-data-clause[optseq] new-line
36293 structured-block */
36295 #define OMP_TARGET_DATA_CLAUSE_MASK \
36296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36301 static tree
36302 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36304 tree clauses
36305 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36306 "#pragma omp target data", pragma_tok);
36307 int map_seen = 0;
36308 for (tree *pc = &clauses; *pc;)
36310 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36311 switch (OMP_CLAUSE_MAP_KIND (*pc))
36313 case GOMP_MAP_TO:
36314 case GOMP_MAP_ALWAYS_TO:
36315 case GOMP_MAP_FROM:
36316 case GOMP_MAP_ALWAYS_FROM:
36317 case GOMP_MAP_TOFROM:
36318 case GOMP_MAP_ALWAYS_TOFROM:
36319 case GOMP_MAP_ALLOC:
36320 map_seen = 3;
36321 break;
36322 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36323 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36324 case GOMP_MAP_ALWAYS_POINTER:
36325 break;
36326 default:
36327 map_seen |= 1;
36328 error_at (OMP_CLAUSE_LOCATION (*pc),
36329 "%<#pragma omp target data%> with map-type other "
36330 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36331 "on %<map%> clause");
36332 *pc = OMP_CLAUSE_CHAIN (*pc);
36333 continue;
36335 pc = &OMP_CLAUSE_CHAIN (*pc);
36338 if (map_seen != 3)
36340 if (map_seen == 0)
36341 error_at (pragma_tok->location,
36342 "%<#pragma omp target data%> must contain at least "
36343 "one %<map%> clause");
36344 return NULL_TREE;
36347 tree stmt = make_node (OMP_TARGET_DATA);
36348 TREE_TYPE (stmt) = void_type_node;
36349 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36351 keep_next_level (true);
36352 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36354 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36355 return add_stmt (stmt);
36358 /* OpenMP 4.5:
36359 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36360 structured-block */
36362 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36369 static tree
36370 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36371 enum pragma_context context)
36373 bool data_seen = false;
36374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36377 const char *p = IDENTIFIER_POINTER (id);
36379 if (strcmp (p, "data") == 0)
36381 cp_lexer_consume_token (parser->lexer);
36382 data_seen = true;
36385 if (!data_seen)
36387 cp_parser_error (parser, "expected %<data%>");
36388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36389 return NULL_TREE;
36392 if (context == pragma_stmt)
36394 error_at (pragma_tok->location,
36395 "%<#pragma %s%> may only be used in compound statements",
36396 "omp target enter data");
36397 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36398 return NULL_TREE;
36401 tree clauses
36402 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36403 "#pragma omp target enter data", pragma_tok);
36404 int map_seen = 0;
36405 for (tree *pc = &clauses; *pc;)
36407 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36408 switch (OMP_CLAUSE_MAP_KIND (*pc))
36410 case GOMP_MAP_TO:
36411 case GOMP_MAP_ALWAYS_TO:
36412 case GOMP_MAP_ALLOC:
36413 map_seen = 3;
36414 break;
36415 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36416 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36417 case GOMP_MAP_ALWAYS_POINTER:
36418 break;
36419 default:
36420 map_seen |= 1;
36421 error_at (OMP_CLAUSE_LOCATION (*pc),
36422 "%<#pragma omp target enter data%> with map-type other "
36423 "than %<to%> or %<alloc%> on %<map%> clause");
36424 *pc = OMP_CLAUSE_CHAIN (*pc);
36425 continue;
36427 pc = &OMP_CLAUSE_CHAIN (*pc);
36430 if (map_seen != 3)
36432 if (map_seen == 0)
36433 error_at (pragma_tok->location,
36434 "%<#pragma omp target enter data%> must contain at least "
36435 "one %<map%> clause");
36436 return NULL_TREE;
36439 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36440 TREE_TYPE (stmt) = void_type_node;
36441 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36442 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36443 return add_stmt (stmt);
36446 /* OpenMP 4.5:
36447 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36448 structured-block */
36450 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36451 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36457 static tree
36458 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36459 enum pragma_context context)
36461 bool data_seen = false;
36462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36465 const char *p = IDENTIFIER_POINTER (id);
36467 if (strcmp (p, "data") == 0)
36469 cp_lexer_consume_token (parser->lexer);
36470 data_seen = true;
36473 if (!data_seen)
36475 cp_parser_error (parser, "expected %<data%>");
36476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36477 return NULL_TREE;
36480 if (context == pragma_stmt)
36482 error_at (pragma_tok->location,
36483 "%<#pragma %s%> may only be used in compound statements",
36484 "omp target exit data");
36485 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36486 return NULL_TREE;
36489 tree clauses
36490 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36491 "#pragma omp target exit data", pragma_tok);
36492 int map_seen = 0;
36493 for (tree *pc = &clauses; *pc;)
36495 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36496 switch (OMP_CLAUSE_MAP_KIND (*pc))
36498 case GOMP_MAP_FROM:
36499 case GOMP_MAP_ALWAYS_FROM:
36500 case GOMP_MAP_RELEASE:
36501 case GOMP_MAP_DELETE:
36502 map_seen = 3;
36503 break;
36504 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36505 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36506 case GOMP_MAP_ALWAYS_POINTER:
36507 break;
36508 default:
36509 map_seen |= 1;
36510 error_at (OMP_CLAUSE_LOCATION (*pc),
36511 "%<#pragma omp target exit data%> with map-type other "
36512 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36513 " clause");
36514 *pc = OMP_CLAUSE_CHAIN (*pc);
36515 continue;
36517 pc = &OMP_CLAUSE_CHAIN (*pc);
36520 if (map_seen != 3)
36522 if (map_seen == 0)
36523 error_at (pragma_tok->location,
36524 "%<#pragma omp target exit data%> must contain at least "
36525 "one %<map%> clause");
36526 return NULL_TREE;
36529 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36530 TREE_TYPE (stmt) = void_type_node;
36531 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36532 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36533 return add_stmt (stmt);
36536 /* OpenMP 4.0:
36537 # pragma omp target update target-update-clause[optseq] new-line */
36539 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36547 static bool
36548 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36549 enum pragma_context context)
36551 if (context == pragma_stmt)
36553 error_at (pragma_tok->location,
36554 "%<#pragma %s%> may only be used in compound statements",
36555 "omp target update");
36556 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36557 return false;
36560 tree clauses
36561 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36562 "#pragma omp target update", pragma_tok);
36563 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36564 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36566 error_at (pragma_tok->location,
36567 "%<#pragma omp target update%> must contain at least one "
36568 "%<from%> or %<to%> clauses");
36569 return false;
36572 tree stmt = make_node (OMP_TARGET_UPDATE);
36573 TREE_TYPE (stmt) = void_type_node;
36574 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36575 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36576 add_stmt (stmt);
36577 return false;
36580 /* OpenMP 4.0:
36581 # pragma omp target target-clause[optseq] new-line
36582 structured-block */
36584 #define OMP_TARGET_CLAUSE_MASK \
36585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36595 static bool
36596 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36597 enum pragma_context context, bool *if_p)
36599 tree *pc = NULL, stmt;
36601 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36603 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36604 const char *p = IDENTIFIER_POINTER (id);
36605 enum tree_code ccode = ERROR_MARK;
36607 if (strcmp (p, "teams") == 0)
36608 ccode = OMP_TEAMS;
36609 else if (strcmp (p, "parallel") == 0)
36610 ccode = OMP_PARALLEL;
36611 else if (strcmp (p, "simd") == 0)
36612 ccode = OMP_SIMD;
36613 if (ccode != ERROR_MARK)
36615 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36616 char p_name[sizeof ("#pragma omp target teams distribute "
36617 "parallel for simd")];
36619 cp_lexer_consume_token (parser->lexer);
36620 strcpy (p_name, "#pragma omp target");
36621 if (!flag_openmp) /* flag_openmp_simd */
36623 tree stmt;
36624 switch (ccode)
36626 case OMP_TEAMS:
36627 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36628 OMP_TARGET_CLAUSE_MASK,
36629 cclauses, if_p);
36630 break;
36631 case OMP_PARALLEL:
36632 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36633 OMP_TARGET_CLAUSE_MASK,
36634 cclauses, if_p);
36635 break;
36636 case OMP_SIMD:
36637 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36638 OMP_TARGET_CLAUSE_MASK,
36639 cclauses, if_p);
36640 break;
36641 default:
36642 gcc_unreachable ();
36644 return stmt != NULL_TREE;
36646 keep_next_level (true);
36647 tree sb = begin_omp_structured_block (), ret;
36648 unsigned save = cp_parser_begin_omp_structured_block (parser);
36649 switch (ccode)
36651 case OMP_TEAMS:
36652 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36653 OMP_TARGET_CLAUSE_MASK, cclauses,
36654 if_p);
36655 break;
36656 case OMP_PARALLEL:
36657 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36658 OMP_TARGET_CLAUSE_MASK, cclauses,
36659 if_p);
36660 break;
36661 case OMP_SIMD:
36662 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36663 OMP_TARGET_CLAUSE_MASK, cclauses,
36664 if_p);
36665 break;
36666 default:
36667 gcc_unreachable ();
36669 cp_parser_end_omp_structured_block (parser, save);
36670 tree body = finish_omp_structured_block (sb);
36671 if (ret == NULL_TREE)
36672 return false;
36673 if (ccode == OMP_TEAMS && !processing_template_decl)
36675 /* For combined target teams, ensure the num_teams and
36676 thread_limit clause expressions are evaluated on the host,
36677 before entering the target construct. */
36678 tree c;
36679 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36680 c; c = OMP_CLAUSE_CHAIN (c))
36681 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36682 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36683 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36685 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36686 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36687 if (expr == error_mark_node)
36688 continue;
36689 tree tmp = TARGET_EXPR_SLOT (expr);
36690 add_stmt (expr);
36691 OMP_CLAUSE_OPERAND (c, 0) = expr;
36692 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36693 OMP_CLAUSE_FIRSTPRIVATE);
36694 OMP_CLAUSE_DECL (tc) = tmp;
36695 OMP_CLAUSE_CHAIN (tc)
36696 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36697 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36700 tree stmt = make_node (OMP_TARGET);
36701 TREE_TYPE (stmt) = void_type_node;
36702 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36703 OMP_TARGET_BODY (stmt) = body;
36704 OMP_TARGET_COMBINED (stmt) = 1;
36705 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36706 add_stmt (stmt);
36707 pc = &OMP_TARGET_CLAUSES (stmt);
36708 goto check_clauses;
36710 else if (!flag_openmp) /* flag_openmp_simd */
36712 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36713 return false;
36715 else if (strcmp (p, "data") == 0)
36717 cp_lexer_consume_token (parser->lexer);
36718 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36719 return true;
36721 else if (strcmp (p, "enter") == 0)
36723 cp_lexer_consume_token (parser->lexer);
36724 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36725 return false;
36727 else if (strcmp (p, "exit") == 0)
36729 cp_lexer_consume_token (parser->lexer);
36730 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36731 return false;
36733 else if (strcmp (p, "update") == 0)
36735 cp_lexer_consume_token (parser->lexer);
36736 return cp_parser_omp_target_update (parser, pragma_tok, context);
36739 if (!flag_openmp) /* flag_openmp_simd */
36741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36742 return false;
36745 stmt = make_node (OMP_TARGET);
36746 TREE_TYPE (stmt) = void_type_node;
36748 OMP_TARGET_CLAUSES (stmt)
36749 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36750 "#pragma omp target", pragma_tok);
36751 pc = &OMP_TARGET_CLAUSES (stmt);
36752 keep_next_level (true);
36753 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36755 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36756 add_stmt (stmt);
36758 check_clauses:
36759 while (*pc)
36761 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36762 switch (OMP_CLAUSE_MAP_KIND (*pc))
36764 case GOMP_MAP_TO:
36765 case GOMP_MAP_ALWAYS_TO:
36766 case GOMP_MAP_FROM:
36767 case GOMP_MAP_ALWAYS_FROM:
36768 case GOMP_MAP_TOFROM:
36769 case GOMP_MAP_ALWAYS_TOFROM:
36770 case GOMP_MAP_ALLOC:
36771 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36772 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36773 case GOMP_MAP_ALWAYS_POINTER:
36774 break;
36775 default:
36776 error_at (OMP_CLAUSE_LOCATION (*pc),
36777 "%<#pragma omp target%> with map-type other "
36778 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36779 "on %<map%> clause");
36780 *pc = OMP_CLAUSE_CHAIN (*pc);
36781 continue;
36783 pc = &OMP_CLAUSE_CHAIN (*pc);
36785 return true;
36788 /* OpenACC 2.0:
36789 # pragma acc cache (variable-list) new-line
36792 static tree
36793 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36795 tree stmt, clauses;
36797 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36798 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36800 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36802 stmt = make_node (OACC_CACHE);
36803 TREE_TYPE (stmt) = void_type_node;
36804 OACC_CACHE_CLAUSES (stmt) = clauses;
36805 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36806 add_stmt (stmt);
36808 return stmt;
36811 /* OpenACC 2.0:
36812 # pragma acc data oacc-data-clause[optseq] new-line
36813 structured-block */
36815 #define OACC_DATA_CLAUSE_MASK \
36816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36824 static tree
36825 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36827 tree stmt, clauses, block;
36828 unsigned int save;
36830 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36831 "#pragma acc data", pragma_tok);
36833 block = begin_omp_parallel ();
36834 save = cp_parser_begin_omp_structured_block (parser);
36835 cp_parser_statement (parser, NULL_TREE, false, if_p);
36836 cp_parser_end_omp_structured_block (parser, save);
36837 stmt = finish_oacc_data (clauses, block);
36838 return stmt;
36841 /* OpenACC 2.0:
36842 # pragma acc host_data <clauses> new-line
36843 structured-block */
36845 #define OACC_HOST_DATA_CLAUSE_MASK \
36846 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36848 static tree
36849 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36851 tree stmt, clauses, block;
36852 unsigned int save;
36854 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36855 "#pragma acc host_data", pragma_tok);
36857 block = begin_omp_parallel ();
36858 save = cp_parser_begin_omp_structured_block (parser);
36859 cp_parser_statement (parser, NULL_TREE, false, if_p);
36860 cp_parser_end_omp_structured_block (parser, save);
36861 stmt = finish_oacc_host_data (clauses, block);
36862 return stmt;
36865 /* OpenACC 2.0:
36866 # pragma acc declare oacc-data-clause[optseq] new-line
36869 #define OACC_DECLARE_CLAUSE_MASK \
36870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36879 static tree
36880 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36882 tree clauses, stmt;
36883 bool error = false;
36885 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36886 "#pragma acc declare", pragma_tok, true);
36889 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36891 error_at (pragma_tok->location,
36892 "no valid clauses specified in %<#pragma acc declare%>");
36893 return NULL_TREE;
36896 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36898 location_t loc = OMP_CLAUSE_LOCATION (t);
36899 tree decl = OMP_CLAUSE_DECL (t);
36900 if (!DECL_P (decl))
36902 error_at (loc, "array section in %<#pragma acc declare%>");
36903 error = true;
36904 continue;
36906 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36907 switch (OMP_CLAUSE_MAP_KIND (t))
36909 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36910 case GOMP_MAP_ALLOC:
36911 case GOMP_MAP_TO:
36912 case GOMP_MAP_FORCE_DEVICEPTR:
36913 case GOMP_MAP_DEVICE_RESIDENT:
36914 break;
36916 case GOMP_MAP_LINK:
36917 if (!global_bindings_p ()
36918 && (TREE_STATIC (decl)
36919 || !DECL_EXTERNAL (decl)))
36921 error_at (loc,
36922 "%qD must be a global variable in "
36923 "%<#pragma acc declare link%>",
36924 decl);
36925 error = true;
36926 continue;
36928 break;
36930 default:
36931 if (global_bindings_p ())
36933 error_at (loc, "invalid OpenACC clause at file scope");
36934 error = true;
36935 continue;
36937 if (DECL_EXTERNAL (decl))
36939 error_at (loc,
36940 "invalid use of %<extern%> variable %qD "
36941 "in %<#pragma acc declare%>", decl);
36942 error = true;
36943 continue;
36945 else if (TREE_PUBLIC (decl))
36947 error_at (loc,
36948 "invalid use of %<global%> variable %qD "
36949 "in %<#pragma acc declare%>", decl);
36950 error = true;
36951 continue;
36953 break;
36956 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36957 || lookup_attribute ("omp declare target link",
36958 DECL_ATTRIBUTES (decl)))
36960 error_at (loc, "variable %qD used more than once with "
36961 "%<#pragma acc declare%>", decl);
36962 error = true;
36963 continue;
36966 if (!error)
36968 tree id;
36970 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36971 id = get_identifier ("omp declare target link");
36972 else
36973 id = get_identifier ("omp declare target");
36975 DECL_ATTRIBUTES (decl)
36976 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36977 if (global_bindings_p ())
36979 symtab_node *node = symtab_node::get (decl);
36980 if (node != NULL)
36982 node->offloadable = 1;
36983 if (ENABLE_OFFLOADING)
36985 g->have_offload = true;
36986 if (is_a <varpool_node *> (node))
36987 vec_safe_push (offload_vars, decl);
36994 if (error || global_bindings_p ())
36995 return NULL_TREE;
36997 stmt = make_node (OACC_DECLARE);
36998 TREE_TYPE (stmt) = void_type_node;
36999 OACC_DECLARE_CLAUSES (stmt) = clauses;
37000 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37002 add_stmt (stmt);
37004 return NULL_TREE;
37007 /* OpenACC 2.0:
37008 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37012 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37014 LOC is the location of the #pragma token.
37017 #define OACC_ENTER_DATA_CLAUSE_MASK \
37018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37024 #define OACC_EXIT_DATA_CLAUSE_MASK \
37025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37032 static tree
37033 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37034 bool enter)
37036 location_t loc = pragma_tok->location;
37037 tree stmt, clauses;
37038 const char *p = "";
37040 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37041 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37043 if (strcmp (p, "data") != 0)
37045 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37046 enter ? "enter" : "exit");
37047 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37048 return NULL_TREE;
37051 cp_lexer_consume_token (parser->lexer);
37053 if (enter)
37054 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37055 "#pragma acc enter data", pragma_tok);
37056 else
37057 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37058 "#pragma acc exit data", pragma_tok);
37060 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37062 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37063 enter ? "enter" : "exit");
37064 return NULL_TREE;
37067 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37068 TREE_TYPE (stmt) = void_type_node;
37069 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37070 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37071 add_stmt (stmt);
37072 return stmt;
37075 /* OpenACC 2.0:
37076 # pragma acc loop oacc-loop-clause[optseq] new-line
37077 structured-block */
37079 #define OACC_LOOP_CLAUSE_MASK \
37080 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37091 static tree
37092 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37093 omp_clause_mask mask, tree *cclauses, bool *if_p)
37095 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37097 strcat (p_name, " loop");
37098 mask |= OACC_LOOP_CLAUSE_MASK;
37100 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37101 cclauses == NULL);
37102 if (cclauses)
37104 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37105 if (*cclauses)
37106 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37107 if (clauses)
37108 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37111 tree block = begin_omp_structured_block ();
37112 int save = cp_parser_begin_omp_structured_block (parser);
37113 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37114 cp_parser_end_omp_structured_block (parser, save);
37115 add_stmt (finish_omp_structured_block (block));
37117 return stmt;
37120 /* OpenACC 2.0:
37121 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37122 structured-block
37126 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37127 structured-block
37130 #define OACC_KERNELS_CLAUSE_MASK \
37131 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37145 #define OACC_PARALLEL_CLAUSE_MASK \
37146 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37163 static tree
37164 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37165 char *p_name, bool *if_p)
37167 omp_clause_mask mask;
37168 enum tree_code code;
37169 switch (cp_parser_pragma_kind (pragma_tok))
37171 case PRAGMA_OACC_KERNELS:
37172 strcat (p_name, " kernels");
37173 mask = OACC_KERNELS_CLAUSE_MASK;
37174 code = OACC_KERNELS;
37175 break;
37176 case PRAGMA_OACC_PARALLEL:
37177 strcat (p_name, " parallel");
37178 mask = OACC_PARALLEL_CLAUSE_MASK;
37179 code = OACC_PARALLEL;
37180 break;
37181 default:
37182 gcc_unreachable ();
37185 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37187 const char *p
37188 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37189 if (strcmp (p, "loop") == 0)
37191 cp_lexer_consume_token (parser->lexer);
37192 tree block = begin_omp_parallel ();
37193 tree clauses;
37194 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37195 if_p);
37196 return finish_omp_construct (code, block, clauses);
37200 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37202 tree block = begin_omp_parallel ();
37203 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37204 cp_parser_statement (parser, NULL_TREE, false, if_p);
37205 cp_parser_end_omp_structured_block (parser, save);
37206 return finish_omp_construct (code, block, clauses);
37209 /* OpenACC 2.0:
37210 # pragma acc update oacc-update-clause[optseq] new-line
37213 #define OACC_UPDATE_CLAUSE_MASK \
37214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37221 static tree
37222 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37224 tree stmt, clauses;
37226 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37227 "#pragma acc update", pragma_tok);
37229 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37231 error_at (pragma_tok->location,
37232 "%<#pragma acc update%> must contain at least one "
37233 "%<device%> or %<host%> or %<self%> clause");
37234 return NULL_TREE;
37237 stmt = make_node (OACC_UPDATE);
37238 TREE_TYPE (stmt) = void_type_node;
37239 OACC_UPDATE_CLAUSES (stmt) = clauses;
37240 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37241 add_stmt (stmt);
37242 return stmt;
37245 /* OpenACC 2.0:
37246 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37248 LOC is the location of the #pragma token.
37251 #define OACC_WAIT_CLAUSE_MASK \
37252 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37254 static tree
37255 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37257 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37258 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37260 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37261 list = cp_parser_oacc_wait_list (parser, loc, list);
37263 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37264 "#pragma acc wait", pragma_tok);
37266 stmt = c_finish_oacc_wait (loc, list, clauses);
37267 stmt = finish_expr_stmt (stmt);
37269 return stmt;
37272 /* OpenMP 4.0:
37273 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37275 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37283 static void
37284 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37285 enum pragma_context context)
37287 bool first_p = parser->omp_declare_simd == NULL;
37288 cp_omp_declare_simd_data data;
37289 if (first_p)
37291 data.error_seen = false;
37292 data.fndecl_seen = false;
37293 data.tokens = vNULL;
37294 data.clauses = NULL_TREE;
37295 /* It is safe to take the address of a local variable; it will only be
37296 used while this scope is live. */
37297 parser->omp_declare_simd = &data;
37300 /* Store away all pragma tokens. */
37301 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37302 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37303 cp_lexer_consume_token (parser->lexer);
37304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37305 parser->omp_declare_simd->error_seen = true;
37306 cp_parser_require_pragma_eol (parser, pragma_tok);
37307 struct cp_token_cache *cp
37308 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37309 parser->omp_declare_simd->tokens.safe_push (cp);
37311 if (first_p)
37313 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37314 cp_parser_pragma (parser, context, NULL);
37315 switch (context)
37317 case pragma_external:
37318 cp_parser_declaration (parser);
37319 break;
37320 case pragma_member:
37321 cp_parser_member_declaration (parser);
37322 break;
37323 case pragma_objc_icode:
37324 cp_parser_block_declaration (parser, /*statement_p=*/false);
37325 break;
37326 default:
37327 cp_parser_declaration_statement (parser);
37328 break;
37330 if (parser->omp_declare_simd
37331 && !parser->omp_declare_simd->error_seen
37332 && !parser->omp_declare_simd->fndecl_seen)
37333 error_at (pragma_tok->location,
37334 "%<#pragma omp declare simd%> not immediately followed by "
37335 "function declaration or definition");
37336 data.tokens.release ();
37337 parser->omp_declare_simd = NULL;
37341 /* Finalize #pragma omp declare simd clauses after direct declarator has
37342 been parsed, and put that into "omp declare simd" attribute. */
37344 static tree
37345 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37347 struct cp_token_cache *ce;
37348 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37349 int i;
37351 if (!data->error_seen && data->fndecl_seen)
37353 error ("%<#pragma omp declare simd%> not immediately followed by "
37354 "a single function declaration or definition");
37355 data->error_seen = true;
37357 if (data->error_seen)
37358 return attrs;
37360 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37362 tree c, cl;
37364 cp_parser_push_lexer_for_tokens (parser, ce);
37365 parser->lexer->in_pragma = true;
37366 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37367 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37368 cp_lexer_consume_token (parser->lexer);
37369 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37370 "#pragma omp declare simd", pragma_tok);
37371 cp_parser_pop_lexer (parser);
37372 if (cl)
37373 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37374 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37375 TREE_CHAIN (c) = attrs;
37376 if (processing_template_decl)
37377 ATTR_IS_DEPENDENT (c) = 1;
37378 attrs = c;
37381 data->fndecl_seen = true;
37382 return attrs;
37386 /* OpenMP 4.0:
37387 # pragma omp declare target new-line
37388 declarations and definitions
37389 # pragma omp end declare target new-line
37391 OpenMP 4.5:
37392 # pragma omp declare target ( extended-list ) new-line
37394 # pragma omp declare target declare-target-clauses[seq] new-line */
37396 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37397 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37400 static void
37401 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37403 tree clauses = NULL_TREE;
37404 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37405 clauses
37406 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37407 "#pragma omp declare target", pragma_tok);
37408 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37410 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37411 clauses);
37412 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37413 cp_parser_require_pragma_eol (parser, pragma_tok);
37415 else
37417 cp_parser_require_pragma_eol (parser, pragma_tok);
37418 scope_chain->omp_declare_target_attribute++;
37419 return;
37421 if (scope_chain->omp_declare_target_attribute)
37422 error_at (pragma_tok->location,
37423 "%<#pragma omp declare target%> with clauses in between "
37424 "%<#pragma omp declare target%> without clauses and "
37425 "%<#pragma omp end declare target%>");
37426 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37428 tree t = OMP_CLAUSE_DECL (c), id;
37429 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37430 tree at2 = lookup_attribute ("omp declare target link",
37431 DECL_ATTRIBUTES (t));
37432 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37434 id = get_identifier ("omp declare target link");
37435 std::swap (at1, at2);
37437 else
37438 id = get_identifier ("omp declare target");
37439 if (at2)
37441 error_at (OMP_CLAUSE_LOCATION (c),
37442 "%qD specified both in declare target %<link%> and %<to%>"
37443 " clauses", t);
37444 continue;
37446 if (!at1)
37448 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37449 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37450 continue;
37452 symtab_node *node = symtab_node::get (t);
37453 if (node != NULL)
37455 node->offloadable = 1;
37456 if (ENABLE_OFFLOADING)
37458 g->have_offload = true;
37459 if (is_a <varpool_node *> (node))
37460 vec_safe_push (offload_vars, t);
37467 static void
37468 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37470 const char *p = "";
37471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37474 p = IDENTIFIER_POINTER (id);
37476 if (strcmp (p, "declare") == 0)
37478 cp_lexer_consume_token (parser->lexer);
37479 p = "";
37480 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37482 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37483 p = IDENTIFIER_POINTER (id);
37485 if (strcmp (p, "target") == 0)
37486 cp_lexer_consume_token (parser->lexer);
37487 else
37489 cp_parser_error (parser, "expected %<target%>");
37490 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37491 return;
37494 else
37496 cp_parser_error (parser, "expected %<declare%>");
37497 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37498 return;
37500 cp_parser_require_pragma_eol (parser, pragma_tok);
37501 if (!scope_chain->omp_declare_target_attribute)
37502 error_at (pragma_tok->location,
37503 "%<#pragma omp end declare target%> without corresponding "
37504 "%<#pragma omp declare target%>");
37505 else
37506 scope_chain->omp_declare_target_attribute--;
37509 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37510 expression and optional initializer clause of
37511 #pragma omp declare reduction. We store the expression(s) as
37512 either 3, 6 or 7 special statements inside of the artificial function's
37513 body. The first two statements are DECL_EXPRs for the artificial
37514 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37515 expression that uses those variables.
37516 If there was any INITIALIZER clause, this is followed by further statements,
37517 the fourth and fifth statements are DECL_EXPRs for the artificial
37518 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37519 constructor variant (first token after open paren is not omp_priv),
37520 then the sixth statement is a statement with the function call expression
37521 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37522 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37523 to initialize the OMP_PRIV artificial variable and there is seventh
37524 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37526 static bool
37527 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37529 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37530 gcc_assert (TYPE_REF_P (type));
37531 type = TREE_TYPE (type);
37532 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37533 DECL_ARTIFICIAL (omp_out) = 1;
37534 pushdecl (omp_out);
37535 add_decl_expr (omp_out);
37536 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37537 DECL_ARTIFICIAL (omp_in) = 1;
37538 pushdecl (omp_in);
37539 add_decl_expr (omp_in);
37540 tree combiner;
37541 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37543 keep_next_level (true);
37544 tree block = begin_omp_structured_block ();
37545 combiner = cp_parser_expression (parser);
37546 finish_expr_stmt (combiner);
37547 block = finish_omp_structured_block (block);
37548 add_stmt (block);
37550 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37551 return false;
37553 const char *p = "";
37554 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37556 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37557 p = IDENTIFIER_POINTER (id);
37560 if (strcmp (p, "initializer") == 0)
37562 cp_lexer_consume_token (parser->lexer);
37563 matching_parens parens;
37564 if (!parens.require_open (parser))
37565 return false;
37567 p = "";
37568 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37570 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37571 p = IDENTIFIER_POINTER (id);
37574 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37575 DECL_ARTIFICIAL (omp_priv) = 1;
37576 pushdecl (omp_priv);
37577 add_decl_expr (omp_priv);
37578 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37579 DECL_ARTIFICIAL (omp_orig) = 1;
37580 pushdecl (omp_orig);
37581 add_decl_expr (omp_orig);
37583 keep_next_level (true);
37584 block = begin_omp_structured_block ();
37586 bool ctor = false;
37587 if (strcmp (p, "omp_priv") == 0)
37589 bool is_direct_init, is_non_constant_init;
37590 ctor = true;
37591 cp_lexer_consume_token (parser->lexer);
37592 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37593 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37594 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37595 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37596 == CPP_CLOSE_PAREN
37597 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37598 == CPP_CLOSE_PAREN))
37600 finish_omp_structured_block (block);
37601 error ("invalid initializer clause");
37602 return false;
37604 initializer = cp_parser_initializer (parser, &is_direct_init,
37605 &is_non_constant_init);
37606 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37607 NULL_TREE, LOOKUP_ONLYCONVERTING);
37609 else
37611 cp_parser_parse_tentatively (parser);
37612 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37613 /*check_dependency_p=*/true,
37614 /*template_p=*/NULL,
37615 /*declarator_p=*/false,
37616 /*optional_p=*/false);
37617 vec<tree, va_gc> *args;
37618 if (fn_name == error_mark_node
37619 || cp_parser_error_occurred (parser)
37620 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37621 || ((args = cp_parser_parenthesized_expression_list
37622 (parser, non_attr, /*cast_p=*/false,
37623 /*allow_expansion_p=*/true,
37624 /*non_constant_p=*/NULL)),
37625 cp_parser_error_occurred (parser)))
37627 finish_omp_structured_block (block);
37628 cp_parser_abort_tentative_parse (parser);
37629 cp_parser_error (parser, "expected id-expression (arguments)");
37630 return false;
37632 unsigned int i;
37633 tree arg;
37634 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37635 if (arg == omp_priv
37636 || (TREE_CODE (arg) == ADDR_EXPR
37637 && TREE_OPERAND (arg, 0) == omp_priv))
37638 break;
37639 cp_parser_abort_tentative_parse (parser);
37640 if (arg == NULL_TREE)
37641 error ("one of the initializer call arguments should be %<omp_priv%>"
37642 " or %<&omp_priv%>");
37643 initializer = cp_parser_postfix_expression (parser, false, false, false,
37644 false, NULL);
37645 finish_expr_stmt (initializer);
37648 block = finish_omp_structured_block (block);
37649 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37650 add_stmt (block);
37652 if (ctor)
37653 add_decl_expr (omp_orig);
37655 if (!parens.require_close (parser))
37656 return false;
37659 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37660 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37661 UNKNOWN_LOCATION);
37663 return true;
37666 /* OpenMP 4.0
37667 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37668 initializer-clause[opt] new-line
37670 initializer-clause:
37671 initializer (omp_priv initializer)
37672 initializer (function-name (argument-list)) */
37674 static void
37675 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37676 enum pragma_context)
37678 auto_vec<tree> types;
37679 enum tree_code reduc_code = ERROR_MARK;
37680 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37681 unsigned int i;
37682 cp_token *first_token;
37683 cp_token_cache *cp;
37684 int errs;
37685 void *p;
37687 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37688 p = obstack_alloc (&declarator_obstack, 0);
37690 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37691 goto fail;
37693 switch (cp_lexer_peek_token (parser->lexer)->type)
37695 case CPP_PLUS:
37696 reduc_code = PLUS_EXPR;
37697 break;
37698 case CPP_MULT:
37699 reduc_code = MULT_EXPR;
37700 break;
37701 case CPP_MINUS:
37702 reduc_code = MINUS_EXPR;
37703 break;
37704 case CPP_AND:
37705 reduc_code = BIT_AND_EXPR;
37706 break;
37707 case CPP_XOR:
37708 reduc_code = BIT_XOR_EXPR;
37709 break;
37710 case CPP_OR:
37711 reduc_code = BIT_IOR_EXPR;
37712 break;
37713 case CPP_AND_AND:
37714 reduc_code = TRUTH_ANDIF_EXPR;
37715 break;
37716 case CPP_OR_OR:
37717 reduc_code = TRUTH_ORIF_EXPR;
37718 break;
37719 case CPP_NAME:
37720 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37721 break;
37722 default:
37723 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37724 "%<|%>, %<&&%>, %<||%> or identifier");
37725 goto fail;
37728 if (reduc_code != ERROR_MARK)
37729 cp_lexer_consume_token (parser->lexer);
37731 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37732 if (reduc_id == error_mark_node)
37733 goto fail;
37735 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37736 goto fail;
37738 /* Types may not be defined in declare reduction type list. */
37739 const char *saved_message;
37740 saved_message = parser->type_definition_forbidden_message;
37741 parser->type_definition_forbidden_message
37742 = G_("types may not be defined in declare reduction type list");
37743 bool saved_colon_corrects_to_scope_p;
37744 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37745 parser->colon_corrects_to_scope_p = false;
37746 bool saved_colon_doesnt_start_class_def_p;
37747 saved_colon_doesnt_start_class_def_p
37748 = parser->colon_doesnt_start_class_def_p;
37749 parser->colon_doesnt_start_class_def_p = true;
37751 while (true)
37753 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37754 type = cp_parser_type_id (parser);
37755 if (type == error_mark_node)
37757 else if (ARITHMETIC_TYPE_P (type)
37758 && (orig_reduc_id == NULL_TREE
37759 || (TREE_CODE (type) != COMPLEX_TYPE
37760 && (id_equal (orig_reduc_id, "min")
37761 || id_equal (orig_reduc_id, "max")))))
37762 error_at (loc, "predeclared arithmetic type %qT in "
37763 "%<#pragma omp declare reduction%>", type);
37764 else if (TREE_CODE (type) == FUNCTION_TYPE
37765 || TREE_CODE (type) == METHOD_TYPE
37766 || TREE_CODE (type) == ARRAY_TYPE)
37767 error_at (loc, "function or array type %qT in "
37768 "%<#pragma omp declare reduction%>", type);
37769 else if (TYPE_REF_P (type))
37770 error_at (loc, "reference type %qT in "
37771 "%<#pragma omp declare reduction%>", type);
37772 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37773 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37774 "%<#pragma omp declare reduction%>", type);
37775 else
37776 types.safe_push (type);
37778 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37779 cp_lexer_consume_token (parser->lexer);
37780 else
37781 break;
37784 /* Restore the saved message. */
37785 parser->type_definition_forbidden_message = saved_message;
37786 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37787 parser->colon_doesnt_start_class_def_p
37788 = saved_colon_doesnt_start_class_def_p;
37790 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37791 || types.is_empty ())
37793 fail:
37794 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37795 goto done;
37798 first_token = cp_lexer_peek_token (parser->lexer);
37799 cp = NULL;
37800 errs = errorcount;
37801 FOR_EACH_VEC_ELT (types, i, type)
37803 tree fntype
37804 = build_function_type_list (void_type_node,
37805 cp_build_reference_type (type, false),
37806 NULL_TREE);
37807 tree this_reduc_id = reduc_id;
37808 if (!dependent_type_p (type))
37809 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37810 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37811 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37812 DECL_ARTIFICIAL (fndecl) = 1;
37813 DECL_EXTERNAL (fndecl) = 1;
37814 DECL_DECLARED_INLINE_P (fndecl) = 1;
37815 DECL_IGNORED_P (fndecl) = 1;
37816 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37817 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37818 DECL_ATTRIBUTES (fndecl)
37819 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37820 DECL_ATTRIBUTES (fndecl));
37821 if (processing_template_decl)
37822 fndecl = push_template_decl (fndecl);
37823 bool block_scope = false;
37824 tree block = NULL_TREE;
37825 if (current_function_decl)
37827 block_scope = true;
37828 DECL_CONTEXT (fndecl) = global_namespace;
37829 if (!processing_template_decl)
37830 pushdecl (fndecl);
37832 else if (current_class_type)
37834 if (cp == NULL)
37836 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37837 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37838 cp_lexer_consume_token (parser->lexer);
37839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37840 goto fail;
37841 cp = cp_token_cache_new (first_token,
37842 cp_lexer_peek_nth_token (parser->lexer,
37843 2));
37845 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37846 finish_member_declaration (fndecl);
37847 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37848 DECL_PENDING_INLINE_P (fndecl) = 1;
37849 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37850 continue;
37852 else
37854 DECL_CONTEXT (fndecl) = current_namespace;
37855 pushdecl (fndecl);
37857 if (!block_scope)
37858 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37859 else
37860 block = begin_omp_structured_block ();
37861 if (cp)
37863 cp_parser_push_lexer_for_tokens (parser, cp);
37864 parser->lexer->in_pragma = true;
37866 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37868 if (!block_scope)
37869 finish_function (/*inline_p=*/false);
37870 else
37871 DECL_CONTEXT (fndecl) = current_function_decl;
37872 if (cp)
37873 cp_parser_pop_lexer (parser);
37874 goto fail;
37876 if (cp)
37877 cp_parser_pop_lexer (parser);
37878 if (!block_scope)
37879 finish_function (/*inline_p=*/false);
37880 else
37882 DECL_CONTEXT (fndecl) = current_function_decl;
37883 block = finish_omp_structured_block (block);
37884 if (TREE_CODE (block) == BIND_EXPR)
37885 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37886 else if (TREE_CODE (block) == STATEMENT_LIST)
37887 DECL_SAVED_TREE (fndecl) = block;
37888 if (processing_template_decl)
37889 add_decl_expr (fndecl);
37891 cp_check_omp_declare_reduction (fndecl);
37892 if (cp == NULL && types.length () > 1)
37893 cp = cp_token_cache_new (first_token,
37894 cp_lexer_peek_nth_token (parser->lexer, 2));
37895 if (errs != errorcount)
37896 break;
37899 cp_parser_require_pragma_eol (parser, pragma_tok);
37901 done:
37902 /* Free any declarators allocated. */
37903 obstack_free (&declarator_obstack, p);
37906 /* OpenMP 4.0
37907 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37908 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37909 initializer-clause[opt] new-line
37910 #pragma omp declare target new-line */
37912 static bool
37913 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37914 enum pragma_context context)
37916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37918 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37919 const char *p = IDENTIFIER_POINTER (id);
37921 if (strcmp (p, "simd") == 0)
37923 cp_lexer_consume_token (parser->lexer);
37924 cp_parser_omp_declare_simd (parser, pragma_tok,
37925 context);
37926 return true;
37928 cp_ensure_no_omp_declare_simd (parser);
37929 if (strcmp (p, "reduction") == 0)
37931 cp_lexer_consume_token (parser->lexer);
37932 cp_parser_omp_declare_reduction (parser, pragma_tok,
37933 context);
37934 return false;
37936 if (!flag_openmp) /* flag_openmp_simd */
37938 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37939 return false;
37941 if (strcmp (p, "target") == 0)
37943 cp_lexer_consume_token (parser->lexer);
37944 cp_parser_omp_declare_target (parser, pragma_tok);
37945 return false;
37948 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37949 "or %<target%>");
37950 cp_parser_require_pragma_eol (parser, pragma_tok);
37951 return false;
37954 /* OpenMP 4.5:
37955 #pragma omp taskloop taskloop-clause[optseq] new-line
37956 for-loop
37958 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37959 for-loop */
37961 #define OMP_TASKLOOP_CLAUSE_MASK \
37962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37977 static tree
37978 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37979 char *p_name, omp_clause_mask mask, tree *cclauses,
37980 bool *if_p)
37982 tree clauses, sb, ret;
37983 unsigned int save;
37984 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37986 strcat (p_name, " taskloop");
37987 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37989 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37991 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37992 const char *p = IDENTIFIER_POINTER (id);
37994 if (strcmp (p, "simd") == 0)
37996 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37997 if (cclauses == NULL)
37998 cclauses = cclauses_buf;
38000 cp_lexer_consume_token (parser->lexer);
38001 if (!flag_openmp) /* flag_openmp_simd */
38002 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38003 cclauses, if_p);
38004 sb = begin_omp_structured_block ();
38005 save = cp_parser_begin_omp_structured_block (parser);
38006 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38007 cclauses, if_p);
38008 cp_parser_end_omp_structured_block (parser, save);
38009 tree body = finish_omp_structured_block (sb);
38010 if (ret == NULL)
38011 return ret;
38012 ret = make_node (OMP_TASKLOOP);
38013 TREE_TYPE (ret) = void_type_node;
38014 OMP_FOR_BODY (ret) = body;
38015 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38016 SET_EXPR_LOCATION (ret, loc);
38017 add_stmt (ret);
38018 return ret;
38021 if (!flag_openmp) /* flag_openmp_simd */
38023 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38024 return NULL_TREE;
38027 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38028 cclauses == NULL);
38029 if (cclauses)
38031 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38032 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38035 sb = begin_omp_structured_block ();
38036 save = cp_parser_begin_omp_structured_block (parser);
38038 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38039 if_p);
38041 cp_parser_end_omp_structured_block (parser, save);
38042 add_stmt (finish_omp_structured_block (sb));
38044 return ret;
38048 /* OpenACC 2.0:
38049 # pragma acc routine oacc-routine-clause[optseq] new-line
38050 function-definition
38052 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38055 #define OACC_ROUTINE_CLAUSE_MASK \
38056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38062 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38063 component, which must resolve to a declared namespace-scope
38064 function. The clauses are either processed directly (for a named
38065 function), or defered until the immediatley following declaration
38066 is parsed. */
38068 static void
38069 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38070 enum pragma_context context)
38072 gcc_checking_assert (context == pragma_external);
38073 /* The checking for "another pragma following this one" in the "no optional
38074 '( name )'" case makes sure that we dont re-enter. */
38075 gcc_checking_assert (parser->oacc_routine == NULL);
38077 cp_oacc_routine_data data;
38078 data.error_seen = false;
38079 data.fndecl_seen = false;
38080 data.tokens = vNULL;
38081 data.clauses = NULL_TREE;
38082 data.loc = pragma_tok->location;
38083 /* It is safe to take the address of a local variable; it will only be
38084 used while this scope is live. */
38085 parser->oacc_routine = &data;
38087 /* Look for optional '( name )'. */
38088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38090 matching_parens parens;
38091 parens.consume_open (parser); /* '(' */
38093 /* We parse the name as an id-expression. If it resolves to
38094 anything other than a non-overloaded function at namespace
38095 scope, it's an error. */
38096 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38097 tree name = cp_parser_id_expression (parser,
38098 /*template_keyword_p=*/false,
38099 /*check_dependency_p=*/false,
38100 /*template_p=*/NULL,
38101 /*declarator_p=*/false,
38102 /*optional_p=*/false);
38103 tree decl = (identifier_p (name)
38104 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38105 : name);
38106 if (name != error_mark_node && decl == error_mark_node)
38107 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38109 if (decl == error_mark_node
38110 || !parens.require_close (parser))
38112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38113 parser->oacc_routine = NULL;
38114 return;
38117 data.clauses
38118 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38119 "#pragma acc routine",
38120 cp_lexer_peek_token (parser->lexer));
38122 if (decl && is_overloaded_fn (decl)
38123 && (TREE_CODE (decl) != FUNCTION_DECL
38124 || DECL_FUNCTION_TEMPLATE_P (decl)))
38126 error_at (name_loc,
38127 "%<#pragma acc routine%> names a set of overloads");
38128 parser->oacc_routine = NULL;
38129 return;
38132 /* Perhaps we should use the same rule as declarations in different
38133 namespaces? */
38134 if (!DECL_NAMESPACE_SCOPE_P (decl))
38136 error_at (name_loc,
38137 "%qD does not refer to a namespace scope function", decl);
38138 parser->oacc_routine = NULL;
38139 return;
38142 if (TREE_CODE (decl) != FUNCTION_DECL)
38144 error_at (name_loc, "%qD does not refer to a function", decl);
38145 parser->oacc_routine = NULL;
38146 return;
38149 cp_finalize_oacc_routine (parser, decl, false);
38150 parser->oacc_routine = NULL;
38152 else /* No optional '( name )'. */
38154 /* Store away all pragma tokens. */
38155 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38156 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38157 cp_lexer_consume_token (parser->lexer);
38158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38159 parser->oacc_routine->error_seen = true;
38160 cp_parser_require_pragma_eol (parser, pragma_tok);
38161 struct cp_token_cache *cp
38162 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38163 parser->oacc_routine->tokens.safe_push (cp);
38165 /* Emit a helpful diagnostic if there's another pragma following this
38166 one. */
38167 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38169 cp_ensure_no_oacc_routine (parser);
38170 data.tokens.release ();
38171 /* ..., and then just keep going. */
38172 return;
38175 /* We only have to consider the pragma_external case here. */
38176 cp_parser_declaration (parser);
38177 if (parser->oacc_routine
38178 && !parser->oacc_routine->fndecl_seen)
38179 cp_ensure_no_oacc_routine (parser);
38180 else
38181 parser->oacc_routine = NULL;
38182 data.tokens.release ();
38186 /* Finalize #pragma acc routine clauses after direct declarator has
38187 been parsed. */
38189 static tree
38190 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38192 struct cp_token_cache *ce;
38193 cp_oacc_routine_data *data = parser->oacc_routine;
38195 if (!data->error_seen && data->fndecl_seen)
38197 error_at (data->loc,
38198 "%<#pragma acc routine%> not immediately followed by "
38199 "a single function declaration or definition");
38200 data->error_seen = true;
38202 if (data->error_seen)
38203 return attrs;
38205 gcc_checking_assert (data->tokens.length () == 1);
38206 ce = data->tokens[0];
38208 cp_parser_push_lexer_for_tokens (parser, ce);
38209 parser->lexer->in_pragma = true;
38210 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38212 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38213 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38214 parser->oacc_routine->clauses
38215 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38216 "#pragma acc routine", pragma_tok);
38217 cp_parser_pop_lexer (parser);
38218 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38219 fndecl_seen. */
38221 return attrs;
38224 /* Apply any saved OpenACC routine clauses to a just-parsed
38225 declaration. */
38227 static void
38228 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38230 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38232 /* Keep going if we're in error reporting mode. */
38233 if (parser->oacc_routine->error_seen
38234 || fndecl == error_mark_node)
38235 return;
38237 if (parser->oacc_routine->fndecl_seen)
38239 error_at (parser->oacc_routine->loc,
38240 "%<#pragma acc routine%> not immediately followed by"
38241 " a single function declaration or definition");
38242 parser->oacc_routine = NULL;
38243 return;
38245 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38247 cp_ensure_no_oacc_routine (parser);
38248 return;
38251 if (oacc_get_fn_attrib (fndecl))
38253 error_at (parser->oacc_routine->loc,
38254 "%<#pragma acc routine%> already applied to %qD", fndecl);
38255 parser->oacc_routine = NULL;
38256 return;
38259 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38261 error_at (parser->oacc_routine->loc,
38262 TREE_USED (fndecl)
38263 ? G_("%<#pragma acc routine%> must be applied before use")
38264 : G_("%<#pragma acc routine%> must be applied before "
38265 "definition"));
38266 parser->oacc_routine = NULL;
38267 return;
38270 /* Process the routine's dimension clauses. */
38271 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38272 oacc_replace_fn_attrib (fndecl, dims);
38274 /* Add an "omp declare target" attribute. */
38275 DECL_ATTRIBUTES (fndecl)
38276 = tree_cons (get_identifier ("omp declare target"),
38277 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38279 /* Don't unset parser->oacc_routine here: we may still need it to
38280 diagnose wrong usage. But, remember that we've used this "#pragma acc
38281 routine". */
38282 parser->oacc_routine->fndecl_seen = true;
38286 /* Main entry point to OpenMP statement pragmas. */
38288 static void
38289 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38291 tree stmt;
38292 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38293 omp_clause_mask mask (0);
38295 switch (cp_parser_pragma_kind (pragma_tok))
38297 case PRAGMA_OACC_ATOMIC:
38298 cp_parser_omp_atomic (parser, pragma_tok);
38299 return;
38300 case PRAGMA_OACC_CACHE:
38301 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38302 break;
38303 case PRAGMA_OACC_DATA:
38304 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38305 break;
38306 case PRAGMA_OACC_ENTER_DATA:
38307 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38308 break;
38309 case PRAGMA_OACC_EXIT_DATA:
38310 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38311 break;
38312 case PRAGMA_OACC_HOST_DATA:
38313 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38314 break;
38315 case PRAGMA_OACC_KERNELS:
38316 case PRAGMA_OACC_PARALLEL:
38317 strcpy (p_name, "#pragma acc");
38318 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38319 if_p);
38320 break;
38321 case PRAGMA_OACC_LOOP:
38322 strcpy (p_name, "#pragma acc");
38323 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38324 if_p);
38325 break;
38326 case PRAGMA_OACC_UPDATE:
38327 stmt = cp_parser_oacc_update (parser, pragma_tok);
38328 break;
38329 case PRAGMA_OACC_WAIT:
38330 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38331 break;
38332 case PRAGMA_OMP_ATOMIC:
38333 cp_parser_omp_atomic (parser, pragma_tok);
38334 return;
38335 case PRAGMA_OMP_CRITICAL:
38336 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38337 break;
38338 case PRAGMA_OMP_DISTRIBUTE:
38339 strcpy (p_name, "#pragma omp");
38340 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38341 if_p);
38342 break;
38343 case PRAGMA_OMP_FOR:
38344 strcpy (p_name, "#pragma omp");
38345 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38346 if_p);
38347 break;
38348 case PRAGMA_OMP_MASTER:
38349 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38350 break;
38351 case PRAGMA_OMP_PARALLEL:
38352 strcpy (p_name, "#pragma omp");
38353 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38354 if_p);
38355 break;
38356 case PRAGMA_OMP_SECTIONS:
38357 strcpy (p_name, "#pragma omp");
38358 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38359 break;
38360 case PRAGMA_OMP_SIMD:
38361 strcpy (p_name, "#pragma omp");
38362 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38363 if_p);
38364 break;
38365 case PRAGMA_OMP_SINGLE:
38366 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38367 break;
38368 case PRAGMA_OMP_TASK:
38369 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38370 break;
38371 case PRAGMA_OMP_TASKGROUP:
38372 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38373 break;
38374 case PRAGMA_OMP_TASKLOOP:
38375 strcpy (p_name, "#pragma omp");
38376 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38377 if_p);
38378 break;
38379 case PRAGMA_OMP_TEAMS:
38380 strcpy (p_name, "#pragma omp");
38381 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38382 if_p);
38383 break;
38384 default:
38385 gcc_unreachable ();
38388 protected_set_expr_location (stmt, pragma_tok->location);
38391 /* Transactional Memory parsing routines. */
38393 /* Parse a transaction attribute.
38395 txn-attribute:
38396 attribute
38397 [ [ identifier ] ]
38399 We use this instead of cp_parser_attributes_opt for transactions to avoid
38400 the pedwarn in C++98 mode. */
38402 static tree
38403 cp_parser_txn_attribute_opt (cp_parser *parser)
38405 cp_token *token;
38406 tree attr_name, attr = NULL;
38408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38409 return cp_parser_attributes_opt (parser);
38411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38412 return NULL_TREE;
38413 cp_lexer_consume_token (parser->lexer);
38414 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38415 goto error1;
38417 token = cp_lexer_peek_token (parser->lexer);
38418 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38420 token = cp_lexer_consume_token (parser->lexer);
38422 attr_name = (token->type == CPP_KEYWORD
38423 /* For keywords, use the canonical spelling,
38424 not the parsed identifier. */
38425 ? ridpointers[(int) token->keyword]
38426 : token->u.value);
38427 attr = build_tree_list (attr_name, NULL_TREE);
38429 else
38430 cp_parser_error (parser, "expected identifier");
38432 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38433 error1:
38434 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38435 return attr;
38438 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38440 transaction-statement:
38441 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38442 compound-statement
38443 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38446 static tree
38447 cp_parser_transaction (cp_parser *parser, cp_token *token)
38449 unsigned char old_in = parser->in_transaction;
38450 unsigned char this_in = 1, new_in;
38451 enum rid keyword = token->keyword;
38452 tree stmt, attrs, noex;
38454 cp_lexer_consume_token (parser->lexer);
38456 if (keyword == RID_TRANSACTION_RELAXED
38457 || keyword == RID_SYNCHRONIZED)
38458 this_in |= TM_STMT_ATTR_RELAXED;
38459 else
38461 attrs = cp_parser_txn_attribute_opt (parser);
38462 if (attrs)
38463 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38466 /* Parse a noexcept specification. */
38467 if (keyword == RID_ATOMIC_NOEXCEPT)
38468 noex = boolean_true_node;
38469 else if (keyword == RID_ATOMIC_CANCEL)
38471 /* cancel-and-throw is unimplemented. */
38472 sorry ("atomic_cancel");
38473 noex = NULL_TREE;
38475 else
38476 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38478 /* Keep track if we're in the lexical scope of an outer transaction. */
38479 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38481 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38483 parser->in_transaction = new_in;
38484 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38485 parser->in_transaction = old_in;
38487 finish_transaction_stmt (stmt, NULL, this_in, noex);
38489 return stmt;
38492 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38494 transaction-expression:
38495 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38496 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38499 static tree
38500 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38502 unsigned char old_in = parser->in_transaction;
38503 unsigned char this_in = 1;
38504 cp_token *token;
38505 tree expr, noex;
38506 bool noex_expr;
38507 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38509 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38510 || keyword == RID_TRANSACTION_RELAXED);
38512 if (!flag_tm)
38513 error_at (loc,
38514 keyword == RID_TRANSACTION_RELAXED
38515 ? G_("%<__transaction_relaxed%> without transactional memory "
38516 "support enabled")
38517 : G_("%<__transaction_atomic%> without transactional memory "
38518 "support enabled"));
38520 token = cp_parser_require_keyword (parser, keyword,
38521 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38522 : RT_TRANSACTION_RELAXED));
38523 gcc_assert (token != NULL);
38525 if (keyword == RID_TRANSACTION_RELAXED)
38526 this_in |= TM_STMT_ATTR_RELAXED;
38528 /* Set this early. This might mean that we allow transaction_cancel in
38529 an expression that we find out later actually has to be a constexpr.
38530 However, we expect that cxx_constant_value will be able to deal with
38531 this; also, if the noexcept has no constexpr, then what we parse next
38532 really is a transaction's body. */
38533 parser->in_transaction = this_in;
38535 /* Parse a noexcept specification. */
38536 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38537 true);
38539 if (!noex || !noex_expr
38540 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38542 matching_parens parens;
38543 parens.require_open (parser);
38545 expr = cp_parser_expression (parser);
38546 expr = finish_parenthesized_expr (expr);
38548 parens.require_close (parser);
38550 else
38552 /* The only expression that is available got parsed for the noexcept
38553 already. noexcept is true then. */
38554 expr = noex;
38555 noex = boolean_true_node;
38558 expr = build_transaction_expr (token->location, expr, this_in, noex);
38559 parser->in_transaction = old_in;
38561 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38562 return error_mark_node;
38564 return (flag_tm ? expr : error_mark_node);
38567 /* Parse a function-transaction-block.
38569 function-transaction-block:
38570 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38571 function-body
38572 __transaction_atomic txn-attribute[opt] function-try-block
38573 __transaction_relaxed ctor-initializer[opt] function-body
38574 __transaction_relaxed function-try-block
38577 static void
38578 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38580 unsigned char old_in = parser->in_transaction;
38581 unsigned char new_in = 1;
38582 tree compound_stmt, stmt, attrs;
38583 cp_token *token;
38585 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38586 || keyword == RID_TRANSACTION_RELAXED);
38587 token = cp_parser_require_keyword (parser, keyword,
38588 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38589 : RT_TRANSACTION_RELAXED));
38590 gcc_assert (token != NULL);
38592 if (keyword == RID_TRANSACTION_RELAXED)
38593 new_in |= TM_STMT_ATTR_RELAXED;
38594 else
38596 attrs = cp_parser_txn_attribute_opt (parser);
38597 if (attrs)
38598 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38601 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38603 parser->in_transaction = new_in;
38605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38606 cp_parser_function_try_block (parser);
38607 else
38608 cp_parser_ctor_initializer_opt_and_function_body
38609 (parser, /*in_function_try_block=*/false);
38611 parser->in_transaction = old_in;
38613 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38616 /* Parse a __transaction_cancel statement.
38618 cancel-statement:
38619 __transaction_cancel txn-attribute[opt] ;
38620 __transaction_cancel txn-attribute[opt] throw-expression ;
38622 ??? Cancel and throw is not yet implemented. */
38624 static tree
38625 cp_parser_transaction_cancel (cp_parser *parser)
38627 cp_token *token;
38628 bool is_outer = false;
38629 tree stmt, attrs;
38631 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38632 RT_TRANSACTION_CANCEL);
38633 gcc_assert (token != NULL);
38635 attrs = cp_parser_txn_attribute_opt (parser);
38636 if (attrs)
38637 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38639 /* ??? Parse cancel-and-throw here. */
38641 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38643 if (!flag_tm)
38645 error_at (token->location, "%<__transaction_cancel%> without "
38646 "transactional memory support enabled");
38647 return error_mark_node;
38649 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38651 error_at (token->location, "%<__transaction_cancel%> within a "
38652 "%<__transaction_relaxed%>");
38653 return error_mark_node;
38655 else if (is_outer)
38657 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38658 && !is_tm_may_cancel_outer (current_function_decl))
38660 error_at (token->location, "outer %<__transaction_cancel%> not "
38661 "within outer %<__transaction_atomic%>");
38662 error_at (token->location,
38663 " or a %<transaction_may_cancel_outer%> function");
38664 return error_mark_node;
38667 else if (parser->in_transaction == 0)
38669 error_at (token->location, "%<__transaction_cancel%> not within "
38670 "%<__transaction_atomic%>");
38671 return error_mark_node;
38674 stmt = build_tm_abort_call (token->location, is_outer);
38675 add_stmt (stmt);
38677 return stmt;
38680 /* The parser. */
38682 static GTY (()) cp_parser *the_parser;
38685 /* Special handling for the first token or line in the file. The first
38686 thing in the file might be #pragma GCC pch_preprocess, which loads a
38687 PCH file, which is a GC collection point. So we need to handle this
38688 first pragma without benefit of an existing lexer structure.
38690 Always returns one token to the caller in *FIRST_TOKEN. This is
38691 either the true first token of the file, or the first token after
38692 the initial pragma. */
38694 static void
38695 cp_parser_initial_pragma (cp_token *first_token)
38697 tree name = NULL;
38699 cp_lexer_get_preprocessor_token (NULL, first_token);
38700 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38701 return;
38703 cp_lexer_get_preprocessor_token (NULL, first_token);
38704 if (first_token->type == CPP_STRING)
38706 name = first_token->u.value;
38708 cp_lexer_get_preprocessor_token (NULL, first_token);
38709 if (first_token->type != CPP_PRAGMA_EOL)
38710 error_at (first_token->location,
38711 "junk at end of %<#pragma GCC pch_preprocess%>");
38713 else
38714 error_at (first_token->location, "expected string literal");
38716 /* Skip to the end of the pragma. */
38717 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38718 cp_lexer_get_preprocessor_token (NULL, first_token);
38720 /* Now actually load the PCH file. */
38721 if (name)
38722 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38724 /* Read one more token to return to our caller. We have to do this
38725 after reading the PCH file in, since its pointers have to be
38726 live. */
38727 cp_lexer_get_preprocessor_token (NULL, first_token);
38730 /* Parse a pragma GCC ivdep. */
38732 static bool
38733 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38735 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38736 return true;
38739 /* Parse a pragma GCC unroll. */
38741 static unsigned short
38742 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38744 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38745 tree expr = cp_parser_constant_expression (parser);
38746 unsigned short unroll;
38747 expr = maybe_constant_value (expr);
38748 HOST_WIDE_INT lunroll = 0;
38749 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38750 || TREE_CODE (expr) != INTEGER_CST
38751 || (lunroll = tree_to_shwi (expr)) < 0
38752 || lunroll >= USHRT_MAX)
38754 error_at (location, "%<#pragma GCC unroll%> requires an"
38755 " assignment-expression that evaluates to a non-negative"
38756 " integral constant less than %u", USHRT_MAX);
38757 unroll = 0;
38759 else
38761 unroll = (unsigned short)lunroll;
38762 if (unroll == 0)
38763 unroll = 1;
38765 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38766 return unroll;
38769 /* Normal parsing of a pragma token. Here we can (and must) use the
38770 regular lexer. */
38772 static bool
38773 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38775 cp_token *pragma_tok;
38776 unsigned int id;
38777 tree stmt;
38778 bool ret;
38780 pragma_tok = cp_lexer_consume_token (parser->lexer);
38781 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38782 parser->lexer->in_pragma = true;
38784 id = cp_parser_pragma_kind (pragma_tok);
38785 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38786 cp_ensure_no_omp_declare_simd (parser);
38787 switch (id)
38789 case PRAGMA_GCC_PCH_PREPROCESS:
38790 error_at (pragma_tok->location,
38791 "%<#pragma GCC pch_preprocess%> must be first");
38792 break;
38794 case PRAGMA_OMP_BARRIER:
38795 switch (context)
38797 case pragma_compound:
38798 cp_parser_omp_barrier (parser, pragma_tok);
38799 return false;
38800 case pragma_stmt:
38801 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38802 "used in compound statements", "omp barrier");
38803 break;
38804 default:
38805 goto bad_stmt;
38807 break;
38809 case PRAGMA_OMP_FLUSH:
38810 switch (context)
38812 case pragma_compound:
38813 cp_parser_omp_flush (parser, pragma_tok);
38814 return false;
38815 case pragma_stmt:
38816 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38817 "used in compound statements", "omp flush");
38818 break;
38819 default:
38820 goto bad_stmt;
38822 break;
38824 case PRAGMA_OMP_TASKWAIT:
38825 switch (context)
38827 case pragma_compound:
38828 cp_parser_omp_taskwait (parser, pragma_tok);
38829 return false;
38830 case pragma_stmt:
38831 error_at (pragma_tok->location,
38832 "%<#pragma %s%> may only be used in compound statements",
38833 "omp taskwait");
38834 break;
38835 default:
38836 goto bad_stmt;
38838 break;
38840 case PRAGMA_OMP_TASKYIELD:
38841 switch (context)
38843 case pragma_compound:
38844 cp_parser_omp_taskyield (parser, pragma_tok);
38845 return false;
38846 case pragma_stmt:
38847 error_at (pragma_tok->location,
38848 "%<#pragma %s%> may only be used in compound statements",
38849 "omp taskyield");
38850 break;
38851 default:
38852 goto bad_stmt;
38854 break;
38856 case PRAGMA_OMP_CANCEL:
38857 switch (context)
38859 case pragma_compound:
38860 cp_parser_omp_cancel (parser, pragma_tok);
38861 return false;
38862 case pragma_stmt:
38863 error_at (pragma_tok->location,
38864 "%<#pragma %s%> may only be used in compound statements",
38865 "omp cancel");
38866 break;
38867 default:
38868 goto bad_stmt;
38870 break;
38872 case PRAGMA_OMP_CANCELLATION_POINT:
38873 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38874 return false;
38876 case PRAGMA_OMP_THREADPRIVATE:
38877 cp_parser_omp_threadprivate (parser, pragma_tok);
38878 return false;
38880 case PRAGMA_OMP_DECLARE:
38881 return cp_parser_omp_declare (parser, pragma_tok, context);
38883 case PRAGMA_OACC_DECLARE:
38884 cp_parser_oacc_declare (parser, pragma_tok);
38885 return false;
38887 case PRAGMA_OACC_ENTER_DATA:
38888 if (context == pragma_stmt)
38890 error_at (pragma_tok->location,
38891 "%<#pragma %s%> may only be used in compound statements",
38892 "acc enter data");
38893 break;
38895 else if (context != pragma_compound)
38896 goto bad_stmt;
38897 cp_parser_omp_construct (parser, pragma_tok, if_p);
38898 return true;
38900 case PRAGMA_OACC_EXIT_DATA:
38901 if (context == pragma_stmt)
38903 error_at (pragma_tok->location,
38904 "%<#pragma %s%> may only be used in compound statements",
38905 "acc exit data");
38906 break;
38908 else if (context != pragma_compound)
38909 goto bad_stmt;
38910 cp_parser_omp_construct (parser, pragma_tok, if_p);
38911 return true;
38913 case PRAGMA_OACC_ROUTINE:
38914 if (context != pragma_external)
38916 error_at (pragma_tok->location,
38917 "%<#pragma acc routine%> must be at file scope");
38918 break;
38920 cp_parser_oacc_routine (parser, pragma_tok, context);
38921 return false;
38923 case PRAGMA_OACC_UPDATE:
38924 if (context == pragma_stmt)
38926 error_at (pragma_tok->location,
38927 "%<#pragma %s%> may only be used in compound statements",
38928 "acc update");
38929 break;
38931 else if (context != pragma_compound)
38932 goto bad_stmt;
38933 cp_parser_omp_construct (parser, pragma_tok, if_p);
38934 return true;
38936 case PRAGMA_OACC_WAIT:
38937 if (context == pragma_stmt)
38939 error_at (pragma_tok->location,
38940 "%<#pragma %s%> may only be used in compound statements",
38941 "acc wait");
38942 break;
38944 else if (context != pragma_compound)
38945 goto bad_stmt;
38946 cp_parser_omp_construct (parser, pragma_tok, if_p);
38947 return true;
38949 case PRAGMA_OACC_ATOMIC:
38950 case PRAGMA_OACC_CACHE:
38951 case PRAGMA_OACC_DATA:
38952 case PRAGMA_OACC_HOST_DATA:
38953 case PRAGMA_OACC_KERNELS:
38954 case PRAGMA_OACC_PARALLEL:
38955 case PRAGMA_OACC_LOOP:
38956 case PRAGMA_OMP_ATOMIC:
38957 case PRAGMA_OMP_CRITICAL:
38958 case PRAGMA_OMP_DISTRIBUTE:
38959 case PRAGMA_OMP_FOR:
38960 case PRAGMA_OMP_MASTER:
38961 case PRAGMA_OMP_PARALLEL:
38962 case PRAGMA_OMP_SECTIONS:
38963 case PRAGMA_OMP_SIMD:
38964 case PRAGMA_OMP_SINGLE:
38965 case PRAGMA_OMP_TASK:
38966 case PRAGMA_OMP_TASKGROUP:
38967 case PRAGMA_OMP_TASKLOOP:
38968 case PRAGMA_OMP_TEAMS:
38969 if (context != pragma_stmt && context != pragma_compound)
38970 goto bad_stmt;
38971 stmt = push_omp_privatization_clauses (false);
38972 cp_parser_omp_construct (parser, pragma_tok, if_p);
38973 pop_omp_privatization_clauses (stmt);
38974 return true;
38976 case PRAGMA_OMP_ORDERED:
38977 if (context != pragma_stmt && context != pragma_compound)
38978 goto bad_stmt;
38979 stmt = push_omp_privatization_clauses (false);
38980 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38981 pop_omp_privatization_clauses (stmt);
38982 return ret;
38984 case PRAGMA_OMP_TARGET:
38985 if (context != pragma_stmt && context != pragma_compound)
38986 goto bad_stmt;
38987 stmt = push_omp_privatization_clauses (false);
38988 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38989 pop_omp_privatization_clauses (stmt);
38990 return ret;
38992 case PRAGMA_OMP_END_DECLARE_TARGET:
38993 cp_parser_omp_end_declare_target (parser, pragma_tok);
38994 return false;
38996 case PRAGMA_OMP_SECTION:
38997 error_at (pragma_tok->location,
38998 "%<#pragma omp section%> may only be used in "
38999 "%<#pragma omp sections%> construct");
39000 break;
39002 case PRAGMA_IVDEP:
39004 if (context == pragma_external)
39006 error_at (pragma_tok->location,
39007 "%<#pragma GCC ivdep%> must be inside a function");
39008 break;
39010 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39011 unsigned short unroll;
39012 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39013 if (tok->type == CPP_PRAGMA
39014 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39016 tok = cp_lexer_consume_token (parser->lexer);
39017 unroll = cp_parser_pragma_unroll (parser, tok);
39018 tok = cp_lexer_peek_token (the_parser->lexer);
39020 else
39021 unroll = 0;
39022 if (tok->type != CPP_KEYWORD
39023 || (tok->keyword != RID_FOR
39024 && tok->keyword != RID_WHILE
39025 && tok->keyword != RID_DO))
39027 cp_parser_error (parser, "for, while or do statement expected");
39028 return false;
39030 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39031 return true;
39034 case PRAGMA_UNROLL:
39036 if (context == pragma_external)
39038 error_at (pragma_tok->location,
39039 "%<#pragma GCC unroll%> must be inside a function");
39040 break;
39042 const unsigned short unroll
39043 = cp_parser_pragma_unroll (parser, pragma_tok);
39044 bool ivdep;
39045 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39046 if (tok->type == CPP_PRAGMA
39047 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39049 tok = cp_lexer_consume_token (parser->lexer);
39050 ivdep = cp_parser_pragma_ivdep (parser, tok);
39051 tok = cp_lexer_peek_token (the_parser->lexer);
39053 else
39054 ivdep = false;
39055 if (tok->type != CPP_KEYWORD
39056 || (tok->keyword != RID_FOR
39057 && tok->keyword != RID_WHILE
39058 && tok->keyword != RID_DO))
39060 cp_parser_error (parser, "for, while or do statement expected");
39061 return false;
39063 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39064 return true;
39067 default:
39068 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39069 c_invoke_pragma_handler (id);
39070 break;
39072 bad_stmt:
39073 cp_parser_error (parser, "expected declaration specifiers");
39074 break;
39077 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39078 return false;
39081 /* The interface the pragma parsers have to the lexer. */
39083 enum cpp_ttype
39084 pragma_lex (tree *value, location_t *loc)
39086 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39087 enum cpp_ttype ret = tok->type;
39089 *value = tok->u.value;
39090 if (loc)
39091 *loc = tok->location;
39093 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39094 ret = CPP_EOF;
39095 else if (ret == CPP_STRING)
39096 *value = cp_parser_string_literal (the_parser, false, false);
39097 else
39099 if (ret == CPP_KEYWORD)
39100 ret = CPP_NAME;
39101 cp_lexer_consume_token (the_parser->lexer);
39104 return ret;
39108 /* External interface. */
39110 /* Parse one entire translation unit. */
39112 void
39113 c_parse_file (void)
39115 static bool already_called = false;
39117 if (already_called)
39118 fatal_error (input_location,
39119 "inter-module optimizations not implemented for C++");
39120 already_called = true;
39122 the_parser = cp_parser_new ();
39123 push_deferring_access_checks (flag_access_control
39124 ? dk_no_deferred : dk_no_check);
39125 cp_parser_translation_unit (the_parser);
39126 the_parser = NULL;
39129 /* Create an identifier for a generic parameter type (a synthesized
39130 template parameter implied by `auto' or a concept identifier). */
39132 static GTY(()) int generic_parm_count;
39133 static tree
39134 make_generic_type_name ()
39136 char buf[32];
39137 sprintf (buf, "auto:%d", ++generic_parm_count);
39138 return get_identifier (buf);
39141 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39142 (creating a new template parameter list if necessary). Returns the newly
39143 created template type parm. */
39145 static tree
39146 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39148 gcc_assert (current_binding_level->kind == sk_function_parms);
39150 /* Before committing to modifying any scope, if we're in an
39151 implicit template scope, and we're trying to synthesize a
39152 constrained parameter, try to find a previous parameter with
39153 the same name. This is the same-type rule for abbreviated
39154 function templates.
39156 NOTE: We can generate implicit parameters when tentatively
39157 parsing a nested name specifier, only to reject that parse
39158 later. However, matching the same template-id as part of a
39159 direct-declarator should generate an identical template
39160 parameter, so this rule will merge them. */
39161 if (parser->implicit_template_scope && constr)
39163 tree t = parser->implicit_template_parms;
39164 while (t)
39166 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39168 tree d = TREE_VALUE (t);
39169 if (TREE_CODE (d) == PARM_DECL)
39170 /* Return the TEMPLATE_PARM_INDEX. */
39171 d = DECL_INITIAL (d);
39172 return d;
39174 t = TREE_CHAIN (t);
39178 /* We are either continuing a function template that already contains implicit
39179 template parameters, creating a new fully-implicit function template, or
39180 extending an existing explicit function template with implicit template
39181 parameters. */
39183 cp_binding_level *const entry_scope = current_binding_level;
39185 bool become_template = false;
39186 cp_binding_level *parent_scope = 0;
39188 if (parser->implicit_template_scope)
39190 gcc_assert (parser->implicit_template_parms);
39192 current_binding_level = parser->implicit_template_scope;
39194 else
39196 /* Roll back to the existing template parameter scope (in the case of
39197 extending an explicit function template) or introduce a new template
39198 parameter scope ahead of the function parameter scope (or class scope
39199 in the case of out-of-line member definitions). The function scope is
39200 added back after template parameter synthesis below. */
39202 cp_binding_level *scope = entry_scope;
39204 while (scope->kind == sk_function_parms)
39206 parent_scope = scope;
39207 scope = scope->level_chain;
39209 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39211 /* If not defining a class, then any class scope is a scope level in
39212 an out-of-line member definition. In this case simply wind back
39213 beyond the first such scope to inject the template parameter list.
39214 Otherwise wind back to the class being defined. The latter can
39215 occur in class member friend declarations such as:
39217 class A {
39218 void foo (auto);
39220 class B {
39221 friend void A::foo (auto);
39224 The template parameter list synthesized for the friend declaration
39225 must be injected in the scope of 'B'. This can also occur in
39226 erroneous cases such as:
39228 struct A {
39229 struct B {
39230 void foo (auto);
39232 void B::foo (auto) {}
39235 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39236 but, nevertheless, the template parameter list synthesized for the
39237 declarator should be injected into the scope of 'A' as if the
39238 ill-formed template was specified explicitly. */
39240 while (scope->kind == sk_class && !scope->defining_class_p)
39242 parent_scope = scope;
39243 scope = scope->level_chain;
39247 current_binding_level = scope;
39249 if (scope->kind != sk_template_parms
39250 || !function_being_declared_is_template_p (parser))
39252 /* Introduce a new template parameter list for implicit template
39253 parameters. */
39255 become_template = true;
39257 parser->implicit_template_scope
39258 = begin_scope (sk_template_parms, NULL);
39260 ++processing_template_decl;
39262 parser->fully_implicit_function_template_p = true;
39263 ++parser->num_template_parameter_lists;
39265 else
39267 /* Synthesize implicit template parameters at the end of the explicit
39268 template parameter list. */
39270 gcc_assert (current_template_parms);
39272 parser->implicit_template_scope = scope;
39274 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39275 parser->implicit_template_parms
39276 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39280 /* Synthesize a new template parameter and track the current template
39281 parameter chain with implicit_template_parms. */
39283 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39284 tree synth_id = make_generic_type_name ();
39285 tree synth_tmpl_parm;
39286 bool non_type = false;
39288 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39289 synth_tmpl_parm
39290 = finish_template_type_parm (class_type_node, synth_id);
39291 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39292 synth_tmpl_parm
39293 = finish_constrained_template_template_parm (proto, synth_id);
39294 else
39296 synth_tmpl_parm = copy_decl (proto);
39297 DECL_NAME (synth_tmpl_parm) = synth_id;
39298 non_type = true;
39301 // Attach the constraint to the parm before processing.
39302 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39303 TREE_TYPE (node) = constr;
39304 tree new_parm
39305 = process_template_parm (parser->implicit_template_parms,
39306 input_location,
39307 node,
39308 /*non_type=*/non_type,
39309 /*param_pack=*/false);
39311 // Chain the new parameter to the list of implicit parameters.
39312 if (parser->implicit_template_parms)
39313 parser->implicit_template_parms
39314 = TREE_CHAIN (parser->implicit_template_parms);
39315 else
39316 parser->implicit_template_parms = new_parm;
39318 tree new_decl = get_local_decls ();
39319 if (non_type)
39320 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39321 new_decl = DECL_INITIAL (new_decl);
39323 /* If creating a fully implicit function template, start the new implicit
39324 template parameter list with this synthesized type, otherwise grow the
39325 current template parameter list. */
39327 if (become_template)
39329 parent_scope->level_chain = current_binding_level;
39331 tree new_parms = make_tree_vec (1);
39332 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39333 current_template_parms = tree_cons (size_int (processing_template_decl),
39334 new_parms, current_template_parms);
39336 else
39338 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39339 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39340 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39341 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39344 // If the new parameter was constrained, we need to add that to the
39345 // constraints in the template parameter list.
39346 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39348 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39349 reqs = conjoin_constraints (reqs, req);
39350 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39353 current_binding_level = entry_scope;
39355 return new_decl;
39358 /* Finish the declaration of a fully implicit function template. Such a
39359 template has no explicit template parameter list so has not been through the
39360 normal template head and tail processing. synthesize_implicit_template_parm
39361 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39362 provided if the declaration is a class member such that its template
39363 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39364 form is returned. Otherwise NULL_TREE is returned. */
39366 static tree
39367 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39369 gcc_assert (parser->fully_implicit_function_template_p);
39371 if (member_decl_opt && member_decl_opt != error_mark_node
39372 && DECL_VIRTUAL_P (member_decl_opt))
39374 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39375 "implicit templates may not be %<virtual%>");
39376 DECL_VIRTUAL_P (member_decl_opt) = false;
39379 if (member_decl_opt)
39380 member_decl_opt = finish_member_template_decl (member_decl_opt);
39381 end_template_decl ();
39383 parser->fully_implicit_function_template_p = false;
39384 parser->implicit_template_parms = 0;
39385 parser->implicit_template_scope = 0;
39386 --parser->num_template_parameter_lists;
39388 return member_decl_opt;
39391 /* Like finish_fully_implicit_template, but to be used in error
39392 recovery, rearranging scopes so that we restore the state we had
39393 before synthesize_implicit_template_parm inserted the implement
39394 template parms scope. */
39396 static void
39397 abort_fully_implicit_template (cp_parser *parser)
39399 cp_binding_level *return_to_scope = current_binding_level;
39401 if (parser->implicit_template_scope
39402 && return_to_scope != parser->implicit_template_scope)
39404 cp_binding_level *child = return_to_scope;
39405 for (cp_binding_level *scope = child->level_chain;
39406 scope != parser->implicit_template_scope;
39407 scope = child->level_chain)
39408 child = scope;
39409 child->level_chain = parser->implicit_template_scope->level_chain;
39410 parser->implicit_template_scope->level_chain = return_to_scope;
39411 current_binding_level = parser->implicit_template_scope;
39413 else
39414 return_to_scope = return_to_scope->level_chain;
39416 finish_fully_implicit_template (parser, NULL);
39418 gcc_assert (current_binding_level == return_to_scope);
39421 /* Helper function for diagnostics that have complained about things
39422 being used with 'extern "C"' linkage.
39424 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39426 void
39427 maybe_show_extern_c_location (void)
39429 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39430 inform (the_parser->innermost_linkage_specification_location,
39431 "%<extern \"C\"%> linkage started here");
39434 #include "gt-cp-parser.h"