builtins.def: (_Float<N> and _Float<N>X BUILT_IN_CEIL): Add _Float<N> and _Float...
[official-gcc.git] / gcc / cp / parser.c
blobb04ed9adce0c040c3c86c619cfc683496cf82da6
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "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 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser *, tree, tree *, tree *);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2058 (cp_token *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2098 /* Statements [gram.stmt.stmt] */
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool, unsigned short);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool, unsigned short);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool, unsigned short);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool, unsigned short);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2140 /* Declarations [gram.dcl.dcl] */
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_block_declaration
2147 (cp_parser *, bool);
2148 static void cp_parser_simple_declaration
2149 (cp_parser *, bool, tree *);
2150 static void cp_parser_decl_specifier_seq
2151 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2152 static tree cp_parser_storage_class_specifier_opt
2153 (cp_parser *);
2154 static tree cp_parser_function_specifier_opt
2155 (cp_parser *, cp_decl_specifier_seq *);
2156 static tree cp_parser_type_specifier
2157 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2158 int *, bool *);
2159 static tree cp_parser_simple_type_specifier
2160 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2161 static tree cp_parser_type_name
2162 (cp_parser *, bool);
2163 static tree cp_parser_type_name
2164 (cp_parser *);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202 /* Declarators [gram.dcl.decl] */
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2206 bool, bool, int, bool *, tree *, location_t *, tree *);
2207 static cp_declarator *cp_parser_declarator
2208 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2209 static cp_declarator *cp_parser_direct_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2211 static enum tree_code cp_parser_ptr_operator
2212 (cp_parser *, tree *, cp_cv_quals *, tree *);
2213 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2214 (cp_parser *);
2215 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2216 (cp_parser *);
2217 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2218 (cp_parser *);
2219 static tree cp_parser_tx_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_late_return_type_opt
2222 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2223 static tree cp_parser_declarator_id
2224 (cp_parser *, bool);
2225 static tree cp_parser_type_id
2226 (cp_parser *);
2227 static tree cp_parser_template_type_arg
2228 (cp_parser *);
2229 static tree cp_parser_trailing_type_id (cp_parser *);
2230 static tree cp_parser_type_id_1
2231 (cp_parser *, bool, bool);
2232 static void cp_parser_type_specifier_seq
2233 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2234 static tree cp_parser_parameter_declaration_clause
2235 (cp_parser *);
2236 static tree cp_parser_parameter_declaration_list
2237 (cp_parser *, bool *);
2238 static cp_parameter_declarator *cp_parser_parameter_declaration
2239 (cp_parser *, bool, bool *);
2240 static tree cp_parser_default_argument
2241 (cp_parser *, bool);
2242 static void cp_parser_function_body
2243 (cp_parser *, bool);
2244 static tree cp_parser_initializer
2245 (cp_parser *, bool *, bool *);
2246 static cp_expr cp_parser_initializer_clause
2247 (cp_parser *, bool *);
2248 static cp_expr cp_parser_braced_list
2249 (cp_parser*, bool*);
2250 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2251 (cp_parser *, bool *);
2253 static void cp_parser_ctor_initializer_opt_and_function_body
2254 (cp_parser *, bool);
2256 static tree cp_parser_late_parsing_omp_declare_simd
2257 (cp_parser *, tree);
2259 static tree cp_parser_late_parsing_oacc_routine
2260 (cp_parser *, tree);
2262 static tree synthesize_implicit_template_parm
2263 (cp_parser *, tree);
2264 static tree finish_fully_implicit_template
2265 (cp_parser *, tree);
2267 /* Classes [gram.class] */
2269 static tree cp_parser_class_name
2270 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2271 static tree cp_parser_class_specifier
2272 (cp_parser *);
2273 static tree cp_parser_class_head
2274 (cp_parser *, bool *);
2275 static enum tag_types cp_parser_class_key
2276 (cp_parser *);
2277 static void cp_parser_type_parameter_key
2278 (cp_parser* parser);
2279 static void cp_parser_member_specification_opt
2280 (cp_parser *);
2281 static void cp_parser_member_declaration
2282 (cp_parser *);
2283 static tree cp_parser_pure_specifier
2284 (cp_parser *);
2285 static tree cp_parser_constant_initializer
2286 (cp_parser *);
2288 /* Derived classes [gram.class.derived] */
2290 static tree cp_parser_base_clause
2291 (cp_parser *);
2292 static tree cp_parser_base_specifier
2293 (cp_parser *);
2295 /* Special member functions [gram.special] */
2297 static tree cp_parser_conversion_function_id
2298 (cp_parser *);
2299 static tree cp_parser_conversion_type_id
2300 (cp_parser *);
2301 static cp_declarator *cp_parser_conversion_declarator_opt
2302 (cp_parser *);
2303 static void cp_parser_ctor_initializer_opt
2304 (cp_parser *);
2305 static void cp_parser_mem_initializer_list
2306 (cp_parser *);
2307 static tree cp_parser_mem_initializer
2308 (cp_parser *);
2309 static tree cp_parser_mem_initializer_id
2310 (cp_parser *);
2312 /* Overloading [gram.over] */
2314 static cp_expr cp_parser_operator_function_id
2315 (cp_parser *);
2316 static cp_expr cp_parser_operator
2317 (cp_parser *);
2319 /* Templates [gram.temp] */
2321 static void cp_parser_template_declaration
2322 (cp_parser *, bool);
2323 static tree cp_parser_template_parameter_list
2324 (cp_parser *);
2325 static tree cp_parser_template_parameter
2326 (cp_parser *, bool *, bool *);
2327 static tree cp_parser_type_parameter
2328 (cp_parser *, bool *);
2329 static tree cp_parser_template_id
2330 (cp_parser *, bool, bool, enum tag_types, bool);
2331 static tree cp_parser_template_name
2332 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2333 static tree cp_parser_template_argument_list
2334 (cp_parser *);
2335 static tree cp_parser_template_argument
2336 (cp_parser *);
2337 static void cp_parser_explicit_instantiation
2338 (cp_parser *);
2339 static void cp_parser_explicit_specialization
2340 (cp_parser *);
2342 /* Exception handling [gram.exception] */
2344 static tree cp_parser_try_block
2345 (cp_parser *);
2346 static void cp_parser_function_try_block
2347 (cp_parser *);
2348 static void cp_parser_handler_seq
2349 (cp_parser *);
2350 static void cp_parser_handler
2351 (cp_parser *);
2352 static tree cp_parser_exception_declaration
2353 (cp_parser *);
2354 static tree cp_parser_throw_expression
2355 (cp_parser *);
2356 static tree cp_parser_exception_specification_opt
2357 (cp_parser *);
2358 static tree cp_parser_type_id_list
2359 (cp_parser *);
2361 /* GNU Extensions */
2363 static tree cp_parser_asm_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_asm_operand_list
2366 (cp_parser *);
2367 static tree cp_parser_asm_clobber_list
2368 (cp_parser *);
2369 static tree cp_parser_asm_label_list
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_attribute_p
2372 (cp_parser *);
2373 static bool cp_next_tokens_can_be_gnu_attribute_p
2374 (cp_parser *);
2375 static bool cp_next_tokens_can_be_std_attribute_p
2376 (cp_parser *);
2377 static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser *, size_t);
2379 static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser *, size_t);
2381 static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser *, size_t);
2383 static tree cp_parser_attributes_opt
2384 (cp_parser *);
2385 static tree cp_parser_gnu_attributes_opt
2386 (cp_parser *);
2387 static tree cp_parser_gnu_attribute_list
2388 (cp_parser *);
2389 static tree cp_parser_std_attribute
2390 (cp_parser *, tree);
2391 static tree cp_parser_std_attribute_spec
2392 (cp_parser *);
2393 static tree cp_parser_std_attribute_spec_seq
2394 (cp_parser *);
2395 static bool cp_parser_extension_opt
2396 (cp_parser *, int *);
2397 static void cp_parser_label_declaration
2398 (cp_parser *);
2400 /* Concept Extensions */
2402 static tree cp_parser_requires_clause
2403 (cp_parser *);
2404 static tree cp_parser_requires_clause_opt
2405 (cp_parser *);
2406 static tree cp_parser_requires_expression
2407 (cp_parser *);
2408 static tree cp_parser_requirement_parameter_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement_body
2411 (cp_parser *);
2412 static tree cp_parser_requirement_list
2413 (cp_parser *);
2414 static tree cp_parser_requirement
2415 (cp_parser *);
2416 static tree cp_parser_simple_requirement
2417 (cp_parser *);
2418 static tree cp_parser_compound_requirement
2419 (cp_parser *);
2420 static tree cp_parser_type_requirement
2421 (cp_parser *);
2422 static tree cp_parser_nested_requirement
2423 (cp_parser *);
2425 /* Transactional Memory Extensions */
2427 static tree cp_parser_transaction
2428 (cp_parser *, cp_token *);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser *, enum rid);
2431 static void cp_parser_function_transaction
2432 (cp_parser *, enum rid);
2433 static tree cp_parser_transaction_cancel
2434 (cp_parser *);
2436 enum pragma_context {
2437 pragma_external,
2438 pragma_member,
2439 pragma_objc_icode,
2440 pragma_stmt,
2441 pragma_compound
2443 static bool cp_parser_pragma
2444 (cp_parser *, enum pragma_context, bool *);
2446 /* Objective-C++ Productions */
2448 static tree cp_parser_objc_message_receiver
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_args
2451 (cp_parser *);
2452 static tree cp_parser_objc_message_expression
2453 (cp_parser *);
2454 static cp_expr cp_parser_objc_encode_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_defs_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_protocol_expression
2459 (cp_parser *);
2460 static tree cp_parser_objc_selector_expression
2461 (cp_parser *);
2462 static cp_expr cp_parser_objc_expression
2463 (cp_parser *);
2464 static bool cp_parser_objc_selector_p
2465 (enum cpp_ttype);
2466 static tree cp_parser_objc_selector
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_refs_opt
2469 (cp_parser *);
2470 static void cp_parser_objc_declaration
2471 (cp_parser *, tree);
2472 static tree cp_parser_objc_statement
2473 (cp_parser *);
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser *, tree *);
2476 static void cp_parser_objc_at_property_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_synthesize_declaration
2479 (cp_parser *) ;
2480 static void cp_parser_objc_at_dynamic_declaration
2481 (cp_parser *) ;
2482 static tree cp_parser_objc_struct_declaration
2483 (cp_parser *) ;
2485 /* Utility Routines */
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser *, tree, location_t);
2491 static tree cp_parser_maybe_treat_template_as_class
2492 (tree, bool);
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser *, cp_declarator *, location_t);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser *, unsigned, location_t, cp_declarator *);
2497 static cp_expr cp_parser_simple_cast_expression
2498 (cp_parser *);
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser *, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser *, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser *, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser *, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec<deferred_access_check, va_gc> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser *, tree);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2517 static tree cp_parser_save_nsdmi
2518 (cp_parser *);
2519 static tree cp_parser_enclosed_template_argument_list
2520 (cp_parser *);
2521 static void cp_parser_save_default_args
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser *, tree);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser *, tree, tree, tree);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser *, tree);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser *, tree);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser *, enum rid);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser *, enum rid);
2535 static bool cp_parser_declares_only_class_p
2536 (cp_parser *);
2537 static void cp_parser_set_storage_class
2538 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq *decl_specs,
2543 cp_decl_spec ds, cp_token *);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq *);
2546 static void cp_parser_required_error
2547 (cp_parser *, required_token, bool, location_t);
2548 static cp_token *cp_parser_require
2549 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2550 static cp_token *cp_parser_require_keyword
2551 (cp_parser *, enum rid, required_token);
2552 static bool cp_parser_token_starts_function_definition_p
2553 (cp_token *);
2554 static bool cp_parser_next_token_starts_class_definition_p
2555 (cp_parser *);
2556 static bool cp_parser_next_token_ends_template_argument_p
2557 (cp_parser *);
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser *, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2561 (cp_token *);
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2563 (cp_token *);
2564 static void cp_parser_check_class_key
2565 (enum tag_types, tree type);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type, location_t location);
2568 static bool cp_parser_optional_template_keyword
2569 (cp_parser *);
2570 static void cp_parser_pre_parsed_nested_name_specifier
2571 (cp_parser *);
2572 static bool cp_parser_cache_group
2573 (cp_parser *, enum cpp_ttype, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser *parser, bool nsdmi);
2576 static void cp_parser_parse_tentatively
2577 (cp_parser *);
2578 static void cp_parser_commit_to_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_commit_to_topmost_tentative_parse
2581 (cp_parser *);
2582 static void cp_parser_abort_tentative_parse
2583 (cp_parser *);
2584 static bool cp_parser_parse_definitely
2585 (cp_parser *);
2586 static inline bool cp_parser_parsing_tentatively
2587 (cp_parser *);
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2589 (cp_parser *);
2590 static void cp_parser_error
2591 (cp_parser *, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser *, tree, tree, name_lookup_error, location_t);
2594 static bool cp_parser_simulate_error
2595 (cp_parser *);
2596 static bool cp_parser_check_type_definition
2597 (cp_parser *);
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator *, tree, location_t type_location);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser *, tree, enum tag_types, location_t location);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser *, non_integral_constant);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser *, tree, location_t);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2607 (cp_parser *);
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser *, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_block_or_statement
2615 (cp_parser *);
2616 static bool cp_parser_skip_to_closing_brace
2617 (cp_parser *);
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2619 (cp_parser *);
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser*, cp_token *);
2622 static bool cp_parser_error_occurred
2623 (cp_parser *);
2624 static bool cp_parser_allow_gnu_extensions_p
2625 (cp_parser *);
2626 static bool cp_parser_is_pure_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_string_literal
2629 (cp_token *);
2630 static bool cp_parser_is_keyword
2631 (cp_token *, enum rid);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser *, tree, location_t location);
2634 static cp_declarator * cp_parser_make_indirect_declarator
2635 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2636 static bool cp_parser_compound_literal_p
2637 (cp_parser *);
2638 static bool cp_parser_array_designator_p
2639 (cp_parser *);
2640 static bool cp_parser_init_statement_p
2641 (cp_parser *);
2642 static bool cp_parser_skip_to_closing_square_bracket
2643 (cp_parser *);
2645 /* Concept-related syntactic transformations */
2647 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2648 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2656 ++cp_unevaluated_operand;
2657 ++c_inhibit_evaluation_warnings;
2660 cp_unevaluated::~cp_unevaluated ()
2662 --c_inhibit_evaluation_warnings;
2663 --cp_unevaluated_operand;
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2669 /* Returns nonzero if we are parsing tentatively. */
2671 static inline bool
2672 cp_parser_parsing_tentatively (cp_parser* parser)
2674 return parser->context->next != NULL;
2677 /* Returns nonzero if TOKEN is a string literal. */
2679 static bool
2680 cp_parser_is_pure_string_literal (cp_token* token)
2682 return (token->type == CPP_STRING ||
2683 token->type == CPP_STRING16 ||
2684 token->type == CPP_STRING32 ||
2685 token->type == CPP_WSTRING ||
2686 token->type == CPP_UTF8STRING);
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2692 static bool
2693 cp_parser_is_string_literal (cp_token* token)
2695 return (cp_parser_is_pure_string_literal (token) ||
2696 token->type == CPP_STRING_USERDEF ||
2697 token->type == CPP_STRING16_USERDEF ||
2698 token->type == CPP_STRING32_USERDEF ||
2699 token->type == CPP_WSTRING_USERDEF ||
2700 token->type == CPP_UTF8STRING_USERDEF);
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2705 static bool
2706 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2708 return token->keyword == keyword;
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 PRAGMA_NONE. */
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token *token)
2717 if (token->type != CPP_PRAGMA)
2718 return PRAGMA_NONE;
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2732 of the marker. */
2734 static bool
2735 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2736 location_t *out_loc)
2738 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2739 if (token2->type != tok1_kind)
2740 return false;
2741 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2742 if (token3->type != tok1_kind)
2743 return false;
2744 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2745 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2746 return false;
2748 /* It must be at the start of the line. */
2749 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2750 if (LOCATION_COLUMN (start_loc) != 1)
2751 return false;
2753 /* We have a conflict marker. Construct a location of the form:
2754 <<<<<<<
2755 ^~~~~~~
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc = get_finish (token4->location);
2758 *out_loc = make_location (start_loc, start_loc, finish_loc);
2760 return true;
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 RT_CLOSE_PAREN. */
2766 static const char *
2767 get_matching_symbol (required_token token_desc)
2769 switch (token_desc)
2771 default:
2772 gcc_unreachable ();
2773 return "";
2774 case RT_CLOSE_BRACE:
2775 return "{";
2776 case RT_CLOSE_PAREN:
2777 return "(";
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc)
2787 switch (token_desc)
2789 case RT_SEMICOLON:
2790 return CPP_SEMICOLON;
2791 case RT_OPEN_PAREN:
2792 return CPP_OPEN_PAREN;
2793 case RT_CLOSE_BRACE:
2794 return CPP_CLOSE_BRACE;
2795 case RT_OPEN_BRACE:
2796 return CPP_OPEN_BRACE;
2797 case RT_CLOSE_SQUARE:
2798 return CPP_CLOSE_SQUARE;
2799 case RT_OPEN_SQUARE:
2800 return CPP_OPEN_SQUARE;
2801 case RT_COMMA:
2802 return CPP_COMMA;
2803 case RT_COLON:
2804 return CPP_COLON;
2805 case RT_CLOSE_PAREN:
2806 return CPP_CLOSE_PAREN;
2808 default:
2809 /* Use CPP_EOF as a "no completions possible" code. */
2810 return CPP_EOF;
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2821 OTHER-TOKEN".
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 location. */
2833 static void
2834 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2835 required_token missing_token_desc,
2836 location_t matching_location)
2838 cp_token *token = cp_lexer_peek_token (parser->lexer);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token);
2843 if (token->type == CPP_PRAGMA)
2845 error_at (token->location,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser, token);
2848 return;
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token->type == CPP_LSHIFT
2853 || token->type == CPP_RSHIFT
2854 || token->type == CPP_EQ_EQ)
2856 location_t loc;
2857 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2859 error_at (loc, "version control conflict marker in file");
2860 return;
2864 gcc_rich_location richloc (input_location);
2866 bool added_matching_location = false;
2868 if (missing_token_desc != RT_NONE)
2870 /* Potentially supply a fix-it hint, suggesting to add the
2871 missing token immediately after the *previous* token.
2872 This may move the primary location within richloc. */
2873 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2874 location_t prev_token_loc
2875 = cp_lexer_previous_token (parser->lexer)->location;
2876 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2878 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2879 Attempt to consolidate diagnostics by printing it as a
2880 secondary range within the main diagnostic. */
2881 if (matching_location != UNKNOWN_LOCATION)
2882 added_matching_location
2883 = richloc.add_location_if_nearby (matching_location);
2886 /* Actually emit the error. */
2887 c_parse_error (gmsgid,
2888 /* Because c_parser_error does not understand
2889 CPP_KEYWORD, keywords are treated like
2890 identifiers. */
2891 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2892 token->u.value, token->flags, &richloc);
2894 if (missing_token_desc != RT_NONE)
2896 /* If we weren't able to consolidate matching_location, then
2897 print it as a secondary diagnostic. */
2898 if (matching_location != UNKNOWN_LOCATION
2899 && !added_matching_location)
2900 inform (matching_location, "to match this %qs",
2901 get_matching_symbol (missing_token_desc));
2905 /* If not parsing tentatively, issue a diagnostic of the form
2906 FILE:LINE: MESSAGE before TOKEN
2907 where TOKEN is the next token in the input stream. MESSAGE
2908 (specified by the caller) is usually of the form "expected
2909 OTHER-TOKEN". */
2911 static void
2912 cp_parser_error (cp_parser* parser, const char* gmsgid)
2914 if (!cp_parser_simulate_error (parser))
2915 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2918 /* Issue an error about name-lookup failing. NAME is the
2919 IDENTIFIER_NODE DECL is the result of
2920 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2921 the thing that we hoped to find. */
2923 static void
2924 cp_parser_name_lookup_error (cp_parser* parser,
2925 tree name,
2926 tree decl,
2927 name_lookup_error desired,
2928 location_t location)
2930 /* If name lookup completely failed, tell the user that NAME was not
2931 declared. */
2932 if (decl == error_mark_node)
2934 if (parser->scope && parser->scope != global_namespace)
2935 error_at (location, "%<%E::%E%> has not been declared",
2936 parser->scope, name);
2937 else if (parser->scope == global_namespace)
2938 error_at (location, "%<::%E%> has not been declared", name);
2939 else if (parser->object_scope
2940 && !CLASS_TYPE_P (parser->object_scope))
2941 error_at (location, "request for member %qE in non-class type %qT",
2942 name, parser->object_scope);
2943 else if (parser->object_scope)
2944 error_at (location, "%<%T::%E%> has not been declared",
2945 parser->object_scope, name);
2946 else
2947 error_at (location, "%qE has not been declared", name);
2949 else if (parser->scope && parser->scope != global_namespace)
2951 switch (desired)
2953 case NLE_TYPE:
2954 error_at (location, "%<%E::%E%> is not a type",
2955 parser->scope, name);
2956 break;
2957 case NLE_CXX98:
2958 error_at (location, "%<%E::%E%> is not a class or namespace",
2959 parser->scope, name);
2960 break;
2961 case NLE_NOT_CXX98:
2962 error_at (location,
2963 "%<%E::%E%> is not a class, namespace, or enumeration",
2964 parser->scope, name);
2965 break;
2966 default:
2967 gcc_unreachable ();
2971 else if (parser->scope == global_namespace)
2973 switch (desired)
2975 case NLE_TYPE:
2976 error_at (location, "%<::%E%> is not a type", name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<::%E%> is not a class or namespace", name);
2980 break;
2981 case NLE_NOT_CXX98:
2982 error_at (location,
2983 "%<::%E%> is not a class, namespace, or enumeration",
2984 name);
2985 break;
2986 default:
2987 gcc_unreachable ();
2990 else
2992 switch (desired)
2994 case NLE_TYPE:
2995 error_at (location, "%qE is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%qE is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%qE is not a class, namespace, or enumeration", name);
3003 break;
3004 default:
3005 gcc_unreachable ();
3010 /* If we are parsing tentatively, remember that an error has occurred
3011 during this tentative parse. Returns true if the error was
3012 simulated; false if a message should be issued by the caller. */
3014 static bool
3015 cp_parser_simulate_error (cp_parser* parser)
3017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3019 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3020 return true;
3022 return false;
3025 /* This function is called when a type is defined. If type
3026 definitions are forbidden at this point, an error message is
3027 issued. */
3029 static bool
3030 cp_parser_check_type_definition (cp_parser* parser)
3032 /* If types are forbidden here, issue a message. */
3033 if (parser->type_definition_forbidden_message)
3035 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3036 in the message need to be interpreted. */
3037 error (parser->type_definition_forbidden_message);
3038 return false;
3040 return true;
3043 /* This function is called when the DECLARATOR is processed. The TYPE
3044 was a type defined in the decl-specifiers. If it is invalid to
3045 define a type in the decl-specifiers for DECLARATOR, an error is
3046 issued. TYPE_LOCATION is the location of TYPE and is used
3047 for error reporting. */
3049 static void
3050 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3051 tree type, location_t type_location)
3053 /* [dcl.fct] forbids type definitions in return types.
3054 Unfortunately, it's not easy to know whether or not we are
3055 processing a return type until after the fact. */
3056 while (declarator
3057 && (declarator->kind == cdk_pointer
3058 || declarator->kind == cdk_reference
3059 || declarator->kind == cdk_ptrmem))
3060 declarator = declarator->declarator;
3061 if (declarator
3062 && declarator->kind == cdk_function)
3064 error_at (type_location,
3065 "new types may not be defined in a return type");
3066 inform (type_location,
3067 "(perhaps a semicolon is missing after the definition of %qT)",
3068 type);
3072 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3073 "<" in any valid C++ program. If the next token is indeed "<",
3074 issue a message warning the user about what appears to be an
3075 invalid attempt to form a template-id. LOCATION is the location
3076 of the type-specifier (TYPE) */
3078 static void
3079 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3080 tree type,
3081 enum tag_types tag_type,
3082 location_t location)
3084 cp_token_position start = 0;
3086 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3088 if (TREE_CODE (type) == TYPE_DECL)
3089 type = TREE_TYPE (type);
3090 if (TYPE_P (type) && !template_placeholder_p (type))
3091 error_at (location, "%qT is not a template", type);
3092 else if (identifier_p (type))
3094 if (tag_type != none_type)
3095 error_at (location, "%qE is not a class template", type);
3096 else
3097 error_at (location, "%qE is not a template", type);
3099 else
3100 error_at (location, "invalid template-id");
3101 /* Remember the location of the invalid "<". */
3102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3103 start = cp_lexer_token_position (parser->lexer, true);
3104 /* Consume the "<". */
3105 cp_lexer_consume_token (parser->lexer);
3106 /* Parse the template arguments. */
3107 cp_parser_enclosed_template_argument_list (parser);
3108 /* Permanently remove the invalid template arguments so that
3109 this error message is not issued again. */
3110 if (start)
3111 cp_lexer_purge_tokens_after (parser->lexer, start);
3115 /* If parsing an integral constant-expression, issue an error message
3116 about the fact that THING appeared and return true. Otherwise,
3117 return false. In either case, set
3118 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3120 static bool
3121 cp_parser_non_integral_constant_expression (cp_parser *parser,
3122 non_integral_constant thing)
3124 parser->non_integral_constant_expression_p = true;
3125 if (parser->integral_constant_expression_p)
3127 if (!parser->allow_non_integral_constant_expression_p)
3129 const char *msg = NULL;
3130 switch (thing)
3132 case NIC_FLOAT:
3133 pedwarn (input_location, OPT_Wpedantic,
3134 "ISO C++ forbids using a floating-point literal "
3135 "in a constant-expression");
3136 return true;
3137 case NIC_CAST:
3138 error ("a cast to a type other than an integral or "
3139 "enumeration type cannot appear in a "
3140 "constant-expression");
3141 return true;
3142 case NIC_TYPEID:
3143 error ("%<typeid%> operator "
3144 "cannot appear in a constant-expression");
3145 return true;
3146 case NIC_NCC:
3147 error ("non-constant compound literals "
3148 "cannot appear in a constant-expression");
3149 return true;
3150 case NIC_FUNC_CALL:
3151 error ("a function call "
3152 "cannot appear in a constant-expression");
3153 return true;
3154 case NIC_INC:
3155 error ("an increment "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_DEC:
3159 error ("an decrement "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_ARRAY_REF:
3163 error ("an array reference "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_ADDR_LABEL:
3167 error ("the address of a label "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_OVERLOADED:
3171 error ("calls to overloaded operators "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ASSIGNMENT:
3175 error ("an assignment cannot appear in a constant-expression");
3176 return true;
3177 case NIC_COMMA:
3178 error ("a comma operator "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_CONSTRUCTOR:
3182 error ("a call to a constructor "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_TRANSACTION:
3186 error ("a transaction expression "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_THIS:
3190 msg = "this";
3191 break;
3192 case NIC_FUNC_NAME:
3193 msg = "__FUNCTION__";
3194 break;
3195 case NIC_PRETTY_FUNC:
3196 msg = "__PRETTY_FUNCTION__";
3197 break;
3198 case NIC_C99_FUNC:
3199 msg = "__func__";
3200 break;
3201 case NIC_VA_ARG:
3202 msg = "va_arg";
3203 break;
3204 case NIC_ARROW:
3205 msg = "->";
3206 break;
3207 case NIC_POINT:
3208 msg = ".";
3209 break;
3210 case NIC_STAR:
3211 msg = "*";
3212 break;
3213 case NIC_ADDR:
3214 msg = "&";
3215 break;
3216 case NIC_PREINCREMENT:
3217 msg = "++";
3218 break;
3219 case NIC_PREDECREMENT:
3220 msg = "--";
3221 break;
3222 case NIC_NEW:
3223 msg = "new";
3224 break;
3225 case NIC_DEL:
3226 msg = "delete";
3227 break;
3228 default:
3229 gcc_unreachable ();
3231 if (msg)
3232 error ("%qs cannot appear in a constant-expression", msg);
3233 return true;
3236 return false;
3239 /* Emit a diagnostic for an invalid type name. This function commits
3240 to the current active tentative parse, if any. (Otherwise, the
3241 problematic construct might be encountered again later, resulting
3242 in duplicate error messages.) LOCATION is the location of ID. */
3244 static void
3245 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3246 location_t location)
3248 tree decl, ambiguous_decls;
3249 cp_parser_commit_to_tentative_parse (parser);
3250 /* Try to lookup the identifier. */
3251 decl = cp_parser_lookup_name (parser, id, none_type,
3252 /*is_template=*/false,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3255 &ambiguous_decls, location);
3256 if (ambiguous_decls)
3257 /* If the lookup was ambiguous, an error will already have
3258 been issued. */
3259 return;
3260 /* If the lookup found a template-name, it means that the user forgot
3261 to specify an argument list. Emit a useful error message. */
3262 if (DECL_TYPE_TEMPLATE_P (decl))
3264 error_at (location,
3265 "invalid use of template-name %qE without an argument list",
3266 decl);
3267 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3268 inform (location, "class template argument deduction is only available "
3269 "with -std=c++17 or -std=gnu++17");
3270 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3272 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3273 error_at (location, "invalid use of destructor %qD as a type", id);
3274 else if (TREE_CODE (decl) == TYPE_DECL)
3275 /* Something like 'unsigned A a;' */
3276 error_at (location, "invalid combination of multiple type-specifiers");
3277 else if (!parser->scope)
3279 /* Issue an error message. */
3280 name_hint hint;
3281 if (TREE_CODE (id) == IDENTIFIER_NODE)
3282 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3283 if (hint)
3285 gcc_rich_location richloc (location);
3286 richloc.add_fixit_replace (hint.suggestion ());
3287 error_at (&richloc,
3288 "%qE does not name a type; did you mean %qs?",
3289 id, hint.suggestion ());
3291 else
3292 error_at (location, "%qE does not name a type", id);
3293 /* If we're in a template class, it's possible that the user was
3294 referring to a type from a base class. For example:
3296 template <typename T> struct A { typedef T X; };
3297 template <typename T> struct B : public A<T> { X x; };
3299 The user should have said "typename A<T>::X". */
3300 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3301 inform (location, "C++11 %<constexpr%> only available with "
3302 "-std=c++11 or -std=gnu++11");
3303 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3304 inform (location, "C++11 %<noexcept%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11
3307 && TREE_CODE (id) == IDENTIFIER_NODE
3308 && id_equal (id, "thread_local"))
3309 inform (location, "C++11 %<thread_local%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3312 inform (location, "%<concept%> only available with -fconcepts");
3313 else if (processing_template_decl && current_class_type
3314 && TYPE_BINFO (current_class_type))
3316 tree b;
3318 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3320 b = TREE_CHAIN (b))
3322 tree base_type = BINFO_TYPE (b);
3323 if (CLASS_TYPE_P (base_type)
3324 && dependent_type_p (base_type))
3326 tree field;
3327 /* Go from a particular instantiation of the
3328 template (which will have an empty TYPE_FIELDs),
3329 to the main version. */
3330 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3331 for (field = TYPE_FIELDS (base_type);
3332 field;
3333 field = DECL_CHAIN (field))
3334 if (TREE_CODE (field) == TYPE_DECL
3335 && DECL_NAME (field) == id)
3337 inform (location,
3338 "(perhaps %<typename %T::%E%> was intended)",
3339 BINFO_TYPE (b), id);
3340 break;
3342 if (field)
3343 break;
3348 /* Here we diagnose qualified-ids where the scope is actually correct,
3349 but the identifier does not resolve to a valid type name. */
3350 else if (parser->scope != error_mark_node)
3352 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3354 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3355 error_at (location_of (id),
3356 "%qE in namespace %qE does not name a template type",
3357 id, parser->scope);
3358 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3359 error_at (location_of (id),
3360 "%qE in namespace %qE does not name a template type",
3361 TREE_OPERAND (id, 0), parser->scope);
3362 else
3363 error_at (location_of (id),
3364 "%qE in namespace %qE does not name a type",
3365 id, parser->scope);
3366 if (DECL_P (decl))
3367 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3368 else if (decl == error_mark_node)
3369 suggest_alternative_in_explicit_scope (location, id,
3370 parser->scope);
3372 else if (CLASS_TYPE_P (parser->scope)
3373 && constructor_name_p (id, parser->scope))
3375 /* A<T>::A<T>() */
3376 error_at (location, "%<%T::%E%> names the constructor, not"
3377 " the type", parser->scope, id);
3378 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3379 error_at (location, "and %qT has no template constructors",
3380 parser->scope);
3382 else if (TYPE_P (parser->scope)
3383 && dependent_scope_p (parser->scope))
3385 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3386 error_at (location,
3387 "need %<typename%> before %<%T::%D::%E%> because "
3388 "%<%T::%D%> is a dependent scope",
3389 TYPE_CONTEXT (parser->scope),
3390 TYPENAME_TYPE_FULLNAME (parser->scope),
3392 TYPE_CONTEXT (parser->scope),
3393 TYPENAME_TYPE_FULLNAME (parser->scope));
3394 else
3395 error_at (location, "need %<typename%> before %<%T::%E%> because "
3396 "%qT is a dependent scope",
3397 parser->scope, id, parser->scope);
3399 else if (TYPE_P (parser->scope))
3401 if (!COMPLETE_TYPE_P (parser->scope))
3402 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3403 parser->scope);
3404 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3405 error_at (location_of (id),
3406 "%qE in %q#T does not name a template type",
3407 id, parser->scope);
3408 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3409 error_at (location_of (id),
3410 "%qE in %q#T does not name a template type",
3411 TREE_OPERAND (id, 0), parser->scope);
3412 else
3413 error_at (location_of (id),
3414 "%qE in %q#T does not name a type",
3415 id, parser->scope);
3416 if (DECL_P (decl))
3417 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3419 else
3420 gcc_unreachable ();
3424 /* Check for a common situation where a type-name should be present,
3425 but is not, and issue a sensible error message. Returns true if an
3426 invalid type-name was detected.
3428 The situation handled by this function are variable declarations of the
3429 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3430 Usually, `ID' should name a type, but if we got here it means that it
3431 does not. We try to emit the best possible error message depending on
3432 how exactly the id-expression looks like. */
3434 static bool
3435 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3437 tree id;
3438 cp_token *token = cp_lexer_peek_token (parser->lexer);
3440 /* Avoid duplicate error about ambiguous lookup. */
3441 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3443 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3444 if (next->type == CPP_NAME && next->error_reported)
3445 goto out;
3448 cp_parser_parse_tentatively (parser);
3449 id = cp_parser_id_expression (parser,
3450 /*template_keyword_p=*/false,
3451 /*check_dependency_p=*/true,
3452 /*template_p=*/NULL,
3453 /*declarator_p=*/true,
3454 /*optional_p=*/false);
3455 /* If the next token is a (, this is a function with no explicit return
3456 type, i.e. constructor, destructor or conversion op. */
3457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3458 || TREE_CODE (id) == TYPE_DECL)
3460 cp_parser_abort_tentative_parse (parser);
3461 return false;
3463 if (!cp_parser_parse_definitely (parser))
3464 return false;
3466 /* Emit a diagnostic for the invalid type. */
3467 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3468 out:
3469 /* If we aren't in the middle of a declarator (i.e. in a
3470 parameter-declaration-clause), skip to the end of the declaration;
3471 there's no point in trying to process it. */
3472 if (!parser->in_declarator_p)
3473 cp_parser_skip_to_end_of_block_or_statement (parser);
3474 return true;
3477 /* Consume tokens up to, and including, the next non-nested closing `)'.
3478 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3479 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3480 found an unnested token of that type. */
3482 static int
3483 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3484 bool recovering,
3485 cpp_ttype or_ttype,
3486 bool consume_paren)
3488 unsigned paren_depth = 0;
3489 unsigned brace_depth = 0;
3490 unsigned square_depth = 0;
3492 if (recovering && or_ttype == CPP_EOF
3493 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3494 return 0;
3496 while (true)
3498 cp_token * token = cp_lexer_peek_token (parser->lexer);
3500 /* Have we found what we're looking for before the closing paren? */
3501 if (token->type == or_ttype && or_ttype != CPP_EOF
3502 && !brace_depth && !paren_depth && !square_depth)
3503 return -1;
3505 switch (token->type)
3507 case CPP_EOF:
3508 case CPP_PRAGMA_EOL:
3509 /* If we've run out of tokens, then there is no closing `)'. */
3510 return 0;
3512 /* This is good for lambda expression capture-lists. */
3513 case CPP_OPEN_SQUARE:
3514 ++square_depth;
3515 break;
3516 case CPP_CLOSE_SQUARE:
3517 if (!square_depth--)
3518 return 0;
3519 break;
3521 case CPP_SEMICOLON:
3522 /* This matches the processing in skip_to_end_of_statement. */
3523 if (!brace_depth)
3524 return 0;
3525 break;
3527 case CPP_OPEN_BRACE:
3528 ++brace_depth;
3529 break;
3530 case CPP_CLOSE_BRACE:
3531 if (!brace_depth--)
3532 return 0;
3533 break;
3535 case CPP_OPEN_PAREN:
3536 if (!brace_depth)
3537 ++paren_depth;
3538 break;
3540 case CPP_CLOSE_PAREN:
3541 if (!brace_depth && !paren_depth--)
3543 if (consume_paren)
3544 cp_lexer_consume_token (parser->lexer);
3545 return 1;
3547 break;
3549 default:
3550 break;
3553 /* Consume the token. */
3554 cp_lexer_consume_token (parser->lexer);
3558 /* Consume tokens up to, and including, the next non-nested closing `)'.
3559 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3560 are doing error recovery. Returns -1 if OR_COMMA is true and we
3561 found an unnested token of that type. */
3563 static int
3564 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3565 bool recovering,
3566 bool or_comma,
3567 bool consume_paren)
3569 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3570 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3571 ttype, consume_paren);
3574 /* Consume tokens until we reach the end of the current statement.
3575 Normally, that will be just before consuming a `;'. However, if a
3576 non-nested `}' comes first, then we stop before consuming that. */
3578 static void
3579 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3581 unsigned nesting_depth = 0;
3583 /* Unwind generic function template scope if necessary. */
3584 if (parser->fully_implicit_function_template_p)
3585 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3587 while (true)
3589 cp_token *token = cp_lexer_peek_token (parser->lexer);
3591 switch (token->type)
3593 case CPP_EOF:
3594 case CPP_PRAGMA_EOL:
3595 /* If we've run out of tokens, stop. */
3596 return;
3598 case CPP_SEMICOLON:
3599 /* If the next token is a `;', we have reached the end of the
3600 statement. */
3601 if (!nesting_depth)
3602 return;
3603 break;
3605 case CPP_CLOSE_BRACE:
3606 /* If this is a non-nested '}', stop before consuming it.
3607 That way, when confronted with something like:
3609 { 3 + }
3611 we stop before consuming the closing '}', even though we
3612 have not yet reached a `;'. */
3613 if (nesting_depth == 0)
3614 return;
3616 /* If it is the closing '}' for a block that we have
3617 scanned, stop -- but only after consuming the token.
3618 That way given:
3620 void f g () { ... }
3621 typedef int I;
3623 we will stop after the body of the erroneously declared
3624 function, but before consuming the following `typedef'
3625 declaration. */
3626 if (--nesting_depth == 0)
3628 cp_lexer_consume_token (parser->lexer);
3629 return;
3631 break;
3633 case CPP_OPEN_BRACE:
3634 ++nesting_depth;
3635 break;
3637 default:
3638 break;
3641 /* Consume the token. */
3642 cp_lexer_consume_token (parser->lexer);
3646 /* This function is called at the end of a statement or declaration.
3647 If the next token is a semicolon, it is consumed; otherwise, error
3648 recovery is attempted. */
3650 static void
3651 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3653 /* Look for the trailing `;'. */
3654 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3656 /* If there is additional (erroneous) input, skip to the end of
3657 the statement. */
3658 cp_parser_skip_to_end_of_statement (parser);
3659 /* If the next token is now a `;', consume it. */
3660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3661 cp_lexer_consume_token (parser->lexer);
3665 /* Skip tokens until we have consumed an entire block, or until we
3666 have consumed a non-nested `;'. */
3668 static void
3669 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3671 int nesting_depth = 0;
3673 /* Unwind generic function template scope if necessary. */
3674 if (parser->fully_implicit_function_template_p)
3675 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3677 while (nesting_depth >= 0)
3679 cp_token *token = cp_lexer_peek_token (parser->lexer);
3681 switch (token->type)
3683 case CPP_EOF:
3684 case CPP_PRAGMA_EOL:
3685 /* If we've run out of tokens, stop. */
3686 return;
3688 case CPP_SEMICOLON:
3689 /* Stop if this is an unnested ';'. */
3690 if (!nesting_depth)
3691 nesting_depth = -1;
3692 break;
3694 case CPP_CLOSE_BRACE:
3695 /* Stop if this is an unnested '}', or closes the outermost
3696 nesting level. */
3697 nesting_depth--;
3698 if (nesting_depth < 0)
3699 return;
3700 if (!nesting_depth)
3701 nesting_depth = -1;
3702 break;
3704 case CPP_OPEN_BRACE:
3705 /* Nest. */
3706 nesting_depth++;
3707 break;
3709 default:
3710 break;
3713 /* Consume the token. */
3714 cp_lexer_consume_token (parser->lexer);
3718 /* Skip tokens until a non-nested closing curly brace is the next
3719 token, or there are no more tokens. Return true in the first case,
3720 false otherwise. */
3722 static bool
3723 cp_parser_skip_to_closing_brace (cp_parser *parser)
3725 unsigned nesting_depth = 0;
3727 while (true)
3729 cp_token *token = cp_lexer_peek_token (parser->lexer);
3731 switch (token->type)
3733 case CPP_EOF:
3734 case CPP_PRAGMA_EOL:
3735 /* If we've run out of tokens, stop. */
3736 return false;
3738 case CPP_CLOSE_BRACE:
3739 /* If the next token is a non-nested `}', then we have reached
3740 the end of the current block. */
3741 if (nesting_depth-- == 0)
3742 return true;
3743 break;
3745 case CPP_OPEN_BRACE:
3746 /* If it the next token is a `{', then we are entering a new
3747 block. Consume the entire block. */
3748 ++nesting_depth;
3749 break;
3751 default:
3752 break;
3755 /* Consume the token. */
3756 cp_lexer_consume_token (parser->lexer);
3760 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3761 parameter is the PRAGMA token, allowing us to purge the entire pragma
3762 sequence. */
3764 static void
3765 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3767 cp_token *token;
3769 parser->lexer->in_pragma = false;
3772 token = cp_lexer_consume_token (parser->lexer);
3773 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3775 /* Ensure that the pragma is not parsed again. */
3776 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3779 /* Require pragma end of line, resyncing with it as necessary. The
3780 arguments are as for cp_parser_skip_to_pragma_eol. */
3782 static void
3783 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3785 parser->lexer->in_pragma = false;
3786 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3787 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3790 /* This is a simple wrapper around make_typename_type. When the id is
3791 an unresolved identifier node, we can provide a superior diagnostic
3792 using cp_parser_diagnose_invalid_type_name. */
3794 static tree
3795 cp_parser_make_typename_type (cp_parser *parser, tree id,
3796 location_t id_location)
3798 tree result;
3799 if (identifier_p (id))
3801 result = make_typename_type (parser->scope, id, typename_type,
3802 /*complain=*/tf_none);
3803 if (result == error_mark_node)
3804 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3805 return result;
3807 return make_typename_type (parser->scope, id, typename_type, tf_error);
3810 /* This is a wrapper around the
3811 make_{pointer,ptrmem,reference}_declarator functions that decides
3812 which one to call based on the CODE and CLASS_TYPE arguments. The
3813 CODE argument should be one of the values returned by
3814 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3815 appertain to the pointer or reference. */
3817 static cp_declarator *
3818 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3819 cp_cv_quals cv_qualifiers,
3820 cp_declarator *target,
3821 tree attributes)
3823 if (code == ERROR_MARK)
3824 return cp_error_declarator;
3826 if (code == INDIRECT_REF)
3827 if (class_type == NULL_TREE)
3828 return make_pointer_declarator (cv_qualifiers, target, attributes);
3829 else
3830 return make_ptrmem_declarator (cv_qualifiers, class_type,
3831 target, attributes);
3832 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3833 return make_reference_declarator (cv_qualifiers, target,
3834 false, attributes);
3835 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3836 return make_reference_declarator (cv_qualifiers, target,
3837 true, attributes);
3838 gcc_unreachable ();
3841 /* Create a new C++ parser. */
3843 static cp_parser *
3844 cp_parser_new (void)
3846 cp_parser *parser;
3847 cp_lexer *lexer;
3848 unsigned i;
3850 /* cp_lexer_new_main is called before doing GC allocation because
3851 cp_lexer_new_main might load a PCH file. */
3852 lexer = cp_lexer_new_main ();
3854 /* Initialize the binops_by_token so that we can get the tree
3855 directly from the token. */
3856 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3857 binops_by_token[binops[i].token_type] = binops[i];
3859 parser = ggc_cleared_alloc<cp_parser> ();
3860 parser->lexer = lexer;
3861 parser->context = cp_parser_context_new (NULL);
3863 /* For now, we always accept GNU extensions. */
3864 parser->allow_gnu_extensions_p = 1;
3866 /* The `>' token is a greater-than operator, not the end of a
3867 template-id. */
3868 parser->greater_than_is_operator_p = true;
3870 parser->default_arg_ok_p = true;
3872 /* We are not parsing a constant-expression. */
3873 parser->integral_constant_expression_p = false;
3874 parser->allow_non_integral_constant_expression_p = false;
3875 parser->non_integral_constant_expression_p = false;
3877 /* Local variable names are not forbidden. */
3878 parser->local_variables_forbidden_p = false;
3880 /* We are not processing an `extern "C"' declaration. */
3881 parser->in_unbraced_linkage_specification_p = false;
3883 /* We are not processing a declarator. */
3884 parser->in_declarator_p = false;
3886 /* We are not processing a template-argument-list. */
3887 parser->in_template_argument_list_p = false;
3889 /* We are not in an iteration statement. */
3890 parser->in_statement = 0;
3892 /* We are not in a switch statement. */
3893 parser->in_switch_statement_p = false;
3895 /* We are not parsing a type-id inside an expression. */
3896 parser->in_type_id_in_expr_p = false;
3898 /* Declarations aren't implicitly extern "C". */
3899 parser->implicit_extern_c = false;
3901 /* String literals should be translated to the execution character set. */
3902 parser->translate_strings_p = true;
3904 /* We are not parsing a function body. */
3905 parser->in_function_body = false;
3907 /* We can correct until told otherwise. */
3908 parser->colon_corrects_to_scope_p = true;
3910 /* The unparsed function queue is empty. */
3911 push_unparsed_function_queues (parser);
3913 /* There are no classes being defined. */
3914 parser->num_classes_being_defined = 0;
3916 /* No template parameters apply. */
3917 parser->num_template_parameter_lists = 0;
3919 /* Special parsing data structures. */
3920 parser->omp_declare_simd = NULL;
3921 parser->oacc_routine = NULL;
3923 /* Not declaring an implicit function template. */
3924 parser->auto_is_implicit_function_template_parm_p = false;
3925 parser->fully_implicit_function_template_p = false;
3926 parser->implicit_template_parms = 0;
3927 parser->implicit_template_scope = 0;
3929 /* Allow constrained-type-specifiers. */
3930 parser->prevent_constrained_type_specifiers = 0;
3932 /* We haven't yet seen an 'extern "C"'. */
3933 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3935 return parser;
3938 /* Create a cp_lexer structure which will emit the tokens in CACHE
3939 and push it onto the parser's lexer stack. This is used for delayed
3940 parsing of in-class method bodies and default arguments, and should
3941 not be confused with tentative parsing. */
3942 static void
3943 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3945 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3946 lexer->next = parser->lexer;
3947 parser->lexer = lexer;
3949 /* Move the current source position to that of the first token in the
3950 new lexer. */
3951 cp_lexer_set_source_position_from_token (lexer->next_token);
3954 /* Pop the top lexer off the parser stack. This is never used for the
3955 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3956 static void
3957 cp_parser_pop_lexer (cp_parser *parser)
3959 cp_lexer *lexer = parser->lexer;
3960 parser->lexer = lexer->next;
3961 cp_lexer_destroy (lexer);
3963 /* Put the current source position back where it was before this
3964 lexer was pushed. */
3965 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3968 /* Lexical conventions [gram.lex] */
3970 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3971 identifier. */
3973 static cp_expr
3974 cp_parser_identifier (cp_parser* parser)
3976 cp_token *token;
3978 /* Look for the identifier. */
3979 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3980 /* Return the value. */
3981 if (token)
3982 return cp_expr (token->u.value, token->location);
3983 else
3984 return error_mark_node;
3987 /* Parse a sequence of adjacent string constants. Returns a
3988 TREE_STRING representing the combined, nul-terminated string
3989 constant. If TRANSLATE is true, translate the string to the
3990 execution character set. If WIDE_OK is true, a wide string is
3991 invalid here.
3993 C++98 [lex.string] says that if a narrow string literal token is
3994 adjacent to a wide string literal token, the behavior is undefined.
3995 However, C99 6.4.5p4 says that this results in a wide string literal.
3996 We follow C99 here, for consistency with the C front end.
3998 This code is largely lifted from lex_string() in c-lex.c.
4000 FUTURE: ObjC++ will need to handle @-strings here. */
4001 static cp_expr
4002 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4003 bool lookup_udlit = true)
4005 tree value;
4006 size_t count;
4007 struct obstack str_ob;
4008 cpp_string str, istr, *strs;
4009 cp_token *tok;
4010 enum cpp_ttype type, curr_type;
4011 int have_suffix_p = 0;
4012 tree string_tree;
4013 tree suffix_id = NULL_TREE;
4014 bool curr_tok_is_userdef_p = false;
4016 tok = cp_lexer_peek_token (parser->lexer);
4017 if (!cp_parser_is_string_literal (tok))
4019 cp_parser_error (parser, "expected string-literal");
4020 return error_mark_node;
4023 location_t loc = tok->location;
4025 if (cpp_userdef_string_p (tok->type))
4027 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4028 curr_type = cpp_userdef_string_remove_type (tok->type);
4029 curr_tok_is_userdef_p = true;
4031 else
4033 string_tree = tok->u.value;
4034 curr_type = tok->type;
4036 type = curr_type;
4038 /* Try to avoid the overhead of creating and destroying an obstack
4039 for the common case of just one string. */
4040 if (!cp_parser_is_string_literal
4041 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4043 cp_lexer_consume_token (parser->lexer);
4045 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4046 str.len = TREE_STRING_LENGTH (string_tree);
4047 count = 1;
4049 if (curr_tok_is_userdef_p)
4051 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4052 have_suffix_p = 1;
4053 curr_type = cpp_userdef_string_remove_type (tok->type);
4055 else
4056 curr_type = tok->type;
4058 strs = &str;
4060 else
4062 location_t last_tok_loc = tok->location;
4063 gcc_obstack_init (&str_ob);
4064 count = 0;
4068 cp_lexer_consume_token (parser->lexer);
4069 count++;
4070 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4071 str.len = TREE_STRING_LENGTH (string_tree);
4073 if (curr_tok_is_userdef_p)
4075 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4076 if (have_suffix_p == 0)
4078 suffix_id = curr_suffix_id;
4079 have_suffix_p = 1;
4081 else if (have_suffix_p == 1
4082 && curr_suffix_id != suffix_id)
4084 error ("inconsistent user-defined literal suffixes"
4085 " %qD and %qD in string literal",
4086 suffix_id, curr_suffix_id);
4087 have_suffix_p = -1;
4089 curr_type = cpp_userdef_string_remove_type (tok->type);
4091 else
4092 curr_type = tok->type;
4094 if (type != curr_type)
4096 if (type == CPP_STRING)
4097 type = curr_type;
4098 else if (curr_type != CPP_STRING)
4100 rich_location rich_loc (line_table, tok->location);
4101 rich_loc.add_range (last_tok_loc, false);
4102 error_at (&rich_loc,
4103 "unsupported non-standard concatenation "
4104 "of string literals");
4108 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4110 last_tok_loc = tok->location;
4112 tok = cp_lexer_peek_token (parser->lexer);
4113 if (cpp_userdef_string_p (tok->type))
4115 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4116 curr_type = cpp_userdef_string_remove_type (tok->type);
4117 curr_tok_is_userdef_p = true;
4119 else
4121 string_tree = tok->u.value;
4122 curr_type = tok->type;
4123 curr_tok_is_userdef_p = false;
4126 while (cp_parser_is_string_literal (tok));
4128 /* A string literal built by concatenation has its caret=start at
4129 the start of the initial string, and its finish at the finish of
4130 the final string literal. */
4131 loc = make_location (loc, loc, get_finish (last_tok_loc));
4133 strs = (cpp_string *) obstack_finish (&str_ob);
4136 if (type != CPP_STRING && !wide_ok)
4138 cp_parser_error (parser, "a wide string is invalid in this context");
4139 type = CPP_STRING;
4142 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4143 (parse_in, strs, count, &istr, type))
4145 value = build_string (istr.len, (const char *)istr.text);
4146 free (CONST_CAST (unsigned char *, istr.text));
4148 switch (type)
4150 default:
4151 case CPP_STRING:
4152 case CPP_UTF8STRING:
4153 TREE_TYPE (value) = char_array_type_node;
4154 break;
4155 case CPP_STRING16:
4156 TREE_TYPE (value) = char16_array_type_node;
4157 break;
4158 case CPP_STRING32:
4159 TREE_TYPE (value) = char32_array_type_node;
4160 break;
4161 case CPP_WSTRING:
4162 TREE_TYPE (value) = wchar_array_type_node;
4163 break;
4166 value = fix_string_type (value);
4168 if (have_suffix_p)
4170 tree literal = build_userdef_literal (suffix_id, value,
4171 OT_NONE, NULL_TREE);
4172 if (lookup_udlit)
4173 value = cp_parser_userdef_string_literal (literal);
4174 else
4175 value = literal;
4178 else
4179 /* cpp_interpret_string has issued an error. */
4180 value = error_mark_node;
4182 if (count > 1)
4183 obstack_free (&str_ob, 0);
4185 return cp_expr (value, loc);
4188 /* Look up a literal operator with the name and the exact arguments. */
4190 static tree
4191 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4193 tree decl;
4194 decl = lookup_name (name);
4195 if (!decl || !is_overloaded_fn (decl))
4196 return error_mark_node;
4198 for (lkp_iterator iter (decl); iter; ++iter)
4200 unsigned int ix;
4201 bool found = true;
4202 tree fn = *iter;
4203 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4204 if (parmtypes != NULL_TREE)
4206 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4207 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4209 tree tparm = TREE_VALUE (parmtypes);
4210 tree targ = TREE_TYPE ((*args)[ix]);
4211 bool ptr = TYPE_PTR_P (tparm);
4212 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4213 if ((ptr || arr || !same_type_p (tparm, targ))
4214 && (!ptr || !arr
4215 || !same_type_p (TREE_TYPE (tparm),
4216 TREE_TYPE (targ))))
4217 found = false;
4219 if (found
4220 && ix == vec_safe_length (args)
4221 /* May be this should be sufficient_parms_p instead,
4222 depending on how exactly should user-defined literals
4223 work in presence of default arguments on the literal
4224 operator parameters. */
4225 && parmtypes == void_list_node)
4226 return decl;
4230 return error_mark_node;
4233 /* Parse a user-defined char constant. Returns a call to a user-defined
4234 literal operator taking the character as an argument. */
4236 static cp_expr
4237 cp_parser_userdef_char_literal (cp_parser *parser)
4239 cp_token *token = cp_lexer_consume_token (parser->lexer);
4240 tree literal = token->u.value;
4241 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4242 tree value = USERDEF_LITERAL_VALUE (literal);
4243 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4244 tree decl, result;
4246 /* Build up a call to the user-defined operator */
4247 /* Lookup the name we got back from the id-expression. */
4248 vec<tree, va_gc> *args = make_tree_vector ();
4249 vec_safe_push (args, value);
4250 decl = lookup_literal_operator (name, args);
4251 if (!decl || decl == error_mark_node)
4253 error ("unable to find character literal operator %qD with %qT argument",
4254 name, TREE_TYPE (value));
4255 release_tree_vector (args);
4256 return error_mark_node;
4258 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4259 release_tree_vector (args);
4260 return result;
4263 /* A subroutine of cp_parser_userdef_numeric_literal to
4264 create a char... template parameter pack from a string node. */
4266 static tree
4267 make_char_string_pack (tree value)
4269 tree charvec;
4270 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4271 const char *str = TREE_STRING_POINTER (value);
4272 int i, len = TREE_STRING_LENGTH (value) - 1;
4273 tree argvec = make_tree_vec (1);
4275 /* Fill in CHARVEC with all of the parameters. */
4276 charvec = make_tree_vec (len);
4277 for (i = 0; i < len; ++i)
4278 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4280 /* Build the argument packs. */
4281 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4283 TREE_VEC_ELT (argvec, 0) = argpack;
4285 return argvec;
4288 /* A subroutine of cp_parser_userdef_numeric_literal to
4289 create a char... template parameter pack from a string node. */
4291 static tree
4292 make_string_pack (tree value)
4294 tree charvec;
4295 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4296 const unsigned char *str
4297 = (const unsigned char *) TREE_STRING_POINTER (value);
4298 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4299 int len = TREE_STRING_LENGTH (value) / sz - 1;
4300 tree argvec = make_tree_vec (2);
4302 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4303 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4305 /* First template parm is character type. */
4306 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4308 /* Fill in CHARVEC with all of the parameters. */
4309 charvec = make_tree_vec (len);
4310 for (int i = 0; i < len; ++i)
4311 TREE_VEC_ELT (charvec, i)
4312 = double_int_to_tree (str_char_type_node,
4313 double_int::from_buffer (str + i * sz, sz));
4315 /* Build the argument packs. */
4316 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4318 TREE_VEC_ELT (argvec, 1) = argpack;
4320 return argvec;
4323 /* Parse a user-defined numeric constant. returns a call to a user-defined
4324 literal operator. */
4326 static cp_expr
4327 cp_parser_userdef_numeric_literal (cp_parser *parser)
4329 cp_token *token = cp_lexer_consume_token (parser->lexer);
4330 tree literal = token->u.value;
4331 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4332 tree value = USERDEF_LITERAL_VALUE (literal);
4333 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4334 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4335 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4336 tree decl, result;
4337 vec<tree, va_gc> *args;
4339 /* Look for a literal operator taking the exact type of numeric argument
4340 as the literal value. */
4341 args = make_tree_vector ();
4342 vec_safe_push (args, value);
4343 decl = lookup_literal_operator (name, args);
4344 if (decl && decl != error_mark_node)
4346 result = finish_call_expr (decl, &args, false, true,
4347 tf_warning_or_error);
4349 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4351 warning_at (token->location, OPT_Woverflow,
4352 "integer literal exceeds range of %qT type",
4353 long_long_unsigned_type_node);
4355 else
4357 if (overflow > 0)
4358 warning_at (token->location, OPT_Woverflow,
4359 "floating literal exceeds range of %qT type",
4360 long_double_type_node);
4361 else if (overflow < 0)
4362 warning_at (token->location, OPT_Woverflow,
4363 "floating literal truncated to zero");
4366 release_tree_vector (args);
4367 return result;
4369 release_tree_vector (args);
4371 /* If the numeric argument didn't work, look for a raw literal
4372 operator taking a const char* argument consisting of the number
4373 in string format. */
4374 args = make_tree_vector ();
4375 vec_safe_push (args, num_string);
4376 decl = lookup_literal_operator (name, args);
4377 if (decl && decl != error_mark_node)
4379 result = finish_call_expr (decl, &args, false, true,
4380 tf_warning_or_error);
4381 release_tree_vector (args);
4382 return result;
4384 release_tree_vector (args);
4386 /* If the raw literal didn't work, look for a non-type template
4387 function with parameter pack char.... Call the function with
4388 template parameter characters representing the number. */
4389 args = make_tree_vector ();
4390 decl = lookup_literal_operator (name, args);
4391 if (decl && decl != error_mark_node)
4393 tree tmpl_args = make_char_string_pack (num_string);
4394 decl = lookup_template_function (decl, tmpl_args);
4395 result = finish_call_expr (decl, &args, false, true,
4396 tf_warning_or_error);
4397 release_tree_vector (args);
4398 return result;
4401 release_tree_vector (args);
4403 /* In C++14 the standard library defines complex number suffixes that
4404 conflict with GNU extensions. Prefer them if <complex> is #included. */
4405 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4406 bool i14 = (cxx_dialect > cxx11
4407 && (id_equal (suffix_id, "i")
4408 || id_equal (suffix_id, "if")
4409 || id_equal (suffix_id, "il")));
4410 diagnostic_t kind = DK_ERROR;
4411 int opt = 0;
4413 if (i14 && ext)
4415 tree cxlit = lookup_qualified_name (std_node,
4416 get_identifier ("complex_literals"),
4417 0, false, false);
4418 if (cxlit == error_mark_node)
4420 /* No <complex>, so pedwarn and use GNU semantics. */
4421 kind = DK_PEDWARN;
4422 opt = OPT_Wpedantic;
4426 bool complained
4427 = emit_diagnostic (kind, input_location, opt,
4428 "unable to find numeric literal operator %qD", name);
4430 if (!complained)
4431 /* Don't inform either. */;
4432 else if (i14)
4434 inform (token->location, "add %<using namespace std::complex_literals%> "
4435 "(from <complex>) to enable the C++14 user-defined literal "
4436 "suffixes");
4437 if (ext)
4438 inform (token->location, "or use %<j%> instead of %<i%> for the "
4439 "GNU built-in suffix");
4441 else if (!ext)
4442 inform (token->location, "use -fext-numeric-literals "
4443 "to enable more built-in suffixes");
4445 if (kind == DK_ERROR)
4446 value = error_mark_node;
4447 else
4449 /* Use the built-in semantics. */
4450 tree type;
4451 if (id_equal (suffix_id, "i"))
4453 if (TREE_CODE (value) == INTEGER_CST)
4454 type = integer_type_node;
4455 else
4456 type = double_type_node;
4458 else if (id_equal (suffix_id, "if"))
4459 type = float_type_node;
4460 else /* if (id_equal (suffix_id, "il")) */
4461 type = long_double_type_node;
4463 value = build_complex (build_complex_type (type),
4464 fold_convert (type, integer_zero_node),
4465 fold_convert (type, value));
4468 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4469 /* Avoid repeated diagnostics. */
4470 token->u.value = value;
4471 return value;
4474 /* Parse a user-defined string constant. Returns a call to a user-defined
4475 literal operator taking a character pointer and the length of the string
4476 as arguments. */
4478 static tree
4479 cp_parser_userdef_string_literal (tree literal)
4481 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4482 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4483 tree value = USERDEF_LITERAL_VALUE (literal);
4484 int len = TREE_STRING_LENGTH (value)
4485 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4486 tree decl, result;
4487 vec<tree, va_gc> *args;
4489 /* Build up a call to the user-defined operator. */
4490 /* Lookup the name we got back from the id-expression. */
4491 args = make_tree_vector ();
4492 vec_safe_push (args, value);
4493 vec_safe_push (args, build_int_cst (size_type_node, len));
4494 decl = lookup_literal_operator (name, args);
4496 if (decl && decl != error_mark_node)
4498 result = finish_call_expr (decl, &args, false, true,
4499 tf_warning_or_error);
4500 release_tree_vector (args);
4501 return result;
4503 release_tree_vector (args);
4505 /* Look for a template function with typename parameter CharT
4506 and parameter pack CharT... Call the function with
4507 template parameter characters representing the string. */
4508 args = make_tree_vector ();
4509 decl = lookup_literal_operator (name, args);
4510 if (decl && decl != error_mark_node)
4512 tree tmpl_args = make_string_pack (value);
4513 decl = lookup_template_function (decl, tmpl_args);
4514 result = finish_call_expr (decl, &args, false, true,
4515 tf_warning_or_error);
4516 release_tree_vector (args);
4517 return result;
4519 release_tree_vector (args);
4521 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4522 name, TREE_TYPE (value), size_type_node);
4523 return error_mark_node;
4527 /* Basic concepts [gram.basic] */
4529 /* Parse a translation-unit.
4531 translation-unit:
4532 declaration-seq [opt]
4534 Returns TRUE if all went well. */
4536 static bool
4537 cp_parser_translation_unit (cp_parser* parser)
4539 /* The address of the first non-permanent object on the declarator
4540 obstack. */
4541 static void *declarator_obstack_base;
4543 bool success;
4545 /* Create the declarator obstack, if necessary. */
4546 if (!cp_error_declarator)
4548 gcc_obstack_init (&declarator_obstack);
4549 /* Create the error declarator. */
4550 cp_error_declarator = make_declarator (cdk_error);
4551 /* Create the empty parameter list. */
4552 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4553 UNKNOWN_LOCATION);
4554 /* Remember where the base of the declarator obstack lies. */
4555 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4558 cp_parser_declaration_seq_opt (parser);
4560 /* If there are no tokens left then all went well. */
4561 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4563 /* Get rid of the token array; we don't need it any more. */
4564 cp_lexer_destroy (parser->lexer);
4565 parser->lexer = NULL;
4567 /* This file might have been a context that's implicitly extern
4568 "C". If so, pop the lang context. (Only relevant for PCH.) */
4569 if (parser->implicit_extern_c)
4571 pop_lang_context ();
4572 parser->implicit_extern_c = false;
4575 /* Finish up. */
4576 finish_translation_unit ();
4578 success = true;
4580 else
4582 cp_parser_error (parser, "expected declaration");
4583 success = false;
4586 /* Make sure the declarator obstack was fully cleaned up. */
4587 gcc_assert (obstack_next_free (&declarator_obstack)
4588 == declarator_obstack_base);
4590 /* All went well. */
4591 return success;
4594 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4595 decltype context. */
4597 static inline tsubst_flags_t
4598 complain_flags (bool decltype_p)
4600 tsubst_flags_t complain = tf_warning_or_error;
4601 if (decltype_p)
4602 complain |= tf_decltype;
4603 return complain;
4606 /* We're about to parse a collection of statements. If we're currently
4607 parsing tentatively, set up a firewall so that any nested
4608 cp_parser_commit_to_tentative_parse won't affect the current context. */
4610 static cp_token_position
4611 cp_parser_start_tentative_firewall (cp_parser *parser)
4613 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4614 return 0;
4616 cp_parser_parse_tentatively (parser);
4617 cp_parser_commit_to_topmost_tentative_parse (parser);
4618 return cp_lexer_token_position (parser->lexer, false);
4621 /* We've finished parsing the collection of statements. Wrap up the
4622 firewall and replace the relevant tokens with the parsed form. */
4624 static void
4625 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4626 tree expr)
4628 if (!start)
4629 return;
4631 /* Finish the firewall level. */
4632 cp_parser_parse_definitely (parser);
4633 /* And remember the result of the parse for when we try again. */
4634 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4635 token->type = CPP_PREPARSED_EXPR;
4636 token->u.value = expr;
4637 token->keyword = RID_MAX;
4638 cp_lexer_purge_tokens_after (parser->lexer, start);
4641 /* Like the above functions, but let the user modify the tokens. Used by
4642 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4643 later parses, so it makes sense to localize the effects of
4644 cp_parser_commit_to_tentative_parse. */
4646 struct tentative_firewall
4648 cp_parser *parser;
4649 bool set;
4651 tentative_firewall (cp_parser *p): parser(p)
4653 /* If we're currently parsing tentatively, start a committed level as a
4654 firewall and then an inner tentative parse. */
4655 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4657 cp_parser_parse_tentatively (parser);
4658 cp_parser_commit_to_topmost_tentative_parse (parser);
4659 cp_parser_parse_tentatively (parser);
4663 ~tentative_firewall()
4665 if (set)
4667 /* Finish the inner tentative parse and the firewall, propagating any
4668 uncommitted error state to the outer tentative parse. */
4669 bool err = cp_parser_error_occurred (parser);
4670 cp_parser_parse_definitely (parser);
4671 cp_parser_parse_definitely (parser);
4672 if (err)
4673 cp_parser_simulate_error (parser);
4678 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4679 This class is for tracking such a matching pair of symbols.
4680 In particular, it tracks the location of the first token,
4681 so that if the second token is missing, we can highlight the
4682 location of the first token when notifying the user about the
4683 problem. */
4685 template <typename traits_t>
4686 class token_pair
4688 public:
4689 /* token_pair's ctor. */
4690 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4692 /* If the next token is the opening symbol for this pair, consume it and
4693 return true.
4694 Otherwise, issue an error and return false.
4695 In either case, record the location of the opening token. */
4697 bool require_open (cp_parser *parser)
4699 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4700 return cp_parser_require (parser, traits_t::open_token_type,
4701 traits_t::required_token_open);
4704 /* Consume the next token from PARSER, recording its location as
4705 that of the opening token within the pair. */
4707 cp_token * consume_open (cp_parser *parser)
4709 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4710 gcc_assert (tok->type == traits_t::open_token_type);
4711 m_open_loc = tok->location;
4712 return tok;
4715 /* If the next token is the closing symbol for this pair, consume it
4716 and return it.
4717 Otherwise, issue an error, highlighting the location of the
4718 corresponding opening token, and return NULL. */
4720 cp_token *require_close (cp_parser *parser) const
4722 return cp_parser_require (parser, traits_t::close_token_type,
4723 traits_t::required_token_close,
4724 m_open_loc);
4727 private:
4728 location_t m_open_loc;
4731 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4733 struct matching_paren_traits
4735 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4736 static const enum required_token required_token_open = RT_OPEN_PAREN;
4737 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4738 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4741 /* "matching_parens" is a token_pair<T> class for tracking matching
4742 pairs of parentheses. */
4744 typedef token_pair<matching_paren_traits> matching_parens;
4746 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4748 struct matching_brace_traits
4750 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4751 static const enum required_token required_token_open = RT_OPEN_BRACE;
4752 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4753 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4756 /* "matching_braces" is a token_pair<T> class for tracking matching
4757 pairs of braces. */
4759 typedef token_pair<matching_brace_traits> matching_braces;
4762 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4763 enclosing parentheses. */
4765 static cp_expr
4766 cp_parser_statement_expr (cp_parser *parser)
4768 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4770 /* Consume the '('. */
4771 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4772 matching_parens parens;
4773 parens.consume_open (parser);
4774 /* Start the statement-expression. */
4775 tree expr = begin_stmt_expr ();
4776 /* Parse the compound-statement. */
4777 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4778 /* Finish up. */
4779 expr = finish_stmt_expr (expr, false);
4780 /* Consume the ')'. */
4781 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4782 if (!parens.require_close (parser))
4783 cp_parser_skip_to_end_of_statement (parser);
4785 cp_parser_end_tentative_firewall (parser, start, expr);
4786 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4787 return cp_expr (expr, combined_loc);
4790 /* Expressions [gram.expr] */
4792 /* Parse a fold-operator.
4794 fold-operator:
4795 - * / % ^ & | = < > << >>
4796 = -= *= /= %= ^= &= |= <<= >>=
4797 == != <= >= && || , .* ->*
4799 This returns the tree code corresponding to the matched operator
4800 as an int. When the current token matches a compound assignment
4801 opertor, the resulting tree code is the negative value of the
4802 non-assignment operator. */
4804 static int
4805 cp_parser_fold_operator (cp_token *token)
4807 switch (token->type)
4809 case CPP_PLUS: return PLUS_EXPR;
4810 case CPP_MINUS: return MINUS_EXPR;
4811 case CPP_MULT: return MULT_EXPR;
4812 case CPP_DIV: return TRUNC_DIV_EXPR;
4813 case CPP_MOD: return TRUNC_MOD_EXPR;
4814 case CPP_XOR: return BIT_XOR_EXPR;
4815 case CPP_AND: return BIT_AND_EXPR;
4816 case CPP_OR: return BIT_IOR_EXPR;
4817 case CPP_LSHIFT: return LSHIFT_EXPR;
4818 case CPP_RSHIFT: return RSHIFT_EXPR;
4820 case CPP_EQ: return -NOP_EXPR;
4821 case CPP_PLUS_EQ: return -PLUS_EXPR;
4822 case CPP_MINUS_EQ: return -MINUS_EXPR;
4823 case CPP_MULT_EQ: return -MULT_EXPR;
4824 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4825 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4826 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4827 case CPP_AND_EQ: return -BIT_AND_EXPR;
4828 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4829 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4830 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4832 case CPP_EQ_EQ: return EQ_EXPR;
4833 case CPP_NOT_EQ: return NE_EXPR;
4834 case CPP_LESS: return LT_EXPR;
4835 case CPP_GREATER: return GT_EXPR;
4836 case CPP_LESS_EQ: return LE_EXPR;
4837 case CPP_GREATER_EQ: return GE_EXPR;
4839 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4840 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4842 case CPP_COMMA: return COMPOUND_EXPR;
4844 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4845 case CPP_DEREF_STAR: return MEMBER_REF;
4847 default: return ERROR_MARK;
4851 /* Returns true if CODE indicates a binary expression, which is not allowed in
4852 the LHS of a fold-expression. More codes will need to be added to use this
4853 function in other contexts. */
4855 static bool
4856 is_binary_op (tree_code code)
4858 switch (code)
4860 case PLUS_EXPR:
4861 case POINTER_PLUS_EXPR:
4862 case MINUS_EXPR:
4863 case MULT_EXPR:
4864 case TRUNC_DIV_EXPR:
4865 case TRUNC_MOD_EXPR:
4866 case BIT_XOR_EXPR:
4867 case BIT_AND_EXPR:
4868 case BIT_IOR_EXPR:
4869 case LSHIFT_EXPR:
4870 case RSHIFT_EXPR:
4872 case MODOP_EXPR:
4874 case EQ_EXPR:
4875 case NE_EXPR:
4876 case LE_EXPR:
4877 case GE_EXPR:
4878 case LT_EXPR:
4879 case GT_EXPR:
4881 case TRUTH_ANDIF_EXPR:
4882 case TRUTH_ORIF_EXPR:
4884 case COMPOUND_EXPR:
4886 case DOTSTAR_EXPR:
4887 case MEMBER_REF:
4888 return true;
4890 default:
4891 return false;
4895 /* If the next token is a suitable fold operator, consume it and return as
4896 the function above. */
4898 static int
4899 cp_parser_fold_operator (cp_parser *parser)
4901 cp_token* token = cp_lexer_peek_token (parser->lexer);
4902 int code = cp_parser_fold_operator (token);
4903 if (code != ERROR_MARK)
4904 cp_lexer_consume_token (parser->lexer);
4905 return code;
4908 /* Parse a fold-expression.
4910 fold-expression:
4911 ( ... folding-operator cast-expression)
4912 ( cast-expression folding-operator ... )
4913 ( cast-expression folding operator ... folding-operator cast-expression)
4915 Note that the '(' and ')' are matched in primary expression. */
4917 static cp_expr
4918 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4920 cp_id_kind pidk;
4922 // Left fold.
4923 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4925 cp_lexer_consume_token (parser->lexer);
4926 int op = cp_parser_fold_operator (parser);
4927 if (op == ERROR_MARK)
4929 cp_parser_error (parser, "expected binary operator");
4930 return error_mark_node;
4933 tree expr = cp_parser_cast_expression (parser, false, false,
4934 false, &pidk);
4935 if (expr == error_mark_node)
4936 return error_mark_node;
4937 return finish_left_unary_fold_expr (expr, op);
4940 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4941 int op = cp_parser_fold_operator (parser);
4942 if (op == ERROR_MARK)
4944 cp_parser_error (parser, "expected binary operator");
4945 return error_mark_node;
4948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4950 cp_parser_error (parser, "expected ...");
4951 return error_mark_node;
4953 cp_lexer_consume_token (parser->lexer);
4955 /* The operands of a fold-expression are cast-expressions, so binary or
4956 conditional expressions are not allowed. We check this here to avoid
4957 tentative parsing. */
4958 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4959 /* OK, the expression was parenthesized. */;
4960 else if (is_binary_op (TREE_CODE (expr1)))
4961 error_at (location_of (expr1),
4962 "binary expression in operand of fold-expression");
4963 else if (TREE_CODE (expr1) == COND_EXPR)
4964 error_at (location_of (expr1),
4965 "conditional expression in operand of fold-expression");
4967 // Right fold.
4968 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4969 return finish_right_unary_fold_expr (expr1, op);
4971 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4973 cp_parser_error (parser, "mismatched operator in fold-expression");
4974 return error_mark_node;
4976 cp_lexer_consume_token (parser->lexer);
4978 // Binary left or right fold.
4979 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4980 if (expr2 == error_mark_node)
4981 return error_mark_node;
4982 return finish_binary_fold_expr (expr1, expr2, op);
4985 /* Parse a primary-expression.
4987 primary-expression:
4988 literal
4989 this
4990 ( expression )
4991 id-expression
4992 lambda-expression (C++11)
4994 GNU Extensions:
4996 primary-expression:
4997 ( compound-statement )
4998 __builtin_va_arg ( assignment-expression , type-id )
4999 __builtin_offsetof ( type-id , offsetof-expression )
5001 C++ Extensions:
5002 __has_nothrow_assign ( type-id )
5003 __has_nothrow_constructor ( type-id )
5004 __has_nothrow_copy ( type-id )
5005 __has_trivial_assign ( type-id )
5006 __has_trivial_constructor ( type-id )
5007 __has_trivial_copy ( type-id )
5008 __has_trivial_destructor ( type-id )
5009 __has_virtual_destructor ( type-id )
5010 __is_abstract ( type-id )
5011 __is_base_of ( type-id , type-id )
5012 __is_class ( type-id )
5013 __is_empty ( type-id )
5014 __is_enum ( type-id )
5015 __is_final ( type-id )
5016 __is_literal_type ( type-id )
5017 __is_pod ( type-id )
5018 __is_polymorphic ( type-id )
5019 __is_std_layout ( type-id )
5020 __is_trivial ( type-id )
5021 __is_union ( type-id )
5023 Objective-C++ Extension:
5025 primary-expression:
5026 objc-expression
5028 literal:
5029 __null
5031 ADDRESS_P is true iff this expression was immediately preceded by
5032 "&" and therefore might denote a pointer-to-member. CAST_P is true
5033 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5034 true iff this expression is a template argument.
5036 Returns a representation of the expression. Upon return, *IDK
5037 indicates what kind of id-expression (if any) was present. */
5039 static cp_expr
5040 cp_parser_primary_expression (cp_parser *parser,
5041 bool address_p,
5042 bool cast_p,
5043 bool template_arg_p,
5044 bool decltype_p,
5045 cp_id_kind *idk)
5047 cp_token *token = NULL;
5049 /* Assume the primary expression is not an id-expression. */
5050 *idk = CP_ID_KIND_NONE;
5052 /* Peek at the next token. */
5053 token = cp_lexer_peek_token (parser->lexer);
5054 switch ((int) token->type)
5056 /* literal:
5057 integer-literal
5058 character-literal
5059 floating-literal
5060 string-literal
5061 boolean-literal
5062 pointer-literal
5063 user-defined-literal */
5064 case CPP_CHAR:
5065 case CPP_CHAR16:
5066 case CPP_CHAR32:
5067 case CPP_WCHAR:
5068 case CPP_UTF8CHAR:
5069 case CPP_NUMBER:
5070 case CPP_PREPARSED_EXPR:
5071 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5072 return cp_parser_userdef_numeric_literal (parser);
5073 token = cp_lexer_consume_token (parser->lexer);
5074 if (TREE_CODE (token->u.value) == FIXED_CST)
5076 error_at (token->location,
5077 "fixed-point types not supported in C++");
5078 return error_mark_node;
5080 /* Floating-point literals are only allowed in an integral
5081 constant expression if they are cast to an integral or
5082 enumeration type. */
5083 if (TREE_CODE (token->u.value) == REAL_CST
5084 && parser->integral_constant_expression_p
5085 && pedantic)
5087 /* CAST_P will be set even in invalid code like "int(2.7 +
5088 ...)". Therefore, we have to check that the next token
5089 is sure to end the cast. */
5090 if (cast_p)
5092 cp_token *next_token;
5094 next_token = cp_lexer_peek_token (parser->lexer);
5095 if (/* The comma at the end of an
5096 enumerator-definition. */
5097 next_token->type != CPP_COMMA
5098 /* The curly brace at the end of an enum-specifier. */
5099 && next_token->type != CPP_CLOSE_BRACE
5100 /* The end of a statement. */
5101 && next_token->type != CPP_SEMICOLON
5102 /* The end of the cast-expression. */
5103 && next_token->type != CPP_CLOSE_PAREN
5104 /* The end of an array bound. */
5105 && next_token->type != CPP_CLOSE_SQUARE
5106 /* The closing ">" in a template-argument-list. */
5107 && (next_token->type != CPP_GREATER
5108 || parser->greater_than_is_operator_p)
5109 /* C++0x only: A ">>" treated like two ">" tokens,
5110 in a template-argument-list. */
5111 && (next_token->type != CPP_RSHIFT
5112 || (cxx_dialect == cxx98)
5113 || parser->greater_than_is_operator_p))
5114 cast_p = false;
5117 /* If we are within a cast, then the constraint that the
5118 cast is to an integral or enumeration type will be
5119 checked at that point. If we are not within a cast, then
5120 this code is invalid. */
5121 if (!cast_p)
5122 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5124 return cp_expr (token->u.value, token->location);
5126 case CPP_CHAR_USERDEF:
5127 case CPP_CHAR16_USERDEF:
5128 case CPP_CHAR32_USERDEF:
5129 case CPP_WCHAR_USERDEF:
5130 case CPP_UTF8CHAR_USERDEF:
5131 return cp_parser_userdef_char_literal (parser);
5133 case CPP_STRING:
5134 case CPP_STRING16:
5135 case CPP_STRING32:
5136 case CPP_WSTRING:
5137 case CPP_UTF8STRING:
5138 case CPP_STRING_USERDEF:
5139 case CPP_STRING16_USERDEF:
5140 case CPP_STRING32_USERDEF:
5141 case CPP_WSTRING_USERDEF:
5142 case CPP_UTF8STRING_USERDEF:
5143 /* ??? Should wide strings be allowed when parser->translate_strings_p
5144 is false (i.e. in attributes)? If not, we can kill the third
5145 argument to cp_parser_string_literal. */
5146 return cp_parser_string_literal (parser,
5147 parser->translate_strings_p,
5148 true);
5150 case CPP_OPEN_PAREN:
5151 /* If we see `( { ' then we are looking at the beginning of
5152 a GNU statement-expression. */
5153 if (cp_parser_allow_gnu_extensions_p (parser)
5154 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5156 /* Statement-expressions are not allowed by the standard. */
5157 pedwarn (token->location, OPT_Wpedantic,
5158 "ISO C++ forbids braced-groups within expressions");
5160 /* And they're not allowed outside of a function-body; you
5161 cannot, for example, write:
5163 int i = ({ int j = 3; j + 1; });
5165 at class or namespace scope. */
5166 if (!parser->in_function_body
5167 || parser->in_template_argument_list_p)
5169 error_at (token->location,
5170 "statement-expressions are not allowed outside "
5171 "functions nor in template-argument lists");
5172 cp_parser_skip_to_end_of_block_or_statement (parser);
5173 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5174 cp_lexer_consume_token (parser->lexer);
5175 return error_mark_node;
5177 else
5178 return cp_parser_statement_expr (parser);
5180 /* Otherwise it's a normal parenthesized expression. */
5182 cp_expr expr;
5183 bool saved_greater_than_is_operator_p;
5185 location_t open_paren_loc = token->location;
5187 /* Consume the `('. */
5188 matching_parens parens;
5189 parens.consume_open (parser);
5190 /* Within a parenthesized expression, a `>' token is always
5191 the greater-than operator. */
5192 saved_greater_than_is_operator_p
5193 = parser->greater_than_is_operator_p;
5194 parser->greater_than_is_operator_p = true;
5196 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5197 /* Left fold expression. */
5198 expr = NULL_TREE;
5199 else
5200 /* Parse the parenthesized expression. */
5201 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5203 token = cp_lexer_peek_token (parser->lexer);
5204 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5206 expr = cp_parser_fold_expression (parser, expr);
5207 if (expr != error_mark_node
5208 && cxx_dialect < cxx17
5209 && !in_system_header_at (input_location))
5210 pedwarn (input_location, 0, "fold-expressions only available "
5211 "with -std=c++17 or -std=gnu++17");
5213 else
5214 /* Let the front end know that this expression was
5215 enclosed in parentheses. This matters in case, for
5216 example, the expression is of the form `A::B', since
5217 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5218 not. */
5219 expr = finish_parenthesized_expr (expr);
5221 /* DR 705: Wrapping an unqualified name in parentheses
5222 suppresses arg-dependent lookup. We want to pass back
5223 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5224 (c++/37862), but none of the others. */
5225 if (*idk != CP_ID_KIND_QUALIFIED)
5226 *idk = CP_ID_KIND_NONE;
5228 /* The `>' token might be the end of a template-id or
5229 template-parameter-list now. */
5230 parser->greater_than_is_operator_p
5231 = saved_greater_than_is_operator_p;
5233 /* Consume the `)'. */
5234 token = cp_lexer_peek_token (parser->lexer);
5235 location_t close_paren_loc = token->location;
5236 expr.set_range (open_paren_loc, close_paren_loc);
5237 if (!parens.require_close (parser)
5238 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5239 cp_parser_skip_to_end_of_statement (parser);
5241 return expr;
5244 case CPP_OPEN_SQUARE:
5246 if (c_dialect_objc ())
5248 /* We might have an Objective-C++ message. */
5249 cp_parser_parse_tentatively (parser);
5250 tree msg = cp_parser_objc_message_expression (parser);
5251 /* If that works out, we're done ... */
5252 if (cp_parser_parse_definitely (parser))
5253 return msg;
5254 /* ... else, fall though to see if it's a lambda. */
5256 cp_expr lam = cp_parser_lambda_expression (parser);
5257 /* Don't warn about a failed tentative parse. */
5258 if (cp_parser_error_occurred (parser))
5259 return error_mark_node;
5260 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5261 return lam;
5264 case CPP_OBJC_STRING:
5265 if (c_dialect_objc ())
5266 /* We have an Objective-C++ string literal. */
5267 return cp_parser_objc_expression (parser);
5268 cp_parser_error (parser, "expected primary-expression");
5269 return error_mark_node;
5271 case CPP_KEYWORD:
5272 switch (token->keyword)
5274 /* These two are the boolean literals. */
5275 case RID_TRUE:
5276 cp_lexer_consume_token (parser->lexer);
5277 return cp_expr (boolean_true_node, token->location);
5278 case RID_FALSE:
5279 cp_lexer_consume_token (parser->lexer);
5280 return cp_expr (boolean_false_node, token->location);
5282 /* The `__null' literal. */
5283 case RID_NULL:
5284 cp_lexer_consume_token (parser->lexer);
5285 return cp_expr (null_node, token->location);
5287 /* The `nullptr' literal. */
5288 case RID_NULLPTR:
5289 cp_lexer_consume_token (parser->lexer);
5290 return cp_expr (nullptr_node, token->location);
5292 /* Recognize the `this' keyword. */
5293 case RID_THIS:
5294 cp_lexer_consume_token (parser->lexer);
5295 if (parser->local_variables_forbidden_p)
5297 error_at (token->location,
5298 "%<this%> may not be used in this context");
5299 return error_mark_node;
5301 /* Pointers cannot appear in constant-expressions. */
5302 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5303 return error_mark_node;
5304 return cp_expr (finish_this_expr (), token->location);
5306 /* The `operator' keyword can be the beginning of an
5307 id-expression. */
5308 case RID_OPERATOR:
5309 goto id_expression;
5311 case RID_FUNCTION_NAME:
5312 case RID_PRETTY_FUNCTION_NAME:
5313 case RID_C99_FUNCTION_NAME:
5315 non_integral_constant name;
5317 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5318 __func__ are the names of variables -- but they are
5319 treated specially. Therefore, they are handled here,
5320 rather than relying on the generic id-expression logic
5321 below. Grammatically, these names are id-expressions.
5323 Consume the token. */
5324 token = cp_lexer_consume_token (parser->lexer);
5326 switch (token->keyword)
5328 case RID_FUNCTION_NAME:
5329 name = NIC_FUNC_NAME;
5330 break;
5331 case RID_PRETTY_FUNCTION_NAME:
5332 name = NIC_PRETTY_FUNC;
5333 break;
5334 case RID_C99_FUNCTION_NAME:
5335 name = NIC_C99_FUNC;
5336 break;
5337 default:
5338 gcc_unreachable ();
5341 if (cp_parser_non_integral_constant_expression (parser, name))
5342 return error_mark_node;
5344 /* Look up the name. */
5345 return finish_fname (token->u.value);
5348 case RID_VA_ARG:
5350 tree expression;
5351 tree type;
5352 source_location type_location;
5353 location_t start_loc
5354 = cp_lexer_peek_token (parser->lexer)->location;
5355 /* The `__builtin_va_arg' construct is used to handle
5356 `va_arg'. Consume the `__builtin_va_arg' token. */
5357 cp_lexer_consume_token (parser->lexer);
5358 /* Look for the opening `('. */
5359 matching_parens parens;
5360 parens.require_open (parser);
5361 /* Now, parse the assignment-expression. */
5362 expression = cp_parser_assignment_expression (parser);
5363 /* Look for the `,'. */
5364 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5365 type_location = cp_lexer_peek_token (parser->lexer)->location;
5366 /* Parse the type-id. */
5368 type_id_in_expr_sentinel s (parser);
5369 type = cp_parser_type_id (parser);
5371 /* Look for the closing `)'. */
5372 location_t finish_loc
5373 = cp_lexer_peek_token (parser->lexer)->location;
5374 parens.require_close (parser);
5375 /* Using `va_arg' in a constant-expression is not
5376 allowed. */
5377 if (cp_parser_non_integral_constant_expression (parser,
5378 NIC_VA_ARG))
5379 return error_mark_node;
5380 /* Construct a location of the form:
5381 __builtin_va_arg (v, int)
5382 ~~~~~~~~~~~~~~~~~~~~~^~~~
5383 with the caret at the type, ranging from the start of the
5384 "__builtin_va_arg" token to the close paren. */
5385 location_t combined_loc
5386 = make_location (type_location, start_loc, finish_loc);
5387 return build_x_va_arg (combined_loc, expression, type);
5390 case RID_OFFSETOF:
5391 return cp_parser_builtin_offsetof (parser);
5393 case RID_HAS_NOTHROW_ASSIGN:
5394 case RID_HAS_NOTHROW_CONSTRUCTOR:
5395 case RID_HAS_NOTHROW_COPY:
5396 case RID_HAS_TRIVIAL_ASSIGN:
5397 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5398 case RID_HAS_TRIVIAL_COPY:
5399 case RID_HAS_TRIVIAL_DESTRUCTOR:
5400 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5401 case RID_HAS_VIRTUAL_DESTRUCTOR:
5402 case RID_IS_ABSTRACT:
5403 case RID_IS_AGGREGATE:
5404 case RID_IS_BASE_OF:
5405 case RID_IS_CLASS:
5406 case RID_IS_EMPTY:
5407 case RID_IS_ENUM:
5408 case RID_IS_FINAL:
5409 case RID_IS_LITERAL_TYPE:
5410 case RID_IS_POD:
5411 case RID_IS_POLYMORPHIC:
5412 case RID_IS_SAME_AS:
5413 case RID_IS_STD_LAYOUT:
5414 case RID_IS_TRIVIAL:
5415 case RID_IS_TRIVIALLY_ASSIGNABLE:
5416 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5417 case RID_IS_TRIVIALLY_COPYABLE:
5418 case RID_IS_UNION:
5419 case RID_IS_ASSIGNABLE:
5420 case RID_IS_CONSTRUCTIBLE:
5421 return cp_parser_trait_expr (parser, token->keyword);
5423 // C++ concepts
5424 case RID_REQUIRES:
5425 return cp_parser_requires_expression (parser);
5427 /* Objective-C++ expressions. */
5428 case RID_AT_ENCODE:
5429 case RID_AT_PROTOCOL:
5430 case RID_AT_SELECTOR:
5431 return cp_parser_objc_expression (parser);
5433 case RID_TEMPLATE:
5434 if (parser->in_function_body
5435 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5436 == CPP_LESS))
5438 error_at (token->location,
5439 "a template declaration cannot appear at block scope");
5440 cp_parser_skip_to_end_of_block_or_statement (parser);
5441 return error_mark_node;
5443 /* FALLTHRU */
5444 default:
5445 cp_parser_error (parser, "expected primary-expression");
5446 return error_mark_node;
5449 /* An id-expression can start with either an identifier, a
5450 `::' as the beginning of a qualified-id, or the "operator"
5451 keyword. */
5452 case CPP_NAME:
5453 case CPP_SCOPE:
5454 case CPP_TEMPLATE_ID:
5455 case CPP_NESTED_NAME_SPECIFIER:
5457 id_expression:
5458 cp_expr id_expression;
5459 cp_expr decl;
5460 const char *error_msg;
5461 bool template_p;
5462 bool done;
5463 cp_token *id_expr_token;
5465 /* Parse the id-expression. */
5466 id_expression
5467 = cp_parser_id_expression (parser,
5468 /*template_keyword_p=*/false,
5469 /*check_dependency_p=*/true,
5470 &template_p,
5471 /*declarator_p=*/false,
5472 /*optional_p=*/false);
5473 if (id_expression == error_mark_node)
5474 return error_mark_node;
5475 id_expr_token = token;
5476 token = cp_lexer_peek_token (parser->lexer);
5477 done = (token->type != CPP_OPEN_SQUARE
5478 && token->type != CPP_OPEN_PAREN
5479 && token->type != CPP_DOT
5480 && token->type != CPP_DEREF
5481 && token->type != CPP_PLUS_PLUS
5482 && token->type != CPP_MINUS_MINUS);
5483 /* If we have a template-id, then no further lookup is
5484 required. If the template-id was for a template-class, we
5485 will sometimes have a TYPE_DECL at this point. */
5486 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5487 || TREE_CODE (id_expression) == TYPE_DECL)
5488 decl = id_expression;
5489 /* Look up the name. */
5490 else
5492 tree ambiguous_decls;
5494 /* If we already know that this lookup is ambiguous, then
5495 we've already issued an error message; there's no reason
5496 to check again. */
5497 if (id_expr_token->type == CPP_NAME
5498 && id_expr_token->error_reported)
5500 cp_parser_simulate_error (parser);
5501 return error_mark_node;
5504 decl = cp_parser_lookup_name (parser, id_expression,
5505 none_type,
5506 template_p,
5507 /*is_namespace=*/false,
5508 /*check_dependency=*/true,
5509 &ambiguous_decls,
5510 id_expr_token->location);
5511 /* If the lookup was ambiguous, an error will already have
5512 been issued. */
5513 if (ambiguous_decls)
5514 return error_mark_node;
5516 /* In Objective-C++, we may have an Objective-C 2.0
5517 dot-syntax for classes here. */
5518 if (c_dialect_objc ()
5519 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5520 && TREE_CODE (decl) == TYPE_DECL
5521 && objc_is_class_name (decl))
5523 tree component;
5524 cp_lexer_consume_token (parser->lexer);
5525 component = cp_parser_identifier (parser);
5526 if (component == error_mark_node)
5527 return error_mark_node;
5529 tree result = objc_build_class_component_ref (id_expression,
5530 component);
5531 /* Build a location of the form:
5532 expr.component
5533 ~~~~~^~~~~~~~~
5534 with caret at the start of the component name (at
5535 input_location), ranging from the start of the id_expression
5536 to the end of the component name. */
5537 location_t combined_loc
5538 = make_location (input_location, id_expression.get_start (),
5539 get_finish (input_location));
5540 protected_set_expr_location (result, combined_loc);
5541 return result;
5544 /* In Objective-C++, an instance variable (ivar) may be preferred
5545 to whatever cp_parser_lookup_name() found.
5546 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5547 rest of c-family, we have to do a little extra work to preserve
5548 any location information in cp_expr "decl". Given that
5549 objc_lookup_ivar is implemented in "c-family" and "objc", we
5550 have a trip through the pure "tree" type, rather than cp_expr.
5551 Naively copying it back to "decl" would implicitly give the
5552 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5553 store an EXPR_LOCATION. Hence we only update "decl" (and
5554 hence its location_t) if we get back a different tree node. */
5555 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5556 id_expression);
5557 if (decl_tree != decl.get_value ())
5558 decl = cp_expr (decl_tree);
5560 /* If name lookup gives us a SCOPE_REF, then the
5561 qualifying scope was dependent. */
5562 if (TREE_CODE (decl) == SCOPE_REF)
5564 /* At this point, we do not know if DECL is a valid
5565 integral constant expression. We assume that it is
5566 in fact such an expression, so that code like:
5568 template <int N> struct A {
5569 int a[B<N>::i];
5572 is accepted. At template-instantiation time, we
5573 will check that B<N>::i is actually a constant. */
5574 return decl;
5576 /* Check to see if DECL is a local variable in a context
5577 where that is forbidden. */
5578 if (parser->local_variables_forbidden_p
5579 && local_variable_p (decl))
5581 /* It might be that we only found DECL because we are
5582 trying to be generous with pre-ISO scoping rules.
5583 For example, consider:
5585 int i;
5586 void g() {
5587 for (int i = 0; i < 10; ++i) {}
5588 extern void f(int j = i);
5591 Here, name look up will originally find the out
5592 of scope `i'. We need to issue a warning message,
5593 but then use the global `i'. */
5594 decl = check_for_out_of_scope_variable (decl);
5595 if (local_variable_p (decl))
5597 error_at (id_expr_token->location,
5598 "local variable %qD may not appear in this context",
5599 decl.get_value ());
5600 return error_mark_node;
5605 decl = (finish_id_expression
5606 (id_expression, decl, parser->scope,
5607 idk,
5608 parser->integral_constant_expression_p,
5609 parser->allow_non_integral_constant_expression_p,
5610 &parser->non_integral_constant_expression_p,
5611 template_p, done, address_p,
5612 template_arg_p,
5613 &error_msg,
5614 id_expression.get_location ()));
5615 if (error_msg)
5616 cp_parser_error (parser, error_msg);
5617 decl.set_location (id_expr_token->location);
5618 return decl;
5621 /* Anything else is an error. */
5622 default:
5623 cp_parser_error (parser, "expected primary-expression");
5624 return error_mark_node;
5628 static inline cp_expr
5629 cp_parser_primary_expression (cp_parser *parser,
5630 bool address_p,
5631 bool cast_p,
5632 bool template_arg_p,
5633 cp_id_kind *idk)
5635 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5636 /*decltype*/false, idk);
5639 /* Parse an id-expression.
5641 id-expression:
5642 unqualified-id
5643 qualified-id
5645 qualified-id:
5646 :: [opt] nested-name-specifier template [opt] unqualified-id
5647 :: identifier
5648 :: operator-function-id
5649 :: template-id
5651 Return a representation of the unqualified portion of the
5652 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5653 a `::' or nested-name-specifier.
5655 Often, if the id-expression was a qualified-id, the caller will
5656 want to make a SCOPE_REF to represent the qualified-id. This
5657 function does not do this in order to avoid wastefully creating
5658 SCOPE_REFs when they are not required.
5660 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5661 `template' keyword.
5663 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5664 uninstantiated templates.
5666 If *TEMPLATE_P is non-NULL, it is set to true iff the
5667 `template' keyword is used to explicitly indicate that the entity
5668 named is a template.
5670 If DECLARATOR_P is true, the id-expression is appearing as part of
5671 a declarator, rather than as part of an expression. */
5673 static cp_expr
5674 cp_parser_id_expression (cp_parser *parser,
5675 bool template_keyword_p,
5676 bool check_dependency_p,
5677 bool *template_p,
5678 bool declarator_p,
5679 bool optional_p)
5681 bool global_scope_p;
5682 bool nested_name_specifier_p;
5684 /* Assume the `template' keyword was not used. */
5685 if (template_p)
5686 *template_p = template_keyword_p;
5688 /* Look for the optional `::' operator. */
5689 global_scope_p
5690 = (!template_keyword_p
5691 && (cp_parser_global_scope_opt (parser,
5692 /*current_scope_valid_p=*/false)
5693 != NULL_TREE));
5695 /* Look for the optional nested-name-specifier. */
5696 nested_name_specifier_p
5697 = (cp_parser_nested_name_specifier_opt (parser,
5698 /*typename_keyword_p=*/false,
5699 check_dependency_p,
5700 /*type_p=*/false,
5701 declarator_p,
5702 template_keyword_p)
5703 != NULL_TREE);
5705 /* If there is a nested-name-specifier, then we are looking at
5706 the first qualified-id production. */
5707 if (nested_name_specifier_p)
5709 tree saved_scope;
5710 tree saved_object_scope;
5711 tree saved_qualifying_scope;
5712 cp_expr unqualified_id;
5713 bool is_template;
5715 /* See if the next token is the `template' keyword. */
5716 if (!template_p)
5717 template_p = &is_template;
5718 *template_p = cp_parser_optional_template_keyword (parser);
5719 /* Name lookup we do during the processing of the
5720 unqualified-id might obliterate SCOPE. */
5721 saved_scope = parser->scope;
5722 saved_object_scope = parser->object_scope;
5723 saved_qualifying_scope = parser->qualifying_scope;
5724 /* Process the final unqualified-id. */
5725 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5726 check_dependency_p,
5727 declarator_p,
5728 /*optional_p=*/false);
5729 /* Restore the SAVED_SCOPE for our caller. */
5730 parser->scope = saved_scope;
5731 parser->object_scope = saved_object_scope;
5732 parser->qualifying_scope = saved_qualifying_scope;
5734 return unqualified_id;
5736 /* Otherwise, if we are in global scope, then we are looking at one
5737 of the other qualified-id productions. */
5738 else if (global_scope_p)
5740 cp_token *token;
5741 tree id;
5743 /* Peek at the next token. */
5744 token = cp_lexer_peek_token (parser->lexer);
5746 /* If it's an identifier, and the next token is not a "<", then
5747 we can avoid the template-id case. This is an optimization
5748 for this common case. */
5749 if (token->type == CPP_NAME
5750 && !cp_parser_nth_token_starts_template_argument_list_p
5751 (parser, 2))
5752 return cp_parser_identifier (parser);
5754 cp_parser_parse_tentatively (parser);
5755 /* Try a template-id. */
5756 id = cp_parser_template_id (parser,
5757 /*template_keyword_p=*/false,
5758 /*check_dependency_p=*/true,
5759 none_type,
5760 declarator_p);
5761 /* If that worked, we're done. */
5762 if (cp_parser_parse_definitely (parser))
5763 return id;
5765 /* Peek at the next token. (Changes in the token buffer may
5766 have invalidated the pointer obtained above.) */
5767 token = cp_lexer_peek_token (parser->lexer);
5769 switch (token->type)
5771 case CPP_NAME:
5772 return cp_parser_identifier (parser);
5774 case CPP_KEYWORD:
5775 if (token->keyword == RID_OPERATOR)
5776 return cp_parser_operator_function_id (parser);
5777 /* Fall through. */
5779 default:
5780 cp_parser_error (parser, "expected id-expression");
5781 return error_mark_node;
5784 else
5785 return cp_parser_unqualified_id (parser, template_keyword_p,
5786 /*check_dependency_p=*/true,
5787 declarator_p,
5788 optional_p);
5791 /* Parse an unqualified-id.
5793 unqualified-id:
5794 identifier
5795 operator-function-id
5796 conversion-function-id
5797 ~ class-name
5798 template-id
5800 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5801 keyword, in a construct like `A::template ...'.
5803 Returns a representation of unqualified-id. For the `identifier'
5804 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5805 production a BIT_NOT_EXPR is returned; the operand of the
5806 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5807 other productions, see the documentation accompanying the
5808 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5809 names are looked up in uninstantiated templates. If DECLARATOR_P
5810 is true, the unqualified-id is appearing as part of a declarator,
5811 rather than as part of an expression. */
5813 static cp_expr
5814 cp_parser_unqualified_id (cp_parser* parser,
5815 bool template_keyword_p,
5816 bool check_dependency_p,
5817 bool declarator_p,
5818 bool optional_p)
5820 cp_token *token;
5822 /* Peek at the next token. */
5823 token = cp_lexer_peek_token (parser->lexer);
5825 switch ((int) token->type)
5827 case CPP_NAME:
5829 tree id;
5831 /* We don't know yet whether or not this will be a
5832 template-id. */
5833 cp_parser_parse_tentatively (parser);
5834 /* Try a template-id. */
5835 id = cp_parser_template_id (parser, template_keyword_p,
5836 check_dependency_p,
5837 none_type,
5838 declarator_p);
5839 /* If it worked, we're done. */
5840 if (cp_parser_parse_definitely (parser))
5841 return id;
5842 /* Otherwise, it's an ordinary identifier. */
5843 return cp_parser_identifier (parser);
5846 case CPP_TEMPLATE_ID:
5847 return cp_parser_template_id (parser, template_keyword_p,
5848 check_dependency_p,
5849 none_type,
5850 declarator_p);
5852 case CPP_COMPL:
5854 tree type_decl;
5855 tree qualifying_scope;
5856 tree object_scope;
5857 tree scope;
5858 bool done;
5860 /* Consume the `~' token. */
5861 cp_lexer_consume_token (parser->lexer);
5862 /* Parse the class-name. The standard, as written, seems to
5863 say that:
5865 template <typename T> struct S { ~S (); };
5866 template <typename T> S<T>::~S() {}
5868 is invalid, since `~' must be followed by a class-name, but
5869 `S<T>' is dependent, and so not known to be a class.
5870 That's not right; we need to look in uninstantiated
5871 templates. A further complication arises from:
5873 template <typename T> void f(T t) {
5874 t.T::~T();
5877 Here, it is not possible to look up `T' in the scope of `T'
5878 itself. We must look in both the current scope, and the
5879 scope of the containing complete expression.
5881 Yet another issue is:
5883 struct S {
5884 int S;
5885 ~S();
5888 S::~S() {}
5890 The standard does not seem to say that the `S' in `~S'
5891 should refer to the type `S' and not the data member
5892 `S::S'. */
5894 /* DR 244 says that we look up the name after the "~" in the
5895 same scope as we looked up the qualifying name. That idea
5896 isn't fully worked out; it's more complicated than that. */
5897 scope = parser->scope;
5898 object_scope = parser->object_scope;
5899 qualifying_scope = parser->qualifying_scope;
5901 /* Check for invalid scopes. */
5902 if (scope == error_mark_node)
5904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5905 cp_lexer_consume_token (parser->lexer);
5906 return error_mark_node;
5908 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5910 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5911 error_at (token->location,
5912 "scope %qT before %<~%> is not a class-name",
5913 scope);
5914 cp_parser_simulate_error (parser);
5915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5916 cp_lexer_consume_token (parser->lexer);
5917 return error_mark_node;
5919 gcc_assert (!scope || TYPE_P (scope));
5921 /* If the name is of the form "X::~X" it's OK even if X is a
5922 typedef. */
5923 token = cp_lexer_peek_token (parser->lexer);
5924 if (scope
5925 && token->type == CPP_NAME
5926 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5927 != CPP_LESS)
5928 && (token->u.value == TYPE_IDENTIFIER (scope)
5929 || (CLASS_TYPE_P (scope)
5930 && constructor_name_p (token->u.value, scope))))
5932 cp_lexer_consume_token (parser->lexer);
5933 return build_nt (BIT_NOT_EXPR, scope);
5936 /* ~auto means the destructor of whatever the object is. */
5937 if (cp_parser_is_keyword (token, RID_AUTO))
5939 if (cxx_dialect < cxx14)
5940 pedwarn (input_location, 0,
5941 "%<~auto%> only available with "
5942 "-std=c++14 or -std=gnu++14");
5943 cp_lexer_consume_token (parser->lexer);
5944 return build_nt (BIT_NOT_EXPR, make_auto ());
5947 /* If there was an explicit qualification (S::~T), first look
5948 in the scope given by the qualification (i.e., S).
5950 Note: in the calls to cp_parser_class_name below we pass
5951 typename_type so that lookup finds the injected-class-name
5952 rather than the constructor. */
5953 done = false;
5954 type_decl = NULL_TREE;
5955 if (scope)
5957 cp_parser_parse_tentatively (parser);
5958 type_decl = cp_parser_class_name (parser,
5959 /*typename_keyword_p=*/false,
5960 /*template_keyword_p=*/false,
5961 typename_type,
5962 /*check_dependency=*/false,
5963 /*class_head_p=*/false,
5964 declarator_p);
5965 if (cp_parser_parse_definitely (parser))
5966 done = true;
5968 /* In "N::S::~S", look in "N" as well. */
5969 if (!done && scope && qualifying_scope)
5971 cp_parser_parse_tentatively (parser);
5972 parser->scope = qualifying_scope;
5973 parser->object_scope = NULL_TREE;
5974 parser->qualifying_scope = NULL_TREE;
5975 type_decl
5976 = cp_parser_class_name (parser,
5977 /*typename_keyword_p=*/false,
5978 /*template_keyword_p=*/false,
5979 typename_type,
5980 /*check_dependency=*/false,
5981 /*class_head_p=*/false,
5982 declarator_p);
5983 if (cp_parser_parse_definitely (parser))
5984 done = true;
5986 /* In "p->S::~T", look in the scope given by "*p" as well. */
5987 else if (!done && object_scope)
5989 cp_parser_parse_tentatively (parser);
5990 parser->scope = object_scope;
5991 parser->object_scope = NULL_TREE;
5992 parser->qualifying_scope = NULL_TREE;
5993 type_decl
5994 = cp_parser_class_name (parser,
5995 /*typename_keyword_p=*/false,
5996 /*template_keyword_p=*/false,
5997 typename_type,
5998 /*check_dependency=*/false,
5999 /*class_head_p=*/false,
6000 declarator_p);
6001 if (cp_parser_parse_definitely (parser))
6002 done = true;
6004 /* Look in the surrounding context. */
6005 if (!done)
6007 parser->scope = NULL_TREE;
6008 parser->object_scope = NULL_TREE;
6009 parser->qualifying_scope = NULL_TREE;
6010 if (processing_template_decl)
6011 cp_parser_parse_tentatively (parser);
6012 type_decl
6013 = cp_parser_class_name (parser,
6014 /*typename_keyword_p=*/false,
6015 /*template_keyword_p=*/false,
6016 typename_type,
6017 /*check_dependency=*/false,
6018 /*class_head_p=*/false,
6019 declarator_p);
6020 if (processing_template_decl
6021 && ! cp_parser_parse_definitely (parser))
6023 /* We couldn't find a type with this name. If we're parsing
6024 tentatively, fail and try something else. */
6025 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6027 cp_parser_simulate_error (parser);
6028 return error_mark_node;
6030 /* Otherwise, accept it and check for a match at instantiation
6031 time. */
6032 type_decl = cp_parser_identifier (parser);
6033 if (type_decl != error_mark_node)
6034 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6035 return type_decl;
6038 /* If an error occurred, assume that the name of the
6039 destructor is the same as the name of the qualifying
6040 class. That allows us to keep parsing after running
6041 into ill-formed destructor names. */
6042 if (type_decl == error_mark_node && scope)
6043 return build_nt (BIT_NOT_EXPR, scope);
6044 else if (type_decl == error_mark_node)
6045 return error_mark_node;
6047 /* Check that destructor name and scope match. */
6048 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6051 error_at (token->location,
6052 "declaration of %<~%T%> as member of %qT",
6053 type_decl, scope);
6054 cp_parser_simulate_error (parser);
6055 return error_mark_node;
6058 /* [class.dtor]
6060 A typedef-name that names a class shall not be used as the
6061 identifier in the declarator for a destructor declaration. */
6062 if (declarator_p
6063 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6064 && !DECL_SELF_REFERENCE_P (type_decl)
6065 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6066 error_at (token->location,
6067 "typedef-name %qD used as destructor declarator",
6068 type_decl);
6070 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6073 case CPP_KEYWORD:
6074 if (token->keyword == RID_OPERATOR)
6076 cp_expr id;
6078 /* This could be a template-id, so we try that first. */
6079 cp_parser_parse_tentatively (parser);
6080 /* Try a template-id. */
6081 id = cp_parser_template_id (parser, template_keyword_p,
6082 /*check_dependency_p=*/true,
6083 none_type,
6084 declarator_p);
6085 /* If that worked, we're done. */
6086 if (cp_parser_parse_definitely (parser))
6087 return id;
6088 /* We still don't know whether we're looking at an
6089 operator-function-id or a conversion-function-id. */
6090 cp_parser_parse_tentatively (parser);
6091 /* Try an operator-function-id. */
6092 id = cp_parser_operator_function_id (parser);
6093 /* If that didn't work, try a conversion-function-id. */
6094 if (!cp_parser_parse_definitely (parser))
6095 id = cp_parser_conversion_function_id (parser);
6096 else if (UDLIT_OPER_P (id))
6098 /* 17.6.3.3.5 */
6099 const char *name = UDLIT_OP_SUFFIX (id);
6100 if (name[0] != '_' && !in_system_header_at (input_location)
6101 && declarator_p)
6102 warning (OPT_Wliteral_suffix,
6103 "literal operator suffixes not preceded by %<_%>"
6104 " are reserved for future standardization");
6107 return id;
6109 /* Fall through. */
6111 default:
6112 if (optional_p)
6113 return NULL_TREE;
6114 cp_parser_error (parser, "expected unqualified-id");
6115 return error_mark_node;
6119 /* Parse an (optional) nested-name-specifier.
6121 nested-name-specifier: [C++98]
6122 class-or-namespace-name :: nested-name-specifier [opt]
6123 class-or-namespace-name :: template nested-name-specifier [opt]
6125 nested-name-specifier: [C++0x]
6126 type-name ::
6127 namespace-name ::
6128 nested-name-specifier identifier ::
6129 nested-name-specifier template [opt] simple-template-id ::
6131 PARSER->SCOPE should be set appropriately before this function is
6132 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6133 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6134 in name lookups.
6136 Sets PARSER->SCOPE to the class (TYPE) or namespace
6137 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6138 it unchanged if there is no nested-name-specifier. Returns the new
6139 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6141 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6142 part of a declaration and/or decl-specifier. */
6144 static tree
6145 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6146 bool typename_keyword_p,
6147 bool check_dependency_p,
6148 bool type_p,
6149 bool is_declaration,
6150 bool template_keyword_p /* = false */)
6152 bool success = false;
6153 cp_token_position start = 0;
6154 cp_token *token;
6156 /* Remember where the nested-name-specifier starts. */
6157 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6159 start = cp_lexer_token_position (parser->lexer, false);
6160 push_deferring_access_checks (dk_deferred);
6163 while (true)
6165 tree new_scope;
6166 tree old_scope;
6167 tree saved_qualifying_scope;
6169 /* Spot cases that cannot be the beginning of a
6170 nested-name-specifier. */
6171 token = cp_lexer_peek_token (parser->lexer);
6173 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6174 the already parsed nested-name-specifier. */
6175 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6177 /* Grab the nested-name-specifier and continue the loop. */
6178 cp_parser_pre_parsed_nested_name_specifier (parser);
6179 /* If we originally encountered this nested-name-specifier
6180 with IS_DECLARATION set to false, we will not have
6181 resolved TYPENAME_TYPEs, so we must do so here. */
6182 if (is_declaration
6183 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6185 new_scope = resolve_typename_type (parser->scope,
6186 /*only_current_p=*/false);
6187 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6188 parser->scope = new_scope;
6190 success = true;
6191 continue;
6194 /* Spot cases that cannot be the beginning of a
6195 nested-name-specifier. On the second and subsequent times
6196 through the loop, we look for the `template' keyword. */
6197 if (success && token->keyword == RID_TEMPLATE)
6199 /* A template-id can start a nested-name-specifier. */
6200 else if (token->type == CPP_TEMPLATE_ID)
6202 /* DR 743: decltype can be used in a nested-name-specifier. */
6203 else if (token_is_decltype (token))
6205 else
6207 /* If the next token is not an identifier, then it is
6208 definitely not a type-name or namespace-name. */
6209 if (token->type != CPP_NAME)
6210 break;
6211 /* If the following token is neither a `<' (to begin a
6212 template-id), nor a `::', then we are not looking at a
6213 nested-name-specifier. */
6214 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6216 if (token->type == CPP_COLON
6217 && parser->colon_corrects_to_scope_p
6218 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6220 gcc_rich_location richloc (token->location);
6221 richloc.add_fixit_replace ("::");
6222 error_at (&richloc,
6223 "found %<:%> in nested-name-specifier, "
6224 "expected %<::%>");
6225 token->type = CPP_SCOPE;
6228 if (token->type != CPP_SCOPE
6229 && !cp_parser_nth_token_starts_template_argument_list_p
6230 (parser, 2))
6231 break;
6234 /* The nested-name-specifier is optional, so we parse
6235 tentatively. */
6236 cp_parser_parse_tentatively (parser);
6238 /* Look for the optional `template' keyword, if this isn't the
6239 first time through the loop. */
6240 if (success)
6241 template_keyword_p = cp_parser_optional_template_keyword (parser);
6243 /* Save the old scope since the name lookup we are about to do
6244 might destroy it. */
6245 old_scope = parser->scope;
6246 saved_qualifying_scope = parser->qualifying_scope;
6247 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6248 look up names in "X<T>::I" in order to determine that "Y" is
6249 a template. So, if we have a typename at this point, we make
6250 an effort to look through it. */
6251 if (is_declaration
6252 && !typename_keyword_p
6253 && parser->scope
6254 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6255 parser->scope = resolve_typename_type (parser->scope,
6256 /*only_current_p=*/false);
6257 /* Parse the qualifying entity. */
6258 new_scope
6259 = cp_parser_qualifying_entity (parser,
6260 typename_keyword_p,
6261 template_keyword_p,
6262 check_dependency_p,
6263 type_p,
6264 is_declaration);
6265 /* Look for the `::' token. */
6266 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6268 /* If we found what we wanted, we keep going; otherwise, we're
6269 done. */
6270 if (!cp_parser_parse_definitely (parser))
6272 bool error_p = false;
6274 /* Restore the OLD_SCOPE since it was valid before the
6275 failed attempt at finding the last
6276 class-or-namespace-name. */
6277 parser->scope = old_scope;
6278 parser->qualifying_scope = saved_qualifying_scope;
6280 /* If the next token is a decltype, and the one after that is a
6281 `::', then the decltype has failed to resolve to a class or
6282 enumeration type. Give this error even when parsing
6283 tentatively since it can't possibly be valid--and we're going
6284 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6285 won't get another chance.*/
6286 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6287 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6288 == CPP_SCOPE))
6290 token = cp_lexer_consume_token (parser->lexer);
6291 error_at (token->location, "decltype evaluates to %qT, "
6292 "which is not a class or enumeration type",
6293 token->u.tree_check_value->value);
6294 parser->scope = error_mark_node;
6295 error_p = true;
6296 /* As below. */
6297 success = true;
6298 cp_lexer_consume_token (parser->lexer);
6301 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6302 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6304 /* If we have a non-type template-id followed by ::, it can't
6305 possibly be valid. */
6306 token = cp_lexer_peek_token (parser->lexer);
6307 tree tid = token->u.tree_check_value->value;
6308 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6309 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6311 tree tmpl = NULL_TREE;
6312 if (is_overloaded_fn (tid))
6314 tree fns = get_fns (tid);
6315 if (OVL_SINGLE_P (fns))
6316 tmpl = OVL_FIRST (fns);
6317 error_at (token->location, "function template-id %qD "
6318 "in nested-name-specifier", tid);
6320 else
6322 /* Variable template. */
6323 tmpl = TREE_OPERAND (tid, 0);
6324 gcc_assert (variable_template_p (tmpl));
6325 error_at (token->location, "variable template-id %qD "
6326 "in nested-name-specifier", tid);
6328 if (tmpl)
6329 inform (DECL_SOURCE_LOCATION (tmpl),
6330 "%qD declared here", tmpl);
6332 parser->scope = error_mark_node;
6333 error_p = true;
6334 /* As below. */
6335 success = true;
6336 cp_lexer_consume_token (parser->lexer);
6337 cp_lexer_consume_token (parser->lexer);
6341 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6342 break;
6343 /* If the next token is an identifier, and the one after
6344 that is a `::', then any valid interpretation would have
6345 found a class-or-namespace-name. */
6346 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6347 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6348 == CPP_SCOPE)
6349 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6350 != CPP_COMPL))
6352 token = cp_lexer_consume_token (parser->lexer);
6353 if (!error_p)
6355 if (!token->error_reported)
6357 tree decl;
6358 tree ambiguous_decls;
6360 decl = cp_parser_lookup_name (parser, token->u.value,
6361 none_type,
6362 /*is_template=*/false,
6363 /*is_namespace=*/false,
6364 /*check_dependency=*/true,
6365 &ambiguous_decls,
6366 token->location);
6367 if (TREE_CODE (decl) == TEMPLATE_DECL)
6368 error_at (token->location,
6369 "%qD used without template parameters",
6370 decl);
6371 else if (ambiguous_decls)
6373 // cp_parser_lookup_name has the same diagnostic,
6374 // thus make sure to emit it at most once.
6375 if (cp_parser_uncommitted_to_tentative_parse_p
6376 (parser))
6378 error_at (token->location,
6379 "reference to %qD is ambiguous",
6380 token->u.value);
6381 print_candidates (ambiguous_decls);
6383 decl = error_mark_node;
6385 else
6387 if (cxx_dialect != cxx98)
6388 cp_parser_name_lookup_error
6389 (parser, token->u.value, decl, NLE_NOT_CXX98,
6390 token->location);
6391 else
6392 cp_parser_name_lookup_error
6393 (parser, token->u.value, decl, NLE_CXX98,
6394 token->location);
6397 parser->scope = error_mark_node;
6398 error_p = true;
6399 /* Treat this as a successful nested-name-specifier
6400 due to:
6402 [basic.lookup.qual]
6404 If the name found is not a class-name (clause
6405 _class_) or namespace-name (_namespace.def_), the
6406 program is ill-formed. */
6407 success = true;
6409 cp_lexer_consume_token (parser->lexer);
6411 break;
6413 /* We've found one valid nested-name-specifier. */
6414 success = true;
6415 /* Name lookup always gives us a DECL. */
6416 if (TREE_CODE (new_scope) == TYPE_DECL)
6417 new_scope = TREE_TYPE (new_scope);
6418 /* Uses of "template" must be followed by actual templates. */
6419 if (template_keyword_p
6420 && !(CLASS_TYPE_P (new_scope)
6421 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6422 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6423 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6424 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6425 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6426 == TEMPLATE_ID_EXPR)))
6427 permerror (input_location, TYPE_P (new_scope)
6428 ? G_("%qT is not a template")
6429 : G_("%qD is not a template"),
6430 new_scope);
6431 /* If it is a class scope, try to complete it; we are about to
6432 be looking up names inside the class. */
6433 if (TYPE_P (new_scope)
6434 /* Since checking types for dependency can be expensive,
6435 avoid doing it if the type is already complete. */
6436 && !COMPLETE_TYPE_P (new_scope)
6437 /* Do not try to complete dependent types. */
6438 && !dependent_type_p (new_scope))
6440 new_scope = complete_type (new_scope);
6441 /* If it is a typedef to current class, use the current
6442 class instead, as the typedef won't have any names inside
6443 it yet. */
6444 if (!COMPLETE_TYPE_P (new_scope)
6445 && currently_open_class (new_scope))
6446 new_scope = TYPE_MAIN_VARIANT (new_scope);
6448 /* Make sure we look in the right scope the next time through
6449 the loop. */
6450 parser->scope = new_scope;
6453 /* If parsing tentatively, replace the sequence of tokens that makes
6454 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6455 token. That way, should we re-parse the token stream, we will
6456 not have to repeat the effort required to do the parse, nor will
6457 we issue duplicate error messages. */
6458 if (success && start)
6460 cp_token *token;
6462 token = cp_lexer_token_at (parser->lexer, start);
6463 /* Reset the contents of the START token. */
6464 token->type = CPP_NESTED_NAME_SPECIFIER;
6465 /* Retrieve any deferred checks. Do not pop this access checks yet
6466 so the memory will not be reclaimed during token replacing below. */
6467 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6468 token->u.tree_check_value->value = parser->scope;
6469 token->u.tree_check_value->checks = get_deferred_access_checks ();
6470 token->u.tree_check_value->qualifying_scope =
6471 parser->qualifying_scope;
6472 token->keyword = RID_MAX;
6474 /* Purge all subsequent tokens. */
6475 cp_lexer_purge_tokens_after (parser->lexer, start);
6478 if (start)
6479 pop_to_parent_deferring_access_checks ();
6481 return success ? parser->scope : NULL_TREE;
6484 /* Parse a nested-name-specifier. See
6485 cp_parser_nested_name_specifier_opt for details. This function
6486 behaves identically, except that it will an issue an error if no
6487 nested-name-specifier is present. */
6489 static tree
6490 cp_parser_nested_name_specifier (cp_parser *parser,
6491 bool typename_keyword_p,
6492 bool check_dependency_p,
6493 bool type_p,
6494 bool is_declaration)
6496 tree scope;
6498 /* Look for the nested-name-specifier. */
6499 scope = cp_parser_nested_name_specifier_opt (parser,
6500 typename_keyword_p,
6501 check_dependency_p,
6502 type_p,
6503 is_declaration);
6504 /* If it was not present, issue an error message. */
6505 if (!scope)
6507 cp_parser_error (parser, "expected nested-name-specifier");
6508 parser->scope = NULL_TREE;
6511 return scope;
6514 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6515 this is either a class-name or a namespace-name (which corresponds
6516 to the class-or-namespace-name production in the grammar). For
6517 C++0x, it can also be a type-name that refers to an enumeration
6518 type or a simple-template-id.
6520 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6521 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6522 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6523 TYPE_P is TRUE iff the next name should be taken as a class-name,
6524 even the same name is declared to be another entity in the same
6525 scope.
6527 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6528 specified by the class-or-namespace-name. If neither is found the
6529 ERROR_MARK_NODE is returned. */
6531 static tree
6532 cp_parser_qualifying_entity (cp_parser *parser,
6533 bool typename_keyword_p,
6534 bool template_keyword_p,
6535 bool check_dependency_p,
6536 bool type_p,
6537 bool is_declaration)
6539 tree saved_scope;
6540 tree saved_qualifying_scope;
6541 tree saved_object_scope;
6542 tree scope;
6543 bool only_class_p;
6544 bool successful_parse_p;
6546 /* DR 743: decltype can appear in a nested-name-specifier. */
6547 if (cp_lexer_next_token_is_decltype (parser->lexer))
6549 scope = cp_parser_decltype (parser);
6550 if (TREE_CODE (scope) != ENUMERAL_TYPE
6551 && !MAYBE_CLASS_TYPE_P (scope))
6553 cp_parser_simulate_error (parser);
6554 return error_mark_node;
6556 if (TYPE_NAME (scope))
6557 scope = TYPE_NAME (scope);
6558 return scope;
6561 /* Before we try to parse the class-name, we must save away the
6562 current PARSER->SCOPE since cp_parser_class_name will destroy
6563 it. */
6564 saved_scope = parser->scope;
6565 saved_qualifying_scope = parser->qualifying_scope;
6566 saved_object_scope = parser->object_scope;
6567 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6568 there is no need to look for a namespace-name. */
6569 only_class_p = template_keyword_p
6570 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6571 if (!only_class_p)
6572 cp_parser_parse_tentatively (parser);
6573 scope = cp_parser_class_name (parser,
6574 typename_keyword_p,
6575 template_keyword_p,
6576 type_p ? class_type : none_type,
6577 check_dependency_p,
6578 /*class_head_p=*/false,
6579 is_declaration,
6580 /*enum_ok=*/cxx_dialect > cxx98);
6581 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6582 /* If that didn't work, try for a namespace-name. */
6583 if (!only_class_p && !successful_parse_p)
6585 /* Restore the saved scope. */
6586 parser->scope = saved_scope;
6587 parser->qualifying_scope = saved_qualifying_scope;
6588 parser->object_scope = saved_object_scope;
6589 /* If we are not looking at an identifier followed by the scope
6590 resolution operator, then this is not part of a
6591 nested-name-specifier. (Note that this function is only used
6592 to parse the components of a nested-name-specifier.) */
6593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6594 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6595 return error_mark_node;
6596 scope = cp_parser_namespace_name (parser);
6599 return scope;
6602 /* Return true if we are looking at a compound-literal, false otherwise. */
6604 static bool
6605 cp_parser_compound_literal_p (cp_parser *parser)
6607 cp_lexer_save_tokens (parser->lexer);
6609 /* Skip tokens until the next token is a closing parenthesis.
6610 If we find the closing `)', and the next token is a `{', then
6611 we are looking at a compound-literal. */
6612 bool compound_literal_p
6613 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6614 /*consume_paren=*/true)
6615 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6617 /* Roll back the tokens we skipped. */
6618 cp_lexer_rollback_tokens (parser->lexer);
6620 return compound_literal_p;
6623 /* Parse a postfix-expression.
6625 postfix-expression:
6626 primary-expression
6627 postfix-expression [ expression ]
6628 postfix-expression ( expression-list [opt] )
6629 simple-type-specifier ( expression-list [opt] )
6630 typename :: [opt] nested-name-specifier identifier
6631 ( expression-list [opt] )
6632 typename :: [opt] nested-name-specifier template [opt] template-id
6633 ( expression-list [opt] )
6634 postfix-expression . template [opt] id-expression
6635 postfix-expression -> template [opt] id-expression
6636 postfix-expression . pseudo-destructor-name
6637 postfix-expression -> pseudo-destructor-name
6638 postfix-expression ++
6639 postfix-expression --
6640 dynamic_cast < type-id > ( expression )
6641 static_cast < type-id > ( expression )
6642 reinterpret_cast < type-id > ( expression )
6643 const_cast < type-id > ( expression )
6644 typeid ( expression )
6645 typeid ( type-id )
6647 GNU Extension:
6649 postfix-expression:
6650 ( type-id ) { initializer-list , [opt] }
6652 This extension is a GNU version of the C99 compound-literal
6653 construct. (The C99 grammar uses `type-name' instead of `type-id',
6654 but they are essentially the same concept.)
6656 If ADDRESS_P is true, the postfix expression is the operand of the
6657 `&' operator. CAST_P is true if this expression is the target of a
6658 cast.
6660 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6661 class member access expressions [expr.ref].
6663 Returns a representation of the expression. */
6665 static cp_expr
6666 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6667 bool member_access_only_p, bool decltype_p,
6668 cp_id_kind * pidk_return)
6670 cp_token *token;
6671 location_t loc;
6672 enum rid keyword;
6673 cp_id_kind idk = CP_ID_KIND_NONE;
6674 cp_expr postfix_expression = NULL_TREE;
6675 bool is_member_access = false;
6677 /* Peek at the next token. */
6678 token = cp_lexer_peek_token (parser->lexer);
6679 loc = token->location;
6680 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6682 /* Some of the productions are determined by keywords. */
6683 keyword = token->keyword;
6684 switch (keyword)
6686 case RID_DYNCAST:
6687 case RID_STATCAST:
6688 case RID_REINTCAST:
6689 case RID_CONSTCAST:
6691 tree type;
6692 cp_expr expression;
6693 const char *saved_message;
6694 bool saved_in_type_id_in_expr_p;
6696 /* All of these can be handled in the same way from the point
6697 of view of parsing. Begin by consuming the token
6698 identifying the cast. */
6699 cp_lexer_consume_token (parser->lexer);
6701 /* New types cannot be defined in the cast. */
6702 saved_message = parser->type_definition_forbidden_message;
6703 parser->type_definition_forbidden_message
6704 = G_("types may not be defined in casts");
6706 /* Look for the opening `<'. */
6707 cp_parser_require (parser, CPP_LESS, RT_LESS);
6708 /* Parse the type to which we are casting. */
6709 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6710 parser->in_type_id_in_expr_p = true;
6711 type = cp_parser_type_id (parser);
6712 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6713 /* Look for the closing `>'. */
6714 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6715 /* Restore the old message. */
6716 parser->type_definition_forbidden_message = saved_message;
6718 bool saved_greater_than_is_operator_p
6719 = parser->greater_than_is_operator_p;
6720 parser->greater_than_is_operator_p = true;
6722 /* And the expression which is being cast. */
6723 matching_parens parens;
6724 parens.require_open (parser);
6725 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6726 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6727 RT_CLOSE_PAREN);
6728 location_t end_loc = close_paren ?
6729 close_paren->location : UNKNOWN_LOCATION;
6731 parser->greater_than_is_operator_p
6732 = saved_greater_than_is_operator_p;
6734 /* Only type conversions to integral or enumeration types
6735 can be used in constant-expressions. */
6736 if (!cast_valid_in_integral_constant_expression_p (type)
6737 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6739 postfix_expression = error_mark_node;
6740 break;
6743 switch (keyword)
6745 case RID_DYNCAST:
6746 postfix_expression
6747 = build_dynamic_cast (type, expression, tf_warning_or_error);
6748 break;
6749 case RID_STATCAST:
6750 postfix_expression
6751 = build_static_cast (type, expression, tf_warning_or_error);
6752 break;
6753 case RID_REINTCAST:
6754 postfix_expression
6755 = build_reinterpret_cast (type, expression,
6756 tf_warning_or_error);
6757 break;
6758 case RID_CONSTCAST:
6759 postfix_expression
6760 = build_const_cast (type, expression, tf_warning_or_error);
6761 break;
6762 default:
6763 gcc_unreachable ();
6766 /* Construct a location e.g. :
6767 reinterpret_cast <int *> (expr)
6768 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6769 ranging from the start of the "*_cast" token to the final closing
6770 paren, with the caret at the start. */
6771 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6772 postfix_expression.set_location (cp_cast_loc);
6774 break;
6776 case RID_TYPEID:
6778 tree type;
6779 const char *saved_message;
6780 bool saved_in_type_id_in_expr_p;
6782 /* Consume the `typeid' token. */
6783 cp_lexer_consume_token (parser->lexer);
6784 /* Look for the `(' token. */
6785 matching_parens parens;
6786 parens.require_open (parser);
6787 /* Types cannot be defined in a `typeid' expression. */
6788 saved_message = parser->type_definition_forbidden_message;
6789 parser->type_definition_forbidden_message
6790 = G_("types may not be defined in a %<typeid%> expression");
6791 /* We can't be sure yet whether we're looking at a type-id or an
6792 expression. */
6793 cp_parser_parse_tentatively (parser);
6794 /* Try a type-id first. */
6795 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6796 parser->in_type_id_in_expr_p = true;
6797 type = cp_parser_type_id (parser);
6798 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6799 /* Look for the `)' token. Otherwise, we can't be sure that
6800 we're not looking at an expression: consider `typeid (int
6801 (3))', for example. */
6802 cp_token *close_paren = parens.require_close (parser);
6803 /* If all went well, simply lookup the type-id. */
6804 if (cp_parser_parse_definitely (parser))
6805 postfix_expression = get_typeid (type, tf_warning_or_error);
6806 /* Otherwise, fall back to the expression variant. */
6807 else
6809 tree expression;
6811 /* Look for an expression. */
6812 expression = cp_parser_expression (parser, & idk);
6813 /* Compute its typeid. */
6814 postfix_expression = build_typeid (expression, tf_warning_or_error);
6815 /* Look for the `)' token. */
6816 close_paren = parens.require_close (parser);
6818 /* Restore the saved message. */
6819 parser->type_definition_forbidden_message = saved_message;
6820 /* `typeid' may not appear in an integral constant expression. */
6821 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6822 postfix_expression = error_mark_node;
6824 /* Construct a location e.g. :
6825 typeid (expr)
6826 ^~~~~~~~~~~~~
6827 ranging from the start of the "typeid" token to the final closing
6828 paren, with the caret at the start. */
6829 if (close_paren)
6831 location_t typeid_loc
6832 = make_location (start_loc, start_loc, close_paren->location);
6833 postfix_expression.set_location (typeid_loc);
6836 break;
6838 case RID_TYPENAME:
6840 tree type;
6841 /* The syntax permitted here is the same permitted for an
6842 elaborated-type-specifier. */
6843 ++parser->prevent_constrained_type_specifiers;
6844 type = cp_parser_elaborated_type_specifier (parser,
6845 /*is_friend=*/false,
6846 /*is_declaration=*/false);
6847 --parser->prevent_constrained_type_specifiers;
6848 postfix_expression = cp_parser_functional_cast (parser, type);
6850 break;
6852 case RID_ADDRESSOF:
6853 case RID_BUILTIN_SHUFFLE:
6854 case RID_BUILTIN_LAUNDER:
6856 vec<tree, va_gc> *vec;
6857 unsigned int i;
6858 tree p;
6860 cp_lexer_consume_token (parser->lexer);
6861 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6862 /*cast_p=*/false, /*allow_expansion_p=*/true,
6863 /*non_constant_p=*/NULL);
6864 if (vec == NULL)
6866 postfix_expression = error_mark_node;
6867 break;
6870 FOR_EACH_VEC_ELT (*vec, i, p)
6871 mark_exp_read (p);
6873 switch (keyword)
6875 case RID_ADDRESSOF:
6876 if (vec->length () == 1)
6877 postfix_expression
6878 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6879 else
6881 error_at (loc, "wrong number of arguments to "
6882 "%<__builtin_addressof%>");
6883 postfix_expression = error_mark_node;
6885 break;
6887 case RID_BUILTIN_LAUNDER:
6888 if (vec->length () == 1)
6889 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6890 tf_warning_or_error);
6891 else
6893 error_at (loc, "wrong number of arguments to "
6894 "%<__builtin_launder%>");
6895 postfix_expression = error_mark_node;
6897 break;
6899 case RID_BUILTIN_SHUFFLE:
6900 if (vec->length () == 2)
6901 postfix_expression
6902 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6903 (*vec)[1], tf_warning_or_error);
6904 else if (vec->length () == 3)
6905 postfix_expression
6906 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6907 (*vec)[2], tf_warning_or_error);
6908 else
6910 error_at (loc, "wrong number of arguments to "
6911 "%<__builtin_shuffle%>");
6912 postfix_expression = error_mark_node;
6914 break;
6916 default:
6917 gcc_unreachable ();
6919 break;
6922 default:
6924 tree type;
6926 /* If the next thing is a simple-type-specifier, we may be
6927 looking at a functional cast. We could also be looking at
6928 an id-expression. So, we try the functional cast, and if
6929 that doesn't work we fall back to the primary-expression. */
6930 cp_parser_parse_tentatively (parser);
6931 /* Look for the simple-type-specifier. */
6932 ++parser->prevent_constrained_type_specifiers;
6933 type = cp_parser_simple_type_specifier (parser,
6934 /*decl_specs=*/NULL,
6935 CP_PARSER_FLAGS_NONE);
6936 --parser->prevent_constrained_type_specifiers;
6937 /* Parse the cast itself. */
6938 if (!cp_parser_error_occurred (parser))
6939 postfix_expression
6940 = cp_parser_functional_cast (parser, type);
6941 /* If that worked, we're done. */
6942 if (cp_parser_parse_definitely (parser))
6943 break;
6945 /* If the functional-cast didn't work out, try a
6946 compound-literal. */
6947 if (cp_parser_allow_gnu_extensions_p (parser)
6948 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6950 cp_expr initializer = NULL_TREE;
6952 cp_parser_parse_tentatively (parser);
6954 matching_parens parens;
6955 parens.consume_open (parser);
6957 /* Avoid calling cp_parser_type_id pointlessly, see comment
6958 in cp_parser_cast_expression about c++/29234. */
6959 if (!cp_parser_compound_literal_p (parser))
6960 cp_parser_simulate_error (parser);
6961 else
6963 /* Parse the type. */
6964 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6965 parser->in_type_id_in_expr_p = true;
6966 type = cp_parser_type_id (parser);
6967 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6968 parens.require_close (parser);
6971 /* If things aren't going well, there's no need to
6972 keep going. */
6973 if (!cp_parser_error_occurred (parser))
6975 bool non_constant_p;
6976 /* Parse the brace-enclosed initializer list. */
6977 initializer = cp_parser_braced_list (parser,
6978 &non_constant_p);
6980 /* If that worked, we're definitely looking at a
6981 compound-literal expression. */
6982 if (cp_parser_parse_definitely (parser))
6984 /* Warn the user that a compound literal is not
6985 allowed in standard C++. */
6986 pedwarn (input_location, OPT_Wpedantic,
6987 "ISO C++ forbids compound-literals");
6988 /* For simplicity, we disallow compound literals in
6989 constant-expressions. We could
6990 allow compound literals of integer type, whose
6991 initializer was a constant, in constant
6992 expressions. Permitting that usage, as a further
6993 extension, would not change the meaning of any
6994 currently accepted programs. (Of course, as
6995 compound literals are not part of ISO C++, the
6996 standard has nothing to say.) */
6997 if (cp_parser_non_integral_constant_expression (parser,
6998 NIC_NCC))
7000 postfix_expression = error_mark_node;
7001 break;
7003 /* Form the representation of the compound-literal. */
7004 postfix_expression
7005 = finish_compound_literal (type, initializer,
7006 tf_warning_or_error, fcl_c99);
7007 postfix_expression.set_location (initializer.get_location ());
7008 break;
7012 /* It must be a primary-expression. */
7013 postfix_expression
7014 = cp_parser_primary_expression (parser, address_p, cast_p,
7015 /*template_arg_p=*/false,
7016 decltype_p,
7017 &idk);
7019 break;
7022 /* Note that we don't need to worry about calling build_cplus_new on a
7023 class-valued CALL_EXPR in decltype when it isn't the end of the
7024 postfix-expression; unary_complex_lvalue will take care of that for
7025 all these cases. */
7027 /* Keep looping until the postfix-expression is complete. */
7028 while (true)
7030 if (idk == CP_ID_KIND_UNQUALIFIED
7031 && identifier_p (postfix_expression)
7032 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7033 /* It is not a Koenig lookup function call. */
7034 postfix_expression
7035 = unqualified_name_lookup_error (postfix_expression);
7037 /* Peek at the next token. */
7038 token = cp_lexer_peek_token (parser->lexer);
7040 switch (token->type)
7042 case CPP_OPEN_SQUARE:
7043 if (cp_next_tokens_can_be_std_attribute_p (parser))
7045 cp_parser_error (parser,
7046 "two consecutive %<[%> shall "
7047 "only introduce an attribute");
7048 return error_mark_node;
7050 postfix_expression
7051 = cp_parser_postfix_open_square_expression (parser,
7052 postfix_expression,
7053 false,
7054 decltype_p);
7055 postfix_expression.set_range (start_loc,
7056 postfix_expression.get_location ());
7058 idk = CP_ID_KIND_NONE;
7059 is_member_access = false;
7060 break;
7062 case CPP_OPEN_PAREN:
7063 /* postfix-expression ( expression-list [opt] ) */
7065 bool koenig_p;
7066 bool is_builtin_constant_p;
7067 bool saved_integral_constant_expression_p = false;
7068 bool saved_non_integral_constant_expression_p = false;
7069 tsubst_flags_t complain = complain_flags (decltype_p);
7070 vec<tree, va_gc> *args;
7071 location_t close_paren_loc = UNKNOWN_LOCATION;
7073 is_member_access = false;
7075 is_builtin_constant_p
7076 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7077 if (is_builtin_constant_p)
7079 /* The whole point of __builtin_constant_p is to allow
7080 non-constant expressions to appear as arguments. */
7081 saved_integral_constant_expression_p
7082 = parser->integral_constant_expression_p;
7083 saved_non_integral_constant_expression_p
7084 = parser->non_integral_constant_expression_p;
7085 parser->integral_constant_expression_p = false;
7087 args = (cp_parser_parenthesized_expression_list
7088 (parser, non_attr,
7089 /*cast_p=*/false, /*allow_expansion_p=*/true,
7090 /*non_constant_p=*/NULL,
7091 /*close_paren_loc=*/&close_paren_loc));
7092 if (is_builtin_constant_p)
7094 parser->integral_constant_expression_p
7095 = saved_integral_constant_expression_p;
7096 parser->non_integral_constant_expression_p
7097 = saved_non_integral_constant_expression_p;
7100 if (args == NULL)
7102 postfix_expression = error_mark_node;
7103 break;
7106 /* Function calls are not permitted in
7107 constant-expressions. */
7108 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7109 && cp_parser_non_integral_constant_expression (parser,
7110 NIC_FUNC_CALL))
7112 postfix_expression = error_mark_node;
7113 release_tree_vector (args);
7114 break;
7117 koenig_p = false;
7118 if (idk == CP_ID_KIND_UNQUALIFIED
7119 || idk == CP_ID_KIND_TEMPLATE_ID)
7121 if (identifier_p (postfix_expression))
7123 if (!args->is_empty ())
7125 koenig_p = true;
7126 if (!any_type_dependent_arguments_p (args))
7127 postfix_expression
7128 = perform_koenig_lookup (postfix_expression, args,
7129 complain);
7131 else
7132 postfix_expression
7133 = unqualified_fn_lookup_error (postfix_expression);
7135 /* We do not perform argument-dependent lookup if
7136 normal lookup finds a non-function, in accordance
7137 with the expected resolution of DR 218. */
7138 else if (!args->is_empty ()
7139 && is_overloaded_fn (postfix_expression))
7141 tree fn = get_first_fn (postfix_expression);
7142 fn = STRIP_TEMPLATE (fn);
7144 /* Do not do argument dependent lookup if regular
7145 lookup finds a member function or a block-scope
7146 function declaration. [basic.lookup.argdep]/3 */
7147 if (!DECL_FUNCTION_MEMBER_P (fn)
7148 && !DECL_LOCAL_FUNCTION_P (fn))
7150 koenig_p = true;
7151 if (!any_type_dependent_arguments_p (args))
7152 postfix_expression
7153 = perform_koenig_lookup (postfix_expression, args,
7154 complain);
7159 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7160 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7161 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7162 && vec_safe_length (args) == 3)
7164 tree arg0 = (*args)[0];
7165 tree arg1 = (*args)[1];
7166 tree arg2 = (*args)[2];
7167 int literal_mask = ((!!integer_zerop (arg1) << 1)
7168 | (!!integer_zerop (arg2) << 2));
7169 if (TREE_CODE (arg2) == CONST_DECL)
7170 arg2 = DECL_INITIAL (arg2);
7171 warn_for_memset (input_location, arg0, arg2, literal_mask);
7174 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7176 tree instance = TREE_OPERAND (postfix_expression, 0);
7177 tree fn = TREE_OPERAND (postfix_expression, 1);
7179 if (processing_template_decl
7180 && (type_dependent_object_expression_p (instance)
7181 || (!BASELINK_P (fn)
7182 && TREE_CODE (fn) != FIELD_DECL)
7183 || type_dependent_expression_p (fn)
7184 || any_type_dependent_arguments_p (args)))
7186 maybe_generic_this_capture (instance, fn);
7187 postfix_expression
7188 = build_min_nt_call_vec (postfix_expression, args);
7189 release_tree_vector (args);
7190 break;
7193 if (BASELINK_P (fn))
7195 postfix_expression
7196 = (build_new_method_call
7197 (instance, fn, &args, NULL_TREE,
7198 (idk == CP_ID_KIND_QUALIFIED
7199 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7200 : LOOKUP_NORMAL),
7201 /*fn_p=*/NULL,
7202 complain));
7204 else
7205 postfix_expression
7206 = finish_call_expr (postfix_expression, &args,
7207 /*disallow_virtual=*/false,
7208 /*koenig_p=*/false,
7209 complain);
7211 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7212 || TREE_CODE (postfix_expression) == MEMBER_REF
7213 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7214 postfix_expression = (build_offset_ref_call_from_tree
7215 (postfix_expression, &args,
7216 complain));
7217 else if (idk == CP_ID_KIND_QUALIFIED)
7218 /* A call to a static class member, or a namespace-scope
7219 function. */
7220 postfix_expression
7221 = finish_call_expr (postfix_expression, &args,
7222 /*disallow_virtual=*/true,
7223 koenig_p,
7224 complain);
7225 else
7226 /* All other function calls. */
7227 postfix_expression
7228 = finish_call_expr (postfix_expression, &args,
7229 /*disallow_virtual=*/false,
7230 koenig_p,
7231 complain);
7233 if (close_paren_loc != UNKNOWN_LOCATION)
7235 location_t combined_loc = make_location (token->location,
7236 start_loc,
7237 close_paren_loc);
7238 postfix_expression.set_location (combined_loc);
7241 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7242 idk = CP_ID_KIND_NONE;
7244 release_tree_vector (args);
7246 break;
7248 case CPP_DOT:
7249 case CPP_DEREF:
7250 /* postfix-expression . template [opt] id-expression
7251 postfix-expression . pseudo-destructor-name
7252 postfix-expression -> template [opt] id-expression
7253 postfix-expression -> pseudo-destructor-name */
7255 /* Consume the `.' or `->' operator. */
7256 cp_lexer_consume_token (parser->lexer);
7258 postfix_expression
7259 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7260 postfix_expression,
7261 false, &idk, loc);
7263 is_member_access = true;
7264 break;
7266 case CPP_PLUS_PLUS:
7267 /* postfix-expression ++ */
7268 /* Consume the `++' token. */
7269 cp_lexer_consume_token (parser->lexer);
7270 /* Generate a representation for the complete expression. */
7271 postfix_expression
7272 = finish_increment_expr (postfix_expression,
7273 POSTINCREMENT_EXPR);
7274 /* Increments may not appear in constant-expressions. */
7275 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7276 postfix_expression = error_mark_node;
7277 idk = CP_ID_KIND_NONE;
7278 is_member_access = false;
7279 break;
7281 case CPP_MINUS_MINUS:
7282 /* postfix-expression -- */
7283 /* Consume the `--' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285 /* Generate a representation for the complete expression. */
7286 postfix_expression
7287 = finish_increment_expr (postfix_expression,
7288 POSTDECREMENT_EXPR);
7289 /* Decrements may not appear in constant-expressions. */
7290 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7291 postfix_expression = error_mark_node;
7292 idk = CP_ID_KIND_NONE;
7293 is_member_access = false;
7294 break;
7296 default:
7297 if (pidk_return != NULL)
7298 * pidk_return = idk;
7299 if (member_access_only_p)
7300 return is_member_access
7301 ? postfix_expression
7302 : cp_expr (error_mark_node);
7303 else
7304 return postfix_expression;
7308 /* We should never get here. */
7309 gcc_unreachable ();
7310 return error_mark_node;
7313 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7314 by cp_parser_builtin_offsetof. We're looking for
7316 postfix-expression [ expression ]
7317 postfix-expression [ braced-init-list ] (C++11)
7319 FOR_OFFSETOF is set if we're being called in that context, which
7320 changes how we deal with integer constant expressions. */
7322 static tree
7323 cp_parser_postfix_open_square_expression (cp_parser *parser,
7324 tree postfix_expression,
7325 bool for_offsetof,
7326 bool decltype_p)
7328 tree index = NULL_TREE;
7329 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7330 bool saved_greater_than_is_operator_p;
7332 /* Consume the `[' token. */
7333 cp_lexer_consume_token (parser->lexer);
7335 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7336 parser->greater_than_is_operator_p = true;
7338 /* Parse the index expression. */
7339 /* ??? For offsetof, there is a question of what to allow here. If
7340 offsetof is not being used in an integral constant expression context,
7341 then we *could* get the right answer by computing the value at runtime.
7342 If we are in an integral constant expression context, then we might
7343 could accept any constant expression; hard to say without analysis.
7344 Rather than open the barn door too wide right away, allow only integer
7345 constant expressions here. */
7346 if (for_offsetof)
7347 index = cp_parser_constant_expression (parser);
7348 else
7350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7352 bool expr_nonconst_p;
7353 cp_lexer_set_source_position (parser->lexer);
7354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7355 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7357 else
7358 index = cp_parser_expression (parser);
7361 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7363 /* Look for the closing `]'. */
7364 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7366 /* Build the ARRAY_REF. */
7367 postfix_expression = grok_array_decl (loc, postfix_expression,
7368 index, decltype_p);
7370 /* When not doing offsetof, array references are not permitted in
7371 constant-expressions. */
7372 if (!for_offsetof
7373 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7374 postfix_expression = error_mark_node;
7376 return postfix_expression;
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7382 postfix-expression . template [opt] id-expression
7383 postfix-expression . pseudo-destructor-name
7384 postfix-expression -> template [opt] id-expression
7385 postfix-expression -> pseudo-destructor-name
7387 FOR_OFFSETOF is set if we're being called in that context. That sorta
7388 limits what of the above we'll actually accept, but nevermind.
7389 TOKEN_TYPE is the "." or "->" token, which will already have been
7390 removed from the stream. */
7392 static tree
7393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7394 enum cpp_ttype token_type,
7395 cp_expr postfix_expression,
7396 bool for_offsetof, cp_id_kind *idk,
7397 location_t location)
7399 tree name;
7400 bool dependent_p;
7401 bool pseudo_destructor_p;
7402 tree scope = NULL_TREE;
7403 location_t start_loc = postfix_expression.get_start ();
7405 /* If this is a `->' operator, dereference the pointer. */
7406 if (token_type == CPP_DEREF)
7407 postfix_expression = build_x_arrow (location, postfix_expression,
7408 tf_warning_or_error);
7409 /* Check to see whether or not the expression is type-dependent and
7410 not the current instantiation. */
7411 dependent_p = type_dependent_object_expression_p (postfix_expression);
7412 /* The identifier following the `->' or `.' is not qualified. */
7413 parser->scope = NULL_TREE;
7414 parser->qualifying_scope = NULL_TREE;
7415 parser->object_scope = NULL_TREE;
7416 *idk = CP_ID_KIND_NONE;
7418 /* Enter the scope corresponding to the type of the object
7419 given by the POSTFIX_EXPRESSION. */
7420 if (!dependent_p)
7422 scope = TREE_TYPE (postfix_expression);
7423 /* According to the standard, no expression should ever have
7424 reference type. Unfortunately, we do not currently match
7425 the standard in this respect in that our internal representation
7426 of an expression may have reference type even when the standard
7427 says it does not. Therefore, we have to manually obtain the
7428 underlying type here. */
7429 scope = non_reference (scope);
7430 /* The type of the POSTFIX_EXPRESSION must be complete. */
7431 /* Unlike the object expression in other contexts, *this is not
7432 required to be of complete type for purposes of class member
7433 access (5.2.5) outside the member function body. */
7434 if (postfix_expression != current_class_ref
7435 && scope != error_mark_node
7436 && !(processing_template_decl
7437 && current_class_type
7438 && (same_type_ignoring_top_level_qualifiers_p
7439 (scope, current_class_type))))
7441 scope = complete_type (scope);
7442 if (!COMPLETE_TYPE_P (scope)
7443 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7444 && EXPR_P (postfix_expression))
7446 /* In a template, be permissive by treating an object expression
7447 of incomplete type as dependent (after a pedwarn). */
7448 diagnostic_t kind = (processing_template_decl
7449 && MAYBE_CLASS_TYPE_P (scope)
7450 ? DK_PEDWARN
7451 : DK_ERROR);
7452 cxx_incomplete_type_diagnostic
7453 (location_of (postfix_expression),
7454 postfix_expression, scope, kind);
7455 if (!MAYBE_CLASS_TYPE_P (scope))
7456 return error_mark_node;
7457 if (processing_template_decl)
7459 dependent_p = true;
7460 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7465 if (!dependent_p)
7467 /* Let the name lookup machinery know that we are processing a
7468 class member access expression. */
7469 parser->context->object_type = scope;
7470 /* If something went wrong, we want to be able to discern that case,
7471 as opposed to the case where there was no SCOPE due to the type
7472 of expression being dependent. */
7473 if (!scope)
7474 scope = error_mark_node;
7475 /* If the SCOPE was erroneous, make the various semantic analysis
7476 functions exit quickly -- and without issuing additional error
7477 messages. */
7478 if (scope == error_mark_node)
7479 postfix_expression = error_mark_node;
7483 if (dependent_p)
7484 /* Tell cp_parser_lookup_name that there was an object, even though it's
7485 type-dependent. */
7486 parser->context->object_type = unknown_type_node;
7488 /* Assume this expression is not a pseudo-destructor access. */
7489 pseudo_destructor_p = false;
7491 /* If the SCOPE is a scalar type, then, if this is a valid program,
7492 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7493 is type dependent, it can be pseudo-destructor-name or something else.
7494 Try to parse it as pseudo-destructor-name first. */
7495 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7497 tree s;
7498 tree type;
7500 cp_parser_parse_tentatively (parser);
7501 /* Parse the pseudo-destructor-name. */
7502 s = NULL_TREE;
7503 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7504 &s, &type);
7505 if (dependent_p
7506 && (cp_parser_error_occurred (parser)
7507 || !SCALAR_TYPE_P (type)))
7508 cp_parser_abort_tentative_parse (parser);
7509 else if (cp_parser_parse_definitely (parser))
7511 pseudo_destructor_p = true;
7512 postfix_expression
7513 = finish_pseudo_destructor_expr (postfix_expression,
7514 s, type, location);
7518 if (!pseudo_destructor_p)
7520 /* If the SCOPE is not a scalar type, we are looking at an
7521 ordinary class member access expression, rather than a
7522 pseudo-destructor-name. */
7523 bool template_p;
7524 cp_token *token = cp_lexer_peek_token (parser->lexer);
7525 /* Parse the id-expression. */
7526 name = (cp_parser_id_expression
7527 (parser,
7528 cp_parser_optional_template_keyword (parser),
7529 /*check_dependency_p=*/true,
7530 &template_p,
7531 /*declarator_p=*/false,
7532 /*optional_p=*/false));
7533 /* In general, build a SCOPE_REF if the member name is qualified.
7534 However, if the name was not dependent and has already been
7535 resolved; there is no need to build the SCOPE_REF. For example;
7537 struct X { void f(); };
7538 template <typename T> void f(T* t) { t->X::f(); }
7540 Even though "t" is dependent, "X::f" is not and has been resolved
7541 to a BASELINK; there is no need to include scope information. */
7543 /* But we do need to remember that there was an explicit scope for
7544 virtual function calls. */
7545 if (parser->scope)
7546 *idk = CP_ID_KIND_QUALIFIED;
7548 /* If the name is a template-id that names a type, we will get a
7549 TYPE_DECL here. That is invalid code. */
7550 if (TREE_CODE (name) == TYPE_DECL)
7552 error_at (token->location, "invalid use of %qD", name);
7553 postfix_expression = error_mark_node;
7555 else
7557 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7559 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7561 error_at (token->location, "%<%D::%D%> is not a class member",
7562 parser->scope, name);
7563 postfix_expression = error_mark_node;
7565 else
7566 name = build_qualified_name (/*type=*/NULL_TREE,
7567 parser->scope,
7568 name,
7569 template_p);
7570 parser->scope = NULL_TREE;
7571 parser->qualifying_scope = NULL_TREE;
7572 parser->object_scope = NULL_TREE;
7574 if (parser->scope && name && BASELINK_P (name))
7575 adjust_result_of_qualified_name_lookup
7576 (name, parser->scope, scope);
7577 postfix_expression
7578 = finish_class_member_access_expr (postfix_expression, name,
7579 template_p,
7580 tf_warning_or_error);
7581 /* Build a location e.g.:
7582 ptr->access_expr
7583 ~~~^~~~~~~~~~~~~
7584 where the caret is at the deref token, ranging from
7585 the start of postfix_expression to the end of the access expr. */
7586 location_t end_loc
7587 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7588 location_t combined_loc
7589 = make_location (input_location, start_loc, end_loc);
7590 protected_set_expr_location (postfix_expression, combined_loc);
7594 /* We no longer need to look up names in the scope of the object on
7595 the left-hand side of the `.' or `->' operator. */
7596 parser->context->object_type = NULL_TREE;
7598 /* Outside of offsetof, these operators may not appear in
7599 constant-expressions. */
7600 if (!for_offsetof
7601 && (cp_parser_non_integral_constant_expression
7602 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7603 postfix_expression = error_mark_node;
7605 return postfix_expression;
7608 /* Parse a parenthesized expression-list.
7610 expression-list:
7611 assignment-expression
7612 expression-list, assignment-expression
7614 attribute-list:
7615 expression-list
7616 identifier
7617 identifier, expression-list
7619 CAST_P is true if this expression is the target of a cast.
7621 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7622 argument pack.
7624 Returns a vector of trees. Each element is a representation of an
7625 assignment-expression. NULL is returned if the ( and or ) are
7626 missing. An empty, but allocated, vector is returned on no
7627 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7628 if we are parsing an attribute list for an attribute that wants a
7629 plain identifier argument, normal_attr for an attribute that wants
7630 an expression, or non_attr if we aren't parsing an attribute list. If
7631 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7632 not all of the expressions in the list were constant.
7633 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7634 will be written to with the location of the closing parenthesis. If
7635 an error occurs, it may or may not be written to. */
7637 static vec<tree, va_gc> *
7638 cp_parser_parenthesized_expression_list (cp_parser* parser,
7639 int is_attribute_list,
7640 bool cast_p,
7641 bool allow_expansion_p,
7642 bool *non_constant_p,
7643 location_t *close_paren_loc)
7645 vec<tree, va_gc> *expression_list;
7646 bool fold_expr_p = is_attribute_list != non_attr;
7647 tree identifier = NULL_TREE;
7648 bool saved_greater_than_is_operator_p;
7650 /* Assume all the expressions will be constant. */
7651 if (non_constant_p)
7652 *non_constant_p = false;
7654 matching_parens parens;
7655 if (!parens.require_open (parser))
7656 return NULL;
7658 expression_list = make_tree_vector ();
7660 /* Within a parenthesized expression, a `>' token is always
7661 the greater-than operator. */
7662 saved_greater_than_is_operator_p
7663 = parser->greater_than_is_operator_p;
7664 parser->greater_than_is_operator_p = true;
7666 /* Consume expressions until there are no more. */
7667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7668 while (true)
7670 tree expr;
7672 /* At the beginning of attribute lists, check to see if the
7673 next token is an identifier. */
7674 if (is_attribute_list == id_attr
7675 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7677 cp_token *token;
7679 /* Consume the identifier. */
7680 token = cp_lexer_consume_token (parser->lexer);
7681 /* Save the identifier. */
7682 identifier = token->u.value;
7684 else
7686 bool expr_non_constant_p;
7688 /* Parse the next assignment-expression. */
7689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7691 /* A braced-init-list. */
7692 cp_lexer_set_source_position (parser->lexer);
7693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7694 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7695 if (non_constant_p && expr_non_constant_p)
7696 *non_constant_p = true;
7698 else if (non_constant_p)
7700 expr = (cp_parser_constant_expression
7701 (parser, /*allow_non_constant_p=*/true,
7702 &expr_non_constant_p));
7703 if (expr_non_constant_p)
7704 *non_constant_p = true;
7706 else
7707 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7708 cast_p);
7710 if (fold_expr_p)
7711 expr = instantiate_non_dependent_expr (expr);
7713 /* If we have an ellipsis, then this is an expression
7714 expansion. */
7715 if (allow_expansion_p
7716 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7718 /* Consume the `...'. */
7719 cp_lexer_consume_token (parser->lexer);
7721 /* Build the argument pack. */
7722 expr = make_pack_expansion (expr);
7725 /* Add it to the list. We add error_mark_node
7726 expressions to the list, so that we can still tell if
7727 the correct form for a parenthesized expression-list
7728 is found. That gives better errors. */
7729 vec_safe_push (expression_list, expr);
7731 if (expr == error_mark_node)
7732 goto skip_comma;
7735 /* After the first item, attribute lists look the same as
7736 expression lists. */
7737 is_attribute_list = non_attr;
7739 get_comma:;
7740 /* If the next token isn't a `,', then we are done. */
7741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7742 break;
7744 /* Otherwise, consume the `,' and keep going. */
7745 cp_lexer_consume_token (parser->lexer);
7748 if (close_paren_loc)
7749 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7751 if (!parens.require_close (parser))
7753 int ending;
7755 skip_comma:;
7756 /* We try and resync to an unnested comma, as that will give the
7757 user better diagnostics. */
7758 ending = cp_parser_skip_to_closing_parenthesis (parser,
7759 /*recovering=*/true,
7760 /*or_comma=*/true,
7761 /*consume_paren=*/true);
7762 if (ending < 0)
7763 goto get_comma;
7764 if (!ending)
7766 parser->greater_than_is_operator_p
7767 = saved_greater_than_is_operator_p;
7768 return NULL;
7772 parser->greater_than_is_operator_p
7773 = saved_greater_than_is_operator_p;
7775 if (identifier)
7776 vec_safe_insert (expression_list, 0, identifier);
7778 return expression_list;
7781 /* Parse a pseudo-destructor-name.
7783 pseudo-destructor-name:
7784 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7785 :: [opt] nested-name-specifier template template-id :: ~ type-name
7786 :: [opt] nested-name-specifier [opt] ~ type-name
7788 If either of the first two productions is used, sets *SCOPE to the
7789 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7790 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7791 or ERROR_MARK_NODE if the parse fails. */
7793 static void
7794 cp_parser_pseudo_destructor_name (cp_parser* parser,
7795 tree object,
7796 tree* scope,
7797 tree* type)
7799 bool nested_name_specifier_p;
7801 /* Handle ~auto. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7803 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7804 && !type_dependent_expression_p (object))
7806 if (cxx_dialect < cxx14)
7807 pedwarn (input_location, 0,
7808 "%<~auto%> only available with "
7809 "-std=c++14 or -std=gnu++14");
7810 cp_lexer_consume_token (parser->lexer);
7811 cp_lexer_consume_token (parser->lexer);
7812 *scope = NULL_TREE;
7813 *type = TREE_TYPE (object);
7814 return;
7817 /* Assume that things will not work out. */
7818 *type = error_mark_node;
7820 /* Look for the optional `::' operator. */
7821 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7822 /* Look for the optional nested-name-specifier. */
7823 nested_name_specifier_p
7824 = (cp_parser_nested_name_specifier_opt (parser,
7825 /*typename_keyword_p=*/false,
7826 /*check_dependency_p=*/true,
7827 /*type_p=*/false,
7828 /*is_declaration=*/false)
7829 != NULL_TREE);
7830 /* Now, if we saw a nested-name-specifier, we might be doing the
7831 second production. */
7832 if (nested_name_specifier_p
7833 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7835 /* Consume the `template' keyword. */
7836 cp_lexer_consume_token (parser->lexer);
7837 /* Parse the template-id. */
7838 cp_parser_template_id (parser,
7839 /*template_keyword_p=*/true,
7840 /*check_dependency_p=*/false,
7841 class_type,
7842 /*is_declaration=*/true);
7843 /* Look for the `::' token. */
7844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7846 /* If the next token is not a `~', then there might be some
7847 additional qualification. */
7848 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7850 /* At this point, we're looking for "type-name :: ~". The type-name
7851 must not be a class-name, since this is a pseudo-destructor. So,
7852 it must be either an enum-name, or a typedef-name -- both of which
7853 are just identifiers. So, we peek ahead to check that the "::"
7854 and "~" tokens are present; if they are not, then we can avoid
7855 calling type_name. */
7856 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7858 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7860 cp_parser_error (parser, "non-scalar type");
7861 return;
7864 /* Look for the type-name. */
7865 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7866 if (*scope == error_mark_node)
7867 return;
7869 /* Look for the `::' token. */
7870 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7872 else
7873 *scope = NULL_TREE;
7875 /* Look for the `~'. */
7876 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7878 /* Once we see the ~, this has to be a pseudo-destructor. */
7879 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7880 cp_parser_commit_to_topmost_tentative_parse (parser);
7882 /* Look for the type-name again. We are not responsible for
7883 checking that it matches the first type-name. */
7884 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7887 /* Parse a unary-expression.
7889 unary-expression:
7890 postfix-expression
7891 ++ cast-expression
7892 -- cast-expression
7893 unary-operator cast-expression
7894 sizeof unary-expression
7895 sizeof ( type-id )
7896 alignof ( type-id ) [C++0x]
7897 new-expression
7898 delete-expression
7900 GNU Extensions:
7902 unary-expression:
7903 __extension__ cast-expression
7904 __alignof__ unary-expression
7905 __alignof__ ( type-id )
7906 alignof unary-expression [C++0x]
7907 __real__ cast-expression
7908 __imag__ cast-expression
7909 && identifier
7910 sizeof ( type-id ) { initializer-list , [opt] }
7911 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7912 __alignof__ ( type-id ) { initializer-list , [opt] }
7914 ADDRESS_P is true iff the unary-expression is appearing as the
7915 operand of the `&' operator. CAST_P is true if this expression is
7916 the target of a cast.
7918 Returns a representation of the expression. */
7920 static cp_expr
7921 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7922 bool address_p, bool cast_p, bool decltype_p)
7924 cp_token *token;
7925 enum tree_code unary_operator;
7927 /* Peek at the next token. */
7928 token = cp_lexer_peek_token (parser->lexer);
7929 /* Some keywords give away the kind of expression. */
7930 if (token->type == CPP_KEYWORD)
7932 enum rid keyword = token->keyword;
7934 switch (keyword)
7936 case RID_ALIGNOF:
7937 case RID_SIZEOF:
7939 tree operand, ret;
7940 enum tree_code op;
7941 location_t start_loc = token->location;
7943 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7944 /* Consume the token. */
7945 cp_lexer_consume_token (parser->lexer);
7946 /* Parse the operand. */
7947 operand = cp_parser_sizeof_operand (parser, keyword);
7949 if (TYPE_P (operand))
7950 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7951 else
7953 /* ISO C++ defines alignof only with types, not with
7954 expressions. So pedwarn if alignof is used with a non-
7955 type expression. However, __alignof__ is ok. */
7956 if (id_equal (token->u.value, "alignof"))
7957 pedwarn (token->location, OPT_Wpedantic,
7958 "ISO C++ does not allow %<alignof%> "
7959 "with a non-type");
7961 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7963 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7964 SIZEOF_EXPR with the original operand. */
7965 if (op == SIZEOF_EXPR && ret != error_mark_node)
7967 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7969 if (!processing_template_decl && TYPE_P (operand))
7971 ret = build_min (SIZEOF_EXPR, size_type_node,
7972 build1 (NOP_EXPR, operand,
7973 error_mark_node));
7974 SIZEOF_EXPR_TYPE_P (ret) = 1;
7976 else
7977 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7978 TREE_SIDE_EFFECTS (ret) = 0;
7979 TREE_READONLY (ret) = 1;
7983 /* Construct a location e.g. :
7984 alignof (expr)
7985 ^~~~~~~~~~~~~~
7986 with start == caret at the start of the "alignof"/"sizeof"
7987 token, with the endpoint at the final closing paren. */
7988 location_t finish_loc
7989 = cp_lexer_previous_token (parser->lexer)->location;
7990 location_t compound_loc
7991 = make_location (start_loc, start_loc, finish_loc);
7993 cp_expr ret_expr (ret);
7994 ret_expr.set_location (compound_loc);
7995 return ret_expr;
7998 case RID_NEW:
7999 return cp_parser_new_expression (parser);
8001 case RID_DELETE:
8002 return cp_parser_delete_expression (parser);
8004 case RID_EXTENSION:
8006 /* The saved value of the PEDANTIC flag. */
8007 int saved_pedantic;
8008 tree expr;
8010 /* Save away the PEDANTIC flag. */
8011 cp_parser_extension_opt (parser, &saved_pedantic);
8012 /* Parse the cast-expression. */
8013 expr = cp_parser_simple_cast_expression (parser);
8014 /* Restore the PEDANTIC flag. */
8015 pedantic = saved_pedantic;
8017 return expr;
8020 case RID_REALPART:
8021 case RID_IMAGPART:
8023 tree expression;
8025 /* Consume the `__real__' or `__imag__' token. */
8026 cp_lexer_consume_token (parser->lexer);
8027 /* Parse the cast-expression. */
8028 expression = cp_parser_simple_cast_expression (parser);
8029 /* Create the complete representation. */
8030 return build_x_unary_op (token->location,
8031 (keyword == RID_REALPART
8032 ? REALPART_EXPR : IMAGPART_EXPR),
8033 expression,
8034 tf_warning_or_error);
8036 break;
8038 case RID_TRANSACTION_ATOMIC:
8039 case RID_TRANSACTION_RELAXED:
8040 return cp_parser_transaction_expression (parser, keyword);
8042 case RID_NOEXCEPT:
8044 tree expr;
8045 const char *saved_message;
8046 bool saved_integral_constant_expression_p;
8047 bool saved_non_integral_constant_expression_p;
8048 bool saved_greater_than_is_operator_p;
8050 location_t start_loc = token->location;
8052 cp_lexer_consume_token (parser->lexer);
8053 matching_parens parens;
8054 parens.require_open (parser);
8056 saved_message = parser->type_definition_forbidden_message;
8057 parser->type_definition_forbidden_message
8058 = G_("types may not be defined in %<noexcept%> expressions");
8060 saved_integral_constant_expression_p
8061 = parser->integral_constant_expression_p;
8062 saved_non_integral_constant_expression_p
8063 = parser->non_integral_constant_expression_p;
8064 parser->integral_constant_expression_p = false;
8066 saved_greater_than_is_operator_p
8067 = parser->greater_than_is_operator_p;
8068 parser->greater_than_is_operator_p = true;
8070 ++cp_unevaluated_operand;
8071 ++c_inhibit_evaluation_warnings;
8072 ++cp_noexcept_operand;
8073 expr = cp_parser_expression (parser);
8074 --cp_noexcept_operand;
8075 --c_inhibit_evaluation_warnings;
8076 --cp_unevaluated_operand;
8078 parser->greater_than_is_operator_p
8079 = saved_greater_than_is_operator_p;
8081 parser->integral_constant_expression_p
8082 = saved_integral_constant_expression_p;
8083 parser->non_integral_constant_expression_p
8084 = saved_non_integral_constant_expression_p;
8086 parser->type_definition_forbidden_message = saved_message;
8088 location_t finish_loc
8089 = cp_lexer_peek_token (parser->lexer)->location;
8090 parens.require_close (parser);
8092 /* Construct a location of the form:
8093 noexcept (expr)
8094 ^~~~~~~~~~~~~~~
8095 with start == caret, finishing at the close-paren. */
8096 location_t noexcept_loc
8097 = make_location (start_loc, start_loc, finish_loc);
8099 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8100 noexcept_loc);
8103 default:
8104 break;
8108 /* Look for the `:: new' and `:: delete', which also signal the
8109 beginning of a new-expression, or delete-expression,
8110 respectively. If the next token is `::', then it might be one of
8111 these. */
8112 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8114 enum rid keyword;
8116 /* See if the token after the `::' is one of the keywords in
8117 which we're interested. */
8118 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8119 /* If it's `new', we have a new-expression. */
8120 if (keyword == RID_NEW)
8121 return cp_parser_new_expression (parser);
8122 /* Similarly, for `delete'. */
8123 else if (keyword == RID_DELETE)
8124 return cp_parser_delete_expression (parser);
8127 /* Look for a unary operator. */
8128 unary_operator = cp_parser_unary_operator (token);
8129 /* The `++' and `--' operators can be handled similarly, even though
8130 they are not technically unary-operators in the grammar. */
8131 if (unary_operator == ERROR_MARK)
8133 if (token->type == CPP_PLUS_PLUS)
8134 unary_operator = PREINCREMENT_EXPR;
8135 else if (token->type == CPP_MINUS_MINUS)
8136 unary_operator = PREDECREMENT_EXPR;
8137 /* Handle the GNU address-of-label extension. */
8138 else if (cp_parser_allow_gnu_extensions_p (parser)
8139 && token->type == CPP_AND_AND)
8141 tree identifier;
8142 tree expression;
8143 location_t start_loc = token->location;
8145 /* Consume the '&&' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Look for the identifier. */
8148 location_t finish_loc
8149 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8150 identifier = cp_parser_identifier (parser);
8151 /* Construct a location of the form:
8152 &&label
8153 ^~~~~~~
8154 with caret==start at the "&&", finish at the end of the label. */
8155 location_t combined_loc
8156 = make_location (start_loc, start_loc, finish_loc);
8157 /* Create an expression representing the address. */
8158 expression = finish_label_address_expr (identifier, combined_loc);
8159 if (cp_parser_non_integral_constant_expression (parser,
8160 NIC_ADDR_LABEL))
8161 expression = error_mark_node;
8162 return expression;
8165 if (unary_operator != ERROR_MARK)
8167 cp_expr cast_expression;
8168 cp_expr expression = error_mark_node;
8169 non_integral_constant non_constant_p = NIC_NONE;
8170 location_t loc = token->location;
8171 tsubst_flags_t complain = complain_flags (decltype_p);
8173 /* Consume the operator token. */
8174 token = cp_lexer_consume_token (parser->lexer);
8175 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8177 /* Parse the cast-expression. */
8178 cast_expression
8179 = cp_parser_cast_expression (parser,
8180 unary_operator == ADDR_EXPR,
8181 /*cast_p=*/false,
8182 /*decltype*/false,
8183 pidk);
8185 /* Make a location:
8186 OP_TOKEN CAST_EXPRESSION
8187 ^~~~~~~~~~~~~~~~~~~~~~~~~
8188 with start==caret at the operator token, and
8189 extending to the end of the cast_expression. */
8190 loc = make_location (loc, loc, cast_expression.get_finish ());
8192 /* Now, build an appropriate representation. */
8193 switch (unary_operator)
8195 case INDIRECT_REF:
8196 non_constant_p = NIC_STAR;
8197 expression = build_x_indirect_ref (loc, cast_expression,
8198 RO_UNARY_STAR,
8199 complain);
8200 /* TODO: build_x_indirect_ref does not always honor the
8201 location, so ensure it is set. */
8202 expression.set_location (loc);
8203 break;
8205 case ADDR_EXPR:
8206 non_constant_p = NIC_ADDR;
8207 /* Fall through. */
8208 case BIT_NOT_EXPR:
8209 expression = build_x_unary_op (loc, unary_operator,
8210 cast_expression,
8211 complain);
8212 /* TODO: build_x_unary_op does not always honor the location,
8213 so ensure it is set. */
8214 expression.set_location (loc);
8215 break;
8217 case PREINCREMENT_EXPR:
8218 case PREDECREMENT_EXPR:
8219 non_constant_p = unary_operator == PREINCREMENT_EXPR
8220 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8221 /* Fall through. */
8222 case NEGATE_EXPR:
8223 /* Immediately fold negation of a constant, unless the constant is 0
8224 (since -0 == 0) or it would overflow. */
8225 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8226 && CONSTANT_CLASS_P (cast_expression)
8227 && !integer_zerop (cast_expression)
8228 && !TREE_OVERFLOW (cast_expression))
8230 tree folded = fold_build1 (unary_operator,
8231 TREE_TYPE (cast_expression),
8232 cast_expression);
8233 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8235 expression = cp_expr (folded, loc);
8236 break;
8239 /* Fall through. */
8240 case UNARY_PLUS_EXPR:
8241 case TRUTH_NOT_EXPR:
8242 expression = finish_unary_op_expr (loc, unary_operator,
8243 cast_expression, complain);
8244 break;
8246 default:
8247 gcc_unreachable ();
8250 if (non_constant_p != NIC_NONE
8251 && cp_parser_non_integral_constant_expression (parser,
8252 non_constant_p))
8253 expression = error_mark_node;
8255 return expression;
8258 return cp_parser_postfix_expression (parser, address_p, cast_p,
8259 /*member_access_only_p=*/false,
8260 decltype_p,
8261 pidk);
8264 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8265 unary-operator, the corresponding tree code is returned. */
8267 static enum tree_code
8268 cp_parser_unary_operator (cp_token* token)
8270 switch (token->type)
8272 case CPP_MULT:
8273 return INDIRECT_REF;
8275 case CPP_AND:
8276 return ADDR_EXPR;
8278 case CPP_PLUS:
8279 return UNARY_PLUS_EXPR;
8281 case CPP_MINUS:
8282 return NEGATE_EXPR;
8284 case CPP_NOT:
8285 return TRUTH_NOT_EXPR;
8287 case CPP_COMPL:
8288 return BIT_NOT_EXPR;
8290 default:
8291 return ERROR_MARK;
8295 /* Parse a new-expression.
8297 new-expression:
8298 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8299 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8301 Returns a representation of the expression. */
8303 static tree
8304 cp_parser_new_expression (cp_parser* parser)
8306 bool global_scope_p;
8307 vec<tree, va_gc> *placement;
8308 tree type;
8309 vec<tree, va_gc> *initializer;
8310 tree nelts = NULL_TREE;
8311 tree ret;
8313 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8315 /* Look for the optional `::' operator. */
8316 global_scope_p
8317 = (cp_parser_global_scope_opt (parser,
8318 /*current_scope_valid_p=*/false)
8319 != NULL_TREE);
8320 /* Look for the `new' operator. */
8321 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8322 /* There's no easy way to tell a new-placement from the
8323 `( type-id )' construct. */
8324 cp_parser_parse_tentatively (parser);
8325 /* Look for a new-placement. */
8326 placement = cp_parser_new_placement (parser);
8327 /* If that didn't work out, there's no new-placement. */
8328 if (!cp_parser_parse_definitely (parser))
8330 if (placement != NULL)
8331 release_tree_vector (placement);
8332 placement = NULL;
8335 /* If the next token is a `(', then we have a parenthesized
8336 type-id. */
8337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8339 cp_token *token;
8340 const char *saved_message = parser->type_definition_forbidden_message;
8342 /* Consume the `('. */
8343 matching_parens parens;
8344 parens.consume_open (parser);
8346 /* Parse the type-id. */
8347 parser->type_definition_forbidden_message
8348 = G_("types may not be defined in a new-expression");
8350 type_id_in_expr_sentinel s (parser);
8351 type = cp_parser_type_id (parser);
8353 parser->type_definition_forbidden_message = saved_message;
8355 /* Look for the closing `)'. */
8356 parens.require_close (parser);
8357 token = cp_lexer_peek_token (parser->lexer);
8358 /* There should not be a direct-new-declarator in this production,
8359 but GCC used to allowed this, so we check and emit a sensible error
8360 message for this case. */
8361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8363 error_at (token->location,
8364 "array bound forbidden after parenthesized type-id");
8365 inform (token->location,
8366 "try removing the parentheses around the type-id");
8367 cp_parser_direct_new_declarator (parser);
8370 /* Otherwise, there must be a new-type-id. */
8371 else
8372 type = cp_parser_new_type_id (parser, &nelts);
8374 /* If the next token is a `(' or '{', then we have a new-initializer. */
8375 cp_token *token = cp_lexer_peek_token (parser->lexer);
8376 if (token->type == CPP_OPEN_PAREN
8377 || token->type == CPP_OPEN_BRACE)
8378 initializer = cp_parser_new_initializer (parser);
8379 else
8380 initializer = NULL;
8382 /* A new-expression may not appear in an integral constant
8383 expression. */
8384 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8385 ret = error_mark_node;
8386 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8387 of a new-type-id or type-id of a new-expression, the new-expression shall
8388 contain a new-initializer of the form ( assignment-expression )".
8389 Additionally, consistently with the spirit of DR 1467, we want to accept
8390 'new auto { 2 }' too. */
8391 else if ((ret = type_uses_auto (type))
8392 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8393 && (vec_safe_length (initializer) != 1
8394 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8395 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8397 error_at (token->location,
8398 "initialization of new-expression for type %<auto%> "
8399 "requires exactly one element");
8400 ret = error_mark_node;
8402 else
8404 /* Construct a location e.g.:
8405 ptr = new int[100]
8406 ^~~~~~~~~~~~
8407 with caret == start at the start of the "new" token, and the end
8408 at the end of the final token we consumed. */
8409 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8410 location_t end_loc = get_finish (end_tok->location);
8411 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8413 /* Create a representation of the new-expression. */
8414 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8415 tf_warning_or_error);
8416 protected_set_expr_location (ret, combined_loc);
8419 if (placement != NULL)
8420 release_tree_vector (placement);
8421 if (initializer != NULL)
8422 release_tree_vector (initializer);
8424 return ret;
8427 /* Parse a new-placement.
8429 new-placement:
8430 ( expression-list )
8432 Returns the same representation as for an expression-list. */
8434 static vec<tree, va_gc> *
8435 cp_parser_new_placement (cp_parser* parser)
8437 vec<tree, va_gc> *expression_list;
8439 /* Parse the expression-list. */
8440 expression_list = (cp_parser_parenthesized_expression_list
8441 (parser, non_attr, /*cast_p=*/false,
8442 /*allow_expansion_p=*/true,
8443 /*non_constant_p=*/NULL));
8445 if (expression_list && expression_list->is_empty ())
8446 error ("expected expression-list or type-id");
8448 return expression_list;
8451 /* Parse a new-type-id.
8453 new-type-id:
8454 type-specifier-seq new-declarator [opt]
8456 Returns the TYPE allocated. If the new-type-id indicates an array
8457 type, *NELTS is set to the number of elements in the last array
8458 bound; the TYPE will not include the last array bound. */
8460 static tree
8461 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8463 cp_decl_specifier_seq type_specifier_seq;
8464 cp_declarator *new_declarator;
8465 cp_declarator *declarator;
8466 cp_declarator *outer_declarator;
8467 const char *saved_message;
8469 /* The type-specifier sequence must not contain type definitions.
8470 (It cannot contain declarations of new types either, but if they
8471 are not definitions we will catch that because they are not
8472 complete.) */
8473 saved_message = parser->type_definition_forbidden_message;
8474 parser->type_definition_forbidden_message
8475 = G_("types may not be defined in a new-type-id");
8476 /* Parse the type-specifier-seq. */
8477 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8478 /*is_trailing_return=*/false,
8479 &type_specifier_seq);
8480 /* Restore the old message. */
8481 parser->type_definition_forbidden_message = saved_message;
8483 if (type_specifier_seq.type == error_mark_node)
8484 return error_mark_node;
8486 /* Parse the new-declarator. */
8487 new_declarator = cp_parser_new_declarator_opt (parser);
8489 /* Determine the number of elements in the last array dimension, if
8490 any. */
8491 *nelts = NULL_TREE;
8492 /* Skip down to the last array dimension. */
8493 declarator = new_declarator;
8494 outer_declarator = NULL;
8495 while (declarator && (declarator->kind == cdk_pointer
8496 || declarator->kind == cdk_ptrmem))
8498 outer_declarator = declarator;
8499 declarator = declarator->declarator;
8501 while (declarator
8502 && declarator->kind == cdk_array
8503 && declarator->declarator
8504 && declarator->declarator->kind == cdk_array)
8506 outer_declarator = declarator;
8507 declarator = declarator->declarator;
8510 if (declarator && declarator->kind == cdk_array)
8512 *nelts = declarator->u.array.bounds;
8513 if (*nelts == error_mark_node)
8514 *nelts = integer_one_node;
8516 if (outer_declarator)
8517 outer_declarator->declarator = declarator->declarator;
8518 else
8519 new_declarator = NULL;
8522 return groktypename (&type_specifier_seq, new_declarator, false);
8525 /* Parse an (optional) new-declarator.
8527 new-declarator:
8528 ptr-operator new-declarator [opt]
8529 direct-new-declarator
8531 Returns the declarator. */
8533 static cp_declarator *
8534 cp_parser_new_declarator_opt (cp_parser* parser)
8536 enum tree_code code;
8537 tree type, std_attributes = NULL_TREE;
8538 cp_cv_quals cv_quals;
8540 /* We don't know if there's a ptr-operator next, or not. */
8541 cp_parser_parse_tentatively (parser);
8542 /* Look for a ptr-operator. */
8543 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8544 /* If that worked, look for more new-declarators. */
8545 if (cp_parser_parse_definitely (parser))
8547 cp_declarator *declarator;
8549 /* Parse another optional declarator. */
8550 declarator = cp_parser_new_declarator_opt (parser);
8552 declarator = cp_parser_make_indirect_declarator
8553 (code, type, cv_quals, declarator, std_attributes);
8555 return declarator;
8558 /* If the next token is a `[', there is a direct-new-declarator. */
8559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8560 return cp_parser_direct_new_declarator (parser);
8562 return NULL;
8565 /* Parse a direct-new-declarator.
8567 direct-new-declarator:
8568 [ expression ]
8569 direct-new-declarator [constant-expression]
8573 static cp_declarator *
8574 cp_parser_direct_new_declarator (cp_parser* parser)
8576 cp_declarator *declarator = NULL;
8578 while (true)
8580 tree expression;
8581 cp_token *token;
8583 /* Look for the opening `['. */
8584 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8586 token = cp_lexer_peek_token (parser->lexer);
8587 expression = cp_parser_expression (parser);
8588 /* The standard requires that the expression have integral
8589 type. DR 74 adds enumeration types. We believe that the
8590 real intent is that these expressions be handled like the
8591 expression in a `switch' condition, which also allows
8592 classes with a single conversion to integral or
8593 enumeration type. */
8594 if (!processing_template_decl)
8596 expression
8597 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8598 expression,
8599 /*complain=*/true);
8600 if (!expression)
8602 error_at (token->location,
8603 "expression in new-declarator must have integral "
8604 "or enumeration type");
8605 expression = error_mark_node;
8609 /* Look for the closing `]'. */
8610 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8612 /* Add this bound to the declarator. */
8613 declarator = make_array_declarator (declarator, expression);
8615 /* If the next token is not a `[', then there are no more
8616 bounds. */
8617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8618 break;
8621 return declarator;
8624 /* Parse a new-initializer.
8626 new-initializer:
8627 ( expression-list [opt] )
8628 braced-init-list
8630 Returns a representation of the expression-list. */
8632 static vec<tree, va_gc> *
8633 cp_parser_new_initializer (cp_parser* parser)
8635 vec<tree, va_gc> *expression_list;
8637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8639 tree t;
8640 bool expr_non_constant_p;
8641 cp_lexer_set_source_position (parser->lexer);
8642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8643 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8644 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8645 expression_list = make_tree_vector_single (t);
8647 else
8648 expression_list = (cp_parser_parenthesized_expression_list
8649 (parser, non_attr, /*cast_p=*/false,
8650 /*allow_expansion_p=*/true,
8651 /*non_constant_p=*/NULL));
8653 return expression_list;
8656 /* Parse a delete-expression.
8658 delete-expression:
8659 :: [opt] delete cast-expression
8660 :: [opt] delete [ ] cast-expression
8662 Returns a representation of the expression. */
8664 static tree
8665 cp_parser_delete_expression (cp_parser* parser)
8667 bool global_scope_p;
8668 bool array_p;
8669 tree expression;
8671 /* Look for the optional `::' operator. */
8672 global_scope_p
8673 = (cp_parser_global_scope_opt (parser,
8674 /*current_scope_valid_p=*/false)
8675 != NULL_TREE);
8676 /* Look for the `delete' keyword. */
8677 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8678 /* See if the array syntax is in use. */
8679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8681 /* Consume the `[' token. */
8682 cp_lexer_consume_token (parser->lexer);
8683 /* Look for the `]' token. */
8684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8685 /* Remember that this is the `[]' construct. */
8686 array_p = true;
8688 else
8689 array_p = false;
8691 /* Parse the cast-expression. */
8692 expression = cp_parser_simple_cast_expression (parser);
8694 /* A delete-expression may not appear in an integral constant
8695 expression. */
8696 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8697 return error_mark_node;
8699 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8700 tf_warning_or_error);
8703 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8704 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8705 0 otherwise. */
8707 static int
8708 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8710 cp_token *token = cp_lexer_peek_token (parser->lexer);
8711 switch (token->type)
8713 case CPP_COMMA:
8714 case CPP_SEMICOLON:
8715 case CPP_QUERY:
8716 case CPP_COLON:
8717 case CPP_CLOSE_SQUARE:
8718 case CPP_CLOSE_PAREN:
8719 case CPP_CLOSE_BRACE:
8720 case CPP_OPEN_BRACE:
8721 case CPP_DOT:
8722 case CPP_DOT_STAR:
8723 case CPP_DEREF:
8724 case CPP_DEREF_STAR:
8725 case CPP_DIV:
8726 case CPP_MOD:
8727 case CPP_LSHIFT:
8728 case CPP_RSHIFT:
8729 case CPP_LESS:
8730 case CPP_GREATER:
8731 case CPP_LESS_EQ:
8732 case CPP_GREATER_EQ:
8733 case CPP_EQ_EQ:
8734 case CPP_NOT_EQ:
8735 case CPP_EQ:
8736 case CPP_MULT_EQ:
8737 case CPP_DIV_EQ:
8738 case CPP_MOD_EQ:
8739 case CPP_PLUS_EQ:
8740 case CPP_MINUS_EQ:
8741 case CPP_RSHIFT_EQ:
8742 case CPP_LSHIFT_EQ:
8743 case CPP_AND_EQ:
8744 case CPP_XOR_EQ:
8745 case CPP_OR_EQ:
8746 case CPP_XOR:
8747 case CPP_OR:
8748 case CPP_OR_OR:
8749 case CPP_EOF:
8750 case CPP_ELLIPSIS:
8751 return 0;
8753 case CPP_OPEN_PAREN:
8754 /* In ((type ()) () the last () isn't a valid cast-expression,
8755 so the whole must be parsed as postfix-expression. */
8756 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8757 != CPP_CLOSE_PAREN;
8759 case CPP_OPEN_SQUARE:
8760 /* '[' may start a primary-expression in obj-c++ and in C++11,
8761 as a lambda-expression, eg, '(void)[]{}'. */
8762 if (cxx_dialect >= cxx11)
8763 return -1;
8764 return c_dialect_objc ();
8766 case CPP_PLUS_PLUS:
8767 case CPP_MINUS_MINUS:
8768 /* '++' and '--' may or may not start a cast-expression:
8770 struct T { void operator++(int); };
8771 void f() { (T())++; }
8775 int a;
8776 (int)++a; */
8777 return -1;
8779 default:
8780 return 1;
8784 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8785 in the order: const_cast, static_cast, reinterpret_cast.
8787 Don't suggest dynamic_cast.
8789 Return the first legal cast kind found, or NULL otherwise. */
8791 static const char *
8792 get_cast_suggestion (tree dst_type, tree orig_expr)
8794 tree trial;
8796 /* Reuse the parser logic by attempting to build the various kinds of
8797 cast, with "complain" disabled.
8798 Identify the first such cast that is valid. */
8800 /* Don't attempt to run such logic within template processing. */
8801 if (processing_template_decl)
8802 return NULL;
8804 /* First try const_cast. */
8805 trial = build_const_cast (dst_type, orig_expr, tf_none);
8806 if (trial != error_mark_node)
8807 return "const_cast";
8809 /* If that fails, try static_cast. */
8810 trial = build_static_cast (dst_type, orig_expr, tf_none);
8811 if (trial != error_mark_node)
8812 return "static_cast";
8814 /* Finally, try reinterpret_cast. */
8815 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8816 if (trial != error_mark_node)
8817 return "reinterpret_cast";
8819 /* No such cast possible. */
8820 return NULL;
8823 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8824 suggesting how to convert a C-style cast of the form:
8826 (DST_TYPE)ORIG_EXPR
8828 to a C++-style cast.
8830 The primary range of RICHLOC is asssumed to be that of the original
8831 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8832 of the parens in the C-style cast. */
8834 static void
8835 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8836 location_t close_paren_loc, tree orig_expr,
8837 tree dst_type)
8839 /* This function is non-trivial, so bail out now if the warning isn't
8840 going to be emitted. */
8841 if (!warn_old_style_cast)
8842 return;
8844 /* Try to find a legal C++ cast, trying them in order:
8845 const_cast, static_cast, reinterpret_cast. */
8846 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8847 if (!cast_suggestion)
8848 return;
8850 /* Replace the open paren with "CAST_SUGGESTION<". */
8851 pretty_printer pp;
8852 pp_printf (&pp, "%s<", cast_suggestion);
8853 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8855 /* Replace the close paren with "> (". */
8856 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8858 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8859 rich_loc->add_fixit_insert_after (")");
8863 /* Parse a cast-expression.
8865 cast-expression:
8866 unary-expression
8867 ( type-id ) cast-expression
8869 ADDRESS_P is true iff the unary-expression is appearing as the
8870 operand of the `&' operator. CAST_P is true if this expression is
8871 the target of a cast.
8873 Returns a representation of the expression. */
8875 static cp_expr
8876 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8877 bool decltype_p, cp_id_kind * pidk)
8879 /* If it's a `(', then we might be looking at a cast. */
8880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8882 tree type = NULL_TREE;
8883 cp_expr expr (NULL_TREE);
8884 int cast_expression = 0;
8885 const char *saved_message;
8887 /* There's no way to know yet whether or not this is a cast.
8888 For example, `(int (3))' is a unary-expression, while `(int)
8889 3' is a cast. So, we resort to parsing tentatively. */
8890 cp_parser_parse_tentatively (parser);
8891 /* Types may not be defined in a cast. */
8892 saved_message = parser->type_definition_forbidden_message;
8893 parser->type_definition_forbidden_message
8894 = G_("types may not be defined in casts");
8895 /* Consume the `('. */
8896 matching_parens parens;
8897 cp_token *open_paren = parens.consume_open (parser);
8898 location_t open_paren_loc = open_paren->location;
8899 location_t close_paren_loc = UNKNOWN_LOCATION;
8901 /* A very tricky bit is that `(struct S) { 3 }' is a
8902 compound-literal (which we permit in C++ as an extension).
8903 But, that construct is not a cast-expression -- it is a
8904 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8905 is legal; if the compound-literal were a cast-expression,
8906 you'd need an extra set of parentheses.) But, if we parse
8907 the type-id, and it happens to be a class-specifier, then we
8908 will commit to the parse at that point, because we cannot
8909 undo the action that is done when creating a new class. So,
8910 then we cannot back up and do a postfix-expression.
8912 Another tricky case is the following (c++/29234):
8914 struct S { void operator () (); };
8916 void foo ()
8918 ( S()() );
8921 As a type-id we parse the parenthesized S()() as a function
8922 returning a function, groktypename complains and we cannot
8923 back up in this case either.
8925 Therefore, we scan ahead to the closing `)', and check to see
8926 if the tokens after the `)' can start a cast-expression. Otherwise
8927 we are dealing with an unary-expression, a postfix-expression
8928 or something else.
8930 Yet another tricky case, in C++11, is the following (c++/54891):
8932 (void)[]{};
8934 The issue is that usually, besides the case of lambda-expressions,
8935 the parenthesized type-id cannot be followed by '[', and, eg, we
8936 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8937 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8938 we don't commit, we try a cast-expression, then an unary-expression.
8940 Save tokens so that we can put them back. */
8941 cp_lexer_save_tokens (parser->lexer);
8943 /* We may be looking at a cast-expression. */
8944 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8945 /*consume_paren=*/true))
8946 cast_expression
8947 = cp_parser_tokens_start_cast_expression (parser);
8949 /* Roll back the tokens we skipped. */
8950 cp_lexer_rollback_tokens (parser->lexer);
8951 /* If we aren't looking at a cast-expression, simulate an error so
8952 that the call to cp_parser_error_occurred below returns true. */
8953 if (!cast_expression)
8954 cp_parser_simulate_error (parser);
8955 else
8957 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8958 parser->in_type_id_in_expr_p = true;
8959 /* Look for the type-id. */
8960 type = cp_parser_type_id (parser);
8961 /* Look for the closing `)'. */
8962 cp_token *close_paren = parens.require_close (parser);
8963 if (close_paren)
8964 close_paren_loc = close_paren->location;
8965 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8968 /* Restore the saved message. */
8969 parser->type_definition_forbidden_message = saved_message;
8971 /* At this point this can only be either a cast or a
8972 parenthesized ctor such as `(T ())' that looks like a cast to
8973 function returning T. */
8974 if (!cp_parser_error_occurred (parser))
8976 /* Only commit if the cast-expression doesn't start with
8977 '++', '--', or '[' in C++11. */
8978 if (cast_expression > 0)
8979 cp_parser_commit_to_topmost_tentative_parse (parser);
8981 expr = cp_parser_cast_expression (parser,
8982 /*address_p=*/false,
8983 /*cast_p=*/true,
8984 /*decltype_p=*/false,
8985 pidk);
8987 if (cp_parser_parse_definitely (parser))
8989 /* Warn about old-style casts, if so requested. */
8990 if (warn_old_style_cast
8991 && !in_system_header_at (input_location)
8992 && !VOID_TYPE_P (type)
8993 && current_lang_name != lang_name_c)
8995 gcc_rich_location rich_loc (input_location);
8996 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8997 expr, type);
8998 warning_at (&rich_loc, OPT_Wold_style_cast,
8999 "use of old-style cast to %q#T", type);
9002 /* Only type conversions to integral or enumeration types
9003 can be used in constant-expressions. */
9004 if (!cast_valid_in_integral_constant_expression_p (type)
9005 && cp_parser_non_integral_constant_expression (parser,
9006 NIC_CAST))
9007 return error_mark_node;
9009 /* Perform the cast. */
9010 /* Make a location:
9011 (TYPE) EXPR
9012 ^~~~~~~~~~~
9013 with start==caret at the open paren, extending to the
9014 end of "expr". */
9015 location_t cast_loc = make_location (open_paren_loc,
9016 open_paren_loc,
9017 expr.get_finish ());
9018 expr = build_c_cast (cast_loc, type, expr);
9019 return expr;
9022 else
9023 cp_parser_abort_tentative_parse (parser);
9026 /* If we get here, then it's not a cast, so it must be a
9027 unary-expression. */
9028 return cp_parser_unary_expression (parser, pidk, address_p,
9029 cast_p, decltype_p);
9032 /* Parse a binary expression of the general form:
9034 pm-expression:
9035 cast-expression
9036 pm-expression .* cast-expression
9037 pm-expression ->* cast-expression
9039 multiplicative-expression:
9040 pm-expression
9041 multiplicative-expression * pm-expression
9042 multiplicative-expression / pm-expression
9043 multiplicative-expression % pm-expression
9045 additive-expression:
9046 multiplicative-expression
9047 additive-expression + multiplicative-expression
9048 additive-expression - multiplicative-expression
9050 shift-expression:
9051 additive-expression
9052 shift-expression << additive-expression
9053 shift-expression >> additive-expression
9055 relational-expression:
9056 shift-expression
9057 relational-expression < shift-expression
9058 relational-expression > shift-expression
9059 relational-expression <= shift-expression
9060 relational-expression >= shift-expression
9062 GNU Extension:
9064 relational-expression:
9065 relational-expression <? shift-expression
9066 relational-expression >? shift-expression
9068 equality-expression:
9069 relational-expression
9070 equality-expression == relational-expression
9071 equality-expression != relational-expression
9073 and-expression:
9074 equality-expression
9075 and-expression & equality-expression
9077 exclusive-or-expression:
9078 and-expression
9079 exclusive-or-expression ^ and-expression
9081 inclusive-or-expression:
9082 exclusive-or-expression
9083 inclusive-or-expression | exclusive-or-expression
9085 logical-and-expression:
9086 inclusive-or-expression
9087 logical-and-expression && inclusive-or-expression
9089 logical-or-expression:
9090 logical-and-expression
9091 logical-or-expression || logical-and-expression
9093 All these are implemented with a single function like:
9095 binary-expression:
9096 simple-cast-expression
9097 binary-expression <token> binary-expression
9099 CAST_P is true if this expression is the target of a cast.
9101 The binops_by_token map is used to get the tree codes for each <token> type.
9102 binary-expressions are associated according to a precedence table. */
9104 #define TOKEN_PRECEDENCE(token) \
9105 (((token->type == CPP_GREATER \
9106 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9107 && !parser->greater_than_is_operator_p) \
9108 ? PREC_NOT_OPERATOR \
9109 : binops_by_token[token->type].prec)
9111 static cp_expr
9112 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9113 bool no_toplevel_fold_p,
9114 bool decltype_p,
9115 enum cp_parser_prec prec,
9116 cp_id_kind * pidk)
9118 cp_parser_expression_stack stack;
9119 cp_parser_expression_stack_entry *sp = &stack[0];
9120 cp_parser_expression_stack_entry current;
9121 cp_expr rhs;
9122 cp_token *token;
9123 enum tree_code rhs_type;
9124 enum cp_parser_prec new_prec, lookahead_prec;
9125 tree overload;
9127 /* Parse the first expression. */
9128 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9129 ? TRUTH_NOT_EXPR : ERROR_MARK);
9130 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9131 cast_p, decltype_p, pidk);
9132 current.prec = prec;
9134 if (cp_parser_error_occurred (parser))
9135 return error_mark_node;
9137 for (;;)
9139 /* Get an operator token. */
9140 token = cp_lexer_peek_token (parser->lexer);
9142 if (warn_cxx11_compat
9143 && token->type == CPP_RSHIFT
9144 && !parser->greater_than_is_operator_p)
9146 if (warning_at (token->location, OPT_Wc__11_compat,
9147 "%<>>%> operator is treated"
9148 " as two right angle brackets in C++11"))
9149 inform (token->location,
9150 "suggest parentheses around %<>>%> expression");
9153 new_prec = TOKEN_PRECEDENCE (token);
9154 if (new_prec != PREC_NOT_OPERATOR
9155 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9156 /* This is a fold-expression; handle it later. */
9157 new_prec = PREC_NOT_OPERATOR;
9159 /* Popping an entry off the stack means we completed a subexpression:
9160 - either we found a token which is not an operator (`>' where it is not
9161 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9162 will happen repeatedly;
9163 - or, we found an operator which has lower priority. This is the case
9164 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9165 parsing `3 * 4'. */
9166 if (new_prec <= current.prec)
9168 if (sp == stack)
9169 break;
9170 else
9171 goto pop;
9174 get_rhs:
9175 current.tree_type = binops_by_token[token->type].tree_type;
9176 current.loc = token->location;
9178 /* We used the operator token. */
9179 cp_lexer_consume_token (parser->lexer);
9181 /* For "false && x" or "true || x", x will never be executed;
9182 disable warnings while evaluating it. */
9183 if (current.tree_type == TRUTH_ANDIF_EXPR)
9184 c_inhibit_evaluation_warnings +=
9185 cp_fully_fold (current.lhs) == truthvalue_false_node;
9186 else if (current.tree_type == TRUTH_ORIF_EXPR)
9187 c_inhibit_evaluation_warnings +=
9188 cp_fully_fold (current.lhs) == truthvalue_true_node;
9190 /* Extract another operand. It may be the RHS of this expression
9191 or the LHS of a new, higher priority expression. */
9192 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9193 ? TRUTH_NOT_EXPR : ERROR_MARK);
9194 rhs = cp_parser_simple_cast_expression (parser);
9196 /* Get another operator token. Look up its precedence to avoid
9197 building a useless (immediately popped) stack entry for common
9198 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9199 token = cp_lexer_peek_token (parser->lexer);
9200 lookahead_prec = TOKEN_PRECEDENCE (token);
9201 if (lookahead_prec != PREC_NOT_OPERATOR
9202 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9203 lookahead_prec = PREC_NOT_OPERATOR;
9204 if (lookahead_prec > new_prec)
9206 /* ... and prepare to parse the RHS of the new, higher priority
9207 expression. Since precedence levels on the stack are
9208 monotonically increasing, we do not have to care about
9209 stack overflows. */
9210 *sp = current;
9211 ++sp;
9212 current.lhs = rhs;
9213 current.lhs_type = rhs_type;
9214 current.prec = new_prec;
9215 new_prec = lookahead_prec;
9216 goto get_rhs;
9218 pop:
9219 lookahead_prec = new_prec;
9220 /* If the stack is not empty, we have parsed into LHS the right side
9221 (`4' in the example above) of an expression we had suspended.
9222 We can use the information on the stack to recover the LHS (`3')
9223 from the stack together with the tree code (`MULT_EXPR'), and
9224 the precedence of the higher level subexpression
9225 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9226 which will be used to actually build the additive expression. */
9227 rhs = current.lhs;
9228 rhs_type = current.lhs_type;
9229 --sp;
9230 current = *sp;
9233 /* Undo the disabling of warnings done above. */
9234 if (current.tree_type == TRUTH_ANDIF_EXPR)
9235 c_inhibit_evaluation_warnings -=
9236 cp_fully_fold (current.lhs) == truthvalue_false_node;
9237 else if (current.tree_type == TRUTH_ORIF_EXPR)
9238 c_inhibit_evaluation_warnings -=
9239 cp_fully_fold (current.lhs) == truthvalue_true_node;
9241 if (warn_logical_not_paren
9242 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9243 && current.lhs_type == TRUTH_NOT_EXPR
9244 /* Avoid warning for !!x == y. */
9245 && (TREE_CODE (current.lhs) != NE_EXPR
9246 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9247 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9248 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9249 /* Avoid warning for !b == y where b is boolean. */
9250 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9251 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9252 != BOOLEAN_TYPE))))
9253 /* Avoid warning for !!b == y where b is boolean. */
9254 && (!DECL_P (current.lhs)
9255 || TREE_TYPE (current.lhs) == NULL_TREE
9256 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9257 warn_logical_not_parentheses (current.loc, current.tree_type,
9258 current.lhs, maybe_constant_value (rhs));
9260 overload = NULL;
9262 location_t combined_loc = make_location (current.loc,
9263 current.lhs.get_start (),
9264 rhs.get_finish ());
9266 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9267 ERROR_MARK for everything that is not a binary expression.
9268 This makes warn_about_parentheses miss some warnings that
9269 involve unary operators. For unary expressions we should
9270 pass the correct tree_code unless the unary expression was
9271 surrounded by parentheses.
9273 if (no_toplevel_fold_p
9274 && lookahead_prec <= current.prec
9275 && sp == stack)
9276 current.lhs = build2_loc (combined_loc,
9277 current.tree_type,
9278 TREE_CODE_CLASS (current.tree_type)
9279 == tcc_comparison
9280 ? boolean_type_node : TREE_TYPE (current.lhs),
9281 current.lhs, rhs);
9282 else
9284 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9285 current.lhs, current.lhs_type,
9286 rhs, rhs_type, &overload,
9287 complain_flags (decltype_p));
9288 /* TODO: build_x_binary_op doesn't always honor the location. */
9289 current.lhs.set_location (combined_loc);
9291 current.lhs_type = current.tree_type;
9293 /* If the binary operator required the use of an overloaded operator,
9294 then this expression cannot be an integral constant-expression.
9295 An overloaded operator can be used even if both operands are
9296 otherwise permissible in an integral constant-expression if at
9297 least one of the operands is of enumeration type. */
9299 if (overload
9300 && cp_parser_non_integral_constant_expression (parser,
9301 NIC_OVERLOADED))
9302 return error_mark_node;
9305 return current.lhs;
9308 static cp_expr
9309 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9310 bool no_toplevel_fold_p,
9311 enum cp_parser_prec prec,
9312 cp_id_kind * pidk)
9314 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9315 /*decltype*/false, prec, pidk);
9318 /* Parse the `? expression : assignment-expression' part of a
9319 conditional-expression. The LOGICAL_OR_EXPR is the
9320 logical-or-expression that started the conditional-expression.
9321 Returns a representation of the entire conditional-expression.
9323 This routine is used by cp_parser_assignment_expression.
9325 ? expression : assignment-expression
9327 GNU Extensions:
9329 ? : assignment-expression */
9331 static tree
9332 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9334 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9335 cp_expr assignment_expr;
9336 struct cp_token *token;
9337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9339 /* Consume the `?' token. */
9340 cp_lexer_consume_token (parser->lexer);
9341 token = cp_lexer_peek_token (parser->lexer);
9342 if (cp_parser_allow_gnu_extensions_p (parser)
9343 && token->type == CPP_COLON)
9345 pedwarn (token->location, OPT_Wpedantic,
9346 "ISO C++ does not allow ?: with omitted middle operand");
9347 /* Implicit true clause. */
9348 expr = NULL_TREE;
9349 c_inhibit_evaluation_warnings +=
9350 folded_logical_or_expr == truthvalue_true_node;
9351 warn_for_omitted_condop (token->location, logical_or_expr);
9353 else
9355 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9356 parser->colon_corrects_to_scope_p = false;
9357 /* Parse the expression. */
9358 c_inhibit_evaluation_warnings +=
9359 folded_logical_or_expr == truthvalue_false_node;
9360 expr = cp_parser_expression (parser);
9361 c_inhibit_evaluation_warnings +=
9362 ((folded_logical_or_expr == truthvalue_true_node)
9363 - (folded_logical_or_expr == truthvalue_false_node));
9364 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9367 /* The next token should be a `:'. */
9368 cp_parser_require (parser, CPP_COLON, RT_COLON);
9369 /* Parse the assignment-expression. */
9370 assignment_expr = cp_parser_assignment_expression (parser);
9371 c_inhibit_evaluation_warnings -=
9372 folded_logical_or_expr == truthvalue_true_node;
9374 /* Make a location:
9375 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9376 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9377 with the caret at the "?", ranging from the start of
9378 the logical_or_expr to the end of the assignment_expr. */
9379 loc = make_location (loc,
9380 logical_or_expr.get_start (),
9381 assignment_expr.get_finish ());
9383 /* Build the conditional-expression. */
9384 return build_x_conditional_expr (loc, logical_or_expr,
9385 expr,
9386 assignment_expr,
9387 tf_warning_or_error);
9390 /* Parse an assignment-expression.
9392 assignment-expression:
9393 conditional-expression
9394 logical-or-expression assignment-operator assignment_expression
9395 throw-expression
9397 CAST_P is true if this expression is the target of a cast.
9398 DECLTYPE_P is true if this expression is the operand of decltype.
9400 Returns a representation for the expression. */
9402 static cp_expr
9403 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9404 bool cast_p, bool decltype_p)
9406 cp_expr expr;
9408 /* If the next token is the `throw' keyword, then we're looking at
9409 a throw-expression. */
9410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9411 expr = cp_parser_throw_expression (parser);
9412 /* Otherwise, it must be that we are looking at a
9413 logical-or-expression. */
9414 else
9416 /* Parse the binary expressions (logical-or-expression). */
9417 expr = cp_parser_binary_expression (parser, cast_p, false,
9418 decltype_p,
9419 PREC_NOT_OPERATOR, pidk);
9420 /* If the next token is a `?' then we're actually looking at a
9421 conditional-expression. */
9422 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9423 return cp_parser_question_colon_clause (parser, expr);
9424 else
9426 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9428 /* If it's an assignment-operator, we're using the second
9429 production. */
9430 enum tree_code assignment_operator
9431 = cp_parser_assignment_operator_opt (parser);
9432 if (assignment_operator != ERROR_MARK)
9434 bool non_constant_p;
9436 /* Parse the right-hand side of the assignment. */
9437 cp_expr rhs = cp_parser_initializer_clause (parser,
9438 &non_constant_p);
9440 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9441 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9443 /* An assignment may not appear in a
9444 constant-expression. */
9445 if (cp_parser_non_integral_constant_expression (parser,
9446 NIC_ASSIGNMENT))
9447 return error_mark_node;
9448 /* Build the assignment expression. Its default
9449 location:
9450 LHS = RHS
9451 ~~~~^~~~~
9452 is the location of the '=' token as the
9453 caret, ranging from the start of the lhs to the
9454 end of the rhs. */
9455 loc = make_location (loc,
9456 expr.get_start (),
9457 rhs.get_finish ());
9458 expr = build_x_modify_expr (loc, expr,
9459 assignment_operator,
9460 rhs,
9461 complain_flags (decltype_p));
9462 /* TODO: build_x_modify_expr doesn't honor the location,
9463 so we must set it here. */
9464 expr.set_location (loc);
9469 return expr;
9472 /* Parse an (optional) assignment-operator.
9474 assignment-operator: one of
9475 = *= /= %= += -= >>= <<= &= ^= |=
9477 GNU Extension:
9479 assignment-operator: one of
9480 <?= >?=
9482 If the next token is an assignment operator, the corresponding tree
9483 code is returned, and the token is consumed. For example, for
9484 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9485 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9486 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9487 operator, ERROR_MARK is returned. */
9489 static enum tree_code
9490 cp_parser_assignment_operator_opt (cp_parser* parser)
9492 enum tree_code op;
9493 cp_token *token;
9495 /* Peek at the next token. */
9496 token = cp_lexer_peek_token (parser->lexer);
9498 switch (token->type)
9500 case CPP_EQ:
9501 op = NOP_EXPR;
9502 break;
9504 case CPP_MULT_EQ:
9505 op = MULT_EXPR;
9506 break;
9508 case CPP_DIV_EQ:
9509 op = TRUNC_DIV_EXPR;
9510 break;
9512 case CPP_MOD_EQ:
9513 op = TRUNC_MOD_EXPR;
9514 break;
9516 case CPP_PLUS_EQ:
9517 op = PLUS_EXPR;
9518 break;
9520 case CPP_MINUS_EQ:
9521 op = MINUS_EXPR;
9522 break;
9524 case CPP_RSHIFT_EQ:
9525 op = RSHIFT_EXPR;
9526 break;
9528 case CPP_LSHIFT_EQ:
9529 op = LSHIFT_EXPR;
9530 break;
9532 case CPP_AND_EQ:
9533 op = BIT_AND_EXPR;
9534 break;
9536 case CPP_XOR_EQ:
9537 op = BIT_XOR_EXPR;
9538 break;
9540 case CPP_OR_EQ:
9541 op = BIT_IOR_EXPR;
9542 break;
9544 default:
9545 /* Nothing else is an assignment operator. */
9546 op = ERROR_MARK;
9549 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9550 if (op != ERROR_MARK
9551 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9552 op = ERROR_MARK;
9554 /* If it was an assignment operator, consume it. */
9555 if (op != ERROR_MARK)
9556 cp_lexer_consume_token (parser->lexer);
9558 return op;
9561 /* Parse an expression.
9563 expression:
9564 assignment-expression
9565 expression , assignment-expression
9567 CAST_P is true if this expression is the target of a cast.
9568 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9569 except possibly parenthesized or on the RHS of a comma (N3276).
9571 Returns a representation of the expression. */
9573 static cp_expr
9574 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9575 bool cast_p, bool decltype_p)
9577 cp_expr expression = NULL_TREE;
9578 location_t loc = UNKNOWN_LOCATION;
9580 while (true)
9582 cp_expr assignment_expression;
9584 /* Parse the next assignment-expression. */
9585 assignment_expression
9586 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9588 /* We don't create a temporary for a call that is the immediate operand
9589 of decltype or on the RHS of a comma. But when we see a comma, we
9590 need to create a temporary for a call on the LHS. */
9591 if (decltype_p && !processing_template_decl
9592 && TREE_CODE (assignment_expression) == CALL_EXPR
9593 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9594 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9595 assignment_expression
9596 = build_cplus_new (TREE_TYPE (assignment_expression),
9597 assignment_expression, tf_warning_or_error);
9599 /* If this is the first assignment-expression, we can just
9600 save it away. */
9601 if (!expression)
9602 expression = assignment_expression;
9603 else
9605 /* Create a location with caret at the comma, ranging
9606 from the start of the LHS to the end of the RHS. */
9607 loc = make_location (loc,
9608 expression.get_start (),
9609 assignment_expression.get_finish ());
9610 expression = build_x_compound_expr (loc, expression,
9611 assignment_expression,
9612 complain_flags (decltype_p));
9613 expression.set_location (loc);
9615 /* If the next token is not a comma, or we're in a fold-expression, then
9616 we are done with the expression. */
9617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9618 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9619 break;
9620 /* Consume the `,'. */
9621 loc = cp_lexer_peek_token (parser->lexer)->location;
9622 cp_lexer_consume_token (parser->lexer);
9623 /* A comma operator cannot appear in a constant-expression. */
9624 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9625 expression = error_mark_node;
9628 return expression;
9631 /* Parse a constant-expression.
9633 constant-expression:
9634 conditional-expression
9636 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9637 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9638 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9639 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9640 only parse a conditional-expression, otherwise parse an
9641 assignment-expression. See below for rationale. */
9643 static cp_expr
9644 cp_parser_constant_expression (cp_parser* parser,
9645 bool allow_non_constant_p,
9646 bool *non_constant_p,
9647 bool strict_p)
9649 bool saved_integral_constant_expression_p;
9650 bool saved_allow_non_integral_constant_expression_p;
9651 bool saved_non_integral_constant_expression_p;
9652 cp_expr expression;
9654 /* It might seem that we could simply parse the
9655 conditional-expression, and then check to see if it were
9656 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9657 one that the compiler can figure out is constant, possibly after
9658 doing some simplifications or optimizations. The standard has a
9659 precise definition of constant-expression, and we must honor
9660 that, even though it is somewhat more restrictive.
9662 For example:
9664 int i[(2, 3)];
9666 is not a legal declaration, because `(2, 3)' is not a
9667 constant-expression. The `,' operator is forbidden in a
9668 constant-expression. However, GCC's constant-folding machinery
9669 will fold this operation to an INTEGER_CST for `3'. */
9671 /* Save the old settings. */
9672 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9673 saved_allow_non_integral_constant_expression_p
9674 = parser->allow_non_integral_constant_expression_p;
9675 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9676 /* We are now parsing a constant-expression. */
9677 parser->integral_constant_expression_p = true;
9678 parser->allow_non_integral_constant_expression_p
9679 = (allow_non_constant_p || cxx_dialect >= cxx11);
9680 parser->non_integral_constant_expression_p = false;
9681 /* Although the grammar says "conditional-expression", when not STRICT_P,
9682 we parse an "assignment-expression", which also permits
9683 "throw-expression" and the use of assignment operators. In the case
9684 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9685 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9686 actually essential that we look for an assignment-expression.
9687 For example, cp_parser_initializer_clauses uses this function to
9688 determine whether a particular assignment-expression is in fact
9689 constant. */
9690 if (strict_p)
9692 /* Parse the binary expressions (logical-or-expression). */
9693 expression = cp_parser_binary_expression (parser, false, false, false,
9694 PREC_NOT_OPERATOR, NULL);
9695 /* If the next token is a `?' then we're actually looking at
9696 a conditional-expression; otherwise we're done. */
9697 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9698 expression = cp_parser_question_colon_clause (parser, expression);
9700 else
9701 expression = cp_parser_assignment_expression (parser);
9702 /* Restore the old settings. */
9703 parser->integral_constant_expression_p
9704 = saved_integral_constant_expression_p;
9705 parser->allow_non_integral_constant_expression_p
9706 = saved_allow_non_integral_constant_expression_p;
9707 if (cxx_dialect >= cxx11)
9709 /* Require an rvalue constant expression here; that's what our
9710 callers expect. Reference constant expressions are handled
9711 separately in e.g. cp_parser_template_argument. */
9712 tree decay = expression;
9713 if (TREE_TYPE (expression)
9714 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9715 decay = build_address (expression);
9716 bool is_const = potential_rvalue_constant_expression (decay);
9717 parser->non_integral_constant_expression_p = !is_const;
9718 if (!is_const && !allow_non_constant_p)
9719 require_potential_rvalue_constant_expression (decay);
9721 if (allow_non_constant_p)
9722 *non_constant_p = parser->non_integral_constant_expression_p;
9723 parser->non_integral_constant_expression_p
9724 = saved_non_integral_constant_expression_p;
9726 return expression;
9729 /* Parse __builtin_offsetof.
9731 offsetof-expression:
9732 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9734 offsetof-member-designator:
9735 id-expression
9736 | offsetof-member-designator "." id-expression
9737 | offsetof-member-designator "[" expression "]"
9738 | offsetof-member-designator "->" id-expression */
9740 static cp_expr
9741 cp_parser_builtin_offsetof (cp_parser *parser)
9743 int save_ice_p, save_non_ice_p;
9744 tree type;
9745 cp_expr expr;
9746 cp_id_kind dummy;
9747 cp_token *token;
9748 location_t finish_loc;
9750 /* We're about to accept non-integral-constant things, but will
9751 definitely yield an integral constant expression. Save and
9752 restore these values around our local parsing. */
9753 save_ice_p = parser->integral_constant_expression_p;
9754 save_non_ice_p = parser->non_integral_constant_expression_p;
9756 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9758 /* Consume the "__builtin_offsetof" token. */
9759 cp_lexer_consume_token (parser->lexer);
9760 /* Consume the opening `('. */
9761 matching_parens parens;
9762 parens.require_open (parser);
9763 /* Parse the type-id. */
9764 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9765 type = cp_parser_type_id (parser);
9766 /* Look for the `,'. */
9767 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9768 token = cp_lexer_peek_token (parser->lexer);
9770 /* Build the (type *)null that begins the traditional offsetof macro. */
9771 tree object_ptr
9772 = build_static_cast (build_pointer_type (type), null_pointer_node,
9773 tf_warning_or_error);
9775 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9776 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9777 true, &dummy, token->location);
9778 while (true)
9780 token = cp_lexer_peek_token (parser->lexer);
9781 switch (token->type)
9783 case CPP_OPEN_SQUARE:
9784 /* offsetof-member-designator "[" expression "]" */
9785 expr = cp_parser_postfix_open_square_expression (parser, expr,
9786 true, false);
9787 break;
9789 case CPP_DEREF:
9790 /* offsetof-member-designator "->" identifier */
9791 expr = grok_array_decl (token->location, expr,
9792 integer_zero_node, false);
9793 /* FALLTHRU */
9795 case CPP_DOT:
9796 /* offsetof-member-designator "." identifier */
9797 cp_lexer_consume_token (parser->lexer);
9798 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9799 expr, true, &dummy,
9800 token->location);
9801 break;
9803 case CPP_CLOSE_PAREN:
9804 /* Consume the ")" token. */
9805 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9806 cp_lexer_consume_token (parser->lexer);
9807 goto success;
9809 default:
9810 /* Error. We know the following require will fail, but
9811 that gives the proper error message. */
9812 parens.require_close (parser);
9813 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9814 expr = error_mark_node;
9815 goto failure;
9819 success:
9820 /* Make a location of the form:
9821 __builtin_offsetof (struct s, f)
9822 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9823 with caret at the type-id, ranging from the start of the
9824 "_builtin_offsetof" token to the close paren. */
9825 loc = make_location (loc, start_loc, finish_loc);
9826 /* The result will be an INTEGER_CST, so we need to explicitly
9827 preserve the location. */
9828 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9830 failure:
9831 parser->integral_constant_expression_p = save_ice_p;
9832 parser->non_integral_constant_expression_p = save_non_ice_p;
9834 return expr;
9837 /* Parse a trait expression.
9839 Returns a representation of the expression, the underlying type
9840 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9842 static cp_expr
9843 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9845 cp_trait_kind kind;
9846 tree type1, type2 = NULL_TREE;
9847 bool binary = false;
9848 bool variadic = false;
9850 switch (keyword)
9852 case RID_HAS_NOTHROW_ASSIGN:
9853 kind = CPTK_HAS_NOTHROW_ASSIGN;
9854 break;
9855 case RID_HAS_NOTHROW_CONSTRUCTOR:
9856 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9857 break;
9858 case RID_HAS_NOTHROW_COPY:
9859 kind = CPTK_HAS_NOTHROW_COPY;
9860 break;
9861 case RID_HAS_TRIVIAL_ASSIGN:
9862 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9863 break;
9864 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9865 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9866 break;
9867 case RID_HAS_TRIVIAL_COPY:
9868 kind = CPTK_HAS_TRIVIAL_COPY;
9869 break;
9870 case RID_HAS_TRIVIAL_DESTRUCTOR:
9871 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9872 break;
9873 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9874 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9875 break;
9876 case RID_HAS_VIRTUAL_DESTRUCTOR:
9877 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9878 break;
9879 case RID_IS_ABSTRACT:
9880 kind = CPTK_IS_ABSTRACT;
9881 break;
9882 case RID_IS_AGGREGATE:
9883 kind = CPTK_IS_AGGREGATE;
9884 break;
9885 case RID_IS_BASE_OF:
9886 kind = CPTK_IS_BASE_OF;
9887 binary = true;
9888 break;
9889 case RID_IS_CLASS:
9890 kind = CPTK_IS_CLASS;
9891 break;
9892 case RID_IS_EMPTY:
9893 kind = CPTK_IS_EMPTY;
9894 break;
9895 case RID_IS_ENUM:
9896 kind = CPTK_IS_ENUM;
9897 break;
9898 case RID_IS_FINAL:
9899 kind = CPTK_IS_FINAL;
9900 break;
9901 case RID_IS_LITERAL_TYPE:
9902 kind = CPTK_IS_LITERAL_TYPE;
9903 break;
9904 case RID_IS_POD:
9905 kind = CPTK_IS_POD;
9906 break;
9907 case RID_IS_POLYMORPHIC:
9908 kind = CPTK_IS_POLYMORPHIC;
9909 break;
9910 case RID_IS_SAME_AS:
9911 kind = CPTK_IS_SAME_AS;
9912 binary = true;
9913 break;
9914 case RID_IS_STD_LAYOUT:
9915 kind = CPTK_IS_STD_LAYOUT;
9916 break;
9917 case RID_IS_TRIVIAL:
9918 kind = CPTK_IS_TRIVIAL;
9919 break;
9920 case RID_IS_TRIVIALLY_ASSIGNABLE:
9921 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9922 binary = true;
9923 break;
9924 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9925 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9926 variadic = true;
9927 break;
9928 case RID_IS_TRIVIALLY_COPYABLE:
9929 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9930 break;
9931 case RID_IS_UNION:
9932 kind = CPTK_IS_UNION;
9933 break;
9934 case RID_UNDERLYING_TYPE:
9935 kind = CPTK_UNDERLYING_TYPE;
9936 break;
9937 case RID_BASES:
9938 kind = CPTK_BASES;
9939 break;
9940 case RID_DIRECT_BASES:
9941 kind = CPTK_DIRECT_BASES;
9942 break;
9943 case RID_IS_ASSIGNABLE:
9944 kind = CPTK_IS_ASSIGNABLE;
9945 binary = true;
9946 break;
9947 case RID_IS_CONSTRUCTIBLE:
9948 kind = CPTK_IS_CONSTRUCTIBLE;
9949 variadic = true;
9950 break;
9951 default:
9952 gcc_unreachable ();
9955 /* Get location of initial token. */
9956 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9958 /* Consume the token. */
9959 cp_lexer_consume_token (parser->lexer);
9961 matching_parens parens;
9962 parens.require_open (parser);
9965 type_id_in_expr_sentinel s (parser);
9966 type1 = cp_parser_type_id (parser);
9969 if (type1 == error_mark_node)
9970 return error_mark_node;
9972 if (binary)
9974 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9977 type_id_in_expr_sentinel s (parser);
9978 type2 = cp_parser_type_id (parser);
9981 if (type2 == error_mark_node)
9982 return error_mark_node;
9984 else if (variadic)
9986 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9988 cp_lexer_consume_token (parser->lexer);
9989 tree elt = cp_parser_type_id (parser);
9990 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9992 cp_lexer_consume_token (parser->lexer);
9993 elt = make_pack_expansion (elt);
9995 if (elt == error_mark_node)
9996 return error_mark_node;
9997 type2 = tree_cons (NULL_TREE, elt, type2);
10001 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10002 parens.require_close (parser);
10004 /* Construct a location of the form:
10005 __is_trivially_copyable(_Tp)
10006 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10007 with start == caret, finishing at the close-paren. */
10008 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10010 /* Complete the trait expression, which may mean either processing
10011 the trait expr now or saving it for template instantiation. */
10012 switch (kind)
10014 case CPTK_UNDERLYING_TYPE:
10015 return cp_expr (finish_underlying_type (type1), trait_loc);
10016 case CPTK_BASES:
10017 return cp_expr (finish_bases (type1, false), trait_loc);
10018 case CPTK_DIRECT_BASES:
10019 return cp_expr (finish_bases (type1, true), trait_loc);
10020 default:
10021 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10025 /* Parse a lambda expression.
10027 lambda-expression:
10028 lambda-introducer lambda-declarator [opt] compound-statement
10030 Returns a representation of the expression. */
10032 static cp_expr
10033 cp_parser_lambda_expression (cp_parser* parser)
10035 tree lambda_expr = build_lambda_expr ();
10036 tree type;
10037 bool ok = true;
10038 cp_token *token = cp_lexer_peek_token (parser->lexer);
10039 cp_token_position start = 0;
10041 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10043 if (cp_unevaluated_operand)
10045 if (!token->error_reported)
10047 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10048 "lambda-expression in unevaluated context");
10049 token->error_reported = true;
10051 ok = false;
10053 else if (parser->in_template_argument_list_p)
10055 if (!token->error_reported)
10057 error_at (token->location, "lambda-expression in template-argument");
10058 token->error_reported = true;
10060 ok = false;
10063 /* We may be in the middle of deferred access check. Disable
10064 it now. */
10065 push_deferring_access_checks (dk_no_deferred);
10067 cp_parser_lambda_introducer (parser, lambda_expr);
10069 type = begin_lambda_type (lambda_expr);
10070 if (type == error_mark_node)
10071 return error_mark_node;
10073 record_lambda_scope (lambda_expr);
10075 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10076 determine_visibility (TYPE_NAME (type));
10078 /* Now that we've started the type, add the capture fields for any
10079 explicit captures. */
10080 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10083 /* Inside the class, surrounding template-parameter-lists do not apply. */
10084 unsigned int saved_num_template_parameter_lists
10085 = parser->num_template_parameter_lists;
10086 unsigned char in_statement = parser->in_statement;
10087 bool in_switch_statement_p = parser->in_switch_statement_p;
10088 bool fully_implicit_function_template_p
10089 = parser->fully_implicit_function_template_p;
10090 tree implicit_template_parms = parser->implicit_template_parms;
10091 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10092 bool auto_is_implicit_function_template_parm_p
10093 = parser->auto_is_implicit_function_template_parm_p;
10095 parser->num_template_parameter_lists = 0;
10096 parser->in_statement = 0;
10097 parser->in_switch_statement_p = false;
10098 parser->fully_implicit_function_template_p = false;
10099 parser->implicit_template_parms = 0;
10100 parser->implicit_template_scope = 0;
10101 parser->auto_is_implicit_function_template_parm_p = false;
10103 /* By virtue of defining a local class, a lambda expression has access to
10104 the private variables of enclosing classes. */
10106 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10108 if (ok && cp_parser_error_occurred (parser))
10109 ok = false;
10111 if (ok)
10113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10114 && cp_parser_start_tentative_firewall (parser))
10115 start = token;
10116 cp_parser_lambda_body (parser, lambda_expr);
10118 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10120 if (cp_parser_skip_to_closing_brace (parser))
10121 cp_lexer_consume_token (parser->lexer);
10124 /* The capture list was built up in reverse order; fix that now. */
10125 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10126 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10128 if (ok)
10129 maybe_add_lambda_conv_op (type);
10131 type = finish_struct (type, /*attributes=*/NULL_TREE);
10133 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10134 parser->in_statement = in_statement;
10135 parser->in_switch_statement_p = in_switch_statement_p;
10136 parser->fully_implicit_function_template_p
10137 = fully_implicit_function_template_p;
10138 parser->implicit_template_parms = implicit_template_parms;
10139 parser->implicit_template_scope = implicit_template_scope;
10140 parser->auto_is_implicit_function_template_parm_p
10141 = auto_is_implicit_function_template_parm_p;
10144 /* This field is only used during parsing of the lambda. */
10145 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10147 /* This lambda shouldn't have any proxies left at this point. */
10148 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10149 /* And now that we're done, push proxies for an enclosing lambda. */
10150 insert_pending_capture_proxies ();
10152 if (ok)
10153 lambda_expr = build_lambda_object (lambda_expr);
10154 else
10155 lambda_expr = error_mark_node;
10157 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10159 pop_deferring_access_checks ();
10161 return lambda_expr;
10164 /* Parse the beginning of a lambda expression.
10166 lambda-introducer:
10167 [ lambda-capture [opt] ]
10169 LAMBDA_EXPR is the current representation of the lambda expression. */
10171 static void
10172 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10174 /* Need commas after the first capture. */
10175 bool first = true;
10177 /* Eat the leading `['. */
10178 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10180 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10181 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10182 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10183 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10184 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10185 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10187 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10189 cp_lexer_consume_token (parser->lexer);
10190 first = false;
10193 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10195 cp_token* capture_token;
10196 tree capture_id;
10197 tree capture_init_expr;
10198 cp_id_kind idk = CP_ID_KIND_NONE;
10199 bool explicit_init_p = false;
10201 enum capture_kind_type
10203 BY_COPY,
10204 BY_REFERENCE
10206 enum capture_kind_type capture_kind = BY_COPY;
10208 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10210 error ("expected end of capture-list");
10211 return;
10214 if (first)
10215 first = false;
10216 else
10217 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10219 /* Possibly capture `this'. */
10220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10222 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10223 if (cxx_dialect < cxx2a
10224 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10225 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10226 "with by-copy capture default");
10227 cp_lexer_consume_token (parser->lexer);
10228 add_capture (lambda_expr,
10229 /*id=*/this_identifier,
10230 /*initializer=*/finish_this_expr (),
10231 /*by_reference_p=*/true,
10232 explicit_init_p);
10233 continue;
10236 /* Possibly capture `*this'. */
10237 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10238 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10241 if (cxx_dialect < cxx17)
10242 pedwarn (loc, 0, "%<*this%> capture only available with "
10243 "-std=c++17 or -std=gnu++17");
10244 cp_lexer_consume_token (parser->lexer);
10245 cp_lexer_consume_token (parser->lexer);
10246 add_capture (lambda_expr,
10247 /*id=*/this_identifier,
10248 /*initializer=*/finish_this_expr (),
10249 /*by_reference_p=*/false,
10250 explicit_init_p);
10251 continue;
10254 /* Remember whether we want to capture as a reference or not. */
10255 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10257 capture_kind = BY_REFERENCE;
10258 cp_lexer_consume_token (parser->lexer);
10261 /* Get the identifier. */
10262 capture_token = cp_lexer_peek_token (parser->lexer);
10263 capture_id = cp_parser_identifier (parser);
10265 if (capture_id == error_mark_node)
10266 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10267 delimiters, but I modified this to stop on unnested ']' as well. It
10268 was already changed to stop on unnested '}', so the
10269 "closing_parenthesis" name is no more misleading with my change. */
10271 cp_parser_skip_to_closing_parenthesis (parser,
10272 /*recovering=*/true,
10273 /*or_comma=*/true,
10274 /*consume_paren=*/true);
10275 break;
10278 /* Find the initializer for this capture. */
10279 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10280 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10281 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10283 bool direct, non_constant;
10284 /* An explicit initializer exists. */
10285 if (cxx_dialect < cxx14)
10286 pedwarn (input_location, 0,
10287 "lambda capture initializers "
10288 "only available with -std=c++14 or -std=gnu++14");
10289 capture_init_expr = cp_parser_initializer (parser, &direct,
10290 &non_constant);
10291 explicit_init_p = true;
10292 if (capture_init_expr == NULL_TREE)
10294 error ("empty initializer for lambda init-capture");
10295 capture_init_expr = error_mark_node;
10298 else
10300 const char* error_msg;
10302 /* Turn the identifier into an id-expression. */
10303 capture_init_expr
10304 = cp_parser_lookup_name_simple (parser, capture_id,
10305 capture_token->location);
10307 if (capture_init_expr == error_mark_node)
10309 unqualified_name_lookup_error (capture_id);
10310 continue;
10312 else if (DECL_P (capture_init_expr)
10313 && (!VAR_P (capture_init_expr)
10314 && TREE_CODE (capture_init_expr) != PARM_DECL))
10316 error_at (capture_token->location,
10317 "capture of non-variable %qD ",
10318 capture_init_expr);
10319 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10320 "%q#D declared here", capture_init_expr);
10321 continue;
10323 if (VAR_P (capture_init_expr)
10324 && decl_storage_duration (capture_init_expr) != dk_auto)
10326 if (pedwarn (capture_token->location, 0, "capture of variable "
10327 "%qD with non-automatic storage duration",
10328 capture_init_expr))
10329 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10330 "%q#D declared here", capture_init_expr);
10331 continue;
10334 capture_init_expr
10335 = finish_id_expression
10336 (capture_id,
10337 capture_init_expr,
10338 parser->scope,
10339 &idk,
10340 /*integral_constant_expression_p=*/false,
10341 /*allow_non_integral_constant_expression_p=*/false,
10342 /*non_integral_constant_expression_p=*/NULL,
10343 /*template_p=*/false,
10344 /*done=*/true,
10345 /*address_p=*/false,
10346 /*template_arg_p=*/false,
10347 &error_msg,
10348 capture_token->location);
10350 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10352 cp_lexer_consume_token (parser->lexer);
10353 capture_init_expr = make_pack_expansion (capture_init_expr);
10355 else
10356 check_for_bare_parameter_packs (capture_init_expr);
10359 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10360 && !explicit_init_p)
10362 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10363 && capture_kind == BY_COPY)
10364 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10365 "of %qD redundant with by-copy capture default",
10366 capture_id);
10367 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10368 && capture_kind == BY_REFERENCE)
10369 pedwarn (capture_token->location, 0, "explicit by-reference "
10370 "capture of %qD redundant with by-reference capture "
10371 "default", capture_id);
10374 add_capture (lambda_expr,
10375 capture_id,
10376 capture_init_expr,
10377 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10378 explicit_init_p);
10381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10384 /* Parse the (optional) middle of a lambda expression.
10386 lambda-declarator:
10387 < template-parameter-list [opt] >
10388 ( parameter-declaration-clause [opt] )
10389 attribute-specifier [opt]
10390 decl-specifier-seq [opt]
10391 exception-specification [opt]
10392 lambda-return-type-clause [opt]
10394 LAMBDA_EXPR is the current representation of the lambda expression. */
10396 static bool
10397 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10399 /* 5.1.1.4 of the standard says:
10400 If a lambda-expression does not include a lambda-declarator, it is as if
10401 the lambda-declarator were ().
10402 This means an empty parameter list, no attributes, and no exception
10403 specification. */
10404 tree param_list = void_list_node;
10405 tree attributes = NULL_TREE;
10406 tree exception_spec = NULL_TREE;
10407 tree template_param_list = NULL_TREE;
10408 tree tx_qual = NULL_TREE;
10409 tree return_type = NULL_TREE;
10410 cp_decl_specifier_seq lambda_specs;
10411 clear_decl_specs (&lambda_specs);
10413 /* The template-parameter-list is optional, but must begin with
10414 an opening angle if present. */
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10417 if (cxx_dialect < cxx14)
10418 pedwarn (parser->lexer->next_token->location, 0,
10419 "lambda templates are only available with "
10420 "-std=c++14 or -std=gnu++14");
10421 else if (cxx_dialect < cxx2a)
10422 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10423 "lambda templates are only available with "
10424 "-std=c++2a or -std=gnu++2a");
10426 cp_lexer_consume_token (parser->lexer);
10428 template_param_list = cp_parser_template_parameter_list (parser);
10430 cp_parser_skip_to_end_of_template_parameter_list (parser);
10432 /* We just processed one more parameter list. */
10433 ++parser->num_template_parameter_lists;
10436 /* The parameter-declaration-clause is optional (unless
10437 template-parameter-list was given), but must begin with an
10438 opening parenthesis if present. */
10439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10441 matching_parens parens;
10442 parens.consume_open (parser);
10444 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10446 /* Parse parameters. */
10447 param_list = cp_parser_parameter_declaration_clause (parser);
10449 /* Default arguments shall not be specified in the
10450 parameter-declaration-clause of a lambda-declarator. */
10451 if (cxx_dialect < cxx14)
10452 for (tree t = param_list; t; t = TREE_CHAIN (t))
10453 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10454 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10455 "default argument specified for lambda parameter");
10457 parens.require_close (parser);
10459 attributes = cp_parser_attributes_opt (parser);
10461 /* In the decl-specifier-seq of the lambda-declarator, each
10462 decl-specifier shall either be mutable or constexpr. */
10463 int declares_class_or_enum;
10464 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10465 cp_parser_decl_specifier_seq (parser,
10466 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10467 &lambda_specs, &declares_class_or_enum);
10468 if (lambda_specs.storage_class == sc_mutable)
10470 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10471 if (lambda_specs.conflicting_specifiers_p)
10472 error_at (lambda_specs.locations[ds_storage_class],
10473 "duplicate %<mutable%>");
10476 tx_qual = cp_parser_tx_qualifier_opt (parser);
10478 /* Parse optional exception specification. */
10479 exception_spec = cp_parser_exception_specification_opt (parser);
10481 /* Parse optional trailing return type. */
10482 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10484 cp_lexer_consume_token (parser->lexer);
10485 return_type = cp_parser_trailing_type_id (parser);
10488 /* The function parameters must be in scope all the way until after the
10489 trailing-return-type in case of decltype. */
10490 pop_bindings_and_leave_scope ();
10492 else if (template_param_list != NULL_TREE) // generate diagnostic
10493 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10495 /* Create the function call operator.
10497 Messing with declarators like this is no uglier than building up the
10498 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10499 other code. */
10501 cp_decl_specifier_seq return_type_specs;
10502 cp_declarator* declarator;
10503 tree fco;
10504 int quals;
10505 void *p;
10507 clear_decl_specs (&return_type_specs);
10508 if (return_type)
10509 return_type_specs.type = return_type;
10510 else
10511 /* Maybe we will deduce the return type later. */
10512 return_type_specs.type = make_auto ();
10514 if (lambda_specs.locations[ds_constexpr])
10516 if (cxx_dialect >= cxx17)
10517 return_type_specs.locations[ds_constexpr]
10518 = lambda_specs.locations[ds_constexpr];
10519 else
10520 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10521 "lambda only available with -std=c++17 or -std=gnu++17");
10524 p = obstack_alloc (&declarator_obstack, 0);
10526 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10528 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10529 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10530 declarator = make_call_declarator (declarator, param_list, quals,
10531 VIRT_SPEC_UNSPECIFIED,
10532 REF_QUAL_NONE,
10533 tx_qual,
10534 exception_spec,
10535 /*late_return_type=*/NULL_TREE,
10536 /*requires_clause*/NULL_TREE);
10537 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10539 fco = grokmethod (&return_type_specs,
10540 declarator,
10541 attributes);
10542 if (fco != error_mark_node)
10544 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10545 DECL_ARTIFICIAL (fco) = 1;
10546 /* Give the object parameter a different name. */
10547 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10548 if (return_type)
10549 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10551 if (template_param_list)
10553 fco = finish_member_template_decl (fco);
10554 finish_template_decl (template_param_list);
10555 --parser->num_template_parameter_lists;
10557 else if (parser->fully_implicit_function_template_p)
10558 fco = finish_fully_implicit_template (parser, fco);
10560 finish_member_declaration (fco);
10562 obstack_free (&declarator_obstack, p);
10564 return (fco != error_mark_node);
10568 /* Parse the body of a lambda expression, which is simply
10570 compound-statement
10572 but which requires special handling.
10573 LAMBDA_EXPR is the current representation of the lambda expression. */
10575 static void
10576 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10578 bool nested = (current_function_decl != NULL_TREE);
10579 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10580 bool in_function_body = parser->in_function_body;
10582 if (nested)
10583 push_function_context ();
10584 else
10585 /* Still increment function_depth so that we don't GC in the
10586 middle of an expression. */
10587 ++function_depth;
10589 vec<tree> omp_privatization_save;
10590 save_omp_privatization_clauses (omp_privatization_save);
10591 /* Clear this in case we're in the middle of a default argument. */
10592 parser->local_variables_forbidden_p = false;
10593 parser->in_function_body = true;
10596 local_specialization_stack s (lss_copy);
10597 tree fco = lambda_function (lambda_expr);
10598 tree body = start_lambda_function (fco, lambda_expr);
10599 matching_braces braces;
10601 if (braces.require_open (parser))
10603 tree compound_stmt = begin_compound_stmt (0);
10605 /* Originally C++11 required us to peek for 'return expr'; and
10606 process it specially here to deduce the return type. N3638
10607 removed the need for that. */
10609 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10610 cp_parser_label_declaration (parser);
10611 cp_parser_statement_seq_opt (parser, NULL_TREE);
10612 braces.require_close (parser);
10614 finish_compound_stmt (compound_stmt);
10617 finish_lambda_function (body);
10620 restore_omp_privatization_clauses (omp_privatization_save);
10621 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10622 parser->in_function_body = in_function_body;
10623 if (nested)
10624 pop_function_context();
10625 else
10626 --function_depth;
10629 /* Statements [gram.stmt.stmt] */
10631 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10633 static void
10634 add_debug_begin_stmt (location_t loc)
10636 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10637 return;
10639 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10640 SET_EXPR_LOCATION (stmt, loc);
10641 add_stmt (stmt);
10644 /* Parse a statement.
10646 statement:
10647 labeled-statement
10648 expression-statement
10649 compound-statement
10650 selection-statement
10651 iteration-statement
10652 jump-statement
10653 declaration-statement
10654 try-block
10656 C++11:
10658 statement:
10659 labeled-statement
10660 attribute-specifier-seq (opt) expression-statement
10661 attribute-specifier-seq (opt) compound-statement
10662 attribute-specifier-seq (opt) selection-statement
10663 attribute-specifier-seq (opt) iteration-statement
10664 attribute-specifier-seq (opt) jump-statement
10665 declaration-statement
10666 attribute-specifier-seq (opt) try-block
10668 init-statement:
10669 expression-statement
10670 simple-declaration
10672 TM Extension:
10674 statement:
10675 atomic-statement
10677 IN_COMPOUND is true when the statement is nested inside a
10678 cp_parser_compound_statement; this matters for certain pragmas.
10680 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10681 is a (possibly labeled) if statement which is not enclosed in braces
10682 and has an else clause. This is used to implement -Wparentheses.
10684 CHAIN is a vector of if-else-if conditions. */
10686 static void
10687 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10688 bool in_compound, bool *if_p, vec<tree> *chain,
10689 location_t *loc_after_labels)
10691 tree statement, std_attrs = NULL_TREE;
10692 cp_token *token;
10693 location_t statement_location, attrs_location;
10695 restart:
10696 if (if_p != NULL)
10697 *if_p = false;
10698 /* There is no statement yet. */
10699 statement = NULL_TREE;
10701 saved_token_sentinel saved_tokens (parser->lexer);
10702 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10703 if (c_dialect_objc ())
10704 /* In obj-c++, seeing '[[' might be the either the beginning of
10705 c++11 attributes, or a nested objc-message-expression. So
10706 let's parse the c++11 attributes tentatively. */
10707 cp_parser_parse_tentatively (parser);
10708 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10709 if (c_dialect_objc ())
10711 if (!cp_parser_parse_definitely (parser))
10712 std_attrs = NULL_TREE;
10715 /* Peek at the next token. */
10716 token = cp_lexer_peek_token (parser->lexer);
10717 /* Remember the location of the first token in the statement. */
10718 statement_location = token->location;
10719 add_debug_begin_stmt (statement_location);
10720 /* If this is a keyword, then that will often determine what kind of
10721 statement we have. */
10722 if (token->type == CPP_KEYWORD)
10724 enum rid keyword = token->keyword;
10726 switch (keyword)
10728 case RID_CASE:
10729 case RID_DEFAULT:
10730 /* Looks like a labeled-statement with a case label.
10731 Parse the label, and then use tail recursion to parse
10732 the statement. */
10733 cp_parser_label_for_labeled_statement (parser, std_attrs);
10734 in_compound = false;
10735 goto restart;
10737 case RID_IF:
10738 case RID_SWITCH:
10739 statement = cp_parser_selection_statement (parser, if_p, chain);
10740 break;
10742 case RID_WHILE:
10743 case RID_DO:
10744 case RID_FOR:
10745 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10746 break;
10748 case RID_BREAK:
10749 case RID_CONTINUE:
10750 case RID_RETURN:
10751 case RID_GOTO:
10752 statement = cp_parser_jump_statement (parser);
10753 break;
10755 /* Objective-C++ exception-handling constructs. */
10756 case RID_AT_TRY:
10757 case RID_AT_CATCH:
10758 case RID_AT_FINALLY:
10759 case RID_AT_SYNCHRONIZED:
10760 case RID_AT_THROW:
10761 statement = cp_parser_objc_statement (parser);
10762 break;
10764 case RID_TRY:
10765 statement = cp_parser_try_block (parser);
10766 break;
10768 case RID_NAMESPACE:
10769 /* This must be a namespace alias definition. */
10770 cp_parser_declaration_statement (parser);
10771 return;
10773 case RID_TRANSACTION_ATOMIC:
10774 case RID_TRANSACTION_RELAXED:
10775 case RID_SYNCHRONIZED:
10776 case RID_ATOMIC_NOEXCEPT:
10777 case RID_ATOMIC_CANCEL:
10778 statement = cp_parser_transaction (parser, token);
10779 break;
10780 case RID_TRANSACTION_CANCEL:
10781 statement = cp_parser_transaction_cancel (parser);
10782 break;
10784 default:
10785 /* It might be a keyword like `int' that can start a
10786 declaration-statement. */
10787 break;
10790 else if (token->type == CPP_NAME)
10792 /* If the next token is a `:', then we are looking at a
10793 labeled-statement. */
10794 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10795 if (token->type == CPP_COLON)
10797 /* Looks like a labeled-statement with an ordinary label.
10798 Parse the label, and then use tail recursion to parse
10799 the statement. */
10801 cp_parser_label_for_labeled_statement (parser, std_attrs);
10802 in_compound = false;
10803 goto restart;
10806 /* Anything that starts with a `{' must be a compound-statement. */
10807 else if (token->type == CPP_OPEN_BRACE)
10808 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10809 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10810 a statement all its own. */
10811 else if (token->type == CPP_PRAGMA)
10813 /* Only certain OpenMP pragmas are attached to statements, and thus
10814 are considered statements themselves. All others are not. In
10815 the context of a compound, accept the pragma as a "statement" and
10816 return so that we can check for a close brace. Otherwise we
10817 require a real statement and must go back and read one. */
10818 if (in_compound)
10819 cp_parser_pragma (parser, pragma_compound, if_p);
10820 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10821 goto restart;
10822 return;
10824 else if (token->type == CPP_EOF)
10826 cp_parser_error (parser, "expected statement");
10827 return;
10830 /* Everything else must be a declaration-statement or an
10831 expression-statement. Try for the declaration-statement
10832 first, unless we are looking at a `;', in which case we know that
10833 we have an expression-statement. */
10834 if (!statement)
10836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10838 if (std_attrs != NULL_TREE)
10840 /* Attributes should be parsed as part of the the
10841 declaration, so let's un-parse them. */
10842 saved_tokens.rollback();
10843 std_attrs = NULL_TREE;
10846 cp_parser_parse_tentatively (parser);
10847 /* Try to parse the declaration-statement. */
10848 cp_parser_declaration_statement (parser);
10849 /* If that worked, we're done. */
10850 if (cp_parser_parse_definitely (parser))
10851 return;
10853 /* All preceding labels have been parsed at this point. */
10854 if (loc_after_labels != NULL)
10855 *loc_after_labels = statement_location;
10857 /* Look for an expression-statement instead. */
10858 statement = cp_parser_expression_statement (parser, in_statement_expr);
10860 /* Handle [[fallthrough]];. */
10861 if (attribute_fallthrough_p (std_attrs))
10863 /* The next token after the fallthrough attribute is ';'. */
10864 if (statement == NULL_TREE)
10866 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10867 statement = build_call_expr_internal_loc (statement_location,
10868 IFN_FALLTHROUGH,
10869 void_type_node, 0);
10870 finish_expr_stmt (statement);
10872 else
10873 warning_at (statement_location, OPT_Wattributes,
10874 "%<fallthrough%> attribute not followed by %<;%>");
10875 std_attrs = NULL_TREE;
10879 /* Set the line number for the statement. */
10880 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10881 SET_EXPR_LOCATION (statement, statement_location);
10883 /* Allow "[[fallthrough]];", but warn otherwise. */
10884 if (std_attrs != NULL_TREE)
10885 warning_at (attrs_location,
10886 OPT_Wattributes,
10887 "attributes at the beginning of statement are ignored");
10890 /* Parse the label for a labeled-statement, i.e.
10892 identifier :
10893 case constant-expression :
10894 default :
10896 GNU Extension:
10897 case constant-expression ... constant-expression : statement
10899 When a label is parsed without errors, the label is added to the
10900 parse tree by the finish_* functions, so this function doesn't
10901 have to return the label. */
10903 static void
10904 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10906 cp_token *token;
10907 tree label = NULL_TREE;
10908 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10910 /* The next token should be an identifier. */
10911 token = cp_lexer_peek_token (parser->lexer);
10912 if (token->type != CPP_NAME
10913 && token->type != CPP_KEYWORD)
10915 cp_parser_error (parser, "expected labeled-statement");
10916 return;
10919 /* Remember whether this case or a user-defined label is allowed to fall
10920 through to. */
10921 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10923 parser->colon_corrects_to_scope_p = false;
10924 switch (token->keyword)
10926 case RID_CASE:
10928 tree expr, expr_hi;
10929 cp_token *ellipsis;
10931 /* Consume the `case' token. */
10932 cp_lexer_consume_token (parser->lexer);
10933 /* Parse the constant-expression. */
10934 expr = cp_parser_constant_expression (parser);
10935 if (check_for_bare_parameter_packs (expr))
10936 expr = error_mark_node;
10938 ellipsis = cp_lexer_peek_token (parser->lexer);
10939 if (ellipsis->type == CPP_ELLIPSIS)
10941 /* Consume the `...' token. */
10942 cp_lexer_consume_token (parser->lexer);
10943 expr_hi = cp_parser_constant_expression (parser);
10944 if (check_for_bare_parameter_packs (expr_hi))
10945 expr_hi = error_mark_node;
10947 /* We don't need to emit warnings here, as the common code
10948 will do this for us. */
10950 else
10951 expr_hi = NULL_TREE;
10953 if (parser->in_switch_statement_p)
10955 tree l = finish_case_label (token->location, expr, expr_hi);
10956 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10957 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10959 else
10960 error_at (token->location,
10961 "case label %qE not within a switch statement",
10962 expr);
10964 break;
10966 case RID_DEFAULT:
10967 /* Consume the `default' token. */
10968 cp_lexer_consume_token (parser->lexer);
10970 if (parser->in_switch_statement_p)
10972 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10973 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10974 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10976 else
10977 error_at (token->location, "case label not within a switch statement");
10978 break;
10980 default:
10981 /* Anything else must be an ordinary label. */
10982 label = finish_label_stmt (cp_parser_identifier (parser));
10983 if (label && TREE_CODE (label) == LABEL_DECL)
10984 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10985 break;
10988 /* Require the `:' token. */
10989 cp_parser_require (parser, CPP_COLON, RT_COLON);
10991 /* An ordinary label may optionally be followed by attributes.
10992 However, this is only permitted if the attributes are then
10993 followed by a semicolon. This is because, for backward
10994 compatibility, when parsing
10995 lab: __attribute__ ((unused)) int i;
10996 we want the attribute to attach to "i", not "lab". */
10997 if (label != NULL_TREE
10998 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11000 tree attrs;
11001 cp_parser_parse_tentatively (parser);
11002 attrs = cp_parser_gnu_attributes_opt (parser);
11003 if (attrs == NULL_TREE
11004 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11005 cp_parser_abort_tentative_parse (parser);
11006 else if (!cp_parser_parse_definitely (parser))
11008 else
11009 attributes = chainon (attributes, attrs);
11012 if (attributes != NULL_TREE)
11013 cplus_decl_attributes (&label, attributes, 0);
11015 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11018 /* Parse an expression-statement.
11020 expression-statement:
11021 expression [opt] ;
11023 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11024 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11025 indicates whether this expression-statement is part of an
11026 expression statement. */
11028 static tree
11029 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11031 tree statement = NULL_TREE;
11032 cp_token *token = cp_lexer_peek_token (parser->lexer);
11033 location_t loc = token->location;
11035 /* There might be attribute fallthrough. */
11036 tree attr = cp_parser_gnu_attributes_opt (parser);
11038 /* If the next token is a ';', then there is no expression
11039 statement. */
11040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11042 statement = cp_parser_expression (parser);
11043 if (statement == error_mark_node
11044 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11046 cp_parser_skip_to_end_of_block_or_statement (parser);
11047 return error_mark_node;
11051 /* Handle [[fallthrough]];. */
11052 if (attribute_fallthrough_p (attr))
11054 /* The next token after the fallthrough attribute is ';'. */
11055 if (statement == NULL_TREE)
11056 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11057 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11058 void_type_node, 0);
11059 else
11060 warning_at (loc, OPT_Wattributes,
11061 "%<fallthrough%> attribute not followed by %<;%>");
11062 attr = NULL_TREE;
11065 /* Allow "[[fallthrough]];", but warn otherwise. */
11066 if (attr != NULL_TREE)
11067 warning_at (loc, OPT_Wattributes,
11068 "attributes at the beginning of statement are ignored");
11070 /* Give a helpful message for "A<T>::type t;" and the like. */
11071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11072 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11074 if (TREE_CODE (statement) == SCOPE_REF)
11075 error_at (token->location, "need %<typename%> before %qE because "
11076 "%qT is a dependent scope",
11077 statement, TREE_OPERAND (statement, 0));
11078 else if (is_overloaded_fn (statement)
11079 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11081 /* A::A a; */
11082 tree fn = get_first_fn (statement);
11083 error_at (token->location,
11084 "%<%T::%D%> names the constructor, not the type",
11085 DECL_CONTEXT (fn), DECL_NAME (fn));
11089 /* Consume the final `;'. */
11090 cp_parser_consume_semicolon_at_end_of_statement (parser);
11092 if (in_statement_expr
11093 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11094 /* This is the final expression statement of a statement
11095 expression. */
11096 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11097 else if (statement)
11098 statement = finish_expr_stmt (statement);
11100 return statement;
11103 /* Parse a compound-statement.
11105 compound-statement:
11106 { statement-seq [opt] }
11108 GNU extension:
11110 compound-statement:
11111 { label-declaration-seq [opt] statement-seq [opt] }
11113 label-declaration-seq:
11114 label-declaration
11115 label-declaration-seq label-declaration
11117 Returns a tree representing the statement. */
11119 static tree
11120 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11121 int bcs_flags, bool function_body)
11123 tree compound_stmt;
11124 matching_braces braces;
11126 /* Consume the `{'. */
11127 if (!braces.require_open (parser))
11128 return error_mark_node;
11129 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11130 && !function_body && cxx_dialect < cxx14)
11131 pedwarn (input_location, OPT_Wpedantic,
11132 "compound-statement in %<constexpr%> function");
11133 /* Begin the compound-statement. */
11134 compound_stmt = begin_compound_stmt (bcs_flags);
11135 /* If the next keyword is `__label__' we have a label declaration. */
11136 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11137 cp_parser_label_declaration (parser);
11138 /* Parse an (optional) statement-seq. */
11139 cp_parser_statement_seq_opt (parser, in_statement_expr);
11140 /* Finish the compound-statement. */
11141 finish_compound_stmt (compound_stmt);
11142 /* Consume the `}'. */
11143 braces.require_close (parser);
11145 return compound_stmt;
11148 /* Parse an (optional) statement-seq.
11150 statement-seq:
11151 statement
11152 statement-seq [opt] statement */
11154 static void
11155 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11157 /* Scan statements until there aren't any more. */
11158 while (true)
11160 cp_token *token = cp_lexer_peek_token (parser->lexer);
11162 /* If we are looking at a `}', then we have run out of
11163 statements; the same is true if we have reached the end
11164 of file, or have stumbled upon a stray '@end'. */
11165 if (token->type == CPP_CLOSE_BRACE
11166 || token->type == CPP_EOF
11167 || token->type == CPP_PRAGMA_EOL
11168 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11169 break;
11171 /* If we are in a compound statement and find 'else' then
11172 something went wrong. */
11173 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11175 if (parser->in_statement & IN_IF_STMT)
11176 break;
11177 else
11179 token = cp_lexer_consume_token (parser->lexer);
11180 error_at (token->location, "%<else%> without a previous %<if%>");
11184 /* Parse the statement. */
11185 cp_parser_statement (parser, in_statement_expr, true, NULL);
11189 /* Return true if we're looking at (init; cond), false otherwise. */
11191 static bool
11192 cp_parser_init_statement_p (cp_parser *parser)
11194 /* Save tokens so that we can put them back. */
11195 cp_lexer_save_tokens (parser->lexer);
11197 /* Look for ';' that is not nested in () or {}. */
11198 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11199 /*recovering=*/false,
11200 CPP_SEMICOLON,
11201 /*consume_paren=*/false);
11203 /* Roll back the tokens we skipped. */
11204 cp_lexer_rollback_tokens (parser->lexer);
11206 return ret == -1;
11209 /* Parse a selection-statement.
11211 selection-statement:
11212 if ( init-statement [opt] condition ) statement
11213 if ( init-statement [opt] condition ) statement else statement
11214 switch ( init-statement [opt] condition ) statement
11216 Returns the new IF_STMT or SWITCH_STMT.
11218 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11219 is a (possibly labeled) if statement which is not enclosed in
11220 braces and has an else clause. This is used to implement
11221 -Wparentheses.
11223 CHAIN is a vector of if-else-if conditions. This is used to implement
11224 -Wduplicated-cond. */
11226 static tree
11227 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11228 vec<tree> *chain)
11230 cp_token *token;
11231 enum rid keyword;
11232 token_indent_info guard_tinfo;
11234 if (if_p != NULL)
11235 *if_p = false;
11237 /* Peek at the next token. */
11238 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11239 guard_tinfo = get_token_indent_info (token);
11241 /* See what kind of keyword it is. */
11242 keyword = token->keyword;
11243 switch (keyword)
11245 case RID_IF:
11246 case RID_SWITCH:
11248 tree statement;
11249 tree condition;
11251 bool cx = false;
11252 if (keyword == RID_IF
11253 && cp_lexer_next_token_is_keyword (parser->lexer,
11254 RID_CONSTEXPR))
11256 cx = true;
11257 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11258 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11259 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11260 "with -std=c++17 or -std=gnu++17");
11263 /* Look for the `('. */
11264 matching_parens parens;
11265 if (!parens.require_open (parser))
11267 cp_parser_skip_to_end_of_statement (parser);
11268 return error_mark_node;
11271 /* Begin the selection-statement. */
11272 if (keyword == RID_IF)
11274 statement = begin_if_stmt ();
11275 IF_STMT_CONSTEXPR_P (statement) = cx;
11277 else
11278 statement = begin_switch_stmt ();
11280 /* Parse the optional init-statement. */
11281 if (cp_parser_init_statement_p (parser))
11283 tree decl;
11284 if (cxx_dialect < cxx17)
11285 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11286 "init-statement in selection statements only available "
11287 "with -std=c++17 or -std=gnu++17");
11288 cp_parser_init_statement (parser, &decl);
11291 /* Parse the condition. */
11292 condition = cp_parser_condition (parser);
11293 /* Look for the `)'. */
11294 if (!parens.require_close (parser))
11295 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11296 /*consume_paren=*/true);
11298 if (keyword == RID_IF)
11300 bool nested_if;
11301 unsigned char in_statement;
11303 /* Add the condition. */
11304 condition = finish_if_stmt_cond (condition, statement);
11306 if (warn_duplicated_cond)
11307 warn_duplicated_cond_add_or_warn (token->location, condition,
11308 &chain);
11310 /* Parse the then-clause. */
11311 in_statement = parser->in_statement;
11312 parser->in_statement |= IN_IF_STMT;
11314 /* Outside a template, the non-selected branch of a constexpr
11315 if is a 'discarded statement', i.e. unevaluated. */
11316 bool was_discarded = in_discarded_stmt;
11317 bool discard_then = (cx && !processing_template_decl
11318 && integer_zerop (condition));
11319 if (discard_then)
11321 in_discarded_stmt = true;
11322 ++c_inhibit_evaluation_warnings;
11325 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11326 guard_tinfo);
11328 parser->in_statement = in_statement;
11330 finish_then_clause (statement);
11332 if (discard_then)
11334 THEN_CLAUSE (statement) = NULL_TREE;
11335 in_discarded_stmt = was_discarded;
11336 --c_inhibit_evaluation_warnings;
11339 /* If the next token is `else', parse the else-clause. */
11340 if (cp_lexer_next_token_is_keyword (parser->lexer,
11341 RID_ELSE))
11343 bool discard_else = (cx && !processing_template_decl
11344 && integer_nonzerop (condition));
11345 if (discard_else)
11347 in_discarded_stmt = true;
11348 ++c_inhibit_evaluation_warnings;
11351 guard_tinfo
11352 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11353 /* Consume the `else' keyword. */
11354 cp_lexer_consume_token (parser->lexer);
11355 if (warn_duplicated_cond)
11357 if (cp_lexer_next_token_is_keyword (parser->lexer,
11358 RID_IF)
11359 && chain == NULL)
11361 /* We've got "if (COND) else if (COND2)". Start
11362 the condition chain and add COND as the first
11363 element. */
11364 chain = new vec<tree> ();
11365 if (!CONSTANT_CLASS_P (condition)
11366 && !TREE_SIDE_EFFECTS (condition))
11368 /* Wrap it in a NOP_EXPR so that we can set the
11369 location of the condition. */
11370 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11371 condition);
11372 SET_EXPR_LOCATION (e, token->location);
11373 chain->safe_push (e);
11376 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11377 RID_IF))
11379 /* This is if-else without subsequent if. Zap the
11380 condition chain; we would have already warned at
11381 this point. */
11382 delete chain;
11383 chain = NULL;
11386 begin_else_clause (statement);
11387 /* Parse the else-clause. */
11388 cp_parser_implicitly_scoped_statement (parser, NULL,
11389 guard_tinfo, chain);
11391 finish_else_clause (statement);
11393 /* If we are currently parsing a then-clause, then
11394 IF_P will not be NULL. We set it to true to
11395 indicate that this if statement has an else clause.
11396 This may trigger the Wparentheses warning below
11397 when we get back up to the parent if statement. */
11398 if (if_p != NULL)
11399 *if_p = true;
11401 if (discard_else)
11403 ELSE_CLAUSE (statement) = NULL_TREE;
11404 in_discarded_stmt = was_discarded;
11405 --c_inhibit_evaluation_warnings;
11408 else
11410 /* This if statement does not have an else clause. If
11411 NESTED_IF is true, then the then-clause has an if
11412 statement which does have an else clause. We warn
11413 about the potential ambiguity. */
11414 if (nested_if)
11415 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11416 "suggest explicit braces to avoid ambiguous"
11417 " %<else%>");
11418 if (warn_duplicated_cond)
11420 /* We don't need the condition chain anymore. */
11421 delete chain;
11422 chain = NULL;
11426 /* Now we're all done with the if-statement. */
11427 finish_if_stmt (statement);
11429 else
11431 bool in_switch_statement_p;
11432 unsigned char in_statement;
11434 /* Add the condition. */
11435 finish_switch_cond (condition, statement);
11437 /* Parse the body of the switch-statement. */
11438 in_switch_statement_p = parser->in_switch_statement_p;
11439 in_statement = parser->in_statement;
11440 parser->in_switch_statement_p = true;
11441 parser->in_statement |= IN_SWITCH_STMT;
11442 cp_parser_implicitly_scoped_statement (parser, if_p,
11443 guard_tinfo);
11444 parser->in_switch_statement_p = in_switch_statement_p;
11445 parser->in_statement = in_statement;
11447 /* Now we're all done with the switch-statement. */
11448 finish_switch_stmt (statement);
11451 return statement;
11453 break;
11455 default:
11456 cp_parser_error (parser, "expected selection-statement");
11457 return error_mark_node;
11461 /* Parse a condition.
11463 condition:
11464 expression
11465 type-specifier-seq declarator = initializer-clause
11466 type-specifier-seq declarator braced-init-list
11468 GNU Extension:
11470 condition:
11471 type-specifier-seq declarator asm-specification [opt]
11472 attributes [opt] = assignment-expression
11474 Returns the expression that should be tested. */
11476 static tree
11477 cp_parser_condition (cp_parser* parser)
11479 cp_decl_specifier_seq type_specifiers;
11480 const char *saved_message;
11481 int declares_class_or_enum;
11483 /* Try the declaration first. */
11484 cp_parser_parse_tentatively (parser);
11485 /* New types are not allowed in the type-specifier-seq for a
11486 condition. */
11487 saved_message = parser->type_definition_forbidden_message;
11488 parser->type_definition_forbidden_message
11489 = G_("types may not be defined in conditions");
11490 /* Parse the type-specifier-seq. */
11491 cp_parser_decl_specifier_seq (parser,
11492 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11493 &type_specifiers,
11494 &declares_class_or_enum);
11495 /* Restore the saved message. */
11496 parser->type_definition_forbidden_message = saved_message;
11497 /* If all is well, we might be looking at a declaration. */
11498 if (!cp_parser_error_occurred (parser))
11500 tree decl;
11501 tree asm_specification;
11502 tree attributes;
11503 cp_declarator *declarator;
11504 tree initializer = NULL_TREE;
11506 /* Parse the declarator. */
11507 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11508 /*ctor_dtor_or_conv_p=*/NULL,
11509 /*parenthesized_p=*/NULL,
11510 /*member_p=*/false,
11511 /*friend_p=*/false);
11512 /* Parse the attributes. */
11513 attributes = cp_parser_attributes_opt (parser);
11514 /* Parse the asm-specification. */
11515 asm_specification = cp_parser_asm_specification_opt (parser);
11516 /* If the next token is not an `=' or '{', then we might still be
11517 looking at an expression. For example:
11519 if (A(a).x)
11521 looks like a decl-specifier-seq and a declarator -- but then
11522 there is no `=', so this is an expression. */
11523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11524 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11525 cp_parser_simulate_error (parser);
11527 /* If we did see an `=' or '{', then we are looking at a declaration
11528 for sure. */
11529 if (cp_parser_parse_definitely (parser))
11531 tree pushed_scope;
11532 bool non_constant_p;
11533 int flags = LOOKUP_ONLYCONVERTING;
11535 /* Create the declaration. */
11536 decl = start_decl (declarator, &type_specifiers,
11537 /*initialized_p=*/true,
11538 attributes, /*prefix_attributes=*/NULL_TREE,
11539 &pushed_scope);
11541 /* Parse the initializer. */
11542 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11544 initializer = cp_parser_braced_list (parser, &non_constant_p);
11545 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11546 flags = 0;
11548 else
11550 /* Consume the `='. */
11551 cp_parser_require (parser, CPP_EQ, RT_EQ);
11552 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11554 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11555 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11557 /* Process the initializer. */
11558 cp_finish_decl (decl,
11559 initializer, !non_constant_p,
11560 asm_specification,
11561 flags);
11563 if (pushed_scope)
11564 pop_scope (pushed_scope);
11566 return convert_from_reference (decl);
11569 /* If we didn't even get past the declarator successfully, we are
11570 definitely not looking at a declaration. */
11571 else
11572 cp_parser_abort_tentative_parse (parser);
11574 /* Otherwise, we are looking at an expression. */
11575 return cp_parser_expression (parser);
11578 /* Parses a for-statement or range-for-statement until the closing ')',
11579 not included. */
11581 static tree
11582 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11584 tree init, scope, decl;
11585 bool is_range_for;
11587 /* Begin the for-statement. */
11588 scope = begin_for_scope (&init);
11590 /* Parse the initialization. */
11591 is_range_for = cp_parser_init_statement (parser, &decl);
11593 if (is_range_for)
11594 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11595 else
11596 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11599 static tree
11600 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11601 unsigned short unroll)
11603 /* Normal for loop */
11604 tree condition = NULL_TREE;
11605 tree expression = NULL_TREE;
11606 tree stmt;
11608 stmt = begin_for_stmt (scope, init);
11609 /* The init-statement has already been parsed in
11610 cp_parser_init_statement, so no work is needed here. */
11611 finish_init_stmt (stmt);
11613 /* If there's a condition, process it. */
11614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11615 condition = cp_parser_condition (parser);
11616 else if (ivdep)
11618 cp_parser_error (parser, "missing loop condition in loop with "
11619 "%<GCC ivdep%> pragma");
11620 condition = error_mark_node;
11622 else if (unroll)
11624 cp_parser_error (parser, "missing loop condition in loop with "
11625 "%<GCC unroll%> pragma");
11626 condition = error_mark_node;
11628 finish_for_cond (condition, stmt, ivdep, unroll);
11629 /* Look for the `;'. */
11630 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11632 /* If there's an expression, process it. */
11633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11634 expression = cp_parser_expression (parser);
11635 finish_for_expr (expression, stmt);
11637 return stmt;
11640 /* Tries to parse a range-based for-statement:
11642 range-based-for:
11643 decl-specifier-seq declarator : expression
11645 The decl-specifier-seq declarator and the `:' are already parsed by
11646 cp_parser_init_statement. If processing_template_decl it returns a
11647 newly created RANGE_FOR_STMT; if not, it is converted to a
11648 regular FOR_STMT. */
11650 static tree
11651 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11652 bool ivdep, unsigned short unroll)
11654 tree stmt, range_expr;
11655 auto_vec <cxx_binding *, 16> bindings;
11656 auto_vec <tree, 16> names;
11657 tree decomp_first_name = NULL_TREE;
11658 unsigned int decomp_cnt = 0;
11660 /* Get the range declaration momentarily out of the way so that
11661 the range expression doesn't clash with it. */
11662 if (range_decl != error_mark_node)
11664 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11666 tree v = DECL_VALUE_EXPR (range_decl);
11667 /* For decomposition declaration get all of the corresponding
11668 declarations out of the way. */
11669 if (TREE_CODE (v) == ARRAY_REF
11670 && VAR_P (TREE_OPERAND (v, 0))
11671 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11673 tree d = range_decl;
11674 range_decl = TREE_OPERAND (v, 0);
11675 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11676 decomp_first_name = d;
11677 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11679 tree name = DECL_NAME (d);
11680 names.safe_push (name);
11681 bindings.safe_push (IDENTIFIER_BINDING (name));
11682 IDENTIFIER_BINDING (name)
11683 = IDENTIFIER_BINDING (name)->previous;
11687 if (names.is_empty ())
11689 tree name = DECL_NAME (range_decl);
11690 names.safe_push (name);
11691 bindings.safe_push (IDENTIFIER_BINDING (name));
11692 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11698 bool expr_non_constant_p;
11699 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11701 else
11702 range_expr = cp_parser_expression (parser);
11704 /* Put the range declaration(s) back into scope. */
11705 for (unsigned int i = 0; i < names.length (); i++)
11707 cxx_binding *binding = bindings[i];
11708 binding->previous = IDENTIFIER_BINDING (names[i]);
11709 IDENTIFIER_BINDING (names[i]) = binding;
11712 /* If in template, STMT is converted to a normal for-statement
11713 at instantiation. If not, it is done just ahead. */
11714 if (processing_template_decl)
11716 if (check_for_bare_parameter_packs (range_expr))
11717 range_expr = error_mark_node;
11718 stmt = begin_range_for_stmt (scope, init);
11719 if (ivdep)
11720 RANGE_FOR_IVDEP (stmt) = 1;
11721 if (unroll)
11722 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11723 finish_range_for_decl (stmt, range_decl, range_expr);
11724 if (!type_dependent_expression_p (range_expr)
11725 /* do_auto_deduction doesn't mess with template init-lists. */
11726 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11727 do_range_for_auto_deduction (range_decl, range_expr);
11729 else
11731 stmt = begin_for_stmt (scope, init);
11732 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11733 decomp_first_name, decomp_cnt, ivdep,
11734 unroll);
11736 return stmt;
11739 /* Subroutine of cp_convert_range_for: given the initializer expression,
11740 builds up the range temporary. */
11742 static tree
11743 build_range_temp (tree range_expr)
11745 tree range_type, range_temp;
11747 /* Find out the type deduced by the declaration
11748 `auto &&__range = range_expr'. */
11749 range_type = cp_build_reference_type (make_auto (), true);
11750 range_type = do_auto_deduction (range_type, range_expr,
11751 type_uses_auto (range_type));
11753 /* Create the __range variable. */
11754 range_temp = build_decl (input_location, VAR_DECL,
11755 get_identifier ("__for_range"), range_type);
11756 TREE_USED (range_temp) = 1;
11757 DECL_ARTIFICIAL (range_temp) = 1;
11759 return range_temp;
11762 /* Used by cp_parser_range_for in template context: we aren't going to
11763 do a full conversion yet, but we still need to resolve auto in the
11764 type of the for-range-declaration if present. This is basically
11765 a shortcut version of cp_convert_range_for. */
11767 static void
11768 do_range_for_auto_deduction (tree decl, tree range_expr)
11770 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11771 if (auto_node)
11773 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11774 range_temp = convert_from_reference (build_range_temp (range_expr));
11775 iter_type = (cp_parser_perform_range_for_lookup
11776 (range_temp, &begin_dummy, &end_dummy));
11777 if (iter_type)
11779 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11780 iter_type);
11781 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11782 RO_UNARY_STAR,
11783 tf_warning_or_error);
11784 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11785 iter_decl, auto_node);
11790 /* Converts a range-based for-statement into a normal
11791 for-statement, as per the definition.
11793 for (RANGE_DECL : RANGE_EXPR)
11794 BLOCK
11796 should be equivalent to:
11799 auto &&__range = RANGE_EXPR;
11800 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11801 __begin != __end;
11802 ++__begin)
11804 RANGE_DECL = *__begin;
11805 BLOCK
11809 If RANGE_EXPR is an array:
11810 BEGIN_EXPR = __range
11811 END_EXPR = __range + ARRAY_SIZE(__range)
11812 Else if RANGE_EXPR has a member 'begin' or 'end':
11813 BEGIN_EXPR = __range.begin()
11814 END_EXPR = __range.end()
11815 Else:
11816 BEGIN_EXPR = begin(__range)
11817 END_EXPR = end(__range);
11819 If __range has a member 'begin' but not 'end', or vice versa, we must
11820 still use the second alternative (it will surely fail, however).
11821 When calling begin()/end() in the third alternative we must use
11822 argument dependent lookup, but always considering 'std' as an associated
11823 namespace. */
11825 tree
11826 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11827 tree decomp_first_name, unsigned int decomp_cnt,
11828 bool ivdep, unsigned short unroll)
11830 tree begin, end;
11831 tree iter_type, begin_expr, end_expr;
11832 tree condition, expression;
11834 range_expr = mark_lvalue_use (range_expr);
11836 if (range_decl == error_mark_node || range_expr == error_mark_node)
11837 /* If an error happened previously do nothing or else a lot of
11838 unhelpful errors would be issued. */
11839 begin_expr = end_expr = iter_type = error_mark_node;
11840 else
11842 tree range_temp;
11844 if (VAR_P (range_expr)
11845 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11846 /* Can't bind a reference to an array of runtime bound. */
11847 range_temp = range_expr;
11848 else
11850 range_temp = build_range_temp (range_expr);
11851 pushdecl (range_temp);
11852 cp_finish_decl (range_temp, range_expr,
11853 /*is_constant_init*/false, NULL_TREE,
11854 LOOKUP_ONLYCONVERTING);
11855 range_temp = convert_from_reference (range_temp);
11857 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11858 &begin_expr, &end_expr);
11861 /* The new for initialization statement. */
11862 begin = build_decl (input_location, VAR_DECL,
11863 get_identifier ("__for_begin"), iter_type);
11864 TREE_USED (begin) = 1;
11865 DECL_ARTIFICIAL (begin) = 1;
11866 pushdecl (begin);
11867 cp_finish_decl (begin, begin_expr,
11868 /*is_constant_init*/false, NULL_TREE,
11869 LOOKUP_ONLYCONVERTING);
11871 if (cxx_dialect >= cxx17)
11872 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11873 end = build_decl (input_location, VAR_DECL,
11874 get_identifier ("__for_end"), iter_type);
11875 TREE_USED (end) = 1;
11876 DECL_ARTIFICIAL (end) = 1;
11877 pushdecl (end);
11878 cp_finish_decl (end, end_expr,
11879 /*is_constant_init*/false, NULL_TREE,
11880 LOOKUP_ONLYCONVERTING);
11882 finish_init_stmt (statement);
11884 /* The new for condition. */
11885 condition = build_x_binary_op (input_location, NE_EXPR,
11886 begin, ERROR_MARK,
11887 end, ERROR_MARK,
11888 NULL, tf_warning_or_error);
11889 finish_for_cond (condition, statement, ivdep, unroll);
11891 /* The new increment expression. */
11892 expression = finish_unary_op_expr (input_location,
11893 PREINCREMENT_EXPR, begin,
11894 tf_warning_or_error);
11895 finish_for_expr (expression, statement);
11897 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11898 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11900 /* The declaration is initialized with *__begin inside the loop body. */
11901 cp_finish_decl (range_decl,
11902 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11903 tf_warning_or_error),
11904 /*is_constant_init*/false, NULL_TREE,
11905 LOOKUP_ONLYCONVERTING);
11906 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11907 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11909 return statement;
11912 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11913 We need to solve both at the same time because the method used
11914 depends on the existence of members begin or end.
11915 Returns the type deduced for the iterator expression. */
11917 static tree
11918 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11920 if (error_operand_p (range))
11922 *begin = *end = error_mark_node;
11923 return error_mark_node;
11926 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11928 error ("range-based %<for%> expression of type %qT "
11929 "has incomplete type", TREE_TYPE (range));
11930 *begin = *end = error_mark_node;
11931 return error_mark_node;
11933 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11935 /* If RANGE is an array, we will use pointer arithmetic. */
11936 *begin = decay_conversion (range, tf_warning_or_error);
11937 *end = build_binary_op (input_location, PLUS_EXPR,
11938 range,
11939 array_type_nelts_top (TREE_TYPE (range)),
11940 false);
11941 return TREE_TYPE (*begin);
11943 else
11945 /* If it is not an array, we must do a bit of magic. */
11946 tree id_begin, id_end;
11947 tree member_begin, member_end;
11949 *begin = *end = error_mark_node;
11951 id_begin = get_identifier ("begin");
11952 id_end = get_identifier ("end");
11953 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11954 /*protect=*/2, /*want_type=*/false,
11955 tf_warning_or_error);
11956 member_end = lookup_member (TREE_TYPE (range), id_end,
11957 /*protect=*/2, /*want_type=*/false,
11958 tf_warning_or_error);
11960 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11962 /* Use the member functions. */
11963 if (member_begin != NULL_TREE)
11964 *begin = cp_parser_range_for_member_function (range, id_begin);
11965 else
11966 error ("range-based %<for%> expression of type %qT has an "
11967 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11969 if (member_end != NULL_TREE)
11970 *end = cp_parser_range_for_member_function (range, id_end);
11971 else
11972 error ("range-based %<for%> expression of type %qT has a "
11973 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11975 else
11977 /* Use global functions with ADL. */
11978 vec<tree, va_gc> *vec;
11979 vec = make_tree_vector ();
11981 vec_safe_push (vec, range);
11983 member_begin = perform_koenig_lookup (id_begin, vec,
11984 tf_warning_or_error);
11985 *begin = finish_call_expr (member_begin, &vec, false, true,
11986 tf_warning_or_error);
11987 member_end = perform_koenig_lookup (id_end, vec,
11988 tf_warning_or_error);
11989 *end = finish_call_expr (member_end, &vec, false, true,
11990 tf_warning_or_error);
11992 release_tree_vector (vec);
11995 /* Last common checks. */
11996 if (*begin == error_mark_node || *end == error_mark_node)
11998 /* If one of the expressions is an error do no more checks. */
11999 *begin = *end = error_mark_node;
12000 return error_mark_node;
12002 else if (type_dependent_expression_p (*begin)
12003 || type_dependent_expression_p (*end))
12004 /* Can happen, when, eg, in a template context, Koenig lookup
12005 can't resolve begin/end (c++/58503). */
12006 return NULL_TREE;
12007 else
12009 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12010 /* The unqualified type of the __begin and __end temporaries should
12011 be the same, as required by the multiple auto declaration. */
12012 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12014 if (cxx_dialect >= cxx17
12015 && (build_x_binary_op (input_location, NE_EXPR,
12016 *begin, ERROR_MARK,
12017 *end, ERROR_MARK,
12018 NULL, tf_none)
12019 != error_mark_node))
12020 /* P0184R0 allows __begin and __end to have different types,
12021 but make sure they are comparable so we can give a better
12022 diagnostic. */;
12023 else
12024 error ("inconsistent begin/end types in range-based %<for%> "
12025 "statement: %qT and %qT",
12026 TREE_TYPE (*begin), TREE_TYPE (*end));
12028 return iter_type;
12033 /* Helper function for cp_parser_perform_range_for_lookup.
12034 Builds a tree for RANGE.IDENTIFIER(). */
12036 static tree
12037 cp_parser_range_for_member_function (tree range, tree identifier)
12039 tree member, res;
12040 vec<tree, va_gc> *vec;
12042 member = finish_class_member_access_expr (range, identifier,
12043 false, tf_warning_or_error);
12044 if (member == error_mark_node)
12045 return error_mark_node;
12047 vec = make_tree_vector ();
12048 res = finish_call_expr (member, &vec,
12049 /*disallow_virtual=*/false,
12050 /*koenig_p=*/false,
12051 tf_warning_or_error);
12052 release_tree_vector (vec);
12053 return res;
12056 /* Parse an iteration-statement.
12058 iteration-statement:
12059 while ( condition ) statement
12060 do statement while ( expression ) ;
12061 for ( init-statement condition [opt] ; expression [opt] )
12062 statement
12064 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12066 static tree
12067 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12068 unsigned short unroll)
12070 cp_token *token;
12071 enum rid keyword;
12072 tree statement;
12073 unsigned char in_statement;
12074 token_indent_info guard_tinfo;
12076 /* Peek at the next token. */
12077 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12078 if (!token)
12079 return error_mark_node;
12081 guard_tinfo = get_token_indent_info (token);
12083 /* Remember whether or not we are already within an iteration
12084 statement. */
12085 in_statement = parser->in_statement;
12087 /* See what kind of keyword it is. */
12088 keyword = token->keyword;
12089 switch (keyword)
12091 case RID_WHILE:
12093 tree condition;
12095 /* Begin the while-statement. */
12096 statement = begin_while_stmt ();
12097 /* Look for the `('. */
12098 matching_parens parens;
12099 parens.require_open (parser);
12100 /* Parse the condition. */
12101 condition = cp_parser_condition (parser);
12102 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12103 /* Look for the `)'. */
12104 parens.require_close (parser);
12105 /* Parse the dependent statement. */
12106 parser->in_statement = IN_ITERATION_STMT;
12107 bool prev = note_iteration_stmt_body_start ();
12108 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12109 note_iteration_stmt_body_end (prev);
12110 parser->in_statement = in_statement;
12111 /* We're done with the while-statement. */
12112 finish_while_stmt (statement);
12114 break;
12116 case RID_DO:
12118 tree expression;
12120 /* Begin the do-statement. */
12121 statement = begin_do_stmt ();
12122 /* Parse the body of the do-statement. */
12123 parser->in_statement = IN_ITERATION_STMT;
12124 bool prev = note_iteration_stmt_body_start ();
12125 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12126 note_iteration_stmt_body_end (prev);
12127 parser->in_statement = in_statement;
12128 finish_do_body (statement);
12129 /* Look for the `while' keyword. */
12130 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12131 /* Look for the `('. */
12132 matching_parens parens;
12133 parens.require_open (parser);
12134 /* Parse the expression. */
12135 expression = cp_parser_expression (parser);
12136 /* We're done with the do-statement. */
12137 finish_do_stmt (expression, statement, ivdep, unroll);
12138 /* Look for the `)'. */
12139 parens.require_close (parser);
12140 /* Look for the `;'. */
12141 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12143 break;
12145 case RID_FOR:
12147 /* Look for the `('. */
12148 matching_parens parens;
12149 parens.require_open (parser);
12151 statement = cp_parser_for (parser, ivdep, unroll);
12153 /* Look for the `)'. */
12154 parens.require_close (parser);
12156 /* Parse the body of the for-statement. */
12157 parser->in_statement = IN_ITERATION_STMT;
12158 bool prev = note_iteration_stmt_body_start ();
12159 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12160 note_iteration_stmt_body_end (prev);
12161 parser->in_statement = in_statement;
12163 /* We're done with the for-statement. */
12164 finish_for_stmt (statement);
12166 break;
12168 default:
12169 cp_parser_error (parser, "expected iteration-statement");
12170 statement = error_mark_node;
12171 break;
12174 return statement;
12177 /* Parse a init-statement or the declarator of a range-based-for.
12178 Returns true if a range-based-for declaration is seen.
12180 init-statement:
12181 expression-statement
12182 simple-declaration */
12184 static bool
12185 cp_parser_init_statement (cp_parser* parser, tree *decl)
12187 /* If the next token is a `;', then we have an empty
12188 expression-statement. Grammatically, this is also a
12189 simple-declaration, but an invalid one, because it does not
12190 declare anything. Therefore, if we did not handle this case
12191 specially, we would issue an error message about an invalid
12192 declaration. */
12193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12195 bool is_range_for = false;
12196 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12198 /* A colon is used in range-based for. */
12199 parser->colon_corrects_to_scope_p = false;
12201 /* We're going to speculatively look for a declaration, falling back
12202 to an expression, if necessary. */
12203 cp_parser_parse_tentatively (parser);
12204 /* Parse the declaration. */
12205 cp_parser_simple_declaration (parser,
12206 /*function_definition_allowed_p=*/false,
12207 decl);
12208 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12209 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12211 /* It is a range-for, consume the ':' */
12212 cp_lexer_consume_token (parser->lexer);
12213 is_range_for = true;
12214 if (cxx_dialect < cxx11)
12216 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12217 "range-based %<for%> loops only available with "
12218 "-std=c++11 or -std=gnu++11");
12219 *decl = error_mark_node;
12222 else
12223 /* The ';' is not consumed yet because we told
12224 cp_parser_simple_declaration not to. */
12225 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12227 if (cp_parser_parse_definitely (parser))
12228 return is_range_for;
12229 /* If the tentative parse failed, then we shall need to look for an
12230 expression-statement. */
12232 /* If we are here, it is an expression-statement. */
12233 cp_parser_expression_statement (parser, NULL_TREE);
12234 return false;
12237 /* Parse a jump-statement.
12239 jump-statement:
12240 break ;
12241 continue ;
12242 return expression [opt] ;
12243 return braced-init-list ;
12244 goto identifier ;
12246 GNU extension:
12248 jump-statement:
12249 goto * expression ;
12251 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12253 static tree
12254 cp_parser_jump_statement (cp_parser* parser)
12256 tree statement = error_mark_node;
12257 cp_token *token;
12258 enum rid keyword;
12259 unsigned char in_statement;
12261 /* Peek at the next token. */
12262 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12263 if (!token)
12264 return error_mark_node;
12266 /* See what kind of keyword it is. */
12267 keyword = token->keyword;
12268 switch (keyword)
12270 case RID_BREAK:
12271 in_statement = parser->in_statement & ~IN_IF_STMT;
12272 switch (in_statement)
12274 case 0:
12275 error_at (token->location, "break statement not within loop or switch");
12276 break;
12277 default:
12278 gcc_assert ((in_statement & IN_SWITCH_STMT)
12279 || in_statement == IN_ITERATION_STMT);
12280 statement = finish_break_stmt ();
12281 if (in_statement == IN_ITERATION_STMT)
12282 break_maybe_infinite_loop ();
12283 break;
12284 case IN_OMP_BLOCK:
12285 error_at (token->location, "invalid exit from OpenMP structured block");
12286 break;
12287 case IN_OMP_FOR:
12288 error_at (token->location, "break statement used with OpenMP for loop");
12289 break;
12291 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12292 break;
12294 case RID_CONTINUE:
12295 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12297 case 0:
12298 error_at (token->location, "continue statement not within a loop");
12299 break;
12300 /* Fall through. */
12301 case IN_ITERATION_STMT:
12302 case IN_OMP_FOR:
12303 statement = finish_continue_stmt ();
12304 break;
12305 case IN_OMP_BLOCK:
12306 error_at (token->location, "invalid exit from OpenMP structured block");
12307 break;
12308 default:
12309 gcc_unreachable ();
12311 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12312 break;
12314 case RID_RETURN:
12316 tree expr;
12317 bool expr_non_constant_p;
12319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12321 cp_lexer_set_source_position (parser->lexer);
12322 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12323 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12325 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12326 expr = cp_parser_expression (parser);
12327 else
12328 /* If the next token is a `;', then there is no
12329 expression. */
12330 expr = NULL_TREE;
12331 /* Build the return-statement. */
12332 if (current_function_auto_return_pattern && in_discarded_stmt)
12333 /* Don't deduce from a discarded return statement. */;
12334 else
12335 statement = finish_return_stmt (expr);
12336 /* Look for the final `;'. */
12337 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12339 break;
12341 case RID_GOTO:
12342 if (parser->in_function_body
12343 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12345 error ("%<goto%> in %<constexpr%> function");
12346 cp_function_chain->invalid_constexpr = true;
12349 /* Create the goto-statement. */
12350 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12352 /* Issue a warning about this use of a GNU extension. */
12353 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12354 /* Consume the '*' token. */
12355 cp_lexer_consume_token (parser->lexer);
12356 /* Parse the dependent expression. */
12357 finish_goto_stmt (cp_parser_expression (parser));
12359 else
12360 finish_goto_stmt (cp_parser_identifier (parser));
12361 /* Look for the final `;'. */
12362 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12363 break;
12365 default:
12366 cp_parser_error (parser, "expected jump-statement");
12367 break;
12370 return statement;
12373 /* Parse a declaration-statement.
12375 declaration-statement:
12376 block-declaration */
12378 static void
12379 cp_parser_declaration_statement (cp_parser* parser)
12381 void *p;
12383 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12384 p = obstack_alloc (&declarator_obstack, 0);
12386 /* Parse the block-declaration. */
12387 cp_parser_block_declaration (parser, /*statement_p=*/true);
12389 /* Free any declarators allocated. */
12390 obstack_free (&declarator_obstack, p);
12393 /* Some dependent statements (like `if (cond) statement'), are
12394 implicitly in their own scope. In other words, if the statement is
12395 a single statement (as opposed to a compound-statement), it is
12396 none-the-less treated as if it were enclosed in braces. Any
12397 declarations appearing in the dependent statement are out of scope
12398 after control passes that point. This function parses a statement,
12399 but ensures that is in its own scope, even if it is not a
12400 compound-statement.
12402 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12403 is a (possibly labeled) if statement which is not enclosed in
12404 braces and has an else clause. This is used to implement
12405 -Wparentheses.
12407 CHAIN is a vector of if-else-if conditions. This is used to implement
12408 -Wduplicated-cond.
12410 Returns the new statement. */
12412 static tree
12413 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12414 const token_indent_info &guard_tinfo,
12415 vec<tree> *chain)
12417 tree statement;
12418 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12419 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12420 token_indent_info body_tinfo
12421 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12423 if (if_p != NULL)
12424 *if_p = false;
12426 /* Mark if () ; with a special NOP_EXPR. */
12427 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12429 cp_lexer_consume_token (parser->lexer);
12430 statement = add_stmt (build_empty_stmt (body_loc));
12432 if (guard_tinfo.keyword == RID_IF
12433 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12434 warning_at (body_loc, OPT_Wempty_body,
12435 "suggest braces around empty body in an %<if%> statement");
12436 else if (guard_tinfo.keyword == RID_ELSE)
12437 warning_at (body_loc, OPT_Wempty_body,
12438 "suggest braces around empty body in an %<else%> statement");
12440 /* if a compound is opened, we simply parse the statement directly. */
12441 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12442 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12443 /* If the token is not a `{', then we must take special action. */
12444 else
12446 /* Create a compound-statement. */
12447 statement = begin_compound_stmt (0);
12448 /* Parse the dependent-statement. */
12449 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12450 &body_loc_after_labels);
12451 /* Finish the dummy compound-statement. */
12452 finish_compound_stmt (statement);
12455 token_indent_info next_tinfo
12456 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12457 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12459 if (body_loc_after_labels != UNKNOWN_LOCATION
12460 && next_tinfo.type != CPP_SEMICOLON)
12461 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12462 guard_tinfo.location, guard_tinfo.keyword);
12464 /* Return the statement. */
12465 return statement;
12468 /* For some dependent statements (like `while (cond) statement'), we
12469 have already created a scope. Therefore, even if the dependent
12470 statement is a compound-statement, we do not want to create another
12471 scope. */
12473 static void
12474 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12475 const token_indent_info &guard_tinfo)
12477 /* If the token is a `{', then we must take special action. */
12478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12480 token_indent_info body_tinfo
12481 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12482 location_t loc_after_labels = UNKNOWN_LOCATION;
12484 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12485 &loc_after_labels);
12486 token_indent_info next_tinfo
12487 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12488 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12490 if (loc_after_labels != UNKNOWN_LOCATION
12491 && next_tinfo.type != CPP_SEMICOLON)
12492 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12493 guard_tinfo.location,
12494 guard_tinfo.keyword);
12496 else
12498 /* Avoid calling cp_parser_compound_statement, so that we
12499 don't create a new scope. Do everything else by hand. */
12500 matching_braces braces;
12501 braces.require_open (parser);
12502 /* If the next keyword is `__label__' we have a label declaration. */
12503 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12504 cp_parser_label_declaration (parser);
12505 /* Parse an (optional) statement-seq. */
12506 cp_parser_statement_seq_opt (parser, NULL_TREE);
12507 braces.require_close (parser);
12511 /* Declarations [gram.dcl.dcl] */
12513 /* Parse an optional declaration-sequence.
12515 declaration-seq:
12516 declaration
12517 declaration-seq declaration */
12519 static void
12520 cp_parser_declaration_seq_opt (cp_parser* parser)
12522 while (true)
12524 cp_token *token;
12526 token = cp_lexer_peek_token (parser->lexer);
12528 if (token->type == CPP_CLOSE_BRACE
12529 || token->type == CPP_EOF
12530 || token->type == CPP_PRAGMA_EOL)
12531 break;
12533 if (token->type == CPP_SEMICOLON)
12535 /* A declaration consisting of a single semicolon is
12536 invalid. Allow it unless we're being pedantic. */
12537 cp_lexer_consume_token (parser->lexer);
12538 if (!in_system_header_at (input_location))
12539 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12540 continue;
12543 /* If we're entering or exiting a region that's implicitly
12544 extern "C", modify the lang context appropriately. */
12545 if (!parser->implicit_extern_c && token->implicit_extern_c)
12547 push_lang_context (lang_name_c);
12548 parser->implicit_extern_c = true;
12550 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12552 pop_lang_context ();
12553 parser->implicit_extern_c = false;
12556 if (token->type == CPP_PRAGMA)
12558 /* A top-level declaration can consist solely of a #pragma.
12559 A nested declaration cannot, so this is done here and not
12560 in cp_parser_declaration. (A #pragma at block scope is
12561 handled in cp_parser_statement.) */
12562 cp_parser_pragma (parser, pragma_external, NULL);
12563 continue;
12566 /* Parse the declaration itself. */
12567 cp_parser_declaration (parser);
12571 /* Parse a declaration.
12573 declaration:
12574 block-declaration
12575 function-definition
12576 template-declaration
12577 explicit-instantiation
12578 explicit-specialization
12579 linkage-specification
12580 namespace-definition
12582 C++17:
12583 deduction-guide
12585 GNU extension:
12587 declaration:
12588 __extension__ declaration */
12590 static void
12591 cp_parser_declaration (cp_parser* parser)
12593 cp_token token1;
12594 cp_token token2;
12595 int saved_pedantic;
12596 void *p;
12597 tree attributes = NULL_TREE;
12599 /* Check for the `__extension__' keyword. */
12600 if (cp_parser_extension_opt (parser, &saved_pedantic))
12602 /* Parse the qualified declaration. */
12603 cp_parser_declaration (parser);
12604 /* Restore the PEDANTIC flag. */
12605 pedantic = saved_pedantic;
12607 return;
12610 /* Try to figure out what kind of declaration is present. */
12611 token1 = *cp_lexer_peek_token (parser->lexer);
12613 if (token1.type != CPP_EOF)
12614 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12615 else
12617 token2.type = CPP_EOF;
12618 token2.keyword = RID_MAX;
12621 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12622 p = obstack_alloc (&declarator_obstack, 0);
12624 /* If the next token is `extern' and the following token is a string
12625 literal, then we have a linkage specification. */
12626 if (token1.keyword == RID_EXTERN
12627 && cp_parser_is_pure_string_literal (&token2))
12628 cp_parser_linkage_specification (parser);
12629 /* If the next token is `template', then we have either a template
12630 declaration, an explicit instantiation, or an explicit
12631 specialization. */
12632 else if (token1.keyword == RID_TEMPLATE)
12634 /* `template <>' indicates a template specialization. */
12635 if (token2.type == CPP_LESS
12636 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12637 cp_parser_explicit_specialization (parser);
12638 /* `template <' indicates a template declaration. */
12639 else if (token2.type == CPP_LESS)
12640 cp_parser_template_declaration (parser, /*member_p=*/false);
12641 /* Anything else must be an explicit instantiation. */
12642 else
12643 cp_parser_explicit_instantiation (parser);
12645 /* If the next token is `export', then we have a template
12646 declaration. */
12647 else if (token1.keyword == RID_EXPORT)
12648 cp_parser_template_declaration (parser, /*member_p=*/false);
12649 /* If the next token is `extern', 'static' or 'inline' and the one
12650 after that is `template', we have a GNU extended explicit
12651 instantiation directive. */
12652 else if (cp_parser_allow_gnu_extensions_p (parser)
12653 && (token1.keyword == RID_EXTERN
12654 || token1.keyword == RID_STATIC
12655 || token1.keyword == RID_INLINE)
12656 && token2.keyword == RID_TEMPLATE)
12657 cp_parser_explicit_instantiation (parser);
12658 /* If the next token is `namespace', check for a named or unnamed
12659 namespace definition. */
12660 else if (token1.keyword == RID_NAMESPACE
12661 && (/* A named namespace definition. */
12662 (token2.type == CPP_NAME
12663 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12664 != CPP_EQ))
12665 || (token2.type == CPP_OPEN_SQUARE
12666 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12667 == CPP_OPEN_SQUARE)
12668 /* An unnamed namespace definition. */
12669 || token2.type == CPP_OPEN_BRACE
12670 || token2.keyword == RID_ATTRIBUTE))
12671 cp_parser_namespace_definition (parser);
12672 /* An inline (associated) namespace definition. */
12673 else if (token1.keyword == RID_INLINE
12674 && token2.keyword == RID_NAMESPACE)
12675 cp_parser_namespace_definition (parser);
12676 /* Objective-C++ declaration/definition. */
12677 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12678 cp_parser_objc_declaration (parser, NULL_TREE);
12679 else if (c_dialect_objc ()
12680 && token1.keyword == RID_ATTRIBUTE
12681 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12682 cp_parser_objc_declaration (parser, attributes);
12683 /* At this point we may have a template declared by a concept
12684 introduction. */
12685 else if (flag_concepts
12686 && cp_parser_template_declaration_after_export (parser,
12687 /*member_p=*/false))
12688 /* We did. */;
12689 else
12690 /* Try to parse a block-declaration, or a function-definition. */
12691 cp_parser_block_declaration (parser, /*statement_p=*/false);
12693 /* Free any declarators allocated. */
12694 obstack_free (&declarator_obstack, p);
12697 /* Parse a block-declaration.
12699 block-declaration:
12700 simple-declaration
12701 asm-definition
12702 namespace-alias-definition
12703 using-declaration
12704 using-directive
12706 GNU Extension:
12708 block-declaration:
12709 __extension__ block-declaration
12711 C++0x Extension:
12713 block-declaration:
12714 static_assert-declaration
12716 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12717 part of a declaration-statement. */
12719 static void
12720 cp_parser_block_declaration (cp_parser *parser,
12721 bool statement_p)
12723 cp_token *token1;
12724 int saved_pedantic;
12726 /* Check for the `__extension__' keyword. */
12727 if (cp_parser_extension_opt (parser, &saved_pedantic))
12729 /* Parse the qualified declaration. */
12730 cp_parser_block_declaration (parser, statement_p);
12731 /* Restore the PEDANTIC flag. */
12732 pedantic = saved_pedantic;
12734 return;
12737 /* Peek at the next token to figure out which kind of declaration is
12738 present. */
12739 token1 = cp_lexer_peek_token (parser->lexer);
12741 /* If the next keyword is `asm', we have an asm-definition. */
12742 if (token1->keyword == RID_ASM)
12744 if (statement_p)
12745 cp_parser_commit_to_tentative_parse (parser);
12746 cp_parser_asm_definition (parser);
12748 /* If the next keyword is `namespace', we have a
12749 namespace-alias-definition. */
12750 else if (token1->keyword == RID_NAMESPACE)
12751 cp_parser_namespace_alias_definition (parser);
12752 /* If the next keyword is `using', we have a
12753 using-declaration, a using-directive, or an alias-declaration. */
12754 else if (token1->keyword == RID_USING)
12756 cp_token *token2;
12758 if (statement_p)
12759 cp_parser_commit_to_tentative_parse (parser);
12760 /* If the token after `using' is `namespace', then we have a
12761 using-directive. */
12762 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12763 if (token2->keyword == RID_NAMESPACE)
12764 cp_parser_using_directive (parser);
12765 /* If the second token after 'using' is '=', then we have an
12766 alias-declaration. */
12767 else if (cxx_dialect >= cxx11
12768 && token2->type == CPP_NAME
12769 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12770 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12771 cp_parser_alias_declaration (parser);
12772 /* Otherwise, it's a using-declaration. */
12773 else
12774 cp_parser_using_declaration (parser,
12775 /*access_declaration_p=*/false);
12777 /* If the next keyword is `__label__' we have a misplaced label
12778 declaration. */
12779 else if (token1->keyword == RID_LABEL)
12781 cp_lexer_consume_token (parser->lexer);
12782 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12783 cp_parser_skip_to_end_of_statement (parser);
12784 /* If the next token is now a `;', consume it. */
12785 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12786 cp_lexer_consume_token (parser->lexer);
12788 /* If the next token is `static_assert' we have a static assertion. */
12789 else if (token1->keyword == RID_STATIC_ASSERT)
12790 cp_parser_static_assert (parser, /*member_p=*/false);
12791 /* Anything else must be a simple-declaration. */
12792 else
12793 cp_parser_simple_declaration (parser, !statement_p,
12794 /*maybe_range_for_decl*/NULL);
12797 /* Parse a simple-declaration.
12799 simple-declaration:
12800 decl-specifier-seq [opt] init-declarator-list [opt] ;
12801 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12802 brace-or-equal-initializer ;
12804 init-declarator-list:
12805 init-declarator
12806 init-declarator-list , init-declarator
12808 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12809 function-definition as a simple-declaration.
12811 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12812 parsed declaration if it is an uninitialized single declarator not followed
12813 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12814 if present, will not be consumed. */
12816 static void
12817 cp_parser_simple_declaration (cp_parser* parser,
12818 bool function_definition_allowed_p,
12819 tree *maybe_range_for_decl)
12821 cp_decl_specifier_seq decl_specifiers;
12822 int declares_class_or_enum;
12823 bool saw_declarator;
12824 location_t comma_loc = UNKNOWN_LOCATION;
12825 location_t init_loc = UNKNOWN_LOCATION;
12827 if (maybe_range_for_decl)
12828 *maybe_range_for_decl = NULL_TREE;
12830 /* Defer access checks until we know what is being declared; the
12831 checks for names appearing in the decl-specifier-seq should be
12832 done as if we were in the scope of the thing being declared. */
12833 push_deferring_access_checks (dk_deferred);
12835 /* Parse the decl-specifier-seq. We have to keep track of whether
12836 or not the decl-specifier-seq declares a named class or
12837 enumeration type, since that is the only case in which the
12838 init-declarator-list is allowed to be empty.
12840 [dcl.dcl]
12842 In a simple-declaration, the optional init-declarator-list can be
12843 omitted only when declaring a class or enumeration, that is when
12844 the decl-specifier-seq contains either a class-specifier, an
12845 elaborated-type-specifier, or an enum-specifier. */
12846 cp_parser_decl_specifier_seq (parser,
12847 CP_PARSER_FLAGS_OPTIONAL,
12848 &decl_specifiers,
12849 &declares_class_or_enum);
12850 /* We no longer need to defer access checks. */
12851 stop_deferring_access_checks ();
12853 /* In a block scope, a valid declaration must always have a
12854 decl-specifier-seq. By not trying to parse declarators, we can
12855 resolve the declaration/expression ambiguity more quickly. */
12856 if (!function_definition_allowed_p
12857 && !decl_specifiers.any_specifiers_p)
12859 cp_parser_error (parser, "expected declaration");
12860 goto done;
12863 /* If the next two tokens are both identifiers, the code is
12864 erroneous. The usual cause of this situation is code like:
12866 T t;
12868 where "T" should name a type -- but does not. */
12869 if (!decl_specifiers.any_type_specifiers_p
12870 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12872 /* If parsing tentatively, we should commit; we really are
12873 looking at a declaration. */
12874 cp_parser_commit_to_tentative_parse (parser);
12875 /* Give up. */
12876 goto done;
12879 /* If we have seen at least one decl-specifier, and the next token
12880 is not a parenthesis, then we must be looking at a declaration.
12881 (After "int (" we might be looking at a functional cast.) */
12882 if (decl_specifiers.any_specifiers_p
12883 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12884 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12885 && !cp_parser_error_occurred (parser))
12886 cp_parser_commit_to_tentative_parse (parser);
12888 /* Look for C++17 decomposition declaration. */
12889 for (size_t n = 1; ; n++)
12890 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12891 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12892 continue;
12893 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12894 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12895 && decl_specifiers.any_specifiers_p)
12897 tree decl
12898 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12899 maybe_range_for_decl,
12900 &init_loc);
12902 /* The next token should be either a `,' or a `;'. */
12903 cp_token *token = cp_lexer_peek_token (parser->lexer);
12904 /* If it's a `;', we are done. */
12905 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12906 goto finish;
12907 /* Anything else is an error. */
12908 else
12910 /* If we have already issued an error message we don't need
12911 to issue another one. */
12912 if ((decl != error_mark_node
12913 && DECL_INITIAL (decl) != error_mark_node)
12914 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12915 cp_parser_error (parser, "expected %<,%> or %<;%>");
12916 /* Skip tokens until we reach the end of the statement. */
12917 cp_parser_skip_to_end_of_statement (parser);
12918 /* If the next token is now a `;', consume it. */
12919 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12920 cp_lexer_consume_token (parser->lexer);
12921 goto done;
12924 else
12925 break;
12927 tree last_type;
12928 bool auto_specifier_p;
12929 /* NULL_TREE if both variable and function declaration are allowed,
12930 error_mark_node if function declaration are not allowed and
12931 a FUNCTION_DECL that should be diagnosed if it is followed by
12932 variable declarations. */
12933 tree auto_function_declaration;
12935 last_type = NULL_TREE;
12936 auto_specifier_p
12937 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12938 auto_function_declaration = NULL_TREE;
12940 /* Keep going until we hit the `;' at the end of the simple
12941 declaration. */
12942 saw_declarator = false;
12943 while (cp_lexer_next_token_is_not (parser->lexer,
12944 CPP_SEMICOLON))
12946 cp_token *token;
12947 bool function_definition_p;
12948 tree decl;
12949 tree auto_result = NULL_TREE;
12951 if (saw_declarator)
12953 /* If we are processing next declarator, comma is expected */
12954 token = cp_lexer_peek_token (parser->lexer);
12955 gcc_assert (token->type == CPP_COMMA);
12956 cp_lexer_consume_token (parser->lexer);
12957 if (maybe_range_for_decl)
12959 *maybe_range_for_decl = error_mark_node;
12960 if (comma_loc == UNKNOWN_LOCATION)
12961 comma_loc = token->location;
12964 else
12965 saw_declarator = true;
12967 /* Parse the init-declarator. */
12968 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12969 /*checks=*/NULL,
12970 function_definition_allowed_p,
12971 /*member_p=*/false,
12972 declares_class_or_enum,
12973 &function_definition_p,
12974 maybe_range_for_decl,
12975 &init_loc,
12976 &auto_result);
12977 /* If an error occurred while parsing tentatively, exit quickly.
12978 (That usually happens when in the body of a function; each
12979 statement is treated as a declaration-statement until proven
12980 otherwise.) */
12981 if (cp_parser_error_occurred (parser))
12982 goto done;
12984 if (auto_specifier_p && cxx_dialect >= cxx14)
12986 /* If the init-declarator-list contains more than one
12987 init-declarator, they shall all form declarations of
12988 variables. */
12989 if (auto_function_declaration == NULL_TREE)
12990 auto_function_declaration
12991 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12992 else if (TREE_CODE (decl) == FUNCTION_DECL
12993 || auto_function_declaration != error_mark_node)
12995 error_at (decl_specifiers.locations[ds_type_spec],
12996 "non-variable %qD in declaration with more than one "
12997 "declarator with placeholder type",
12998 TREE_CODE (decl) == FUNCTION_DECL
12999 ? decl : auto_function_declaration);
13000 auto_function_declaration = error_mark_node;
13004 if (auto_result
13005 && (!processing_template_decl || !type_uses_auto (auto_result)))
13007 if (last_type
13008 && last_type != error_mark_node
13009 && !same_type_p (auto_result, last_type))
13011 /* If the list of declarators contains more than one declarator,
13012 the type of each declared variable is determined as described
13013 above. If the type deduced for the template parameter U is not
13014 the same in each deduction, the program is ill-formed. */
13015 error_at (decl_specifiers.locations[ds_type_spec],
13016 "inconsistent deduction for %qT: %qT and then %qT",
13017 decl_specifiers.type, last_type, auto_result);
13018 last_type = error_mark_node;
13020 else
13021 last_type = auto_result;
13024 /* Handle function definitions specially. */
13025 if (function_definition_p)
13027 /* If the next token is a `,', then we are probably
13028 processing something like:
13030 void f() {}, *p;
13032 which is erroneous. */
13033 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13035 cp_token *token = cp_lexer_peek_token (parser->lexer);
13036 error_at (token->location,
13037 "mixing"
13038 " declarations and function-definitions is forbidden");
13040 /* Otherwise, we're done with the list of declarators. */
13041 else
13043 pop_deferring_access_checks ();
13044 return;
13047 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13048 *maybe_range_for_decl = decl;
13049 /* The next token should be either a `,' or a `;'. */
13050 token = cp_lexer_peek_token (parser->lexer);
13051 /* If it's a `,', there are more declarators to come. */
13052 if (token->type == CPP_COMMA)
13053 /* will be consumed next time around */;
13054 /* If it's a `;', we are done. */
13055 else if (token->type == CPP_SEMICOLON)
13056 break;
13057 else if (maybe_range_for_decl)
13059 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13060 permerror (decl_specifiers.locations[ds_type_spec],
13061 "types may not be defined in a for-range-declaration");
13062 break;
13064 /* Anything else is an error. */
13065 else
13067 /* If we have already issued an error message we don't need
13068 to issue another one. */
13069 if ((decl != error_mark_node
13070 && DECL_INITIAL (decl) != error_mark_node)
13071 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13072 cp_parser_error (parser, "expected %<,%> or %<;%>");
13073 /* Skip tokens until we reach the end of the statement. */
13074 cp_parser_skip_to_end_of_statement (parser);
13075 /* If the next token is now a `;', consume it. */
13076 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13077 cp_lexer_consume_token (parser->lexer);
13078 goto done;
13080 /* After the first time around, a function-definition is not
13081 allowed -- even if it was OK at first. For example:
13083 int i, f() {}
13085 is not valid. */
13086 function_definition_allowed_p = false;
13089 /* Issue an error message if no declarators are present, and the
13090 decl-specifier-seq does not itself declare a class or
13091 enumeration: [dcl.dcl]/3. */
13092 if (!saw_declarator)
13094 if (cp_parser_declares_only_class_p (parser))
13096 if (!declares_class_or_enum
13097 && decl_specifiers.type
13098 && OVERLOAD_TYPE_P (decl_specifiers.type))
13099 /* Ensure an error is issued anyway when finish_decltype_type,
13100 called via cp_parser_decl_specifier_seq, returns a class or
13101 an enumeration (c++/51786). */
13102 decl_specifiers.type = NULL_TREE;
13103 shadow_tag (&decl_specifiers);
13105 /* Perform any deferred access checks. */
13106 perform_deferred_access_checks (tf_warning_or_error);
13109 /* Consume the `;'. */
13110 finish:
13111 if (!maybe_range_for_decl)
13112 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13113 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13115 if (init_loc != UNKNOWN_LOCATION)
13116 error_at (init_loc, "initializer in range-based %<for%> loop");
13117 if (comma_loc != UNKNOWN_LOCATION)
13118 error_at (comma_loc,
13119 "multiple declarations in range-based %<for%> loop");
13122 done:
13123 pop_deferring_access_checks ();
13126 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13127 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13128 initializer ; */
13130 static tree
13131 cp_parser_decomposition_declaration (cp_parser *parser,
13132 cp_decl_specifier_seq *decl_specifiers,
13133 tree *maybe_range_for_decl,
13134 location_t *init_loc)
13136 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13137 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13138 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13140 /* Parse the identifier-list. */
13141 auto_vec<cp_expr, 10> v;
13142 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13143 while (true)
13145 cp_expr e = cp_parser_identifier (parser);
13146 if (e.get_value () == error_mark_node)
13147 break;
13148 v.safe_push (e);
13149 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13150 break;
13151 cp_lexer_consume_token (parser->lexer);
13154 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13155 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13157 end_loc = UNKNOWN_LOCATION;
13158 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13159 false);
13160 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13161 cp_lexer_consume_token (parser->lexer);
13162 else
13164 cp_parser_skip_to_end_of_statement (parser);
13165 return error_mark_node;
13169 if (cxx_dialect < cxx17)
13170 pedwarn (loc, 0, "structured bindings only available with "
13171 "-std=c++17 or -std=gnu++17");
13173 tree pushed_scope;
13174 cp_declarator *declarator = make_declarator (cdk_decomp);
13175 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13176 declarator->id_loc = loc;
13177 if (ref_qual != REF_QUAL_NONE)
13178 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13179 ref_qual == REF_QUAL_RVALUE,
13180 NULL_TREE);
13181 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13182 NULL_TREE, decl_specifiers->attributes,
13183 &pushed_scope);
13184 tree orig_decl = decl;
13186 unsigned int i;
13187 cp_expr e;
13188 cp_decl_specifier_seq decl_specs;
13189 clear_decl_specs (&decl_specs);
13190 decl_specs.type = make_auto ();
13191 tree prev = decl;
13192 FOR_EACH_VEC_ELT (v, i, e)
13194 if (i == 0)
13195 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13196 else
13197 declarator->u.id.unqualified_name = e.get_value ();
13198 declarator->id_loc = e.get_location ();
13199 tree elt_pushed_scope;
13200 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13201 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13202 if (decl2 == error_mark_node)
13203 decl = error_mark_node;
13204 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13206 /* Ensure we've diagnosed redeclaration if we aren't creating
13207 a new VAR_DECL. */
13208 gcc_assert (errorcount);
13209 decl = error_mark_node;
13211 else
13212 prev = decl2;
13213 if (elt_pushed_scope)
13214 pop_scope (elt_pushed_scope);
13217 if (v.is_empty ())
13219 error_at (loc, "empty structured binding declaration");
13220 decl = error_mark_node;
13223 if (maybe_range_for_decl == NULL
13224 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13226 bool non_constant_p = false, is_direct_init = false;
13227 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13228 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13229 &non_constant_p);
13230 if (initializer == NULL_TREE
13231 || (TREE_CODE (initializer) == TREE_LIST
13232 && TREE_CHAIN (initializer))
13233 || (is_direct_init
13234 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13235 && CONSTRUCTOR_NELTS (initializer) != 1))
13237 error_at (loc, "invalid initializer for structured binding "
13238 "declaration");
13239 initializer = error_mark_node;
13242 if (decl != error_mark_node)
13244 cp_maybe_mangle_decomp (decl, prev, v.length ());
13245 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13246 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13247 cp_finish_decomp (decl, prev, v.length ());
13250 else if (decl != error_mark_node)
13252 *maybe_range_for_decl = prev;
13253 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13254 the underlying DECL. */
13255 cp_finish_decomp (decl, prev, v.length ());
13258 if (pushed_scope)
13259 pop_scope (pushed_scope);
13261 if (decl == error_mark_node && DECL_P (orig_decl))
13263 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13264 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13267 return decl;
13270 /* Parse a decl-specifier-seq.
13272 decl-specifier-seq:
13273 decl-specifier-seq [opt] decl-specifier
13274 decl-specifier attribute-specifier-seq [opt] (C++11)
13276 decl-specifier:
13277 storage-class-specifier
13278 type-specifier
13279 function-specifier
13280 friend
13281 typedef
13283 GNU Extension:
13285 decl-specifier:
13286 attributes
13288 Concepts Extension:
13290 decl-specifier:
13291 concept
13293 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13295 The parser flags FLAGS is used to control type-specifier parsing.
13297 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13298 flags:
13300 1: one of the decl-specifiers is an elaborated-type-specifier
13301 (i.e., a type declaration)
13302 2: one of the decl-specifiers is an enum-specifier or a
13303 class-specifier (i.e., a type definition)
13307 static void
13308 cp_parser_decl_specifier_seq (cp_parser* parser,
13309 cp_parser_flags flags,
13310 cp_decl_specifier_seq *decl_specs,
13311 int* declares_class_or_enum)
13313 bool constructor_possible_p = !parser->in_declarator_p;
13314 bool found_decl_spec = false;
13315 cp_token *start_token = NULL;
13316 cp_decl_spec ds;
13318 /* Clear DECL_SPECS. */
13319 clear_decl_specs (decl_specs);
13321 /* Assume no class or enumeration type is declared. */
13322 *declares_class_or_enum = 0;
13324 /* Keep reading specifiers until there are no more to read. */
13325 while (true)
13327 bool constructor_p;
13328 cp_token *token;
13329 ds = ds_last;
13331 /* Peek at the next token. */
13332 token = cp_lexer_peek_token (parser->lexer);
13334 /* Save the first token of the decl spec list for error
13335 reporting. */
13336 if (!start_token)
13337 start_token = token;
13338 /* Handle attributes. */
13339 if (cp_next_tokens_can_be_attribute_p (parser))
13341 /* Parse the attributes. */
13342 tree attrs = cp_parser_attributes_opt (parser);
13344 /* In a sequence of declaration specifiers, c++11 attributes
13345 appertain to the type that precede them. In that case
13346 [dcl.spec]/1 says:
13348 The attribute-specifier-seq affects the type only for
13349 the declaration it appears in, not other declarations
13350 involving the same type.
13352 But for now let's force the user to position the
13353 attribute either at the beginning of the declaration or
13354 after the declarator-id, which would clearly mean that it
13355 applies to the declarator. */
13356 if (cxx11_attribute_p (attrs))
13358 if (!found_decl_spec)
13359 /* The c++11 attribute is at the beginning of the
13360 declaration. It appertains to the entity being
13361 declared. */;
13362 else
13364 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13366 /* This is an attribute following a
13367 class-specifier. */
13368 if (decl_specs->type_definition_p)
13369 warn_misplaced_attr_for_class_type (token->location,
13370 decl_specs->type);
13371 attrs = NULL_TREE;
13373 else
13375 decl_specs->std_attributes
13376 = chainon (decl_specs->std_attributes,
13377 attrs);
13378 if (decl_specs->locations[ds_std_attribute] == 0)
13379 decl_specs->locations[ds_std_attribute] = token->location;
13381 continue;
13385 decl_specs->attributes
13386 = chainon (decl_specs->attributes,
13387 attrs);
13388 if (decl_specs->locations[ds_attribute] == 0)
13389 decl_specs->locations[ds_attribute] = token->location;
13390 continue;
13392 /* Assume we will find a decl-specifier keyword. */
13393 found_decl_spec = true;
13394 /* If the next token is an appropriate keyword, we can simply
13395 add it to the list. */
13396 switch (token->keyword)
13398 /* decl-specifier:
13399 friend
13400 constexpr */
13401 case RID_FRIEND:
13402 if (!at_class_scope_p ())
13404 gcc_rich_location richloc (token->location);
13405 richloc.add_fixit_remove ();
13406 error_at (&richloc, "%<friend%> used outside of class");
13407 cp_lexer_purge_token (parser->lexer);
13409 else
13411 ds = ds_friend;
13412 /* Consume the token. */
13413 cp_lexer_consume_token (parser->lexer);
13415 break;
13417 case RID_CONSTEXPR:
13418 ds = ds_constexpr;
13419 cp_lexer_consume_token (parser->lexer);
13420 break;
13422 case RID_CONCEPT:
13423 ds = ds_concept;
13424 cp_lexer_consume_token (parser->lexer);
13425 break;
13427 /* function-specifier:
13428 inline
13429 virtual
13430 explicit */
13431 case RID_INLINE:
13432 case RID_VIRTUAL:
13433 case RID_EXPLICIT:
13434 cp_parser_function_specifier_opt (parser, decl_specs);
13435 break;
13437 /* decl-specifier:
13438 typedef */
13439 case RID_TYPEDEF:
13440 ds = ds_typedef;
13441 /* Consume the token. */
13442 cp_lexer_consume_token (parser->lexer);
13443 /* A constructor declarator cannot appear in a typedef. */
13444 constructor_possible_p = false;
13445 /* The "typedef" keyword can only occur in a declaration; we
13446 may as well commit at this point. */
13447 cp_parser_commit_to_tentative_parse (parser);
13449 if (decl_specs->storage_class != sc_none)
13450 decl_specs->conflicting_specifiers_p = true;
13451 break;
13453 /* storage-class-specifier:
13454 auto
13455 register
13456 static
13457 extern
13458 mutable
13460 GNU Extension:
13461 thread */
13462 case RID_AUTO:
13463 if (cxx_dialect == cxx98)
13465 /* Consume the token. */
13466 cp_lexer_consume_token (parser->lexer);
13468 /* Complain about `auto' as a storage specifier, if
13469 we're complaining about C++0x compatibility. */
13470 gcc_rich_location richloc (token->location);
13471 richloc.add_fixit_remove ();
13472 warning_at (&richloc, OPT_Wc__11_compat,
13473 "%<auto%> changes meaning in C++11; "
13474 "please remove it");
13476 /* Set the storage class anyway. */
13477 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13478 token);
13480 else
13481 /* C++0x auto type-specifier. */
13482 found_decl_spec = false;
13483 break;
13485 case RID_REGISTER:
13486 case RID_STATIC:
13487 case RID_EXTERN:
13488 case RID_MUTABLE:
13489 /* Consume the token. */
13490 cp_lexer_consume_token (parser->lexer);
13491 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13492 token);
13493 break;
13494 case RID_THREAD:
13495 /* Consume the token. */
13496 ds = ds_thread;
13497 cp_lexer_consume_token (parser->lexer);
13498 break;
13500 default:
13501 /* We did not yet find a decl-specifier yet. */
13502 found_decl_spec = false;
13503 break;
13506 if (found_decl_spec
13507 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13508 && token->keyword != RID_CONSTEXPR)
13509 error ("decl-specifier invalid in condition");
13511 if (found_decl_spec
13512 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13513 && token->keyword != RID_MUTABLE
13514 && token->keyword != RID_CONSTEXPR)
13515 error_at (token->location, "%qD invalid in lambda",
13516 ridpointers[token->keyword]);
13518 if (ds != ds_last)
13519 set_and_check_decl_spec_loc (decl_specs, ds, token);
13521 /* Constructors are a special case. The `S' in `S()' is not a
13522 decl-specifier; it is the beginning of the declarator. */
13523 constructor_p
13524 = (!found_decl_spec
13525 && constructor_possible_p
13526 && (cp_parser_constructor_declarator_p
13527 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13529 /* If we don't have a DECL_SPEC yet, then we must be looking at
13530 a type-specifier. */
13531 if (!found_decl_spec && !constructor_p)
13533 int decl_spec_declares_class_or_enum;
13534 bool is_cv_qualifier;
13535 tree type_spec;
13537 type_spec
13538 = cp_parser_type_specifier (parser, flags,
13539 decl_specs,
13540 /*is_declaration=*/true,
13541 &decl_spec_declares_class_or_enum,
13542 &is_cv_qualifier);
13543 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13545 /* If this type-specifier referenced a user-defined type
13546 (a typedef, class-name, etc.), then we can't allow any
13547 more such type-specifiers henceforth.
13549 [dcl.spec]
13551 The longest sequence of decl-specifiers that could
13552 possibly be a type name is taken as the
13553 decl-specifier-seq of a declaration. The sequence shall
13554 be self-consistent as described below.
13556 [dcl.type]
13558 As a general rule, at most one type-specifier is allowed
13559 in the complete decl-specifier-seq of a declaration. The
13560 only exceptions are the following:
13562 -- const or volatile can be combined with any other
13563 type-specifier.
13565 -- signed or unsigned can be combined with char, long,
13566 short, or int.
13568 -- ..
13570 Example:
13572 typedef char* Pc;
13573 void g (const int Pc);
13575 Here, Pc is *not* part of the decl-specifier seq; it's
13576 the declarator. Therefore, once we see a type-specifier
13577 (other than a cv-qualifier), we forbid any additional
13578 user-defined types. We *do* still allow things like `int
13579 int' to be considered a decl-specifier-seq, and issue the
13580 error message later. */
13581 if (type_spec && !is_cv_qualifier)
13582 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13583 /* A constructor declarator cannot follow a type-specifier. */
13584 if (type_spec)
13586 constructor_possible_p = false;
13587 found_decl_spec = true;
13588 if (!is_cv_qualifier)
13589 decl_specs->any_type_specifiers_p = true;
13593 /* If we still do not have a DECL_SPEC, then there are no more
13594 decl-specifiers. */
13595 if (!found_decl_spec)
13596 break;
13598 decl_specs->any_specifiers_p = true;
13599 /* After we see one decl-specifier, further decl-specifiers are
13600 always optional. */
13601 flags |= CP_PARSER_FLAGS_OPTIONAL;
13604 /* Don't allow a friend specifier with a class definition. */
13605 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13606 && (*declares_class_or_enum & 2))
13607 error_at (decl_specs->locations[ds_friend],
13608 "class definition may not be declared a friend");
13611 /* Parse an (optional) storage-class-specifier.
13613 storage-class-specifier:
13614 auto
13615 register
13616 static
13617 extern
13618 mutable
13620 GNU Extension:
13622 storage-class-specifier:
13623 thread
13625 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13627 static tree
13628 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13630 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13632 case RID_AUTO:
13633 if (cxx_dialect != cxx98)
13634 return NULL_TREE;
13635 /* Fall through for C++98. */
13636 gcc_fallthrough ();
13638 case RID_REGISTER:
13639 case RID_STATIC:
13640 case RID_EXTERN:
13641 case RID_MUTABLE:
13642 case RID_THREAD:
13643 /* Consume the token. */
13644 return cp_lexer_consume_token (parser->lexer)->u.value;
13646 default:
13647 return NULL_TREE;
13651 /* Parse an (optional) function-specifier.
13653 function-specifier:
13654 inline
13655 virtual
13656 explicit
13658 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13659 Updates DECL_SPECS, if it is non-NULL. */
13661 static tree
13662 cp_parser_function_specifier_opt (cp_parser* parser,
13663 cp_decl_specifier_seq *decl_specs)
13665 cp_token *token = cp_lexer_peek_token (parser->lexer);
13666 switch (token->keyword)
13668 case RID_INLINE:
13669 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13670 break;
13672 case RID_VIRTUAL:
13673 /* 14.5.2.3 [temp.mem]
13675 A member function template shall not be virtual. */
13676 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13677 && current_class_type)
13678 error_at (token->location, "templates may not be %<virtual%>");
13679 else
13680 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13681 break;
13683 case RID_EXPLICIT:
13684 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13685 break;
13687 default:
13688 return NULL_TREE;
13691 /* Consume the token. */
13692 return cp_lexer_consume_token (parser->lexer)->u.value;
13695 /* Parse a linkage-specification.
13697 linkage-specification:
13698 extern string-literal { declaration-seq [opt] }
13699 extern string-literal declaration */
13701 static void
13702 cp_parser_linkage_specification (cp_parser* parser)
13704 tree linkage;
13706 /* Look for the `extern' keyword. */
13707 cp_token *extern_token
13708 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13710 /* Look for the string-literal. */
13711 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13712 linkage = cp_parser_string_literal (parser, false, false);
13714 /* Transform the literal into an identifier. If the literal is a
13715 wide-character string, or contains embedded NULs, then we can't
13716 handle it as the user wants. */
13717 if (strlen (TREE_STRING_POINTER (linkage))
13718 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13720 cp_parser_error (parser, "invalid linkage-specification");
13721 /* Assume C++ linkage. */
13722 linkage = lang_name_cplusplus;
13724 else
13725 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13727 /* We're now using the new linkage. */
13728 push_lang_context (linkage);
13730 /* Preserve the location of the the innermost linkage specification,
13731 tracking the locations of nested specifications via a local. */
13732 location_t saved_location
13733 = parser->innermost_linkage_specification_location;
13734 /* Construct a location ranging from the start of the "extern" to
13735 the end of the string-literal, with the caret at the start, e.g.:
13736 extern "C" {
13737 ^~~~~~~~~~
13739 parser->innermost_linkage_specification_location
13740 = make_location (extern_token->location,
13741 extern_token->location,
13742 get_finish (string_token->location));
13744 /* If the next token is a `{', then we're using the first
13745 production. */
13746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13748 cp_ensure_no_omp_declare_simd (parser);
13749 cp_ensure_no_oacc_routine (parser);
13751 /* Consume the `{' token. */
13752 matching_braces braces;
13753 braces.consume_open (parser)->location;
13754 /* Parse the declarations. */
13755 cp_parser_declaration_seq_opt (parser);
13756 /* Look for the closing `}'. */
13757 braces.require_close (parser);
13759 /* Otherwise, there's just one declaration. */
13760 else
13762 bool saved_in_unbraced_linkage_specification_p;
13764 saved_in_unbraced_linkage_specification_p
13765 = parser->in_unbraced_linkage_specification_p;
13766 parser->in_unbraced_linkage_specification_p = true;
13767 cp_parser_declaration (parser);
13768 parser->in_unbraced_linkage_specification_p
13769 = saved_in_unbraced_linkage_specification_p;
13772 /* We're done with the linkage-specification. */
13773 pop_lang_context ();
13775 /* Restore location of parent linkage specification, if any. */
13776 parser->innermost_linkage_specification_location = saved_location;
13779 /* Parse a static_assert-declaration.
13781 static_assert-declaration:
13782 static_assert ( constant-expression , string-literal ) ;
13783 static_assert ( constant-expression ) ; (C++17)
13785 If MEMBER_P, this static_assert is a class member. */
13787 static void
13788 cp_parser_static_assert(cp_parser *parser, bool member_p)
13790 cp_expr condition;
13791 location_t token_loc;
13792 tree message;
13793 bool dummy;
13795 /* Peek at the `static_assert' token so we can keep track of exactly
13796 where the static assertion started. */
13797 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13799 /* Look for the `static_assert' keyword. */
13800 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13801 RT_STATIC_ASSERT))
13802 return;
13804 /* We know we are in a static assertion; commit to any tentative
13805 parse. */
13806 if (cp_parser_parsing_tentatively (parser))
13807 cp_parser_commit_to_tentative_parse (parser);
13809 /* Parse the `(' starting the static assertion condition. */
13810 matching_parens parens;
13811 parens.require_open (parser);
13813 /* Parse the constant-expression. Allow a non-constant expression
13814 here in order to give better diagnostics in finish_static_assert. */
13815 condition =
13816 cp_parser_constant_expression (parser,
13817 /*allow_non_constant_p=*/true,
13818 /*non_constant_p=*/&dummy);
13820 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13822 if (cxx_dialect < cxx17)
13823 pedwarn (input_location, OPT_Wpedantic,
13824 "static_assert without a message "
13825 "only available with -std=c++17 or -std=gnu++17");
13826 /* Eat the ')' */
13827 cp_lexer_consume_token (parser->lexer);
13828 message = build_string (1, "");
13829 TREE_TYPE (message) = char_array_type_node;
13830 fix_string_type (message);
13832 else
13834 /* Parse the separating `,'. */
13835 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13837 /* Parse the string-literal message. */
13838 message = cp_parser_string_literal (parser,
13839 /*translate=*/false,
13840 /*wide_ok=*/true);
13842 /* A `)' completes the static assertion. */
13843 if (!parens.require_close (parser))
13844 cp_parser_skip_to_closing_parenthesis (parser,
13845 /*recovering=*/true,
13846 /*or_comma=*/false,
13847 /*consume_paren=*/true);
13850 /* A semicolon terminates the declaration. */
13851 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13853 /* Get the location for the static assertion. Use that of the
13854 condition if available, otherwise, use that of the "static_assert"
13855 token. */
13856 location_t assert_loc = condition.get_location ();
13857 if (assert_loc == UNKNOWN_LOCATION)
13858 assert_loc = token_loc;
13860 /* Complete the static assertion, which may mean either processing
13861 the static assert now or saving it for template instantiation. */
13862 finish_static_assert (condition, message, assert_loc, member_p);
13865 /* Parse the expression in decltype ( expression ). */
13867 static tree
13868 cp_parser_decltype_expr (cp_parser *parser,
13869 bool &id_expression_or_member_access_p)
13871 cp_token *id_expr_start_token;
13872 tree expr;
13874 /* Since we're going to preserve any side-effects from this parse, set up a
13875 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13876 in the expression. */
13877 tentative_firewall firewall (parser);
13879 /* First, try parsing an id-expression. */
13880 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13881 cp_parser_parse_tentatively (parser);
13882 expr = cp_parser_id_expression (parser,
13883 /*template_keyword_p=*/false,
13884 /*check_dependency_p=*/true,
13885 /*template_p=*/NULL,
13886 /*declarator_p=*/false,
13887 /*optional_p=*/false);
13889 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13891 bool non_integral_constant_expression_p = false;
13892 tree id_expression = expr;
13893 cp_id_kind idk;
13894 const char *error_msg;
13896 if (identifier_p (expr))
13897 /* Lookup the name we got back from the id-expression. */
13898 expr = cp_parser_lookup_name_simple (parser, expr,
13899 id_expr_start_token->location);
13901 if (expr
13902 && expr != error_mark_node
13903 && TREE_CODE (expr) != TYPE_DECL
13904 && (TREE_CODE (expr) != BIT_NOT_EXPR
13905 || !TYPE_P (TREE_OPERAND (expr, 0)))
13906 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13908 /* Complete lookup of the id-expression. */
13909 expr = (finish_id_expression
13910 (id_expression, expr, parser->scope, &idk,
13911 /*integral_constant_expression_p=*/false,
13912 /*allow_non_integral_constant_expression_p=*/true,
13913 &non_integral_constant_expression_p,
13914 /*template_p=*/false,
13915 /*done=*/true,
13916 /*address_p=*/false,
13917 /*template_arg_p=*/false,
13918 &error_msg,
13919 id_expr_start_token->location));
13921 if (expr == error_mark_node)
13922 /* We found an id-expression, but it was something that we
13923 should not have found. This is an error, not something
13924 we can recover from, so note that we found an
13925 id-expression and we'll recover as gracefully as
13926 possible. */
13927 id_expression_or_member_access_p = true;
13930 if (expr
13931 && expr != error_mark_node
13932 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13933 /* We have an id-expression. */
13934 id_expression_or_member_access_p = true;
13937 if (!id_expression_or_member_access_p)
13939 /* Abort the id-expression parse. */
13940 cp_parser_abort_tentative_parse (parser);
13942 /* Parsing tentatively, again. */
13943 cp_parser_parse_tentatively (parser);
13945 /* Parse a class member access. */
13946 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13947 /*cast_p=*/false, /*decltype*/true,
13948 /*member_access_only_p=*/true, NULL);
13950 if (expr
13951 && expr != error_mark_node
13952 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13953 /* We have an id-expression. */
13954 id_expression_or_member_access_p = true;
13957 if (id_expression_or_member_access_p)
13958 /* We have parsed the complete id-expression or member access. */
13959 cp_parser_parse_definitely (parser);
13960 else
13962 /* Abort our attempt to parse an id-expression or member access
13963 expression. */
13964 cp_parser_abort_tentative_parse (parser);
13966 /* Parse a full expression. */
13967 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13968 /*decltype_p=*/true);
13971 return expr;
13974 /* Parse a `decltype' type. Returns the type.
13976 simple-type-specifier:
13977 decltype ( expression )
13978 C++14 proposal:
13979 decltype ( auto ) */
13981 static tree
13982 cp_parser_decltype (cp_parser *parser)
13984 tree expr;
13985 bool id_expression_or_member_access_p = false;
13986 const char *saved_message;
13987 bool saved_integral_constant_expression_p;
13988 bool saved_non_integral_constant_expression_p;
13989 bool saved_greater_than_is_operator_p;
13990 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13992 if (start_token->type == CPP_DECLTYPE)
13994 /* Already parsed. */
13995 cp_lexer_consume_token (parser->lexer);
13996 return saved_checks_value (start_token->u.tree_check_value);
13999 /* Look for the `decltype' token. */
14000 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14001 return error_mark_node;
14003 /* Parse the opening `('. */
14004 matching_parens parens;
14005 if (!parens.require_open (parser))
14006 return error_mark_node;
14008 /* decltype (auto) */
14009 if (cxx_dialect >= cxx14
14010 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14012 cp_lexer_consume_token (parser->lexer);
14013 if (!parens.require_close (parser))
14014 return error_mark_node;
14015 expr = make_decltype_auto ();
14016 AUTO_IS_DECLTYPE (expr) = true;
14017 goto rewrite;
14020 /* Types cannot be defined in a `decltype' expression. Save away the
14021 old message. */
14022 saved_message = parser->type_definition_forbidden_message;
14024 /* And create the new one. */
14025 parser->type_definition_forbidden_message
14026 = G_("types may not be defined in %<decltype%> expressions");
14028 /* The restrictions on constant-expressions do not apply inside
14029 decltype expressions. */
14030 saved_integral_constant_expression_p
14031 = parser->integral_constant_expression_p;
14032 saved_non_integral_constant_expression_p
14033 = parser->non_integral_constant_expression_p;
14034 parser->integral_constant_expression_p = false;
14036 /* Within a parenthesized expression, a `>' token is always
14037 the greater-than operator. */
14038 saved_greater_than_is_operator_p
14039 = parser->greater_than_is_operator_p;
14040 parser->greater_than_is_operator_p = true;
14042 /* Do not actually evaluate the expression. */
14043 ++cp_unevaluated_operand;
14045 /* Do not warn about problems with the expression. */
14046 ++c_inhibit_evaluation_warnings;
14048 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14050 /* Go back to evaluating expressions. */
14051 --cp_unevaluated_operand;
14052 --c_inhibit_evaluation_warnings;
14054 /* The `>' token might be the end of a template-id or
14055 template-parameter-list now. */
14056 parser->greater_than_is_operator_p
14057 = saved_greater_than_is_operator_p;
14059 /* Restore the old message and the integral constant expression
14060 flags. */
14061 parser->type_definition_forbidden_message = saved_message;
14062 parser->integral_constant_expression_p
14063 = saved_integral_constant_expression_p;
14064 parser->non_integral_constant_expression_p
14065 = saved_non_integral_constant_expression_p;
14067 /* Parse to the closing `)'. */
14068 if (!parens.require_close (parser))
14070 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14071 /*consume_paren=*/true);
14072 return error_mark_node;
14075 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14076 tf_warning_or_error);
14078 rewrite:
14079 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14080 it again. */
14081 start_token->type = CPP_DECLTYPE;
14082 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14083 start_token->u.tree_check_value->value = expr;
14084 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14085 start_token->keyword = RID_MAX;
14086 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14088 return expr;
14091 /* Special member functions [gram.special] */
14093 /* Parse a conversion-function-id.
14095 conversion-function-id:
14096 operator conversion-type-id
14098 Returns an IDENTIFIER_NODE representing the operator. */
14100 static tree
14101 cp_parser_conversion_function_id (cp_parser* parser)
14103 tree type;
14104 tree saved_scope;
14105 tree saved_qualifying_scope;
14106 tree saved_object_scope;
14107 tree pushed_scope = NULL_TREE;
14109 /* Look for the `operator' token. */
14110 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14111 return error_mark_node;
14112 /* When we parse the conversion-type-id, the current scope will be
14113 reset. However, we need that information in able to look up the
14114 conversion function later, so we save it here. */
14115 saved_scope = parser->scope;
14116 saved_qualifying_scope = parser->qualifying_scope;
14117 saved_object_scope = parser->object_scope;
14118 /* We must enter the scope of the class so that the names of
14119 entities declared within the class are available in the
14120 conversion-type-id. For example, consider:
14122 struct S {
14123 typedef int I;
14124 operator I();
14127 S::operator I() { ... }
14129 In order to see that `I' is a type-name in the definition, we
14130 must be in the scope of `S'. */
14131 if (saved_scope)
14132 pushed_scope = push_scope (saved_scope);
14133 /* Parse the conversion-type-id. */
14134 type = cp_parser_conversion_type_id (parser);
14135 /* Leave the scope of the class, if any. */
14136 if (pushed_scope)
14137 pop_scope (pushed_scope);
14138 /* Restore the saved scope. */
14139 parser->scope = saved_scope;
14140 parser->qualifying_scope = saved_qualifying_scope;
14141 parser->object_scope = saved_object_scope;
14142 /* If the TYPE is invalid, indicate failure. */
14143 if (type == error_mark_node)
14144 return error_mark_node;
14145 return make_conv_op_name (type);
14148 /* Parse a conversion-type-id:
14150 conversion-type-id:
14151 type-specifier-seq conversion-declarator [opt]
14153 Returns the TYPE specified. */
14155 static tree
14156 cp_parser_conversion_type_id (cp_parser* parser)
14158 tree attributes;
14159 cp_decl_specifier_seq type_specifiers;
14160 cp_declarator *declarator;
14161 tree type_specified;
14162 const char *saved_message;
14164 /* Parse the attributes. */
14165 attributes = cp_parser_attributes_opt (parser);
14167 saved_message = parser->type_definition_forbidden_message;
14168 parser->type_definition_forbidden_message
14169 = G_("types may not be defined in a conversion-type-id");
14171 /* Parse the type-specifiers. */
14172 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14173 /*is_trailing_return=*/false,
14174 &type_specifiers);
14176 parser->type_definition_forbidden_message = saved_message;
14178 /* If that didn't work, stop. */
14179 if (type_specifiers.type == error_mark_node)
14180 return error_mark_node;
14181 /* Parse the conversion-declarator. */
14182 declarator = cp_parser_conversion_declarator_opt (parser);
14184 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14185 /*initialized=*/0, &attributes);
14186 if (attributes)
14187 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14189 /* Don't give this error when parsing tentatively. This happens to
14190 work because we always parse this definitively once. */
14191 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14192 && type_uses_auto (type_specified))
14194 if (cxx_dialect < cxx14)
14196 error ("invalid use of %<auto%> in conversion operator");
14197 return error_mark_node;
14199 else if (template_parm_scope_p ())
14200 warning (0, "use of %<auto%> in member template "
14201 "conversion operator can never be deduced");
14204 return type_specified;
14207 /* Parse an (optional) conversion-declarator.
14209 conversion-declarator:
14210 ptr-operator conversion-declarator [opt]
14214 static cp_declarator *
14215 cp_parser_conversion_declarator_opt (cp_parser* parser)
14217 enum tree_code code;
14218 tree class_type, std_attributes = NULL_TREE;
14219 cp_cv_quals cv_quals;
14221 /* We don't know if there's a ptr-operator next, or not. */
14222 cp_parser_parse_tentatively (parser);
14223 /* Try the ptr-operator. */
14224 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14225 &std_attributes);
14226 /* If it worked, look for more conversion-declarators. */
14227 if (cp_parser_parse_definitely (parser))
14229 cp_declarator *declarator;
14231 /* Parse another optional declarator. */
14232 declarator = cp_parser_conversion_declarator_opt (parser);
14234 declarator = cp_parser_make_indirect_declarator
14235 (code, class_type, cv_quals, declarator, std_attributes);
14237 return declarator;
14240 return NULL;
14243 /* Parse an (optional) ctor-initializer.
14245 ctor-initializer:
14246 : mem-initializer-list */
14248 static void
14249 cp_parser_ctor_initializer_opt (cp_parser* parser)
14251 /* If the next token is not a `:', then there is no
14252 ctor-initializer. */
14253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14255 /* Do default initialization of any bases and members. */
14256 if (DECL_CONSTRUCTOR_P (current_function_decl))
14257 finish_mem_initializers (NULL_TREE);
14258 return;
14261 /* Consume the `:' token. */
14262 cp_lexer_consume_token (parser->lexer);
14263 /* And the mem-initializer-list. */
14264 cp_parser_mem_initializer_list (parser);
14267 /* Parse a mem-initializer-list.
14269 mem-initializer-list:
14270 mem-initializer ... [opt]
14271 mem-initializer ... [opt] , mem-initializer-list */
14273 static void
14274 cp_parser_mem_initializer_list (cp_parser* parser)
14276 tree mem_initializer_list = NULL_TREE;
14277 tree target_ctor = error_mark_node;
14278 cp_token *token = cp_lexer_peek_token (parser->lexer);
14280 /* Let the semantic analysis code know that we are starting the
14281 mem-initializer-list. */
14282 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14283 error_at (token->location,
14284 "only constructors take member initializers");
14286 /* Loop through the list. */
14287 while (true)
14289 tree mem_initializer;
14291 token = cp_lexer_peek_token (parser->lexer);
14292 /* Parse the mem-initializer. */
14293 mem_initializer = cp_parser_mem_initializer (parser);
14294 /* If the next token is a `...', we're expanding member initializers. */
14295 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14297 /* Consume the `...'. */
14298 cp_lexer_consume_token (parser->lexer);
14300 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14301 can be expanded but members cannot. */
14302 if (mem_initializer != error_mark_node
14303 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14305 error_at (token->location,
14306 "cannot expand initializer for member %qD",
14307 TREE_PURPOSE (mem_initializer));
14308 mem_initializer = error_mark_node;
14311 /* Construct the pack expansion type. */
14312 if (mem_initializer != error_mark_node)
14313 mem_initializer = make_pack_expansion (mem_initializer);
14315 if (target_ctor != error_mark_node
14316 && mem_initializer != error_mark_node)
14318 error ("mem-initializer for %qD follows constructor delegation",
14319 TREE_PURPOSE (mem_initializer));
14320 mem_initializer = error_mark_node;
14322 /* Look for a target constructor. */
14323 if (mem_initializer != error_mark_node
14324 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14325 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14327 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14328 if (mem_initializer_list)
14330 error ("constructor delegation follows mem-initializer for %qD",
14331 TREE_PURPOSE (mem_initializer_list));
14332 mem_initializer = error_mark_node;
14334 target_ctor = mem_initializer;
14336 /* Add it to the list, unless it was erroneous. */
14337 if (mem_initializer != error_mark_node)
14339 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14340 mem_initializer_list = mem_initializer;
14342 /* If the next token is not a `,', we're done. */
14343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14344 break;
14345 /* Consume the `,' token. */
14346 cp_lexer_consume_token (parser->lexer);
14349 /* Perform semantic analysis. */
14350 if (DECL_CONSTRUCTOR_P (current_function_decl))
14351 finish_mem_initializers (mem_initializer_list);
14354 /* Parse a mem-initializer.
14356 mem-initializer:
14357 mem-initializer-id ( expression-list [opt] )
14358 mem-initializer-id braced-init-list
14360 GNU extension:
14362 mem-initializer:
14363 ( expression-list [opt] )
14365 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14366 class) or FIELD_DECL (for a non-static data member) to initialize;
14367 the TREE_VALUE is the expression-list. An empty initialization
14368 list is represented by void_list_node. */
14370 static tree
14371 cp_parser_mem_initializer (cp_parser* parser)
14373 tree mem_initializer_id;
14374 tree expression_list;
14375 tree member;
14376 cp_token *token = cp_lexer_peek_token (parser->lexer);
14378 /* Find out what is being initialized. */
14379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14381 permerror (token->location,
14382 "anachronistic old-style base class initializer");
14383 mem_initializer_id = NULL_TREE;
14385 else
14387 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14388 if (mem_initializer_id == error_mark_node)
14389 return mem_initializer_id;
14391 member = expand_member_init (mem_initializer_id);
14392 if (member && !DECL_P (member))
14393 in_base_initializer = 1;
14395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14397 bool expr_non_constant_p;
14398 cp_lexer_set_source_position (parser->lexer);
14399 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14400 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14401 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14402 expression_list = build_tree_list (NULL_TREE, expression_list);
14404 else
14406 vec<tree, va_gc> *vec;
14407 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14408 /*cast_p=*/false,
14409 /*allow_expansion_p=*/true,
14410 /*non_constant_p=*/NULL);
14411 if (vec == NULL)
14412 return error_mark_node;
14413 expression_list = build_tree_list_vec (vec);
14414 release_tree_vector (vec);
14417 if (expression_list == error_mark_node)
14418 return error_mark_node;
14419 if (!expression_list)
14420 expression_list = void_type_node;
14422 in_base_initializer = 0;
14424 return member ? build_tree_list (member, expression_list) : error_mark_node;
14427 /* Parse a mem-initializer-id.
14429 mem-initializer-id:
14430 :: [opt] nested-name-specifier [opt] class-name
14431 decltype-specifier (C++11)
14432 identifier
14434 Returns a TYPE indicating the class to be initialized for the first
14435 production (and the second in C++11). Returns an IDENTIFIER_NODE
14436 indicating the data member to be initialized for the last production. */
14438 static tree
14439 cp_parser_mem_initializer_id (cp_parser* parser)
14441 bool global_scope_p;
14442 bool nested_name_specifier_p;
14443 bool template_p = false;
14444 tree id;
14446 cp_token *token = cp_lexer_peek_token (parser->lexer);
14448 /* `typename' is not allowed in this context ([temp.res]). */
14449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14451 error_at (token->location,
14452 "keyword %<typename%> not allowed in this context (a qualified "
14453 "member initializer is implicitly a type)");
14454 cp_lexer_consume_token (parser->lexer);
14456 /* Look for the optional `::' operator. */
14457 global_scope_p
14458 = (cp_parser_global_scope_opt (parser,
14459 /*current_scope_valid_p=*/false)
14460 != NULL_TREE);
14461 /* Look for the optional nested-name-specifier. The simplest way to
14462 implement:
14464 [temp.res]
14466 The keyword `typename' is not permitted in a base-specifier or
14467 mem-initializer; in these contexts a qualified name that
14468 depends on a template-parameter is implicitly assumed to be a
14469 type name.
14471 is to assume that we have seen the `typename' keyword at this
14472 point. */
14473 nested_name_specifier_p
14474 = (cp_parser_nested_name_specifier_opt (parser,
14475 /*typename_keyword_p=*/true,
14476 /*check_dependency_p=*/true,
14477 /*type_p=*/true,
14478 /*is_declaration=*/true)
14479 != NULL_TREE);
14480 if (nested_name_specifier_p)
14481 template_p = cp_parser_optional_template_keyword (parser);
14482 /* If there is a `::' operator or a nested-name-specifier, then we
14483 are definitely looking for a class-name. */
14484 if (global_scope_p || nested_name_specifier_p)
14485 return cp_parser_class_name (parser,
14486 /*typename_keyword_p=*/true,
14487 /*template_keyword_p=*/template_p,
14488 typename_type,
14489 /*check_dependency_p=*/true,
14490 /*class_head_p=*/false,
14491 /*is_declaration=*/true);
14492 /* Otherwise, we could also be looking for an ordinary identifier. */
14493 cp_parser_parse_tentatively (parser);
14494 if (cp_lexer_next_token_is_decltype (parser->lexer))
14495 /* Try a decltype-specifier. */
14496 id = cp_parser_decltype (parser);
14497 else
14498 /* Otherwise, try a class-name. */
14499 id = cp_parser_class_name (parser,
14500 /*typename_keyword_p=*/true,
14501 /*template_keyword_p=*/false,
14502 none_type,
14503 /*check_dependency_p=*/true,
14504 /*class_head_p=*/false,
14505 /*is_declaration=*/true);
14506 /* If we found one, we're done. */
14507 if (cp_parser_parse_definitely (parser))
14508 return id;
14509 /* Otherwise, look for an ordinary identifier. */
14510 return cp_parser_identifier (parser);
14513 /* Overloading [gram.over] */
14515 /* Parse an operator-function-id.
14517 operator-function-id:
14518 operator operator
14520 Returns an IDENTIFIER_NODE for the operator which is a
14521 human-readable spelling of the identifier, e.g., `operator +'. */
14523 static cp_expr
14524 cp_parser_operator_function_id (cp_parser* parser)
14526 /* Look for the `operator' keyword. */
14527 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14528 return error_mark_node;
14529 /* And then the name of the operator itself. */
14530 return cp_parser_operator (parser);
14533 /* Return an identifier node for a user-defined literal operator.
14534 The suffix identifier is chained to the operator name identifier. */
14536 tree
14537 cp_literal_operator_id (const char* name)
14539 tree identifier;
14540 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14541 + strlen (name) + 10);
14542 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14543 identifier = get_identifier (buffer);
14545 return identifier;
14548 /* Parse an operator.
14550 operator:
14551 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14552 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14553 || ++ -- , ->* -> () []
14555 GNU Extensions:
14557 operator:
14558 <? >? <?= >?=
14560 Returns an IDENTIFIER_NODE for the operator which is a
14561 human-readable spelling of the identifier, e.g., `operator +'. */
14563 static cp_expr
14564 cp_parser_operator (cp_parser* parser)
14566 tree id = NULL_TREE;
14567 cp_token *token;
14568 bool utf8 = false;
14570 /* Peek at the next token. */
14571 token = cp_lexer_peek_token (parser->lexer);
14573 location_t start_loc = token->location;
14575 /* Figure out which operator we have. */
14576 enum tree_code op = ERROR_MARK;
14577 bool assop = false;
14578 bool consumed = false;
14579 switch (token->type)
14581 case CPP_KEYWORD:
14583 /* The keyword should be either `new' or `delete'. */
14584 if (token->keyword == RID_NEW)
14585 op = NEW_EXPR;
14586 else if (token->keyword == RID_DELETE)
14587 op = DELETE_EXPR;
14588 else
14589 break;
14591 /* Consume the `new' or `delete' token. */
14592 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14594 /* Peek at the next token. */
14595 token = cp_lexer_peek_token (parser->lexer);
14596 /* If it's a `[' token then this is the array variant of the
14597 operator. */
14598 if (token->type == CPP_OPEN_SQUARE)
14600 /* Consume the `[' token. */
14601 cp_lexer_consume_token (parser->lexer);
14602 /* Look for the `]' token. */
14603 if (cp_token *close_token
14604 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14605 end_loc = close_token->location;
14606 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14608 start_loc = make_location (start_loc, start_loc, end_loc);
14609 consumed = true;
14610 break;
14613 case CPP_PLUS:
14614 op = PLUS_EXPR;
14615 break;
14617 case CPP_MINUS:
14618 op = MINUS_EXPR;
14619 break;
14621 case CPP_MULT:
14622 op = MULT_EXPR;
14623 break;
14625 case CPP_DIV:
14626 op = TRUNC_DIV_EXPR;
14627 break;
14629 case CPP_MOD:
14630 op = TRUNC_MOD_EXPR;
14631 break;
14633 case CPP_XOR:
14634 op = BIT_XOR_EXPR;
14635 break;
14637 case CPP_AND:
14638 op = BIT_AND_EXPR;
14639 break;
14641 case CPP_OR:
14642 op = BIT_IOR_EXPR;
14643 break;
14645 case CPP_COMPL:
14646 op = BIT_NOT_EXPR;
14647 break;
14649 case CPP_NOT:
14650 op = TRUTH_NOT_EXPR;
14651 break;
14653 case CPP_EQ:
14654 assop = true;
14655 op = NOP_EXPR;
14656 break;
14658 case CPP_LESS:
14659 op = LT_EXPR;
14660 break;
14662 case CPP_GREATER:
14663 op = GT_EXPR;
14664 break;
14666 case CPP_PLUS_EQ:
14667 assop = true;
14668 op = PLUS_EXPR;
14669 break;
14671 case CPP_MINUS_EQ:
14672 assop = true;
14673 op = MINUS_EXPR;
14674 break;
14676 case CPP_MULT_EQ:
14677 assop = true;
14678 op = MULT_EXPR;
14679 break;
14681 case CPP_DIV_EQ:
14682 assop = true;
14683 op = TRUNC_DIV_EXPR;
14684 break;
14686 case CPP_MOD_EQ:
14687 assop = true;
14688 op = TRUNC_MOD_EXPR;
14689 break;
14691 case CPP_XOR_EQ:
14692 assop = true;
14693 op = BIT_XOR_EXPR;
14694 break;
14696 case CPP_AND_EQ:
14697 assop = true;
14698 op = BIT_AND_EXPR;
14699 break;
14701 case CPP_OR_EQ:
14702 assop = true;
14703 op = BIT_IOR_EXPR;
14704 break;
14706 case CPP_LSHIFT:
14707 op = LSHIFT_EXPR;
14708 break;
14710 case CPP_RSHIFT:
14711 op = RSHIFT_EXPR;
14712 break;
14714 case CPP_LSHIFT_EQ:
14715 assop = true;
14716 op = LSHIFT_EXPR;
14717 break;
14719 case CPP_RSHIFT_EQ:
14720 assop = true;
14721 op = RSHIFT_EXPR;
14722 break;
14724 case CPP_EQ_EQ:
14725 op = EQ_EXPR;
14726 break;
14728 case CPP_NOT_EQ:
14729 op = NE_EXPR;
14730 break;
14732 case CPP_LESS_EQ:
14733 op = LE_EXPR;
14734 break;
14736 case CPP_GREATER_EQ:
14737 op = GE_EXPR;
14738 break;
14740 case CPP_AND_AND:
14741 op = TRUTH_ANDIF_EXPR;
14742 break;
14744 case CPP_OR_OR:
14745 op = TRUTH_ORIF_EXPR;
14746 break;
14748 case CPP_PLUS_PLUS:
14749 op = POSTINCREMENT_EXPR;
14750 break;
14752 case CPP_MINUS_MINUS:
14753 op = PREDECREMENT_EXPR;
14754 break;
14756 case CPP_COMMA:
14757 op = COMPOUND_EXPR;
14758 break;
14760 case CPP_DEREF_STAR:
14761 op = MEMBER_REF;
14762 break;
14764 case CPP_DEREF:
14765 op = COMPONENT_REF;
14766 break;
14768 case CPP_OPEN_PAREN:
14770 /* Consume the `('. */
14771 matching_parens parens;
14772 parens.consume_open (parser);
14773 /* Look for the matching `)'. */
14774 parens.require_close (parser);
14775 op = CALL_EXPR;
14776 consumed = true;
14777 break;
14780 case CPP_OPEN_SQUARE:
14781 /* Consume the `['. */
14782 cp_lexer_consume_token (parser->lexer);
14783 /* Look for the matching `]'. */
14784 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14785 op = ARRAY_REF;
14786 consumed = true;
14787 break;
14789 case CPP_UTF8STRING:
14790 case CPP_UTF8STRING_USERDEF:
14791 utf8 = true;
14792 /* FALLTHRU */
14793 case CPP_STRING:
14794 case CPP_WSTRING:
14795 case CPP_STRING16:
14796 case CPP_STRING32:
14797 case CPP_STRING_USERDEF:
14798 case CPP_WSTRING_USERDEF:
14799 case CPP_STRING16_USERDEF:
14800 case CPP_STRING32_USERDEF:
14802 tree str, string_tree;
14803 int sz, len;
14805 if (cxx_dialect == cxx98)
14806 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14808 /* Consume the string. */
14809 str = cp_parser_string_literal (parser, /*translate=*/true,
14810 /*wide_ok=*/true, /*lookup_udlit=*/false);
14811 if (str == error_mark_node)
14812 return error_mark_node;
14813 else if (TREE_CODE (str) == USERDEF_LITERAL)
14815 string_tree = USERDEF_LITERAL_VALUE (str);
14816 id = USERDEF_LITERAL_SUFFIX_ID (str);
14818 else
14820 string_tree = str;
14821 /* Look for the suffix identifier. */
14822 token = cp_lexer_peek_token (parser->lexer);
14823 if (token->type == CPP_NAME)
14824 id = cp_parser_identifier (parser);
14825 else if (token->type == CPP_KEYWORD)
14827 error ("unexpected keyword;"
14828 " remove space between quotes and suffix identifier");
14829 return error_mark_node;
14831 else
14833 error ("expected suffix identifier");
14834 return error_mark_node;
14837 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14838 (TREE_TYPE (TREE_TYPE (string_tree))));
14839 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14840 if (len != 0)
14842 error ("expected empty string after %<operator%> keyword");
14843 return error_mark_node;
14845 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14846 != char_type_node)
14848 error ("invalid encoding prefix in literal operator");
14849 return error_mark_node;
14851 if (id != error_mark_node)
14853 const char *name = IDENTIFIER_POINTER (id);
14854 id = cp_literal_operator_id (name);
14856 return id;
14859 default:
14860 /* Anything else is an error. */
14861 break;
14864 /* If we have selected an identifier, we need to consume the
14865 operator token. */
14866 if (op != ERROR_MARK)
14868 id = ovl_op_identifier (assop, op);
14869 if (!consumed)
14870 cp_lexer_consume_token (parser->lexer);
14872 /* Otherwise, no valid operator name was present. */
14873 else
14875 cp_parser_error (parser, "expected operator");
14876 id = error_mark_node;
14879 return cp_expr (id, start_loc);
14882 /* Parse a template-declaration.
14884 template-declaration:
14885 export [opt] template < template-parameter-list > declaration
14887 If MEMBER_P is TRUE, this template-declaration occurs within a
14888 class-specifier.
14890 The grammar rule given by the standard isn't correct. What
14891 is really meant is:
14893 template-declaration:
14894 export [opt] template-parameter-list-seq
14895 decl-specifier-seq [opt] init-declarator [opt] ;
14896 export [opt] template-parameter-list-seq
14897 function-definition
14899 template-parameter-list-seq:
14900 template-parameter-list-seq [opt]
14901 template < template-parameter-list >
14903 Concept Extensions:
14905 template-parameter-list-seq:
14906 template < template-parameter-list > requires-clause [opt]
14908 requires-clause:
14909 requires logical-or-expression */
14911 static void
14912 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14914 /* Check for `export'. */
14915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14917 /* Consume the `export' token. */
14918 cp_lexer_consume_token (parser->lexer);
14919 /* Warn that we do not support `export'. */
14920 warning (0, "keyword %<export%> not implemented, and will be ignored");
14923 cp_parser_template_declaration_after_export (parser, member_p);
14926 /* Parse a template-parameter-list.
14928 template-parameter-list:
14929 template-parameter
14930 template-parameter-list , template-parameter
14932 Returns a TREE_LIST. Each node represents a template parameter.
14933 The nodes are connected via their TREE_CHAINs. */
14935 static tree
14936 cp_parser_template_parameter_list (cp_parser* parser)
14938 tree parameter_list = NULL_TREE;
14940 begin_template_parm_list ();
14942 /* The loop below parses the template parms. We first need to know
14943 the total number of template parms to be able to compute proper
14944 canonical types of each dependent type. So after the loop, when
14945 we know the total number of template parms,
14946 end_template_parm_list computes the proper canonical types and
14947 fixes up the dependent types accordingly. */
14948 while (true)
14950 tree parameter;
14951 bool is_non_type;
14952 bool is_parameter_pack;
14953 location_t parm_loc;
14955 /* Parse the template-parameter. */
14956 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14957 parameter = cp_parser_template_parameter (parser,
14958 &is_non_type,
14959 &is_parameter_pack);
14960 /* Add it to the list. */
14961 if (parameter != error_mark_node)
14962 parameter_list = process_template_parm (parameter_list,
14963 parm_loc,
14964 parameter,
14965 is_non_type,
14966 is_parameter_pack);
14967 else
14969 tree err_parm = build_tree_list (parameter, parameter);
14970 parameter_list = chainon (parameter_list, err_parm);
14973 /* If the next token is not a `,', we're done. */
14974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14975 break;
14976 /* Otherwise, consume the `,' token. */
14977 cp_lexer_consume_token (parser->lexer);
14980 return end_template_parm_list (parameter_list);
14983 /* Parse a introduction-list.
14985 introduction-list:
14986 introduced-parameter
14987 introduction-list , introduced-parameter
14989 introduced-parameter:
14990 ...[opt] identifier
14992 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14993 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14994 WILDCARD_DECL will also have DECL_NAME set and token location in
14995 DECL_SOURCE_LOCATION. */
14997 static tree
14998 cp_parser_introduction_list (cp_parser *parser)
15000 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15002 while (true)
15004 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15005 if (is_pack)
15006 cp_lexer_consume_token (parser->lexer);
15008 /* Build placeholder. */
15009 tree parm = build_nt (WILDCARD_DECL);
15010 DECL_SOURCE_LOCATION (parm)
15011 = cp_lexer_peek_token (parser->lexer)->location;
15012 DECL_NAME (parm) = cp_parser_identifier (parser);
15013 WILDCARD_PACK_P (parm) = is_pack;
15014 vec_safe_push (introduction_vec, parm);
15016 /* If the next token is not a `,', we're done. */
15017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15018 break;
15019 /* Otherwise, consume the `,' token. */
15020 cp_lexer_consume_token (parser->lexer);
15023 /* Convert the vec into a TREE_VEC. */
15024 tree introduction_list = make_tree_vec (introduction_vec->length ());
15025 unsigned int n;
15026 tree parm;
15027 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15028 TREE_VEC_ELT (introduction_list, n) = parm;
15030 release_tree_vector (introduction_vec);
15031 return introduction_list;
15034 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15035 is an abstract declarator. */
15037 static inline cp_declarator*
15038 get_id_declarator (cp_declarator *declarator)
15040 cp_declarator *d = declarator;
15041 while (d && d->kind != cdk_id)
15042 d = d->declarator;
15043 return d;
15046 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15047 is an abstract declarator. */
15049 static inline tree
15050 get_unqualified_id (cp_declarator *declarator)
15052 declarator = get_id_declarator (declarator);
15053 if (declarator)
15054 return declarator->u.id.unqualified_name;
15055 else
15056 return NULL_TREE;
15059 /* Returns true if DECL represents a constrained-parameter. */
15061 static inline bool
15062 is_constrained_parameter (tree decl)
15064 return (decl
15065 && TREE_CODE (decl) == TYPE_DECL
15066 && CONSTRAINED_PARM_CONCEPT (decl)
15067 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15070 /* Returns true if PARM declares a constrained-parameter. */
15072 static inline bool
15073 is_constrained_parameter (cp_parameter_declarator *parm)
15075 return is_constrained_parameter (parm->decl_specifiers.type);
15078 /* Check that the type parameter is only a declarator-id, and that its
15079 type is not cv-qualified. */
15081 bool
15082 cp_parser_check_constrained_type_parm (cp_parser *parser,
15083 cp_parameter_declarator *parm)
15085 if (!parm->declarator)
15086 return true;
15088 if (parm->declarator->kind != cdk_id)
15090 cp_parser_error (parser, "invalid constrained type parameter");
15091 return false;
15094 /* Don't allow cv-qualified type parameters. */
15095 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15096 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15098 cp_parser_error (parser, "cv-qualified type parameter");
15099 return false;
15102 return true;
15105 /* Finish parsing/processing a template type parameter and checking
15106 various restrictions. */
15108 static inline tree
15109 cp_parser_constrained_type_template_parm (cp_parser *parser,
15110 tree id,
15111 cp_parameter_declarator* parmdecl)
15113 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15114 return finish_template_type_parm (class_type_node, id);
15115 else
15116 return error_mark_node;
15119 static tree
15120 finish_constrained_template_template_parm (tree proto, tree id)
15122 /* FIXME: This should probably be copied, and we may need to adjust
15123 the template parameter depths. */
15124 tree saved_parms = current_template_parms;
15125 begin_template_parm_list ();
15126 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15127 end_template_parm_list ();
15129 tree parm = finish_template_template_parm (class_type_node, id);
15130 current_template_parms = saved_parms;
15132 return parm;
15135 /* Finish parsing/processing a template template parameter by borrowing
15136 the template parameter list from the prototype parameter. */
15138 static tree
15139 cp_parser_constrained_template_template_parm (cp_parser *parser,
15140 tree proto,
15141 tree id,
15142 cp_parameter_declarator *parmdecl)
15144 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15145 return error_mark_node;
15146 return finish_constrained_template_template_parm (proto, id);
15149 /* Create a new non-type template parameter from the given PARM
15150 declarator. */
15152 static tree
15153 constrained_non_type_template_parm (bool *is_non_type,
15154 cp_parameter_declarator *parm)
15156 *is_non_type = true;
15157 cp_declarator *decl = parm->declarator;
15158 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15159 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15160 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15163 /* Build a constrained template parameter based on the PARMDECL
15164 declarator. The type of PARMDECL is the constrained type, which
15165 refers to the prototype template parameter that ultimately
15166 specifies the type of the declared parameter. */
15168 static tree
15169 finish_constrained_parameter (cp_parser *parser,
15170 cp_parameter_declarator *parmdecl,
15171 bool *is_non_type,
15172 bool *is_parameter_pack)
15174 tree decl = parmdecl->decl_specifiers.type;
15175 tree id = get_unqualified_id (parmdecl->declarator);
15176 tree def = parmdecl->default_argument;
15177 tree proto = DECL_INITIAL (decl);
15179 /* A template parameter constrained by a variadic concept shall also
15180 be declared as a template parameter pack. */
15181 bool is_variadic = template_parameter_pack_p (proto);
15182 if (is_variadic && !*is_parameter_pack)
15183 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15185 /* Build the parameter. Return an error if the declarator was invalid. */
15186 tree parm;
15187 if (TREE_CODE (proto) == TYPE_DECL)
15188 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15189 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15190 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15191 parmdecl);
15192 else
15193 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15194 if (parm == error_mark_node)
15195 return error_mark_node;
15197 /* Finish the parameter decl and create a node attaching the
15198 default argument and constraint. */
15199 parm = build_tree_list (def, parm);
15200 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15202 return parm;
15205 /* Returns true if the parsed type actually represents the declaration
15206 of a type template-parameter. */
15208 static inline bool
15209 declares_constrained_type_template_parameter (tree type)
15211 return (is_constrained_parameter (type)
15212 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15216 /* Returns true if the parsed type actually represents the declaration of
15217 a template template-parameter. */
15219 static bool
15220 declares_constrained_template_template_parameter (tree type)
15222 return (is_constrained_parameter (type)
15223 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15226 /* Parse a default argument for a type template-parameter.
15227 Note that diagnostics are handled in cp_parser_template_parameter. */
15229 static tree
15230 cp_parser_default_type_template_argument (cp_parser *parser)
15232 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15234 /* Consume the `=' token. */
15235 cp_lexer_consume_token (parser->lexer);
15237 cp_token *token = cp_lexer_peek_token (parser->lexer);
15239 /* Parse the default-argument. */
15240 push_deferring_access_checks (dk_no_deferred);
15241 tree default_argument = cp_parser_type_id (parser);
15242 pop_deferring_access_checks ();
15244 if (flag_concepts && type_uses_auto (default_argument))
15246 error_at (token->location,
15247 "invalid use of %<auto%> in default template argument");
15248 return error_mark_node;
15251 return default_argument;
15254 /* Parse a default argument for a template template-parameter. */
15256 static tree
15257 cp_parser_default_template_template_argument (cp_parser *parser)
15259 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15261 bool is_template;
15263 /* Consume the `='. */
15264 cp_lexer_consume_token (parser->lexer);
15265 /* Parse the id-expression. */
15266 push_deferring_access_checks (dk_no_deferred);
15267 /* save token before parsing the id-expression, for error
15268 reporting */
15269 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15270 tree default_argument
15271 = cp_parser_id_expression (parser,
15272 /*template_keyword_p=*/false,
15273 /*check_dependency_p=*/true,
15274 /*template_p=*/&is_template,
15275 /*declarator_p=*/false,
15276 /*optional_p=*/false);
15277 if (TREE_CODE (default_argument) == TYPE_DECL)
15278 /* If the id-expression was a template-id that refers to
15279 a template-class, we already have the declaration here,
15280 so no further lookup is needed. */
15282 else
15283 /* Look up the name. */
15284 default_argument
15285 = cp_parser_lookup_name (parser, default_argument,
15286 none_type,
15287 /*is_template=*/is_template,
15288 /*is_namespace=*/false,
15289 /*check_dependency=*/true,
15290 /*ambiguous_decls=*/NULL,
15291 token->location);
15292 /* See if the default argument is valid. */
15293 default_argument = check_template_template_default_arg (default_argument);
15294 pop_deferring_access_checks ();
15295 return default_argument;
15298 /* Parse a template-parameter.
15300 template-parameter:
15301 type-parameter
15302 parameter-declaration
15304 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15305 the parameter. The TREE_PURPOSE is the default value, if any.
15306 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15307 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15308 set to true iff this parameter is a parameter pack. */
15310 static tree
15311 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15312 bool *is_parameter_pack)
15314 cp_token *token;
15315 cp_parameter_declarator *parameter_declarator;
15316 tree parm;
15318 /* Assume it is a type parameter or a template parameter. */
15319 *is_non_type = false;
15320 /* Assume it not a parameter pack. */
15321 *is_parameter_pack = false;
15322 /* Peek at the next token. */
15323 token = cp_lexer_peek_token (parser->lexer);
15324 /* If it is `template', we have a type-parameter. */
15325 if (token->keyword == RID_TEMPLATE)
15326 return cp_parser_type_parameter (parser, is_parameter_pack);
15327 /* If it is `class' or `typename' we do not know yet whether it is a
15328 type parameter or a non-type parameter. Consider:
15330 template <typename T, typename T::X X> ...
15334 template <class C, class D*> ...
15336 Here, the first parameter is a type parameter, and the second is
15337 a non-type parameter. We can tell by looking at the token after
15338 the identifier -- if it is a `,', `=', or `>' then we have a type
15339 parameter. */
15340 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15342 /* Peek at the token after `class' or `typename'. */
15343 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15344 /* If it's an ellipsis, we have a template type parameter
15345 pack. */
15346 if (token->type == CPP_ELLIPSIS)
15347 return cp_parser_type_parameter (parser, is_parameter_pack);
15348 /* If it's an identifier, skip it. */
15349 if (token->type == CPP_NAME)
15350 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15351 /* Now, see if the token looks like the end of a template
15352 parameter. */
15353 if (token->type == CPP_COMMA
15354 || token->type == CPP_EQ
15355 || token->type == CPP_GREATER)
15356 return cp_parser_type_parameter (parser, is_parameter_pack);
15359 /* Otherwise, it is a non-type parameter or a constrained parameter.
15361 [temp.param]
15363 When parsing a default template-argument for a non-type
15364 template-parameter, the first non-nested `>' is taken as the end
15365 of the template parameter-list rather than a greater-than
15366 operator. */
15367 parameter_declarator
15368 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15369 /*parenthesized_p=*/NULL);
15371 if (!parameter_declarator)
15372 return error_mark_node;
15374 /* If the parameter declaration is marked as a parameter pack, set
15375 *IS_PARAMETER_PACK to notify the caller. */
15376 if (parameter_declarator->template_parameter_pack_p)
15377 *is_parameter_pack = true;
15379 if (parameter_declarator->default_argument)
15381 /* Can happen in some cases of erroneous input (c++/34892). */
15382 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15383 /* Consume the `...' for better error recovery. */
15384 cp_lexer_consume_token (parser->lexer);
15387 // The parameter may have been constrained.
15388 if (is_constrained_parameter (parameter_declarator))
15389 return finish_constrained_parameter (parser,
15390 parameter_declarator,
15391 is_non_type,
15392 is_parameter_pack);
15394 // Now we're sure that the parameter is a non-type parameter.
15395 *is_non_type = true;
15397 parm = grokdeclarator (parameter_declarator->declarator,
15398 &parameter_declarator->decl_specifiers,
15399 TPARM, /*initialized=*/0,
15400 /*attrlist=*/NULL);
15401 if (parm == error_mark_node)
15402 return error_mark_node;
15404 return build_tree_list (parameter_declarator->default_argument, parm);
15407 /* Parse a type-parameter.
15409 type-parameter:
15410 class identifier [opt]
15411 class identifier [opt] = type-id
15412 typename identifier [opt]
15413 typename identifier [opt] = type-id
15414 template < template-parameter-list > class identifier [opt]
15415 template < template-parameter-list > class identifier [opt]
15416 = id-expression
15418 GNU Extension (variadic templates):
15420 type-parameter:
15421 class ... identifier [opt]
15422 typename ... identifier [opt]
15424 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15425 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15426 the declaration of the parameter.
15428 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15430 static tree
15431 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15433 cp_token *token;
15434 tree parameter;
15436 /* Look for a keyword to tell us what kind of parameter this is. */
15437 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15438 if (!token)
15439 return error_mark_node;
15441 switch (token->keyword)
15443 case RID_CLASS:
15444 case RID_TYPENAME:
15446 tree identifier;
15447 tree default_argument;
15449 /* If the next token is an ellipsis, we have a template
15450 argument pack. */
15451 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15453 /* Consume the `...' token. */
15454 cp_lexer_consume_token (parser->lexer);
15455 maybe_warn_variadic_templates ();
15457 *is_parameter_pack = true;
15460 /* If the next token is an identifier, then it names the
15461 parameter. */
15462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15463 identifier = cp_parser_identifier (parser);
15464 else
15465 identifier = NULL_TREE;
15467 /* Create the parameter. */
15468 parameter = finish_template_type_parm (class_type_node, identifier);
15470 /* If the next token is an `=', we have a default argument. */
15471 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15473 default_argument
15474 = cp_parser_default_type_template_argument (parser);
15476 /* Template parameter packs cannot have default
15477 arguments. */
15478 if (*is_parameter_pack)
15480 if (identifier)
15481 error_at (token->location,
15482 "template parameter pack %qD cannot have a "
15483 "default argument", identifier);
15484 else
15485 error_at (token->location,
15486 "template parameter packs cannot have "
15487 "default arguments");
15488 default_argument = NULL_TREE;
15490 else if (check_for_bare_parameter_packs (default_argument))
15491 default_argument = error_mark_node;
15493 else
15494 default_argument = NULL_TREE;
15496 /* Create the combined representation of the parameter and the
15497 default argument. */
15498 parameter = build_tree_list (default_argument, parameter);
15500 break;
15502 case RID_TEMPLATE:
15504 tree identifier;
15505 tree default_argument;
15507 /* Look for the `<'. */
15508 cp_parser_require (parser, CPP_LESS, RT_LESS);
15509 /* Parse the template-parameter-list. */
15510 cp_parser_template_parameter_list (parser);
15511 /* Look for the `>'. */
15512 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15514 // If template requirements are present, parse them.
15515 if (flag_concepts)
15517 tree reqs = get_shorthand_constraints (current_template_parms);
15518 if (tree r = cp_parser_requires_clause_opt (parser))
15519 reqs = conjoin_constraints (reqs, normalize_expression (r));
15520 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15523 /* Look for the `class' or 'typename' keywords. */
15524 cp_parser_type_parameter_key (parser);
15525 /* If the next token is an ellipsis, we have a template
15526 argument pack. */
15527 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15529 /* Consume the `...' token. */
15530 cp_lexer_consume_token (parser->lexer);
15531 maybe_warn_variadic_templates ();
15533 *is_parameter_pack = true;
15535 /* If the next token is an `=', then there is a
15536 default-argument. If the next token is a `>', we are at
15537 the end of the parameter-list. If the next token is a `,',
15538 then we are at the end of this parameter. */
15539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15540 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15541 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15543 identifier = cp_parser_identifier (parser);
15544 /* Treat invalid names as if the parameter were nameless. */
15545 if (identifier == error_mark_node)
15546 identifier = NULL_TREE;
15548 else
15549 identifier = NULL_TREE;
15551 /* Create the template parameter. */
15552 parameter = finish_template_template_parm (class_type_node,
15553 identifier);
15555 /* If the next token is an `=', then there is a
15556 default-argument. */
15557 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15559 default_argument
15560 = cp_parser_default_template_template_argument (parser);
15562 /* Template parameter packs cannot have default
15563 arguments. */
15564 if (*is_parameter_pack)
15566 if (identifier)
15567 error_at (token->location,
15568 "template parameter pack %qD cannot "
15569 "have a default argument",
15570 identifier);
15571 else
15572 error_at (token->location, "template parameter packs cannot "
15573 "have default arguments");
15574 default_argument = NULL_TREE;
15577 else
15578 default_argument = NULL_TREE;
15580 /* Create the combined representation of the parameter and the
15581 default argument. */
15582 parameter = build_tree_list (default_argument, parameter);
15584 break;
15586 default:
15587 gcc_unreachable ();
15588 break;
15591 return parameter;
15594 /* Parse a template-id.
15596 template-id:
15597 template-name < template-argument-list [opt] >
15599 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15600 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15601 returned. Otherwise, if the template-name names a function, or set
15602 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15603 names a class, returns a TYPE_DECL for the specialization.
15605 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15606 uninstantiated templates. */
15608 static tree
15609 cp_parser_template_id (cp_parser *parser,
15610 bool template_keyword_p,
15611 bool check_dependency_p,
15612 enum tag_types tag_type,
15613 bool is_declaration)
15615 tree templ;
15616 tree arguments;
15617 tree template_id;
15618 cp_token_position start_of_id = 0;
15619 cp_token *next_token = NULL, *next_token_2 = NULL;
15620 bool is_identifier;
15622 /* If the next token corresponds to a template-id, there is no need
15623 to reparse it. */
15624 cp_token *token = cp_lexer_peek_token (parser->lexer);
15625 if (token->type == CPP_TEMPLATE_ID)
15627 cp_lexer_consume_token (parser->lexer);
15628 return saved_checks_value (token->u.tree_check_value);
15631 /* Avoid performing name lookup if there is no possibility of
15632 finding a template-id. */
15633 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15634 || (token->type == CPP_NAME
15635 && !cp_parser_nth_token_starts_template_argument_list_p
15636 (parser, 2)))
15638 cp_parser_error (parser, "expected template-id");
15639 return error_mark_node;
15642 /* Remember where the template-id starts. */
15643 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15644 start_of_id = cp_lexer_token_position (parser->lexer, false);
15646 push_deferring_access_checks (dk_deferred);
15648 /* Parse the template-name. */
15649 is_identifier = false;
15650 templ = cp_parser_template_name (parser, template_keyword_p,
15651 check_dependency_p,
15652 is_declaration,
15653 tag_type,
15654 &is_identifier);
15655 if (templ == error_mark_node || is_identifier)
15657 pop_deferring_access_checks ();
15658 return templ;
15661 /* Since we're going to preserve any side-effects from this parse, set up a
15662 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15663 in the template arguments. */
15664 tentative_firewall firewall (parser);
15666 /* If we find the sequence `[:' after a template-name, it's probably
15667 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15668 parse correctly the argument list. */
15669 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15670 == CPP_OPEN_SQUARE)
15671 && next_token->flags & DIGRAPH
15672 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15673 == CPP_COLON)
15674 && !(next_token_2->flags & PREV_WHITE))
15676 cp_parser_parse_tentatively (parser);
15677 /* Change `:' into `::'. */
15678 next_token_2->type = CPP_SCOPE;
15679 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15680 CPP_LESS. */
15681 cp_lexer_consume_token (parser->lexer);
15683 /* Parse the arguments. */
15684 arguments = cp_parser_enclosed_template_argument_list (parser);
15685 if (!cp_parser_parse_definitely (parser))
15687 /* If we couldn't parse an argument list, then we revert our changes
15688 and return simply an error. Maybe this is not a template-id
15689 after all. */
15690 next_token_2->type = CPP_COLON;
15691 cp_parser_error (parser, "expected %<<%>");
15692 pop_deferring_access_checks ();
15693 return error_mark_node;
15695 /* Otherwise, emit an error about the invalid digraph, but continue
15696 parsing because we got our argument list. */
15697 if (permerror (next_token->location,
15698 "%<<::%> cannot begin a template-argument list"))
15700 static bool hint = false;
15701 inform (next_token->location,
15702 "%<<:%> is an alternate spelling for %<[%>."
15703 " Insert whitespace between %<<%> and %<::%>");
15704 if (!hint && !flag_permissive)
15706 inform (next_token->location, "(if you use %<-fpermissive%> "
15707 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15708 "accept your code)");
15709 hint = true;
15713 else
15715 /* Look for the `<' that starts the template-argument-list. */
15716 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15718 pop_deferring_access_checks ();
15719 return error_mark_node;
15721 /* Parse the arguments. */
15722 arguments = cp_parser_enclosed_template_argument_list (parser);
15725 /* Set the location to be of the form:
15726 template-name < template-argument-list [opt] >
15727 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15728 with caret == start at the start of the template-name,
15729 ranging until the closing '>'. */
15730 location_t finish_loc
15731 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15732 location_t combined_loc
15733 = make_location (token->location, token->location, finish_loc);
15735 /* Build a representation of the specialization. */
15736 if (identifier_p (templ))
15737 template_id = build_min_nt_loc (combined_loc,
15738 TEMPLATE_ID_EXPR,
15739 templ, arguments);
15740 else if (DECL_TYPE_TEMPLATE_P (templ)
15741 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15743 bool entering_scope;
15744 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15745 template (rather than some instantiation thereof) only if
15746 is not nested within some other construct. For example, in
15747 "template <typename T> void f(T) { A<T>::", A<T> is just an
15748 instantiation of A. */
15749 entering_scope = (template_parm_scope_p ()
15750 && cp_lexer_next_token_is (parser->lexer,
15751 CPP_SCOPE));
15752 template_id
15753 = finish_template_type (templ, arguments, entering_scope);
15755 /* A template-like identifier may be a partial concept id. */
15756 else if (flag_concepts
15757 && (template_id = (cp_parser_maybe_partial_concept_id
15758 (parser, templ, arguments))))
15759 return template_id;
15760 else if (variable_template_p (templ))
15762 template_id = lookup_template_variable (templ, arguments);
15763 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15764 SET_EXPR_LOCATION (template_id, combined_loc);
15766 else
15768 /* If it's not a class-template or a template-template, it should be
15769 a function-template. */
15770 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15771 || TREE_CODE (templ) == OVERLOAD
15772 || BASELINK_P (templ)));
15774 template_id = lookup_template_function (templ, arguments);
15775 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15776 SET_EXPR_LOCATION (template_id, combined_loc);
15779 /* If parsing tentatively, replace the sequence of tokens that makes
15780 up the template-id with a CPP_TEMPLATE_ID token. That way,
15781 should we re-parse the token stream, we will not have to repeat
15782 the effort required to do the parse, nor will we issue duplicate
15783 error messages about problems during instantiation of the
15784 template. */
15785 if (start_of_id
15786 /* Don't do this if we had a parse error in a declarator; re-parsing
15787 might succeed if a name changes meaning (60361). */
15788 && !(cp_parser_error_occurred (parser)
15789 && cp_parser_parsing_tentatively (parser)
15790 && parser->in_declarator_p))
15792 /* Reset the contents of the START_OF_ID token. */
15793 token->type = CPP_TEMPLATE_ID;
15794 token->location = combined_loc;
15796 /* We must mark the lookup as kept, so we don't throw it away on
15797 the first parse. */
15798 if (is_overloaded_fn (template_id))
15799 lookup_keep (get_fns (template_id), true);
15801 /* Retrieve any deferred checks. Do not pop this access checks yet
15802 so the memory will not be reclaimed during token replacing below. */
15803 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15804 token->u.tree_check_value->value = template_id;
15805 token->u.tree_check_value->checks = get_deferred_access_checks ();
15806 token->keyword = RID_MAX;
15808 /* Purge all subsequent tokens. */
15809 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15811 /* ??? Can we actually assume that, if template_id ==
15812 error_mark_node, we will have issued a diagnostic to the
15813 user, as opposed to simply marking the tentative parse as
15814 failed? */
15815 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15816 error_at (token->location, "parse error in template argument list");
15819 pop_to_parent_deferring_access_checks ();
15820 return template_id;
15823 /* Parse a template-name.
15825 template-name:
15826 identifier
15828 The standard should actually say:
15830 template-name:
15831 identifier
15832 operator-function-id
15834 A defect report has been filed about this issue.
15836 A conversion-function-id cannot be a template name because they cannot
15837 be part of a template-id. In fact, looking at this code:
15839 a.operator K<int>()
15841 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15842 It is impossible to call a templated conversion-function-id with an
15843 explicit argument list, since the only allowed template parameter is
15844 the type to which it is converting.
15846 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15847 `template' keyword, in a construction like:
15849 T::template f<3>()
15851 In that case `f' is taken to be a template-name, even though there
15852 is no way of knowing for sure.
15854 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15855 name refers to a set of overloaded functions, at least one of which
15856 is a template, or an IDENTIFIER_NODE with the name of the template,
15857 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15858 names are looked up inside uninstantiated templates. */
15860 static tree
15861 cp_parser_template_name (cp_parser* parser,
15862 bool template_keyword_p,
15863 bool check_dependency_p,
15864 bool is_declaration,
15865 enum tag_types tag_type,
15866 bool *is_identifier)
15868 tree identifier;
15869 tree decl;
15870 cp_token *token = cp_lexer_peek_token (parser->lexer);
15872 /* If the next token is `operator', then we have either an
15873 operator-function-id or a conversion-function-id. */
15874 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15876 /* We don't know whether we're looking at an
15877 operator-function-id or a conversion-function-id. */
15878 cp_parser_parse_tentatively (parser);
15879 /* Try an operator-function-id. */
15880 identifier = cp_parser_operator_function_id (parser);
15881 /* If that didn't work, try a conversion-function-id. */
15882 if (!cp_parser_parse_definitely (parser))
15884 cp_parser_error (parser, "expected template-name");
15885 return error_mark_node;
15888 /* Look for the identifier. */
15889 else
15890 identifier = cp_parser_identifier (parser);
15892 /* If we didn't find an identifier, we don't have a template-id. */
15893 if (identifier == error_mark_node)
15894 return error_mark_node;
15896 /* If the name immediately followed the `template' keyword, then it
15897 is a template-name. However, if the next token is not `<', then
15898 we do not treat it as a template-name, since it is not being used
15899 as part of a template-id. This enables us to handle constructs
15900 like:
15902 template <typename T> struct S { S(); };
15903 template <typename T> S<T>::S();
15905 correctly. We would treat `S' as a template -- if it were `S<T>'
15906 -- but we do not if there is no `<'. */
15908 if (processing_template_decl
15909 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15911 /* In a declaration, in a dependent context, we pretend that the
15912 "template" keyword was present in order to improve error
15913 recovery. For example, given:
15915 template <typename T> void f(T::X<int>);
15917 we want to treat "X<int>" as a template-id. */
15918 if (is_declaration
15919 && !template_keyword_p
15920 && parser->scope && TYPE_P (parser->scope)
15921 && check_dependency_p
15922 && dependent_scope_p (parser->scope)
15923 /* Do not do this for dtors (or ctors), since they never
15924 need the template keyword before their name. */
15925 && !constructor_name_p (identifier, parser->scope))
15927 cp_token_position start = 0;
15929 /* Explain what went wrong. */
15930 error_at (token->location, "non-template %qD used as template",
15931 identifier);
15932 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15933 parser->scope, identifier);
15934 /* If parsing tentatively, find the location of the "<" token. */
15935 if (cp_parser_simulate_error (parser))
15936 start = cp_lexer_token_position (parser->lexer, true);
15937 /* Parse the template arguments so that we can issue error
15938 messages about them. */
15939 cp_lexer_consume_token (parser->lexer);
15940 cp_parser_enclosed_template_argument_list (parser);
15941 /* Skip tokens until we find a good place from which to
15942 continue parsing. */
15943 cp_parser_skip_to_closing_parenthesis (parser,
15944 /*recovering=*/true,
15945 /*or_comma=*/true,
15946 /*consume_paren=*/false);
15947 /* If parsing tentatively, permanently remove the
15948 template argument list. That will prevent duplicate
15949 error messages from being issued about the missing
15950 "template" keyword. */
15951 if (start)
15952 cp_lexer_purge_tokens_after (parser->lexer, start);
15953 if (is_identifier)
15954 *is_identifier = true;
15955 parser->context->object_type = NULL_TREE;
15956 return identifier;
15959 /* If the "template" keyword is present, then there is generally
15960 no point in doing name-lookup, so we just return IDENTIFIER.
15961 But, if the qualifying scope is non-dependent then we can
15962 (and must) do name-lookup normally. */
15963 if (template_keyword_p)
15965 tree scope = (parser->scope ? parser->scope
15966 : parser->context->object_type);
15967 if (scope && TYPE_P (scope)
15968 && (!CLASS_TYPE_P (scope)
15969 || (check_dependency_p && dependent_type_p (scope))))
15971 /* We're optimizing away the call to cp_parser_lookup_name, but
15972 we still need to do this. */
15973 parser->context->object_type = NULL_TREE;
15974 return identifier;
15979 /* Look up the name. */
15980 decl = cp_parser_lookup_name (parser, identifier,
15981 tag_type,
15982 /*is_template=*/true,
15983 /*is_namespace=*/false,
15984 check_dependency_p,
15985 /*ambiguous_decls=*/NULL,
15986 token->location);
15988 decl = strip_using_decl (decl);
15990 /* If DECL is a template, then the name was a template-name. */
15991 if (TREE_CODE (decl) == TEMPLATE_DECL)
15993 if (TREE_DEPRECATED (decl)
15994 && deprecated_state != DEPRECATED_SUPPRESS)
15995 warn_deprecated_use (decl, NULL_TREE);
15997 else
15999 /* The standard does not explicitly indicate whether a name that
16000 names a set of overloaded declarations, some of which are
16001 templates, is a template-name. However, such a name should
16002 be a template-name; otherwise, there is no way to form a
16003 template-id for the overloaded templates. */
16004 bool found = false;
16006 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16007 !found && iter; ++iter)
16008 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16009 found = true;
16011 if (!found)
16013 /* The name does not name a template. */
16014 cp_parser_error (parser, "expected template-name");
16015 return error_mark_node;
16019 /* If DECL is dependent, and refers to a function, then just return
16020 its name; we will look it up again during template instantiation. */
16021 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16023 tree scope = ovl_scope (decl);
16024 if (TYPE_P (scope) && dependent_type_p (scope))
16025 return identifier;
16028 return decl;
16031 /* Parse a template-argument-list.
16033 template-argument-list:
16034 template-argument ... [opt]
16035 template-argument-list , template-argument ... [opt]
16037 Returns a TREE_VEC containing the arguments. */
16039 static tree
16040 cp_parser_template_argument_list (cp_parser* parser)
16042 tree fixed_args[10];
16043 unsigned n_args = 0;
16044 unsigned alloced = 10;
16045 tree *arg_ary = fixed_args;
16046 tree vec;
16047 bool saved_in_template_argument_list_p;
16048 bool saved_ice_p;
16049 bool saved_non_ice_p;
16051 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16052 parser->in_template_argument_list_p = true;
16053 /* Even if the template-id appears in an integral
16054 constant-expression, the contents of the argument list do
16055 not. */
16056 saved_ice_p = parser->integral_constant_expression_p;
16057 parser->integral_constant_expression_p = false;
16058 saved_non_ice_p = parser->non_integral_constant_expression_p;
16059 parser->non_integral_constant_expression_p = false;
16061 /* Parse the arguments. */
16064 tree argument;
16066 if (n_args)
16067 /* Consume the comma. */
16068 cp_lexer_consume_token (parser->lexer);
16070 /* Parse the template-argument. */
16071 argument = cp_parser_template_argument (parser);
16073 /* If the next token is an ellipsis, we're expanding a template
16074 argument pack. */
16075 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16077 if (argument == error_mark_node)
16079 cp_token *token = cp_lexer_peek_token (parser->lexer);
16080 error_at (token->location,
16081 "expected parameter pack before %<...%>");
16083 /* Consume the `...' token. */
16084 cp_lexer_consume_token (parser->lexer);
16086 /* Make the argument into a TYPE_PACK_EXPANSION or
16087 EXPR_PACK_EXPANSION. */
16088 argument = make_pack_expansion (argument);
16091 if (n_args == alloced)
16093 alloced *= 2;
16095 if (arg_ary == fixed_args)
16097 arg_ary = XNEWVEC (tree, alloced);
16098 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16100 else
16101 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16103 arg_ary[n_args++] = argument;
16105 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16107 vec = make_tree_vec (n_args);
16109 while (n_args--)
16110 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16112 if (arg_ary != fixed_args)
16113 free (arg_ary);
16114 parser->non_integral_constant_expression_p = saved_non_ice_p;
16115 parser->integral_constant_expression_p = saved_ice_p;
16116 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16117 if (CHECKING_P)
16118 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16119 return vec;
16122 /* Parse a template-argument.
16124 template-argument:
16125 assignment-expression
16126 type-id
16127 id-expression
16129 The representation is that of an assignment-expression, type-id, or
16130 id-expression -- except that the qualified id-expression is
16131 evaluated, so that the value returned is either a DECL or an
16132 OVERLOAD.
16134 Although the standard says "assignment-expression", it forbids
16135 throw-expressions or assignments in the template argument.
16136 Therefore, we use "conditional-expression" instead. */
16138 static tree
16139 cp_parser_template_argument (cp_parser* parser)
16141 tree argument;
16142 bool template_p;
16143 bool address_p;
16144 bool maybe_type_id = false;
16145 cp_token *token = NULL, *argument_start_token = NULL;
16146 location_t loc = 0;
16147 cp_id_kind idk;
16149 /* There's really no way to know what we're looking at, so we just
16150 try each alternative in order.
16152 [temp.arg]
16154 In a template-argument, an ambiguity between a type-id and an
16155 expression is resolved to a type-id, regardless of the form of
16156 the corresponding template-parameter.
16158 Therefore, we try a type-id first. */
16159 cp_parser_parse_tentatively (parser);
16160 argument = cp_parser_template_type_arg (parser);
16161 /* If there was no error parsing the type-id but the next token is a
16162 '>>', our behavior depends on which dialect of C++ we're
16163 parsing. In C++98, we probably found a typo for '> >'. But there
16164 are type-id which are also valid expressions. For instance:
16166 struct X { int operator >> (int); };
16167 template <int V> struct Foo {};
16168 Foo<X () >> 5> r;
16170 Here 'X()' is a valid type-id of a function type, but the user just
16171 wanted to write the expression "X() >> 5". Thus, we remember that we
16172 found a valid type-id, but we still try to parse the argument as an
16173 expression to see what happens.
16175 In C++0x, the '>>' will be considered two separate '>'
16176 tokens. */
16177 if (!cp_parser_error_occurred (parser)
16178 && cxx_dialect == cxx98
16179 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16181 maybe_type_id = true;
16182 cp_parser_abort_tentative_parse (parser);
16184 else
16186 /* If the next token isn't a `,' or a `>', then this argument wasn't
16187 really finished. This means that the argument is not a valid
16188 type-id. */
16189 if (!cp_parser_next_token_ends_template_argument_p (parser))
16190 cp_parser_error (parser, "expected template-argument");
16191 /* If that worked, we're done. */
16192 if (cp_parser_parse_definitely (parser))
16193 return argument;
16195 /* We're still not sure what the argument will be. */
16196 cp_parser_parse_tentatively (parser);
16197 /* Try a template. */
16198 argument_start_token = cp_lexer_peek_token (parser->lexer);
16199 argument = cp_parser_id_expression (parser,
16200 /*template_keyword_p=*/false,
16201 /*check_dependency_p=*/true,
16202 &template_p,
16203 /*declarator_p=*/false,
16204 /*optional_p=*/false);
16205 /* If the next token isn't a `,' or a `>', then this argument wasn't
16206 really finished. */
16207 if (!cp_parser_next_token_ends_template_argument_p (parser))
16208 cp_parser_error (parser, "expected template-argument");
16209 if (!cp_parser_error_occurred (parser))
16211 /* Figure out what is being referred to. If the id-expression
16212 was for a class template specialization, then we will have a
16213 TYPE_DECL at this point. There is no need to do name lookup
16214 at this point in that case. */
16215 if (TREE_CODE (argument) != TYPE_DECL)
16216 argument = cp_parser_lookup_name (parser, argument,
16217 none_type,
16218 /*is_template=*/template_p,
16219 /*is_namespace=*/false,
16220 /*check_dependency=*/true,
16221 /*ambiguous_decls=*/NULL,
16222 argument_start_token->location);
16223 /* Handle a constrained-type-specifier for a non-type template
16224 parameter. */
16225 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16226 argument = decl;
16227 else if (TREE_CODE (argument) != TEMPLATE_DECL
16228 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16229 cp_parser_error (parser, "expected template-name");
16231 if (cp_parser_parse_definitely (parser))
16233 if (TREE_DEPRECATED (argument))
16234 warn_deprecated_use (argument, NULL_TREE);
16235 return argument;
16237 /* It must be a non-type argument. In C++17 any constant-expression is
16238 allowed. */
16239 if (cxx_dialect > cxx14)
16240 goto general_expr;
16242 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16244 -- an integral constant-expression of integral or enumeration
16245 type; or
16247 -- the name of a non-type template-parameter; or
16249 -- the name of an object or function with external linkage...
16251 -- the address of an object or function with external linkage...
16253 -- a pointer to member... */
16254 /* Look for a non-type template parameter. */
16255 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16257 cp_parser_parse_tentatively (parser);
16258 argument = cp_parser_primary_expression (parser,
16259 /*address_p=*/false,
16260 /*cast_p=*/false,
16261 /*template_arg_p=*/true,
16262 &idk);
16263 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16264 || !cp_parser_next_token_ends_template_argument_p (parser))
16265 cp_parser_simulate_error (parser);
16266 if (cp_parser_parse_definitely (parser))
16267 return argument;
16270 /* If the next token is "&", the argument must be the address of an
16271 object or function with external linkage. */
16272 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16273 if (address_p)
16275 loc = cp_lexer_peek_token (parser->lexer)->location;
16276 cp_lexer_consume_token (parser->lexer);
16278 /* See if we might have an id-expression. */
16279 token = cp_lexer_peek_token (parser->lexer);
16280 if (token->type == CPP_NAME
16281 || token->keyword == RID_OPERATOR
16282 || token->type == CPP_SCOPE
16283 || token->type == CPP_TEMPLATE_ID
16284 || token->type == CPP_NESTED_NAME_SPECIFIER)
16286 cp_parser_parse_tentatively (parser);
16287 argument = cp_parser_primary_expression (parser,
16288 address_p,
16289 /*cast_p=*/false,
16290 /*template_arg_p=*/true,
16291 &idk);
16292 if (cp_parser_error_occurred (parser)
16293 || !cp_parser_next_token_ends_template_argument_p (parser))
16294 cp_parser_abort_tentative_parse (parser);
16295 else
16297 tree probe;
16299 if (INDIRECT_REF_P (argument))
16301 /* Strip the dereference temporarily. */
16302 gcc_assert (REFERENCE_REF_P (argument));
16303 argument = TREE_OPERAND (argument, 0);
16306 /* If we're in a template, we represent a qualified-id referring
16307 to a static data member as a SCOPE_REF even if the scope isn't
16308 dependent so that we can check access control later. */
16309 probe = argument;
16310 if (TREE_CODE (probe) == SCOPE_REF)
16311 probe = TREE_OPERAND (probe, 1);
16312 if (VAR_P (probe))
16314 /* A variable without external linkage might still be a
16315 valid constant-expression, so no error is issued here
16316 if the external-linkage check fails. */
16317 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16318 cp_parser_simulate_error (parser);
16320 else if (is_overloaded_fn (argument))
16321 /* All overloaded functions are allowed; if the external
16322 linkage test does not pass, an error will be issued
16323 later. */
16325 else if (address_p
16326 && (TREE_CODE (argument) == OFFSET_REF
16327 || TREE_CODE (argument) == SCOPE_REF))
16328 /* A pointer-to-member. */
16330 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16332 else
16333 cp_parser_simulate_error (parser);
16335 if (cp_parser_parse_definitely (parser))
16337 if (address_p)
16338 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16339 tf_warning_or_error);
16340 else
16341 argument = convert_from_reference (argument);
16342 return argument;
16346 /* If the argument started with "&", there are no other valid
16347 alternatives at this point. */
16348 if (address_p)
16350 cp_parser_error (parser, "invalid non-type template argument");
16351 return error_mark_node;
16354 general_expr:
16355 /* If the argument wasn't successfully parsed as a type-id followed
16356 by '>>', the argument can only be a constant expression now.
16357 Otherwise, we try parsing the constant-expression tentatively,
16358 because the argument could really be a type-id. */
16359 if (maybe_type_id)
16360 cp_parser_parse_tentatively (parser);
16362 if (cxx_dialect <= cxx14)
16363 argument = cp_parser_constant_expression (parser);
16364 else
16366 /* With C++17 generalized non-type template arguments we need to handle
16367 lvalue constant expressions, too. */
16368 argument = cp_parser_assignment_expression (parser);
16369 require_potential_constant_expression (argument);
16372 if (!maybe_type_id)
16373 return argument;
16374 if (!cp_parser_next_token_ends_template_argument_p (parser))
16375 cp_parser_error (parser, "expected template-argument");
16376 if (cp_parser_parse_definitely (parser))
16377 return argument;
16378 /* We did our best to parse the argument as a non type-id, but that
16379 was the only alternative that matched (albeit with a '>' after
16380 it). We can assume it's just a typo from the user, and a
16381 diagnostic will then be issued. */
16382 return cp_parser_template_type_arg (parser);
16385 /* Parse an explicit-instantiation.
16387 explicit-instantiation:
16388 template declaration
16390 Although the standard says `declaration', what it really means is:
16392 explicit-instantiation:
16393 template decl-specifier-seq [opt] declarator [opt] ;
16395 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16396 supposed to be allowed. A defect report has been filed about this
16397 issue.
16399 GNU Extension:
16401 explicit-instantiation:
16402 storage-class-specifier template
16403 decl-specifier-seq [opt] declarator [opt] ;
16404 function-specifier template
16405 decl-specifier-seq [opt] declarator [opt] ; */
16407 static void
16408 cp_parser_explicit_instantiation (cp_parser* parser)
16410 int declares_class_or_enum;
16411 cp_decl_specifier_seq decl_specifiers;
16412 tree extension_specifier = NULL_TREE;
16414 timevar_push (TV_TEMPLATE_INST);
16416 /* Look for an (optional) storage-class-specifier or
16417 function-specifier. */
16418 if (cp_parser_allow_gnu_extensions_p (parser))
16420 extension_specifier
16421 = cp_parser_storage_class_specifier_opt (parser);
16422 if (!extension_specifier)
16423 extension_specifier
16424 = cp_parser_function_specifier_opt (parser,
16425 /*decl_specs=*/NULL);
16428 /* Look for the `template' keyword. */
16429 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16430 /* Let the front end know that we are processing an explicit
16431 instantiation. */
16432 begin_explicit_instantiation ();
16433 /* [temp.explicit] says that we are supposed to ignore access
16434 control while processing explicit instantiation directives. */
16435 push_deferring_access_checks (dk_no_check);
16436 /* Parse a decl-specifier-seq. */
16437 cp_parser_decl_specifier_seq (parser,
16438 CP_PARSER_FLAGS_OPTIONAL,
16439 &decl_specifiers,
16440 &declares_class_or_enum);
16441 /* If there was exactly one decl-specifier, and it declared a class,
16442 and there's no declarator, then we have an explicit type
16443 instantiation. */
16444 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16446 tree type;
16448 type = check_tag_decl (&decl_specifiers,
16449 /*explicit_type_instantiation_p=*/true);
16450 /* Turn access control back on for names used during
16451 template instantiation. */
16452 pop_deferring_access_checks ();
16453 if (type)
16454 do_type_instantiation (type, extension_specifier,
16455 /*complain=*/tf_error);
16457 else
16459 cp_declarator *declarator;
16460 tree decl;
16462 /* Parse the declarator. */
16463 declarator
16464 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16465 /*ctor_dtor_or_conv_p=*/NULL,
16466 /*parenthesized_p=*/NULL,
16467 /*member_p=*/false,
16468 /*friend_p=*/false);
16469 if (declares_class_or_enum & 2)
16470 cp_parser_check_for_definition_in_return_type (declarator,
16471 decl_specifiers.type,
16472 decl_specifiers.locations[ds_type_spec]);
16473 if (declarator != cp_error_declarator)
16475 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16476 permerror (decl_specifiers.locations[ds_inline],
16477 "explicit instantiation shall not use"
16478 " %<inline%> specifier");
16479 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16480 permerror (decl_specifiers.locations[ds_constexpr],
16481 "explicit instantiation shall not use"
16482 " %<constexpr%> specifier");
16484 decl = grokdeclarator (declarator, &decl_specifiers,
16485 NORMAL, 0, &decl_specifiers.attributes);
16486 /* Turn access control back on for names used during
16487 template instantiation. */
16488 pop_deferring_access_checks ();
16489 /* Do the explicit instantiation. */
16490 do_decl_instantiation (decl, extension_specifier);
16492 else
16494 pop_deferring_access_checks ();
16495 /* Skip the body of the explicit instantiation. */
16496 cp_parser_skip_to_end_of_statement (parser);
16499 /* We're done with the instantiation. */
16500 end_explicit_instantiation ();
16502 cp_parser_consume_semicolon_at_end_of_statement (parser);
16504 timevar_pop (TV_TEMPLATE_INST);
16507 /* Parse an explicit-specialization.
16509 explicit-specialization:
16510 template < > declaration
16512 Although the standard says `declaration', what it really means is:
16514 explicit-specialization:
16515 template <> decl-specifier [opt] init-declarator [opt] ;
16516 template <> function-definition
16517 template <> explicit-specialization
16518 template <> template-declaration */
16520 static void
16521 cp_parser_explicit_specialization (cp_parser* parser)
16523 bool need_lang_pop;
16524 cp_token *token = cp_lexer_peek_token (parser->lexer);
16526 /* Look for the `template' keyword. */
16527 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16528 /* Look for the `<'. */
16529 cp_parser_require (parser, CPP_LESS, RT_LESS);
16530 /* Look for the `>'. */
16531 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16532 /* We have processed another parameter list. */
16533 ++parser->num_template_parameter_lists;
16534 /* [temp]
16536 A template ... explicit specialization ... shall not have C
16537 linkage. */
16538 if (current_lang_name == lang_name_c)
16540 error_at (token->location, "template specialization with C linkage");
16541 maybe_show_extern_c_location ();
16542 /* Give it C++ linkage to avoid confusing other parts of the
16543 front end. */
16544 push_lang_context (lang_name_cplusplus);
16545 need_lang_pop = true;
16547 else
16548 need_lang_pop = false;
16549 /* Let the front end know that we are beginning a specialization. */
16550 if (!begin_specialization ())
16552 end_specialization ();
16553 return;
16556 /* If the next keyword is `template', we need to figure out whether
16557 or not we're looking a template-declaration. */
16558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16560 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16561 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16562 cp_parser_template_declaration_after_export (parser,
16563 /*member_p=*/false);
16564 else
16565 cp_parser_explicit_specialization (parser);
16567 else
16568 /* Parse the dependent declaration. */
16569 cp_parser_single_declaration (parser,
16570 /*checks=*/NULL,
16571 /*member_p=*/false,
16572 /*explicit_specialization_p=*/true,
16573 /*friend_p=*/NULL);
16574 /* We're done with the specialization. */
16575 end_specialization ();
16576 /* For the erroneous case of a template with C linkage, we pushed an
16577 implicit C++ linkage scope; exit that scope now. */
16578 if (need_lang_pop)
16579 pop_lang_context ();
16580 /* We're done with this parameter list. */
16581 --parser->num_template_parameter_lists;
16584 /* Parse a type-specifier.
16586 type-specifier:
16587 simple-type-specifier
16588 class-specifier
16589 enum-specifier
16590 elaborated-type-specifier
16591 cv-qualifier
16593 GNU Extension:
16595 type-specifier:
16596 __complex__
16598 Returns a representation of the type-specifier. For a
16599 class-specifier, enum-specifier, or elaborated-type-specifier, a
16600 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16602 The parser flags FLAGS is used to control type-specifier parsing.
16604 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16605 in a decl-specifier-seq.
16607 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16608 class-specifier, enum-specifier, or elaborated-type-specifier, then
16609 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16610 if a type is declared; 2 if it is defined. Otherwise, it is set to
16611 zero.
16613 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16614 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16615 is set to FALSE. */
16617 static tree
16618 cp_parser_type_specifier (cp_parser* parser,
16619 cp_parser_flags flags,
16620 cp_decl_specifier_seq *decl_specs,
16621 bool is_declaration,
16622 int* declares_class_or_enum,
16623 bool* is_cv_qualifier)
16625 tree type_spec = NULL_TREE;
16626 cp_token *token;
16627 enum rid keyword;
16628 cp_decl_spec ds = ds_last;
16630 /* Assume this type-specifier does not declare a new type. */
16631 if (declares_class_or_enum)
16632 *declares_class_or_enum = 0;
16633 /* And that it does not specify a cv-qualifier. */
16634 if (is_cv_qualifier)
16635 *is_cv_qualifier = false;
16636 /* Peek at the next token. */
16637 token = cp_lexer_peek_token (parser->lexer);
16639 /* If we're looking at a keyword, we can use that to guide the
16640 production we choose. */
16641 keyword = token->keyword;
16642 switch (keyword)
16644 case RID_ENUM:
16645 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16646 goto elaborated_type_specifier;
16648 /* Look for the enum-specifier. */
16649 type_spec = cp_parser_enum_specifier (parser);
16650 /* If that worked, we're done. */
16651 if (type_spec)
16653 if (declares_class_or_enum)
16654 *declares_class_or_enum = 2;
16655 if (decl_specs)
16656 cp_parser_set_decl_spec_type (decl_specs,
16657 type_spec,
16658 token,
16659 /*type_definition_p=*/true);
16660 return type_spec;
16662 else
16663 goto elaborated_type_specifier;
16665 /* Any of these indicate either a class-specifier, or an
16666 elaborated-type-specifier. */
16667 case RID_CLASS:
16668 case RID_STRUCT:
16669 case RID_UNION:
16670 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16671 goto elaborated_type_specifier;
16673 /* Parse tentatively so that we can back up if we don't find a
16674 class-specifier. */
16675 cp_parser_parse_tentatively (parser);
16676 /* Look for the class-specifier. */
16677 type_spec = cp_parser_class_specifier (parser);
16678 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16679 /* If that worked, we're done. */
16680 if (cp_parser_parse_definitely (parser))
16682 if (declares_class_or_enum)
16683 *declares_class_or_enum = 2;
16684 if (decl_specs)
16685 cp_parser_set_decl_spec_type (decl_specs,
16686 type_spec,
16687 token,
16688 /*type_definition_p=*/true);
16689 return type_spec;
16692 /* Fall through. */
16693 elaborated_type_specifier:
16694 /* We're declaring (not defining) a class or enum. */
16695 if (declares_class_or_enum)
16696 *declares_class_or_enum = 1;
16698 /* Fall through. */
16699 case RID_TYPENAME:
16700 /* Look for an elaborated-type-specifier. */
16701 type_spec
16702 = (cp_parser_elaborated_type_specifier
16703 (parser,
16704 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16705 is_declaration));
16706 if (decl_specs)
16707 cp_parser_set_decl_spec_type (decl_specs,
16708 type_spec,
16709 token,
16710 /*type_definition_p=*/false);
16711 return type_spec;
16713 case RID_CONST:
16714 ds = ds_const;
16715 if (is_cv_qualifier)
16716 *is_cv_qualifier = true;
16717 break;
16719 case RID_VOLATILE:
16720 ds = ds_volatile;
16721 if (is_cv_qualifier)
16722 *is_cv_qualifier = true;
16723 break;
16725 case RID_RESTRICT:
16726 ds = ds_restrict;
16727 if (is_cv_qualifier)
16728 *is_cv_qualifier = true;
16729 break;
16731 case RID_COMPLEX:
16732 /* The `__complex__' keyword is a GNU extension. */
16733 ds = ds_complex;
16734 break;
16736 default:
16737 break;
16740 /* Handle simple keywords. */
16741 if (ds != ds_last)
16743 if (decl_specs)
16745 set_and_check_decl_spec_loc (decl_specs, ds, token);
16746 decl_specs->any_specifiers_p = true;
16748 return cp_lexer_consume_token (parser->lexer)->u.value;
16751 /* If we do not already have a type-specifier, assume we are looking
16752 at a simple-type-specifier. */
16753 type_spec = cp_parser_simple_type_specifier (parser,
16754 decl_specs,
16755 flags);
16757 /* If we didn't find a type-specifier, and a type-specifier was not
16758 optional in this context, issue an error message. */
16759 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16761 cp_parser_error (parser, "expected type specifier");
16762 return error_mark_node;
16765 return type_spec;
16768 /* Parse a simple-type-specifier.
16770 simple-type-specifier:
16771 :: [opt] nested-name-specifier [opt] type-name
16772 :: [opt] nested-name-specifier template template-id
16773 char
16774 wchar_t
16775 bool
16776 short
16778 long
16779 signed
16780 unsigned
16781 float
16782 double
16783 void
16785 C++11 Extension:
16787 simple-type-specifier:
16788 auto
16789 decltype ( expression )
16790 char16_t
16791 char32_t
16792 __underlying_type ( type-id )
16794 C++17 extension:
16796 nested-name-specifier(opt) template-name
16798 GNU Extension:
16800 simple-type-specifier:
16801 __int128
16802 __typeof__ unary-expression
16803 __typeof__ ( type-id )
16804 __typeof__ ( type-id ) { initializer-list , [opt] }
16806 Concepts Extension:
16808 simple-type-specifier:
16809 constrained-type-specifier
16811 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16812 appropriately updated. */
16814 static tree
16815 cp_parser_simple_type_specifier (cp_parser* parser,
16816 cp_decl_specifier_seq *decl_specs,
16817 cp_parser_flags flags)
16819 tree type = NULL_TREE;
16820 cp_token *token;
16821 int idx;
16823 /* Peek at the next token. */
16824 token = cp_lexer_peek_token (parser->lexer);
16826 /* If we're looking at a keyword, things are easy. */
16827 switch (token->keyword)
16829 case RID_CHAR:
16830 if (decl_specs)
16831 decl_specs->explicit_char_p = true;
16832 type = char_type_node;
16833 break;
16834 case RID_CHAR16:
16835 type = char16_type_node;
16836 break;
16837 case RID_CHAR32:
16838 type = char32_type_node;
16839 break;
16840 case RID_WCHAR:
16841 type = wchar_type_node;
16842 break;
16843 case RID_BOOL:
16844 type = boolean_type_node;
16845 break;
16846 case RID_SHORT:
16847 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16848 type = short_integer_type_node;
16849 break;
16850 case RID_INT:
16851 if (decl_specs)
16852 decl_specs->explicit_int_p = true;
16853 type = integer_type_node;
16854 break;
16855 case RID_INT_N_0:
16856 case RID_INT_N_1:
16857 case RID_INT_N_2:
16858 case RID_INT_N_3:
16859 idx = token->keyword - RID_INT_N_0;
16860 if (! int_n_enabled_p [idx])
16861 break;
16862 if (decl_specs)
16864 decl_specs->explicit_intN_p = true;
16865 decl_specs->int_n_idx = idx;
16867 type = int_n_trees [idx].signed_type;
16868 break;
16869 case RID_LONG:
16870 if (decl_specs)
16871 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16872 type = long_integer_type_node;
16873 break;
16874 case RID_SIGNED:
16875 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16876 type = integer_type_node;
16877 break;
16878 case RID_UNSIGNED:
16879 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16880 type = unsigned_type_node;
16881 break;
16882 case RID_FLOAT:
16883 type = float_type_node;
16884 break;
16885 case RID_DOUBLE:
16886 type = double_type_node;
16887 break;
16888 case RID_VOID:
16889 type = void_type_node;
16890 break;
16892 case RID_AUTO:
16893 maybe_warn_cpp0x (CPP0X_AUTO);
16894 if (parser->auto_is_implicit_function_template_parm_p)
16896 /* The 'auto' might be the placeholder return type for a function decl
16897 with trailing return type. */
16898 bool have_trailing_return_fn_decl = false;
16900 cp_parser_parse_tentatively (parser);
16901 cp_lexer_consume_token (parser->lexer);
16902 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16903 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16904 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16905 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16909 cp_lexer_consume_token (parser->lexer);
16910 cp_parser_skip_to_closing_parenthesis (parser,
16911 /*recovering*/false,
16912 /*or_comma*/false,
16913 /*consume_paren*/true);
16914 continue;
16917 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16919 have_trailing_return_fn_decl = true;
16920 break;
16923 cp_lexer_consume_token (parser->lexer);
16925 cp_parser_abort_tentative_parse (parser);
16927 if (have_trailing_return_fn_decl)
16929 type = make_auto ();
16930 break;
16933 if (cxx_dialect >= cxx14)
16935 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16936 type = TREE_TYPE (type);
16938 else
16939 type = error_mark_node;
16941 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16943 if (cxx_dialect < cxx14)
16944 error_at (token->location,
16945 "use of %<auto%> in lambda parameter declaration "
16946 "only available with "
16947 "-std=c++14 or -std=gnu++14");
16949 else if (cxx_dialect < cxx14)
16950 error_at (token->location,
16951 "use of %<auto%> in parameter declaration "
16952 "only available with "
16953 "-std=c++14 or -std=gnu++14");
16954 else if (!flag_concepts)
16955 pedwarn (token->location, OPT_Wpedantic,
16956 "ISO C++ forbids use of %<auto%> in parameter "
16957 "declaration");
16959 else
16960 type = make_auto ();
16961 break;
16963 case RID_DECLTYPE:
16964 /* Since DR 743, decltype can either be a simple-type-specifier by
16965 itself or begin a nested-name-specifier. Parsing it will replace
16966 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16967 handling below decide what to do. */
16968 cp_parser_decltype (parser);
16969 cp_lexer_set_token_position (parser->lexer, token);
16970 break;
16972 case RID_TYPEOF:
16973 /* Consume the `typeof' token. */
16974 cp_lexer_consume_token (parser->lexer);
16975 /* Parse the operand to `typeof'. */
16976 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16977 /* If it is not already a TYPE, take its type. */
16978 if (!TYPE_P (type))
16979 type = finish_typeof (type);
16981 if (decl_specs)
16982 cp_parser_set_decl_spec_type (decl_specs, type,
16983 token,
16984 /*type_definition_p=*/false);
16986 return type;
16988 case RID_UNDERLYING_TYPE:
16989 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16990 if (decl_specs)
16991 cp_parser_set_decl_spec_type (decl_specs, type,
16992 token,
16993 /*type_definition_p=*/false);
16995 return type;
16997 case RID_BASES:
16998 case RID_DIRECT_BASES:
16999 type = cp_parser_trait_expr (parser, token->keyword);
17000 if (decl_specs)
17001 cp_parser_set_decl_spec_type (decl_specs, type,
17002 token,
17003 /*type_definition_p=*/false);
17004 return type;
17005 default:
17006 break;
17009 /* If token is an already-parsed decltype not followed by ::,
17010 it's a simple-type-specifier. */
17011 if (token->type == CPP_DECLTYPE
17012 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17014 type = saved_checks_value (token->u.tree_check_value);
17015 if (decl_specs)
17017 cp_parser_set_decl_spec_type (decl_specs, type,
17018 token,
17019 /*type_definition_p=*/false);
17020 /* Remember that we are handling a decltype in order to
17021 implement the resolution of DR 1510 when the argument
17022 isn't instantiation dependent. */
17023 decl_specs->decltype_p = true;
17025 cp_lexer_consume_token (parser->lexer);
17026 return type;
17029 /* If the type-specifier was for a built-in type, we're done. */
17030 if (type)
17032 /* Record the type. */
17033 if (decl_specs
17034 && (token->keyword != RID_SIGNED
17035 && token->keyword != RID_UNSIGNED
17036 && token->keyword != RID_SHORT
17037 && token->keyword != RID_LONG))
17038 cp_parser_set_decl_spec_type (decl_specs,
17039 type,
17040 token,
17041 /*type_definition_p=*/false);
17042 if (decl_specs)
17043 decl_specs->any_specifiers_p = true;
17045 /* Consume the token. */
17046 cp_lexer_consume_token (parser->lexer);
17048 if (type == error_mark_node)
17049 return error_mark_node;
17051 /* There is no valid C++ program where a non-template type is
17052 followed by a "<". That usually indicates that the user thought
17053 that the type was a template. */
17054 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17055 token->location);
17057 return TYPE_NAME (type);
17060 /* The type-specifier must be a user-defined type. */
17061 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17063 bool qualified_p;
17064 bool global_p;
17066 /* Don't gobble tokens or issue error messages if this is an
17067 optional type-specifier. */
17068 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17069 cp_parser_parse_tentatively (parser);
17071 token = cp_lexer_peek_token (parser->lexer);
17073 /* Look for the optional `::' operator. */
17074 global_p
17075 = (cp_parser_global_scope_opt (parser,
17076 /*current_scope_valid_p=*/false)
17077 != NULL_TREE);
17078 /* Look for the nested-name specifier. */
17079 qualified_p
17080 = (cp_parser_nested_name_specifier_opt (parser,
17081 /*typename_keyword_p=*/false,
17082 /*check_dependency_p=*/true,
17083 /*type_p=*/false,
17084 /*is_declaration=*/false)
17085 != NULL_TREE);
17086 /* If we have seen a nested-name-specifier, and the next token
17087 is `template', then we are using the template-id production. */
17088 if (parser->scope
17089 && cp_parser_optional_template_keyword (parser))
17091 /* Look for the template-id. */
17092 type = cp_parser_template_id (parser,
17093 /*template_keyword_p=*/true,
17094 /*check_dependency_p=*/true,
17095 none_type,
17096 /*is_declaration=*/false);
17097 /* If the template-id did not name a type, we are out of
17098 luck. */
17099 if (TREE_CODE (type) != TYPE_DECL)
17101 cp_parser_error (parser, "expected template-id for type");
17102 type = NULL_TREE;
17105 /* Otherwise, look for a type-name. */
17106 else
17107 type = cp_parser_type_name (parser);
17108 /* Keep track of all name-lookups performed in class scopes. */
17109 if (type
17110 && !global_p
17111 && !qualified_p
17112 && TREE_CODE (type) == TYPE_DECL
17113 && identifier_p (DECL_NAME (type)))
17114 maybe_note_name_used_in_class (DECL_NAME (type), type);
17115 /* If it didn't work out, we don't have a TYPE. */
17116 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17117 && !cp_parser_parse_definitely (parser))
17118 type = NULL_TREE;
17119 if (!type && cxx_dialect >= cxx17)
17121 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17122 cp_parser_parse_tentatively (parser);
17124 cp_parser_global_scope_opt (parser,
17125 /*current_scope_valid_p=*/false);
17126 cp_parser_nested_name_specifier_opt (parser,
17127 /*typename_keyword_p=*/false,
17128 /*check_dependency_p=*/true,
17129 /*type_p=*/false,
17130 /*is_declaration=*/false);
17131 tree name = cp_parser_identifier (parser);
17132 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17133 && parser->scope != error_mark_node)
17135 tree tmpl = cp_parser_lookup_name (parser, name,
17136 none_type,
17137 /*is_template=*/false,
17138 /*is_namespace=*/false,
17139 /*check_dependency=*/true,
17140 /*ambiguous_decls=*/NULL,
17141 token->location);
17142 if (tmpl && tmpl != error_mark_node
17143 && (DECL_CLASS_TEMPLATE_P (tmpl)
17144 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17145 type = make_template_placeholder (tmpl);
17146 else
17148 type = error_mark_node;
17149 if (!cp_parser_simulate_error (parser))
17150 cp_parser_name_lookup_error (parser, name, tmpl,
17151 NLE_TYPE, token->location);
17154 else
17155 type = error_mark_node;
17157 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17158 && !cp_parser_parse_definitely (parser))
17159 type = NULL_TREE;
17161 if (type && decl_specs)
17162 cp_parser_set_decl_spec_type (decl_specs, type,
17163 token,
17164 /*type_definition_p=*/false);
17167 /* If we didn't get a type-name, issue an error message. */
17168 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17170 cp_parser_error (parser, "expected type-name");
17171 return error_mark_node;
17174 if (type && type != error_mark_node)
17176 /* See if TYPE is an Objective-C type, and if so, parse and
17177 accept any protocol references following it. Do this before
17178 the cp_parser_check_for_invalid_template_id() call, because
17179 Objective-C types can be followed by '<...>' which would
17180 enclose protocol names rather than template arguments, and so
17181 everything is fine. */
17182 if (c_dialect_objc () && !parser->scope
17183 && (objc_is_id (type) || objc_is_class_name (type)))
17185 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17186 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17188 /* Clobber the "unqualified" type previously entered into
17189 DECL_SPECS with the new, improved protocol-qualified version. */
17190 if (decl_specs)
17191 decl_specs->type = qual_type;
17193 return qual_type;
17196 /* There is no valid C++ program where a non-template type is
17197 followed by a "<". That usually indicates that the user
17198 thought that the type was a template. */
17199 cp_parser_check_for_invalid_template_id (parser, type,
17200 none_type,
17201 token->location);
17204 return type;
17207 /* Parse a type-name.
17209 type-name:
17210 class-name
17211 enum-name
17212 typedef-name
17213 simple-template-id [in c++0x]
17215 enum-name:
17216 identifier
17218 typedef-name:
17219 identifier
17221 Concepts:
17223 type-name:
17224 concept-name
17225 partial-concept-id
17227 concept-name:
17228 identifier
17230 Returns a TYPE_DECL for the type. */
17232 static tree
17233 cp_parser_type_name (cp_parser* parser)
17235 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17238 /* See above. */
17239 static tree
17240 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17242 tree type_decl;
17244 /* We can't know yet whether it is a class-name or not. */
17245 cp_parser_parse_tentatively (parser);
17246 /* Try a class-name. */
17247 type_decl = cp_parser_class_name (parser,
17248 typename_keyword_p,
17249 /*template_keyword_p=*/false,
17250 none_type,
17251 /*check_dependency_p=*/true,
17252 /*class_head_p=*/false,
17253 /*is_declaration=*/false);
17254 /* If it's not a class-name, keep looking. */
17255 if (!cp_parser_parse_definitely (parser))
17257 if (cxx_dialect < cxx11)
17258 /* It must be a typedef-name or an enum-name. */
17259 return cp_parser_nonclass_name (parser);
17261 cp_parser_parse_tentatively (parser);
17262 /* It is either a simple-template-id representing an
17263 instantiation of an alias template... */
17264 type_decl = cp_parser_template_id (parser,
17265 /*template_keyword_p=*/false,
17266 /*check_dependency_p=*/true,
17267 none_type,
17268 /*is_declaration=*/false);
17269 /* Note that this must be an instantiation of an alias template
17270 because [temp.names]/6 says:
17272 A template-id that names an alias template specialization
17273 is a type-name.
17275 Whereas [temp.names]/7 says:
17277 A simple-template-id that names a class template
17278 specialization is a class-name.
17280 With concepts, this could also be a partial-concept-id that
17281 declares a non-type template parameter. */
17282 if (type_decl != NULL_TREE
17283 && TREE_CODE (type_decl) == TYPE_DECL
17284 && TYPE_DECL_ALIAS_P (type_decl))
17285 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17286 else if (is_constrained_parameter (type_decl))
17287 /* Don't do anything. */ ;
17288 else
17289 cp_parser_simulate_error (parser);
17291 if (!cp_parser_parse_definitely (parser))
17292 /* ... Or a typedef-name or an enum-name. */
17293 return cp_parser_nonclass_name (parser);
17296 return type_decl;
17299 /* Check if DECL and ARGS can form a constrained-type-specifier.
17300 If ARGS is non-null, we try to form a concept check of the
17301 form DECL<?, ARGS> where ? is a wildcard that matches any
17302 kind of template argument. If ARGS is NULL, then we try to
17303 form a concept check of the form DECL<?>. */
17305 static tree
17306 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17307 tree decl, tree args)
17309 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17311 /* If we a constrained-type-specifier cannot be deduced. */
17312 if (parser->prevent_constrained_type_specifiers)
17313 return NULL_TREE;
17315 /* A constrained type specifier can only be found in an
17316 overload set or as a reference to a template declaration.
17318 FIXME: This might be masking a bug. It's possible that
17319 that the deduction below is causing template specializations
17320 to be formed with the wildcard as an argument. */
17321 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17322 return NULL_TREE;
17324 /* Try to build a call expression that evaluates the
17325 concept. This can fail if the overload set refers
17326 only to non-templates. */
17327 tree placeholder = build_nt (WILDCARD_DECL);
17328 tree check = build_concept_check (decl, placeholder, args);
17329 if (check == error_mark_node)
17330 return NULL_TREE;
17332 /* Deduce the checked constraint and the prototype parameter.
17334 FIXME: In certain cases, failure to deduce should be a
17335 diagnosable error. */
17336 tree conc;
17337 tree proto;
17338 if (!deduce_constrained_parameter (check, conc, proto))
17339 return NULL_TREE;
17341 /* In template parameter scope, this results in a constrained
17342 parameter. Return a descriptor of that parm. */
17343 if (processing_template_parmlist)
17344 return build_constrained_parameter (conc, proto, args);
17346 /* In a parameter-declaration-clause, constrained-type
17347 specifiers result in invented template parameters. */
17348 if (parser->auto_is_implicit_function_template_parm_p)
17350 tree x = build_constrained_parameter (conc, proto, args);
17351 return synthesize_implicit_template_parm (parser, x);
17353 else
17355 /* Otherwise, we're in a context where the constrained
17356 type name is deduced and the constraint applies
17357 after deduction. */
17358 return make_constrained_auto (conc, args);
17361 return NULL_TREE;
17364 /* If DECL refers to a concept, return a TYPE_DECL representing
17365 the result of using the constrained type specifier in the
17366 current context. DECL refers to a concept if
17368 - it is an overload set containing a function concept taking a single
17369 type argument, or
17371 - it is a variable concept taking a single type argument. */
17373 static tree
17374 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17376 if (flag_concepts
17377 && (TREE_CODE (decl) == OVERLOAD
17378 || BASELINK_P (decl)
17379 || variable_concept_p (decl)))
17380 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17381 else
17382 return NULL_TREE;
17385 /* Check if DECL and ARGS form a partial-concept-id. If so,
17386 assign ID to the resulting constrained placeholder.
17388 Returns true if the partial-concept-id designates a placeholder
17389 and false otherwise. Note that *id is set to NULL_TREE in
17390 this case. */
17392 static tree
17393 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17395 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17398 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17399 or a concept-name.
17401 enum-name:
17402 identifier
17404 typedef-name:
17405 identifier
17407 concept-name:
17408 identifier
17410 Returns a TYPE_DECL for the type. */
17412 static tree
17413 cp_parser_nonclass_name (cp_parser* parser)
17415 tree type_decl;
17416 tree identifier;
17418 cp_token *token = cp_lexer_peek_token (parser->lexer);
17419 identifier = cp_parser_identifier (parser);
17420 if (identifier == error_mark_node)
17421 return error_mark_node;
17423 /* Look up the type-name. */
17424 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17426 type_decl = strip_using_decl (type_decl);
17428 /* If we found an overload set, then it may refer to a concept-name. */
17429 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17430 type_decl = decl;
17432 if (TREE_CODE (type_decl) != TYPE_DECL
17433 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17435 /* See if this is an Objective-C type. */
17436 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17437 tree type = objc_get_protocol_qualified_type (identifier, protos);
17438 if (type)
17439 type_decl = TYPE_NAME (type);
17442 /* Issue an error if we did not find a type-name. */
17443 if (TREE_CODE (type_decl) != TYPE_DECL
17444 /* In Objective-C, we have the complication that class names are
17445 normally type names and start declarations (eg, the
17446 "NSObject" in "NSObject *object;"), but can be used in an
17447 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17448 is an expression. So, a classname followed by a dot is not a
17449 valid type-name. */
17450 || (objc_is_class_name (TREE_TYPE (type_decl))
17451 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17453 if (!cp_parser_simulate_error (parser))
17454 cp_parser_name_lookup_error (parser, identifier, type_decl,
17455 NLE_TYPE, token->location);
17456 return error_mark_node;
17458 /* Remember that the name was used in the definition of the
17459 current class so that we can check later to see if the
17460 meaning would have been different after the class was
17461 entirely defined. */
17462 else if (type_decl != error_mark_node
17463 && !parser->scope)
17464 maybe_note_name_used_in_class (identifier, type_decl);
17466 return type_decl;
17469 /* Parse an elaborated-type-specifier. Note that the grammar given
17470 here incorporates the resolution to DR68.
17472 elaborated-type-specifier:
17473 class-key :: [opt] nested-name-specifier [opt] identifier
17474 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17475 enum-key :: [opt] nested-name-specifier [opt] identifier
17476 typename :: [opt] nested-name-specifier identifier
17477 typename :: [opt] nested-name-specifier template [opt]
17478 template-id
17480 GNU extension:
17482 elaborated-type-specifier:
17483 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17484 class-key attributes :: [opt] nested-name-specifier [opt]
17485 template [opt] template-id
17486 enum attributes :: [opt] nested-name-specifier [opt] identifier
17488 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17489 declared `friend'. If IS_DECLARATION is TRUE, then this
17490 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17491 something is being declared.
17493 Returns the TYPE specified. */
17495 static tree
17496 cp_parser_elaborated_type_specifier (cp_parser* parser,
17497 bool is_friend,
17498 bool is_declaration)
17500 enum tag_types tag_type;
17501 tree identifier;
17502 tree type = NULL_TREE;
17503 tree attributes = NULL_TREE;
17504 tree globalscope;
17505 cp_token *token = NULL;
17507 /* See if we're looking at the `enum' keyword. */
17508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17510 /* Consume the `enum' token. */
17511 cp_lexer_consume_token (parser->lexer);
17512 /* Remember that it's an enumeration type. */
17513 tag_type = enum_type;
17514 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17515 enums) is used here. */
17516 cp_token *token = cp_lexer_peek_token (parser->lexer);
17517 if (cp_parser_is_keyword (token, RID_CLASS)
17518 || cp_parser_is_keyword (token, RID_STRUCT))
17520 gcc_rich_location richloc (token->location);
17521 richloc.add_range (input_location, false);
17522 richloc.add_fixit_remove ();
17523 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17524 "a scoped enum must not use the %qD keyword",
17525 token->u.value);
17526 /* Consume the `struct' or `class' and parse it anyway. */
17527 cp_lexer_consume_token (parser->lexer);
17529 /* Parse the attributes. */
17530 attributes = cp_parser_attributes_opt (parser);
17532 /* Or, it might be `typename'. */
17533 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17534 RID_TYPENAME))
17536 /* Consume the `typename' token. */
17537 cp_lexer_consume_token (parser->lexer);
17538 /* Remember that it's a `typename' type. */
17539 tag_type = typename_type;
17541 /* Otherwise it must be a class-key. */
17542 else
17544 tag_type = cp_parser_class_key (parser);
17545 if (tag_type == none_type)
17546 return error_mark_node;
17547 /* Parse the attributes. */
17548 attributes = cp_parser_attributes_opt (parser);
17551 /* Look for the `::' operator. */
17552 globalscope = cp_parser_global_scope_opt (parser,
17553 /*current_scope_valid_p=*/false);
17554 /* Look for the nested-name-specifier. */
17555 tree nested_name_specifier;
17556 if (tag_type == typename_type && !globalscope)
17558 nested_name_specifier
17559 = cp_parser_nested_name_specifier (parser,
17560 /*typename_keyword_p=*/true,
17561 /*check_dependency_p=*/true,
17562 /*type_p=*/true,
17563 is_declaration);
17564 if (!nested_name_specifier)
17565 return error_mark_node;
17567 else
17568 /* Even though `typename' is not present, the proposed resolution
17569 to Core Issue 180 says that in `class A<T>::B', `B' should be
17570 considered a type-name, even if `A<T>' is dependent. */
17571 nested_name_specifier
17572 = cp_parser_nested_name_specifier_opt (parser,
17573 /*typename_keyword_p=*/true,
17574 /*check_dependency_p=*/true,
17575 /*type_p=*/true,
17576 is_declaration);
17577 /* For everything but enumeration types, consider a template-id.
17578 For an enumeration type, consider only a plain identifier. */
17579 if (tag_type != enum_type)
17581 bool template_p = false;
17582 tree decl;
17584 /* Allow the `template' keyword. */
17585 template_p = cp_parser_optional_template_keyword (parser);
17586 /* If we didn't see `template', we don't know if there's a
17587 template-id or not. */
17588 if (!template_p)
17589 cp_parser_parse_tentatively (parser);
17590 /* Parse the template-id. */
17591 token = cp_lexer_peek_token (parser->lexer);
17592 decl = cp_parser_template_id (parser, template_p,
17593 /*check_dependency_p=*/true,
17594 tag_type,
17595 is_declaration);
17596 /* If we didn't find a template-id, look for an ordinary
17597 identifier. */
17598 if (!template_p && !cp_parser_parse_definitely (parser))
17600 /* We can get here when cp_parser_template_id, called by
17601 cp_parser_class_name with tag_type == none_type, succeeds
17602 and caches a BASELINK. Then, when called again here,
17603 instead of failing and returning an error_mark_node
17604 returns it (see template/typename17.C in C++11).
17605 ??? Could we diagnose this earlier? */
17606 else if (tag_type == typename_type && BASELINK_P (decl))
17608 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17609 type = error_mark_node;
17611 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17612 in effect, then we must assume that, upon instantiation, the
17613 template will correspond to a class. */
17614 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17615 && tag_type == typename_type)
17616 type = make_typename_type (parser->scope, decl,
17617 typename_type,
17618 /*complain=*/tf_error);
17619 /* If the `typename' keyword is in effect and DECL is not a type
17620 decl, then type is non existent. */
17621 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17623 else if (TREE_CODE (decl) == TYPE_DECL)
17625 type = check_elaborated_type_specifier (tag_type, decl,
17626 /*allow_template_p=*/true);
17628 /* If the next token is a semicolon, this must be a specialization,
17629 instantiation, or friend declaration. Check the scope while we
17630 still know whether or not we had a nested-name-specifier. */
17631 if (type != error_mark_node
17632 && !nested_name_specifier && !is_friend
17633 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17634 check_unqualified_spec_or_inst (type, token->location);
17636 else if (decl == error_mark_node)
17637 type = error_mark_node;
17640 if (!type)
17642 token = cp_lexer_peek_token (parser->lexer);
17643 identifier = cp_parser_identifier (parser);
17645 if (identifier == error_mark_node)
17647 parser->scope = NULL_TREE;
17648 return error_mark_node;
17651 /* For a `typename', we needn't call xref_tag. */
17652 if (tag_type == typename_type
17653 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17654 return cp_parser_make_typename_type (parser, identifier,
17655 token->location);
17657 /* Template parameter lists apply only if we are not within a
17658 function parameter list. */
17659 bool template_parm_lists_apply
17660 = parser->num_template_parameter_lists;
17661 if (template_parm_lists_apply)
17662 for (cp_binding_level *s = current_binding_level;
17663 s && s->kind != sk_template_parms;
17664 s = s->level_chain)
17665 if (s->kind == sk_function_parms)
17666 template_parm_lists_apply = false;
17668 /* Look up a qualified name in the usual way. */
17669 if (parser->scope)
17671 tree decl;
17672 tree ambiguous_decls;
17674 decl = cp_parser_lookup_name (parser, identifier,
17675 tag_type,
17676 /*is_template=*/false,
17677 /*is_namespace=*/false,
17678 /*check_dependency=*/true,
17679 &ambiguous_decls,
17680 token->location);
17682 /* If the lookup was ambiguous, an error will already have been
17683 issued. */
17684 if (ambiguous_decls)
17685 return error_mark_node;
17687 /* If we are parsing friend declaration, DECL may be a
17688 TEMPLATE_DECL tree node here. However, we need to check
17689 whether this TEMPLATE_DECL results in valid code. Consider
17690 the following example:
17692 namespace N {
17693 template <class T> class C {};
17695 class X {
17696 template <class T> friend class N::C; // #1, valid code
17698 template <class T> class Y {
17699 friend class N::C; // #2, invalid code
17702 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17703 name lookup of `N::C'. We see that friend declaration must
17704 be template for the code to be valid. Note that
17705 processing_template_decl does not work here since it is
17706 always 1 for the above two cases. */
17708 decl = (cp_parser_maybe_treat_template_as_class
17709 (decl, /*tag_name_p=*/is_friend
17710 && template_parm_lists_apply));
17712 if (TREE_CODE (decl) != TYPE_DECL)
17714 cp_parser_diagnose_invalid_type_name (parser,
17715 identifier,
17716 token->location);
17717 return error_mark_node;
17720 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17722 bool allow_template = (template_parm_lists_apply
17723 || DECL_SELF_REFERENCE_P (decl));
17724 type = check_elaborated_type_specifier (tag_type, decl,
17725 allow_template);
17727 if (type == error_mark_node)
17728 return error_mark_node;
17731 /* Forward declarations of nested types, such as
17733 class C1::C2;
17734 class C1::C2::C3;
17736 are invalid unless all components preceding the final '::'
17737 are complete. If all enclosing types are complete, these
17738 declarations become merely pointless.
17740 Invalid forward declarations of nested types are errors
17741 caught elsewhere in parsing. Those that are pointless arrive
17742 here. */
17744 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17745 && !is_friend && !processing_explicit_instantiation)
17746 warning (0, "declaration %qD does not declare anything", decl);
17748 type = TREE_TYPE (decl);
17750 else
17752 /* An elaborated-type-specifier sometimes introduces a new type and
17753 sometimes names an existing type. Normally, the rule is that it
17754 introduces a new type only if there is not an existing type of
17755 the same name already in scope. For example, given:
17757 struct S {};
17758 void f() { struct S s; }
17760 the `struct S' in the body of `f' is the same `struct S' as in
17761 the global scope; the existing definition is used. However, if
17762 there were no global declaration, this would introduce a new
17763 local class named `S'.
17765 An exception to this rule applies to the following code:
17767 namespace N { struct S; }
17769 Here, the elaborated-type-specifier names a new type
17770 unconditionally; even if there is already an `S' in the
17771 containing scope this declaration names a new type.
17772 This exception only applies if the elaborated-type-specifier
17773 forms the complete declaration:
17775 [class.name]
17777 A declaration consisting solely of `class-key identifier ;' is
17778 either a redeclaration of the name in the current scope or a
17779 forward declaration of the identifier as a class name. It
17780 introduces the name into the current scope.
17782 We are in this situation precisely when the next token is a `;'.
17784 An exception to the exception is that a `friend' declaration does
17785 *not* name a new type; i.e., given:
17787 struct S { friend struct T; };
17789 `T' is not a new type in the scope of `S'.
17791 Also, `new struct S' or `sizeof (struct S)' never results in the
17792 definition of a new type; a new type can only be declared in a
17793 declaration context. */
17795 tag_scope ts;
17796 bool template_p;
17798 if (is_friend)
17799 /* Friends have special name lookup rules. */
17800 ts = ts_within_enclosing_non_class;
17801 else if (is_declaration
17802 && cp_lexer_next_token_is (parser->lexer,
17803 CPP_SEMICOLON))
17804 /* This is a `class-key identifier ;' */
17805 ts = ts_current;
17806 else
17807 ts = ts_global;
17809 template_p =
17810 (template_parm_lists_apply
17811 && (cp_parser_next_token_starts_class_definition_p (parser)
17812 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17813 /* An unqualified name was used to reference this type, so
17814 there were no qualifying templates. */
17815 if (template_parm_lists_apply
17816 && !cp_parser_check_template_parameters (parser,
17817 /*num_templates=*/0,
17818 token->location,
17819 /*declarator=*/NULL))
17820 return error_mark_node;
17821 type = xref_tag (tag_type, identifier, ts, template_p);
17825 if (type == error_mark_node)
17826 return error_mark_node;
17828 /* Allow attributes on forward declarations of classes. */
17829 if (attributes)
17831 if (TREE_CODE (type) == TYPENAME_TYPE)
17832 warning (OPT_Wattributes,
17833 "attributes ignored on uninstantiated type");
17834 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17835 && ! processing_explicit_instantiation)
17836 warning (OPT_Wattributes,
17837 "attributes ignored on template instantiation");
17838 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17839 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17840 else
17841 warning (OPT_Wattributes,
17842 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17845 if (tag_type != enum_type)
17847 /* Indicate whether this class was declared as a `class' or as a
17848 `struct'. */
17849 if (CLASS_TYPE_P (type))
17850 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17851 cp_parser_check_class_key (tag_type, type);
17854 /* A "<" cannot follow an elaborated type specifier. If that
17855 happens, the user was probably trying to form a template-id. */
17856 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17857 token->location);
17859 return type;
17862 /* Parse an enum-specifier.
17864 enum-specifier:
17865 enum-head { enumerator-list [opt] }
17866 enum-head { enumerator-list , } [C++0x]
17868 enum-head:
17869 enum-key identifier [opt] enum-base [opt]
17870 enum-key nested-name-specifier identifier enum-base [opt]
17872 enum-key:
17873 enum
17874 enum class [C++0x]
17875 enum struct [C++0x]
17877 enum-base: [C++0x]
17878 : type-specifier-seq
17880 opaque-enum-specifier:
17881 enum-key identifier enum-base [opt] ;
17883 GNU Extensions:
17884 enum-key attributes[opt] identifier [opt] enum-base [opt]
17885 { enumerator-list [opt] }attributes[opt]
17886 enum-key attributes[opt] identifier [opt] enum-base [opt]
17887 { enumerator-list, }attributes[opt] [C++0x]
17889 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17890 if the token stream isn't an enum-specifier after all. */
17892 static tree
17893 cp_parser_enum_specifier (cp_parser* parser)
17895 tree identifier;
17896 tree type = NULL_TREE;
17897 tree prev_scope;
17898 tree nested_name_specifier = NULL_TREE;
17899 tree attributes;
17900 bool scoped_enum_p = false;
17901 bool has_underlying_type = false;
17902 bool nested_being_defined = false;
17903 bool new_value_list = false;
17904 bool is_new_type = false;
17905 bool is_unnamed = false;
17906 tree underlying_type = NULL_TREE;
17907 cp_token *type_start_token = NULL;
17908 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17910 parser->colon_corrects_to_scope_p = false;
17912 /* Parse tentatively so that we can back up if we don't find a
17913 enum-specifier. */
17914 cp_parser_parse_tentatively (parser);
17916 /* Caller guarantees that the current token is 'enum', an identifier
17917 possibly follows, and the token after that is an opening brace.
17918 If we don't have an identifier, fabricate an anonymous name for
17919 the enumeration being defined. */
17920 cp_lexer_consume_token (parser->lexer);
17922 /* Parse the "class" or "struct", which indicates a scoped
17923 enumeration type in C++0x. */
17924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17925 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17927 if (cxx_dialect < cxx11)
17928 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17930 /* Consume the `struct' or `class' token. */
17931 cp_lexer_consume_token (parser->lexer);
17933 scoped_enum_p = true;
17936 attributes = cp_parser_attributes_opt (parser);
17938 /* Clear the qualification. */
17939 parser->scope = NULL_TREE;
17940 parser->qualifying_scope = NULL_TREE;
17941 parser->object_scope = NULL_TREE;
17943 /* Figure out in what scope the declaration is being placed. */
17944 prev_scope = current_scope ();
17946 type_start_token = cp_lexer_peek_token (parser->lexer);
17948 push_deferring_access_checks (dk_no_check);
17949 nested_name_specifier
17950 = cp_parser_nested_name_specifier_opt (parser,
17951 /*typename_keyword_p=*/true,
17952 /*check_dependency_p=*/false,
17953 /*type_p=*/false,
17954 /*is_declaration=*/false);
17956 if (nested_name_specifier)
17958 tree name;
17960 identifier = cp_parser_identifier (parser);
17961 name = cp_parser_lookup_name (parser, identifier,
17962 enum_type,
17963 /*is_template=*/false,
17964 /*is_namespace=*/false,
17965 /*check_dependency=*/true,
17966 /*ambiguous_decls=*/NULL,
17967 input_location);
17968 if (name && name != error_mark_node)
17970 type = TREE_TYPE (name);
17971 if (TREE_CODE (type) == TYPENAME_TYPE)
17973 /* Are template enums allowed in ISO? */
17974 if (template_parm_scope_p ())
17975 pedwarn (type_start_token->location, OPT_Wpedantic,
17976 "%qD is an enumeration template", name);
17977 /* ignore a typename reference, for it will be solved by name
17978 in start_enum. */
17979 type = NULL_TREE;
17982 else if (nested_name_specifier == error_mark_node)
17983 /* We already issued an error. */;
17984 else
17986 error_at (type_start_token->location,
17987 "%qD does not name an enumeration in %qT",
17988 identifier, nested_name_specifier);
17989 nested_name_specifier = error_mark_node;
17992 else
17994 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17995 identifier = cp_parser_identifier (parser);
17996 else
17998 identifier = make_anon_name ();
17999 is_unnamed = true;
18000 if (scoped_enum_p)
18001 error_at (type_start_token->location,
18002 "unnamed scoped enum is not allowed");
18005 pop_deferring_access_checks ();
18007 /* Check for the `:' that denotes a specified underlying type in C++0x.
18008 Note that a ':' could also indicate a bitfield width, however. */
18009 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18011 cp_decl_specifier_seq type_specifiers;
18013 /* Consume the `:'. */
18014 cp_lexer_consume_token (parser->lexer);
18016 /* Parse the type-specifier-seq. */
18017 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18018 /*is_trailing_return=*/false,
18019 &type_specifiers);
18021 /* At this point this is surely not elaborated type specifier. */
18022 if (!cp_parser_parse_definitely (parser))
18023 return NULL_TREE;
18025 if (cxx_dialect < cxx11)
18026 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18028 has_underlying_type = true;
18030 /* If that didn't work, stop. */
18031 if (type_specifiers.type != error_mark_node)
18033 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18034 /*initialized=*/0, NULL);
18035 if (underlying_type == error_mark_node
18036 || check_for_bare_parameter_packs (underlying_type))
18037 underlying_type = NULL_TREE;
18041 /* Look for the `{' but don't consume it yet. */
18042 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18044 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18046 cp_parser_error (parser, "expected %<{%>");
18047 if (has_underlying_type)
18049 type = NULL_TREE;
18050 goto out;
18053 /* An opaque-enum-specifier must have a ';' here. */
18054 if ((scoped_enum_p || underlying_type)
18055 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18057 cp_parser_error (parser, "expected %<;%> or %<{%>");
18058 if (has_underlying_type)
18060 type = NULL_TREE;
18061 goto out;
18066 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18067 return NULL_TREE;
18069 if (nested_name_specifier)
18071 if (CLASS_TYPE_P (nested_name_specifier))
18073 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18074 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18075 push_scope (nested_name_specifier);
18077 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18079 push_nested_namespace (nested_name_specifier);
18083 /* Issue an error message if type-definitions are forbidden here. */
18084 if (!cp_parser_check_type_definition (parser))
18085 type = error_mark_node;
18086 else
18087 /* Create the new type. We do this before consuming the opening
18088 brace so the enum will be recorded as being on the line of its
18089 tag (or the 'enum' keyword, if there is no tag). */
18090 type = start_enum (identifier, type, underlying_type,
18091 attributes, scoped_enum_p, &is_new_type);
18093 /* If the next token is not '{' it is an opaque-enum-specifier or an
18094 elaborated-type-specifier. */
18095 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18097 timevar_push (TV_PARSE_ENUM);
18098 if (nested_name_specifier
18099 && nested_name_specifier != error_mark_node)
18101 /* The following catches invalid code such as:
18102 enum class S<int>::E { A, B, C }; */
18103 if (!processing_specialization
18104 && CLASS_TYPE_P (nested_name_specifier)
18105 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18106 error_at (type_start_token->location, "cannot add an enumerator "
18107 "list to a template instantiation");
18109 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18111 error_at (type_start_token->location,
18112 "%<%T::%E%> has not been declared",
18113 TYPE_CONTEXT (nested_name_specifier),
18114 nested_name_specifier);
18115 type = error_mark_node;
18117 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18118 && !CLASS_TYPE_P (nested_name_specifier))
18120 error_at (type_start_token->location, "nested name specifier "
18121 "%qT for enum declaration does not name a class "
18122 "or namespace", nested_name_specifier);
18123 type = error_mark_node;
18125 /* If that scope does not contain the scope in which the
18126 class was originally declared, the program is invalid. */
18127 else if (prev_scope && !is_ancestor (prev_scope,
18128 nested_name_specifier))
18130 if (at_namespace_scope_p ())
18131 error_at (type_start_token->location,
18132 "declaration of %qD in namespace %qD which does not "
18133 "enclose %qD",
18134 type, prev_scope, nested_name_specifier);
18135 else
18136 error_at (type_start_token->location,
18137 "declaration of %qD in %qD which does not "
18138 "enclose %qD",
18139 type, prev_scope, nested_name_specifier);
18140 type = error_mark_node;
18142 /* If that scope is the scope where the declaration is being placed
18143 the program is invalid. */
18144 else if (CLASS_TYPE_P (nested_name_specifier)
18145 && CLASS_TYPE_P (prev_scope)
18146 && same_type_p (nested_name_specifier, prev_scope))
18148 permerror (type_start_token->location,
18149 "extra qualification not allowed");
18150 nested_name_specifier = NULL_TREE;
18154 if (scoped_enum_p)
18155 begin_scope (sk_scoped_enum, type);
18157 /* Consume the opening brace. */
18158 matching_braces braces;
18159 braces.consume_open (parser);
18161 if (type == error_mark_node)
18162 ; /* Nothing to add */
18163 else if (OPAQUE_ENUM_P (type)
18164 || (cxx_dialect > cxx98 && processing_specialization))
18166 new_value_list = true;
18167 SET_OPAQUE_ENUM_P (type, false);
18168 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18170 else
18172 error_at (type_start_token->location,
18173 "multiple definition of %q#T", type);
18174 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18175 "previous definition here");
18176 type = error_mark_node;
18179 if (type == error_mark_node)
18180 cp_parser_skip_to_end_of_block_or_statement (parser);
18181 /* If the next token is not '}', then there are some enumerators. */
18182 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18184 if (is_unnamed && !scoped_enum_p)
18185 pedwarn (type_start_token->location, OPT_Wpedantic,
18186 "ISO C++ forbids empty unnamed enum");
18188 else
18189 cp_parser_enumerator_list (parser, type);
18191 /* Consume the final '}'. */
18192 braces.require_close (parser);
18194 if (scoped_enum_p)
18195 finish_scope ();
18196 timevar_pop (TV_PARSE_ENUM);
18198 else
18200 /* If a ';' follows, then it is an opaque-enum-specifier
18201 and additional restrictions apply. */
18202 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18204 if (is_unnamed)
18205 error_at (type_start_token->location,
18206 "opaque-enum-specifier without name");
18207 else if (nested_name_specifier)
18208 error_at (type_start_token->location,
18209 "opaque-enum-specifier must use a simple identifier");
18213 /* Look for trailing attributes to apply to this enumeration, and
18214 apply them if appropriate. */
18215 if (cp_parser_allow_gnu_extensions_p (parser))
18217 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18218 cplus_decl_attributes (&type,
18219 trailing_attr,
18220 (int) ATTR_FLAG_TYPE_IN_PLACE);
18223 /* Finish up the enumeration. */
18224 if (type != error_mark_node)
18226 if (new_value_list)
18227 finish_enum_value_list (type);
18228 if (is_new_type)
18229 finish_enum (type);
18232 if (nested_name_specifier)
18234 if (CLASS_TYPE_P (nested_name_specifier))
18236 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18237 pop_scope (nested_name_specifier);
18239 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18241 pop_nested_namespace (nested_name_specifier);
18244 out:
18245 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18246 return type;
18249 /* Parse an enumerator-list. The enumerators all have the indicated
18250 TYPE.
18252 enumerator-list:
18253 enumerator-definition
18254 enumerator-list , enumerator-definition */
18256 static void
18257 cp_parser_enumerator_list (cp_parser* parser, tree type)
18259 while (true)
18261 /* Parse an enumerator-definition. */
18262 cp_parser_enumerator_definition (parser, type);
18264 /* If the next token is not a ',', we've reached the end of
18265 the list. */
18266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18267 break;
18268 /* Otherwise, consume the `,' and keep going. */
18269 cp_lexer_consume_token (parser->lexer);
18270 /* If the next token is a `}', there is a trailing comma. */
18271 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18273 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18274 pedwarn (input_location, OPT_Wpedantic,
18275 "comma at end of enumerator list");
18276 break;
18281 /* Parse an enumerator-definition. The enumerator has the indicated
18282 TYPE.
18284 enumerator-definition:
18285 enumerator
18286 enumerator = constant-expression
18288 enumerator:
18289 identifier
18291 GNU Extensions:
18293 enumerator-definition:
18294 enumerator attributes [opt]
18295 enumerator attributes [opt] = constant-expression */
18297 static void
18298 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18300 tree identifier;
18301 tree value;
18302 location_t loc;
18304 /* Save the input location because we are interested in the location
18305 of the identifier and not the location of the explicit value. */
18306 loc = cp_lexer_peek_token (parser->lexer)->location;
18308 /* Look for the identifier. */
18309 identifier = cp_parser_identifier (parser);
18310 if (identifier == error_mark_node)
18311 return;
18313 /* Parse any specified attributes. */
18314 tree attrs = cp_parser_attributes_opt (parser);
18316 /* If the next token is an '=', then there is an explicit value. */
18317 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18319 /* Consume the `=' token. */
18320 cp_lexer_consume_token (parser->lexer);
18321 /* Parse the value. */
18322 value = cp_parser_constant_expression (parser);
18324 else
18325 value = NULL_TREE;
18327 /* If we are processing a template, make sure the initializer of the
18328 enumerator doesn't contain any bare template parameter pack. */
18329 if (check_for_bare_parameter_packs (value))
18330 value = error_mark_node;
18332 /* Create the enumerator. */
18333 build_enumerator (identifier, value, type, attrs, loc);
18336 /* Parse a namespace-name.
18338 namespace-name:
18339 original-namespace-name
18340 namespace-alias
18342 Returns the NAMESPACE_DECL for the namespace. */
18344 static tree
18345 cp_parser_namespace_name (cp_parser* parser)
18347 tree identifier;
18348 tree namespace_decl;
18350 cp_token *token = cp_lexer_peek_token (parser->lexer);
18352 /* Get the name of the namespace. */
18353 identifier = cp_parser_identifier (parser);
18354 if (identifier == error_mark_node)
18355 return error_mark_node;
18357 /* Look up the identifier in the currently active scope. Look only
18358 for namespaces, due to:
18360 [basic.lookup.udir]
18362 When looking up a namespace-name in a using-directive or alias
18363 definition, only namespace names are considered.
18365 And:
18367 [basic.lookup.qual]
18369 During the lookup of a name preceding the :: scope resolution
18370 operator, object, function, and enumerator names are ignored.
18372 (Note that cp_parser_qualifying_entity only calls this
18373 function if the token after the name is the scope resolution
18374 operator.) */
18375 namespace_decl = cp_parser_lookup_name (parser, identifier,
18376 none_type,
18377 /*is_template=*/false,
18378 /*is_namespace=*/true,
18379 /*check_dependency=*/true,
18380 /*ambiguous_decls=*/NULL,
18381 token->location);
18382 /* If it's not a namespace, issue an error. */
18383 if (namespace_decl == error_mark_node
18384 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18386 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18388 error_at (token->location, "%qD is not a namespace-name", identifier);
18389 if (namespace_decl == error_mark_node
18390 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18391 suggest_alternative_in_explicit_scope (token->location, identifier,
18392 parser->scope);
18394 cp_parser_error (parser, "expected namespace-name");
18395 namespace_decl = error_mark_node;
18398 return namespace_decl;
18401 /* Parse a namespace-definition.
18403 namespace-definition:
18404 named-namespace-definition
18405 unnamed-namespace-definition
18407 named-namespace-definition:
18408 original-namespace-definition
18409 extension-namespace-definition
18411 original-namespace-definition:
18412 namespace identifier { namespace-body }
18414 extension-namespace-definition:
18415 namespace original-namespace-name { namespace-body }
18417 unnamed-namespace-definition:
18418 namespace { namespace-body } */
18420 static void
18421 cp_parser_namespace_definition (cp_parser* parser)
18423 tree identifier;
18424 int nested_definition_count = 0;
18426 cp_ensure_no_omp_declare_simd (parser);
18427 cp_ensure_no_oacc_routine (parser);
18429 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18431 if (is_inline)
18433 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18434 cp_lexer_consume_token (parser->lexer);
18437 /* Look for the `namespace' keyword. */
18438 cp_token* token
18439 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18441 /* Parse any specified attributes before the identifier. */
18442 tree attribs = cp_parser_attributes_opt (parser);
18444 for (;;)
18446 identifier = NULL_TREE;
18448 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18450 identifier = cp_parser_identifier (parser);
18452 /* Parse any attributes specified after the identifier. */
18453 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18457 break;
18459 if (!nested_definition_count && cxx_dialect < cxx17)
18460 pedwarn (input_location, OPT_Wpedantic,
18461 "nested namespace definitions only available with "
18462 "-std=c++17 or -std=gnu++17");
18464 /* Nested namespace names can create new namespaces (unlike
18465 other qualified-ids). */
18466 if (int count = identifier ? push_namespace (identifier) : 0)
18467 nested_definition_count += count;
18468 else
18469 cp_parser_error (parser, "nested namespace name required");
18470 cp_lexer_consume_token (parser->lexer);
18473 if (nested_definition_count && !identifier)
18474 cp_parser_error (parser, "namespace name required");
18476 if (nested_definition_count && attribs)
18477 error_at (token->location,
18478 "a nested namespace definition cannot have attributes");
18479 if (nested_definition_count && is_inline)
18480 error_at (token->location,
18481 "a nested namespace definition cannot be inline");
18483 /* Start the namespace. */
18484 nested_definition_count += push_namespace (identifier, is_inline);
18486 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18488 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18490 /* Look for the `{' to validate starting the namespace. */
18491 matching_braces braces;
18492 if (braces.require_open (parser))
18494 /* Parse the body of the namespace. */
18495 cp_parser_namespace_body (parser);
18497 /* Look for the final `}'. */
18498 braces.require_close (parser);
18501 if (has_visibility)
18502 pop_visibility (1);
18504 /* Pop the nested namespace definitions. */
18505 while (nested_definition_count--)
18506 pop_namespace ();
18509 /* Parse a namespace-body.
18511 namespace-body:
18512 declaration-seq [opt] */
18514 static void
18515 cp_parser_namespace_body (cp_parser* parser)
18517 cp_parser_declaration_seq_opt (parser);
18520 /* Parse a namespace-alias-definition.
18522 namespace-alias-definition:
18523 namespace identifier = qualified-namespace-specifier ; */
18525 static void
18526 cp_parser_namespace_alias_definition (cp_parser* parser)
18528 tree identifier;
18529 tree namespace_specifier;
18531 cp_token *token = cp_lexer_peek_token (parser->lexer);
18533 /* Look for the `namespace' keyword. */
18534 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18535 /* Look for the identifier. */
18536 identifier = cp_parser_identifier (parser);
18537 if (identifier == error_mark_node)
18538 return;
18539 /* Look for the `=' token. */
18540 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18541 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18543 error_at (token->location, "%<namespace%> definition is not allowed here");
18544 /* Skip the definition. */
18545 cp_lexer_consume_token (parser->lexer);
18546 if (cp_parser_skip_to_closing_brace (parser))
18547 cp_lexer_consume_token (parser->lexer);
18548 return;
18550 cp_parser_require (parser, CPP_EQ, RT_EQ);
18551 /* Look for the qualified-namespace-specifier. */
18552 namespace_specifier
18553 = cp_parser_qualified_namespace_specifier (parser);
18554 /* Look for the `;' token. */
18555 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18557 /* Register the alias in the symbol table. */
18558 do_namespace_alias (identifier, namespace_specifier);
18561 /* Parse a qualified-namespace-specifier.
18563 qualified-namespace-specifier:
18564 :: [opt] nested-name-specifier [opt] namespace-name
18566 Returns a NAMESPACE_DECL corresponding to the specified
18567 namespace. */
18569 static tree
18570 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18572 /* Look for the optional `::'. */
18573 cp_parser_global_scope_opt (parser,
18574 /*current_scope_valid_p=*/false);
18576 /* Look for the optional nested-name-specifier. */
18577 cp_parser_nested_name_specifier_opt (parser,
18578 /*typename_keyword_p=*/false,
18579 /*check_dependency_p=*/true,
18580 /*type_p=*/false,
18581 /*is_declaration=*/true);
18583 return cp_parser_namespace_name (parser);
18586 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18587 access declaration.
18589 using-declaration:
18590 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18591 using :: unqualified-id ;
18593 access-declaration:
18594 qualified-id ;
18598 static bool
18599 cp_parser_using_declaration (cp_parser* parser,
18600 bool access_declaration_p)
18602 cp_token *token;
18603 bool typename_p = false;
18604 bool global_scope_p;
18605 tree decl;
18606 tree identifier;
18607 tree qscope;
18608 int oldcount = errorcount;
18609 cp_token *diag_token = NULL;
18611 if (access_declaration_p)
18613 diag_token = cp_lexer_peek_token (parser->lexer);
18614 cp_parser_parse_tentatively (parser);
18616 else
18618 /* Look for the `using' keyword. */
18619 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18621 again:
18622 /* Peek at the next token. */
18623 token = cp_lexer_peek_token (parser->lexer);
18624 /* See if it's `typename'. */
18625 if (token->keyword == RID_TYPENAME)
18627 /* Remember that we've seen it. */
18628 typename_p = true;
18629 /* Consume the `typename' token. */
18630 cp_lexer_consume_token (parser->lexer);
18634 /* Look for the optional global scope qualification. */
18635 global_scope_p
18636 = (cp_parser_global_scope_opt (parser,
18637 /*current_scope_valid_p=*/false)
18638 != NULL_TREE);
18640 /* If we saw `typename', or didn't see `::', then there must be a
18641 nested-name-specifier present. */
18642 if (typename_p || !global_scope_p)
18644 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18645 /*check_dependency_p=*/true,
18646 /*type_p=*/false,
18647 /*is_declaration=*/true);
18648 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18650 cp_parser_skip_to_end_of_block_or_statement (parser);
18651 return false;
18654 /* Otherwise, we could be in either of the two productions. In that
18655 case, treat the nested-name-specifier as optional. */
18656 else
18657 qscope = cp_parser_nested_name_specifier_opt (parser,
18658 /*typename_keyword_p=*/false,
18659 /*check_dependency_p=*/true,
18660 /*type_p=*/false,
18661 /*is_declaration=*/true);
18662 if (!qscope)
18663 qscope = global_namespace;
18664 else if (UNSCOPED_ENUM_P (qscope))
18665 qscope = CP_TYPE_CONTEXT (qscope);
18667 if (access_declaration_p && cp_parser_error_occurred (parser))
18668 /* Something has already gone wrong; there's no need to parse
18669 further. Since an error has occurred, the return value of
18670 cp_parser_parse_definitely will be false, as required. */
18671 return cp_parser_parse_definitely (parser);
18673 token = cp_lexer_peek_token (parser->lexer);
18674 /* Parse the unqualified-id. */
18675 identifier = cp_parser_unqualified_id (parser,
18676 /*template_keyword_p=*/false,
18677 /*check_dependency_p=*/true,
18678 /*declarator_p=*/true,
18679 /*optional_p=*/false);
18681 if (access_declaration_p)
18683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18684 cp_parser_simulate_error (parser);
18685 if (!cp_parser_parse_definitely (parser))
18686 return false;
18688 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18690 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18691 if (cxx_dialect < cxx17
18692 && !in_system_header_at (ell->location))
18693 pedwarn (ell->location, 0,
18694 "pack expansion in using-declaration only available "
18695 "with -std=c++17 or -std=gnu++17");
18696 qscope = make_pack_expansion (qscope);
18699 /* The function we call to handle a using-declaration is different
18700 depending on what scope we are in. */
18701 if (qscope == error_mark_node || identifier == error_mark_node)
18703 else if (!identifier_p (identifier)
18704 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18705 /* [namespace.udecl]
18707 A using declaration shall not name a template-id. */
18708 error_at (token->location,
18709 "a template-id may not appear in a using-declaration");
18710 else
18712 if (at_class_scope_p ())
18714 /* Create the USING_DECL. */
18715 decl = do_class_using_decl (qscope, identifier);
18717 if (decl && typename_p)
18718 USING_DECL_TYPENAME_P (decl) = 1;
18720 if (check_for_bare_parameter_packs (decl))
18722 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18723 return false;
18725 else
18726 /* Add it to the list of members in this class. */
18727 finish_member_declaration (decl);
18729 else
18731 decl = cp_parser_lookup_name_simple (parser,
18732 identifier,
18733 token->location);
18734 if (decl == error_mark_node)
18735 cp_parser_name_lookup_error (parser, identifier,
18736 decl, NLE_NULL,
18737 token->location);
18738 else if (check_for_bare_parameter_packs (decl))
18740 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18741 return false;
18743 else if (!at_namespace_scope_p ())
18744 finish_local_using_decl (decl, qscope, identifier);
18745 else
18746 finish_namespace_using_decl (decl, qscope, identifier);
18750 if (!access_declaration_p
18751 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18753 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18754 if (cxx_dialect < cxx17)
18755 pedwarn (comma->location, 0,
18756 "comma-separated list in using-declaration only available "
18757 "with -std=c++17 or -std=gnu++17");
18758 goto again;
18761 /* Look for the final `;'. */
18762 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18764 if (access_declaration_p && errorcount == oldcount)
18765 warning_at (diag_token->location, OPT_Wdeprecated,
18766 "access declarations are deprecated "
18767 "in favour of using-declarations; "
18768 "suggestion: add the %<using%> keyword");
18770 return true;
18773 /* Parse an alias-declaration.
18775 alias-declaration:
18776 using identifier attribute-specifier-seq [opt] = type-id */
18778 static tree
18779 cp_parser_alias_declaration (cp_parser* parser)
18781 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18782 location_t id_location;
18783 cp_declarator *declarator;
18784 cp_decl_specifier_seq decl_specs;
18785 bool member_p;
18786 const char *saved_message = NULL;
18788 /* Look for the `using' keyword. */
18789 cp_token *using_token
18790 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18791 if (using_token == NULL)
18792 return error_mark_node;
18794 id_location = cp_lexer_peek_token (parser->lexer)->location;
18795 id = cp_parser_identifier (parser);
18796 if (id == error_mark_node)
18797 return error_mark_node;
18799 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18800 attributes = cp_parser_attributes_opt (parser);
18801 if (attributes == error_mark_node)
18802 return error_mark_node;
18804 cp_parser_require (parser, CPP_EQ, RT_EQ);
18806 if (cp_parser_error_occurred (parser))
18807 return error_mark_node;
18809 cp_parser_commit_to_tentative_parse (parser);
18811 /* Now we are going to parse the type-id of the declaration. */
18814 [dcl.type]/3 says:
18816 "A type-specifier-seq shall not define a class or enumeration
18817 unless it appears in the type-id of an alias-declaration (7.1.3) that
18818 is not the declaration of a template-declaration."
18820 In other words, if we currently are in an alias template, the
18821 type-id should not define a type.
18823 So let's set parser->type_definition_forbidden_message in that
18824 case; cp_parser_check_type_definition (called by
18825 cp_parser_class_specifier) will then emit an error if a type is
18826 defined in the type-id. */
18827 if (parser->num_template_parameter_lists)
18829 saved_message = parser->type_definition_forbidden_message;
18830 parser->type_definition_forbidden_message =
18831 G_("types may not be defined in alias template declarations");
18834 type = cp_parser_type_id (parser);
18836 /* Restore the error message if need be. */
18837 if (parser->num_template_parameter_lists)
18838 parser->type_definition_forbidden_message = saved_message;
18840 if (type == error_mark_node
18841 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18843 cp_parser_skip_to_end_of_block_or_statement (parser);
18844 return error_mark_node;
18847 /* A typedef-name can also be introduced by an alias-declaration. The
18848 identifier following the using keyword becomes a typedef-name. It has
18849 the same semantics as if it were introduced by the typedef
18850 specifier. In particular, it does not define a new type and it shall
18851 not appear in the type-id. */
18853 clear_decl_specs (&decl_specs);
18854 decl_specs.type = type;
18855 if (attributes != NULL_TREE)
18857 decl_specs.attributes = attributes;
18858 set_and_check_decl_spec_loc (&decl_specs,
18859 ds_attribute,
18860 attrs_token);
18862 set_and_check_decl_spec_loc (&decl_specs,
18863 ds_typedef,
18864 using_token);
18865 set_and_check_decl_spec_loc (&decl_specs,
18866 ds_alias,
18867 using_token);
18869 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18870 declarator->id_loc = id_location;
18872 member_p = at_class_scope_p ();
18873 if (member_p)
18874 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18875 NULL_TREE, attributes);
18876 else
18877 decl = start_decl (declarator, &decl_specs, 0,
18878 attributes, NULL_TREE, &pushed_scope);
18879 if (decl == error_mark_node)
18880 return decl;
18882 // Attach constraints to the alias declaration.
18883 if (flag_concepts && current_template_parms)
18885 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18886 tree constr = build_constraints (reqs, NULL_TREE);
18887 set_constraints (decl, constr);
18890 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18892 if (pushed_scope)
18893 pop_scope (pushed_scope);
18895 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18896 added into the symbol table; otherwise, return the TYPE_DECL. */
18897 if (DECL_LANG_SPECIFIC (decl)
18898 && DECL_TEMPLATE_INFO (decl)
18899 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18901 decl = DECL_TI_TEMPLATE (decl);
18902 if (member_p)
18903 check_member_template (decl);
18906 return decl;
18909 /* Parse a using-directive.
18911 using-directive:
18912 using namespace :: [opt] nested-name-specifier [opt]
18913 namespace-name ; */
18915 static void
18916 cp_parser_using_directive (cp_parser* parser)
18918 tree namespace_decl;
18919 tree attribs;
18921 /* Look for the `using' keyword. */
18922 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18923 /* And the `namespace' keyword. */
18924 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18925 /* Look for the optional `::' operator. */
18926 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18927 /* And the optional nested-name-specifier. */
18928 cp_parser_nested_name_specifier_opt (parser,
18929 /*typename_keyword_p=*/false,
18930 /*check_dependency_p=*/true,
18931 /*type_p=*/false,
18932 /*is_declaration=*/true);
18933 /* Get the namespace being used. */
18934 namespace_decl = cp_parser_namespace_name (parser);
18935 /* And any specified attributes. */
18936 attribs = cp_parser_attributes_opt (parser);
18938 /* Update the symbol table. */
18939 if (namespace_bindings_p ())
18940 finish_namespace_using_directive (namespace_decl, attribs);
18941 else
18942 finish_local_using_directive (namespace_decl, attribs);
18944 /* Look for the final `;'. */
18945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18948 /* Parse an asm-definition.
18950 asm-definition:
18951 asm ( string-literal ) ;
18953 GNU Extension:
18955 asm-definition:
18956 asm volatile [opt] ( string-literal ) ;
18957 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18958 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18959 : asm-operand-list [opt] ) ;
18960 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18961 : asm-operand-list [opt]
18962 : asm-clobber-list [opt] ) ;
18963 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18964 : asm-clobber-list [opt]
18965 : asm-goto-list ) ; */
18967 static void
18968 cp_parser_asm_definition (cp_parser* parser)
18970 tree string;
18971 tree outputs = NULL_TREE;
18972 tree inputs = NULL_TREE;
18973 tree clobbers = NULL_TREE;
18974 tree labels = NULL_TREE;
18975 tree asm_stmt;
18976 bool volatile_p = false;
18977 bool extended_p = false;
18978 bool invalid_inputs_p = false;
18979 bool invalid_outputs_p = false;
18980 bool goto_p = false;
18981 required_token missing = RT_NONE;
18983 /* Look for the `asm' keyword. */
18984 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18986 if (parser->in_function_body
18987 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18989 error ("%<asm%> in %<constexpr%> function");
18990 cp_function_chain->invalid_constexpr = true;
18993 /* See if the next token is `volatile'. */
18994 if (cp_parser_allow_gnu_extensions_p (parser)
18995 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18997 /* Remember that we saw the `volatile' keyword. */
18998 volatile_p = true;
18999 /* Consume the token. */
19000 cp_lexer_consume_token (parser->lexer);
19002 if (cp_parser_allow_gnu_extensions_p (parser)
19003 && parser->in_function_body
19004 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19006 /* Remember that we saw the `goto' keyword. */
19007 goto_p = true;
19008 /* Consume the token. */
19009 cp_lexer_consume_token (parser->lexer);
19011 /* Look for the opening `('. */
19012 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19013 return;
19014 /* Look for the string. */
19015 string = cp_parser_string_literal (parser, false, false);
19016 if (string == error_mark_node)
19018 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19019 /*consume_paren=*/true);
19020 return;
19023 /* If we're allowing GNU extensions, check for the extended assembly
19024 syntax. Unfortunately, the `:' tokens need not be separated by
19025 a space in C, and so, for compatibility, we tolerate that here
19026 too. Doing that means that we have to treat the `::' operator as
19027 two `:' tokens. */
19028 if (cp_parser_allow_gnu_extensions_p (parser)
19029 && parser->in_function_body
19030 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19031 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19033 bool inputs_p = false;
19034 bool clobbers_p = false;
19035 bool labels_p = false;
19037 /* The extended syntax was used. */
19038 extended_p = true;
19040 /* Look for outputs. */
19041 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19043 /* Consume the `:'. */
19044 cp_lexer_consume_token (parser->lexer);
19045 /* Parse the output-operands. */
19046 if (cp_lexer_next_token_is_not (parser->lexer,
19047 CPP_COLON)
19048 && cp_lexer_next_token_is_not (parser->lexer,
19049 CPP_SCOPE)
19050 && cp_lexer_next_token_is_not (parser->lexer,
19051 CPP_CLOSE_PAREN)
19052 && !goto_p)
19054 outputs = cp_parser_asm_operand_list (parser);
19055 if (outputs == error_mark_node)
19056 invalid_outputs_p = true;
19059 /* If the next token is `::', there are no outputs, and the
19060 next token is the beginning of the inputs. */
19061 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19062 /* The inputs are coming next. */
19063 inputs_p = true;
19065 /* Look for inputs. */
19066 if (inputs_p
19067 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19069 /* Consume the `:' or `::'. */
19070 cp_lexer_consume_token (parser->lexer);
19071 /* Parse the output-operands. */
19072 if (cp_lexer_next_token_is_not (parser->lexer,
19073 CPP_COLON)
19074 && cp_lexer_next_token_is_not (parser->lexer,
19075 CPP_SCOPE)
19076 && cp_lexer_next_token_is_not (parser->lexer,
19077 CPP_CLOSE_PAREN))
19079 inputs = cp_parser_asm_operand_list (parser);
19080 if (inputs == error_mark_node)
19081 invalid_inputs_p = true;
19084 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19085 /* The clobbers are coming next. */
19086 clobbers_p = true;
19088 /* Look for clobbers. */
19089 if (clobbers_p
19090 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19092 clobbers_p = true;
19093 /* Consume the `:' or `::'. */
19094 cp_lexer_consume_token (parser->lexer);
19095 /* Parse the clobbers. */
19096 if (cp_lexer_next_token_is_not (parser->lexer,
19097 CPP_COLON)
19098 && cp_lexer_next_token_is_not (parser->lexer,
19099 CPP_CLOSE_PAREN))
19100 clobbers = cp_parser_asm_clobber_list (parser);
19102 else if (goto_p
19103 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19104 /* The labels are coming next. */
19105 labels_p = true;
19107 /* Look for labels. */
19108 if (labels_p
19109 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19111 labels_p = true;
19112 /* Consume the `:' or `::'. */
19113 cp_lexer_consume_token (parser->lexer);
19114 /* Parse the labels. */
19115 labels = cp_parser_asm_label_list (parser);
19118 if (goto_p && !labels_p)
19119 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19121 else if (goto_p)
19122 missing = RT_COLON_SCOPE;
19124 /* Look for the closing `)'. */
19125 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19126 missing ? missing : RT_CLOSE_PAREN))
19127 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19128 /*consume_paren=*/true);
19129 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19131 if (!invalid_inputs_p && !invalid_outputs_p)
19133 /* Create the ASM_EXPR. */
19134 if (parser->in_function_body)
19136 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19137 inputs, clobbers, labels);
19138 /* If the extended syntax was not used, mark the ASM_EXPR. */
19139 if (!extended_p)
19141 tree temp = asm_stmt;
19142 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19143 temp = TREE_OPERAND (temp, 0);
19145 ASM_INPUT_P (temp) = 1;
19148 else
19149 symtab->finalize_toplevel_asm (string);
19153 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19154 type that comes from the decl-specifier-seq. */
19156 static tree
19157 strip_declarator_types (tree type, cp_declarator *declarator)
19159 for (cp_declarator *d = declarator; d;)
19160 switch (d->kind)
19162 case cdk_id:
19163 case cdk_decomp:
19164 case cdk_error:
19165 d = NULL;
19166 break;
19168 default:
19169 if (TYPE_PTRMEMFUNC_P (type))
19170 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19171 type = TREE_TYPE (type);
19172 d = d->declarator;
19173 break;
19176 return type;
19179 /* Declarators [gram.dcl.decl] */
19181 /* Parse an init-declarator.
19183 init-declarator:
19184 declarator initializer [opt]
19186 GNU Extension:
19188 init-declarator:
19189 declarator asm-specification [opt] attributes [opt] initializer [opt]
19191 function-definition:
19192 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19193 function-body
19194 decl-specifier-seq [opt] declarator function-try-block
19196 GNU Extension:
19198 function-definition:
19199 __extension__ function-definition
19201 TM Extension:
19203 function-definition:
19204 decl-specifier-seq [opt] declarator function-transaction-block
19206 The DECL_SPECIFIERS apply to this declarator. Returns a
19207 representation of the entity declared. If MEMBER_P is TRUE, then
19208 this declarator appears in a class scope. The new DECL created by
19209 this declarator is returned.
19211 The CHECKS are access checks that should be performed once we know
19212 what entity is being declared (and, therefore, what classes have
19213 befriended it).
19215 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19216 for a function-definition here as well. If the declarator is a
19217 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19218 be TRUE upon return. By that point, the function-definition will
19219 have been completely parsed.
19221 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19222 is FALSE.
19224 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19225 parsed declaration if it is an uninitialized single declarator not followed
19226 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19227 if present, will not be consumed. If returned, this declarator will be
19228 created with SD_INITIALIZED but will not call cp_finish_decl.
19230 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19231 and there is an initializer, the pointed location_t is set to the
19232 location of the '=' or `(', or '{' in C++11 token introducing the
19233 initializer. */
19235 static tree
19236 cp_parser_init_declarator (cp_parser* parser,
19237 cp_decl_specifier_seq *decl_specifiers,
19238 vec<deferred_access_check, va_gc> *checks,
19239 bool function_definition_allowed_p,
19240 bool member_p,
19241 int declares_class_or_enum,
19242 bool* function_definition_p,
19243 tree* maybe_range_for_decl,
19244 location_t* init_loc,
19245 tree* auto_result)
19247 cp_token *token = NULL, *asm_spec_start_token = NULL,
19248 *attributes_start_token = NULL;
19249 cp_declarator *declarator;
19250 tree prefix_attributes;
19251 tree attributes = NULL;
19252 tree asm_specification;
19253 tree initializer;
19254 tree decl = NULL_TREE;
19255 tree scope;
19256 int is_initialized;
19257 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19258 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19259 "(...)". */
19260 enum cpp_ttype initialization_kind;
19261 bool is_direct_init = false;
19262 bool is_non_constant_init;
19263 int ctor_dtor_or_conv_p;
19264 bool friend_p = cp_parser_friend_p (decl_specifiers);
19265 tree pushed_scope = NULL_TREE;
19266 bool range_for_decl_p = false;
19267 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19268 location_t tmp_init_loc = UNKNOWN_LOCATION;
19270 /* Gather the attributes that were provided with the
19271 decl-specifiers. */
19272 prefix_attributes = decl_specifiers->attributes;
19274 /* Assume that this is not the declarator for a function
19275 definition. */
19276 if (function_definition_p)
19277 *function_definition_p = false;
19279 /* Default arguments are only permitted for function parameters. */
19280 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19281 parser->default_arg_ok_p = false;
19283 /* Defer access checks while parsing the declarator; we cannot know
19284 what names are accessible until we know what is being
19285 declared. */
19286 resume_deferring_access_checks ();
19288 token = cp_lexer_peek_token (parser->lexer);
19290 /* Parse the declarator. */
19291 declarator
19292 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19293 &ctor_dtor_or_conv_p,
19294 /*parenthesized_p=*/NULL,
19295 member_p, friend_p);
19296 /* Gather up the deferred checks. */
19297 stop_deferring_access_checks ();
19299 parser->default_arg_ok_p = saved_default_arg_ok_p;
19301 /* If the DECLARATOR was erroneous, there's no need to go
19302 further. */
19303 if (declarator == cp_error_declarator)
19304 return error_mark_node;
19306 /* Check that the number of template-parameter-lists is OK. */
19307 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19308 token->location))
19309 return error_mark_node;
19311 if (declares_class_or_enum & 2)
19312 cp_parser_check_for_definition_in_return_type (declarator,
19313 decl_specifiers->type,
19314 decl_specifiers->locations[ds_type_spec]);
19316 /* Figure out what scope the entity declared by the DECLARATOR is
19317 located in. `grokdeclarator' sometimes changes the scope, so
19318 we compute it now. */
19319 scope = get_scope_of_declarator (declarator);
19321 /* Perform any lookups in the declared type which were thought to be
19322 dependent, but are not in the scope of the declarator. */
19323 decl_specifiers->type
19324 = maybe_update_decl_type (decl_specifiers->type, scope);
19326 /* If we're allowing GNU extensions, look for an
19327 asm-specification. */
19328 if (cp_parser_allow_gnu_extensions_p (parser))
19330 /* Look for an asm-specification. */
19331 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19332 asm_specification = cp_parser_asm_specification_opt (parser);
19334 else
19335 asm_specification = NULL_TREE;
19337 /* Look for attributes. */
19338 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19339 attributes = cp_parser_attributes_opt (parser);
19341 /* Peek at the next token. */
19342 token = cp_lexer_peek_token (parser->lexer);
19344 bool bogus_implicit_tmpl = false;
19346 if (function_declarator_p (declarator))
19348 /* Handle C++17 deduction guides. */
19349 if (!decl_specifiers->type
19350 && ctor_dtor_or_conv_p <= 0
19351 && cxx_dialect >= cxx17)
19353 cp_declarator *id = get_id_declarator (declarator);
19354 tree name = id->u.id.unqualified_name;
19355 parser->scope = id->u.id.qualifying_scope;
19356 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19357 if (tmpl
19358 && (DECL_CLASS_TEMPLATE_P (tmpl)
19359 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19361 id->u.id.unqualified_name = dguide_name (tmpl);
19362 id->u.id.sfk = sfk_deduction_guide;
19363 ctor_dtor_or_conv_p = 1;
19367 /* Check to see if the token indicates the start of a
19368 function-definition. */
19369 if (cp_parser_token_starts_function_definition_p (token))
19371 if (!function_definition_allowed_p)
19373 /* If a function-definition should not appear here, issue an
19374 error message. */
19375 cp_parser_error (parser,
19376 "a function-definition is not allowed here");
19377 return error_mark_node;
19380 location_t func_brace_location
19381 = cp_lexer_peek_token (parser->lexer)->location;
19383 /* Neither attributes nor an asm-specification are allowed
19384 on a function-definition. */
19385 if (asm_specification)
19386 error_at (asm_spec_start_token->location,
19387 "an asm-specification is not allowed "
19388 "on a function-definition");
19389 if (attributes)
19390 error_at (attributes_start_token->location,
19391 "attributes are not allowed "
19392 "on a function-definition");
19393 /* This is a function-definition. */
19394 *function_definition_p = true;
19396 /* Parse the function definition. */
19397 if (member_p)
19398 decl = cp_parser_save_member_function_body (parser,
19399 decl_specifiers,
19400 declarator,
19401 prefix_attributes);
19402 else
19403 decl =
19404 (cp_parser_function_definition_from_specifiers_and_declarator
19405 (parser, decl_specifiers, prefix_attributes, declarator));
19407 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19409 /* This is where the prologue starts... */
19410 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19411 = func_brace_location;
19414 return decl;
19417 else if (parser->fully_implicit_function_template_p)
19419 /* A non-template declaration involving a function parameter list
19420 containing an implicit template parameter will be made into a
19421 template. If the resulting declaration is not going to be an
19422 actual function then finish the template scope here to prevent it.
19423 An error message will be issued once we have a decl to talk about.
19425 FIXME probably we should do type deduction rather than create an
19426 implicit template, but the standard currently doesn't allow it. */
19427 bogus_implicit_tmpl = true;
19428 finish_fully_implicit_template (parser, NULL_TREE);
19431 /* [dcl.dcl]
19433 Only in function declarations for constructors, destructors, type
19434 conversions, and deduction guides can the decl-specifier-seq be omitted.
19436 We explicitly postpone this check past the point where we handle
19437 function-definitions because we tolerate function-definitions
19438 that are missing their return types in some modes. */
19439 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19441 cp_parser_error (parser,
19442 "expected constructor, destructor, or type conversion");
19443 return error_mark_node;
19446 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19447 if (token->type == CPP_EQ
19448 || token->type == CPP_OPEN_PAREN
19449 || token->type == CPP_OPEN_BRACE)
19451 is_initialized = SD_INITIALIZED;
19452 initialization_kind = token->type;
19453 if (maybe_range_for_decl)
19454 *maybe_range_for_decl = error_mark_node;
19455 tmp_init_loc = token->location;
19456 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19457 *init_loc = tmp_init_loc;
19459 if (token->type == CPP_EQ
19460 && function_declarator_p (declarator))
19462 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19463 if (t2->keyword == RID_DEFAULT)
19464 is_initialized = SD_DEFAULTED;
19465 else if (t2->keyword == RID_DELETE)
19466 is_initialized = SD_DELETED;
19469 else
19471 /* If the init-declarator isn't initialized and isn't followed by a
19472 `,' or `;', it's not a valid init-declarator. */
19473 if (token->type != CPP_COMMA
19474 && token->type != CPP_SEMICOLON)
19476 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19477 range_for_decl_p = true;
19478 else
19480 if (!maybe_range_for_decl)
19481 cp_parser_error (parser, "expected initializer");
19482 return error_mark_node;
19485 is_initialized = SD_UNINITIALIZED;
19486 initialization_kind = CPP_EOF;
19489 /* Because start_decl has side-effects, we should only call it if we
19490 know we're going ahead. By this point, we know that we cannot
19491 possibly be looking at any other construct. */
19492 cp_parser_commit_to_tentative_parse (parser);
19494 /* Enter the newly declared entry in the symbol table. If we're
19495 processing a declaration in a class-specifier, we wait until
19496 after processing the initializer. */
19497 if (!member_p)
19499 if (parser->in_unbraced_linkage_specification_p)
19500 decl_specifiers->storage_class = sc_extern;
19501 decl = start_decl (declarator, decl_specifiers,
19502 range_for_decl_p? SD_INITIALIZED : is_initialized,
19503 attributes, prefix_attributes, &pushed_scope);
19504 cp_finalize_omp_declare_simd (parser, decl);
19505 cp_finalize_oacc_routine (parser, decl, false);
19506 /* Adjust location of decl if declarator->id_loc is more appropriate:
19507 set, and decl wasn't merged with another decl, in which case its
19508 location would be different from input_location, and more accurate. */
19509 if (DECL_P (decl)
19510 && declarator->id_loc != UNKNOWN_LOCATION
19511 && DECL_SOURCE_LOCATION (decl) == input_location)
19512 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19514 else if (scope)
19515 /* Enter the SCOPE. That way unqualified names appearing in the
19516 initializer will be looked up in SCOPE. */
19517 pushed_scope = push_scope (scope);
19519 /* Perform deferred access control checks, now that we know in which
19520 SCOPE the declared entity resides. */
19521 if (!member_p && decl)
19523 tree saved_current_function_decl = NULL_TREE;
19525 /* If the entity being declared is a function, pretend that we
19526 are in its scope. If it is a `friend', it may have access to
19527 things that would not otherwise be accessible. */
19528 if (TREE_CODE (decl) == FUNCTION_DECL)
19530 saved_current_function_decl = current_function_decl;
19531 current_function_decl = decl;
19534 /* Perform access checks for template parameters. */
19535 cp_parser_perform_template_parameter_access_checks (checks);
19537 /* Perform the access control checks for the declarator and the
19538 decl-specifiers. */
19539 perform_deferred_access_checks (tf_warning_or_error);
19541 /* Restore the saved value. */
19542 if (TREE_CODE (decl) == FUNCTION_DECL)
19543 current_function_decl = saved_current_function_decl;
19546 /* Parse the initializer. */
19547 initializer = NULL_TREE;
19548 is_direct_init = false;
19549 is_non_constant_init = true;
19550 if (is_initialized)
19552 if (function_declarator_p (declarator))
19554 if (initialization_kind == CPP_EQ)
19555 initializer = cp_parser_pure_specifier (parser);
19556 else
19558 /* If the declaration was erroneous, we don't really
19559 know what the user intended, so just silently
19560 consume the initializer. */
19561 if (decl != error_mark_node)
19562 error_at (tmp_init_loc, "initializer provided for function");
19563 cp_parser_skip_to_closing_parenthesis (parser,
19564 /*recovering=*/true,
19565 /*or_comma=*/false,
19566 /*consume_paren=*/true);
19569 else
19571 /* We want to record the extra mangling scope for in-class
19572 initializers of class members and initializers of static data
19573 member templates. The former involves deferring
19574 parsing of the initializer until end of class as with default
19575 arguments. So right here we only handle the latter. */
19576 if (!member_p && processing_template_decl)
19577 start_lambda_scope (decl);
19578 initializer = cp_parser_initializer (parser,
19579 &is_direct_init,
19580 &is_non_constant_init);
19581 if (!member_p && processing_template_decl)
19582 finish_lambda_scope ();
19583 if (initializer == error_mark_node)
19584 cp_parser_skip_to_end_of_statement (parser);
19588 /* The old parser allows attributes to appear after a parenthesized
19589 initializer. Mark Mitchell proposed removing this functionality
19590 on the GCC mailing lists on 2002-08-13. This parser accepts the
19591 attributes -- but ignores them. */
19592 if (cp_parser_allow_gnu_extensions_p (parser)
19593 && initialization_kind == CPP_OPEN_PAREN)
19594 if (cp_parser_attributes_opt (parser))
19595 warning (OPT_Wattributes,
19596 "attributes after parenthesized initializer ignored");
19598 /* And now complain about a non-function implicit template. */
19599 if (bogus_implicit_tmpl && decl != error_mark_node)
19600 error_at (DECL_SOURCE_LOCATION (decl),
19601 "non-function %qD declared as implicit template", decl);
19603 /* For an in-class declaration, use `grokfield' to create the
19604 declaration. */
19605 if (member_p)
19607 if (pushed_scope)
19609 pop_scope (pushed_scope);
19610 pushed_scope = NULL_TREE;
19612 decl = grokfield (declarator, decl_specifiers,
19613 initializer, !is_non_constant_init,
19614 /*asmspec=*/NULL_TREE,
19615 chainon (attributes, prefix_attributes));
19616 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19617 cp_parser_save_default_args (parser, decl);
19618 cp_finalize_omp_declare_simd (parser, decl);
19619 cp_finalize_oacc_routine (parser, decl, false);
19622 /* Finish processing the declaration. But, skip member
19623 declarations. */
19624 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19626 cp_finish_decl (decl,
19627 initializer, !is_non_constant_init,
19628 asm_specification,
19629 /* If the initializer is in parentheses, then this is
19630 a direct-initialization, which means that an
19631 `explicit' constructor is OK. Otherwise, an
19632 `explicit' constructor cannot be used. */
19633 ((is_direct_init || !is_initialized)
19634 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19636 else if ((cxx_dialect != cxx98) && friend_p
19637 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19638 /* Core issue #226 (C++0x only): A default template-argument
19639 shall not be specified in a friend class template
19640 declaration. */
19641 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19642 /*is_partial=*/false, /*is_friend_decl=*/1);
19644 if (!friend_p && pushed_scope)
19645 pop_scope (pushed_scope);
19647 if (function_declarator_p (declarator)
19648 && parser->fully_implicit_function_template_p)
19650 if (member_p)
19651 decl = finish_fully_implicit_template (parser, decl);
19652 else
19653 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19656 if (auto_result && is_initialized && decl_specifiers->type
19657 && type_uses_auto (decl_specifiers->type))
19658 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19660 return decl;
19663 /* Parse a declarator.
19665 declarator:
19666 direct-declarator
19667 ptr-operator declarator
19669 abstract-declarator:
19670 ptr-operator abstract-declarator [opt]
19671 direct-abstract-declarator
19673 GNU Extensions:
19675 declarator:
19676 attributes [opt] direct-declarator
19677 attributes [opt] ptr-operator declarator
19679 abstract-declarator:
19680 attributes [opt] ptr-operator abstract-declarator [opt]
19681 attributes [opt] direct-abstract-declarator
19683 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19684 detect constructors, destructors, deduction guides, or conversion operators.
19685 It is set to -1 if the declarator is a name, and +1 if it is a
19686 function. Otherwise it is set to zero. Usually you just want to
19687 test for >0, but internally the negative value is used.
19689 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19690 a decl-specifier-seq unless it declares a constructor, destructor,
19691 or conversion. It might seem that we could check this condition in
19692 semantic analysis, rather than parsing, but that makes it difficult
19693 to handle something like `f()'. We want to notice that there are
19694 no decl-specifiers, and therefore realize that this is an
19695 expression, not a declaration.)
19697 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19698 the declarator is a direct-declarator of the form "(...)".
19700 MEMBER_P is true iff this declarator is a member-declarator.
19702 FRIEND_P is true iff this declarator is a friend. */
19704 static cp_declarator *
19705 cp_parser_declarator (cp_parser* parser,
19706 cp_parser_declarator_kind dcl_kind,
19707 int* ctor_dtor_or_conv_p,
19708 bool* parenthesized_p,
19709 bool member_p, bool friend_p)
19711 cp_declarator *declarator;
19712 enum tree_code code;
19713 cp_cv_quals cv_quals;
19714 tree class_type;
19715 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19717 /* Assume this is not a constructor, destructor, or type-conversion
19718 operator. */
19719 if (ctor_dtor_or_conv_p)
19720 *ctor_dtor_or_conv_p = 0;
19722 if (cp_parser_allow_gnu_extensions_p (parser))
19723 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19725 /* Check for the ptr-operator production. */
19726 cp_parser_parse_tentatively (parser);
19727 /* Parse the ptr-operator. */
19728 code = cp_parser_ptr_operator (parser,
19729 &class_type,
19730 &cv_quals,
19731 &std_attributes);
19733 /* If that worked, then we have a ptr-operator. */
19734 if (cp_parser_parse_definitely (parser))
19736 /* If a ptr-operator was found, then this declarator was not
19737 parenthesized. */
19738 if (parenthesized_p)
19739 *parenthesized_p = true;
19740 /* The dependent declarator is optional if we are parsing an
19741 abstract-declarator. */
19742 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19743 cp_parser_parse_tentatively (parser);
19745 /* Parse the dependent declarator. */
19746 declarator = cp_parser_declarator (parser, dcl_kind,
19747 /*ctor_dtor_or_conv_p=*/NULL,
19748 /*parenthesized_p=*/NULL,
19749 /*member_p=*/false,
19750 friend_p);
19752 /* If we are parsing an abstract-declarator, we must handle the
19753 case where the dependent declarator is absent. */
19754 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19755 && !cp_parser_parse_definitely (parser))
19756 declarator = NULL;
19758 declarator = cp_parser_make_indirect_declarator
19759 (code, class_type, cv_quals, declarator, std_attributes);
19761 /* Everything else is a direct-declarator. */
19762 else
19764 if (parenthesized_p)
19765 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19766 CPP_OPEN_PAREN);
19767 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19768 ctor_dtor_or_conv_p,
19769 member_p, friend_p);
19772 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19773 declarator->attributes = gnu_attributes;
19774 return declarator;
19777 /* Parse a direct-declarator or direct-abstract-declarator.
19779 direct-declarator:
19780 declarator-id
19781 direct-declarator ( parameter-declaration-clause )
19782 cv-qualifier-seq [opt]
19783 ref-qualifier [opt]
19784 exception-specification [opt]
19785 direct-declarator [ constant-expression [opt] ]
19786 ( declarator )
19788 direct-abstract-declarator:
19789 direct-abstract-declarator [opt]
19790 ( parameter-declaration-clause )
19791 cv-qualifier-seq [opt]
19792 ref-qualifier [opt]
19793 exception-specification [opt]
19794 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19795 ( abstract-declarator )
19797 Returns a representation of the declarator. DCL_KIND is
19798 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19799 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19800 we are parsing a direct-declarator. It is
19801 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19802 of ambiguity we prefer an abstract declarator, as per
19803 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19804 as for cp_parser_declarator. */
19806 static cp_declarator *
19807 cp_parser_direct_declarator (cp_parser* parser,
19808 cp_parser_declarator_kind dcl_kind,
19809 int* ctor_dtor_or_conv_p,
19810 bool member_p, bool friend_p)
19812 cp_token *token;
19813 cp_declarator *declarator = NULL;
19814 tree scope = NULL_TREE;
19815 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19816 bool saved_in_declarator_p = parser->in_declarator_p;
19817 bool first = true;
19818 tree pushed_scope = NULL_TREE;
19819 cp_token *open_paren = NULL, *close_paren = NULL;
19821 while (true)
19823 /* Peek at the next token. */
19824 token = cp_lexer_peek_token (parser->lexer);
19825 if (token->type == CPP_OPEN_PAREN)
19827 /* This is either a parameter-declaration-clause, or a
19828 parenthesized declarator. When we know we are parsing a
19829 named declarator, it must be a parenthesized declarator
19830 if FIRST is true. For instance, `(int)' is a
19831 parameter-declaration-clause, with an omitted
19832 direct-abstract-declarator. But `((*))', is a
19833 parenthesized abstract declarator. Finally, when T is a
19834 template parameter `(T)' is a
19835 parameter-declaration-clause, and not a parenthesized
19836 named declarator.
19838 We first try and parse a parameter-declaration-clause,
19839 and then try a nested declarator (if FIRST is true).
19841 It is not an error for it not to be a
19842 parameter-declaration-clause, even when FIRST is
19843 false. Consider,
19845 int i (int);
19846 int i (3);
19848 The first is the declaration of a function while the
19849 second is the definition of a variable, including its
19850 initializer.
19852 Having seen only the parenthesis, we cannot know which of
19853 these two alternatives should be selected. Even more
19854 complex are examples like:
19856 int i (int (a));
19857 int i (int (3));
19859 The former is a function-declaration; the latter is a
19860 variable initialization.
19862 Thus again, we try a parameter-declaration-clause, and if
19863 that fails, we back out and return. */
19865 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19867 tree params;
19868 bool is_declarator = false;
19870 open_paren = NULL;
19872 /* In a member-declarator, the only valid interpretation
19873 of a parenthesis is the start of a
19874 parameter-declaration-clause. (It is invalid to
19875 initialize a static data member with a parenthesized
19876 initializer; only the "=" form of initialization is
19877 permitted.) */
19878 if (!member_p)
19879 cp_parser_parse_tentatively (parser);
19881 /* Consume the `('. */
19882 matching_parens parens;
19883 parens.consume_open (parser);
19884 if (first)
19886 /* If this is going to be an abstract declarator, we're
19887 in a declarator and we can't have default args. */
19888 parser->default_arg_ok_p = false;
19889 parser->in_declarator_p = true;
19892 begin_scope (sk_function_parms, NULL_TREE);
19894 /* Parse the parameter-declaration-clause. */
19895 params = cp_parser_parameter_declaration_clause (parser);
19897 /* Consume the `)'. */
19898 parens.require_close (parser);
19900 /* If all went well, parse the cv-qualifier-seq,
19901 ref-qualifier and the exception-specification. */
19902 if (member_p || cp_parser_parse_definitely (parser))
19904 cp_cv_quals cv_quals;
19905 cp_virt_specifiers virt_specifiers;
19906 cp_ref_qualifier ref_qual;
19907 tree exception_specification;
19908 tree late_return;
19909 tree attrs;
19910 bool memfn = (member_p || (pushed_scope
19911 && CLASS_TYPE_P (pushed_scope)));
19913 is_declarator = true;
19915 if (ctor_dtor_or_conv_p)
19916 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19917 first = false;
19919 /* Parse the cv-qualifier-seq. */
19920 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19921 /* Parse the ref-qualifier. */
19922 ref_qual = cp_parser_ref_qualifier_opt (parser);
19923 /* Parse the tx-qualifier. */
19924 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19925 /* And the exception-specification. */
19926 exception_specification
19927 = cp_parser_exception_specification_opt (parser);
19929 attrs = cp_parser_std_attribute_spec_seq (parser);
19931 /* In here, we handle cases where attribute is used after
19932 the function declaration. For example:
19933 void func (int x) __attribute__((vector(..))); */
19934 tree gnu_attrs = NULL_TREE;
19935 tree requires_clause = NULL_TREE;
19936 late_return = (cp_parser_late_return_type_opt
19937 (parser, declarator, requires_clause,
19938 memfn ? cv_quals : -1));
19940 /* Parse the virt-specifier-seq. */
19941 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19943 /* Create the function-declarator. */
19944 declarator = make_call_declarator (declarator,
19945 params,
19946 cv_quals,
19947 virt_specifiers,
19948 ref_qual,
19949 tx_qual,
19950 exception_specification,
19951 late_return,
19952 requires_clause);
19953 declarator->std_attributes = attrs;
19954 declarator->attributes = gnu_attrs;
19955 /* Any subsequent parameter lists are to do with
19956 return type, so are not those of the declared
19957 function. */
19958 parser->default_arg_ok_p = false;
19961 /* Remove the function parms from scope. */
19962 pop_bindings_and_leave_scope ();
19964 if (is_declarator)
19965 /* Repeat the main loop. */
19966 continue;
19969 /* If this is the first, we can try a parenthesized
19970 declarator. */
19971 if (first)
19973 bool saved_in_type_id_in_expr_p;
19975 parser->default_arg_ok_p = saved_default_arg_ok_p;
19976 parser->in_declarator_p = saved_in_declarator_p;
19978 open_paren = token;
19979 /* Consume the `('. */
19980 matching_parens parens;
19981 parens.consume_open (parser);
19982 /* Parse the nested declarator. */
19983 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19984 parser->in_type_id_in_expr_p = true;
19985 declarator
19986 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19987 /*parenthesized_p=*/NULL,
19988 member_p, friend_p);
19989 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19990 first = false;
19991 /* Expect a `)'. */
19992 close_paren = cp_lexer_peek_token (parser->lexer);
19993 if (!parens.require_close (parser))
19994 declarator = cp_error_declarator;
19995 if (declarator == cp_error_declarator)
19996 break;
19998 goto handle_declarator;
20000 /* Otherwise, we must be done. */
20001 else
20002 break;
20004 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20005 && token->type == CPP_OPEN_SQUARE
20006 && !cp_next_tokens_can_be_attribute_p (parser))
20008 /* Parse an array-declarator. */
20009 tree bounds, attrs;
20011 if (ctor_dtor_or_conv_p)
20012 *ctor_dtor_or_conv_p = 0;
20014 open_paren = NULL;
20015 first = false;
20016 parser->default_arg_ok_p = false;
20017 parser->in_declarator_p = true;
20018 /* Consume the `['. */
20019 cp_lexer_consume_token (parser->lexer);
20020 /* Peek at the next token. */
20021 token = cp_lexer_peek_token (parser->lexer);
20022 /* If the next token is `]', then there is no
20023 constant-expression. */
20024 if (token->type != CPP_CLOSE_SQUARE)
20026 bool non_constant_p;
20027 bounds
20028 = cp_parser_constant_expression (parser,
20029 /*allow_non_constant=*/true,
20030 &non_constant_p);
20031 if (!non_constant_p)
20032 /* OK */;
20033 else if (error_operand_p (bounds))
20034 /* Already gave an error. */;
20035 else if (!parser->in_function_body
20036 || current_binding_level->kind == sk_function_parms)
20038 /* Normally, the array bound must be an integral constant
20039 expression. However, as an extension, we allow VLAs
20040 in function scopes as long as they aren't part of a
20041 parameter declaration. */
20042 cp_parser_error (parser,
20043 "array bound is not an integer constant");
20044 bounds = error_mark_node;
20046 else if (processing_template_decl
20047 && !type_dependent_expression_p (bounds))
20049 /* Remember this wasn't a constant-expression. */
20050 bounds = build_nop (TREE_TYPE (bounds), bounds);
20051 TREE_SIDE_EFFECTS (bounds) = 1;
20054 else
20055 bounds = NULL_TREE;
20056 /* Look for the closing `]'. */
20057 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20059 declarator = cp_error_declarator;
20060 break;
20063 attrs = cp_parser_std_attribute_spec_seq (parser);
20064 declarator = make_array_declarator (declarator, bounds);
20065 declarator->std_attributes = attrs;
20067 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20070 tree qualifying_scope;
20071 tree unqualified_name;
20072 tree attrs;
20073 special_function_kind sfk;
20074 bool abstract_ok;
20075 bool pack_expansion_p = false;
20076 cp_token *declarator_id_start_token;
20078 /* Parse a declarator-id */
20079 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20080 if (abstract_ok)
20082 cp_parser_parse_tentatively (parser);
20084 /* If we see an ellipsis, we should be looking at a
20085 parameter pack. */
20086 if (token->type == CPP_ELLIPSIS)
20088 /* Consume the `...' */
20089 cp_lexer_consume_token (parser->lexer);
20091 pack_expansion_p = true;
20095 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20096 unqualified_name
20097 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20098 qualifying_scope = parser->scope;
20099 if (abstract_ok)
20101 bool okay = false;
20103 if (!unqualified_name && pack_expansion_p)
20105 /* Check whether an error occurred. */
20106 okay = !cp_parser_error_occurred (parser);
20108 /* We already consumed the ellipsis to mark a
20109 parameter pack, but we have no way to report it,
20110 so abort the tentative parse. We will be exiting
20111 immediately anyway. */
20112 cp_parser_abort_tentative_parse (parser);
20114 else
20115 okay = cp_parser_parse_definitely (parser);
20117 if (!okay)
20118 unqualified_name = error_mark_node;
20119 else if (unqualified_name
20120 && (qualifying_scope
20121 || (!identifier_p (unqualified_name))))
20123 cp_parser_error (parser, "expected unqualified-id");
20124 unqualified_name = error_mark_node;
20128 if (!unqualified_name)
20129 return NULL;
20130 if (unqualified_name == error_mark_node)
20132 declarator = cp_error_declarator;
20133 pack_expansion_p = false;
20134 declarator->parameter_pack_p = false;
20135 break;
20138 attrs = cp_parser_std_attribute_spec_seq (parser);
20140 if (qualifying_scope && at_namespace_scope_p ()
20141 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20143 /* In the declaration of a member of a template class
20144 outside of the class itself, the SCOPE will sometimes
20145 be a TYPENAME_TYPE. For example, given:
20147 template <typename T>
20148 int S<T>::R::i = 3;
20150 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20151 this context, we must resolve S<T>::R to an ordinary
20152 type, rather than a typename type.
20154 The reason we normally avoid resolving TYPENAME_TYPEs
20155 is that a specialization of `S' might render
20156 `S<T>::R' not a type. However, if `S' is
20157 specialized, then this `i' will not be used, so there
20158 is no harm in resolving the types here. */
20159 tree type;
20161 /* Resolve the TYPENAME_TYPE. */
20162 type = resolve_typename_type (qualifying_scope,
20163 /*only_current_p=*/false);
20164 /* If that failed, the declarator is invalid. */
20165 if (TREE_CODE (type) == TYPENAME_TYPE)
20167 if (typedef_variant_p (type))
20168 error_at (declarator_id_start_token->location,
20169 "cannot define member of dependent typedef "
20170 "%qT", type);
20171 else
20172 error_at (declarator_id_start_token->location,
20173 "%<%T::%E%> is not a type",
20174 TYPE_CONTEXT (qualifying_scope),
20175 TYPE_IDENTIFIER (qualifying_scope));
20177 qualifying_scope = type;
20180 sfk = sfk_none;
20182 if (unqualified_name)
20184 tree class_type;
20186 if (qualifying_scope
20187 && CLASS_TYPE_P (qualifying_scope))
20188 class_type = qualifying_scope;
20189 else
20190 class_type = current_class_type;
20192 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20194 tree name_type = TREE_TYPE (unqualified_name);
20196 if (!class_type || !same_type_p (name_type, class_type))
20198 /* We do not attempt to print the declarator
20199 here because we do not have enough
20200 information about its original syntactic
20201 form. */
20202 cp_parser_error (parser, "invalid declarator");
20203 declarator = cp_error_declarator;
20204 break;
20206 else if (qualifying_scope
20207 && CLASSTYPE_USE_TEMPLATE (name_type))
20209 error_at (declarator_id_start_token->location,
20210 "invalid use of constructor as a template");
20211 inform (declarator_id_start_token->location,
20212 "use %<%T::%D%> instead of %<%T::%D%> to "
20213 "name the constructor in a qualified name",
20214 class_type,
20215 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20216 class_type, name_type);
20217 declarator = cp_error_declarator;
20218 break;
20220 unqualified_name = constructor_name (class_type);
20223 if (class_type)
20225 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20226 sfk = sfk_destructor;
20227 else if (identifier_p (unqualified_name)
20228 && IDENTIFIER_CONV_OP_P (unqualified_name))
20229 sfk = sfk_conversion;
20230 else if (/* There's no way to declare a constructor
20231 for an unnamed type, even if the type
20232 got a name for linkage purposes. */
20233 !TYPE_WAS_UNNAMED (class_type)
20234 /* Handle correctly (c++/19200):
20236 struct S {
20237 struct T{};
20238 friend void S(T);
20241 and also:
20243 namespace N {
20244 void S();
20247 struct S {
20248 friend void N::S();
20249 }; */
20250 && (!friend_p || class_type == qualifying_scope)
20251 && constructor_name_p (unqualified_name,
20252 class_type))
20253 sfk = sfk_constructor;
20254 else if (is_overloaded_fn (unqualified_name)
20255 && DECL_CONSTRUCTOR_P (get_first_fn
20256 (unqualified_name)))
20257 sfk = sfk_constructor;
20259 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20260 *ctor_dtor_or_conv_p = -1;
20263 declarator = make_id_declarator (qualifying_scope,
20264 unqualified_name,
20265 sfk);
20266 declarator->std_attributes = attrs;
20267 declarator->id_loc = token->location;
20268 declarator->parameter_pack_p = pack_expansion_p;
20270 if (pack_expansion_p)
20271 maybe_warn_variadic_templates ();
20274 handle_declarator:;
20275 scope = get_scope_of_declarator (declarator);
20276 if (scope)
20278 /* Any names that appear after the declarator-id for a
20279 member are looked up in the containing scope. */
20280 if (at_function_scope_p ())
20282 /* But declarations with qualified-ids can't appear in a
20283 function. */
20284 cp_parser_error (parser, "qualified-id in declaration");
20285 declarator = cp_error_declarator;
20286 break;
20288 pushed_scope = push_scope (scope);
20290 parser->in_declarator_p = true;
20291 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20292 || (declarator && declarator->kind == cdk_id))
20293 /* Default args are only allowed on function
20294 declarations. */
20295 parser->default_arg_ok_p = saved_default_arg_ok_p;
20296 else
20297 parser->default_arg_ok_p = false;
20299 first = false;
20301 /* We're done. */
20302 else
20303 break;
20306 /* For an abstract declarator, we might wind up with nothing at this
20307 point. That's an error; the declarator is not optional. */
20308 if (!declarator)
20309 cp_parser_error (parser, "expected declarator");
20310 else if (open_paren)
20312 /* Record overly parenthesized declarator so we can give a
20313 diagnostic about confusing decl/expr disambiguation. */
20314 if (declarator->kind == cdk_array)
20316 /* If the open and close parens are on different lines, this
20317 is probably a formatting thing, so ignore. */
20318 expanded_location open = expand_location (open_paren->location);
20319 expanded_location close = expand_location (close_paren->location);
20320 if (open.line != close.line || open.file != close.file)
20321 open_paren = NULL;
20323 if (open_paren)
20324 declarator->parenthesized = open_paren->location;
20327 /* If we entered a scope, we must exit it now. */
20328 if (pushed_scope)
20329 pop_scope (pushed_scope);
20331 parser->default_arg_ok_p = saved_default_arg_ok_p;
20332 parser->in_declarator_p = saved_in_declarator_p;
20334 return declarator;
20337 /* Parse a ptr-operator.
20339 ptr-operator:
20340 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20341 * cv-qualifier-seq [opt]
20343 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20344 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20346 GNU Extension:
20348 ptr-operator:
20349 & cv-qualifier-seq [opt]
20351 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20352 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20353 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20354 filled in with the TYPE containing the member. *CV_QUALS is
20355 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20356 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20357 Note that the tree codes returned by this function have nothing
20358 to do with the types of trees that will be eventually be created
20359 to represent the pointer or reference type being parsed. They are
20360 just constants with suggestive names. */
20361 static enum tree_code
20362 cp_parser_ptr_operator (cp_parser* parser,
20363 tree* type,
20364 cp_cv_quals *cv_quals,
20365 tree *attributes)
20367 enum tree_code code = ERROR_MARK;
20368 cp_token *token;
20369 tree attrs = NULL_TREE;
20371 /* Assume that it's not a pointer-to-member. */
20372 *type = NULL_TREE;
20373 /* And that there are no cv-qualifiers. */
20374 *cv_quals = TYPE_UNQUALIFIED;
20376 /* Peek at the next token. */
20377 token = cp_lexer_peek_token (parser->lexer);
20379 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20380 if (token->type == CPP_MULT)
20381 code = INDIRECT_REF;
20382 else if (token->type == CPP_AND)
20383 code = ADDR_EXPR;
20384 else if ((cxx_dialect != cxx98) &&
20385 token->type == CPP_AND_AND) /* C++0x only */
20386 code = NON_LVALUE_EXPR;
20388 if (code != ERROR_MARK)
20390 /* Consume the `*', `&' or `&&'. */
20391 cp_lexer_consume_token (parser->lexer);
20393 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20394 `&', if we are allowing GNU extensions. (The only qualifier
20395 that can legally appear after `&' is `restrict', but that is
20396 enforced during semantic analysis. */
20397 if (code == INDIRECT_REF
20398 || cp_parser_allow_gnu_extensions_p (parser))
20399 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20401 attrs = cp_parser_std_attribute_spec_seq (parser);
20402 if (attributes != NULL)
20403 *attributes = attrs;
20405 else
20407 /* Try the pointer-to-member case. */
20408 cp_parser_parse_tentatively (parser);
20409 /* Look for the optional `::' operator. */
20410 cp_parser_global_scope_opt (parser,
20411 /*current_scope_valid_p=*/false);
20412 /* Look for the nested-name specifier. */
20413 token = cp_lexer_peek_token (parser->lexer);
20414 cp_parser_nested_name_specifier (parser,
20415 /*typename_keyword_p=*/false,
20416 /*check_dependency_p=*/true,
20417 /*type_p=*/false,
20418 /*is_declaration=*/false);
20419 /* If we found it, and the next token is a `*', then we are
20420 indeed looking at a pointer-to-member operator. */
20421 if (!cp_parser_error_occurred (parser)
20422 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20424 /* Indicate that the `*' operator was used. */
20425 code = INDIRECT_REF;
20427 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20428 error_at (token->location, "%qD is a namespace", parser->scope);
20429 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20430 error_at (token->location, "cannot form pointer to member of "
20431 "non-class %q#T", parser->scope);
20432 else
20434 /* The type of which the member is a member is given by the
20435 current SCOPE. */
20436 *type = parser->scope;
20437 /* The next name will not be qualified. */
20438 parser->scope = NULL_TREE;
20439 parser->qualifying_scope = NULL_TREE;
20440 parser->object_scope = NULL_TREE;
20441 /* Look for optional c++11 attributes. */
20442 attrs = cp_parser_std_attribute_spec_seq (parser);
20443 if (attributes != NULL)
20444 *attributes = attrs;
20445 /* Look for the optional cv-qualifier-seq. */
20446 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20449 /* If that didn't work we don't have a ptr-operator. */
20450 if (!cp_parser_parse_definitely (parser))
20451 cp_parser_error (parser, "expected ptr-operator");
20454 return code;
20457 /* Parse an (optional) cv-qualifier-seq.
20459 cv-qualifier-seq:
20460 cv-qualifier cv-qualifier-seq [opt]
20462 cv-qualifier:
20463 const
20464 volatile
20466 GNU Extension:
20468 cv-qualifier:
20469 __restrict__
20471 Returns a bitmask representing the cv-qualifiers. */
20473 static cp_cv_quals
20474 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20476 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20478 while (true)
20480 cp_token *token;
20481 cp_cv_quals cv_qualifier;
20483 /* Peek at the next token. */
20484 token = cp_lexer_peek_token (parser->lexer);
20485 /* See if it's a cv-qualifier. */
20486 switch (token->keyword)
20488 case RID_CONST:
20489 cv_qualifier = TYPE_QUAL_CONST;
20490 break;
20492 case RID_VOLATILE:
20493 cv_qualifier = TYPE_QUAL_VOLATILE;
20494 break;
20496 case RID_RESTRICT:
20497 cv_qualifier = TYPE_QUAL_RESTRICT;
20498 break;
20500 default:
20501 cv_qualifier = TYPE_UNQUALIFIED;
20502 break;
20505 if (!cv_qualifier)
20506 break;
20508 if (cv_quals & cv_qualifier)
20510 gcc_rich_location richloc (token->location);
20511 richloc.add_fixit_remove ();
20512 error_at (&richloc, "duplicate cv-qualifier");
20513 cp_lexer_purge_token (parser->lexer);
20515 else
20517 cp_lexer_consume_token (parser->lexer);
20518 cv_quals |= cv_qualifier;
20522 return cv_quals;
20525 /* Parse an (optional) ref-qualifier
20527 ref-qualifier:
20531 Returns cp_ref_qualifier representing ref-qualifier. */
20533 static cp_ref_qualifier
20534 cp_parser_ref_qualifier_opt (cp_parser* parser)
20536 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20538 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20539 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20540 return ref_qual;
20542 while (true)
20544 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20545 cp_token *token = cp_lexer_peek_token (parser->lexer);
20547 switch (token->type)
20549 case CPP_AND:
20550 curr_ref_qual = REF_QUAL_LVALUE;
20551 break;
20553 case CPP_AND_AND:
20554 curr_ref_qual = REF_QUAL_RVALUE;
20555 break;
20557 default:
20558 curr_ref_qual = REF_QUAL_NONE;
20559 break;
20562 if (!curr_ref_qual)
20563 break;
20564 else if (ref_qual)
20566 error_at (token->location, "multiple ref-qualifiers");
20567 cp_lexer_purge_token (parser->lexer);
20569 else
20571 ref_qual = curr_ref_qual;
20572 cp_lexer_consume_token (parser->lexer);
20576 return ref_qual;
20579 /* Parse an optional tx-qualifier.
20581 tx-qualifier:
20582 transaction_safe
20583 transaction_safe_dynamic */
20585 static tree
20586 cp_parser_tx_qualifier_opt (cp_parser *parser)
20588 cp_token *token = cp_lexer_peek_token (parser->lexer);
20589 if (token->type == CPP_NAME)
20591 tree name = token->u.value;
20592 const char *p = IDENTIFIER_POINTER (name);
20593 const int len = strlen ("transaction_safe");
20594 if (!strncmp (p, "transaction_safe", len))
20596 p += len;
20597 if (*p == '\0'
20598 || !strcmp (p, "_dynamic"))
20600 cp_lexer_consume_token (parser->lexer);
20601 if (!flag_tm)
20603 error ("%qE requires %<-fgnu-tm%>", name);
20604 return NULL_TREE;
20606 else
20607 return name;
20611 return NULL_TREE;
20614 /* Parse an (optional) virt-specifier-seq.
20616 virt-specifier-seq:
20617 virt-specifier virt-specifier-seq [opt]
20619 virt-specifier:
20620 override
20621 final
20623 Returns a bitmask representing the virt-specifiers. */
20625 static cp_virt_specifiers
20626 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20628 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20630 while (true)
20632 cp_token *token;
20633 cp_virt_specifiers virt_specifier;
20635 /* Peek at the next token. */
20636 token = cp_lexer_peek_token (parser->lexer);
20637 /* See if it's a virt-specifier-qualifier. */
20638 if (token->type != CPP_NAME)
20639 break;
20640 if (id_equal (token->u.value, "override"))
20642 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20643 virt_specifier = VIRT_SPEC_OVERRIDE;
20645 else if (id_equal (token->u.value, "final"))
20647 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20648 virt_specifier = VIRT_SPEC_FINAL;
20650 else if (id_equal (token->u.value, "__final"))
20652 virt_specifier = VIRT_SPEC_FINAL;
20654 else
20655 break;
20657 if (virt_specifiers & virt_specifier)
20659 gcc_rich_location richloc (token->location);
20660 richloc.add_fixit_remove ();
20661 error_at (&richloc, "duplicate virt-specifier");
20662 cp_lexer_purge_token (parser->lexer);
20664 else
20666 cp_lexer_consume_token (parser->lexer);
20667 virt_specifiers |= virt_specifier;
20670 return virt_specifiers;
20673 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20674 is in scope even though it isn't real. */
20676 void
20677 inject_this_parameter (tree ctype, cp_cv_quals quals)
20679 tree this_parm;
20681 if (current_class_ptr)
20683 /* We don't clear this between NSDMIs. Is it already what we want? */
20684 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20685 if (DECL_P (current_class_ptr)
20686 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20687 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20688 && cp_type_quals (type) == quals)
20689 return;
20692 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20693 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20694 current_class_ptr = NULL_TREE;
20695 current_class_ref
20696 = cp_build_fold_indirect_ref (this_parm);
20697 current_class_ptr = this_parm;
20700 /* Return true iff our current scope is a non-static data member
20701 initializer. */
20703 bool
20704 parsing_nsdmi (void)
20706 /* We recognize NSDMI context by the context-less 'this' pointer set up
20707 by the function above. */
20708 if (current_class_ptr
20709 && TREE_CODE (current_class_ptr) == PARM_DECL
20710 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20711 return true;
20712 return false;
20715 /* Parse a late-specified return type, if any. This is not a separate
20716 non-terminal, but part of a function declarator, which looks like
20718 -> trailing-type-specifier-seq abstract-declarator(opt)
20720 Returns the type indicated by the type-id.
20722 In addition to this, parse any queued up #pragma omp declare simd
20723 clauses, and #pragma acc routine clauses.
20725 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20726 function. */
20728 static tree
20729 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20730 tree& requires_clause, cp_cv_quals quals)
20732 cp_token *token;
20733 tree type = NULL_TREE;
20734 bool declare_simd_p = (parser->omp_declare_simd
20735 && declarator
20736 && declarator->kind == cdk_id);
20738 bool oacc_routine_p = (parser->oacc_routine
20739 && declarator
20740 && declarator->kind == cdk_id);
20742 /* Peek at the next token. */
20743 token = cp_lexer_peek_token (parser->lexer);
20744 /* A late-specified return type is indicated by an initial '->'. */
20745 if (token->type != CPP_DEREF
20746 && token->keyword != RID_REQUIRES
20747 && !(token->type == CPP_NAME
20748 && token->u.value == ridpointers[RID_REQUIRES])
20749 && !(declare_simd_p || oacc_routine_p))
20750 return NULL_TREE;
20752 tree save_ccp = current_class_ptr;
20753 tree save_ccr = current_class_ref;
20754 if (quals >= 0)
20756 /* DR 1207: 'this' is in scope in the trailing return type. */
20757 inject_this_parameter (current_class_type, quals);
20760 if (token->type == CPP_DEREF)
20762 /* Consume the ->. */
20763 cp_lexer_consume_token (parser->lexer);
20765 type = cp_parser_trailing_type_id (parser);
20768 /* Function declarations may be followed by a trailing
20769 requires-clause. */
20770 requires_clause = cp_parser_requires_clause_opt (parser);
20772 if (declare_simd_p)
20773 declarator->attributes
20774 = cp_parser_late_parsing_omp_declare_simd (parser,
20775 declarator->attributes);
20776 if (oacc_routine_p)
20777 declarator->attributes
20778 = cp_parser_late_parsing_oacc_routine (parser,
20779 declarator->attributes);
20781 if (quals >= 0)
20783 current_class_ptr = save_ccp;
20784 current_class_ref = save_ccr;
20787 return type;
20790 /* Parse a declarator-id.
20792 declarator-id:
20793 id-expression
20794 :: [opt] nested-name-specifier [opt] type-name
20796 In the `id-expression' case, the value returned is as for
20797 cp_parser_id_expression if the id-expression was an unqualified-id.
20798 If the id-expression was a qualified-id, then a SCOPE_REF is
20799 returned. The first operand is the scope (either a NAMESPACE_DECL
20800 or TREE_TYPE), but the second is still just a representation of an
20801 unqualified-id. */
20803 static tree
20804 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20806 tree id;
20807 /* The expression must be an id-expression. Assume that qualified
20808 names are the names of types so that:
20810 template <class T>
20811 int S<T>::R::i = 3;
20813 will work; we must treat `S<T>::R' as the name of a type.
20814 Similarly, assume that qualified names are templates, where
20815 required, so that:
20817 template <class T>
20818 int S<T>::R<T>::i = 3;
20820 will work, too. */
20821 id = cp_parser_id_expression (parser,
20822 /*template_keyword_p=*/false,
20823 /*check_dependency_p=*/false,
20824 /*template_p=*/NULL,
20825 /*declarator_p=*/true,
20826 optional_p);
20827 if (id && BASELINK_P (id))
20828 id = BASELINK_FUNCTIONS (id);
20829 return id;
20832 /* Parse a type-id.
20834 type-id:
20835 type-specifier-seq abstract-declarator [opt]
20837 Returns the TYPE specified. */
20839 static tree
20840 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20841 bool is_trailing_return)
20843 cp_decl_specifier_seq type_specifier_seq;
20844 cp_declarator *abstract_declarator;
20846 /* Parse the type-specifier-seq. */
20847 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20848 is_trailing_return,
20849 &type_specifier_seq);
20850 if (is_template_arg && type_specifier_seq.type
20851 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20852 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20853 /* A bare template name as a template argument is a template template
20854 argument, not a placeholder, so fail parsing it as a type argument. */
20856 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20857 cp_parser_simulate_error (parser);
20858 return error_mark_node;
20860 if (type_specifier_seq.type == error_mark_node)
20861 return error_mark_node;
20863 /* There might or might not be an abstract declarator. */
20864 cp_parser_parse_tentatively (parser);
20865 /* Look for the declarator. */
20866 abstract_declarator
20867 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20868 /*parenthesized_p=*/NULL,
20869 /*member_p=*/false,
20870 /*friend_p=*/false);
20871 /* Check to see if there really was a declarator. */
20872 if (!cp_parser_parse_definitely (parser))
20873 abstract_declarator = NULL;
20875 if (type_specifier_seq.type
20876 /* The concepts TS allows 'auto' as a type-id. */
20877 && (!flag_concepts || parser->in_type_id_in_expr_p)
20878 /* None of the valid uses of 'auto' in C++14 involve the type-id
20879 nonterminal, but it is valid in a trailing-return-type. */
20880 && !(cxx_dialect >= cxx14 && is_trailing_return))
20881 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20883 /* A type-id with type 'auto' is only ok if the abstract declarator
20884 is a function declarator with a late-specified return type.
20886 A type-id with 'auto' is also valid in a trailing-return-type
20887 in a compound-requirement. */
20888 if (abstract_declarator
20889 && abstract_declarator->kind == cdk_function
20890 && abstract_declarator->u.function.late_return_type)
20891 /* OK */;
20892 else if (parser->in_result_type_constraint_p)
20893 /* OK */;
20894 else
20896 location_t loc = type_specifier_seq.locations[ds_type_spec];
20897 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20899 error_at (loc, "missing template arguments after %qT",
20900 auto_node);
20901 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20902 tmpl);
20904 else
20905 error_at (loc, "invalid use of %qT", auto_node);
20906 return error_mark_node;
20910 return groktypename (&type_specifier_seq, abstract_declarator,
20911 is_template_arg);
20914 static tree
20915 cp_parser_type_id (cp_parser *parser)
20917 return cp_parser_type_id_1 (parser, false, false);
20920 static tree
20921 cp_parser_template_type_arg (cp_parser *parser)
20923 tree r;
20924 const char *saved_message = parser->type_definition_forbidden_message;
20925 parser->type_definition_forbidden_message
20926 = G_("types may not be defined in template arguments");
20927 r = cp_parser_type_id_1 (parser, true, false);
20928 parser->type_definition_forbidden_message = saved_message;
20929 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20931 error ("invalid use of %<auto%> in template argument");
20932 r = error_mark_node;
20934 return r;
20937 static tree
20938 cp_parser_trailing_type_id (cp_parser *parser)
20940 return cp_parser_type_id_1 (parser, false, true);
20943 /* Parse a type-specifier-seq.
20945 type-specifier-seq:
20946 type-specifier type-specifier-seq [opt]
20948 GNU extension:
20950 type-specifier-seq:
20951 attributes type-specifier-seq [opt]
20953 If IS_DECLARATION is true, we are at the start of a "condition" or
20954 exception-declaration, so we might be followed by a declarator-id.
20956 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20957 i.e. we've just seen "->".
20959 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20961 static void
20962 cp_parser_type_specifier_seq (cp_parser* parser,
20963 bool is_declaration,
20964 bool is_trailing_return,
20965 cp_decl_specifier_seq *type_specifier_seq)
20967 bool seen_type_specifier = false;
20968 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20969 cp_token *start_token = NULL;
20971 /* Clear the TYPE_SPECIFIER_SEQ. */
20972 clear_decl_specs (type_specifier_seq);
20974 /* In the context of a trailing return type, enum E { } is an
20975 elaborated-type-specifier followed by a function-body, not an
20976 enum-specifier. */
20977 if (is_trailing_return)
20978 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20980 /* Parse the type-specifiers and attributes. */
20981 while (true)
20983 tree type_specifier;
20984 bool is_cv_qualifier;
20986 /* Check for attributes first. */
20987 if (cp_next_tokens_can_be_attribute_p (parser))
20989 type_specifier_seq->attributes =
20990 chainon (type_specifier_seq->attributes,
20991 cp_parser_attributes_opt (parser));
20992 continue;
20995 /* record the token of the beginning of the type specifier seq,
20996 for error reporting purposes*/
20997 if (!start_token)
20998 start_token = cp_lexer_peek_token (parser->lexer);
21000 /* Look for the type-specifier. */
21001 type_specifier = cp_parser_type_specifier (parser,
21002 flags,
21003 type_specifier_seq,
21004 /*is_declaration=*/false,
21005 NULL,
21006 &is_cv_qualifier);
21007 if (!type_specifier)
21009 /* If the first type-specifier could not be found, this is not a
21010 type-specifier-seq at all. */
21011 if (!seen_type_specifier)
21013 /* Set in_declarator_p to avoid skipping to the semicolon. */
21014 int in_decl = parser->in_declarator_p;
21015 parser->in_declarator_p = true;
21017 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21018 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21019 cp_parser_error (parser, "expected type-specifier");
21021 parser->in_declarator_p = in_decl;
21023 type_specifier_seq->type = error_mark_node;
21024 return;
21026 /* If subsequent type-specifiers could not be found, the
21027 type-specifier-seq is complete. */
21028 break;
21031 seen_type_specifier = true;
21032 /* The standard says that a condition can be:
21034 type-specifier-seq declarator = assignment-expression
21036 However, given:
21038 struct S {};
21039 if (int S = ...)
21041 we should treat the "S" as a declarator, not as a
21042 type-specifier. The standard doesn't say that explicitly for
21043 type-specifier-seq, but it does say that for
21044 decl-specifier-seq in an ordinary declaration. Perhaps it
21045 would be clearer just to allow a decl-specifier-seq here, and
21046 then add a semantic restriction that if any decl-specifiers
21047 that are not type-specifiers appear, the program is invalid. */
21048 if (is_declaration && !is_cv_qualifier)
21049 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21053 /* Return whether the function currently being declared has an associated
21054 template parameter list. */
21056 static bool
21057 function_being_declared_is_template_p (cp_parser* parser)
21059 if (!current_template_parms || processing_template_parmlist)
21060 return false;
21062 if (parser->implicit_template_scope)
21063 return true;
21065 if (at_class_scope_p ()
21066 && TYPE_BEING_DEFINED (current_class_type))
21067 return parser->num_template_parameter_lists != 0;
21069 return ((int) parser->num_template_parameter_lists > template_class_depth
21070 (current_class_type));
21073 /* Parse a parameter-declaration-clause.
21075 parameter-declaration-clause:
21076 parameter-declaration-list [opt] ... [opt]
21077 parameter-declaration-list , ...
21079 Returns a representation for the parameter declarations. A return
21080 value of NULL indicates a parameter-declaration-clause consisting
21081 only of an ellipsis. */
21083 static tree
21084 cp_parser_parameter_declaration_clause (cp_parser* parser)
21086 tree parameters;
21087 cp_token *token;
21088 bool ellipsis_p;
21089 bool is_error;
21091 struct cleanup {
21092 cp_parser* parser;
21093 int auto_is_implicit_function_template_parm_p;
21094 ~cleanup() {
21095 parser->auto_is_implicit_function_template_parm_p
21096 = auto_is_implicit_function_template_parm_p;
21098 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21100 (void) cleanup;
21102 if (!processing_specialization
21103 && !processing_template_parmlist
21104 && !processing_explicit_instantiation)
21105 if (!current_function_decl
21106 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21107 parser->auto_is_implicit_function_template_parm_p = true;
21109 /* Peek at the next token. */
21110 token = cp_lexer_peek_token (parser->lexer);
21111 /* Check for trivial parameter-declaration-clauses. */
21112 if (token->type == CPP_ELLIPSIS)
21114 /* Consume the `...' token. */
21115 cp_lexer_consume_token (parser->lexer);
21116 return NULL_TREE;
21118 else if (token->type == CPP_CLOSE_PAREN)
21119 /* There are no parameters. */
21121 #ifndef NO_IMPLICIT_EXTERN_C
21122 if (in_system_header_at (input_location)
21123 && current_class_type == NULL
21124 && current_lang_name == lang_name_c)
21125 return NULL_TREE;
21126 else
21127 #endif
21128 return void_list_node;
21130 /* Check for `(void)', too, which is a special case. */
21131 else if (token->keyword == RID_VOID
21132 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21133 == CPP_CLOSE_PAREN))
21135 /* Consume the `void' token. */
21136 cp_lexer_consume_token (parser->lexer);
21137 /* There are no parameters. */
21138 return void_list_node;
21141 /* Parse the parameter-declaration-list. */
21142 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21143 /* If a parse error occurred while parsing the
21144 parameter-declaration-list, then the entire
21145 parameter-declaration-clause is erroneous. */
21146 if (is_error)
21147 return NULL;
21149 /* Peek at the next token. */
21150 token = cp_lexer_peek_token (parser->lexer);
21151 /* If it's a `,', the clause should terminate with an ellipsis. */
21152 if (token->type == CPP_COMMA)
21154 /* Consume the `,'. */
21155 cp_lexer_consume_token (parser->lexer);
21156 /* Expect an ellipsis. */
21157 ellipsis_p
21158 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21160 /* It might also be `...' if the optional trailing `,' was
21161 omitted. */
21162 else if (token->type == CPP_ELLIPSIS)
21164 /* Consume the `...' token. */
21165 cp_lexer_consume_token (parser->lexer);
21166 /* And remember that we saw it. */
21167 ellipsis_p = true;
21169 else
21170 ellipsis_p = false;
21172 /* Finish the parameter list. */
21173 if (!ellipsis_p)
21174 parameters = chainon (parameters, void_list_node);
21176 return parameters;
21179 /* Parse a parameter-declaration-list.
21181 parameter-declaration-list:
21182 parameter-declaration
21183 parameter-declaration-list , parameter-declaration
21185 Returns a representation of the parameter-declaration-list, as for
21186 cp_parser_parameter_declaration_clause. However, the
21187 `void_list_node' is never appended to the list. Upon return,
21188 *IS_ERROR will be true iff an error occurred. */
21190 static tree
21191 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21193 tree parameters = NULL_TREE;
21194 tree *tail = &parameters;
21195 bool saved_in_unbraced_linkage_specification_p;
21196 int index = 0;
21198 /* Assume all will go well. */
21199 *is_error = false;
21200 /* The special considerations that apply to a function within an
21201 unbraced linkage specifications do not apply to the parameters
21202 to the function. */
21203 saved_in_unbraced_linkage_specification_p
21204 = parser->in_unbraced_linkage_specification_p;
21205 parser->in_unbraced_linkage_specification_p = false;
21207 /* Look for more parameters. */
21208 while (true)
21210 cp_parameter_declarator *parameter;
21211 tree decl = error_mark_node;
21212 bool parenthesized_p = false;
21213 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21214 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21215 (current_template_parms)) : 0);
21217 /* Parse the parameter. */
21218 parameter
21219 = cp_parser_parameter_declaration (parser,
21220 /*template_parm_p=*/false,
21221 &parenthesized_p);
21223 /* We don't know yet if the enclosing context is deprecated, so wait
21224 and warn in grokparms if appropriate. */
21225 deprecated_state = DEPRECATED_SUPPRESS;
21227 if (parameter)
21229 /* If a function parameter pack was specified and an implicit template
21230 parameter was introduced during cp_parser_parameter_declaration,
21231 change any implicit parameters introduced into packs. */
21232 if (parser->implicit_template_parms
21233 && parameter->declarator
21234 && parameter->declarator->parameter_pack_p)
21236 int latest_template_parm_idx = TREE_VEC_LENGTH
21237 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21239 if (latest_template_parm_idx != template_parm_idx)
21240 parameter->decl_specifiers.type = convert_generic_types_to_packs
21241 (parameter->decl_specifiers.type,
21242 template_parm_idx, latest_template_parm_idx);
21245 decl = grokdeclarator (parameter->declarator,
21246 &parameter->decl_specifiers,
21247 PARM,
21248 parameter->default_argument != NULL_TREE,
21249 &parameter->decl_specifiers.attributes);
21250 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21251 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21254 deprecated_state = DEPRECATED_NORMAL;
21256 /* If a parse error occurred parsing the parameter declaration,
21257 then the entire parameter-declaration-list is erroneous. */
21258 if (decl == error_mark_node)
21260 *is_error = true;
21261 parameters = error_mark_node;
21262 break;
21265 if (parameter->decl_specifiers.attributes)
21266 cplus_decl_attributes (&decl,
21267 parameter->decl_specifiers.attributes,
21269 if (DECL_NAME (decl))
21270 decl = pushdecl (decl);
21272 if (decl != error_mark_node)
21274 retrofit_lang_decl (decl);
21275 DECL_PARM_INDEX (decl) = ++index;
21276 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21279 /* Add the new parameter to the list. */
21280 *tail = build_tree_list (parameter->default_argument, decl);
21281 tail = &TREE_CHAIN (*tail);
21283 /* Peek at the next token. */
21284 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21285 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21286 /* These are for Objective-C++ */
21287 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21288 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21289 /* The parameter-declaration-list is complete. */
21290 break;
21291 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21293 cp_token *token;
21295 /* Peek at the next token. */
21296 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21297 /* If it's an ellipsis, then the list is complete. */
21298 if (token->type == CPP_ELLIPSIS)
21299 break;
21300 /* Otherwise, there must be more parameters. Consume the
21301 `,'. */
21302 cp_lexer_consume_token (parser->lexer);
21303 /* When parsing something like:
21305 int i(float f, double d)
21307 we can tell after seeing the declaration for "f" that we
21308 are not looking at an initialization of a variable "i",
21309 but rather at the declaration of a function "i".
21311 Due to the fact that the parsing of template arguments
21312 (as specified to a template-id) requires backtracking we
21313 cannot use this technique when inside a template argument
21314 list. */
21315 if (!parser->in_template_argument_list_p
21316 && !parser->in_type_id_in_expr_p
21317 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21318 /* However, a parameter-declaration of the form
21319 "float(f)" (which is a valid declaration of a
21320 parameter "f") can also be interpreted as an
21321 expression (the conversion of "f" to "float"). */
21322 && !parenthesized_p)
21323 cp_parser_commit_to_tentative_parse (parser);
21325 else
21327 cp_parser_error (parser, "expected %<,%> or %<...%>");
21328 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21329 cp_parser_skip_to_closing_parenthesis (parser,
21330 /*recovering=*/true,
21331 /*or_comma=*/false,
21332 /*consume_paren=*/false);
21333 break;
21337 parser->in_unbraced_linkage_specification_p
21338 = saved_in_unbraced_linkage_specification_p;
21340 /* Reset implicit_template_scope if we are about to leave the function
21341 parameter list that introduced it. Note that for out-of-line member
21342 definitions, there will be one or more class scopes before we get to
21343 the template parameter scope. */
21345 if (cp_binding_level *its = parser->implicit_template_scope)
21346 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21348 while (maybe_its->kind == sk_class)
21349 maybe_its = maybe_its->level_chain;
21350 if (maybe_its == its)
21352 parser->implicit_template_parms = 0;
21353 parser->implicit_template_scope = 0;
21357 return parameters;
21360 /* Parse a parameter declaration.
21362 parameter-declaration:
21363 decl-specifier-seq ... [opt] declarator
21364 decl-specifier-seq declarator = assignment-expression
21365 decl-specifier-seq ... [opt] abstract-declarator [opt]
21366 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21368 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21369 declares a template parameter. (In that case, a non-nested `>'
21370 token encountered during the parsing of the assignment-expression
21371 is not interpreted as a greater-than operator.)
21373 Returns a representation of the parameter, or NULL if an error
21374 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21375 true iff the declarator is of the form "(p)". */
21377 static cp_parameter_declarator *
21378 cp_parser_parameter_declaration (cp_parser *parser,
21379 bool template_parm_p,
21380 bool *parenthesized_p)
21382 int declares_class_or_enum;
21383 cp_decl_specifier_seq decl_specifiers;
21384 cp_declarator *declarator;
21385 tree default_argument;
21386 cp_token *token = NULL, *declarator_token_start = NULL;
21387 const char *saved_message;
21388 bool template_parameter_pack_p = false;
21390 /* In a template parameter, `>' is not an operator.
21392 [temp.param]
21394 When parsing a default template-argument for a non-type
21395 template-parameter, the first non-nested `>' is taken as the end
21396 of the template parameter-list rather than a greater-than
21397 operator. */
21399 /* Type definitions may not appear in parameter types. */
21400 saved_message = parser->type_definition_forbidden_message;
21401 parser->type_definition_forbidden_message
21402 = G_("types may not be defined in parameter types");
21404 /* Parse the declaration-specifiers. */
21405 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21406 cp_parser_decl_specifier_seq (parser,
21407 CP_PARSER_FLAGS_NONE,
21408 &decl_specifiers,
21409 &declares_class_or_enum);
21411 /* Complain about missing 'typename' or other invalid type names. */
21412 if (!decl_specifiers.any_type_specifiers_p
21413 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21414 decl_specifiers.type = error_mark_node;
21416 /* If an error occurred, there's no reason to attempt to parse the
21417 rest of the declaration. */
21418 if (cp_parser_error_occurred (parser))
21420 parser->type_definition_forbidden_message = saved_message;
21421 return NULL;
21424 /* Peek at the next token. */
21425 token = cp_lexer_peek_token (parser->lexer);
21427 /* If the next token is a `)', `,', `=', `>', or `...', then there
21428 is no declarator. However, when variadic templates are enabled,
21429 there may be a declarator following `...'. */
21430 if (token->type == CPP_CLOSE_PAREN
21431 || token->type == CPP_COMMA
21432 || token->type == CPP_EQ
21433 || token->type == CPP_GREATER)
21435 declarator = NULL;
21436 if (parenthesized_p)
21437 *parenthesized_p = false;
21439 /* Otherwise, there should be a declarator. */
21440 else
21442 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21443 parser->default_arg_ok_p = false;
21445 /* After seeing a decl-specifier-seq, if the next token is not a
21446 "(", there is no possibility that the code is a valid
21447 expression. Therefore, if parsing tentatively, we commit at
21448 this point. */
21449 if (!parser->in_template_argument_list_p
21450 /* In an expression context, having seen:
21452 (int((char ...
21454 we cannot be sure whether we are looking at a
21455 function-type (taking a "char" as a parameter) or a cast
21456 of some object of type "char" to "int". */
21457 && !parser->in_type_id_in_expr_p
21458 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21459 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21460 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21461 cp_parser_commit_to_tentative_parse (parser);
21462 /* Parse the declarator. */
21463 declarator_token_start = token;
21464 declarator = cp_parser_declarator (parser,
21465 CP_PARSER_DECLARATOR_EITHER,
21466 /*ctor_dtor_or_conv_p=*/NULL,
21467 parenthesized_p,
21468 /*member_p=*/false,
21469 /*friend_p=*/false);
21470 parser->default_arg_ok_p = saved_default_arg_ok_p;
21471 /* After the declarator, allow more attributes. */
21472 decl_specifiers.attributes
21473 = chainon (decl_specifiers.attributes,
21474 cp_parser_attributes_opt (parser));
21476 /* If the declarator is a template parameter pack, remember that and
21477 clear the flag in the declarator itself so we don't get errors
21478 from grokdeclarator. */
21479 if (template_parm_p && declarator && declarator->parameter_pack_p)
21481 declarator->parameter_pack_p = false;
21482 template_parameter_pack_p = true;
21486 /* If the next token is an ellipsis, and we have not seen a declarator
21487 name, and if either the type of the declarator contains parameter
21488 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21489 for, eg, abbreviated integral type names), then we actually have a
21490 parameter pack expansion expression. Otherwise, leave the ellipsis
21491 for a C-style variadic function. */
21492 token = cp_lexer_peek_token (parser->lexer);
21493 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21495 tree type = decl_specifiers.type;
21497 if (type && DECL_P (type))
21498 type = TREE_TYPE (type);
21500 if (((type
21501 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21502 && (template_parm_p || uses_parameter_packs (type)))
21503 || (!type && template_parm_p))
21504 && declarator_can_be_parameter_pack (declarator))
21506 /* Consume the `...'. */
21507 cp_lexer_consume_token (parser->lexer);
21508 maybe_warn_variadic_templates ();
21510 /* Build a pack expansion type */
21511 if (template_parm_p)
21512 template_parameter_pack_p = true;
21513 else if (declarator)
21514 declarator->parameter_pack_p = true;
21515 else
21516 decl_specifiers.type = make_pack_expansion (type);
21520 /* The restriction on defining new types applies only to the type
21521 of the parameter, not to the default argument. */
21522 parser->type_definition_forbidden_message = saved_message;
21524 /* If the next token is `=', then process a default argument. */
21525 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21527 tree type = decl_specifiers.type;
21528 token = cp_lexer_peek_token (parser->lexer);
21529 /* If we are defining a class, then the tokens that make up the
21530 default argument must be saved and processed later. */
21531 if (!template_parm_p && at_class_scope_p ()
21532 && TYPE_BEING_DEFINED (current_class_type)
21533 && !LAMBDA_TYPE_P (current_class_type))
21534 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21536 // A constrained-type-specifier may declare a type template-parameter.
21537 else if (declares_constrained_type_template_parameter (type))
21538 default_argument
21539 = cp_parser_default_type_template_argument (parser);
21541 // A constrained-type-specifier may declare a template-template-parameter.
21542 else if (declares_constrained_template_template_parameter (type))
21543 default_argument
21544 = cp_parser_default_template_template_argument (parser);
21546 /* Outside of a class definition, we can just parse the
21547 assignment-expression. */
21548 else
21549 default_argument
21550 = cp_parser_default_argument (parser, template_parm_p);
21552 if (!parser->default_arg_ok_p)
21554 permerror (token->location,
21555 "default arguments are only "
21556 "permitted for function parameters");
21558 else if ((declarator && declarator->parameter_pack_p)
21559 || template_parameter_pack_p
21560 || (decl_specifiers.type
21561 && PACK_EXPANSION_P (decl_specifiers.type)))
21563 /* Find the name of the parameter pack. */
21564 cp_declarator *id_declarator = declarator;
21565 while (id_declarator && id_declarator->kind != cdk_id)
21566 id_declarator = id_declarator->declarator;
21568 if (id_declarator && id_declarator->kind == cdk_id)
21569 error_at (declarator_token_start->location,
21570 template_parm_p
21571 ? G_("template parameter pack %qD "
21572 "cannot have a default argument")
21573 : G_("parameter pack %qD cannot have "
21574 "a default argument"),
21575 id_declarator->u.id.unqualified_name);
21576 else
21577 error_at (declarator_token_start->location,
21578 template_parm_p
21579 ? G_("template parameter pack cannot have "
21580 "a default argument")
21581 : G_("parameter pack cannot have a "
21582 "default argument"));
21584 default_argument = NULL_TREE;
21587 else
21588 default_argument = NULL_TREE;
21590 /* Generate a location for the parameter, ranging from the start of the
21591 initial token to the end of the final token (using input_location for
21592 the latter, set up by cp_lexer_set_source_position_from_token when
21593 consuming tokens).
21595 If we have a identifier, then use it for the caret location, e.g.
21597 extern int callee (int one, int (*two)(int, int), float three);
21598 ~~~~~~^~~~~~~~~~~~~~
21600 otherwise, reuse the start location for the caret location e.g.:
21602 extern int callee (int one, int (*)(int, int), float three);
21603 ^~~~~~~~~~~~~~~~~
21606 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21607 ? declarator->id_loc
21608 : decl_spec_token_start->location);
21609 location_t param_loc = make_location (caret_loc,
21610 decl_spec_token_start->location,
21611 input_location);
21613 return make_parameter_declarator (&decl_specifiers,
21614 declarator,
21615 default_argument,
21616 param_loc,
21617 template_parameter_pack_p);
21620 /* Parse a default argument and return it.
21622 TEMPLATE_PARM_P is true if this is a default argument for a
21623 non-type template parameter. */
21624 static tree
21625 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21627 tree default_argument = NULL_TREE;
21628 bool saved_greater_than_is_operator_p;
21629 bool saved_local_variables_forbidden_p;
21630 bool non_constant_p, is_direct_init;
21632 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21633 set correctly. */
21634 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21635 parser->greater_than_is_operator_p = !template_parm_p;
21636 /* Local variable names (and the `this' keyword) may not
21637 appear in a default argument. */
21638 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21639 parser->local_variables_forbidden_p = true;
21640 /* Parse the assignment-expression. */
21641 if (template_parm_p)
21642 push_deferring_access_checks (dk_no_deferred);
21643 tree saved_class_ptr = NULL_TREE;
21644 tree saved_class_ref = NULL_TREE;
21645 /* The "this" pointer is not valid in a default argument. */
21646 if (cfun)
21648 saved_class_ptr = current_class_ptr;
21649 cp_function_chain->x_current_class_ptr = NULL_TREE;
21650 saved_class_ref = current_class_ref;
21651 cp_function_chain->x_current_class_ref = NULL_TREE;
21653 default_argument
21654 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21655 /* Restore the "this" pointer. */
21656 if (cfun)
21658 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21659 cp_function_chain->x_current_class_ref = saved_class_ref;
21661 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21662 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21663 if (template_parm_p)
21664 pop_deferring_access_checks ();
21665 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21666 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21668 return default_argument;
21671 /* Parse a function-body.
21673 function-body:
21674 compound_statement */
21676 static void
21677 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21679 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21680 ? BCS_TRY_BLOCK : BCS_NORMAL),
21681 true);
21684 /* Parse a ctor-initializer-opt followed by a function-body. Return
21685 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21686 is true we are parsing a function-try-block. */
21688 static void
21689 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21690 bool in_function_try_block)
21692 tree body, list;
21693 const bool check_body_p =
21694 DECL_CONSTRUCTOR_P (current_function_decl)
21695 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21696 tree last = NULL;
21698 /* Begin the function body. */
21699 body = begin_function_body ();
21700 /* Parse the optional ctor-initializer. */
21701 cp_parser_ctor_initializer_opt (parser);
21703 /* If we're parsing a constexpr constructor definition, we need
21704 to check that the constructor body is indeed empty. However,
21705 before we get to cp_parser_function_body lot of junk has been
21706 generated, so we can't just check that we have an empty block.
21707 Rather we take a snapshot of the outermost block, and check whether
21708 cp_parser_function_body changed its state. */
21709 if (check_body_p)
21711 list = cur_stmt_list;
21712 if (STATEMENT_LIST_TAIL (list))
21713 last = STATEMENT_LIST_TAIL (list)->stmt;
21715 /* Parse the function-body. */
21716 cp_parser_function_body (parser, in_function_try_block);
21717 if (check_body_p)
21718 check_constexpr_ctor_body (last, list, /*complain=*/true);
21719 /* Finish the function body. */
21720 finish_function_body (body);
21723 /* Parse an initializer.
21725 initializer:
21726 = initializer-clause
21727 ( expression-list )
21729 Returns an expression representing the initializer. If no
21730 initializer is present, NULL_TREE is returned.
21732 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21733 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21734 set to TRUE if there is no initializer present. If there is an
21735 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21736 is set to true; otherwise it is set to false. */
21738 static tree
21739 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21740 bool* non_constant_p)
21742 cp_token *token;
21743 tree init;
21745 /* Peek at the next token. */
21746 token = cp_lexer_peek_token (parser->lexer);
21748 /* Let our caller know whether or not this initializer was
21749 parenthesized. */
21750 *is_direct_init = (token->type != CPP_EQ);
21751 /* Assume that the initializer is constant. */
21752 *non_constant_p = false;
21754 if (token->type == CPP_EQ)
21756 /* Consume the `='. */
21757 cp_lexer_consume_token (parser->lexer);
21758 /* Parse the initializer-clause. */
21759 init = cp_parser_initializer_clause (parser, non_constant_p);
21761 else if (token->type == CPP_OPEN_PAREN)
21763 vec<tree, va_gc> *vec;
21764 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21765 /*cast_p=*/false,
21766 /*allow_expansion_p=*/true,
21767 non_constant_p);
21768 if (vec == NULL)
21769 return error_mark_node;
21770 init = build_tree_list_vec (vec);
21771 release_tree_vector (vec);
21773 else if (token->type == CPP_OPEN_BRACE)
21775 cp_lexer_set_source_position (parser->lexer);
21776 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21777 init = cp_parser_braced_list (parser, non_constant_p);
21778 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21780 else
21782 /* Anything else is an error. */
21783 cp_parser_error (parser, "expected initializer");
21784 init = error_mark_node;
21787 if (check_for_bare_parameter_packs (init))
21788 init = error_mark_node;
21790 return init;
21793 /* Parse an initializer-clause.
21795 initializer-clause:
21796 assignment-expression
21797 braced-init-list
21799 Returns an expression representing the initializer.
21801 If the `assignment-expression' production is used the value
21802 returned is simply a representation for the expression.
21804 Otherwise, calls cp_parser_braced_list. */
21806 static cp_expr
21807 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21809 cp_expr initializer;
21811 /* Assume the expression is constant. */
21812 *non_constant_p = false;
21814 /* If it is not a `{', then we are looking at an
21815 assignment-expression. */
21816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21818 initializer
21819 = cp_parser_constant_expression (parser,
21820 /*allow_non_constant_p=*/true,
21821 non_constant_p);
21823 else
21824 initializer = cp_parser_braced_list (parser, non_constant_p);
21826 return initializer;
21829 /* Parse a brace-enclosed initializer list.
21831 braced-init-list:
21832 { initializer-list , [opt] }
21833 { designated-initializer-list , [opt] }
21836 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21837 the elements of the initializer-list (or NULL, if the last
21838 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21839 NULL_TREE. There is no way to detect whether or not the optional
21840 trailing `,' was provided. NON_CONSTANT_P is as for
21841 cp_parser_initializer. */
21843 static cp_expr
21844 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21846 tree initializer;
21847 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21849 /* Consume the `{' token. */
21850 matching_braces braces;
21851 braces.consume_open (parser);
21852 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21853 initializer = make_node (CONSTRUCTOR);
21854 /* If it's not a `}', then there is a non-trivial initializer. */
21855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21857 /* Parse the initializer list. */
21858 CONSTRUCTOR_ELTS (initializer)
21859 = cp_parser_initializer_list (parser, non_constant_p);
21860 /* A trailing `,' token is allowed. */
21861 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21862 cp_lexer_consume_token (parser->lexer);
21864 else
21865 *non_constant_p = false;
21866 /* Now, there should be a trailing `}'. */
21867 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21868 braces.require_close (parser);
21869 TREE_TYPE (initializer) = init_list_type_node;
21871 cp_expr result (initializer);
21872 /* Build a location of the form:
21873 { ... }
21874 ^~~~~~~
21875 with caret==start at the open brace, finish at the close brace. */
21876 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21877 result.set_location (combined_loc);
21878 return result;
21881 /* Consume tokens up to, and including, the next non-nested closing `]'.
21882 Returns true iff we found a closing `]'. */
21884 static bool
21885 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21887 unsigned square_depth = 0;
21889 while (true)
21891 cp_token * token = cp_lexer_peek_token (parser->lexer);
21893 switch (token->type)
21895 case CPP_EOF:
21896 case CPP_PRAGMA_EOL:
21897 /* If we've run out of tokens, then there is no closing `]'. */
21898 return false;
21900 case CPP_OPEN_SQUARE:
21901 ++square_depth;
21902 break;
21904 case CPP_CLOSE_SQUARE:
21905 if (!square_depth--)
21907 cp_lexer_consume_token (parser->lexer);
21908 return true;
21910 break;
21912 default:
21913 break;
21916 /* Consume the token. */
21917 cp_lexer_consume_token (parser->lexer);
21921 /* Return true if we are looking at an array-designator, false otherwise. */
21923 static bool
21924 cp_parser_array_designator_p (cp_parser *parser)
21926 /* Consume the `['. */
21927 cp_lexer_consume_token (parser->lexer);
21929 cp_lexer_save_tokens (parser->lexer);
21931 /* Skip tokens until the next token is a closing square bracket.
21932 If we find the closing `]', and the next token is a `=', then
21933 we are looking at an array designator. */
21934 bool array_designator_p
21935 = (cp_parser_skip_to_closing_square_bracket (parser)
21936 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21938 /* Roll back the tokens we skipped. */
21939 cp_lexer_rollback_tokens (parser->lexer);
21941 return array_designator_p;
21944 /* Parse an initializer-list.
21946 initializer-list:
21947 initializer-clause ... [opt]
21948 initializer-list , initializer-clause ... [opt]
21950 C++2A Extension:
21952 designated-initializer-list:
21953 designated-initializer-clause
21954 designated-initializer-list , designated-initializer-clause
21956 designated-initializer-clause:
21957 designator brace-or-equal-initializer
21959 designator:
21960 . identifier
21962 GNU Extension:
21964 initializer-list:
21965 designation initializer-clause ...[opt]
21966 initializer-list , designation initializer-clause ...[opt]
21968 designation:
21969 . identifier =
21970 identifier :
21971 [ constant-expression ] =
21973 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21974 for the initializer. If the INDEX of the elt is non-NULL, it is the
21975 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21976 as for cp_parser_initializer. */
21978 static vec<constructor_elt, va_gc> *
21979 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21981 vec<constructor_elt, va_gc> *v = NULL;
21982 bool first_p = true;
21983 tree first_designator = NULL_TREE;
21985 /* Assume all of the expressions are constant. */
21986 *non_constant_p = false;
21988 /* Parse the rest of the list. */
21989 while (true)
21991 cp_token *token;
21992 tree designator;
21993 tree initializer;
21994 bool clause_non_constant_p;
21995 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21997 /* Handle the C++2A syntax, '. id ='. */
21998 if ((cxx_dialect >= cxx2a
21999 || cp_parser_allow_gnu_extensions_p (parser))
22000 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22001 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22002 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22003 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22004 == CPP_OPEN_BRACE)))
22006 if (cxx_dialect < cxx2a)
22007 pedwarn (loc, OPT_Wpedantic,
22008 "C++ designated initializers only available with "
22009 "-std=c++2a or -std=gnu++2a");
22010 /* Consume the `.'. */
22011 cp_lexer_consume_token (parser->lexer);
22012 /* Consume the identifier. */
22013 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22014 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22015 /* Consume the `='. */
22016 cp_lexer_consume_token (parser->lexer);
22018 /* Also, if the next token is an identifier and the following one is a
22019 colon, we are looking at the GNU designated-initializer
22020 syntax. */
22021 else if (cp_parser_allow_gnu_extensions_p (parser)
22022 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22023 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22024 == CPP_COLON))
22026 /* Warn the user that they are using an extension. */
22027 pedwarn (loc, OPT_Wpedantic,
22028 "ISO C++ does not allow GNU designated initializers");
22029 /* Consume the identifier. */
22030 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22031 /* Consume the `:'. */
22032 cp_lexer_consume_token (parser->lexer);
22034 /* Also handle C99 array designators, '[ const ] ='. */
22035 else if (cp_parser_allow_gnu_extensions_p (parser)
22036 && !c_dialect_objc ()
22037 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22039 /* In C++11, [ could start a lambda-introducer. */
22040 bool non_const = false;
22042 cp_parser_parse_tentatively (parser);
22044 if (!cp_parser_array_designator_p (parser))
22046 cp_parser_simulate_error (parser);
22047 designator = NULL_TREE;
22049 else
22051 designator = cp_parser_constant_expression (parser, true,
22052 &non_const);
22053 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22054 cp_parser_require (parser, CPP_EQ, RT_EQ);
22057 if (!cp_parser_parse_definitely (parser))
22058 designator = NULL_TREE;
22059 else if (non_const
22060 && (!require_potential_rvalue_constant_expression
22061 (designator)))
22062 designator = NULL_TREE;
22063 if (designator)
22064 /* Warn the user that they are using an extension. */
22065 pedwarn (loc, OPT_Wpedantic,
22066 "ISO C++ does not allow C99 designated initializers");
22068 else
22069 designator = NULL_TREE;
22071 if (first_p)
22073 first_designator = designator;
22074 first_p = false;
22076 else if (cxx_dialect >= cxx2a
22077 && first_designator != error_mark_node
22078 && (!first_designator != !designator))
22080 error_at (loc, "either all initializer clauses should be designated "
22081 "or none of them should be");
22082 first_designator = error_mark_node;
22084 else if (cxx_dialect < cxx2a && !first_designator)
22085 first_designator = designator;
22087 /* Parse the initializer. */
22088 initializer = cp_parser_initializer_clause (parser,
22089 &clause_non_constant_p);
22090 /* If any clause is non-constant, so is the entire initializer. */
22091 if (clause_non_constant_p)
22092 *non_constant_p = true;
22094 /* If we have an ellipsis, this is an initializer pack
22095 expansion. */
22096 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22100 /* Consume the `...'. */
22101 cp_lexer_consume_token (parser->lexer);
22103 if (designator && cxx_dialect >= cxx2a)
22104 error_at (loc,
22105 "%<...%> not allowed in designated initializer list");
22107 /* Turn the initializer into an initializer expansion. */
22108 initializer = make_pack_expansion (initializer);
22111 /* Add it to the vector. */
22112 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22114 /* If the next token is not a comma, we have reached the end of
22115 the list. */
22116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22117 break;
22119 /* Peek at the next token. */
22120 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22121 /* If the next token is a `}', then we're still done. An
22122 initializer-clause can have a trailing `,' after the
22123 initializer-list and before the closing `}'. */
22124 if (token->type == CPP_CLOSE_BRACE)
22125 break;
22127 /* Consume the `,' token. */
22128 cp_lexer_consume_token (parser->lexer);
22131 /* The same identifier shall not appear in multiple designators
22132 of a designated-initializer-list. */
22133 if (first_designator)
22135 unsigned int i;
22136 tree designator, val;
22137 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22138 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22140 if (IDENTIFIER_MARKED (designator))
22142 error_at (EXPR_LOC_OR_LOC (val, input_location),
22143 "%<.%s%> designator used multiple times in "
22144 "the same initializer list",
22145 IDENTIFIER_POINTER (designator));
22146 (*v)[i].index = NULL_TREE;
22148 else
22149 IDENTIFIER_MARKED (designator) = 1;
22151 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22152 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22153 IDENTIFIER_MARKED (designator) = 0;
22156 return v;
22159 /* Classes [gram.class] */
22161 /* Parse a class-name.
22163 class-name:
22164 identifier
22165 template-id
22167 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22168 to indicate that names looked up in dependent types should be
22169 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22170 keyword has been used to indicate that the name that appears next
22171 is a template. TAG_TYPE indicates the explicit tag given before
22172 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22173 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22174 is the class being defined in a class-head. If ENUM_OK is TRUE,
22175 enum-names are also accepted.
22177 Returns the TYPE_DECL representing the class. */
22179 static tree
22180 cp_parser_class_name (cp_parser *parser,
22181 bool typename_keyword_p,
22182 bool template_keyword_p,
22183 enum tag_types tag_type,
22184 bool check_dependency_p,
22185 bool class_head_p,
22186 bool is_declaration,
22187 bool enum_ok)
22189 tree decl;
22190 tree scope;
22191 bool typename_p;
22192 cp_token *token;
22193 tree identifier = NULL_TREE;
22195 /* All class-names start with an identifier. */
22196 token = cp_lexer_peek_token (parser->lexer);
22197 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22199 cp_parser_error (parser, "expected class-name");
22200 return error_mark_node;
22203 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22204 to a template-id, so we save it here. */
22205 scope = parser->scope;
22206 if (scope == error_mark_node)
22207 return error_mark_node;
22209 /* Any name names a type if we're following the `typename' keyword
22210 in a qualified name where the enclosing scope is type-dependent. */
22211 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22212 && dependent_type_p (scope));
22213 /* Handle the common case (an identifier, but not a template-id)
22214 efficiently. */
22215 if (token->type == CPP_NAME
22216 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22218 cp_token *identifier_token;
22219 bool ambiguous_p;
22221 /* Look for the identifier. */
22222 identifier_token = cp_lexer_peek_token (parser->lexer);
22223 ambiguous_p = identifier_token->error_reported;
22224 identifier = cp_parser_identifier (parser);
22225 /* If the next token isn't an identifier, we are certainly not
22226 looking at a class-name. */
22227 if (identifier == error_mark_node)
22228 decl = error_mark_node;
22229 /* If we know this is a type-name, there's no need to look it
22230 up. */
22231 else if (typename_p)
22232 decl = identifier;
22233 else
22235 tree ambiguous_decls;
22236 /* If we already know that this lookup is ambiguous, then
22237 we've already issued an error message; there's no reason
22238 to check again. */
22239 if (ambiguous_p)
22241 cp_parser_simulate_error (parser);
22242 return error_mark_node;
22244 /* If the next token is a `::', then the name must be a type
22245 name.
22247 [basic.lookup.qual]
22249 During the lookup for a name preceding the :: scope
22250 resolution operator, object, function, and enumerator
22251 names are ignored. */
22252 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22253 tag_type = scope_type;
22254 /* Look up the name. */
22255 decl = cp_parser_lookup_name (parser, identifier,
22256 tag_type,
22257 /*is_template=*/false,
22258 /*is_namespace=*/false,
22259 check_dependency_p,
22260 &ambiguous_decls,
22261 identifier_token->location);
22262 if (ambiguous_decls)
22264 if (cp_parser_parsing_tentatively (parser))
22265 cp_parser_simulate_error (parser);
22266 return error_mark_node;
22270 else
22272 /* Try a template-id. */
22273 decl = cp_parser_template_id (parser, template_keyword_p,
22274 check_dependency_p,
22275 tag_type,
22276 is_declaration);
22277 if (decl == error_mark_node)
22278 return error_mark_node;
22281 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22283 /* If this is a typename, create a TYPENAME_TYPE. */
22284 if (typename_p && decl != error_mark_node)
22286 decl = make_typename_type (scope, decl, typename_type,
22287 /*complain=*/tf_error);
22288 if (decl != error_mark_node)
22289 decl = TYPE_NAME (decl);
22292 decl = strip_using_decl (decl);
22294 /* Check to see that it is really the name of a class. */
22295 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22296 && identifier_p (TREE_OPERAND (decl, 0))
22297 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22298 /* Situations like this:
22300 template <typename T> struct A {
22301 typename T::template X<int>::I i;
22304 are problematic. Is `T::template X<int>' a class-name? The
22305 standard does not seem to be definitive, but there is no other
22306 valid interpretation of the following `::'. Therefore, those
22307 names are considered class-names. */
22309 decl = make_typename_type (scope, decl, tag_type, tf_error);
22310 if (decl != error_mark_node)
22311 decl = TYPE_NAME (decl);
22313 else if (TREE_CODE (decl) != TYPE_DECL
22314 || TREE_TYPE (decl) == error_mark_node
22315 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22316 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22317 /* In Objective-C 2.0, a classname followed by '.' starts a
22318 dot-syntax expression, and it's not a type-name. */
22319 || (c_dialect_objc ()
22320 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22321 && objc_is_class_name (decl)))
22322 decl = error_mark_node;
22324 if (decl == error_mark_node)
22325 cp_parser_error (parser, "expected class-name");
22326 else if (identifier && !parser->scope)
22327 maybe_note_name_used_in_class (identifier, decl);
22329 return decl;
22332 /* Parse a class-specifier.
22334 class-specifier:
22335 class-head { member-specification [opt] }
22337 Returns the TREE_TYPE representing the class. */
22339 static tree
22340 cp_parser_class_specifier_1 (cp_parser* parser)
22342 tree type;
22343 tree attributes = NULL_TREE;
22344 bool nested_name_specifier_p;
22345 unsigned saved_num_template_parameter_lists;
22346 bool saved_in_function_body;
22347 unsigned char in_statement;
22348 bool in_switch_statement_p;
22349 bool saved_in_unbraced_linkage_specification_p;
22350 tree old_scope = NULL_TREE;
22351 tree scope = NULL_TREE;
22352 cp_token *closing_brace;
22354 push_deferring_access_checks (dk_no_deferred);
22356 /* Parse the class-head. */
22357 type = cp_parser_class_head (parser,
22358 &nested_name_specifier_p);
22359 /* If the class-head was a semantic disaster, skip the entire body
22360 of the class. */
22361 if (!type)
22363 cp_parser_skip_to_end_of_block_or_statement (parser);
22364 pop_deferring_access_checks ();
22365 return error_mark_node;
22368 /* Look for the `{'. */
22369 matching_braces braces;
22370 if (!braces.require_open (parser))
22372 pop_deferring_access_checks ();
22373 return error_mark_node;
22376 cp_ensure_no_omp_declare_simd (parser);
22377 cp_ensure_no_oacc_routine (parser);
22379 /* Issue an error message if type-definitions are forbidden here. */
22380 cp_parser_check_type_definition (parser);
22381 /* Remember that we are defining one more class. */
22382 ++parser->num_classes_being_defined;
22383 /* Inside the class, surrounding template-parameter-lists do not
22384 apply. */
22385 saved_num_template_parameter_lists
22386 = parser->num_template_parameter_lists;
22387 parser->num_template_parameter_lists = 0;
22388 /* We are not in a function body. */
22389 saved_in_function_body = parser->in_function_body;
22390 parser->in_function_body = false;
22391 /* Or in a loop. */
22392 in_statement = parser->in_statement;
22393 parser->in_statement = 0;
22394 /* Or in a switch. */
22395 in_switch_statement_p = parser->in_switch_statement_p;
22396 parser->in_switch_statement_p = false;
22397 /* We are not immediately inside an extern "lang" block. */
22398 saved_in_unbraced_linkage_specification_p
22399 = parser->in_unbraced_linkage_specification_p;
22400 parser->in_unbraced_linkage_specification_p = false;
22402 // Associate constraints with the type.
22403 if (flag_concepts)
22404 type = associate_classtype_constraints (type);
22406 /* Start the class. */
22407 if (nested_name_specifier_p)
22409 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22410 old_scope = push_inner_scope (scope);
22412 type = begin_class_definition (type);
22414 if (type == error_mark_node)
22415 /* If the type is erroneous, skip the entire body of the class. */
22416 cp_parser_skip_to_closing_brace (parser);
22417 else
22418 /* Parse the member-specification. */
22419 cp_parser_member_specification_opt (parser);
22421 /* Look for the trailing `}'. */
22422 closing_brace = braces.require_close (parser);
22423 /* Look for trailing attributes to apply to this class. */
22424 if (cp_parser_allow_gnu_extensions_p (parser))
22425 attributes = cp_parser_gnu_attributes_opt (parser);
22426 if (type != error_mark_node)
22427 type = finish_struct (type, attributes);
22428 if (nested_name_specifier_p)
22429 pop_inner_scope (old_scope, scope);
22431 /* We've finished a type definition. Check for the common syntax
22432 error of forgetting a semicolon after the definition. We need to
22433 be careful, as we can't just check for not-a-semicolon and be done
22434 with it; the user might have typed:
22436 class X { } c = ...;
22437 class X { } *p = ...;
22439 and so forth. Instead, enumerate all the possible tokens that
22440 might follow this production; if we don't see one of them, then
22441 complain and silently insert the semicolon. */
22443 cp_token *token = cp_lexer_peek_token (parser->lexer);
22444 bool want_semicolon = true;
22446 if (cp_next_tokens_can_be_std_attribute_p (parser))
22447 /* Don't try to parse c++11 attributes here. As per the
22448 grammar, that should be a task for
22449 cp_parser_decl_specifier_seq. */
22450 want_semicolon = false;
22452 switch (token->type)
22454 case CPP_NAME:
22455 case CPP_SEMICOLON:
22456 case CPP_MULT:
22457 case CPP_AND:
22458 case CPP_OPEN_PAREN:
22459 case CPP_CLOSE_PAREN:
22460 case CPP_COMMA:
22461 want_semicolon = false;
22462 break;
22464 /* While it's legal for type qualifiers and storage class
22465 specifiers to follow type definitions in the grammar, only
22466 compiler testsuites contain code like that. Assume that if
22467 we see such code, then what we're really seeing is a case
22468 like:
22470 class X { }
22471 const <type> var = ...;
22475 class Y { }
22476 static <type> func (...) ...
22478 i.e. the qualifier or specifier applies to the next
22479 declaration. To do so, however, we need to look ahead one
22480 more token to see if *that* token is a type specifier.
22482 This code could be improved to handle:
22484 class Z { }
22485 static const <type> var = ...; */
22486 case CPP_KEYWORD:
22487 if (keyword_is_decl_specifier (token->keyword))
22489 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22491 /* Handling user-defined types here would be nice, but very
22492 tricky. */
22493 want_semicolon
22494 = (lookahead->type == CPP_KEYWORD
22495 && keyword_begins_type_specifier (lookahead->keyword));
22497 break;
22498 default:
22499 break;
22502 /* If we don't have a type, then something is very wrong and we
22503 shouldn't try to do anything clever. Likewise for not seeing the
22504 closing brace. */
22505 if (closing_brace && TYPE_P (type) && want_semicolon)
22507 /* Locate the closing brace. */
22508 cp_token_position prev
22509 = cp_lexer_previous_token_position (parser->lexer);
22510 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22511 location_t loc = prev_token->location;
22513 /* We want to suggest insertion of a ';' immediately *after* the
22514 closing brace, so, if we can, offset the location by 1 column. */
22515 location_t next_loc = loc;
22516 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22517 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22519 rich_location richloc (line_table, next_loc);
22521 /* If we successfully offset the location, suggest the fix-it. */
22522 if (next_loc != loc)
22523 richloc.add_fixit_insert_before (next_loc, ";");
22525 if (CLASSTYPE_DECLARED_CLASS (type))
22526 error_at (&richloc,
22527 "expected %<;%> after class definition");
22528 else if (TREE_CODE (type) == RECORD_TYPE)
22529 error_at (&richloc,
22530 "expected %<;%> after struct definition");
22531 else if (TREE_CODE (type) == UNION_TYPE)
22532 error_at (&richloc,
22533 "expected %<;%> after union definition");
22534 else
22535 gcc_unreachable ();
22537 /* Unget one token and smash it to look as though we encountered
22538 a semicolon in the input stream. */
22539 cp_lexer_set_token_position (parser->lexer, prev);
22540 token = cp_lexer_peek_token (parser->lexer);
22541 token->type = CPP_SEMICOLON;
22542 token->keyword = RID_MAX;
22546 /* If this class is not itself within the scope of another class,
22547 then we need to parse the bodies of all of the queued function
22548 definitions. Note that the queued functions defined in a class
22549 are not always processed immediately following the
22550 class-specifier for that class. Consider:
22552 struct A {
22553 struct B { void f() { sizeof (A); } };
22556 If `f' were processed before the processing of `A' were
22557 completed, there would be no way to compute the size of `A'.
22558 Note that the nesting we are interested in here is lexical --
22559 not the semantic nesting given by TYPE_CONTEXT. In particular,
22560 for:
22562 struct A { struct B; };
22563 struct A::B { void f() { } };
22565 there is no need to delay the parsing of `A::B::f'. */
22566 if (--parser->num_classes_being_defined == 0)
22568 tree decl;
22569 tree class_type = NULL_TREE;
22570 tree pushed_scope = NULL_TREE;
22571 unsigned ix;
22572 cp_default_arg_entry *e;
22573 tree save_ccp, save_ccr;
22575 /* In a first pass, parse default arguments to the functions.
22576 Then, in a second pass, parse the bodies of the functions.
22577 This two-phased approach handles cases like:
22579 struct S {
22580 void f() { g(); }
22581 void g(int i = 3);
22585 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22587 decl = e->decl;
22588 /* If there are default arguments that have not yet been processed,
22589 take care of them now. */
22590 if (class_type != e->class_type)
22592 if (pushed_scope)
22593 pop_scope (pushed_scope);
22594 class_type = e->class_type;
22595 pushed_scope = push_scope (class_type);
22597 /* Make sure that any template parameters are in scope. */
22598 maybe_begin_member_template_processing (decl);
22599 /* Parse the default argument expressions. */
22600 cp_parser_late_parsing_default_args (parser, decl);
22601 /* Remove any template parameters from the symbol table. */
22602 maybe_end_member_template_processing ();
22604 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22605 /* Now parse any NSDMIs. */
22606 save_ccp = current_class_ptr;
22607 save_ccr = current_class_ref;
22608 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22610 if (class_type != DECL_CONTEXT (decl))
22612 if (pushed_scope)
22613 pop_scope (pushed_scope);
22614 class_type = DECL_CONTEXT (decl);
22615 pushed_scope = push_scope (class_type);
22617 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22618 cp_parser_late_parsing_nsdmi (parser, decl);
22620 vec_safe_truncate (unparsed_nsdmis, 0);
22621 current_class_ptr = save_ccp;
22622 current_class_ref = save_ccr;
22623 if (pushed_scope)
22624 pop_scope (pushed_scope);
22626 /* Now do some post-NSDMI bookkeeping. */
22627 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22628 after_nsdmi_defaulted_late_checks (class_type);
22629 vec_safe_truncate (unparsed_classes, 0);
22630 after_nsdmi_defaulted_late_checks (type);
22632 /* Now parse the body of the functions. */
22633 if (flag_openmp)
22635 /* OpenMP UDRs need to be parsed before all other functions. */
22636 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22637 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22638 cp_parser_late_parsing_for_member (parser, decl);
22639 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22640 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22641 cp_parser_late_parsing_for_member (parser, decl);
22643 else
22644 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22645 cp_parser_late_parsing_for_member (parser, decl);
22646 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22648 else
22649 vec_safe_push (unparsed_classes, type);
22651 /* Put back any saved access checks. */
22652 pop_deferring_access_checks ();
22654 /* Restore saved state. */
22655 parser->in_switch_statement_p = in_switch_statement_p;
22656 parser->in_statement = in_statement;
22657 parser->in_function_body = saved_in_function_body;
22658 parser->num_template_parameter_lists
22659 = saved_num_template_parameter_lists;
22660 parser->in_unbraced_linkage_specification_p
22661 = saved_in_unbraced_linkage_specification_p;
22663 return type;
22666 static tree
22667 cp_parser_class_specifier (cp_parser* parser)
22669 tree ret;
22670 timevar_push (TV_PARSE_STRUCT);
22671 ret = cp_parser_class_specifier_1 (parser);
22672 timevar_pop (TV_PARSE_STRUCT);
22673 return ret;
22676 /* Parse a class-head.
22678 class-head:
22679 class-key identifier [opt] base-clause [opt]
22680 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22681 class-key nested-name-specifier [opt] template-id
22682 base-clause [opt]
22684 class-virt-specifier:
22685 final
22687 GNU Extensions:
22688 class-key attributes identifier [opt] base-clause [opt]
22689 class-key attributes nested-name-specifier identifier base-clause [opt]
22690 class-key attributes nested-name-specifier [opt] template-id
22691 base-clause [opt]
22693 Upon return BASES is initialized to the list of base classes (or
22694 NULL, if there are none) in the same form returned by
22695 cp_parser_base_clause.
22697 Returns the TYPE of the indicated class. Sets
22698 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22699 involving a nested-name-specifier was used, and FALSE otherwise.
22701 Returns error_mark_node if this is not a class-head.
22703 Returns NULL_TREE if the class-head is syntactically valid, but
22704 semantically invalid in a way that means we should skip the entire
22705 body of the class. */
22707 static tree
22708 cp_parser_class_head (cp_parser* parser,
22709 bool* nested_name_specifier_p)
22711 tree nested_name_specifier;
22712 enum tag_types class_key;
22713 tree id = NULL_TREE;
22714 tree type = NULL_TREE;
22715 tree attributes;
22716 tree bases;
22717 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22718 bool template_id_p = false;
22719 bool qualified_p = false;
22720 bool invalid_nested_name_p = false;
22721 bool invalid_explicit_specialization_p = false;
22722 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22723 tree pushed_scope = NULL_TREE;
22724 unsigned num_templates;
22725 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22726 /* Assume no nested-name-specifier will be present. */
22727 *nested_name_specifier_p = false;
22728 /* Assume no template parameter lists will be used in defining the
22729 type. */
22730 num_templates = 0;
22731 parser->colon_corrects_to_scope_p = false;
22733 /* Look for the class-key. */
22734 class_key = cp_parser_class_key (parser);
22735 if (class_key == none_type)
22736 return error_mark_node;
22738 location_t class_head_start_location = input_location;
22740 /* Parse the attributes. */
22741 attributes = cp_parser_attributes_opt (parser);
22743 /* If the next token is `::', that is invalid -- but sometimes
22744 people do try to write:
22746 struct ::S {};
22748 Handle this gracefully by accepting the extra qualifier, and then
22749 issuing an error about it later if this really is a
22750 class-head. If it turns out just to be an elaborated type
22751 specifier, remain silent. */
22752 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22753 qualified_p = true;
22755 push_deferring_access_checks (dk_no_check);
22757 /* Determine the name of the class. Begin by looking for an
22758 optional nested-name-specifier. */
22759 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22760 nested_name_specifier
22761 = cp_parser_nested_name_specifier_opt (parser,
22762 /*typename_keyword_p=*/false,
22763 /*check_dependency_p=*/false,
22764 /*type_p=*/true,
22765 /*is_declaration=*/false);
22766 /* If there was a nested-name-specifier, then there *must* be an
22767 identifier. */
22769 cp_token *bad_template_keyword = NULL;
22771 if (nested_name_specifier)
22773 type_start_token = cp_lexer_peek_token (parser->lexer);
22774 /* Although the grammar says `identifier', it really means
22775 `class-name' or `template-name'. You are only allowed to
22776 define a class that has already been declared with this
22777 syntax.
22779 The proposed resolution for Core Issue 180 says that wherever
22780 you see `class T::X' you should treat `X' as a type-name.
22782 It is OK to define an inaccessible class; for example:
22784 class A { class B; };
22785 class A::B {};
22787 We do not know if we will see a class-name, or a
22788 template-name. We look for a class-name first, in case the
22789 class-name is a template-id; if we looked for the
22790 template-name first we would stop after the template-name. */
22791 cp_parser_parse_tentatively (parser);
22792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22793 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22794 type = cp_parser_class_name (parser,
22795 /*typename_keyword_p=*/false,
22796 /*template_keyword_p=*/false,
22797 class_type,
22798 /*check_dependency_p=*/false,
22799 /*class_head_p=*/true,
22800 /*is_declaration=*/false);
22801 /* If that didn't work, ignore the nested-name-specifier. */
22802 if (!cp_parser_parse_definitely (parser))
22804 invalid_nested_name_p = true;
22805 type_start_token = cp_lexer_peek_token (parser->lexer);
22806 id = cp_parser_identifier (parser);
22807 if (id == error_mark_node)
22808 id = NULL_TREE;
22810 /* If we could not find a corresponding TYPE, treat this
22811 declaration like an unqualified declaration. */
22812 if (type == error_mark_node)
22813 nested_name_specifier = NULL_TREE;
22814 /* Otherwise, count the number of templates used in TYPE and its
22815 containing scopes. */
22816 else
22818 tree scope;
22820 for (scope = TREE_TYPE (type);
22821 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22822 scope = get_containing_scope (scope))
22823 if (TYPE_P (scope)
22824 && CLASS_TYPE_P (scope)
22825 && CLASSTYPE_TEMPLATE_INFO (scope)
22826 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22827 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22828 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22829 ++num_templates;
22832 /* Otherwise, the identifier is optional. */
22833 else
22835 /* We don't know whether what comes next is a template-id,
22836 an identifier, or nothing at all. */
22837 cp_parser_parse_tentatively (parser);
22838 /* Check for a template-id. */
22839 type_start_token = cp_lexer_peek_token (parser->lexer);
22840 id = cp_parser_template_id (parser,
22841 /*template_keyword_p=*/false,
22842 /*check_dependency_p=*/true,
22843 class_key,
22844 /*is_declaration=*/true);
22845 /* If that didn't work, it could still be an identifier. */
22846 if (!cp_parser_parse_definitely (parser))
22848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22850 type_start_token = cp_lexer_peek_token (parser->lexer);
22851 id = cp_parser_identifier (parser);
22853 else
22854 id = NULL_TREE;
22856 else
22858 template_id_p = true;
22859 ++num_templates;
22863 pop_deferring_access_checks ();
22865 if (id)
22867 cp_parser_check_for_invalid_template_id (parser, id,
22868 class_key,
22869 type_start_token->location);
22871 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22873 /* If it's not a `:' or a `{' then we can't really be looking at a
22874 class-head, since a class-head only appears as part of a
22875 class-specifier. We have to detect this situation before calling
22876 xref_tag, since that has irreversible side-effects. */
22877 if (!cp_parser_next_token_starts_class_definition_p (parser))
22879 cp_parser_error (parser, "expected %<{%> or %<:%>");
22880 type = error_mark_node;
22881 goto out;
22884 /* At this point, we're going ahead with the class-specifier, even
22885 if some other problem occurs. */
22886 cp_parser_commit_to_tentative_parse (parser);
22887 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22889 cp_parser_error (parser,
22890 "cannot specify %<override%> for a class");
22891 type = error_mark_node;
22892 goto out;
22894 /* Issue the error about the overly-qualified name now. */
22895 if (qualified_p)
22897 cp_parser_error (parser,
22898 "global qualification of class name is invalid");
22899 type = error_mark_node;
22900 goto out;
22902 else if (invalid_nested_name_p)
22904 cp_parser_error (parser,
22905 "qualified name does not name a class");
22906 type = error_mark_node;
22907 goto out;
22909 else if (nested_name_specifier)
22911 tree scope;
22913 if (bad_template_keyword)
22914 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22915 keyword template shall not appear at the top level. */
22916 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22917 "keyword %<template%> not allowed in class-head-name");
22919 /* Reject typedef-names in class heads. */
22920 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22922 error_at (type_start_token->location,
22923 "invalid class name in declaration of %qD",
22924 type);
22925 type = NULL_TREE;
22926 goto done;
22929 /* Figure out in what scope the declaration is being placed. */
22930 scope = current_scope ();
22931 /* If that scope does not contain the scope in which the
22932 class was originally declared, the program is invalid. */
22933 if (scope && !is_ancestor (scope, nested_name_specifier))
22935 if (at_namespace_scope_p ())
22936 error_at (type_start_token->location,
22937 "declaration of %qD in namespace %qD which does not "
22938 "enclose %qD",
22939 type, scope, nested_name_specifier);
22940 else
22941 error_at (type_start_token->location,
22942 "declaration of %qD in %qD which does not enclose %qD",
22943 type, scope, nested_name_specifier);
22944 type = NULL_TREE;
22945 goto done;
22947 /* [dcl.meaning]
22949 A declarator-id shall not be qualified except for the
22950 definition of a ... nested class outside of its class
22951 ... [or] the definition or explicit instantiation of a
22952 class member of a namespace outside of its namespace. */
22953 if (scope == nested_name_specifier)
22955 permerror (nested_name_specifier_token_start->location,
22956 "extra qualification not allowed");
22957 nested_name_specifier = NULL_TREE;
22958 num_templates = 0;
22961 /* An explicit-specialization must be preceded by "template <>". If
22962 it is not, try to recover gracefully. */
22963 if (at_namespace_scope_p ()
22964 && parser->num_template_parameter_lists == 0
22965 && !processing_template_parmlist
22966 && template_id_p)
22968 /* Build a location of this form:
22969 struct typename <ARGS>
22970 ^~~~~~~~~~~~~~~~~~~~~~
22971 with caret==start at the start token, and
22972 finishing at the end of the type. */
22973 location_t reported_loc
22974 = make_location (class_head_start_location,
22975 class_head_start_location,
22976 get_finish (type_start_token->location));
22977 rich_location richloc (line_table, reported_loc);
22978 richloc.add_fixit_insert_before (class_head_start_location,
22979 "template <> ");
22980 error_at (&richloc,
22981 "an explicit specialization must be preceded by"
22982 " %<template <>%>");
22983 invalid_explicit_specialization_p = true;
22984 /* Take the same action that would have been taken by
22985 cp_parser_explicit_specialization. */
22986 ++parser->num_template_parameter_lists;
22987 begin_specialization ();
22989 /* There must be no "return" statements between this point and the
22990 end of this function; set "type "to the correct return value and
22991 use "goto done;" to return. */
22992 /* Make sure that the right number of template parameters were
22993 present. */
22994 if (!cp_parser_check_template_parameters (parser, num_templates,
22995 type_start_token->location,
22996 /*declarator=*/NULL))
22998 /* If something went wrong, there is no point in even trying to
22999 process the class-definition. */
23000 type = NULL_TREE;
23001 goto done;
23004 /* Look up the type. */
23005 if (template_id_p)
23007 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23008 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23009 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23011 error_at (type_start_token->location,
23012 "function template %qD redeclared as a class template", id);
23013 type = error_mark_node;
23015 else
23017 type = TREE_TYPE (id);
23018 type = maybe_process_partial_specialization (type);
23020 /* Check the scope while we still know whether or not we had a
23021 nested-name-specifier. */
23022 if (type != error_mark_node)
23023 check_unqualified_spec_or_inst (type, type_start_token->location);
23025 if (nested_name_specifier)
23026 pushed_scope = push_scope (nested_name_specifier);
23028 else if (nested_name_specifier)
23030 tree class_type;
23032 /* Given:
23034 template <typename T> struct S { struct T };
23035 template <typename T> struct S<T>::T { };
23037 we will get a TYPENAME_TYPE when processing the definition of
23038 `S::T'. We need to resolve it to the actual type before we
23039 try to define it. */
23040 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23042 class_type = resolve_typename_type (TREE_TYPE (type),
23043 /*only_current_p=*/false);
23044 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23045 type = TYPE_NAME (class_type);
23046 else
23048 cp_parser_error (parser, "could not resolve typename type");
23049 type = error_mark_node;
23053 if (maybe_process_partial_specialization (TREE_TYPE (type))
23054 == error_mark_node)
23056 type = NULL_TREE;
23057 goto done;
23060 class_type = current_class_type;
23061 /* Enter the scope indicated by the nested-name-specifier. */
23062 pushed_scope = push_scope (nested_name_specifier);
23063 /* Get the canonical version of this type. */
23064 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23065 /* Call push_template_decl if it seems like we should be defining a
23066 template either from the template headers or the type we're
23067 defining, so that we diagnose both extra and missing headers. */
23068 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23069 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23070 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23072 type = push_template_decl (type);
23073 if (type == error_mark_node)
23075 type = NULL_TREE;
23076 goto done;
23080 type = TREE_TYPE (type);
23081 *nested_name_specifier_p = true;
23083 else /* The name is not a nested name. */
23085 /* If the class was unnamed, create a dummy name. */
23086 if (!id)
23087 id = make_anon_name ();
23088 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23089 ? ts_within_enclosing_non_class
23090 : ts_current);
23091 type = xref_tag (class_key, id, tag_scope,
23092 parser->num_template_parameter_lists);
23095 /* Indicate whether this class was declared as a `class' or as a
23096 `struct'. */
23097 if (TREE_CODE (type) == RECORD_TYPE)
23098 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23099 cp_parser_check_class_key (class_key, type);
23101 /* If this type was already complete, and we see another definition,
23102 that's an error. */
23103 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23105 error_at (type_start_token->location, "redefinition of %q#T",
23106 type);
23107 inform (location_of (type), "previous definition of %q#T",
23108 type);
23109 type = NULL_TREE;
23110 goto done;
23112 else if (type == error_mark_node)
23113 type = NULL_TREE;
23115 if (type)
23117 /* Apply attributes now, before any use of the class as a template
23118 argument in its base list. */
23119 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23120 fixup_attribute_variants (type);
23123 /* We will have entered the scope containing the class; the names of
23124 base classes should be looked up in that context. For example:
23126 struct A { struct B {}; struct C; };
23127 struct A::C : B {};
23129 is valid. */
23131 /* Get the list of base-classes, if there is one. */
23132 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23134 /* PR59482: enter the class scope so that base-specifiers are looked
23135 up correctly. */
23136 if (type)
23137 pushclass (type);
23138 bases = cp_parser_base_clause (parser);
23139 /* PR59482: get out of the previously pushed class scope so that the
23140 subsequent pops pop the right thing. */
23141 if (type)
23142 popclass ();
23144 else
23145 bases = NULL_TREE;
23147 /* If we're really defining a class, process the base classes.
23148 If they're invalid, fail. */
23149 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23150 xref_basetypes (type, bases);
23152 done:
23153 /* Leave the scope given by the nested-name-specifier. We will
23154 enter the class scope itself while processing the members. */
23155 if (pushed_scope)
23156 pop_scope (pushed_scope);
23158 if (invalid_explicit_specialization_p)
23160 end_specialization ();
23161 --parser->num_template_parameter_lists;
23164 if (type)
23165 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23166 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23167 CLASSTYPE_FINAL (type) = 1;
23168 out:
23169 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23170 return type;
23173 /* Parse a class-key.
23175 class-key:
23176 class
23177 struct
23178 union
23180 Returns the kind of class-key specified, or none_type to indicate
23181 error. */
23183 static enum tag_types
23184 cp_parser_class_key (cp_parser* parser)
23186 cp_token *token;
23187 enum tag_types tag_type;
23189 /* Look for the class-key. */
23190 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23191 if (!token)
23192 return none_type;
23194 /* Check to see if the TOKEN is a class-key. */
23195 tag_type = cp_parser_token_is_class_key (token);
23196 if (!tag_type)
23197 cp_parser_error (parser, "expected class-key");
23198 return tag_type;
23201 /* Parse a type-parameter-key.
23203 type-parameter-key:
23204 class
23205 typename
23208 static void
23209 cp_parser_type_parameter_key (cp_parser* parser)
23211 /* Look for the type-parameter-key. */
23212 enum tag_types tag_type = none_type;
23213 cp_token *token = cp_lexer_peek_token (parser->lexer);
23214 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23216 cp_lexer_consume_token (parser->lexer);
23217 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23218 /* typename is not allowed in a template template parameter
23219 by the standard until C++17. */
23220 pedwarn (token->location, OPT_Wpedantic,
23221 "ISO C++ forbids typename key in template template parameter;"
23222 " use -std=c++17 or -std=gnu++17");
23224 else
23225 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23227 return;
23230 /* Parse an (optional) member-specification.
23232 member-specification:
23233 member-declaration member-specification [opt]
23234 access-specifier : member-specification [opt] */
23236 static void
23237 cp_parser_member_specification_opt (cp_parser* parser)
23239 while (true)
23241 cp_token *token;
23242 enum rid keyword;
23244 /* Peek at the next token. */
23245 token = cp_lexer_peek_token (parser->lexer);
23246 /* If it's a `}', or EOF then we've seen all the members. */
23247 if (token->type == CPP_CLOSE_BRACE
23248 || token->type == CPP_EOF
23249 || token->type == CPP_PRAGMA_EOL)
23250 break;
23252 /* See if this token is a keyword. */
23253 keyword = token->keyword;
23254 switch (keyword)
23256 case RID_PUBLIC:
23257 case RID_PROTECTED:
23258 case RID_PRIVATE:
23259 /* Consume the access-specifier. */
23260 cp_lexer_consume_token (parser->lexer);
23261 /* Remember which access-specifier is active. */
23262 current_access_specifier = token->u.value;
23263 /* Look for the `:'. */
23264 cp_parser_require (parser, CPP_COLON, RT_COLON);
23265 break;
23267 default:
23268 /* Accept #pragmas at class scope. */
23269 if (token->type == CPP_PRAGMA)
23271 cp_parser_pragma (parser, pragma_member, NULL);
23272 break;
23275 /* Otherwise, the next construction must be a
23276 member-declaration. */
23277 cp_parser_member_declaration (parser);
23282 /* Parse a member-declaration.
23284 member-declaration:
23285 decl-specifier-seq [opt] member-declarator-list [opt] ;
23286 function-definition ; [opt]
23287 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23288 using-declaration
23289 template-declaration
23290 alias-declaration
23292 member-declarator-list:
23293 member-declarator
23294 member-declarator-list , member-declarator
23296 member-declarator:
23297 declarator pure-specifier [opt]
23298 declarator constant-initializer [opt]
23299 identifier [opt] : constant-expression
23301 GNU Extensions:
23303 member-declaration:
23304 __extension__ member-declaration
23306 member-declarator:
23307 declarator attributes [opt] pure-specifier [opt]
23308 declarator attributes [opt] constant-initializer [opt]
23309 identifier [opt] attributes [opt] : constant-expression
23311 C++0x Extensions:
23313 member-declaration:
23314 static_assert-declaration */
23316 static void
23317 cp_parser_member_declaration (cp_parser* parser)
23319 cp_decl_specifier_seq decl_specifiers;
23320 tree prefix_attributes;
23321 tree decl;
23322 int declares_class_or_enum;
23323 bool friend_p;
23324 cp_token *token = NULL;
23325 cp_token *decl_spec_token_start = NULL;
23326 cp_token *initializer_token_start = NULL;
23327 int saved_pedantic;
23328 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23330 /* Check for the `__extension__' keyword. */
23331 if (cp_parser_extension_opt (parser, &saved_pedantic))
23333 /* Recurse. */
23334 cp_parser_member_declaration (parser);
23335 /* Restore the old value of the PEDANTIC flag. */
23336 pedantic = saved_pedantic;
23338 return;
23341 /* Check for a template-declaration. */
23342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23344 /* An explicit specialization here is an error condition, and we
23345 expect the specialization handler to detect and report this. */
23346 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23347 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23348 cp_parser_explicit_specialization (parser);
23349 else
23350 cp_parser_template_declaration (parser, /*member_p=*/true);
23352 return;
23354 /* Check for a template introduction. */
23355 else if (cp_parser_template_declaration_after_export (parser, true))
23356 return;
23358 /* Check for a using-declaration. */
23359 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23361 if (cxx_dialect < cxx11)
23363 /* Parse the using-declaration. */
23364 cp_parser_using_declaration (parser,
23365 /*access_declaration_p=*/false);
23366 return;
23368 else
23370 tree decl;
23371 bool alias_decl_expected;
23372 cp_parser_parse_tentatively (parser);
23373 decl = cp_parser_alias_declaration (parser);
23374 /* Note that if we actually see the '=' token after the
23375 identifier, cp_parser_alias_declaration commits the
23376 tentative parse. In that case, we really expect an
23377 alias-declaration. Otherwise, we expect a using
23378 declaration. */
23379 alias_decl_expected =
23380 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23381 cp_parser_parse_definitely (parser);
23383 if (alias_decl_expected)
23384 finish_member_declaration (decl);
23385 else
23386 cp_parser_using_declaration (parser,
23387 /*access_declaration_p=*/false);
23388 return;
23392 /* Check for @defs. */
23393 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23395 tree ivar, member;
23396 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23397 ivar = ivar_chains;
23398 while (ivar)
23400 member = ivar;
23401 ivar = TREE_CHAIN (member);
23402 TREE_CHAIN (member) = NULL_TREE;
23403 finish_member_declaration (member);
23405 return;
23408 /* If the next token is `static_assert' we have a static assertion. */
23409 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23411 cp_parser_static_assert (parser, /*member_p=*/true);
23412 return;
23415 parser->colon_corrects_to_scope_p = false;
23417 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23418 goto out;
23420 /* Parse the decl-specifier-seq. */
23421 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23422 cp_parser_decl_specifier_seq (parser,
23423 CP_PARSER_FLAGS_OPTIONAL,
23424 &decl_specifiers,
23425 &declares_class_or_enum);
23426 /* Check for an invalid type-name. */
23427 if (!decl_specifiers.any_type_specifiers_p
23428 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23429 goto out;
23430 /* If there is no declarator, then the decl-specifier-seq should
23431 specify a type. */
23432 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23434 /* If there was no decl-specifier-seq, and the next token is a
23435 `;', then we have something like:
23437 struct S { ; };
23439 [class.mem]
23441 Each member-declaration shall declare at least one member
23442 name of the class. */
23443 if (!decl_specifiers.any_specifiers_p)
23445 cp_token *token = cp_lexer_peek_token (parser->lexer);
23446 if (!in_system_header_at (token->location))
23448 gcc_rich_location richloc (token->location);
23449 richloc.add_fixit_remove ();
23450 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23453 else
23455 tree type;
23457 /* See if this declaration is a friend. */
23458 friend_p = cp_parser_friend_p (&decl_specifiers);
23459 /* If there were decl-specifiers, check to see if there was
23460 a class-declaration. */
23461 type = check_tag_decl (&decl_specifiers,
23462 /*explicit_type_instantiation_p=*/false);
23463 /* Nested classes have already been added to the class, but
23464 a `friend' needs to be explicitly registered. */
23465 if (friend_p)
23467 /* If the `friend' keyword was present, the friend must
23468 be introduced with a class-key. */
23469 if (!declares_class_or_enum && cxx_dialect < cxx11)
23470 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23471 "in C++03 a class-key must be used "
23472 "when declaring a friend");
23473 /* In this case:
23475 template <typename T> struct A {
23476 friend struct A<T>::B;
23479 A<T>::B will be represented by a TYPENAME_TYPE, and
23480 therefore not recognized by check_tag_decl. */
23481 if (!type)
23483 type = decl_specifiers.type;
23484 if (type && TREE_CODE (type) == TYPE_DECL)
23485 type = TREE_TYPE (type);
23487 if (!type || !TYPE_P (type))
23488 error_at (decl_spec_token_start->location,
23489 "friend declaration does not name a class or "
23490 "function");
23491 else
23492 make_friend_class (current_class_type, type,
23493 /*complain=*/true);
23495 /* If there is no TYPE, an error message will already have
23496 been issued. */
23497 else if (!type || type == error_mark_node)
23499 /* An anonymous aggregate has to be handled specially; such
23500 a declaration really declares a data member (with a
23501 particular type), as opposed to a nested class. */
23502 else if (ANON_AGGR_TYPE_P (type))
23504 /* C++11 9.5/6. */
23505 if (decl_specifiers.storage_class != sc_none)
23506 error_at (decl_spec_token_start->location,
23507 "a storage class on an anonymous aggregate "
23508 "in class scope is not allowed");
23510 /* Remove constructors and such from TYPE, now that we
23511 know it is an anonymous aggregate. */
23512 fixup_anonymous_aggr (type);
23513 /* And make the corresponding data member. */
23514 decl = build_decl (decl_spec_token_start->location,
23515 FIELD_DECL, NULL_TREE, type);
23516 /* Add it to the class. */
23517 finish_member_declaration (decl);
23519 else
23520 cp_parser_check_access_in_redeclaration
23521 (TYPE_NAME (type),
23522 decl_spec_token_start->location);
23525 else
23527 bool assume_semicolon = false;
23529 /* Clear attributes from the decl_specifiers but keep them
23530 around as prefix attributes that apply them to the entity
23531 being declared. */
23532 prefix_attributes = decl_specifiers.attributes;
23533 decl_specifiers.attributes = NULL_TREE;
23535 /* See if these declarations will be friends. */
23536 friend_p = cp_parser_friend_p (&decl_specifiers);
23538 /* Keep going until we hit the `;' at the end of the
23539 declaration. */
23540 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23542 tree attributes = NULL_TREE;
23543 tree first_attribute;
23544 tree initializer;
23545 bool is_bitfld = false;
23546 bool named_bitfld = false;
23548 /* Peek at the next token. */
23549 token = cp_lexer_peek_token (parser->lexer);
23551 /* The following code wants to know early if it is a bit-field
23552 or some other declaration. Attributes can appear before
23553 the `:' token, but are hopefully rare enough that the
23554 simplicity of the tentative lookup pays off. */
23555 if (cp_next_tokens_can_be_attribute_p (parser)
23556 || (token->type == CPP_NAME
23557 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23558 && (named_bitfld = true)))
23560 cp_parser_parse_tentatively (parser);
23561 if (named_bitfld)
23562 cp_lexer_consume_token (parser->lexer);
23563 cp_parser_attributes_opt (parser);
23564 token = cp_lexer_peek_token (parser->lexer);
23565 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23566 cp_parser_abort_tentative_parse (parser);
23569 /* Check for a bitfield declaration. */
23570 if (is_bitfld
23571 || token->type == CPP_COLON
23572 || (token->type == CPP_NAME
23573 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23574 && (named_bitfld = true)))
23576 tree identifier;
23577 tree width;
23578 tree late_attributes = NULL_TREE;
23580 if (named_bitfld)
23581 identifier = cp_parser_identifier (parser);
23582 else
23583 identifier = NULL_TREE;
23585 /* Look for attributes that apply to the bitfield. */
23586 attributes = cp_parser_attributes_opt (parser);
23588 /* Consume the `:' token. */
23589 cp_lexer_consume_token (parser->lexer);
23591 /* Get the width of the bitfield. */
23592 width = cp_parser_constant_expression (parser, false, NULL,
23593 cxx_dialect >= cxx11);
23595 /* In C++2A and as extension for C++11 and above we allow
23596 default member initializers for bit-fields. */
23597 initializer = NULL_TREE;
23598 if (cxx_dialect >= cxx11
23599 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23600 || cp_lexer_next_token_is (parser->lexer,
23601 CPP_OPEN_BRACE)))
23603 location_t loc
23604 = cp_lexer_peek_token (parser->lexer)->location;
23605 if (cxx_dialect < cxx2a
23606 && !in_system_header_at (loc)
23607 && identifier != NULL_TREE)
23608 pedwarn (loc, 0,
23609 "default member initializers for bit-fields "
23610 "only available with -std=c++2a or "
23611 "-std=gnu++2a");
23613 initializer = cp_parser_save_nsdmi (parser);
23614 if (identifier == NULL_TREE)
23616 error_at (loc, "default member initializer for "
23617 "unnamed bit-field");
23618 initializer = NULL_TREE;
23621 else
23623 /* Look for attributes that apply to the bitfield after
23624 the `:' token and width. This is where GCC used to
23625 parse attributes in the past, pedwarn if there is
23626 a std attribute. */
23627 if (cp_next_tokens_can_be_std_attribute_p (parser))
23628 pedwarn (input_location, OPT_Wpedantic,
23629 "ISO C++ allows bit-field attributes only "
23630 "before the %<:%> token");
23632 late_attributes = cp_parser_attributes_opt (parser);
23635 attributes = chainon (attributes, late_attributes);
23637 /* Remember which attributes are prefix attributes and
23638 which are not. */
23639 first_attribute = attributes;
23640 /* Combine the attributes. */
23641 attributes = chainon (prefix_attributes, attributes);
23643 /* Create the bitfield declaration. */
23644 decl = grokbitfield (identifier
23645 ? make_id_declarator (NULL_TREE,
23646 identifier,
23647 sfk_none)
23648 : NULL,
23649 &decl_specifiers,
23650 width, initializer,
23651 attributes);
23653 else
23655 cp_declarator *declarator;
23656 tree asm_specification;
23657 int ctor_dtor_or_conv_p;
23659 /* Parse the declarator. */
23660 declarator
23661 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23662 &ctor_dtor_or_conv_p,
23663 /*parenthesized_p=*/NULL,
23664 /*member_p=*/true,
23665 friend_p);
23667 /* If something went wrong parsing the declarator, make sure
23668 that we at least consume some tokens. */
23669 if (declarator == cp_error_declarator)
23671 /* Skip to the end of the statement. */
23672 cp_parser_skip_to_end_of_statement (parser);
23673 /* If the next token is not a semicolon, that is
23674 probably because we just skipped over the body of
23675 a function. So, we consume a semicolon if
23676 present, but do not issue an error message if it
23677 is not present. */
23678 if (cp_lexer_next_token_is (parser->lexer,
23679 CPP_SEMICOLON))
23680 cp_lexer_consume_token (parser->lexer);
23681 goto out;
23684 if (declares_class_or_enum & 2)
23685 cp_parser_check_for_definition_in_return_type
23686 (declarator, decl_specifiers.type,
23687 decl_specifiers.locations[ds_type_spec]);
23689 /* Look for an asm-specification. */
23690 asm_specification = cp_parser_asm_specification_opt (parser);
23691 /* Look for attributes that apply to the declaration. */
23692 attributes = cp_parser_attributes_opt (parser);
23693 /* Remember which attributes are prefix attributes and
23694 which are not. */
23695 first_attribute = attributes;
23696 /* Combine the attributes. */
23697 attributes = chainon (prefix_attributes, attributes);
23699 /* If it's an `=', then we have a constant-initializer or a
23700 pure-specifier. It is not correct to parse the
23701 initializer before registering the member declaration
23702 since the member declaration should be in scope while
23703 its initializer is processed. However, the rest of the
23704 front end does not yet provide an interface that allows
23705 us to handle this correctly. */
23706 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23708 /* In [class.mem]:
23710 A pure-specifier shall be used only in the declaration of
23711 a virtual function.
23713 A member-declarator can contain a constant-initializer
23714 only if it declares a static member of integral or
23715 enumeration type.
23717 Therefore, if the DECLARATOR is for a function, we look
23718 for a pure-specifier; otherwise, we look for a
23719 constant-initializer. When we call `grokfield', it will
23720 perform more stringent semantics checks. */
23721 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23722 if (function_declarator_p (declarator)
23723 || (decl_specifiers.type
23724 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23725 && declarator->kind == cdk_id
23726 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23727 == FUNCTION_TYPE)))
23728 initializer = cp_parser_pure_specifier (parser);
23729 else if (decl_specifiers.storage_class != sc_static)
23730 initializer = cp_parser_save_nsdmi (parser);
23731 else if (cxx_dialect >= cxx11)
23733 bool nonconst;
23734 /* Don't require a constant rvalue in C++11, since we
23735 might want a reference constant. We'll enforce
23736 constancy later. */
23737 cp_lexer_consume_token (parser->lexer);
23738 /* Parse the initializer. */
23739 initializer = cp_parser_initializer_clause (parser,
23740 &nonconst);
23742 else
23743 /* Parse the initializer. */
23744 initializer = cp_parser_constant_initializer (parser);
23746 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23747 && !function_declarator_p (declarator))
23749 bool x;
23750 if (decl_specifiers.storage_class != sc_static)
23751 initializer = cp_parser_save_nsdmi (parser);
23752 else
23753 initializer = cp_parser_initializer (parser, &x, &x);
23755 /* Otherwise, there is no initializer. */
23756 else
23757 initializer = NULL_TREE;
23759 /* See if we are probably looking at a function
23760 definition. We are certainly not looking at a
23761 member-declarator. Calling `grokfield' has
23762 side-effects, so we must not do it unless we are sure
23763 that we are looking at a member-declarator. */
23764 if (cp_parser_token_starts_function_definition_p
23765 (cp_lexer_peek_token (parser->lexer)))
23767 /* The grammar does not allow a pure-specifier to be
23768 used when a member function is defined. (It is
23769 possible that this fact is an oversight in the
23770 standard, since a pure function may be defined
23771 outside of the class-specifier. */
23772 if (initializer && initializer_token_start)
23773 error_at (initializer_token_start->location,
23774 "pure-specifier on function-definition");
23775 decl = cp_parser_save_member_function_body (parser,
23776 &decl_specifiers,
23777 declarator,
23778 attributes);
23779 if (parser->fully_implicit_function_template_p)
23780 decl = finish_fully_implicit_template (parser, decl);
23781 /* If the member was not a friend, declare it here. */
23782 if (!friend_p)
23783 finish_member_declaration (decl);
23784 /* Peek at the next token. */
23785 token = cp_lexer_peek_token (parser->lexer);
23786 /* If the next token is a semicolon, consume it. */
23787 if (token->type == CPP_SEMICOLON)
23789 location_t semicolon_loc
23790 = cp_lexer_consume_token (parser->lexer)->location;
23791 gcc_rich_location richloc (semicolon_loc);
23792 richloc.add_fixit_remove ();
23793 warning_at (&richloc, OPT_Wextra_semi,
23794 "extra %<;%> after in-class "
23795 "function definition");
23797 goto out;
23799 else
23800 if (declarator->kind == cdk_function)
23801 declarator->id_loc = token->location;
23802 /* Create the declaration. */
23803 decl = grokfield (declarator, &decl_specifiers,
23804 initializer, /*init_const_expr_p=*/true,
23805 asm_specification, attributes);
23806 if (parser->fully_implicit_function_template_p)
23808 if (friend_p)
23809 finish_fully_implicit_template (parser, 0);
23810 else
23811 decl = finish_fully_implicit_template (parser, decl);
23815 cp_finalize_omp_declare_simd (parser, decl);
23816 cp_finalize_oacc_routine (parser, decl, false);
23818 /* Reset PREFIX_ATTRIBUTES. */
23819 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23820 attributes = TREE_CHAIN (attributes);
23821 if (attributes)
23822 TREE_CHAIN (attributes) = NULL_TREE;
23824 /* If there is any qualification still in effect, clear it
23825 now; we will be starting fresh with the next declarator. */
23826 parser->scope = NULL_TREE;
23827 parser->qualifying_scope = NULL_TREE;
23828 parser->object_scope = NULL_TREE;
23829 /* If it's a `,', then there are more declarators. */
23830 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23832 cp_lexer_consume_token (parser->lexer);
23833 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23835 cp_token *token = cp_lexer_previous_token (parser->lexer);
23836 gcc_rich_location richloc (token->location);
23837 richloc.add_fixit_remove ();
23838 error_at (&richloc, "stray %<,%> at end of "
23839 "member declaration");
23842 /* If the next token isn't a `;', then we have a parse error. */
23843 else if (cp_lexer_next_token_is_not (parser->lexer,
23844 CPP_SEMICOLON))
23846 /* The next token might be a ways away from where the
23847 actual semicolon is missing. Find the previous token
23848 and use that for our error position. */
23849 cp_token *token = cp_lexer_previous_token (parser->lexer);
23850 gcc_rich_location richloc (token->location);
23851 richloc.add_fixit_insert_after (";");
23852 error_at (&richloc, "expected %<;%> at end of "
23853 "member declaration");
23855 /* Assume that the user meant to provide a semicolon. If
23856 we were to cp_parser_skip_to_end_of_statement, we might
23857 skip to a semicolon inside a member function definition
23858 and issue nonsensical error messages. */
23859 assume_semicolon = true;
23862 if (decl)
23864 /* Add DECL to the list of members. */
23865 if (!friend_p
23866 /* Explicitly include, eg, NSDMIs, for better error
23867 recovery (c++/58650). */
23868 || !DECL_DECLARES_FUNCTION_P (decl))
23869 finish_member_declaration (decl);
23871 if (TREE_CODE (decl) == FUNCTION_DECL)
23872 cp_parser_save_default_args (parser, decl);
23873 else if (TREE_CODE (decl) == FIELD_DECL
23874 && DECL_INITIAL (decl))
23875 /* Add DECL to the queue of NSDMI to be parsed later. */
23876 vec_safe_push (unparsed_nsdmis, decl);
23879 if (assume_semicolon)
23880 goto out;
23884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23885 out:
23886 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23889 /* Parse a pure-specifier.
23891 pure-specifier:
23894 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23895 Otherwise, ERROR_MARK_NODE is returned. */
23897 static tree
23898 cp_parser_pure_specifier (cp_parser* parser)
23900 cp_token *token;
23902 /* Look for the `=' token. */
23903 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23904 return error_mark_node;
23905 /* Look for the `0' token. */
23906 token = cp_lexer_peek_token (parser->lexer);
23908 if (token->type == CPP_EOF
23909 || token->type == CPP_PRAGMA_EOL)
23910 return error_mark_node;
23912 cp_lexer_consume_token (parser->lexer);
23914 /* Accept = default or = delete in c++0x mode. */
23915 if (token->keyword == RID_DEFAULT
23916 || token->keyword == RID_DELETE)
23918 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23919 return token->u.value;
23922 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23923 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23925 cp_parser_error (parser,
23926 "invalid pure specifier (only %<= 0%> is allowed)");
23927 cp_parser_skip_to_end_of_statement (parser);
23928 return error_mark_node;
23930 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23932 error_at (token->location, "templates may not be %<virtual%>");
23933 return error_mark_node;
23936 return integer_zero_node;
23939 /* Parse a constant-initializer.
23941 constant-initializer:
23942 = constant-expression
23944 Returns a representation of the constant-expression. */
23946 static tree
23947 cp_parser_constant_initializer (cp_parser* parser)
23949 /* Look for the `=' token. */
23950 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23951 return error_mark_node;
23953 /* It is invalid to write:
23955 struct S { static const int i = { 7 }; };
23958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23960 cp_parser_error (parser,
23961 "a brace-enclosed initializer is not allowed here");
23962 /* Consume the opening brace. */
23963 matching_braces braces;
23964 braces.consume_open (parser);
23965 /* Skip the initializer. */
23966 cp_parser_skip_to_closing_brace (parser);
23967 /* Look for the trailing `}'. */
23968 braces.require_close (parser);
23970 return error_mark_node;
23973 return cp_parser_constant_expression (parser);
23976 /* Derived classes [gram.class.derived] */
23978 /* Parse a base-clause.
23980 base-clause:
23981 : base-specifier-list
23983 base-specifier-list:
23984 base-specifier ... [opt]
23985 base-specifier-list , base-specifier ... [opt]
23987 Returns a TREE_LIST representing the base-classes, in the order in
23988 which they were declared. The representation of each node is as
23989 described by cp_parser_base_specifier.
23991 In the case that no bases are specified, this function will return
23992 NULL_TREE, not ERROR_MARK_NODE. */
23994 static tree
23995 cp_parser_base_clause (cp_parser* parser)
23997 tree bases = NULL_TREE;
23999 /* Look for the `:' that begins the list. */
24000 cp_parser_require (parser, CPP_COLON, RT_COLON);
24002 /* Scan the base-specifier-list. */
24003 while (true)
24005 cp_token *token;
24006 tree base;
24007 bool pack_expansion_p = false;
24009 /* Look for the base-specifier. */
24010 base = cp_parser_base_specifier (parser);
24011 /* Look for the (optional) ellipsis. */
24012 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24014 /* Consume the `...'. */
24015 cp_lexer_consume_token (parser->lexer);
24017 pack_expansion_p = true;
24020 /* Add BASE to the front of the list. */
24021 if (base && base != error_mark_node)
24023 if (pack_expansion_p)
24024 /* Make this a pack expansion type. */
24025 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24027 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24029 TREE_CHAIN (base) = bases;
24030 bases = base;
24033 /* Peek at the next token. */
24034 token = cp_lexer_peek_token (parser->lexer);
24035 /* If it's not a comma, then the list is complete. */
24036 if (token->type != CPP_COMMA)
24037 break;
24038 /* Consume the `,'. */
24039 cp_lexer_consume_token (parser->lexer);
24042 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24043 base class had a qualified name. However, the next name that
24044 appears is certainly not qualified. */
24045 parser->scope = NULL_TREE;
24046 parser->qualifying_scope = NULL_TREE;
24047 parser->object_scope = NULL_TREE;
24049 return nreverse (bases);
24052 /* Parse a base-specifier.
24054 base-specifier:
24055 :: [opt] nested-name-specifier [opt] class-name
24056 virtual access-specifier [opt] :: [opt] nested-name-specifier
24057 [opt] class-name
24058 access-specifier virtual [opt] :: [opt] nested-name-specifier
24059 [opt] class-name
24061 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24062 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24063 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24064 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24066 static tree
24067 cp_parser_base_specifier (cp_parser* parser)
24069 cp_token *token;
24070 bool done = false;
24071 bool virtual_p = false;
24072 bool duplicate_virtual_error_issued_p = false;
24073 bool duplicate_access_error_issued_p = false;
24074 bool class_scope_p, template_p;
24075 tree access = access_default_node;
24076 tree type;
24078 /* Process the optional `virtual' and `access-specifier'. */
24079 while (!done)
24081 /* Peek at the next token. */
24082 token = cp_lexer_peek_token (parser->lexer);
24083 /* Process `virtual'. */
24084 switch (token->keyword)
24086 case RID_VIRTUAL:
24087 /* If `virtual' appears more than once, issue an error. */
24088 if (virtual_p && !duplicate_virtual_error_issued_p)
24090 cp_parser_error (parser,
24091 "%<virtual%> specified more than once in base-specifier");
24092 duplicate_virtual_error_issued_p = true;
24095 virtual_p = true;
24097 /* Consume the `virtual' token. */
24098 cp_lexer_consume_token (parser->lexer);
24100 break;
24102 case RID_PUBLIC:
24103 case RID_PROTECTED:
24104 case RID_PRIVATE:
24105 /* If more than one access specifier appears, issue an
24106 error. */
24107 if (access != access_default_node
24108 && !duplicate_access_error_issued_p)
24110 cp_parser_error (parser,
24111 "more than one access specifier in base-specifier");
24112 duplicate_access_error_issued_p = true;
24115 access = ridpointers[(int) token->keyword];
24117 /* Consume the access-specifier. */
24118 cp_lexer_consume_token (parser->lexer);
24120 break;
24122 default:
24123 done = true;
24124 break;
24127 /* It is not uncommon to see programs mechanically, erroneously, use
24128 the 'typename' keyword to denote (dependent) qualified types
24129 as base classes. */
24130 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24132 token = cp_lexer_peek_token (parser->lexer);
24133 if (!processing_template_decl)
24134 error_at (token->location,
24135 "keyword %<typename%> not allowed outside of templates");
24136 else
24137 error_at (token->location,
24138 "keyword %<typename%> not allowed in this context "
24139 "(the base class is implicitly a type)");
24140 cp_lexer_consume_token (parser->lexer);
24143 /* Look for the optional `::' operator. */
24144 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24145 /* Look for the nested-name-specifier. The simplest way to
24146 implement:
24148 [temp.res]
24150 The keyword `typename' is not permitted in a base-specifier or
24151 mem-initializer; in these contexts a qualified name that
24152 depends on a template-parameter is implicitly assumed to be a
24153 type name.
24155 is to pretend that we have seen the `typename' keyword at this
24156 point. */
24157 cp_parser_nested_name_specifier_opt (parser,
24158 /*typename_keyword_p=*/true,
24159 /*check_dependency_p=*/true,
24160 /*type_p=*/true,
24161 /*is_declaration=*/true);
24162 /* If the base class is given by a qualified name, assume that names
24163 we see are type names or templates, as appropriate. */
24164 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24165 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24167 if (!parser->scope
24168 && cp_lexer_next_token_is_decltype (parser->lexer))
24169 /* DR 950 allows decltype as a base-specifier. */
24170 type = cp_parser_decltype (parser);
24171 else
24173 /* Otherwise, look for the class-name. */
24174 type = cp_parser_class_name (parser,
24175 class_scope_p,
24176 template_p,
24177 typename_type,
24178 /*check_dependency_p=*/true,
24179 /*class_head_p=*/false,
24180 /*is_declaration=*/true);
24181 type = TREE_TYPE (type);
24184 if (type == error_mark_node)
24185 return error_mark_node;
24187 return finish_base_specifier (type, access, virtual_p);
24190 /* Exception handling [gram.exception] */
24192 /* Parse an (optional) noexcept-specification.
24194 noexcept-specification:
24195 noexcept ( constant-expression ) [opt]
24197 If no noexcept-specification is present, returns NULL_TREE.
24198 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24199 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24200 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24201 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24202 in which case a boolean condition is returned instead. */
24204 static tree
24205 cp_parser_noexcept_specification_opt (cp_parser* parser,
24206 bool require_constexpr,
24207 bool* consumed_expr,
24208 bool return_cond)
24210 cp_token *token;
24211 const char *saved_message;
24213 /* Peek at the next token. */
24214 token = cp_lexer_peek_token (parser->lexer);
24216 /* Is it a noexcept-specification? */
24217 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24219 tree expr;
24220 cp_lexer_consume_token (parser->lexer);
24222 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24224 matching_parens parens;
24225 parens.consume_open (parser);
24227 if (require_constexpr)
24229 /* Types may not be defined in an exception-specification. */
24230 saved_message = parser->type_definition_forbidden_message;
24231 parser->type_definition_forbidden_message
24232 = G_("types may not be defined in an exception-specification");
24234 expr = cp_parser_constant_expression (parser);
24236 /* Restore the saved message. */
24237 parser->type_definition_forbidden_message = saved_message;
24239 else
24241 expr = cp_parser_expression (parser);
24242 *consumed_expr = true;
24245 parens.require_close (parser);
24247 else
24249 expr = boolean_true_node;
24250 if (!require_constexpr)
24251 *consumed_expr = false;
24254 /* We cannot build a noexcept-spec right away because this will check
24255 that expr is a constexpr. */
24256 if (!return_cond)
24257 return build_noexcept_spec (expr, tf_warning_or_error);
24258 else
24259 return expr;
24261 else
24262 return NULL_TREE;
24265 /* Parse an (optional) exception-specification.
24267 exception-specification:
24268 throw ( type-id-list [opt] )
24270 Returns a TREE_LIST representing the exception-specification. The
24271 TREE_VALUE of each node is a type. */
24273 static tree
24274 cp_parser_exception_specification_opt (cp_parser* parser)
24276 cp_token *token;
24277 tree type_id_list;
24278 const char *saved_message;
24280 /* Peek at the next token. */
24281 token = cp_lexer_peek_token (parser->lexer);
24283 /* Is it a noexcept-specification? */
24284 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24285 false);
24286 if (type_id_list != NULL_TREE)
24287 return type_id_list;
24289 /* If it's not `throw', then there's no exception-specification. */
24290 if (!cp_parser_is_keyword (token, RID_THROW))
24291 return NULL_TREE;
24293 location_t loc = token->location;
24295 /* Consume the `throw'. */
24296 cp_lexer_consume_token (parser->lexer);
24298 /* Look for the `('. */
24299 matching_parens parens;
24300 parens.require_open (parser);
24302 /* Peek at the next token. */
24303 token = cp_lexer_peek_token (parser->lexer);
24304 /* If it's not a `)', then there is a type-id-list. */
24305 if (token->type != CPP_CLOSE_PAREN)
24307 /* Types may not be defined in an exception-specification. */
24308 saved_message = parser->type_definition_forbidden_message;
24309 parser->type_definition_forbidden_message
24310 = G_("types may not be defined in an exception-specification");
24311 /* Parse the type-id-list. */
24312 type_id_list = cp_parser_type_id_list (parser);
24313 /* Restore the saved message. */
24314 parser->type_definition_forbidden_message = saved_message;
24316 if (cxx_dialect >= cxx17)
24318 error_at (loc, "ISO C++17 does not allow dynamic exception "
24319 "specifications");
24320 type_id_list = NULL_TREE;
24322 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24323 warning_at (loc, OPT_Wdeprecated,
24324 "dynamic exception specifications are deprecated in "
24325 "C++11");
24327 /* In C++17, throw() is equivalent to noexcept (true). throw()
24328 is deprecated in C++11 and above as well, but is still widely used,
24329 so don't warn about it yet. */
24330 else if (cxx_dialect >= cxx17)
24331 type_id_list = noexcept_true_spec;
24332 else
24333 type_id_list = empty_except_spec;
24335 /* Look for the `)'. */
24336 parens.require_close (parser);
24338 return type_id_list;
24341 /* Parse an (optional) type-id-list.
24343 type-id-list:
24344 type-id ... [opt]
24345 type-id-list , type-id ... [opt]
24347 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24348 in the order that the types were presented. */
24350 static tree
24351 cp_parser_type_id_list (cp_parser* parser)
24353 tree types = NULL_TREE;
24355 while (true)
24357 cp_token *token;
24358 tree type;
24360 token = cp_lexer_peek_token (parser->lexer);
24362 /* Get the next type-id. */
24363 type = cp_parser_type_id (parser);
24364 /* Check for invalid 'auto'. */
24365 if (flag_concepts && type_uses_auto (type))
24367 error_at (token->location,
24368 "invalid use of %<auto%> in exception-specification");
24369 type = error_mark_node;
24371 /* Parse the optional ellipsis. */
24372 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24374 /* Consume the `...'. */
24375 cp_lexer_consume_token (parser->lexer);
24377 /* Turn the type into a pack expansion expression. */
24378 type = make_pack_expansion (type);
24380 /* Add it to the list. */
24381 types = add_exception_specifier (types, type, /*complain=*/1);
24382 /* Peek at the next token. */
24383 token = cp_lexer_peek_token (parser->lexer);
24384 /* If it is not a `,', we are done. */
24385 if (token->type != CPP_COMMA)
24386 break;
24387 /* Consume the `,'. */
24388 cp_lexer_consume_token (parser->lexer);
24391 return nreverse (types);
24394 /* Parse a try-block.
24396 try-block:
24397 try compound-statement handler-seq */
24399 static tree
24400 cp_parser_try_block (cp_parser* parser)
24402 tree try_block;
24404 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24405 if (parser->in_function_body
24406 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24407 error ("%<try%> in %<constexpr%> function");
24409 try_block = begin_try_block ();
24410 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24411 finish_try_block (try_block);
24412 cp_parser_handler_seq (parser);
24413 finish_handler_sequence (try_block);
24415 return try_block;
24418 /* Parse a function-try-block.
24420 function-try-block:
24421 try ctor-initializer [opt] function-body handler-seq */
24423 static void
24424 cp_parser_function_try_block (cp_parser* parser)
24426 tree compound_stmt;
24427 tree try_block;
24429 /* Look for the `try' keyword. */
24430 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24431 return;
24432 /* Let the rest of the front end know where we are. */
24433 try_block = begin_function_try_block (&compound_stmt);
24434 /* Parse the function-body. */
24435 cp_parser_ctor_initializer_opt_and_function_body
24436 (parser, /*in_function_try_block=*/true);
24437 /* We're done with the `try' part. */
24438 finish_function_try_block (try_block);
24439 /* Parse the handlers. */
24440 cp_parser_handler_seq (parser);
24441 /* We're done with the handlers. */
24442 finish_function_handler_sequence (try_block, compound_stmt);
24445 /* Parse a handler-seq.
24447 handler-seq:
24448 handler handler-seq [opt] */
24450 static void
24451 cp_parser_handler_seq (cp_parser* parser)
24453 while (true)
24455 cp_token *token;
24457 /* Parse the handler. */
24458 cp_parser_handler (parser);
24459 /* Peek at the next token. */
24460 token = cp_lexer_peek_token (parser->lexer);
24461 /* If it's not `catch' then there are no more handlers. */
24462 if (!cp_parser_is_keyword (token, RID_CATCH))
24463 break;
24467 /* Parse a handler.
24469 handler:
24470 catch ( exception-declaration ) compound-statement */
24472 static void
24473 cp_parser_handler (cp_parser* parser)
24475 tree handler;
24476 tree declaration;
24478 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24479 handler = begin_handler ();
24480 matching_parens parens;
24481 parens.require_open (parser);
24482 declaration = cp_parser_exception_declaration (parser);
24483 finish_handler_parms (declaration, handler);
24484 parens.require_close (parser);
24485 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24486 finish_handler (handler);
24489 /* Parse an exception-declaration.
24491 exception-declaration:
24492 type-specifier-seq declarator
24493 type-specifier-seq abstract-declarator
24494 type-specifier-seq
24497 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24498 ellipsis variant is used. */
24500 static tree
24501 cp_parser_exception_declaration (cp_parser* parser)
24503 cp_decl_specifier_seq type_specifiers;
24504 cp_declarator *declarator;
24505 const char *saved_message;
24507 /* If it's an ellipsis, it's easy to handle. */
24508 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24510 /* Consume the `...' token. */
24511 cp_lexer_consume_token (parser->lexer);
24512 return NULL_TREE;
24515 /* Types may not be defined in exception-declarations. */
24516 saved_message = parser->type_definition_forbidden_message;
24517 parser->type_definition_forbidden_message
24518 = G_("types may not be defined in exception-declarations");
24520 /* Parse the type-specifier-seq. */
24521 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24522 /*is_trailing_return=*/false,
24523 &type_specifiers);
24524 /* If it's a `)', then there is no declarator. */
24525 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24526 declarator = NULL;
24527 else
24528 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24529 /*ctor_dtor_or_conv_p=*/NULL,
24530 /*parenthesized_p=*/NULL,
24531 /*member_p=*/false,
24532 /*friend_p=*/false);
24534 /* Restore the saved message. */
24535 parser->type_definition_forbidden_message = saved_message;
24537 if (!type_specifiers.any_specifiers_p)
24538 return error_mark_node;
24540 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24543 /* Parse a throw-expression.
24545 throw-expression:
24546 throw assignment-expression [opt]
24548 Returns a THROW_EXPR representing the throw-expression. */
24550 static tree
24551 cp_parser_throw_expression (cp_parser* parser)
24553 tree expression;
24554 cp_token* token;
24556 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24557 token = cp_lexer_peek_token (parser->lexer);
24558 /* Figure out whether or not there is an assignment-expression
24559 following the "throw" keyword. */
24560 if (token->type == CPP_COMMA
24561 || token->type == CPP_SEMICOLON
24562 || token->type == CPP_CLOSE_PAREN
24563 || token->type == CPP_CLOSE_SQUARE
24564 || token->type == CPP_CLOSE_BRACE
24565 || token->type == CPP_COLON)
24566 expression = NULL_TREE;
24567 else
24568 expression = cp_parser_assignment_expression (parser);
24570 return build_throw (expression);
24573 /* GNU Extensions */
24575 /* Parse an (optional) asm-specification.
24577 asm-specification:
24578 asm ( string-literal )
24580 If the asm-specification is present, returns a STRING_CST
24581 corresponding to the string-literal. Otherwise, returns
24582 NULL_TREE. */
24584 static tree
24585 cp_parser_asm_specification_opt (cp_parser* parser)
24587 cp_token *token;
24588 tree asm_specification;
24590 /* Peek at the next token. */
24591 token = cp_lexer_peek_token (parser->lexer);
24592 /* If the next token isn't the `asm' keyword, then there's no
24593 asm-specification. */
24594 if (!cp_parser_is_keyword (token, RID_ASM))
24595 return NULL_TREE;
24597 /* Consume the `asm' token. */
24598 cp_lexer_consume_token (parser->lexer);
24599 /* Look for the `('. */
24600 matching_parens parens;
24601 parens.require_open (parser);
24603 /* Look for the string-literal. */
24604 asm_specification = cp_parser_string_literal (parser, false, false);
24606 /* Look for the `)'. */
24607 parens.require_close (parser);
24609 return asm_specification;
24612 /* Parse an asm-operand-list.
24614 asm-operand-list:
24615 asm-operand
24616 asm-operand-list , asm-operand
24618 asm-operand:
24619 string-literal ( expression )
24620 [ string-literal ] string-literal ( expression )
24622 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24623 each node is the expression. The TREE_PURPOSE is itself a
24624 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24625 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24626 is a STRING_CST for the string literal before the parenthesis. Returns
24627 ERROR_MARK_NODE if any of the operands are invalid. */
24629 static tree
24630 cp_parser_asm_operand_list (cp_parser* parser)
24632 tree asm_operands = NULL_TREE;
24633 bool invalid_operands = false;
24635 while (true)
24637 tree string_literal;
24638 tree expression;
24639 tree name;
24641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24643 /* Consume the `[' token. */
24644 cp_lexer_consume_token (parser->lexer);
24645 /* Read the operand name. */
24646 name = cp_parser_identifier (parser);
24647 if (name != error_mark_node)
24648 name = build_string (IDENTIFIER_LENGTH (name),
24649 IDENTIFIER_POINTER (name));
24650 /* Look for the closing `]'. */
24651 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24653 else
24654 name = NULL_TREE;
24655 /* Look for the string-literal. */
24656 string_literal = cp_parser_string_literal (parser, false, false);
24658 /* Look for the `('. */
24659 matching_parens parens;
24660 parens.require_open (parser);
24661 /* Parse the expression. */
24662 expression = cp_parser_expression (parser);
24663 /* Look for the `)'. */
24664 parens.require_close (parser);
24666 if (name == error_mark_node
24667 || string_literal == error_mark_node
24668 || expression == error_mark_node)
24669 invalid_operands = true;
24671 /* Add this operand to the list. */
24672 asm_operands = tree_cons (build_tree_list (name, string_literal),
24673 expression,
24674 asm_operands);
24675 /* If the next token is not a `,', there are no more
24676 operands. */
24677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24678 break;
24679 /* Consume the `,'. */
24680 cp_lexer_consume_token (parser->lexer);
24683 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24686 /* Parse an asm-clobber-list.
24688 asm-clobber-list:
24689 string-literal
24690 asm-clobber-list , string-literal
24692 Returns a TREE_LIST, indicating the clobbers in the order that they
24693 appeared. The TREE_VALUE of each node is a STRING_CST. */
24695 static tree
24696 cp_parser_asm_clobber_list (cp_parser* parser)
24698 tree clobbers = NULL_TREE;
24700 while (true)
24702 tree string_literal;
24704 /* Look for the string literal. */
24705 string_literal = cp_parser_string_literal (parser, false, false);
24706 /* Add it to the list. */
24707 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24708 /* If the next token is not a `,', then the list is
24709 complete. */
24710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24711 break;
24712 /* Consume the `,' token. */
24713 cp_lexer_consume_token (parser->lexer);
24716 return clobbers;
24719 /* Parse an asm-label-list.
24721 asm-label-list:
24722 identifier
24723 asm-label-list , identifier
24725 Returns a TREE_LIST, indicating the labels in the order that they
24726 appeared. The TREE_VALUE of each node is a label. */
24728 static tree
24729 cp_parser_asm_label_list (cp_parser* parser)
24731 tree labels = NULL_TREE;
24733 while (true)
24735 tree identifier, label, name;
24737 /* Look for the identifier. */
24738 identifier = cp_parser_identifier (parser);
24739 if (!error_operand_p (identifier))
24741 label = lookup_label (identifier);
24742 if (TREE_CODE (label) == LABEL_DECL)
24744 TREE_USED (label) = 1;
24745 check_goto (label);
24746 name = build_string (IDENTIFIER_LENGTH (identifier),
24747 IDENTIFIER_POINTER (identifier));
24748 labels = tree_cons (name, label, labels);
24751 /* If the next token is not a `,', then the list is
24752 complete. */
24753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24754 break;
24755 /* Consume the `,' token. */
24756 cp_lexer_consume_token (parser->lexer);
24759 return nreverse (labels);
24762 /* Return TRUE iff the next tokens in the stream are possibly the
24763 beginning of a GNU extension attribute. */
24765 static bool
24766 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24768 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24771 /* Return TRUE iff the next tokens in the stream are possibly the
24772 beginning of a standard C++-11 attribute specifier. */
24774 static bool
24775 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24777 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24780 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24781 beginning of a standard C++-11 attribute specifier. */
24783 static bool
24784 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24786 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24788 return (cxx_dialect >= cxx11
24789 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24790 || (token->type == CPP_OPEN_SQUARE
24791 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24792 && token->type == CPP_OPEN_SQUARE)));
24795 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24796 beginning of a GNU extension attribute. */
24798 static bool
24799 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24801 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24803 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24806 /* Return true iff the next tokens can be the beginning of either a
24807 GNU attribute list, or a standard C++11 attribute sequence. */
24809 static bool
24810 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24812 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24813 || cp_next_tokens_can_be_std_attribute_p (parser));
24816 /* Return true iff the next Nth tokens can be the beginning of either
24817 a GNU attribute list, or a standard C++11 attribute sequence. */
24819 static bool
24820 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24822 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24823 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24826 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24827 of GNU attributes, or return NULL. */
24829 static tree
24830 cp_parser_attributes_opt (cp_parser *parser)
24832 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24833 return cp_parser_gnu_attributes_opt (parser);
24834 return cp_parser_std_attribute_spec_seq (parser);
24837 /* Parse an (optional) series of attributes.
24839 attributes:
24840 attributes attribute
24842 attribute:
24843 __attribute__ (( attribute-list [opt] ))
24845 The return value is as for cp_parser_gnu_attribute_list. */
24847 static tree
24848 cp_parser_gnu_attributes_opt (cp_parser* parser)
24850 tree attributes = NULL_TREE;
24852 while (true)
24854 cp_token *token;
24855 tree attribute_list;
24856 bool ok = true;
24858 /* Peek at the next token. */
24859 token = cp_lexer_peek_token (parser->lexer);
24860 /* If it's not `__attribute__', then we're done. */
24861 if (token->keyword != RID_ATTRIBUTE)
24862 break;
24864 /* Consume the `__attribute__' keyword. */
24865 cp_lexer_consume_token (parser->lexer);
24866 /* Look for the two `(' tokens. */
24867 matching_parens outer_parens;
24868 outer_parens.require_open (parser);
24869 matching_parens inner_parens;
24870 inner_parens.require_open (parser);
24872 /* Peek at the next token. */
24873 token = cp_lexer_peek_token (parser->lexer);
24874 if (token->type != CPP_CLOSE_PAREN)
24875 /* Parse the attribute-list. */
24876 attribute_list = cp_parser_gnu_attribute_list (parser);
24877 else
24878 /* If the next token is a `)', then there is no attribute
24879 list. */
24880 attribute_list = NULL;
24882 /* Look for the two `)' tokens. */
24883 if (!inner_parens.require_close (parser))
24884 ok = false;
24885 if (!outer_parens.require_close (parser))
24886 ok = false;
24887 if (!ok)
24888 cp_parser_skip_to_end_of_statement (parser);
24890 /* Add these new attributes to the list. */
24891 attributes = chainon (attributes, attribute_list);
24894 return attributes;
24897 /* Parse a GNU attribute-list.
24899 attribute-list:
24900 attribute
24901 attribute-list , attribute
24903 attribute:
24904 identifier
24905 identifier ( identifier )
24906 identifier ( identifier , expression-list )
24907 identifier ( expression-list )
24909 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24910 to an attribute. The TREE_PURPOSE of each node is the identifier
24911 indicating which attribute is in use. The TREE_VALUE represents
24912 the arguments, if any. */
24914 static tree
24915 cp_parser_gnu_attribute_list (cp_parser* parser)
24917 tree attribute_list = NULL_TREE;
24918 bool save_translate_strings_p = parser->translate_strings_p;
24920 parser->translate_strings_p = false;
24921 while (true)
24923 cp_token *token;
24924 tree identifier;
24925 tree attribute;
24927 /* Look for the identifier. We also allow keywords here; for
24928 example `__attribute__ ((const))' is legal. */
24929 token = cp_lexer_peek_token (parser->lexer);
24930 if (token->type == CPP_NAME
24931 || token->type == CPP_KEYWORD)
24933 tree arguments = NULL_TREE;
24935 /* Consume the token, but save it since we need it for the
24936 SIMD enabled function parsing. */
24937 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24939 /* Save away the identifier that indicates which attribute
24940 this is. */
24941 identifier = (token->type == CPP_KEYWORD)
24942 /* For keywords, use the canonical spelling, not the
24943 parsed identifier. */
24944 ? ridpointers[(int) token->keyword]
24945 : id_token->u.value;
24947 identifier = canonicalize_attr_name (identifier);
24948 attribute = build_tree_list (identifier, NULL_TREE);
24950 /* Peek at the next token. */
24951 token = cp_lexer_peek_token (parser->lexer);
24952 /* If it's an `(', then parse the attribute arguments. */
24953 if (token->type == CPP_OPEN_PAREN)
24955 vec<tree, va_gc> *vec;
24956 int attr_flag = (attribute_takes_identifier_p (identifier)
24957 ? id_attr : normal_attr);
24958 vec = cp_parser_parenthesized_expression_list
24959 (parser, attr_flag, /*cast_p=*/false,
24960 /*allow_expansion_p=*/false,
24961 /*non_constant_p=*/NULL);
24962 if (vec == NULL)
24963 arguments = error_mark_node;
24964 else
24966 arguments = build_tree_list_vec (vec);
24967 release_tree_vector (vec);
24969 /* Save the arguments away. */
24970 TREE_VALUE (attribute) = arguments;
24973 if (arguments != error_mark_node)
24975 /* Add this attribute to the list. */
24976 TREE_CHAIN (attribute) = attribute_list;
24977 attribute_list = attribute;
24980 token = cp_lexer_peek_token (parser->lexer);
24982 /* Now, look for more attributes. If the next token isn't a
24983 `,', we're done. */
24984 if (token->type != CPP_COMMA)
24985 break;
24987 /* Consume the comma and keep going. */
24988 cp_lexer_consume_token (parser->lexer);
24990 parser->translate_strings_p = save_translate_strings_p;
24992 /* We built up the list in reverse order. */
24993 return nreverse (attribute_list);
24996 /* Parse a standard C++11 attribute.
24998 The returned representation is a TREE_LIST which TREE_PURPOSE is
24999 the scoped name of the attribute, and the TREE_VALUE is its
25000 arguments list.
25002 Note that the scoped name of the attribute is itself a TREE_LIST
25003 which TREE_PURPOSE is the namespace of the attribute, and
25004 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25005 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25006 and which TREE_PURPOSE is directly the attribute name.
25008 Clients of the attribute code should use get_attribute_namespace
25009 and get_attribute_name to get the actual namespace and name of
25010 attributes, regardless of their being GNU or C++11 attributes.
25012 attribute:
25013 attribute-token attribute-argument-clause [opt]
25015 attribute-token:
25016 identifier
25017 attribute-scoped-token
25019 attribute-scoped-token:
25020 attribute-namespace :: identifier
25022 attribute-namespace:
25023 identifier
25025 attribute-argument-clause:
25026 ( balanced-token-seq )
25028 balanced-token-seq:
25029 balanced-token [opt]
25030 balanced-token-seq balanced-token
25032 balanced-token:
25033 ( balanced-token-seq )
25034 [ balanced-token-seq ]
25035 { balanced-token-seq }. */
25037 static tree
25038 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25040 tree attribute, attr_id = NULL_TREE, arguments;
25041 cp_token *token;
25043 /* First, parse name of the attribute, a.k.a attribute-token. */
25045 token = cp_lexer_peek_token (parser->lexer);
25046 if (token->type == CPP_NAME)
25047 attr_id = token->u.value;
25048 else if (token->type == CPP_KEYWORD)
25049 attr_id = ridpointers[(int) token->keyword];
25050 else if (token->flags & NAMED_OP)
25051 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25053 if (attr_id == NULL_TREE)
25054 return NULL_TREE;
25056 cp_lexer_consume_token (parser->lexer);
25058 token = cp_lexer_peek_token (parser->lexer);
25059 if (token->type == CPP_SCOPE)
25061 /* We are seeing a scoped attribute token. */
25063 cp_lexer_consume_token (parser->lexer);
25064 if (attr_ns)
25065 error_at (token->location, "attribute using prefix used together "
25066 "with scoped attribute token");
25067 attr_ns = attr_id;
25069 token = cp_lexer_consume_token (parser->lexer);
25070 if (token->type == CPP_NAME)
25071 attr_id = token->u.value;
25072 else if (token->type == CPP_KEYWORD)
25073 attr_id = ridpointers[(int) token->keyword];
25074 else if (token->flags & NAMED_OP)
25075 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25076 else
25078 error_at (token->location,
25079 "expected an identifier for the attribute name");
25080 return error_mark_node;
25083 attr_id = canonicalize_attr_name (attr_id);
25084 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25085 NULL_TREE);
25086 token = cp_lexer_peek_token (parser->lexer);
25088 else if (attr_ns)
25089 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25090 NULL_TREE);
25091 else
25093 attr_id = canonicalize_attr_name (attr_id);
25094 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25095 NULL_TREE);
25096 /* C++11 noreturn attribute is equivalent to GNU's. */
25097 if (is_attribute_p ("noreturn", attr_id))
25098 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25099 /* C++14 deprecated attribute is equivalent to GNU's. */
25100 else if (is_attribute_p ("deprecated", attr_id))
25101 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25102 /* C++17 fallthrough attribute is equivalent to GNU's. */
25103 else if (is_attribute_p ("fallthrough", attr_id))
25104 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25105 /* Transactional Memory TS optimize_for_synchronized attribute is
25106 equivalent to GNU transaction_callable. */
25107 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25108 TREE_PURPOSE (attribute)
25109 = get_identifier ("transaction_callable");
25110 /* Transactional Memory attributes are GNU attributes. */
25111 else if (tm_attr_to_mask (attr_id))
25112 TREE_PURPOSE (attribute) = attr_id;
25115 /* Now parse the optional argument clause of the attribute. */
25117 if (token->type != CPP_OPEN_PAREN)
25118 return attribute;
25121 vec<tree, va_gc> *vec;
25122 int attr_flag = normal_attr;
25124 if (attr_ns == get_identifier ("gnu")
25125 && attribute_takes_identifier_p (attr_id))
25126 /* A GNU attribute that takes an identifier in parameter. */
25127 attr_flag = id_attr;
25129 vec = cp_parser_parenthesized_expression_list
25130 (parser, attr_flag, /*cast_p=*/false,
25131 /*allow_expansion_p=*/true,
25132 /*non_constant_p=*/NULL);
25133 if (vec == NULL)
25134 arguments = error_mark_node;
25135 else
25137 arguments = build_tree_list_vec (vec);
25138 release_tree_vector (vec);
25141 if (arguments == error_mark_node)
25142 attribute = error_mark_node;
25143 else
25144 TREE_VALUE (attribute) = arguments;
25147 return attribute;
25150 /* Check that the attribute ATTRIBUTE appears at most once in the
25151 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25152 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25153 isn't implemented yet in GCC. */
25155 static void
25156 cp_parser_check_std_attribute (tree attributes, tree attribute)
25158 if (attributes)
25160 tree name = get_attribute_name (attribute);
25161 if (is_attribute_p ("noreturn", name)
25162 && lookup_attribute ("noreturn", attributes))
25163 error ("attribute %<noreturn%> can appear at most once "
25164 "in an attribute-list");
25165 else if (is_attribute_p ("deprecated", name)
25166 && lookup_attribute ("deprecated", attributes))
25167 error ("attribute %<deprecated%> can appear at most once "
25168 "in an attribute-list");
25172 /* Parse a list of standard C++-11 attributes.
25174 attribute-list:
25175 attribute [opt]
25176 attribute-list , attribute[opt]
25177 attribute ...
25178 attribute-list , attribute ...
25181 static tree
25182 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25184 tree attributes = NULL_TREE, attribute = NULL_TREE;
25185 cp_token *token = NULL;
25187 while (true)
25189 attribute = cp_parser_std_attribute (parser, attr_ns);
25190 if (attribute == error_mark_node)
25191 break;
25192 if (attribute != NULL_TREE)
25194 cp_parser_check_std_attribute (attributes, attribute);
25195 TREE_CHAIN (attribute) = attributes;
25196 attributes = attribute;
25198 token = cp_lexer_peek_token (parser->lexer);
25199 if (token->type == CPP_ELLIPSIS)
25201 cp_lexer_consume_token (parser->lexer);
25202 if (attribute == NULL_TREE)
25203 error_at (token->location,
25204 "expected attribute before %<...%>");
25205 else
25207 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25208 if (pack == error_mark_node)
25209 return error_mark_node;
25210 TREE_VALUE (attribute) = pack;
25212 token = cp_lexer_peek_token (parser->lexer);
25214 if (token->type != CPP_COMMA)
25215 break;
25216 cp_lexer_consume_token (parser->lexer);
25218 attributes = nreverse (attributes);
25219 return attributes;
25222 /* Parse a standard C++-11 attribute specifier.
25224 attribute-specifier:
25225 [ [ attribute-using-prefix [opt] attribute-list ] ]
25226 alignment-specifier
25228 attribute-using-prefix:
25229 using attribute-namespace :
25231 alignment-specifier:
25232 alignas ( type-id ... [opt] )
25233 alignas ( alignment-expression ... [opt] ). */
25235 static tree
25236 cp_parser_std_attribute_spec (cp_parser *parser)
25238 tree attributes = NULL_TREE;
25239 cp_token *token = cp_lexer_peek_token (parser->lexer);
25241 if (token->type == CPP_OPEN_SQUARE
25242 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25244 tree attr_ns = NULL_TREE;
25246 cp_lexer_consume_token (parser->lexer);
25247 cp_lexer_consume_token (parser->lexer);
25249 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25251 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25252 if (token->type == CPP_NAME)
25253 attr_ns = token->u.value;
25254 else if (token->type == CPP_KEYWORD)
25255 attr_ns = ridpointers[(int) token->keyword];
25256 else if (token->flags & NAMED_OP)
25257 attr_ns = get_identifier (cpp_type2name (token->type,
25258 token->flags));
25259 if (attr_ns
25260 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25262 if (cxx_dialect < cxx17
25263 && !in_system_header_at (input_location))
25264 pedwarn (input_location, 0,
25265 "attribute using prefix only available "
25266 "with -std=c++17 or -std=gnu++17");
25268 cp_lexer_consume_token (parser->lexer);
25269 cp_lexer_consume_token (parser->lexer);
25270 cp_lexer_consume_token (parser->lexer);
25272 else
25273 attr_ns = NULL_TREE;
25276 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25278 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25279 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25280 cp_parser_skip_to_end_of_statement (parser);
25281 else
25282 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25283 when we are sure that we have actually parsed them. */
25284 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25286 else
25288 tree alignas_expr;
25290 /* Look for an alignment-specifier. */
25292 token = cp_lexer_peek_token (parser->lexer);
25294 if (token->type != CPP_KEYWORD
25295 || token->keyword != RID_ALIGNAS)
25296 return NULL_TREE;
25298 cp_lexer_consume_token (parser->lexer);
25299 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25301 matching_parens parens;
25302 if (!parens.require_open (parser))
25304 cp_parser_error (parser, "expected %<(%>");
25305 return error_mark_node;
25308 cp_parser_parse_tentatively (parser);
25309 alignas_expr = cp_parser_type_id (parser);
25311 if (!cp_parser_parse_definitely (parser))
25313 alignas_expr = cp_parser_assignment_expression (parser);
25314 if (alignas_expr == error_mark_node)
25315 cp_parser_skip_to_end_of_statement (parser);
25316 if (alignas_expr == NULL_TREE
25317 || alignas_expr == error_mark_node)
25318 return alignas_expr;
25321 alignas_expr = cxx_alignas_expr (alignas_expr);
25322 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25324 /* Handle alignas (pack...). */
25325 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25327 cp_lexer_consume_token (parser->lexer);
25328 alignas_expr = make_pack_expansion (alignas_expr);
25331 /* Something went wrong, so don't build the attribute. */
25332 if (alignas_expr == error_mark_node)
25333 return error_mark_node;
25335 if (!parens.require_close (parser))
25337 cp_parser_error (parser, "expected %<)%>");
25338 return error_mark_node;
25341 /* Build the C++-11 representation of an 'aligned'
25342 attribute. */
25343 attributes =
25344 build_tree_list (build_tree_list (get_identifier ("gnu"),
25345 get_identifier ("aligned")),
25346 alignas_expr);
25349 return attributes;
25352 /* Parse a standard C++-11 attribute-specifier-seq.
25354 attribute-specifier-seq:
25355 attribute-specifier-seq [opt] attribute-specifier
25358 static tree
25359 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25361 tree attr_specs = NULL_TREE;
25362 tree attr_last = NULL_TREE;
25364 while (true)
25366 tree attr_spec = cp_parser_std_attribute_spec (parser);
25367 if (attr_spec == NULL_TREE)
25368 break;
25369 if (attr_spec == error_mark_node)
25370 return error_mark_node;
25372 if (attr_last)
25373 TREE_CHAIN (attr_last) = attr_spec;
25374 else
25375 attr_specs = attr_last = attr_spec;
25376 attr_last = tree_last (attr_last);
25379 return attr_specs;
25382 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25383 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25384 current value of the PEDANTIC flag, regardless of whether or not
25385 the `__extension__' keyword is present. The caller is responsible
25386 for restoring the value of the PEDANTIC flag. */
25388 static bool
25389 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25391 /* Save the old value of the PEDANTIC flag. */
25392 *saved_pedantic = pedantic;
25394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25396 /* Consume the `__extension__' token. */
25397 cp_lexer_consume_token (parser->lexer);
25398 /* We're not being pedantic while the `__extension__' keyword is
25399 in effect. */
25400 pedantic = 0;
25402 return true;
25405 return false;
25408 /* Parse a label declaration.
25410 label-declaration:
25411 __label__ label-declarator-seq ;
25413 label-declarator-seq:
25414 identifier , label-declarator-seq
25415 identifier */
25417 static void
25418 cp_parser_label_declaration (cp_parser* parser)
25420 /* Look for the `__label__' keyword. */
25421 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25423 while (true)
25425 tree identifier;
25427 /* Look for an identifier. */
25428 identifier = cp_parser_identifier (parser);
25429 /* If we failed, stop. */
25430 if (identifier == error_mark_node)
25431 break;
25432 /* Declare it as a label. */
25433 finish_label_decl (identifier);
25434 /* If the next token is a `;', stop. */
25435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25436 break;
25437 /* Look for the `,' separating the label declarations. */
25438 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25441 /* Look for the final `;'. */
25442 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25445 // -------------------------------------------------------------------------- //
25446 // Requires Clause
25448 // Parse a requires clause.
25450 // requires-clause:
25451 // 'requires' logical-or-expression
25453 // The required logical-or-expression must be a constant expression. Note
25454 // that we don't check that the expression is constepxr here. We defer until
25455 // we analyze constraints and then, we only check atomic constraints.
25456 static tree
25457 cp_parser_requires_clause (cp_parser *parser)
25459 // Parse the requires clause so that it is not automatically folded.
25460 ++processing_template_decl;
25461 tree expr = cp_parser_binary_expression (parser, false, false,
25462 PREC_NOT_OPERATOR, NULL);
25463 if (check_for_bare_parameter_packs (expr))
25464 expr = error_mark_node;
25465 --processing_template_decl;
25466 return expr;
25469 // Optionally parse a requires clause:
25470 static tree
25471 cp_parser_requires_clause_opt (cp_parser *parser)
25473 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25474 if (tok->keyword != RID_REQUIRES)
25476 if (!flag_concepts && tok->type == CPP_NAME
25477 && tok->u.value == ridpointers[RID_REQUIRES])
25479 error_at (cp_lexer_peek_token (parser->lexer)->location,
25480 "%<requires%> only available with -fconcepts");
25481 /* Parse and discard the requires-clause. */
25482 cp_lexer_consume_token (parser->lexer);
25483 cp_parser_requires_clause (parser);
25485 return NULL_TREE;
25487 cp_lexer_consume_token (parser->lexer);
25488 return cp_parser_requires_clause (parser);
25492 /*---------------------------------------------------------------------------
25493 Requires expressions
25494 ---------------------------------------------------------------------------*/
25496 /* Parse a requires expression
25498 requirement-expression:
25499 'requires' requirement-parameter-list [opt] requirement-body */
25500 static tree
25501 cp_parser_requires_expression (cp_parser *parser)
25503 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25504 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25506 /* A requires-expression shall appear only within a concept
25507 definition or a requires-clause.
25509 TODO: Implement this diagnostic correctly. */
25510 if (!processing_template_decl)
25512 error_at (loc, "a requires expression cannot appear outside a template");
25513 cp_parser_skip_to_end_of_statement (parser);
25514 return error_mark_node;
25517 tree parms, reqs;
25519 /* Local parameters are delared as variables within the scope
25520 of the expression. They are not visible past the end of
25521 the expression. Expressions within the requires-expression
25522 are unevaluated. */
25523 struct scope_sentinel
25525 scope_sentinel ()
25527 ++cp_unevaluated_operand;
25528 begin_scope (sk_block, NULL_TREE);
25531 ~scope_sentinel ()
25533 pop_bindings_and_leave_scope ();
25534 --cp_unevaluated_operand;
25536 } s;
25538 /* Parse the optional parameter list. */
25539 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25541 parms = cp_parser_requirement_parameter_list (parser);
25542 if (parms == error_mark_node)
25543 return error_mark_node;
25545 else
25546 parms = NULL_TREE;
25548 /* Parse the requirement body. */
25549 reqs = cp_parser_requirement_body (parser);
25550 if (reqs == error_mark_node)
25551 return error_mark_node;
25554 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25555 the parm chain. */
25556 grokparms (parms, &parms);
25557 return finish_requires_expr (parms, reqs);
25560 /* Parse a parameterized requirement.
25562 requirement-parameter-list:
25563 '(' parameter-declaration-clause ')' */
25564 static tree
25565 cp_parser_requirement_parameter_list (cp_parser *parser)
25567 matching_parens parens;
25568 if (!parens.require_open (parser))
25569 return error_mark_node;
25571 tree parms = cp_parser_parameter_declaration_clause (parser);
25573 if (!parens.require_close (parser))
25574 return error_mark_node;
25576 return parms;
25579 /* Parse the body of a requirement.
25581 requirement-body:
25582 '{' requirement-list '}' */
25583 static tree
25584 cp_parser_requirement_body (cp_parser *parser)
25586 matching_braces braces;
25587 if (!braces.require_open (parser))
25588 return error_mark_node;
25590 tree reqs = cp_parser_requirement_list (parser);
25592 if (!braces.require_close (parser))
25593 return error_mark_node;
25595 return reqs;
25598 /* Parse a list of requirements.
25600 requirement-list:
25601 requirement
25602 requirement-list ';' requirement[opt] */
25603 static tree
25604 cp_parser_requirement_list (cp_parser *parser)
25606 tree result = NULL_TREE;
25607 while (true)
25609 tree req = cp_parser_requirement (parser);
25610 if (req == error_mark_node)
25611 return error_mark_node;
25613 result = tree_cons (NULL_TREE, req, result);
25615 /* If we see a semi-colon, consume it. */
25616 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25617 cp_lexer_consume_token (parser->lexer);
25619 /* Stop processing at the end of the list. */
25620 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25621 break;
25624 /* Reverse the order of requirements so they are analyzed in
25625 declaration order. */
25626 return nreverse (result);
25629 /* Parse a syntactic requirement or type requirement.
25631 requirement:
25632 simple-requirement
25633 compound-requirement
25634 type-requirement
25635 nested-requirement */
25636 static tree
25637 cp_parser_requirement (cp_parser *parser)
25639 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25640 return cp_parser_compound_requirement (parser);
25641 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25642 return cp_parser_type_requirement (parser);
25643 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25644 return cp_parser_nested_requirement (parser);
25645 else
25646 return cp_parser_simple_requirement (parser);
25649 /* Parse a simple requirement.
25651 simple-requirement:
25652 expression ';' */
25653 static tree
25654 cp_parser_simple_requirement (cp_parser *parser)
25656 tree expr = cp_parser_expression (parser, NULL, false, false);
25657 if (!expr || expr == error_mark_node)
25658 return error_mark_node;
25660 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25661 return error_mark_node;
25663 return finish_simple_requirement (expr);
25666 /* Parse a type requirement
25668 type-requirement
25669 nested-name-specifier [opt] required-type-name ';'
25671 required-type-name:
25672 type-name
25673 'template' [opt] simple-template-id */
25674 static tree
25675 cp_parser_type_requirement (cp_parser *parser)
25677 cp_lexer_consume_token (parser->lexer);
25679 // Save the scope before parsing name specifiers.
25680 tree saved_scope = parser->scope;
25681 tree saved_object_scope = parser->object_scope;
25682 tree saved_qualifying_scope = parser->qualifying_scope;
25683 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25684 cp_parser_nested_name_specifier_opt (parser,
25685 /*typename_keyword_p=*/true,
25686 /*check_dependency_p=*/false,
25687 /*type_p=*/true,
25688 /*is_declaration=*/false);
25690 tree type;
25691 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25693 cp_lexer_consume_token (parser->lexer);
25694 type = cp_parser_template_id (parser,
25695 /*template_keyword_p=*/true,
25696 /*check_dependency=*/false,
25697 /*tag_type=*/none_type,
25698 /*is_declaration=*/false);
25699 type = make_typename_type (parser->scope, type, typename_type,
25700 /*complain=*/tf_error);
25702 else
25703 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25705 if (TREE_CODE (type) == TYPE_DECL)
25706 type = TREE_TYPE (type);
25708 parser->scope = saved_scope;
25709 parser->object_scope = saved_object_scope;
25710 parser->qualifying_scope = saved_qualifying_scope;
25712 if (type == error_mark_node)
25713 cp_parser_skip_to_end_of_statement (parser);
25715 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25716 return error_mark_node;
25717 if (type == error_mark_node)
25718 return error_mark_node;
25720 return finish_type_requirement (type);
25723 /* Parse a compound requirement
25725 compound-requirement:
25726 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25727 static tree
25728 cp_parser_compound_requirement (cp_parser *parser)
25730 /* Parse an expression enclosed in '{ }'s. */
25731 matching_braces braces;
25732 if (!braces.require_open (parser))
25733 return error_mark_node;
25735 tree expr = cp_parser_expression (parser, NULL, false, false);
25736 if (!expr || expr == error_mark_node)
25737 return error_mark_node;
25739 if (!braces.require_close (parser))
25740 return error_mark_node;
25742 /* Parse the optional noexcept. */
25743 bool noexcept_p = false;
25744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25746 cp_lexer_consume_token (parser->lexer);
25747 noexcept_p = true;
25750 /* Parse the optional trailing return type. */
25751 tree type = NULL_TREE;
25752 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25754 cp_lexer_consume_token (parser->lexer);
25755 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25756 parser->in_result_type_constraint_p = true;
25757 type = cp_parser_trailing_type_id (parser);
25758 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25759 if (type == error_mark_node)
25760 return error_mark_node;
25763 return finish_compound_requirement (expr, type, noexcept_p);
25766 /* Parse a nested requirement. This is the same as a requires clause.
25768 nested-requirement:
25769 requires-clause */
25770 static tree
25771 cp_parser_nested_requirement (cp_parser *parser)
25773 cp_lexer_consume_token (parser->lexer);
25774 tree req = cp_parser_requires_clause (parser);
25775 if (req == error_mark_node)
25776 return error_mark_node;
25777 return finish_nested_requirement (req);
25780 /* Support Functions */
25782 /* Return the appropriate prefer_type argument for lookup_name_real based on
25783 tag_type and template_mem_access. */
25785 static inline int
25786 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25788 /* DR 141: When looking in the current enclosing context for a template-name
25789 after -> or ., only consider class templates. */
25790 if (template_mem_access)
25791 return 2;
25792 switch (tag_type)
25794 case none_type: return 0; // No preference.
25795 case scope_type: return 1; // Type or namespace.
25796 default: return 2; // Type only.
25800 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25801 NAME should have one of the representations used for an
25802 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25803 is returned. If PARSER->SCOPE is a dependent type, then a
25804 SCOPE_REF is returned.
25806 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25807 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25808 was formed. Abstractly, such entities should not be passed to this
25809 function, because they do not need to be looked up, but it is
25810 simpler to check for this special case here, rather than at the
25811 call-sites.
25813 In cases not explicitly covered above, this function returns a
25814 DECL, OVERLOAD, or baselink representing the result of the lookup.
25815 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25816 is returned.
25818 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25819 (e.g., "struct") that was used. In that case bindings that do not
25820 refer to types are ignored.
25822 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25823 ignored.
25825 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25826 are ignored.
25828 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25829 types.
25831 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25832 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25833 NULL_TREE otherwise. */
25835 static cp_expr
25836 cp_parser_lookup_name (cp_parser *parser, tree name,
25837 enum tag_types tag_type,
25838 bool is_template,
25839 bool is_namespace,
25840 bool check_dependency,
25841 tree *ambiguous_decls,
25842 location_t name_location)
25844 tree decl;
25845 tree object_type = parser->context->object_type;
25847 /* Assume that the lookup will be unambiguous. */
25848 if (ambiguous_decls)
25849 *ambiguous_decls = NULL_TREE;
25851 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25852 no longer valid. Note that if we are parsing tentatively, and
25853 the parse fails, OBJECT_TYPE will be automatically restored. */
25854 parser->context->object_type = NULL_TREE;
25856 if (name == error_mark_node)
25857 return error_mark_node;
25859 /* A template-id has already been resolved; there is no lookup to
25860 do. */
25861 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25862 return name;
25863 if (BASELINK_P (name))
25865 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25866 == TEMPLATE_ID_EXPR);
25867 return name;
25870 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25871 it should already have been checked to make sure that the name
25872 used matches the type being destroyed. */
25873 if (TREE_CODE (name) == BIT_NOT_EXPR)
25875 tree type;
25877 /* Figure out to which type this destructor applies. */
25878 if (parser->scope)
25879 type = parser->scope;
25880 else if (object_type)
25881 type = object_type;
25882 else
25883 type = current_class_type;
25884 /* If that's not a class type, there is no destructor. */
25885 if (!type || !CLASS_TYPE_P (type))
25886 return error_mark_node;
25888 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25889 lazily_declare_fn (sfk_destructor, type);
25891 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25892 return dtor;
25894 return error_mark_node;
25897 /* By this point, the NAME should be an ordinary identifier. If
25898 the id-expression was a qualified name, the qualifying scope is
25899 stored in PARSER->SCOPE at this point. */
25900 gcc_assert (identifier_p (name));
25902 /* Perform the lookup. */
25903 if (parser->scope)
25905 bool dependent_p;
25907 if (parser->scope == error_mark_node)
25908 return error_mark_node;
25910 /* If the SCOPE is dependent, the lookup must be deferred until
25911 the template is instantiated -- unless we are explicitly
25912 looking up names in uninstantiated templates. Even then, we
25913 cannot look up the name if the scope is not a class type; it
25914 might, for example, be a template type parameter. */
25915 dependent_p = (TYPE_P (parser->scope)
25916 && dependent_scope_p (parser->scope));
25917 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25918 && dependent_p)
25919 /* Defer lookup. */
25920 decl = error_mark_node;
25921 else
25923 tree pushed_scope = NULL_TREE;
25925 /* If PARSER->SCOPE is a dependent type, then it must be a
25926 class type, and we must not be checking dependencies;
25927 otherwise, we would have processed this lookup above. So
25928 that PARSER->SCOPE is not considered a dependent base by
25929 lookup_member, we must enter the scope here. */
25930 if (dependent_p)
25931 pushed_scope = push_scope (parser->scope);
25933 /* If the PARSER->SCOPE is a template specialization, it
25934 may be instantiated during name lookup. In that case,
25935 errors may be issued. Even if we rollback the current
25936 tentative parse, those errors are valid. */
25937 decl = lookup_qualified_name (parser->scope, name,
25938 prefer_type_arg (tag_type),
25939 /*complain=*/true);
25941 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25942 lookup result and the nested-name-specifier nominates a class C:
25943 * if the name specified after the nested-name-specifier, when
25944 looked up in C, is the injected-class-name of C (Clause 9), or
25945 * if the name specified after the nested-name-specifier is the
25946 same as the identifier or the simple-template-id's template-
25947 name in the last component of the nested-name-specifier,
25948 the name is instead considered to name the constructor of
25949 class C. [ Note: for example, the constructor is not an
25950 acceptable lookup result in an elaborated-type-specifier so
25951 the constructor would not be used in place of the
25952 injected-class-name. --end note ] Such a constructor name
25953 shall be used only in the declarator-id of a declaration that
25954 names a constructor or in a using-declaration. */
25955 if (tag_type == none_type
25956 && DECL_SELF_REFERENCE_P (decl)
25957 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25958 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25959 prefer_type_arg (tag_type),
25960 /*complain=*/true);
25962 /* If we have a single function from a using decl, pull it out. */
25963 if (TREE_CODE (decl) == OVERLOAD
25964 && !really_overloaded_fn (decl))
25965 decl = OVL_FUNCTION (decl);
25967 if (pushed_scope)
25968 pop_scope (pushed_scope);
25971 /* If the scope is a dependent type and either we deferred lookup or
25972 we did lookup but didn't find the name, rememeber the name. */
25973 if (decl == error_mark_node && TYPE_P (parser->scope)
25974 && dependent_type_p (parser->scope))
25976 if (tag_type)
25978 tree type;
25980 /* The resolution to Core Issue 180 says that `struct
25981 A::B' should be considered a type-name, even if `A'
25982 is dependent. */
25983 type = make_typename_type (parser->scope, name, tag_type,
25984 /*complain=*/tf_error);
25985 if (type != error_mark_node)
25986 decl = TYPE_NAME (type);
25988 else if (is_template
25989 && (cp_parser_next_token_ends_template_argument_p (parser)
25990 || cp_lexer_next_token_is (parser->lexer,
25991 CPP_CLOSE_PAREN)))
25992 decl = make_unbound_class_template (parser->scope,
25993 name, NULL_TREE,
25994 /*complain=*/tf_error);
25995 else
25996 decl = build_qualified_name (/*type=*/NULL_TREE,
25997 parser->scope, name,
25998 is_template);
26000 parser->qualifying_scope = parser->scope;
26001 parser->object_scope = NULL_TREE;
26003 else if (object_type)
26005 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26006 OBJECT_TYPE is not a class. */
26007 if (CLASS_TYPE_P (object_type))
26008 /* If the OBJECT_TYPE is a template specialization, it may
26009 be instantiated during name lookup. In that case, errors
26010 may be issued. Even if we rollback the current tentative
26011 parse, those errors are valid. */
26012 decl = lookup_member (object_type,
26013 name,
26014 /*protect=*/0,
26015 prefer_type_arg (tag_type),
26016 tf_warning_or_error);
26017 else
26018 decl = NULL_TREE;
26020 if (!decl)
26021 /* Look it up in the enclosing context. DR 141: When looking for a
26022 template-name after -> or ., only consider class templates. */
26023 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26024 /*nonclass=*/0,
26025 /*block_p=*/true, is_namespace, 0);
26026 if (object_type == unknown_type_node)
26027 /* The object is type-dependent, so we can't look anything up; we used
26028 this to get the DR 141 behavior. */
26029 object_type = NULL_TREE;
26030 parser->object_scope = object_type;
26031 parser->qualifying_scope = NULL_TREE;
26033 else
26035 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26036 /*nonclass=*/0,
26037 /*block_p=*/true, is_namespace, 0);
26038 parser->qualifying_scope = NULL_TREE;
26039 parser->object_scope = NULL_TREE;
26042 /* If the lookup failed, let our caller know. */
26043 if (!decl || decl == error_mark_node)
26044 return error_mark_node;
26046 /* Pull out the template from an injected-class-name (or multiple). */
26047 if (is_template)
26048 decl = maybe_get_template_decl_from_type_decl (decl);
26050 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26051 if (TREE_CODE (decl) == TREE_LIST)
26053 if (ambiguous_decls)
26054 *ambiguous_decls = decl;
26055 /* The error message we have to print is too complicated for
26056 cp_parser_error, so we incorporate its actions directly. */
26057 if (!cp_parser_simulate_error (parser))
26059 error_at (name_location, "reference to %qD is ambiguous",
26060 name);
26061 print_candidates (decl);
26063 return error_mark_node;
26066 gcc_assert (DECL_P (decl)
26067 || TREE_CODE (decl) == OVERLOAD
26068 || TREE_CODE (decl) == SCOPE_REF
26069 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26070 || BASELINK_P (decl));
26072 /* If we have resolved the name of a member declaration, check to
26073 see if the declaration is accessible. When the name resolves to
26074 set of overloaded functions, accessibility is checked when
26075 overload resolution is done.
26077 During an explicit instantiation, access is not checked at all,
26078 as per [temp.explicit]. */
26079 if (DECL_P (decl))
26080 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26082 maybe_record_typedef_use (decl);
26084 return cp_expr (decl, name_location);
26087 /* Like cp_parser_lookup_name, but for use in the typical case where
26088 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26089 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26091 static tree
26092 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26094 return cp_parser_lookup_name (parser, name,
26095 none_type,
26096 /*is_template=*/false,
26097 /*is_namespace=*/false,
26098 /*check_dependency=*/true,
26099 /*ambiguous_decls=*/NULL,
26100 location);
26103 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26104 the current context, return the TYPE_DECL. If TAG_NAME_P is
26105 true, the DECL indicates the class being defined in a class-head,
26106 or declared in an elaborated-type-specifier.
26108 Otherwise, return DECL. */
26110 static tree
26111 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26113 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26114 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26116 struct A {
26117 template <typename T> struct B;
26120 template <typename T> struct A::B {};
26122 Similarly, in an elaborated-type-specifier:
26124 namespace N { struct X{}; }
26126 struct A {
26127 template <typename T> friend struct N::X;
26130 However, if the DECL refers to a class type, and we are in
26131 the scope of the class, then the name lookup automatically
26132 finds the TYPE_DECL created by build_self_reference rather
26133 than a TEMPLATE_DECL. For example, in:
26135 template <class T> struct S {
26136 S s;
26139 there is no need to handle such case. */
26141 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26142 return DECL_TEMPLATE_RESULT (decl);
26144 return decl;
26147 /* If too many, or too few, template-parameter lists apply to the
26148 declarator, issue an error message. Returns TRUE if all went well,
26149 and FALSE otherwise. */
26151 static bool
26152 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26153 cp_declarator *declarator,
26154 location_t declarator_location)
26156 switch (declarator->kind)
26158 case cdk_id:
26160 unsigned num_templates = 0;
26161 tree scope = declarator->u.id.qualifying_scope;
26163 if (scope)
26164 num_templates = num_template_headers_for_class (scope);
26165 else if (TREE_CODE (declarator->u.id.unqualified_name)
26166 == TEMPLATE_ID_EXPR)
26167 /* If the DECLARATOR has the form `X<y>' then it uses one
26168 additional level of template parameters. */
26169 ++num_templates;
26171 return cp_parser_check_template_parameters
26172 (parser, num_templates, declarator_location, declarator);
26175 case cdk_function:
26176 case cdk_array:
26177 case cdk_pointer:
26178 case cdk_reference:
26179 case cdk_ptrmem:
26180 return (cp_parser_check_declarator_template_parameters
26181 (parser, declarator->declarator, declarator_location));
26183 case cdk_decomp:
26184 case cdk_error:
26185 return true;
26187 default:
26188 gcc_unreachable ();
26190 return false;
26193 /* NUM_TEMPLATES were used in the current declaration. If that is
26194 invalid, return FALSE and issue an error messages. Otherwise,
26195 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26196 declarator and we can print more accurate diagnostics. */
26198 static bool
26199 cp_parser_check_template_parameters (cp_parser* parser,
26200 unsigned num_templates,
26201 location_t location,
26202 cp_declarator *declarator)
26204 /* If there are the same number of template classes and parameter
26205 lists, that's OK. */
26206 if (parser->num_template_parameter_lists == num_templates)
26207 return true;
26208 /* If there are more, but only one more, then we are referring to a
26209 member template. That's OK too. */
26210 if (parser->num_template_parameter_lists == num_templates + 1)
26211 return true;
26212 /* If there are more template classes than parameter lists, we have
26213 something like:
26215 template <class T> void S<T>::R<T>::f (); */
26216 if (parser->num_template_parameter_lists < num_templates)
26218 if (declarator && !current_function_decl)
26219 error_at (location, "specializing member %<%T::%E%> "
26220 "requires %<template<>%> syntax",
26221 declarator->u.id.qualifying_scope,
26222 declarator->u.id.unqualified_name);
26223 else if (declarator)
26224 error_at (location, "invalid declaration of %<%T::%E%>",
26225 declarator->u.id.qualifying_scope,
26226 declarator->u.id.unqualified_name);
26227 else
26228 error_at (location, "too few template-parameter-lists");
26229 return false;
26231 /* Otherwise, there are too many template parameter lists. We have
26232 something like:
26234 template <class T> template <class U> void S::f(); */
26235 error_at (location, "too many template-parameter-lists");
26236 return false;
26239 /* Parse an optional `::' token indicating that the following name is
26240 from the global namespace. If so, PARSER->SCOPE is set to the
26241 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26242 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26243 Returns the new value of PARSER->SCOPE, if the `::' token is
26244 present, and NULL_TREE otherwise. */
26246 static tree
26247 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26249 cp_token *token;
26251 /* Peek at the next token. */
26252 token = cp_lexer_peek_token (parser->lexer);
26253 /* If we're looking at a `::' token then we're starting from the
26254 global namespace, not our current location. */
26255 if (token->type == CPP_SCOPE)
26257 /* Consume the `::' token. */
26258 cp_lexer_consume_token (parser->lexer);
26259 /* Set the SCOPE so that we know where to start the lookup. */
26260 parser->scope = global_namespace;
26261 parser->qualifying_scope = global_namespace;
26262 parser->object_scope = NULL_TREE;
26264 return parser->scope;
26266 else if (!current_scope_valid_p)
26268 parser->scope = NULL_TREE;
26269 parser->qualifying_scope = NULL_TREE;
26270 parser->object_scope = NULL_TREE;
26273 return NULL_TREE;
26276 /* Returns TRUE if the upcoming token sequence is the start of a
26277 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26278 declarator is preceded by the `friend' specifier. */
26280 static bool
26281 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26283 bool constructor_p;
26284 bool outside_class_specifier_p;
26285 tree nested_name_specifier;
26286 cp_token *next_token;
26288 /* The common case is that this is not a constructor declarator, so
26289 try to avoid doing lots of work if at all possible. It's not
26290 valid declare a constructor at function scope. */
26291 if (parser->in_function_body)
26292 return false;
26293 /* And only certain tokens can begin a constructor declarator. */
26294 next_token = cp_lexer_peek_token (parser->lexer);
26295 if (next_token->type != CPP_NAME
26296 && next_token->type != CPP_SCOPE
26297 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26298 && next_token->type != CPP_TEMPLATE_ID)
26299 return false;
26301 /* Parse tentatively; we are going to roll back all of the tokens
26302 consumed here. */
26303 cp_parser_parse_tentatively (parser);
26304 /* Assume that we are looking at a constructor declarator. */
26305 constructor_p = true;
26307 /* Look for the optional `::' operator. */
26308 cp_parser_global_scope_opt (parser,
26309 /*current_scope_valid_p=*/false);
26310 /* Look for the nested-name-specifier. */
26311 nested_name_specifier
26312 = (cp_parser_nested_name_specifier_opt (parser,
26313 /*typename_keyword_p=*/false,
26314 /*check_dependency_p=*/false,
26315 /*type_p=*/false,
26316 /*is_declaration=*/false));
26318 outside_class_specifier_p = (!at_class_scope_p ()
26319 || !TYPE_BEING_DEFINED (current_class_type)
26320 || friend_p);
26322 /* Outside of a class-specifier, there must be a
26323 nested-name-specifier. Except in C++17 mode, where we
26324 might be declaring a guiding declaration. */
26325 if (!nested_name_specifier && outside_class_specifier_p
26326 && cxx_dialect < cxx17)
26327 constructor_p = false;
26328 else if (nested_name_specifier == error_mark_node)
26329 constructor_p = false;
26331 /* If we have a class scope, this is easy; DR 147 says that S::S always
26332 names the constructor, and no other qualified name could. */
26333 if (constructor_p && nested_name_specifier
26334 && CLASS_TYPE_P (nested_name_specifier))
26336 tree id = cp_parser_unqualified_id (parser,
26337 /*template_keyword_p=*/false,
26338 /*check_dependency_p=*/false,
26339 /*declarator_p=*/true,
26340 /*optional_p=*/false);
26341 if (is_overloaded_fn (id))
26342 id = DECL_NAME (get_first_fn (id));
26343 if (!constructor_name_p (id, nested_name_specifier))
26344 constructor_p = false;
26346 /* If we still think that this might be a constructor-declarator,
26347 look for a class-name. */
26348 else if (constructor_p)
26350 /* If we have:
26352 template <typename T> struct S {
26353 S();
26356 we must recognize that the nested `S' names a class. */
26357 if (cxx_dialect >= cxx17)
26358 cp_parser_parse_tentatively (parser);
26360 tree type_decl;
26361 type_decl = cp_parser_class_name (parser,
26362 /*typename_keyword_p=*/false,
26363 /*template_keyword_p=*/false,
26364 none_type,
26365 /*check_dependency_p=*/false,
26366 /*class_head_p=*/false,
26367 /*is_declaration=*/false);
26369 if (cxx_dialect >= cxx17
26370 && !cp_parser_parse_definitely (parser))
26372 type_decl = NULL_TREE;
26373 tree tmpl = cp_parser_template_name (parser,
26374 /*template_keyword*/false,
26375 /*check_dependency_p*/false,
26376 /*is_declaration*/false,
26377 none_type,
26378 /*is_identifier*/NULL);
26379 if (DECL_CLASS_TEMPLATE_P (tmpl)
26380 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26381 /* It's a deduction guide, return true. */;
26382 else
26383 cp_parser_simulate_error (parser);
26386 /* If there was no class-name, then this is not a constructor.
26387 Otherwise, if we are in a class-specifier and we aren't
26388 handling a friend declaration, check that its type matches
26389 current_class_type (c++/38313). Note: error_mark_node
26390 is left alone for error recovery purposes. */
26391 constructor_p = (!cp_parser_error_occurred (parser)
26392 && (outside_class_specifier_p
26393 || type_decl == NULL_TREE
26394 || type_decl == error_mark_node
26395 || same_type_p (current_class_type,
26396 TREE_TYPE (type_decl))));
26398 /* If we're still considering a constructor, we have to see a `(',
26399 to begin the parameter-declaration-clause, followed by either a
26400 `)', an `...', or a decl-specifier. We need to check for a
26401 type-specifier to avoid being fooled into thinking that:
26403 S (f) (int);
26405 is a constructor. (It is actually a function named `f' that
26406 takes one parameter (of type `int') and returns a value of type
26407 `S'. */
26408 if (constructor_p
26409 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26410 constructor_p = false;
26412 if (constructor_p
26413 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26414 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26415 /* A parameter declaration begins with a decl-specifier,
26416 which is either the "attribute" keyword, a storage class
26417 specifier, or (usually) a type-specifier. */
26418 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26420 tree type;
26421 tree pushed_scope = NULL_TREE;
26422 unsigned saved_num_template_parameter_lists;
26424 /* Names appearing in the type-specifier should be looked up
26425 in the scope of the class. */
26426 if (current_class_type)
26427 type = NULL_TREE;
26428 else if (type_decl)
26430 type = TREE_TYPE (type_decl);
26431 if (TREE_CODE (type) == TYPENAME_TYPE)
26433 type = resolve_typename_type (type,
26434 /*only_current_p=*/false);
26435 if (TREE_CODE (type) == TYPENAME_TYPE)
26437 cp_parser_abort_tentative_parse (parser);
26438 return false;
26441 pushed_scope = push_scope (type);
26444 /* Inside the constructor parameter list, surrounding
26445 template-parameter-lists do not apply. */
26446 saved_num_template_parameter_lists
26447 = parser->num_template_parameter_lists;
26448 parser->num_template_parameter_lists = 0;
26450 /* Look for the type-specifier. */
26451 cp_parser_type_specifier (parser,
26452 CP_PARSER_FLAGS_NONE,
26453 /*decl_specs=*/NULL,
26454 /*is_declarator=*/true,
26455 /*declares_class_or_enum=*/NULL,
26456 /*is_cv_qualifier=*/NULL);
26458 parser->num_template_parameter_lists
26459 = saved_num_template_parameter_lists;
26461 /* Leave the scope of the class. */
26462 if (pushed_scope)
26463 pop_scope (pushed_scope);
26465 constructor_p = !cp_parser_error_occurred (parser);
26469 /* We did not really want to consume any tokens. */
26470 cp_parser_abort_tentative_parse (parser);
26472 return constructor_p;
26475 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26476 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26477 they must be performed once we are in the scope of the function.
26479 Returns the function defined. */
26481 static tree
26482 cp_parser_function_definition_from_specifiers_and_declarator
26483 (cp_parser* parser,
26484 cp_decl_specifier_seq *decl_specifiers,
26485 tree attributes,
26486 const cp_declarator *declarator)
26488 tree fn;
26489 bool success_p;
26491 /* Begin the function-definition. */
26492 success_p = start_function (decl_specifiers, declarator, attributes);
26494 /* The things we're about to see are not directly qualified by any
26495 template headers we've seen thus far. */
26496 reset_specialization ();
26498 /* If there were names looked up in the decl-specifier-seq that we
26499 did not check, check them now. We must wait until we are in the
26500 scope of the function to perform the checks, since the function
26501 might be a friend. */
26502 perform_deferred_access_checks (tf_warning_or_error);
26504 if (success_p)
26506 cp_finalize_omp_declare_simd (parser, current_function_decl);
26507 parser->omp_declare_simd = NULL;
26508 cp_finalize_oacc_routine (parser, current_function_decl, true);
26509 parser->oacc_routine = NULL;
26512 if (!success_p)
26514 /* Skip the entire function. */
26515 cp_parser_skip_to_end_of_block_or_statement (parser);
26516 fn = error_mark_node;
26518 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26520 /* Seen already, skip it. An error message has already been output. */
26521 cp_parser_skip_to_end_of_block_or_statement (parser);
26522 fn = current_function_decl;
26523 current_function_decl = NULL_TREE;
26524 /* If this is a function from a class, pop the nested class. */
26525 if (current_class_name)
26526 pop_nested_class ();
26528 else
26530 timevar_id_t tv;
26531 if (DECL_DECLARED_INLINE_P (current_function_decl))
26532 tv = TV_PARSE_INLINE;
26533 else
26534 tv = TV_PARSE_FUNC;
26535 timevar_push (tv);
26536 fn = cp_parser_function_definition_after_declarator (parser,
26537 /*inline_p=*/false);
26538 timevar_pop (tv);
26541 return fn;
26544 /* Parse the part of a function-definition that follows the
26545 declarator. INLINE_P is TRUE iff this function is an inline
26546 function defined within a class-specifier.
26548 Returns the function defined. */
26550 static tree
26551 cp_parser_function_definition_after_declarator (cp_parser* parser,
26552 bool inline_p)
26554 tree fn;
26555 bool saved_in_unbraced_linkage_specification_p;
26556 bool saved_in_function_body;
26557 unsigned saved_num_template_parameter_lists;
26558 cp_token *token;
26559 bool fully_implicit_function_template_p
26560 = parser->fully_implicit_function_template_p;
26561 parser->fully_implicit_function_template_p = false;
26562 tree implicit_template_parms
26563 = parser->implicit_template_parms;
26564 parser->implicit_template_parms = 0;
26565 cp_binding_level* implicit_template_scope
26566 = parser->implicit_template_scope;
26567 parser->implicit_template_scope = 0;
26569 saved_in_function_body = parser->in_function_body;
26570 parser->in_function_body = true;
26571 /* If the next token is `return', then the code may be trying to
26572 make use of the "named return value" extension that G++ used to
26573 support. */
26574 token = cp_lexer_peek_token (parser->lexer);
26575 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26577 /* Consume the `return' keyword. */
26578 cp_lexer_consume_token (parser->lexer);
26579 /* Look for the identifier that indicates what value is to be
26580 returned. */
26581 cp_parser_identifier (parser);
26582 /* Issue an error message. */
26583 error_at (token->location,
26584 "named return values are no longer supported");
26585 /* Skip tokens until we reach the start of the function body. */
26586 while (true)
26588 cp_token *token = cp_lexer_peek_token (parser->lexer);
26589 if (token->type == CPP_OPEN_BRACE
26590 || token->type == CPP_EOF
26591 || token->type == CPP_PRAGMA_EOL)
26592 break;
26593 cp_lexer_consume_token (parser->lexer);
26596 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26597 anything declared inside `f'. */
26598 saved_in_unbraced_linkage_specification_p
26599 = parser->in_unbraced_linkage_specification_p;
26600 parser->in_unbraced_linkage_specification_p = false;
26601 /* Inside the function, surrounding template-parameter-lists do not
26602 apply. */
26603 saved_num_template_parameter_lists
26604 = parser->num_template_parameter_lists;
26605 parser->num_template_parameter_lists = 0;
26607 /* If the next token is `try', `__transaction_atomic', or
26608 `__transaction_relaxed`, then we are looking at either function-try-block
26609 or function-transaction-block. Note that all of these include the
26610 function-body. */
26611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26612 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26613 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26614 RID_TRANSACTION_RELAXED))
26615 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26616 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26617 cp_parser_function_try_block (parser);
26618 else
26619 cp_parser_ctor_initializer_opt_and_function_body
26620 (parser, /*in_function_try_block=*/false);
26622 /* Finish the function. */
26623 fn = finish_function (inline_p);
26624 /* Generate code for it, if necessary. */
26625 expand_or_defer_fn (fn);
26626 /* Restore the saved values. */
26627 parser->in_unbraced_linkage_specification_p
26628 = saved_in_unbraced_linkage_specification_p;
26629 parser->num_template_parameter_lists
26630 = saved_num_template_parameter_lists;
26631 parser->in_function_body = saved_in_function_body;
26633 parser->fully_implicit_function_template_p
26634 = fully_implicit_function_template_p;
26635 parser->implicit_template_parms
26636 = implicit_template_parms;
26637 parser->implicit_template_scope
26638 = implicit_template_scope;
26640 if (parser->fully_implicit_function_template_p)
26641 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26643 return fn;
26646 /* Parse a template-declaration body (following argument list). */
26648 static void
26649 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26650 tree parameter_list,
26651 bool member_p)
26653 tree decl = NULL_TREE;
26654 bool friend_p = false;
26656 /* We just processed one more parameter list. */
26657 ++parser->num_template_parameter_lists;
26659 /* Get the deferred access checks from the parameter list. These
26660 will be checked once we know what is being declared, as for a
26661 member template the checks must be performed in the scope of the
26662 class containing the member. */
26663 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26665 /* Tentatively parse for a new template parameter list, which can either be
26666 the template keyword or a template introduction. */
26667 if (cp_parser_template_declaration_after_export (parser, member_p))
26668 /* OK */;
26669 else if (cxx_dialect >= cxx11
26670 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26671 decl = cp_parser_alias_declaration (parser);
26672 else
26674 /* There are no access checks when parsing a template, as we do not
26675 know if a specialization will be a friend. */
26676 push_deferring_access_checks (dk_no_check);
26677 cp_token *token = cp_lexer_peek_token (parser->lexer);
26678 decl = cp_parser_single_declaration (parser,
26679 checks,
26680 member_p,
26681 /*explicit_specialization_p=*/false,
26682 &friend_p);
26683 pop_deferring_access_checks ();
26685 /* If this is a member template declaration, let the front
26686 end know. */
26687 if (member_p && !friend_p && decl)
26689 if (TREE_CODE (decl) == TYPE_DECL)
26690 cp_parser_check_access_in_redeclaration (decl, token->location);
26692 decl = finish_member_template_decl (decl);
26694 else if (friend_p && decl
26695 && DECL_DECLARES_TYPE_P (decl))
26696 make_friend_class (current_class_type, TREE_TYPE (decl),
26697 /*complain=*/true);
26699 /* We are done with the current parameter list. */
26700 --parser->num_template_parameter_lists;
26702 pop_deferring_access_checks ();
26704 /* Finish up. */
26705 finish_template_decl (parameter_list);
26707 /* Check the template arguments for a literal operator template. */
26708 if (decl
26709 && DECL_DECLARES_FUNCTION_P (decl)
26710 && UDLIT_OPER_P (DECL_NAME (decl)))
26712 bool ok = true;
26713 if (parameter_list == NULL_TREE)
26714 ok = false;
26715 else
26717 int num_parms = TREE_VEC_LENGTH (parameter_list);
26718 if (num_parms == 1)
26720 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26721 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26722 if (TREE_TYPE (parm) != char_type_node
26723 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26724 ok = false;
26726 else if (num_parms == 2 && cxx_dialect >= cxx14)
26728 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26729 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26730 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26731 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26732 if (parm == error_mark_node
26733 || TREE_TYPE (parm) != TREE_TYPE (type)
26734 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26735 ok = false;
26737 else
26738 ok = false;
26740 if (!ok)
26742 if (cxx_dialect >= cxx14)
26743 error ("literal operator template %qD has invalid parameter list."
26744 " Expected non-type template argument pack <char...>"
26745 " or <typename CharT, CharT...>",
26746 decl);
26747 else
26748 error ("literal operator template %qD has invalid parameter list."
26749 " Expected non-type template argument pack <char...>",
26750 decl);
26754 /* Register member declarations. */
26755 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26756 finish_member_declaration (decl);
26757 /* If DECL is a function template, we must return to parse it later.
26758 (Even though there is no definition, there might be default
26759 arguments that need handling.) */
26760 if (member_p && decl
26761 && DECL_DECLARES_FUNCTION_P (decl))
26762 vec_safe_push (unparsed_funs_with_definitions, decl);
26765 /* Parse a template introduction header for a template-declaration. Returns
26766 false if tentative parse fails. */
26768 static bool
26769 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26771 cp_parser_parse_tentatively (parser);
26773 tree saved_scope = parser->scope;
26774 tree saved_object_scope = parser->object_scope;
26775 tree saved_qualifying_scope = parser->qualifying_scope;
26777 /* Look for the optional `::' operator. */
26778 cp_parser_global_scope_opt (parser,
26779 /*current_scope_valid_p=*/false);
26780 /* Look for the nested-name-specifier. */
26781 cp_parser_nested_name_specifier_opt (parser,
26782 /*typename_keyword_p=*/false,
26783 /*check_dependency_p=*/true,
26784 /*type_p=*/false,
26785 /*is_declaration=*/false);
26787 cp_token *token = cp_lexer_peek_token (parser->lexer);
26788 tree concept_name = cp_parser_identifier (parser);
26790 /* Look up the concept for which we will be matching
26791 template parameters. */
26792 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26793 token->location);
26794 parser->scope = saved_scope;
26795 parser->object_scope = saved_object_scope;
26796 parser->qualifying_scope = saved_qualifying_scope;
26798 if (concept_name == error_mark_node)
26799 cp_parser_simulate_error (parser);
26801 /* Look for opening brace for introduction. */
26802 matching_braces braces;
26803 braces.require_open (parser);
26805 if (!cp_parser_parse_definitely (parser))
26806 return false;
26808 push_deferring_access_checks (dk_deferred);
26810 /* Build vector of placeholder parameters and grab
26811 matching identifiers. */
26812 tree introduction_list = cp_parser_introduction_list (parser);
26814 /* The introduction-list shall not be empty. */
26815 int nargs = TREE_VEC_LENGTH (introduction_list);
26816 if (nargs == 0)
26818 error ("empty introduction-list");
26819 return true;
26822 /* Look for closing brace for introduction. */
26823 if (!braces.require_close (parser))
26824 return true;
26826 if (tmpl_decl == error_mark_node)
26828 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26829 token->location);
26830 return true;
26833 /* Build and associate the constraint. */
26834 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26835 if (parms && parms != error_mark_node)
26837 cp_parser_template_declaration_after_parameters (parser, parms,
26838 member_p);
26839 return true;
26842 error_at (token->location, "no matching concept for template-introduction");
26843 return true;
26846 /* Parse a normal template-declaration following the template keyword. */
26848 static void
26849 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26851 tree parameter_list;
26852 bool need_lang_pop;
26853 location_t location = input_location;
26855 /* Look for the `<' token. */
26856 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26857 return;
26858 if (at_class_scope_p () && current_function_decl)
26860 /* 14.5.2.2 [temp.mem]
26862 A local class shall not have member templates. */
26863 error_at (location,
26864 "invalid declaration of member template in local class");
26865 cp_parser_skip_to_end_of_block_or_statement (parser);
26866 return;
26868 /* [temp]
26870 A template ... shall not have C linkage. */
26871 if (current_lang_name == lang_name_c)
26873 error_at (location, "template with C linkage");
26874 maybe_show_extern_c_location ();
26875 /* Give it C++ linkage to avoid confusing other parts of the
26876 front end. */
26877 push_lang_context (lang_name_cplusplus);
26878 need_lang_pop = true;
26880 else
26881 need_lang_pop = false;
26883 /* We cannot perform access checks on the template parameter
26884 declarations until we know what is being declared, just as we
26885 cannot check the decl-specifier list. */
26886 push_deferring_access_checks (dk_deferred);
26888 /* If the next token is `>', then we have an invalid
26889 specialization. Rather than complain about an invalid template
26890 parameter, issue an error message here. */
26891 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26893 cp_parser_error (parser, "invalid explicit specialization");
26894 begin_specialization ();
26895 parameter_list = NULL_TREE;
26897 else
26899 /* Parse the template parameters. */
26900 parameter_list = cp_parser_template_parameter_list (parser);
26903 /* Look for the `>'. */
26904 cp_parser_skip_to_end_of_template_parameter_list (parser);
26906 /* Manage template requirements */
26907 if (flag_concepts)
26909 tree reqs = get_shorthand_constraints (current_template_parms);
26910 if (tree r = cp_parser_requires_clause_opt (parser))
26911 reqs = conjoin_constraints (reqs, normalize_expression (r));
26912 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26915 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26916 member_p);
26918 /* For the erroneous case of a template with C linkage, we pushed an
26919 implicit C++ linkage scope; exit that scope now. */
26920 if (need_lang_pop)
26921 pop_lang_context ();
26924 /* Parse a template-declaration, assuming that the `export' (and
26925 `extern') keywords, if present, has already been scanned. MEMBER_P
26926 is as for cp_parser_template_declaration. */
26928 static bool
26929 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26933 cp_lexer_consume_token (parser->lexer);
26934 cp_parser_explicit_template_declaration (parser, member_p);
26935 return true;
26937 else if (flag_concepts)
26938 return cp_parser_template_introduction (parser, member_p);
26940 return false;
26943 /* Perform the deferred access checks from a template-parameter-list.
26944 CHECKS is a TREE_LIST of access checks, as returned by
26945 get_deferred_access_checks. */
26947 static void
26948 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26950 ++processing_template_parmlist;
26951 perform_access_checks (checks, tf_warning_or_error);
26952 --processing_template_parmlist;
26955 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26956 `function-definition' sequence that follows a template header.
26957 If MEMBER_P is true, this declaration appears in a class scope.
26959 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26960 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26962 static tree
26963 cp_parser_single_declaration (cp_parser* parser,
26964 vec<deferred_access_check, va_gc> *checks,
26965 bool member_p,
26966 bool explicit_specialization_p,
26967 bool* friend_p)
26969 int declares_class_or_enum;
26970 tree decl = NULL_TREE;
26971 cp_decl_specifier_seq decl_specifiers;
26972 bool function_definition_p = false;
26973 cp_token *decl_spec_token_start;
26975 /* This function is only used when processing a template
26976 declaration. */
26977 gcc_assert (innermost_scope_kind () == sk_template_parms
26978 || innermost_scope_kind () == sk_template_spec);
26980 /* Defer access checks until we know what is being declared. */
26981 push_deferring_access_checks (dk_deferred);
26983 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26984 alternative. */
26985 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26986 cp_parser_decl_specifier_seq (parser,
26987 CP_PARSER_FLAGS_OPTIONAL,
26988 &decl_specifiers,
26989 &declares_class_or_enum);
26990 if (friend_p)
26991 *friend_p = cp_parser_friend_p (&decl_specifiers);
26993 /* There are no template typedefs. */
26994 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26996 error_at (decl_spec_token_start->location,
26997 "template declaration of %<typedef%>");
26998 decl = error_mark_node;
27001 /* Gather up the access checks that occurred the
27002 decl-specifier-seq. */
27003 stop_deferring_access_checks ();
27005 /* Check for the declaration of a template class. */
27006 if (declares_class_or_enum)
27008 if (cp_parser_declares_only_class_p (parser)
27009 || (declares_class_or_enum & 2))
27011 // If this is a declaration, but not a definition, associate
27012 // any constraints with the type declaration. Constraints
27013 // are associated with definitions in cp_parser_class_specifier.
27014 if (declares_class_or_enum == 1)
27015 associate_classtype_constraints (decl_specifiers.type);
27017 decl = shadow_tag (&decl_specifiers);
27019 /* In this case:
27021 struct C {
27022 friend template <typename T> struct A<T>::B;
27025 A<T>::B will be represented by a TYPENAME_TYPE, and
27026 therefore not recognized by shadow_tag. */
27027 if (friend_p && *friend_p
27028 && !decl
27029 && decl_specifiers.type
27030 && TYPE_P (decl_specifiers.type))
27031 decl = decl_specifiers.type;
27033 if (decl && decl != error_mark_node)
27034 decl = TYPE_NAME (decl);
27035 else
27036 decl = error_mark_node;
27038 /* Perform access checks for template parameters. */
27039 cp_parser_perform_template_parameter_access_checks (checks);
27041 /* Give a helpful diagnostic for
27042 template <class T> struct A { } a;
27043 if we aren't already recovering from an error. */
27044 if (!cp_parser_declares_only_class_p (parser)
27045 && !seen_error ())
27047 error_at (cp_lexer_peek_token (parser->lexer)->location,
27048 "a class template declaration must not declare "
27049 "anything else");
27050 cp_parser_skip_to_end_of_block_or_statement (parser);
27051 goto out;
27056 /* Complain about missing 'typename' or other invalid type names. */
27057 if (!decl_specifiers.any_type_specifiers_p
27058 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27060 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27061 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27062 the rest of this declaration. */
27063 decl = error_mark_node;
27064 goto out;
27067 /* If it's not a template class, try for a template function. If
27068 the next token is a `;', then this declaration does not declare
27069 anything. But, if there were errors in the decl-specifiers, then
27070 the error might well have come from an attempted class-specifier.
27071 In that case, there's no need to warn about a missing declarator. */
27072 if (!decl
27073 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27074 || decl_specifiers.type != error_mark_node))
27076 decl = cp_parser_init_declarator (parser,
27077 &decl_specifiers,
27078 checks,
27079 /*function_definition_allowed_p=*/true,
27080 member_p,
27081 declares_class_or_enum,
27082 &function_definition_p,
27083 NULL, NULL, NULL);
27085 /* 7.1.1-1 [dcl.stc]
27087 A storage-class-specifier shall not be specified in an explicit
27088 specialization... */
27089 if (decl
27090 && explicit_specialization_p
27091 && decl_specifiers.storage_class != sc_none)
27093 error_at (decl_spec_token_start->location,
27094 "explicit template specialization cannot have a storage class");
27095 decl = error_mark_node;
27098 if (decl && VAR_P (decl))
27099 check_template_variable (decl);
27102 /* Look for a trailing `;' after the declaration. */
27103 if (!function_definition_p
27104 && (decl == error_mark_node
27105 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27106 cp_parser_skip_to_end_of_block_or_statement (parser);
27108 out:
27109 pop_deferring_access_checks ();
27111 /* Clear any current qualification; whatever comes next is the start
27112 of something new. */
27113 parser->scope = NULL_TREE;
27114 parser->qualifying_scope = NULL_TREE;
27115 parser->object_scope = NULL_TREE;
27117 return decl;
27120 /* Parse a cast-expression that is not the operand of a unary "&". */
27122 static cp_expr
27123 cp_parser_simple_cast_expression (cp_parser *parser)
27125 return cp_parser_cast_expression (parser, /*address_p=*/false,
27126 /*cast_p=*/false, /*decltype*/false, NULL);
27129 /* Parse a functional cast to TYPE. Returns an expression
27130 representing the cast. */
27132 static cp_expr
27133 cp_parser_functional_cast (cp_parser* parser, tree type)
27135 vec<tree, va_gc> *vec;
27136 tree expression_list;
27137 cp_expr cast;
27138 bool nonconst_p;
27140 location_t start_loc = input_location;
27142 if (!type)
27143 type = error_mark_node;
27145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27147 cp_lexer_set_source_position (parser->lexer);
27148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27149 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27150 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27151 if (TREE_CODE (type) == TYPE_DECL)
27152 type = TREE_TYPE (type);
27154 cast = finish_compound_literal (type, expression_list,
27155 tf_warning_or_error, fcl_functional);
27156 /* Create a location of the form:
27157 type_name{i, f}
27158 ^~~~~~~~~~~~~~~
27159 with caret == start at the start of the type name,
27160 finishing at the closing brace. */
27161 location_t finish_loc
27162 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27163 location_t combined_loc = make_location (start_loc, start_loc,
27164 finish_loc);
27165 cast.set_location (combined_loc);
27166 return cast;
27170 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27171 /*cast_p=*/true,
27172 /*allow_expansion_p=*/true,
27173 /*non_constant_p=*/NULL);
27174 if (vec == NULL)
27175 expression_list = error_mark_node;
27176 else
27178 expression_list = build_tree_list_vec (vec);
27179 release_tree_vector (vec);
27182 cast = build_functional_cast (type, expression_list,
27183 tf_warning_or_error);
27184 /* [expr.const]/1: In an integral constant expression "only type
27185 conversions to integral or enumeration type can be used". */
27186 if (TREE_CODE (type) == TYPE_DECL)
27187 type = TREE_TYPE (type);
27188 if (cast != error_mark_node
27189 && !cast_valid_in_integral_constant_expression_p (type)
27190 && cp_parser_non_integral_constant_expression (parser,
27191 NIC_CONSTRUCTOR))
27192 return error_mark_node;
27194 /* Create a location of the form:
27195 float(i)
27196 ^~~~~~~~
27197 with caret == start at the start of the type name,
27198 finishing at the closing paren. */
27199 location_t finish_loc
27200 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27201 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27202 cast.set_location (combined_loc);
27203 return cast;
27206 /* Save the tokens that make up the body of a member function defined
27207 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27208 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27209 specifiers applied to the declaration. Returns the FUNCTION_DECL
27210 for the member function. */
27212 static tree
27213 cp_parser_save_member_function_body (cp_parser* parser,
27214 cp_decl_specifier_seq *decl_specifiers,
27215 cp_declarator *declarator,
27216 tree attributes)
27218 cp_token *first;
27219 cp_token *last;
27220 tree fn;
27221 bool function_try_block = false;
27223 /* Create the FUNCTION_DECL. */
27224 fn = grokmethod (decl_specifiers, declarator, attributes);
27225 cp_finalize_omp_declare_simd (parser, fn);
27226 cp_finalize_oacc_routine (parser, fn, true);
27227 /* If something went badly wrong, bail out now. */
27228 if (fn == error_mark_node)
27230 /* If there's a function-body, skip it. */
27231 if (cp_parser_token_starts_function_definition_p
27232 (cp_lexer_peek_token (parser->lexer)))
27233 cp_parser_skip_to_end_of_block_or_statement (parser);
27234 return error_mark_node;
27237 /* Remember it, if there default args to post process. */
27238 cp_parser_save_default_args (parser, fn);
27240 /* Save away the tokens that make up the body of the
27241 function. */
27242 first = parser->lexer->next_token;
27244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27245 cp_lexer_consume_token (parser->lexer);
27246 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27247 RID_TRANSACTION_ATOMIC))
27249 cp_lexer_consume_token (parser->lexer);
27250 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27252 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27253 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27254 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27255 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27256 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27258 cp_lexer_consume_token (parser->lexer);
27259 cp_lexer_consume_token (parser->lexer);
27260 cp_lexer_consume_token (parser->lexer);
27261 cp_lexer_consume_token (parser->lexer);
27262 cp_lexer_consume_token (parser->lexer);
27264 else
27265 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27266 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27268 cp_lexer_consume_token (parser->lexer);
27269 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27270 break;
27274 /* Handle function try blocks. */
27275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27277 cp_lexer_consume_token (parser->lexer);
27278 function_try_block = true;
27280 /* We can have braced-init-list mem-initializers before the fn body. */
27281 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27283 cp_lexer_consume_token (parser->lexer);
27284 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27286 /* cache_group will stop after an un-nested { } pair, too. */
27287 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27288 break;
27290 /* variadic mem-inits have ... after the ')'. */
27291 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27292 cp_lexer_consume_token (parser->lexer);
27295 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27296 /* Handle function try blocks. */
27297 if (function_try_block)
27298 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27299 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27300 last = parser->lexer->next_token;
27302 /* Save away the inline definition; we will process it when the
27303 class is complete. */
27304 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27305 DECL_PENDING_INLINE_P (fn) = 1;
27307 /* We need to know that this was defined in the class, so that
27308 friend templates are handled correctly. */
27309 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27311 /* Add FN to the queue of functions to be parsed later. */
27312 vec_safe_push (unparsed_funs_with_definitions, fn);
27314 return fn;
27317 /* Save the tokens that make up the in-class initializer for a non-static
27318 data member. Returns a DEFAULT_ARG. */
27320 static tree
27321 cp_parser_save_nsdmi (cp_parser* parser)
27323 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27326 /* Parse a template-argument-list, as well as the trailing ">" (but
27327 not the opening "<"). See cp_parser_template_argument_list for the
27328 return value. */
27330 static tree
27331 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27333 tree arguments;
27334 tree saved_scope;
27335 tree saved_qualifying_scope;
27336 tree saved_object_scope;
27337 bool saved_greater_than_is_operator_p;
27338 int saved_unevaluated_operand;
27339 int saved_inhibit_evaluation_warnings;
27341 /* [temp.names]
27343 When parsing a template-id, the first non-nested `>' is taken as
27344 the end of the template-argument-list rather than a greater-than
27345 operator. */
27346 saved_greater_than_is_operator_p
27347 = parser->greater_than_is_operator_p;
27348 parser->greater_than_is_operator_p = false;
27349 /* Parsing the argument list may modify SCOPE, so we save it
27350 here. */
27351 saved_scope = parser->scope;
27352 saved_qualifying_scope = parser->qualifying_scope;
27353 saved_object_scope = parser->object_scope;
27354 /* We need to evaluate the template arguments, even though this
27355 template-id may be nested within a "sizeof". */
27356 saved_unevaluated_operand = cp_unevaluated_operand;
27357 cp_unevaluated_operand = 0;
27358 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27359 c_inhibit_evaluation_warnings = 0;
27360 /* Parse the template-argument-list itself. */
27361 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27362 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27363 arguments = NULL_TREE;
27364 else
27365 arguments = cp_parser_template_argument_list (parser);
27366 /* Look for the `>' that ends the template-argument-list. If we find
27367 a '>>' instead, it's probably just a typo. */
27368 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27370 if (cxx_dialect != cxx98)
27372 /* In C++0x, a `>>' in a template argument list or cast
27373 expression is considered to be two separate `>'
27374 tokens. So, change the current token to a `>', but don't
27375 consume it: it will be consumed later when the outer
27376 template argument list (or cast expression) is parsed.
27377 Note that this replacement of `>' for `>>' is necessary
27378 even if we are parsing tentatively: in the tentative
27379 case, after calling
27380 cp_parser_enclosed_template_argument_list we will always
27381 throw away all of the template arguments and the first
27382 closing `>', either because the template argument list
27383 was erroneous or because we are replacing those tokens
27384 with a CPP_TEMPLATE_ID token. The second `>' (which will
27385 not have been thrown away) is needed either to close an
27386 outer template argument list or to complete a new-style
27387 cast. */
27388 cp_token *token = cp_lexer_peek_token (parser->lexer);
27389 token->type = CPP_GREATER;
27391 else if (!saved_greater_than_is_operator_p)
27393 /* If we're in a nested template argument list, the '>>' has
27394 to be a typo for '> >'. We emit the error message, but we
27395 continue parsing and we push a '>' as next token, so that
27396 the argument list will be parsed correctly. Note that the
27397 global source location is still on the token before the
27398 '>>', so we need to say explicitly where we want it. */
27399 cp_token *token = cp_lexer_peek_token (parser->lexer);
27400 gcc_rich_location richloc (token->location);
27401 richloc.add_fixit_replace ("> >");
27402 error_at (&richloc, "%<>>%> should be %<> >%> "
27403 "within a nested template argument list");
27405 token->type = CPP_GREATER;
27407 else
27409 /* If this is not a nested template argument list, the '>>'
27410 is a typo for '>'. Emit an error message and continue.
27411 Same deal about the token location, but here we can get it
27412 right by consuming the '>>' before issuing the diagnostic. */
27413 cp_token *token = cp_lexer_consume_token (parser->lexer);
27414 error_at (token->location,
27415 "spurious %<>>%>, use %<>%> to terminate "
27416 "a template argument list");
27419 else
27420 cp_parser_skip_to_end_of_template_parameter_list (parser);
27421 /* The `>' token might be a greater-than operator again now. */
27422 parser->greater_than_is_operator_p
27423 = saved_greater_than_is_operator_p;
27424 /* Restore the SAVED_SCOPE. */
27425 parser->scope = saved_scope;
27426 parser->qualifying_scope = saved_qualifying_scope;
27427 parser->object_scope = saved_object_scope;
27428 cp_unevaluated_operand = saved_unevaluated_operand;
27429 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27431 return arguments;
27434 /* MEMBER_FUNCTION is a member function, or a friend. If default
27435 arguments, or the body of the function have not yet been parsed,
27436 parse them now. */
27438 static void
27439 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27441 timevar_push (TV_PARSE_INMETH);
27442 /* If this member is a template, get the underlying
27443 FUNCTION_DECL. */
27444 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27445 member_function = DECL_TEMPLATE_RESULT (member_function);
27447 /* There should not be any class definitions in progress at this
27448 point; the bodies of members are only parsed outside of all class
27449 definitions. */
27450 gcc_assert (parser->num_classes_being_defined == 0);
27451 /* While we're parsing the member functions we might encounter more
27452 classes. We want to handle them right away, but we don't want
27453 them getting mixed up with functions that are currently in the
27454 queue. */
27455 push_unparsed_function_queues (parser);
27457 /* Make sure that any template parameters are in scope. */
27458 maybe_begin_member_template_processing (member_function);
27460 /* If the body of the function has not yet been parsed, parse it
27461 now. */
27462 if (DECL_PENDING_INLINE_P (member_function))
27464 tree function_scope;
27465 cp_token_cache *tokens;
27467 /* The function is no longer pending; we are processing it. */
27468 tokens = DECL_PENDING_INLINE_INFO (member_function);
27469 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27470 DECL_PENDING_INLINE_P (member_function) = 0;
27472 /* If this is a local class, enter the scope of the containing
27473 function. */
27474 function_scope = current_function_decl;
27475 if (function_scope)
27476 push_function_context ();
27478 /* Push the body of the function onto the lexer stack. */
27479 cp_parser_push_lexer_for_tokens (parser, tokens);
27481 /* Let the front end know that we going to be defining this
27482 function. */
27483 start_preparsed_function (member_function, NULL_TREE,
27484 SF_PRE_PARSED | SF_INCLASS_INLINE);
27486 /* Don't do access checking if it is a templated function. */
27487 if (processing_template_decl)
27488 push_deferring_access_checks (dk_no_check);
27490 /* #pragma omp declare reduction needs special parsing. */
27491 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27493 parser->lexer->in_pragma = true;
27494 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27495 finish_function (/*inline_p=*/true);
27496 cp_check_omp_declare_reduction (member_function);
27498 else
27499 /* Now, parse the body of the function. */
27500 cp_parser_function_definition_after_declarator (parser,
27501 /*inline_p=*/true);
27503 if (processing_template_decl)
27504 pop_deferring_access_checks ();
27506 /* Leave the scope of the containing function. */
27507 if (function_scope)
27508 pop_function_context ();
27509 cp_parser_pop_lexer (parser);
27512 /* Remove any template parameters from the symbol table. */
27513 maybe_end_member_template_processing ();
27515 /* Restore the queue. */
27516 pop_unparsed_function_queues (parser);
27517 timevar_pop (TV_PARSE_INMETH);
27520 /* If DECL contains any default args, remember it on the unparsed
27521 functions queue. */
27523 static void
27524 cp_parser_save_default_args (cp_parser* parser, tree decl)
27526 tree probe;
27528 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27529 probe;
27530 probe = TREE_CHAIN (probe))
27531 if (TREE_PURPOSE (probe))
27533 cp_default_arg_entry entry = {current_class_type, decl};
27534 vec_safe_push (unparsed_funs_with_default_args, entry);
27535 break;
27539 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27540 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27541 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27542 from the parameter-type-list. */
27544 static tree
27545 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27546 tree default_arg, tree parmtype)
27548 cp_token_cache *tokens;
27549 tree parsed_arg;
27550 bool dummy;
27552 if (default_arg == error_mark_node)
27553 return error_mark_node;
27555 /* Push the saved tokens for the default argument onto the parser's
27556 lexer stack. */
27557 tokens = DEFARG_TOKENS (default_arg);
27558 cp_parser_push_lexer_for_tokens (parser, tokens);
27560 start_lambda_scope (decl);
27562 /* Parse the default argument. */
27563 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27564 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27565 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27567 finish_lambda_scope ();
27569 if (parsed_arg == error_mark_node)
27570 cp_parser_skip_to_end_of_statement (parser);
27572 if (!processing_template_decl)
27574 /* In a non-template class, check conversions now. In a template,
27575 we'll wait and instantiate these as needed. */
27576 if (TREE_CODE (decl) == PARM_DECL)
27577 parsed_arg = check_default_argument (parmtype, parsed_arg,
27578 tf_warning_or_error);
27579 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27580 parsed_arg = error_mark_node;
27581 else
27582 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27585 /* If the token stream has not been completely used up, then
27586 there was extra junk after the end of the default
27587 argument. */
27588 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27590 if (TREE_CODE (decl) == PARM_DECL)
27591 cp_parser_error (parser, "expected %<,%>");
27592 else
27593 cp_parser_error (parser, "expected %<;%>");
27596 /* Revert to the main lexer. */
27597 cp_parser_pop_lexer (parser);
27599 return parsed_arg;
27602 /* FIELD is a non-static data member with an initializer which we saved for
27603 later; parse it now. */
27605 static void
27606 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27608 tree def;
27610 maybe_begin_member_template_processing (field);
27612 push_unparsed_function_queues (parser);
27613 def = cp_parser_late_parse_one_default_arg (parser, field,
27614 DECL_INITIAL (field),
27615 NULL_TREE);
27616 pop_unparsed_function_queues (parser);
27618 maybe_end_member_template_processing ();
27620 DECL_INITIAL (field) = def;
27623 /* FN is a FUNCTION_DECL which may contains a parameter with an
27624 unparsed DEFAULT_ARG. Parse the default args now. This function
27625 assumes that the current scope is the scope in which the default
27626 argument should be processed. */
27628 static void
27629 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27631 bool saved_local_variables_forbidden_p;
27632 tree parm, parmdecl;
27634 /* While we're parsing the default args, we might (due to the
27635 statement expression extension) encounter more classes. We want
27636 to handle them right away, but we don't want them getting mixed
27637 up with default args that are currently in the queue. */
27638 push_unparsed_function_queues (parser);
27640 /* Local variable names (and the `this' keyword) may not appear
27641 in a default argument. */
27642 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27643 parser->local_variables_forbidden_p = true;
27645 push_defarg_context (fn);
27647 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27648 parmdecl = DECL_ARGUMENTS (fn);
27649 parm && parm != void_list_node;
27650 parm = TREE_CHAIN (parm),
27651 parmdecl = DECL_CHAIN (parmdecl))
27653 tree default_arg = TREE_PURPOSE (parm);
27654 tree parsed_arg;
27655 vec<tree, va_gc> *insts;
27656 tree copy;
27657 unsigned ix;
27659 if (!default_arg)
27660 continue;
27662 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27663 /* This can happen for a friend declaration for a function
27664 already declared with default arguments. */
27665 continue;
27667 parsed_arg
27668 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27669 default_arg,
27670 TREE_VALUE (parm));
27671 TREE_PURPOSE (parm) = parsed_arg;
27673 /* Update any instantiations we've already created. */
27674 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27675 vec_safe_iterate (insts, ix, &copy); ix++)
27676 TREE_PURPOSE (copy) = parsed_arg;
27679 pop_defarg_context ();
27681 /* Make sure no default arg is missing. */
27682 check_default_args (fn);
27684 /* Restore the state of local_variables_forbidden_p. */
27685 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27687 /* Restore the queue. */
27688 pop_unparsed_function_queues (parser);
27691 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27693 sizeof ... ( identifier )
27695 where the 'sizeof' token has already been consumed. */
27697 static tree
27698 cp_parser_sizeof_pack (cp_parser *parser)
27700 /* Consume the `...'. */
27701 cp_lexer_consume_token (parser->lexer);
27702 maybe_warn_variadic_templates ();
27704 matching_parens parens;
27705 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27706 if (paren)
27707 parens.consume_open (parser);
27708 else
27709 permerror (cp_lexer_peek_token (parser->lexer)->location,
27710 "%<sizeof...%> argument must be surrounded by parentheses");
27712 cp_token *token = cp_lexer_peek_token (parser->lexer);
27713 tree name = cp_parser_identifier (parser);
27714 if (name == error_mark_node)
27715 return error_mark_node;
27716 /* The name is not qualified. */
27717 parser->scope = NULL_TREE;
27718 parser->qualifying_scope = NULL_TREE;
27719 parser->object_scope = NULL_TREE;
27720 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27721 if (expr == error_mark_node)
27722 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27723 token->location);
27724 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27725 expr = TREE_TYPE (expr);
27726 else if (TREE_CODE (expr) == CONST_DECL)
27727 expr = DECL_INITIAL (expr);
27728 expr = make_pack_expansion (expr);
27729 PACK_EXPANSION_SIZEOF_P (expr) = true;
27731 if (paren)
27732 parens.require_close (parser);
27734 return expr;
27737 /* Parse the operand of `sizeof' (or a similar operator). Returns
27738 either a TYPE or an expression, depending on the form of the
27739 input. The KEYWORD indicates which kind of expression we have
27740 encountered. */
27742 static tree
27743 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27745 tree expr = NULL_TREE;
27746 const char *saved_message;
27747 char *tmp;
27748 bool saved_integral_constant_expression_p;
27749 bool saved_non_integral_constant_expression_p;
27751 /* If it's a `...', then we are computing the length of a parameter
27752 pack. */
27753 if (keyword == RID_SIZEOF
27754 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27755 return cp_parser_sizeof_pack (parser);
27757 /* Types cannot be defined in a `sizeof' expression. Save away the
27758 old message. */
27759 saved_message = parser->type_definition_forbidden_message;
27760 /* And create the new one. */
27761 tmp = concat ("types may not be defined in %<",
27762 IDENTIFIER_POINTER (ridpointers[keyword]),
27763 "%> expressions", NULL);
27764 parser->type_definition_forbidden_message = tmp;
27766 /* The restrictions on constant-expressions do not apply inside
27767 sizeof expressions. */
27768 saved_integral_constant_expression_p
27769 = parser->integral_constant_expression_p;
27770 saved_non_integral_constant_expression_p
27771 = parser->non_integral_constant_expression_p;
27772 parser->integral_constant_expression_p = false;
27774 /* Do not actually evaluate the expression. */
27775 ++cp_unevaluated_operand;
27776 ++c_inhibit_evaluation_warnings;
27777 /* If it's a `(', then we might be looking at the type-id
27778 construction. */
27779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27781 tree type = NULL_TREE;
27783 /* We can't be sure yet whether we're looking at a type-id or an
27784 expression. */
27785 cp_parser_parse_tentatively (parser);
27787 matching_parens parens;
27788 parens.consume_open (parser);
27790 /* Note: as a GNU Extension, compound literals are considered
27791 postfix-expressions as they are in C99, so they are valid
27792 arguments to sizeof. See comment in cp_parser_cast_expression
27793 for details. */
27794 if (cp_parser_compound_literal_p (parser))
27795 cp_parser_simulate_error (parser);
27796 else
27798 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27799 parser->in_type_id_in_expr_p = true;
27800 /* Look for the type-id. */
27801 type = cp_parser_type_id (parser);
27802 /* Look for the closing `)'. */
27803 parens.require_close (parser);
27804 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27807 /* If all went well, then we're done. */
27808 if (cp_parser_parse_definitely (parser))
27810 cp_decl_specifier_seq decl_specs;
27812 /* Build a trivial decl-specifier-seq. */
27813 clear_decl_specs (&decl_specs);
27814 decl_specs.type = type;
27816 /* Call grokdeclarator to figure out what type this is. */
27817 expr = grokdeclarator (NULL,
27818 &decl_specs,
27819 TYPENAME,
27820 /*initialized=*/0,
27821 /*attrlist=*/NULL);
27825 /* If the type-id production did not work out, then we must be
27826 looking at the unary-expression production. */
27827 if (!expr)
27828 expr = cp_parser_unary_expression (parser);
27830 /* Go back to evaluating expressions. */
27831 --cp_unevaluated_operand;
27832 --c_inhibit_evaluation_warnings;
27834 /* Free the message we created. */
27835 free (tmp);
27836 /* And restore the old one. */
27837 parser->type_definition_forbidden_message = saved_message;
27838 parser->integral_constant_expression_p
27839 = saved_integral_constant_expression_p;
27840 parser->non_integral_constant_expression_p
27841 = saved_non_integral_constant_expression_p;
27843 return expr;
27846 /* If the current declaration has no declarator, return true. */
27848 static bool
27849 cp_parser_declares_only_class_p (cp_parser *parser)
27851 /* If the next token is a `;' or a `,' then there is no
27852 declarator. */
27853 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27854 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27857 /* Update the DECL_SPECS to reflect the storage class indicated by
27858 KEYWORD. */
27860 static void
27861 cp_parser_set_storage_class (cp_parser *parser,
27862 cp_decl_specifier_seq *decl_specs,
27863 enum rid keyword,
27864 cp_token *token)
27866 cp_storage_class storage_class;
27868 if (parser->in_unbraced_linkage_specification_p)
27870 error_at (token->location, "invalid use of %qD in linkage specification",
27871 ridpointers[keyword]);
27872 return;
27874 else if (decl_specs->storage_class != sc_none)
27876 decl_specs->conflicting_specifiers_p = true;
27877 return;
27880 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27881 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27882 && decl_specs->gnu_thread_keyword_p)
27884 pedwarn (decl_specs->locations[ds_thread], 0,
27885 "%<__thread%> before %qD", ridpointers[keyword]);
27888 switch (keyword)
27890 case RID_AUTO:
27891 storage_class = sc_auto;
27892 break;
27893 case RID_REGISTER:
27894 storage_class = sc_register;
27895 break;
27896 case RID_STATIC:
27897 storage_class = sc_static;
27898 break;
27899 case RID_EXTERN:
27900 storage_class = sc_extern;
27901 break;
27902 case RID_MUTABLE:
27903 storage_class = sc_mutable;
27904 break;
27905 default:
27906 gcc_unreachable ();
27908 decl_specs->storage_class = storage_class;
27909 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27911 /* A storage class specifier cannot be applied alongside a typedef
27912 specifier. If there is a typedef specifier present then set
27913 conflicting_specifiers_p which will trigger an error later
27914 on in grokdeclarator. */
27915 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27916 decl_specs->conflicting_specifiers_p = true;
27919 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27920 is true, the type is a class or enum definition. */
27922 static void
27923 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27924 tree type_spec,
27925 cp_token *token,
27926 bool type_definition_p)
27928 decl_specs->any_specifiers_p = true;
27930 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27931 (with, for example, in "typedef int wchar_t;") we remember that
27932 this is what happened. In system headers, we ignore these
27933 declarations so that G++ can work with system headers that are not
27934 C++-safe. */
27935 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27936 && !type_definition_p
27937 && (type_spec == boolean_type_node
27938 || type_spec == char16_type_node
27939 || type_spec == char32_type_node
27940 || type_spec == wchar_type_node)
27941 && (decl_specs->type
27942 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27943 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27944 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27945 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27947 decl_specs->redefined_builtin_type = type_spec;
27948 set_and_check_decl_spec_loc (decl_specs,
27949 ds_redefined_builtin_type_spec,
27950 token);
27951 if (!decl_specs->type)
27953 decl_specs->type = type_spec;
27954 decl_specs->type_definition_p = false;
27955 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27958 else if (decl_specs->type)
27959 decl_specs->multiple_types_p = true;
27960 else
27962 decl_specs->type = type_spec;
27963 decl_specs->type_definition_p = type_definition_p;
27964 decl_specs->redefined_builtin_type = NULL_TREE;
27965 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27969 /* True iff TOKEN is the GNU keyword __thread. */
27971 static bool
27972 token_is__thread (cp_token *token)
27974 gcc_assert (token->keyword == RID_THREAD);
27975 return id_equal (token->u.value, "__thread");
27978 /* Set the location for a declarator specifier and check if it is
27979 duplicated.
27981 DECL_SPECS is the sequence of declarator specifiers onto which to
27982 set the location.
27984 DS is the single declarator specifier to set which location is to
27985 be set onto the existing sequence of declarators.
27987 LOCATION is the location for the declarator specifier to
27988 consider. */
27990 static void
27991 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27992 cp_decl_spec ds, cp_token *token)
27994 gcc_assert (ds < ds_last);
27996 if (decl_specs == NULL)
27997 return;
27999 source_location location = token->location;
28001 if (decl_specs->locations[ds] == 0)
28003 decl_specs->locations[ds] = location;
28004 if (ds == ds_thread)
28005 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28007 else
28009 if (ds == ds_long)
28011 if (decl_specs->locations[ds_long_long] != 0)
28012 error_at (location,
28013 "%<long long long%> is too long for GCC");
28014 else
28016 decl_specs->locations[ds_long_long] = location;
28017 pedwarn_cxx98 (location,
28018 OPT_Wlong_long,
28019 "ISO C++ 1998 does not support %<long long%>");
28022 else if (ds == ds_thread)
28024 bool gnu = token_is__thread (token);
28025 if (gnu != decl_specs->gnu_thread_keyword_p)
28026 error_at (location,
28027 "both %<__thread%> and %<thread_local%> specified");
28028 else
28030 gcc_rich_location richloc (location);
28031 richloc.add_fixit_remove ();
28032 error_at (&richloc, "duplicate %qD", token->u.value);
28035 else
28037 static const char *const decl_spec_names[] = {
28038 "signed",
28039 "unsigned",
28040 "short",
28041 "long",
28042 "const",
28043 "volatile",
28044 "restrict",
28045 "inline",
28046 "virtual",
28047 "explicit",
28048 "friend",
28049 "typedef",
28050 "using",
28051 "constexpr",
28052 "__complex"
28054 gcc_rich_location richloc (location);
28055 richloc.add_fixit_remove ();
28056 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28061 /* Return true iff the declarator specifier DS is present in the
28062 sequence of declarator specifiers DECL_SPECS. */
28064 bool
28065 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28066 cp_decl_spec ds)
28068 gcc_assert (ds < ds_last);
28070 if (decl_specs == NULL)
28071 return false;
28073 return decl_specs->locations[ds] != 0;
28076 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28077 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28079 static bool
28080 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28082 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28085 /* Issue an error message indicating that TOKEN_DESC was expected.
28086 If KEYWORD is true, it indicated this function is called by
28087 cp_parser_require_keword and the required token can only be
28088 a indicated keyword.
28090 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28091 within any error as the location of an "opening" token matching
28092 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28093 RT_CLOSE_PAREN). */
28095 static void
28096 cp_parser_required_error (cp_parser *parser,
28097 required_token token_desc,
28098 bool keyword,
28099 location_t matching_location)
28101 if (cp_parser_simulate_error (parser))
28102 return;
28104 const char *gmsgid = NULL;
28105 switch (token_desc)
28107 case RT_NEW:
28108 gmsgid = G_("expected %<new%>");
28109 break;
28110 case RT_DELETE:
28111 gmsgid = G_("expected %<delete%>");
28112 break;
28113 case RT_RETURN:
28114 gmsgid = G_("expected %<return%>");
28115 break;
28116 case RT_WHILE:
28117 gmsgid = G_("expected %<while%>");
28118 break;
28119 case RT_EXTERN:
28120 gmsgid = G_("expected %<extern%>");
28121 break;
28122 case RT_STATIC_ASSERT:
28123 gmsgid = G_("expected %<static_assert%>");
28124 break;
28125 case RT_DECLTYPE:
28126 gmsgid = G_("expected %<decltype%>");
28127 break;
28128 case RT_OPERATOR:
28129 gmsgid = G_("expected %<operator%>");
28130 break;
28131 case RT_CLASS:
28132 gmsgid = G_("expected %<class%>");
28133 break;
28134 case RT_TEMPLATE:
28135 gmsgid = G_("expected %<template%>");
28136 break;
28137 case RT_NAMESPACE:
28138 gmsgid = G_("expected %<namespace%>");
28139 break;
28140 case RT_USING:
28141 gmsgid = G_("expected %<using%>");
28142 break;
28143 case RT_ASM:
28144 gmsgid = G_("expected %<asm%>");
28145 break;
28146 case RT_TRY:
28147 gmsgid = G_("expected %<try%>");
28148 break;
28149 case RT_CATCH:
28150 gmsgid = G_("expected %<catch%>");
28151 break;
28152 case RT_THROW:
28153 gmsgid = G_("expected %<throw%>");
28154 break;
28155 case RT_LABEL:
28156 gmsgid = G_("expected %<__label__%>");
28157 break;
28158 case RT_AT_TRY:
28159 gmsgid = G_("expected %<@try%>");
28160 break;
28161 case RT_AT_SYNCHRONIZED:
28162 gmsgid = G_("expected %<@synchronized%>");
28163 break;
28164 case RT_AT_THROW:
28165 gmsgid = G_("expected %<@throw%>");
28166 break;
28167 case RT_TRANSACTION_ATOMIC:
28168 gmsgid = G_("expected %<__transaction_atomic%>");
28169 break;
28170 case RT_TRANSACTION_RELAXED:
28171 gmsgid = G_("expected %<__transaction_relaxed%>");
28172 break;
28173 default:
28174 break;
28177 if (!gmsgid && !keyword)
28179 switch (token_desc)
28181 case RT_SEMICOLON:
28182 gmsgid = G_("expected %<;%>");
28183 break;
28184 case RT_OPEN_PAREN:
28185 gmsgid = G_("expected %<(%>");
28186 break;
28187 case RT_CLOSE_BRACE:
28188 gmsgid = G_("expected %<}%>");
28189 break;
28190 case RT_OPEN_BRACE:
28191 gmsgid = G_("expected %<{%>");
28192 break;
28193 case RT_CLOSE_SQUARE:
28194 gmsgid = G_("expected %<]%>");
28195 break;
28196 case RT_OPEN_SQUARE:
28197 gmsgid = G_("expected %<[%>");
28198 break;
28199 case RT_COMMA:
28200 gmsgid = G_("expected %<,%>");
28201 break;
28202 case RT_SCOPE:
28203 gmsgid = G_("expected %<::%>");
28204 break;
28205 case RT_LESS:
28206 gmsgid = G_("expected %<<%>");
28207 break;
28208 case RT_GREATER:
28209 gmsgid = G_("expected %<>%>");
28210 break;
28211 case RT_EQ:
28212 gmsgid = G_("expected %<=%>");
28213 break;
28214 case RT_ELLIPSIS:
28215 gmsgid = G_("expected %<...%>");
28216 break;
28217 case RT_MULT:
28218 gmsgid = G_("expected %<*%>");
28219 break;
28220 case RT_COMPL:
28221 gmsgid = G_("expected %<~%>");
28222 break;
28223 case RT_COLON:
28224 gmsgid = G_("expected %<:%>");
28225 break;
28226 case RT_COLON_SCOPE:
28227 gmsgid = G_("expected %<:%> or %<::%>");
28228 break;
28229 case RT_CLOSE_PAREN:
28230 gmsgid = G_("expected %<)%>");
28231 break;
28232 case RT_COMMA_CLOSE_PAREN:
28233 gmsgid = G_("expected %<,%> or %<)%>");
28234 break;
28235 case RT_PRAGMA_EOL:
28236 gmsgid = G_("expected end of line");
28237 break;
28238 case RT_NAME:
28239 gmsgid = G_("expected identifier");
28240 break;
28241 case RT_SELECT:
28242 gmsgid = G_("expected selection-statement");
28243 break;
28244 case RT_ITERATION:
28245 gmsgid = G_("expected iteration-statement");
28246 break;
28247 case RT_JUMP:
28248 gmsgid = G_("expected jump-statement");
28249 break;
28250 case RT_CLASS_KEY:
28251 gmsgid = G_("expected class-key");
28252 break;
28253 case RT_CLASS_TYPENAME_TEMPLATE:
28254 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28255 break;
28256 default:
28257 gcc_unreachable ();
28261 if (gmsgid)
28262 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28266 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28267 issue an error message indicating that TOKEN_DESC was expected.
28269 Returns the token consumed, if the token had the appropriate type.
28270 Otherwise, returns NULL.
28272 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28273 within any error as the location of an "opening" token matching
28274 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28275 RT_CLOSE_PAREN). */
28277 static cp_token *
28278 cp_parser_require (cp_parser* parser,
28279 enum cpp_ttype type,
28280 required_token token_desc,
28281 location_t matching_location)
28283 if (cp_lexer_next_token_is (parser->lexer, type))
28284 return cp_lexer_consume_token (parser->lexer);
28285 else
28287 /* Output the MESSAGE -- unless we're parsing tentatively. */
28288 if (!cp_parser_simulate_error (parser))
28289 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28290 matching_location);
28291 return NULL;
28295 /* An error message is produced if the next token is not '>'.
28296 All further tokens are skipped until the desired token is
28297 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28299 static void
28300 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28302 /* Current level of '< ... >'. */
28303 unsigned level = 0;
28304 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28305 unsigned nesting_depth = 0;
28307 /* Are we ready, yet? If not, issue error message. */
28308 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28309 return;
28311 /* Skip tokens until the desired token is found. */
28312 while (true)
28314 /* Peek at the next token. */
28315 switch (cp_lexer_peek_token (parser->lexer)->type)
28317 case CPP_LESS:
28318 if (!nesting_depth)
28319 ++level;
28320 break;
28322 case CPP_RSHIFT:
28323 if (cxx_dialect == cxx98)
28324 /* C++0x views the `>>' operator as two `>' tokens, but
28325 C++98 does not. */
28326 break;
28327 else if (!nesting_depth && level-- == 0)
28329 /* We've hit a `>>' where the first `>' closes the
28330 template argument list, and the second `>' is
28331 spurious. Just consume the `>>' and stop; we've
28332 already produced at least one error. */
28333 cp_lexer_consume_token (parser->lexer);
28334 return;
28336 /* Fall through for C++0x, so we handle the second `>' in
28337 the `>>'. */
28338 gcc_fallthrough ();
28340 case CPP_GREATER:
28341 if (!nesting_depth && level-- == 0)
28343 /* We've reached the token we want, consume it and stop. */
28344 cp_lexer_consume_token (parser->lexer);
28345 return;
28347 break;
28349 case CPP_OPEN_PAREN:
28350 case CPP_OPEN_SQUARE:
28351 ++nesting_depth;
28352 break;
28354 case CPP_CLOSE_PAREN:
28355 case CPP_CLOSE_SQUARE:
28356 if (nesting_depth-- == 0)
28357 return;
28358 break;
28360 case CPP_EOF:
28361 case CPP_PRAGMA_EOL:
28362 case CPP_SEMICOLON:
28363 case CPP_OPEN_BRACE:
28364 case CPP_CLOSE_BRACE:
28365 /* The '>' was probably forgotten, don't look further. */
28366 return;
28368 default:
28369 break;
28372 /* Consume this token. */
28373 cp_lexer_consume_token (parser->lexer);
28377 /* If the next token is the indicated keyword, consume it. Otherwise,
28378 issue an error message indicating that TOKEN_DESC was expected.
28380 Returns the token consumed, if the token had the appropriate type.
28381 Otherwise, returns NULL. */
28383 static cp_token *
28384 cp_parser_require_keyword (cp_parser* parser,
28385 enum rid keyword,
28386 required_token token_desc)
28388 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28390 if (token && token->keyword != keyword)
28392 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28393 UNKNOWN_LOCATION);
28394 return NULL;
28397 return token;
28400 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28401 function-definition. */
28403 static bool
28404 cp_parser_token_starts_function_definition_p (cp_token* token)
28406 return (/* An ordinary function-body begins with an `{'. */
28407 token->type == CPP_OPEN_BRACE
28408 /* A ctor-initializer begins with a `:'. */
28409 || token->type == CPP_COLON
28410 /* A function-try-block begins with `try'. */
28411 || token->keyword == RID_TRY
28412 /* A function-transaction-block begins with `__transaction_atomic'
28413 or `__transaction_relaxed'. */
28414 || token->keyword == RID_TRANSACTION_ATOMIC
28415 || token->keyword == RID_TRANSACTION_RELAXED
28416 /* The named return value extension begins with `return'. */
28417 || token->keyword == RID_RETURN);
28420 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28421 definition. */
28423 static bool
28424 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28426 cp_token *token;
28428 token = cp_lexer_peek_token (parser->lexer);
28429 return (token->type == CPP_OPEN_BRACE
28430 || (token->type == CPP_COLON
28431 && !parser->colon_doesnt_start_class_def_p));
28434 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28435 C++0x) ending a template-argument. */
28437 static bool
28438 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28440 cp_token *token;
28442 token = cp_lexer_peek_token (parser->lexer);
28443 return (token->type == CPP_COMMA
28444 || token->type == CPP_GREATER
28445 || token->type == CPP_ELLIPSIS
28446 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28449 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28450 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28452 static bool
28453 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28454 size_t n)
28456 cp_token *token;
28458 token = cp_lexer_peek_nth_token (parser->lexer, n);
28459 if (token->type == CPP_LESS)
28460 return true;
28461 /* Check for the sequence `<::' in the original code. It would be lexed as
28462 `[:', where `[' is a digraph, and there is no whitespace before
28463 `:'. */
28464 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28466 cp_token *token2;
28467 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28468 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28469 return true;
28471 return false;
28474 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28475 or none_type otherwise. */
28477 static enum tag_types
28478 cp_parser_token_is_class_key (cp_token* token)
28480 switch (token->keyword)
28482 case RID_CLASS:
28483 return class_type;
28484 case RID_STRUCT:
28485 return record_type;
28486 case RID_UNION:
28487 return union_type;
28489 default:
28490 return none_type;
28494 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28495 or none_type otherwise or if the token is null. */
28497 static enum tag_types
28498 cp_parser_token_is_type_parameter_key (cp_token* token)
28500 if (!token)
28501 return none_type;
28503 switch (token->keyword)
28505 case RID_CLASS:
28506 return class_type;
28507 case RID_TYPENAME:
28508 return typename_type;
28510 default:
28511 return none_type;
28515 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28517 static void
28518 cp_parser_check_class_key (enum tag_types class_key, tree type)
28520 if (type == error_mark_node)
28521 return;
28522 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28524 if (permerror (input_location, "%qs tag used in naming %q#T",
28525 class_key == union_type ? "union"
28526 : class_key == record_type ? "struct" : "class",
28527 type))
28528 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28529 "%q#T was previously declared here", type);
28533 /* Issue an error message if DECL is redeclared with different
28534 access than its original declaration [class.access.spec/3].
28535 This applies to nested classes, nested class templates and
28536 enumerations [class.mem/1]. */
28538 static void
28539 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28541 if (!decl
28542 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28543 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28544 return;
28546 if ((TREE_PRIVATE (decl)
28547 != (current_access_specifier == access_private_node))
28548 || (TREE_PROTECTED (decl)
28549 != (current_access_specifier == access_protected_node)))
28550 error_at (location, "%qD redeclared with different access", decl);
28553 /* Look for the `template' keyword, as a syntactic disambiguator.
28554 Return TRUE iff it is present, in which case it will be
28555 consumed. */
28557 static bool
28558 cp_parser_optional_template_keyword (cp_parser *parser)
28560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28562 /* In C++98 the `template' keyword can only be used within templates;
28563 outside templates the parser can always figure out what is a
28564 template and what is not. In C++11, per the resolution of DR 468,
28565 `template' is allowed in cases where it is not strictly necessary. */
28566 if (!processing_template_decl
28567 && pedantic && cxx_dialect == cxx98)
28569 cp_token *token = cp_lexer_peek_token (parser->lexer);
28570 pedwarn (token->location, OPT_Wpedantic,
28571 "in C++98 %<template%> (as a disambiguator) is only "
28572 "allowed within templates");
28573 /* If this part of the token stream is rescanned, the same
28574 error message would be generated. So, we purge the token
28575 from the stream. */
28576 cp_lexer_purge_token (parser->lexer);
28577 return false;
28579 else
28581 /* Consume the `template' keyword. */
28582 cp_lexer_consume_token (parser->lexer);
28583 return true;
28586 return false;
28589 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28590 set PARSER->SCOPE, and perform other related actions. */
28592 static void
28593 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28595 struct tree_check *check_value;
28597 /* Get the stored value. */
28598 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28599 /* Set the scope from the stored value. */
28600 parser->scope = saved_checks_value (check_value);
28601 parser->qualifying_scope = check_value->qualifying_scope;
28602 parser->object_scope = NULL_TREE;
28605 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28606 encounter the end of a block before what we were looking for. */
28608 static bool
28609 cp_parser_cache_group (cp_parser *parser,
28610 enum cpp_ttype end,
28611 unsigned depth)
28613 while (true)
28615 cp_token *token = cp_lexer_peek_token (parser->lexer);
28617 /* Abort a parenthesized expression if we encounter a semicolon. */
28618 if ((end == CPP_CLOSE_PAREN || depth == 0)
28619 && token->type == CPP_SEMICOLON)
28620 return true;
28621 /* If we've reached the end of the file, stop. */
28622 if (token->type == CPP_EOF
28623 || (end != CPP_PRAGMA_EOL
28624 && token->type == CPP_PRAGMA_EOL))
28625 return true;
28626 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28627 /* We've hit the end of an enclosing block, so there's been some
28628 kind of syntax error. */
28629 return true;
28631 /* Consume the token. */
28632 cp_lexer_consume_token (parser->lexer);
28633 /* See if it starts a new group. */
28634 if (token->type == CPP_OPEN_BRACE)
28636 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28637 /* In theory this should probably check end == '}', but
28638 cp_parser_save_member_function_body needs it to exit
28639 after either '}' or ')' when called with ')'. */
28640 if (depth == 0)
28641 return false;
28643 else if (token->type == CPP_OPEN_PAREN)
28645 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28646 if (depth == 0 && end == CPP_CLOSE_PAREN)
28647 return false;
28649 else if (token->type == CPP_PRAGMA)
28650 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28651 else if (token->type == end)
28652 return false;
28656 /* Like above, for caching a default argument or NSDMI. Both of these are
28657 terminated by a non-nested comma, but it can be unclear whether or not a
28658 comma is nested in a template argument list unless we do more parsing.
28659 In order to handle this ambiguity, when we encounter a ',' after a '<'
28660 we try to parse what follows as a parameter-declaration-list (in the
28661 case of a default argument) or a member-declarator (in the case of an
28662 NSDMI). If that succeeds, then we stop caching. */
28664 static tree
28665 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28667 unsigned depth = 0;
28668 int maybe_template_id = 0;
28669 cp_token *first_token;
28670 cp_token *token;
28671 tree default_argument;
28673 /* Add tokens until we have processed the entire default
28674 argument. We add the range [first_token, token). */
28675 first_token = cp_lexer_peek_token (parser->lexer);
28676 if (first_token->type == CPP_OPEN_BRACE)
28678 /* For list-initialization, this is straightforward. */
28679 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28680 token = cp_lexer_peek_token (parser->lexer);
28682 else while (true)
28684 bool done = false;
28686 /* Peek at the next token. */
28687 token = cp_lexer_peek_token (parser->lexer);
28688 /* What we do depends on what token we have. */
28689 switch (token->type)
28691 /* In valid code, a default argument must be
28692 immediately followed by a `,' `)', or `...'. */
28693 case CPP_COMMA:
28694 if (depth == 0 && maybe_template_id)
28696 /* If we've seen a '<', we might be in a
28697 template-argument-list. Until Core issue 325 is
28698 resolved, we don't know how this situation ought
28699 to be handled, so try to DTRT. We check whether
28700 what comes after the comma is a valid parameter
28701 declaration list. If it is, then the comma ends
28702 the default argument; otherwise the default
28703 argument continues. */
28704 bool error = false;
28705 cp_token *peek;
28707 /* Set ITALP so cp_parser_parameter_declaration_list
28708 doesn't decide to commit to this parse. */
28709 bool saved_italp = parser->in_template_argument_list_p;
28710 parser->in_template_argument_list_p = true;
28712 cp_parser_parse_tentatively (parser);
28714 if (nsdmi)
28716 /* Parse declarators until we reach a non-comma or
28717 somthing that cannot be an initializer.
28718 Just checking whether we're looking at a single
28719 declarator is insufficient. Consider:
28720 int var = tuple<T,U>::x;
28721 The template parameter 'U' looks exactly like a
28722 declarator. */
28725 int ctor_dtor_or_conv_p;
28726 cp_lexer_consume_token (parser->lexer);
28727 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28728 &ctor_dtor_or_conv_p,
28729 /*parenthesized_p=*/NULL,
28730 /*member_p=*/true,
28731 /*friend_p=*/false);
28732 peek = cp_lexer_peek_token (parser->lexer);
28733 if (cp_parser_error_occurred (parser))
28734 break;
28736 while (peek->type == CPP_COMMA);
28737 /* If we met an '=' or ';' then the original comma
28738 was the end of the NSDMI. Otherwise assume
28739 we're still in the NSDMI. */
28740 error = (peek->type != CPP_EQ
28741 && peek->type != CPP_SEMICOLON);
28743 else
28745 cp_lexer_consume_token (parser->lexer);
28746 begin_scope (sk_function_parms, NULL_TREE);
28747 cp_parser_parameter_declaration_list (parser, &error);
28748 pop_bindings_and_leave_scope ();
28750 if (!cp_parser_error_occurred (parser) && !error)
28751 done = true;
28752 cp_parser_abort_tentative_parse (parser);
28754 parser->in_template_argument_list_p = saved_italp;
28755 break;
28757 /* FALLTHRU */
28758 case CPP_CLOSE_PAREN:
28759 case CPP_ELLIPSIS:
28760 /* If we run into a non-nested `;', `}', or `]',
28761 then the code is invalid -- but the default
28762 argument is certainly over. */
28763 case CPP_SEMICOLON:
28764 case CPP_CLOSE_BRACE:
28765 case CPP_CLOSE_SQUARE:
28766 if (depth == 0
28767 /* Handle correctly int n = sizeof ... ( p ); */
28768 && token->type != CPP_ELLIPSIS)
28769 done = true;
28770 /* Update DEPTH, if necessary. */
28771 else if (token->type == CPP_CLOSE_PAREN
28772 || token->type == CPP_CLOSE_BRACE
28773 || token->type == CPP_CLOSE_SQUARE)
28774 --depth;
28775 break;
28777 case CPP_OPEN_PAREN:
28778 case CPP_OPEN_SQUARE:
28779 case CPP_OPEN_BRACE:
28780 ++depth;
28781 break;
28783 case CPP_LESS:
28784 if (depth == 0)
28785 /* This might be the comparison operator, or it might
28786 start a template argument list. */
28787 ++maybe_template_id;
28788 break;
28790 case CPP_RSHIFT:
28791 if (cxx_dialect == cxx98)
28792 break;
28793 /* Fall through for C++0x, which treats the `>>'
28794 operator like two `>' tokens in certain
28795 cases. */
28796 gcc_fallthrough ();
28798 case CPP_GREATER:
28799 if (depth == 0)
28801 /* This might be an operator, or it might close a
28802 template argument list. But if a previous '<'
28803 started a template argument list, this will have
28804 closed it, so we can't be in one anymore. */
28805 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28806 if (maybe_template_id < 0)
28807 maybe_template_id = 0;
28809 break;
28811 /* If we run out of tokens, issue an error message. */
28812 case CPP_EOF:
28813 case CPP_PRAGMA_EOL:
28814 error_at (token->location, "file ends in default argument");
28815 return error_mark_node;
28817 case CPP_NAME:
28818 case CPP_SCOPE:
28819 /* In these cases, we should look for template-ids.
28820 For example, if the default argument is
28821 `X<int, double>()', we need to do name lookup to
28822 figure out whether or not `X' is a template; if
28823 so, the `,' does not end the default argument.
28825 That is not yet done. */
28826 break;
28828 default:
28829 break;
28832 /* If we've reached the end, stop. */
28833 if (done)
28834 break;
28836 /* Add the token to the token block. */
28837 token = cp_lexer_consume_token (parser->lexer);
28840 /* Create a DEFAULT_ARG to represent the unparsed default
28841 argument. */
28842 default_argument = make_node (DEFAULT_ARG);
28843 DEFARG_TOKENS (default_argument)
28844 = cp_token_cache_new (first_token, token);
28845 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28847 return default_argument;
28850 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28852 location_t
28853 defarg_location (tree default_argument)
28855 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28856 location_t start = tokens->first->location;
28857 location_t end = tokens->last->location;
28858 return make_location (start, start, end);
28861 /* Begin parsing tentatively. We always save tokens while parsing
28862 tentatively so that if the tentative parsing fails we can restore the
28863 tokens. */
28865 static void
28866 cp_parser_parse_tentatively (cp_parser* parser)
28868 /* Enter a new parsing context. */
28869 parser->context = cp_parser_context_new (parser->context);
28870 /* Begin saving tokens. */
28871 cp_lexer_save_tokens (parser->lexer);
28872 /* In order to avoid repetitive access control error messages,
28873 access checks are queued up until we are no longer parsing
28874 tentatively. */
28875 push_deferring_access_checks (dk_deferred);
28878 /* Commit to the currently active tentative parse. */
28880 static void
28881 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28883 cp_parser_context *context;
28884 cp_lexer *lexer;
28886 /* Mark all of the levels as committed. */
28887 lexer = parser->lexer;
28888 for (context = parser->context; context->next; context = context->next)
28890 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28891 break;
28892 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28893 while (!cp_lexer_saving_tokens (lexer))
28894 lexer = lexer->next;
28895 cp_lexer_commit_tokens (lexer);
28899 /* Commit to the topmost currently active tentative parse.
28901 Note that this function shouldn't be called when there are
28902 irreversible side-effects while in a tentative state. For
28903 example, we shouldn't create a permanent entry in the symbol
28904 table, or issue an error message that might not apply if the
28905 tentative parse is aborted. */
28907 static void
28908 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28910 cp_parser_context *context = parser->context;
28911 cp_lexer *lexer = parser->lexer;
28913 if (context)
28915 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28916 return;
28917 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28919 while (!cp_lexer_saving_tokens (lexer))
28920 lexer = lexer->next;
28921 cp_lexer_commit_tokens (lexer);
28925 /* Abort the currently active tentative parse. All consumed tokens
28926 will be rolled back, and no diagnostics will be issued. */
28928 static void
28929 cp_parser_abort_tentative_parse (cp_parser* parser)
28931 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28932 || errorcount > 0);
28933 cp_parser_simulate_error (parser);
28934 /* Now, pretend that we want to see if the construct was
28935 successfully parsed. */
28936 cp_parser_parse_definitely (parser);
28939 /* Stop parsing tentatively. If a parse error has occurred, restore the
28940 token stream. Otherwise, commit to the tokens we have consumed.
28941 Returns true if no error occurred; false otherwise. */
28943 static bool
28944 cp_parser_parse_definitely (cp_parser* parser)
28946 bool error_occurred;
28947 cp_parser_context *context;
28949 /* Remember whether or not an error occurred, since we are about to
28950 destroy that information. */
28951 error_occurred = cp_parser_error_occurred (parser);
28952 /* Remove the topmost context from the stack. */
28953 context = parser->context;
28954 parser->context = context->next;
28955 /* If no parse errors occurred, commit to the tentative parse. */
28956 if (!error_occurred)
28958 /* Commit to the tokens read tentatively, unless that was
28959 already done. */
28960 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28961 cp_lexer_commit_tokens (parser->lexer);
28963 pop_to_parent_deferring_access_checks ();
28965 /* Otherwise, if errors occurred, roll back our state so that things
28966 are just as they were before we began the tentative parse. */
28967 else
28969 cp_lexer_rollback_tokens (parser->lexer);
28970 pop_deferring_access_checks ();
28972 /* Add the context to the front of the free list. */
28973 context->next = cp_parser_context_free_list;
28974 cp_parser_context_free_list = context;
28976 return !error_occurred;
28979 /* Returns true if we are parsing tentatively and are not committed to
28980 this tentative parse. */
28982 static bool
28983 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28985 return (cp_parser_parsing_tentatively (parser)
28986 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28989 /* Returns nonzero iff an error has occurred during the most recent
28990 tentative parse. */
28992 static bool
28993 cp_parser_error_occurred (cp_parser* parser)
28995 return (cp_parser_parsing_tentatively (parser)
28996 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28999 /* Returns nonzero if GNU extensions are allowed. */
29001 static bool
29002 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29004 return parser->allow_gnu_extensions_p;
29007 /* Objective-C++ Productions */
29010 /* Parse an Objective-C expression, which feeds into a primary-expression
29011 above.
29013 objc-expression:
29014 objc-message-expression
29015 objc-string-literal
29016 objc-encode-expression
29017 objc-protocol-expression
29018 objc-selector-expression
29020 Returns a tree representation of the expression. */
29022 static cp_expr
29023 cp_parser_objc_expression (cp_parser* parser)
29025 /* Try to figure out what kind of declaration is present. */
29026 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29028 switch (kwd->type)
29030 case CPP_OPEN_SQUARE:
29031 return cp_parser_objc_message_expression (parser);
29033 case CPP_OBJC_STRING:
29034 kwd = cp_lexer_consume_token (parser->lexer);
29035 return objc_build_string_object (kwd->u.value);
29037 case CPP_KEYWORD:
29038 switch (kwd->keyword)
29040 case RID_AT_ENCODE:
29041 return cp_parser_objc_encode_expression (parser);
29043 case RID_AT_PROTOCOL:
29044 return cp_parser_objc_protocol_expression (parser);
29046 case RID_AT_SELECTOR:
29047 return cp_parser_objc_selector_expression (parser);
29049 default:
29050 break;
29052 /* FALLTHRU */
29053 default:
29054 error_at (kwd->location,
29055 "misplaced %<@%D%> Objective-C++ construct",
29056 kwd->u.value);
29057 cp_parser_skip_to_end_of_block_or_statement (parser);
29060 return error_mark_node;
29063 /* Parse an Objective-C message expression.
29065 objc-message-expression:
29066 [ objc-message-receiver objc-message-args ]
29068 Returns a representation of an Objective-C message. */
29070 static tree
29071 cp_parser_objc_message_expression (cp_parser* parser)
29073 tree receiver, messageargs;
29075 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29076 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29077 receiver = cp_parser_objc_message_receiver (parser);
29078 messageargs = cp_parser_objc_message_args (parser);
29079 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29080 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29082 tree result = objc_build_message_expr (receiver, messageargs);
29084 /* Construct a location e.g.
29085 [self func1:5]
29086 ^~~~~~~~~~~~~~
29087 ranging from the '[' to the ']', with the caret at the start. */
29088 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29089 protected_set_expr_location (result, combined_loc);
29091 return result;
29094 /* Parse an objc-message-receiver.
29096 objc-message-receiver:
29097 expression
29098 simple-type-specifier
29100 Returns a representation of the type or expression. */
29102 static tree
29103 cp_parser_objc_message_receiver (cp_parser* parser)
29105 tree rcv;
29107 /* An Objective-C message receiver may be either (1) a type
29108 or (2) an expression. */
29109 cp_parser_parse_tentatively (parser);
29110 rcv = cp_parser_expression (parser);
29112 /* If that worked out, fine. */
29113 if (cp_parser_parse_definitely (parser))
29114 return rcv;
29116 cp_parser_parse_tentatively (parser);
29117 rcv = cp_parser_simple_type_specifier (parser,
29118 /*decl_specs=*/NULL,
29119 CP_PARSER_FLAGS_NONE);
29121 if (cp_parser_parse_definitely (parser))
29122 return objc_get_class_reference (rcv);
29124 cp_parser_error (parser, "objective-c++ message receiver expected");
29125 return error_mark_node;
29128 /* Parse the arguments and selectors comprising an Objective-C message.
29130 objc-message-args:
29131 objc-selector
29132 objc-selector-args
29133 objc-selector-args , objc-comma-args
29135 objc-selector-args:
29136 objc-selector [opt] : assignment-expression
29137 objc-selector-args objc-selector [opt] : assignment-expression
29139 objc-comma-args:
29140 assignment-expression
29141 objc-comma-args , assignment-expression
29143 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29144 selector arguments and TREE_VALUE containing a list of comma
29145 arguments. */
29147 static tree
29148 cp_parser_objc_message_args (cp_parser* parser)
29150 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29151 bool maybe_unary_selector_p = true;
29152 cp_token *token = cp_lexer_peek_token (parser->lexer);
29154 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29156 tree selector = NULL_TREE, arg;
29158 if (token->type != CPP_COLON)
29159 selector = cp_parser_objc_selector (parser);
29161 /* Detect if we have a unary selector. */
29162 if (maybe_unary_selector_p
29163 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29164 return build_tree_list (selector, NULL_TREE);
29166 maybe_unary_selector_p = false;
29167 cp_parser_require (parser, CPP_COLON, RT_COLON);
29168 arg = cp_parser_assignment_expression (parser);
29170 sel_args
29171 = chainon (sel_args,
29172 build_tree_list (selector, arg));
29174 token = cp_lexer_peek_token (parser->lexer);
29177 /* Handle non-selector arguments, if any. */
29178 while (token->type == CPP_COMMA)
29180 tree arg;
29182 cp_lexer_consume_token (parser->lexer);
29183 arg = cp_parser_assignment_expression (parser);
29185 addl_args
29186 = chainon (addl_args,
29187 build_tree_list (NULL_TREE, arg));
29189 token = cp_lexer_peek_token (parser->lexer);
29192 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29194 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29195 return build_tree_list (error_mark_node, error_mark_node);
29198 return build_tree_list (sel_args, addl_args);
29201 /* Parse an Objective-C encode expression.
29203 objc-encode-expression:
29204 @encode objc-typename
29206 Returns an encoded representation of the type argument. */
29208 static cp_expr
29209 cp_parser_objc_encode_expression (cp_parser* parser)
29211 tree type;
29212 cp_token *token;
29213 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29215 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29216 matching_parens parens;
29217 parens.require_open (parser);
29218 token = cp_lexer_peek_token (parser->lexer);
29219 type = complete_type (cp_parser_type_id (parser));
29220 parens.require_close (parser);
29222 if (!type)
29224 error_at (token->location,
29225 "%<@encode%> must specify a type as an argument");
29226 return error_mark_node;
29229 /* This happens if we find @encode(T) (where T is a template
29230 typename or something dependent on a template typename) when
29231 parsing a template. In that case, we can't compile it
29232 immediately, but we rather create an AT_ENCODE_EXPR which will
29233 need to be instantiated when the template is used.
29235 if (dependent_type_p (type))
29237 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29238 TREE_READONLY (value) = 1;
29239 return value;
29243 /* Build a location of the form:
29244 @encode(int)
29245 ^~~~~~~~~~~~
29246 with caret==start at the @ token, finishing at the close paren. */
29247 location_t combined_loc
29248 = make_location (start_loc, start_loc,
29249 cp_lexer_previous_token (parser->lexer)->location);
29251 return cp_expr (objc_build_encode_expr (type), combined_loc);
29254 /* Parse an Objective-C @defs expression. */
29256 static tree
29257 cp_parser_objc_defs_expression (cp_parser *parser)
29259 tree name;
29261 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29262 matching_parens parens;
29263 parens.require_open (parser);
29264 name = cp_parser_identifier (parser);
29265 parens.require_close (parser);
29267 return objc_get_class_ivars (name);
29270 /* Parse an Objective-C protocol expression.
29272 objc-protocol-expression:
29273 @protocol ( identifier )
29275 Returns a representation of the protocol expression. */
29277 static tree
29278 cp_parser_objc_protocol_expression (cp_parser* parser)
29280 tree proto;
29281 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29283 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29284 matching_parens parens;
29285 parens.require_open (parser);
29286 proto = cp_parser_identifier (parser);
29287 parens.require_close (parser);
29289 /* Build a location of the form:
29290 @protocol(prot)
29291 ^~~~~~~~~~~~~~~
29292 with caret==start at the @ token, finishing at the close paren. */
29293 location_t combined_loc
29294 = make_location (start_loc, start_loc,
29295 cp_lexer_previous_token (parser->lexer)->location);
29296 tree result = objc_build_protocol_expr (proto);
29297 protected_set_expr_location (result, combined_loc);
29298 return result;
29301 /* Parse an Objective-C selector expression.
29303 objc-selector-expression:
29304 @selector ( objc-method-signature )
29306 objc-method-signature:
29307 objc-selector
29308 objc-selector-seq
29310 objc-selector-seq:
29311 objc-selector :
29312 objc-selector-seq objc-selector :
29314 Returns a representation of the method selector. */
29316 static tree
29317 cp_parser_objc_selector_expression (cp_parser* parser)
29319 tree sel_seq = NULL_TREE;
29320 bool maybe_unary_selector_p = true;
29321 cp_token *token;
29322 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29324 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29325 matching_parens parens;
29326 parens.require_open (parser);
29327 token = cp_lexer_peek_token (parser->lexer);
29329 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29330 || token->type == CPP_SCOPE)
29332 tree selector = NULL_TREE;
29334 if (token->type != CPP_COLON
29335 || token->type == CPP_SCOPE)
29336 selector = cp_parser_objc_selector (parser);
29338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29339 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29341 /* Detect if we have a unary selector. */
29342 if (maybe_unary_selector_p)
29344 sel_seq = selector;
29345 goto finish_selector;
29347 else
29349 cp_parser_error (parser, "expected %<:%>");
29352 maybe_unary_selector_p = false;
29353 token = cp_lexer_consume_token (parser->lexer);
29355 if (token->type == CPP_SCOPE)
29357 sel_seq
29358 = chainon (sel_seq,
29359 build_tree_list (selector, NULL_TREE));
29360 sel_seq
29361 = chainon (sel_seq,
29362 build_tree_list (NULL_TREE, NULL_TREE));
29364 else
29365 sel_seq
29366 = chainon (sel_seq,
29367 build_tree_list (selector, NULL_TREE));
29369 token = cp_lexer_peek_token (parser->lexer);
29372 finish_selector:
29373 parens.require_close (parser);
29376 /* Build a location of the form:
29377 @selector(func)
29378 ^~~~~~~~~~~~~~~
29379 with caret==start at the @ token, finishing at the close paren. */
29380 location_t combined_loc
29381 = make_location (loc, loc,
29382 cp_lexer_previous_token (parser->lexer)->location);
29383 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29384 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29385 protected_set_expr_location (result, combined_loc);
29386 return result;
29389 /* Parse a list of identifiers.
29391 objc-identifier-list:
29392 identifier
29393 objc-identifier-list , identifier
29395 Returns a TREE_LIST of identifier nodes. */
29397 static tree
29398 cp_parser_objc_identifier_list (cp_parser* parser)
29400 tree identifier;
29401 tree list;
29402 cp_token *sep;
29404 identifier = cp_parser_identifier (parser);
29405 if (identifier == error_mark_node)
29406 return error_mark_node;
29408 list = build_tree_list (NULL_TREE, identifier);
29409 sep = cp_lexer_peek_token (parser->lexer);
29411 while (sep->type == CPP_COMMA)
29413 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29414 identifier = cp_parser_identifier (parser);
29415 if (identifier == error_mark_node)
29416 return list;
29418 list = chainon (list, build_tree_list (NULL_TREE,
29419 identifier));
29420 sep = cp_lexer_peek_token (parser->lexer);
29423 return list;
29426 /* Parse an Objective-C alias declaration.
29428 objc-alias-declaration:
29429 @compatibility_alias identifier identifier ;
29431 This function registers the alias mapping with the Objective-C front end.
29432 It returns nothing. */
29434 static void
29435 cp_parser_objc_alias_declaration (cp_parser* parser)
29437 tree alias, orig;
29439 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29440 alias = cp_parser_identifier (parser);
29441 orig = cp_parser_identifier (parser);
29442 objc_declare_alias (alias, orig);
29443 cp_parser_consume_semicolon_at_end_of_statement (parser);
29446 /* Parse an Objective-C class forward-declaration.
29448 objc-class-declaration:
29449 @class objc-identifier-list ;
29451 The function registers the forward declarations with the Objective-C
29452 front end. It returns nothing. */
29454 static void
29455 cp_parser_objc_class_declaration (cp_parser* parser)
29457 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29458 while (true)
29460 tree id;
29462 id = cp_parser_identifier (parser);
29463 if (id == error_mark_node)
29464 break;
29466 objc_declare_class (id);
29468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29469 cp_lexer_consume_token (parser->lexer);
29470 else
29471 break;
29473 cp_parser_consume_semicolon_at_end_of_statement (parser);
29476 /* Parse a list of Objective-C protocol references.
29478 objc-protocol-refs-opt:
29479 objc-protocol-refs [opt]
29481 objc-protocol-refs:
29482 < objc-identifier-list >
29484 Returns a TREE_LIST of identifiers, if any. */
29486 static tree
29487 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29489 tree protorefs = NULL_TREE;
29491 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29493 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29494 protorefs = cp_parser_objc_identifier_list (parser);
29495 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29498 return protorefs;
29501 /* Parse a Objective-C visibility specification. */
29503 static void
29504 cp_parser_objc_visibility_spec (cp_parser* parser)
29506 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29508 switch (vis->keyword)
29510 case RID_AT_PRIVATE:
29511 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29512 break;
29513 case RID_AT_PROTECTED:
29514 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29515 break;
29516 case RID_AT_PUBLIC:
29517 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29518 break;
29519 case RID_AT_PACKAGE:
29520 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29521 break;
29522 default:
29523 return;
29526 /* Eat '@private'/'@protected'/'@public'. */
29527 cp_lexer_consume_token (parser->lexer);
29530 /* Parse an Objective-C method type. Return 'true' if it is a class
29531 (+) method, and 'false' if it is an instance (-) method. */
29533 static inline bool
29534 cp_parser_objc_method_type (cp_parser* parser)
29536 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29537 return true;
29538 else
29539 return false;
29542 /* Parse an Objective-C protocol qualifier. */
29544 static tree
29545 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29547 tree quals = NULL_TREE, node;
29548 cp_token *token = cp_lexer_peek_token (parser->lexer);
29550 node = token->u.value;
29552 while (node && identifier_p (node)
29553 && (node == ridpointers [(int) RID_IN]
29554 || node == ridpointers [(int) RID_OUT]
29555 || node == ridpointers [(int) RID_INOUT]
29556 || node == ridpointers [(int) RID_BYCOPY]
29557 || node == ridpointers [(int) RID_BYREF]
29558 || node == ridpointers [(int) RID_ONEWAY]))
29560 quals = tree_cons (NULL_TREE, node, quals);
29561 cp_lexer_consume_token (parser->lexer);
29562 token = cp_lexer_peek_token (parser->lexer);
29563 node = token->u.value;
29566 return quals;
29569 /* Parse an Objective-C typename. */
29571 static tree
29572 cp_parser_objc_typename (cp_parser* parser)
29574 tree type_name = NULL_TREE;
29576 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29578 tree proto_quals, cp_type = NULL_TREE;
29580 matching_parens parens;
29581 parens.consume_open (parser); /* Eat '('. */
29582 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29584 /* An ObjC type name may consist of just protocol qualifiers, in which
29585 case the type shall default to 'id'. */
29586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29588 cp_type = cp_parser_type_id (parser);
29590 /* If the type could not be parsed, an error has already
29591 been produced. For error recovery, behave as if it had
29592 not been specified, which will use the default type
29593 'id'. */
29594 if (cp_type == error_mark_node)
29596 cp_type = NULL_TREE;
29597 /* We need to skip to the closing parenthesis as
29598 cp_parser_type_id() does not seem to do it for
29599 us. */
29600 cp_parser_skip_to_closing_parenthesis (parser,
29601 /*recovering=*/true,
29602 /*or_comma=*/false,
29603 /*consume_paren=*/false);
29607 parens.require_close (parser);
29608 type_name = build_tree_list (proto_quals, cp_type);
29611 return type_name;
29614 /* Check to see if TYPE refers to an Objective-C selector name. */
29616 static bool
29617 cp_parser_objc_selector_p (enum cpp_ttype type)
29619 return (type == CPP_NAME || type == CPP_KEYWORD
29620 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29621 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29622 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29623 || type == CPP_XOR || type == CPP_XOR_EQ);
29626 /* Parse an Objective-C selector. */
29628 static tree
29629 cp_parser_objc_selector (cp_parser* parser)
29631 cp_token *token = cp_lexer_consume_token (parser->lexer);
29633 if (!cp_parser_objc_selector_p (token->type))
29635 error_at (token->location, "invalid Objective-C++ selector name");
29636 return error_mark_node;
29639 /* C++ operator names are allowed to appear in ObjC selectors. */
29640 switch (token->type)
29642 case CPP_AND_AND: return get_identifier ("and");
29643 case CPP_AND_EQ: return get_identifier ("and_eq");
29644 case CPP_AND: return get_identifier ("bitand");
29645 case CPP_OR: return get_identifier ("bitor");
29646 case CPP_COMPL: return get_identifier ("compl");
29647 case CPP_NOT: return get_identifier ("not");
29648 case CPP_NOT_EQ: return get_identifier ("not_eq");
29649 case CPP_OR_OR: return get_identifier ("or");
29650 case CPP_OR_EQ: return get_identifier ("or_eq");
29651 case CPP_XOR: return get_identifier ("xor");
29652 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29653 default: return token->u.value;
29657 /* Parse an Objective-C params list. */
29659 static tree
29660 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29662 tree params = NULL_TREE;
29663 bool maybe_unary_selector_p = true;
29664 cp_token *token = cp_lexer_peek_token (parser->lexer);
29666 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29668 tree selector = NULL_TREE, type_name, identifier;
29669 tree parm_attr = NULL_TREE;
29671 if (token->keyword == RID_ATTRIBUTE)
29672 break;
29674 if (token->type != CPP_COLON)
29675 selector = cp_parser_objc_selector (parser);
29677 /* Detect if we have a unary selector. */
29678 if (maybe_unary_selector_p
29679 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29681 params = selector; /* Might be followed by attributes. */
29682 break;
29685 maybe_unary_selector_p = false;
29686 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29688 /* Something went quite wrong. There should be a colon
29689 here, but there is not. Stop parsing parameters. */
29690 break;
29692 type_name = cp_parser_objc_typename (parser);
29693 /* New ObjC allows attributes on parameters too. */
29694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29695 parm_attr = cp_parser_attributes_opt (parser);
29696 identifier = cp_parser_identifier (parser);
29698 params
29699 = chainon (params,
29700 objc_build_keyword_decl (selector,
29701 type_name,
29702 identifier,
29703 parm_attr));
29705 token = cp_lexer_peek_token (parser->lexer);
29708 if (params == NULL_TREE)
29710 cp_parser_error (parser, "objective-c++ method declaration is expected");
29711 return error_mark_node;
29714 /* We allow tail attributes for the method. */
29715 if (token->keyword == RID_ATTRIBUTE)
29717 *attributes = cp_parser_attributes_opt (parser);
29718 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29719 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29720 return params;
29721 cp_parser_error (parser,
29722 "method attributes must be specified at the end");
29723 return error_mark_node;
29726 if (params == NULL_TREE)
29728 cp_parser_error (parser, "objective-c++ method declaration is expected");
29729 return error_mark_node;
29731 return params;
29734 /* Parse the non-keyword Objective-C params. */
29736 static tree
29737 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29738 tree* attributes)
29740 tree params = make_node (TREE_LIST);
29741 cp_token *token = cp_lexer_peek_token (parser->lexer);
29742 *ellipsisp = false; /* Initially, assume no ellipsis. */
29744 while (token->type == CPP_COMMA)
29746 cp_parameter_declarator *parmdecl;
29747 tree parm;
29749 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29750 token = cp_lexer_peek_token (parser->lexer);
29752 if (token->type == CPP_ELLIPSIS)
29754 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29755 *ellipsisp = true;
29756 token = cp_lexer_peek_token (parser->lexer);
29757 break;
29760 /* TODO: parse attributes for tail parameters. */
29761 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29762 parm = grokdeclarator (parmdecl->declarator,
29763 &parmdecl->decl_specifiers,
29764 PARM, /*initialized=*/0,
29765 /*attrlist=*/NULL);
29767 chainon (params, build_tree_list (NULL_TREE, parm));
29768 token = cp_lexer_peek_token (parser->lexer);
29771 /* We allow tail attributes for the method. */
29772 if (token->keyword == RID_ATTRIBUTE)
29774 if (*attributes == NULL_TREE)
29776 *attributes = cp_parser_attributes_opt (parser);
29777 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29778 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29779 return params;
29781 else
29782 /* We have an error, but parse the attributes, so that we can
29783 carry on. */
29784 *attributes = cp_parser_attributes_opt (parser);
29786 cp_parser_error (parser,
29787 "method attributes must be specified at the end");
29788 return error_mark_node;
29791 return params;
29794 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29796 static void
29797 cp_parser_objc_interstitial_code (cp_parser* parser)
29799 cp_token *token = cp_lexer_peek_token (parser->lexer);
29801 /* If the next token is `extern' and the following token is a string
29802 literal, then we have a linkage specification. */
29803 if (token->keyword == RID_EXTERN
29804 && cp_parser_is_pure_string_literal
29805 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29806 cp_parser_linkage_specification (parser);
29807 /* Handle #pragma, if any. */
29808 else if (token->type == CPP_PRAGMA)
29809 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29810 /* Allow stray semicolons. */
29811 else if (token->type == CPP_SEMICOLON)
29812 cp_lexer_consume_token (parser->lexer);
29813 /* Mark methods as optional or required, when building protocols. */
29814 else if (token->keyword == RID_AT_OPTIONAL)
29816 cp_lexer_consume_token (parser->lexer);
29817 objc_set_method_opt (true);
29819 else if (token->keyword == RID_AT_REQUIRED)
29821 cp_lexer_consume_token (parser->lexer);
29822 objc_set_method_opt (false);
29824 else if (token->keyword == RID_NAMESPACE)
29825 cp_parser_namespace_definition (parser);
29826 /* Other stray characters must generate errors. */
29827 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29829 cp_lexer_consume_token (parser->lexer);
29830 error ("stray %qs between Objective-C++ methods",
29831 token->type == CPP_OPEN_BRACE ? "{" : "}");
29833 /* Finally, try to parse a block-declaration, or a function-definition. */
29834 else
29835 cp_parser_block_declaration (parser, /*statement_p=*/false);
29838 /* Parse a method signature. */
29840 static tree
29841 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29843 tree rettype, kwdparms, optparms;
29844 bool ellipsis = false;
29845 bool is_class_method;
29847 is_class_method = cp_parser_objc_method_type (parser);
29848 rettype = cp_parser_objc_typename (parser);
29849 *attributes = NULL_TREE;
29850 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29851 if (kwdparms == error_mark_node)
29852 return error_mark_node;
29853 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29854 if (optparms == error_mark_node)
29855 return error_mark_node;
29857 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29860 static bool
29861 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29863 tree tattr;
29864 cp_lexer_save_tokens (parser->lexer);
29865 tattr = cp_parser_attributes_opt (parser);
29866 gcc_assert (tattr) ;
29868 /* If the attributes are followed by a method introducer, this is not allowed.
29869 Dump the attributes and flag the situation. */
29870 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29871 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29872 return true;
29874 /* Otherwise, the attributes introduce some interstitial code, possibly so
29875 rewind to allow that check. */
29876 cp_lexer_rollback_tokens (parser->lexer);
29877 return false;
29880 /* Parse an Objective-C method prototype list. */
29882 static void
29883 cp_parser_objc_method_prototype_list (cp_parser* parser)
29885 cp_token *token = cp_lexer_peek_token (parser->lexer);
29887 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29889 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29891 tree attributes, sig;
29892 bool is_class_method;
29893 if (token->type == CPP_PLUS)
29894 is_class_method = true;
29895 else
29896 is_class_method = false;
29897 sig = cp_parser_objc_method_signature (parser, &attributes);
29898 if (sig == error_mark_node)
29900 cp_parser_skip_to_end_of_block_or_statement (parser);
29901 token = cp_lexer_peek_token (parser->lexer);
29902 continue;
29904 objc_add_method_declaration (is_class_method, sig, attributes);
29905 cp_parser_consume_semicolon_at_end_of_statement (parser);
29907 else if (token->keyword == RID_AT_PROPERTY)
29908 cp_parser_objc_at_property_declaration (parser);
29909 else if (token->keyword == RID_ATTRIBUTE
29910 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29911 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29912 OPT_Wattributes,
29913 "prefix attributes are ignored for methods");
29914 else
29915 /* Allow for interspersed non-ObjC++ code. */
29916 cp_parser_objc_interstitial_code (parser);
29918 token = cp_lexer_peek_token (parser->lexer);
29921 if (token->type != CPP_EOF)
29922 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29923 else
29924 cp_parser_error (parser, "expected %<@end%>");
29926 objc_finish_interface ();
29929 /* Parse an Objective-C method definition list. */
29931 static void
29932 cp_parser_objc_method_definition_list (cp_parser* parser)
29934 cp_token *token = cp_lexer_peek_token (parser->lexer);
29936 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29938 tree meth;
29940 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29942 cp_token *ptk;
29943 tree sig, attribute;
29944 bool is_class_method;
29945 if (token->type == CPP_PLUS)
29946 is_class_method = true;
29947 else
29948 is_class_method = false;
29949 push_deferring_access_checks (dk_deferred);
29950 sig = cp_parser_objc_method_signature (parser, &attribute);
29951 if (sig == error_mark_node)
29953 cp_parser_skip_to_end_of_block_or_statement (parser);
29954 token = cp_lexer_peek_token (parser->lexer);
29955 continue;
29957 objc_start_method_definition (is_class_method, sig, attribute,
29958 NULL_TREE);
29960 /* For historical reasons, we accept an optional semicolon. */
29961 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29962 cp_lexer_consume_token (parser->lexer);
29964 ptk = cp_lexer_peek_token (parser->lexer);
29965 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29966 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29968 perform_deferred_access_checks (tf_warning_or_error);
29969 stop_deferring_access_checks ();
29970 meth = cp_parser_function_definition_after_declarator (parser,
29971 false);
29972 pop_deferring_access_checks ();
29973 objc_finish_method_definition (meth);
29976 /* The following case will be removed once @synthesize is
29977 completely implemented. */
29978 else if (token->keyword == RID_AT_PROPERTY)
29979 cp_parser_objc_at_property_declaration (parser);
29980 else if (token->keyword == RID_AT_SYNTHESIZE)
29981 cp_parser_objc_at_synthesize_declaration (parser);
29982 else if (token->keyword == RID_AT_DYNAMIC)
29983 cp_parser_objc_at_dynamic_declaration (parser);
29984 else if (token->keyword == RID_ATTRIBUTE
29985 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29986 warning_at (token->location, OPT_Wattributes,
29987 "prefix attributes are ignored for methods");
29988 else
29989 /* Allow for interspersed non-ObjC++ code. */
29990 cp_parser_objc_interstitial_code (parser);
29992 token = cp_lexer_peek_token (parser->lexer);
29995 if (token->type != CPP_EOF)
29996 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29997 else
29998 cp_parser_error (parser, "expected %<@end%>");
30000 objc_finish_implementation ();
30003 /* Parse Objective-C ivars. */
30005 static void
30006 cp_parser_objc_class_ivars (cp_parser* parser)
30008 cp_token *token = cp_lexer_peek_token (parser->lexer);
30010 if (token->type != CPP_OPEN_BRACE)
30011 return; /* No ivars specified. */
30013 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30014 token = cp_lexer_peek_token (parser->lexer);
30016 while (token->type != CPP_CLOSE_BRACE
30017 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30019 cp_decl_specifier_seq declspecs;
30020 int decl_class_or_enum_p;
30021 tree prefix_attributes;
30023 cp_parser_objc_visibility_spec (parser);
30025 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30026 break;
30028 cp_parser_decl_specifier_seq (parser,
30029 CP_PARSER_FLAGS_OPTIONAL,
30030 &declspecs,
30031 &decl_class_or_enum_p);
30033 /* auto, register, static, extern, mutable. */
30034 if (declspecs.storage_class != sc_none)
30036 cp_parser_error (parser, "invalid type for instance variable");
30037 declspecs.storage_class = sc_none;
30040 /* thread_local. */
30041 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30043 cp_parser_error (parser, "invalid type for instance variable");
30044 declspecs.locations[ds_thread] = 0;
30047 /* typedef. */
30048 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30050 cp_parser_error (parser, "invalid type for instance variable");
30051 declspecs.locations[ds_typedef] = 0;
30054 prefix_attributes = declspecs.attributes;
30055 declspecs.attributes = NULL_TREE;
30057 /* Keep going until we hit the `;' at the end of the
30058 declaration. */
30059 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30061 tree width = NULL_TREE, attributes, first_attribute, decl;
30062 cp_declarator *declarator = NULL;
30063 int ctor_dtor_or_conv_p;
30065 /* Check for a (possibly unnamed) bitfield declaration. */
30066 token = cp_lexer_peek_token (parser->lexer);
30067 if (token->type == CPP_COLON)
30068 goto eat_colon;
30070 if (token->type == CPP_NAME
30071 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30072 == CPP_COLON))
30074 /* Get the name of the bitfield. */
30075 declarator = make_id_declarator (NULL_TREE,
30076 cp_parser_identifier (parser),
30077 sfk_none);
30079 eat_colon:
30080 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30081 /* Get the width of the bitfield. */
30082 width
30083 = cp_parser_constant_expression (parser);
30085 else
30087 /* Parse the declarator. */
30088 declarator
30089 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30090 &ctor_dtor_or_conv_p,
30091 /*parenthesized_p=*/NULL,
30092 /*member_p=*/false,
30093 /*friend_p=*/false);
30096 /* Look for attributes that apply to the ivar. */
30097 attributes = cp_parser_attributes_opt (parser);
30098 /* Remember which attributes are prefix attributes and
30099 which are not. */
30100 first_attribute = attributes;
30101 /* Combine the attributes. */
30102 attributes = chainon (prefix_attributes, attributes);
30104 if (width)
30105 /* Create the bitfield declaration. */
30106 decl = grokbitfield (declarator, &declspecs,
30107 width, NULL_TREE, attributes);
30108 else
30109 decl = grokfield (declarator, &declspecs,
30110 NULL_TREE, /*init_const_expr_p=*/false,
30111 NULL_TREE, attributes);
30113 /* Add the instance variable. */
30114 if (decl != error_mark_node && decl != NULL_TREE)
30115 objc_add_instance_variable (decl);
30117 /* Reset PREFIX_ATTRIBUTES. */
30118 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30119 attributes = TREE_CHAIN (attributes);
30120 if (attributes)
30121 TREE_CHAIN (attributes) = NULL_TREE;
30123 token = cp_lexer_peek_token (parser->lexer);
30125 if (token->type == CPP_COMMA)
30127 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30128 continue;
30130 break;
30133 cp_parser_consume_semicolon_at_end_of_statement (parser);
30134 token = cp_lexer_peek_token (parser->lexer);
30137 if (token->keyword == RID_AT_END)
30138 cp_parser_error (parser, "expected %<}%>");
30140 /* Do not consume the RID_AT_END, so it will be read again as terminating
30141 the @interface of @implementation. */
30142 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30143 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30145 /* For historical reasons, we accept an optional semicolon. */
30146 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30147 cp_lexer_consume_token (parser->lexer);
30150 /* Parse an Objective-C protocol declaration. */
30152 static void
30153 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30155 tree proto, protorefs;
30156 cp_token *tok;
30158 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30161 tok = cp_lexer_peek_token (parser->lexer);
30162 error_at (tok->location, "identifier expected after %<@protocol%>");
30163 cp_parser_consume_semicolon_at_end_of_statement (parser);
30164 return;
30167 /* See if we have a forward declaration or a definition. */
30168 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30170 /* Try a forward declaration first. */
30171 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30173 while (true)
30175 tree id;
30177 id = cp_parser_identifier (parser);
30178 if (id == error_mark_node)
30179 break;
30181 objc_declare_protocol (id, attributes);
30183 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30184 cp_lexer_consume_token (parser->lexer);
30185 else
30186 break;
30188 cp_parser_consume_semicolon_at_end_of_statement (parser);
30191 /* Ok, we got a full-fledged definition (or at least should). */
30192 else
30194 proto = cp_parser_identifier (parser);
30195 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30196 objc_start_protocol (proto, protorefs, attributes);
30197 cp_parser_objc_method_prototype_list (parser);
30201 /* Parse an Objective-C superclass or category. */
30203 static void
30204 cp_parser_objc_superclass_or_category (cp_parser *parser,
30205 bool iface_p,
30206 tree *super,
30207 tree *categ, bool *is_class_extension)
30209 cp_token *next = cp_lexer_peek_token (parser->lexer);
30211 *super = *categ = NULL_TREE;
30212 *is_class_extension = false;
30213 if (next->type == CPP_COLON)
30215 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30216 *super = cp_parser_identifier (parser);
30218 else if (next->type == CPP_OPEN_PAREN)
30220 matching_parens parens;
30221 parens.consume_open (parser); /* Eat '('. */
30223 /* If there is no category name, and this is an @interface, we
30224 have a class extension. */
30225 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30227 *categ = NULL_TREE;
30228 *is_class_extension = true;
30230 else
30231 *categ = cp_parser_identifier (parser);
30233 parens.require_close (parser);
30237 /* Parse an Objective-C class interface. */
30239 static void
30240 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30242 tree name, super, categ, protos;
30243 bool is_class_extension;
30245 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30246 name = cp_parser_identifier (parser);
30247 if (name == error_mark_node)
30249 /* It's hard to recover because even if valid @interface stuff
30250 is to follow, we can't compile it (or validate it) if we
30251 don't even know which class it refers to. Let's assume this
30252 was a stray '@interface' token in the stream and skip it.
30254 return;
30256 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30257 &is_class_extension);
30258 protos = cp_parser_objc_protocol_refs_opt (parser);
30260 /* We have either a class or a category on our hands. */
30261 if (categ || is_class_extension)
30262 objc_start_category_interface (name, categ, protos, attributes);
30263 else
30265 objc_start_class_interface (name, super, protos, attributes);
30266 /* Handle instance variable declarations, if any. */
30267 cp_parser_objc_class_ivars (parser);
30268 objc_continue_interface ();
30271 cp_parser_objc_method_prototype_list (parser);
30274 /* Parse an Objective-C class implementation. */
30276 static void
30277 cp_parser_objc_class_implementation (cp_parser* parser)
30279 tree name, super, categ;
30280 bool is_class_extension;
30282 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30283 name = cp_parser_identifier (parser);
30284 if (name == error_mark_node)
30286 /* It's hard to recover because even if valid @implementation
30287 stuff is to follow, we can't compile it (or validate it) if
30288 we don't even know which class it refers to. Let's assume
30289 this was a stray '@implementation' token in the stream and
30290 skip it.
30292 return;
30294 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30295 &is_class_extension);
30297 /* We have either a class or a category on our hands. */
30298 if (categ)
30299 objc_start_category_implementation (name, categ);
30300 else
30302 objc_start_class_implementation (name, super);
30303 /* Handle instance variable declarations, if any. */
30304 cp_parser_objc_class_ivars (parser);
30305 objc_continue_implementation ();
30308 cp_parser_objc_method_definition_list (parser);
30311 /* Consume the @end token and finish off the implementation. */
30313 static void
30314 cp_parser_objc_end_implementation (cp_parser* parser)
30316 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30317 objc_finish_implementation ();
30320 /* Parse an Objective-C declaration. */
30322 static void
30323 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30325 /* Try to figure out what kind of declaration is present. */
30326 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30328 if (attributes)
30329 switch (kwd->keyword)
30331 case RID_AT_ALIAS:
30332 case RID_AT_CLASS:
30333 case RID_AT_END:
30334 error_at (kwd->location, "attributes may not be specified before"
30335 " the %<@%D%> Objective-C++ keyword",
30336 kwd->u.value);
30337 attributes = NULL;
30338 break;
30339 case RID_AT_IMPLEMENTATION:
30340 warning_at (kwd->location, OPT_Wattributes,
30341 "prefix attributes are ignored before %<@%D%>",
30342 kwd->u.value);
30343 attributes = NULL;
30344 default:
30345 break;
30348 switch (kwd->keyword)
30350 case RID_AT_ALIAS:
30351 cp_parser_objc_alias_declaration (parser);
30352 break;
30353 case RID_AT_CLASS:
30354 cp_parser_objc_class_declaration (parser);
30355 break;
30356 case RID_AT_PROTOCOL:
30357 cp_parser_objc_protocol_declaration (parser, attributes);
30358 break;
30359 case RID_AT_INTERFACE:
30360 cp_parser_objc_class_interface (parser, attributes);
30361 break;
30362 case RID_AT_IMPLEMENTATION:
30363 cp_parser_objc_class_implementation (parser);
30364 break;
30365 case RID_AT_END:
30366 cp_parser_objc_end_implementation (parser);
30367 break;
30368 default:
30369 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30370 kwd->u.value);
30371 cp_parser_skip_to_end_of_block_or_statement (parser);
30375 /* Parse an Objective-C try-catch-finally statement.
30377 objc-try-catch-finally-stmt:
30378 @try compound-statement objc-catch-clause-seq [opt]
30379 objc-finally-clause [opt]
30381 objc-catch-clause-seq:
30382 objc-catch-clause objc-catch-clause-seq [opt]
30384 objc-catch-clause:
30385 @catch ( objc-exception-declaration ) compound-statement
30387 objc-finally-clause:
30388 @finally compound-statement
30390 objc-exception-declaration:
30391 parameter-declaration
30392 '...'
30394 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30396 Returns NULL_TREE.
30398 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30399 for C. Keep them in sync. */
30401 static tree
30402 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30404 location_t location;
30405 tree stmt;
30407 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30408 location = cp_lexer_peek_token (parser->lexer)->location;
30409 objc_maybe_warn_exceptions (location);
30410 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30411 node, lest it get absorbed into the surrounding block. */
30412 stmt = push_stmt_list ();
30413 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30414 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30416 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30418 cp_parameter_declarator *parm;
30419 tree parameter_declaration = error_mark_node;
30420 bool seen_open_paren = false;
30421 matching_parens parens;
30423 cp_lexer_consume_token (parser->lexer);
30424 if (parens.require_open (parser))
30425 seen_open_paren = true;
30426 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30428 /* We have "@catch (...)" (where the '...' are literally
30429 what is in the code). Skip the '...'.
30430 parameter_declaration is set to NULL_TREE, and
30431 objc_being_catch_clauses() knows that that means
30432 '...'. */
30433 cp_lexer_consume_token (parser->lexer);
30434 parameter_declaration = NULL_TREE;
30436 else
30438 /* We have "@catch (NSException *exception)" or something
30439 like that. Parse the parameter declaration. */
30440 parm = cp_parser_parameter_declaration (parser, false, NULL);
30441 if (parm == NULL)
30442 parameter_declaration = error_mark_node;
30443 else
30444 parameter_declaration = grokdeclarator (parm->declarator,
30445 &parm->decl_specifiers,
30446 PARM, /*initialized=*/0,
30447 /*attrlist=*/NULL);
30449 if (seen_open_paren)
30450 parens.require_close (parser);
30451 else
30453 /* If there was no open parenthesis, we are recovering from
30454 an error, and we are trying to figure out what mistake
30455 the user has made. */
30457 /* If there is an immediate closing parenthesis, the user
30458 probably forgot the opening one (ie, they typed "@catch
30459 NSException *e)". Parse the closing parenthesis and keep
30460 going. */
30461 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30462 cp_lexer_consume_token (parser->lexer);
30464 /* If these is no immediate closing parenthesis, the user
30465 probably doesn't know that parenthesis are required at
30466 all (ie, they typed "@catch NSException *e"). So, just
30467 forget about the closing parenthesis and keep going. */
30469 objc_begin_catch_clause (parameter_declaration);
30470 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30471 objc_finish_catch_clause ();
30473 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30475 cp_lexer_consume_token (parser->lexer);
30476 location = cp_lexer_peek_token (parser->lexer)->location;
30477 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30478 node, lest it get absorbed into the surrounding block. */
30479 stmt = push_stmt_list ();
30480 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30481 objc_build_finally_clause (location, pop_stmt_list (stmt));
30484 return objc_finish_try_stmt ();
30487 /* Parse an Objective-C synchronized statement.
30489 objc-synchronized-stmt:
30490 @synchronized ( expression ) compound-statement
30492 Returns NULL_TREE. */
30494 static tree
30495 cp_parser_objc_synchronized_statement (cp_parser *parser)
30497 location_t location;
30498 tree lock, stmt;
30500 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30502 location = cp_lexer_peek_token (parser->lexer)->location;
30503 objc_maybe_warn_exceptions (location);
30504 matching_parens parens;
30505 parens.require_open (parser);
30506 lock = cp_parser_expression (parser);
30507 parens.require_close (parser);
30509 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30510 node, lest it get absorbed into the surrounding block. */
30511 stmt = push_stmt_list ();
30512 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30514 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30517 /* Parse an Objective-C throw statement.
30519 objc-throw-stmt:
30520 @throw assignment-expression [opt] ;
30522 Returns a constructed '@throw' statement. */
30524 static tree
30525 cp_parser_objc_throw_statement (cp_parser *parser)
30527 tree expr = NULL_TREE;
30528 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30530 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30533 expr = cp_parser_expression (parser);
30535 cp_parser_consume_semicolon_at_end_of_statement (parser);
30537 return objc_build_throw_stmt (loc, expr);
30540 /* Parse an Objective-C statement. */
30542 static tree
30543 cp_parser_objc_statement (cp_parser * parser)
30545 /* Try to figure out what kind of declaration is present. */
30546 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30548 switch (kwd->keyword)
30550 case RID_AT_TRY:
30551 return cp_parser_objc_try_catch_finally_statement (parser);
30552 case RID_AT_SYNCHRONIZED:
30553 return cp_parser_objc_synchronized_statement (parser);
30554 case RID_AT_THROW:
30555 return cp_parser_objc_throw_statement (parser);
30556 default:
30557 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30558 kwd->u.value);
30559 cp_parser_skip_to_end_of_block_or_statement (parser);
30562 return error_mark_node;
30565 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30566 look ahead to see if an objc keyword follows the attributes. This
30567 is to detect the use of prefix attributes on ObjC @interface and
30568 @protocol. */
30570 static bool
30571 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30573 cp_lexer_save_tokens (parser->lexer);
30574 *attrib = cp_parser_attributes_opt (parser);
30575 gcc_assert (*attrib);
30576 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30578 cp_lexer_commit_tokens (parser->lexer);
30579 return true;
30581 cp_lexer_rollback_tokens (parser->lexer);
30582 return false;
30585 /* This routine is a minimal replacement for
30586 c_parser_struct_declaration () used when parsing the list of
30587 types/names or ObjC++ properties. For example, when parsing the
30588 code
30590 @property (readonly) int a, b, c;
30592 this function is responsible for parsing "int a, int b, int c" and
30593 returning the declarations as CHAIN of DECLs.
30595 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30596 similar parsing. */
30597 static tree
30598 cp_parser_objc_struct_declaration (cp_parser *parser)
30600 tree decls = NULL_TREE;
30601 cp_decl_specifier_seq declspecs;
30602 int decl_class_or_enum_p;
30603 tree prefix_attributes;
30605 cp_parser_decl_specifier_seq (parser,
30606 CP_PARSER_FLAGS_NONE,
30607 &declspecs,
30608 &decl_class_or_enum_p);
30610 if (declspecs.type == error_mark_node)
30611 return error_mark_node;
30613 /* auto, register, static, extern, mutable. */
30614 if (declspecs.storage_class != sc_none)
30616 cp_parser_error (parser, "invalid type for property");
30617 declspecs.storage_class = sc_none;
30620 /* thread_local. */
30621 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30623 cp_parser_error (parser, "invalid type for property");
30624 declspecs.locations[ds_thread] = 0;
30627 /* typedef. */
30628 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30630 cp_parser_error (parser, "invalid type for property");
30631 declspecs.locations[ds_typedef] = 0;
30634 prefix_attributes = declspecs.attributes;
30635 declspecs.attributes = NULL_TREE;
30637 /* Keep going until we hit the `;' at the end of the declaration. */
30638 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30640 tree attributes, first_attribute, decl;
30641 cp_declarator *declarator;
30642 cp_token *token;
30644 /* Parse the declarator. */
30645 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30646 NULL, NULL, false, false);
30648 /* Look for attributes that apply to the ivar. */
30649 attributes = cp_parser_attributes_opt (parser);
30650 /* Remember which attributes are prefix attributes and
30651 which are not. */
30652 first_attribute = attributes;
30653 /* Combine the attributes. */
30654 attributes = chainon (prefix_attributes, attributes);
30656 decl = grokfield (declarator, &declspecs,
30657 NULL_TREE, /*init_const_expr_p=*/false,
30658 NULL_TREE, attributes);
30660 if (decl == error_mark_node || decl == NULL_TREE)
30661 return error_mark_node;
30663 /* Reset PREFIX_ATTRIBUTES. */
30664 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30665 attributes = TREE_CHAIN (attributes);
30666 if (attributes)
30667 TREE_CHAIN (attributes) = NULL_TREE;
30669 DECL_CHAIN (decl) = decls;
30670 decls = decl;
30672 token = cp_lexer_peek_token (parser->lexer);
30673 if (token->type == CPP_COMMA)
30675 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30676 continue;
30678 else
30679 break;
30681 return decls;
30684 /* Parse an Objective-C @property declaration. The syntax is:
30686 objc-property-declaration:
30687 '@property' objc-property-attributes[opt] struct-declaration ;
30689 objc-property-attributes:
30690 '(' objc-property-attribute-list ')'
30692 objc-property-attribute-list:
30693 objc-property-attribute
30694 objc-property-attribute-list, objc-property-attribute
30696 objc-property-attribute
30697 'getter' = identifier
30698 'setter' = identifier
30699 'readonly'
30700 'readwrite'
30701 'assign'
30702 'retain'
30703 'copy'
30704 'nonatomic'
30706 For example:
30707 @property NSString *name;
30708 @property (readonly) id object;
30709 @property (retain, nonatomic, getter=getTheName) id name;
30710 @property int a, b, c;
30712 PS: This function is identical to
30713 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30714 static void
30715 cp_parser_objc_at_property_declaration (cp_parser *parser)
30717 /* The following variables hold the attributes of the properties as
30718 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30719 seen. When we see an attribute, we set them to 'true' (if they
30720 are boolean properties) or to the identifier (if they have an
30721 argument, ie, for getter and setter). Note that here we only
30722 parse the list of attributes, check the syntax and accumulate the
30723 attributes that we find. objc_add_property_declaration() will
30724 then process the information. */
30725 bool property_assign = false;
30726 bool property_copy = false;
30727 tree property_getter_ident = NULL_TREE;
30728 bool property_nonatomic = false;
30729 bool property_readonly = false;
30730 bool property_readwrite = false;
30731 bool property_retain = false;
30732 tree property_setter_ident = NULL_TREE;
30734 /* 'properties' is the list of properties that we read. Usually a
30735 single one, but maybe more (eg, in "@property int a, b, c;" there
30736 are three). */
30737 tree properties;
30738 location_t loc;
30740 loc = cp_lexer_peek_token (parser->lexer)->location;
30742 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30744 /* Parse the optional attribute list... */
30745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30747 /* Eat the '('. */
30748 matching_parens parens;
30749 parens.consume_open (parser);
30751 while (true)
30753 bool syntax_error = false;
30754 cp_token *token = cp_lexer_peek_token (parser->lexer);
30755 enum rid keyword;
30757 if (token->type != CPP_NAME)
30759 cp_parser_error (parser, "expected identifier");
30760 break;
30762 keyword = C_RID_CODE (token->u.value);
30763 cp_lexer_consume_token (parser->lexer);
30764 switch (keyword)
30766 case RID_ASSIGN: property_assign = true; break;
30767 case RID_COPY: property_copy = true; break;
30768 case RID_NONATOMIC: property_nonatomic = true; break;
30769 case RID_READONLY: property_readonly = true; break;
30770 case RID_READWRITE: property_readwrite = true; break;
30771 case RID_RETAIN: property_retain = true; break;
30773 case RID_GETTER:
30774 case RID_SETTER:
30775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30777 if (keyword == RID_GETTER)
30778 cp_parser_error (parser,
30779 "missing %<=%> (after %<getter%> attribute)");
30780 else
30781 cp_parser_error (parser,
30782 "missing %<=%> (after %<setter%> attribute)");
30783 syntax_error = true;
30784 break;
30786 cp_lexer_consume_token (parser->lexer); /* eat the = */
30787 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30789 cp_parser_error (parser, "expected identifier");
30790 syntax_error = true;
30791 break;
30793 if (keyword == RID_SETTER)
30795 if (property_setter_ident != NULL_TREE)
30797 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30798 cp_lexer_consume_token (parser->lexer);
30800 else
30801 property_setter_ident = cp_parser_objc_selector (parser);
30802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30803 cp_parser_error (parser, "setter name must terminate with %<:%>");
30804 else
30805 cp_lexer_consume_token (parser->lexer);
30807 else
30809 if (property_getter_ident != NULL_TREE)
30811 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30812 cp_lexer_consume_token (parser->lexer);
30814 else
30815 property_getter_ident = cp_parser_objc_selector (parser);
30817 break;
30818 default:
30819 cp_parser_error (parser, "unknown property attribute");
30820 syntax_error = true;
30821 break;
30824 if (syntax_error)
30825 break;
30827 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30828 cp_lexer_consume_token (parser->lexer);
30829 else
30830 break;
30833 /* FIXME: "@property (setter, assign);" will generate a spurious
30834 "error: expected ‘)’ before ‘,’ token". This is because
30835 cp_parser_require, unlike the C counterpart, will produce an
30836 error even if we are in error recovery. */
30837 if (!parens.require_close (parser))
30839 cp_parser_skip_to_closing_parenthesis (parser,
30840 /*recovering=*/true,
30841 /*or_comma=*/false,
30842 /*consume_paren=*/true);
30846 /* ... and the property declaration(s). */
30847 properties = cp_parser_objc_struct_declaration (parser);
30849 if (properties == error_mark_node)
30851 cp_parser_skip_to_end_of_statement (parser);
30852 /* If the next token is now a `;', consume it. */
30853 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30854 cp_lexer_consume_token (parser->lexer);
30855 return;
30858 if (properties == NULL_TREE)
30859 cp_parser_error (parser, "expected identifier");
30860 else
30862 /* Comma-separated properties are chained together in
30863 reverse order; add them one by one. */
30864 properties = nreverse (properties);
30866 for (; properties; properties = TREE_CHAIN (properties))
30867 objc_add_property_declaration (loc, copy_node (properties),
30868 property_readonly, property_readwrite,
30869 property_assign, property_retain,
30870 property_copy, property_nonatomic,
30871 property_getter_ident, property_setter_ident);
30874 cp_parser_consume_semicolon_at_end_of_statement (parser);
30877 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30879 objc-synthesize-declaration:
30880 @synthesize objc-synthesize-identifier-list ;
30882 objc-synthesize-identifier-list:
30883 objc-synthesize-identifier
30884 objc-synthesize-identifier-list, objc-synthesize-identifier
30886 objc-synthesize-identifier
30887 identifier
30888 identifier = identifier
30890 For example:
30891 @synthesize MyProperty;
30892 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30894 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30895 for C. Keep them in sync.
30897 static void
30898 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30900 tree list = NULL_TREE;
30901 location_t loc;
30902 loc = cp_lexer_peek_token (parser->lexer)->location;
30904 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30905 while (true)
30907 tree property, ivar;
30908 property = cp_parser_identifier (parser);
30909 if (property == error_mark_node)
30911 cp_parser_consume_semicolon_at_end_of_statement (parser);
30912 return;
30914 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30916 cp_lexer_consume_token (parser->lexer);
30917 ivar = cp_parser_identifier (parser);
30918 if (ivar == error_mark_node)
30920 cp_parser_consume_semicolon_at_end_of_statement (parser);
30921 return;
30924 else
30925 ivar = NULL_TREE;
30926 list = chainon (list, build_tree_list (ivar, property));
30927 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30928 cp_lexer_consume_token (parser->lexer);
30929 else
30930 break;
30932 cp_parser_consume_semicolon_at_end_of_statement (parser);
30933 objc_add_synthesize_declaration (loc, list);
30936 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30938 objc-dynamic-declaration:
30939 @dynamic identifier-list ;
30941 For example:
30942 @dynamic MyProperty;
30943 @dynamic MyProperty, AnotherProperty;
30945 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30946 for C. Keep them in sync.
30948 static void
30949 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30951 tree list = NULL_TREE;
30952 location_t loc;
30953 loc = cp_lexer_peek_token (parser->lexer)->location;
30955 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30956 while (true)
30958 tree property;
30959 property = cp_parser_identifier (parser);
30960 if (property == error_mark_node)
30962 cp_parser_consume_semicolon_at_end_of_statement (parser);
30963 return;
30965 list = chainon (list, build_tree_list (NULL, property));
30966 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30967 cp_lexer_consume_token (parser->lexer);
30968 else
30969 break;
30971 cp_parser_consume_semicolon_at_end_of_statement (parser);
30972 objc_add_dynamic_declaration (loc, list);
30976 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30978 /* Returns name of the next clause.
30979 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30980 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30981 returned and the token is consumed. */
30983 static pragma_omp_clause
30984 cp_parser_omp_clause_name (cp_parser *parser)
30986 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30989 result = PRAGMA_OACC_CLAUSE_AUTO;
30990 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30991 result = PRAGMA_OMP_CLAUSE_IF;
30992 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30993 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30994 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30995 result = PRAGMA_OACC_CLAUSE_DELETE;
30996 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30997 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30998 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30999 result = PRAGMA_OMP_CLAUSE_FOR;
31000 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31002 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31003 const char *p = IDENTIFIER_POINTER (id);
31005 switch (p[0])
31007 case 'a':
31008 if (!strcmp ("aligned", p))
31009 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31010 else if (!strcmp ("async", p))
31011 result = PRAGMA_OACC_CLAUSE_ASYNC;
31012 break;
31013 case 'c':
31014 if (!strcmp ("collapse", p))
31015 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31016 else if (!strcmp ("copy", p))
31017 result = PRAGMA_OACC_CLAUSE_COPY;
31018 else if (!strcmp ("copyin", p))
31019 result = PRAGMA_OMP_CLAUSE_COPYIN;
31020 else if (!strcmp ("copyout", p))
31021 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31022 else if (!strcmp ("copyprivate", p))
31023 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31024 else if (!strcmp ("create", p))
31025 result = PRAGMA_OACC_CLAUSE_CREATE;
31026 break;
31027 case 'd':
31028 if (!strcmp ("defaultmap", p))
31029 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31030 else if (!strcmp ("depend", p))
31031 result = PRAGMA_OMP_CLAUSE_DEPEND;
31032 else if (!strcmp ("device", p))
31033 result = PRAGMA_OMP_CLAUSE_DEVICE;
31034 else if (!strcmp ("deviceptr", p))
31035 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31036 else if (!strcmp ("device_resident", p))
31037 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31038 else if (!strcmp ("dist_schedule", p))
31039 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31040 break;
31041 case 'f':
31042 if (!strcmp ("final", p))
31043 result = PRAGMA_OMP_CLAUSE_FINAL;
31044 else if (!strcmp ("firstprivate", p))
31045 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31046 else if (!strcmp ("from", p))
31047 result = PRAGMA_OMP_CLAUSE_FROM;
31048 break;
31049 case 'g':
31050 if (!strcmp ("gang", p))
31051 result = PRAGMA_OACC_CLAUSE_GANG;
31052 else if (!strcmp ("grainsize", p))
31053 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31054 break;
31055 case 'h':
31056 if (!strcmp ("hint", p))
31057 result = PRAGMA_OMP_CLAUSE_HINT;
31058 else if (!strcmp ("host", p))
31059 result = PRAGMA_OACC_CLAUSE_HOST;
31060 break;
31061 case 'i':
31062 if (!strcmp ("inbranch", p))
31063 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31064 else if (!strcmp ("independent", p))
31065 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31066 else if (!strcmp ("is_device_ptr", p))
31067 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31068 break;
31069 case 'l':
31070 if (!strcmp ("lastprivate", p))
31071 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31072 else if (!strcmp ("linear", p))
31073 result = PRAGMA_OMP_CLAUSE_LINEAR;
31074 else if (!strcmp ("link", p))
31075 result = PRAGMA_OMP_CLAUSE_LINK;
31076 break;
31077 case 'm':
31078 if (!strcmp ("map", p))
31079 result = PRAGMA_OMP_CLAUSE_MAP;
31080 else if (!strcmp ("mergeable", p))
31081 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31082 break;
31083 case 'n':
31084 if (!strcmp ("nogroup", p))
31085 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31086 else if (!strcmp ("notinbranch", p))
31087 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31088 else if (!strcmp ("nowait", p))
31089 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31090 else if (!strcmp ("num_gangs", p))
31091 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31092 else if (!strcmp ("num_tasks", p))
31093 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31094 else if (!strcmp ("num_teams", p))
31095 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31096 else if (!strcmp ("num_threads", p))
31097 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31098 else if (!strcmp ("num_workers", p))
31099 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31100 break;
31101 case 'o':
31102 if (!strcmp ("ordered", p))
31103 result = PRAGMA_OMP_CLAUSE_ORDERED;
31104 break;
31105 case 'p':
31106 if (!strcmp ("parallel", p))
31107 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31108 else if (!strcmp ("present", p))
31109 result = PRAGMA_OACC_CLAUSE_PRESENT;
31110 else if (!strcmp ("present_or_copy", p)
31111 || !strcmp ("pcopy", p))
31112 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31113 else if (!strcmp ("present_or_copyin", p)
31114 || !strcmp ("pcopyin", p))
31115 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31116 else if (!strcmp ("present_or_copyout", p)
31117 || !strcmp ("pcopyout", p))
31118 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31119 else if (!strcmp ("present_or_create", p)
31120 || !strcmp ("pcreate", p))
31121 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31122 else if (!strcmp ("priority", p))
31123 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31124 else if (!strcmp ("proc_bind", p))
31125 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31126 break;
31127 case 'r':
31128 if (!strcmp ("reduction", p))
31129 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31130 break;
31131 case 's':
31132 if (!strcmp ("safelen", p))
31133 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31134 else if (!strcmp ("schedule", p))
31135 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31136 else if (!strcmp ("sections", p))
31137 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31138 else if (!strcmp ("self", p))
31139 result = PRAGMA_OACC_CLAUSE_SELF;
31140 else if (!strcmp ("seq", p))
31141 result = PRAGMA_OACC_CLAUSE_SEQ;
31142 else if (!strcmp ("shared", p))
31143 result = PRAGMA_OMP_CLAUSE_SHARED;
31144 else if (!strcmp ("simd", p))
31145 result = PRAGMA_OMP_CLAUSE_SIMD;
31146 else if (!strcmp ("simdlen", p))
31147 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31148 break;
31149 case 't':
31150 if (!strcmp ("taskgroup", p))
31151 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31152 else if (!strcmp ("thread_limit", p))
31153 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31154 else if (!strcmp ("threads", p))
31155 result = PRAGMA_OMP_CLAUSE_THREADS;
31156 else if (!strcmp ("tile", p))
31157 result = PRAGMA_OACC_CLAUSE_TILE;
31158 else if (!strcmp ("to", p))
31159 result = PRAGMA_OMP_CLAUSE_TO;
31160 break;
31161 case 'u':
31162 if (!strcmp ("uniform", p))
31163 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31164 else if (!strcmp ("untied", p))
31165 result = PRAGMA_OMP_CLAUSE_UNTIED;
31166 else if (!strcmp ("use_device", p))
31167 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31168 else if (!strcmp ("use_device_ptr", p))
31169 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31170 break;
31171 case 'v':
31172 if (!strcmp ("vector", p))
31173 result = PRAGMA_OACC_CLAUSE_VECTOR;
31174 else if (!strcmp ("vector_length", p))
31175 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31176 break;
31177 case 'w':
31178 if (!strcmp ("wait", p))
31179 result = PRAGMA_OACC_CLAUSE_WAIT;
31180 else if (!strcmp ("worker", p))
31181 result = PRAGMA_OACC_CLAUSE_WORKER;
31182 break;
31186 if (result != PRAGMA_OMP_CLAUSE_NONE)
31187 cp_lexer_consume_token (parser->lexer);
31189 return result;
31192 /* Validate that a clause of the given type does not already exist. */
31194 static void
31195 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31196 const char *name, location_t location)
31198 tree c;
31200 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31201 if (OMP_CLAUSE_CODE (c) == code)
31203 error_at (location, "too many %qs clauses", name);
31204 break;
31208 /* OpenMP 2.5:
31209 variable-list:
31210 identifier
31211 variable-list , identifier
31213 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31214 colon). An opening parenthesis will have been consumed by the caller.
31216 If KIND is nonzero, create the appropriate node and install the decl
31217 in OMP_CLAUSE_DECL and add the node to the head of the list.
31219 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31220 return the list created.
31222 COLON can be NULL if only closing parenthesis should end the list,
31223 or pointer to bool which will receive false if the list is terminated
31224 by closing parenthesis or true if the list is terminated by colon. */
31226 static tree
31227 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31228 tree list, bool *colon)
31230 cp_token *token;
31231 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31232 if (colon)
31234 parser->colon_corrects_to_scope_p = false;
31235 *colon = false;
31237 while (1)
31239 tree name, decl;
31241 token = cp_lexer_peek_token (parser->lexer);
31242 if (kind != 0
31243 && current_class_ptr
31244 && cp_parser_is_keyword (token, RID_THIS))
31246 decl = finish_this_expr ();
31247 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31248 || CONVERT_EXPR_P (decl))
31249 decl = TREE_OPERAND (decl, 0);
31250 cp_lexer_consume_token (parser->lexer);
31252 else
31254 name = cp_parser_id_expression (parser, /*template_p=*/false,
31255 /*check_dependency_p=*/true,
31256 /*template_p=*/NULL,
31257 /*declarator_p=*/false,
31258 /*optional_p=*/false);
31259 if (name == error_mark_node)
31260 goto skip_comma;
31262 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31263 if (decl == error_mark_node)
31264 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31265 token->location);
31267 if (decl == error_mark_node)
31269 else if (kind != 0)
31271 switch (kind)
31273 case OMP_CLAUSE__CACHE_:
31274 /* The OpenACC cache directive explicitly only allows "array
31275 elements or subarrays". */
31276 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31278 error_at (token->location, "expected %<[%>");
31279 decl = error_mark_node;
31280 break;
31282 /* FALLTHROUGH. */
31283 case OMP_CLAUSE_MAP:
31284 case OMP_CLAUSE_FROM:
31285 case OMP_CLAUSE_TO:
31286 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31288 location_t loc
31289 = cp_lexer_peek_token (parser->lexer)->location;
31290 cp_id_kind idk = CP_ID_KIND_NONE;
31291 cp_lexer_consume_token (parser->lexer);
31292 decl = convert_from_reference (decl);
31293 decl
31294 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31295 decl, false,
31296 &idk, loc);
31298 /* FALLTHROUGH. */
31299 case OMP_CLAUSE_DEPEND:
31300 case OMP_CLAUSE_REDUCTION:
31301 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31303 tree low_bound = NULL_TREE, length = NULL_TREE;
31305 parser->colon_corrects_to_scope_p = false;
31306 cp_lexer_consume_token (parser->lexer);
31307 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31308 low_bound = cp_parser_expression (parser);
31309 if (!colon)
31310 parser->colon_corrects_to_scope_p
31311 = saved_colon_corrects_to_scope_p;
31312 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31313 length = integer_one_node;
31314 else
31316 /* Look for `:'. */
31317 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31318 goto skip_comma;
31319 if (!cp_lexer_next_token_is (parser->lexer,
31320 CPP_CLOSE_SQUARE))
31321 length = cp_parser_expression (parser);
31323 /* Look for the closing `]'. */
31324 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31325 RT_CLOSE_SQUARE))
31326 goto skip_comma;
31328 decl = tree_cons (low_bound, length, decl);
31330 break;
31331 default:
31332 break;
31335 tree u = build_omp_clause (token->location, kind);
31336 OMP_CLAUSE_DECL (u) = decl;
31337 OMP_CLAUSE_CHAIN (u) = list;
31338 list = u;
31340 else
31341 list = tree_cons (decl, NULL_TREE, list);
31343 get_comma:
31344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31345 break;
31346 cp_lexer_consume_token (parser->lexer);
31349 if (colon)
31350 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31352 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31354 *colon = true;
31355 cp_parser_require (parser, CPP_COLON, RT_COLON);
31356 return list;
31359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31361 int ending;
31363 /* Try to resync to an unnested comma. Copied from
31364 cp_parser_parenthesized_expression_list. */
31365 skip_comma:
31366 if (colon)
31367 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31368 ending = cp_parser_skip_to_closing_parenthesis (parser,
31369 /*recovering=*/true,
31370 /*or_comma=*/true,
31371 /*consume_paren=*/true);
31372 if (ending < 0)
31373 goto get_comma;
31376 return list;
31379 /* Similarly, but expect leading and trailing parenthesis. This is a very
31380 common case for omp clauses. */
31382 static tree
31383 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31385 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31386 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31387 return list;
31390 /* OpenACC 2.0:
31391 copy ( variable-list )
31392 copyin ( variable-list )
31393 copyout ( variable-list )
31394 create ( variable-list )
31395 delete ( variable-list )
31396 present ( variable-list )
31397 present_or_copy ( variable-list )
31398 pcopy ( variable-list )
31399 present_or_copyin ( variable-list )
31400 pcopyin ( variable-list )
31401 present_or_copyout ( variable-list )
31402 pcopyout ( variable-list )
31403 present_or_create ( variable-list )
31404 pcreate ( variable-list ) */
31406 static tree
31407 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31408 tree list)
31410 enum gomp_map_kind kind;
31411 switch (c_kind)
31413 case PRAGMA_OACC_CLAUSE_COPY:
31414 kind = GOMP_MAP_FORCE_TOFROM;
31415 break;
31416 case PRAGMA_OACC_CLAUSE_COPYIN:
31417 kind = GOMP_MAP_FORCE_TO;
31418 break;
31419 case PRAGMA_OACC_CLAUSE_COPYOUT:
31420 kind = GOMP_MAP_FORCE_FROM;
31421 break;
31422 case PRAGMA_OACC_CLAUSE_CREATE:
31423 kind = GOMP_MAP_FORCE_ALLOC;
31424 break;
31425 case PRAGMA_OACC_CLAUSE_DELETE:
31426 kind = GOMP_MAP_DELETE;
31427 break;
31428 case PRAGMA_OACC_CLAUSE_DEVICE:
31429 kind = GOMP_MAP_FORCE_TO;
31430 break;
31431 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31432 kind = GOMP_MAP_DEVICE_RESIDENT;
31433 break;
31434 case PRAGMA_OACC_CLAUSE_HOST:
31435 case PRAGMA_OACC_CLAUSE_SELF:
31436 kind = GOMP_MAP_FORCE_FROM;
31437 break;
31438 case PRAGMA_OACC_CLAUSE_LINK:
31439 kind = GOMP_MAP_LINK;
31440 break;
31441 case PRAGMA_OACC_CLAUSE_PRESENT:
31442 kind = GOMP_MAP_FORCE_PRESENT;
31443 break;
31444 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31445 kind = GOMP_MAP_TOFROM;
31446 break;
31447 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31448 kind = GOMP_MAP_TO;
31449 break;
31450 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31451 kind = GOMP_MAP_FROM;
31452 break;
31453 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31454 kind = GOMP_MAP_ALLOC;
31455 break;
31456 default:
31457 gcc_unreachable ();
31459 tree nl, c;
31460 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31462 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31463 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31465 return nl;
31468 /* OpenACC 2.0:
31469 deviceptr ( variable-list ) */
31471 static tree
31472 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31474 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31475 tree vars, t;
31477 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31478 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31479 variable-list must only allow for pointer variables. */
31480 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31481 for (t = vars; t; t = TREE_CHAIN (t))
31483 tree v = TREE_PURPOSE (t);
31484 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31485 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31486 OMP_CLAUSE_DECL (u) = v;
31487 OMP_CLAUSE_CHAIN (u) = list;
31488 list = u;
31491 return list;
31494 /* OpenACC 2.0:
31495 auto
31496 independent
31497 nohost
31498 seq */
31500 static tree
31501 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31502 enum omp_clause_code code,
31503 tree list, location_t location)
31505 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31506 tree c = build_omp_clause (location, code);
31507 OMP_CLAUSE_CHAIN (c) = list;
31508 return c;
31511 /* OpenACC:
31512 num_gangs ( expression )
31513 num_workers ( expression )
31514 vector_length ( expression ) */
31516 static tree
31517 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31518 const char *str, tree list)
31520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31522 matching_parens parens;
31523 if (!parens.require_open (parser))
31524 return list;
31526 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31528 if (t == error_mark_node
31529 || !parens.require_close (parser))
31531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31532 /*or_comma=*/false,
31533 /*consume_paren=*/true);
31534 return list;
31537 check_no_duplicate_clause (list, code, str, loc);
31539 tree c = build_omp_clause (loc, code);
31540 OMP_CLAUSE_OPERAND (c, 0) = t;
31541 OMP_CLAUSE_CHAIN (c) = list;
31542 return c;
31545 /* OpenACC:
31547 gang [( gang-arg-list )]
31548 worker [( [num:] int-expr )]
31549 vector [( [length:] int-expr )]
31551 where gang-arg is one of:
31553 [num:] int-expr
31554 static: size-expr
31556 and size-expr may be:
31559 int-expr
31562 static tree
31563 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31564 const char *str, tree list)
31566 const char *id = "num";
31567 cp_lexer *lexer = parser->lexer;
31568 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31569 location_t loc = cp_lexer_peek_token (lexer)->location;
31571 if (kind == OMP_CLAUSE_VECTOR)
31572 id = "length";
31574 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31576 matching_parens parens;
31577 parens.consume_open (parser);
31581 cp_token *next = cp_lexer_peek_token (lexer);
31582 int idx = 0;
31584 /* Gang static argument. */
31585 if (kind == OMP_CLAUSE_GANG
31586 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31588 cp_lexer_consume_token (lexer);
31590 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31591 goto cleanup_error;
31593 idx = 1;
31594 if (ops[idx] != NULL)
31596 cp_parser_error (parser, "too many %<static%> arguments");
31597 goto cleanup_error;
31600 /* Check for the '*' argument. */
31601 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31602 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31603 || cp_lexer_nth_token_is (parser->lexer, 2,
31604 CPP_CLOSE_PAREN)))
31606 cp_lexer_consume_token (lexer);
31607 ops[idx] = integer_minus_one_node;
31609 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31611 cp_lexer_consume_token (lexer);
31612 continue;
31614 else break;
31617 /* Worker num: argument and vector length: arguments. */
31618 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31619 && id_equal (next->u.value, id)
31620 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31622 cp_lexer_consume_token (lexer); /* id */
31623 cp_lexer_consume_token (lexer); /* ':' */
31626 /* Now collect the actual argument. */
31627 if (ops[idx] != NULL_TREE)
31629 cp_parser_error (parser, "unexpected argument");
31630 goto cleanup_error;
31633 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31634 false);
31635 if (expr == error_mark_node)
31636 goto cleanup_error;
31638 mark_exp_read (expr);
31639 ops[idx] = expr;
31641 if (kind == OMP_CLAUSE_GANG
31642 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31644 cp_lexer_consume_token (lexer);
31645 continue;
31647 break;
31649 while (1);
31651 if (!parens.require_close (parser))
31652 goto cleanup_error;
31655 check_no_duplicate_clause (list, kind, str, loc);
31657 c = build_omp_clause (loc, kind);
31659 if (ops[1])
31660 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31662 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31663 OMP_CLAUSE_CHAIN (c) = list;
31665 return c;
31667 cleanup_error:
31668 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31669 return list;
31672 /* OpenACC 2.0:
31673 tile ( size-expr-list ) */
31675 static tree
31676 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31678 tree c, expr = error_mark_node;
31679 tree tile = NULL_TREE;
31681 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31682 so, but the spec authors never considered such a case and have
31683 differing opinions on what it might mean, including 'not
31684 allowed'.) */
31685 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31686 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31687 clause_loc);
31689 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31690 return list;
31694 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31695 return list;
31697 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31698 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31699 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31701 cp_lexer_consume_token (parser->lexer);
31702 expr = integer_zero_node;
31704 else
31705 expr = cp_parser_constant_expression (parser);
31707 tile = tree_cons (NULL_TREE, expr, tile);
31709 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31711 /* Consume the trailing ')'. */
31712 cp_lexer_consume_token (parser->lexer);
31714 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31715 tile = nreverse (tile);
31716 OMP_CLAUSE_TILE_LIST (c) = tile;
31717 OMP_CLAUSE_CHAIN (c) = list;
31718 return c;
31721 /* OpenACC 2.0
31722 Parse wait clause or directive parameters. */
31724 static tree
31725 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31727 vec<tree, va_gc> *args;
31728 tree t, args_tree;
31730 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31731 /*cast_p=*/false,
31732 /*allow_expansion_p=*/true,
31733 /*non_constant_p=*/NULL);
31735 if (args == NULL || args->length () == 0)
31737 cp_parser_error (parser, "expected integer expression before ')'");
31738 if (args != NULL)
31739 release_tree_vector (args);
31740 return list;
31743 args_tree = build_tree_list_vec (args);
31745 release_tree_vector (args);
31747 for (t = args_tree; t; t = TREE_CHAIN (t))
31749 tree targ = TREE_VALUE (t);
31751 if (targ != error_mark_node)
31753 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31754 error ("%<wait%> expression must be integral");
31755 else
31757 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31759 targ = mark_rvalue_use (targ);
31760 OMP_CLAUSE_DECL (c) = targ;
31761 OMP_CLAUSE_CHAIN (c) = list;
31762 list = c;
31767 return list;
31770 /* OpenACC:
31771 wait ( int-expr-list ) */
31773 static tree
31774 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31776 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31778 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31779 return list;
31781 list = cp_parser_oacc_wait_list (parser, location, list);
31783 return list;
31786 /* OpenMP 3.0:
31787 collapse ( constant-expression ) */
31789 static tree
31790 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31792 tree c, num;
31793 location_t loc;
31794 HOST_WIDE_INT n;
31796 loc = cp_lexer_peek_token (parser->lexer)->location;
31797 matching_parens parens;
31798 if (!parens.require_open (parser))
31799 return list;
31801 num = cp_parser_constant_expression (parser);
31803 if (!parens.require_close (parser))
31804 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31805 /*or_comma=*/false,
31806 /*consume_paren=*/true);
31808 if (num == error_mark_node)
31809 return list;
31810 num = fold_non_dependent_expr (num);
31811 if (!tree_fits_shwi_p (num)
31812 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31813 || (n = tree_to_shwi (num)) <= 0
31814 || (int) n != n)
31816 error_at (loc, "collapse argument needs positive constant integer expression");
31817 return list;
31820 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31821 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31822 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31823 OMP_CLAUSE_CHAIN (c) = list;
31824 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31826 return c;
31829 /* OpenMP 2.5:
31830 default ( none | shared )
31832 OpenACC:
31833 default ( none | present ) */
31835 static tree
31836 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31837 location_t location, bool is_oacc)
31839 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31840 tree c;
31842 matching_parens parens;
31843 if (!parens.require_open (parser))
31844 return list;
31845 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31848 const char *p = IDENTIFIER_POINTER (id);
31850 switch (p[0])
31852 case 'n':
31853 if (strcmp ("none", p) != 0)
31854 goto invalid_kind;
31855 kind = OMP_CLAUSE_DEFAULT_NONE;
31856 break;
31858 case 'p':
31859 if (strcmp ("present", p) != 0 || !is_oacc)
31860 goto invalid_kind;
31861 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31862 break;
31864 case 's':
31865 if (strcmp ("shared", p) != 0 || is_oacc)
31866 goto invalid_kind;
31867 kind = OMP_CLAUSE_DEFAULT_SHARED;
31868 break;
31870 default:
31871 goto invalid_kind;
31874 cp_lexer_consume_token (parser->lexer);
31876 else
31878 invalid_kind:
31879 if (is_oacc)
31880 cp_parser_error (parser, "expected %<none%> or %<present%>");
31881 else
31882 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31885 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31886 || !parens.require_close (parser))
31887 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31888 /*or_comma=*/false,
31889 /*consume_paren=*/true);
31891 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31892 return list;
31894 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31895 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31896 OMP_CLAUSE_CHAIN (c) = list;
31897 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31899 return c;
31902 /* OpenMP 3.1:
31903 final ( expression ) */
31905 static tree
31906 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31908 tree t, c;
31910 matching_parens parens;
31911 if (!parens.require_open (parser))
31912 return list;
31914 t = cp_parser_condition (parser);
31916 if (t == error_mark_node
31917 || !parens.require_close (parser))
31918 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31919 /*or_comma=*/false,
31920 /*consume_paren=*/true);
31922 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31924 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31925 OMP_CLAUSE_FINAL_EXPR (c) = t;
31926 OMP_CLAUSE_CHAIN (c) = list;
31928 return c;
31931 /* OpenMP 2.5:
31932 if ( expression )
31934 OpenMP 4.5:
31935 if ( directive-name-modifier : expression )
31937 directive-name-modifier:
31938 parallel | task | taskloop | target data | target | target update
31939 | target enter data | target exit data */
31941 static tree
31942 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31943 bool is_omp)
31945 tree t, c;
31946 enum tree_code if_modifier = ERROR_MARK;
31948 matching_parens parens;
31949 if (!parens.require_open (parser))
31950 return list;
31952 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31955 const char *p = IDENTIFIER_POINTER (id);
31956 int n = 2;
31958 if (strcmp ("parallel", p) == 0)
31959 if_modifier = OMP_PARALLEL;
31960 else if (strcmp ("task", p) == 0)
31961 if_modifier = OMP_TASK;
31962 else if (strcmp ("taskloop", p) == 0)
31963 if_modifier = OMP_TASKLOOP;
31964 else if (strcmp ("target", p) == 0)
31966 if_modifier = OMP_TARGET;
31967 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31969 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31970 p = IDENTIFIER_POINTER (id);
31971 if (strcmp ("data", p) == 0)
31972 if_modifier = OMP_TARGET_DATA;
31973 else if (strcmp ("update", p) == 0)
31974 if_modifier = OMP_TARGET_UPDATE;
31975 else if (strcmp ("enter", p) == 0)
31976 if_modifier = OMP_TARGET_ENTER_DATA;
31977 else if (strcmp ("exit", p) == 0)
31978 if_modifier = OMP_TARGET_EXIT_DATA;
31979 if (if_modifier != OMP_TARGET)
31980 n = 3;
31981 else
31983 location_t loc
31984 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31985 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31986 "or %<exit%>");
31987 if_modifier = ERROR_MARK;
31989 if (if_modifier == OMP_TARGET_ENTER_DATA
31990 || if_modifier == OMP_TARGET_EXIT_DATA)
31992 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31994 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31995 p = IDENTIFIER_POINTER (id);
31996 if (strcmp ("data", p) == 0)
31997 n = 4;
31999 if (n != 4)
32001 location_t loc
32002 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32003 error_at (loc, "expected %<data%>");
32004 if_modifier = ERROR_MARK;
32009 if (if_modifier != ERROR_MARK)
32011 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32013 while (n-- > 0)
32014 cp_lexer_consume_token (parser->lexer);
32016 else
32018 if (n > 2)
32020 location_t loc
32021 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32022 error_at (loc, "expected %<:%>");
32024 if_modifier = ERROR_MARK;
32029 t = cp_parser_condition (parser);
32031 if (t == error_mark_node
32032 || !parens.require_close (parser))
32033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32034 /*or_comma=*/false,
32035 /*consume_paren=*/true);
32037 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32038 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32040 if (if_modifier != ERROR_MARK
32041 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32043 const char *p = NULL;
32044 switch (if_modifier)
32046 case OMP_PARALLEL: p = "parallel"; break;
32047 case OMP_TASK: p = "task"; break;
32048 case OMP_TASKLOOP: p = "taskloop"; break;
32049 case OMP_TARGET_DATA: p = "target data"; break;
32050 case OMP_TARGET: p = "target"; break;
32051 case OMP_TARGET_UPDATE: p = "target update"; break;
32052 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32053 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32054 default: gcc_unreachable ();
32056 error_at (location, "too many %<if%> clauses with %qs modifier",
32058 return list;
32060 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32062 if (!is_omp)
32063 error_at (location, "too many %<if%> clauses");
32064 else
32065 error_at (location, "too many %<if%> clauses without modifier");
32066 return list;
32068 else if (if_modifier == ERROR_MARK
32069 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32071 error_at (location, "if any %<if%> clause has modifier, then all "
32072 "%<if%> clauses have to use modifier");
32073 return list;
32077 c = build_omp_clause (location, OMP_CLAUSE_IF);
32078 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32079 OMP_CLAUSE_IF_EXPR (c) = t;
32080 OMP_CLAUSE_CHAIN (c) = list;
32082 return c;
32085 /* OpenMP 3.1:
32086 mergeable */
32088 static tree
32089 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32090 tree list, location_t location)
32092 tree c;
32094 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32095 location);
32097 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32098 OMP_CLAUSE_CHAIN (c) = list;
32099 return c;
32102 /* OpenMP 2.5:
32103 nowait */
32105 static tree
32106 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32107 tree list, location_t location)
32109 tree c;
32111 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32113 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32114 OMP_CLAUSE_CHAIN (c) = list;
32115 return c;
32118 /* OpenMP 2.5:
32119 num_threads ( expression ) */
32121 static tree
32122 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32123 location_t location)
32125 tree t, c;
32127 matching_parens parens;
32128 if (!parens.require_open (parser))
32129 return list;
32131 t = cp_parser_expression (parser);
32133 if (t == error_mark_node
32134 || !parens.require_close (parser))
32135 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32136 /*or_comma=*/false,
32137 /*consume_paren=*/true);
32139 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32140 "num_threads", location);
32142 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32143 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32144 OMP_CLAUSE_CHAIN (c) = list;
32146 return c;
32149 /* OpenMP 4.5:
32150 num_tasks ( expression ) */
32152 static tree
32153 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32154 location_t location)
32156 tree t, c;
32158 matching_parens parens;
32159 if (!parens.require_open (parser))
32160 return list;
32162 t = cp_parser_expression (parser);
32164 if (t == error_mark_node
32165 || !parens.require_close (parser))
32166 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32167 /*or_comma=*/false,
32168 /*consume_paren=*/true);
32170 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32171 "num_tasks", location);
32173 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32174 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32175 OMP_CLAUSE_CHAIN (c) = list;
32177 return c;
32180 /* OpenMP 4.5:
32181 grainsize ( expression ) */
32183 static tree
32184 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32185 location_t location)
32187 tree t, c;
32189 matching_parens parens;
32190 if (!parens.require_open (parser))
32191 return list;
32193 t = cp_parser_expression (parser);
32195 if (t == error_mark_node
32196 || !parens.require_close (parser))
32197 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32198 /*or_comma=*/false,
32199 /*consume_paren=*/true);
32201 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32202 "grainsize", location);
32204 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32205 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32206 OMP_CLAUSE_CHAIN (c) = list;
32208 return c;
32211 /* OpenMP 4.5:
32212 priority ( expression ) */
32214 static tree
32215 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32216 location_t location)
32218 tree t, c;
32220 matching_parens parens;
32221 if (!parens.require_open (parser))
32222 return list;
32224 t = cp_parser_expression (parser);
32226 if (t == error_mark_node
32227 || !parens.require_close (parser))
32228 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32229 /*or_comma=*/false,
32230 /*consume_paren=*/true);
32232 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32233 "priority", location);
32235 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32236 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32237 OMP_CLAUSE_CHAIN (c) = list;
32239 return c;
32242 /* OpenMP 4.5:
32243 hint ( expression ) */
32245 static tree
32246 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32247 location_t location)
32249 tree t, c;
32251 matching_parens parens;
32252 if (!parens.require_open (parser))
32253 return list;
32255 t = cp_parser_expression (parser);
32257 if (t == error_mark_node
32258 || !parens.require_close (parser))
32259 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32260 /*or_comma=*/false,
32261 /*consume_paren=*/true);
32263 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32265 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32266 OMP_CLAUSE_HINT_EXPR (c) = t;
32267 OMP_CLAUSE_CHAIN (c) = list;
32269 return c;
32272 /* OpenMP 4.5:
32273 defaultmap ( tofrom : scalar ) */
32275 static tree
32276 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32277 location_t location)
32279 tree c, id;
32280 const char *p;
32282 matching_parens parens;
32283 if (!parens.require_open (parser))
32284 return list;
32286 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32288 cp_parser_error (parser, "expected %<tofrom%>");
32289 goto out_err;
32291 id = cp_lexer_peek_token (parser->lexer)->u.value;
32292 p = IDENTIFIER_POINTER (id);
32293 if (strcmp (p, "tofrom") != 0)
32295 cp_parser_error (parser, "expected %<tofrom%>");
32296 goto out_err;
32298 cp_lexer_consume_token (parser->lexer);
32299 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32300 goto out_err;
32302 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32304 cp_parser_error (parser, "expected %<scalar%>");
32305 goto out_err;
32307 id = cp_lexer_peek_token (parser->lexer)->u.value;
32308 p = IDENTIFIER_POINTER (id);
32309 if (strcmp (p, "scalar") != 0)
32311 cp_parser_error (parser, "expected %<scalar%>");
32312 goto out_err;
32314 cp_lexer_consume_token (parser->lexer);
32315 if (!parens.require_close (parser))
32316 goto out_err;
32318 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32319 location);
32321 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32322 OMP_CLAUSE_CHAIN (c) = list;
32323 return c;
32325 out_err:
32326 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32327 /*or_comma=*/false,
32328 /*consume_paren=*/true);
32329 return list;
32332 /* OpenMP 2.5:
32333 ordered
32335 OpenMP 4.5:
32336 ordered ( constant-expression ) */
32338 static tree
32339 cp_parser_omp_clause_ordered (cp_parser *parser,
32340 tree list, location_t location)
32342 tree c, num = NULL_TREE;
32343 HOST_WIDE_INT n;
32345 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32346 "ordered", location);
32348 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32350 matching_parens parens;
32351 parens.consume_open (parser);
32353 num = cp_parser_constant_expression (parser);
32355 if (!parens.require_close (parser))
32356 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32357 /*or_comma=*/false,
32358 /*consume_paren=*/true);
32360 if (num == error_mark_node)
32361 return list;
32362 num = fold_non_dependent_expr (num);
32363 if (!tree_fits_shwi_p (num)
32364 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32365 || (n = tree_to_shwi (num)) <= 0
32366 || (int) n != n)
32368 error_at (location,
32369 "ordered argument needs positive constant integer "
32370 "expression");
32371 return list;
32375 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32376 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32377 OMP_CLAUSE_CHAIN (c) = list;
32378 return c;
32381 /* OpenMP 2.5:
32382 reduction ( reduction-operator : variable-list )
32384 reduction-operator:
32385 One of: + * - & ^ | && ||
32387 OpenMP 3.1:
32389 reduction-operator:
32390 One of: + * - & ^ | && || min max
32392 OpenMP 4.0:
32394 reduction-operator:
32395 One of: + * - & ^ | && ||
32396 id-expression */
32398 static tree
32399 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32401 enum tree_code code = ERROR_MARK;
32402 tree nlist, c, id = NULL_TREE;
32404 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32405 return list;
32407 switch (cp_lexer_peek_token (parser->lexer)->type)
32409 case CPP_PLUS: code = PLUS_EXPR; break;
32410 case CPP_MULT: code = MULT_EXPR; break;
32411 case CPP_MINUS: code = MINUS_EXPR; break;
32412 case CPP_AND: code = BIT_AND_EXPR; break;
32413 case CPP_XOR: code = BIT_XOR_EXPR; break;
32414 case CPP_OR: code = BIT_IOR_EXPR; break;
32415 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32416 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32417 default: break;
32420 if (code != ERROR_MARK)
32421 cp_lexer_consume_token (parser->lexer);
32422 else
32424 bool saved_colon_corrects_to_scope_p;
32425 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32426 parser->colon_corrects_to_scope_p = false;
32427 id = cp_parser_id_expression (parser, /*template_p=*/false,
32428 /*check_dependency_p=*/true,
32429 /*template_p=*/NULL,
32430 /*declarator_p=*/false,
32431 /*optional_p=*/false);
32432 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32433 if (identifier_p (id))
32435 const char *p = IDENTIFIER_POINTER (id);
32437 if (strcmp (p, "min") == 0)
32438 code = MIN_EXPR;
32439 else if (strcmp (p, "max") == 0)
32440 code = MAX_EXPR;
32441 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32442 code = PLUS_EXPR;
32443 else if (id == ovl_op_identifier (false, MULT_EXPR))
32444 code = MULT_EXPR;
32445 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32446 code = MINUS_EXPR;
32447 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32448 code = BIT_AND_EXPR;
32449 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32450 code = BIT_IOR_EXPR;
32451 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32452 code = BIT_XOR_EXPR;
32453 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32454 code = TRUTH_ANDIF_EXPR;
32455 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32456 code = TRUTH_ORIF_EXPR;
32457 id = omp_reduction_id (code, id, NULL_TREE);
32458 tree scope = parser->scope;
32459 if (scope)
32460 id = build_qualified_name (NULL_TREE, scope, id, false);
32461 parser->scope = NULL_TREE;
32462 parser->qualifying_scope = NULL_TREE;
32463 parser->object_scope = NULL_TREE;
32465 else
32467 error ("invalid reduction-identifier");
32468 resync_fail:
32469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32470 /*or_comma=*/false,
32471 /*consume_paren=*/true);
32472 return list;
32476 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32477 goto resync_fail;
32479 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32480 NULL);
32481 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32483 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32484 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32487 return nlist;
32490 /* OpenMP 2.5:
32491 schedule ( schedule-kind )
32492 schedule ( schedule-kind , expression )
32494 schedule-kind:
32495 static | dynamic | guided | runtime | auto
32497 OpenMP 4.5:
32498 schedule ( schedule-modifier : schedule-kind )
32499 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32501 schedule-modifier:
32502 simd
32503 monotonic
32504 nonmonotonic */
32506 static tree
32507 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32509 tree c, t;
32510 int modifiers = 0, nmodifiers = 0;
32512 matching_parens parens;
32513 if (!parens.require_open (parser))
32514 return list;
32516 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32518 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32521 const char *p = IDENTIFIER_POINTER (id);
32522 if (strcmp ("simd", p) == 0)
32523 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32524 else if (strcmp ("monotonic", p) == 0)
32525 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32526 else if (strcmp ("nonmonotonic", p) == 0)
32527 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32528 else
32529 break;
32530 cp_lexer_consume_token (parser->lexer);
32531 if (nmodifiers++ == 0
32532 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32533 cp_lexer_consume_token (parser->lexer);
32534 else
32536 cp_parser_require (parser, CPP_COLON, RT_COLON);
32537 break;
32541 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32543 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32544 const char *p = IDENTIFIER_POINTER (id);
32546 switch (p[0])
32548 case 'd':
32549 if (strcmp ("dynamic", p) != 0)
32550 goto invalid_kind;
32551 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32552 break;
32554 case 'g':
32555 if (strcmp ("guided", p) != 0)
32556 goto invalid_kind;
32557 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32558 break;
32560 case 'r':
32561 if (strcmp ("runtime", p) != 0)
32562 goto invalid_kind;
32563 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32564 break;
32566 default:
32567 goto invalid_kind;
32570 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32571 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32572 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32573 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32574 else
32575 goto invalid_kind;
32576 cp_lexer_consume_token (parser->lexer);
32578 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32579 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32580 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32581 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32583 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32584 "specified");
32585 modifiers = 0;
32588 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32590 cp_token *token;
32591 cp_lexer_consume_token (parser->lexer);
32593 token = cp_lexer_peek_token (parser->lexer);
32594 t = cp_parser_assignment_expression (parser);
32596 if (t == error_mark_node)
32597 goto resync_fail;
32598 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32599 error_at (token->location, "schedule %<runtime%> does not take "
32600 "a %<chunk_size%> parameter");
32601 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32602 error_at (token->location, "schedule %<auto%> does not take "
32603 "a %<chunk_size%> parameter");
32604 else
32605 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32607 if (!parens.require_close (parser))
32608 goto resync_fail;
32610 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32611 goto resync_fail;
32613 OMP_CLAUSE_SCHEDULE_KIND (c)
32614 = (enum omp_clause_schedule_kind)
32615 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32617 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32618 OMP_CLAUSE_CHAIN (c) = list;
32619 return c;
32621 invalid_kind:
32622 cp_parser_error (parser, "invalid schedule kind");
32623 resync_fail:
32624 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32625 /*or_comma=*/false,
32626 /*consume_paren=*/true);
32627 return list;
32630 /* OpenMP 3.0:
32631 untied */
32633 static tree
32634 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32635 tree list, location_t location)
32637 tree c;
32639 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32641 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32642 OMP_CLAUSE_CHAIN (c) = list;
32643 return c;
32646 /* OpenMP 4.0:
32647 inbranch
32648 notinbranch */
32650 static tree
32651 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32652 tree list, location_t location)
32654 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32655 tree c = build_omp_clause (location, code);
32656 OMP_CLAUSE_CHAIN (c) = list;
32657 return c;
32660 /* OpenMP 4.0:
32661 parallel
32663 sections
32664 taskgroup */
32666 static tree
32667 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32668 enum omp_clause_code code,
32669 tree list, location_t location)
32671 tree c = build_omp_clause (location, code);
32672 OMP_CLAUSE_CHAIN (c) = list;
32673 return c;
32676 /* OpenMP 4.5:
32677 nogroup */
32679 static tree
32680 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32681 tree list, location_t location)
32683 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32684 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32685 OMP_CLAUSE_CHAIN (c) = list;
32686 return c;
32689 /* OpenMP 4.5:
32690 simd
32691 threads */
32693 static tree
32694 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32695 enum omp_clause_code code,
32696 tree list, location_t location)
32698 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32699 tree c = build_omp_clause (location, code);
32700 OMP_CLAUSE_CHAIN (c) = list;
32701 return c;
32704 /* OpenMP 4.0:
32705 num_teams ( expression ) */
32707 static tree
32708 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32709 location_t location)
32711 tree t, c;
32713 matching_parens parens;
32714 if (!parens.require_open (parser))
32715 return list;
32717 t = cp_parser_expression (parser);
32719 if (t == error_mark_node
32720 || !parens.require_close (parser))
32721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32722 /*or_comma=*/false,
32723 /*consume_paren=*/true);
32725 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32726 "num_teams", location);
32728 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32729 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32730 OMP_CLAUSE_CHAIN (c) = list;
32732 return c;
32735 /* OpenMP 4.0:
32736 thread_limit ( expression ) */
32738 static tree
32739 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32740 location_t location)
32742 tree t, c;
32744 matching_parens parens;
32745 if (!parens.require_open (parser))
32746 return list;
32748 t = cp_parser_expression (parser);
32750 if (t == error_mark_node
32751 || !parens.require_close (parser))
32752 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32753 /*or_comma=*/false,
32754 /*consume_paren=*/true);
32756 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32757 "thread_limit", location);
32759 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32760 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32761 OMP_CLAUSE_CHAIN (c) = list;
32763 return c;
32766 /* OpenMP 4.0:
32767 aligned ( variable-list )
32768 aligned ( variable-list : constant-expression ) */
32770 static tree
32771 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32773 tree nlist, c, alignment = NULL_TREE;
32774 bool colon;
32776 matching_parens parens;
32777 if (!parens.require_open (parser))
32778 return list;
32780 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32781 &colon);
32783 if (colon)
32785 alignment = cp_parser_constant_expression (parser);
32787 if (!parens.require_close (parser))
32788 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32789 /*or_comma=*/false,
32790 /*consume_paren=*/true);
32792 if (alignment == error_mark_node)
32793 alignment = NULL_TREE;
32796 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32797 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32799 return nlist;
32802 /* OpenMP 4.0:
32803 linear ( variable-list )
32804 linear ( variable-list : expression )
32806 OpenMP 4.5:
32807 linear ( modifier ( variable-list ) )
32808 linear ( modifier ( variable-list ) : expression ) */
32810 static tree
32811 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32812 bool declare_simd)
32814 tree nlist, c, step = integer_one_node;
32815 bool colon;
32816 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32818 matching_parens parens;
32819 if (!parens.require_open (parser))
32820 return list;
32822 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32824 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32825 const char *p = IDENTIFIER_POINTER (id);
32827 if (strcmp ("ref", p) == 0)
32828 kind = OMP_CLAUSE_LINEAR_REF;
32829 else if (strcmp ("val", p) == 0)
32830 kind = OMP_CLAUSE_LINEAR_VAL;
32831 else if (strcmp ("uval", p) == 0)
32832 kind = OMP_CLAUSE_LINEAR_UVAL;
32833 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32834 cp_lexer_consume_token (parser->lexer);
32835 else
32836 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32839 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32840 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32841 &colon);
32842 else
32844 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32845 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32846 if (colon)
32847 cp_parser_require (parser, CPP_COLON, RT_COLON);
32848 else if (!parens.require_close (parser))
32849 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32850 /*or_comma=*/false,
32851 /*consume_paren=*/true);
32854 if (colon)
32856 step = NULL_TREE;
32857 if (declare_simd
32858 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32859 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32861 cp_token *token = cp_lexer_peek_token (parser->lexer);
32862 cp_parser_parse_tentatively (parser);
32863 step = cp_parser_id_expression (parser, /*template_p=*/false,
32864 /*check_dependency_p=*/true,
32865 /*template_p=*/NULL,
32866 /*declarator_p=*/false,
32867 /*optional_p=*/false);
32868 if (step != error_mark_node)
32869 step = cp_parser_lookup_name_simple (parser, step, token->location);
32870 if (step == error_mark_node)
32872 step = NULL_TREE;
32873 cp_parser_abort_tentative_parse (parser);
32875 else if (!cp_parser_parse_definitely (parser))
32876 step = NULL_TREE;
32878 if (!step)
32879 step = cp_parser_expression (parser);
32881 if (!parens.require_close (parser))
32882 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32883 /*or_comma=*/false,
32884 /*consume_paren=*/true);
32886 if (step == error_mark_node)
32887 return list;
32890 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32892 OMP_CLAUSE_LINEAR_STEP (c) = step;
32893 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32896 return nlist;
32899 /* OpenMP 4.0:
32900 safelen ( constant-expression ) */
32902 static tree
32903 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32904 location_t location)
32906 tree t, c;
32908 matching_parens parens;
32909 if (!parens.require_open (parser))
32910 return list;
32912 t = cp_parser_constant_expression (parser);
32914 if (t == error_mark_node
32915 || !parens.require_close (parser))
32916 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32917 /*or_comma=*/false,
32918 /*consume_paren=*/true);
32920 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32922 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32923 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32924 OMP_CLAUSE_CHAIN (c) = list;
32926 return c;
32929 /* OpenMP 4.0:
32930 simdlen ( constant-expression ) */
32932 static tree
32933 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32934 location_t location)
32936 tree t, c;
32938 matching_parens parens;
32939 if (!parens.require_open (parser))
32940 return list;
32942 t = cp_parser_constant_expression (parser);
32944 if (t == error_mark_node
32945 || !parens.require_close (parser))
32946 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32947 /*or_comma=*/false,
32948 /*consume_paren=*/true);
32950 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32952 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32953 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32954 OMP_CLAUSE_CHAIN (c) = list;
32956 return c;
32959 /* OpenMP 4.5:
32960 vec:
32961 identifier [+/- integer]
32962 vec , identifier [+/- integer]
32965 static tree
32966 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32967 tree list)
32969 tree vec = NULL;
32971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32973 cp_parser_error (parser, "expected identifier");
32974 return list;
32977 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32979 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32980 tree t, identifier = cp_parser_identifier (parser);
32981 tree addend = NULL;
32983 if (identifier == error_mark_node)
32984 t = error_mark_node;
32985 else
32987 t = cp_parser_lookup_name_simple
32988 (parser, identifier,
32989 cp_lexer_peek_token (parser->lexer)->location);
32990 if (t == error_mark_node)
32991 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32992 id_loc);
32995 bool neg = false;
32996 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32997 neg = true;
32998 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33000 addend = integer_zero_node;
33001 goto add_to_vector;
33003 cp_lexer_consume_token (parser->lexer);
33005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33007 cp_parser_error (parser, "expected integer");
33008 return list;
33011 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33012 if (TREE_CODE (addend) != INTEGER_CST)
33014 cp_parser_error (parser, "expected integer");
33015 return list;
33017 cp_lexer_consume_token (parser->lexer);
33019 add_to_vector:
33020 if (t != error_mark_node)
33022 vec = tree_cons (addend, t, vec);
33023 if (neg)
33024 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33027 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33028 break;
33030 cp_lexer_consume_token (parser->lexer);
33033 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33035 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33036 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33037 OMP_CLAUSE_DECL (u) = nreverse (vec);
33038 OMP_CLAUSE_CHAIN (u) = list;
33039 return u;
33041 return list;
33044 /* OpenMP 4.0:
33045 depend ( depend-kind : variable-list )
33047 depend-kind:
33048 in | out | inout
33050 OpenMP 4.5:
33051 depend ( source )
33053 depend ( sink : vec ) */
33055 static tree
33056 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33058 tree nlist, c;
33059 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33061 matching_parens parens;
33062 if (!parens.require_open (parser))
33063 return list;
33065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33067 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33068 const char *p = IDENTIFIER_POINTER (id);
33070 if (strcmp ("in", p) == 0)
33071 kind = OMP_CLAUSE_DEPEND_IN;
33072 else if (strcmp ("inout", p) == 0)
33073 kind = OMP_CLAUSE_DEPEND_INOUT;
33074 else if (strcmp ("out", p) == 0)
33075 kind = OMP_CLAUSE_DEPEND_OUT;
33076 else if (strcmp ("source", p) == 0)
33077 kind = OMP_CLAUSE_DEPEND_SOURCE;
33078 else if (strcmp ("sink", p) == 0)
33079 kind = OMP_CLAUSE_DEPEND_SINK;
33080 else
33081 goto invalid_kind;
33083 else
33084 goto invalid_kind;
33086 cp_lexer_consume_token (parser->lexer);
33088 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33090 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33091 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33092 OMP_CLAUSE_DECL (c) = NULL_TREE;
33093 OMP_CLAUSE_CHAIN (c) = list;
33094 if (!parens.require_close (parser))
33095 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33096 /*or_comma=*/false,
33097 /*consume_paren=*/true);
33098 return c;
33101 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33102 goto resync_fail;
33104 if (kind == OMP_CLAUSE_DEPEND_SINK)
33105 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33106 else
33108 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33109 list, NULL);
33111 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33112 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33114 return nlist;
33116 invalid_kind:
33117 cp_parser_error (parser, "invalid depend kind");
33118 resync_fail:
33119 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33120 /*or_comma=*/false,
33121 /*consume_paren=*/true);
33122 return list;
33125 /* OpenMP 4.0:
33126 map ( map-kind : variable-list )
33127 map ( variable-list )
33129 map-kind:
33130 alloc | to | from | tofrom
33132 OpenMP 4.5:
33133 map-kind:
33134 alloc | to | from | tofrom | release | delete
33136 map ( always [,] map-kind: variable-list ) */
33138 static tree
33139 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33141 tree nlist, c;
33142 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33143 bool always = false;
33145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33146 return list;
33148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33150 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33151 const char *p = IDENTIFIER_POINTER (id);
33153 if (strcmp ("always", p) == 0)
33155 int nth = 2;
33156 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33157 nth++;
33158 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33159 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33160 == RID_DELETE))
33161 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33162 == CPP_COLON))
33164 always = true;
33165 cp_lexer_consume_token (parser->lexer);
33166 if (nth == 3)
33167 cp_lexer_consume_token (parser->lexer);
33172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33173 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33175 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33176 const char *p = IDENTIFIER_POINTER (id);
33178 if (strcmp ("alloc", p) == 0)
33179 kind = GOMP_MAP_ALLOC;
33180 else if (strcmp ("to", p) == 0)
33181 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33182 else if (strcmp ("from", p) == 0)
33183 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33184 else if (strcmp ("tofrom", p) == 0)
33185 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33186 else if (strcmp ("release", p) == 0)
33187 kind = GOMP_MAP_RELEASE;
33188 else
33190 cp_parser_error (parser, "invalid map kind");
33191 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33192 /*or_comma=*/false,
33193 /*consume_paren=*/true);
33194 return list;
33196 cp_lexer_consume_token (parser->lexer);
33197 cp_lexer_consume_token (parser->lexer);
33199 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33200 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33202 kind = GOMP_MAP_DELETE;
33203 cp_lexer_consume_token (parser->lexer);
33204 cp_lexer_consume_token (parser->lexer);
33207 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33208 NULL);
33210 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33211 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33213 return nlist;
33216 /* OpenMP 4.0:
33217 device ( expression ) */
33219 static tree
33220 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33221 location_t location)
33223 tree t, c;
33225 matching_parens parens;
33226 if (!parens.require_open (parser))
33227 return list;
33229 t = cp_parser_expression (parser);
33231 if (t == error_mark_node
33232 || !parens.require_close (parser))
33233 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33234 /*or_comma=*/false,
33235 /*consume_paren=*/true);
33237 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33238 "device", location);
33240 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33241 OMP_CLAUSE_DEVICE_ID (c) = t;
33242 OMP_CLAUSE_CHAIN (c) = list;
33244 return c;
33247 /* OpenMP 4.0:
33248 dist_schedule ( static )
33249 dist_schedule ( static , expression ) */
33251 static tree
33252 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33253 location_t location)
33255 tree c, t;
33257 matching_parens parens;
33258 if (!parens.require_open (parser))
33259 return list;
33261 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33263 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33264 goto invalid_kind;
33265 cp_lexer_consume_token (parser->lexer);
33267 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33269 cp_lexer_consume_token (parser->lexer);
33271 t = cp_parser_assignment_expression (parser);
33273 if (t == error_mark_node)
33274 goto resync_fail;
33275 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33277 if (!parens.require_close (parser))
33278 goto resync_fail;
33280 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33281 goto resync_fail;
33283 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33284 location);
33285 OMP_CLAUSE_CHAIN (c) = list;
33286 return c;
33288 invalid_kind:
33289 cp_parser_error (parser, "invalid dist_schedule kind");
33290 resync_fail:
33291 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33292 /*or_comma=*/false,
33293 /*consume_paren=*/true);
33294 return list;
33297 /* OpenMP 4.0:
33298 proc_bind ( proc-bind-kind )
33300 proc-bind-kind:
33301 master | close | spread */
33303 static tree
33304 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33305 location_t location)
33307 tree c;
33308 enum omp_clause_proc_bind_kind kind;
33310 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33311 return list;
33313 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33315 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33316 const char *p = IDENTIFIER_POINTER (id);
33318 if (strcmp ("master", p) == 0)
33319 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33320 else if (strcmp ("close", p) == 0)
33321 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33322 else if (strcmp ("spread", p) == 0)
33323 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33324 else
33325 goto invalid_kind;
33327 else
33328 goto invalid_kind;
33330 cp_lexer_consume_token (parser->lexer);
33331 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33332 goto resync_fail;
33334 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33335 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33336 location);
33337 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33338 OMP_CLAUSE_CHAIN (c) = list;
33339 return c;
33341 invalid_kind:
33342 cp_parser_error (parser, "invalid depend kind");
33343 resync_fail:
33344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33345 /*or_comma=*/false,
33346 /*consume_paren=*/true);
33347 return list;
33350 /* OpenACC:
33351 async [( int-expr )] */
33353 static tree
33354 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33356 tree c, t;
33357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33359 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33361 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33363 matching_parens parens;
33364 parens.consume_open (parser);
33366 t = cp_parser_expression (parser);
33367 if (t == error_mark_node
33368 || !parens.require_close (parser))
33369 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33370 /*or_comma=*/false,
33371 /*consume_paren=*/true);
33374 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33376 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33377 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33378 OMP_CLAUSE_CHAIN (c) = list;
33379 list = c;
33381 return list;
33384 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33385 is a bitmask in MASK. Return the list of clauses found. */
33387 static tree
33388 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33389 const char *where, cp_token *pragma_tok,
33390 bool finish_p = true)
33392 tree clauses = NULL;
33393 bool first = true;
33395 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33397 location_t here;
33398 pragma_omp_clause c_kind;
33399 omp_clause_code code;
33400 const char *c_name;
33401 tree prev = clauses;
33403 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33404 cp_lexer_consume_token (parser->lexer);
33406 here = cp_lexer_peek_token (parser->lexer)->location;
33407 c_kind = cp_parser_omp_clause_name (parser);
33409 switch (c_kind)
33411 case PRAGMA_OACC_CLAUSE_ASYNC:
33412 clauses = cp_parser_oacc_clause_async (parser, clauses);
33413 c_name = "async";
33414 break;
33415 case PRAGMA_OACC_CLAUSE_AUTO:
33416 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33417 clauses, here);
33418 c_name = "auto";
33419 break;
33420 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33421 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33422 c_name = "collapse";
33423 break;
33424 case PRAGMA_OACC_CLAUSE_COPY:
33425 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33426 c_name = "copy";
33427 break;
33428 case PRAGMA_OACC_CLAUSE_COPYIN:
33429 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33430 c_name = "copyin";
33431 break;
33432 case PRAGMA_OACC_CLAUSE_COPYOUT:
33433 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33434 c_name = "copyout";
33435 break;
33436 case PRAGMA_OACC_CLAUSE_CREATE:
33437 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33438 c_name = "create";
33439 break;
33440 case PRAGMA_OACC_CLAUSE_DELETE:
33441 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33442 c_name = "delete";
33443 break;
33444 case PRAGMA_OMP_CLAUSE_DEFAULT:
33445 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33446 c_name = "default";
33447 break;
33448 case PRAGMA_OACC_CLAUSE_DEVICE:
33449 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33450 c_name = "device";
33451 break;
33452 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33453 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33454 c_name = "deviceptr";
33455 break;
33456 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33457 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33458 c_name = "device_resident";
33459 break;
33460 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33461 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33462 clauses);
33463 c_name = "firstprivate";
33464 break;
33465 case PRAGMA_OACC_CLAUSE_GANG:
33466 c_name = "gang";
33467 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33468 c_name, clauses);
33469 break;
33470 case PRAGMA_OACC_CLAUSE_HOST:
33471 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33472 c_name = "host";
33473 break;
33474 case PRAGMA_OACC_CLAUSE_IF:
33475 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33476 c_name = "if";
33477 break;
33478 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33479 clauses = cp_parser_oacc_simple_clause (parser,
33480 OMP_CLAUSE_INDEPENDENT,
33481 clauses, here);
33482 c_name = "independent";
33483 break;
33484 case PRAGMA_OACC_CLAUSE_LINK:
33485 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33486 c_name = "link";
33487 break;
33488 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33489 code = OMP_CLAUSE_NUM_GANGS;
33490 c_name = "num_gangs";
33491 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33492 clauses);
33493 break;
33494 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33495 c_name = "num_workers";
33496 code = OMP_CLAUSE_NUM_WORKERS;
33497 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33498 clauses);
33499 break;
33500 case PRAGMA_OACC_CLAUSE_PRESENT:
33501 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33502 c_name = "present";
33503 break;
33504 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33505 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33506 c_name = "present_or_copy";
33507 break;
33508 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33509 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33510 c_name = "present_or_copyin";
33511 break;
33512 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33513 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33514 c_name = "present_or_copyout";
33515 break;
33516 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33517 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33518 c_name = "present_or_create";
33519 break;
33520 case PRAGMA_OACC_CLAUSE_PRIVATE:
33521 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33522 clauses);
33523 c_name = "private";
33524 break;
33525 case PRAGMA_OACC_CLAUSE_REDUCTION:
33526 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33527 c_name = "reduction";
33528 break;
33529 case PRAGMA_OACC_CLAUSE_SELF:
33530 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33531 c_name = "self";
33532 break;
33533 case PRAGMA_OACC_CLAUSE_SEQ:
33534 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33535 clauses, here);
33536 c_name = "seq";
33537 break;
33538 case PRAGMA_OACC_CLAUSE_TILE:
33539 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33540 c_name = "tile";
33541 break;
33542 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33543 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33544 clauses);
33545 c_name = "use_device";
33546 break;
33547 case PRAGMA_OACC_CLAUSE_VECTOR:
33548 c_name = "vector";
33549 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33550 c_name, clauses);
33551 break;
33552 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33553 c_name = "vector_length";
33554 code = OMP_CLAUSE_VECTOR_LENGTH;
33555 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33556 clauses);
33557 break;
33558 case PRAGMA_OACC_CLAUSE_WAIT:
33559 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33560 c_name = "wait";
33561 break;
33562 case PRAGMA_OACC_CLAUSE_WORKER:
33563 c_name = "worker";
33564 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33565 c_name, clauses);
33566 break;
33567 default:
33568 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33569 goto saw_error;
33572 first = false;
33574 if (((mask >> c_kind) & 1) == 0)
33576 /* Remove the invalid clause(s) from the list to avoid
33577 confusing the rest of the compiler. */
33578 clauses = prev;
33579 error_at (here, "%qs is not valid for %qs", c_name, where);
33583 saw_error:
33584 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33586 if (finish_p)
33587 return finish_omp_clauses (clauses, C_ORT_ACC);
33589 return clauses;
33592 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33593 is a bitmask in MASK. Return the list of clauses found; the result
33594 of clause default goes in *pdefault. */
33596 static tree
33597 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33598 const char *where, cp_token *pragma_tok,
33599 bool finish_p = true)
33601 tree clauses = NULL;
33602 bool first = true;
33603 cp_token *token = NULL;
33605 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33607 pragma_omp_clause c_kind;
33608 const char *c_name;
33609 tree prev = clauses;
33611 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33612 cp_lexer_consume_token (parser->lexer);
33614 token = cp_lexer_peek_token (parser->lexer);
33615 c_kind = cp_parser_omp_clause_name (parser);
33617 switch (c_kind)
33619 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33620 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33621 token->location);
33622 c_name = "collapse";
33623 break;
33624 case PRAGMA_OMP_CLAUSE_COPYIN:
33625 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33626 c_name = "copyin";
33627 break;
33628 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33629 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33630 clauses);
33631 c_name = "copyprivate";
33632 break;
33633 case PRAGMA_OMP_CLAUSE_DEFAULT:
33634 clauses = cp_parser_omp_clause_default (parser, clauses,
33635 token->location, false);
33636 c_name = "default";
33637 break;
33638 case PRAGMA_OMP_CLAUSE_FINAL:
33639 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33640 c_name = "final";
33641 break;
33642 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33643 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33644 clauses);
33645 c_name = "firstprivate";
33646 break;
33647 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33648 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33649 token->location);
33650 c_name = "grainsize";
33651 break;
33652 case PRAGMA_OMP_CLAUSE_HINT:
33653 clauses = cp_parser_omp_clause_hint (parser, clauses,
33654 token->location);
33655 c_name = "hint";
33656 break;
33657 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33658 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33659 token->location);
33660 c_name = "defaultmap";
33661 break;
33662 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33663 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33664 clauses);
33665 c_name = "use_device_ptr";
33666 break;
33667 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33668 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33669 clauses);
33670 c_name = "is_device_ptr";
33671 break;
33672 case PRAGMA_OMP_CLAUSE_IF:
33673 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33674 true);
33675 c_name = "if";
33676 break;
33677 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33678 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33679 clauses);
33680 c_name = "lastprivate";
33681 break;
33682 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33683 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33684 token->location);
33685 c_name = "mergeable";
33686 break;
33687 case PRAGMA_OMP_CLAUSE_NOWAIT:
33688 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33689 c_name = "nowait";
33690 break;
33691 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33692 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33693 token->location);
33694 c_name = "num_tasks";
33695 break;
33696 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33697 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33698 token->location);
33699 c_name = "num_threads";
33700 break;
33701 case PRAGMA_OMP_CLAUSE_ORDERED:
33702 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33703 token->location);
33704 c_name = "ordered";
33705 break;
33706 case PRAGMA_OMP_CLAUSE_PRIORITY:
33707 clauses = cp_parser_omp_clause_priority (parser, clauses,
33708 token->location);
33709 c_name = "priority";
33710 break;
33711 case PRAGMA_OMP_CLAUSE_PRIVATE:
33712 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33713 clauses);
33714 c_name = "private";
33715 break;
33716 case PRAGMA_OMP_CLAUSE_REDUCTION:
33717 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33718 c_name = "reduction";
33719 break;
33720 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33721 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33722 token->location);
33723 c_name = "schedule";
33724 break;
33725 case PRAGMA_OMP_CLAUSE_SHARED:
33726 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33727 clauses);
33728 c_name = "shared";
33729 break;
33730 case PRAGMA_OMP_CLAUSE_UNTIED:
33731 clauses = cp_parser_omp_clause_untied (parser, clauses,
33732 token->location);
33733 c_name = "untied";
33734 break;
33735 case PRAGMA_OMP_CLAUSE_INBRANCH:
33736 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33737 clauses, token->location);
33738 c_name = "inbranch";
33739 break;
33740 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33741 clauses = cp_parser_omp_clause_branch (parser,
33742 OMP_CLAUSE_NOTINBRANCH,
33743 clauses, token->location);
33744 c_name = "notinbranch";
33745 break;
33746 case PRAGMA_OMP_CLAUSE_PARALLEL:
33747 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33748 clauses, token->location);
33749 c_name = "parallel";
33750 if (!first)
33752 clause_not_first:
33753 error_at (token->location, "%qs must be the first clause of %qs",
33754 c_name, where);
33755 clauses = prev;
33757 break;
33758 case PRAGMA_OMP_CLAUSE_FOR:
33759 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33760 clauses, token->location);
33761 c_name = "for";
33762 if (!first)
33763 goto clause_not_first;
33764 break;
33765 case PRAGMA_OMP_CLAUSE_SECTIONS:
33766 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33767 clauses, token->location);
33768 c_name = "sections";
33769 if (!first)
33770 goto clause_not_first;
33771 break;
33772 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33773 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33774 clauses, token->location);
33775 c_name = "taskgroup";
33776 if (!first)
33777 goto clause_not_first;
33778 break;
33779 case PRAGMA_OMP_CLAUSE_LINK:
33780 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33781 c_name = "to";
33782 break;
33783 case PRAGMA_OMP_CLAUSE_TO:
33784 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33785 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33786 clauses);
33787 else
33788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33789 c_name = "to";
33790 break;
33791 case PRAGMA_OMP_CLAUSE_FROM:
33792 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33793 c_name = "from";
33794 break;
33795 case PRAGMA_OMP_CLAUSE_UNIFORM:
33796 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33797 clauses);
33798 c_name = "uniform";
33799 break;
33800 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33801 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33802 token->location);
33803 c_name = "num_teams";
33804 break;
33805 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33806 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33807 token->location);
33808 c_name = "thread_limit";
33809 break;
33810 case PRAGMA_OMP_CLAUSE_ALIGNED:
33811 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33812 c_name = "aligned";
33813 break;
33814 case PRAGMA_OMP_CLAUSE_LINEAR:
33816 bool declare_simd = false;
33817 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33818 declare_simd = true;
33819 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
33821 c_name = "linear";
33822 break;
33823 case PRAGMA_OMP_CLAUSE_DEPEND:
33824 clauses = cp_parser_omp_clause_depend (parser, clauses,
33825 token->location);
33826 c_name = "depend";
33827 break;
33828 case PRAGMA_OMP_CLAUSE_MAP:
33829 clauses = cp_parser_omp_clause_map (parser, clauses);
33830 c_name = "map";
33831 break;
33832 case PRAGMA_OMP_CLAUSE_DEVICE:
33833 clauses = cp_parser_omp_clause_device (parser, clauses,
33834 token->location);
33835 c_name = "device";
33836 break;
33837 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33838 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33839 token->location);
33840 c_name = "dist_schedule";
33841 break;
33842 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33843 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33844 token->location);
33845 c_name = "proc_bind";
33846 break;
33847 case PRAGMA_OMP_CLAUSE_SAFELEN:
33848 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33849 token->location);
33850 c_name = "safelen";
33851 break;
33852 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33853 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33854 token->location);
33855 c_name = "simdlen";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_NOGROUP:
33858 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33859 token->location);
33860 c_name = "nogroup";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_THREADS:
33863 clauses
33864 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33865 clauses, token->location);
33866 c_name = "threads";
33867 break;
33868 case PRAGMA_OMP_CLAUSE_SIMD:
33869 clauses
33870 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33871 clauses, token->location);
33872 c_name = "simd";
33873 break;
33874 default:
33875 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33876 goto saw_error;
33879 first = false;
33881 if (((mask >> c_kind) & 1) == 0)
33883 /* Remove the invalid clause(s) from the list to avoid
33884 confusing the rest of the compiler. */
33885 clauses = prev;
33886 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33889 saw_error:
33890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33891 if (finish_p)
33893 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33894 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33895 else
33896 return finish_omp_clauses (clauses, C_ORT_OMP);
33898 return clauses;
33901 /* OpenMP 2.5:
33902 structured-block:
33903 statement
33905 In practice, we're also interested in adding the statement to an
33906 outer node. So it is convenient if we work around the fact that
33907 cp_parser_statement calls add_stmt. */
33909 static unsigned
33910 cp_parser_begin_omp_structured_block (cp_parser *parser)
33912 unsigned save = parser->in_statement;
33914 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33915 This preserves the "not within loop or switch" style error messages
33916 for nonsense cases like
33917 void foo() {
33918 #pragma omp single
33919 break;
33922 if (parser->in_statement)
33923 parser->in_statement = IN_OMP_BLOCK;
33925 return save;
33928 static void
33929 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33931 parser->in_statement = save;
33934 static tree
33935 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33937 tree stmt = begin_omp_structured_block ();
33938 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33940 cp_parser_statement (parser, NULL_TREE, false, if_p);
33942 cp_parser_end_omp_structured_block (parser, save);
33943 return finish_omp_structured_block (stmt);
33946 /* OpenMP 2.5:
33947 # pragma omp atomic new-line
33948 expression-stmt
33950 expression-stmt:
33951 x binop= expr | x++ | ++x | x-- | --x
33952 binop:
33953 +, *, -, /, &, ^, |, <<, >>
33955 where x is an lvalue expression with scalar type.
33957 OpenMP 3.1:
33958 # pragma omp atomic new-line
33959 update-stmt
33961 # pragma omp atomic read new-line
33962 read-stmt
33964 # pragma omp atomic write new-line
33965 write-stmt
33967 # pragma omp atomic update new-line
33968 update-stmt
33970 # pragma omp atomic capture new-line
33971 capture-stmt
33973 # pragma omp atomic capture new-line
33974 capture-block
33976 read-stmt:
33977 v = x
33978 write-stmt:
33979 x = expr
33980 update-stmt:
33981 expression-stmt | x = x binop expr
33982 capture-stmt:
33983 v = expression-stmt
33984 capture-block:
33985 { v = x; update-stmt; } | { update-stmt; v = x; }
33987 OpenMP 4.0:
33988 update-stmt:
33989 expression-stmt | x = x binop expr | x = expr binop x
33990 capture-stmt:
33991 v = update-stmt
33992 capture-block:
33993 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33995 where x and v are lvalue expressions with scalar type. */
33997 static void
33998 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34000 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34001 tree rhs1 = NULL_TREE, orig_lhs;
34002 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34003 bool structured_block = false;
34004 bool seq_cst = false;
34006 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34008 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34009 const char *p = IDENTIFIER_POINTER (id);
34011 if (!strcmp (p, "seq_cst"))
34013 seq_cst = true;
34014 cp_lexer_consume_token (parser->lexer);
34015 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34016 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34017 cp_lexer_consume_token (parser->lexer);
34020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34022 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34023 const char *p = IDENTIFIER_POINTER (id);
34025 if (!strcmp (p, "read"))
34026 code = OMP_ATOMIC_READ;
34027 else if (!strcmp (p, "write"))
34028 code = NOP_EXPR;
34029 else if (!strcmp (p, "update"))
34030 code = OMP_ATOMIC;
34031 else if (!strcmp (p, "capture"))
34032 code = OMP_ATOMIC_CAPTURE_NEW;
34033 else
34034 p = NULL;
34035 if (p)
34036 cp_lexer_consume_token (parser->lexer);
34038 if (!seq_cst)
34040 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34041 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34042 cp_lexer_consume_token (parser->lexer);
34044 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34046 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34047 const char *p = IDENTIFIER_POINTER (id);
34049 if (!strcmp (p, "seq_cst"))
34051 seq_cst = true;
34052 cp_lexer_consume_token (parser->lexer);
34056 cp_parser_require_pragma_eol (parser, pragma_tok);
34058 switch (code)
34060 case OMP_ATOMIC_READ:
34061 case NOP_EXPR: /* atomic write */
34062 v = cp_parser_unary_expression (parser);
34063 if (v == error_mark_node)
34064 goto saw_error;
34065 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34066 goto saw_error;
34067 if (code == NOP_EXPR)
34068 lhs = cp_parser_expression (parser);
34069 else
34070 lhs = cp_parser_unary_expression (parser);
34071 if (lhs == error_mark_node)
34072 goto saw_error;
34073 if (code == NOP_EXPR)
34075 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34076 opcode. */
34077 code = OMP_ATOMIC;
34078 rhs = lhs;
34079 lhs = v;
34080 v = NULL_TREE;
34082 goto done;
34083 case OMP_ATOMIC_CAPTURE_NEW:
34084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34086 cp_lexer_consume_token (parser->lexer);
34087 structured_block = true;
34089 else
34091 v = cp_parser_unary_expression (parser);
34092 if (v == error_mark_node)
34093 goto saw_error;
34094 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34095 goto saw_error;
34097 default:
34098 break;
34101 restart:
34102 lhs = cp_parser_unary_expression (parser);
34103 orig_lhs = lhs;
34104 switch (TREE_CODE (lhs))
34106 case ERROR_MARK:
34107 goto saw_error;
34109 case POSTINCREMENT_EXPR:
34110 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34111 code = OMP_ATOMIC_CAPTURE_OLD;
34112 /* FALLTHROUGH */
34113 case PREINCREMENT_EXPR:
34114 lhs = TREE_OPERAND (lhs, 0);
34115 opcode = PLUS_EXPR;
34116 rhs = integer_one_node;
34117 break;
34119 case POSTDECREMENT_EXPR:
34120 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34121 code = OMP_ATOMIC_CAPTURE_OLD;
34122 /* FALLTHROUGH */
34123 case PREDECREMENT_EXPR:
34124 lhs = TREE_OPERAND (lhs, 0);
34125 opcode = MINUS_EXPR;
34126 rhs = integer_one_node;
34127 break;
34129 case COMPOUND_EXPR:
34130 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34131 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34132 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34133 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34134 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34135 (TREE_OPERAND (lhs, 1), 0), 0)))
34136 == BOOLEAN_TYPE)
34137 /* Undo effects of boolean_increment for post {in,de}crement. */
34138 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34139 /* FALLTHRU */
34140 case MODIFY_EXPR:
34141 if (TREE_CODE (lhs) == MODIFY_EXPR
34142 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34144 /* Undo effects of boolean_increment. */
34145 if (integer_onep (TREE_OPERAND (lhs, 1)))
34147 /* This is pre or post increment. */
34148 rhs = TREE_OPERAND (lhs, 1);
34149 lhs = TREE_OPERAND (lhs, 0);
34150 opcode = NOP_EXPR;
34151 if (code == OMP_ATOMIC_CAPTURE_NEW
34152 && !structured_block
34153 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34154 code = OMP_ATOMIC_CAPTURE_OLD;
34155 break;
34158 /* FALLTHRU */
34159 default:
34160 switch (cp_lexer_peek_token (parser->lexer)->type)
34162 case CPP_MULT_EQ:
34163 opcode = MULT_EXPR;
34164 break;
34165 case CPP_DIV_EQ:
34166 opcode = TRUNC_DIV_EXPR;
34167 break;
34168 case CPP_PLUS_EQ:
34169 opcode = PLUS_EXPR;
34170 break;
34171 case CPP_MINUS_EQ:
34172 opcode = MINUS_EXPR;
34173 break;
34174 case CPP_LSHIFT_EQ:
34175 opcode = LSHIFT_EXPR;
34176 break;
34177 case CPP_RSHIFT_EQ:
34178 opcode = RSHIFT_EXPR;
34179 break;
34180 case CPP_AND_EQ:
34181 opcode = BIT_AND_EXPR;
34182 break;
34183 case CPP_OR_EQ:
34184 opcode = BIT_IOR_EXPR;
34185 break;
34186 case CPP_XOR_EQ:
34187 opcode = BIT_XOR_EXPR;
34188 break;
34189 case CPP_EQ:
34190 enum cp_parser_prec oprec;
34191 cp_token *token;
34192 cp_lexer_consume_token (parser->lexer);
34193 cp_parser_parse_tentatively (parser);
34194 rhs1 = cp_parser_simple_cast_expression (parser);
34195 if (rhs1 == error_mark_node)
34197 cp_parser_abort_tentative_parse (parser);
34198 cp_parser_simple_cast_expression (parser);
34199 goto saw_error;
34201 token = cp_lexer_peek_token (parser->lexer);
34202 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34204 cp_parser_abort_tentative_parse (parser);
34205 cp_parser_parse_tentatively (parser);
34206 rhs = cp_parser_binary_expression (parser, false, true,
34207 PREC_NOT_OPERATOR, NULL);
34208 if (rhs == error_mark_node)
34210 cp_parser_abort_tentative_parse (parser);
34211 cp_parser_binary_expression (parser, false, true,
34212 PREC_NOT_OPERATOR, NULL);
34213 goto saw_error;
34215 switch (TREE_CODE (rhs))
34217 case MULT_EXPR:
34218 case TRUNC_DIV_EXPR:
34219 case RDIV_EXPR:
34220 case PLUS_EXPR:
34221 case MINUS_EXPR:
34222 case LSHIFT_EXPR:
34223 case RSHIFT_EXPR:
34224 case BIT_AND_EXPR:
34225 case BIT_IOR_EXPR:
34226 case BIT_XOR_EXPR:
34227 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34229 if (cp_parser_parse_definitely (parser))
34231 opcode = TREE_CODE (rhs);
34232 rhs1 = TREE_OPERAND (rhs, 0);
34233 rhs = TREE_OPERAND (rhs, 1);
34234 goto stmt_done;
34236 else
34237 goto saw_error;
34239 break;
34240 default:
34241 break;
34243 cp_parser_abort_tentative_parse (parser);
34244 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34246 rhs = cp_parser_expression (parser);
34247 if (rhs == error_mark_node)
34248 goto saw_error;
34249 opcode = NOP_EXPR;
34250 rhs1 = NULL_TREE;
34251 goto stmt_done;
34253 cp_parser_error (parser,
34254 "invalid form of %<#pragma omp atomic%>");
34255 goto saw_error;
34257 if (!cp_parser_parse_definitely (parser))
34258 goto saw_error;
34259 switch (token->type)
34261 case CPP_SEMICOLON:
34262 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34264 code = OMP_ATOMIC_CAPTURE_OLD;
34265 v = lhs;
34266 lhs = NULL_TREE;
34267 lhs1 = rhs1;
34268 rhs1 = NULL_TREE;
34269 cp_lexer_consume_token (parser->lexer);
34270 goto restart;
34272 else if (structured_block)
34274 opcode = NOP_EXPR;
34275 rhs = rhs1;
34276 rhs1 = NULL_TREE;
34277 goto stmt_done;
34279 cp_parser_error (parser,
34280 "invalid form of %<#pragma omp atomic%>");
34281 goto saw_error;
34282 case CPP_MULT:
34283 opcode = MULT_EXPR;
34284 break;
34285 case CPP_DIV:
34286 opcode = TRUNC_DIV_EXPR;
34287 break;
34288 case CPP_PLUS:
34289 opcode = PLUS_EXPR;
34290 break;
34291 case CPP_MINUS:
34292 opcode = MINUS_EXPR;
34293 break;
34294 case CPP_LSHIFT:
34295 opcode = LSHIFT_EXPR;
34296 break;
34297 case CPP_RSHIFT:
34298 opcode = RSHIFT_EXPR;
34299 break;
34300 case CPP_AND:
34301 opcode = BIT_AND_EXPR;
34302 break;
34303 case CPP_OR:
34304 opcode = BIT_IOR_EXPR;
34305 break;
34306 case CPP_XOR:
34307 opcode = BIT_XOR_EXPR;
34308 break;
34309 default:
34310 cp_parser_error (parser,
34311 "invalid operator for %<#pragma omp atomic%>");
34312 goto saw_error;
34314 oprec = TOKEN_PRECEDENCE (token);
34315 gcc_assert (oprec != PREC_NOT_OPERATOR);
34316 if (commutative_tree_code (opcode))
34317 oprec = (enum cp_parser_prec) (oprec - 1);
34318 cp_lexer_consume_token (parser->lexer);
34319 rhs = cp_parser_binary_expression (parser, false, false,
34320 oprec, NULL);
34321 if (rhs == error_mark_node)
34322 goto saw_error;
34323 goto stmt_done;
34324 /* FALLTHROUGH */
34325 default:
34326 cp_parser_error (parser,
34327 "invalid operator for %<#pragma omp atomic%>");
34328 goto saw_error;
34330 cp_lexer_consume_token (parser->lexer);
34332 rhs = cp_parser_expression (parser);
34333 if (rhs == error_mark_node)
34334 goto saw_error;
34335 break;
34337 stmt_done:
34338 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34340 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34341 goto saw_error;
34342 v = cp_parser_unary_expression (parser);
34343 if (v == error_mark_node)
34344 goto saw_error;
34345 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34346 goto saw_error;
34347 lhs1 = cp_parser_unary_expression (parser);
34348 if (lhs1 == error_mark_node)
34349 goto saw_error;
34351 if (structured_block)
34353 cp_parser_consume_semicolon_at_end_of_statement (parser);
34354 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34356 done:
34357 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34358 if (!structured_block)
34359 cp_parser_consume_semicolon_at_end_of_statement (parser);
34360 return;
34362 saw_error:
34363 cp_parser_skip_to_end_of_block_or_statement (parser);
34364 if (structured_block)
34366 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34367 cp_lexer_consume_token (parser->lexer);
34368 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34370 cp_parser_skip_to_end_of_block_or_statement (parser);
34371 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34372 cp_lexer_consume_token (parser->lexer);
34378 /* OpenMP 2.5:
34379 # pragma omp barrier new-line */
34381 static void
34382 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34384 cp_parser_require_pragma_eol (parser, pragma_tok);
34385 finish_omp_barrier ();
34388 /* OpenMP 2.5:
34389 # pragma omp critical [(name)] new-line
34390 structured-block
34392 OpenMP 4.5:
34393 # pragma omp critical [(name) [hint(expression)]] new-line
34394 structured-block */
34396 #define OMP_CRITICAL_CLAUSE_MASK \
34397 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34399 static tree
34400 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34402 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34406 matching_parens parens;
34407 parens.consume_open (parser);
34409 name = cp_parser_identifier (parser);
34411 if (name == error_mark_node
34412 || !parens.require_close (parser))
34413 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34414 /*or_comma=*/false,
34415 /*consume_paren=*/true);
34416 if (name == error_mark_node)
34417 name = NULL;
34419 clauses = cp_parser_omp_all_clauses (parser,
34420 OMP_CRITICAL_CLAUSE_MASK,
34421 "#pragma omp critical", pragma_tok);
34423 else
34424 cp_parser_require_pragma_eol (parser, pragma_tok);
34426 stmt = cp_parser_omp_structured_block (parser, if_p);
34427 return c_finish_omp_critical (input_location, stmt, name, clauses);
34430 /* OpenMP 2.5:
34431 # pragma omp flush flush-vars[opt] new-line
34433 flush-vars:
34434 ( variable-list ) */
34436 static void
34437 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34440 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34441 cp_parser_require_pragma_eol (parser, pragma_tok);
34443 finish_omp_flush ();
34446 /* Helper function, to parse omp for increment expression. */
34448 static tree
34449 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34451 tree cond = cp_parser_binary_expression (parser, false, true,
34452 PREC_NOT_OPERATOR, NULL);
34453 if (cond == error_mark_node
34454 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34456 cp_parser_skip_to_end_of_statement (parser);
34457 return error_mark_node;
34460 switch (TREE_CODE (cond))
34462 case GT_EXPR:
34463 case GE_EXPR:
34464 case LT_EXPR:
34465 case LE_EXPR:
34466 break;
34467 case NE_EXPR:
34468 /* Fall through: OpenMP disallows NE_EXPR. */
34469 gcc_fallthrough ();
34470 default:
34471 return error_mark_node;
34474 /* If decl is an iterator, preserve LHS and RHS of the relational
34475 expr until finish_omp_for. */
34476 if (decl
34477 && (type_dependent_expression_p (decl)
34478 || CLASS_TYPE_P (TREE_TYPE (decl))))
34479 return cond;
34481 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34482 TREE_CODE (cond),
34483 TREE_OPERAND (cond, 0), ERROR_MARK,
34484 TREE_OPERAND (cond, 1), ERROR_MARK,
34485 /*overload=*/NULL, tf_warning_or_error);
34488 /* Helper function, to parse omp for increment expression. */
34490 static tree
34491 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34493 cp_token *token = cp_lexer_peek_token (parser->lexer);
34494 enum tree_code op;
34495 tree lhs, rhs;
34496 cp_id_kind idk;
34497 bool decl_first;
34499 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34501 op = (token->type == CPP_PLUS_PLUS
34502 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34503 cp_lexer_consume_token (parser->lexer);
34504 lhs = cp_parser_simple_cast_expression (parser);
34505 if (lhs != decl
34506 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34507 return error_mark_node;
34508 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34511 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34512 if (lhs != decl
34513 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34514 return error_mark_node;
34516 token = cp_lexer_peek_token (parser->lexer);
34517 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34519 op = (token->type == CPP_PLUS_PLUS
34520 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34521 cp_lexer_consume_token (parser->lexer);
34522 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34525 op = cp_parser_assignment_operator_opt (parser);
34526 if (op == ERROR_MARK)
34527 return error_mark_node;
34529 if (op != NOP_EXPR)
34531 rhs = cp_parser_assignment_expression (parser);
34532 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34533 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34536 lhs = cp_parser_binary_expression (parser, false, false,
34537 PREC_ADDITIVE_EXPRESSION, NULL);
34538 token = cp_lexer_peek_token (parser->lexer);
34539 decl_first = (lhs == decl
34540 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34541 if (decl_first)
34542 lhs = NULL_TREE;
34543 if (token->type != CPP_PLUS
34544 && token->type != CPP_MINUS)
34545 return error_mark_node;
34549 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34550 cp_lexer_consume_token (parser->lexer);
34551 rhs = cp_parser_binary_expression (parser, false, false,
34552 PREC_ADDITIVE_EXPRESSION, NULL);
34553 token = cp_lexer_peek_token (parser->lexer);
34554 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34556 if (lhs == NULL_TREE)
34558 if (op == PLUS_EXPR)
34559 lhs = rhs;
34560 else
34561 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34562 tf_warning_or_error);
34564 else
34565 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34566 ERROR_MARK, NULL, tf_warning_or_error);
34569 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34571 if (!decl_first)
34573 if ((rhs != decl
34574 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34575 || op == MINUS_EXPR)
34576 return error_mark_node;
34577 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34579 else
34580 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34582 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34585 /* Parse the initialization statement of an OpenMP for loop.
34587 Return true if the resulting construct should have an
34588 OMP_CLAUSE_PRIVATE added to it. */
34590 static tree
34591 cp_parser_omp_for_loop_init (cp_parser *parser,
34592 tree &this_pre_body,
34593 vec<tree, va_gc> *for_block,
34594 tree &init,
34595 tree &orig_init,
34596 tree &decl,
34597 tree &real_decl)
34599 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34600 return NULL_TREE;
34602 tree add_private_clause = NULL_TREE;
34604 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34606 init-expr:
34607 var = lb
34608 integer-type var = lb
34609 random-access-iterator-type var = lb
34610 pointer-type var = lb
34612 cp_decl_specifier_seq type_specifiers;
34614 /* First, try to parse as an initialized declaration. See
34615 cp_parser_condition, from whence the bulk of this is copied. */
34617 cp_parser_parse_tentatively (parser);
34618 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34619 /*is_trailing_return=*/false,
34620 &type_specifiers);
34621 if (cp_parser_parse_definitely (parser))
34623 /* If parsing a type specifier seq succeeded, then this
34624 MUST be a initialized declaration. */
34625 tree asm_specification, attributes;
34626 cp_declarator *declarator;
34628 declarator = cp_parser_declarator (parser,
34629 CP_PARSER_DECLARATOR_NAMED,
34630 /*ctor_dtor_or_conv_p=*/NULL,
34631 /*parenthesized_p=*/NULL,
34632 /*member_p=*/false,
34633 /*friend_p=*/false);
34634 attributes = cp_parser_attributes_opt (parser);
34635 asm_specification = cp_parser_asm_specification_opt (parser);
34637 if (declarator == cp_error_declarator)
34638 cp_parser_skip_to_end_of_statement (parser);
34640 else
34642 tree pushed_scope, auto_node;
34644 decl = start_decl (declarator, &type_specifiers,
34645 SD_INITIALIZED, attributes,
34646 /*prefix_attributes=*/NULL_TREE,
34647 &pushed_scope);
34649 auto_node = type_uses_auto (TREE_TYPE (decl));
34650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34652 if (cp_lexer_next_token_is (parser->lexer,
34653 CPP_OPEN_PAREN))
34654 error ("parenthesized initialization is not allowed in "
34655 "OpenMP %<for%> loop");
34656 else
34657 /* Trigger an error. */
34658 cp_parser_require (parser, CPP_EQ, RT_EQ);
34660 init = error_mark_node;
34661 cp_parser_skip_to_end_of_statement (parser);
34663 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34664 || type_dependent_expression_p (decl)
34665 || auto_node)
34667 bool is_direct_init, is_non_constant_init;
34669 init = cp_parser_initializer (parser,
34670 &is_direct_init,
34671 &is_non_constant_init);
34673 if (auto_node)
34675 TREE_TYPE (decl)
34676 = do_auto_deduction (TREE_TYPE (decl), init,
34677 auto_node);
34679 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34680 && !type_dependent_expression_p (decl))
34681 goto non_class;
34684 cp_finish_decl (decl, init, !is_non_constant_init,
34685 asm_specification,
34686 LOOKUP_ONLYCONVERTING);
34687 orig_init = init;
34688 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34690 vec_safe_push (for_block, this_pre_body);
34691 init = NULL_TREE;
34693 else
34695 init = pop_stmt_list (this_pre_body);
34696 if (init && TREE_CODE (init) == STATEMENT_LIST)
34698 tree_stmt_iterator i = tsi_start (init);
34699 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34700 while (!tsi_end_p (i))
34702 tree t = tsi_stmt (i);
34703 if (TREE_CODE (t) == DECL_EXPR
34704 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34706 tsi_delink (&i);
34707 vec_safe_push (for_block, t);
34708 continue;
34710 break;
34712 if (tsi_one_before_end_p (i))
34714 tree t = tsi_stmt (i);
34715 tsi_delink (&i);
34716 free_stmt_list (init);
34717 init = t;
34721 this_pre_body = NULL_TREE;
34723 else
34725 /* Consume '='. */
34726 cp_lexer_consume_token (parser->lexer);
34727 init = cp_parser_assignment_expression (parser);
34729 non_class:
34730 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34731 init = error_mark_node;
34732 else
34733 cp_finish_decl (decl, NULL_TREE,
34734 /*init_const_expr_p=*/false,
34735 asm_specification,
34736 LOOKUP_ONLYCONVERTING);
34739 if (pushed_scope)
34740 pop_scope (pushed_scope);
34743 else
34745 cp_id_kind idk;
34746 /* If parsing a type specifier sequence failed, then
34747 this MUST be a simple expression. */
34748 cp_parser_parse_tentatively (parser);
34749 decl = cp_parser_primary_expression (parser, false, false,
34750 false, &idk);
34751 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34752 if (!cp_parser_error_occurred (parser)
34753 && decl
34754 && (TREE_CODE (decl) == COMPONENT_REF
34755 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34757 cp_parser_abort_tentative_parse (parser);
34758 cp_parser_parse_tentatively (parser);
34759 cp_token *token = cp_lexer_peek_token (parser->lexer);
34760 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34761 /*check_dependency_p=*/true,
34762 /*template_p=*/NULL,
34763 /*declarator_p=*/false,
34764 /*optional_p=*/false);
34765 if (name != error_mark_node
34766 && last_tok == cp_lexer_peek_token (parser->lexer))
34768 decl = cp_parser_lookup_name_simple (parser, name,
34769 token->location);
34770 if (TREE_CODE (decl) == FIELD_DECL)
34771 add_private_clause = omp_privatize_field (decl, false);
34773 cp_parser_abort_tentative_parse (parser);
34774 cp_parser_parse_tentatively (parser);
34775 decl = cp_parser_primary_expression (parser, false, false,
34776 false, &idk);
34778 if (!cp_parser_error_occurred (parser)
34779 && decl
34780 && DECL_P (decl)
34781 && CLASS_TYPE_P (TREE_TYPE (decl)))
34783 tree rhs;
34785 cp_parser_parse_definitely (parser);
34786 cp_parser_require (parser, CPP_EQ, RT_EQ);
34787 rhs = cp_parser_assignment_expression (parser);
34788 orig_init = rhs;
34789 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34790 decl, NOP_EXPR,
34791 rhs,
34792 tf_warning_or_error));
34793 if (!add_private_clause)
34794 add_private_clause = decl;
34796 else
34798 decl = NULL;
34799 cp_parser_abort_tentative_parse (parser);
34800 init = cp_parser_expression (parser);
34801 if (init)
34803 if (TREE_CODE (init) == MODIFY_EXPR
34804 || TREE_CODE (init) == MODOP_EXPR)
34805 real_decl = TREE_OPERAND (init, 0);
34809 return add_private_clause;
34812 /* Parse the restricted form of the for statement allowed by OpenMP. */
34814 static tree
34815 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34816 tree *cclauses, bool *if_p)
34818 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34819 tree real_decl, initv, condv, incrv, declv;
34820 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34821 location_t loc_first;
34822 bool collapse_err = false;
34823 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34824 vec<tree, va_gc> *for_block = make_tree_vector ();
34825 auto_vec<tree, 4> orig_inits;
34826 bool tiling = false;
34828 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34829 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34830 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34831 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34833 tiling = true;
34834 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34836 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34837 && OMP_CLAUSE_ORDERED_EXPR (cl))
34839 ordered_cl = cl;
34840 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34843 if (ordered && ordered < collapse)
34845 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34846 "%<ordered%> clause parameter is less than %<collapse%>");
34847 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34848 = build_int_cst (NULL_TREE, collapse);
34849 ordered = collapse;
34851 if (ordered)
34853 for (tree *pc = &clauses; *pc; )
34854 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34856 error_at (OMP_CLAUSE_LOCATION (*pc),
34857 "%<linear%> clause may not be specified together "
34858 "with %<ordered%> clause with a parameter");
34859 *pc = OMP_CLAUSE_CHAIN (*pc);
34861 else
34862 pc = &OMP_CLAUSE_CHAIN (*pc);
34865 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34866 count = ordered ? ordered : collapse;
34868 declv = make_tree_vec (count);
34869 initv = make_tree_vec (count);
34870 condv = make_tree_vec (count);
34871 incrv = make_tree_vec (count);
34873 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34875 for (i = 0; i < count; i++)
34877 int bracecount = 0;
34878 tree add_private_clause = NULL_TREE;
34879 location_t loc;
34881 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34883 if (!collapse_err)
34884 cp_parser_error (parser, "for statement expected");
34885 return NULL;
34887 loc = cp_lexer_consume_token (parser->lexer)->location;
34889 matching_parens parens;
34890 if (!parens.require_open (parser))
34891 return NULL;
34893 init = orig_init = decl = real_decl = NULL;
34894 this_pre_body = push_stmt_list ();
34896 add_private_clause
34897 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
34898 init, orig_init, decl, real_decl);
34900 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34901 if (this_pre_body)
34903 this_pre_body = pop_stmt_list (this_pre_body);
34904 if (pre_body)
34906 tree t = pre_body;
34907 pre_body = push_stmt_list ();
34908 add_stmt (t);
34909 add_stmt (this_pre_body);
34910 pre_body = pop_stmt_list (pre_body);
34912 else
34913 pre_body = this_pre_body;
34916 if (decl)
34917 real_decl = decl;
34918 if (cclauses != NULL
34919 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34920 && real_decl != NULL_TREE)
34922 tree *c;
34923 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34924 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34925 && OMP_CLAUSE_DECL (*c) == real_decl)
34927 error_at (loc, "iteration variable %qD"
34928 " should not be firstprivate", real_decl);
34929 *c = OMP_CLAUSE_CHAIN (*c);
34931 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34932 && OMP_CLAUSE_DECL (*c) == real_decl)
34934 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34935 tree l = *c;
34936 *c = OMP_CLAUSE_CHAIN (*c);
34937 if (code == OMP_SIMD)
34939 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34940 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34942 else
34944 OMP_CLAUSE_CHAIN (l) = clauses;
34945 clauses = l;
34947 add_private_clause = NULL_TREE;
34949 else
34951 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34952 && OMP_CLAUSE_DECL (*c) == real_decl)
34953 add_private_clause = NULL_TREE;
34954 c = &OMP_CLAUSE_CHAIN (*c);
34958 if (add_private_clause)
34960 tree c;
34961 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34963 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34964 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34965 && OMP_CLAUSE_DECL (c) == decl)
34966 break;
34967 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34968 && OMP_CLAUSE_DECL (c) == decl)
34969 error_at (loc, "iteration variable %qD "
34970 "should not be firstprivate",
34971 decl);
34972 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34973 && OMP_CLAUSE_DECL (c) == decl)
34974 error_at (loc, "iteration variable %qD should not be reduction",
34975 decl);
34977 if (c == NULL)
34979 if (code != OMP_SIMD)
34980 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34981 else if (collapse == 1)
34982 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34983 else
34984 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34985 OMP_CLAUSE_DECL (c) = add_private_clause;
34986 c = finish_omp_clauses (c, C_ORT_OMP);
34987 if (c)
34989 OMP_CLAUSE_CHAIN (c) = clauses;
34990 clauses = c;
34991 /* For linear, signal that we need to fill up
34992 the so far unknown linear step. */
34993 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34994 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34999 cond = NULL;
35000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35001 cond = cp_parser_omp_for_cond (parser, decl);
35002 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35004 incr = NULL;
35005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35007 /* If decl is an iterator, preserve the operator on decl
35008 until finish_omp_for. */
35009 if (real_decl
35010 && ((processing_template_decl
35011 && (TREE_TYPE (real_decl) == NULL_TREE
35012 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35013 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35014 incr = cp_parser_omp_for_incr (parser, real_decl);
35015 else
35016 incr = cp_parser_expression (parser);
35017 if (!EXPR_HAS_LOCATION (incr))
35018 protected_set_expr_location (incr, input_location);
35021 if (!parens.require_close (parser))
35022 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35023 /*or_comma=*/false,
35024 /*consume_paren=*/true);
35026 TREE_VEC_ELT (declv, i) = decl;
35027 TREE_VEC_ELT (initv, i) = init;
35028 TREE_VEC_ELT (condv, i) = cond;
35029 TREE_VEC_ELT (incrv, i) = incr;
35030 if (orig_init)
35032 orig_inits.safe_grow_cleared (i + 1);
35033 orig_inits[i] = orig_init;
35036 if (i == count - 1)
35037 break;
35039 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35040 in between the collapsed for loops to be still considered perfectly
35041 nested. Hopefully the final version clarifies this.
35042 For now handle (multiple) {'s and empty statements. */
35043 cp_parser_parse_tentatively (parser);
35044 for (;;)
35046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35047 break;
35048 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35050 cp_lexer_consume_token (parser->lexer);
35051 bracecount++;
35053 else if (bracecount
35054 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35055 cp_lexer_consume_token (parser->lexer);
35056 else
35058 loc = cp_lexer_peek_token (parser->lexer)->location;
35059 error_at (loc, "not enough for loops to collapse");
35060 collapse_err = true;
35061 cp_parser_abort_tentative_parse (parser);
35062 declv = NULL_TREE;
35063 break;
35067 if (declv)
35069 cp_parser_parse_definitely (parser);
35070 nbraces += bracecount;
35074 if (nbraces)
35075 if_p = NULL;
35077 /* Note that we saved the original contents of this flag when we entered
35078 the structured block, and so we don't need to re-save it here. */
35079 parser->in_statement = IN_OMP_FOR;
35081 /* Note that the grammar doesn't call for a structured block here,
35082 though the loop as a whole is a structured block. */
35083 body = push_stmt_list ();
35084 cp_parser_statement (parser, NULL_TREE, false, if_p);
35085 body = pop_stmt_list (body);
35087 if (declv == NULL_TREE)
35088 ret = NULL_TREE;
35089 else
35090 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35091 body, pre_body, &orig_inits, clauses);
35093 while (nbraces)
35095 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35097 cp_lexer_consume_token (parser->lexer);
35098 nbraces--;
35100 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35101 cp_lexer_consume_token (parser->lexer);
35102 else
35104 if (!collapse_err)
35106 error_at (cp_lexer_peek_token (parser->lexer)->location,
35107 "collapsed loops not perfectly nested");
35109 collapse_err = true;
35110 cp_parser_statement_seq_opt (parser, NULL);
35111 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35112 break;
35116 while (!for_block->is_empty ())
35118 tree t = for_block->pop ();
35119 if (TREE_CODE (t) == STATEMENT_LIST)
35120 add_stmt (pop_stmt_list (t));
35121 else
35122 add_stmt (t);
35124 release_tree_vector (for_block);
35126 return ret;
35129 /* Helper function for OpenMP parsing, split clauses and call
35130 finish_omp_clauses on each of the set of clauses afterwards. */
35132 static void
35133 cp_omp_split_clauses (location_t loc, enum tree_code code,
35134 omp_clause_mask mask, tree clauses, tree *cclauses)
35136 int i;
35137 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35138 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35139 if (cclauses[i])
35140 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35143 /* OpenMP 4.0:
35144 #pragma omp simd simd-clause[optseq] new-line
35145 for-loop */
35147 #define OMP_SIMD_CLAUSE_MASK \
35148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35157 static tree
35158 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35159 char *p_name, omp_clause_mask mask, tree *cclauses,
35160 bool *if_p)
35162 tree clauses, sb, ret;
35163 unsigned int save;
35164 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35166 strcat (p_name, " simd");
35167 mask |= OMP_SIMD_CLAUSE_MASK;
35169 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35170 cclauses == NULL);
35171 if (cclauses)
35173 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35174 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35175 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35176 OMP_CLAUSE_ORDERED);
35177 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35179 error_at (OMP_CLAUSE_LOCATION (c),
35180 "%<ordered%> clause with parameter may not be specified "
35181 "on %qs construct", p_name);
35182 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35186 sb = begin_omp_structured_block ();
35187 save = cp_parser_begin_omp_structured_block (parser);
35189 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35191 cp_parser_end_omp_structured_block (parser, save);
35192 add_stmt (finish_omp_structured_block (sb));
35194 return ret;
35197 /* OpenMP 2.5:
35198 #pragma omp for for-clause[optseq] new-line
35199 for-loop
35201 OpenMP 4.0:
35202 #pragma omp for simd for-simd-clause[optseq] new-line
35203 for-loop */
35205 #define OMP_FOR_CLAUSE_MASK \
35206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35216 static tree
35217 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35218 char *p_name, omp_clause_mask mask, tree *cclauses,
35219 bool *if_p)
35221 tree clauses, sb, ret;
35222 unsigned int save;
35223 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35225 strcat (p_name, " for");
35226 mask |= OMP_FOR_CLAUSE_MASK;
35227 /* parallel for{, simd} disallows nowait clause, but for
35228 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35229 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35230 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35231 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35232 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35233 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35237 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35238 const char *p = IDENTIFIER_POINTER (id);
35240 if (strcmp (p, "simd") == 0)
35242 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35243 if (cclauses == NULL)
35244 cclauses = cclauses_buf;
35246 cp_lexer_consume_token (parser->lexer);
35247 if (!flag_openmp) /* flag_openmp_simd */
35248 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35249 cclauses, if_p);
35250 sb = begin_omp_structured_block ();
35251 save = cp_parser_begin_omp_structured_block (parser);
35252 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35253 cclauses, if_p);
35254 cp_parser_end_omp_structured_block (parser, save);
35255 tree body = finish_omp_structured_block (sb);
35256 if (ret == NULL)
35257 return ret;
35258 ret = make_node (OMP_FOR);
35259 TREE_TYPE (ret) = void_type_node;
35260 OMP_FOR_BODY (ret) = body;
35261 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35262 SET_EXPR_LOCATION (ret, loc);
35263 add_stmt (ret);
35264 return ret;
35267 if (!flag_openmp) /* flag_openmp_simd */
35269 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35270 return NULL_TREE;
35273 /* Composite distribute parallel for disallows linear clause. */
35274 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35275 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35277 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35278 cclauses == NULL);
35279 if (cclauses)
35281 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35282 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35285 sb = begin_omp_structured_block ();
35286 save = cp_parser_begin_omp_structured_block (parser);
35288 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35290 cp_parser_end_omp_structured_block (parser, save);
35291 add_stmt (finish_omp_structured_block (sb));
35293 return ret;
35296 /* OpenMP 2.5:
35297 # pragma omp master new-line
35298 structured-block */
35300 static tree
35301 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35303 cp_parser_require_pragma_eol (parser, pragma_tok);
35304 return c_finish_omp_master (input_location,
35305 cp_parser_omp_structured_block (parser, if_p));
35308 /* OpenMP 2.5:
35309 # pragma omp ordered new-line
35310 structured-block
35312 OpenMP 4.5:
35313 # pragma omp ordered ordered-clauses new-line
35314 structured-block */
35316 #define OMP_ORDERED_CLAUSE_MASK \
35317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35320 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35321 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35323 static bool
35324 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35325 enum pragma_context context, bool *if_p)
35327 location_t loc = pragma_tok->location;
35329 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35331 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35332 const char *p = IDENTIFIER_POINTER (id);
35334 if (strcmp (p, "depend") == 0)
35336 if (!flag_openmp) /* flag_openmp_simd */
35338 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35339 return false;
35341 if (context == pragma_stmt)
35343 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35344 "%<depend%> clause may only be used in compound "
35345 "statements");
35346 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35347 return false;
35349 tree clauses
35350 = cp_parser_omp_all_clauses (parser,
35351 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35352 "#pragma omp ordered", pragma_tok);
35353 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35354 return false;
35358 tree clauses
35359 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35360 "#pragma omp ordered", pragma_tok);
35362 if (!flag_openmp /* flag_openmp_simd */
35363 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35364 return false;
35366 c_finish_omp_ordered (loc, clauses,
35367 cp_parser_omp_structured_block (parser, if_p));
35368 return true;
35371 /* OpenMP 2.5:
35373 section-scope:
35374 { section-sequence }
35376 section-sequence:
35377 section-directive[opt] structured-block
35378 section-sequence section-directive structured-block */
35380 static tree
35381 cp_parser_omp_sections_scope (cp_parser *parser)
35383 tree stmt, substmt;
35384 bool error_suppress = false;
35385 cp_token *tok;
35387 matching_braces braces;
35388 if (!braces.require_open (parser))
35389 return NULL_TREE;
35391 stmt = push_stmt_list ();
35393 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35394 != PRAGMA_OMP_SECTION)
35396 substmt = cp_parser_omp_structured_block (parser, NULL);
35397 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35398 add_stmt (substmt);
35401 while (1)
35403 tok = cp_lexer_peek_token (parser->lexer);
35404 if (tok->type == CPP_CLOSE_BRACE)
35405 break;
35406 if (tok->type == CPP_EOF)
35407 break;
35409 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35411 cp_lexer_consume_token (parser->lexer);
35412 cp_parser_require_pragma_eol (parser, tok);
35413 error_suppress = false;
35415 else if (!error_suppress)
35417 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35418 error_suppress = true;
35421 substmt = cp_parser_omp_structured_block (parser, NULL);
35422 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35423 add_stmt (substmt);
35425 braces.require_close (parser);
35427 substmt = pop_stmt_list (stmt);
35429 stmt = make_node (OMP_SECTIONS);
35430 TREE_TYPE (stmt) = void_type_node;
35431 OMP_SECTIONS_BODY (stmt) = substmt;
35433 add_stmt (stmt);
35434 return stmt;
35437 /* OpenMP 2.5:
35438 # pragma omp sections sections-clause[optseq] newline
35439 sections-scope */
35441 #define OMP_SECTIONS_CLAUSE_MASK \
35442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35448 static tree
35449 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35450 char *p_name, omp_clause_mask mask, tree *cclauses)
35452 tree clauses, ret;
35453 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35455 strcat (p_name, " sections");
35456 mask |= OMP_SECTIONS_CLAUSE_MASK;
35457 if (cclauses)
35458 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35460 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35461 cclauses == NULL);
35462 if (cclauses)
35464 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35465 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35468 ret = cp_parser_omp_sections_scope (parser);
35469 if (ret)
35470 OMP_SECTIONS_CLAUSES (ret) = clauses;
35472 return ret;
35475 /* OpenMP 2.5:
35476 # pragma omp parallel parallel-clause[optseq] new-line
35477 structured-block
35478 # pragma omp parallel for parallel-for-clause[optseq] new-line
35479 structured-block
35480 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35481 structured-block
35483 OpenMP 4.0:
35484 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35485 structured-block */
35487 #define OMP_PARALLEL_CLAUSE_MASK \
35488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35498 static tree
35499 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35500 char *p_name, omp_clause_mask mask, tree *cclauses,
35501 bool *if_p)
35503 tree stmt, clauses, block;
35504 unsigned int save;
35505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35507 strcat (p_name, " parallel");
35508 mask |= OMP_PARALLEL_CLAUSE_MASK;
35509 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35510 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35511 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35512 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35516 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35517 if (cclauses == NULL)
35518 cclauses = cclauses_buf;
35520 cp_lexer_consume_token (parser->lexer);
35521 if (!flag_openmp) /* flag_openmp_simd */
35522 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35523 if_p);
35524 block = begin_omp_parallel ();
35525 save = cp_parser_begin_omp_structured_block (parser);
35526 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35527 if_p);
35528 cp_parser_end_omp_structured_block (parser, save);
35529 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35530 block);
35531 if (ret == NULL_TREE)
35532 return ret;
35533 OMP_PARALLEL_COMBINED (stmt) = 1;
35534 return stmt;
35536 /* When combined with distribute, parallel has to be followed by for.
35537 #pragma omp target parallel is allowed though. */
35538 else if (cclauses
35539 && (mask & (OMP_CLAUSE_MASK_1
35540 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35542 error_at (loc, "expected %<for%> after %qs", p_name);
35543 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35544 return NULL_TREE;
35546 else if (!flag_openmp) /* flag_openmp_simd */
35548 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35549 return NULL_TREE;
35551 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35554 const char *p = IDENTIFIER_POINTER (id);
35555 if (strcmp (p, "sections") == 0)
35557 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35558 cclauses = cclauses_buf;
35560 cp_lexer_consume_token (parser->lexer);
35561 block = begin_omp_parallel ();
35562 save = cp_parser_begin_omp_structured_block (parser);
35563 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35564 cp_parser_end_omp_structured_block (parser, save);
35565 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35566 block);
35567 OMP_PARALLEL_COMBINED (stmt) = 1;
35568 return stmt;
35572 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35573 cclauses == NULL);
35574 if (cclauses)
35576 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35577 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35580 block = begin_omp_parallel ();
35581 save = cp_parser_begin_omp_structured_block (parser);
35582 cp_parser_statement (parser, NULL_TREE, false, if_p);
35583 cp_parser_end_omp_structured_block (parser, save);
35584 stmt = finish_omp_parallel (clauses, block);
35585 return stmt;
35588 /* OpenMP 2.5:
35589 # pragma omp single single-clause[optseq] new-line
35590 structured-block */
35592 #define OMP_SINGLE_CLAUSE_MASK \
35593 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35598 static tree
35599 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35601 tree stmt = make_node (OMP_SINGLE);
35602 TREE_TYPE (stmt) = void_type_node;
35604 OMP_SINGLE_CLAUSES (stmt)
35605 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35606 "#pragma omp single", pragma_tok);
35607 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35609 return add_stmt (stmt);
35612 /* OpenMP 3.0:
35613 # pragma omp task task-clause[optseq] new-line
35614 structured-block */
35616 #define OMP_TASK_CLAUSE_MASK \
35617 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35628 static tree
35629 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35631 tree clauses, block;
35632 unsigned int save;
35634 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35635 "#pragma omp task", pragma_tok);
35636 block = begin_omp_task ();
35637 save = cp_parser_begin_omp_structured_block (parser);
35638 cp_parser_statement (parser, NULL_TREE, false, if_p);
35639 cp_parser_end_omp_structured_block (parser, save);
35640 return finish_omp_task (clauses, block);
35643 /* OpenMP 3.0:
35644 # pragma omp taskwait new-line */
35646 static void
35647 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35649 cp_parser_require_pragma_eol (parser, pragma_tok);
35650 finish_omp_taskwait ();
35653 /* OpenMP 3.1:
35654 # pragma omp taskyield new-line */
35656 static void
35657 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35659 cp_parser_require_pragma_eol (parser, pragma_tok);
35660 finish_omp_taskyield ();
35663 /* OpenMP 4.0:
35664 # pragma omp taskgroup new-line
35665 structured-block */
35667 static tree
35668 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35670 cp_parser_require_pragma_eol (parser, pragma_tok);
35671 return c_finish_omp_taskgroup (input_location,
35672 cp_parser_omp_structured_block (parser,
35673 if_p));
35677 /* OpenMP 2.5:
35678 # pragma omp threadprivate (variable-list) */
35680 static void
35681 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35683 tree vars;
35685 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35686 cp_parser_require_pragma_eol (parser, pragma_tok);
35688 finish_omp_threadprivate (vars);
35691 /* OpenMP 4.0:
35692 # pragma omp cancel cancel-clause[optseq] new-line */
35694 #define OMP_CANCEL_CLAUSE_MASK \
35695 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35701 static void
35702 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35704 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35705 "#pragma omp cancel", pragma_tok);
35706 finish_omp_cancel (clauses);
35709 /* OpenMP 4.0:
35710 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35712 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35713 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35718 static void
35719 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35720 enum pragma_context context)
35722 tree clauses;
35723 bool point_seen = false;
35725 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35727 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35728 const char *p = IDENTIFIER_POINTER (id);
35730 if (strcmp (p, "point") == 0)
35732 cp_lexer_consume_token (parser->lexer);
35733 point_seen = true;
35736 if (!point_seen)
35738 cp_parser_error (parser, "expected %<point%>");
35739 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35740 return;
35743 if (context != pragma_compound)
35745 if (context == pragma_stmt)
35746 error_at (pragma_tok->location,
35747 "%<#pragma %s%> may only be used in compound statements",
35748 "omp cancellation point");
35749 else
35750 cp_parser_error (parser, "expected declaration specifiers");
35751 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35752 return;
35755 clauses = cp_parser_omp_all_clauses (parser,
35756 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35757 "#pragma omp cancellation point",
35758 pragma_tok);
35759 finish_omp_cancellation_point (clauses);
35762 /* OpenMP 4.0:
35763 #pragma omp distribute distribute-clause[optseq] new-line
35764 for-loop */
35766 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35773 static tree
35774 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35775 char *p_name, omp_clause_mask mask, tree *cclauses,
35776 bool *if_p)
35778 tree clauses, sb, ret;
35779 unsigned int save;
35780 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35782 strcat (p_name, " distribute");
35783 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35788 const char *p = IDENTIFIER_POINTER (id);
35789 bool simd = false;
35790 bool parallel = false;
35792 if (strcmp (p, "simd") == 0)
35793 simd = true;
35794 else
35795 parallel = strcmp (p, "parallel") == 0;
35796 if (parallel || simd)
35798 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35799 if (cclauses == NULL)
35800 cclauses = cclauses_buf;
35801 cp_lexer_consume_token (parser->lexer);
35802 if (!flag_openmp) /* flag_openmp_simd */
35804 if (simd)
35805 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35806 cclauses, if_p);
35807 else
35808 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35809 cclauses, if_p);
35811 sb = begin_omp_structured_block ();
35812 save = cp_parser_begin_omp_structured_block (parser);
35813 if (simd)
35814 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35815 cclauses, if_p);
35816 else
35817 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35818 cclauses, if_p);
35819 cp_parser_end_omp_structured_block (parser, save);
35820 tree body = finish_omp_structured_block (sb);
35821 if (ret == NULL)
35822 return ret;
35823 ret = make_node (OMP_DISTRIBUTE);
35824 TREE_TYPE (ret) = void_type_node;
35825 OMP_FOR_BODY (ret) = body;
35826 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35827 SET_EXPR_LOCATION (ret, loc);
35828 add_stmt (ret);
35829 return ret;
35832 if (!flag_openmp) /* flag_openmp_simd */
35834 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35835 return NULL_TREE;
35838 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35839 cclauses == NULL);
35840 if (cclauses)
35842 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35843 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35846 sb = begin_omp_structured_block ();
35847 save = cp_parser_begin_omp_structured_block (parser);
35849 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35851 cp_parser_end_omp_structured_block (parser, save);
35852 add_stmt (finish_omp_structured_block (sb));
35854 return ret;
35857 /* OpenMP 4.0:
35858 # pragma omp teams teams-clause[optseq] new-line
35859 structured-block */
35861 #define OMP_TEAMS_CLAUSE_MASK \
35862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35870 static tree
35871 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35872 char *p_name, omp_clause_mask mask, tree *cclauses,
35873 bool *if_p)
35875 tree clauses, sb, ret;
35876 unsigned int save;
35877 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35879 strcat (p_name, " teams");
35880 mask |= OMP_TEAMS_CLAUSE_MASK;
35882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35884 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35885 const char *p = IDENTIFIER_POINTER (id);
35886 if (strcmp (p, "distribute") == 0)
35888 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35889 if (cclauses == NULL)
35890 cclauses = cclauses_buf;
35892 cp_lexer_consume_token (parser->lexer);
35893 if (!flag_openmp) /* flag_openmp_simd */
35894 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35895 cclauses, if_p);
35896 sb = begin_omp_structured_block ();
35897 save = cp_parser_begin_omp_structured_block (parser);
35898 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35899 cclauses, if_p);
35900 cp_parser_end_omp_structured_block (parser, save);
35901 tree body = finish_omp_structured_block (sb);
35902 if (ret == NULL)
35903 return ret;
35904 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35905 ret = make_node (OMP_TEAMS);
35906 TREE_TYPE (ret) = void_type_node;
35907 OMP_TEAMS_CLAUSES (ret) = clauses;
35908 OMP_TEAMS_BODY (ret) = body;
35909 OMP_TEAMS_COMBINED (ret) = 1;
35910 SET_EXPR_LOCATION (ret, loc);
35911 return add_stmt (ret);
35914 if (!flag_openmp) /* flag_openmp_simd */
35916 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35917 return NULL_TREE;
35920 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35921 cclauses == NULL);
35922 if (cclauses)
35924 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35925 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35928 tree stmt = make_node (OMP_TEAMS);
35929 TREE_TYPE (stmt) = void_type_node;
35930 OMP_TEAMS_CLAUSES (stmt) = clauses;
35931 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35932 SET_EXPR_LOCATION (stmt, loc);
35934 return add_stmt (stmt);
35937 /* OpenMP 4.0:
35938 # pragma omp target data target-data-clause[optseq] new-line
35939 structured-block */
35941 #define OMP_TARGET_DATA_CLAUSE_MASK \
35942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35947 static tree
35948 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35950 tree clauses
35951 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35952 "#pragma omp target data", pragma_tok);
35953 int map_seen = 0;
35954 for (tree *pc = &clauses; *pc;)
35956 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35957 switch (OMP_CLAUSE_MAP_KIND (*pc))
35959 case GOMP_MAP_TO:
35960 case GOMP_MAP_ALWAYS_TO:
35961 case GOMP_MAP_FROM:
35962 case GOMP_MAP_ALWAYS_FROM:
35963 case GOMP_MAP_TOFROM:
35964 case GOMP_MAP_ALWAYS_TOFROM:
35965 case GOMP_MAP_ALLOC:
35966 map_seen = 3;
35967 break;
35968 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35969 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35970 case GOMP_MAP_ALWAYS_POINTER:
35971 break;
35972 default:
35973 map_seen |= 1;
35974 error_at (OMP_CLAUSE_LOCATION (*pc),
35975 "%<#pragma omp target data%> with map-type other "
35976 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35977 "on %<map%> clause");
35978 *pc = OMP_CLAUSE_CHAIN (*pc);
35979 continue;
35981 pc = &OMP_CLAUSE_CHAIN (*pc);
35984 if (map_seen != 3)
35986 if (map_seen == 0)
35987 error_at (pragma_tok->location,
35988 "%<#pragma omp target data%> must contain at least "
35989 "one %<map%> clause");
35990 return NULL_TREE;
35993 tree stmt = make_node (OMP_TARGET_DATA);
35994 TREE_TYPE (stmt) = void_type_node;
35995 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35997 keep_next_level (true);
35998 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36000 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36001 return add_stmt (stmt);
36004 /* OpenMP 4.5:
36005 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36006 structured-block */
36008 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36015 static tree
36016 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36017 enum pragma_context context)
36019 bool data_seen = false;
36020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36022 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36023 const char *p = IDENTIFIER_POINTER (id);
36025 if (strcmp (p, "data") == 0)
36027 cp_lexer_consume_token (parser->lexer);
36028 data_seen = true;
36031 if (!data_seen)
36033 cp_parser_error (parser, "expected %<data%>");
36034 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36035 return NULL_TREE;
36038 if (context == pragma_stmt)
36040 error_at (pragma_tok->location,
36041 "%<#pragma %s%> may only be used in compound statements",
36042 "omp target enter data");
36043 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36044 return NULL_TREE;
36047 tree clauses
36048 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36049 "#pragma omp target enter data", pragma_tok);
36050 int map_seen = 0;
36051 for (tree *pc = &clauses; *pc;)
36053 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36054 switch (OMP_CLAUSE_MAP_KIND (*pc))
36056 case GOMP_MAP_TO:
36057 case GOMP_MAP_ALWAYS_TO:
36058 case GOMP_MAP_ALLOC:
36059 map_seen = 3;
36060 break;
36061 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36062 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36063 case GOMP_MAP_ALWAYS_POINTER:
36064 break;
36065 default:
36066 map_seen |= 1;
36067 error_at (OMP_CLAUSE_LOCATION (*pc),
36068 "%<#pragma omp target enter data%> with map-type other "
36069 "than %<to%> or %<alloc%> on %<map%> clause");
36070 *pc = OMP_CLAUSE_CHAIN (*pc);
36071 continue;
36073 pc = &OMP_CLAUSE_CHAIN (*pc);
36076 if (map_seen != 3)
36078 if (map_seen == 0)
36079 error_at (pragma_tok->location,
36080 "%<#pragma omp target enter data%> must contain at least "
36081 "one %<map%> clause");
36082 return NULL_TREE;
36085 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36086 TREE_TYPE (stmt) = void_type_node;
36087 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36088 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36089 return add_stmt (stmt);
36092 /* OpenMP 4.5:
36093 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36094 structured-block */
36096 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36103 static tree
36104 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36105 enum pragma_context context)
36107 bool data_seen = false;
36108 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36110 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36111 const char *p = IDENTIFIER_POINTER (id);
36113 if (strcmp (p, "data") == 0)
36115 cp_lexer_consume_token (parser->lexer);
36116 data_seen = true;
36119 if (!data_seen)
36121 cp_parser_error (parser, "expected %<data%>");
36122 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36123 return NULL_TREE;
36126 if (context == pragma_stmt)
36128 error_at (pragma_tok->location,
36129 "%<#pragma %s%> may only be used in compound statements",
36130 "omp target exit data");
36131 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36132 return NULL_TREE;
36135 tree clauses
36136 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36137 "#pragma omp target exit data", pragma_tok);
36138 int map_seen = 0;
36139 for (tree *pc = &clauses; *pc;)
36141 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36142 switch (OMP_CLAUSE_MAP_KIND (*pc))
36144 case GOMP_MAP_FROM:
36145 case GOMP_MAP_ALWAYS_FROM:
36146 case GOMP_MAP_RELEASE:
36147 case GOMP_MAP_DELETE:
36148 map_seen = 3;
36149 break;
36150 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36151 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36152 case GOMP_MAP_ALWAYS_POINTER:
36153 break;
36154 default:
36155 map_seen |= 1;
36156 error_at (OMP_CLAUSE_LOCATION (*pc),
36157 "%<#pragma omp target exit data%> with map-type other "
36158 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36159 " clause");
36160 *pc = OMP_CLAUSE_CHAIN (*pc);
36161 continue;
36163 pc = &OMP_CLAUSE_CHAIN (*pc);
36166 if (map_seen != 3)
36168 if (map_seen == 0)
36169 error_at (pragma_tok->location,
36170 "%<#pragma omp target exit data%> must contain at least "
36171 "one %<map%> clause");
36172 return NULL_TREE;
36175 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36176 TREE_TYPE (stmt) = void_type_node;
36177 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36178 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36179 return add_stmt (stmt);
36182 /* OpenMP 4.0:
36183 # pragma omp target update target-update-clause[optseq] new-line */
36185 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36186 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36193 static bool
36194 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36195 enum pragma_context context)
36197 if (context == pragma_stmt)
36199 error_at (pragma_tok->location,
36200 "%<#pragma %s%> may only be used in compound statements",
36201 "omp target update");
36202 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36203 return false;
36206 tree clauses
36207 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36208 "#pragma omp target update", pragma_tok);
36209 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36210 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36212 error_at (pragma_tok->location,
36213 "%<#pragma omp target update%> must contain at least one "
36214 "%<from%> or %<to%> clauses");
36215 return false;
36218 tree stmt = make_node (OMP_TARGET_UPDATE);
36219 TREE_TYPE (stmt) = void_type_node;
36220 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36221 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36222 add_stmt (stmt);
36223 return false;
36226 /* OpenMP 4.0:
36227 # pragma omp target target-clause[optseq] new-line
36228 structured-block */
36230 #define OMP_TARGET_CLAUSE_MASK \
36231 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36241 static bool
36242 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36243 enum pragma_context context, bool *if_p)
36245 tree *pc = NULL, stmt;
36247 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36249 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36250 const char *p = IDENTIFIER_POINTER (id);
36251 enum tree_code ccode = ERROR_MARK;
36253 if (strcmp (p, "teams") == 0)
36254 ccode = OMP_TEAMS;
36255 else if (strcmp (p, "parallel") == 0)
36256 ccode = OMP_PARALLEL;
36257 else if (strcmp (p, "simd") == 0)
36258 ccode = OMP_SIMD;
36259 if (ccode != ERROR_MARK)
36261 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36262 char p_name[sizeof ("#pragma omp target teams distribute "
36263 "parallel for simd")];
36265 cp_lexer_consume_token (parser->lexer);
36266 strcpy (p_name, "#pragma omp target");
36267 if (!flag_openmp) /* flag_openmp_simd */
36269 tree stmt;
36270 switch (ccode)
36272 case OMP_TEAMS:
36273 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36274 OMP_TARGET_CLAUSE_MASK,
36275 cclauses, if_p);
36276 break;
36277 case OMP_PARALLEL:
36278 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36279 OMP_TARGET_CLAUSE_MASK,
36280 cclauses, if_p);
36281 break;
36282 case OMP_SIMD:
36283 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36284 OMP_TARGET_CLAUSE_MASK,
36285 cclauses, if_p);
36286 break;
36287 default:
36288 gcc_unreachable ();
36290 return stmt != NULL_TREE;
36292 keep_next_level (true);
36293 tree sb = begin_omp_structured_block (), ret;
36294 unsigned save = cp_parser_begin_omp_structured_block (parser);
36295 switch (ccode)
36297 case OMP_TEAMS:
36298 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36299 OMP_TARGET_CLAUSE_MASK, cclauses,
36300 if_p);
36301 break;
36302 case OMP_PARALLEL:
36303 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36304 OMP_TARGET_CLAUSE_MASK, cclauses,
36305 if_p);
36306 break;
36307 case OMP_SIMD:
36308 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36309 OMP_TARGET_CLAUSE_MASK, cclauses,
36310 if_p);
36311 break;
36312 default:
36313 gcc_unreachable ();
36315 cp_parser_end_omp_structured_block (parser, save);
36316 tree body = finish_omp_structured_block (sb);
36317 if (ret == NULL_TREE)
36318 return false;
36319 if (ccode == OMP_TEAMS && !processing_template_decl)
36321 /* For combined target teams, ensure the num_teams and
36322 thread_limit clause expressions are evaluated on the host,
36323 before entering the target construct. */
36324 tree c;
36325 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36326 c; c = OMP_CLAUSE_CHAIN (c))
36327 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36328 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36329 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36331 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36332 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36333 if (expr == error_mark_node)
36334 continue;
36335 tree tmp = TARGET_EXPR_SLOT (expr);
36336 add_stmt (expr);
36337 OMP_CLAUSE_OPERAND (c, 0) = expr;
36338 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36339 OMP_CLAUSE_FIRSTPRIVATE);
36340 OMP_CLAUSE_DECL (tc) = tmp;
36341 OMP_CLAUSE_CHAIN (tc)
36342 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36343 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36346 tree stmt = make_node (OMP_TARGET);
36347 TREE_TYPE (stmt) = void_type_node;
36348 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36349 OMP_TARGET_BODY (stmt) = body;
36350 OMP_TARGET_COMBINED (stmt) = 1;
36351 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36352 add_stmt (stmt);
36353 pc = &OMP_TARGET_CLAUSES (stmt);
36354 goto check_clauses;
36356 else if (!flag_openmp) /* flag_openmp_simd */
36358 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36359 return false;
36361 else if (strcmp (p, "data") == 0)
36363 cp_lexer_consume_token (parser->lexer);
36364 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36365 return true;
36367 else if (strcmp (p, "enter") == 0)
36369 cp_lexer_consume_token (parser->lexer);
36370 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36371 return false;
36373 else if (strcmp (p, "exit") == 0)
36375 cp_lexer_consume_token (parser->lexer);
36376 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36377 return false;
36379 else if (strcmp (p, "update") == 0)
36381 cp_lexer_consume_token (parser->lexer);
36382 return cp_parser_omp_target_update (parser, pragma_tok, context);
36385 if (!flag_openmp) /* flag_openmp_simd */
36387 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36388 return false;
36391 stmt = make_node (OMP_TARGET);
36392 TREE_TYPE (stmt) = void_type_node;
36394 OMP_TARGET_CLAUSES (stmt)
36395 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36396 "#pragma omp target", pragma_tok);
36397 pc = &OMP_TARGET_CLAUSES (stmt);
36398 keep_next_level (true);
36399 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36401 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36402 add_stmt (stmt);
36404 check_clauses:
36405 while (*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_FROM:
36413 case GOMP_MAP_ALWAYS_FROM:
36414 case GOMP_MAP_TOFROM:
36415 case GOMP_MAP_ALWAYS_TOFROM:
36416 case GOMP_MAP_ALLOC:
36417 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36418 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36419 case GOMP_MAP_ALWAYS_POINTER:
36420 break;
36421 default:
36422 error_at (OMP_CLAUSE_LOCATION (*pc),
36423 "%<#pragma omp target%> with map-type other "
36424 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36425 "on %<map%> clause");
36426 *pc = OMP_CLAUSE_CHAIN (*pc);
36427 continue;
36429 pc = &OMP_CLAUSE_CHAIN (*pc);
36431 return true;
36434 /* OpenACC 2.0:
36435 # pragma acc cache (variable-list) new-line
36438 static tree
36439 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36441 tree stmt, clauses;
36443 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36444 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36446 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36448 stmt = make_node (OACC_CACHE);
36449 TREE_TYPE (stmt) = void_type_node;
36450 OACC_CACHE_CLAUSES (stmt) = clauses;
36451 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36452 add_stmt (stmt);
36454 return stmt;
36457 /* OpenACC 2.0:
36458 # pragma acc data oacc-data-clause[optseq] new-line
36459 structured-block */
36461 #define OACC_DATA_CLAUSE_MASK \
36462 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36474 static tree
36475 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36477 tree stmt, clauses, block;
36478 unsigned int save;
36480 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36481 "#pragma acc data", pragma_tok);
36483 block = begin_omp_parallel ();
36484 save = cp_parser_begin_omp_structured_block (parser);
36485 cp_parser_statement (parser, NULL_TREE, false, if_p);
36486 cp_parser_end_omp_structured_block (parser, save);
36487 stmt = finish_oacc_data (clauses, block);
36488 return stmt;
36491 /* OpenACC 2.0:
36492 # pragma acc host_data <clauses> new-line
36493 structured-block */
36495 #define OACC_HOST_DATA_CLAUSE_MASK \
36496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36498 static tree
36499 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36501 tree stmt, clauses, block;
36502 unsigned int save;
36504 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36505 "#pragma acc host_data", pragma_tok);
36507 block = begin_omp_parallel ();
36508 save = cp_parser_begin_omp_structured_block (parser);
36509 cp_parser_statement (parser, NULL_TREE, false, if_p);
36510 cp_parser_end_omp_structured_block (parser, save);
36511 stmt = finish_oacc_host_data (clauses, block);
36512 return stmt;
36515 /* OpenACC 2.0:
36516 # pragma acc declare oacc-data-clause[optseq] new-line
36519 #define OACC_DECLARE_CLAUSE_MASK \
36520 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36533 static tree
36534 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36536 tree clauses, stmt;
36537 bool error = false;
36539 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36540 "#pragma acc declare", pragma_tok, true);
36543 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36545 error_at (pragma_tok->location,
36546 "no valid clauses specified in %<#pragma acc declare%>");
36547 return NULL_TREE;
36550 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36552 location_t loc = OMP_CLAUSE_LOCATION (t);
36553 tree decl = OMP_CLAUSE_DECL (t);
36554 if (!DECL_P (decl))
36556 error_at (loc, "array section in %<#pragma acc declare%>");
36557 error = true;
36558 continue;
36560 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36561 switch (OMP_CLAUSE_MAP_KIND (t))
36563 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36564 case GOMP_MAP_FORCE_ALLOC:
36565 case GOMP_MAP_FORCE_TO:
36566 case GOMP_MAP_FORCE_DEVICEPTR:
36567 case GOMP_MAP_DEVICE_RESIDENT:
36568 break;
36570 case GOMP_MAP_LINK:
36571 if (!global_bindings_p ()
36572 && (TREE_STATIC (decl)
36573 || !DECL_EXTERNAL (decl)))
36575 error_at (loc,
36576 "%qD must be a global variable in "
36577 "%<#pragma acc declare link%>",
36578 decl);
36579 error = true;
36580 continue;
36582 break;
36584 default:
36585 if (global_bindings_p ())
36587 error_at (loc, "invalid OpenACC clause at file scope");
36588 error = true;
36589 continue;
36591 if (DECL_EXTERNAL (decl))
36593 error_at (loc,
36594 "invalid use of %<extern%> variable %qD "
36595 "in %<#pragma acc declare%>", decl);
36596 error = true;
36597 continue;
36599 else if (TREE_PUBLIC (decl))
36601 error_at (loc,
36602 "invalid use of %<global%> variable %qD "
36603 "in %<#pragma acc declare%>", decl);
36604 error = true;
36605 continue;
36607 break;
36610 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36611 || lookup_attribute ("omp declare target link",
36612 DECL_ATTRIBUTES (decl)))
36614 error_at (loc, "variable %qD used more than once with "
36615 "%<#pragma acc declare%>", decl);
36616 error = true;
36617 continue;
36620 if (!error)
36622 tree id;
36624 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36625 id = get_identifier ("omp declare target link");
36626 else
36627 id = get_identifier ("omp declare target");
36629 DECL_ATTRIBUTES (decl)
36630 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36631 if (global_bindings_p ())
36633 symtab_node *node = symtab_node::get (decl);
36634 if (node != NULL)
36636 node->offloadable = 1;
36637 if (ENABLE_OFFLOADING)
36639 g->have_offload = true;
36640 if (is_a <varpool_node *> (node))
36641 vec_safe_push (offload_vars, decl);
36648 if (error || global_bindings_p ())
36649 return NULL_TREE;
36651 stmt = make_node (OACC_DECLARE);
36652 TREE_TYPE (stmt) = void_type_node;
36653 OACC_DECLARE_CLAUSES (stmt) = clauses;
36654 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36656 add_stmt (stmt);
36658 return NULL_TREE;
36661 /* OpenACC 2.0:
36662 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36666 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36668 LOC is the location of the #pragma token.
36671 #define OACC_ENTER_DATA_CLAUSE_MASK \
36672 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36680 #define OACC_EXIT_DATA_CLAUSE_MASK \
36681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36687 static tree
36688 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36689 bool enter)
36691 location_t loc = pragma_tok->location;
36692 tree stmt, clauses;
36693 const char *p = "";
36695 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36696 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36698 if (strcmp (p, "data") != 0)
36700 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36701 enter ? "enter" : "exit");
36702 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36703 return NULL_TREE;
36706 cp_lexer_consume_token (parser->lexer);
36708 if (enter)
36709 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36710 "#pragma acc enter data", pragma_tok);
36711 else
36712 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36713 "#pragma acc exit data", pragma_tok);
36715 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36717 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36718 enter ? "enter" : "exit");
36719 return NULL_TREE;
36722 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36723 TREE_TYPE (stmt) = void_type_node;
36724 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36725 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36726 add_stmt (stmt);
36727 return stmt;
36730 /* OpenACC 2.0:
36731 # pragma acc loop oacc-loop-clause[optseq] new-line
36732 structured-block */
36734 #define OACC_LOOP_CLAUSE_MASK \
36735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36746 static tree
36747 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36748 omp_clause_mask mask, tree *cclauses, bool *if_p)
36750 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36752 strcat (p_name, " loop");
36753 mask |= OACC_LOOP_CLAUSE_MASK;
36755 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36756 cclauses == NULL);
36757 if (cclauses)
36759 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36760 if (*cclauses)
36761 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36762 if (clauses)
36763 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36766 tree block = begin_omp_structured_block ();
36767 int save = cp_parser_begin_omp_structured_block (parser);
36768 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36769 cp_parser_end_omp_structured_block (parser, save);
36770 add_stmt (finish_omp_structured_block (block));
36772 return stmt;
36775 /* OpenACC 2.0:
36776 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36777 structured-block
36781 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36782 structured-block
36785 #define OACC_KERNELS_CLAUSE_MASK \
36786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36804 #define OACC_PARALLEL_CLAUSE_MASK \
36805 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36826 static tree
36827 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36828 char *p_name, bool *if_p)
36830 omp_clause_mask mask;
36831 enum tree_code code;
36832 switch (cp_parser_pragma_kind (pragma_tok))
36834 case PRAGMA_OACC_KERNELS:
36835 strcat (p_name, " kernels");
36836 mask = OACC_KERNELS_CLAUSE_MASK;
36837 code = OACC_KERNELS;
36838 break;
36839 case PRAGMA_OACC_PARALLEL:
36840 strcat (p_name, " parallel");
36841 mask = OACC_PARALLEL_CLAUSE_MASK;
36842 code = OACC_PARALLEL;
36843 break;
36844 default:
36845 gcc_unreachable ();
36848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36850 const char *p
36851 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36852 if (strcmp (p, "loop") == 0)
36854 cp_lexer_consume_token (parser->lexer);
36855 tree block = begin_omp_parallel ();
36856 tree clauses;
36857 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36858 if_p);
36859 return finish_omp_construct (code, block, clauses);
36863 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36865 tree block = begin_omp_parallel ();
36866 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36867 cp_parser_statement (parser, NULL_TREE, false, if_p);
36868 cp_parser_end_omp_structured_block (parser, save);
36869 return finish_omp_construct (code, block, clauses);
36872 /* OpenACC 2.0:
36873 # pragma acc update oacc-update-clause[optseq] new-line
36876 #define OACC_UPDATE_CLAUSE_MASK \
36877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36884 static tree
36885 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36887 tree stmt, clauses;
36889 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36890 "#pragma acc update", pragma_tok);
36892 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36894 error_at (pragma_tok->location,
36895 "%<#pragma acc update%> must contain at least one "
36896 "%<device%> or %<host%> or %<self%> clause");
36897 return NULL_TREE;
36900 stmt = make_node (OACC_UPDATE);
36901 TREE_TYPE (stmt) = void_type_node;
36902 OACC_UPDATE_CLAUSES (stmt) = clauses;
36903 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36904 add_stmt (stmt);
36905 return stmt;
36908 /* OpenACC 2.0:
36909 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36911 LOC is the location of the #pragma token.
36914 #define OACC_WAIT_CLAUSE_MASK \
36915 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36917 static tree
36918 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36920 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36921 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36923 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36924 list = cp_parser_oacc_wait_list (parser, loc, list);
36926 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36927 "#pragma acc wait", pragma_tok);
36929 stmt = c_finish_oacc_wait (loc, list, clauses);
36930 stmt = finish_expr_stmt (stmt);
36932 return stmt;
36935 /* OpenMP 4.0:
36936 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36938 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36939 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36946 static void
36947 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36948 enum pragma_context context)
36950 bool first_p = parser->omp_declare_simd == NULL;
36951 cp_omp_declare_simd_data data;
36952 if (first_p)
36954 data.error_seen = false;
36955 data.fndecl_seen = false;
36956 data.tokens = vNULL;
36957 data.clauses = NULL_TREE;
36958 /* It is safe to take the address of a local variable; it will only be
36959 used while this scope is live. */
36960 parser->omp_declare_simd = &data;
36963 /* Store away all pragma tokens. */
36964 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36965 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36966 cp_lexer_consume_token (parser->lexer);
36967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36968 parser->omp_declare_simd->error_seen = true;
36969 cp_parser_require_pragma_eol (parser, pragma_tok);
36970 struct cp_token_cache *cp
36971 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36972 parser->omp_declare_simd->tokens.safe_push (cp);
36974 if (first_p)
36976 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36977 cp_parser_pragma (parser, context, NULL);
36978 switch (context)
36980 case pragma_external:
36981 cp_parser_declaration (parser);
36982 break;
36983 case pragma_member:
36984 cp_parser_member_declaration (parser);
36985 break;
36986 case pragma_objc_icode:
36987 cp_parser_block_declaration (parser, /*statement_p=*/false);
36988 break;
36989 default:
36990 cp_parser_declaration_statement (parser);
36991 break;
36993 if (parser->omp_declare_simd
36994 && !parser->omp_declare_simd->error_seen
36995 && !parser->omp_declare_simd->fndecl_seen)
36996 error_at (pragma_tok->location,
36997 "%<#pragma omp declare simd%> not immediately followed by "
36998 "function declaration or definition");
36999 data.tokens.release ();
37000 parser->omp_declare_simd = NULL;
37004 /* Finalize #pragma omp declare simd clauses after direct declarator has
37005 been parsed, and put that into "omp declare simd" attribute. */
37007 static tree
37008 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37010 struct cp_token_cache *ce;
37011 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37012 int i;
37014 if (!data->error_seen && data->fndecl_seen)
37016 error ("%<#pragma omp declare simd%> not immediately followed by "
37017 "a single function declaration or definition");
37018 data->error_seen = true;
37020 if (data->error_seen)
37021 return attrs;
37023 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37025 tree c, cl;
37027 cp_parser_push_lexer_for_tokens (parser, ce);
37028 parser->lexer->in_pragma = true;
37029 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37030 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37031 cp_lexer_consume_token (parser->lexer);
37032 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37033 "#pragma omp declare simd", pragma_tok);
37034 cp_parser_pop_lexer (parser);
37035 if (cl)
37036 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37037 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37038 TREE_CHAIN (c) = attrs;
37039 if (processing_template_decl)
37040 ATTR_IS_DEPENDENT (c) = 1;
37041 attrs = c;
37044 data->fndecl_seen = true;
37045 return attrs;
37049 /* OpenMP 4.0:
37050 # pragma omp declare target new-line
37051 declarations and definitions
37052 # pragma omp end declare target new-line
37054 OpenMP 4.5:
37055 # pragma omp declare target ( extended-list ) new-line
37057 # pragma omp declare target declare-target-clauses[seq] new-line */
37059 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37063 static void
37064 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37066 tree clauses = NULL_TREE;
37067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37068 clauses
37069 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37070 "#pragma omp declare target", pragma_tok);
37071 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37073 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37074 clauses);
37075 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37076 cp_parser_require_pragma_eol (parser, pragma_tok);
37078 else
37080 cp_parser_require_pragma_eol (parser, pragma_tok);
37081 scope_chain->omp_declare_target_attribute++;
37082 return;
37084 if (scope_chain->omp_declare_target_attribute)
37085 error_at (pragma_tok->location,
37086 "%<#pragma omp declare target%> with clauses in between "
37087 "%<#pragma omp declare target%> without clauses and "
37088 "%<#pragma omp end declare target%>");
37089 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37091 tree t = OMP_CLAUSE_DECL (c), id;
37092 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37093 tree at2 = lookup_attribute ("omp declare target link",
37094 DECL_ATTRIBUTES (t));
37095 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37097 id = get_identifier ("omp declare target link");
37098 std::swap (at1, at2);
37100 else
37101 id = get_identifier ("omp declare target");
37102 if (at2)
37104 error_at (OMP_CLAUSE_LOCATION (c),
37105 "%qD specified both in declare target %<link%> and %<to%>"
37106 " clauses", t);
37107 continue;
37109 if (!at1)
37111 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37112 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37113 continue;
37115 symtab_node *node = symtab_node::get (t);
37116 if (node != NULL)
37118 node->offloadable = 1;
37119 if (ENABLE_OFFLOADING)
37121 g->have_offload = true;
37122 if (is_a <varpool_node *> (node))
37123 vec_safe_push (offload_vars, t);
37130 static void
37131 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37133 const char *p = "";
37134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37137 p = IDENTIFIER_POINTER (id);
37139 if (strcmp (p, "declare") == 0)
37141 cp_lexer_consume_token (parser->lexer);
37142 p = "";
37143 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37145 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37146 p = IDENTIFIER_POINTER (id);
37148 if (strcmp (p, "target") == 0)
37149 cp_lexer_consume_token (parser->lexer);
37150 else
37152 cp_parser_error (parser, "expected %<target%>");
37153 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37154 return;
37157 else
37159 cp_parser_error (parser, "expected %<declare%>");
37160 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37161 return;
37163 cp_parser_require_pragma_eol (parser, pragma_tok);
37164 if (!scope_chain->omp_declare_target_attribute)
37165 error_at (pragma_tok->location,
37166 "%<#pragma omp end declare target%> without corresponding "
37167 "%<#pragma omp declare target%>");
37168 else
37169 scope_chain->omp_declare_target_attribute--;
37172 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37173 expression and optional initializer clause of
37174 #pragma omp declare reduction. We store the expression(s) as
37175 either 3, 6 or 7 special statements inside of the artificial function's
37176 body. The first two statements are DECL_EXPRs for the artificial
37177 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37178 expression that uses those variables.
37179 If there was any INITIALIZER clause, this is followed by further statements,
37180 the fourth and fifth statements are DECL_EXPRs for the artificial
37181 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37182 constructor variant (first token after open paren is not omp_priv),
37183 then the sixth statement is a statement with the function call expression
37184 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37185 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37186 to initialize the OMP_PRIV artificial variable and there is seventh
37187 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37189 static bool
37190 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37192 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37193 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37194 type = TREE_TYPE (type);
37195 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37196 DECL_ARTIFICIAL (omp_out) = 1;
37197 pushdecl (omp_out);
37198 add_decl_expr (omp_out);
37199 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37200 DECL_ARTIFICIAL (omp_in) = 1;
37201 pushdecl (omp_in);
37202 add_decl_expr (omp_in);
37203 tree combiner;
37204 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37206 keep_next_level (true);
37207 tree block = begin_omp_structured_block ();
37208 combiner = cp_parser_expression (parser);
37209 finish_expr_stmt (combiner);
37210 block = finish_omp_structured_block (block);
37211 add_stmt (block);
37213 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37214 return false;
37216 const char *p = "";
37217 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37219 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37220 p = IDENTIFIER_POINTER (id);
37223 if (strcmp (p, "initializer") == 0)
37225 cp_lexer_consume_token (parser->lexer);
37226 matching_parens parens;
37227 if (!parens.require_open (parser))
37228 return false;
37230 p = "";
37231 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37233 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37234 p = IDENTIFIER_POINTER (id);
37237 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37238 DECL_ARTIFICIAL (omp_priv) = 1;
37239 pushdecl (omp_priv);
37240 add_decl_expr (omp_priv);
37241 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37242 DECL_ARTIFICIAL (omp_orig) = 1;
37243 pushdecl (omp_orig);
37244 add_decl_expr (omp_orig);
37246 keep_next_level (true);
37247 block = begin_omp_structured_block ();
37249 bool ctor = false;
37250 if (strcmp (p, "omp_priv") == 0)
37252 bool is_direct_init, is_non_constant_init;
37253 ctor = true;
37254 cp_lexer_consume_token (parser->lexer);
37255 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37256 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37257 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37258 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37259 == CPP_CLOSE_PAREN
37260 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37261 == CPP_CLOSE_PAREN))
37263 finish_omp_structured_block (block);
37264 error ("invalid initializer clause");
37265 return false;
37267 initializer = cp_parser_initializer (parser, &is_direct_init,
37268 &is_non_constant_init);
37269 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37270 NULL_TREE, LOOKUP_ONLYCONVERTING);
37272 else
37274 cp_parser_parse_tentatively (parser);
37275 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37276 /*check_dependency_p=*/true,
37277 /*template_p=*/NULL,
37278 /*declarator_p=*/false,
37279 /*optional_p=*/false);
37280 vec<tree, va_gc> *args;
37281 if (fn_name == error_mark_node
37282 || cp_parser_error_occurred (parser)
37283 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37284 || ((args = cp_parser_parenthesized_expression_list
37285 (parser, non_attr, /*cast_p=*/false,
37286 /*allow_expansion_p=*/true,
37287 /*non_constant_p=*/NULL)),
37288 cp_parser_error_occurred (parser)))
37290 finish_omp_structured_block (block);
37291 cp_parser_abort_tentative_parse (parser);
37292 cp_parser_error (parser, "expected id-expression (arguments)");
37293 return false;
37295 unsigned int i;
37296 tree arg;
37297 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37298 if (arg == omp_priv
37299 || (TREE_CODE (arg) == ADDR_EXPR
37300 && TREE_OPERAND (arg, 0) == omp_priv))
37301 break;
37302 cp_parser_abort_tentative_parse (parser);
37303 if (arg == NULL_TREE)
37304 error ("one of the initializer call arguments should be %<omp_priv%>"
37305 " or %<&omp_priv%>");
37306 initializer = cp_parser_postfix_expression (parser, false, false, false,
37307 false, NULL);
37308 finish_expr_stmt (initializer);
37311 block = finish_omp_structured_block (block);
37312 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37313 add_stmt (block);
37315 if (ctor)
37316 add_decl_expr (omp_orig);
37318 if (!parens.require_close (parser))
37319 return false;
37322 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37323 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37324 UNKNOWN_LOCATION);
37326 return true;
37329 /* OpenMP 4.0
37330 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37331 initializer-clause[opt] new-line
37333 initializer-clause:
37334 initializer (omp_priv initializer)
37335 initializer (function-name (argument-list)) */
37337 static void
37338 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37339 enum pragma_context)
37341 auto_vec<tree> types;
37342 enum tree_code reduc_code = ERROR_MARK;
37343 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37344 unsigned int i;
37345 cp_token *first_token;
37346 cp_token_cache *cp;
37347 int errs;
37348 void *p;
37350 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37351 p = obstack_alloc (&declarator_obstack, 0);
37353 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37354 goto fail;
37356 switch (cp_lexer_peek_token (parser->lexer)->type)
37358 case CPP_PLUS:
37359 reduc_code = PLUS_EXPR;
37360 break;
37361 case CPP_MULT:
37362 reduc_code = MULT_EXPR;
37363 break;
37364 case CPP_MINUS:
37365 reduc_code = MINUS_EXPR;
37366 break;
37367 case CPP_AND:
37368 reduc_code = BIT_AND_EXPR;
37369 break;
37370 case CPP_XOR:
37371 reduc_code = BIT_XOR_EXPR;
37372 break;
37373 case CPP_OR:
37374 reduc_code = BIT_IOR_EXPR;
37375 break;
37376 case CPP_AND_AND:
37377 reduc_code = TRUTH_ANDIF_EXPR;
37378 break;
37379 case CPP_OR_OR:
37380 reduc_code = TRUTH_ORIF_EXPR;
37381 break;
37382 case CPP_NAME:
37383 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37384 break;
37385 default:
37386 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37387 "%<|%>, %<&&%>, %<||%> or identifier");
37388 goto fail;
37391 if (reduc_code != ERROR_MARK)
37392 cp_lexer_consume_token (parser->lexer);
37394 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37395 if (reduc_id == error_mark_node)
37396 goto fail;
37398 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37399 goto fail;
37401 /* Types may not be defined in declare reduction type list. */
37402 const char *saved_message;
37403 saved_message = parser->type_definition_forbidden_message;
37404 parser->type_definition_forbidden_message
37405 = G_("types may not be defined in declare reduction type list");
37406 bool saved_colon_corrects_to_scope_p;
37407 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37408 parser->colon_corrects_to_scope_p = false;
37409 bool saved_colon_doesnt_start_class_def_p;
37410 saved_colon_doesnt_start_class_def_p
37411 = parser->colon_doesnt_start_class_def_p;
37412 parser->colon_doesnt_start_class_def_p = true;
37414 while (true)
37416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37417 type = cp_parser_type_id (parser);
37418 if (type == error_mark_node)
37420 else if (ARITHMETIC_TYPE_P (type)
37421 && (orig_reduc_id == NULL_TREE
37422 || (TREE_CODE (type) != COMPLEX_TYPE
37423 && (id_equal (orig_reduc_id, "min")
37424 || id_equal (orig_reduc_id, "max")))))
37425 error_at (loc, "predeclared arithmetic type %qT in "
37426 "%<#pragma omp declare reduction%>", type);
37427 else if (TREE_CODE (type) == FUNCTION_TYPE
37428 || TREE_CODE (type) == METHOD_TYPE
37429 || TREE_CODE (type) == ARRAY_TYPE)
37430 error_at (loc, "function or array type %qT in "
37431 "%<#pragma omp declare reduction%>", type);
37432 else if (TREE_CODE (type) == REFERENCE_TYPE)
37433 error_at (loc, "reference type %qT in "
37434 "%<#pragma omp declare reduction%>", type);
37435 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37436 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37437 "%<#pragma omp declare reduction%>", type);
37438 else
37439 types.safe_push (type);
37441 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37442 cp_lexer_consume_token (parser->lexer);
37443 else
37444 break;
37447 /* Restore the saved message. */
37448 parser->type_definition_forbidden_message = saved_message;
37449 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37450 parser->colon_doesnt_start_class_def_p
37451 = saved_colon_doesnt_start_class_def_p;
37453 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37454 || types.is_empty ())
37456 fail:
37457 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37458 goto done;
37461 first_token = cp_lexer_peek_token (parser->lexer);
37462 cp = NULL;
37463 errs = errorcount;
37464 FOR_EACH_VEC_ELT (types, i, type)
37466 tree fntype
37467 = build_function_type_list (void_type_node,
37468 cp_build_reference_type (type, false),
37469 NULL_TREE);
37470 tree this_reduc_id = reduc_id;
37471 if (!dependent_type_p (type))
37472 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37473 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37474 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37475 DECL_ARTIFICIAL (fndecl) = 1;
37476 DECL_EXTERNAL (fndecl) = 1;
37477 DECL_DECLARED_INLINE_P (fndecl) = 1;
37478 DECL_IGNORED_P (fndecl) = 1;
37479 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37480 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37481 DECL_ATTRIBUTES (fndecl)
37482 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37483 DECL_ATTRIBUTES (fndecl));
37484 if (processing_template_decl)
37485 fndecl = push_template_decl (fndecl);
37486 bool block_scope = false;
37487 tree block = NULL_TREE;
37488 if (current_function_decl)
37490 block_scope = true;
37491 DECL_CONTEXT (fndecl) = global_namespace;
37492 if (!processing_template_decl)
37493 pushdecl (fndecl);
37495 else if (current_class_type)
37497 if (cp == NULL)
37499 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37500 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37501 cp_lexer_consume_token (parser->lexer);
37502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37503 goto fail;
37504 cp = cp_token_cache_new (first_token,
37505 cp_lexer_peek_nth_token (parser->lexer,
37506 2));
37508 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37509 finish_member_declaration (fndecl);
37510 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37511 DECL_PENDING_INLINE_P (fndecl) = 1;
37512 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37513 continue;
37515 else
37517 DECL_CONTEXT (fndecl) = current_namespace;
37518 pushdecl (fndecl);
37520 if (!block_scope)
37521 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37522 else
37523 block = begin_omp_structured_block ();
37524 if (cp)
37526 cp_parser_push_lexer_for_tokens (parser, cp);
37527 parser->lexer->in_pragma = true;
37529 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37531 if (!block_scope)
37532 finish_function (/*inline_p=*/false);
37533 else
37534 DECL_CONTEXT (fndecl) = current_function_decl;
37535 if (cp)
37536 cp_parser_pop_lexer (parser);
37537 goto fail;
37539 if (cp)
37540 cp_parser_pop_lexer (parser);
37541 if (!block_scope)
37542 finish_function (/*inline_p=*/false);
37543 else
37545 DECL_CONTEXT (fndecl) = current_function_decl;
37546 block = finish_omp_structured_block (block);
37547 if (TREE_CODE (block) == BIND_EXPR)
37548 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37549 else if (TREE_CODE (block) == STATEMENT_LIST)
37550 DECL_SAVED_TREE (fndecl) = block;
37551 if (processing_template_decl)
37552 add_decl_expr (fndecl);
37554 cp_check_omp_declare_reduction (fndecl);
37555 if (cp == NULL && types.length () > 1)
37556 cp = cp_token_cache_new (first_token,
37557 cp_lexer_peek_nth_token (parser->lexer, 2));
37558 if (errs != errorcount)
37559 break;
37562 cp_parser_require_pragma_eol (parser, pragma_tok);
37564 done:
37565 /* Free any declarators allocated. */
37566 obstack_free (&declarator_obstack, p);
37569 /* OpenMP 4.0
37570 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37571 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37572 initializer-clause[opt] new-line
37573 #pragma omp declare target new-line */
37575 static bool
37576 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37577 enum pragma_context context)
37579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37581 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37582 const char *p = IDENTIFIER_POINTER (id);
37584 if (strcmp (p, "simd") == 0)
37586 cp_lexer_consume_token (parser->lexer);
37587 cp_parser_omp_declare_simd (parser, pragma_tok,
37588 context);
37589 return true;
37591 cp_ensure_no_omp_declare_simd (parser);
37592 if (strcmp (p, "reduction") == 0)
37594 cp_lexer_consume_token (parser->lexer);
37595 cp_parser_omp_declare_reduction (parser, pragma_tok,
37596 context);
37597 return false;
37599 if (!flag_openmp) /* flag_openmp_simd */
37601 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37602 return false;
37604 if (strcmp (p, "target") == 0)
37606 cp_lexer_consume_token (parser->lexer);
37607 cp_parser_omp_declare_target (parser, pragma_tok);
37608 return false;
37611 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37612 "or %<target%>");
37613 cp_parser_require_pragma_eol (parser, pragma_tok);
37614 return false;
37617 /* OpenMP 4.5:
37618 #pragma omp taskloop taskloop-clause[optseq] new-line
37619 for-loop
37621 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37622 for-loop */
37624 #define OMP_TASKLOOP_CLAUSE_MASK \
37625 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37640 static tree
37641 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37642 char *p_name, omp_clause_mask mask, tree *cclauses,
37643 bool *if_p)
37645 tree clauses, sb, ret;
37646 unsigned int save;
37647 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37649 strcat (p_name, " taskloop");
37650 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37655 const char *p = IDENTIFIER_POINTER (id);
37657 if (strcmp (p, "simd") == 0)
37659 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37660 if (cclauses == NULL)
37661 cclauses = cclauses_buf;
37663 cp_lexer_consume_token (parser->lexer);
37664 if (!flag_openmp) /* flag_openmp_simd */
37665 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37666 cclauses, if_p);
37667 sb = begin_omp_structured_block ();
37668 save = cp_parser_begin_omp_structured_block (parser);
37669 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37670 cclauses, if_p);
37671 cp_parser_end_omp_structured_block (parser, save);
37672 tree body = finish_omp_structured_block (sb);
37673 if (ret == NULL)
37674 return ret;
37675 ret = make_node (OMP_TASKLOOP);
37676 TREE_TYPE (ret) = void_type_node;
37677 OMP_FOR_BODY (ret) = body;
37678 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37679 SET_EXPR_LOCATION (ret, loc);
37680 add_stmt (ret);
37681 return ret;
37684 if (!flag_openmp) /* flag_openmp_simd */
37686 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37687 return NULL_TREE;
37690 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37691 cclauses == NULL);
37692 if (cclauses)
37694 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37695 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37698 sb = begin_omp_structured_block ();
37699 save = cp_parser_begin_omp_structured_block (parser);
37701 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37702 if_p);
37704 cp_parser_end_omp_structured_block (parser, save);
37705 add_stmt (finish_omp_structured_block (sb));
37707 return ret;
37711 /* OpenACC 2.0:
37712 # pragma acc routine oacc-routine-clause[optseq] new-line
37713 function-definition
37715 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37718 #define OACC_ROUTINE_CLAUSE_MASK \
37719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37725 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37726 component, which must resolve to a declared namespace-scope
37727 function. The clauses are either processed directly (for a named
37728 function), or defered until the immediatley following declaration
37729 is parsed. */
37731 static void
37732 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37733 enum pragma_context context)
37735 gcc_checking_assert (context == pragma_external);
37736 /* The checking for "another pragma following this one" in the "no optional
37737 '( name )'" case makes sure that we dont re-enter. */
37738 gcc_checking_assert (parser->oacc_routine == NULL);
37740 cp_oacc_routine_data data;
37741 data.error_seen = false;
37742 data.fndecl_seen = false;
37743 data.tokens = vNULL;
37744 data.clauses = NULL_TREE;
37745 data.loc = pragma_tok->location;
37746 /* It is safe to take the address of a local variable; it will only be
37747 used while this scope is live. */
37748 parser->oacc_routine = &data;
37750 /* Look for optional '( name )'. */
37751 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37753 matching_parens parens;
37754 parens.consume_open (parser); /* '(' */
37756 /* We parse the name as an id-expression. If it resolves to
37757 anything other than a non-overloaded function at namespace
37758 scope, it's an error. */
37759 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37760 tree name = cp_parser_id_expression (parser,
37761 /*template_keyword_p=*/false,
37762 /*check_dependency_p=*/false,
37763 /*template_p=*/NULL,
37764 /*declarator_p=*/false,
37765 /*optional_p=*/false);
37766 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37767 if (name != error_mark_node && decl == error_mark_node)
37768 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37770 if (decl == error_mark_node
37771 || !parens.require_close (parser))
37773 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37774 parser->oacc_routine = NULL;
37775 return;
37778 data.clauses
37779 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37780 "#pragma acc routine",
37781 cp_lexer_peek_token (parser->lexer));
37783 if (decl && is_overloaded_fn (decl)
37784 && (TREE_CODE (decl) != FUNCTION_DECL
37785 || DECL_FUNCTION_TEMPLATE_P (decl)))
37787 error_at (name_loc,
37788 "%<#pragma acc routine%> names a set of overloads");
37789 parser->oacc_routine = NULL;
37790 return;
37793 /* Perhaps we should use the same rule as declarations in different
37794 namespaces? */
37795 if (!DECL_NAMESPACE_SCOPE_P (decl))
37797 error_at (name_loc,
37798 "%qD does not refer to a namespace scope function", decl);
37799 parser->oacc_routine = NULL;
37800 return;
37803 if (TREE_CODE (decl) != FUNCTION_DECL)
37805 error_at (name_loc, "%qD does not refer to a function", decl);
37806 parser->oacc_routine = NULL;
37807 return;
37810 cp_finalize_oacc_routine (parser, decl, false);
37811 parser->oacc_routine = NULL;
37813 else /* No optional '( name )'. */
37815 /* Store away all pragma tokens. */
37816 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37817 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37818 cp_lexer_consume_token (parser->lexer);
37819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37820 parser->oacc_routine->error_seen = true;
37821 cp_parser_require_pragma_eol (parser, pragma_tok);
37822 struct cp_token_cache *cp
37823 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37824 parser->oacc_routine->tokens.safe_push (cp);
37826 /* Emit a helpful diagnostic if there's another pragma following this
37827 one. */
37828 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37830 cp_ensure_no_oacc_routine (parser);
37831 data.tokens.release ();
37832 /* ..., and then just keep going. */
37833 return;
37836 /* We only have to consider the pragma_external case here. */
37837 cp_parser_declaration (parser);
37838 if (parser->oacc_routine
37839 && !parser->oacc_routine->fndecl_seen)
37840 cp_ensure_no_oacc_routine (parser);
37841 else
37842 parser->oacc_routine = NULL;
37843 data.tokens.release ();
37847 /* Finalize #pragma acc routine clauses after direct declarator has
37848 been parsed. */
37850 static tree
37851 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37853 struct cp_token_cache *ce;
37854 cp_oacc_routine_data *data = parser->oacc_routine;
37856 if (!data->error_seen && data->fndecl_seen)
37858 error_at (data->loc,
37859 "%<#pragma acc routine%> not immediately followed by "
37860 "a single function declaration or definition");
37861 data->error_seen = true;
37863 if (data->error_seen)
37864 return attrs;
37866 gcc_checking_assert (data->tokens.length () == 1);
37867 ce = data->tokens[0];
37869 cp_parser_push_lexer_for_tokens (parser, ce);
37870 parser->lexer->in_pragma = true;
37871 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37873 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37874 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37875 parser->oacc_routine->clauses
37876 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37877 "#pragma acc routine", pragma_tok);
37878 cp_parser_pop_lexer (parser);
37879 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37880 fndecl_seen. */
37882 return attrs;
37885 /* Apply any saved OpenACC routine clauses to a just-parsed
37886 declaration. */
37888 static void
37889 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37891 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37893 /* Keep going if we're in error reporting mode. */
37894 if (parser->oacc_routine->error_seen
37895 || fndecl == error_mark_node)
37896 return;
37898 if (parser->oacc_routine->fndecl_seen)
37900 error_at (parser->oacc_routine->loc,
37901 "%<#pragma acc routine%> not immediately followed by"
37902 " a single function declaration or definition");
37903 parser->oacc_routine = NULL;
37904 return;
37906 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37908 cp_ensure_no_oacc_routine (parser);
37909 return;
37912 if (oacc_get_fn_attrib (fndecl))
37914 error_at (parser->oacc_routine->loc,
37915 "%<#pragma acc routine%> already applied to %qD", fndecl);
37916 parser->oacc_routine = NULL;
37917 return;
37920 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37922 error_at (parser->oacc_routine->loc,
37923 TREE_USED (fndecl)
37924 ? G_("%<#pragma acc routine%> must be applied before use")
37925 : G_("%<#pragma acc routine%> must be applied before "
37926 "definition"));
37927 parser->oacc_routine = NULL;
37928 return;
37931 /* Process the routine's dimension clauses. */
37932 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37933 oacc_replace_fn_attrib (fndecl, dims);
37935 /* Add an "omp declare target" attribute. */
37936 DECL_ATTRIBUTES (fndecl)
37937 = tree_cons (get_identifier ("omp declare target"),
37938 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37940 /* Don't unset parser->oacc_routine here: we may still need it to
37941 diagnose wrong usage. But, remember that we've used this "#pragma acc
37942 routine". */
37943 parser->oacc_routine->fndecl_seen = true;
37947 /* Main entry point to OpenMP statement pragmas. */
37949 static void
37950 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37952 tree stmt;
37953 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37954 omp_clause_mask mask (0);
37956 switch (cp_parser_pragma_kind (pragma_tok))
37958 case PRAGMA_OACC_ATOMIC:
37959 cp_parser_omp_atomic (parser, pragma_tok);
37960 return;
37961 case PRAGMA_OACC_CACHE:
37962 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37963 break;
37964 case PRAGMA_OACC_DATA:
37965 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37966 break;
37967 case PRAGMA_OACC_ENTER_DATA:
37968 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37969 break;
37970 case PRAGMA_OACC_EXIT_DATA:
37971 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37972 break;
37973 case PRAGMA_OACC_HOST_DATA:
37974 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37975 break;
37976 case PRAGMA_OACC_KERNELS:
37977 case PRAGMA_OACC_PARALLEL:
37978 strcpy (p_name, "#pragma acc");
37979 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37980 if_p);
37981 break;
37982 case PRAGMA_OACC_LOOP:
37983 strcpy (p_name, "#pragma acc");
37984 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37985 if_p);
37986 break;
37987 case PRAGMA_OACC_UPDATE:
37988 stmt = cp_parser_oacc_update (parser, pragma_tok);
37989 break;
37990 case PRAGMA_OACC_WAIT:
37991 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37992 break;
37993 case PRAGMA_OMP_ATOMIC:
37994 cp_parser_omp_atomic (parser, pragma_tok);
37995 return;
37996 case PRAGMA_OMP_CRITICAL:
37997 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37998 break;
37999 case PRAGMA_OMP_DISTRIBUTE:
38000 strcpy (p_name, "#pragma omp");
38001 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38002 if_p);
38003 break;
38004 case PRAGMA_OMP_FOR:
38005 strcpy (p_name, "#pragma omp");
38006 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38007 if_p);
38008 break;
38009 case PRAGMA_OMP_MASTER:
38010 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38011 break;
38012 case PRAGMA_OMP_PARALLEL:
38013 strcpy (p_name, "#pragma omp");
38014 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38015 if_p);
38016 break;
38017 case PRAGMA_OMP_SECTIONS:
38018 strcpy (p_name, "#pragma omp");
38019 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38020 break;
38021 case PRAGMA_OMP_SIMD:
38022 strcpy (p_name, "#pragma omp");
38023 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38024 if_p);
38025 break;
38026 case PRAGMA_OMP_SINGLE:
38027 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38028 break;
38029 case PRAGMA_OMP_TASK:
38030 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38031 break;
38032 case PRAGMA_OMP_TASKGROUP:
38033 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38034 break;
38035 case PRAGMA_OMP_TASKLOOP:
38036 strcpy (p_name, "#pragma omp");
38037 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38038 if_p);
38039 break;
38040 case PRAGMA_OMP_TEAMS:
38041 strcpy (p_name, "#pragma omp");
38042 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38043 if_p);
38044 break;
38045 default:
38046 gcc_unreachable ();
38049 protected_set_expr_location (stmt, pragma_tok->location);
38052 /* Transactional Memory parsing routines. */
38054 /* Parse a transaction attribute.
38056 txn-attribute:
38057 attribute
38058 [ [ identifier ] ]
38060 We use this instead of cp_parser_attributes_opt for transactions to avoid
38061 the pedwarn in C++98 mode. */
38063 static tree
38064 cp_parser_txn_attribute_opt (cp_parser *parser)
38066 cp_token *token;
38067 tree attr_name, attr = NULL;
38069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38070 return cp_parser_attributes_opt (parser);
38072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38073 return NULL_TREE;
38074 cp_lexer_consume_token (parser->lexer);
38075 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38076 goto error1;
38078 token = cp_lexer_peek_token (parser->lexer);
38079 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38081 token = cp_lexer_consume_token (parser->lexer);
38083 attr_name = (token->type == CPP_KEYWORD
38084 /* For keywords, use the canonical spelling,
38085 not the parsed identifier. */
38086 ? ridpointers[(int) token->keyword]
38087 : token->u.value);
38088 attr = build_tree_list (attr_name, NULL_TREE);
38090 else
38091 cp_parser_error (parser, "expected identifier");
38093 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38094 error1:
38095 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38096 return attr;
38099 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38101 transaction-statement:
38102 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38103 compound-statement
38104 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38107 static tree
38108 cp_parser_transaction (cp_parser *parser, cp_token *token)
38110 unsigned char old_in = parser->in_transaction;
38111 unsigned char this_in = 1, new_in;
38112 enum rid keyword = token->keyword;
38113 tree stmt, attrs, noex;
38115 cp_lexer_consume_token (parser->lexer);
38117 if (keyword == RID_TRANSACTION_RELAXED
38118 || keyword == RID_SYNCHRONIZED)
38119 this_in |= TM_STMT_ATTR_RELAXED;
38120 else
38122 attrs = cp_parser_txn_attribute_opt (parser);
38123 if (attrs)
38124 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38127 /* Parse a noexcept specification. */
38128 if (keyword == RID_ATOMIC_NOEXCEPT)
38129 noex = boolean_true_node;
38130 else if (keyword == RID_ATOMIC_CANCEL)
38132 /* cancel-and-throw is unimplemented. */
38133 sorry ("atomic_cancel");
38134 noex = NULL_TREE;
38136 else
38137 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38139 /* Keep track if we're in the lexical scope of an outer transaction. */
38140 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38142 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38144 parser->in_transaction = new_in;
38145 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38146 parser->in_transaction = old_in;
38148 finish_transaction_stmt (stmt, NULL, this_in, noex);
38150 return stmt;
38153 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38155 transaction-expression:
38156 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38157 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38160 static tree
38161 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38163 unsigned char old_in = parser->in_transaction;
38164 unsigned char this_in = 1;
38165 cp_token *token;
38166 tree expr, noex;
38167 bool noex_expr;
38168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38170 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38171 || keyword == RID_TRANSACTION_RELAXED);
38173 if (!flag_tm)
38174 error_at (loc,
38175 keyword == RID_TRANSACTION_RELAXED
38176 ? G_("%<__transaction_relaxed%> without transactional memory "
38177 "support enabled")
38178 : G_("%<__transaction_atomic%> without transactional memory "
38179 "support enabled"));
38181 token = cp_parser_require_keyword (parser, keyword,
38182 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38183 : RT_TRANSACTION_RELAXED));
38184 gcc_assert (token != NULL);
38186 if (keyword == RID_TRANSACTION_RELAXED)
38187 this_in |= TM_STMT_ATTR_RELAXED;
38189 /* Set this early. This might mean that we allow transaction_cancel in
38190 an expression that we find out later actually has to be a constexpr.
38191 However, we expect that cxx_constant_value will be able to deal with
38192 this; also, if the noexcept has no constexpr, then what we parse next
38193 really is a transaction's body. */
38194 parser->in_transaction = this_in;
38196 /* Parse a noexcept specification. */
38197 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38198 true);
38200 if (!noex || !noex_expr
38201 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38203 matching_parens parens;
38204 parens.require_open (parser);
38206 expr = cp_parser_expression (parser);
38207 expr = finish_parenthesized_expr (expr);
38209 parens.require_close (parser);
38211 else
38213 /* The only expression that is available got parsed for the noexcept
38214 already. noexcept is true then. */
38215 expr = noex;
38216 noex = boolean_true_node;
38219 expr = build_transaction_expr (token->location, expr, this_in, noex);
38220 parser->in_transaction = old_in;
38222 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38223 return error_mark_node;
38225 return (flag_tm ? expr : error_mark_node);
38228 /* Parse a function-transaction-block.
38230 function-transaction-block:
38231 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38232 function-body
38233 __transaction_atomic txn-attribute[opt] function-try-block
38234 __transaction_relaxed ctor-initializer[opt] function-body
38235 __transaction_relaxed function-try-block
38238 static void
38239 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38241 unsigned char old_in = parser->in_transaction;
38242 unsigned char new_in = 1;
38243 tree compound_stmt, stmt, attrs;
38244 cp_token *token;
38246 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38247 || keyword == RID_TRANSACTION_RELAXED);
38248 token = cp_parser_require_keyword (parser, keyword,
38249 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38250 : RT_TRANSACTION_RELAXED));
38251 gcc_assert (token != NULL);
38253 if (keyword == RID_TRANSACTION_RELAXED)
38254 new_in |= TM_STMT_ATTR_RELAXED;
38255 else
38257 attrs = cp_parser_txn_attribute_opt (parser);
38258 if (attrs)
38259 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38262 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38264 parser->in_transaction = new_in;
38266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38267 cp_parser_function_try_block (parser);
38268 else
38269 cp_parser_ctor_initializer_opt_and_function_body
38270 (parser, /*in_function_try_block=*/false);
38272 parser->in_transaction = old_in;
38274 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38277 /* Parse a __transaction_cancel statement.
38279 cancel-statement:
38280 __transaction_cancel txn-attribute[opt] ;
38281 __transaction_cancel txn-attribute[opt] throw-expression ;
38283 ??? Cancel and throw is not yet implemented. */
38285 static tree
38286 cp_parser_transaction_cancel (cp_parser *parser)
38288 cp_token *token;
38289 bool is_outer = false;
38290 tree stmt, attrs;
38292 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38293 RT_TRANSACTION_CANCEL);
38294 gcc_assert (token != NULL);
38296 attrs = cp_parser_txn_attribute_opt (parser);
38297 if (attrs)
38298 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38300 /* ??? Parse cancel-and-throw here. */
38302 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38304 if (!flag_tm)
38306 error_at (token->location, "%<__transaction_cancel%> without "
38307 "transactional memory support enabled");
38308 return error_mark_node;
38310 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38312 error_at (token->location, "%<__transaction_cancel%> within a "
38313 "%<__transaction_relaxed%>");
38314 return error_mark_node;
38316 else if (is_outer)
38318 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38319 && !is_tm_may_cancel_outer (current_function_decl))
38321 error_at (token->location, "outer %<__transaction_cancel%> not "
38322 "within outer %<__transaction_atomic%>");
38323 error_at (token->location,
38324 " or a %<transaction_may_cancel_outer%> function");
38325 return error_mark_node;
38328 else if (parser->in_transaction == 0)
38330 error_at (token->location, "%<__transaction_cancel%> not within "
38331 "%<__transaction_atomic%>");
38332 return error_mark_node;
38335 stmt = build_tm_abort_call (token->location, is_outer);
38336 add_stmt (stmt);
38338 return stmt;
38341 /* The parser. */
38343 static GTY (()) cp_parser *the_parser;
38346 /* Special handling for the first token or line in the file. The first
38347 thing in the file might be #pragma GCC pch_preprocess, which loads a
38348 PCH file, which is a GC collection point. So we need to handle this
38349 first pragma without benefit of an existing lexer structure.
38351 Always returns one token to the caller in *FIRST_TOKEN. This is
38352 either the true first token of the file, or the first token after
38353 the initial pragma. */
38355 static void
38356 cp_parser_initial_pragma (cp_token *first_token)
38358 tree name = NULL;
38360 cp_lexer_get_preprocessor_token (NULL, first_token);
38361 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38362 return;
38364 cp_lexer_get_preprocessor_token (NULL, first_token);
38365 if (first_token->type == CPP_STRING)
38367 name = first_token->u.value;
38369 cp_lexer_get_preprocessor_token (NULL, first_token);
38370 if (first_token->type != CPP_PRAGMA_EOL)
38371 error_at (first_token->location,
38372 "junk at end of %<#pragma GCC pch_preprocess%>");
38374 else
38375 error_at (first_token->location, "expected string literal");
38377 /* Skip to the end of the pragma. */
38378 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38379 cp_lexer_get_preprocessor_token (NULL, first_token);
38381 /* Now actually load the PCH file. */
38382 if (name)
38383 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38385 /* Read one more token to return to our caller. We have to do this
38386 after reading the PCH file in, since its pointers have to be
38387 live. */
38388 cp_lexer_get_preprocessor_token (NULL, first_token);
38391 /* Parse a pragma GCC ivdep. */
38393 static bool
38394 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38396 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38397 return true;
38400 /* Parse a pragma GCC unroll. */
38402 static unsigned short
38403 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38405 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38406 tree expr = cp_parser_constant_expression (parser);
38407 unsigned short unroll;
38408 expr = maybe_constant_value (expr);
38409 HOST_WIDE_INT lunroll = 0;
38410 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38411 || TREE_CODE (expr) != INTEGER_CST
38412 || (lunroll = tree_to_shwi (expr)) < 0
38413 || lunroll >= USHRT_MAX)
38415 error_at (location, "%<#pragma GCC unroll%> requires an"
38416 " assignment-expression that evaluates to a non-negative"
38417 " integral constant less than %u", USHRT_MAX);
38418 unroll = 0;
38420 else
38422 unroll = (unsigned short)lunroll;
38423 if (unroll == 0)
38424 unroll = 1;
38426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38427 return unroll;
38430 /* Normal parsing of a pragma token. Here we can (and must) use the
38431 regular lexer. */
38433 static bool
38434 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38436 cp_token *pragma_tok;
38437 unsigned int id;
38438 tree stmt;
38439 bool ret;
38441 pragma_tok = cp_lexer_consume_token (parser->lexer);
38442 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38443 parser->lexer->in_pragma = true;
38445 id = cp_parser_pragma_kind (pragma_tok);
38446 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38447 cp_ensure_no_omp_declare_simd (parser);
38448 switch (id)
38450 case PRAGMA_GCC_PCH_PREPROCESS:
38451 error_at (pragma_tok->location,
38452 "%<#pragma GCC pch_preprocess%> must be first");
38453 break;
38455 case PRAGMA_OMP_BARRIER:
38456 switch (context)
38458 case pragma_compound:
38459 cp_parser_omp_barrier (parser, pragma_tok);
38460 return false;
38461 case pragma_stmt:
38462 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38463 "used in compound statements", "omp barrier");
38464 break;
38465 default:
38466 goto bad_stmt;
38468 break;
38470 case PRAGMA_OMP_FLUSH:
38471 switch (context)
38473 case pragma_compound:
38474 cp_parser_omp_flush (parser, pragma_tok);
38475 return false;
38476 case pragma_stmt:
38477 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38478 "used in compound statements", "omp flush");
38479 break;
38480 default:
38481 goto bad_stmt;
38483 break;
38485 case PRAGMA_OMP_TASKWAIT:
38486 switch (context)
38488 case pragma_compound:
38489 cp_parser_omp_taskwait (parser, pragma_tok);
38490 return false;
38491 case pragma_stmt:
38492 error_at (pragma_tok->location,
38493 "%<#pragma %s%> may only be used in compound statements",
38494 "omp taskwait");
38495 break;
38496 default:
38497 goto bad_stmt;
38499 break;
38501 case PRAGMA_OMP_TASKYIELD:
38502 switch (context)
38504 case pragma_compound:
38505 cp_parser_omp_taskyield (parser, pragma_tok);
38506 return false;
38507 case pragma_stmt:
38508 error_at (pragma_tok->location,
38509 "%<#pragma %s%> may only be used in compound statements",
38510 "omp taskyield");
38511 break;
38512 default:
38513 goto bad_stmt;
38515 break;
38517 case PRAGMA_OMP_CANCEL:
38518 switch (context)
38520 case pragma_compound:
38521 cp_parser_omp_cancel (parser, pragma_tok);
38522 return false;
38523 case pragma_stmt:
38524 error_at (pragma_tok->location,
38525 "%<#pragma %s%> may only be used in compound statements",
38526 "omp cancel");
38527 break;
38528 default:
38529 goto bad_stmt;
38531 break;
38533 case PRAGMA_OMP_CANCELLATION_POINT:
38534 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38535 return false;
38537 case PRAGMA_OMP_THREADPRIVATE:
38538 cp_parser_omp_threadprivate (parser, pragma_tok);
38539 return false;
38541 case PRAGMA_OMP_DECLARE:
38542 return cp_parser_omp_declare (parser, pragma_tok, context);
38544 case PRAGMA_OACC_DECLARE:
38545 cp_parser_oacc_declare (parser, pragma_tok);
38546 return false;
38548 case PRAGMA_OACC_ENTER_DATA:
38549 if (context == pragma_stmt)
38551 error_at (pragma_tok->location,
38552 "%<#pragma %s%> may only be used in compound statements",
38553 "acc enter data");
38554 break;
38556 else if (context != pragma_compound)
38557 goto bad_stmt;
38558 cp_parser_omp_construct (parser, pragma_tok, if_p);
38559 return true;
38561 case PRAGMA_OACC_EXIT_DATA:
38562 if (context == pragma_stmt)
38564 error_at (pragma_tok->location,
38565 "%<#pragma %s%> may only be used in compound statements",
38566 "acc exit data");
38567 break;
38569 else if (context != pragma_compound)
38570 goto bad_stmt;
38571 cp_parser_omp_construct (parser, pragma_tok, if_p);
38572 return true;
38574 case PRAGMA_OACC_ROUTINE:
38575 if (context != pragma_external)
38577 error_at (pragma_tok->location,
38578 "%<#pragma acc routine%> must be at file scope");
38579 break;
38581 cp_parser_oacc_routine (parser, pragma_tok, context);
38582 return false;
38584 case PRAGMA_OACC_UPDATE:
38585 if (context == pragma_stmt)
38587 error_at (pragma_tok->location,
38588 "%<#pragma %s%> may only be used in compound statements",
38589 "acc update");
38590 break;
38592 else if (context != pragma_compound)
38593 goto bad_stmt;
38594 cp_parser_omp_construct (parser, pragma_tok, if_p);
38595 return true;
38597 case PRAGMA_OACC_WAIT:
38598 if (context == pragma_stmt)
38600 error_at (pragma_tok->location,
38601 "%<#pragma %s%> may only be used in compound statements",
38602 "acc wait");
38603 break;
38605 else if (context != pragma_compound)
38606 goto bad_stmt;
38607 cp_parser_omp_construct (parser, pragma_tok, if_p);
38608 return true;
38610 case PRAGMA_OACC_ATOMIC:
38611 case PRAGMA_OACC_CACHE:
38612 case PRAGMA_OACC_DATA:
38613 case PRAGMA_OACC_HOST_DATA:
38614 case PRAGMA_OACC_KERNELS:
38615 case PRAGMA_OACC_PARALLEL:
38616 case PRAGMA_OACC_LOOP:
38617 case PRAGMA_OMP_ATOMIC:
38618 case PRAGMA_OMP_CRITICAL:
38619 case PRAGMA_OMP_DISTRIBUTE:
38620 case PRAGMA_OMP_FOR:
38621 case PRAGMA_OMP_MASTER:
38622 case PRAGMA_OMP_PARALLEL:
38623 case PRAGMA_OMP_SECTIONS:
38624 case PRAGMA_OMP_SIMD:
38625 case PRAGMA_OMP_SINGLE:
38626 case PRAGMA_OMP_TASK:
38627 case PRAGMA_OMP_TASKGROUP:
38628 case PRAGMA_OMP_TASKLOOP:
38629 case PRAGMA_OMP_TEAMS:
38630 if (context != pragma_stmt && context != pragma_compound)
38631 goto bad_stmt;
38632 stmt = push_omp_privatization_clauses (false);
38633 cp_parser_omp_construct (parser, pragma_tok, if_p);
38634 pop_omp_privatization_clauses (stmt);
38635 return true;
38637 case PRAGMA_OMP_ORDERED:
38638 if (context != pragma_stmt && context != pragma_compound)
38639 goto bad_stmt;
38640 stmt = push_omp_privatization_clauses (false);
38641 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38642 pop_omp_privatization_clauses (stmt);
38643 return ret;
38645 case PRAGMA_OMP_TARGET:
38646 if (context != pragma_stmt && context != pragma_compound)
38647 goto bad_stmt;
38648 stmt = push_omp_privatization_clauses (false);
38649 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38650 pop_omp_privatization_clauses (stmt);
38651 return ret;
38653 case PRAGMA_OMP_END_DECLARE_TARGET:
38654 cp_parser_omp_end_declare_target (parser, pragma_tok);
38655 return false;
38657 case PRAGMA_OMP_SECTION:
38658 error_at (pragma_tok->location,
38659 "%<#pragma omp section%> may only be used in "
38660 "%<#pragma omp sections%> construct");
38661 break;
38663 case PRAGMA_IVDEP:
38665 if (context == pragma_external)
38667 error_at (pragma_tok->location,
38668 "%<#pragma GCC ivdep%> must be inside a function");
38669 break;
38671 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38672 unsigned short unroll;
38673 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38674 if (tok->type == CPP_PRAGMA
38675 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38677 tok = cp_lexer_consume_token (parser->lexer);
38678 unroll = cp_parser_pragma_unroll (parser, tok);
38679 tok = cp_lexer_peek_token (the_parser->lexer);
38681 else
38682 unroll = 0;
38683 if (tok->type != CPP_KEYWORD
38684 || (tok->keyword != RID_FOR
38685 && tok->keyword != RID_WHILE
38686 && tok->keyword != RID_DO))
38688 cp_parser_error (parser, "for, while or do statement expected");
38689 return false;
38691 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38692 return true;
38695 case PRAGMA_UNROLL:
38697 if (context == pragma_external)
38699 error_at (pragma_tok->location,
38700 "%<#pragma GCC unroll%> must be inside a function");
38701 break;
38703 const unsigned short unroll
38704 = cp_parser_pragma_unroll (parser, pragma_tok);
38705 bool ivdep;
38706 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38707 if (tok->type == CPP_PRAGMA
38708 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38710 tok = cp_lexer_consume_token (parser->lexer);
38711 ivdep = cp_parser_pragma_ivdep (parser, tok);
38712 tok = cp_lexer_peek_token (the_parser->lexer);
38714 else
38715 ivdep = false;
38716 if (tok->type != CPP_KEYWORD
38717 || (tok->keyword != RID_FOR
38718 && tok->keyword != RID_WHILE
38719 && tok->keyword != RID_DO))
38721 cp_parser_error (parser, "for, while or do statement expected");
38722 return false;
38724 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38725 return true;
38728 default:
38729 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38730 c_invoke_pragma_handler (id);
38731 break;
38733 bad_stmt:
38734 cp_parser_error (parser, "expected declaration specifiers");
38735 break;
38738 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38739 return false;
38742 /* The interface the pragma parsers have to the lexer. */
38744 enum cpp_ttype
38745 pragma_lex (tree *value, location_t *loc)
38747 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38748 enum cpp_ttype ret = tok->type;
38750 *value = tok->u.value;
38751 if (loc)
38752 *loc = tok->location;
38754 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38755 ret = CPP_EOF;
38756 else if (ret == CPP_STRING)
38757 *value = cp_parser_string_literal (the_parser, false, false);
38758 else
38760 if (ret == CPP_KEYWORD)
38761 ret = CPP_NAME;
38762 cp_lexer_consume_token (the_parser->lexer);
38765 return ret;
38769 /* External interface. */
38771 /* Parse one entire translation unit. */
38773 void
38774 c_parse_file (void)
38776 static bool already_called = false;
38778 if (already_called)
38779 fatal_error (input_location,
38780 "inter-module optimizations not implemented for C++");
38781 already_called = true;
38783 the_parser = cp_parser_new ();
38784 push_deferring_access_checks (flag_access_control
38785 ? dk_no_deferred : dk_no_check);
38786 cp_parser_translation_unit (the_parser);
38787 the_parser = NULL;
38790 /* Create an identifier for a generic parameter type (a synthesized
38791 template parameter implied by `auto' or a concept identifier). */
38793 static GTY(()) int generic_parm_count;
38794 static tree
38795 make_generic_type_name ()
38797 char buf[32];
38798 sprintf (buf, "auto:%d", ++generic_parm_count);
38799 return get_identifier (buf);
38802 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38803 (creating a new template parameter list if necessary). Returns the newly
38804 created template type parm. */
38806 static tree
38807 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38809 gcc_assert (current_binding_level->kind == sk_function_parms);
38811 /* Before committing to modifying any scope, if we're in an
38812 implicit template scope, and we're trying to synthesize a
38813 constrained parameter, try to find a previous parameter with
38814 the same name. This is the same-type rule for abbreviated
38815 function templates.
38817 NOTE: We can generate implicit parameters when tentatively
38818 parsing a nested name specifier, only to reject that parse
38819 later. However, matching the same template-id as part of a
38820 direct-declarator should generate an identical template
38821 parameter, so this rule will merge them. */
38822 if (parser->implicit_template_scope && constr)
38824 tree t = parser->implicit_template_parms;
38825 while (t)
38827 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38829 tree d = TREE_VALUE (t);
38830 if (TREE_CODE (d) == PARM_DECL)
38831 /* Return the TEMPLATE_PARM_INDEX. */
38832 d = DECL_INITIAL (d);
38833 return d;
38835 t = TREE_CHAIN (t);
38839 /* We are either continuing a function template that already contains implicit
38840 template parameters, creating a new fully-implicit function template, or
38841 extending an existing explicit function template with implicit template
38842 parameters. */
38844 cp_binding_level *const entry_scope = current_binding_level;
38846 bool become_template = false;
38847 cp_binding_level *parent_scope = 0;
38849 if (parser->implicit_template_scope)
38851 gcc_assert (parser->implicit_template_parms);
38853 current_binding_level = parser->implicit_template_scope;
38855 else
38857 /* Roll back to the existing template parameter scope (in the case of
38858 extending an explicit function template) or introduce a new template
38859 parameter scope ahead of the function parameter scope (or class scope
38860 in the case of out-of-line member definitions). The function scope is
38861 added back after template parameter synthesis below. */
38863 cp_binding_level *scope = entry_scope;
38865 while (scope->kind == sk_function_parms)
38867 parent_scope = scope;
38868 scope = scope->level_chain;
38870 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38872 /* If not defining a class, then any class scope is a scope level in
38873 an out-of-line member definition. In this case simply wind back
38874 beyond the first such scope to inject the template parameter list.
38875 Otherwise wind back to the class being defined. The latter can
38876 occur in class member friend declarations such as:
38878 class A {
38879 void foo (auto);
38881 class B {
38882 friend void A::foo (auto);
38885 The template parameter list synthesized for the friend declaration
38886 must be injected in the scope of 'B'. This can also occur in
38887 erroneous cases such as:
38889 struct A {
38890 struct B {
38891 void foo (auto);
38893 void B::foo (auto) {}
38896 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38897 but, nevertheless, the template parameter list synthesized for the
38898 declarator should be injected into the scope of 'A' as if the
38899 ill-formed template was specified explicitly. */
38901 while (scope->kind == sk_class && !scope->defining_class_p)
38903 parent_scope = scope;
38904 scope = scope->level_chain;
38908 current_binding_level = scope;
38910 if (scope->kind != sk_template_parms
38911 || !function_being_declared_is_template_p (parser))
38913 /* Introduce a new template parameter list for implicit template
38914 parameters. */
38916 become_template = true;
38918 parser->implicit_template_scope
38919 = begin_scope (sk_template_parms, NULL);
38921 ++processing_template_decl;
38923 parser->fully_implicit_function_template_p = true;
38924 ++parser->num_template_parameter_lists;
38926 else
38928 /* Synthesize implicit template parameters at the end of the explicit
38929 template parameter list. */
38931 gcc_assert (current_template_parms);
38933 parser->implicit_template_scope = scope;
38935 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38936 parser->implicit_template_parms
38937 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38941 /* Synthesize a new template parameter and track the current template
38942 parameter chain with implicit_template_parms. */
38944 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38945 tree synth_id = make_generic_type_name ();
38946 tree synth_tmpl_parm;
38947 bool non_type = false;
38949 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38950 synth_tmpl_parm
38951 = finish_template_type_parm (class_type_node, synth_id);
38952 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38953 synth_tmpl_parm
38954 = finish_constrained_template_template_parm (proto, synth_id);
38955 else
38957 synth_tmpl_parm = copy_decl (proto);
38958 DECL_NAME (synth_tmpl_parm) = synth_id;
38959 non_type = true;
38962 // Attach the constraint to the parm before processing.
38963 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38964 TREE_TYPE (node) = constr;
38965 tree new_parm
38966 = process_template_parm (parser->implicit_template_parms,
38967 input_location,
38968 node,
38969 /*non_type=*/non_type,
38970 /*param_pack=*/false);
38972 // Chain the new parameter to the list of implicit parameters.
38973 if (parser->implicit_template_parms)
38974 parser->implicit_template_parms
38975 = TREE_CHAIN (parser->implicit_template_parms);
38976 else
38977 parser->implicit_template_parms = new_parm;
38979 tree new_decl = get_local_decls ();
38980 if (non_type)
38981 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38982 new_decl = DECL_INITIAL (new_decl);
38984 /* If creating a fully implicit function template, start the new implicit
38985 template parameter list with this synthesized type, otherwise grow the
38986 current template parameter list. */
38988 if (become_template)
38990 parent_scope->level_chain = current_binding_level;
38992 tree new_parms = make_tree_vec (1);
38993 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38994 current_template_parms = tree_cons (size_int (processing_template_decl),
38995 new_parms, current_template_parms);
38997 else
38999 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39000 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39001 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39002 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39005 // If the new parameter was constrained, we need to add that to the
39006 // constraints in the template parameter list.
39007 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39009 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39010 reqs = conjoin_constraints (reqs, req);
39011 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39014 current_binding_level = entry_scope;
39016 return new_decl;
39019 /* Finish the declaration of a fully implicit function template. Such a
39020 template has no explicit template parameter list so has not been through the
39021 normal template head and tail processing. synthesize_implicit_template_parm
39022 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39023 provided if the declaration is a class member such that its template
39024 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39025 form is returned. Otherwise NULL_TREE is returned. */
39027 static tree
39028 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39030 gcc_assert (parser->fully_implicit_function_template_p);
39032 if (member_decl_opt && member_decl_opt != error_mark_node
39033 && DECL_VIRTUAL_P (member_decl_opt))
39035 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39036 "implicit templates may not be %<virtual%>");
39037 DECL_VIRTUAL_P (member_decl_opt) = false;
39040 if (member_decl_opt)
39041 member_decl_opt = finish_member_template_decl (member_decl_opt);
39042 end_template_decl ();
39044 parser->fully_implicit_function_template_p = false;
39045 --parser->num_template_parameter_lists;
39047 return member_decl_opt;
39050 /* Helper function for diagnostics that have complained about things
39051 being used with 'extern "C"' linkage.
39053 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39055 void
39056 maybe_show_extern_c_location (void)
39058 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39059 inform (the_parser->innermost_linkage_specification_location,
39060 "%<extern \"C\"%> linkage started here");
39063 #include "gt-cp-parser.h"